Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 1 | //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame^] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 10 | // This pass is a simple loop invariant code motion pass. An interesting aspect |
| 11 | // of this pass is that it uses alias analysis for two purposes: |
| 12 | // |
| 13 | // 1. Moving loop invariant loads out of loops. If we can determine that a |
| 14 | // load inside of a loop never aliases anything stored to, we can hoist it |
| 15 | // like any other instruction. |
| 16 | // 2. Scalar Promotion of Memory - If there is a store instruction inside of |
| 17 | // the loop, we try to move the store to happen AFTER the loop instead of |
| 18 | // inside of the loop. This can only happen if a few conditions are true: |
| 19 | // A. The pointer stored through is loop invariant |
| 20 | // B. There are no stores or loads in the loop which _may_ alias the |
| 21 | // pointer. There are no calls in the loop which mod/ref the pointer. |
| 22 | // If these conditions are true, we can promote the loads and stores in the |
| 23 | // loop of the pointer to use a temporary alloca'd variable. We then use |
| 24 | // the mem2reg functionality to construct the appropriate SSA form for the |
| 25 | // variable. |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 26 | // |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 30 | #include "llvm/Transforms/Utils/PromoteMemToReg.h" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 31 | #include "llvm/Transforms/Utils/Local.h" |
| 32 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/AliasSetTracker.h" |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 36 | #include "llvm/Instructions.h" |
| 37 | #include "llvm/DerivedTypes.h" |
Chris Lattner | e27406e | 2003-03-03 17:25:18 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 39 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 40 | #include "llvm/Support/CFG.h" |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 41 | #include "Support/CommandLine.h" |
Chris Lattner | 8abcd56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 42 | #include "Support/Debug.h" |
| 43 | #include "Support/Statistic.h" |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 44 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 45 | #include <algorithm> |
| 46 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 47 | namespace { |
Chris Lattner | 1789570 | 2003-10-13 05:04:27 +0000 | [diff] [blame] | 48 | cl::opt<bool> |
| 49 | DisablePromotion("disable-licm-promotion", cl::Hidden, |
| 50 | cl::desc("Disable memory promotion in LICM pass")); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 51 | |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 52 | Statistic<> NumHoisted("licm", "Number of instructions hoisted out of loop"); |
| 53 | Statistic<> NumHoistedLoads("licm", "Number of load insts hoisted"); |
Chris Lattner | 1789570 | 2003-10-13 05:04:27 +0000 | [diff] [blame] | 54 | Statistic<> NumPromoted("licm", |
| 55 | "Number of memory locations promoted to registers"); |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 57 | struct LICM : public FunctionPass, public InstVisitor<LICM> { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 58 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 59 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 60 | /// This transformation requires natural loop information & requires that |
| 61 | /// loop preheaders be inserted into the CFG... |
| 62 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 63 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 64 | AU.setPreservesCFG(); |
Chris Lattner | 72272a7 | 2003-10-12 21:52:28 +0000 | [diff] [blame] | 65 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | f0ed55d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 66 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 67 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 68 | AU.addRequired<DominanceFrontier>(); // For scalar promotion (mem2reg) |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 69 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | private: |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 73 | LoopInfo *LI; // Current LoopInfo |
| 74 | AliasAnalysis *AA; // Current AliasAnalysis information |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 75 | DominanceFrontier *DF; // Current Dominance Frontier |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 76 | bool Changed; // Set to true when we change anything. |
| 77 | BasicBlock *Preheader; // The preheader block of the current loop... |
| 78 | Loop *CurLoop; // The current loop we are working on... |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 79 | AliasSetTracker *CurAST; // AliasSet information for the current loop... |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 80 | DominatorTree *DT; // Dominator Tree for the current Loop... |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 81 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 82 | /// visitLoop - Hoist expressions out of the specified loop... |
| 83 | /// |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 84 | void visitLoop(Loop *L, AliasSetTracker &AST); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 86 | /// HoistRegion - Walk the specified region of the CFG (defined by all |
| 87 | /// blocks dominated by the specified block, and that are in the current |
| 88 | /// loop) in depth first order w.r.t the DominatorTree. This allows us to |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 89 | /// visit definitions before uses, allowing us to hoist a loop body in one |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 90 | /// pass without iteration. |
| 91 | /// |
| 92 | void HoistRegion(DominatorTree::Node *N); |
| 93 | |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 94 | /// inSubLoop - Little predicate that returns true if the specified basic |
| 95 | /// block is in a subloop of the current one, not the current one itself. |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 96 | /// |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 97 | bool inSubLoop(BasicBlock *BB) { |
| 98 | assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop"); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 99 | for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i) |
| 100 | if (CurLoop->getSubLoops()[i]->contains(BB)) |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 101 | return true; // A subloop actually contains this block! |
| 102 | return false; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 105 | /// hoist - When an instruction is found to only use loop invariant operands |
| 106 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 107 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 108 | void hoist(Instruction &I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 1789570 | 2003-10-13 05:04:27 +0000 | [diff] [blame] | 110 | /// SafeToHoist - Only hoist an instruction if it is not a trapping |
| 111 | /// instruction or if it is a trapping instruction and is guaranteed to |
| 112 | /// execute. |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 113 | /// |
| 114 | bool SafeToHoist(Instruction &I); |
| 115 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 116 | /// pointerInvalidatedByLoop - Return true if the body of this loop may |
| 117 | /// store into the memory location pointed to by V. |
| 118 | /// |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 119 | bool pointerInvalidatedByLoop(Value *V) { |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 120 | // Check to see if any of the basic blocks in CurLoop invalidate *V. |
| 121 | return CurAST->getAliasSetForPointer(V, 0).isMod(); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 122 | } |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 123 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 124 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 125 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 126 | inline bool isLoopInvariant(Value *V) { |
| 127 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 128 | return !CurLoop->contains(I->getParent()); |
| 129 | return true; // All non-instructions are loop invariant |
| 130 | } |
| 131 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 132 | /// PromoteValuesInLoop - Look at the stores in the loop and promote as many |
| 133 | /// to scalars as we can. |
| 134 | /// |
| 135 | void PromoteValuesInLoop(); |
| 136 | |
| 137 | /// findPromotableValuesInLoop - Check the current loop for stores to |
Misha Brukman | 9b8d339 | 2003-09-11 15:32:37 +0000 | [diff] [blame] | 138 | /// definite pointers, which are not loaded and stored through may aliases. |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 139 | /// If these are found, create an alloca for the value, add it to the |
| 140 | /// PromotedValues list, and keep track of the mapping from value to |
| 141 | /// alloca... |
| 142 | /// |
| 143 | void findPromotableValuesInLoop( |
| 144 | std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, |
| 145 | std::map<Value*, AllocaInst*> &Val2AlMap); |
| 146 | |
| 147 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 148 | /// Instruction visitation handlers... these basically control whether or |
| 149 | /// not the specified instruction types are hoisted. |
| 150 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 151 | friend class InstVisitor<LICM>; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 152 | void visitBinaryOperator(Instruction &I) { |
Chris Lattner | 1789570 | 2003-10-13 05:04:27 +0000 | [diff] [blame] | 153 | if (isLoopInvariant(I.getOperand(0)) && |
| 154 | isLoopInvariant(I.getOperand(1)) && SafeToHoist(I)) |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 155 | hoist(I); |
| 156 | } |
Chris Lattner | b80b69c | 2002-08-14 18:22:19 +0000 | [diff] [blame] | 157 | void visitCastInst(CastInst &CI) { |
| 158 | Instruction &I = (Instruction&)CI; |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 159 | if (isLoopInvariant(I.getOperand(0)) && SafeToHoist(CI)) hoist(I); |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 160 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 161 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); } |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 162 | |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 163 | void visitLoadInst(LoadInst &LI); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 165 | void visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 166 | Instruction &I = (Instruction&)GEPI; |
| 167 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 168 | if (!isLoopInvariant(I.getOperand(i))) return; |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 169 | if(SafeToHoist(GEPI)) |
| 170 | hoist(I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 171 | } |
| 172 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 173 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 174 | RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion"); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | Pass *createLICMPass() { return new LICM(); } |
| 178 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 179 | /// runOnFunction - For LICM, this simply traverses the loop structure of the |
| 180 | /// function, hoisting expressions out of loops if possible. |
| 181 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 182 | bool LICM::runOnFunction(Function &) { |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 183 | Changed = false; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 185 | // Get our Loop and Alias Analysis information... |
| 186 | LI = &getAnalysis<LoopInfo>(); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 187 | AA = &getAnalysis<AliasAnalysis>(); |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 188 | DF = &getAnalysis<DominanceFrontier>(); |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 189 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 190 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 191 | // Hoist expressions out of all of the top-level loops. |
| 192 | const std::vector<Loop*> &TopLevelLoops = LI->getTopLevelLoops(); |
| 193 | for (std::vector<Loop*>::const_iterator I = TopLevelLoops.begin(), |
| 194 | E = TopLevelLoops.end(); I != E; ++I) { |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 195 | AliasSetTracker AST(*AA); |
| 196 | LICM::visitLoop(*I, AST); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 197 | } |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 198 | return Changed; |
| 199 | } |
| 200 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 201 | |
| 202 | /// visitLoop - Hoist expressions out of the specified loop... |
| 203 | /// |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 204 | void LICM::visitLoop(Loop *L, AliasSetTracker &AST) { |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 205 | // Recurse through all subloops before we process this loop... |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 206 | for (std::vector<Loop*>::const_iterator I = L->getSubLoops().begin(), |
| 207 | E = L->getSubLoops().end(); I != E; ++I) { |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 208 | AliasSetTracker SubAST(*AA); |
| 209 | LICM::visitLoop(*I, SubAST); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 210 | |
| 211 | // Incorporate information about the subloops into this loop... |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 212 | AST.add(SubAST); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 213 | } |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 214 | CurLoop = L; |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 215 | CurAST = &AST; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 216 | |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 217 | // Get the preheader block to move instructions into... |
| 218 | Preheader = L->getLoopPreheader(); |
| 219 | assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!"); |
| 220 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 221 | // Loop over the body of this loop, looking for calls, invokes, and stores. |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 222 | // Because subloops have already been incorporated into AST, we skip blocks in |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 223 | // subloops. |
| 224 | // |
| 225 | const std::vector<BasicBlock*> &LoopBBs = L->getBlocks(); |
| 226 | for (std::vector<BasicBlock*>::const_iterator I = LoopBBs.begin(), |
| 227 | E = LoopBBs.end(); I != E; ++I) |
| 228 | if (LI->getLoopFor(*I) == L) // Ignore blocks in subloops... |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 229 | AST.add(**I); // Incorporate the specified basic block |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 230 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 231 | // We want to visit all of the instructions in this loop... that are not parts |
| 232 | // of our subloops (they have already had their invariants hoisted out of |
| 233 | // their loop, into this loop, so there is no need to process the BODIES of |
| 234 | // the subloops). |
| 235 | // |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 236 | // Traverse the body of the loop in depth first order on the dominator tree so |
| 237 | // that we are guaranteed to see definitions before we see uses. This allows |
| 238 | // us to perform the LICM transformation in one pass, without iteration. |
| 239 | // |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 240 | HoistRegion(DT->getNode(L->getHeader())); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 242 | // Now that all loop invariants have been removed from the loop, promote any |
| 243 | // memory references to scalars that we can... |
| 244 | if (!DisablePromotion) |
| 245 | PromoteValuesInLoop(); |
| 246 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 247 | // Clear out loops state information for the next iteration |
| 248 | CurLoop = 0; |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 249 | Preheader = 0; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 252 | /// HoistRegion - Walk the specified region of the CFG (defined by all blocks |
| 253 | /// dominated by the specified block, and that are in the current loop) in depth |
Misha Brukman | 8b2bd4e | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 254 | /// first order w.r.t the DominatorTree. This allows us to visit definitions |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 255 | /// before uses, allowing us to hoist a loop body in one pass without iteration. |
| 256 | /// |
| 257 | void LICM::HoistRegion(DominatorTree::Node *N) { |
| 258 | assert(N != 0 && "Null dominator tree node?"); |
| 259 | |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 260 | // If this subregion is not in the top level loop at all, exit. |
Chris Lattner | bb9d03b | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 261 | if (!CurLoop->contains(N->getBlock())) return; |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 262 | |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 263 | // Only need to hoist the contents of this block if it is not part of a |
| 264 | // subloop (which would already have been hoisted) |
Chris Lattner | bb9d03b | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 265 | if (!inSubLoop(N->getBlock())) |
| 266 | visit(*N->getBlock()); |
Chris Lattner | 6443769 | 2002-09-29 21:46:09 +0000 | [diff] [blame] | 267 | |
| 268 | const std::vector<DominatorTree::Node*> &Children = N->getChildren(); |
| 269 | for (unsigned i = 0, e = Children.size(); i != e; ++i) |
| 270 | HoistRegion(Children[i]); |
| 271 | } |
| 272 | |
| 273 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 274 | /// hoist - When an instruction is found to only use loop invariant operands |
| 275 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 276 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 277 | void LICM::hoist(Instruction &Inst) { |
Chris Lattner | 05e8630 | 2002-09-29 22:26:07 +0000 | [diff] [blame] | 278 | DEBUG(std::cerr << "LICM hoisting to"; |
| 279 | WriteAsOperand(std::cerr, Preheader, false); |
| 280 | std::cerr << ": " << Inst); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 281 | |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 282 | // Remove the instruction from its current basic block... but don't delete the |
| 283 | // instruction. |
| 284 | Inst.getParent()->getInstList().remove(&Inst); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 286 | // Insert the new node in Preheader, before the terminator. |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 287 | Preheader->getInstList().insert(Preheader->getTerminator(), &Inst); |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 289 | ++NumHoisted; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 290 | Changed = true; |
| 291 | } |
| 292 | |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 293 | /// SafeToHoist - Only hoist an instruction if it is not a trapping instruction |
| 294 | /// or if it is a trapping instruction and is guaranteed to execute |
| 295 | /// |
| 296 | bool LICM::SafeToHoist(Instruction &Inst) { |
| 297 | |
| 298 | //If it is a trapping instruction, then check if its guaranteed to execute. |
| 299 | if(Inst.isTrapping()) { |
| 300 | |
| 301 | //Get the instruction's basic block. |
| 302 | BasicBlock *InstBB = Inst.getParent(); |
| 303 | |
| 304 | //Get the Dominator Tree Node for the instruction's basic block/ |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 305 | DominatorTree::Node *InstDTNode = DT->getNode(InstBB); |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 306 | |
| 307 | //Get the exit blocks for the current loop. |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 308 | const std::vector<BasicBlock* > &ExitBlocks = CurLoop->getExitBlocks(); |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 309 | |
| 310 | //For each exit block, get the DT node and walk up the DT until |
| 311 | //the instruction's basic block is found or we exit the loop. |
| 312 | for(unsigned i=0; i < ExitBlocks.size(); ++i) { |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 313 | DominatorTree::Node *IDom = DT->getNode(ExitBlocks[i]); |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 314 | |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 315 | while(IDom != InstDTNode) { |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 316 | |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 317 | //Get next Immediate Dominator. |
| 318 | IDom = IDom->getIDom(); |
| 319 | |
| 320 | //See if we exited the loop. |
Chris Lattner | bb9d03b | 2003-09-11 16:26:13 +0000 | [diff] [blame] | 321 | if(!CurLoop->contains(IDom->getBlock())) |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 322 | return false; |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 323 | } |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
Tanya Lattner | dc3c9a8 | 2003-08-05 20:39:02 +0000 | [diff] [blame] | 326 | |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 327 | return true; |
| 328 | } |
| 329 | |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 330 | |
| 331 | void LICM::visitLoadInst(LoadInst &LI) { |
Chris Lattner | 01a8391 | 2003-09-08 18:17:14 +0000 | [diff] [blame] | 332 | if (isLoopInvariant(LI.getOperand(0)) && !LI.isVolatile() && |
Tanya Lattner | 57c03df | 2003-08-05 18:45:46 +0000 | [diff] [blame] | 333 | !pointerInvalidatedByLoop(LI.getOperand(0)) && SafeToHoist(LI)) { |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 334 | hoist(LI); |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame] | 335 | ++NumHoistedLoads; |
| 336 | } |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 339 | /// PromoteValuesInLoop - Try to promote memory values to scalars by sinking |
| 340 | /// stores out of the loop and moving loads to before the loop. We do this by |
| 341 | /// looping over the stores in the loop, looking for stores to Must pointers |
| 342 | /// which are loop invariant. We promote these memory locations to use allocas |
| 343 | /// instead. These allocas can easily be raised to register values by the |
| 344 | /// PromoteMem2Reg functionality. |
| 345 | /// |
| 346 | void LICM::PromoteValuesInLoop() { |
| 347 | // PromotedValues - List of values that are promoted out of the loop. Each |
Chris Lattner | 216c7b8 | 2003-09-10 05:29:43 +0000 | [diff] [blame] | 348 | // value has an alloca instruction for it, and a canonical version of the |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 349 | // pointer. |
| 350 | std::vector<std::pair<AllocaInst*, Value*> > PromotedValues; |
| 351 | std::map<Value*, AllocaInst*> ValueToAllocaMap; // Map of ptr to alloca |
| 352 | |
| 353 | findPromotableValuesInLoop(PromotedValues, ValueToAllocaMap); |
| 354 | if (ValueToAllocaMap.empty()) return; // If there are values to promote... |
| 355 | |
| 356 | Changed = true; |
| 357 | NumPromoted += PromotedValues.size(); |
| 358 | |
| 359 | // Emit a copy from the value into the alloca'd value in the loop preheader |
| 360 | TerminatorInst *LoopPredInst = Preheader->getTerminator(); |
| 361 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { |
| 362 | // Load from the memory we are promoting... |
| 363 | LoadInst *LI = new LoadInst(PromotedValues[i].second, |
| 364 | PromotedValues[i].second->getName()+".promoted", |
| 365 | LoopPredInst); |
| 366 | // Store into the temporary alloca... |
| 367 | new StoreInst(LI, PromotedValues[i].first, LoopPredInst); |
| 368 | } |
| 369 | |
| 370 | // Scan the basic blocks in the loop, replacing uses of our pointers with |
| 371 | // uses of the allocas in question. If we find a branch that exits the |
| 372 | // loop, make sure to put reload code into all of the successors of the |
| 373 | // loop. |
| 374 | // |
| 375 | const std::vector<BasicBlock*> &LoopBBs = CurLoop->getBlocks(); |
| 376 | for (std::vector<BasicBlock*>::const_iterator I = LoopBBs.begin(), |
| 377 | E = LoopBBs.end(); I != E; ++I) { |
| 378 | // Rewrite all loads and stores in the block of the pointer... |
| 379 | for (BasicBlock::iterator II = (*I)->begin(), E = (*I)->end(); |
| 380 | II != E; ++II) { |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 381 | if (LoadInst *L = dyn_cast<LoadInst>(II)) { |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 382 | std::map<Value*, AllocaInst*>::iterator |
| 383 | I = ValueToAllocaMap.find(L->getOperand(0)); |
| 384 | if (I != ValueToAllocaMap.end()) |
| 385 | L->setOperand(0, I->second); // Rewrite load instruction... |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 386 | } else if (StoreInst *S = dyn_cast<StoreInst>(II)) { |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 387 | std::map<Value*, AllocaInst*>::iterator |
| 388 | I = ValueToAllocaMap.find(S->getOperand(1)); |
| 389 | if (I != ValueToAllocaMap.end()) |
| 390 | S->setOperand(1, I->second); // Rewrite store instruction... |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | // Check to see if any successors of this block are outside of the loop. |
| 395 | // If so, we need to copy the value from the alloca back into the memory |
| 396 | // location... |
| 397 | // |
| 398 | for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI) |
| 399 | if (!CurLoop->contains(*SI)) { |
| 400 | // Copy all of the allocas into their memory locations... |
Chris Lattner | 1ad80e2 | 2003-02-27 21:59:36 +0000 | [diff] [blame] | 401 | BasicBlock::iterator BI = (*SI)->begin(); |
| 402 | while (isa<PHINode>(*BI)) |
| 403 | ++BI; // Skip over all of the phi nodes in the block... |
| 404 | Instruction *InsertPos = BI; |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 405 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) { |
| 406 | // Load from the alloca... |
| 407 | LoadInst *LI = new LoadInst(PromotedValues[i].first, "", InsertPos); |
| 408 | // Store into the memory we promoted... |
| 409 | new StoreInst(LI, PromotedValues[i].second, InsertPos); |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // Now that we have done the deed, use the mem2reg functionality to promote |
| 415 | // all of the new allocas we just created into real SSA registers... |
| 416 | // |
| 417 | std::vector<AllocaInst*> PromotedAllocas; |
| 418 | PromotedAllocas.reserve(PromotedValues.size()); |
| 419 | for (unsigned i = 0, e = PromotedValues.size(); i != e; ++i) |
| 420 | PromotedAllocas.push_back(PromotedValues[i].first); |
Chris Lattner | a906bac | 2003-10-05 21:20:13 +0000 | [diff] [blame] | 421 | PromoteMemToReg(PromotedAllocas, *DT, *DF, AA->getTargetData()); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Misha Brukman | 9b8d339 | 2003-09-11 15:32:37 +0000 | [diff] [blame] | 424 | /// findPromotableValuesInLoop - Check the current loop for stores to definite |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 425 | /// pointers, which are not loaded and stored through may aliases. If these are |
| 426 | /// found, create an alloca for the value, add it to the PromotedValues list, |
| 427 | /// and keep track of the mapping from value to alloca... |
| 428 | /// |
| 429 | void LICM::findPromotableValuesInLoop( |
| 430 | std::vector<std::pair<AllocaInst*, Value*> > &PromotedValues, |
| 431 | std::map<Value*, AllocaInst*> &ValueToAllocaMap) { |
| 432 | Instruction *FnStart = CurLoop->getHeader()->getParent()->begin()->begin(); |
| 433 | |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 434 | // Loop over all of the alias sets in the tracker object... |
| 435 | for (AliasSetTracker::iterator I = CurAST->begin(), E = CurAST->end(); |
| 436 | I != E; ++I) { |
| 437 | AliasSet &AS = *I; |
| 438 | // We can promote this alias set if it has a store, if it is a "Must" alias |
| 439 | // set, and if the pointer is loop invariant. |
| 440 | if (!AS.isForwardingAliasSet() && AS.isMod() && AS.isMustAlias() && |
| 441 | isLoopInvariant(AS.begin()->first)) { |
| 442 | assert(AS.begin() != AS.end() && |
| 443 | "Must alias set should have at least one pointer element in it!"); |
| 444 | Value *V = AS.begin()->first; |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 446 | // Check that all of the pointers in the alias set have the same type. We |
| 447 | // cannot (yet) promote a memory location that is loaded and stored in |
| 448 | // different sizes. |
| 449 | bool PointerOk = true; |
| 450 | for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) |
| 451 | if (V->getType() != I->first->getType()) { |
| 452 | PointerOk = false; |
| 453 | break; |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 454 | } |
Chris Lattner | 0592bb7 | 2003-03-03 23:32:45 +0000 | [diff] [blame] | 455 | |
| 456 | if (PointerOk) { |
| 457 | const Type *Ty = cast<PointerType>(V->getType())->getElementType(); |
| 458 | AllocaInst *AI = new AllocaInst(Ty, 0, V->getName()+".tmp", FnStart); |
| 459 | PromotedValues.push_back(std::make_pair(AI, V)); |
| 460 | |
| 461 | for (AliasSet::iterator I = AS.begin(), E = AS.end(); I != E; ++I) |
| 462 | ValueToAllocaMap.insert(std::make_pair(I->first, AI)); |
| 463 | |
| 464 | DEBUG(std::cerr << "LICM: Promoting value: " << *V << "\n"); |
Chris Lattner | 45d67d6 | 2003-02-24 03:52:32 +0000 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | } |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 468 | } |