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