blob: bd74902c53cc9633d174ff535a9ac43d55069a3c [file] [log] [blame]
Nate Begemanb18121e2004-10-18 21:08:22 +00001//===- LoopStrengthReduce.cpp - Strength Reduce GEPs in Loops -------------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Nate Begemanb18121e2004-10-18 21:08:22 +00003// 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.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Nate Begemanb18121e2004-10-18 21:08:22 +00008//===----------------------------------------------------------------------===//
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//
Nate Begemanb18121e2004-10-18 21:08:22 +000016//===----------------------------------------------------------------------===//
17
Chris Lattnerbb78c972005-08-03 23:30:08 +000018#define DEBUG_TYPE "loop-reduce"
Nate Begemanb18121e2004-10-18 21:08:22 +000019#include "llvm/Transforms/Scalar.h"
20#include "llvm/Constants.h"
21#include "llvm/Instructions.h"
22#include "llvm/Type.h"
Jeff Cohena2c59b72005-03-04 04:04:26 +000023#include "llvm/DerivedTypes.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000024#include "llvm/Analysis/Dominators.h"
25#include "llvm/Analysis/LoopInfo.h"
Devang Patelb0743b52007-03-06 21:14:09 +000026#include "llvm/Analysis/LoopPass.h"
Nate Begemane68bcd12005-07-30 00:15:07 +000027#include "llvm/Analysis/ScalarEvolutionExpander.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000028#include "llvm/Support/CFG.h"
Nate Begemane68bcd12005-07-30 00:15:07 +000029#include "llvm/Support/GetElementPtrTypeIterator.h"
Chris Lattner4fec86d2005-08-12 22:06:11 +000030#include "llvm/Transforms/Utils/BasicBlockUtils.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000031#include "llvm/Transforms/Utils/Local.h"
Jeff Cohena2c59b72005-03-04 04:04:26 +000032#include "llvm/Target/TargetData.h"
Nate Begemanb18121e2004-10-18 21:08:22 +000033#include "llvm/ADT/Statistic.h"
Nate Begemane68bcd12005-07-30 00:15:07 +000034#include "llvm/Support/Debug.h"
Chris Lattner3d27be12006-08-27 12:54:02 +000035#include "llvm/Support/Compiler.h"
Evan Chengc567c4e2006-03-13 23:14:23 +000036#include "llvm/Target/TargetLowering.h"
Jeff Cohenc5009912005-07-30 18:22:27 +000037#include <algorithm>
Nate Begemanb18121e2004-10-18 21:08:22 +000038#include <set>
39using namespace llvm;
40
Chris Lattner79a42ac2006-12-19 21:40:18 +000041STATISTIC(NumReduced , "Number of GEPs strength reduced");
42STATISTIC(NumInserted, "Number of PHIs inserted");
43STATISTIC(NumVariable, "Number of PHIs with variable strides");
Nate Begemanb18121e2004-10-18 21:08:22 +000044
Chris Lattner79a42ac2006-12-19 21:40:18 +000045namespace {
Dale Johannesene3a02be2007-03-20 00:47:50 +000046
47 class BasedUser;
48
Chris Lattner430d0022005-08-03 22:21:05 +000049 /// IVStrideUse - Keep track of one use of a strided induction variable, where
50 /// the stride is stored externally. The Offset member keeps track of the
51 /// offset from the IV, User is the actual user of the operand, and 'Operand'
52 /// is the operand # of the User that is the use.
Reid Spencer557ab152007-02-05 23:32:05 +000053 struct VISIBILITY_HIDDEN IVStrideUse {
Chris Lattner430d0022005-08-03 22:21:05 +000054 SCEVHandle Offset;
55 Instruction *User;
56 Value *OperandValToReplace;
Chris Lattner9bfa6f82005-08-08 05:28:22 +000057
58 // isUseOfPostIncrementedValue - True if this should use the
59 // post-incremented version of this IV, not the preincremented version.
60 // This can only be set in special cases, such as the terminating setcc
Chris Lattnera6764832005-09-12 06:04:47 +000061 // instruction for a loop or uses dominated by the loop.
Chris Lattner9bfa6f82005-08-08 05:28:22 +000062 bool isUseOfPostIncrementedValue;
Chris Lattner430d0022005-08-03 22:21:05 +000063
64 IVStrideUse(const SCEVHandle &Offs, Instruction *U, Value *O)
Chris Lattner9bfa6f82005-08-08 05:28:22 +000065 : Offset(Offs), User(U), OperandValToReplace(O),
66 isUseOfPostIncrementedValue(false) {}
Chris Lattner430d0022005-08-03 22:21:05 +000067 };
68
69 /// IVUsersOfOneStride - This structure keeps track of all instructions that
70 /// have an operand that is based on the trip count multiplied by some stride.
71 /// The stride for all of these users is common and kept external to this
72 /// structure.
Reid Spencer557ab152007-02-05 23:32:05 +000073 struct VISIBILITY_HIDDEN IVUsersOfOneStride {
Nate Begemane68bcd12005-07-30 00:15:07 +000074 /// Users - Keep track of all of the users of this stride as well as the
Chris Lattner430d0022005-08-03 22:21:05 +000075 /// initial value and the operand that uses the IV.
76 std::vector<IVStrideUse> Users;
77
78 void addUser(const SCEVHandle &Offset,Instruction *User, Value *Operand) {
79 Users.push_back(IVStrideUse(Offset, User, Operand));
Nate Begemane68bcd12005-07-30 00:15:07 +000080 }
81 };
82
Evan Cheng3df447d2006-03-16 21:53:05 +000083 /// IVInfo - This structure keeps track of one IV expression inserted during
Evan Chengc28282b2006-03-18 08:03:12 +000084 /// StrengthReduceStridedIVUsers. It contains the stride, the common base, as
85 /// well as the PHI node and increment value created for rewrite.
Reid Spencer557ab152007-02-05 23:32:05 +000086 struct VISIBILITY_HIDDEN IVExpr {
Evan Chengc28282b2006-03-18 08:03:12 +000087 SCEVHandle Stride;
Evan Cheng3df447d2006-03-16 21:53:05 +000088 SCEVHandle Base;
89 PHINode *PHI;
90 Value *IncV;
91
Evan Chengc28282b2006-03-18 08:03:12 +000092 IVExpr()
Reid Spencerc635f472006-12-31 05:48:39 +000093 : Stride(SCEVUnknown::getIntegerSCEV(0, Type::Int32Ty)),
94 Base (SCEVUnknown::getIntegerSCEV(0, Type::Int32Ty)) {}
Evan Chengc28282b2006-03-18 08:03:12 +000095 IVExpr(const SCEVHandle &stride, const SCEVHandle &base, PHINode *phi,
96 Value *incv)
97 : Stride(stride), Base(base), PHI(phi), IncV(incv) {}
Evan Cheng3df447d2006-03-16 21:53:05 +000098 };
99
100 /// IVsOfOneStride - This structure keeps track of all IV expression inserted
101 /// during StrengthReduceStridedIVUsers for a particular stride of the IV.
Reid Spencer557ab152007-02-05 23:32:05 +0000102 struct VISIBILITY_HIDDEN IVsOfOneStride {
Evan Cheng3df447d2006-03-16 21:53:05 +0000103 std::vector<IVExpr> IVs;
104
Evan Chengc28282b2006-03-18 08:03:12 +0000105 void addIV(const SCEVHandle &Stride, const SCEVHandle &Base, PHINode *PHI,
106 Value *IncV) {
107 IVs.push_back(IVExpr(Stride, Base, PHI, IncV));
Evan Cheng3df447d2006-03-16 21:53:05 +0000108 }
109 };
Nate Begemane68bcd12005-07-30 00:15:07 +0000110
Devang Patelb0743b52007-03-06 21:14:09 +0000111 class VISIBILITY_HIDDEN LoopStrengthReduce : public LoopPass {
Nate Begemanb18121e2004-10-18 21:08:22 +0000112 LoopInfo *LI;
Chris Lattnercb367102006-01-11 05:10:20 +0000113 ETForest *EF;
Nate Begemane68bcd12005-07-30 00:15:07 +0000114 ScalarEvolution *SE;
115 const TargetData *TD;
116 const Type *UIntPtrTy;
Nate Begemanb18121e2004-10-18 21:08:22 +0000117 bool Changed;
Chris Lattner75a44e12005-08-02 02:52:02 +0000118
Nate Begemane68bcd12005-07-30 00:15:07 +0000119 /// IVUsesByStride - Keep track of all uses of induction variables that we
120 /// are interested in. The key of the map is the stride of the access.
Chris Lattneredff91a2005-08-10 00:45:21 +0000121 std::map<SCEVHandle, IVUsersOfOneStride> IVUsesByStride;
Nate Begemane68bcd12005-07-30 00:15:07 +0000122
Evan Cheng3df447d2006-03-16 21:53:05 +0000123 /// IVsByStride - Keep track of all IVs that have been inserted for a
124 /// particular stride.
125 std::map<SCEVHandle, IVsOfOneStride> IVsByStride;
126
Chris Lattner4ea0a3e2005-10-09 06:20:55 +0000127 /// StrideOrder - An ordering of the keys in IVUsesByStride that is stable:
128 /// We use this to iterate over the IVUsesByStride collection without being
129 /// dependent on random ordering of pointers in the process.
130 std::vector<SCEVHandle> StrideOrder;
131
Chris Lattner6f286b72005-08-04 01:19:13 +0000132 /// CastedValues - As we need to cast values to uintptr_t, this keeps track
133 /// of the casted version of each value. This is accessed by
134 /// getCastedVersionOf.
135 std::map<Value*, Value*> CastedPointers;
Nate Begemane68bcd12005-07-30 00:15:07 +0000136
137 /// DeadInsts - Keep track of instructions we may have made dead, so that
138 /// we can remove them after we are done working.
139 std::set<Instruction*> DeadInsts;
Evan Chengc567c4e2006-03-13 23:14:23 +0000140
141 /// TLI - Keep a pointer of a TargetLowering to consult for determining
142 /// transformation profitability.
143 const TargetLowering *TLI;
144
Nate Begemanb18121e2004-10-18 21:08:22 +0000145 public:
Evan Cheng3df447d2006-03-16 21:53:05 +0000146 LoopStrengthReduce(const TargetLowering *tli = NULL)
147 : TLI(tli) {
Jeff Cohena2c59b72005-03-04 04:04:26 +0000148 }
149
Devang Patelb0743b52007-03-06 21:14:09 +0000150 bool runOnLoop(Loop *L, LPPassManager &LPM);
Nate Begemanb18121e2004-10-18 21:08:22 +0000151
152 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000153 // We split critical edges, so we change the CFG. However, we do update
154 // many analyses if they are around.
155 AU.addPreservedID(LoopSimplifyID);
156 AU.addPreserved<LoopInfo>();
157 AU.addPreserved<DominatorSet>();
Chris Lattnercb367102006-01-11 05:10:20 +0000158 AU.addPreserved<ETForest>();
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000159 AU.addPreserved<ImmediateDominators>();
160 AU.addPreserved<DominanceFrontier>();
161 AU.addPreserved<DominatorTree>();
162
Jeff Cohen39751c32005-02-27 19:37:07 +0000163 AU.addRequiredID(LoopSimplifyID);
Nate Begemanb18121e2004-10-18 21:08:22 +0000164 AU.addRequired<LoopInfo>();
Chris Lattnercb367102006-01-11 05:10:20 +0000165 AU.addRequired<ETForest>();
Jeff Cohena2c59b72005-03-04 04:04:26 +0000166 AU.addRequired<TargetData>();
Nate Begemane68bcd12005-07-30 00:15:07 +0000167 AU.addRequired<ScalarEvolution>();
Nate Begemanb18121e2004-10-18 21:08:22 +0000168 }
Chris Lattner6f286b72005-08-04 01:19:13 +0000169
170 /// getCastedVersionOf - Return the specified value casted to uintptr_t.
171 ///
Reid Spencerdf1f19a2006-12-13 08:06:42 +0000172 Value *getCastedVersionOf(Instruction::CastOps opcode, Value *V);
Chris Lattner6f286b72005-08-04 01:19:13 +0000173private:
Chris Lattnereaf24722005-08-04 17:40:30 +0000174 bool AddUsersIfInteresting(Instruction *I, Loop *L,
175 std::set<Instruction*> &Processed);
176 SCEVHandle GetExpressionSCEV(Instruction *E, Loop *L);
177
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000178 void OptimizeIndvars(Loop *L);
Nate Begemane68bcd12005-07-30 00:15:07 +0000179
Dale Johannesene3a02be2007-03-20 00:47:50 +0000180 unsigned CheckForIVReuse(const SCEVHandle&, IVExpr&, const Type*,
181 const std::vector<BasedUser>& UsersToProcess);
182
183 bool ValidStride(int64_t, const std::vector<BasedUser>& UsersToProcess);
Evan Cheng45206982006-03-17 19:52:23 +0000184
Chris Lattneredff91a2005-08-10 00:45:21 +0000185 void StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
186 IVUsersOfOneStride &Uses,
Chris Lattner430d0022005-08-03 22:21:05 +0000187 Loop *L, bool isOnlyStride);
Nate Begemanb18121e2004-10-18 21:08:22 +0000188 void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts);
189 };
Chris Lattnerc2d3d312006-08-27 22:42:52 +0000190 RegisterPass<LoopStrengthReduce> X("loop-reduce", "Loop Strength Reduction");
Nate Begemanb18121e2004-10-18 21:08:22 +0000191}
192
Devang Patelb0743b52007-03-06 21:14:09 +0000193LoopPass *llvm::createLoopStrengthReducePass(const TargetLowering *TLI) {
Evan Cheng3df447d2006-03-16 21:53:05 +0000194 return new LoopStrengthReduce(TLI);
Nate Begemanb18121e2004-10-18 21:08:22 +0000195}
196
Reid Spencerb341b082006-12-12 05:05:00 +0000197/// getCastedVersionOf - Return the specified value casted to uintptr_t. This
198/// assumes that the Value* V is of integer or pointer type only.
Chris Lattner6f286b72005-08-04 01:19:13 +0000199///
Reid Spencerdf1f19a2006-12-13 08:06:42 +0000200Value *LoopStrengthReduce::getCastedVersionOf(Instruction::CastOps opcode,
201 Value *V) {
Chris Lattner6f286b72005-08-04 01:19:13 +0000202 if (V->getType() == UIntPtrTy) return V;
203 if (Constant *CB = dyn_cast<Constant>(V))
Reid Spencerdf1f19a2006-12-13 08:06:42 +0000204 return ConstantExpr::getCast(opcode, CB, UIntPtrTy);
Chris Lattner6f286b72005-08-04 01:19:13 +0000205
206 Value *&New = CastedPointers[V];
207 if (New) return New;
208
Reid Spencerdf1f19a2006-12-13 08:06:42 +0000209 New = SCEVExpander::InsertCastOfTo(opcode, V, UIntPtrTy);
Chris Lattneracc42c42005-08-04 19:08:16 +0000210 DeadInsts.insert(cast<Instruction>(New));
211 return New;
Chris Lattner6f286b72005-08-04 01:19:13 +0000212}
213
214
Nate Begemanb18121e2004-10-18 21:08:22 +0000215/// DeleteTriviallyDeadInstructions - If any of the instructions is the
216/// specified set are trivially dead, delete them and see if this makes any of
217/// their operands subsequently dead.
218void LoopStrengthReduce::
219DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) {
220 while (!Insts.empty()) {
221 Instruction *I = *Insts.begin();
222 Insts.erase(Insts.begin());
223 if (isInstructionTriviallyDead(I)) {
Jeff Cohen8ea6f9e2005-03-01 03:46:11 +0000224 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
225 if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i)))
226 Insts.insert(U);
Chris Lattner84e9baa2005-08-03 21:36:09 +0000227 SE->deleteInstructionFromRecords(I);
228 I->eraseFromParent();
Nate Begemanb18121e2004-10-18 21:08:22 +0000229 Changed = true;
230 }
231 }
232}
233
Jeff Cohen39751c32005-02-27 19:37:07 +0000234
Chris Lattnereaf24722005-08-04 17:40:30 +0000235/// GetExpressionSCEV - Compute and return the SCEV for the specified
236/// instruction.
237SCEVHandle LoopStrengthReduce::GetExpressionSCEV(Instruction *Exp, Loop *L) {
Chris Lattnerc6c4d992005-08-09 23:39:36 +0000238 // Scalar Evolutions doesn't know how to compute SCEV's for GEP instructions.
239 // If this is a GEP that SE doesn't know about, compute it now and insert it.
240 // If this is not a GEP, or if we have already done this computation, just let
241 // SE figure it out.
Chris Lattnereaf24722005-08-04 17:40:30 +0000242 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Exp);
Chris Lattnerc6c4d992005-08-09 23:39:36 +0000243 if (!GEP || SE->hasSCEV(GEP))
Chris Lattnereaf24722005-08-04 17:40:30 +0000244 return SE->getSCEV(Exp);
245
Nate Begemane68bcd12005-07-30 00:15:07 +0000246 // Analyze all of the subscripts of this getelementptr instruction, looking
247 // for uses that are determined by the trip count of L. First, skip all
248 // operands the are not dependent on the IV.
249
250 // Build up the base expression. Insert an LLVM cast of the pointer to
251 // uintptr_t first.
Reid Spencerdf1f19a2006-12-13 08:06:42 +0000252 SCEVHandle GEPVal = SCEVUnknown::get(
253 getCastedVersionOf(Instruction::PtrToInt, GEP->getOperand(0)));
Nate Begemane68bcd12005-07-30 00:15:07 +0000254
255 gep_type_iterator GTI = gep_type_begin(GEP);
Chris Lattnereaf24722005-08-04 17:40:30 +0000256
257 for (unsigned i = 1, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {
Nate Begemane68bcd12005-07-30 00:15:07 +0000258 // If this is a use of a recurrence that we can analyze, and it comes before
259 // Op does in the GEP operand list, we will handle this when we process this
260 // operand.
261 if (const StructType *STy = dyn_cast<StructType>(*GTI)) {
262 const StructLayout *SL = TD->getStructLayout(STy);
Reid Spencere0fc4df2006-10-20 07:07:24 +0000263 unsigned Idx = cast<ConstantInt>(GEP->getOperand(i))->getZExtValue();
Chris Lattnerc473d8e2007-02-10 19:55:17 +0000264 uint64_t Offset = SL->getElementOffset(Idx);
Chris Lattnereaf24722005-08-04 17:40:30 +0000265 GEPVal = SCEVAddExpr::get(GEPVal,
266 SCEVUnknown::getIntegerSCEV(Offset, UIntPtrTy));
Nate Begemane68bcd12005-07-30 00:15:07 +0000267 } else {
Reid Spencerdf1f19a2006-12-13 08:06:42 +0000268 unsigned GEPOpiBits =
269 GEP->getOperand(i)->getType()->getPrimitiveSizeInBits();
270 unsigned IntPtrBits = UIntPtrTy->getPrimitiveSizeInBits();
271 Instruction::CastOps opcode = (GEPOpiBits < IntPtrBits ?
272 Instruction::SExt : (GEPOpiBits > IntPtrBits ? Instruction::Trunc :
273 Instruction::BitCast));
274 Value *OpVal = getCastedVersionOf(opcode, GEP->getOperand(i));
Chris Lattneracc42c42005-08-04 19:08:16 +0000275 SCEVHandle Idx = SE->getSCEV(OpVal);
276
Chris Lattnereaf24722005-08-04 17:40:30 +0000277 uint64_t TypeSize = TD->getTypeSize(GTI.getIndexedType());
278 if (TypeSize != 1)
279 Idx = SCEVMulExpr::get(Idx,
Reid Spencere0fc4df2006-10-20 07:07:24 +0000280 SCEVConstant::get(ConstantInt::get(UIntPtrTy,
Chris Lattnereaf24722005-08-04 17:40:30 +0000281 TypeSize)));
282 GEPVal = SCEVAddExpr::get(GEPVal, Idx);
Nate Begemane68bcd12005-07-30 00:15:07 +0000283 }
284 }
285
Chris Lattnerc6c4d992005-08-09 23:39:36 +0000286 SE->setSCEV(GEP, GEPVal);
Chris Lattnereaf24722005-08-04 17:40:30 +0000287 return GEPVal;
Nate Begemane68bcd12005-07-30 00:15:07 +0000288}
289
Chris Lattneracc42c42005-08-04 19:08:16 +0000290/// getSCEVStartAndStride - Compute the start and stride of this expression,
291/// returning false if the expression is not a start/stride pair, or true if it
292/// is. The stride must be a loop invariant expression, but the start may be
293/// a mix of loop invariant and loop variant expressions.
294static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
Chris Lattneredff91a2005-08-10 00:45:21 +0000295 SCEVHandle &Start, SCEVHandle &Stride) {
Chris Lattneracc42c42005-08-04 19:08:16 +0000296 SCEVHandle TheAddRec = Start; // Initialize to zero.
297
298 // If the outer level is an AddExpr, the operands are all start values except
299 // for a nested AddRecExpr.
300 if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(SH)) {
301 for (unsigned i = 0, e = AE->getNumOperands(); i != e; ++i)
302 if (SCEVAddRecExpr *AddRec =
303 dyn_cast<SCEVAddRecExpr>(AE->getOperand(i))) {
304 if (AddRec->getLoop() == L)
305 TheAddRec = SCEVAddExpr::get(AddRec, TheAddRec);
306 else
307 return false; // Nested IV of some sort?
308 } else {
309 Start = SCEVAddExpr::get(Start, AE->getOperand(i));
310 }
311
Reid Spencerde46e482006-11-02 20:25:50 +0000312 } else if (isa<SCEVAddRecExpr>(SH)) {
Chris Lattneracc42c42005-08-04 19:08:16 +0000313 TheAddRec = SH;
314 } else {
315 return false; // not analyzable.
316 }
317
318 SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(TheAddRec);
319 if (!AddRec || AddRec->getLoop() != L) return false;
320
321 // FIXME: Generalize to non-affine IV's.
322 if (!AddRec->isAffine()) return false;
323
324 Start = SCEVAddExpr::get(Start, AddRec->getOperand(0));
325
Chris Lattneracc42c42005-08-04 19:08:16 +0000326 if (!isa<SCEVConstant>(AddRec->getOperand(1)))
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000327 DOUT << "[" << L->getHeader()->getName()
328 << "] Variable stride: " << *AddRec << "\n";
Chris Lattneracc42c42005-08-04 19:08:16 +0000329
Chris Lattneredff91a2005-08-10 00:45:21 +0000330 Stride = AddRec->getOperand(1);
Chris Lattneracc42c42005-08-04 19:08:16 +0000331 return true;
332}
333
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000334/// IVUseShouldUsePostIncValue - We have discovered a "User" of an IV expression
335/// and now we need to decide whether the user should use the preinc or post-inc
336/// value. If this user should use the post-inc version of the IV, return true.
337///
338/// Choosing wrong here can break dominance properties (if we choose to use the
339/// post-inc value when we cannot) or it can end up adding extra live-ranges to
340/// the loop, resulting in reg-reg copies (if we use the pre-inc value when we
341/// should use the post-inc value).
342static bool IVUseShouldUsePostIncValue(Instruction *User, Instruction *IV,
Chris Lattnercb367102006-01-11 05:10:20 +0000343 Loop *L, ETForest *EF, Pass *P) {
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000344 // If the user is in the loop, use the preinc value.
345 if (L->contains(User->getParent())) return false;
346
Chris Lattnerf07a5872005-10-03 02:50:05 +0000347 BasicBlock *LatchBlock = L->getLoopLatch();
348
349 // Ok, the user is outside of the loop. If it is dominated by the latch
350 // block, use the post-inc value.
Chris Lattnercb367102006-01-11 05:10:20 +0000351 if (EF->dominates(LatchBlock, User->getParent()))
Chris Lattnerf07a5872005-10-03 02:50:05 +0000352 return true;
353
354 // There is one case we have to be careful of: PHI nodes. These little guys
355 // can live in blocks that do not dominate the latch block, but (since their
356 // uses occur in the predecessor block, not the block the PHI lives in) should
357 // still use the post-inc value. Check for this case now.
358 PHINode *PN = dyn_cast<PHINode>(User);
359 if (!PN) return false; // not a phi, not dominated by latch block.
360
361 // Look at all of the uses of IV by the PHI node. If any use corresponds to
362 // a block that is not dominated by the latch block, give up and use the
363 // preincremented value.
364 unsigned NumUses = 0;
365 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
366 if (PN->getIncomingValue(i) == IV) {
367 ++NumUses;
Chris Lattnercb367102006-01-11 05:10:20 +0000368 if (!EF->dominates(LatchBlock, PN->getIncomingBlock(i)))
Chris Lattnerf07a5872005-10-03 02:50:05 +0000369 return false;
370 }
371
372 // Okay, all uses of IV by PN are in predecessor blocks that really are
373 // dominated by the latch block. Split the critical edges and use the
374 // post-incremented value.
375 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
376 if (PN->getIncomingValue(i) == IV) {
Chris Lattnera6eb7e02006-10-28 06:45:33 +0000377 SplitCriticalEdge(PN->getIncomingBlock(i), PN->getParent(), P,
378 true);
Chris Lattner5191c652006-10-28 00:59:20 +0000379 // Splitting the critical edge can reduce the number of entries in this
380 // PHI.
381 e = PN->getNumIncomingValues();
Chris Lattnerf07a5872005-10-03 02:50:05 +0000382 if (--NumUses == 0) break;
383 }
384
385 return true;
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000386}
387
388
389
Nate Begemane68bcd12005-07-30 00:15:07 +0000390/// AddUsersIfInteresting - Inspect the specified instruction. If it is a
391/// reducible SCEV, recursively add its users to the IVUsesByStride set and
392/// return true. Otherwise, return false.
Chris Lattnereaf24722005-08-04 17:40:30 +0000393bool LoopStrengthReduce::AddUsersIfInteresting(Instruction *I, Loop *L,
394 std::set<Instruction*> &Processed) {
Chris Lattner03c49532007-01-15 02:27:26 +0000395 if (!I->getType()->isInteger() && !isa<PointerType>(I->getType()))
Chris Lattner5df0e362005-10-21 05:45:41 +0000396 return false; // Void and FP expressions cannot be reduced.
Chris Lattnereaf24722005-08-04 17:40:30 +0000397 if (!Processed.insert(I).second)
398 return true; // Instruction already handled.
399
Chris Lattneracc42c42005-08-04 19:08:16 +0000400 // Get the symbolic expression for this instruction.
Chris Lattnereaf24722005-08-04 17:40:30 +0000401 SCEVHandle ISE = GetExpressionSCEV(I, L);
Chris Lattneracc42c42005-08-04 19:08:16 +0000402 if (isa<SCEVCouldNotCompute>(ISE)) return false;
Chris Lattnereaf24722005-08-04 17:40:30 +0000403
Chris Lattneracc42c42005-08-04 19:08:16 +0000404 // Get the start and stride for this expression.
405 SCEVHandle Start = SCEVUnknown::getIntegerSCEV(0, ISE->getType());
Chris Lattneredff91a2005-08-10 00:45:21 +0000406 SCEVHandle Stride = Start;
Chris Lattneracc42c42005-08-04 19:08:16 +0000407 if (!getSCEVStartAndStride(ISE, L, Start, Stride))
408 return false; // Non-reducible symbolic expression, bail out.
Devang Patel58818c52007-03-09 21:19:53 +0000409
410 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E;) {
Nate Begemane68bcd12005-07-30 00:15:07 +0000411 Instruction *User = cast<Instruction>(*UI);
412
Devang Patel58818c52007-03-09 21:19:53 +0000413 // Increment iterator now because IVUseShouldUsePostIncValue may remove
414 // User from the list of I users.
415 ++UI;
416
Nate Begemane68bcd12005-07-30 00:15:07 +0000417 // Do not infinitely recurse on PHI nodes.
Chris Lattnerfd018c82005-09-13 02:09:55 +0000418 if (isa<PHINode>(User) && Processed.count(User))
Nate Begemane68bcd12005-07-30 00:15:07 +0000419 continue;
420
421 // If this is an instruction defined in a nested loop, or outside this loop,
Chris Lattnera0102fb2005-08-04 00:14:11 +0000422 // don't recurse into it.
Chris Lattneracc42c42005-08-04 19:08:16 +0000423 bool AddUserToIVUsers = false;
Chris Lattnera0102fb2005-08-04 00:14:11 +0000424 if (LI->getLoopFor(User->getParent()) != L) {
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000425 DOUT << "FOUND USER in other loop: " << *User
426 << " OF SCEV: " << *ISE << "\n";
Chris Lattneracc42c42005-08-04 19:08:16 +0000427 AddUserToIVUsers = true;
Chris Lattnereaf24722005-08-04 17:40:30 +0000428 } else if (!AddUsersIfInteresting(User, L, Processed)) {
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000429 DOUT << "FOUND USER: " << *User
430 << " OF SCEV: " << *ISE << "\n";
Chris Lattneracc42c42005-08-04 19:08:16 +0000431 AddUserToIVUsers = true;
432 }
Nate Begemane68bcd12005-07-30 00:15:07 +0000433
Chris Lattneracc42c42005-08-04 19:08:16 +0000434 if (AddUserToIVUsers) {
Chris Lattner4ea0a3e2005-10-09 06:20:55 +0000435 IVUsersOfOneStride &StrideUses = IVUsesByStride[Stride];
436 if (StrideUses.Users.empty()) // First occurance of this stride?
437 StrideOrder.push_back(Stride);
438
Chris Lattner65107492005-08-04 00:40:47 +0000439 // Okay, we found a user that we cannot reduce. Analyze the instruction
Chris Lattnera6764832005-09-12 06:04:47 +0000440 // and decide what to do with it. If we are a use inside of the loop, use
441 // the value before incrementation, otherwise use it after incrementation.
Chris Lattnercb367102006-01-11 05:10:20 +0000442 if (IVUseShouldUsePostIncValue(User, I, L, EF, this)) {
Chris Lattnera6764832005-09-12 06:04:47 +0000443 // The value used will be incremented by the stride more than we are
444 // expecting, so subtract this off.
445 SCEVHandle NewStart = SCEV::getMinusSCEV(Start, Stride);
Chris Lattner4ea0a3e2005-10-09 06:20:55 +0000446 StrideUses.addUser(NewStart, User, I);
447 StrideUses.Users.back().isUseOfPostIncrementedValue = true;
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000448 DOUT << " USING POSTINC SCEV, START=" << *NewStart<< "\n";
Chris Lattnere4ed42a2005-10-03 01:04:44 +0000449 } else {
Chris Lattner4ea0a3e2005-10-09 06:20:55 +0000450 StrideUses.addUser(Start, User, I);
Chris Lattnera6764832005-09-12 06:04:47 +0000451 }
Nate Begemane68bcd12005-07-30 00:15:07 +0000452 }
453 }
454 return true;
455}
456
457namespace {
458 /// BasedUser - For a particular base value, keep information about how we've
459 /// partitioned the expression so far.
460 struct BasedUser {
Chris Lattner37c24cc2005-08-08 22:56:21 +0000461 /// Base - The Base value for the PHI node that needs to be inserted for
462 /// this use. As the use is processed, information gets moved from this
463 /// field to the Imm field (below). BasedUser values are sorted by this
464 /// field.
465 SCEVHandle Base;
466
Nate Begemane68bcd12005-07-30 00:15:07 +0000467 /// Inst - The instruction using the induction variable.
468 Instruction *Inst;
469
Chris Lattner430d0022005-08-03 22:21:05 +0000470 /// OperandValToReplace - The operand value of Inst to replace with the
471 /// EmittedBase.
472 Value *OperandValToReplace;
Nate Begemane68bcd12005-07-30 00:15:07 +0000473
474 /// Imm - The immediate value that should be added to the base immediately
475 /// before Inst, because it will be folded into the imm field of the
476 /// instruction.
477 SCEVHandle Imm;
478
479 /// EmittedBase - The actual value* to use for the base value of this
480 /// operation. This is null if we should just use zero so far.
481 Value *EmittedBase;
482
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000483 // isUseOfPostIncrementedValue - True if this should use the
484 // post-incremented version of this IV, not the preincremented version.
485 // This can only be set in special cases, such as the terminating setcc
Chris Lattnera6764832005-09-12 06:04:47 +0000486 // instruction for a loop and uses outside the loop that are dominated by
487 // the loop.
Chris Lattner9bfa6f82005-08-08 05:28:22 +0000488 bool isUseOfPostIncrementedValue;
Chris Lattner37c24cc2005-08-08 22:56:21 +0000489
490 BasedUser(IVStrideUse &IVSU)
491 : Base(IVSU.Offset), Inst(IVSU.User),
492 OperandValToReplace(IVSU.OperandValToReplace),
493 Imm(SCEVUnknown::getIntegerSCEV(0, Base->getType())), EmittedBase(0),
494 isUseOfPostIncrementedValue(IVSU.isUseOfPostIncrementedValue) {}
Nate Begemane68bcd12005-07-30 00:15:07 +0000495
Chris Lattnera6d7c352005-08-04 20:03:32 +0000496 // Once we rewrite the code to insert the new IVs we want, update the
497 // operands of Inst to use the new expression 'NewBase', with 'Imm' added
498 // to it.
Chris Lattnera091ff12005-08-09 00:18:09 +0000499 void RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
Chris Lattner8447b492005-08-12 22:22:17 +0000500 SCEVExpander &Rewriter, Loop *L,
501 Pass *P);
Chris Lattner2959f002006-02-04 07:36:50 +0000502
503 Value *InsertCodeForBaseAtPosition(const SCEVHandle &NewBase,
504 SCEVExpander &Rewriter,
505 Instruction *IP, Loop *L);
Nate Begemane68bcd12005-07-30 00:15:07 +0000506 void dump() const;
507 };
508}
509
510void BasedUser::dump() const {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000511 cerr << " Base=" << *Base;
512 cerr << " Imm=" << *Imm;
Nate Begemane68bcd12005-07-30 00:15:07 +0000513 if (EmittedBase)
Bill Wendlingf3baad32006-12-07 01:30:32 +0000514 cerr << " EB=" << *EmittedBase;
Nate Begemane68bcd12005-07-30 00:15:07 +0000515
Bill Wendlingf3baad32006-12-07 01:30:32 +0000516 cerr << " Inst: " << *Inst;
Nate Begemane68bcd12005-07-30 00:15:07 +0000517}
518
Chris Lattner2959f002006-02-04 07:36:50 +0000519Value *BasedUser::InsertCodeForBaseAtPosition(const SCEVHandle &NewBase,
520 SCEVExpander &Rewriter,
521 Instruction *IP, Loop *L) {
522 // Figure out where we *really* want to insert this code. In particular, if
523 // the user is inside of a loop that is nested inside of L, we really don't
524 // want to insert this expression before the user, we'd rather pull it out as
525 // many loops as possible.
526 LoopInfo &LI = Rewriter.getLoopInfo();
527 Instruction *BaseInsertPt = IP;
528
529 // Figure out the most-nested loop that IP is in.
530 Loop *InsertLoop = LI.getLoopFor(IP->getParent());
531
532 // If InsertLoop is not L, and InsertLoop is nested inside of L, figure out
533 // the preheader of the outer-most loop where NewBase is not loop invariant.
534 while (InsertLoop && NewBase->isLoopInvariant(InsertLoop)) {
535 BaseInsertPt = InsertLoop->getLoopPreheader()->getTerminator();
536 InsertLoop = InsertLoop->getParentLoop();
537 }
538
539 // If there is no immediate value, skip the next part.
540 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Imm))
Reid Spencer53a37392007-03-02 23:51:25 +0000541 if (SC->getValue()->isZero())
Chris Lattner2959f002006-02-04 07:36:50 +0000542 return Rewriter.expandCodeFor(NewBase, BaseInsertPt,
543 OperandValToReplace->getType());
544
545 Value *Base = Rewriter.expandCodeFor(NewBase, BaseInsertPt);
546
547 // Always emit the immediate (if non-zero) into the same block as the user.
548 SCEVHandle NewValSCEV = SCEVAddExpr::get(SCEVUnknown::get(Base), Imm);
549 return Rewriter.expandCodeFor(NewValSCEV, IP,
550 OperandValToReplace->getType());
551}
552
553
Chris Lattnera6d7c352005-08-04 20:03:32 +0000554// Once we rewrite the code to insert the new IVs we want, update the
555// operands of Inst to use the new expression 'NewBase', with 'Imm' added
556// to it.
Chris Lattnera091ff12005-08-09 00:18:09 +0000557void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
Chris Lattner4fec86d2005-08-12 22:06:11 +0000558 SCEVExpander &Rewriter,
Chris Lattner8447b492005-08-12 22:22:17 +0000559 Loop *L, Pass *P) {
Chris Lattnera6d7c352005-08-04 20:03:32 +0000560 if (!isa<PHINode>(Inst)) {
Chris Lattner2959f002006-02-04 07:36:50 +0000561 Value *NewVal = InsertCodeForBaseAtPosition(NewBase, Rewriter, Inst, L);
Chris Lattnera6d7c352005-08-04 20:03:32 +0000562 // Replace the use of the operand Value with the new Phi we just created.
563 Inst->replaceUsesOfWith(OperandValToReplace, NewVal);
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000564 DOUT << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst;
Chris Lattnera6d7c352005-08-04 20:03:32 +0000565 return;
566 }
567
568 // PHI nodes are more complex. We have to insert one copy of the NewBase+Imm
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000569 // expression into each operand block that uses it. Note that PHI nodes can
570 // have multiple entries for the same predecessor. We use a map to make sure
571 // that a PHI node only has a single Value* for each predecessor (which also
572 // prevents us from inserting duplicate code in some blocks).
573 std::map<BasicBlock*, Value*> InsertedCode;
Chris Lattnera6d7c352005-08-04 20:03:32 +0000574 PHINode *PN = cast<PHINode>(Inst);
575 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
576 if (PN->getIncomingValue(i) == OperandValToReplace) {
Chris Lattner4fec86d2005-08-12 22:06:11 +0000577 // If this is a critical edge, split the edge so that we do not insert the
Chris Lattnerfd018c82005-09-13 02:09:55 +0000578 // code on all predecessor/successor paths. We do this unless this is the
579 // canonical backedge for this loop, as this can make some inserted code
580 // be in an illegal position.
Chris Lattner8fcce172005-10-03 00:31:52 +0000581 BasicBlock *PHIPred = PN->getIncomingBlock(i);
582 if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
583 (PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
Chris Lattner8fcce172005-10-03 00:31:52 +0000584
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000585 // First step, split the critical edge.
Chris Lattnera6eb7e02006-10-28 06:45:33 +0000586 SplitCriticalEdge(PHIPred, PN->getParent(), P, true);
Chris Lattner8447b492005-08-12 22:22:17 +0000587
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000588 // Next step: move the basic block. In particular, if the PHI node
589 // is outside of the loop, and PredTI is in the loop, we want to
590 // move the block to be immediately before the PHI block, not
591 // immediately after PredTI.
Chris Lattner8fcce172005-10-03 00:31:52 +0000592 if (L->contains(PHIPred) && !L->contains(PN->getParent())) {
Chris Lattner2bf7cb52005-08-17 06:35:16 +0000593 BasicBlock *NewBB = PN->getIncomingBlock(i);
594 NewBB->moveBefore(PN->getParent());
Chris Lattner4fec86d2005-08-12 22:06:11 +0000595 }
Chris Lattner5191c652006-10-28 00:59:20 +0000596
597 // Splitting the edge can reduce the number of PHI entries we have.
598 e = PN->getNumIncomingValues();
Chris Lattner4fec86d2005-08-12 22:06:11 +0000599 }
Chris Lattnera6d7c352005-08-04 20:03:32 +0000600
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000601 Value *&Code = InsertedCode[PN->getIncomingBlock(i)];
602 if (!Code) {
603 // Insert the code into the end of the predecessor block.
Chris Lattner2959f002006-02-04 07:36:50 +0000604 Instruction *InsertPt = PN->getIncomingBlock(i)->getTerminator();
605 Code = InsertCodeForBaseAtPosition(NewBase, Rewriter, InsertPt, L);
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000606 }
Chris Lattnera6d7c352005-08-04 20:03:32 +0000607
608 // Replace the use of the operand Value with the new Phi we just created.
Chris Lattnerdde7dc52005-08-10 00:35:32 +0000609 PN->setIncomingValue(i, Code);
Chris Lattnera6d7c352005-08-04 20:03:32 +0000610 Rewriter.clear();
611 }
612 }
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000613 DOUT << " CHANGED: IMM =" << *Imm << " Inst = " << *Inst;
Chris Lattnera6d7c352005-08-04 20:03:32 +0000614}
615
616
Nate Begemane68bcd12005-07-30 00:15:07 +0000617/// isTargetConstant - Return true if the following can be referenced by the
618/// immediate field of a target instruction.
Evan Chengb5eb9322007-03-13 20:34:37 +0000619static bool isTargetConstant(const SCEVHandle &V, const Type *UseTy,
620 const TargetLowering *TLI) {
Chris Lattner14203e82005-08-08 06:25:50 +0000621 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
Evan Cheng720acdf2007-03-12 23:27:37 +0000622 int64_t VC = SC->getValue()->getSExtValue();
Evan Chengc567c4e2006-03-13 23:14:23 +0000623 if (TLI)
Evan Chengb5eb9322007-03-13 20:34:37 +0000624 return TLI->isLegalAddressImmediate(VC, UseTy);
Evan Chengc567c4e2006-03-13 23:14:23 +0000625 else
626 // Defaults to PPC. PPC allows a sign-extended 16-bit immediate field.
Evan Cheng720acdf2007-03-12 23:27:37 +0000627 return (VC > -(1 << 16) && VC < (1 << 16)-1);
Chris Lattner14203e82005-08-08 06:25:50 +0000628 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000629
Nate Begemane68bcd12005-07-30 00:15:07 +0000630 if (SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V))
631 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(SU->getValue()))
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000632 if (CE->getOpcode() == Instruction::PtrToInt) {
Evan Chengc567c4e2006-03-13 23:14:23 +0000633 Constant *Op0 = CE->getOperand(0);
Reid Spencer6c38f0b2006-11-27 01:05:10 +0000634 if (isa<GlobalValue>(Op0) && TLI &&
Evan Chengc567c4e2006-03-13 23:14:23 +0000635 TLI->isLegalAddressImmediate(cast<GlobalValue>(Op0)))
Nate Begemane68bcd12005-07-30 00:15:07 +0000636 return true;
Evan Chengc567c4e2006-03-13 23:14:23 +0000637 }
Nate Begemane68bcd12005-07-30 00:15:07 +0000638 return false;
639}
640
Chris Lattner37ed8952005-08-08 22:32:34 +0000641/// MoveLoopVariantsToImediateField - Move any subexpressions from Val that are
642/// loop varying to the Imm operand.
643static void MoveLoopVariantsToImediateField(SCEVHandle &Val, SCEVHandle &Imm,
644 Loop *L) {
645 if (Val->isLoopInvariant(L)) return; // Nothing to do.
646
647 if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
648 std::vector<SCEVHandle> NewOps;
649 NewOps.reserve(SAE->getNumOperands());
650
651 for (unsigned i = 0; i != SAE->getNumOperands(); ++i)
652 if (!SAE->getOperand(i)->isLoopInvariant(L)) {
653 // If this is a loop-variant expression, it must stay in the immediate
654 // field of the expression.
655 Imm = SCEVAddExpr::get(Imm, SAE->getOperand(i));
656 } else {
657 NewOps.push_back(SAE->getOperand(i));
658 }
659
660 if (NewOps.empty())
661 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
662 else
663 Val = SCEVAddExpr::get(NewOps);
664 } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
665 // Try to pull immediates out of the start value of nested addrec's.
666 SCEVHandle Start = SARE->getStart();
667 MoveLoopVariantsToImediateField(Start, Imm, L);
668
669 std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
670 Ops[0] = Start;
671 Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
672 } else {
673 // Otherwise, all of Val is variant, move the whole thing over.
674 Imm = SCEVAddExpr::get(Imm, Val);
675 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
676 }
677}
678
679
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000680/// MoveImmediateValues - Look at Val, and pull out any additions of constants
Nate Begemane68bcd12005-07-30 00:15:07 +0000681/// that can fit into the immediate field of instructions in the target.
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000682/// Accumulate these immediate values into the Imm value.
Evan Chengc567c4e2006-03-13 23:14:23 +0000683static void MoveImmediateValues(const TargetLowering *TLI,
Evan Chengb5eb9322007-03-13 20:34:37 +0000684 Instruction *User,
Evan Chengc567c4e2006-03-13 23:14:23 +0000685 SCEVHandle &Val, SCEVHandle &Imm,
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000686 bool isAddress, Loop *L) {
Evan Chengb5eb9322007-03-13 20:34:37 +0000687 const Type *UseTy = User->getType();
688 if (StoreInst *SI = dyn_cast<StoreInst>(User))
689 UseTy = SI->getOperand(0)->getType();
690
Chris Lattnerfc624702005-08-03 23:44:42 +0000691 if (SCEVAddExpr *SAE = dyn_cast<SCEVAddExpr>(Val)) {
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000692 std::vector<SCEVHandle> NewOps;
693 NewOps.reserve(SAE->getNumOperands());
694
Chris Lattner2959f002006-02-04 07:36:50 +0000695 for (unsigned i = 0; i != SAE->getNumOperands(); ++i) {
696 SCEVHandle NewOp = SAE->getOperand(i);
Evan Chengb5eb9322007-03-13 20:34:37 +0000697 MoveImmediateValues(TLI, User, NewOp, Imm, isAddress, L);
Chris Lattner2959f002006-02-04 07:36:50 +0000698
699 if (!NewOp->isLoopInvariant(L)) {
Chris Lattneracc42c42005-08-04 19:08:16 +0000700 // If this is a loop-variant expression, it must stay in the immediate
701 // field of the expression.
Chris Lattner2959f002006-02-04 07:36:50 +0000702 Imm = SCEVAddExpr::get(Imm, NewOp);
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000703 } else {
Chris Lattner2959f002006-02-04 07:36:50 +0000704 NewOps.push_back(NewOp);
Nate Begemane68bcd12005-07-30 00:15:07 +0000705 }
Chris Lattner2959f002006-02-04 07:36:50 +0000706 }
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000707
708 if (NewOps.empty())
709 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
710 else
711 Val = SCEVAddExpr::get(NewOps);
712 return;
Chris Lattnerfc624702005-08-03 23:44:42 +0000713 } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Val)) {
714 // Try to pull immediates out of the start value of nested addrec's.
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000715 SCEVHandle Start = SARE->getStart();
Evan Chengb5eb9322007-03-13 20:34:37 +0000716 MoveImmediateValues(TLI, User, Start, Imm, isAddress, L);
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000717
718 if (Start != SARE->getStart()) {
719 std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
720 Ops[0] = Start;
721 Val = SCEVAddRecExpr::get(Ops, SARE->getLoop());
722 }
723 return;
Chris Lattner2959f002006-02-04 07:36:50 +0000724 } else if (SCEVMulExpr *SME = dyn_cast<SCEVMulExpr>(Val)) {
725 // Transform "8 * (4 + v)" -> "32 + 8*V" if "32" fits in the immed field.
Evan Chengb5eb9322007-03-13 20:34:37 +0000726 if (isAddress && isTargetConstant(SME->getOperand(0), UseTy, TLI) &&
Chris Lattner2959f002006-02-04 07:36:50 +0000727 SME->getNumOperands() == 2 && SME->isLoopInvariant(L)) {
728
729 SCEVHandle SubImm = SCEVUnknown::getIntegerSCEV(0, Val->getType());
730 SCEVHandle NewOp = SME->getOperand(1);
Evan Chengb5eb9322007-03-13 20:34:37 +0000731 MoveImmediateValues(TLI, User, NewOp, SubImm, isAddress, L);
Chris Lattner2959f002006-02-04 07:36:50 +0000732
733 // If we extracted something out of the subexpressions, see if we can
734 // simplify this!
735 if (NewOp != SME->getOperand(1)) {
736 // Scale SubImm up by "8". If the result is a target constant, we are
737 // good.
738 SubImm = SCEVMulExpr::get(SubImm, SME->getOperand(0));
Evan Chengb5eb9322007-03-13 20:34:37 +0000739 if (isTargetConstant(SubImm, UseTy, TLI)) {
Chris Lattner2959f002006-02-04 07:36:50 +0000740 // Accumulate the immediate.
741 Imm = SCEVAddExpr::get(Imm, SubImm);
742
743 // Update what is left of 'Val'.
744 Val = SCEVMulExpr::get(SME->getOperand(0), NewOp);
745 return;
746 }
747 }
748 }
Nate Begemane68bcd12005-07-30 00:15:07 +0000749 }
750
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000751 // Loop-variant expressions must stay in the immediate field of the
752 // expression.
Evan Chengb5eb9322007-03-13 20:34:37 +0000753 if ((isAddress && isTargetConstant(Val, UseTy, TLI)) ||
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000754 !Val->isLoopInvariant(L)) {
755 Imm = SCEVAddExpr::get(Imm, Val);
756 Val = SCEVUnknown::getIntegerSCEV(0, Val->getType());
757 return;
Chris Lattner0f7c0fa2005-08-04 19:26:19 +0000758 }
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000759
760 // Otherwise, no immediates to move.
Nate Begemane68bcd12005-07-30 00:15:07 +0000761}
762
Chris Lattner5949d492005-08-13 07:27:18 +0000763
Chris Lattner3ff62012006-08-03 06:34:50 +0000764/// SeparateSubExprs - Decompose Expr into all of the subexpressions that are
765/// added together. This is used to reassociate common addition subexprs
766/// together for maximal sharing when rewriting bases.
Chris Lattner5949d492005-08-13 07:27:18 +0000767static void SeparateSubExprs(std::vector<SCEVHandle> &SubExprs,
768 SCEVHandle Expr) {
769 if (SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(Expr)) {
770 for (unsigned j = 0, e = AE->getNumOperands(); j != e; ++j)
771 SeparateSubExprs(SubExprs, AE->getOperand(j));
772 } else if (SCEVAddRecExpr *SARE = dyn_cast<SCEVAddRecExpr>(Expr)) {
773 SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Expr->getType());
774 if (SARE->getOperand(0) == Zero) {
775 SubExprs.push_back(Expr);
776 } else {
777 // Compute the addrec with zero as its base.
778 std::vector<SCEVHandle> Ops(SARE->op_begin(), SARE->op_end());
779 Ops[0] = Zero; // Start with zero base.
780 SubExprs.push_back(SCEVAddRecExpr::get(Ops, SARE->getLoop()));
781
782
783 SeparateSubExprs(SubExprs, SARE->getOperand(0));
784 }
785 } else if (!isa<SCEVConstant>(Expr) ||
Reid Spencer53a37392007-03-02 23:51:25 +0000786 !cast<SCEVConstant>(Expr)->getValue()->isZero()) {
Chris Lattner5949d492005-08-13 07:27:18 +0000787 // Do not add zero.
788 SubExprs.push_back(Expr);
789 }
790}
791
792
Chris Lattnera091ff12005-08-09 00:18:09 +0000793/// RemoveCommonExpressionsFromUseBases - Look through all of the uses in Bases,
794/// removing any common subexpressions from it. Anything truly common is
795/// removed, accumulated, and returned. This looks for things like (a+b+c) and
796/// (a+c+d) -> (a+c). The common expression is *removed* from the Bases.
797static SCEVHandle
798RemoveCommonExpressionsFromUseBases(std::vector<BasedUser> &Uses) {
799 unsigned NumUses = Uses.size();
800
801 // Only one use? Use its base, regardless of what it is!
802 SCEVHandle Zero = SCEVUnknown::getIntegerSCEV(0, Uses[0].Base->getType());
803 SCEVHandle Result = Zero;
804 if (NumUses == 1) {
805 std::swap(Result, Uses[0].Base);
806 return Result;
807 }
808
809 // To find common subexpressions, count how many of Uses use each expression.
810 // If any subexpressions are used Uses.size() times, they are common.
811 std::map<SCEVHandle, unsigned> SubExpressionUseCounts;
812
Chris Lattner192cd182005-10-11 18:41:04 +0000813 // UniqueSubExprs - Keep track of all of the subexpressions we see in the
814 // order we see them.
815 std::vector<SCEVHandle> UniqueSubExprs;
816
Chris Lattner5949d492005-08-13 07:27:18 +0000817 std::vector<SCEVHandle> SubExprs;
818 for (unsigned i = 0; i != NumUses; ++i) {
819 // If the base is zero (which is common), return zero now, there are no
820 // CSEs we can find.
821 if (Uses[i].Base == Zero) return Zero;
822
823 // Split the expression into subexprs.
824 SeparateSubExprs(SubExprs, Uses[i].Base);
825 // Add one to SubExpressionUseCounts for each subexpr present.
826 for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
Chris Lattner192cd182005-10-11 18:41:04 +0000827 if (++SubExpressionUseCounts[SubExprs[j]] == 1)
828 UniqueSubExprs.push_back(SubExprs[j]);
Chris Lattner5949d492005-08-13 07:27:18 +0000829 SubExprs.clear();
830 }
831
Chris Lattner192cd182005-10-11 18:41:04 +0000832 // Now that we know how many times each is used, build Result. Iterate over
833 // UniqueSubexprs so that we have a stable ordering.
834 for (unsigned i = 0, e = UniqueSubExprs.size(); i != e; ++i) {
835 std::map<SCEVHandle, unsigned>::iterator I =
836 SubExpressionUseCounts.find(UniqueSubExprs[i]);
837 assert(I != SubExpressionUseCounts.end() && "Entry not found?");
Chris Lattnera091ff12005-08-09 00:18:09 +0000838 if (I->second == NumUses) { // Found CSE!
839 Result = SCEVAddExpr::get(Result, I->first);
Chris Lattnera091ff12005-08-09 00:18:09 +0000840 } else {
841 // Remove non-cse's from SubExpressionUseCounts.
Chris Lattner192cd182005-10-11 18:41:04 +0000842 SubExpressionUseCounts.erase(I);
Chris Lattnera091ff12005-08-09 00:18:09 +0000843 }
Chris Lattner192cd182005-10-11 18:41:04 +0000844 }
Chris Lattnera091ff12005-08-09 00:18:09 +0000845
846 // If we found no CSE's, return now.
847 if (Result == Zero) return Result;
848
849 // Otherwise, remove all of the CSE's we found from each of the base values.
Chris Lattner5949d492005-08-13 07:27:18 +0000850 for (unsigned i = 0; i != NumUses; ++i) {
851 // Split the expression into subexprs.
852 SeparateSubExprs(SubExprs, Uses[i].Base);
853
854 // Remove any common subexpressions.
855 for (unsigned j = 0, e = SubExprs.size(); j != e; ++j)
856 if (SubExpressionUseCounts.count(SubExprs[j])) {
857 SubExprs.erase(SubExprs.begin()+j);
858 --j; --e;
859 }
860
861 // Finally, the non-shared expressions together.
862 if (SubExprs.empty())
Chris Lattnera091ff12005-08-09 00:18:09 +0000863 Uses[i].Base = Zero;
Chris Lattner5949d492005-08-13 07:27:18 +0000864 else
865 Uses[i].Base = SCEVAddExpr::get(SubExprs);
Chris Lattner47d3ec32005-08-13 07:42:01 +0000866 SubExprs.clear();
Chris Lattner5949d492005-08-13 07:27:18 +0000867 }
Chris Lattnera091ff12005-08-09 00:18:09 +0000868
869 return Result;
870}
871
Evan Cheng3df447d2006-03-16 21:53:05 +0000872/// isZero - returns true if the scalar evolution expression is zero.
873///
874static bool isZero(SCEVHandle &V) {
875 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V))
Reid Spencer53a37392007-03-02 23:51:25 +0000876 return SC->getValue()->isZero();
Evan Cheng3df447d2006-03-16 21:53:05 +0000877 return false;
878}
879
Dale Johannesene3a02be2007-03-20 00:47:50 +0000880/// ValidStride - Check whether the given Scale is valid for all loads and
881/// stores in UsersToProcess. Pulled into a function to avoid disturbing the
882/// sensibilities of those who dislike goto's.
883///
884bool LoopStrengthReduce::ValidStride(int64_t Scale,
885 const std::vector<BasedUser>& UsersToProcess) {
886 for (unsigned i=0, e = UsersToProcess.size(); i!=e; ++i)
887 if (!TLI->isLegalAddressScale(Scale, UsersToProcess[i].Inst->getType()))
888 return false;
889 return true;
890}
Chris Lattnera091ff12005-08-09 00:18:09 +0000891
Evan Cheng45206982006-03-17 19:52:23 +0000892/// CheckForIVReuse - Returns the multiple if the stride is the multiple
893/// of a previous stride and it is a legal value for the target addressing
894/// mode scale component. This allows the users of this stride to be rewritten
Evan Chengc28282b2006-03-18 08:03:12 +0000895/// as prev iv * factor. It returns 0 if no reuse is possible.
Dale Johannesene3a02be2007-03-20 00:47:50 +0000896unsigned LoopStrengthReduce::CheckForIVReuse(const SCEVHandle &Stride,
897 IVExpr &IV, const Type *Ty,
898 const std::vector<BasedUser>& UsersToProcess) {
Evan Chengc28282b2006-03-18 08:03:12 +0000899 if (!TLI) return 0;
Evan Cheng45206982006-03-17 19:52:23 +0000900
901 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Stride)) {
Reid Spencerba547cb2007-03-02 23:37:53 +0000902 int64_t SInt = SC->getValue()->getSExtValue();
903 if (SInt == 1) return 0;
Evan Cheng45206982006-03-17 19:52:23 +0000904
Evan Cheng720acdf2007-03-12 23:27:37 +0000905 for (std::map<SCEVHandle, IVsOfOneStride>::iterator SI= IVsByStride.begin(),
906 SE = IVsByStride.end(); SI != SE; ++SI) {
907 int64_t SSInt = cast<SCEVConstant>(SI->first)->getValue()->getSExtValue();
908 if (unsigned(abs(SInt)) < SSInt || (SInt % SSInt) != 0)
Evan Cheng45206982006-03-17 19:52:23 +0000909 continue;
Evan Cheng720acdf2007-03-12 23:27:37 +0000910 int64_t Scale = SInt / SSInt;
Dale Johannesene3a02be2007-03-20 00:47:50 +0000911 // Check that this stride is valid for all the types used for loads and
912 // stores; if it can be used for some and not others, we might as well use
913 // the original stride everywhere, since we have to create the IV for it
914 // anyway.
915 if (ValidStride(Scale, UsersToProcess))
Evan Cheng720acdf2007-03-12 23:27:37 +0000916 for (std::vector<IVExpr>::iterator II = SI->second.IVs.begin(),
917 IE = SI->second.IVs.end(); II != IE; ++II)
918 // FIXME: Only handle base == 0 for now.
919 // Only reuse previous IV if it would not require a type conversion.
920 if (isZero(II->Base) && II->Base->getType() == Ty) {
921 IV = *II;
922 return Scale;
923 }
Evan Cheng45206982006-03-17 19:52:23 +0000924 }
925 }
Evan Chengc28282b2006-03-18 08:03:12 +0000926 return 0;
Evan Cheng45206982006-03-17 19:52:23 +0000927}
928
Chris Lattner3ff62012006-08-03 06:34:50 +0000929/// PartitionByIsUseOfPostIncrementedValue - Simple boolean predicate that
930/// returns true if Val's isUseOfPostIncrementedValue is true.
931static bool PartitionByIsUseOfPostIncrementedValue(const BasedUser &Val) {
932 return Val.isUseOfPostIncrementedValue;
933}
Evan Cheng45206982006-03-17 19:52:23 +0000934
Nate Begemane68bcd12005-07-30 00:15:07 +0000935/// StrengthReduceStridedIVUsers - Strength reduce all of the users of a single
936/// stride of IV. All of the users may have different starting values, and this
937/// may not be the only stride (we know it is if isOnlyStride is true).
Chris Lattneredff91a2005-08-10 00:45:21 +0000938void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
Chris Lattner430d0022005-08-03 22:21:05 +0000939 IVUsersOfOneStride &Uses,
940 Loop *L,
Nate Begemane68bcd12005-07-30 00:15:07 +0000941 bool isOnlyStride) {
942 // Transform our list of users and offsets to a bit more complex table. In
Chris Lattner37c24cc2005-08-08 22:56:21 +0000943 // this new vector, each 'BasedUser' contains 'Base' the base of the
944 // strided accessas well as the old information from Uses. We progressively
945 // move information from the Base field to the Imm field, until we eventually
946 // have the full access expression to rewrite the use.
947 std::vector<BasedUser> UsersToProcess;
Nate Begemane68bcd12005-07-30 00:15:07 +0000948 UsersToProcess.reserve(Uses.Users.size());
Chris Lattner37c24cc2005-08-08 22:56:21 +0000949 for (unsigned i = 0, e = Uses.Users.size(); i != e; ++i) {
950 UsersToProcess.push_back(Uses.Users[i]);
951
952 // Move any loop invariant operands from the offset field to the immediate
953 // field of the use, so that we don't try to use something before it is
954 // computed.
955 MoveLoopVariantsToImediateField(UsersToProcess.back().Base,
956 UsersToProcess.back().Imm, L);
957 assert(UsersToProcess.back().Base->isLoopInvariant(L) &&
Chris Lattner45f8b6e2005-08-04 22:34:05 +0000958 "Base value is not loop invariant!");
Nate Begemane68bcd12005-07-30 00:15:07 +0000959 }
Evan Cheng45206982006-03-17 19:52:23 +0000960
Chris Lattnera091ff12005-08-09 00:18:09 +0000961 // We now have a whole bunch of uses of like-strided induction variables, but
962 // they might all have different bases. We want to emit one PHI node for this
963 // stride which we fold as many common expressions (between the IVs) into as
964 // possible. Start by identifying the common expressions in the base values
965 // for the strides (e.g. if we have "A+C+B" and "A+B+D" as our bases, find
966 // "A+B"), emit it to the preheader, then remove the expression from the
967 // UsersToProcess base values.
Evan Cheng3df447d2006-03-16 21:53:05 +0000968 SCEVHandle CommonExprs =
969 RemoveCommonExpressionsFromUseBases(UsersToProcess);
Chris Lattnera091ff12005-08-09 00:18:09 +0000970
Evan Chenge9c68f52006-07-18 19:07:58 +0000971 // Check if it is possible to reuse a IV with stride that is factor of this
972 // stride. And the multiple is a number that can be encoded in the scale
973 // field of the target addressing mode.
974 PHINode *NewPHI = NULL;
975 Value *IncV = NULL;
976 IVExpr ReuseIV;
977 unsigned RewriteFactor = CheckForIVReuse(Stride, ReuseIV,
Dale Johannesene3a02be2007-03-20 00:47:50 +0000978 CommonExprs->getType(),
979 UsersToProcess);
Evan Chenge9c68f52006-07-18 19:07:58 +0000980 if (RewriteFactor != 0) {
Bill Wendling5dbf43c2006-11-26 09:46:52 +0000981 DOUT << "BASED ON IV of STRIDE " << *ReuseIV.Stride
982 << " and BASE " << *ReuseIV.Base << " :\n";
Evan Chenge9c68f52006-07-18 19:07:58 +0000983 NewPHI = ReuseIV.PHI;
984 IncV = ReuseIV.IncV;
985 }
986
Chris Lattner37ed8952005-08-08 22:32:34 +0000987 // Next, figure out what we can represent in the immediate fields of
988 // instructions. If we can represent anything there, move it to the imm
Chris Lattnera091ff12005-08-09 00:18:09 +0000989 // fields of the BasedUsers. We do this so that it increases the commonality
990 // of the remaining uses.
Chris Lattner37ed8952005-08-08 22:32:34 +0000991 for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
Chris Lattner5cf983e2005-08-16 00:38:11 +0000992 // If the user is not in the current loop, this means it is using the exit
993 // value of the IV. Do not put anything in the base, make sure it's all in
994 // the immediate field to allow as much factoring as possible.
995 if (!L->contains(UsersToProcess[i].Inst->getParent())) {
Chris Lattnerea7dfd52005-08-17 21:22:41 +0000996 UsersToProcess[i].Imm = SCEVAddExpr::get(UsersToProcess[i].Imm,
997 UsersToProcess[i].Base);
998 UsersToProcess[i].Base =
999 SCEVUnknown::getIntegerSCEV(0, UsersToProcess[i].Base->getType());
Chris Lattner5cf983e2005-08-16 00:38:11 +00001000 } else {
1001
1002 // Addressing modes can be folded into loads and stores. Be careful that
1003 // the store is through the expression, not of the expression though.
1004 bool isAddress = isa<LoadInst>(UsersToProcess[i].Inst);
1005 if (StoreInst *SI = dyn_cast<StoreInst>(UsersToProcess[i].Inst))
1006 if (SI->getOperand(1) == UsersToProcess[i].OperandValToReplace)
1007 isAddress = true;
1008
Evan Chengb5eb9322007-03-13 20:34:37 +00001009 MoveImmediateValues(TLI, UsersToProcess[i].Inst, UsersToProcess[i].Base,
1010 UsersToProcess[i].Imm, isAddress, L);
Chris Lattner5cf983e2005-08-16 00:38:11 +00001011 }
Chris Lattner37ed8952005-08-08 22:32:34 +00001012 }
Evan Cheng3df447d2006-03-16 21:53:05 +00001013
Chris Lattnera091ff12005-08-09 00:18:09 +00001014 // Now that we know what we need to do, insert the PHI node itself.
1015 //
Bill Wendling5dbf43c2006-11-26 09:46:52 +00001016 DOUT << "INSERTING IV of STRIDE " << *Stride << " and BASE "
1017 << *CommonExprs << " :\n";
Evan Cheng3df447d2006-03-16 21:53:05 +00001018
Chris Lattnera091ff12005-08-09 00:18:09 +00001019 SCEVExpander Rewriter(*SE, *LI);
1020 SCEVExpander PreheaderRewriter(*SE, *LI);
Chris Lattner37ed8952005-08-08 22:32:34 +00001021
Chris Lattnera091ff12005-08-09 00:18:09 +00001022 BasicBlock *Preheader = L->getLoopPreheader();
1023 Instruction *PreInsertPt = Preheader->getTerminator();
1024 Instruction *PhiInsertBefore = L->getHeader()->begin();
Chris Lattner37ed8952005-08-08 22:32:34 +00001025
Chris Lattner8048b852005-09-12 17:11:27 +00001026 BasicBlock *LatchBlock = L->getLoopLatch();
Evan Cheng3df447d2006-03-16 21:53:05 +00001027
Chris Lattnera091ff12005-08-09 00:18:09 +00001028 const Type *ReplacedTy = CommonExprs->getType();
Evan Cheng45206982006-03-17 19:52:23 +00001029
1030 // Emit the initial base value into the loop preheader.
1031 Value *CommonBaseV
1032 = PreheaderRewriter.expandCodeFor(CommonExprs, PreInsertPt,
1033 ReplacedTy);
1034
Evan Chengc28282b2006-03-18 08:03:12 +00001035 if (RewriteFactor == 0) {
Evan Cheng3df447d2006-03-16 21:53:05 +00001036 // Create a new Phi for this base, and stick it in the loop header.
1037 NewPHI = new PHINode(ReplacedTy, "iv.", PhiInsertBefore);
1038 ++NumInserted;
Chris Lattnera091ff12005-08-09 00:18:09 +00001039
Evan Cheng45206982006-03-17 19:52:23 +00001040 // Add common base to the new Phi node.
1041 NewPHI->addIncoming(CommonBaseV, Preheader);
1042
Evan Cheng3df447d2006-03-16 21:53:05 +00001043 // Insert the stride into the preheader.
1044 Value *StrideV = PreheaderRewriter.expandCodeFor(Stride, PreInsertPt,
1045 ReplacedTy);
1046 if (!isa<ConstantInt>(StrideV)) ++NumVariable;
Chris Lattneredff91a2005-08-10 00:45:21 +00001047
Evan Cheng3df447d2006-03-16 21:53:05 +00001048 // Emit the increment of the base value before the terminator of the loop
1049 // latch block, and add it to the Phi node.
1050 SCEVHandle IncExp = SCEVAddExpr::get(SCEVUnknown::get(NewPHI),
1051 SCEVUnknown::get(StrideV));
Chris Lattnera091ff12005-08-09 00:18:09 +00001052
Evan Cheng3df447d2006-03-16 21:53:05 +00001053 IncV = Rewriter.expandCodeFor(IncExp, LatchBlock->getTerminator(),
1054 ReplacedTy);
1055 IncV->setName(NewPHI->getName()+".inc");
1056 NewPHI->addIncoming(IncV, LatchBlock);
1057
Evan Cheng45206982006-03-17 19:52:23 +00001058 // Remember this in case a later stride is multiple of this.
Evan Chengc28282b2006-03-18 08:03:12 +00001059 IVsByStride[Stride].addIV(Stride, CommonExprs, NewPHI, IncV);
Evan Cheng45206982006-03-17 19:52:23 +00001060 } else {
1061 Constant *C = dyn_cast<Constant>(CommonBaseV);
1062 if (!C ||
1063 (!C->isNullValue() &&
Evan Chengb5eb9322007-03-13 20:34:37 +00001064 !isTargetConstant(SCEVUnknown::get(CommonBaseV), ReplacedTy, TLI)))
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001065 // We want the common base emitted into the preheader! This is just
1066 // using cast as a copy so BitCast (no-op cast) is appropriate
1067 CommonBaseV = new BitCastInst(CommonBaseV, CommonBaseV->getType(),
1068 "commonbase", PreInsertPt);
Evan Cheng3df447d2006-03-16 21:53:05 +00001069 }
Chris Lattnera091ff12005-08-09 00:18:09 +00001070
Chris Lattner3ff62012006-08-03 06:34:50 +00001071 // We want to emit code for users inside the loop first. To do this, we
1072 // rearrange BasedUser so that the entries at the end have
1073 // isUseOfPostIncrementedValue = false, because we pop off the end of the
1074 // vector (so we handle them first).
1075 std::partition(UsersToProcess.begin(), UsersToProcess.end(),
1076 PartitionByIsUseOfPostIncrementedValue);
1077
1078 // Sort this by base, so that things with the same base are handled
1079 // together. By partitioning first and stable-sorting later, we are
1080 // guaranteed that within each base we will pop off users from within the
1081 // loop before users outside of the loop with a particular base.
1082 //
1083 // We would like to use stable_sort here, but we can't. The problem is that
1084 // SCEVHandle's don't have a deterministic ordering w.r.t to each other, so
1085 // we don't have anything to do a '<' comparison on. Because we think the
1086 // number of uses is small, do a horrible bubble sort which just relies on
1087 // ==.
1088 for (unsigned i = 0, e = UsersToProcess.size(); i != e; ++i) {
1089 // Get a base value.
1090 SCEVHandle Base = UsersToProcess[i].Base;
1091
1092 // Compact everything with this base to be consequetive with this one.
1093 for (unsigned j = i+1; j != e; ++j) {
1094 if (UsersToProcess[j].Base == Base) {
1095 std::swap(UsersToProcess[i+1], UsersToProcess[j]);
1096 ++i;
1097 }
1098 }
1099 }
1100
1101 // Process all the users now. This outer loop handles all bases, the inner
1102 // loop handles all users of a particular base.
Nate Begemane68bcd12005-07-30 00:15:07 +00001103 while (!UsersToProcess.empty()) {
Chris Lattner5c9d63d2005-10-11 18:30:57 +00001104 SCEVHandle Base = UsersToProcess.back().Base;
Chris Lattnerbb78c972005-08-03 23:30:08 +00001105
Bill Wendling5dbf43c2006-11-26 09:46:52 +00001106 DOUT << " INSERTING code for BASE = " << *Base << ":\n";
Chris Lattnerbb78c972005-08-03 23:30:08 +00001107
Chris Lattnera091ff12005-08-09 00:18:09 +00001108 // Emit the code for Base into the preheader.
Chris Lattnerc70bbc02005-08-08 05:47:49 +00001109 Value *BaseV = PreheaderRewriter.expandCodeFor(Base, PreInsertPt,
1110 ReplacedTy);
Chris Lattnera091ff12005-08-09 00:18:09 +00001111
1112 // If BaseV is a constant other than 0, make sure that it gets inserted into
1113 // the preheader, instead of being forward substituted into the uses. We do
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001114 // this by forcing a BitCast (noop cast) to be inserted into the preheader
1115 // in this case.
Chris Lattner3ff62012006-08-03 06:34:50 +00001116 if (Constant *C = dyn_cast<Constant>(BaseV)) {
Evan Chengb5eb9322007-03-13 20:34:37 +00001117 if (!C->isNullValue() && !isTargetConstant(Base, ReplacedTy, TLI)) {
Reid Spencer6c38f0b2006-11-27 01:05:10 +00001118 // We want this constant emitted into the preheader! This is just
1119 // using cast as a copy so BitCast (no-op cast) is appropriate
1120 BaseV = new BitCastInst(BaseV, BaseV->getType(), "preheaderinsert",
Chris Lattnera091ff12005-08-09 00:18:09 +00001121 PreInsertPt);
1122 }
Chris Lattner3ff62012006-08-03 06:34:50 +00001123 }
1124
Nate Begemane68bcd12005-07-30 00:15:07 +00001125 // Emit the code to add the immediate offset to the Phi value, just before
Chris Lattnerdb23c742005-08-03 22:51:21 +00001126 // the instructions that we identified as using this stride and base.
Chris Lattner5c9d63d2005-10-11 18:30:57 +00001127 do {
Chris Lattner3ff62012006-08-03 06:34:50 +00001128 // FIXME: Use emitted users to emit other users.
Chris Lattner5c9d63d2005-10-11 18:30:57 +00001129 BasedUser &User = UsersToProcess.back();
Jeff Cohen546fd592005-07-30 18:33:25 +00001130
Chris Lattnera091ff12005-08-09 00:18:09 +00001131 // If this instruction wants to use the post-incremented value, move it
1132 // after the post-inc and use its value instead of the PHI.
1133 Value *RewriteOp = NewPHI;
1134 if (User.isUseOfPostIncrementedValue) {
1135 RewriteOp = IncV;
Chris Lattnera6764832005-09-12 06:04:47 +00001136
1137 // If this user is in the loop, make sure it is the last thing in the
1138 // loop to ensure it is dominated by the increment.
1139 if (L->contains(User.Inst->getParent()))
1140 User.Inst->moveBefore(LatchBlock->getTerminator());
Chris Lattnera091ff12005-08-09 00:18:09 +00001141 }
Reid Spencerdf1f19a2006-12-13 08:06:42 +00001142 if (RewriteOp->getType() != ReplacedTy) {
1143 Instruction::CastOps opcode = Instruction::Trunc;
1144 if (ReplacedTy->getPrimitiveSizeInBits() ==
1145 RewriteOp->getType()->getPrimitiveSizeInBits())
1146 opcode = Instruction::BitCast;
1147 RewriteOp = SCEVExpander::InsertCastOfTo(opcode, RewriteOp, ReplacedTy);
1148 }
Evan Cheng398f7022006-06-09 00:12:42 +00001149
Chris Lattnera091ff12005-08-09 00:18:09 +00001150 SCEVHandle RewriteExpr = SCEVUnknown::get(RewriteOp);
1151
Chris Lattnerdb23c742005-08-03 22:51:21 +00001152 // Clear the SCEVExpander's expression map so that we are guaranteed
1153 // to have the code emitted where we expect it.
1154 Rewriter.clear();
Evan Cheng3df447d2006-03-16 21:53:05 +00001155
1156 // If we are reusing the iv, then it must be multiplied by a constant
1157 // factor take advantage of addressing mode scale component.
Evan Chengc28282b2006-03-18 08:03:12 +00001158 if (RewriteFactor != 0) {
Evan Cheng3df447d2006-03-16 21:53:05 +00001159 RewriteExpr =
1160 SCEVMulExpr::get(SCEVUnknown::getIntegerSCEV(RewriteFactor,
Evan Cheng45206982006-03-17 19:52:23 +00001161 RewriteExpr->getType()),
1162 RewriteExpr);
1163
1164 // The common base is emitted in the loop preheader. But since we
1165 // are reusing an IV, it has not been used to initialize the PHI node.
1166 // Add it to the expression used to rewrite the uses.
1167 if (!isa<ConstantInt>(CommonBaseV) ||
Reid Spencer53a37392007-03-02 23:51:25 +00001168 !cast<ConstantInt>(CommonBaseV)->isZero())
Evan Cheng45206982006-03-17 19:52:23 +00001169 RewriteExpr = SCEVAddExpr::get(RewriteExpr,
1170 SCEVUnknown::get(CommonBaseV));
1171 }
Evan Cheng3df447d2006-03-16 21:53:05 +00001172
Chris Lattnera6d7c352005-08-04 20:03:32 +00001173 // Now that we know what we need to do, insert code before User for the
1174 // immediate and any loop-variant expressions.
Reid Spencer53a37392007-03-02 23:51:25 +00001175 if (!isa<ConstantInt>(BaseV) || !cast<ConstantInt>(BaseV)->isZero())
Chris Lattnera091ff12005-08-09 00:18:09 +00001176 // Add BaseV to the PHI value if needed.
1177 RewriteExpr = SCEVAddExpr::get(RewriteExpr, SCEVUnknown::get(BaseV));
Evan Cheng3df447d2006-03-16 21:53:05 +00001178
Chris Lattner8447b492005-08-12 22:22:17 +00001179 User.RewriteInstructionToUseNewBase(RewriteExpr, Rewriter, L, this);
Jeff Cohen546fd592005-07-30 18:33:25 +00001180
Chris Lattnerdb23c742005-08-03 22:51:21 +00001181 // Mark old value we replaced as possibly dead, so that it is elminated
1182 // if we just replaced the last use of that value.
Chris Lattnera6d7c352005-08-04 20:03:32 +00001183 DeadInsts.insert(cast<Instruction>(User.OperandValToReplace));
Nate Begemane68bcd12005-07-30 00:15:07 +00001184
Chris Lattner5c9d63d2005-10-11 18:30:57 +00001185 UsersToProcess.pop_back();
Chris Lattnerdb23c742005-08-03 22:51:21 +00001186 ++NumReduced;
Chris Lattner5c9d63d2005-10-11 18:30:57 +00001187
Chris Lattner3ff62012006-08-03 06:34:50 +00001188 // If there are any more users to process with the same base, process them
1189 // now. We sorted by base above, so we just have to check the last elt.
Chris Lattner5c9d63d2005-10-11 18:30:57 +00001190 } while (!UsersToProcess.empty() && UsersToProcess.back().Base == Base);
Nate Begemane68bcd12005-07-30 00:15:07 +00001191 // TODO: Next, find out which base index is the most common, pull it out.
1192 }
1193
1194 // IMPORTANT TODO: Figure out how to partition the IV's with this stride, but
1195 // different starting values, into different PHIs.
Nate Begemane68bcd12005-07-30 00:15:07 +00001196}
1197
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001198// OptimizeIndvars - Now that IVUsesByStride is set up with all of the indvar
1199// uses in the loop, look to see if we can eliminate some, in favor of using
1200// common indvars for the different uses.
1201void LoopStrengthReduce::OptimizeIndvars(Loop *L) {
1202 // TODO: implement optzns here.
1203
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001204 // Finally, get the terminating condition for the loop if possible. If we
1205 // can, we want to change it to use a post-incremented version of its
Chris Lattnerf365f5f2006-03-24 07:14:34 +00001206 // induction variable, to allow coalescing the live ranges for the IV into
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001207 // one register value.
1208 PHINode *SomePHI = cast<PHINode>(L->getHeader()->begin());
1209 BasicBlock *Preheader = L->getLoopPreheader();
1210 BasicBlock *LatchBlock =
1211 SomePHI->getIncomingBlock(SomePHI->getIncomingBlock(0) == Preheader);
1212 BranchInst *TermBr = dyn_cast<BranchInst>(LatchBlock->getTerminator());
Reid Spencer266e42b2006-12-23 06:05:41 +00001213 if (!TermBr || TermBr->isUnconditional() ||
1214 !isa<ICmpInst>(TermBr->getCondition()))
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001215 return;
Reid Spencer266e42b2006-12-23 06:05:41 +00001216 ICmpInst *Cond = cast<ICmpInst>(TermBr->getCondition());
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001217
1218 // Search IVUsesByStride to find Cond's IVUse if there is one.
1219 IVStrideUse *CondUse = 0;
Chris Lattneredff91a2005-08-10 00:45:21 +00001220 const SCEVHandle *CondStride = 0;
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001221
Chris Lattnerb7a38942005-10-11 18:17:57 +00001222 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e && !CondUse;
1223 ++Stride) {
1224 std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
1225 IVUsesByStride.find(StrideOrder[Stride]);
1226 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
1227
1228 for (std::vector<IVStrideUse>::iterator UI = SI->second.Users.begin(),
1229 E = SI->second.Users.end(); UI != E; ++UI)
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001230 if (UI->User == Cond) {
1231 CondUse = &*UI;
Chris Lattnerb7a38942005-10-11 18:17:57 +00001232 CondStride = &SI->first;
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001233 // NOTE: we could handle setcc instructions with multiple uses here, but
1234 // InstCombine does it as well for simple uses, it's not clear that it
1235 // occurs enough in real life to handle.
1236 break;
1237 }
Chris Lattnerb7a38942005-10-11 18:17:57 +00001238 }
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001239 if (!CondUse) return; // setcc doesn't use the IV.
1240
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001241 // It's possible for the setcc instruction to be anywhere in the loop, and
1242 // possible for it to have multiple users. If it is not immediately before
1243 // the latch block branch, move it.
1244 if (&*++BasicBlock::iterator(Cond) != (Instruction*)TermBr) {
1245 if (Cond->hasOneUse()) { // Condition has a single use, just move it.
1246 Cond->moveBefore(TermBr);
1247 } else {
1248 // Otherwise, clone the terminating condition and insert into the loopend.
Reid Spencer266e42b2006-12-23 06:05:41 +00001249 Cond = cast<ICmpInst>(Cond->clone());
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001250 Cond->setName(L->getHeader()->getName() + ".termcond");
1251 LatchBlock->getInstList().insert(TermBr, Cond);
1252
1253 // Clone the IVUse, as the old use still exists!
Chris Lattneredff91a2005-08-10 00:45:21 +00001254 IVUsesByStride[*CondStride].addUser(CondUse->Offset, Cond,
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001255 CondUse->OperandValToReplace);
Chris Lattneredff91a2005-08-10 00:45:21 +00001256 CondUse = &IVUsesByStride[*CondStride].Users.back();
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001257 }
1258 }
1259
1260 // If we get to here, we know that we can transform the setcc instruction to
Chris Lattnerf365f5f2006-03-24 07:14:34 +00001261 // use the post-incremented version of the IV, allowing us to coalesce the
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001262 // live ranges for the IV correctly.
Chris Lattneredff91a2005-08-10 00:45:21 +00001263 CondUse->Offset = SCEV::getMinusSCEV(CondUse->Offset, *CondStride);
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001264 CondUse->isUseOfPostIncrementedValue = true;
1265}
Nate Begemane68bcd12005-07-30 00:15:07 +00001266
Evan Chengf09f0eb2006-03-18 00:44:49 +00001267namespace {
1268 // Constant strides come first which in turns are sorted by their absolute
1269 // values. If absolute values are the same, then positive strides comes first.
1270 // e.g.
1271 // 4, -1, X, 1, 2 ==> 1, -1, 2, 4, X
1272 struct StrideCompare {
1273 bool operator()(const SCEVHandle &LHS, const SCEVHandle &RHS) {
1274 SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS);
1275 SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS);
1276 if (LHSC && RHSC) {
Reid Spencer197adfa2007-03-02 00:31:39 +00001277 int64_t LV = LHSC->getValue()->getSExtValue();
1278 int64_t RV = RHSC->getValue()->getSExtValue();
1279 uint64_t ALV = (LV < 0) ? -LV : LV;
1280 uint64_t ARV = (RV < 0) ? -RV : RV;
Evan Chengf09f0eb2006-03-18 00:44:49 +00001281 if (ALV == ARV)
Reid Spencer197adfa2007-03-02 00:31:39 +00001282 return LV > RV;
Evan Chengf09f0eb2006-03-18 00:44:49 +00001283 else
Reid Spencer197adfa2007-03-02 00:31:39 +00001284 return ALV < ARV;
Chris Lattner7d80b4f2006-03-22 17:27:24 +00001285 }
1286 return (LHSC && !RHSC);
Evan Chengf09f0eb2006-03-18 00:44:49 +00001287 }
1288 };
1289}
1290
Devang Patelb0743b52007-03-06 21:14:09 +00001291bool LoopStrengthReduce::runOnLoop(Loop *L, LPPassManager &LPM) {
Nate Begemanb18121e2004-10-18 21:08:22 +00001292
Devang Patelb0743b52007-03-06 21:14:09 +00001293 LI = &getAnalysis<LoopInfo>();
1294 EF = &getAnalysis<ETForest>();
1295 SE = &getAnalysis<ScalarEvolution>();
1296 TD = &getAnalysis<TargetData>();
1297 UIntPtrTy = TD->getIntPtrType();
1298
1299 // Find all uses of induction variables in this loop, and catagorize
Nate Begemane68bcd12005-07-30 00:15:07 +00001300 // them by stride. Start by finding all of the PHI nodes in the header for
1301 // this loop. If they are induction variables, inspect their uses.
Chris Lattnereaf24722005-08-04 17:40:30 +00001302 std::set<Instruction*> Processed; // Don't reprocess instructions.
Nate Begemane68bcd12005-07-30 00:15:07 +00001303 for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I)
Chris Lattnereaf24722005-08-04 17:40:30 +00001304 AddUsersIfInteresting(I, L, Processed);
Nate Begemanb18121e2004-10-18 21:08:22 +00001305
Nate Begemane68bcd12005-07-30 00:15:07 +00001306 // If we have nothing to do, return.
Devang Patelb0743b52007-03-06 21:14:09 +00001307 if (IVUsesByStride.empty()) return false;
Chris Lattner9bfa6f82005-08-08 05:28:22 +00001308
1309 // Optimize induction variables. Some indvar uses can be transformed to use
1310 // strides that will be needed for other purposes. A common example of this
1311 // is the exit test for the loop, which can often be rewritten to use the
1312 // computation of some other indvar to decide when to terminate the loop.
1313 OptimizeIndvars(L);
1314
Misha Brukmanb1c93172005-04-21 23:48:37 +00001315
Nate Begemane68bcd12005-07-30 00:15:07 +00001316 // FIXME: We can widen subreg IV's here for RISC targets. e.g. instead of
1317 // doing computation in byte values, promote to 32-bit values if safe.
1318
1319 // FIXME: Attempt to reuse values across multiple IV's. In particular, we
1320 // could have something like "for(i) { foo(i*8); bar(i*16) }", which should be
1321 // codegened as "for (j = 0;; j+=8) { foo(j); bar(j+j); }" on X86/PPC. Need
1322 // to be careful that IV's are all the same type. Only works for intptr_t
1323 // indvars.
1324
1325 // If we only have one stride, we can more aggressively eliminate some things.
1326 bool HasOneStride = IVUsesByStride.size() == 1;
Evan Cheng3df447d2006-03-16 21:53:05 +00001327
1328#ifndef NDEBUG
Bill Wendling5dbf43c2006-11-26 09:46:52 +00001329 DOUT << "\nLSR on ";
Evan Cheng3df447d2006-03-16 21:53:05 +00001330 DEBUG(L->dump());
1331#endif
1332
1333 // IVsByStride keeps IVs for one particular loop.
1334 IVsByStride.clear();
1335
Evan Chengf09f0eb2006-03-18 00:44:49 +00001336 // Sort the StrideOrder so we process larger strides first.
1337 std::stable_sort(StrideOrder.begin(), StrideOrder.end(), StrideCompare());
1338
Chris Lattnera091ff12005-08-09 00:18:09 +00001339 // Note: this processes each stride/type pair individually. All users passed
Chris Lattner4ea0a3e2005-10-09 06:20:55 +00001340 // into StrengthReduceStridedIVUsers have the same type AND stride. Also,
1341 // node that we iterate over IVUsesByStride indirectly by using StrideOrder.
1342 // This extra layer of indirection makes the ordering of strides deterministic
1343 // - not dependent on map order.
1344 for (unsigned Stride = 0, e = StrideOrder.size(); Stride != e; ++Stride) {
1345 std::map<SCEVHandle, IVUsersOfOneStride>::iterator SI =
1346 IVUsesByStride.find(StrideOrder[Stride]);
1347 assert(SI != IVUsesByStride.end() && "Stride doesn't exist!");
Nate Begemane68bcd12005-07-30 00:15:07 +00001348 StrengthReduceStridedIVUsers(SI->first, SI->second, L, HasOneStride);
Chris Lattner4ea0a3e2005-10-09 06:20:55 +00001349 }
Nate Begemanb18121e2004-10-18 21:08:22 +00001350
1351 // Clean up after ourselves
1352 if (!DeadInsts.empty()) {
1353 DeleteTriviallyDeadInstructions(DeadInsts);
1354
Nate Begemane68bcd12005-07-30 00:15:07 +00001355 BasicBlock::iterator I = L->getHeader()->begin();
1356 PHINode *PN;
Chris Lattnerdcce49e2005-08-02 02:44:31 +00001357 while ((PN = dyn_cast<PHINode>(I))) {
Chris Lattner564900e2005-08-02 00:41:11 +00001358 ++I; // Preincrement iterator to avoid invalidating it when deleting PN.
1359
Chris Lattnerc6c4d992005-08-09 23:39:36 +00001360 // At this point, we know that we have killed one or more GEP
1361 // instructions. It is worth checking to see if the cann indvar is also
1362 // dead, so that we can remove it as well. The requirements for the cann
1363 // indvar to be considered dead are:
Nate Begemane68bcd12005-07-30 00:15:07 +00001364 // 1. the cann indvar has one use
1365 // 2. the use is an add instruction
1366 // 3. the add has one use
1367 // 4. the add is used by the cann indvar
1368 // If all four cases above are true, then we can remove both the add and
1369 // the cann indvar.
1370 // FIXME: this needs to eliminate an induction variable even if it's being
1371 // compared against some value to decide loop termination.
1372 if (PN->hasOneUse()) {
Reid Spencer266e42b2006-12-23 06:05:41 +00001373 Instruction *BO = dyn_cast<Instruction>(*PN->use_begin());
1374 if (BO && (isa<BinaryOperator>(BO) || isa<CmpInst>(BO))) {
1375 if (BO->hasOneUse() && PN == *(BO->use_begin())) {
Chris Lattner75a44e12005-08-02 02:52:02 +00001376 DeadInsts.insert(BO);
1377 // Break the cycle, then delete the PHI.
1378 PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
Chris Lattner84e9baa2005-08-03 21:36:09 +00001379 SE->deleteInstructionFromRecords(PN);
Chris Lattner75a44e12005-08-02 02:52:02 +00001380 PN->eraseFromParent();
Nate Begemanb18121e2004-10-18 21:08:22 +00001381 }
Chris Lattner75a44e12005-08-02 02:52:02 +00001382 }
Nate Begemane68bcd12005-07-30 00:15:07 +00001383 }
Nate Begemanb18121e2004-10-18 21:08:22 +00001384 }
Nate Begemane68bcd12005-07-30 00:15:07 +00001385 DeleteTriviallyDeadInstructions(DeadInsts);
Nate Begemanb18121e2004-10-18 21:08:22 +00001386 }
Nate Begemane68bcd12005-07-30 00:15:07 +00001387
Chris Lattner11e7a5e2005-08-05 01:30:11 +00001388 CastedPointers.clear();
Nate Begemane68bcd12005-07-30 00:15:07 +00001389 IVUsesByStride.clear();
Chris Lattner4ea0a3e2005-10-09 06:20:55 +00001390 StrideOrder.clear();
Devang Patelb0743b52007-03-06 21:14:09 +00001391 return false;
Nate Begemanb18121e2004-10-18 21:08:22 +00001392}