blob: 1cb899bf6e2bc41910e5fbfd2eca0bcd05275058 [file] [log] [blame]
Chris Lattnere0e734e2002-05-10 22:44:58 +00001//===-- LICM.cpp - Loop Invariant Code Motion Pass ------------------------===//
2//
3// This pass is a simple loop invariant code motion pass.
4//
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.
11//
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
14// REQUIRE it. :)
15//
16//===----------------------------------------------------------------------===//
17
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"
27#include <algorithm>
Anand Shukla5ba99bd2002-06-25 21:07:58 +000028using std::string;
Chris Lattnere0e734e2002-05-10 22:44:58 +000029
30static Statistic<> NumHoistedNPH("licm\t\t- Number of insts hoisted to multiple"
31 " loop preds (bad, no loop pre-header)");
32static Statistic<> NumHoistedPH("licm\t\t- Number of insts hoisted to a loop "
33 "pre-header");
34
35namespace {
36 struct LICM : public FunctionPass, public InstVisitor<LICM> {
Chris Lattner7e708292002-06-25 16:13:24 +000037 virtual bool runOnFunction(Function &F);
Chris Lattnere0e734e2002-05-10 22:44:58 +000038
39 // This transformation requires natural loop information...
40 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41 AU.preservesCFG();
Chris Lattner5f0eb8d2002-08-08 19:01:30 +000042 AU.addRequired<LoopInfo>();
Chris Lattnere0e734e2002-05-10 22:44:58 +000043 }
44
45 private:
46 // List of predecessor blocks for the current loop - These blocks are where
47 // we hoist loop invariants to for the current loop.
48 //
49 std::vector<BasicBlock*> LoopPreds, LoopBackEdges;
50
51 Loop *CurLoop; // The current loop we are working on...
52 bool Changed; // Set to true when we change anything.
53
54 // visitLoop - Hoist expressions out of the specified loop...
55 void visitLoop(Loop *L);
56
57 // notInCurrentLoop - Little predicate that returns true if the specified
58 // basic block is in a subloop of the current one, not the current one
59 // itself.
60 //
61 bool notInCurrentLoop(BasicBlock *BB) {
62 for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
63 if (CurLoop->getSubLoops()[i]->contains(BB))
64 return true; // A subloop actually contains this block!
65 return false;
66 }
67
68 // hoist - When an instruction is found to only use loop invariant operands
69 // that is safe to hoist, this instruction is called to do the dirty work.
70 //
Chris Lattner7e708292002-06-25 16:13:24 +000071 void hoist(Instruction &I);
Chris Lattnere0e734e2002-05-10 22:44:58 +000072
73 // isLoopInvariant - Return true if the specified value is loop invariant
74 inline bool isLoopInvariant(Value *V) {
75 if (Instruction *I = dyn_cast<Instruction>(V))
76 return !CurLoop->contains(I->getParent());
77 return true; // All non-instructions are loop invariant
78 }
79
80 // visitBasicBlock - Run LICM on a particular block.
81 void visitBasicBlock(BasicBlock *BB);
82
83 // Instruction visitation handlers... these basically control whether or not
84 // the specified instruction types are hoisted.
85 //
86 friend class InstVisitor<LICM>;
Chris Lattner7e708292002-06-25 16:13:24 +000087 void visitBinaryOperator(Instruction &I) {
88 if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
Chris Lattnere0e734e2002-05-10 22:44:58 +000089 hoist(I);
90 }
Chris Lattner9b2b80f2002-08-14 18:22:19 +000091 void visitCastInst(CastInst &CI) {
92 Instruction &I = (Instruction&)CI;
93 if (isLoopInvariant(I.getOperand(0))) hoist(I);
Chris Lattner0513e9f2002-08-14 18:18:02 +000094 }
Chris Lattner7e708292002-06-25 16:13:24 +000095 void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
Chris Lattnere0e734e2002-05-10 22:44:58 +000096
Chris Lattner7e708292002-06-25 16:13:24 +000097 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
98 Instruction &I = (Instruction&)GEPI;
99 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
100 if (!isLoopInvariant(I.getOperand(i))) return;
Chris Lattnere0e734e2002-05-10 22:44:58 +0000101 hoist(I);
102 }
103 };
Chris Lattnerf6293092002-07-23 18:06:35 +0000104
Chris Lattnera6275cc2002-07-26 21:12:46 +0000105 RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
Chris Lattnere0e734e2002-05-10 22:44:58 +0000106}
107
108Pass *createLICMPass() { return new LICM(); }
109
Chris Lattner7e708292002-06-25 16:13:24 +0000110bool LICM::runOnFunction(Function &) {
Chris Lattnere0e734e2002-05-10 22:44:58 +0000111 // get our loop information...
112 const std::vector<Loop*> &TopLevelLoops =
113 getAnalysis<LoopInfo>().getTopLevelLoops();
114
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
124void 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));
128 CurLoop = L;
129
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.
133 //
134 BasicBlock *Header = L->getHeader();
135
136 // Calculate the sets of predecessors and backedges of the loop...
137 LoopBackEdges.insert(LoopBackEdges.end(),pred_begin(Header),pred_end(Header));
138
139 std::vector<BasicBlock*>::iterator LPI =
140 std::partition(LoopBackEdges.begin(), LoopBackEdges.end(),
141 bind_obj(CurLoop, &Loop::contains));
142
143 // Move all predecessors to the LoopPreds vector...
144 LoopPreds.insert(LoopPreds.end(), LPI, LoopBackEdges.end());
145
146 // Remove predecessors from backedges list...
147 LoopBackEdges.erase(LPI, LoopBackEdges.end());
148
149
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.
153 //
154 if (LoopPreds.empty()) return;
155
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
159 // the subloops).
160 //
161 std::vector<BasicBlock*> BBs(L->getBlocks().begin(), L->getBlocks().end());
162
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());
166
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
169 // unfortunately...
170 //
171 for_each(BBs.begin(), BBs.end(), bind_obj(this, &LICM::visitBasicBlock));
172
173 // Clear out loops state information for the next iteration
174 CurLoop = 0;
175 LoopPreds.clear();
176 LoopBackEdges.clear();
177}
178
179void LICM::visitBasicBlock(BasicBlock *BB) {
Chris Lattner7e708292002-06-25 16:13:24 +0000180 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
181 visit(*I);
Chris Lattnere0e734e2002-05-10 22:44:58 +0000182
Chris Lattner7e708292002-06-25 16:13:24 +0000183 if (dceInstruction(I))
Chris Lattnere0e734e2002-05-10 22:44:58 +0000184 Changed = true;
185 else
Chris Lattner7e708292002-06-25 16:13:24 +0000186 ++I;
Chris Lattnere0e734e2002-05-10 22:44:58 +0000187 }
188}
189
190
Chris Lattner7e708292002-06-25 16:13:24 +0000191void LICM::hoist(Instruction &Inst) {
192 if (Inst.use_empty()) return; // Don't (re) hoist dead instructions!
Chris Lattnere0e734e2002-05-10 22:44:58 +0000193 //cerr << "Hoisting " << Inst;
194
195 BasicBlock *Header = CurLoop->getHeader();
196
197 // Old instruction will be removed, so take it's name...
Chris Lattner7e708292002-06-25 16:13:24 +0000198 string InstName = Inst.getName();
199 Inst.setName("");
Chris Lattnere0e734e2002-05-10 22:44:58 +0000200
201 // The common case is that we have a pre-header. Generate special case code
202 // that is faster if that is the case.
203 //
204 if (LoopPreds.size() == 1) {
205 BasicBlock *Pred = LoopPreds[0];
206
207 // Create a new copy of the instruction, for insertion into Pred.
Chris Lattner7e708292002-06-25 16:13:24 +0000208 Instruction *New = Inst.clone();
Chris Lattnere0e734e2002-05-10 22:44:58 +0000209 New->setName(InstName);
210
211 // Insert the new node in Pred, before the terminator.
Chris Lattner7e708292002-06-25 16:13:24 +0000212 Pred->getInstList().insert(--Pred->end(), New);
Chris Lattnere0e734e2002-05-10 22:44:58 +0000213
Chris Lattner7e708292002-06-25 16:13:24 +0000214 // Kill the old instruction...
215 Inst.replaceAllUsesWith(New);
Chris Lattnere0e734e2002-05-10 22:44:58 +0000216 ++NumHoistedPH;
217
218 } else {
219 // No loop pre-header, insert a PHI node into header to capture all of the
220 // incoming versions of the value.
221 //
Chris Lattner7e708292002-06-25 16:13:24 +0000222 PHINode *LoopVal = new PHINode(Inst.getType(), InstName+".phi");
Chris Lattnere0e734e2002-05-10 22:44:58 +0000223
224 // Insert the new PHI node into the loop header...
225 Header->getInstList().push_front(LoopVal);
226
227 // Insert cloned versions of the instruction into all of the loop preds.
228 for (unsigned i = 0, e = LoopPreds.size(); i != e; ++i) {
229 BasicBlock *Pred = LoopPreds[i];
230
231 // Create a new copy of the instruction, for insertion into Pred.
Chris Lattner7e708292002-06-25 16:13:24 +0000232 Instruction *New = Inst.clone();
Chris Lattnere0e734e2002-05-10 22:44:58 +0000233 New->setName(InstName);
234
235 // Insert the new node in Pred, before the terminator.
Chris Lattner7e708292002-06-25 16:13:24 +0000236 Pred->getInstList().insert(--Pred->end(), New);
Chris Lattnere0e734e2002-05-10 22:44:58 +0000237
238 // Add the incoming value to the PHI node.
239 LoopVal->addIncoming(New, Pred);
240 }
241
242 // Add incoming values to the PHI node for all backedges in the loop...
243 for (unsigned i = 0, e = LoopBackEdges.size(); i != e; ++i)
244 LoopVal->addIncoming(LoopVal, LoopBackEdges[i]);
245
246 // Replace all uses of the old version of the instruction in the loop with
247 // the new version that is out of the loop. We know that this is ok,
248 // because the new definition is in the loop header, which dominates the
249 // entire loop body. The old definition was defined _inside_ of the loop,
250 // so the scope cannot extend outside of the loop, so we're ok.
251 //
Chris Lattner7e708292002-06-25 16:13:24 +0000252 Inst.replaceAllUsesWith(LoopVal);
Chris Lattnere0e734e2002-05-10 22:44:58 +0000253 ++NumHoistedNPH;
254 }
255
256 Changed = true;
257}
258