When UpdateDT is set, DT is invalid, which could cause problems when trying to
[oota-llvm.git] / lib / Transforms / Scalar / CodeGenPrepare.cpp
1 //===- CodeGenPrepare.cpp - Prepare a function for code generation --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass munges the code in the input function to better prepare it for
11 // SelectionDAG-based code generation. This works around limitations in it's
12 // basic-block-at-a-time approach. It should eventually be removed.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #define DEBUG_TYPE "codegenprepare"
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Function.h"
21 #include "llvm/InlineAsm.h"
22 #include "llvm/Instructions.h"
23 #include "llvm/IntrinsicInst.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Analysis/Dominators.h"
26 #include "llvm/Analysis/InstructionSimplify.h"
27 #include "llvm/Analysis/ProfileInfo.h"
28 #include "llvm/Target/TargetData.h"
29 #include "llvm/Target/TargetLowering.h"
30 #include "llvm/Transforms/Utils/AddrModeMatcher.h"
31 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
32 #include "llvm/Transforms/Utils/Local.h"
33 #include "llvm/Transforms/Utils/BuildLibCalls.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/SmallSet.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/Assembly/Writer.h"
38 #include "llvm/Support/CallSite.h"
39 #include "llvm/Support/CommandLine.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/GetElementPtrTypeIterator.h"
42 #include "llvm/Support/PatternMatch.h"
43 #include "llvm/Support/raw_ostream.h"
44 #include "llvm/Support/IRBuilder.h"
45 #include "llvm/Support/ValueHandle.h"
46 using namespace llvm;
47 using namespace llvm::PatternMatch;
48
49 STATISTIC(NumBlocksElim, "Number of blocks eliminated");
50 STATISTIC(NumPHIsElim,   "Number of trivial PHIs eliminated");
51 STATISTIC(NumGEPsElim,   "Number of GEPs converted to casts");
52 STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
53                       "sunken Cmps");
54 STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
55                        "of sunken Casts");
56 STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
57                           "computations were sunk");
58 STATISTIC(NumExtsMoved,  "Number of [s|z]ext instructions combined with loads");
59 STATISTIC(NumExtUses,    "Number of uses of [s|z]ext instructions optimized");
60 STATISTIC(NumRetsDup,    "Number of return instructions duplicated");
61
62 static cl::opt<bool> DisableBranchOpts(
63   "disable-cgp-branch-opts", cl::Hidden, cl::init(false),
64   cl::desc("Disable branch optimizations in CodeGenPrepare"));
65
66 namespace {
67   class CodeGenPrepare : public FunctionPass {
68     /// TLI - Keep a pointer of a TargetLowering to consult for determining
69     /// transformation profitability.
70     const TargetLowering *TLI;
71     DominatorTree *DT;
72     ProfileInfo *PFI;
73     
74     /// CurInstIterator - As we scan instructions optimizing them, this is the
75     /// next instruction to optimize.  Xforms that can invalidate this should
76     /// update it.
77     BasicBlock::iterator CurInstIterator;
78
79     /// Keeps track of non-local addresses that have been sunk into a block.
80     /// This allows us to avoid inserting duplicate code for blocks with
81     /// multiple load/stores of the same address.
82     DenseMap<Value*, Value*> SunkAddrs;
83
84     /// UpdateDT - If CFG is modified in anyway, dominator tree may need to
85     /// be updated.
86     bool UpdateDT;
87
88   public:
89     static char ID; // Pass identification, replacement for typeid
90     explicit CodeGenPrepare(const TargetLowering *tli = 0)
91       : FunctionPass(ID), TLI(tli) {
92         initializeCodeGenPreparePass(*PassRegistry::getPassRegistry());
93       }
94     bool runOnFunction(Function &F);
95
96     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
97       AU.addPreserved<DominatorTree>();
98       AU.addPreserved<ProfileInfo>();
99     }
100
101   private:
102     bool EliminateMostlyEmptyBlocks(Function &F);
103     bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
104     void EliminateMostlyEmptyBlock(BasicBlock *BB);
105     bool OptimizeBlock(BasicBlock &BB);
106     bool OptimizeInst(Instruction *I);
107     bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy);
108     bool OptimizeInlineAsmInst(CallInst *CS);
109     bool OptimizeCallInst(CallInst *CI);
110     bool MoveExtToFormExtLoad(Instruction *I);
111     bool OptimizeExtUses(Instruction *I);
112     bool DupRetToEnableTailCallOpts(ReturnInst *RI);
113   };
114 }
115
116 char CodeGenPrepare::ID = 0;
117 INITIALIZE_PASS(CodeGenPrepare, "codegenprepare",
118                 "Optimize for code generation", false, false)
119
120 FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) {
121   return new CodeGenPrepare(TLI);
122 }
123
124 bool CodeGenPrepare::runOnFunction(Function &F) {
125   bool EverMadeChange = false;
126
127   UpdateDT = false;
128   DT = getAnalysisIfAvailable<DominatorTree>();
129   PFI = getAnalysisIfAvailable<ProfileInfo>();
130
131   // First pass, eliminate blocks that contain only PHI nodes and an
132   // unconditional branch.
133   EverMadeChange |= EliminateMostlyEmptyBlocks(F);
134
135   bool MadeChange = true;
136   while (MadeChange) {
137     MadeChange = false;
138     for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
139       BasicBlock *BB = I++;
140       MadeChange |= OptimizeBlock(*BB);
141     }
142     EverMadeChange |= MadeChange;
143   }
144
145   SunkAddrs.clear();
146
147   if (!DisableBranchOpts) {
148     MadeChange = false;
149     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
150       MadeChange |= ConstantFoldTerminator(BB);
151
152     if (MadeChange)
153       UpdateDT = true;
154     EverMadeChange |= MadeChange;
155   }
156
157   if (UpdateDT && DT)
158     DT->DT->recalculate(F);
159
160   return EverMadeChange;
161 }
162
163 /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes,
164 /// debug info directives, and an unconditional branch.  Passes before isel
165 /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for
166 /// isel.  Start by eliminating these blocks so we can split them the way we
167 /// want them.
168 bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) {
169   bool MadeChange = false;
170   // Note that this intentionally skips the entry block.
171   for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) {
172     BasicBlock *BB = I++;
173
174     // If this block doesn't end with an uncond branch, ignore it.
175     BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
176     if (!BI || !BI->isUnconditional())
177       continue;
178
179     // If the instruction before the branch (skipping debug info) isn't a phi
180     // node, then other stuff is happening here.
181     BasicBlock::iterator BBI = BI;
182     if (BBI != BB->begin()) {
183       --BBI;
184       while (isa<DbgInfoIntrinsic>(BBI)) {
185         if (BBI == BB->begin())
186           break;
187         --BBI;
188       }
189       if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
190         continue;
191     }
192
193     // Do not break infinite loops.
194     BasicBlock *DestBB = BI->getSuccessor(0);
195     if (DestBB == BB)
196       continue;
197
198     if (!CanMergeBlocks(BB, DestBB))
199       continue;
200
201     EliminateMostlyEmptyBlock(BB);
202     MadeChange = true;
203   }
204   return MadeChange;
205 }
206
207 /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a
208 /// single uncond branch between them, and BB contains no other non-phi
209 /// instructions.
210 bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB,
211                                     const BasicBlock *DestBB) const {
212   // We only want to eliminate blocks whose phi nodes are used by phi nodes in
213   // the successor.  If there are more complex condition (e.g. preheaders),
214   // don't mess around with them.
215   BasicBlock::const_iterator BBI = BB->begin();
216   while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
217     for (Value::const_use_iterator UI = PN->use_begin(), E = PN->use_end();
218          UI != E; ++UI) {
219       const Instruction *User = cast<Instruction>(*UI);
220       if (User->getParent() != DestBB || !isa<PHINode>(User))
221         return false;
222       // If User is inside DestBB block and it is a PHINode then check
223       // incoming value. If incoming value is not from BB then this is
224       // a complex condition (e.g. preheaders) we want to avoid here.
225       if (User->getParent() == DestBB) {
226         if (const PHINode *UPN = dyn_cast<PHINode>(User))
227           for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
228             Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
229             if (Insn && Insn->getParent() == BB &&
230                 Insn->getParent() != UPN->getIncomingBlock(I))
231               return false;
232           }
233       }
234     }
235   }
236
237   // If BB and DestBB contain any common predecessors, then the phi nodes in BB
238   // and DestBB may have conflicting incoming values for the block.  If so, we
239   // can't merge the block.
240   const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
241   if (!DestBBPN) return true;  // no conflict.
242
243   // Collect the preds of BB.
244   SmallPtrSet<const BasicBlock*, 16> BBPreds;
245   if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
246     // It is faster to get preds from a PHI than with pred_iterator.
247     for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
248       BBPreds.insert(BBPN->getIncomingBlock(i));
249   } else {
250     BBPreds.insert(pred_begin(BB), pred_end(BB));
251   }
252
253   // Walk the preds of DestBB.
254   for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
255     BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
256     if (BBPreds.count(Pred)) {   // Common predecessor?
257       BBI = DestBB->begin();
258       while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
259         const Value *V1 = PN->getIncomingValueForBlock(Pred);
260         const Value *V2 = PN->getIncomingValueForBlock(BB);
261
262         // If V2 is a phi node in BB, look up what the mapped value will be.
263         if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
264           if (V2PN->getParent() == BB)
265             V2 = V2PN->getIncomingValueForBlock(Pred);
266
267         // If there is a conflict, bail out.
268         if (V1 != V2) return false;
269       }
270     }
271   }
272
273   return true;
274 }
275
276
277 /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and
278 /// an unconditional branch in it.
279 void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
280   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
281   BasicBlock *DestBB = BI->getSuccessor(0);
282
283   DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
284
285   // If the destination block has a single pred, then this is a trivial edge,
286   // just collapse it.
287   if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
288     if (SinglePred != DestBB) {
289       // Remember if SinglePred was the entry block of the function.  If so, we
290       // will need to move BB back to the entry position.
291       bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
292       MergeBasicBlockIntoOnlyPred(DestBB, this);
293
294       if (isEntry && BB != &BB->getParent()->getEntryBlock())
295         BB->moveBefore(&BB->getParent()->getEntryBlock());
296       
297       DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
298       return;
299     }
300   }
301
302   // Otherwise, we have multiple predecessors of BB.  Update the PHIs in DestBB
303   // to handle the new incoming edges it is about to have.
304   PHINode *PN;
305   for (BasicBlock::iterator BBI = DestBB->begin();
306        (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
307     // Remove the incoming value for BB, and remember it.
308     Value *InVal = PN->removeIncomingValue(BB, false);
309
310     // Two options: either the InVal is a phi node defined in BB or it is some
311     // value that dominates BB.
312     PHINode *InValPhi = dyn_cast<PHINode>(InVal);
313     if (InValPhi && InValPhi->getParent() == BB) {
314       // Add all of the input values of the input PHI as inputs of this phi.
315       for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
316         PN->addIncoming(InValPhi->getIncomingValue(i),
317                         InValPhi->getIncomingBlock(i));
318     } else {
319       // Otherwise, add one instance of the dominating value for each edge that
320       // we will be adding.
321       if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
322         for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
323           PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
324       } else {
325         for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
326           PN->addIncoming(InVal, *PI);
327       }
328     }
329   }
330
331   // The PHIs are now updated, change everything that refers to BB to use
332   // DestBB and remove BB.
333   BB->replaceAllUsesWith(DestBB);
334   if (DT && !UpdateDT) {
335     BasicBlock *BBIDom  = DT->getNode(BB)->getIDom()->getBlock();
336     BasicBlock *DestBBIDom = DT->getNode(DestBB)->getIDom()->getBlock();
337     BasicBlock *NewIDom = DT->findNearestCommonDominator(BBIDom, DestBBIDom);
338     DT->changeImmediateDominator(DestBB, NewIDom);
339     DT->eraseNode(BB);
340   }
341   if (PFI) {
342     PFI->replaceAllUses(BB, DestBB);
343     PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB));
344   }
345   BB->eraseFromParent();
346   ++NumBlocksElim;
347
348   DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
349 }
350
351 /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
352 /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC),
353 /// sink it into user blocks to reduce the number of virtual
354 /// registers that must be created and coalesced.
355 ///
356 /// Return true if any changes are made.
357 ///
358 static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
359   // If this is a noop copy,
360   EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
361   EVT DstVT = TLI.getValueType(CI->getType());
362
363   // This is an fp<->int conversion?
364   if (SrcVT.isInteger() != DstVT.isInteger())
365     return false;
366
367   // If this is an extension, it will be a zero or sign extension, which
368   // isn't a noop.
369   if (SrcVT.bitsLT(DstVT)) return false;
370
371   // If these values will be promoted, find out what they will be promoted
372   // to.  This helps us consider truncates on PPC as noop copies when they
373   // are.
374   if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote)
375     SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
376   if (TLI.getTypeAction(DstVT) == TargetLowering::Promote)
377     DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
378
379   // If, after promotion, these are the same types, this is a noop copy.
380   if (SrcVT != DstVT)
381     return false;
382
383   BasicBlock *DefBB = CI->getParent();
384
385   /// InsertedCasts - Only insert a cast in each block once.
386   DenseMap<BasicBlock*, CastInst*> InsertedCasts;
387
388   bool MadeChange = false;
389   for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
390        UI != E; ) {
391     Use &TheUse = UI.getUse();
392     Instruction *User = cast<Instruction>(*UI);
393
394     // Figure out which BB this cast is used in.  For PHI's this is the
395     // appropriate predecessor block.
396     BasicBlock *UserBB = User->getParent();
397     if (PHINode *PN = dyn_cast<PHINode>(User)) {
398       UserBB = PN->getIncomingBlock(UI);
399     }
400
401     // Preincrement use iterator so we don't invalidate it.
402     ++UI;
403
404     // If this user is in the same block as the cast, don't change the cast.
405     if (UserBB == DefBB) continue;
406
407     // If we have already inserted a cast into this block, use it.
408     CastInst *&InsertedCast = InsertedCasts[UserBB];
409
410     if (!InsertedCast) {
411       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
412
413       InsertedCast =
414         CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
415                          InsertPt);
416       MadeChange = true;
417     }
418
419     // Replace a use of the cast with a use of the new cast.
420     TheUse = InsertedCast;
421     ++NumCastUses;
422   }
423
424   // If we removed all uses, nuke the cast.
425   if (CI->use_empty()) {
426     CI->eraseFromParent();
427     MadeChange = true;
428   }
429
430   return MadeChange;
431 }
432
433 /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce
434 /// the number of virtual registers that must be created and coalesced.  This is
435 /// a clear win except on targets with multiple condition code registers
436 ///  (PowerPC), where it might lose; some adjustment may be wanted there.
437 ///
438 /// Return true if any changes are made.
439 static bool OptimizeCmpExpression(CmpInst *CI) {
440   BasicBlock *DefBB = CI->getParent();
441
442   /// InsertedCmp - Only insert a cmp in each block once.
443   DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
444
445   bool MadeChange = false;
446   for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
447        UI != E; ) {
448     Use &TheUse = UI.getUse();
449     Instruction *User = cast<Instruction>(*UI);
450
451     // Preincrement use iterator so we don't invalidate it.
452     ++UI;
453
454     // Don't bother for PHI nodes.
455     if (isa<PHINode>(User))
456       continue;
457
458     // Figure out which BB this cmp is used in.
459     BasicBlock *UserBB = User->getParent();
460
461     // If this user is in the same block as the cmp, don't change the cmp.
462     if (UserBB == DefBB) continue;
463
464     // If we have already inserted a cmp into this block, use it.
465     CmpInst *&InsertedCmp = InsertedCmps[UserBB];
466
467     if (!InsertedCmp) {
468       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
469
470       InsertedCmp =
471         CmpInst::Create(CI->getOpcode(),
472                         CI->getPredicate(),  CI->getOperand(0),
473                         CI->getOperand(1), "", InsertPt);
474       MadeChange = true;
475     }
476
477     // Replace a use of the cmp with a use of the new cmp.
478     TheUse = InsertedCmp;
479     ++NumCmpUses;
480   }
481
482   // If we removed all uses, nuke the cmp.
483   if (CI->use_empty())
484     CI->eraseFromParent();
485
486   return MadeChange;
487 }
488
489 namespace {
490 class CodeGenPrepareFortifiedLibCalls : public SimplifyFortifiedLibCalls {
491 protected:
492   void replaceCall(Value *With) {
493     CI->replaceAllUsesWith(With);
494     CI->eraseFromParent();
495   }
496   bool isFoldable(unsigned SizeCIOp, unsigned, bool) const {
497       if (ConstantInt *SizeCI =
498                              dyn_cast<ConstantInt>(CI->getArgOperand(SizeCIOp)))
499         return SizeCI->isAllOnesValue();
500     return false;
501   }
502 };
503 } // end anonymous namespace
504
505 bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
506   BasicBlock *BB = CI->getParent();
507   
508   // Lower inline assembly if we can.
509   // If we found an inline asm expession, and if the target knows how to
510   // lower it to normal LLVM code, do so now.
511   if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
512     if (TLI->ExpandInlineAsm(CI)) {
513       // Avoid invalidating the iterator.
514       CurInstIterator = BB->begin();
515       // Avoid processing instructions out of order, which could cause
516       // reuse before a value is defined.
517       SunkAddrs.clear();
518       return true;
519     }
520     // Sink address computing for memory operands into the block.
521     if (OptimizeInlineAsmInst(CI))
522       return true;
523   }
524   
525   // Lower all uses of llvm.objectsize.*
526   IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
527   if (II && II->getIntrinsicID() == Intrinsic::objectsize) {
528     bool Min = (cast<ConstantInt>(II->getArgOperand(1))->getZExtValue() == 1);
529     const Type *ReturnTy = CI->getType();
530     Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);    
531     
532     // Substituting this can cause recursive simplifications, which can
533     // invalidate our iterator.  Use a WeakVH to hold onto it in case this
534     // happens.
535     WeakVH IterHandle(CurInstIterator);
536     
537     ReplaceAndSimplifyAllUses(CI, RetVal, TLI ? TLI->getTargetData() : 0,
538                               UpdateDT ? 0 : DT);
539
540     // If the iterator instruction was recursively deleted, start over at the
541     // start of the block.
542     if (IterHandle != CurInstIterator) {
543       CurInstIterator = BB->begin();
544       SunkAddrs.clear();
545     }
546     return true;
547   }
548
549   // From here on out we're working with named functions.
550   if (CI->getCalledFunction() == 0) return false;
551   
552   // We'll need TargetData from here on out.
553   const TargetData *TD = TLI ? TLI->getTargetData() : 0;
554   if (!TD) return false;
555   
556   // Lower all default uses of _chk calls.  This is very similar
557   // to what InstCombineCalls does, but here we are only lowering calls
558   // that have the default "don't know" as the objectsize.  Anything else
559   // should be left alone.
560   CodeGenPrepareFortifiedLibCalls Simplifier;
561   return Simplifier.fold(CI, TD);
562 }
563
564 /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return
565 /// instructions to the predecessor to enable tail call optimizations. The
566 /// case it is currently looking for is:
567 /// bb0:
568 ///   %tmp0 = tail call i32 @f0()
569 ///   br label %return
570 /// bb1:
571 ///   %tmp1 = tail call i32 @f1()
572 ///   br label %return
573 /// bb2:
574 ///   %tmp2 = tail call i32 @f2()
575 ///   br label %return
576 /// return:
577 ///   %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
578 ///   ret i32 %retval
579 ///
580 /// =>
581 ///
582 /// bb0:
583 ///   %tmp0 = tail call i32 @f0()
584 ///   ret i32 %tmp0
585 /// bb1:
586 ///   %tmp1 = tail call i32 @f1()
587 ///   ret i32 %tmp1
588 /// bb2:
589 ///   %tmp2 = tail call i32 @f2()
590 ///   ret i32 %tmp2
591 ///
592 bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
593   if (!TLI)
594     return false;
595
596   Value *V = RI->getReturnValue();
597   if (!V)
598     return false;
599
600   if (PHINode *PN = dyn_cast<PHINode>(V)) {
601     BasicBlock *BB = RI->getParent();
602     if (PN->getParent() != BB)
603       return false;
604
605     // It's not safe to eliminate the sign / zero extension of the return value.
606     // See llvm::isInTailCallPosition().
607     const Function *F = BB->getParent();
608     unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
609     if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
610       return false;
611
612     // Make sure there are no instructions between PHI and return.
613     BasicBlock::iterator BI = PN;
614     do { ++BI; } while (isa<DbgInfoIntrinsic>(BI));
615     if (&*BI != RI)
616       return false;
617
618     /// Only dup the ReturnInst if the CallInst is likely to be emitted as a
619     /// tail call.
620     SmallVector<CallInst*, 4> TailCalls;
621     for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
622       CallInst *CI = dyn_cast<CallInst>(PN->getIncomingValue(I));
623       // Make sure the phi value is indeed produced by the tail call.
624       if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
625           TLI->mayBeEmittedAsTailCall(CI))
626         TailCalls.push_back(CI);
627     }
628
629     bool Changed = false;
630     for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
631       CallInst *CI = TailCalls[i];
632       CallSite CS(CI);
633
634       // Conservatively require the attributes of the call to match those of
635       // the return. Ignore noalias because it doesn't affect the call sequence.
636       unsigned CalleeRetAttr = CS.getAttributes().getRetAttributes();
637       if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
638         continue;
639
640       // Make sure the call instruction is followed by an unconditional branch
641       // to the return block.
642       BasicBlock *CallBB = CI->getParent();
643       BranchInst *BI = dyn_cast<BranchInst>(CallBB->getTerminator());
644       if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
645         continue;
646
647       // Duplicate the return into CallBB.
648       (void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
649       UpdateDT = Changed = true;
650       ++NumRetsDup;
651     }
652
653     // If we eliminated all predecessors of the block, delete the block now.
654     if (Changed && pred_begin(BB) == pred_end(BB))
655       BB->eraseFromParent();
656
657     return Changed;
658   }
659
660   return false;
661 }
662
663 //===----------------------------------------------------------------------===//
664 // Memory Optimization
665 //===----------------------------------------------------------------------===//
666
667 /// IsNonLocalValue - Return true if the specified values are defined in a
668 /// different basic block than BB.
669 static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
670   if (Instruction *I = dyn_cast<Instruction>(V))
671     return I->getParent() != BB;
672   return false;
673 }
674
675 /// OptimizeMemoryInst - Load and Store Instructions often have
676 /// addressing modes that can do significant amounts of computation.  As such,
677 /// instruction selection will try to get the load or store to do as much
678 /// computation as possible for the program.  The problem is that isel can only
679 /// see within a single block.  As such, we sink as much legal addressing mode
680 /// stuff into the block as possible.
681 ///
682 /// This method is used to optimize both load/store and inline asms with memory
683 /// operands.
684 bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
685                                         const Type *AccessTy) {
686   Value *Repl = Addr;
687   
688   // Try to collapse single-value PHI nodes.  This is necessary to undo 
689   // unprofitable PRE transformations.
690   SmallVector<Value*, 8> worklist;
691   SmallPtrSet<Value*, 16> Visited;
692   worklist.push_back(Addr);
693   
694   // Use a worklist to iteratively look through PHI nodes, and ensure that
695   // the addressing mode obtained from the non-PHI roots of the graph
696   // are equivalent.
697   Value *Consensus = 0;
698   unsigned NumUsesConsensus = 0;
699   bool IsNumUsesConsensusValid = false;
700   SmallVector<Instruction*, 16> AddrModeInsts;
701   ExtAddrMode AddrMode;
702   while (!worklist.empty()) {
703     Value *V = worklist.back();
704     worklist.pop_back();
705     
706     // Break use-def graph loops.
707     if (Visited.count(V)) {
708       Consensus = 0;
709       break;
710     }
711     
712     Visited.insert(V);
713     
714     // For a PHI node, push all of its incoming values.
715     if (PHINode *P = dyn_cast<PHINode>(V)) {
716       for (unsigned i = 0, e = P->getNumIncomingValues(); i != e; ++i)
717         worklist.push_back(P->getIncomingValue(i));
718       continue;
719     }
720     
721     // For non-PHIs, determine the addressing mode being computed.
722     SmallVector<Instruction*, 16> NewAddrModeInsts;
723     ExtAddrMode NewAddrMode =
724       AddressingModeMatcher::Match(V, AccessTy,MemoryInst,
725                                    NewAddrModeInsts, *TLI);
726
727     // This check is broken into two cases with very similar code to avoid using
728     // getNumUses() as much as possible. Some values have a lot of uses, so
729     // calling getNumUses() unconditionally caused a significant compile-time
730     // regression.
731     if (!Consensus) {
732       Consensus = V;
733       AddrMode = NewAddrMode;
734       AddrModeInsts = NewAddrModeInsts;
735       continue;
736     } else if (NewAddrMode == AddrMode) {
737       if (!IsNumUsesConsensusValid) {
738         NumUsesConsensus = Consensus->getNumUses();
739         IsNumUsesConsensusValid = true;
740       }
741
742       // Ensure that the obtained addressing mode is equivalent to that obtained
743       // for all other roots of the PHI traversal.  Also, when choosing one
744       // such root as representative, select the one with the most uses in order
745       // to keep the cost modeling heuristics in AddressingModeMatcher
746       // applicable.
747       unsigned NumUses = V->getNumUses();
748       if (NumUses > NumUsesConsensus) {
749         Consensus = V;
750         NumUsesConsensus = NumUses;
751         AddrModeInsts = NewAddrModeInsts;
752       }
753       continue;
754     }
755     
756     Consensus = 0;
757     break;
758   }
759   
760   // If the addressing mode couldn't be determined, or if multiple different
761   // ones were determined, bail out now.
762   if (!Consensus) return false;
763   
764   // Check to see if any of the instructions supersumed by this addr mode are
765   // non-local to I's BB.
766   bool AnyNonLocal = false;
767   for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
768     if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
769       AnyNonLocal = true;
770       break;
771     }
772   }
773
774   // If all the instructions matched are already in this BB, don't do anything.
775   if (!AnyNonLocal) {
776     DEBUG(dbgs() << "CGP: Found      local addrmode: " << AddrMode << "\n");
777     return false;
778   }
779
780   // Insert this computation right after this user.  Since our caller is
781   // scanning from the top of the BB to the bottom, reuse of the expr are
782   // guaranteed to happen later.
783   BasicBlock::iterator InsertPt = MemoryInst;
784
785   // Now that we determined the addressing expression we want to use and know
786   // that we have to sink it into this block.  Check to see if we have already
787   // done this for some other load/store instr in this block.  If so, reuse the
788   // computation.
789   Value *&SunkAddr = SunkAddrs[Addr];
790   if (SunkAddr) {
791     DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
792                  << *MemoryInst);
793     if (SunkAddr->getType() != Addr->getType())
794       SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt);
795   } else {
796     DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
797                  << *MemoryInst);
798     const Type *IntPtrTy =
799           TLI->getTargetData()->getIntPtrType(AccessTy->getContext());
800
801     Value *Result = 0;
802
803     // Start with the base register. Do this first so that subsequent address
804     // matching finds it last, which will prevent it from trying to match it
805     // as the scaled value in case it happens to be a mul. That would be
806     // problematic if we've sunk a different mul for the scale, because then
807     // we'd end up sinking both muls.
808     if (AddrMode.BaseReg) {
809       Value *V = AddrMode.BaseReg;
810       if (V->getType()->isPointerTy())
811         V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
812       if (V->getType() != IntPtrTy)
813         V = CastInst::CreateIntegerCast(V, IntPtrTy, /*isSigned=*/true,
814                                         "sunkaddr", InsertPt);
815       Result = V;
816     }
817
818     // Add the scale value.
819     if (AddrMode.Scale) {
820       Value *V = AddrMode.ScaledReg;
821       if (V->getType() == IntPtrTy) {
822         // done.
823       } else if (V->getType()->isPointerTy()) {
824         V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
825       } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
826                  cast<IntegerType>(V->getType())->getBitWidth()) {
827         V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt);
828       } else {
829         V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt);
830       }
831       if (AddrMode.Scale != 1)
832         V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy,
833                                                                 AddrMode.Scale),
834                                       "sunkaddr", InsertPt);
835       if (Result)
836         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
837       else
838         Result = V;
839     }
840
841     // Add in the BaseGV if present.
842     if (AddrMode.BaseGV) {
843       Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr",
844                                   InsertPt);
845       if (Result)
846         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
847       else
848         Result = V;
849     }
850
851     // Add in the Base Offset if present.
852     if (AddrMode.BaseOffs) {
853       Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
854       if (Result)
855         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
856       else
857         Result = V;
858     }
859
860     if (Result == 0)
861       SunkAddr = Constant::getNullValue(Addr->getType());
862     else
863       SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt);
864   }
865
866   MemoryInst->replaceUsesOfWith(Repl, SunkAddr);
867
868   if (Repl->use_empty()) {
869     RecursivelyDeleteTriviallyDeadInstructions(Repl);
870     // This address is now available for reassignment, so erase the table entry;
871     // we don't want to match some completely different instruction.
872     SunkAddrs[Addr] = 0;
873   }
874   ++NumMemoryInsts;
875   return true;
876 }
877
878 /// OptimizeInlineAsmInst - If there are any memory operands, use
879 /// OptimizeMemoryInst to sink their address computing into the block when
880 /// possible / profitable.
881 bool CodeGenPrepare::OptimizeInlineAsmInst(CallInst *CS) {
882   bool MadeChange = false;
883
884   TargetLowering::AsmOperandInfoVector 
885     TargetConstraints = TLI->ParseConstraints(CS);
886   unsigned ArgNo = 0;
887   for (unsigned i = 0, e = TargetConstraints.size(); i != e; ++i) {
888     TargetLowering::AsmOperandInfo &OpInfo = TargetConstraints[i];
889     
890     // Compute the constraint code and ConstraintType to use.
891     TLI->ComputeConstraintToUse(OpInfo, SDValue());
892
893     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
894         OpInfo.isIndirect) {
895       Value *OpVal = CS->getArgOperand(ArgNo++);
896       MadeChange |= OptimizeMemoryInst(CS, OpVal, OpVal->getType());
897     } else if (OpInfo.Type == InlineAsm::isInput)
898       ArgNo++;
899   }
900
901   return MadeChange;
902 }
903
904 /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same
905 /// basic block as the load, unless conditions are unfavorable. This allows
906 /// SelectionDAG to fold the extend into the load.
907 ///
908 bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) {
909   // Look for a load being extended.
910   LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0));
911   if (!LI) return false;
912
913   // If they're already in the same block, there's nothing to do.
914   if (LI->getParent() == I->getParent())
915     return false;
916
917   // If the load has other users and the truncate is not free, this probably
918   // isn't worthwhile.
919   if (!LI->hasOneUse() &&
920       TLI && (TLI->isTypeLegal(TLI->getValueType(LI->getType())) ||
921               !TLI->isTypeLegal(TLI->getValueType(I->getType()))) &&
922       !TLI->isTruncateFree(I->getType(), LI->getType()))
923     return false;
924
925   // Check whether the target supports casts folded into loads.
926   unsigned LType;
927   if (isa<ZExtInst>(I))
928     LType = ISD::ZEXTLOAD;
929   else {
930     assert(isa<SExtInst>(I) && "Unexpected ext type!");
931     LType = ISD::SEXTLOAD;
932   }
933   if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType())))
934     return false;
935
936   // Move the extend into the same block as the load, so that SelectionDAG
937   // can fold it.
938   I->removeFromParent();
939   I->insertAfter(LI);
940   ++NumExtsMoved;
941   return true;
942 }
943
944 bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
945   BasicBlock *DefBB = I->getParent();
946
947   // If the result of a {s|z}ext and its source are both live out, rewrite all
948   // other uses of the source with result of extension.
949   Value *Src = I->getOperand(0);
950   if (Src->hasOneUse())
951     return false;
952
953   // Only do this xform if truncating is free.
954   if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
955     return false;
956
957   // Only safe to perform the optimization if the source is also defined in
958   // this block.
959   if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
960     return false;
961
962   bool DefIsLiveOut = false;
963   for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
964        UI != E; ++UI) {
965     Instruction *User = cast<Instruction>(*UI);
966
967     // Figure out which BB this ext is used in.
968     BasicBlock *UserBB = User->getParent();
969     if (UserBB == DefBB) continue;
970     DefIsLiveOut = true;
971     break;
972   }
973   if (!DefIsLiveOut)
974     return false;
975
976   // Make sure non of the uses are PHI nodes.
977   for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
978        UI != E; ++UI) {
979     Instruction *User = cast<Instruction>(*UI);
980     BasicBlock *UserBB = User->getParent();
981     if (UserBB == DefBB) continue;
982     // Be conservative. We don't want this xform to end up introducing
983     // reloads just before load / store instructions.
984     if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User))
985       return false;
986   }
987
988   // InsertedTruncs - Only insert one trunc in each block once.
989   DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
990
991   bool MadeChange = false;
992   for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
993        UI != E; ++UI) {
994     Use &TheUse = UI.getUse();
995     Instruction *User = cast<Instruction>(*UI);
996
997     // Figure out which BB this ext is used in.
998     BasicBlock *UserBB = User->getParent();
999     if (UserBB == DefBB) continue;
1000
1001     // Both src and def are live in this block. Rewrite the use.
1002     Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
1003
1004     if (!InsertedTrunc) {
1005       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
1006
1007       InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
1008     }
1009
1010     // Replace a use of the {s|z}ext source with a use of the result.
1011     TheUse = InsertedTrunc;
1012     ++NumExtUses;
1013     MadeChange = true;
1014   }
1015
1016   return MadeChange;
1017 }
1018
1019 bool CodeGenPrepare::OptimizeInst(Instruction *I) {
1020   if (PHINode *P = dyn_cast<PHINode>(I)) {
1021     // It is possible for very late stage optimizations (such as SimplifyCFG)
1022     // to introduce PHI nodes too late to be cleaned up.  If we detect such a
1023     // trivial PHI, go ahead and zap it here.
1024     if (Value *V = SimplifyInstruction(P)) {
1025       P->replaceAllUsesWith(V);
1026       P->eraseFromParent();
1027       ++NumPHIsElim;
1028       return true;
1029     }
1030     return false;
1031   }
1032   
1033   if (CastInst *CI = dyn_cast<CastInst>(I)) {
1034     // If the source of the cast is a constant, then this should have
1035     // already been constant folded.  The only reason NOT to constant fold
1036     // it is if something (e.g. LSR) was careful to place the constant
1037     // evaluation in a block other than then one that uses it (e.g. to hoist
1038     // the address of globals out of a loop).  If this is the case, we don't
1039     // want to forward-subst the cast.
1040     if (isa<Constant>(CI->getOperand(0)))
1041       return false;
1042
1043     if (TLI && OptimizeNoopCopyExpression(CI, *TLI))
1044       return true;
1045
1046     if (isa<ZExtInst>(I) || isa<SExtInst>(I)) {
1047       bool MadeChange = MoveExtToFormExtLoad(I);
1048       return MadeChange | OptimizeExtUses(I);
1049     }
1050     return false;
1051   }
1052   
1053   if (CmpInst *CI = dyn_cast<CmpInst>(I))
1054     return OptimizeCmpExpression(CI);
1055   
1056   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1057     if (TLI)
1058       return OptimizeMemoryInst(I, I->getOperand(0), LI->getType());
1059     return false;
1060   }
1061   
1062   if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1063     if (TLI)
1064       return OptimizeMemoryInst(I, SI->getOperand(1),
1065                                 SI->getOperand(0)->getType());
1066     return false;
1067   }
1068   
1069   if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1070     if (GEPI->hasAllZeroIndices()) {
1071       /// The GEP operand must be a pointer, so must its result -> BitCast
1072       Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
1073                                         GEPI->getName(), GEPI);
1074       GEPI->replaceAllUsesWith(NC);
1075       GEPI->eraseFromParent();
1076       ++NumGEPsElim;
1077       OptimizeInst(NC);
1078       return true;
1079     }
1080     return false;
1081   }
1082   
1083   if (CallInst *CI = dyn_cast<CallInst>(I))
1084     return OptimizeCallInst(CI);
1085
1086   if (ReturnInst *RI = dyn_cast<ReturnInst>(I))
1087     return DupRetToEnableTailCallOpts(RI);
1088
1089   return false;
1090 }
1091
1092 // In this pass we look for GEP and cast instructions that are used
1093 // across basic blocks and rewrite them to improve basic-block-at-a-time
1094 // selection.
1095 bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) {
1096   SunkAddrs.clear();
1097   bool MadeChange = false;
1098
1099   CurInstIterator = BB.begin();
1100   for (BasicBlock::iterator E = BB.end(); CurInstIterator != E; )
1101     MadeChange |= OptimizeInst(CurInstIterator++);
1102
1103   return MadeChange;
1104 }