Turn on my codegen prepare heuristic by default. It doesn't affect
[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/Pass.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetLowering.h"
27 #include "llvm/Target/TargetMachine.h"
28 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
29 #include "llvm/Transforms/Utils/Local.h"
30 #include "llvm/ADT/DenseMap.h"
31 #include "llvm/ADT/SmallSet.h"
32 #include "llvm/Support/CallSite.h"
33 #include "llvm/Support/Compiler.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/GetElementPtrTypeIterator.h"
36 #include "llvm/Support/PatternMatch.h"
37 using namespace llvm;
38 using namespace llvm::PatternMatch;
39
40 namespace {
41   class VISIBILITY_HIDDEN CodeGenPrepare : public FunctionPass {
42     /// TLI - Keep a pointer of a TargetLowering to consult for determining
43     /// transformation profitability.
44     const TargetLowering *TLI;
45   public:
46     static char ID; // Pass identification, replacement for typeid
47     explicit CodeGenPrepare(const TargetLowering *tli = 0)
48       : FunctionPass(&ID), TLI(tli) {}
49     bool runOnFunction(Function &F);
50
51   private:
52     bool EliminateMostlyEmptyBlocks(Function &F);
53     bool CanMergeBlocks(const BasicBlock *BB, const BasicBlock *DestBB) const;
54     void EliminateMostlyEmptyBlock(BasicBlock *BB);
55     bool OptimizeBlock(BasicBlock &BB);
56     bool OptimizeMemoryInst(Instruction *I, Value *Addr, const Type *AccessTy,
57                             DenseMap<Value*,Value*> &SunkAddrs);
58     bool OptimizeInlineAsmInst(Instruction *I, CallSite CS,
59                                DenseMap<Value*,Value*> &SunkAddrs);
60     bool OptimizeExtUses(Instruction *I);
61   };
62 }
63
64 char CodeGenPrepare::ID = 0;
65 static RegisterPass<CodeGenPrepare> X("codegenprepare",
66                                       "Optimize for code generation");
67
68 FunctionPass *llvm::createCodeGenPreparePass(const TargetLowering *TLI) {
69   return new CodeGenPrepare(TLI);
70 }
71
72
73 bool CodeGenPrepare::runOnFunction(Function &F) {
74   bool EverMadeChange = false;
75
76   // First pass, eliminate blocks that contain only PHI nodes and an
77   // unconditional branch.
78   EverMadeChange |= EliminateMostlyEmptyBlocks(F);
79
80   bool MadeChange = true;
81   while (MadeChange) {
82     MadeChange = false;
83     for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
84       MadeChange |= OptimizeBlock(*BB);
85     EverMadeChange |= MadeChange;
86   }
87   return EverMadeChange;
88 }
89
90 /// EliminateMostlyEmptyBlocks - eliminate blocks that contain only PHI nodes
91 /// and an unconditional branch.  Passes before isel (e.g. LSR/loopsimplify)
92 /// often split edges in ways that are non-optimal for isel.  Start by
93 /// eliminating these blocks so we can split them the way we want them.
94 bool CodeGenPrepare::EliminateMostlyEmptyBlocks(Function &F) {
95   bool MadeChange = false;
96   // Note that this intentionally skips the entry block.
97   for (Function::iterator I = ++F.begin(), E = F.end(); I != E; ) {
98     BasicBlock *BB = I++;
99
100     // If this block doesn't end with an uncond branch, ignore it.
101     BranchInst *BI = dyn_cast<BranchInst>(BB->getTerminator());
102     if (!BI || !BI->isUnconditional())
103       continue;
104
105     // If the instruction before the branch isn't a phi node, then other stuff
106     // is happening here.
107     BasicBlock::iterator BBI = BI;
108     if (BBI != BB->begin()) {
109       --BBI;
110       if (!isa<PHINode>(BBI)) continue;
111     }
112
113     // Do not break infinite loops.
114     BasicBlock *DestBB = BI->getSuccessor(0);
115     if (DestBB == BB)
116       continue;
117
118     if (!CanMergeBlocks(BB, DestBB))
119       continue;
120
121     EliminateMostlyEmptyBlock(BB);
122     MadeChange = true;
123   }
124   return MadeChange;
125 }
126
127 /// CanMergeBlocks - Return true if we can merge BB into DestBB if there is a
128 /// single uncond branch between them, and BB contains no other non-phi
129 /// instructions.
130 bool CodeGenPrepare::CanMergeBlocks(const BasicBlock *BB,
131                                     const BasicBlock *DestBB) const {
132   // We only want to eliminate blocks whose phi nodes are used by phi nodes in
133   // the successor.  If there are more complex condition (e.g. preheaders),
134   // don't mess around with them.
135   BasicBlock::const_iterator BBI = BB->begin();
136   while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
137     for (Value::use_const_iterator UI = PN->use_begin(), E = PN->use_end();
138          UI != E; ++UI) {
139       const Instruction *User = cast<Instruction>(*UI);
140       if (User->getParent() != DestBB || !isa<PHINode>(User))
141         return false;
142       // If User is inside DestBB block and it is a PHINode then check
143       // incoming value. If incoming value is not from BB then this is
144       // a complex condition (e.g. preheaders) we want to avoid here.
145       if (User->getParent() == DestBB) {
146         if (const PHINode *UPN = dyn_cast<PHINode>(User))
147           for (unsigned I = 0, E = UPN->getNumIncomingValues(); I != E; ++I) {
148             Instruction *Insn = dyn_cast<Instruction>(UPN->getIncomingValue(I));
149             if (Insn && Insn->getParent() == BB &&
150                 Insn->getParent() != UPN->getIncomingBlock(I))
151               return false;
152           }
153       }
154     }
155   }
156
157   // If BB and DestBB contain any common predecessors, then the phi nodes in BB
158   // and DestBB may have conflicting incoming values for the block.  If so, we
159   // can't merge the block.
160   const PHINode *DestBBPN = dyn_cast<PHINode>(DestBB->begin());
161   if (!DestBBPN) return true;  // no conflict.
162
163   // Collect the preds of BB.
164   SmallPtrSet<const BasicBlock*, 16> BBPreds;
165   if (const PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
166     // It is faster to get preds from a PHI than with pred_iterator.
167     for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
168       BBPreds.insert(BBPN->getIncomingBlock(i));
169   } else {
170     BBPreds.insert(pred_begin(BB), pred_end(BB));
171   }
172
173   // Walk the preds of DestBB.
174   for (unsigned i = 0, e = DestBBPN->getNumIncomingValues(); i != e; ++i) {
175     BasicBlock *Pred = DestBBPN->getIncomingBlock(i);
176     if (BBPreds.count(Pred)) {   // Common predecessor?
177       BBI = DestBB->begin();
178       while (const PHINode *PN = dyn_cast<PHINode>(BBI++)) {
179         const Value *V1 = PN->getIncomingValueForBlock(Pred);
180         const Value *V2 = PN->getIncomingValueForBlock(BB);
181
182         // If V2 is a phi node in BB, look up what the mapped value will be.
183         if (const PHINode *V2PN = dyn_cast<PHINode>(V2))
184           if (V2PN->getParent() == BB)
185             V2 = V2PN->getIncomingValueForBlock(Pred);
186
187         // If there is a conflict, bail out.
188         if (V1 != V2) return false;
189       }
190     }
191   }
192
193   return true;
194 }
195
196
197 /// EliminateMostlyEmptyBlock - Eliminate a basic block that have only phi's and
198 /// an unconditional branch in it.
199 void CodeGenPrepare::EliminateMostlyEmptyBlock(BasicBlock *BB) {
200   BranchInst *BI = cast<BranchInst>(BB->getTerminator());
201   BasicBlock *DestBB = BI->getSuccessor(0);
202
203   DOUT << "MERGING MOSTLY EMPTY BLOCKS - BEFORE:\n" << *BB << *DestBB;
204
205   // If the destination block has a single pred, then this is a trivial edge,
206   // just collapse it.
207   if (DestBB->getSinglePredecessor()) {
208     // If DestBB has single-entry PHI nodes, fold them.
209     while (PHINode *PN = dyn_cast<PHINode>(DestBB->begin())) {
210       Value *NewVal = PN->getIncomingValue(0);
211       // Replace self referencing PHI with undef, it must be dead.
212       if (NewVal == PN) NewVal = UndefValue::get(PN->getType());
213       PN->replaceAllUsesWith(NewVal);
214       PN->eraseFromParent();
215     }
216
217     // Splice all the PHI nodes from BB over to DestBB.
218     DestBB->getInstList().splice(DestBB->begin(), BB->getInstList(),
219                                  BB->begin(), BI);
220
221     // Anything that branched to BB now branches to DestBB.
222     BB->replaceAllUsesWith(DestBB);
223
224     // Nuke BB.
225     BB->eraseFromParent();
226
227     DOUT << "AFTER:\n" << *DestBB << "\n\n\n";
228     return;
229   }
230
231   // Otherwise, we have multiple predecessors of BB.  Update the PHIs in DestBB
232   // to handle the new incoming edges it is about to have.
233   PHINode *PN;
234   for (BasicBlock::iterator BBI = DestBB->begin();
235        (PN = dyn_cast<PHINode>(BBI)); ++BBI) {
236     // Remove the incoming value for BB, and remember it.
237     Value *InVal = PN->removeIncomingValue(BB, false);
238
239     // Two options: either the InVal is a phi node defined in BB or it is some
240     // value that dominates BB.
241     PHINode *InValPhi = dyn_cast<PHINode>(InVal);
242     if (InValPhi && InValPhi->getParent() == BB) {
243       // Add all of the input values of the input PHI as inputs of this phi.
244       for (unsigned i = 0, e = InValPhi->getNumIncomingValues(); i != e; ++i)
245         PN->addIncoming(InValPhi->getIncomingValue(i),
246                         InValPhi->getIncomingBlock(i));
247     } else {
248       // Otherwise, add one instance of the dominating value for each edge that
249       // we will be adding.
250       if (PHINode *BBPN = dyn_cast<PHINode>(BB->begin())) {
251         for (unsigned i = 0, e = BBPN->getNumIncomingValues(); i != e; ++i)
252           PN->addIncoming(InVal, BBPN->getIncomingBlock(i));
253       } else {
254         for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI)
255           PN->addIncoming(InVal, *PI);
256       }
257     }
258   }
259
260   // The PHIs are now updated, change everything that refers to BB to use
261   // DestBB and remove BB.
262   BB->replaceAllUsesWith(DestBB);
263   BB->eraseFromParent();
264
265   DOUT << "AFTER:\n" << *DestBB << "\n\n\n";
266 }
267
268
269 /// SplitEdgeNicely - Split the critical edge from TI to its specified
270 /// successor if it will improve codegen.  We only do this if the successor has
271 /// phi nodes (otherwise critical edges are ok).  If there is already another
272 /// predecessor of the succ that is empty (and thus has no phi nodes), use it
273 /// instead of introducing a new block.
274 static void SplitEdgeNicely(TerminatorInst *TI, unsigned SuccNum, Pass *P) {
275   BasicBlock *TIBB = TI->getParent();
276   BasicBlock *Dest = TI->getSuccessor(SuccNum);
277   assert(isa<PHINode>(Dest->begin()) &&
278          "This should only be called if Dest has a PHI!");
279
280   // As a hack, never split backedges of loops.  Even though the copy for any
281   // PHIs inserted on the backedge would be dead for exits from the loop, we
282   // assume that the cost of *splitting* the backedge would be too high.
283   if (Dest == TIBB)
284     return;
285
286   /// TIPHIValues - This array is lazily computed to determine the values of
287   /// PHIs in Dest that TI would provide.
288   SmallVector<Value*, 32> TIPHIValues;
289
290   // Check to see if Dest has any blocks that can be used as a split edge for
291   // this terminator.
292   for (pred_iterator PI = pred_begin(Dest), E = pred_end(Dest); PI != E; ++PI) {
293     BasicBlock *Pred = *PI;
294     // To be usable, the pred has to end with an uncond branch to the dest.
295     BranchInst *PredBr = dyn_cast<BranchInst>(Pred->getTerminator());
296     if (!PredBr || !PredBr->isUnconditional() ||
297         // Must be empty other than the branch.
298         &Pred->front() != PredBr ||
299         // Cannot be the entry block; its label does not get emitted.
300         Pred == &(Dest->getParent()->getEntryBlock()))
301       continue;
302
303     // Finally, since we know that Dest has phi nodes in it, we have to make
304     // sure that jumping to Pred will have the same affect as going to Dest in
305     // terms of PHI values.
306     PHINode *PN;
307     unsigned PHINo = 0;
308     bool FoundMatch = true;
309     for (BasicBlock::iterator I = Dest->begin();
310          (PN = dyn_cast<PHINode>(I)); ++I, ++PHINo) {
311       if (PHINo == TIPHIValues.size())
312         TIPHIValues.push_back(PN->getIncomingValueForBlock(TIBB));
313
314       // If the PHI entry doesn't work, we can't use this pred.
315       if (TIPHIValues[PHINo] != PN->getIncomingValueForBlock(Pred)) {
316         FoundMatch = false;
317         break;
318       }
319     }
320
321     // If we found a workable predecessor, change TI to branch to Succ.
322     if (FoundMatch) {
323       Dest->removePredecessor(TIBB);
324       TI->setSuccessor(SuccNum, Pred);
325       return;
326     }
327   }
328
329   SplitCriticalEdge(TI, SuccNum, P, true);
330 }
331
332 /// OptimizeNoopCopyExpression - If the specified cast instruction is a noop
333 /// copy (e.g. it's casting from one pointer type to another, int->uint, or
334 /// int->sbyte on PPC), sink it into user blocks to reduce the number of virtual
335 /// registers that must be created and coalesced.
336 ///
337 /// Return true if any changes are made.
338 ///
339 static bool OptimizeNoopCopyExpression(CastInst *CI, const TargetLowering &TLI){
340   // If this is a noop copy,
341   MVT SrcVT = TLI.getValueType(CI->getOperand(0)->getType());
342   MVT DstVT = TLI.getValueType(CI->getType());
343
344   // This is an fp<->int conversion?
345   if (SrcVT.isInteger() != DstVT.isInteger())
346     return false;
347
348   // If this is an extension, it will be a zero or sign extension, which
349   // isn't a noop.
350   if (SrcVT.bitsLT(DstVT)) return false;
351
352   // If these values will be promoted, find out what they will be promoted
353   // to.  This helps us consider truncates on PPC as noop copies when they
354   // are.
355   if (TLI.getTypeAction(SrcVT) == TargetLowering::Promote)
356     SrcVT = TLI.getTypeToTransformTo(SrcVT);
357   if (TLI.getTypeAction(DstVT) == TargetLowering::Promote)
358     DstVT = TLI.getTypeToTransformTo(DstVT);
359
360   // If, after promotion, these are the same types, this is a noop copy.
361   if (SrcVT != DstVT)
362     return false;
363
364   BasicBlock *DefBB = CI->getParent();
365
366   /// InsertedCasts - Only insert a cast in each block once.
367   DenseMap<BasicBlock*, CastInst*> InsertedCasts;
368
369   bool MadeChange = false;
370   for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
371        UI != E; ) {
372     Use &TheUse = UI.getUse();
373     Instruction *User = cast<Instruction>(*UI);
374
375     // Figure out which BB this cast is used in.  For PHI's this is the
376     // appropriate predecessor block.
377     BasicBlock *UserBB = User->getParent();
378     if (PHINode *PN = dyn_cast<PHINode>(User)) {
379       unsigned OpVal = UI.getOperandNo()/2;
380       UserBB = PN->getIncomingBlock(OpVal);
381     }
382
383     // Preincrement use iterator so we don't invalidate it.
384     ++UI;
385
386     // If this user is in the same block as the cast, don't change the cast.
387     if (UserBB == DefBB) continue;
388
389     // If we have already inserted a cast into this block, use it.
390     CastInst *&InsertedCast = InsertedCasts[UserBB];
391
392     if (!InsertedCast) {
393       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
394
395       InsertedCast =
396         CastInst::Create(CI->getOpcode(), CI->getOperand(0), CI->getType(), "",
397                          InsertPt);
398       MadeChange = true;
399     }
400
401     // Replace a use of the cast with a use of the new cast.
402     TheUse = InsertedCast;
403   }
404
405   // If we removed all uses, nuke the cast.
406   if (CI->use_empty()) {
407     CI->eraseFromParent();
408     MadeChange = true;
409   }
410
411   return MadeChange;
412 }
413
414 /// OptimizeCmpExpression - sink the given CmpInst into user blocks to reduce
415 /// the number of virtual registers that must be created and coalesced.  This is
416 /// a clear win except on targets with multiple condition code registers
417 ///  (PowerPC), where it might lose; some adjustment may be wanted there.
418 ///
419 /// Return true if any changes are made.
420 static bool OptimizeCmpExpression(CmpInst *CI) {
421   BasicBlock *DefBB = CI->getParent();
422
423   /// InsertedCmp - Only insert a cmp in each block once.
424   DenseMap<BasicBlock*, CmpInst*> InsertedCmps;
425
426   bool MadeChange = false;
427   for (Value::use_iterator UI = CI->use_begin(), E = CI->use_end();
428        UI != E; ) {
429     Use &TheUse = UI.getUse();
430     Instruction *User = cast<Instruction>(*UI);
431
432     // Preincrement use iterator so we don't invalidate it.
433     ++UI;
434
435     // Don't bother for PHI nodes.
436     if (isa<PHINode>(User))
437       continue;
438
439     // Figure out which BB this cmp is used in.
440     BasicBlock *UserBB = User->getParent();
441
442     // If this user is in the same block as the cmp, don't change the cmp.
443     if (UserBB == DefBB) continue;
444
445     // If we have already inserted a cmp into this block, use it.
446     CmpInst *&InsertedCmp = InsertedCmps[UserBB];
447
448     if (!InsertedCmp) {
449       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
450
451       InsertedCmp =
452         CmpInst::Create(CI->getOpcode(), CI->getPredicate(), CI->getOperand(0),
453                         CI->getOperand(1), "", InsertPt);
454       MadeChange = true;
455     }
456
457     // Replace a use of the cmp with a use of the new cmp.
458     TheUse = InsertedCmp;
459   }
460
461   // If we removed all uses, nuke the cmp.
462   if (CI->use_empty())
463     CI->eraseFromParent();
464
465   return MadeChange;
466 }
467
468 /// EraseDeadInstructions - Erase any dead instructions, recursively.
469 static void EraseDeadInstructions(Value *V) {
470   Instruction *I = dyn_cast<Instruction>(V);
471   if (!I || !I->use_empty()) return;
472
473   SmallPtrSet<Instruction*, 16> Insts;
474   Insts.insert(I);
475
476   while (!Insts.empty()) {
477     I = *Insts.begin();
478     Insts.erase(I);
479     if (isInstructionTriviallyDead(I)) {
480       for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
481         if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
482           Insts.insert(U);
483       I->eraseFromParent();
484     }
485   }
486 }
487
488 //===----------------------------------------------------------------------===//
489 // Addressing Mode Analysis and Optimization
490 //===----------------------------------------------------------------------===//
491
492 namespace {
493   /// ExtAddrMode - This is an extended version of TargetLowering::AddrMode
494   /// which holds actual Value*'s for register values.
495   struct ExtAddrMode : public TargetLowering::AddrMode {
496     Value *BaseReg;
497     Value *ScaledReg;
498     ExtAddrMode() : BaseReg(0), ScaledReg(0) {}
499     void print(OStream &OS) const;
500     void dump() const {
501       print(cerr);
502       cerr << '\n';
503     }
504   };
505 } // end anonymous namespace
506
507 static inline OStream &operator<<(OStream &OS, const ExtAddrMode &AM) {
508   AM.print(OS);
509   return OS;
510 }
511
512 void ExtAddrMode::print(OStream &OS) const {
513   bool NeedPlus = false;
514   OS << "[";
515   if (BaseGV)
516     OS << (NeedPlus ? " + " : "")
517        << "GV:%" << BaseGV->getName(), NeedPlus = true;
518
519   if (BaseOffs)
520     OS << (NeedPlus ? " + " : "") << BaseOffs, NeedPlus = true;
521
522   if (BaseReg)
523     OS << (NeedPlus ? " + " : "")
524        << "Base:%" << BaseReg->getName(), NeedPlus = true;
525   if (Scale)
526     OS << (NeedPlus ? " + " : "")
527        << Scale << "*%" << ScaledReg->getName(), NeedPlus = true;
528
529   OS << ']';
530 }
531
532 namespace {
533 /// AddressingModeMatcher - This class exposes a single public method, which is
534 /// used to construct a "maximal munch" of the addressing mode for the target
535 /// specified by TLI for an access to "V" with an access type of AccessTy.  This
536 /// returns the addressing mode that is actually matched by value, but also
537 /// returns the list of instructions involved in that addressing computation in
538 /// AddrModeInsts.
539 class AddressingModeMatcher {
540   SmallVectorImpl<Instruction*> &AddrModeInsts;
541   const TargetLowering &TLI;
542
543   /// AccessTy/MemoryInst - This is the type for the access (e.g. double) and
544   /// the memory instruction that we're computing this address for.
545   const Type *AccessTy;
546   Instruction *MemoryInst;
547   
548   /// AddrMode - This is the addressing mode that we're building up.  This is
549   /// part of the return value of this addressing mode matching stuff.
550   ExtAddrMode &AddrMode;
551   
552   /// IgnoreProfitability - This is set to true when we should not do
553   /// profitability checks.  When true, IsProfitableToFoldIntoAddressingMode
554   /// always returns true.
555   bool IgnoreProfitability;
556   
557   AddressingModeMatcher(SmallVectorImpl<Instruction*> &AMI,
558                         const TargetLowering &T, const Type *AT,
559                         Instruction *MI, ExtAddrMode &AM)
560     : AddrModeInsts(AMI), TLI(T), AccessTy(AT), MemoryInst(MI), AddrMode(AM) {
561     IgnoreProfitability = false;
562   }
563 public:
564   
565   /// Match - Find the maximal addressing mode that a load/store of V can fold,
566   /// give an access type of AccessTy.  This returns a list of involved
567   /// instructions in AddrModeInsts.
568   static ExtAddrMode Match(Value *V, const Type *AccessTy,
569                            Instruction *MemoryInst,
570                            SmallVectorImpl<Instruction*> &AddrModeInsts,
571                            const TargetLowering &TLI) {
572     ExtAddrMode Result;
573
574     bool Success = 
575       AddressingModeMatcher(AddrModeInsts, TLI, AccessTy,
576                             MemoryInst, Result).MatchAddr(V, 0);
577     Success = Success; assert(Success && "Couldn't select *anything*?");
578     return Result;
579   }
580 private:
581   bool MatchScaledValue(Value *ScaleReg, int64_t Scale, unsigned Depth);
582   bool MatchAddr(Value *V, unsigned Depth);
583   bool MatchOperationAddr(User *Operation, unsigned Opcode, unsigned Depth);
584   bool IsProfitableToFoldIntoAddressingMode(Instruction *I,
585                                             ExtAddrMode &AMBefore,
586                                             ExtAddrMode &AMAfter);
587   bool ValueAlreadyLiveAtInst(Value *Val, Value *KnownLive1, Value *KnownLive2);
588 };
589 } // end anonymous namespace
590
591 /// MatchScaledValue - Try adding ScaleReg*Scale to the current addressing mode.
592 /// Return true and update AddrMode if this addr mode is legal for the target,
593 /// false if not.
594 bool AddressingModeMatcher::MatchScaledValue(Value *ScaleReg, int64_t Scale,
595                                              unsigned Depth) {
596   // If Scale is 1, then this is the same as adding ScaleReg to the addressing
597   // mode.  Just process that directly.
598   if (Scale == 1)
599     return MatchAddr(ScaleReg, Depth);
600   
601   // If the scale is 0, it takes nothing to add this.
602   if (Scale == 0)
603     return true;
604   
605   // If we already have a scale of this value, we can add to it, otherwise, we
606   // need an available scale field.
607   if (AddrMode.Scale != 0 && AddrMode.ScaledReg != ScaleReg)
608     return false;
609
610   ExtAddrMode TestAddrMode = AddrMode;
611
612   // Add scale to turn X*4+X*3 -> X*7.  This could also do things like
613   // [A+B + A*7] -> [B+A*8].
614   TestAddrMode.Scale += Scale;
615   TestAddrMode.ScaledReg = ScaleReg;
616
617   // If the new address isn't legal, bail out.
618   if (!TLI.isLegalAddressingMode(TestAddrMode, AccessTy))
619     return false;
620
621   // It was legal, so commit it.
622   AddrMode = TestAddrMode;
623   
624   // Okay, we decided that we can add ScaleReg+Scale to AddrMode.  Check now
625   // to see if ScaleReg is actually X+C.  If so, we can turn this into adding
626   // X*Scale + C*Scale to addr mode.
627   ConstantInt *CI; Value *AddLHS;
628   if (match(ScaleReg, m_Add(m_Value(AddLHS), m_ConstantInt(CI)))) {
629     TestAddrMode.ScaledReg = AddLHS;
630     TestAddrMode.BaseOffs += CI->getSExtValue()*TestAddrMode.Scale;
631       
632     // If this addressing mode is legal, commit it and remember that we folded
633     // this instruction.
634     if (TLI.isLegalAddressingMode(TestAddrMode, AccessTy)) {
635       AddrModeInsts.push_back(cast<Instruction>(ScaleReg));
636       AddrMode = TestAddrMode;
637       return true;
638     }
639   }
640
641   // Otherwise, not (x+c)*scale, just return what we have.
642   return true;
643 }
644
645 /// MightBeFoldableInst - This is a little filter, which returns true if an
646 /// addressing computation involving I might be folded into a load/store
647 /// accessing it.  This doesn't need to be perfect, but needs to accept at least
648 /// the set of instructions that MatchOperationAddr can.
649 static bool MightBeFoldableInst(Instruction *I) {
650   switch (I->getOpcode()) {
651   case Instruction::BitCast:
652     // Don't touch identity bitcasts.
653     if (I->getType() == I->getOperand(0)->getType())
654       return false;
655     return isa<PointerType>(I->getType()) || isa<IntegerType>(I->getType());
656   case Instruction::PtrToInt:
657     // PtrToInt is always a noop, as we know that the int type is pointer sized.
658     return true;
659   case Instruction::IntToPtr:
660     // We know the input is intptr_t, so this is foldable.
661     return true;
662   case Instruction::Add:
663     return true;
664   case Instruction::Mul:
665   case Instruction::Shl:
666     // Can only handle X*C and X << C.
667     return isa<ConstantInt>(I->getOperand(1));
668   case Instruction::GetElementPtr:
669     return true;
670   default:
671     return false;
672   }
673 }
674
675
676 /// MatchOperationAddr - Given an instruction or constant expr, see if we can
677 /// fold the operation into the addressing mode.  If so, update the addressing
678 /// mode and return true, otherwise return false without modifying AddrMode.
679 bool AddressingModeMatcher::MatchOperationAddr(User *AddrInst, unsigned Opcode,
680                                                unsigned Depth) {
681   // Avoid exponential behavior on extremely deep expression trees.
682   if (Depth >= 5) return false;
683   
684   switch (Opcode) {
685   case Instruction::PtrToInt:
686     // PtrToInt is always a noop, as we know that the int type is pointer sized.
687     return MatchAddr(AddrInst->getOperand(0), Depth);
688   case Instruction::IntToPtr:
689     // This inttoptr is a no-op if the integer type is pointer sized.
690     if (TLI.getValueType(AddrInst->getOperand(0)->getType()) ==
691         TLI.getPointerTy())
692       return MatchAddr(AddrInst->getOperand(0), Depth);
693     return false;
694   case Instruction::BitCast:
695     // BitCast is always a noop, and we can handle it as long as it is
696     // int->int or pointer->pointer (we don't want int<->fp or something).
697     if ((isa<PointerType>(AddrInst->getOperand(0)->getType()) ||
698          isa<IntegerType>(AddrInst->getOperand(0)->getType())) &&
699         // Don't touch identity bitcasts.  These were probably put here by LSR,
700         // and we don't want to mess around with them.  Assume it knows what it
701         // is doing.
702         AddrInst->getOperand(0)->getType() != AddrInst->getType())
703       return MatchAddr(AddrInst->getOperand(0), Depth);
704     return false;
705   case Instruction::Add: {
706     // Check to see if we can merge in the RHS then the LHS.  If so, we win.
707     ExtAddrMode BackupAddrMode = AddrMode;
708     unsigned OldSize = AddrModeInsts.size();
709     if (MatchAddr(AddrInst->getOperand(1), Depth+1) &&
710         MatchAddr(AddrInst->getOperand(0), Depth+1))
711       return true;
712     
713     // Restore the old addr mode info.
714     AddrMode = BackupAddrMode;
715     AddrModeInsts.resize(OldSize);
716     
717     // Otherwise this was over-aggressive.  Try merging in the LHS then the RHS.
718     if (MatchAddr(AddrInst->getOperand(0), Depth+1) &&
719         MatchAddr(AddrInst->getOperand(1), Depth+1))
720       return true;
721     
722     // Otherwise we definitely can't merge the ADD in.
723     AddrMode = BackupAddrMode;
724     AddrModeInsts.resize(OldSize);
725     break;
726   }
727   //case Instruction::Or:
728   // TODO: We can handle "Or Val, Imm" iff this OR is equivalent to an ADD.
729   //break;
730   case Instruction::Mul:
731   case Instruction::Shl: {
732     // Can only handle X*C and X << C.
733     ConstantInt *RHS = dyn_cast<ConstantInt>(AddrInst->getOperand(1));
734     if (!RHS) return false;
735     int64_t Scale = RHS->getSExtValue();
736     if (Opcode == Instruction::Shl)
737       Scale = 1 << Scale;
738     
739     return MatchScaledValue(AddrInst->getOperand(0), Scale, Depth);
740   }
741   case Instruction::GetElementPtr: {
742     // Scan the GEP.  We check it if it contains constant offsets and at most
743     // one variable offset.
744     int VariableOperand = -1;
745     unsigned VariableScale = 0;
746     
747     int64_t ConstantOffset = 0;
748     const TargetData *TD = TLI.getTargetData();
749     gep_type_iterator GTI = gep_type_begin(AddrInst);
750     for (unsigned i = 1, e = AddrInst->getNumOperands(); i != e; ++i, ++GTI) {
751       if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
752         const StructLayout *SL = TD->getStructLayout(STy);
753         unsigned Idx =
754           cast<ConstantInt>(AddrInst->getOperand(i))->getZExtValue();
755         ConstantOffset += SL->getElementOffset(Idx);
756       } else {
757         uint64_t TypeSize = TD->getABITypeSize(GTI.getIndexedType());
758         if (ConstantInt *CI = dyn_cast<ConstantInt>(AddrInst->getOperand(i))) {
759           ConstantOffset += CI->getSExtValue()*TypeSize;
760         } else if (TypeSize) {  // Scales of zero don't do anything.
761           // We only allow one variable index at the moment.
762           if (VariableOperand != -1)
763             return false;
764           
765           // Remember the variable index.
766           VariableOperand = i;
767           VariableScale = TypeSize;
768         }
769       }
770     }
771     
772     // A common case is for the GEP to only do a constant offset.  In this case,
773     // just add it to the disp field and check validity.
774     if (VariableOperand == -1) {
775       AddrMode.BaseOffs += ConstantOffset;
776       if (ConstantOffset == 0 || TLI.isLegalAddressingMode(AddrMode, AccessTy)){
777         // Check to see if we can fold the base pointer in too.
778         if (MatchAddr(AddrInst->getOperand(0), Depth+1))
779           return true;
780       }
781       AddrMode.BaseOffs -= ConstantOffset;
782       return false;
783     }
784
785     // Save the valid addressing mode in case we can't match.
786     ExtAddrMode BackupAddrMode = AddrMode;
787     
788     // Check that this has no base reg yet.  If so, we won't have a place to
789     // put the base of the GEP (assuming it is not a null ptr).
790     bool SetBaseReg = true;
791     if (isa<ConstantPointerNull>(AddrInst->getOperand(0)))
792       SetBaseReg = false;   // null pointer base doesn't need representation.
793     else if (AddrMode.HasBaseReg)
794       return false;  // Base register already specified, can't match GEP.
795     else {
796       // Otherwise, we'll use the GEP base as the BaseReg.
797       AddrMode.HasBaseReg = true;
798       AddrMode.BaseReg = AddrInst->getOperand(0);
799     }
800     
801     // See if the scale and offset amount is valid for this target.
802     AddrMode.BaseOffs += ConstantOffset;
803     
804     if (!MatchScaledValue(AddrInst->getOperand(VariableOperand), VariableScale,
805                           Depth)) {
806       AddrMode = BackupAddrMode;
807       return false;
808     }
809     
810     // If we have a null as the base of the GEP, folding in the constant offset
811     // plus variable scale is all we can do.
812     if (!SetBaseReg) return true;
813       
814     // If this match succeeded, we know that we can form an address with the
815     // GepBase as the basereg.  Match the base pointer of the GEP more
816     // aggressively by zeroing out BaseReg and rematching.  If the base is
817     // (for example) another GEP, this allows merging in that other GEP into
818     // the addressing mode we're forming.
819     AddrMode.HasBaseReg = false;
820     AddrMode.BaseReg = 0;
821     bool Success = MatchAddr(AddrInst->getOperand(0), Depth+1);
822     assert(Success && "MatchAddr should be able to fill in BaseReg!");
823     Success=Success;
824     return true;
825   }
826   }
827   return false;
828 }
829
830 /// MatchAddr - If we can, try to add the value of 'Addr' into the current
831 /// addressing mode.  If Addr can't be added to AddrMode this returns false and
832 /// leaves AddrMode unmodified.  This assumes that Addr is either a pointer type
833 /// or intptr_t for the target.
834 ///
835 bool AddressingModeMatcher::MatchAddr(Value *Addr, unsigned Depth) {
836   if (ConstantInt *CI = dyn_cast<ConstantInt>(Addr)) {
837     // Fold in immediates if legal for the target.
838     AddrMode.BaseOffs += CI->getSExtValue();
839     if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
840       return true;
841     AddrMode.BaseOffs -= CI->getSExtValue();
842   } else if (GlobalValue *GV = dyn_cast<GlobalValue>(Addr)) {
843     // If this is a global variable, try to fold it into the addressing mode.
844     if (AddrMode.BaseGV == 0) {
845       AddrMode.BaseGV = GV;
846       if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
847         return true;
848       AddrMode.BaseGV = 0;
849     }
850   } else if (Instruction *I = dyn_cast<Instruction>(Addr)) {
851     ExtAddrMode BackupAddrMode = AddrMode;
852     unsigned OldSize = AddrModeInsts.size();
853
854     // Check to see if it is possible to fold this operation.
855     if (MatchOperationAddr(I, I->getOpcode(), Depth)) {
856       // Okay, it's possible to fold this.  Check to see if it is actually
857       // *profitable* to do so.  We use a simple cost model to avoid increasing
858       // register pressure too much.
859       if (I->hasOneUse() ||
860           IsProfitableToFoldIntoAddressingMode(I, BackupAddrMode, AddrMode)) {
861         AddrModeInsts.push_back(I);
862         return true;
863       }
864       
865       // It isn't profitable to do this, roll back.
866       //cerr << "NOT FOLDING: " << *I;
867       AddrMode = BackupAddrMode;
868       AddrModeInsts.resize(OldSize);
869     }
870   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(Addr)) {
871     if (MatchOperationAddr(CE, CE->getOpcode(), Depth))
872       return true;
873   } else if (isa<ConstantPointerNull>(Addr)) {
874     // Null pointer gets folded without affecting the addressing mode.
875     return true;
876   }
877
878   // Worse case, the target should support [reg] addressing modes. :)
879   if (!AddrMode.HasBaseReg) {
880     AddrMode.HasBaseReg = true;
881     AddrMode.BaseReg = Addr;
882     // Still check for legality in case the target supports [imm] but not [i+r].
883     if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
884       return true;
885     AddrMode.HasBaseReg = false;
886     AddrMode.BaseReg = 0;
887   }
888
889   // If the base register is already taken, see if we can do [r+r].
890   if (AddrMode.Scale == 0) {
891     AddrMode.Scale = 1;
892     AddrMode.ScaledReg = Addr;
893     if (TLI.isLegalAddressingMode(AddrMode, AccessTy))
894       return true;
895     AddrMode.Scale = 0;
896     AddrMode.ScaledReg = 0;
897   }
898   // Couldn't match.
899   return false;
900 }
901
902
903 /// IsOperandAMemoryOperand - Check to see if all uses of OpVal by the specified
904 /// inline asm call are due to memory operands.  If so, return true, otherwise
905 /// return false.
906 static bool IsOperandAMemoryOperand(CallInst *CI, InlineAsm *IA, Value *OpVal,
907                                     const TargetLowering &TLI) {
908   std::vector<InlineAsm::ConstraintInfo>
909   Constraints = IA->ParseConstraints();
910   
911   unsigned ArgNo = 1;   // ArgNo - The operand of the CallInst.
912   for (unsigned i = 0, e = Constraints.size(); i != e; ++i) {
913     TargetLowering::AsmOperandInfo OpInfo(Constraints[i]);
914     
915     // Compute the value type for each operand.
916     switch (OpInfo.Type) {
917       case InlineAsm::isOutput:
918         if (OpInfo.isIndirect)
919           OpInfo.CallOperandVal = CI->getOperand(ArgNo++);
920         break;
921       case InlineAsm::isInput:
922         OpInfo.CallOperandVal = CI->getOperand(ArgNo++);
923         break;
924       case InlineAsm::isClobber:
925         // Nothing to do.
926         break;
927     }
928     
929     // Compute the constraint code and ConstraintType to use.
930     TLI.ComputeConstraintToUse(OpInfo, SDValue(),
931                              OpInfo.ConstraintType == TargetLowering::C_Memory);
932     
933     // If this asm operand is our Value*, and if it isn't an indirect memory
934     // operand, we can't fold it!
935     if (OpInfo.CallOperandVal == OpVal &&
936         (OpInfo.ConstraintType != TargetLowering::C_Memory ||
937          !OpInfo.isIndirect))
938       return false;
939   }
940   
941   return true;
942 }
943
944
945 /// FindAllMemoryUses - Recursively walk all the uses of I until we find a
946 /// memory use.  If we find an obviously non-foldable instruction, return true.
947 /// Add the ultimately found memory instructions to MemoryUses.
948 static bool FindAllMemoryUses(Instruction *I,
949                 SmallVectorImpl<std::pair<Instruction*,unsigned> > &MemoryUses,
950                               SmallPtrSet<Instruction*, 16> &ConsideredInsts,
951                               const TargetLowering &TLI) {
952   // If we already considered this instruction, we're done.
953   if (!ConsideredInsts.insert(I))
954     return false;
955   
956   // If this is an obviously unfoldable instruction, bail out.
957   if (!MightBeFoldableInst(I))
958     return true;
959
960   // Loop over all the uses, recursively processing them.
961   for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
962        UI != E; ++UI) {
963     if (LoadInst *LI = dyn_cast<LoadInst>(*UI)) {
964       MemoryUses.push_back(std::make_pair(LI, UI.getOperandNo()));
965       continue;
966     }
967     
968     if (StoreInst *SI = dyn_cast<StoreInst>(*UI)) {
969       if (UI.getOperandNo() == 0) return true; // Storing addr, not into addr.
970       MemoryUses.push_back(std::make_pair(SI, UI.getOperandNo()));
971       continue;
972     }
973     
974     if (CallInst *CI = dyn_cast<CallInst>(*UI)) {
975       InlineAsm *IA = dyn_cast<InlineAsm>(CI->getCalledValue());
976       if (IA == 0) return true;
977       
978       // If this is a memory operand, we're cool, otherwise bail out.
979       if (!IsOperandAMemoryOperand(CI, IA, I, TLI))
980         return true;
981       continue;
982     }
983     
984     if (FindAllMemoryUses(cast<Instruction>(*UI), MemoryUses, ConsideredInsts,
985                           TLI))
986       return true;
987   }
988
989   return false;
990 }
991
992
993 /// ValueAlreadyLiveAtInst - Retrn true if Val is already known to be live at
994 /// the use site that we're folding it into.  If so, there is no cost to
995 /// include it in the addressing mode.  KnownLive1 and KnownLive2 are two values
996 /// that we know are live at the instruction already.
997 bool AddressingModeMatcher::ValueAlreadyLiveAtInst(Value *Val,Value *KnownLive1,
998                                                    Value *KnownLive2) {
999   // If Val is either of the known-live values, we know it is live!
1000   if (Val == 0 || Val == KnownLive1 || Val == KnownLive2)
1001     return true;
1002   
1003   // All values other than instructions and arguments (e.g. constants) are live.
1004   if (!isa<Instruction>(Val) && !isa<Argument>(Val)) return true;
1005   
1006   // If Val is a constant sized alloca in the entry block, it is live, this is
1007   // true because it is just a reference to the stack/frame pointer, which is
1008   // live for the whole function.
1009   if (AllocaInst *AI = dyn_cast<AllocaInst>(Val))
1010     if (AI->isStaticAlloca())
1011       return true;
1012   
1013   // Check to see if this value is already used in the memory instruction's
1014   // block.  If so, it's already live into the block at the very least, so we
1015   // can reasonably fold it.
1016   BasicBlock *MemBB = MemoryInst->getParent();
1017   for (Value::use_iterator UI = Val->use_begin(), E = Val->use_end();
1018        UI != E; ++UI)
1019     // We know that uses of arguments and instructions have to be instructions.
1020     if (cast<Instruction>(*UI)->getParent() == MemBB)
1021       return true;
1022   
1023   return false;
1024 }
1025
1026
1027
1028 /// IsProfitableToFoldIntoAddressingMode - It is possible for the addressing
1029 /// mode of the machine to fold the specified instruction into a load or store
1030 /// that ultimately uses it.  However, the specified instruction has multiple
1031 /// uses.  Given this, it may actually increase register pressure to fold it
1032 /// into the load.  For example, consider this code:
1033 ///
1034 ///     X = ...
1035 ///     Y = X+1
1036 ///     use(Y)   -> nonload/store
1037 ///     Z = Y+1
1038 ///     load Z
1039 ///
1040 /// In this case, Y has multiple uses, and can be folded into the load of Z
1041 /// (yielding load [X+2]).  However, doing this will cause both "X" and "X+1" to
1042 /// be live at the use(Y) line.  If we don't fold Y into load Z, we use one
1043 /// fewer register.  Since Y can't be folded into "use(Y)" we don't increase the
1044 /// number of computations either.
1045 ///
1046 /// Note that this (like most of CodeGenPrepare) is just a rough heuristic.  If
1047 /// X was live across 'load Z' for other reasons, we actually *would* want to
1048 /// fold the addressing mode in the Z case.  This would make Y die earlier.
1049 bool AddressingModeMatcher::
1050 IsProfitableToFoldIntoAddressingMode(Instruction *I, ExtAddrMode &AMBefore,
1051                                      ExtAddrMode &AMAfter) {
1052   if (IgnoreProfitability) return true;
1053   
1054   // AMBefore is the addressing mode before this instruction was folded into it,
1055   // and AMAfter is the addressing mode after the instruction was folded.  Get
1056   // the set of registers referenced by AMAfter and subtract out those
1057   // referenced by AMBefore: this is the set of values which folding in this
1058   // address extends the lifetime of.
1059   //
1060   // Note that there are only two potential values being referenced here,
1061   // BaseReg and ScaleReg (global addresses are always available, as are any
1062   // folded immediates).
1063   Value *BaseReg = AMAfter.BaseReg, *ScaledReg = AMAfter.ScaledReg;
1064   
1065   // If the BaseReg or ScaledReg was referenced by the previous addrmode, their
1066   // lifetime wasn't extended by adding this instruction.
1067   if (ValueAlreadyLiveAtInst(BaseReg, AMBefore.BaseReg, AMBefore.ScaledReg))
1068     BaseReg = 0;
1069   if (ValueAlreadyLiveAtInst(ScaledReg, AMBefore.BaseReg, AMBefore.ScaledReg))
1070     ScaledReg = 0;
1071
1072   // If folding this instruction (and it's subexprs) didn't extend any live
1073   // ranges, we're ok with it.
1074   if (BaseReg == 0 && ScaledReg == 0)
1075     return true;
1076
1077   // If all uses of this instruction are ultimately load/store/inlineasm's,
1078   // check to see if their addressing modes will include this instruction.  If
1079   // so, we can fold it into all uses, so it doesn't matter if it has multiple
1080   // uses.
1081   SmallVector<std::pair<Instruction*,unsigned>, 16> MemoryUses;
1082   SmallPtrSet<Instruction*, 16> ConsideredInsts;
1083   if (FindAllMemoryUses(I, MemoryUses, ConsideredInsts, TLI))
1084     return false;  // Has a non-memory, non-foldable use!
1085   
1086   // Now that we know that all uses of this instruction are part of a chain of
1087   // computation involving only operations that could theoretically be folded
1088   // into a memory use, loop over each of these uses and see if they could
1089   // *actually* fold the instruction.
1090   SmallVector<Instruction*, 32> MatchedAddrModeInsts;
1091   for (unsigned i = 0, e = MemoryUses.size(); i != e; ++i) {
1092     Instruction *User = MemoryUses[i].first;
1093     unsigned OpNo = MemoryUses[i].second;
1094     
1095     // Get the access type of this use.  If the use isn't a pointer, we don't
1096     // know what it accesses.
1097     Value *Address = User->getOperand(OpNo);
1098     if (!isa<PointerType>(Address->getType()))
1099       return false;
1100     const Type *AddressAccessTy =
1101       cast<PointerType>(Address->getType())->getElementType();
1102     
1103     // Do a match against the root of this address, ignoring profitability. This
1104     // will tell us if the addressing mode for the memory operation will
1105     // *actually* cover the shared instruction.
1106     ExtAddrMode Result;
1107     AddressingModeMatcher Matcher(MatchedAddrModeInsts, TLI, AddressAccessTy,
1108                                   MemoryInst, Result);
1109     Matcher.IgnoreProfitability = true;
1110     bool Success = Matcher.MatchAddr(Address, 0);
1111     Success = Success; assert(Success && "Couldn't select *anything*?");
1112
1113     // If the match didn't cover I, then it won't be shared by it.
1114     if (std::find(MatchedAddrModeInsts.begin(), MatchedAddrModeInsts.end(),
1115                   I) == MatchedAddrModeInsts.end())
1116       return false;
1117     
1118     MatchedAddrModeInsts.clear();
1119   }
1120   
1121   return true;
1122 }
1123
1124
1125 //===----------------------------------------------------------------------===//
1126 // Memory Optimization
1127 //===----------------------------------------------------------------------===//
1128
1129 /// IsNonLocalValue - Return true if the specified values are defined in a
1130 /// different basic block than BB.
1131 static bool IsNonLocalValue(Value *V, BasicBlock *BB) {
1132   if (Instruction *I = dyn_cast<Instruction>(V))
1133     return I->getParent() != BB;
1134   return false;
1135 }
1136
1137 /// OptimizeMemoryInst - Load and Store Instructions have often have
1138 /// addressing modes that can do significant amounts of computation.  As such,
1139 /// instruction selection will try to get the load or store to do as much
1140 /// computation as possible for the program.  The problem is that isel can only
1141 /// see within a single block.  As such, we sink as much legal addressing mode
1142 /// stuff into the block as possible.
1143 ///
1144 /// This method is used to optimize both load/store and inline asms with memory
1145 /// operands.
1146 bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
1147                                         const Type *AccessTy,
1148                                         DenseMap<Value*,Value*> &SunkAddrs) {
1149   // Figure out what addressing mode will be built up for this operation.
1150   SmallVector<Instruction*, 16> AddrModeInsts;
1151   ExtAddrMode AddrMode = AddressingModeMatcher::Match(Addr, AccessTy,MemoryInst,
1152                                                       AddrModeInsts, *TLI);
1153
1154   // Check to see if any of the instructions supersumed by this addr mode are
1155   // non-local to I's BB.
1156   bool AnyNonLocal = false;
1157   for (unsigned i = 0, e = AddrModeInsts.size(); i != e; ++i) {
1158     if (IsNonLocalValue(AddrModeInsts[i], MemoryInst->getParent())) {
1159       AnyNonLocal = true;
1160       break;
1161     }
1162   }
1163
1164   // If all the instructions matched are already in this BB, don't do anything.
1165   if (!AnyNonLocal) {
1166     DEBUG(cerr << "CGP: Found      local addrmode: " << AddrMode << "\n");
1167     return false;
1168   }
1169
1170   // Insert this computation right after this user.  Since our caller is
1171   // scanning from the top of the BB to the bottom, reuse of the expr are
1172   // guaranteed to happen later.
1173   BasicBlock::iterator InsertPt = MemoryInst;
1174
1175   // Now that we determined the addressing expression we want to use and know
1176   // that we have to sink it into this block.  Check to see if we have already
1177   // done this for some other load/store instr in this block.  If so, reuse the
1178   // computation.
1179   Value *&SunkAddr = SunkAddrs[Addr];
1180   if (SunkAddr) {
1181     DEBUG(cerr << "CGP: Reusing nonlocal addrmode: " << AddrMode << "\n");
1182     if (SunkAddr->getType() != Addr->getType())
1183       SunkAddr = new BitCastInst(SunkAddr, Addr->getType(), "tmp", InsertPt);
1184   } else {
1185     DEBUG(cerr << "CGP: SINKING nonlocal addrmode: " << AddrMode << "\n");
1186     const Type *IntPtrTy = TLI->getTargetData()->getIntPtrType();
1187
1188     Value *Result = 0;
1189     // Start with the scale value.
1190     if (AddrMode.Scale) {
1191       Value *V = AddrMode.ScaledReg;
1192       if (V->getType() == IntPtrTy) {
1193         // done.
1194       } else if (isa<PointerType>(V->getType())) {
1195         V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
1196       } else if (cast<IntegerType>(IntPtrTy)->getBitWidth() <
1197                  cast<IntegerType>(V->getType())->getBitWidth()) {
1198         V = new TruncInst(V, IntPtrTy, "sunkaddr", InsertPt);
1199       } else {
1200         V = new SExtInst(V, IntPtrTy, "sunkaddr", InsertPt);
1201       }
1202       if (AddrMode.Scale != 1)
1203         V = BinaryOperator::CreateMul(V, ConstantInt::get(IntPtrTy,
1204                                                           AddrMode.Scale),
1205                                       "sunkaddr", InsertPt);
1206       Result = V;
1207     }
1208
1209     // Add in the base register.
1210     if (AddrMode.BaseReg) {
1211       Value *V = AddrMode.BaseReg;
1212       if (V->getType() != IntPtrTy)
1213         V = new PtrToIntInst(V, IntPtrTy, "sunkaddr", InsertPt);
1214       if (Result)
1215         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
1216       else
1217         Result = V;
1218     }
1219
1220     // Add in the BaseGV if present.
1221     if (AddrMode.BaseGV) {
1222       Value *V = new PtrToIntInst(AddrMode.BaseGV, IntPtrTy, "sunkaddr",
1223                                   InsertPt);
1224       if (Result)
1225         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
1226       else
1227         Result = V;
1228     }
1229
1230     // Add in the Base Offset if present.
1231     if (AddrMode.BaseOffs) {
1232       Value *V = ConstantInt::get(IntPtrTy, AddrMode.BaseOffs);
1233       if (Result)
1234         Result = BinaryOperator::CreateAdd(Result, V, "sunkaddr", InsertPt);
1235       else
1236         Result = V;
1237     }
1238
1239     if (Result == 0)
1240       SunkAddr = Constant::getNullValue(Addr->getType());
1241     else
1242       SunkAddr = new IntToPtrInst(Result, Addr->getType(), "sunkaddr",InsertPt);
1243   }
1244
1245   MemoryInst->replaceUsesOfWith(Addr, SunkAddr);
1246
1247   if (Addr->use_empty())
1248     EraseDeadInstructions(Addr);
1249   return true;
1250 }
1251
1252 /// OptimizeInlineAsmInst - If there are any memory operands, use
1253 /// OptimizeMemoryInst to sink their address computing into the block when
1254 /// possible / profitable.
1255 bool CodeGenPrepare::OptimizeInlineAsmInst(Instruction *I, CallSite CS,
1256                                            DenseMap<Value*,Value*> &SunkAddrs) {
1257   bool MadeChange = false;
1258   InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
1259
1260   // Do a prepass over the constraints, canonicalizing them, and building up the
1261   // ConstraintOperands list.
1262   std::vector<InlineAsm::ConstraintInfo>
1263     ConstraintInfos = IA->ParseConstraints();
1264
1265   /// ConstraintOperands - Information about all of the constraints.
1266   std::vector<TargetLowering::AsmOperandInfo> ConstraintOperands;
1267   unsigned ArgNo = 0;   // ArgNo - The argument of the CallInst.
1268   for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
1269     ConstraintOperands.
1270       push_back(TargetLowering::AsmOperandInfo(ConstraintInfos[i]));
1271     TargetLowering::AsmOperandInfo &OpInfo = ConstraintOperands.back();
1272
1273     // Compute the value type for each operand.
1274     switch (OpInfo.Type) {
1275     case InlineAsm::isOutput:
1276       if (OpInfo.isIndirect)
1277         OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
1278       break;
1279     case InlineAsm::isInput:
1280       OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
1281       break;
1282     case InlineAsm::isClobber:
1283       // Nothing to do.
1284       break;
1285     }
1286
1287     // Compute the constraint code and ConstraintType to use.
1288     TLI->ComputeConstraintToUse(OpInfo, SDValue(),
1289                              OpInfo.ConstraintType == TargetLowering::C_Memory);
1290
1291     if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
1292         OpInfo.isIndirect) {
1293       Value *OpVal = OpInfo.CallOperandVal;
1294       MadeChange |= OptimizeMemoryInst(I, OpVal, OpVal->getType(), SunkAddrs);
1295     }
1296   }
1297
1298   return MadeChange;
1299 }
1300
1301 bool CodeGenPrepare::OptimizeExtUses(Instruction *I) {
1302   BasicBlock *DefBB = I->getParent();
1303
1304   // If both result of the {s|z}xt and its source are live out, rewrite all
1305   // other uses of the source with result of extension.
1306   Value *Src = I->getOperand(0);
1307   if (Src->hasOneUse())
1308     return false;
1309
1310   // Only do this xform if truncating is free.
1311   if (TLI && !TLI->isTruncateFree(I->getType(), Src->getType()))
1312     return false;
1313
1314   // Only safe to perform the optimization if the source is also defined in
1315   // this block.
1316   if (!isa<Instruction>(Src) || DefBB != cast<Instruction>(Src)->getParent())
1317     return false;
1318
1319   bool DefIsLiveOut = false;
1320   for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1321        UI != E; ++UI) {
1322     Instruction *User = cast<Instruction>(*UI);
1323
1324     // Figure out which BB this ext is used in.
1325     BasicBlock *UserBB = User->getParent();
1326     if (UserBB == DefBB) continue;
1327     DefIsLiveOut = true;
1328     break;
1329   }
1330   if (!DefIsLiveOut)
1331     return false;
1332
1333   // Make sure non of the uses are PHI nodes.
1334   for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
1335        UI != E; ++UI) {
1336     Instruction *User = cast<Instruction>(*UI);
1337     BasicBlock *UserBB = User->getParent();
1338     if (UserBB == DefBB) continue;
1339     // Be conservative. We don't want this xform to end up introducing
1340     // reloads just before load / store instructions.
1341     if (isa<PHINode>(User) || isa<LoadInst>(User) || isa<StoreInst>(User))
1342       return false;
1343   }
1344
1345   // InsertedTruncs - Only insert one trunc in each block once.
1346   DenseMap<BasicBlock*, Instruction*> InsertedTruncs;
1347
1348   bool MadeChange = false;
1349   for (Value::use_iterator UI = Src->use_begin(), E = Src->use_end();
1350        UI != E; ++UI) {
1351     Use &TheUse = UI.getUse();
1352     Instruction *User = cast<Instruction>(*UI);
1353
1354     // Figure out which BB this ext is used in.
1355     BasicBlock *UserBB = User->getParent();
1356     if (UserBB == DefBB) continue;
1357
1358     // Both src and def are live in this block. Rewrite the use.
1359     Instruction *&InsertedTrunc = InsertedTruncs[UserBB];
1360
1361     if (!InsertedTrunc) {
1362       BasicBlock::iterator InsertPt = UserBB->getFirstNonPHI();
1363
1364       InsertedTrunc = new TruncInst(I, Src->getType(), "", InsertPt);
1365     }
1366
1367     // Replace a use of the {s|z}ext source with a use of the result.
1368     TheUse = InsertedTrunc;
1369
1370     MadeChange = true;
1371   }
1372
1373   return MadeChange;
1374 }
1375
1376 // In this pass we look for GEP and cast instructions that are used
1377 // across basic blocks and rewrite them to improve basic-block-at-a-time
1378 // selection.
1379 bool CodeGenPrepare::OptimizeBlock(BasicBlock &BB) {
1380   bool MadeChange = false;
1381
1382   // Split all critical edges where the dest block has a PHI and where the phi
1383   // has shared immediate operands.
1384   TerminatorInst *BBTI = BB.getTerminator();
1385   if (BBTI->getNumSuccessors() > 1) {
1386     for (unsigned i = 0, e = BBTI->getNumSuccessors(); i != e; ++i)
1387       if (isa<PHINode>(BBTI->getSuccessor(i)->begin()) &&
1388           isCriticalEdge(BBTI, i, true))
1389         SplitEdgeNicely(BBTI, i, this);
1390   }
1391
1392
1393   // Keep track of non-local addresses that have been sunk into this block.
1394   // This allows us to avoid inserting duplicate code for blocks with multiple
1395   // load/stores of the same address.
1396   DenseMap<Value*, Value*> SunkAddrs;
1397
1398   for (BasicBlock::iterator BBI = BB.begin(), E = BB.end(); BBI != E; ) {
1399     Instruction *I = BBI++;
1400
1401     if (CastInst *CI = dyn_cast<CastInst>(I)) {
1402       // If the source of the cast is a constant, then this should have
1403       // already been constant folded.  The only reason NOT to constant fold
1404       // it is if something (e.g. LSR) was careful to place the constant
1405       // evaluation in a block other than then one that uses it (e.g. to hoist
1406       // the address of globals out of a loop).  If this is the case, we don't
1407       // want to forward-subst the cast.
1408       if (isa<Constant>(CI->getOperand(0)))
1409         continue;
1410
1411       bool Change = false;
1412       if (TLI) {
1413         Change = OptimizeNoopCopyExpression(CI, *TLI);
1414         MadeChange |= Change;
1415       }
1416
1417       if (!Change && (isa<ZExtInst>(I) || isa<SExtInst>(I)))
1418         MadeChange |= OptimizeExtUses(I);
1419     } else if (CmpInst *CI = dyn_cast<CmpInst>(I)) {
1420       MadeChange |= OptimizeCmpExpression(CI);
1421     } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1422       if (TLI)
1423         MadeChange |= OptimizeMemoryInst(I, I->getOperand(0), LI->getType(),
1424                                          SunkAddrs);
1425     } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
1426       if (TLI)
1427         MadeChange |= OptimizeMemoryInst(I, SI->getOperand(1),
1428                                          SI->getOperand(0)->getType(),
1429                                          SunkAddrs);
1430     } else if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1431       if (GEPI->hasAllZeroIndices()) {
1432         /// The GEP operand must be a pointer, so must its result -> BitCast
1433         Instruction *NC = new BitCastInst(GEPI->getOperand(0), GEPI->getType(),
1434                                           GEPI->getName(), GEPI);
1435         GEPI->replaceAllUsesWith(NC);
1436         GEPI->eraseFromParent();
1437         MadeChange = true;
1438         BBI = NC;
1439       }
1440     } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
1441       // If we found an inline asm expession, and if the target knows how to
1442       // lower it to normal LLVM code, do so now.
1443       if (TLI && isa<InlineAsm>(CI->getCalledValue()))
1444         if (const TargetAsmInfo *TAI =
1445             TLI->getTargetMachine().getTargetAsmInfo()) {
1446           if (TAI->ExpandInlineAsm(CI))
1447             BBI = BB.begin();
1448           else
1449             // Sink address computing for memory operands into the block.
1450             MadeChange |= OptimizeInlineAsmInst(I, &(*CI), SunkAddrs);
1451         }
1452     }
1453   }
1454
1455   return MadeChange;
1456 }