blob: 19381b9e8eafb7c91b6d6905427f4151a702f79a [file] [log] [blame]
Nate Begemanb18121e2004-10-18 21:08:22 +00001//===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Nate Begeman and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass performs a strength reduction on array references inside loops that
11// have as one or more of their components the loop induction variable. This is
12// accomplished by creating a new Value to hold the initial value of the array
13// access for the first iteration, and then creating a new GEP instruction in
14// the loop to increment the value by the appropriate amount.
15//
16// There are currently several deficiencies in the implementation, marked with
17// FIXME in the code.
18//
19//===----------------------------------------------------------------------===//
20
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/Constants.h"
23#include "llvm/Instructions.h"
24#include "llvm/Type.h"
25#include "llvm/Analysis/Dominators.h"
26#include "llvm/Analysis/LoopInfo.h"
27#include "llvm/Support/CFG.h"
28#include "llvm/Transforms/Utils/Local.h"
29#include "llvm/ADT/Statistic.h"
30#include <set>
31using namespace llvm;
32
33namespace {
34 Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced");
35
36 class LoopStrengthReduce : public FunctionPass {
37 LoopInfo *LI;
38 DominatorSet *DS;
39 bool Changed;
40 public:
41 virtual bool runOnFunction(Function &) {
42 LI = &getAnalysis<LoopInfo>();
43 DS = &getAnalysis<DominatorSet>();
44 Changed = false;
45
46 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
47 runOnLoop(*I);
48 return Changed;
49 }
50
51 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
52 AU.setPreservesCFG();
Jeff Cohen39751c32005-02-27 19:37:07 +000053 AU.addRequiredID(LoopSimplifyID);
Nate Begemanb18121e2004-10-18 21:08:22 +000054 AU.addRequired<LoopInfo>();
55 AU.addRequired<DominatorSet>();
56 }
57 private:
58 void runOnLoop(Loop *L);
59 void strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L,
60 Instruction *InsertBefore,
61 std::set<Instruction*> &DeadInsts);
62 void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
63 };
64 RegisterOpt<LoopStrengthReduce> X("loop-reduce",
65 "Strength Reduce GEP Uses of Ind. Vars");
66}
67
68FunctionPass *llvm::createLoopStrengthReducePass() {
69 return new LoopStrengthReduce();
70}
71
72/// DeleteTriviallyDeadInstructions - If any of the instructions is the
73/// specified set are trivially dead, delete them and see if this makes any of
74/// their operands subsequently dead.
75void LoopStrengthReduce::
76DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
77 while (!Insts.empty()) {
78 Instruction *I = *Insts.begin();
79 Insts.erase(Insts.begin());
80 if (isInstructionTriviallyDead(I)) {
81 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
82 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
83 Insts.insert(U);
84 I->getParent()->getInstList().erase(I);
85 Changed = true;
86 }
87 }
88}
89
90void LoopStrengthReduce::strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L,
91 Instruction *InsertBefore,
92 std::set<Instruction*> &DeadInsts) {
93 // We will strength reduce the GEP by splitting it into two parts. The first
94 // is a GEP to hold the initial value of the non-strength-reduced GEP upon
95 // entering the loop, which we will insert at the end of the loop preheader.
96 // The second is a GEP to hold the incremented value of the initial GEP.
97 // The LoopIndVarSimplify pass guarantees that loop counts start at zero, so
98 // we will replace the indvar with a constant zero value to create the first
99 // GEP.
100 //
101 // We currently only handle GEP instructions that consist of zero or more
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000102 // constants or loop invariable expressions prior to an instance of the
103 // canonical induction variable.
Jeff Cohen39751c32005-02-27 19:37:07 +0000104 unsigned indvar = 0;
Nate Begemanb18121e2004-10-18 21:08:22 +0000105 std::vector<Value *> pre_op_vector;
106 std::vector<Value *> inc_op_vector;
107 Value *CanonicalIndVar = L->getCanonicalInductionVariable();
Jeff Cohen39751c32005-02-27 19:37:07 +0000108 BasicBlock *Header = L->getHeader();
109
Nate Begemanb18121e2004-10-18 21:08:22 +0000110 for (unsigned op = 1, e = GEPI->getNumOperands(); op != e; ++op) {
111 Value *operand = GEPI->getOperand(op);
112 if (operand == CanonicalIndVar) {
Nate Begemanb18121e2004-10-18 21:08:22 +0000113 // FIXME: use getCanonicalInductionVariableIncrement to choose between
114 // one and neg one maybe? We need to support int *foo = GEP base, -1
115 const Type *Ty = CanonicalIndVar->getType();
116 pre_op_vector.push_back(Constant::getNullValue(Ty));
117 inc_op_vector.push_back(ConstantInt::get(Ty, 1));
Jeff Cohen39751c32005-02-27 19:37:07 +0000118 indvar = op;
119 break;
Nate Begemanb18121e2004-10-18 21:08:22 +0000120 } else if (isa<Constant>(operand)) {
121 pre_op_vector.push_back(operand);
Jeff Cohen39751c32005-02-27 19:37:07 +0000122 } else if (Instruction *inst = dyn_cast<Instruction>(operand)) {
123 if (!DS->dominates(inst, Header->begin()))
124 return;
125 pre_op_vector.push_back(operand);
Nate Begemanb18121e2004-10-18 21:08:22 +0000126 } else
127 return;
128 }
Jeff Cohen39751c32005-02-27 19:37:07 +0000129 assert(indvar > 0 && "Indvar used by GEP not found in operand list");
Nate Begemanb18121e2004-10-18 21:08:22 +0000130
Jeff Cohen39751c32005-02-27 19:37:07 +0000131 // Ensure the pointer base is loop invariant. While strength reduction
132 // makes sense even if the pointer changed on every iteration, there is no
133 // realistic way of handling it unless GEPs were completely decomposed into
134 // their constituent operations so we have explicit multiplications to work
135 // with.
Nate Begemanb18121e2004-10-18 21:08:22 +0000136 if (Instruction *GepPtrOp = dyn_cast<Instruction>(GEPI->getOperand(0)))
137 if (!DS->dominates(GepPtrOp, Header->begin()))
138 return;
139
140 // If all operands of the GEP we are going to insert into the preheader
141 // are constants, generate a GEP ConstantExpr instead.
142 //
143 // If there is only one operand after the initial non-constant one, we know
144 // that it was the induction variable, and has been replaced by a constant
145 // null value. In this case, replace the GEP with a use of pointer directly.
Nate Begemanb18121e2004-10-18 21:08:22 +0000146 BasicBlock *Preheader = L->getLoopPreheader();
147 Value *PreGEP;
148 if (isa<Constant>(GEPI->getOperand(0))) {
149 Constant *C = dyn_cast<Constant>(GEPI->getOperand(0));
150 PreGEP = ConstantExpr::getGetElementPtr(C, pre_op_vector);
151 } else if (pre_op_vector.size() == 1) {
152 PreGEP = GEPI->getOperand(0);
153 } else {
154 PreGEP = new GetElementPtrInst(GEPI->getOperand(0),
Jeff Cohen39751c32005-02-27 19:37:07 +0000155 pre_op_vector, GEPI->getName()+".pre",
Nate Begemanb18121e2004-10-18 21:08:22 +0000156 Preheader->getTerminator());
157 }
158
159 // The next step of the strength reduction is to create a PHI that will choose
160 // between the initial GEP we created and inserted into the preheader, and
161 // the incremented GEP that we will create below and insert into the loop body
162 PHINode *NewPHI = new PHINode(PreGEP->getType(),
163 GEPI->getName()+".str", InsertBefore);
164 NewPHI->addIncoming(PreGEP, Preheader);
165
Jeff Cohen39751c32005-02-27 19:37:07 +0000166 // Now, create the GEP instruction to increment by one the value selected by
167 // the PHI instruction we just created above, and add it as the second
168 // incoming Value/BasicBlock pair to the PHINode. It is inserted before the
169 // increment of the canonical induction variable.
Nate Begemanb18121e2004-10-18 21:08:22 +0000170 Instruction *IncrInst =
171 const_cast<Instruction*>(L->getCanonicalInductionVariableIncrement());
172 GetElementPtrInst *StrGEP = new GetElementPtrInst(NewPHI, inc_op_vector,
173 GEPI->getName()+".inc",
174 IncrInst);
175 NewPHI->addIncoming(StrGEP, IncrInst->getParent());
176
Jeff Cohen39751c32005-02-27 19:37:07 +0000177 if (GEPI->getNumOperands() - 1 == indvar) {
178 // If there were no operands following the induction variable, replace all
179 // uses of the old GEP instruction with the new PHI.
180 GEPI->replaceAllUsesWith(NewPHI);
181 } else {
182 // Create a new GEP instruction using the new PHI as the base. The
183 // operands of the original GEP past the induction variable become
184 // operands of this new GEP.
185 std::vector<Value *> op_vector;
186 const Type *Ty = CanonicalIndVar->getType();
187 op_vector.push_back(Constant::getNullValue(Ty));
188 for (unsigned op = indvar + 1; op < GEPI->getNumOperands(); op++)
189 op_vector.push_back(GEPI->getOperand(op));
190 GetElementPtrInst *newGEP = new GetElementPtrInst(NewPHI, op_vector,
191 GEPI->getName() + ".lsr",
192 GEPI);
193 GEPI->replaceAllUsesWith(newGEP);
194}
Nate Begemanb18121e2004-10-18 21:08:22 +0000195
196 // The old GEP is now dead.
197 DeadInsts.insert(GEPI);
198 ++NumReduced;
199}
200
201void LoopStrengthReduce::runOnLoop(Loop *L) {
202 // First step, transform all loops nesting inside of this loop.
203 for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
204 runOnLoop(*I);
205
206 // Next, get the first PHINode since it is guaranteed to be the canonical
207 // induction variable for the loop by the preceding IndVarSimplify pass.
208 PHINode *PN = L->getCanonicalInductionVariable();
209 if (0 == PN)
210 return;
211
Nate Begemanb18121e2004-10-18 21:08:22 +0000212 // FIXME: Need to use SCEV to detect GEP uses of the indvar, since indvars
213 // pass creates code like this, which we can't currently detect:
214 // %tmp.1 = sub uint 2000, %indvar
215 // %tmp.8 = getelementptr int* %y, uint %tmp.1
216
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000217 // Strength reduce all GEPs in the Loop. Insert secondary PHI nodes for the
218 // strength reduced pointers we'll be creating after the canonical induction
219 // variable's PHI.
Nate Begemanb18121e2004-10-18 21:08:22 +0000220 std::set<Instruction*> DeadInsts;
221 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
222 UI != UE; ++UI)
223 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000224 strengthReduceGEP(GEPI, L, PN->getNext(), DeadInsts);
Nate Begemanb18121e2004-10-18 21:08:22 +0000225
226 // Clean up after ourselves
227 if (!DeadInsts.empty()) {
228 DeleteTriviallyDeadInstructions(DeadInsts);
229
230 // At this point, we know that we have killed one or more GEP instructions.
231 // It is worth checking to see if the cann indvar is also dead, so that we
232 // can remove it as well. The requirements for the cann indvar to be
233 // considered dead are:
234 // 1. the cann indvar has one use
235 // 2. the use is an add instruction
236 // 3. the add has one use
237 // 4. the add is used by the cann indvar
238 // If all four cases above are true, then we can remove both the add and
239 // the cann indvar.
240 if (PN->hasOneUse()) {
241 BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
242 if (BO && BO->getOpcode() == Instruction::Add)
243 if (BO->hasOneUse()) {
244 PHINode *PotentialIndvar = dyn_cast<PHINode>(*(BO->use_begin()));
245 if (PotentialIndvar && PN == PotentialIndvar) {
246 PN->dropAllReferences();
247 DeadInsts.insert(BO);
248 DeadInsts.insert(PN);
249 DeleteTriviallyDeadInstructions(DeadInsts);
250 }
251 }
252 }
253 }
254}