blob: 88abccda00b8351cf48dd384375595a0957afd25 [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 Lattner64437692002-09-29 21:46:09 +000011#include "llvm/Analysis/Dominators.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000012#include "llvm/iOperators.h"
Chris Lattnera51fa882002-08-22 21:39:55 +000013#include "llvm/iMemory.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000014#include "llvm/Support/InstVisitor.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000015#include "Support/STLExtras.h"
16#include "Support/StatisticReporter.h"
17#include <algorithm>
Anand Shukla2bc64192002-06-25 21:07:58 +000018using std::string;
Chris Lattner6ec05f52002-05-10 22:44:58 +000019
Chris Lattner6ec05f52002-05-10 22:44:58 +000020namespace {
Chris Lattner718b2212002-09-26 16:38:03 +000021 Statistic<>NumHoisted("licm\t\t- Number of instructions hoisted out of loop");
22 Statistic<> NumHoistedLoads("licm\t\t- Number of load insts hoisted");
23
Chris Lattner6ec05f52002-05-10 22:44:58 +000024 struct LICM : public FunctionPass, public InstVisitor<LICM> {
Chris Lattner113f4f42002-06-25 16:13:24 +000025 virtual bool runOnFunction(Function &F);
Chris Lattner6ec05f52002-05-10 22:44:58 +000026
Chris Lattnerf64f2d32002-09-26 16:52:07 +000027 /// This transformation requires natural loop information & requires that
28 /// loop preheaders be inserted into the CFG...
29 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000030 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
31 AU.preservesCFG();
Chris Lattnerd771fdf2002-09-26 16:19:31 +000032 AU.addRequiredID(LoopPreheadersID);
Chris Lattnerf0ed55d2002-08-08 19:01:30 +000033 AU.addRequired<LoopInfo>();
Chris Lattner64437692002-09-29 21:46:09 +000034 AU.addRequired<DominatorTree>();
Chris Lattnera51fa882002-08-22 21:39:55 +000035 AU.addRequired<AliasAnalysis>();
Chris Lattner6ec05f52002-05-10 22:44:58 +000036 }
37
38 private:
Chris Lattnerd57f3f52002-09-26 19:40:25 +000039 Loop *CurLoop; // The current loop we are working on...
40 BasicBlock *Preheader; // The preheader block of the current loop...
41 bool Changed; // Set to true when we change anything.
42 AliasAnalysis *AA; // Currently AliasAnalysis information
Chris Lattner6ec05f52002-05-10 22:44:58 +000043
Chris Lattnerf64f2d32002-09-26 16:52:07 +000044 /// visitLoop - Hoist expressions out of the specified loop...
45 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000046 void visitLoop(Loop *L);
47
Chris Lattner64437692002-09-29 21:46:09 +000048 /// HoistRegion - Walk the specified region of the CFG (defined by all
49 /// blocks dominated by the specified block, and that are in the current
50 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
51 /// visit defintions before uses, allowing us to hoist a loop body in one
52 /// pass without iteration.
53 ///
54 void HoistRegion(DominatorTree::Node *N);
55
Chris Lattnerd57f3f52002-09-26 19:40:25 +000056 /// inCurrentLoop - Little predicate that returns false if the specified
Chris Lattnerf64f2d32002-09-26 16:52:07 +000057 /// basic block is in a subloop of the current one, not the current one
58 /// itself.
59 ///
Chris Lattnerd57f3f52002-09-26 19:40:25 +000060 bool inCurrentLoop(BasicBlock *BB) {
Chris Lattner6ec05f52002-05-10 22:44:58 +000061 for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
62 if (CurLoop->getSubLoops()[i]->contains(BB))
Chris Lattnerd57f3f52002-09-26 19:40:25 +000063 return false; // A subloop actually contains this block!
64 return true;
Chris Lattner6ec05f52002-05-10 22:44:58 +000065 }
66
Chris Lattnerf64f2d32002-09-26 16:52:07 +000067 /// hoist - When an instruction is found to only use loop invariant operands
68 /// that is safe to hoist, this instruction is called to do the dirty work.
69 ///
Chris Lattner113f4f42002-06-25 16:13:24 +000070 void hoist(Instruction &I);
Chris Lattner6ec05f52002-05-10 22:44:58 +000071
Chris Lattnerf64f2d32002-09-26 16:52:07 +000072 /// pointerInvalidatedByLoop - Return true if the body of this loop may
73 /// store into the memory location pointed to by V.
74 ///
Chris Lattnera51fa882002-08-22 21:39:55 +000075 bool pointerInvalidatedByLoop(Value *V);
76
Chris Lattnerf64f2d32002-09-26 16:52:07 +000077 /// isLoopInvariant - Return true if the specified value is loop invariant
78 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000079 inline bool isLoopInvariant(Value *V) {
80 if (Instruction *I = dyn_cast<Instruction>(V))
81 return !CurLoop->contains(I->getParent());
82 return true; // All non-instructions are loop invariant
83 }
84
Chris Lattnerf64f2d32002-09-26 16:52:07 +000085 /// Instruction visitation handlers... these basically control whether or
86 /// not the specified instruction types are hoisted.
87 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000088 friend class InstVisitor<LICM>;
Chris Lattner113f4f42002-06-25 16:13:24 +000089 void visitBinaryOperator(Instruction &I) {
90 if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
Chris Lattner6ec05f52002-05-10 22:44:58 +000091 hoist(I);
92 }
Chris Lattnerb80b69c2002-08-14 18:22:19 +000093 void visitCastInst(CastInst &CI) {
94 Instruction &I = (Instruction&)CI;
95 if (isLoopInvariant(I.getOperand(0))) hoist(I);
Chris Lattnerb193ff82002-08-14 18:18:02 +000096 }
Chris Lattner113f4f42002-06-25 16:13:24 +000097 void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
Chris Lattner6ec05f52002-05-10 22:44:58 +000098
Chris Lattnerd771fdf2002-09-26 16:19:31 +000099 void visitLoadInst(LoadInst &LI);
Chris Lattnera51fa882002-08-22 21:39:55 +0000100
Chris Lattner113f4f42002-06-25 16:13:24 +0000101 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
102 Instruction &I = (Instruction&)GEPI;
103 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
104 if (!isLoopInvariant(I.getOperand(i))) return;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000105 hoist(I);
106 }
107 };
Chris Lattnerb28b6802002-07-23 18:06:35 +0000108
Chris Lattnerc8b70922002-07-26 21:12:46 +0000109 RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000110}
111
112Pass *createLICMPass() { return new LICM(); }
113
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000114/// runOnFunction - For LICM, this simply traverses the loop structure of the
115/// function, hoisting expressions out of loops if possible.
116///
Chris Lattner113f4f42002-06-25 16:13:24 +0000117bool LICM::runOnFunction(Function &) {
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000118 // Get information about the top level loops in the function...
Chris Lattner6ec05f52002-05-10 22:44:58 +0000119 const std::vector<Loop*> &TopLevelLoops =
120 getAnalysis<LoopInfo>().getTopLevelLoops();
121
Chris Lattnera51fa882002-08-22 21:39:55 +0000122 // Get our alias analysis information...
123 AA = &getAnalysis<AliasAnalysis>();
124
Chris Lattner6ec05f52002-05-10 22:44:58 +0000125 // Traverse loops in postorder, hoisting expressions out of the deepest loops
126 // first.
127 //
128 Changed = false;
129 std::for_each(TopLevelLoops.begin(), TopLevelLoops.end(),
130 bind_obj(this, &LICM::visitLoop));
131 return Changed;
132}
133
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000134
135/// visitLoop - Hoist expressions out of the specified loop...
136///
Chris Lattner6ec05f52002-05-10 22:44:58 +0000137void LICM::visitLoop(Loop *L) {
138 // Recurse through all subloops before we process this loop...
139 std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(),
140 bind_obj(this, &LICM::visitLoop));
141 CurLoop = L;
142
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000143 // Get the preheader block to move instructions into...
144 Preheader = L->getLoopPreheader();
145 assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
146
Chris Lattner6ec05f52002-05-10 22:44:58 +0000147 // We want to visit all of the instructions in this loop... that are not parts
148 // of our subloops (they have already had their invariants hoisted out of
149 // their loop, into this loop, so there is no need to process the BODIES of
150 // the subloops).
151 //
Chris Lattner64437692002-09-29 21:46:09 +0000152 // Traverse the body of the loop in depth first order on the dominator tree so
153 // that we are guaranteed to see definitions before we see uses. This allows
154 // us to perform the LICM transformation in one pass, without iteration.
155 //
156 HoistRegion(getAnalysis<DominatorTree>()[L->getHeader()]);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000157
158 // Clear out loops state information for the next iteration
159 CurLoop = 0;
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000160 Preheader = 0;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000161}
162
Chris Lattner64437692002-09-29 21:46:09 +0000163/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
164/// dominated by the specified block, and that are in the current loop) in depth
165/// first order w.r.t the DominatorTree. This allows us to visit defintions
166/// before uses, allowing us to hoist a loop body in one pass without iteration.
167///
168void LICM::HoistRegion(DominatorTree::Node *N) {
169 assert(N != 0 && "Null dominator tree node?");
170
171 // This subregion is not in the loop, it has already been already been hoisted
172 if (!inCurrentLoop(N->getNode()))
173 return;
174
175 visit(*N->getNode());
176
177 const std::vector<DominatorTree::Node*> &Children = N->getChildren();
178 for (unsigned i = 0, e = Children.size(); i != e; ++i)
179 HoistRegion(Children[i]);
180}
181
182
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000183/// hoist - When an instruction is found to only use loop invariant operands
184/// that is safe to hoist, this instruction is called to do the dirty work.
185///
Chris Lattner113f4f42002-06-25 16:13:24 +0000186void LICM::hoist(Instruction &Inst) {
Chris Lattner64437692002-09-29 21:46:09 +0000187 DEBUG(std::cerr << "LICM hoisting: " << Inst);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000188
189 BasicBlock *Header = CurLoop->getHeader();
190
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000191 // Remove the instruction from its current basic block... but don't delete the
192 // instruction.
193 Inst.getParent()->getInstList().remove(&Inst);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000194
Chris Lattner718b2212002-09-26 16:38:03 +0000195 // Insert the new node in Preheader, before the terminator.
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000196 Preheader->getInstList().insert(Preheader->getTerminator(), &Inst);
Chris Lattner718b2212002-09-26 16:38:03 +0000197
Chris Lattner718b2212002-09-26 16:38:03 +0000198 ++NumHoisted;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000199 Changed = true;
200}
201
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000202
203void LICM::visitLoadInst(LoadInst &LI) {
204 if (isLoopInvariant(LI.getOperand(0)) &&
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000205 !pointerInvalidatedByLoop(LI.getOperand(0))) {
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000206 hoist(LI);
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000207 ++NumHoistedLoads;
208 }
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000209}
210
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000211/// pointerInvalidatedByLoop - Return true if the body of this loop may store
212/// into the memory location pointed to by V.
213///
Chris Lattnera51fa882002-08-22 21:39:55 +0000214bool LICM::pointerInvalidatedByLoop(Value *V) {
215 // Check to see if any of the basic blocks in CurLoop invalidate V.
216 for (unsigned i = 0, e = CurLoop->getBlocks().size(); i != e; ++i)
217 if (AA->canBasicBlockModify(*CurLoop->getBlocks()[i], V))
218 return true;
219 return false;
220}