blob: 9a30fbf9d1f5359398d3792d14c16dcd51f5d3d7 [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 Lattnerd57f3f52002-09-26 19:40:25 +000037 Loop *CurLoop; // The current loop we are working on...
38 BasicBlock *Preheader; // The preheader block of the current loop...
39 bool Changed; // Set to true when we change anything.
40 AliasAnalysis *AA; // Currently AliasAnalysis information
Chris Lattner6ec05f52002-05-10 22:44:58 +000041
Chris Lattnerf64f2d32002-09-26 16:52:07 +000042 /// visitLoop - Hoist expressions out of the specified loop...
43 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000044 void visitLoop(Loop *L);
45
Chris Lattnerd57f3f52002-09-26 19:40:25 +000046 /// inCurrentLoop - Little predicate that returns false if the specified
Chris Lattnerf64f2d32002-09-26 16:52:07 +000047 /// basic block is in a subloop of the current one, not the current one
48 /// itself.
49 ///
Chris Lattnerd57f3f52002-09-26 19:40:25 +000050 bool inCurrentLoop(BasicBlock *BB) {
Chris Lattner6ec05f52002-05-10 22:44:58 +000051 for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
52 if (CurLoop->getSubLoops()[i]->contains(BB))
Chris Lattnerd57f3f52002-09-26 19:40:25 +000053 return false; // A subloop actually contains this block!
54 return true;
Chris Lattner6ec05f52002-05-10 22:44:58 +000055 }
56
Chris Lattnerf64f2d32002-09-26 16:52:07 +000057 /// hoist - When an instruction is found to only use loop invariant operands
58 /// that is safe to hoist, this instruction is called to do the dirty work.
59 ///
Chris Lattner113f4f42002-06-25 16:13:24 +000060 void hoist(Instruction &I);
Chris Lattner6ec05f52002-05-10 22:44:58 +000061
Chris Lattnerf64f2d32002-09-26 16:52:07 +000062 /// pointerInvalidatedByLoop - Return true if the body of this loop may
63 /// store into the memory location pointed to by V.
64 ///
Chris Lattnera51fa882002-08-22 21:39:55 +000065 bool pointerInvalidatedByLoop(Value *V);
66
Chris Lattnerf64f2d32002-09-26 16:52:07 +000067 /// isLoopInvariant - Return true if the specified value is loop invariant
68 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000069 inline bool isLoopInvariant(Value *V) {
70 if (Instruction *I = dyn_cast<Instruction>(V))
71 return !CurLoop->contains(I->getParent());
72 return true; // All non-instructions are loop invariant
73 }
74
Chris Lattnerf64f2d32002-09-26 16:52:07 +000075 /// Instruction visitation handlers... these basically control whether or
76 /// not the specified instruction types are hoisted.
77 ///
Chris Lattner6ec05f52002-05-10 22:44:58 +000078 friend class InstVisitor<LICM>;
Chris Lattner113f4f42002-06-25 16:13:24 +000079 void visitBinaryOperator(Instruction &I) {
80 if (isLoopInvariant(I.getOperand(0)) && isLoopInvariant(I.getOperand(1)))
Chris Lattner6ec05f52002-05-10 22:44:58 +000081 hoist(I);
82 }
Chris Lattnerb80b69c2002-08-14 18:22:19 +000083 void visitCastInst(CastInst &CI) {
84 Instruction &I = (Instruction&)CI;
85 if (isLoopInvariant(I.getOperand(0))) hoist(I);
Chris Lattnerb193ff82002-08-14 18:18:02 +000086 }
Chris Lattner113f4f42002-06-25 16:13:24 +000087 void visitShiftInst(ShiftInst &I) { visitBinaryOperator((Instruction&)I); }
Chris Lattner6ec05f52002-05-10 22:44:58 +000088
Chris Lattnerd771fdf2002-09-26 16:19:31 +000089 void visitLoadInst(LoadInst &LI);
Chris Lattnera51fa882002-08-22 21:39:55 +000090
Chris Lattner113f4f42002-06-25 16:13:24 +000091 void visitGetElementPtrInst(GetElementPtrInst &GEPI) {
92 Instruction &I = (Instruction&)GEPI;
93 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
94 if (!isLoopInvariant(I.getOperand(i))) return;
Chris Lattner6ec05f52002-05-10 22:44:58 +000095 hoist(I);
96 }
97 };
Chris Lattnerb28b6802002-07-23 18:06:35 +000098
Chris Lattnerc8b70922002-07-26 21:12:46 +000099 RegisterOpt<LICM> X("licm", "Loop Invariant Code Motion");
Chris Lattner6ec05f52002-05-10 22:44:58 +0000100}
101
102Pass *createLICMPass() { return new LICM(); }
103
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000104/// runOnFunction - For LICM, this simply traverses the loop structure of the
105/// function, hoisting expressions out of loops if possible.
106///
Chris Lattner113f4f42002-06-25 16:13:24 +0000107bool LICM::runOnFunction(Function &) {
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000108 // Get information about the top level loops in the function...
Chris Lattner6ec05f52002-05-10 22:44:58 +0000109 const std::vector<Loop*> &TopLevelLoops =
110 getAnalysis<LoopInfo>().getTopLevelLoops();
111
Chris Lattnera51fa882002-08-22 21:39:55 +0000112 // Get our alias analysis information...
113 AA = &getAnalysis<AliasAnalysis>();
114
Chris Lattner6ec05f52002-05-10 22:44:58 +0000115 // 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
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000124
125/// visitLoop - Hoist expressions out of the specified loop...
126///
Chris Lattner6ec05f52002-05-10 22:44:58 +0000127void LICM::visitLoop(Loop *L) {
128 // Recurse through all subloops before we process this loop...
129 std::for_each(L->getSubLoops().begin(), L->getSubLoops().end(),
130 bind_obj(this, &LICM::visitLoop));
131 CurLoop = L;
132
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000133 // Get the preheader block to move instructions into...
134 Preheader = L->getLoopPreheader();
135 assert(Preheader&&"Preheader insertion pass guarantees we have a preheader!");
136
Chris Lattner6ec05f52002-05-10 22:44:58 +0000137 // We want to visit all of the instructions in this loop... that are not parts
138 // of our subloops (they have already had their invariants hoisted out of
139 // their loop, into this loop, so there is no need to process the BODIES of
140 // the subloops).
141 //
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000142 for (std::vector<BasicBlock*>::const_iterator
143 I = L->getBlocks().begin(), E = L->getBlocks().end(); I != E; ++I)
144 if (inCurrentLoop(*I))
145 visit(**I);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000146
147 // Clear out loops state information for the next iteration
148 CurLoop = 0;
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000149 Preheader = 0;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000150}
151
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000152/// hoist - When an instruction is found to only use loop invariant operands
153/// that is safe to hoist, this instruction is called to do the dirty work.
154///
Chris Lattner113f4f42002-06-25 16:13:24 +0000155void LICM::hoist(Instruction &Inst) {
156 if (Inst.use_empty()) return; // Don't (re) hoist dead instructions!
Chris Lattner6ec05f52002-05-10 22:44:58 +0000157 //cerr << "Hoisting " << Inst;
158
159 BasicBlock *Header = CurLoop->getHeader();
160
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000161 // Remove the instruction from its current basic block... but don't delete the
162 // instruction.
163 Inst.getParent()->getInstList().remove(&Inst);
Chris Lattner6ec05f52002-05-10 22:44:58 +0000164
Chris Lattner718b2212002-09-26 16:38:03 +0000165 // Insert the new node in Preheader, before the terminator.
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000166 Preheader->getInstList().insert(Preheader->getTerminator(), &Inst);
Chris Lattner718b2212002-09-26 16:38:03 +0000167
Chris Lattner718b2212002-09-26 16:38:03 +0000168 ++NumHoisted;
Chris Lattner6ec05f52002-05-10 22:44:58 +0000169 Changed = true;
170}
171
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000172
173void LICM::visitLoadInst(LoadInst &LI) {
174 if (isLoopInvariant(LI.getOperand(0)) &&
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000175 !pointerInvalidatedByLoop(LI.getOperand(0))) {
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000176 hoist(LI);
Chris Lattnerd57f3f52002-09-26 19:40:25 +0000177 ++NumHoistedLoads;
178 }
Chris Lattnerd771fdf2002-09-26 16:19:31 +0000179}
180
Chris Lattnerf64f2d32002-09-26 16:52:07 +0000181/// pointerInvalidatedByLoop - Return true if the body of this loop may store
182/// into the memory location pointed to by V.
183///
Chris Lattnera51fa882002-08-22 21:39:55 +0000184bool LICM::pointerInvalidatedByLoop(Value *V) {
185 // Check to see if any of the basic blocks in CurLoop invalidate V.
186 for (unsigned i = 0, e = CurLoop->getBlocks().size(); i != e; ++i)
187 if (AA->canBasicBlockModify(*CurLoop->getBlocks()[i], V))
188 return true;
189 return false;
190}