blob: 52b6b9887a79f651a3ce2a01ff201f9ff222abfc [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
38 class LoopStrengthReduce : public FunctionPass {
39 LoopInfo *LI;
40 DominatorSet *DS;
41 bool Changed;
Jeff Cohena2c59b72005-03-04 04:04:26 +000042 unsigned MaxTargetAMSize;
Nate Begemanb18121e2004-10-18 21:08:22 +000043 public:
Jeff Cohena2c59b72005-03-04 04:04:26 +000044 LoopStrengthReduce(unsigned MTAMS = 1)
45 : MaxTargetAMSize(MTAMS) {
46 }
47
Nate Begemanb18121e2004-10-18 21:08:22 +000048 virtual bool runOnFunction(Function &) {
49 LI = &getAnalysis<LoopInfo>();
50 DS = &getAnalysis<DominatorSet>();
51 Changed = false;
52
53 for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
54 runOnLoop(*I);
55 return Changed;
56 }
57
58 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
59 AU.setPreservesCFG();
Jeff Cohen39751c32005-02-27 19:37:07 +000060 AU.addRequiredID(LoopSimplifyID);
Nate Begemanb18121e2004-10-18 21:08:22 +000061 AU.addRequired<LoopInfo>();
62 AU.addRequired<DominatorSet>();
Jeff Cohena2c59b72005-03-04 04:04:26 +000063 AU.addRequired<TargetData>();
Nate Begemanb18121e2004-10-18 21:08:22 +000064 }
65 private:
66 void runOnLoop(Loop *L);
67 void strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L,
68 Instruction *InsertBefore,
69 std::set<Instruction*> &DeadInsts);
70 void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
71 };
72 RegisterOpt<LoopStrengthReduce> X("loop-reduce",
73 "Strength Reduce GEP Uses of Ind. Vars");
74}
75
Jeff Cohena2c59b72005-03-04 04:04:26 +000076FunctionPass *llvm::createLoopStrengthReducePass(unsigned MaxTargetAMSize) {
77 return new LoopStrengthReduce(MaxTargetAMSize);
Nate Begemanb18121e2004-10-18 21:08:22 +000078}
79
80/// DeleteTriviallyDeadInstructions - If any of the instructions is the
81/// specified set are trivially dead, delete them and see if this makes any of
82/// their operands subsequently dead.
83void LoopStrengthReduce::
84DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
85 while (!Insts.empty()) {
86 Instruction *I = *Insts.begin();
87 Insts.erase(Insts.begin());
88 if (isInstructionTriviallyDead(I)) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +000089 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
90 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
91 Insts.insert(U);
Nate Begemanb18121e2004-10-18 21:08:22 +000092 I->getParent()->getInstList().erase(I);
93 Changed = true;
94 }
95 }
96}
97
98void LoopStrengthReduce::strengthReduceGEP(GetElementPtrInst *GEPI, Loop *L,
99 Instruction *InsertBefore,
100 std::set<Instruction*> &DeadInsts) {
101 // We will strength reduce the GEP by splitting it into two parts. The first
102 // is a GEP to hold the initial value of the non-strength-reduced GEP upon
103 // entering the loop, which we will insert at the end of the loop preheader.
104 // The second is a GEP to hold the incremented value of the initial GEP.
105 // The LoopIndVarSimplify pass guarantees that loop counts start at zero, so
106 // we will replace the indvar with a constant zero value to create the first
107 // GEP.
108 //
109 // We currently only handle GEP instructions that consist of zero or more
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000110 // constants or loop invariable expressions prior to an instance of the
111 // canonical induction variable.
Jeff Cohen39751c32005-02-27 19:37:07 +0000112 unsigned indvar = 0;
Nate Begemanb18121e2004-10-18 21:08:22 +0000113 std::vector<Value *> pre_op_vector;
114 std::vector<Value *> inc_op_vector;
Jeff Cohena2c59b72005-03-04 04:04:26 +0000115 const Type *ty = GEPI->getOperand(0)->getType();
Nate Begemanb18121e2004-10-18 21:08:22 +0000116 Value *CanonicalIndVar = L->getCanonicalInductionVariable();
Jeff Cohen39751c32005-02-27 19:37:07 +0000117 BasicBlock *Header = L->getHeader();
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000118 BasicBlock *Preheader = L->getLoopPreheader();
119 bool AllConstantOperands = true;
Jeff Cohen39751c32005-02-27 19:37:07 +0000120
Nate Begemanb18121e2004-10-18 21:08:22 +0000121 for (unsigned op = 1, e = GEPI->getNumOperands(); op != e; ++op) {
122 Value *operand = GEPI->getOperand(op);
Jeff Cohena2c59b72005-03-04 04:04:26 +0000123 if (ty->getTypeID() == Type::StructTyID) {
124 assert(isa<ConstantUInt>(operand));
125 ConstantUInt *c = dyn_cast<ConstantUInt>(operand);
126 ty = ty->getContainedType(unsigned(c->getValue()));
127 } else {
128 ty = ty->getContainedType(0);
129 }
130
Nate Begemanb18121e2004-10-18 21:08:22 +0000131 if (operand == CanonicalIndVar) {
Nate Begemanb18121e2004-10-18 21:08:22 +0000132 // FIXME: use getCanonicalInductionVariableIncrement to choose between
133 // one and neg one maybe? We need to support int *foo = GEP base, -1
134 const Type *Ty = CanonicalIndVar->getType();
135 pre_op_vector.push_back(Constant::getNullValue(Ty));
136 inc_op_vector.push_back(ConstantInt::get(Ty, 1));
Jeff Cohen39751c32005-02-27 19:37:07 +0000137 indvar = op;
138 break;
Nate Begemanb18121e2004-10-18 21:08:22 +0000139 } else if (isa<Constant>(operand)) {
140 pre_op_vector.push_back(operand);
Jeff Cohen39751c32005-02-27 19:37:07 +0000141 } else if (Instruction *inst = dyn_cast<Instruction>(operand)) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000142 if (!DS->dominates(inst, Preheader->getTerminator()))
Jeff Cohen39751c32005-02-27 19:37:07 +0000143 return;
144 pre_op_vector.push_back(operand);
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000145 AllConstantOperands = false;
Nate Begemanb18121e2004-10-18 21:08:22 +0000146 } else
147 return;
148 }
Jeff Cohen39751c32005-02-27 19:37:07 +0000149 assert(indvar > 0 && "Indvar used by GEP not found in operand list");
Nate Begemanb18121e2004-10-18 21:08:22 +0000150
Jeff Cohen39751c32005-02-27 19:37:07 +0000151 // Ensure the pointer base is loop invariant. While strength reduction
152 // makes sense even if the pointer changed on every iteration, there is no
153 // realistic way of handling it unless GEPs were completely decomposed into
154 // their constituent operations so we have explicit multiplications to work
155 // with.
Nate Begemanb18121e2004-10-18 21:08:22 +0000156 if (Instruction *GepPtrOp = dyn_cast<Instruction>(GEPI->getOperand(0)))
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000157 if (!DS->dominates(GepPtrOp, Preheader->getTerminator()))
Nate Begemanb18121e2004-10-18 21:08:22 +0000158 return;
Jeff Cohena2c59b72005-03-04 04:04:26 +0000159
160 // Don't reduced multiplies that the target can handle via addressing modes.
161 uint64_t sz = getAnalysis<TargetData>().getTypeSize(ty);
162 for (unsigned i = 1; i <= MaxTargetAMSize; i *= 2)
163 if (i == sz)
164 return;
Nate Begemanb18121e2004-10-18 21:08:22 +0000165
166 // If all operands of the GEP we are going to insert into the preheader
167 // are constants, generate a GEP ConstantExpr instead.
168 //
169 // If there is only one operand after the initial non-constant one, we know
170 // that it was the induction variable, and has been replaced by a constant
171 // null value. In this case, replace the GEP with a use of pointer directly.
Nate Begemanb18121e2004-10-18 21:08:22 +0000172 Value *PreGEP;
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000173 if (AllConstantOperands && isa<Constant>(GEPI->getOperand(0))) {
Nate Begemanb18121e2004-10-18 21:08:22 +0000174 Constant *C = dyn_cast<Constant>(GEPI->getOperand(0));
175 PreGEP = ConstantExpr::getGetElementPtr(C, pre_op_vector);
176 } else if (pre_op_vector.size() == 1) {
177 PreGEP = GEPI->getOperand(0);
178 } else {
179 PreGEP = new GetElementPtrInst(GEPI->getOperand(0),
Jeff Cohen39751c32005-02-27 19:37:07 +0000180 pre_op_vector, GEPI->getName()+".pre",
Nate Begemanb18121e2004-10-18 21:08:22 +0000181 Preheader->getTerminator());
182 }
183
184 // The next step of the strength reduction is to create a PHI that will choose
185 // between the initial GEP we created and inserted into the preheader, and
186 // the incremented GEP that we will create below and insert into the loop body
187 PHINode *NewPHI = new PHINode(PreGEP->getType(),
188 GEPI->getName()+".str", InsertBefore);
189 NewPHI->addIncoming(PreGEP, Preheader);
190
Jeff Cohen39751c32005-02-27 19:37:07 +0000191 // Now, create the GEP instruction to increment by one the value selected by
192 // the PHI instruction we just created above, and add it as the second
193 // incoming Value/BasicBlock pair to the PHINode. It is inserted before the
194 // increment of the canonical induction variable.
Nate Begemanb18121e2004-10-18 21:08:22 +0000195 Instruction *IncrInst =
196 const_cast<Instruction*>(L->getCanonicalInductionVariableIncrement());
197 GetElementPtrInst *StrGEP = new GetElementPtrInst(NewPHI, inc_op_vector,
198 GEPI->getName()+".inc",
199 IncrInst);
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000200 pred_iterator PI = pred_begin(Header);
201 if (*PI == Preheader)
202 ++PI;
203 NewPHI->addIncoming(StrGEP, *PI);
Nate Begemanb18121e2004-10-18 21:08:22 +0000204
Jeff Cohen39751c32005-02-27 19:37:07 +0000205 if (GEPI->getNumOperands() - 1 == indvar) {
206 // If there were no operands following the induction variable, replace all
207 // uses of the old GEP instruction with the new PHI.
208 GEPI->replaceAllUsesWith(NewPHI);
209 } else {
210 // Create a new GEP instruction using the new PHI as the base. The
211 // operands of the original GEP past the induction variable become
212 // operands of this new GEP.
213 std::vector<Value *> op_vector;
214 const Type *Ty = CanonicalIndVar->getType();
215 op_vector.push_back(Constant::getNullValue(Ty));
216 for (unsigned op = indvar + 1; op < GEPI->getNumOperands(); op++)
217 op_vector.push_back(GEPI->getOperand(op));
218 GetElementPtrInst *newGEP = new GetElementPtrInst(NewPHI, op_vector,
219 GEPI->getName() + ".lsr",
220 GEPI);
221 GEPI->replaceAllUsesWith(newGEP);
222}
Nate Begemanb18121e2004-10-18 21:08:22 +0000223
224 // The old GEP is now dead.
225 DeadInsts.insert(GEPI);
226 ++NumReduced;
227}
228
229void LoopStrengthReduce::runOnLoop(Loop *L) {
230 // First step, transform all loops nesting inside of this loop.
231 for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I)
232 runOnLoop(*I);
233
234 // Next, get the first PHINode since it is guaranteed to be the canonical
235 // induction variable for the loop by the preceding IndVarSimplify pass.
236 PHINode *PN = L->getCanonicalInductionVariable();
237 if (0 == PN)
238 return;
239
Nate Begemanb18121e2004-10-18 21:08:22 +0000240 // FIXME: Need to use SCEV to detect GEP uses of the indvar, since indvars
241 // pass creates code like this, which we can't currently detect:
242 // %tmp.1 = sub uint 2000, %indvar
243 // %tmp.8 = getelementptr int* %y, uint %tmp.1
244
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000245 // Strength reduce all GEPs in the Loop. Insert secondary PHI nodes for the
246 // strength reduced pointers we'll be creating after the canonical induction
247 // variable's PHI.
Nate Begemanb18121e2004-10-18 21:08:22 +0000248 std::set<Instruction*> DeadInsts;
249 for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end();
250 UI != UE; ++UI)
251 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(*UI))
Jeff Cohenfd63d3a2005-02-27 21:08:04 +0000252 strengthReduceGEP(GEPI, L, PN->getNext(), DeadInsts);
Nate Begemanb18121e2004-10-18 21:08:22 +0000253
254 // Clean up after ourselves
255 if (!DeadInsts.empty()) {
256 DeleteTriviallyDeadInstructions(DeadInsts);
257
258 // At this point, we know that we have killed one or more GEP instructions.
259 // It is worth checking to see if the cann indvar is also dead, so that we
260 // can remove it as well. The requirements for the cann indvar to be
261 // considered dead are:
262 // 1. the cann indvar has one use
263 // 2. the use is an add instruction
264 // 3. the add has one use
265 // 4. the add is used by the cann indvar
266 // If all four cases above are true, then we can remove both the add and
267 // the cann indvar.
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000268 // FIXME: this needs to eliminate an induction variable even if it's being
269 // compared against some value to decide loop termination.
Nate Begemanb18121e2004-10-18 21:08:22 +0000270 if (PN->hasOneUse()) {
271 BinaryOperator *BO = dyn_cast<BinaryOperator>(*(PN->use_begin()));
272 if (BO && BO->getOpcode() == Instruction::Add)
273 if (BO->hasOneUse()) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000274 if (PN == dyn_cast<PHINode>(*(BO->use_begin()))) {
Nate Begemanb18121e2004-10-18 21:08:22 +0000275 DeadInsts.insert(BO);
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000276 // Break the cycle, then delete the PHI.
277 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
278 PN->eraseFromParent();
Nate Begemanb18121e2004-10-18 21:08:22 +0000279 DeleteTriviallyDeadInstructions(DeadInsts);
280 }
281 }
282 }
Nate Begemanb18121e2004-10-18 21:08:22 +0000283 }
284}