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