blob: 37ba34c86f2ba7667c956b19ed9ba1529164dae1 [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
26 // This transformation requires natural loop information...
27 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
28 AU.preservesCFG();
Chris Lattnerd771fdf2002-09-26 16:19:31 +000029 AU.addRequiredID(LoopPreheadersID);
Chris Lattnerf0ed55d2002-08-08 19:01:30 +000030 AU.addRequired<LoopInfo>();
Chris Lattnera51fa882002-08-22 21:39:55 +000031 AU.addRequired<AliasAnalysis>();
Chris Lattner6ec05f52002-05-10 22:44:58 +000032 }
33
34 private:
Chris Lattnera51fa882002-08-22 21:39:55 +000035 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 Lattner6ec05f52002-05-10 22:44:58 +000038
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 Lattner113f4f42002-06-25 16:13:24 +000056 void hoist(Instruction &I);
Chris Lattner6ec05f52002-05-10 22:44:58 +000057
Chris Lattnera51fa882002-08-22 21:39:55 +000058 // 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 Lattner6ec05f52002-05-10 22:44:58 +000063 // 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 Lattner113f4f42002-06-25 16:13:24 +000077 void visitBinaryOperator(Instruction &I) {
78 if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
Chris Lattner6ec05f52002-05-10 22:44:58 +000079 hoist(I);
80 }
Chris Lattnerb80b69c2002-08-14 18:22:19 +000081 void visitCastInst(CastInst &CI) {
82 Instruction &I = (Instruction&)CI;
83 if (isLoopInvariant(I.getOperand(0))) hoist(I);
Chris Lattnerb193ff82002-08-14 18:18:02 +000084 }
Chris Lattner113f4f42002-06-25 16:13:24 +000085 void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
Chris Lattner6ec05f52002-05-10 22:44:58 +000086
Chris Lattnerd771fdf2002-09-26 16:19:31 +000087 void visitLoadInst(LoadInst &LI);
Chris Lattnera51fa882002-08-22 21:39:55 +000088
Chris Lattner113f4f42002-06-25 16:13:24 +000089 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 Lattner6ec05f52002-05-10 22:44:58 +000093 hoist(I);
94 }
95 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000096
Chris Lattnerc8b70922002-07-26 21:12:46 +000097 RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
Chris Lattner6ec05f52002-05-10 22:44:58 +000098}
99
100Pass *createLICMPass() { return new LICM(); }
101
Chris Lattner113f4f42002-06-25 16:13:24 +0000102bool LICM::runOnFunction(Function &) {
Chris Lattner6ec05f52002-05-10 22:44:58 +0000103 // get our loop information...
104 const std::vector<Loop*> &TopLevelLoops =
105 getAnalysis<LoopInfo>().getTopLevelLoops();
106
Chris Lattnera51fa882002-08-22 21:39:55 +0000107 // Get our alias analysis information...
108 AA = &getAnalysis<AliasAnalysis>();
109
Chris Lattner6ec05f52002-05-10 22:44:58 +0000110 // 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
119void 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 Lattner6ec05f52002-05-10 22:44:58 +0000125 // 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 Lattner6ec05f52002-05-10 22:44:58 +0000144}
145
146void LICM::visitBasicBlock(BasicBlock *BB) {
Chris Lattner113f4f42002-06-25 16:13:24 +0000147 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
148 visit(*I);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000149
Chris Lattner113f4f42002-06-25 16:13:24 +0000150 if (dceInstruction(I))
Chris Lattner6ec05f52002-05-10 22:44:58 +0000151 Changed = true;
152 else
Chris Lattner113f4f42002-06-25 16:13:24 +0000153 ++I;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000154 }
155}
156
157
Chris Lattner113f4f42002-06-25 16:13:24 +0000158void LICM::hoist(Instruction &Inst) {
159 if (Inst.use_empty()) return; // Don't (re) hoist dead instructions!
Chris Lattner6ec05f52002-05-10 22:44:58 +0000160 //cerr << "Hoisting " << Inst;
161
162 BasicBlock *Header = CurLoop->getHeader();
163
164 // Old instruction will be removed, so take it's name...
Chris Lattner113f4f42002-06-25 16:13:24 +0000165 string InstName = Inst.getName();
166 Inst.setName("");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000167
Chris Lattnera51fa882002-08-22 21:39:55 +0000168 if (isa<LoadInst>(Inst))
169 ++NumHoistedLoads;
170
Chris Lattner6ec05f52002-05-10 22:44:58 +0000171 // 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 Lattner718b2212002-09-26 16:38:03 +0000174 BasicBlock *Preheader = CurLoop->getLoopPreheader();
175 assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000176
Chris Lattner718b2212002-09-26 16:38:03 +0000177 // Create a new copy of the instruction, for insertion into Preheader.
178 Instruction *New = Inst.clone();
179 New->setName(InstName);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000180
Chris Lattner718b2212002-09-26 16:38:03 +0000181 // 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 Lattner6ec05f52002-05-10 22:44:58 +0000187
188 Changed = true;
189}
190
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000191
192void LICM::visitLoadInst(LoadInst &LI) {
193 if (isLoopInvariant(LI.getOperand(0)) &&
194 !pointerInvalidatedByLoop(LI.getOperand(0)))
195 hoist(LI);
196
197}
198
Chris Lattnera51fa882002-08-22 21:39:55 +0000199// pointerInvalidatedByLoop - Return true if the body of this loop may store
200// into the memory location pointed to by V.
201//
202bool 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}