blob: 4cc5ebc29534696ce986fa2738120352e50cb06e [file] [log] [blame]
Nate Begeman36f891b2005-07-30 00:12:19 +00001//===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Nate Begeman36f891b2005-07-30 00:12:19 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the implementation of the scalar evolution expander,
11// which is used to generate the code corresponding to a given scalar evolution
12// expression.
13//
14//===----------------------------------------------------------------------===//
15
Nate Begeman36f891b2005-07-30 00:12:19 +000016#include "llvm/Analysis/ScalarEvolutionExpander.h"
Bill Wendlinge8156192006-12-07 01:30:32 +000017#include "llvm/Analysis/LoopInfo.h"
Dan Gohman5be18e82009-05-19 02:15:55 +000018#include "llvm/Target/TargetData.h"
Dan Gohman4d8414f2009-06-13 16:25:49 +000019#include "llvm/ADT/STLExtras.h"
Nate Begeman36f891b2005-07-30 00:12:19 +000020using namespace llvm;
21
Chris Lattnerca1a4be2006-02-04 09:51:53 +000022/// InsertCastOfTo - Insert a cast of V to the specified type, doing what
23/// we can to share the casts.
Reid Spencer3ba68b92006-12-13 08:06:42 +000024Value *SCEVExpander::InsertCastOfTo(Instruction::CastOps opcode, Value *V,
25 const Type *Ty) {
Dan Gohman2d1be872009-04-16 03:18:22 +000026 // Short-circuit unnecessary bitcasts.
27 if (opcode == Instruction::BitCast && V->getType() == Ty)
28 return V;
29
Dan Gohmanf04fa482009-04-16 15:52:57 +000030 // Short-circuit unnecessary inttoptr<->ptrtoint casts.
Dan Gohmanaf79fb52009-04-21 01:07:12 +000031 if ((opcode == Instruction::PtrToInt || opcode == Instruction::IntToPtr) &&
Dan Gohman80dcdee2009-05-01 17:00:00 +000032 SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +000033 if (CastInst *CI = dyn_cast<CastInst>(V))
34 if ((CI->getOpcode() == Instruction::PtrToInt ||
35 CI->getOpcode() == Instruction::IntToPtr) &&
36 SE.getTypeSizeInBits(CI->getType()) ==
37 SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
38 return CI->getOperand(0);
Dan Gohman80dcdee2009-05-01 17:00:00 +000039 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
40 if ((CE->getOpcode() == Instruction::PtrToInt ||
41 CE->getOpcode() == Instruction::IntToPtr) &&
42 SE.getTypeSizeInBits(CE->getType()) ==
43 SE.getTypeSizeInBits(CE->getOperand(0)->getType()))
44 return CE->getOperand(0);
45 }
Dan Gohmanf04fa482009-04-16 15:52:57 +000046
Chris Lattnerca1a4be2006-02-04 09:51:53 +000047 // FIXME: keep track of the cast instruction.
48 if (Constant *C = dyn_cast<Constant>(V))
Reid Spencerd977d862006-12-12 23:36:14 +000049 return ConstantExpr::getCast(opcode, C, Ty);
Chris Lattnerca1a4be2006-02-04 09:51:53 +000050
51 if (Argument *A = dyn_cast<Argument>(V)) {
52 // Check to see if there is already a cast!
53 for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
Dan Gohman40a5a1b2009-06-24 01:18:18 +000054 UI != E; ++UI)
Chris Lattnerca1a4be2006-02-04 09:51:53 +000055 if ((*UI)->getType() == Ty)
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000056 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
57 if (CI->getOpcode() == opcode) {
58 // If the cast isn't the first instruction of the function, move it.
Dan Gohman40a5a1b2009-06-24 01:18:18 +000059 if (BasicBlock::iterator(CI) !=
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000060 A->getParent()->getEntryBlock().begin()) {
Dan Gohman40a5a1b2009-06-24 01:18:18 +000061 // Recreate the cast at the beginning of the entry block.
62 // The old cast is left in place in case it is being used
63 // as an insert point.
64 Instruction *NewCI =
65 CastInst::Create(opcode, V, Ty, "",
66 A->getParent()->getEntryBlock().begin());
67 NewCI->takeName(CI);
68 CI->replaceAllUsesWith(NewCI);
69 return NewCI;
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000070 }
71 return CI;
Chris Lattnerca1a4be2006-02-04 09:51:53 +000072 }
Dan Gohman40a5a1b2009-06-24 01:18:18 +000073
Dan Gohmancf5ab822009-05-01 17:13:31 +000074 Instruction *I = CastInst::Create(opcode, V, Ty, V->getName(),
75 A->getParent()->getEntryBlock().begin());
76 InsertedValues.insert(I);
77 return I;
Chris Lattnerca1a4be2006-02-04 09:51:53 +000078 }
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000079
Chris Lattnerca1a4be2006-02-04 09:51:53 +000080 Instruction *I = cast<Instruction>(V);
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000081
Chris Lattnerca1a4be2006-02-04 09:51:53 +000082 // Check to see if there is already a cast. If there is, use it.
83 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
84 UI != E; ++UI) {
85 if ((*UI)->getType() == Ty)
Wojciech Matyjewicz39131872008-02-09 18:30:13 +000086 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
87 if (CI->getOpcode() == opcode) {
88 BasicBlock::iterator It = I; ++It;
89 if (isa<InvokeInst>(I))
90 It = cast<InvokeInst>(I)->getNormalDest()->begin();
91 while (isa<PHINode>(It)) ++It;
92 if (It != BasicBlock::iterator(CI)) {
Dan Gohman40a5a1b2009-06-24 01:18:18 +000093 // Recreate the cast at the beginning of the entry block.
94 // The old cast is left in place in case it is being used
95 // as an insert point.
96 Instruction *NewCI = CastInst::Create(opcode, V, Ty, "", It);
97 NewCI->takeName(CI);
98 CI->replaceAllUsesWith(NewCI);
99 return NewCI;
Wojciech Matyjewicz39131872008-02-09 18:30:13 +0000100 }
101 return CI;
Chris Lattnerca1a4be2006-02-04 09:51:53 +0000102 }
Chris Lattnerca1a4be2006-02-04 09:51:53 +0000103 }
104 BasicBlock::iterator IP = I; ++IP;
105 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
106 IP = II->getNormalDest()->begin();
107 while (isa<PHINode>(IP)) ++IP;
Dan Gohmancf5ab822009-05-01 17:13:31 +0000108 Instruction *CI = CastInst::Create(opcode, V, Ty, V->getName(), IP);
109 InsertedValues.insert(CI);
110 return CI;
Chris Lattnerca1a4be2006-02-04 09:51:53 +0000111}
112
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000113/// InsertNoopCastOfTo - Insert a cast of V to the specified type,
114/// which must be possible with a noop cast.
115Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) {
116 Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
117 assert((Op == Instruction::BitCast ||
Devang Patele2a17462009-04-22 18:51:05 +0000118 Op == Instruction::PtrToInt ||
119 Op == Instruction::IntToPtr) &&
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000120 "InsertNoopCastOfTo cannot perform non-noop casts!");
121 assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
122 "InsertNoopCastOfTo cannot change sizes!");
123 return InsertCastOfTo(Op, V, Ty);
124}
125
Chris Lattner7fec90e2007-04-13 05:04:18 +0000126/// InsertBinop - Insert the specified binary operator, doing a small amount
127/// of work to avoid inserting an obviously redundant operation.
128Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode, Value *LHS,
Dan Gohman6cdc7272009-04-22 16:05:50 +0000129 Value *RHS, BasicBlock::iterator InsertPt) {
Dan Gohman0f0eb182007-06-15 19:21:55 +0000130 // Fold a binop with constant operands.
131 if (Constant *CLHS = dyn_cast<Constant>(LHS))
132 if (Constant *CRHS = dyn_cast<Constant>(RHS))
133 return ConstantExpr::get(Opcode, CLHS, CRHS);
134
Chris Lattner7fec90e2007-04-13 05:04:18 +0000135 // Do a quick scan to see if we have this binop nearby. If so, reuse it.
136 unsigned ScanLimit = 6;
Wojciech Matyjewicz8a087692008-06-15 19:07:39 +0000137 BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin();
138 if (InsertPt != BlockBegin) {
139 // Scanning starts from the last instruction before InsertPt.
140 BasicBlock::iterator IP = InsertPt;
141 --IP;
142 for (; ScanLimit; --IP, --ScanLimit) {
Dan Gohman5be18e82009-05-19 02:15:55 +0000143 if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS &&
144 IP->getOperand(1) == RHS)
145 return IP;
Wojciech Matyjewicz8a087692008-06-15 19:07:39 +0000146 if (IP == BlockBegin) break;
147 }
Chris Lattner7fec90e2007-04-13 05:04:18 +0000148 }
Wojciech Matyjewicz8a087692008-06-15 19:07:39 +0000149
150 // If we haven't found this binop, insert it.
Dan Gohmancf5ab822009-05-01 17:13:31 +0000151 Instruction *BO = BinaryOperator::Create(Opcode, LHS, RHS, "tmp", InsertPt);
152 InsertedValues.insert(BO);
153 return BO;
Chris Lattner7fec90e2007-04-13 05:04:18 +0000154}
155
Dan Gohman4a4f7672009-05-27 02:00:53 +0000156/// FactorOutConstant - Test if S is divisible by Factor, using signed
Dan Gohman453aa4f2009-05-24 18:06:31 +0000157/// division. If so, update S with Factor divided out and return true.
Dan Gohman4a4f7672009-05-27 02:00:53 +0000158/// S need not be evenly divisble if a reasonable remainder can be
159/// computed.
Dan Gohman453aa4f2009-05-24 18:06:31 +0000160/// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made
161/// unnecessary; in its place, just signed-divide Ops[i] by the scale and
162/// check to see if the divide was folded.
Owen Anderson372b46c2009-06-22 21:39:50 +0000163static bool FactorOutConstant(const SCEV* &S,
164 const SCEV* &Remainder,
Dan Gohman453aa4f2009-05-24 18:06:31 +0000165 const APInt &Factor,
166 ScalarEvolution &SE) {
167 // Everything is divisible by one.
168 if (Factor == 1)
169 return true;
170
171 // For a Constant, check for a multiple of the given factor.
Dan Gohman4a4f7672009-05-27 02:00:53 +0000172 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
173 ConstantInt *CI =
174 ConstantInt::get(C->getValue()->getValue().sdiv(Factor));
175 // If the quotient is zero and the remainder is non-zero, reject
176 // the value at this scale. It will be considered for subsequent
177 // smaller scales.
178 if (C->isZero() || !CI->isZero()) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000179 const SCEV* Div = SE.getConstant(CI);
Dan Gohman453aa4f2009-05-24 18:06:31 +0000180 S = Div;
Dan Gohman4a4f7672009-05-27 02:00:53 +0000181 Remainder =
182 SE.getAddExpr(Remainder,
183 SE.getConstant(C->getValue()->getValue().srem(Factor)));
Dan Gohman453aa4f2009-05-24 18:06:31 +0000184 return true;
185 }
Dan Gohman4a4f7672009-05-27 02:00:53 +0000186 }
Dan Gohman453aa4f2009-05-24 18:06:31 +0000187
188 // In a Mul, check if there is a constant operand which is a multiple
189 // of the given factor.
190 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S))
191 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0)))
192 if (!C->getValue()->getValue().srem(Factor)) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000193 const SmallVectorImpl<const SCEV*> &MOperands = M->getOperands();
194 SmallVector<const SCEV*, 4> NewMulOps(MOperands.begin(), MOperands.end());
Dan Gohman453aa4f2009-05-24 18:06:31 +0000195 NewMulOps[0] =
196 SE.getConstant(C->getValue()->getValue().sdiv(Factor));
197 S = SE.getMulExpr(NewMulOps);
198 return true;
199 }
200
201 // In an AddRec, check if both start and step are divisible.
202 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000203 const SCEV* Step = A->getStepRecurrence(SE);
204 const SCEV* StepRem = SE.getIntegerSCEV(0, Step->getType());
Dan Gohman4a4f7672009-05-27 02:00:53 +0000205 if (!FactorOutConstant(Step, StepRem, Factor, SE))
206 return false;
207 if (!StepRem->isZero())
208 return false;
Owen Anderson372b46c2009-06-22 21:39:50 +0000209 const SCEV* Start = A->getStart();
Dan Gohman4a4f7672009-05-27 02:00:53 +0000210 if (!FactorOutConstant(Start, Remainder, Factor, SE))
Dan Gohman453aa4f2009-05-24 18:06:31 +0000211 return false;
212 S = SE.getAddRecExpr(Start, Step, A->getLoop());
213 return true;
214 }
215
216 return false;
217}
218
Dan Gohman5be18e82009-05-19 02:15:55 +0000219/// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
Dan Gohman453aa4f2009-05-24 18:06:31 +0000220/// instead of using ptrtoint+arithmetic+inttoptr. This helps
221/// BasicAliasAnalysis analyze the result. However, it suffers from the
222/// underlying bug described in PR2831. Addition in LLVM currently always
223/// has two's complement wrapping guaranteed. However, the semantics for
224/// getelementptr overflow are ambiguous. In the common case though, this
225/// expansion gets used when a GEP in the original code has been converted
226/// into integer arithmetic, in which case the resulting code will be no
227/// more undefined than it was originally.
228///
229/// Design note: It might seem desirable for this function to be more
230/// loop-aware. If some of the indices are loop-invariant while others
231/// aren't, it might seem desirable to emit multiple GEPs, keeping the
232/// loop-invariant portions of the overall computation outside the loop.
233/// However, there are a few reasons this is not done here. Hoisting simple
234/// arithmetic is a low-level optimization that often isn't very
235/// important until late in the optimization process. In fact, passes
236/// like InstructionCombining will combine GEPs, even if it means
237/// pushing loop-invariant computation down into loops, so even if the
238/// GEPs were split here, the work would quickly be undone. The
239/// LoopStrengthReduction pass, which is usually run quite late (and
240/// after the last InstructionCombining pass), takes care of hoisting
241/// loop-invariant portions of expressions, after considering what
242/// can be folded using target addressing modes.
243///
Owen Anderson372b46c2009-06-22 21:39:50 +0000244Value *SCEVExpander::expandAddToGEP(const SCEV* const *op_begin,
245 const SCEV* const *op_end,
Dan Gohman5be18e82009-05-19 02:15:55 +0000246 const PointerType *PTy,
247 const Type *Ty,
248 Value *V) {
249 const Type *ElTy = PTy->getElementType();
250 SmallVector<Value *, 4> GepIndices;
Owen Anderson372b46c2009-06-22 21:39:50 +0000251 SmallVector<const SCEV*, 8> Ops(op_begin, op_end);
Dan Gohman5be18e82009-05-19 02:15:55 +0000252 bool AnyNonZeroIndices = false;
Dan Gohman5be18e82009-05-19 02:15:55 +0000253
254 // Decend down the pointer's type and attempt to convert the other
255 // operands into GEP indices, at each level. The first index in a GEP
256 // indexes into the array implied by the pointer operand; the rest of
257 // the indices index into the element or field type selected by the
258 // preceding index.
259 for (;;) {
260 APInt ElSize = APInt(SE.getTypeSizeInBits(Ty),
261 ElTy->isSized() ? SE.TD->getTypeAllocSize(ElTy) : 0);
Owen Anderson372b46c2009-06-22 21:39:50 +0000262 SmallVector<const SCEV*, 8> NewOps;
263 SmallVector<const SCEV*, 8> ScaledOps;
Dan Gohman5be18e82009-05-19 02:15:55 +0000264 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
Dan Gohman453aa4f2009-05-24 18:06:31 +0000265 // Split AddRecs up into parts as either of the parts may be usable
266 // without the other.
267 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i]))
268 if (!A->getStart()->isZero()) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000269 const SCEV* Start = A->getStart();
Dan Gohman453aa4f2009-05-24 18:06:31 +0000270 Ops.push_back(SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
271 A->getStepRecurrence(SE),
272 A->getLoop()));
273 Ops[i] = Start;
274 ++e;
275 }
276 // If the scale size is not 0, attempt to factor out a scale.
Dan Gohman5be18e82009-05-19 02:15:55 +0000277 if (ElSize != 0) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000278 const SCEV* Op = Ops[i];
279 const SCEV* Remainder = SE.getIntegerSCEV(0, Op->getType());
Dan Gohman4a4f7672009-05-27 02:00:53 +0000280 if (FactorOutConstant(Op, Remainder, ElSize, SE)) {
Dan Gohman453aa4f2009-05-24 18:06:31 +0000281 ScaledOps.push_back(Op); // Op now has ElSize factored out.
Dan Gohman4a4f7672009-05-27 02:00:53 +0000282 NewOps.push_back(Remainder);
Dan Gohman5be18e82009-05-19 02:15:55 +0000283 continue;
284 }
285 }
Dan Gohman453aa4f2009-05-24 18:06:31 +0000286 // If the operand was not divisible, add it to the list of operands
287 // we'll scan next iteration.
Dan Gohman5be18e82009-05-19 02:15:55 +0000288 NewOps.push_back(Ops[i]);
289 }
290 Ops = NewOps;
291 AnyNonZeroIndices |= !ScaledOps.empty();
292 Value *Scaled = ScaledOps.empty() ?
293 Constant::getNullValue(Ty) :
294 expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
295 GepIndices.push_back(Scaled);
296
297 // Collect struct field index operands.
298 if (!Ops.empty())
299 while (const StructType *STy = dyn_cast<StructType>(ElTy)) {
300 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0]))
301 if (SE.getTypeSizeInBits(C->getType()) <= 64) {
302 const StructLayout &SL = *SE.TD->getStructLayout(STy);
303 uint64_t FullOffset = C->getValue()->getZExtValue();
304 if (FullOffset < SL.getSizeInBytes()) {
305 unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
306 GepIndices.push_back(ConstantInt::get(Type::Int32Ty, ElIdx));
307 ElTy = STy->getTypeAtIndex(ElIdx);
308 Ops[0] =
Dan Gohman6de29f82009-06-15 22:12:54 +0000309 SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
Dan Gohman5be18e82009-05-19 02:15:55 +0000310 AnyNonZeroIndices = true;
311 continue;
312 }
313 }
314 break;
315 }
316
317 if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) {
318 ElTy = ATy->getElementType();
319 continue;
320 }
321 break;
322 }
323
324 // If none of the operands were convertable to proper GEP indices, cast
325 // the base to i8* and do an ugly getelementptr with that. It's still
326 // better than ptrtoint+arithmetic+inttoptr at least.
327 if (!AnyNonZeroIndices) {
328 V = InsertNoopCastOfTo(V,
329 Type::Int8Ty->getPointerTo(PTy->getAddressSpace()));
Dan Gohman92fcdca2009-06-09 17:18:38 +0000330 Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
Dan Gohman5be18e82009-05-19 02:15:55 +0000331
332 // Fold a GEP with constant operands.
333 if (Constant *CLHS = dyn_cast<Constant>(V))
334 if (Constant *CRHS = dyn_cast<Constant>(Idx))
Dan Gohman278b49a2009-05-19 19:18:01 +0000335 return ConstantExpr::getGetElementPtr(CLHS, &CRHS, 1);
Dan Gohman5be18e82009-05-19 02:15:55 +0000336
337 // Do a quick scan to see if we have this GEP nearby. If so, reuse it.
338 unsigned ScanLimit = 6;
339 BasicBlock::iterator BlockBegin = InsertPt->getParent()->begin();
340 if (InsertPt != BlockBegin) {
341 // Scanning starts from the last instruction before InsertPt.
342 BasicBlock::iterator IP = InsertPt;
343 --IP;
344 for (; ScanLimit; --IP, --ScanLimit) {
345 if (IP->getOpcode() == Instruction::GetElementPtr &&
346 IP->getOperand(0) == V && IP->getOperand(1) == Idx)
347 return IP;
348 if (IP == BlockBegin) break;
349 }
350 }
351
352 Value *GEP = GetElementPtrInst::Create(V, Idx, "scevgep", InsertPt);
353 InsertedValues.insert(GEP);
354 return GEP;
355 }
356
357 // Insert a pretty getelementptr.
358 Value *GEP = GetElementPtrInst::Create(V,
359 GepIndices.begin(),
360 GepIndices.end(),
361 "scevgep", InsertPt);
362 Ops.push_back(SE.getUnknown(GEP));
363 InsertedValues.insert(GEP);
364 return expand(SE.getAddExpr(Ops));
365}
366
Dan Gohman890f92b2009-04-18 17:56:28 +0000367Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000368 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohmane24fa642008-06-18 16:37:11 +0000369 Value *V = expand(S->getOperand(S->getNumOperands()-1));
Dan Gohman5be18e82009-05-19 02:15:55 +0000370
Dan Gohman453aa4f2009-05-24 18:06:31 +0000371 // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
372 // comments on expandAddToGEP for details.
Dan Gohman5be18e82009-05-19 02:15:55 +0000373 if (SE.TD)
Dan Gohman453aa4f2009-05-24 18:06:31 +0000374 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000375 const SmallVectorImpl<const SCEV*> &Ops = S->getOperands();
Dan Gohmanfb5a3412009-05-24 19:02:45 +0000376 return expandAddToGEP(&Ops[0], &Ops[Ops.size() - 1],
Dan Gohman453aa4f2009-05-24 18:06:31 +0000377 PTy, Ty, V);
378 }
Dan Gohman5be18e82009-05-19 02:15:55 +0000379
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000380 V = InsertNoopCastOfTo(V, Ty);
Dan Gohmane24fa642008-06-18 16:37:11 +0000381
382 // Emit a bunch of add instructions
Dan Gohman2d1be872009-04-16 03:18:22 +0000383 for (int i = S->getNumOperands()-2; i >= 0; --i) {
Dan Gohman92fcdca2009-06-09 17:18:38 +0000384 Value *W = expandCodeFor(S->getOperand(i), Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000385 V = InsertBinop(Instruction::Add, V, W, InsertPt);
386 }
Dan Gohmane24fa642008-06-18 16:37:11 +0000387 return V;
388}
Dan Gohman5be18e82009-05-19 02:15:55 +0000389
Dan Gohman890f92b2009-04-18 17:56:28 +0000390Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000391 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Nate Begeman36f891b2005-07-30 00:12:19 +0000392 int FirstOp = 0; // Set if we should emit a subtract.
Dan Gohman890f92b2009-04-18 17:56:28 +0000393 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
Nate Begeman36f891b2005-07-30 00:12:19 +0000394 if (SC->getValue()->isAllOnesValue())
395 FirstOp = 1;
396
397 int i = S->getNumOperands()-2;
Dan Gohman92fcdca2009-06-09 17:18:38 +0000398 Value *V = expandCodeFor(S->getOperand(i+1), Ty);
Nate Begeman36f891b2005-07-30 00:12:19 +0000399
400 // Emit a bunch of multiply instructions
Dan Gohman2d1be872009-04-16 03:18:22 +0000401 for (; i >= FirstOp; --i) {
Dan Gohman92fcdca2009-06-09 17:18:38 +0000402 Value *W = expandCodeFor(S->getOperand(i), Ty);
Dan Gohman2d1be872009-04-16 03:18:22 +0000403 V = InsertBinop(Instruction::Mul, V, W, InsertPt);
404 }
405
Nate Begeman36f891b2005-07-30 00:12:19 +0000406 // -1 * ... ---> 0 - ...
407 if (FirstOp == 1)
Dan Gohman2d1be872009-04-16 03:18:22 +0000408 V = InsertBinop(Instruction::Sub, Constant::getNullValue(Ty), V, InsertPt);
Nate Begeman36f891b2005-07-30 00:12:19 +0000409 return V;
410}
411
Dan Gohman890f92b2009-04-18 17:56:28 +0000412Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000413 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman2d1be872009-04-16 03:18:22 +0000414
Dan Gohman92fcdca2009-06-09 17:18:38 +0000415 Value *LHS = expandCodeFor(S->getLHS(), Ty);
Dan Gohman890f92b2009-04-18 17:56:28 +0000416 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
Nick Lewycky6177fd42008-07-08 05:05:37 +0000417 const APInt &RHS = SC->getValue()->getValue();
418 if (RHS.isPowerOf2())
419 return InsertBinop(Instruction::LShr, LHS,
Dan Gohman2d1be872009-04-16 03:18:22 +0000420 ConstantInt::get(Ty, RHS.logBase2()),
Nick Lewycky6177fd42008-07-08 05:05:37 +0000421 InsertPt);
422 }
423
Dan Gohman92fcdca2009-06-09 17:18:38 +0000424 Value *RHS = expandCodeFor(S->getRHS(), Ty);
Nick Lewycky6177fd42008-07-08 05:05:37 +0000425 return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
426}
427
Dan Gohman453aa4f2009-05-24 18:06:31 +0000428/// Move parts of Base into Rest to leave Base with the minimal
429/// expression that provides a pointer operand suitable for a
430/// GEP expansion.
Owen Anderson372b46c2009-06-22 21:39:50 +0000431static void ExposePointerBase(const SCEV* &Base, const SCEV* &Rest,
Dan Gohman453aa4f2009-05-24 18:06:31 +0000432 ScalarEvolution &SE) {
433 while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) {
434 Base = A->getStart();
435 Rest = SE.getAddExpr(Rest,
436 SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
437 A->getStepRecurrence(SE),
438 A->getLoop()));
439 }
440 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) {
441 Base = A->getOperand(A->getNumOperands()-1);
Owen Anderson372b46c2009-06-22 21:39:50 +0000442 SmallVector<const SCEV*, 8> NewAddOps(A->op_begin(), A->op_end());
Dan Gohman453aa4f2009-05-24 18:06:31 +0000443 NewAddOps.back() = Rest;
444 Rest = SE.getAddExpr(NewAddOps);
445 ExposePointerBase(Base, Rest, SE);
446 }
447}
448
Dan Gohman890f92b2009-04-18 17:56:28 +0000449Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000450 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Nate Begeman36f891b2005-07-30 00:12:19 +0000451 const Loop *L = S->getLoop();
Nate Begeman36f891b2005-07-30 00:12:19 +0000452
Dan Gohman4d8414f2009-06-13 16:25:49 +0000453 // First check for an existing canonical IV in a suitable type.
454 PHINode *CanonicalIV = 0;
455 if (PHINode *PN = L->getCanonicalInductionVariable())
456 if (SE.isSCEVable(PN->getType()) &&
457 isa<IntegerType>(SE.getEffectiveSCEVType(PN->getType())) &&
458 SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty))
459 CanonicalIV = PN;
460
461 // Rewrite an AddRec in terms of the canonical induction variable, if
462 // its type is more narrow.
463 if (CanonicalIV &&
464 SE.getTypeSizeInBits(CanonicalIV->getType()) >
465 SE.getTypeSizeInBits(Ty)) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000466 const SCEV* Start = SE.getAnyExtendExpr(S->getStart(),
Dan Gohman4d8414f2009-06-13 16:25:49 +0000467 CanonicalIV->getType());
Owen Anderson372b46c2009-06-22 21:39:50 +0000468 const SCEV* Step = SE.getAnyExtendExpr(S->getStepRecurrence(SE),
Dan Gohman4d8414f2009-06-13 16:25:49 +0000469 CanonicalIV->getType());
470 Value *V = expand(SE.getAddRecExpr(Start, Step, S->getLoop()));
Dan Gohman667d7872009-06-26 22:53:46 +0000471 BasicBlock::iterator SaveInsertPt = InsertPt;
Dan Gohman4d8414f2009-06-13 16:25:49 +0000472 BasicBlock::iterator NewInsertPt =
473 next(BasicBlock::iterator(cast<Instruction>(V)));
474 while (isa<PHINode>(NewInsertPt)) ++NewInsertPt;
475 V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0,
476 NewInsertPt);
Dan Gohman667d7872009-06-26 22:53:46 +0000477 InsertPt = SaveInsertPt;
Dan Gohman4d8414f2009-06-13 16:25:49 +0000478 return V;
479 }
480
Nate Begeman36f891b2005-07-30 00:12:19 +0000481 // {X,+,F} --> X + {0,+,F}
Dan Gohmancfeb6a42008-06-18 16:23:07 +0000482 if (!S->getStart()->isZero()) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000483 const SmallVectorImpl<const SCEV*> &SOperands = S->getOperands();
484 SmallVector<const SCEV*, 4> NewOps(SOperands.begin(), SOperands.end());
Dan Gohman246b2562007-10-22 18:31:58 +0000485 NewOps[0] = SE.getIntegerSCEV(0, Ty);
Owen Anderson372b46c2009-06-22 21:39:50 +0000486 const SCEV* Rest = SE.getAddRecExpr(NewOps, L);
Dan Gohman453aa4f2009-05-24 18:06:31 +0000487
488 // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
489 // comments on expandAddToGEP for details.
490 if (SE.TD) {
Owen Anderson372b46c2009-06-22 21:39:50 +0000491 const SCEV* Base = S->getStart();
492 const SCEV* RestArray[1] = { Rest };
Dan Gohman453aa4f2009-05-24 18:06:31 +0000493 // Dig into the expression to find the pointer base for a GEP.
494 ExposePointerBase(Base, RestArray[0], SE);
495 // If we found a pointer, expand the AddRec with a GEP.
496 if (const PointerType *PTy = dyn_cast<PointerType>(Base->getType())) {
Dan Gohmanf876ad02009-05-26 17:41:16 +0000497 // Make sure the Base isn't something exotic, such as a multiplied
498 // or divided pointer value. In those cases, the result type isn't
499 // actually a pointer type.
500 if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) {
501 Value *StartV = expand(Base);
502 assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
503 return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
504 }
Dan Gohman453aa4f2009-05-24 18:06:31 +0000505 }
506 }
507
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000508 // Just do a normal add. Pre-expand the operands to suppress folding.
509 return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())),
510 SE.getUnknown(expand(Rest))));
Nate Begeman36f891b2005-07-30 00:12:19 +0000511 }
512
513 // {0,+,1} --> Insert a canonical induction variable into the loop!
Dan Gohman17f19722008-06-22 19:23:09 +0000514 if (S->isAffine() &&
Dan Gohman246b2562007-10-22 18:31:58 +0000515 S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) {
Dan Gohman4d8414f2009-06-13 16:25:49 +0000516 // If there's a canonical IV, just use it.
517 if (CanonicalIV) {
518 assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
519 "IVs with types different from the canonical IV should "
520 "already have been handled!");
521 return CanonicalIV;
522 }
523
Nate Begeman36f891b2005-07-30 00:12:19 +0000524 // Create and insert the PHI node for the induction variable in the
525 // specified loop.
526 BasicBlock *Header = L->getHeader();
Gabor Greif051a9502008-04-06 20:25:17 +0000527 PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
Dan Gohmancf5ab822009-05-01 17:13:31 +0000528 InsertedValues.insert(PN);
Nate Begeman36f891b2005-07-30 00:12:19 +0000529 PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
530
531 pred_iterator HPI = pred_begin(Header);
532 assert(HPI != pred_end(Header) && "Loop with zero preds???");
533 if (!L->contains(*HPI)) ++HPI;
534 assert(HPI != pred_end(Header) && L->contains(*HPI) &&
535 "No backedge in loop?");
536
537 // Insert a unit add instruction right before the terminator corresponding
538 // to the back-edge.
Reid Spencer24d6da52007-01-21 00:29:26 +0000539 Constant *One = ConstantInt::get(Ty, 1);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000540 Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
Nate Begeman36f891b2005-07-30 00:12:19 +0000541 (*HPI)->getTerminator());
Dan Gohmancf5ab822009-05-01 17:13:31 +0000542 InsertedValues.insert(Add);
Nate Begeman36f891b2005-07-30 00:12:19 +0000543
544 pred_iterator PI = pred_begin(Header);
545 if (*PI == L->getLoopPreheader())
546 ++PI;
547 PN->addIncoming(Add, *PI);
548 return PN;
549 }
550
Dan Gohman4d8414f2009-06-13 16:25:49 +0000551 // {0,+,F} --> {0,+,1} * F
Nate Begeman36f891b2005-07-30 00:12:19 +0000552 // Get the canonical induction variable I for this loop.
Dan Gohman4d8414f2009-06-13 16:25:49 +0000553 Value *I = CanonicalIV ?
554 CanonicalIV :
555 getOrInsertCanonicalInductionVariable(L, Ty);
Nate Begeman36f891b2005-07-30 00:12:19 +0000556
Chris Lattnerdf14a042005-10-30 06:24:33 +0000557 // If this is a simple linear addrec, emit it now as a special case.
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000558 if (S->isAffine()) // {0,+,F} --> i*F
559 return
560 expand(SE.getTruncateOrNoop(
561 SE.getMulExpr(SE.getUnknown(I),
562 SE.getNoopOrAnyExtend(S->getOperand(1),
563 I->getType())),
564 Ty));
Nate Begeman36f891b2005-07-30 00:12:19 +0000565
566 // If this is a chain of recurrences, turn it into a closed form, using the
567 // folders, then expandCodeFor the closed form. This allows the folders to
568 // simplify the expression without having to build a bunch of special code
569 // into this folder.
Owen Anderson372b46c2009-06-22 21:39:50 +0000570 const SCEV* IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV.
Nate Begeman36f891b2005-07-30 00:12:19 +0000571
Dan Gohman4d8414f2009-06-13 16:25:49 +0000572 // Promote S up to the canonical IV type, if the cast is foldable.
Owen Anderson372b46c2009-06-22 21:39:50 +0000573 const SCEV* NewS = S;
574 const SCEV* Ext = SE.getNoopOrAnyExtend(S, I->getType());
Dan Gohman4d8414f2009-06-13 16:25:49 +0000575 if (isa<SCEVAddRecExpr>(Ext))
576 NewS = Ext;
577
Owen Anderson372b46c2009-06-22 21:39:50 +0000578 const SCEV* V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
Bill Wendlinge8156192006-12-07 01:30:32 +0000579 //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";
Nate Begeman36f891b2005-07-30 00:12:19 +0000580
Dan Gohman4d8414f2009-06-13 16:25:49 +0000581 // Truncate the result down to the original type, if needed.
Owen Anderson372b46c2009-06-22 21:39:50 +0000582 const SCEV* T = SE.getTruncateOrNoop(V, Ty);
Dan Gohman469f3cd2009-06-22 22:08:45 +0000583 return expand(T);
Nate Begeman36f891b2005-07-30 00:12:19 +0000584}
Anton Korobeynikov96fea332007-08-20 21:17:26 +0000585
Dan Gohman890f92b2009-04-18 17:56:28 +0000586Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000587 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman92fcdca2009-06-09 17:18:38 +0000588 Value *V = expandCodeFor(S->getOperand(),
589 SE.getEffectiveSCEVType(S->getOperand()->getType()));
Dan Gohmancf5ab822009-05-01 17:13:31 +0000590 Instruction *I = new TruncInst(V, Ty, "tmp.", InsertPt);
591 InsertedValues.insert(I);
592 return I;
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000593}
594
Dan Gohman890f92b2009-04-18 17:56:28 +0000595Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000596 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman92fcdca2009-06-09 17:18:38 +0000597 Value *V = expandCodeFor(S->getOperand(),
598 SE.getEffectiveSCEVType(S->getOperand()->getType()));
Dan Gohmancf5ab822009-05-01 17:13:31 +0000599 Instruction *I = new ZExtInst(V, Ty, "tmp.", InsertPt);
600 InsertedValues.insert(I);
601 return I;
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000602}
603
Dan Gohman890f92b2009-04-18 17:56:28 +0000604Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000605 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman92fcdca2009-06-09 17:18:38 +0000606 Value *V = expandCodeFor(S->getOperand(),
607 SE.getEffectiveSCEVType(S->getOperand()->getType()));
Dan Gohmancf5ab822009-05-01 17:13:31 +0000608 Instruction *I = new SExtInst(V, Ty, "tmp.", InsertPt);
609 InsertedValues.insert(I);
610 return I;
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000611}
612
Dan Gohman890f92b2009-04-18 17:56:28 +0000613Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000614 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman92fcdca2009-06-09 17:18:38 +0000615 Value *LHS = expandCodeFor(S->getOperand(0), Ty);
Nick Lewyckyc54c5612007-11-25 22:41:31 +0000616 for (unsigned i = 1; i < S->getNumOperands(); ++i) {
Dan Gohman92fcdca2009-06-09 17:18:38 +0000617 Value *RHS = expandCodeFor(S->getOperand(i), Ty);
Dan Gohmancf5ab822009-05-01 17:13:31 +0000618 Instruction *ICmp =
619 new ICmpInst(ICmpInst::ICMP_SGT, LHS, RHS, "tmp", InsertPt);
620 InsertedValues.insert(ICmp);
621 Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "smax", InsertPt);
622 InsertedValues.insert(Sel);
623 LHS = Sel;
Nick Lewyckyc54c5612007-11-25 22:41:31 +0000624 }
625 return LHS;
626}
627
Dan Gohman890f92b2009-04-18 17:56:28 +0000628Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
Dan Gohmanaf79fb52009-04-21 01:07:12 +0000629 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
Dan Gohman92fcdca2009-06-09 17:18:38 +0000630 Value *LHS = expandCodeFor(S->getOperand(0), Ty);
Nick Lewycky3e630762008-02-20 06:48:22 +0000631 for (unsigned i = 1; i < S->getNumOperands(); ++i) {
Dan Gohman92fcdca2009-06-09 17:18:38 +0000632 Value *RHS = expandCodeFor(S->getOperand(i), Ty);
Dan Gohmancf5ab822009-05-01 17:13:31 +0000633 Instruction *ICmp =
634 new ICmpInst(ICmpInst::ICMP_UGT, LHS, RHS, "tmp", InsertPt);
635 InsertedValues.insert(ICmp);
636 Instruction *Sel = SelectInst::Create(ICmp, LHS, RHS, "umax", InsertPt);
637 InsertedValues.insert(Sel);
638 LHS = Sel;
Nick Lewycky3e630762008-02-20 06:48:22 +0000639 }
640 return LHS;
641}
642
Owen Anderson372b46c2009-06-22 21:39:50 +0000643Value *SCEVExpander::expandCodeFor(const SCEV* SH, const Type *Ty) {
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000644 // Expand the code for this SCEV.
Dan Gohman2d1be872009-04-16 03:18:22 +0000645 Value *V = expand(SH);
Dan Gohman5be18e82009-05-19 02:15:55 +0000646 if (Ty) {
647 assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
648 "non-trivial casts should be done with the SCEVs directly!");
649 V = InsertNoopCastOfTo(V, Ty);
650 }
651 return V;
Dan Gohman11f6d3b2008-06-22 19:09:18 +0000652}
653
Dan Gohman890f92b2009-04-18 17:56:28 +0000654Value *SCEVExpander::expand(const SCEV *S) {
Dan Gohman667d7872009-06-26 22:53:46 +0000655 BasicBlock::iterator SaveInsertPt = InsertPt;
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000656
657 // Compute an insertion point for this SCEV object. Hoist the instructions
658 // as far out in the loop nest as possible.
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000659 for (Loop *L = SE.LI->getLoopFor(InsertPt->getParent()); ;
660 L = L->getParentLoop())
661 if (S->isLoopInvariant(L)) {
662 if (!L) break;
663 if (BasicBlock *Preheader = L->getLoopPreheader())
664 InsertPt = Preheader->getTerminator();
665 } else {
666 // If the SCEV is computable at this level, insert it into the header
667 // after the PHIs (and after any other instructions that we've inserted
668 // there) so that it is guaranteed to dominate any user inside the loop.
669 if (L && S->hasComputableLoopEvolution(L))
670 InsertPt = L->getHeader()->getFirstNonPHI();
671 while (isInsertedInstruction(InsertPt)) ++InsertPt;
672 break;
673 }
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000674
Dan Gohman667d7872009-06-26 22:53:46 +0000675 // Check to see if we already expanded this here.
676 std::map<std::pair<const SCEV *, Instruction *>,
677 AssertingVH<Value> >::iterator I =
678 InsertedExpressions.find(std::make_pair(S, InsertPt));
679 if (I != InsertedExpressions.end()) {
680 InsertPt = SaveInsertPt;
681 return I->second;
682 }
683
684 // Expand the expression into instructions.
Anton Korobeynikov96fea332007-08-20 21:17:26 +0000685 Value *V = visit(S);
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000686
Dan Gohman667d7872009-06-26 22:53:46 +0000687 // Remember the expanded value for this SCEV at this location.
688 InsertedExpressions[std::make_pair(S, InsertPt)] = V;
689
690 InsertPt = SaveInsertPt;
Anton Korobeynikov96fea332007-08-20 21:17:26 +0000691 return V;
692}
Dan Gohman1d09de32009-06-05 16:35:53 +0000693
694/// getOrInsertCanonicalInductionVariable - This method returns the
695/// canonical induction variable of the specified type for the specified
696/// loop (inserting one if there is none). A canonical induction variable
697/// starts at zero and steps by one on each iteration.
698Value *
699SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
700 const Type *Ty) {
701 assert(Ty->isInteger() && "Can only insert integer induction variables!");
Owen Anderson372b46c2009-06-22 21:39:50 +0000702 const SCEV* H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000703 SE.getIntegerSCEV(1, Ty), L);
Dan Gohman667d7872009-06-26 22:53:46 +0000704 BasicBlock::iterator SaveInsertPt = InsertPt;
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000705 Value *V = expandCodeFor(H, 0, L->getHeader()->begin());
Dan Gohman667d7872009-06-26 22:53:46 +0000706 InsertPt = SaveInsertPt;
Dan Gohman40a5a1b2009-06-24 01:18:18 +0000707 return V;
Dan Gohman1d09de32009-06-05 16:35:53 +0000708}