blob: 549bf60044aa047e625e297427c4f2b46b1af35f [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"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000016#include "Support/Statistic.h"
Chris Lattner05e86302002-09-29 22:26:07 +000017#include "llvm/Assembly/Writer.h"
Chris Lattner6ec05f52002-05-10 22:44:58 +000018#include <algorithm>
Anand Shukla2bc64192002-06-25 21:07:58 +000019using std::string;
Chris Lattner6ec05f52002-05-10 22:44:58 +000020
Chris Lattner6ec05f52002-05-10 22:44:58 +000021namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000022 Statistic<> NumHoisted("licm", "Number of instructions hoisted out of loop");
23 Statistic<> NumHoistedLoads("licm", "Number of load insts hoisted");
Chris Lattner718b2212002-09-26 16:38:03 +000024
Chris Lattner6ec05f52002-05-10 22:44:58 +000025 struct LICM : public FunctionPass, public InstVisitor<LICM> {
Chris Lattner113f4f42002-06-25 16:13:24 +000026 virtual bool runOnFunction(Function &F);
Chris Lattner6ec05f52002-05-10 22:44:58 +000027
Chris Lattnerf64f2d32002-09-26 16:52:07 +000028 /// This transformation requires natural loop information & requires that
29 /// loop preheaders be inserted into the CFG...
30 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000031 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner820d9712002-10-21 20:00:28 +000032 AU.setPreservesCFG();
Chris Lattnerd771fdf2002-09-26 16:19:31 +000033 AU.addRequiredID(LoopPreheadersID);
Chris Lattnerf0ed55d2002-08-08 19:01:30 +000034 AU.addRequired<LoopInfo>();
Chris Lattner64437692002-09-29 21:46:09 +000035 AU.addRequired<DominatorTree>();
Chris Lattnera51fa882002-08-22 21:39:55 +000036 AU.addRequired<AliasAnalysis>();
Chris Lattner6ec05f52002-05-10 22:44:58 +000037 }
38
39 private:
Chris Lattnerd57f3f52002-09-26 19:40:25 +000040 Loop *CurLoop; // The current loop we are working on...
41 BasicBlock *Preheader; // The preheader block of the current loop...
42 bool Changed; // Set to true when we change anything.
43 AliasAnalysis *AA; // Currently AliasAnalysis information
Chris Lattner6ec05f52002-05-10 22:44:58 +000044
Chris Lattnerf64f2d32002-09-26 16:52:07 +000045 /// visitLoop - Hoist expressions out of the specified loop...
46 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000047 void visitLoop(Loop *L);
48
Chris Lattner64437692002-09-29 21:46:09 +000049 /// HoistRegion - Walk the specified region of the CFG (defined by all
50 /// blocks dominated by the specified block, and that are in the current
51 /// loop) in depth first order w.r.t the DominatorTree. This allows us to
52 /// visit defintions before uses, allowing us to hoist a loop body in one
53 /// pass without iteration.
54 ///
55 void HoistRegion(DominatorTree::Node *N);
56
Chris Lattner05e86302002-09-29 22:26:07 +000057 /// inSubLoop - Little predicate that returns true if the specified basic
58 /// block is in a subloop of the current one, not the current one itself.
Chris Lattnerf64f2d32002-09-26 16:52:07 +000059 ///
Chris Lattner05e86302002-09-29 22:26:07 +000060 bool inSubLoop(BasicBlock *BB) {
61 assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
Chris Lattner6ec05f52002-05-10 22:44:58 +000062 for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
63 if (CurLoop->getSubLoops()[i]->contains(BB))
Chris Lattner05e86302002-09-29 22:26:07 +000064 return true; // A subloop actually contains this block!
65 return false;
Chris Lattner6ec05f52002-05-10 22:44:58 +000066 }
67
Chris Lattnerf64f2d32002-09-26 16:52:07 +000068 /// 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 Lattner113f4f42002-06-25 16:13:24 +000071 void hoist(Instruction &I);
Chris Lattner6ec05f52002-05-10 22:44:58 +000072
Chris Lattnerf64f2d32002-09-26 16:52:07 +000073 /// pointerInvalidatedByLoop - Return true if the body of this loop may
74 /// store into the memory location pointed to by V.
75 ///
Chris Lattnera51fa882002-08-22 21:39:55 +000076 bool pointerInvalidatedByLoop(Value *V);
77
Chris Lattnerf64f2d32002-09-26 16:52:07 +000078 /// isLoopInvariant - Return true if the specified value is loop invariant
79 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000080 inline bool isLoopInvariant(Value *V) {
81 if (Instruction *I = dyn_cast<Instruction>(V))
82 return !CurLoop->contains(I->getParent());
83 return true; // All non-instructions are loop invariant
84 }
85
Chris Lattnerf64f2d32002-09-26 16:52:07 +000086 /// Instruction visitation handlers... these basically control whether or
87 /// not the specified instruction types are hoisted.
88 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000089 friend class InstVisitor<LICM>;
Chris Lattner113f4f42002-06-25 16:13:24 +000090 void visitBinaryOperator(Instruction &I) {
91 if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
Chris Lattner6ec05f52002-05-10 22:44:58 +000092 hoist(I);
93 }
Chris Lattnerb80b69c2002-08-14 18:22:19 +000094 void visitCastInst(CastInst &CI) {
95 Instruction &I = (Instruction&)CI;
96 if (isLoopInvariant(I.getOperand(0))) hoist(I);
Chris Lattnerb193ff82002-08-14 18:18:02 +000097 }
Chris Lattner113f4f42002-06-25 16:13:24 +000098 void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
Chris Lattner6ec05f52002-05-10 22:44:58 +000099
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000100 void visitLoadInst(LoadInst &LI);
Chris Lattnera51fa882002-08-22 21:39:55 +0000101
Chris Lattner113f4f42002-06-25 16:13:24 +0000102 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
103 Instruction &I = (Instruction&)GEPI;
104 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
105 if (!isLoopInvariant(I.getOperand(i))) return;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000106 hoist(I);
107 }
108 };
Chris Lattnerb28b6802002-07-23 18:06:35 +0000109
Chris Lattnerc8b70922002-07-26 21:12:46 +0000110 RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000111}
112
113Pass *createLICMPass() { return new LICM(); }
114
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000115/// runOnFunction - For LICM, this simply traverses the loop structure of the
116/// function, hoisting expressions out of loops if possible.
117///
Chris Lattner113f4f42002-06-25 16:13:24 +0000118bool LICM::runOnFunction(Function &) {
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000119 // Get information about the top level loops in the function...
Chris Lattner6ec05f52002-05-10 22:44:58 +0000120 const std::vector<Loop*> &TopLevelLoops =
121 getAnalysis<LoopInfo>().getTopLevelLoops();
122
Chris Lattnera51fa882002-08-22 21:39:55 +0000123 // Get our alias analysis information...
124 AA = &getAnalysis<AliasAnalysis>();
125
Chris Lattner6ec05f52002-05-10 22:44:58 +0000126 // Traverse loops in postorder, hoisting expressions out of the deepest loops
127 // first.
128 //
129 Changed = false;
130 std::for_each(TopLevelLoops.begin(), TopLevelLoops.end(),
131 bind_obj(this, &LICM::visitLoop));
132 return Changed;
133}
134
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000135
136/// visitLoop - Hoist expressions out of the specified loop...
137///
Chris Lattner6ec05f52002-05-10 22:44:58 +0000138void LICM::visitLoop(Loop *L) {
139 // Recurse through all subloops before we process this loop...
140 std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(),
141 bind_obj(this, &LICM::visitLoop));
142 CurLoop = L;
143
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000144 // Get the preheader block to move instructions into...
145 Preheader = L->getLoopPreheader();
146 assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
147
Chris Lattner6ec05f52002-05-10 22:44:58 +0000148 // We want to visit all of the instructions in this loop... that are not parts
149 // of our subloops (they have already had their invariants hoisted out of
150 // their loop, into this loop, so there is no need to process the BODIES of
151 // the subloops).
152 //
Chris Lattner64437692002-09-29 21:46:09 +0000153 // Traverse the body of the loop in depth first order on the dominator tree so
154 // that we are guaranteed to see definitions before we see uses. This allows
155 // us to perform the LICM transformation in one pass, without iteration.
156 //
157 HoistRegion(getAnalysis<DominatorTree>()[L->getHeader()]);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000158
159 // Clear out loops state information for the next iteration
160 CurLoop = 0;
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000161 Preheader = 0;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000162}
163
Chris Lattner64437692002-09-29 21:46:09 +0000164/// HoistRegion - Walk the specified region of the CFG (defined by all blocks
165/// dominated by the specified block, and that are in the current loop) in depth
166/// first order w.r.t the DominatorTree. This allows us to visit defintions
167/// before uses, allowing us to hoist a loop body in one pass without iteration.
168///
169void LICM::HoistRegion(DominatorTree::Node *N) {
170 assert(N != 0 && "Null dominator tree node?");
171
Chris Lattner05e86302002-09-29 22:26:07 +0000172 // If this subregion is not in the top level loop at all, exit.
173 if (!CurLoop->contains(N->getNode())) return;
Chris Lattner64437692002-09-29 21:46:09 +0000174
Chris Lattner05e86302002-09-29 22:26:07 +0000175 // Only need to hoist the contents of this block if it is not part of a
176 // subloop (which would already have been hoisted)
177 if (!inSubLoop(N->getNode()))
178 visit(*N->getNode());
Chris Lattner64437692002-09-29 21:46:09 +0000179
180 const std::vector<DominatorTree::Node*> &Children = N->getChildren();
181 for (unsigned i = 0, e = Children.size(); i != e; ++i)
182 HoistRegion(Children[i]);
183}
184
185
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000186/// hoist - When an instruction is found to only use loop invariant operands
187/// that is safe to hoist, this instruction is called to do the dirty work.
188///
Chris Lattner113f4f42002-06-25 16:13:24 +0000189void LICM::hoist(Instruction &Inst) {
Chris Lattner05e86302002-09-29 22:26:07 +0000190 DEBUG(std::cerr << "LICM hoisting to";
191 WriteAsOperand(std::cerr, Preheader, false);
192 std::cerr << ": " << Inst);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000193
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000194 // Remove the instruction from its current basic block... but don't delete the
195 // instruction.
196 Inst.getParent()->getInstList().remove(&Inst);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000197
Chris Lattner718b2212002-09-26 16:38:03 +0000198 // Insert the new node in Preheader, before the terminator.
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000199 Preheader->getInstList().insert(Preheader->getTerminator(), &Inst);
Chris Lattner718b2212002-09-26 16:38:03 +0000200
Chris Lattner718b2212002-09-26 16:38:03 +0000201 ++NumHoisted;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000202 Changed = true;
203}
204
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000205
206void LICM::visitLoadInst(LoadInst &LI) {
207 if (isLoopInvariant(LI.getOperand(0)) &&
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000208 !pointerInvalidatedByLoop(LI.getOperand(0))) {
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000209 hoist(LI);
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000210 ++NumHoistedLoads;
211 }
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000212}
213
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000214/// pointerInvalidatedByLoop - Return true if the body of this loop may store
215/// into the memory location pointed to by V.
216///
Chris Lattnera51fa882002-08-22 21:39:55 +0000217bool LICM::pointerInvalidatedByLoop(Value *V) {
218 // Check to see if any of the basic blocks in CurLoop invalidate V.
219 for (unsigned i = 0, e = CurLoop->getBlocks().size(); i != e; ++i)
220 if (AA->canBasicBlockModify(*CurLoop->getBlocks()[i], V))
221 return true;
222 return false;
223}