Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 1 | //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===// |
| 2 | // |
| 3 | // This pass is a simple loop invariant code motion pass. |
| 4 | // |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "llvm/Transforms/Scalar.h" |
| 8 | #include "llvm/Transforms/Utils/Local.h" |
| 9 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 10 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 11 | #include "llvm/iOperators.h" |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 12 | #include "llvm/iMemory.h" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 13 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 14 | #include "Support/STLExtras.h" |
| 15 | #include "Support/StatisticReporter.h" |
| 16 | #include <algorithm> |
Anand Shukla | 2bc6419 | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 17 | using std::string; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 18 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 19 | namespace { |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 20 | Statistic<>NumHoisted("licm\t\t- Number of instructions hoisted out of loop"); |
| 21 | Statistic<> NumHoistedLoads("licm\t\t- Number of load insts hoisted"); |
| 22 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 23 | struct LICM : public FunctionPass, public InstVisitor<LICM> { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 24 | virtual bool runOnFunction(Function &F); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 25 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 26 | /// This transformation requires natural loop information & requires that |
| 27 | /// loop preheaders be inserted into the CFG... |
| 28 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 29 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 30 | AU.preservesCFG(); |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 31 | AU.addRequiredID(LoopPreheadersID); |
Chris Lattner | f0ed55d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 32 | AU.addRequired<LoopInfo>(); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 33 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | private: |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 37 | Loop *CurLoop; // The current loop we are working on... |
| 38 | BasicBlock *Preheader; // The preheader block of the current loop... |
| 39 | bool Changed; // Set to true when we change anything. |
| 40 | AliasAnalysis *AA; // Currently AliasAnalysis information |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 41 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 42 | /// visitLoop - Hoist expressions out of the specified loop... |
| 43 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 44 | void visitLoop(Loop *L); |
| 45 | |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 46 | /// inCurrentLoop - Little predicate that returns false if the specified |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 47 | /// basic block is in a subloop of the current one, not the current one |
| 48 | /// itself. |
| 49 | /// |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 50 | bool inCurrentLoop(BasicBlock *BB) { |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 51 | for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i) |
| 52 | if (CurLoop->getSubLoops()[i]->contains(BB)) |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 53 | return false; // A subloop actually contains this block! |
| 54 | return true; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 57 | /// hoist - When an instruction is found to only use loop invariant operands |
| 58 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 59 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 60 | void hoist(Instruction &I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 61 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 62 | /// pointerInvalidatedByLoop - Return true if the body of this loop may |
| 63 | /// store into the memory location pointed to by V. |
| 64 | /// |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 65 | bool pointerInvalidatedByLoop(Value *V); |
| 66 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 67 | /// isLoopInvariant - Return true if the specified value is loop invariant |
| 68 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 69 | inline bool isLoopInvariant(Value *V) { |
| 70 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 71 | return !CurLoop->contains(I->getParent()); |
| 72 | return true; // All non-instructions are loop invariant |
| 73 | } |
| 74 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 75 | /// Instruction visitation handlers... these basically control whether or |
| 76 | /// not the specified instruction types are hoisted. |
| 77 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 78 | friend class InstVisitor<LICM>; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 79 | void visitBinaryOperator(Instruction &I) { |
| 80 | if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1))) |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 81 | hoist(I); |
| 82 | } |
Chris Lattner | b80b69c | 2002-08-14 18:22:19 +0000 | [diff] [blame] | 83 | void visitCastInst(CastInst &CI) { |
| 84 | Instruction &I = (Instruction&)CI; |
| 85 | if (isLoopInvariant(I.getOperand(0))) hoist(I); |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 86 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 87 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); } |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 88 | |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 89 | void visitLoadInst(LoadInst &LI); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 91 | void visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 92 | Instruction &I = (Instruction&)GEPI; |
| 93 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 94 | if (!isLoopInvariant(I.getOperand(i))) return; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 95 | hoist(I); |
| 96 | } |
| 97 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 98 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 99 | RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion"); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | Pass *createLICMPass() { return new LICM(); } |
| 103 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 104 | /// runOnFunction - For LICM, this simply traverses the loop structure of the |
| 105 | /// function, hoisting expressions out of loops if possible. |
| 106 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 107 | bool LICM::runOnFunction(Function &) { |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 108 | // Get information about the top level loops in the function... |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 109 | const std::vector<Loop*> &TopLevelLoops = |
| 110 | getAnalysis<LoopInfo>().getTopLevelLoops(); |
| 111 | |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 112 | // Get our alias analysis information... |
| 113 | AA = &getAnalysis<AliasAnalysis>(); |
| 114 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 115 | // Traverse loops in postorder, hoisting expressions out of the deepest loops |
| 116 | // first. |
| 117 | // |
| 118 | Changed = false; |
| 119 | std::for_each(TopLevelLoops.begin(), TopLevelLoops.end(), |
| 120 | bind_obj(this, &LICM::visitLoop)); |
| 121 | return Changed; |
| 122 | } |
| 123 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 124 | |
| 125 | /// visitLoop - Hoist expressions out of the specified loop... |
| 126 | /// |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 127 | void LICM::visitLoop(Loop *L) { |
| 128 | // Recurse through all subloops before we process this loop... |
| 129 | std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(), |
| 130 | bind_obj(this, &LICM::visitLoop)); |
| 131 | CurLoop = L; |
| 132 | |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 133 | // Get the preheader block to move instructions into... |
| 134 | Preheader = L->getLoopPreheader(); |
| 135 | assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!"); |
| 136 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 137 | // We want to visit all of the instructions in this loop... that are not parts |
| 138 | // of our subloops (they have already had their invariants hoisted out of |
| 139 | // their loop, into this loop, so there is no need to process the BODIES of |
| 140 | // the subloops). |
| 141 | // |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 142 | for (std::vector<BasicBlock*>::const_iterator |
| 143 | I = L->getBlocks().begin(), E = L->getBlocks().end(); I != E; ++I) |
| 144 | if (inCurrentLoop(*I)) |
| 145 | visit(**I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 146 | |
| 147 | // Clear out loops state information for the next iteration |
| 148 | CurLoop = 0; |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 149 | Preheader = 0; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 152 | /// hoist - When an instruction is found to only use loop invariant operands |
| 153 | /// that is safe to hoist, this instruction is called to do the dirty work. |
| 154 | /// |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 155 | void LICM::hoist(Instruction &Inst) { |
| 156 | if (Inst.use_empty()) return; // Don't (re) hoist dead instructions! |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 157 | //cerr << "Hoisting " << Inst; |
| 158 | |
| 159 | BasicBlock *Header = CurLoop->getHeader(); |
| 160 | |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 161 | // Remove the instruction from its current basic block... but don't delete the |
| 162 | // instruction. |
| 163 | Inst.getParent()->getInstList().remove(&Inst); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 165 | // Insert the new node in Preheader, before the terminator. |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 166 | Preheader->getInstList().insert(Preheader->getTerminator(), &Inst); |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 168 | ++NumHoisted; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 169 | Changed = true; |
| 170 | } |
| 171 | |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 172 | |
| 173 | void LICM::visitLoadInst(LoadInst &LI) { |
| 174 | if (isLoopInvariant(LI.getOperand(0)) && |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 175 | !pointerInvalidatedByLoop(LI.getOperand(0))) { |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 176 | hoist(LI); |
Chris Lattner | d57f3f5 | 2002-09-26 19:40:25 +0000 | [diff] [blame^] | 177 | ++NumHoistedLoads; |
| 178 | } |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Chris Lattner | f64f2d3 | 2002-09-26 16:52:07 +0000 | [diff] [blame] | 181 | /// pointerInvalidatedByLoop - Return true if the body of this loop may store |
| 182 | /// into the memory location pointed to by V. |
| 183 | /// |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 184 | bool LICM::pointerInvalidatedByLoop(Value *V) { |
| 185 | // Check to see if any of the basic blocks in CurLoop invalidate V. |
| 186 | for (unsigned i = 0, e = CurLoop->getBlocks().size(); i != e; ++i) |
| 187 | if (AA->canBasicBlockModify(*CurLoop->getBlocks()[i], V)) |
| 188 | return true; |
| 189 | return false; |
| 190 | } |