blob: cfd946e7eb6e7cc8181a7734de0402cf16e03e82 [file] [log] [blame]
Chris Lattner6ec05f52002-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//
Chris Lattner6ec05f52002-05-10 22:44:58 +00005//===----------------------------------------------------------------------===//
6
7#include "llvm/Transforms/Scalar.h"
8#include "llvm/Transforms/Utils/Local.h"
9#include "llvm/Analysis/LoopInfo.h"
Chris Lattnera51fa882002-08-22 21:39:55 +000010#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000011#include "llvm/iOperators.h"
Chris Lattnera51fa882002-08-22 21:39:55 +000012#include "llvm/iMemory.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000013#include "llvm/Support/InstVisitor.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000014#include "Support/STLExtras.h"
15#include "Support/StatisticReporter.h"
16#include <algorithm>
Anand Shukla2bc64192002-06-25 21:07:58 +000017using std::string;
Chris Lattner6ec05f52002-05-10 22:44:58 +000018
Chris Lattner6ec05f52002-05-10 22:44:58 +000019namespace {
Chris Lattner718b2212002-09-26 16:38:03 +000020 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 Lattner6ec05f52002-05-10 22:44:58 +000023 struct LICM : public FunctionPass, public InstVisitor<LICM> {
Chris Lattner113f4f42002-06-25 16:13:24 +000024 virtual bool runOnFunction(Function &F);
Chris Lattner6ec05f52002-05-10 22:44:58 +000025
Chris Lattnerf64f2d32002-09-26 16:52:07 +000026 /// This transformation requires natural loop information & requires that
27 /// loop preheaders be inserted into the CFG...
28 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000029 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
30 AU.preservesCFG();
Chris Lattnerd771fdf2002-09-26 16:19:31 +000031 AU.addRequiredID(LoopPreheadersID);
Chris Lattnerf0ed55d2002-08-08 19:01:30 +000032 AU.addRequired<LoopInfo>();
Chris Lattnera51fa882002-08-22 21:39:55 +000033 AU.addRequired<AliasAnalysis>();
Chris Lattner6ec05f52002-05-10 22:44:58 +000034 }
35
36 private:
Chris Lattnera51fa882002-08-22 21:39:55 +000037 Loop *CurLoop; // The current loop we are working on...
38 bool Changed; // Set to true when we change anything.
39 AliasAnalysis *AA; // Currently AliasAnalysis information
Chris Lattner6ec05f52002-05-10 22:44:58 +000040
Chris Lattnerf64f2d32002-09-26 16:52:07 +000041 /// visitLoop - Hoist expressions out of the specified loop...
42 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000043 void visitLoop(Loop *L);
44
Chris Lattnerf64f2d32002-09-26 16:52:07 +000045 /// notInCurrentLoop - Little predicate that returns true if the specified
46 /// basic block is in a subloop of the current one, not the current one
47 /// itself.
48 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000049 bool notInCurrentLoop(BasicBlock *BB) {
50 for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
51 if (CurLoop->getSubLoops()[i]->contains(BB))
52 return true; // A subloop actually contains this block!
53 return false;
54 }
55
Chris Lattnerf64f2d32002-09-26 16:52:07 +000056 /// hoist - When an instruction is found to only use loop invariant operands
57 /// that is safe to hoist, this instruction is called to do the dirty work.
58 ///
Chris Lattner113f4f42002-06-25 16:13:24 +000059 void hoist(Instruction &I);
Chris Lattner6ec05f52002-05-10 22:44:58 +000060
Chris Lattnerf64f2d32002-09-26 16:52:07 +000061 /// pointerInvalidatedByLoop - Return true if the body of this loop may
62 /// store into the memory location pointed to by V.
63 ///
Chris Lattnera51fa882002-08-22 21:39:55 +000064 bool pointerInvalidatedByLoop(Value *V);
65
Chris Lattnerf64f2d32002-09-26 16:52:07 +000066 /// isLoopInvariant - Return true if the specified value is loop invariant
67 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000068 inline bool isLoopInvariant(Value *V) {
69 if (Instruction *I = dyn_cast<Instruction>(V))
70 return !CurLoop->contains(I->getParent());
71 return true; // All non-instructions are loop invariant
72 }
73
Chris Lattnerf64f2d32002-09-26 16:52:07 +000074 /// visitBasicBlock - Run LICM on a particular block.
75 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000076 void visitBasicBlock(BasicBlock *BB);
77
Chris Lattnerf64f2d32002-09-26 16:52:07 +000078 /// Instruction visitation handlers... these basically control whether or
79 /// not the specified instruction types are hoisted.
80 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000081 friend class InstVisitor<LICM>;
Chris Lattner113f4f42002-06-25 16:13:24 +000082 void visitBinaryOperator(Instruction &I) {
83 if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
Chris Lattner6ec05f52002-05-10 22:44:58 +000084 hoist(I);
85 }
Chris Lattnerb80b69c2002-08-14 18:22:19 +000086 void visitCastInst(CastInst &CI) {
87 Instruction &I = (Instruction&)CI;
88 if (isLoopInvariant(I.getOperand(0))) hoist(I);
Chris Lattnerb193ff82002-08-14 18:18:02 +000089 }
Chris Lattner113f4f42002-06-25 16:13:24 +000090 void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
Chris Lattner6ec05f52002-05-10 22:44:58 +000091
Chris Lattnerd771fdf2002-09-26 16:19:31 +000092 void visitLoadInst(LoadInst &LI);
Chris Lattnera51fa882002-08-22 21:39:55 +000093
Chris Lattner113f4f42002-06-25 16:13:24 +000094 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
95 Instruction &I = (Instruction&)GEPI;
96 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
97 if (!isLoopInvariant(I.getOperand(i))) return;
Chris Lattner6ec05f52002-05-10 22:44:58 +000098 hoist(I);
99 }
100 };
Chris Lattnerb28b6802002-07-23 18:06:35 +0000101
Chris Lattnerc8b70922002-07-26 21:12:46 +0000102 RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000103}
104
105Pass *createLICMPass() { return new LICM(); }
106
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000107/// runOnFunction - For LICM, this simply traverses the loop structure of the
108/// function, hoisting expressions out of loops if possible.
109///
Chris Lattner113f4f42002-06-25 16:13:24 +0000110bool LICM::runOnFunction(Function &) {
Chris Lattner6ec05f52002-05-10 22:44:58 +0000111 // get our loop information...
112 const std::vector<Loop*> &TopLevelLoops =
113 getAnalysis<LoopInfo>().getTopLevelLoops();
114
Chris Lattnera51fa882002-08-22 21:39:55 +0000115 // Get our alias analysis information...
116 AA = &getAnalysis<AliasAnalysis>();
117
Chris Lattner6ec05f52002-05-10 22:44:58 +0000118 // Traverse loops in postorder, hoisting expressions out of the deepest loops
119 // first.
120 //
121 Changed = false;
122 std::for_each(TopLevelLoops.begin(), TopLevelLoops.end(),
123 bind_obj(this, &LICM::visitLoop));
124 return Changed;
125}
126
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000127
128/// visitLoop - Hoist expressions out of the specified loop...
129///
Chris Lattner6ec05f52002-05-10 22:44:58 +0000130void LICM::visitLoop(Loop *L) {
131 // Recurse through all subloops before we process this loop...
132 std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(),
133 bind_obj(this, &LICM::visitLoop));
134 CurLoop = L;
135
Chris Lattner6ec05f52002-05-10 22:44:58 +0000136 // We want to visit all of the instructions in this loop... that are not parts
137 // of our subloops (they have already had their invariants hoisted out of
138 // their loop, into this loop, so there is no need to process the BODIES of
139 // the subloops).
140 //
141 std::vector<BasicBlock*> BBs(L->getBlocks().begin(), L->getBlocks().end());
142
143 // Remove blocks that are actually in subloops...
144 BBs.erase(std::remove_if(BBs.begin(), BBs.end(),
145 bind_obj(this, &LICM::notInCurrentLoop)), BBs.end());
146
147 // Visit all of the basic blocks we have chosen, hoisting out the instructions
148 // as neccesary. This leaves dead copies of the instruction in the loop
149 // unfortunately...
150 //
151 for_each(BBs.begin(), BBs.end(), bind_obj(this, &LICM::visitBasicBlock));
152
153 // Clear out loops state information for the next iteration
154 CurLoop = 0;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000155}
156
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000157/// visitBasicBlock - Run LICM on a particular block.
158///
Chris Lattner6ec05f52002-05-10 22:44:58 +0000159void LICM::visitBasicBlock(BasicBlock *BB) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000160 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
161 visit(*I);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000162
Chris Lattner113f4f42002-06-25 16:13:24 +0000163 if (dceInstruction(I))
Chris Lattner6ec05f52002-05-10 22:44:58 +0000164 Changed = true;
165 else
Chris Lattner113f4f42002-06-25 16:13:24 +0000166 ++I;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000167 }
168}
169
170
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000171/// hoist - When an instruction is found to only use loop invariant operands
172/// that is safe to hoist, this instruction is called to do the dirty work.
173///
Chris Lattner113f4f42002-06-25 16:13:24 +0000174void LICM::hoist(Instruction &Inst) {
175 if (Inst.use_empty()) return; // Don't (re) hoist dead instructions!
Chris Lattner6ec05f52002-05-10 22:44:58 +0000176 //cerr << "Hoisting " << Inst;
177
178 BasicBlock *Header = CurLoop->getHeader();
179
180 // Old instruction will be removed, so take it's name...
Chris Lattner113f4f42002-06-25 16:13:24 +0000181 string InstName = Inst.getName();
182 Inst.setName("");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000183
Chris Lattnera51fa882002-08-22 21:39:55 +0000184 if (isa<LoadInst>(Inst))
185 ++NumHoistedLoads;
186
Chris Lattner6ec05f52002-05-10 22:44:58 +0000187 // The common case is that we have a pre-header. Generate special case code
188 // that is faster if that is the case.
189 //
Chris Lattner718b2212002-09-26 16:38:03 +0000190 BasicBlock *Preheader = CurLoop->getLoopPreheader();
191 assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000192
Chris Lattner718b2212002-09-26 16:38:03 +0000193 // Create a new copy of the instruction, for insertion into Preheader.
194 Instruction *New = Inst.clone();
195 New->setName(InstName);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000196
Chris Lattner718b2212002-09-26 16:38:03 +0000197 // Insert the new node in Preheader, before the terminator.
198 Preheader->getInstList().insert(--Preheader->end(), New);
199
200 // Kill the old instruction...
201 Inst.replaceAllUsesWith(New);
202 ++NumHoisted;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000203
204 Changed = true;
205}
206
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000207
208void LICM::visitLoadInst(LoadInst &LI) {
209 if (isLoopInvariant(LI.getOperand(0)) &&
210 !pointerInvalidatedByLoop(LI.getOperand(0)))
211 hoist(LI);
212
213}
214
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000215/// pointerInvalidatedByLoop - Return true if the body of this loop may store
216/// into the memory location pointed to by V.
217///
Chris Lattnera51fa882002-08-22 21:39:55 +0000218bool LICM::pointerInvalidatedByLoop(Value *V) {
219 // Check to see if any of the basic blocks in CurLoop invalidate V.
220 for (unsigned i = 0, e = CurLoop->getBlocks().size(); i != e; ++i)
221 if (AA->canBasicBlockModify(*CurLoop->getBlocks()[i], V))
222 return true;
223 return false;
224}