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 | |
| 26 | // This transformation requires natural loop information... |
| 27 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 28 | AU.preservesCFG(); |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 29 | AU.addRequiredID(LoopPreheadersID); |
Chris Lattner | f0ed55d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 30 | AU.addRequired<LoopInfo>(); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 31 | AU.addRequired<AliasAnalysis>(); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | private: |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 35 | Loop *CurLoop; // The current loop we are working on... |
| 36 | bool Changed; // Set to true when we change anything. |
| 37 | AliasAnalysis *AA; // Currently AliasAnalysis information |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 38 | |
| 39 | // visitLoop - Hoist expressions out of the specified loop... |
| 40 | void visitLoop(Loop *L); |
| 41 | |
| 42 | // notInCurrentLoop - Little predicate that returns true if the specified |
| 43 | // basic block is in a subloop of the current one, not the current one |
| 44 | // itself. |
| 45 | // |
| 46 | bool notInCurrentLoop(BasicBlock *BB) { |
| 47 | for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i) |
| 48 | if (CurLoop->getSubLoops()[i]->contains(BB)) |
| 49 | return true; // A subloop actually contains this block! |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | // hoist - When an instruction is found to only use loop invariant operands |
| 54 | // that is safe to hoist, this instruction is called to do the dirty work. |
| 55 | // |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 56 | void hoist(Instruction &I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 57 | |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 58 | // pointerInvalidatedByLoop - Return true if the body of this loop may store |
| 59 | // into the memory location pointed to by V. |
| 60 | // |
| 61 | bool pointerInvalidatedByLoop(Value *V); |
| 62 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 63 | // isLoopInvariant - Return true if the specified value is loop invariant |
| 64 | inline bool isLoopInvariant(Value *V) { |
| 65 | if (Instruction *I = dyn_cast<Instruction>(V)) |
| 66 | return !CurLoop->contains(I->getParent()); |
| 67 | return true; // All non-instructions are loop invariant |
| 68 | } |
| 69 | |
| 70 | // visitBasicBlock - Run LICM on a particular block. |
| 71 | void visitBasicBlock(BasicBlock *BB); |
| 72 | |
| 73 | // Instruction visitation handlers... these basically control whether or not |
| 74 | // the specified instruction types are hoisted. |
| 75 | // |
| 76 | friend class InstVisitor<LICM>; |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 77 | void visitBinaryOperator(Instruction &I) { |
| 78 | if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1))) |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 79 | hoist(I); |
| 80 | } |
Chris Lattner | b80b69c | 2002-08-14 18:22:19 +0000 | [diff] [blame] | 81 | void visitCastInst(CastInst &CI) { |
| 82 | Instruction &I = (Instruction&)CI; |
| 83 | if (isLoopInvariant(I.getOperand(0))) hoist(I); |
Chris Lattner | b193ff8 | 2002-08-14 18:18:02 +0000 | [diff] [blame] | 84 | } |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 85 | void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); } |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 86 | |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 87 | void visitLoadInst(LoadInst &LI); |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 88 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 89 | void visitGetElementPtrInst(GetElementPtrInst &GEPI) { |
| 90 | Instruction &I = (Instruction&)GEPI; |
| 91 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 92 | if (!isLoopInvariant(I.getOperand(i))) return; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 93 | hoist(I); |
| 94 | } |
| 95 | }; |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 96 | |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 97 | RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion"); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | Pass *createLICMPass() { return new LICM(); } |
| 101 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 102 | bool LICM::runOnFunction(Function &) { |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 103 | // get our loop information... |
| 104 | const std::vector<Loop*> &TopLevelLoops = |
| 105 | getAnalysis<LoopInfo>().getTopLevelLoops(); |
| 106 | |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 107 | // Get our alias analysis information... |
| 108 | AA = &getAnalysis<AliasAnalysis>(); |
| 109 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 110 | // Traverse loops in postorder, hoisting expressions out of the deepest loops |
| 111 | // first. |
| 112 | // |
| 113 | Changed = false; |
| 114 | std::for_each(TopLevelLoops.begin(), TopLevelLoops.end(), |
| 115 | bind_obj(this, &LICM::visitLoop)); |
| 116 | return Changed; |
| 117 | } |
| 118 | |
| 119 | void LICM::visitLoop(Loop *L) { |
| 120 | // Recurse through all subloops before we process this loop... |
| 121 | std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(), |
| 122 | bind_obj(this, &LICM::visitLoop)); |
| 123 | CurLoop = L; |
| 124 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 125 | // We want to visit all of the instructions in this loop... that are not parts |
| 126 | // of our subloops (they have already had their invariants hoisted out of |
| 127 | // their loop, into this loop, so there is no need to process the BODIES of |
| 128 | // the subloops). |
| 129 | // |
| 130 | std::vector<BasicBlock*> BBs(L->getBlocks().begin(), L->getBlocks().end()); |
| 131 | |
| 132 | // Remove blocks that are actually in subloops... |
| 133 | BBs.erase(std::remove_if(BBs.begin(), BBs.end(), |
| 134 | bind_obj(this, &LICM::notInCurrentLoop)), BBs.end()); |
| 135 | |
| 136 | // Visit all of the basic blocks we have chosen, hoisting out the instructions |
| 137 | // as neccesary. This leaves dead copies of the instruction in the loop |
| 138 | // unfortunately... |
| 139 | // |
| 140 | for_each(BBs.begin(), BBs.end(), bind_obj(this, &LICM::visitBasicBlock)); |
| 141 | |
| 142 | // Clear out loops state information for the next iteration |
| 143 | CurLoop = 0; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | void LICM::visitBasicBlock(BasicBlock *BB) { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 147 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { |
| 148 | visit(*I); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 149 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 150 | if (dceInstruction(I)) |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 151 | Changed = true; |
| 152 | else |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 153 | ++I; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | |
| 157 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 158 | void LICM::hoist(Instruction &Inst) { |
| 159 | if (Inst.use_empty()) return; // Don't (re) hoist dead instructions! |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 160 | //cerr << "Hoisting " << Inst; |
| 161 | |
| 162 | BasicBlock *Header = CurLoop->getHeader(); |
| 163 | |
| 164 | // Old instruction will be removed, so take it's name... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 165 | string InstName = Inst.getName(); |
| 166 | Inst.setName(""); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 167 | |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 168 | if (isa<LoadInst>(Inst)) |
| 169 | ++NumHoistedLoads; |
| 170 | |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 171 | // The common case is that we have a pre-header. Generate special case code |
| 172 | // that is faster if that is the case. |
| 173 | // |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 174 | BasicBlock *Preheader = CurLoop->getLoopPreheader(); |
| 175 | assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!"); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 177 | // Create a new copy of the instruction, for insertion into Preheader. |
| 178 | Instruction *New = Inst.clone(); |
| 179 | New->setName(InstName); |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 180 | |
Chris Lattner | 718b221 | 2002-09-26 16:38:03 +0000 | [diff] [blame] | 181 | // Insert the new node in Preheader, before the terminator. |
| 182 | Preheader->getInstList().insert(--Preheader->end(), New); |
| 183 | |
| 184 | // Kill the old instruction... |
| 185 | Inst.replaceAllUsesWith(New); |
| 186 | ++NumHoisted; |
Chris Lattner | 6ec05f5 | 2002-05-10 22:44:58 +0000 | [diff] [blame] | 187 | |
| 188 | Changed = true; |
| 189 | } |
| 190 | |
Chris Lattner | d771fdf | 2002-09-26 16:19:31 +0000 | [diff] [blame] | 191 | |
| 192 | void LICM::visitLoadInst(LoadInst &LI) { |
| 193 | if (isLoopInvariant(LI.getOperand(0)) && |
| 194 | !pointerInvalidatedByLoop(LI.getOperand(0))) |
| 195 | hoist(LI); |
| 196 | |
| 197 | } |
| 198 | |
Chris Lattner | a51fa88 | 2002-08-22 21:39:55 +0000 | [diff] [blame] | 199 | // pointerInvalidatedByLoop - Return true if the body of this loop may store |
| 200 | // into the memory location pointed to by V. |
| 201 | // |
| 202 | bool LICM::pointerInvalidatedByLoop(Value *V) { |
| 203 | // Check to see if any of the basic blocks in CurLoop invalidate V. |
| 204 | for (unsigned i = 0, e = CurLoop->getBlocks().size(); i != e; ++i) |
| 205 | if (AA->canBasicBlockModify(*CurLoop->getBlocks()[i], V)) |
| 206 | return true; |
| 207 | return false; |
| 208 | } |