blob: b2b0064e112112ad7cbe732b8ffe067a548f3f5d [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"
Jeff Cohena2c59b72005-03-04 04:04:26 +000025#include "llvm/DerivedTypes.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000026#include "llvm/Analysis/Dominators.h"
27#include "llvm/Analysis/LoopInfo.h"
28#include "llvm/Support/CFG.h"
29#include "llvm/Transforms/Utils/Local.h"
Jeff Cohena2c59b72005-03-04 04:04:26 +000030#include "llvm/Target/TargetData.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000031#include "llvm/ADT/Statistic.h"
32#include <set>
33using namespace llvm;
34
35namespace {
36 Statistic<> NumReduced ("loop-reduce", "Number of GEPs strength reduced");
37
Jeff Cohenbe37fa02005-03-05 22:40:34 +000038 class GEPCache
39 {
40 public:
41 GEPCache() : CachedPHINode(0), Map() {}
42
43 GEPCache* operator[](Value *v) {
44 std::map<Value *, GEPCache>::iterator I = Map.find(v);
45 if (I == Map.end())
46 I = Map.insert(std::pair<Value *, GEPCache>(v, GEPCache())).first;
47 return &(I->second);
48 }
49
50 PHINode *CachedPHINode;
51 std::map<Value *, GEPCache> Map;
52 };
53
Nate Begemanb18121e2004-10-18 21:08:22 +000054 class LoopStrengthReduce : public FunctionPass {
55 LoopInfo *LI;
56 DominatorSet *DS;
57 bool Changed;
Jeff Cohena2c59b72005-03-04 04:04:26 +000058 unsigned MaxTargetAMSize;
Nate Begemanb18121e2004-10-18 21:08:22 +000059 public:
Jeff Cohena2c59b72005-03-04 04:04:26 +000060 LoopStrengthReduce(unsigned MTAMS = 1)
61 : MaxTargetAMSize(MTAMS) {
62 }
63
Nate Begemanb18121e2004-10-18 21:08:22 +000064 virtual bool runOnFunction(Function &) {
65 LI = &getAnalysis<LoopInfo>();
66 DS = &getAnalysis<DominatorSet>();
67 Changed = false;
68
69 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
70 runOnLoop(*I);
71 return Changed;
72 }
73
74 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75 AU.setPreservesCFG();
Jeff Cohen39751c32005-02-27 19:37:07 +000076 AU.addRequiredID(LoopSimplifyID);
Nate Begemanb18121e2004-10-18 21:08:22 +000077 AU.addRequired<LoopInfo>();
78 AU.addRequired<DominatorSet>();
Jeff Cohena2c59b72005-03-04 04:04:26 +000079 AU.addRequired<TargetData>();
Nate Begemanb18121e2004-10-18 21:08:22 +000080 }
81 private:
82 void runOnLoop(Loop *L);
83 void strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L,
Jeff Cohenbe37fa02005-03-05 22:40:34 +000084 GEPCache* GEPCache,
Nate Begemanb18121e2004-10-18 21:08:22 +000085 Instruction *InsertBefore,
86 std::set<Instruction*> &DeadInsts);
87 void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
88 };
89 RegisterOpt<LoopStrengthReduce> X("loop-reduce",
90 "Strength Reduce GEP Uses of Ind. Vars");
91}
92
Jeff Cohena2c59b72005-03-04 04:04:26 +000093FunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) {
94 return new LoopStrengthReduce(MaxTargetAMSize);
Nate Begemanb18121e2004-10-18 21:08:22 +000095}
96
97/// DeleteTriviallyDeadInstructions - If any of the instructions is the
98/// specified set are trivially dead, delete them and see if this makes any of
99/// their operands subsequently dead.
100void LoopStrengthReduce::
101DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
102 while (!Insts.empty()) {
103 Instruction *I = *Insts.begin();
104 Insts.erase(Insts.begin());
105 if (isInstructionTriviallyDead(I)) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000106 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
107 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
108 Insts.insert(U);
Nate Begemanb18121e2004-10-18 21:08:22 +0000109 I->getParent()->getInstList().erase(I);
110 Changed = true;
111 }
112 }
113}
114
115void LoopStrengthReduce::strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L,
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000116 GEPCache *Cache,
Nate Begemanb18121e2004-10-18 21:08:22 +0000117 Instruction *InsertBefore,
118 std::set<Instruction*> &DeadInsts) {
119 // We will strength reduce the GEP by splitting it into two parts. The first
120 // is a GEP to hold the initial value of the non-strength-reduced GEP upon
121 // entering the loop, which we will insert at the end of the loop preheader.
122 // The second is a GEP to hold the incremented value of the initial GEP.
123 // The LoopIndVarSimplify pass guarantees that loop counts start at zero, so
124 // we will replace the indvar with a constant zero value to create the first
125 // GEP.
126 //
127 // We currently only handle GEP instructions that consist of zero or more
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000128 // constants or loop invariable expressions prior to an instance of the
129 // canonical induction variable.
Jeff Cohen39751c32005-02-27 19:37:07 +0000130 unsigned indvar = 0;
Nate Begemanb18121e2004-10-18 21:08:22 +0000131 std::vector<Value *> pre_op_vector;
132 std::vector<Value *> inc_op_vector;
Jeff Cohena2c59b72005-03-04 04:04:26 +0000133 const Type *ty = GEPI->getOperand(0)->getType();
Nate Begemanb18121e2004-10-18 21:08:22 +0000134 Value *CanonicalIndVar = L->getCanonicalInductionVariable();
Jeff Cohen39751c32005-02-27 19:37:07 +0000135 BasicBlock *Header = L->getHeader();
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000136 BasicBlock *Preheader = L->getLoopPreheader();
137 bool AllConstantOperands = true;
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000138 Cache = (*Cache)[GEPI->getOperand(0)];
Jeff Cohen39751c32005-02-27 19:37:07 +0000139
Nate Begemanb18121e2004-10-18 21:08:22 +0000140 for (unsigned op = 1, e = GEPI->getNumOperands(); op != e; ++op) {
141 Value *operand = GEPI->getOperand(op);
Jeff Cohena2c59b72005-03-04 04:04:26 +0000142 if (ty->getTypeID() == Type::StructTyID) {
143 assert(isa<ConstantUInt>(operand));
144 ConstantUInt *c = dyn_cast<ConstantUInt>(operand);
145 ty = ty->getContainedType(unsigned(c->getValue()));
146 } else {
147 ty = ty->getContainedType(0);
148 }
149
Nate Begemanb18121e2004-10-18 21:08:22 +0000150 if (operand == CanonicalIndVar) {
Nate Begemanb18121e2004-10-18 21:08:22 +0000151 // FIXME: use getCanonicalInductionVariableIncrement to choose between
152 // one and neg one maybe? We need to support int *foo = GEP base, -1
153 const Type *Ty = CanonicalIndVar->getType();
154 pre_op_vector.push_back(Constant::getNullValue(Ty));
155 inc_op_vector.push_back(ConstantInt::get(Ty, 1));
Jeff Cohen39751c32005-02-27 19:37:07 +0000156 indvar = op;
157 break;
Nate Begemanb18121e2004-10-18 21:08:22 +0000158 } else if (isa<Constant>(operand)) {
159 pre_op_vector.push_back(operand);
Jeff Cohen39751c32005-02-27 19:37:07 +0000160 } else if (Instruction *inst = dyn_cast<Instruction>(operand)) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000161 if (!DS->dominates(inst, Preheader->getTerminator()))
Jeff Cohen39751c32005-02-27 19:37:07 +0000162 return;
163 pre_op_vector.push_back(operand);
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000164 AllConstantOperands = false;
Nate Begemanb18121e2004-10-18 21:08:22 +0000165 } else
166 return;
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000167 Cache = (*Cache)[operand];
Nate Begemanb18121e2004-10-18 21:08:22 +0000168 }
Jeff Cohen39751c32005-02-27 19:37:07 +0000169 assert(indvar > 0 && "Indvar used by GEP not found in operand list");
Nate Begemanb18121e2004-10-18 21:08:22 +0000170
Jeff Cohen39751c32005-02-27 19:37:07 +0000171 // Ensure the pointer base is loop invariant. While strength reduction
172 // makes sense even if the pointer changed on every iteration, there is no
173 // realistic way of handling it unless GEPs were completely decomposed into
174 // their constituent operations so we have explicit multiplications to work
175 // with.
Nate Begemanb18121e2004-10-18 21:08:22 +0000176 if (Instruction *GepPtrOp = dyn_cast<Instruction>(GEPI->getOperand(0)))
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000177 if (!DS->dominates(GepPtrOp, Preheader->getTerminator()))
Nate Begemanb18121e2004-10-18 21:08:22 +0000178 return;
Jeff Cohena2c59b72005-03-04 04:04:26 +0000179
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000180 // Don't reduce multiplies that the target can handle via addressing modes.
Jeff Cohena2c59b72005-03-04 04:04:26 +0000181 uint64_t sz = getAnalysis<TargetData>().getTypeSize(ty);
182 for (unsigned i = 1; i <= MaxTargetAMSize; i *= 2)
183 if (i == sz)
184 return;
Nate Begemanb18121e2004-10-18 21:08:22 +0000185
186 // If all operands of the GEP we are going to insert into the preheader
187 // are constants, generate a GEP ConstantExpr instead.
188 //
189 // If there is only one operand after the initial non-constant one, we know
190 // that it was the induction variable, and has been replaced by a constant
191 // null value. In this case, replace the GEP with a use of pointer directly.
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000192 PHINode *NewPHI;
193 if (1) {
194 Value *PreGEP;
195 if (AllConstantOperands && isa<Constant>(GEPI->getOperand(0))) {
196 Constant *C = dyn_cast<Constant>(GEPI->getOperand(0));
197 PreGEP = ConstantExpr::getGetElementPtr(C, pre_op_vector);
198 } else if (pre_op_vector.size() == 1) {
199 PreGEP = GEPI->getOperand(0);
200 } else {
201 PreGEP = new GetElementPtrInst(GEPI->getOperand(0),
202 pre_op_vector, GEPI->getName()+".pre",
203 Preheader->getTerminator());
204 }
Nate Begemanb18121e2004-10-18 21:08:22 +0000205
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000206 // The next step of the strength reduction is to create a PHI that will choose
207 // between the initial GEP we created and inserted into the preheader, and
208 // the incremented GEP that we will create below and insert into the loop body
209 NewPHI = new PHINode(PreGEP->getType(),
210 GEPI->getName()+".str", InsertBefore);
211 NewPHI->addIncoming(PreGEP, Preheader);
212
213 // Now, create the GEP instruction to increment by one the value selected by
214 // the PHI instruction we just created above, and add it as the second
215 // incoming Value/BasicBlock pair to the PHINode. It is inserted before the
216 // increment of the canonical induction variable.
217 Instruction *IncrInst =
218 const_cast<Instruction*>(L->getCanonicalInductionVariableIncrement());
219 GetElementPtrInst *StrGEP = new GetElementPtrInst(NewPHI, inc_op_vector,
220 GEPI->getName()+".inc",
221 IncrInst);
222 pred_iterator PI = pred_begin(Header);
223 if (*PI == Preheader)
224 ++PI;
225 NewPHI->addIncoming(StrGEP, *PI);
226 Cache->CachedPHINode = NewPHI;
227 } else {
228 // Reuse previously created pointer, as it is identical to the one we were
229 // about to create.
230 NewPHI = Cache->CachedPHINode;
231 }
Nate Begemanb18121e2004-10-18 21:08:22 +0000232
Jeff Cohen39751c32005-02-27 19:37:07 +0000233 if (GEPI->getNumOperands() - 1 == indvar) {
234 // If there were no operands following the induction variable, replace all
235 // uses of the old GEP instruction with the new PHI.
236 GEPI->replaceAllUsesWith(NewPHI);
237 } else {
238 // Create a new GEP instruction using the new PHI as the base. The
239 // operands of the original GEP past the induction variable become
240 // operands of this new GEP.
241 std::vector<Value *> op_vector;
242 const Type *Ty = CanonicalIndVar->getType();
243 op_vector.push_back(Constant::getNullValue(Ty));
244 for (unsigned op = indvar + 1; op < GEPI->getNumOperands(); op++)
245 op_vector.push_back(GEPI->getOperand(op));
246 GetElementPtrInst *newGEP = new GetElementPtrInst(NewPHI, op_vector,
247 GEPI->getName() + ".lsr",
248 GEPI);
249 GEPI->replaceAllUsesWith(newGEP);
250}
Nate Begemanb18121e2004-10-18 21:08:22 +0000251
252 // The old GEP is now dead.
253 DeadInsts.insert(GEPI);
254 ++NumReduced;
255}
256
257void LoopStrengthReduce::runOnLoop(Loop *L) {
258 // First step, transform all loops nesting inside of this loop.
259 for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
260 runOnLoop(*I);
261
262 // Next, get the first PHINode since it is guaranteed to be the canonical
263 // induction variable for the loop by the preceding IndVarSimplify pass.
264 PHINode *PN = L->getCanonicalInductionVariable();
265 if (0 == PN)
266 return;
267
Nate Begemanb18121e2004-10-18 21:08:22 +0000268 // FIXME: Need to use SCEV to detect GEP uses of the indvar, since indvars
269 // pass creates code like this, which we can't currently detect:
270 // %tmp.1 = sub uint 2000, %indvar
271 // %tmp.8 = getelementptr int* %y, uint %tmp.1
272
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000273 // Strength reduce all GEPs in the Loop. Insert secondary PHI nodes for the
274 // strength reduced pointers we'll be creating after the canonical induction
275 // variable's PHI.
Nate Begemanb18121e2004-10-18 21:08:22 +0000276 std::set<Instruction*> DeadInsts;
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000277 GEPCache Cache;
Nate Begemanb18121e2004-10-18 21:08:22 +0000278 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
279 UI != UE; ++UI)
280 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
Jeff Cohenbe37fa02005-03-05 22:40:34 +0000281 strengthReduceGEP(GEPI, L, &Cache, PN->getNext(), DeadInsts);
Nate Begemanb18121e2004-10-18 21:08:22 +0000282
283 // Clean up after ourselves
284 if (!DeadInsts.empty()) {
285 DeleteTriviallyDeadInstructions(DeadInsts);
286
287 // At this point, we know that we have killed one or more GEP instructions.
288 // It is worth checking to see if the cann indvar is also dead, so that we
289 // can remove it as well. The requirements for the cann indvar to be
290 // considered dead are:
291 // 1. the cann indvar has one use
292 // 2. the use is an add instruction
293 // 3. the add has one use
294 // 4. the add is used by the cann indvar
295 // If all four cases above are true, then we can remove both the add and
296 // the cann indvar.
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000297 // FIXME: this needs to eliminate an induction variable even if it's being
298 // compared against some value to decide loop termination.
Nate Begemanb18121e2004-10-18 21:08:22 +0000299 if (PN->hasOneUse()) {
300 BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
301 if (BO && BO->getOpcode() == Instruction::Add)
302 if (BO->hasOneUse()) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000303 if (PN == dyn_cast<PHINode>(*(BO->use_begin()))) {
Nate Begemanb18121e2004-10-18 21:08:22 +0000304 DeadInsts.insert(BO);
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000305 // Break the cycle, then delete the PHI.
306 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
307 PN->eraseFromParent();
Nate Begemanb18121e2004-10-18 21:08:22 +0000308 DeleteTriviallyDeadInstructions(DeadInsts);
309 }
310 }
311 }
Nate Begemanb18121e2004-10-18 21:08:22 +0000312 }
313}