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