stpcpy is so similar to strcpy, it doesn't deserve a complete copy of the __strcpy_ch...
[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/ProfileInfo.h"
26 #include "llvm/Target/TargetData.h"
27 #include "llvm/Target/TargetLowering.h"
28 #include "llvm/Transforms/Utils/AddrModeMatcher.h"
29 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
30 #include "llvm/Transforms/Utils/Local.h"
31 #include "llvm/Transforms/Utils/BuildLibCalls.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/SmallSet.h"
34 #include "llvm/Assembly/Writer.h"
35 #include "llvm/Support/CallSite.h"
36 #include "llvm/Support/Debug.h"
37 #include "llvm/Support/GetElementPtrTypeIterator.h"
38 #include "llvm/Support/PatternMatch.h"
39 #include "llvm/Support/raw_ostream.h"
40 #include "llvm/Support/IRBuilder.h"
41 using namespace llvm;
42 using namespace llvm::PatternMatch;
43
44 namespace {
45   class CodeGenPrepare : public FunctionPass {
46     /// TLI - Keep a pointer of a TargetLowering to consult for determining
47     /// transformation profitability.
48     const TargetLowering *TLI;
49     ProfileInfo *PFI;
50
51     /// BackEdges - Keep a set of all the loop back edges.
52     ///
53     SmallSet<std::pair<const BasicBlock*, const BasicBlock*>, 8> BackEdges;
54   public:
55     static char ID; // Pass identification, replacement for typeid
56     explicit CodeGenPrepare(const TargetLowering *tli = 0)
57       : FunctionPass(&ID), TLI(tli) {}
58     bool runOnFunction(Function &F);
59
60     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
61       AU.addPreserved<ProfileInfo>();
62     }
63
64     virtual void releaseMemory() {
65       BackEdges.clear();
66     }
67
68   private:
69     bool EliminateMostlyEmptyBlocks(Function &F);
70     bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
71     void EliminateMostlyEmptyBlock(BasicBlock *BB);
72     bool OptimizeBlock(BasicBlock &BB);
73     bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy,
74                             DenseMap<Value*,Value*> &SunkAddrs);
75     bool OptimizeInlineAsmInst(Instruction *I, CallSite CS,
76                                DenseMap<Value*,Value*> &SunkAddrs);
77     bool OptimizeCallInst(CallInst *CI);
78     bool MoveExtToFormExtLoad(Instruction *I);
79     bool OptimizeExtUses(Instruction *I);
80     void findLoopBackEdges(const Function &F);
81   };
82 }
83
84 char CodeGenPrepare::ID = 0;
85 static RegisterPass<CodeGenPrepare> X("codegenprepare",
86                                       "Optimize for code generation");
87
88 FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) {
89   return new CodeGenPrepare(TLI);
90 }
91
92 /// findLoopBackEdges - Do a DFS walk to find loop back edges.
93 ///
94 void CodeGenPrepare::findLoopBackEdges(const Function &F) {
95   SmallVector<std::pair<const BasicBlock*,const BasicBlock*>, 32> Edges;
96   FindFunctionBackedges(F, Edges);
97   
98   BackEdges.insert(Edges.begin(), Edges.end());
99 }
100
101
102 bool CodeGenPrepare::runOnFunction(Function &F) {
103   bool EverMadeChange = false;
104
105   PFI = getAnalysisIfAvailable<ProfileInfo>();
106   // First pass, eliminate blocks that contain only PHI nodes and an
107   // unconditional branch.
108   EverMadeChange |= EliminateMostlyEmptyBlocks(F);
109
110   // Now find loop back edges.
111   findLoopBackEdges(F);
112
113   bool MadeChange = true;
114   while (MadeChange) {
115     MadeChange = false;
116     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
117       MadeChange |= OptimizeBlock(*BB);
118     EverMadeChange |= MadeChange;
119   }
120   return EverMadeChange;
121 }
122
123 /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes,
124 /// debug info directives, and an unconditional branch.  Passes before isel
125 /// (e.g. LSR/loopsimplify) often split edges in ways that are non-optimal for
126 /// isel.  Start by eliminating these blocks so we can split them the way we
127 /// want them.
128 bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) {
129   bool MadeChange = false;
130   // Note that this intentionally skips the entry block.
131   for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) {
132     BasicBlock *BB = I++;
133
134     // If this block doesn't end with an uncond branch, ignore it.
135     BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
136     if (!BI || !BI->isUnconditional())
137       continue;
138
139     // If the instruction before the branch (skipping debug info) isn't a phi
140     // node, then other stuff is happening here.
141     BasicBlock::iterator BBI = BI;
142     if (BBI != BB->begin()) {
143       --BBI;
144       while (isa<DbgInfoIntrinsic>(BBI)) {
145         if (BBI == BB->begin())
146           break;
147         --BBI;
148       }
149       if (!isa<DbgInfoIntrinsic>(BBI) && !isa<PHINode>(BBI))
150         continue;
151     }
152
153     // Do not break infinite loops.
154     BasicBlock *DestBB = BI->getSuccessor(0);
155     if (DestBB == BB)
156       continue;
157
158     if (!CanMergeBlocks(BB, DestBB))
159       continue;
160
161     EliminateMostlyEmptyBlock(BB);
162     MadeChange = true;
163   }
164   return MadeChange;
165 }
166
167 /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a
168 /// single uncond branch between them, and BB contains no other non-phi
169 /// instructions.
170 bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB,
171                                     const BasicBlock *DestBB) const {
172   // We only want to eliminate blocks whose phi nodes are used by phi nodes in
173   // the successor.  If there are more complex condition (e.g. preheaders),
174   // don't mess around with them.
175   BasicBlock::const_iterator BBI = BB->begin();
176   while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
177     for (Value::use_const_iterator UI = PN->use_begin(), E = PN->use_end();
178          UI != E; ++UI) {
179       const Instruction *User = cast<Instruction>(*UI);
180       if (User->getParent() != DestBB || !isa<PHINode>(User))
181         return false;
182       // If User is inside DestBB block and it is a PHINode then check
183       // incoming value. If incoming value is not from BB then this is
184       // a complex condition (e.g. preheaders) we want to avoid here.
185       if (User->getParent() == DestBB) {
186         if (const PHINode *UPN = dyn_cast<PHINode>(User))
187           for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
188             Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
189             if (Insn && Insn->getParent() == BB &&
190                 Insn->getParent() != UPN->getIncomingBlock(I))
191               return false;
192           }
193       }
194     }
195   }
196
197   // If BB and DestBB contain any common predecessors, then the phi nodes in BB
198   // and DestBB may have conflicting incoming values for the block.  If so, we
199   // can't merge the block.
200   const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
201   if (!DestBBPN) return true;  // no conflict.
202
203   // Collect the preds of BB.
204   SmallPtrSet<const BasicBlock*, 16> BBPreds;
205   if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
206     // It is faster to get preds from a PHI than with pred_iterator.
207     for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
208       BBPreds.insert(BBPN->getIncomingBlock(i));
209   } else {
210     BBPreds.insert(pred_begin(BB), pred_end(BB));
211   }
212
213   // Walk the preds of DestBB.
214   for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
215     BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
216     if (BBPreds.count(Pred)) {   // Common predecessor?
217       BBI = DestBB->begin();
218       while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
219         const Value *V1 = PN->getIncomingValueForBlock(Pred);
220         const Value *V2 = PN->getIncomingValueForBlock(BB);
221
222         // If V2 is a phi node in BB, look up what the mapped value will be.
223         if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
224           if (V2PN->getParent() == BB)
225             V2 = V2PN->getIncomingValueForBlock(Pred);
226
227         // If there is a conflict, bail out.
228         if (V1 != V2) return false;
229       }
230     }
231   }
232
233   return true;
234 }
235
236
237 /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and
238 /// an unconditional branch in it.
239 void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
240   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
241   BasicBlock *DestBB = BI->getSuccessor(0);
242
243   DEBUG(dbgs() << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB);
244
245   // If the destination block has a single pred, then this is a trivial edge,
246   // just collapse it.
247   if (BasicBlock *SinglePred = DestBB->getSinglePredecessor()) {
248     if (SinglePred != DestBB) {
249       // Remember if SinglePred was the entry block of the function.  If so, we
250       // will need to move BB back to the entry position.
251       bool isEntry = SinglePred == &SinglePred->getParent()->getEntryBlock();
252       MergeBasicBlockIntoOnlyPred(DestBB, this);
253
254       if (isEntry && BB != &BB->getParent()->getEntryBlock())
255         BB->moveBefore(&BB->getParent()->getEntryBlock());
256       
257       DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
258       return;
259     }
260   }
261
262   // Otherwise, we have multiple predecessors of BB.  Update the PHIs in DestBB
263   // to handle the new incoming edges it is about to have.
264   PHINode *PN;
265   for (BasicBlock::iterator BBI = DestBB->begin();
266        (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
267     // Remove the incoming value for BB, and remember it.
268     Value *InVal = PN->removeIncomingValue(BB, false);
269
270     // Two options: either the InVal is a phi node defined in BB or it is some
271     // value that dominates BB.
272     PHINode *InValPhi = dyn_cast<PHINode>(InVal);
273     if (InValPhi && InValPhi->getParent() == BB) {
274       // Add all of the input values of the input PHI as inputs of this phi.
275       for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
276         PN->addIncoming(InValPhi->getIncomingValue(i),
277                         InValPhi->getIncomingBlock(i));
278     } else {
279       // Otherwise, add one instance of the dominating value for each edge that
280       // we will be adding.
281       if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
282         for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
283           PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
284       } else {
285         for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
286           PN->addIncoming(InVal, *PI);
287       }
288     }
289   }
290
291   // The PHIs are now updated, change everything that refers to BB to use
292   // DestBB and remove BB.
293   BB->replaceAllUsesWith(DestBB);
294   if (PFI) {
295     PFI->replaceAllUses(BB, DestBB);
296     PFI->removeEdge(ProfileInfo::getEdge(BB, DestBB));
297   }
298   BB->eraseFromParent();
299
300   DEBUG(dbgs() << "AFTER:\n" << *DestBB << "\n\n\n");
301 }
302
303 /// FindReusablePredBB - Check all of the predecessors of the block DestPHI
304 /// lives in to see if there is a block that we can reuse as a critical edge
305 /// from TIBB.
306 static BasicBlock *FindReusablePredBB(PHINode *DestPHI, BasicBlock *TIBB) {
307   BasicBlock *Dest = DestPHI->getParent();
308   
309   /// TIPHIValues - This array is lazily computed to determine the values of
310   /// PHIs in Dest that TI would provide.
311   SmallVector<Value*, 32> TIPHIValues;
312   
313   /// TIBBEntryNo - This is a cache to speed up pred queries for TIBB.
314   unsigned TIBBEntryNo = 0;
315   
316   // Check to see if Dest has any blocks that can be used as a split edge for
317   // this terminator.
318   for (unsigned pi = 0, e = DestPHI->getNumIncomingValues(); pi != e; ++pi) {
319     BasicBlock *Pred = DestPHI->getIncomingBlock(pi);
320     // To be usable, the pred has to end with an uncond branch to the dest.
321     BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator());
322     if (!PredBr || !PredBr->isUnconditional())
323       continue;
324     // Must be empty other than the branch and debug info.
325     BasicBlock::iterator I = Pred->begin();
326     while (isa<DbgInfoIntrinsic>(I))
327       I++;
328     if (&*I != PredBr)
329       continue;
330     // Cannot be the entry block; its label does not get emitted.
331     if (Pred == &Dest->getParent()->getEntryBlock())
332       continue;
333     
334     // Finally, since we know that Dest has phi nodes in it, we have to make
335     // sure that jumping to Pred will have the same effect as going to Dest in
336     // terms of PHI values.
337     PHINode *PN;
338     unsigned PHINo = 0;
339     unsigned PredEntryNo = pi;
340     
341     bool FoundMatch = true;
342     for (BasicBlock::iterator I = Dest->begin();
343          (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) {
344       if (PHINo == TIPHIValues.size()) {
345         if (PN->getIncomingBlock(TIBBEntryNo) != TIBB)
346           TIBBEntryNo = PN->getBasicBlockIndex(TIBB);
347         TIPHIValues.push_back(PN->getIncomingValue(TIBBEntryNo));
348       }
349       
350       // If the PHI entry doesn't work, we can't use this pred.
351       if (PN->getIncomingBlock(PredEntryNo) != Pred)
352         PredEntryNo = PN->getBasicBlockIndex(Pred);
353       
354       if (TIPHIValues[PHINo] != PN->getIncomingValue(PredEntryNo)) {
355         FoundMatch = false;
356         break;
357       }
358     }
359     
360     // If we found a workable predecessor, change TI to branch to Succ.
361     if (FoundMatch)
362       return Pred;
363   }
364   return 0;  
365 }
366
367
368 /// SplitEdgeNicely - Split the critical edge from TI to its specified
369 /// successor if it will improve codegen.  We only do this if the successor has
370 /// phi nodes (otherwise critical edges are ok).  If there is already another
371 /// predecessor of the succ that is empty (and thus has no phi nodes), use it
372 /// instead of introducing a new block.
373 static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum,
374                      SmallSet<std::pair<const BasicBlock*,
375                                         const BasicBlock*>, 8> &BackEdges,
376                              Pass *P) {
377   BasicBlock *TIBB = TI->getParent();
378   BasicBlock *Dest = TI->getSuccessor(SuccNum);
379   assert(isa<PHINode>(Dest->begin()) &&
380          "This should only be called if Dest has a PHI!");
381   PHINode *DestPHI = cast<PHINode>(Dest->begin());
382
383   // Do not split edges to EH landing pads.
384   if (InvokeInst *Invoke = dyn_cast<InvokeInst>(TI))
385     if (Invoke->getSuccessor(1) == Dest)
386       return;
387
388   // As a hack, never split backedges of loops.  Even though the copy for any
389   // PHIs inserted on the backedge would be dead for exits from the loop, we
390   // assume that the cost of *splitting* the backedge would be too high.
391   if (BackEdges.count(std::make_pair(TIBB, Dest)))
392     return;
393
394   if (BasicBlock *ReuseBB = FindReusablePredBB(DestPHI, TIBB)) {
395     ProfileInfo *PFI = P->getAnalysisIfAvailable<ProfileInfo>();
396     if (PFI)
397       PFI->splitEdge(TIBB, Dest, ReuseBB);
398     Dest->removePredecessor(TIBB);
399     TI->setSuccessor(SuccNum, ReuseBB);
400     return;
401   }
402
403   SplitCriticalEdge(TI, SuccNum, P, true);
404 }
405
406
407 /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
408 /// copy (e.g. it's casting from one pointer type to another, i32->i8 on PPC),
409 /// sink it into user blocks to reduce the number of virtual
410 /// registers that must be created and coalesced.
411 ///
412 /// Return true if any changes are made.
413 ///
414 static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
415   // If this is a noop copy,
416   EVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
417   EVT DstVT = TLI.getValueType(CI->getType());
418
419   // This is an fp<->int conversion?
420   if (SrcVT.isInteger() != DstVT.isInteger())
421     return false;
422
423   // If this is an extension, it will be a zero or sign extension, which
424   // isn't a noop.
425   if (SrcVT.bitsLT(DstVT)) return false;
426
427   // If these values will be promoted, find out what they will be promoted
428   // to.  This helps us consider truncates on PPC as noop copies when they
429   // are.
430   if (TLI.getTypeAction(CI->getContext(), SrcVT) == TargetLowering::Promote)
431     SrcVT = TLI.getTypeToTransformTo(CI->getContext(), SrcVT);
432   if (TLI.getTypeAction(CI->getContext(), DstVT) == TargetLowering::Promote)
433     DstVT = TLI.getTypeToTransformTo(CI->getContext(), DstVT);
434
435   // If, after promotion, these are the same types, this is a noop copy.
436   if (SrcVT != DstVT)
437     return false;
438
439   BasicBlock *DefBB = CI->getParent();
440
441   /// InsertedCasts - Only insert a cast in each block once.
442   DenseMap<BasicBlock*, CastInst*> InsertedCasts;
443
444   bool MadeChange = false;
445   for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
446        UI != E; ) {
447     Use &TheUse = UI.getUse();
448     Instruction *User = cast<Instruction>(*UI);
449
450     // Figure out which BB this cast is used in.  For PHI's this is the
451     // appropriate predecessor block.
452     BasicBlock *UserBB = User->getParent();
453     if (PHINode *PN = dyn_cast<PHINode>(User)) {
454       UserBB = PN->getIncomingBlock(UI);
455     }
456
457     // Preincrement use iterator so we don't invalidate it.
458     ++UI;
459
460     // If this user is in the same block as the cast, don't change the cast.
461     if (UserBB == DefBB) continue;
462
463     // If we have already inserted a cast into this block, use it.
464     CastInst *&InsertedCast = InsertedCasts[UserBB];
465
466     if (!InsertedCast) {
467       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
468
469       InsertedCast =
470         CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
471                          InsertPt);
472       MadeChange = true;
473     }
474
475     // Replace a use of the cast with a use of the new cast.
476     TheUse = InsertedCast;
477   }
478
479   // If we removed all uses, nuke the cast.
480   if (CI->use_empty()) {
481     CI->eraseFromParent();
482     MadeChange = true;
483   }
484
485   return MadeChange;
486 }
487
488 /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce
489 /// the number of virtual registers that must be created and coalesced.  This is
490 /// a clear win except on targets with multiple condition code registers
491 ///  (PowerPC), where it might lose; some adjustment may be wanted there.
492 ///
493 /// Return true if any changes are made.
494 static bool OptimizeCmpExpression(CmpInst *CI) {
495   BasicBlock *DefBB = CI->getParent();
496
497   /// InsertedCmp - Only insert a cmp in each block once.
498   DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
499
500   bool MadeChange = false;
501   for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
502        UI != E; ) {
503     Use &TheUse = UI.getUse();
504     Instruction *User = cast<Instruction>(*UI);
505
506     // Preincrement use iterator so we don't invalidate it.
507     ++UI;
508
509     // Don't bother for PHI nodes.
510     if (isa<PHINode>(User))
511       continue;
512
513     // Figure out which BB this cmp is used in.
514     BasicBlock *UserBB = User->getParent();
515
516     // If this user is in the same block as the cmp, don't change the cmp.
517     if (UserBB == DefBB) continue;
518
519     // If we have already inserted a cmp into this block, use it.
520     CmpInst *&InsertedCmp = InsertedCmps[UserBB];
521
522     if (!InsertedCmp) {
523       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
524
525       InsertedCmp =
526         CmpInst::Create(CI->getOpcode(),
527                         CI->getPredicate(),  CI->getOperand(0),
528                         CI->getOperand(1), "", InsertPt);
529       MadeChange = true;
530     }
531
532     // Replace a use of the cmp with a use of the new cmp.
533     TheUse = InsertedCmp;
534   }
535
536   // If we removed all uses, nuke the cmp.
537   if (CI->use_empty())
538     CI->eraseFromParent();
539
540   return MadeChange;
541 }
542
543 bool CodeGenPrepare::OptimizeCallInst(CallInst *CI) {
544   bool MadeChange = false;
545   
546   // Lower all uses of llvm.objectsize.*
547   IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
548   if (II && II->getIntrinsicID() == Intrinsic::objectsize) {
549     bool Min = (cast<ConstantInt>(II->getOperand(2))->getZExtValue() == 1);
550     const Type *ReturnTy = CI->getType();
551     Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);    
552     CI->replaceAllUsesWith(RetVal);
553     CI->eraseFromParent();
554     return true;
555   }
556
557   // From here on out we're working with named functions.
558   if (CI->getCalledFunction() == 0) return false;
559   
560   // We'll need TargetData from here on out.
561   const TargetData *TD = TLI ? TLI->getTargetData() : 0;
562   if (!TD) return false;
563   
564   // Lower all default uses of _chk calls.  This is code very similar
565   // to the code in InstCombineCalls, but here we are only lowering calls
566   // that have the default "don't know" as the objectsize.  Anything else
567   // should be left alone.
568   StringRef Name = CI->getCalledFunction()->getName();
569   BasicBlock *BB = CI->getParent();
570   IRBuilder<> B(CI->getParent()->getContext());
571   
572   // Set the builder to the instruction after the call.
573   B.SetInsertPoint(BB, CI);
574
575   if (Name == "__memcpy_chk") {
576     ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
577     if (!SizeCI)
578       return 0;
579     if (SizeCI->isAllOnesValue()) {
580       EmitMemCpy(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
581                  1, B, TD);
582       CI->replaceAllUsesWith(CI->getOperand(1));
583       CI->eraseFromParent();
584       return true;
585     }
586     return 0;
587   }
588
589   // Should be similar to memcpy.
590   if (Name == "__mempcpy_chk") {
591     return 0;
592   }
593
594   if (Name == "__memmove_chk") {
595     ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
596     if (!SizeCI)
597       return 0;
598     if (SizeCI->isAllOnesValue()) {
599       EmitMemMove(CI->getOperand(1), CI->getOperand(2), CI->getOperand(3),
600                   1, B, TD);
601       CI->replaceAllUsesWith(CI->getOperand(1));
602       CI->eraseFromParent();
603       return true;
604     }
605     return 0;
606   }
607
608   if (Name == "__memset_chk") {
609     ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
610     if (!SizeCI)
611       return 0;
612     if (SizeCI->isAllOnesValue()) {
613       Value *Val = B.CreateIntCast(CI->getOperand(2), B.getInt8Ty(),
614                                    false);
615       EmitMemSet(CI->getOperand(1), Val,  CI->getOperand(3), B, TD);
616       CI->replaceAllUsesWith(CI->getOperand(1));
617       CI->eraseFromParent();
618       return true;
619     }
620     return 0;
621   }
622
623   if (Name == "__strcpy_chk" || Name == "__stpcpy_chk") {
624     ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(3));
625     if (!SizeCI)
626       return 0;
627     if (SizeCI->isAllOnesValue()) {
628       Value *Ret = EmitStrCpy(CI->getOperand(1), CI->getOperand(2), B, TD,
629                               Name.substr(2, 6));
630       CI->replaceAllUsesWith(Ret);
631       CI->eraseFromParent();
632       return true;
633     }
634     return 0;
635   }
636
637   if (Name == "__strncpy_chk") {
638     ConstantInt *SizeCI = dyn_cast<ConstantInt>(CI->getOperand(4));
639     if (!SizeCI)
640       return 0;
641     if (SizeCI->isAllOnesValue()) {
642       Value *Ret = EmitStrNCpy(CI->getOperand(1), CI->getOperand(2),
643                                CI->getOperand(3), B, TD);
644       CI->replaceAllUsesWith(Ret);
645       CI->eraseFromParent();
646       return true;
647     }
648     return 0; 
649   }
650
651   if (Name == "__strcat_chk") {
652     return 0;
653   }
654
655   if (Name == "__strncat_chk") {
656     return 0;
657   }
658   
659   return MadeChange;
660 }
661 //===----------------------------------------------------------------------===//
662 // Memory Optimization
663 //===----------------------------------------------------------------------===//
664
665 /// IsNonLocalValue - Return true if the specified values are defined in a
666 /// different basic block than BB.
667 static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
668   if (Instruction *I = dyn_cast<Instruction>(V))
669     return I->getParent() != BB;
670   return false;
671 }
672
673 /// OptimizeMemoryInst - Load and Store Instructions often have
674 /// addressing modes that can do significant amounts of computation.  As such,
675 /// instruction selection will try to get the load or store to do as much
676 /// computation as possible for the program.  The problem is that isel can only
677 /// see within a single block.  As such, we sink as much legal addressing mode
678 /// stuff into the block as possible.
679 ///
680 /// This method is used to optimize both load/store and inline asms with memory
681 /// operands.
682 bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
683                                         const Type *AccessTy,
684                                         DenseMap<Value*,Value*> &SunkAddrs) {
685   // Figure out what addressing mode will be built up for this operation.
686   SmallVector<Instruction*, 16> AddrModeInsts;
687   ExtAddrMode AddrMode = AddressingModeMatcher::Match(Addr, AccessTy,MemoryInst,
688                                                       AddrModeInsts, *TLI);
689
690   // Check to see if any of the instructions supersumed by this addr mode are
691   // non-local to I's BB.
692   bool AnyNonLocal = false;
693   for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
694     if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
695       AnyNonLocal = true;
696       break;
697     }
698   }
699
700   // If all the instructions matched are already in this BB, don't do anything.
701   if (!AnyNonLocal) {
702     DEBUG(dbgs() << "CGP: Found      local addrmode: " << AddrMode << "\n");
703     return false;
704   }
705
706   // Insert this computation right after this user.  Since our caller is
707   // scanning from the top of the BB to the bottom, reuse of the expr are
708   // guaranteed to happen later.
709   BasicBlock::iterator InsertPt = MemoryInst;
710
711   // Now that we determined the addressing expression we want to use and know
712   // that we have to sink it into this block.  Check to see if we have already
713   // done this for some other load/store instr in this block.  If so, reuse the
714   // computation.
715   Value *&SunkAddr = SunkAddrs[Addr];
716   if (SunkAddr) {
717     DEBUG(dbgs() << "CGP: Reusing nonlocal addrmode: " << AddrMode << " for "
718                  << *MemoryInst);
719     if (SunkAddr->getType() != Addr->getType())
720       SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt);
721   } else {
722     DEBUG(dbgs() << "CGP: SINKING nonlocal addrmode: " << AddrMode << " for "
723                  << *MemoryInst);
724     const Type *IntPtrTy =
725           TLI->getTargetData()->getIntPtrType(AccessTy->getContext());
726
727     Value *Result = 0;
728
729     // Start with the base register. Do this first so that subsequent address
730     // matching finds it last, which will prevent it from trying to match it
731     // as the scaled value in case it happens to be a mul. That would be
732     // problematic if we've sunk a different mul for the scale, because then
733     // we'd end up sinking both muls.
734     if (AddrMode.BaseReg) {
735       Value *V = AddrMode.BaseReg;
736       if (V->getType()->isPointerTy())
737         V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
738       if (V->getType() != IntPtrTy)
739         V = CastInst::CreateIntegerCast(V, IntPtrTy, /*isSigned=*/true,
740                                         "sunkaddr", InsertPt);
741       Result = V;
742     }
743
744     // Add the scale value.
745     if (AddrMode.Scale) {
746       Value *V = AddrMode.ScaledReg;
747       if (V->getType() == IntPtrTy) {
748         // done.
749       } else if (V->getType()->isPointerTy()) {
750         V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
751       } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
752                  cast<IntegerType>(V->getType())->getBitWidth()) {
753         V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt);
754       } else {
755         V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt);
756       }
757       if (AddrMode.Scale != 1)
758         V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy,
759                                                                 AddrMode.Scale),
760                                       "sunkaddr", InsertPt);
761       if (Result)
762         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
763       else
764         Result = V;
765     }
766
767     // Add in the BaseGV if present.
768     if (AddrMode.BaseGV) {
769       Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr",
770                                   InsertPt);
771       if (Result)
772         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
773       else
774         Result = V;
775     }
776
777     // Add in the Base Offset if present.
778     if (AddrMode.BaseOffs) {
779       Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
780       if (Result)
781         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
782       else
783         Result = V;
784     }
785
786     if (Result == 0)
787       SunkAddr = Constant::getNullValue(Addr->getType());
788     else
789       SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt);
790   }
791
792   MemoryInst->replaceUsesOfWith(Addr, SunkAddr);
793
794   if (Addr->use_empty())
795     RecursivelyDeleteTriviallyDeadInstructions(Addr);
796   return true;
797 }
798
799 /// OptimizeInlineAsmInst - If there are any memory operands, use
800 /// OptimizeMemoryInst to sink their address computing into the block when
801 /// possible / profitable.
802 bool CodeGenPrepare::OptimizeInlineAsmInst(Instruction *I, CallSite CS,
803                                            DenseMap<Value*,Value*> &SunkAddrs) {
804   bool MadeChange = false;
805   InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
806
807   // Do a prepass over the constraints, canonicalizing them, and building up the
808   // ConstraintOperands list.
809   std::vector<InlineAsm::ConstraintInfo>
810     ConstraintInfos = IA->ParseConstraints();
811
812   /// ConstraintOperands - Information about all of the constraints.
813   std::vector<TargetLowering::AsmOperandInfo> ConstraintOperands;
814   unsigned ArgNo = 0;   // ArgNo - The argument of the CallInst.
815   for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
816     ConstraintOperands.
817       push_back(TargetLowering::AsmOperandInfo(ConstraintInfos[i]));
818     TargetLowering::AsmOperandInfo &OpInfo = ConstraintOperands.back();
819
820     // Compute the value type for each operand.
821     switch (OpInfo.Type) {
822     case InlineAsm::isOutput:
823       if (OpInfo.isIndirect)
824         OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
825       break;
826     case InlineAsm::isInput:
827       OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
828       break;
829     case InlineAsm::isClobber:
830       // Nothing to do.
831       break;
832     }
833
834     // Compute the constraint code and ConstraintType to use.
835     TLI->ComputeConstraintToUse(OpInfo, SDValue(),
836                              OpInfo.ConstraintType == TargetLowering::C_Memory);
837
838     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
839         OpInfo.isIndirect) {
840       Value *OpVal = OpInfo.CallOperandVal;
841       MadeChange |= OptimizeMemoryInst(I, OpVal, OpVal->getType(), SunkAddrs);
842     }
843   }
844
845   return MadeChange;
846 }
847
848 /// MoveExtToFormExtLoad - Move a zext or sext fed by a load into the same
849 /// basic block as the load, unless conditions are unfavorable. This allows
850 /// SelectionDAG to fold the extend into the load.
851 ///
852 bool CodeGenPrepare::MoveExtToFormExtLoad(Instruction *I) {
853   // Look for a load being extended.
854   LoadInst *LI = dyn_cast<LoadInst>(I->getOperand(0));
855   if (!LI) return false;
856
857   // If they're already in the same block, there's nothing to do.
858   if (LI->getParent() == I->getParent())
859     return false;
860
861   // If the load has other users and the truncate is not free, this probably
862   // isn't worthwhile.
863   if (!LI->hasOneUse() &&
864       TLI && !TLI->isTruncateFree(I->getType(), LI->getType()))
865     return false;
866
867   // Check whether the target supports casts folded into loads.
868   unsigned LType;
869   if (isa<ZExtInst>(I))
870     LType = ISD::ZEXTLOAD;
871   else {
872     assert(isa<SExtInst>(I) && "Unexpected ext type!");
873     LType = ISD::SEXTLOAD;
874   }
875   if (TLI && !TLI->isLoadExtLegal(LType, TLI->getValueType(LI->getType())))
876     return false;
877
878   // Move the extend into the same block as the load, so that SelectionDAG
879   // can fold it.
880   I->removeFromParent();
881   I->insertAfter(LI);
882   return true;
883 }
884
885 bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
886   BasicBlock *DefBB = I->getParent();
887
888   // If both result of the {s|z}xt and its source are live out, rewrite all
889   // other uses of the source with result of extension.
890   Value *Src = I->getOperand(0);
891   if (Src->hasOneUse())
892     return false;
893
894   // Only do this xform if truncating is free.
895   if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
896     return false;
897
898   // Only safe to perform the optimization if the source is also defined in
899   // this block.
900   if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
901     return false;
902
903   bool DefIsLiveOut = false;
904   for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
905        UI != E; ++UI) {
906     Instruction *User = cast<Instruction>(*UI);
907
908     // Figure out which BB this ext is used in.
909     BasicBlock *UserBB = User->getParent();
910     if (UserBB == DefBB) continue;
911     DefIsLiveOut = true;
912     break;
913   }
914   if (!DefIsLiveOut)
915     return false;
916
917   // Make sure non of the uses are PHI nodes.
918   for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
919        UI != E; ++UI) {
920     Instruction *User = cast<Instruction>(*UI);
921     BasicBlock *UserBB = User->getParent();
922     if (UserBB == DefBB) continue;
923     // Be conservative. We don't want this xform to end up introducing
924     // reloads just before load / store instructions.
925     if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User))
926       return false;
927   }
928
929   // InsertedTruncs - Only insert one trunc in each block once.
930   DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
931
932   bool MadeChange = false;
933   for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
934        UI != E; ++UI) {
935     Use &TheUse = UI.getUse();
936     Instruction *User = cast<Instruction>(*UI);
937
938     // Figure out which BB this ext is used in.
939     BasicBlock *UserBB = User->getParent();
940     if (UserBB == DefBB) continue;
941
942     // Both src and def are live in this block. Rewrite the use.
943     Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
944
945     if (!InsertedTrunc) {
946       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
947
948       InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
949     }
950
951     // Replace a use of the {s|z}ext source with a use of the result.
952     TheUse = InsertedTrunc;
953
954     MadeChange = true;
955   }
956
957   return MadeChange;
958 }
959
960 // In this pass we look for GEP and cast instructions that are used
961 // across basic blocks and rewrite them to improve basic-block-at-a-time
962 // selection.
963 bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) {
964   bool MadeChange = false;
965
966   // Split all critical edges where the dest block has a PHI.
967   TerminatorInst *BBTI = BB.getTerminator();
968   if (BBTI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(BBTI)) {
969     for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i) {
970       BasicBlock *SuccBB = BBTI->getSuccessor(i);
971       if (isa<PHINode>(SuccBB->begin()) && isCriticalEdge(BBTI, i, true))
972         SplitEdgeNicely(BBTI, i, BackEdges, this);
973     }
974   }
975
976   // Keep track of non-local addresses that have been sunk into this block.
977   // This allows us to avoid inserting duplicate code for blocks with multiple
978   // load/stores of the same address.
979   DenseMap<Value*, Value*> SunkAddrs;
980
981   for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E; ) {
982     Instruction *I = BBI++;
983
984     if (CastInst *CI = dyn_cast<CastInst>(I)) {
985       // If the source of the cast is a constant, then this should have
986       // already been constant folded.  The only reason NOT to constant fold
987       // it is if something (e.g. LSR) was careful to place the constant
988       // evaluation in a block other than then one that uses it (e.g. to hoist
989       // the address of globals out of a loop).  If this is the case, we don't
990       // want to forward-subst the cast.
991       if (isa<Constant>(CI->getOperand(0)))
992         continue;
993
994       bool Change = false;
995       if (TLI) {
996         Change = OptimizeNoopCopyExpression(CI, *TLI);
997         MadeChange |= Change;
998       }
999
1000       if (!Change && (isa<ZExtInst>(I) || isa<SExtInst>(I))) {
1001         MadeChange |= MoveExtToFormExtLoad(I);
1002         MadeChange |= OptimizeExtUses(I);
1003       }
1004     } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) {
1005       MadeChange |= OptimizeCmpExpression(CI);
1006     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1007       if (TLI)
1008         MadeChange |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType(),
1009                                          SunkAddrs);
1010     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1011       if (TLI)
1012         MadeChange |= OptimizeMemoryInst(I, SI->getOperand(1),
1013                                          SI->getOperand(0)->getType(),
1014                                          SunkAddrs);
1015     } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1016       if (GEPI->hasAllZeroIndices()) {
1017         /// The GEP operand must be a pointer, so must its result -> BitCast
1018         Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
1019                                           GEPI->getName(), GEPI);
1020         GEPI->replaceAllUsesWith(NC);
1021         GEPI->eraseFromParent();
1022         MadeChange = true;
1023         BBI = NC;
1024       }
1025     } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
1026       // If we found an inline asm expession, and if the target knows how to
1027       // lower it to normal LLVM code, do so now.
1028       if (TLI && isa<InlineAsm>(CI->getCalledValue())) {
1029         if (TLI->ExpandInlineAsm(CI)) {
1030           BBI = BB.begin();
1031           // Avoid processing instructions out of order, which could cause
1032           // reuse before a value is defined.
1033           SunkAddrs.clear();
1034         } else
1035           // Sink address computing for memory operands into the block.
1036           MadeChange |= OptimizeInlineAsmInst(I, &(*CI), SunkAddrs);
1037       } else {
1038         // Other CallInst optimizations that don't need to muck with the
1039         // enclosing iterator here.
1040         MadeChange |= OptimizeCallInst(CI);
1041       }
1042     }
1043   }
1044
1045   return MadeChange;
1046 }