1 //===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
3 // This pass is a simple loop invariant code motion pass.
5 // Note that this pass does NOT require pre-headers to exist on loops in the
6 // CFG, but if there is not distinct preheader for a loop, the hoisted code will
7 // be *DUPLICATED* in every basic block, outside of the loop, that preceeds the
8 // loop header. Additionally, any use of one of these hoisted expressions
9 // cannot be loop invariant itself, because the expression hoisted gets a PHI
10 // node that is loop variant.
12 // For these reasons, and many more, it makes sense to run a pass before this
13 // that ensures that there are preheaders on all loops. That said, we don't
16 //===----------------------------------------------------------------------===//
18 #include "llvm/Transforms/Scalar.h"
19 #include "llvm/Transforms/Utils/Local.h"
20 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/iOperators.h"
22 #include "llvm/iPHINode.h"
23 #include "llvm/Support/InstVisitor.h"
24 #include "llvm/Support/CFG.h"
25 #include "Support/STLExtras.h"
26 #include "Support/StatisticReporter.h"
29 static Statistic<> NumHoistedNPH("licm\t\t- Number of insts hoisted to multiple"
30 " loop preds (bad, no loop pre-header)");
31 static Statistic<> NumHoistedPH("licm\t\t- Number of insts hoisted to a loop "
35 struct LICM : public FunctionPass, public InstVisitor<LICM> {
36 const char *getPassName() const { return "Loop Invariant Code Motion"; }
38 virtual bool runOnFunction(Function *F);
40 // This transformation requires natural loop information...
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequired(LoopInfo::ID);
47 // List of predecessor blocks for the current loop - These blocks are where
48 // we hoist loop invariants to for the current loop.
50 std::vector<BasicBlock*> LoopPreds, LoopBackEdges;
52 Loop *CurLoop; // The current loop we are working on...
53 bool Changed; // Set to true when we change anything.
55 // visitLoop - Hoist expressions out of the specified loop...
56 void visitLoop(Loop *L);
58 // notInCurrentLoop - Little predicate that returns true if the specified
59 // basic block is in a subloop of the current one, not the current one
62 bool notInCurrentLoop(BasicBlock *BB) {
63 for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
64 if (CurLoop->getSubLoops()[i]->contains(BB))
65 return true; // A subloop actually contains this block!
69 // hoist - When an instruction is found to only use loop invariant operands
70 // that is safe to hoist, this instruction is called to do the dirty work.
72 void hoist(Instruction *I);
74 // isLoopInvariant - Return true if the specified value is loop invariant
75 inline bool isLoopInvariant(Value *V) {
76 if (Instruction *I = dyn_cast<Instruction>(V))
77 return !CurLoop->contains(I->getParent());
78 return true; // All non-instructions are loop invariant
81 // visitBasicBlock - Run LICM on a particular block.
82 void visitBasicBlock(BasicBlock *BB);
84 // Instruction visitation handlers... these basically control whether or not
85 // the specified instruction types are hoisted.
87 friend class InstVisitor<LICM>;
88 void visitUnaryOperator(Instruction *I) {
89 if (isLoopInvariant(I->getOperand(0))) hoist(I);
91 void visitBinaryOperator(Instruction *I) {
92 if (isLoopInvariant(I->getOperand(0)) &&isLoopInvariant(I->getOperand(1)))
96 void visitCastInst(CastInst *I) { visitUnaryOperator((Instruction*)I); }
97 void visitShiftInst(ShiftInst *I) { visitBinaryOperator((Instruction*)I); }
99 void visitGetElementPtrInst(GetElementPtrInst *GEPI) {
100 Instruction *I = (Instruction*)GEPI;
101 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
102 if (!isLoopInvariant(I->getOperand(i))) return;
108 Pass *createLICMPass() { return new LICM(); }
110 bool LICM::runOnFunction(Function *F) {
111 // get our loop information...
112 const std::vector<Loop*> &TopLevelLoops =
113 getAnalysis<LoopInfo>().getTopLevelLoops();
115 // Traverse loops in postorder, hoisting expressions out of the deepest loops
119 std::for_each(TopLevelLoops.begin(), TopLevelLoops.end(),
120 bind_obj(this, &LICM::visitLoop));
124 void LICM::visitLoop(Loop *L) {
125 // Recurse through all subloops before we process this loop...
126 std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(),
127 bind_obj(this, &LICM::visitLoop));
130 // Calculate the set of predecessors for this loop. The predecessors for this
131 // loop are equal to the predecessors for the header node of the loop that are
132 // not themselves in the loop.
134 BasicBlock *Header = L->getHeader();
136 // Calculate the sets of predecessors and backedges of the loop...
137 LoopBackEdges.insert(LoopBackEdges.end(),pred_begin(Header),pred_end(Header));
139 std::vector<BasicBlock*>::iterator LPI =
140 std::partition(LoopBackEdges.begin(), LoopBackEdges.end(),
141 bind_obj(CurLoop, &Loop::contains));
143 // Move all predecessors to the LoopPreds vector...
144 LoopPreds.insert(LoopPreds.end(), LPI, LoopBackEdges.end());
146 // Remove predecessors from backedges list...
147 LoopBackEdges.erase(LPI, LoopBackEdges.end());
150 // The only way that there could be no predecessors to a loop is if the loop
151 // is not reachable. Since we don't care about optimizing dead loops,
152 // summarily ignore them.
154 if (LoopPreds.empty()) return;
156 // We want to visit all of the instructions in this loop... that are not parts
157 // of our subloops (they have already had their invariants hoisted out of
158 // their loop, into this loop, so there is no need to process the BODIES of
161 std::vector<BasicBlock*> BBs(L->getBlocks().begin(), L->getBlocks().end());
163 // Remove blocks that are actually in subloops...
164 BBs.erase(std::remove_if(BBs.begin(), BBs.end(),
165 bind_obj(this, &LICM::notInCurrentLoop)), BBs.end());
167 // Visit all of the basic blocks we have chosen, hoisting out the instructions
168 // as neccesary. This leaves dead copies of the instruction in the loop
171 for_each(BBs.begin(), BBs.end(), bind_obj(this, &LICM::visitBasicBlock));
173 // Clear out loops state information for the next iteration
176 LoopBackEdges.clear();
179 void LICM::visitBasicBlock(BasicBlock *BB) {
180 // This cannot use an iterator, because it might get invalidated when PHI
181 // nodes are inserted!
183 for (unsigned i = 0; i < BB->size(); ) {
184 visit(BB->begin()[i]);
186 BasicBlock::iterator It = BB->begin()+i;
187 if (dceInstruction(BB->getInstList(), It))
195 void LICM::hoist(Instruction *Inst) {
196 if (Inst->use_empty()) return; // Don't (re) hoist dead instructions!
197 //cerr << "Hoisting " << Inst;
199 BasicBlock *Header = CurLoop->getHeader();
201 // Old instruction will be removed, so take it's name...
202 string InstName = Inst->getName();
205 // The common case is that we have a pre-header. Generate special case code
206 // that is faster if that is the case.
208 if (LoopPreds.size() == 1) {
209 BasicBlock *Pred = LoopPreds[0];
211 // Create a new copy of the instruction, for insertion into Pred.
212 Instruction *New = Inst->clone();
213 New->setName(InstName);
215 // Insert the new node in Pred, before the terminator.
216 Pred->getInstList().insert(Pred->end()-1, New);
218 // Kill the old instruction.
219 Inst->replaceAllUsesWith(New);
223 // No loop pre-header, insert a PHI node into header to capture all of the
224 // incoming versions of the value.
226 PHINode *LoopVal = new PHINode(Inst->getType(), InstName+".phi");
228 // Insert the new PHI node into the loop header...
229 Header->getInstList().push_front(LoopVal);
231 // Insert cloned versions of the instruction into all of the loop preds.
232 for (unsigned i = 0, e = LoopPreds.size(); i != e; ++i) {
233 BasicBlock *Pred = LoopPreds[i];
235 // Create a new copy of the instruction, for insertion into Pred.
236 Instruction *New = Inst->clone();
237 New->setName(InstName);
239 // Insert the new node in Pred, before the terminator.
240 Pred->getInstList().insert(Pred->end()-1, New);
242 // Add the incoming value to the PHI node.
243 LoopVal->addIncoming(New, Pred);
246 // Add incoming values to the PHI node for all backedges in the loop...
247 for (unsigned i = 0, e = LoopBackEdges.size(); i != e; ++i)
248 LoopVal->addIncoming(LoopVal, LoopBackEdges[i]);
250 // Replace all uses of the old version of the instruction in the loop with
251 // the new version that is out of the loop. We know that this is ok,
252 // because the new definition is in the loop header, which dominates the
253 // entire loop body. The old definition was defined _inside_ of the loop,
254 // so the scope cannot extend outside of the loop, so we're ok.
256 Inst->replaceAllUsesWith(LoopVal);