MemoryObject - Abstract base class for contiguous addressable memory.
[llvm/avr.git] / lib / Analysis / ScalarEvolutionExpander.cpp
blobeb2236b2d77996ae886ffe5732362d15681ed598
1 //===- ScalarEvolutionExpander.cpp - Scalar Evolution Analysis --*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/ScalarEvolutionExpander.h"
17 #include "llvm/Analysis/LoopInfo.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Target/TargetData.h"
20 #include "llvm/ADT/STLExtras.h"
21 using namespace llvm;
23 /// InsertNoopCastOfTo - Insert a cast of V to the specified type,
24 /// which must be possible with a noop cast, doing what we can to share
25 /// the casts.
26 Value *SCEVExpander::InsertNoopCastOfTo(Value *V, const Type *Ty) {
27 Instruction::CastOps Op = CastInst::getCastOpcode(V, false, Ty, false);
28 assert((Op == Instruction::BitCast ||
29 Op == Instruction::PtrToInt ||
30 Op == Instruction::IntToPtr) &&
31 "InsertNoopCastOfTo cannot perform non-noop casts!");
32 assert(SE.getTypeSizeInBits(V->getType()) == SE.getTypeSizeInBits(Ty) &&
33 "InsertNoopCastOfTo cannot change sizes!");
35 // Short-circuit unnecessary bitcasts.
36 if (Op == Instruction::BitCast && V->getType() == Ty)
37 return V;
39 // Short-circuit unnecessary inttoptr<->ptrtoint casts.
40 if ((Op == Instruction::PtrToInt || Op == Instruction::IntToPtr) &&
41 SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(V->getType())) {
42 if (CastInst *CI = dyn_cast<CastInst>(V))
43 if ((CI->getOpcode() == Instruction::PtrToInt ||
44 CI->getOpcode() == Instruction::IntToPtr) &&
45 SE.getTypeSizeInBits(CI->getType()) ==
46 SE.getTypeSizeInBits(CI->getOperand(0)->getType()))
47 return CI->getOperand(0);
48 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
49 if ((CE->getOpcode() == Instruction::PtrToInt ||
50 CE->getOpcode() == Instruction::IntToPtr) &&
51 SE.getTypeSizeInBits(CE->getType()) ==
52 SE.getTypeSizeInBits(CE->getOperand(0)->getType()))
53 return CE->getOperand(0);
56 // FIXME: keep track of the cast instruction.
57 if (Constant *C = dyn_cast<Constant>(V))
58 return getContext().getConstantExprCast(Op, C, Ty);
60 if (Argument *A = dyn_cast<Argument>(V)) {
61 // Check to see if there is already a cast!
62 for (Value::use_iterator UI = A->use_begin(), E = A->use_end();
63 UI != E; ++UI)
64 if ((*UI)->getType() == Ty)
65 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
66 if (CI->getOpcode() == Op) {
67 // If the cast isn't the first instruction of the function, move it.
68 if (BasicBlock::iterator(CI) !=
69 A->getParent()->getEntryBlock().begin()) {
70 // Recreate the cast at the beginning of the entry block.
71 // The old cast is left in place in case it is being used
72 // as an insert point.
73 Instruction *NewCI =
74 CastInst::Create(Op, V, Ty, "",
75 A->getParent()->getEntryBlock().begin());
76 NewCI->takeName(CI);
77 CI->replaceAllUsesWith(NewCI);
78 return NewCI;
80 return CI;
83 Instruction *I = CastInst::Create(Op, V, Ty, V->getName(),
84 A->getParent()->getEntryBlock().begin());
85 InsertedValues.insert(I);
86 return I;
89 Instruction *I = cast<Instruction>(V);
91 // Check to see if there is already a cast. If there is, use it.
92 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
93 UI != E; ++UI) {
94 if ((*UI)->getType() == Ty)
95 if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI)))
96 if (CI->getOpcode() == Op) {
97 BasicBlock::iterator It = I; ++It;
98 if (isa<InvokeInst>(I))
99 It = cast<InvokeInst>(I)->getNormalDest()->begin();
100 while (isa<PHINode>(It)) ++It;
101 if (It != BasicBlock::iterator(CI)) {
102 // Recreate the cast at the beginning of the entry block.
103 // The old cast is left in place in case it is being used
104 // as an insert point.
105 Instruction *NewCI = CastInst::Create(Op, V, Ty, "", It);
106 NewCI->takeName(CI);
107 CI->replaceAllUsesWith(NewCI);
108 return NewCI;
110 return CI;
113 BasicBlock::iterator IP = I; ++IP;
114 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
115 IP = II->getNormalDest()->begin();
116 while (isa<PHINode>(IP)) ++IP;
117 Instruction *CI = CastInst::Create(Op, V, Ty, V->getName(), IP);
118 InsertedValues.insert(CI);
119 return CI;
122 /// InsertBinop - Insert the specified binary operator, doing a small amount
123 /// of work to avoid inserting an obviously redundant operation.
124 Value *SCEVExpander::InsertBinop(Instruction::BinaryOps Opcode,
125 Value *LHS, Value *RHS) {
126 // Fold a binop with constant operands.
127 if (Constant *CLHS = dyn_cast<Constant>(LHS))
128 if (Constant *CRHS = dyn_cast<Constant>(RHS))
129 return getContext().getConstantExpr(Opcode, CLHS, CRHS);
131 // Do a quick scan to see if we have this binop nearby. If so, reuse it.
132 unsigned ScanLimit = 6;
133 BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
134 // Scanning starts from the last instruction before the insertion point.
135 BasicBlock::iterator IP = Builder.GetInsertPoint();
136 if (IP != BlockBegin) {
137 --IP;
138 for (; ScanLimit; --IP, --ScanLimit) {
139 if (IP->getOpcode() == (unsigned)Opcode && IP->getOperand(0) == LHS &&
140 IP->getOperand(1) == RHS)
141 return IP;
142 if (IP == BlockBegin) break;
146 // If we haven't found this binop, insert it.
147 Value *BO = Builder.CreateBinOp(Opcode, LHS, RHS, "tmp");
148 InsertedValues.insert(BO);
149 return BO;
152 /// FactorOutConstant - Test if S is divisible by Factor, using signed
153 /// division. If so, update S with Factor divided out and return true.
154 /// S need not be evenly divisble if a reasonable remainder can be
155 /// computed.
156 /// TODO: When ScalarEvolution gets a SCEVSDivExpr, this can be made
157 /// unnecessary; in its place, just signed-divide Ops[i] by the scale and
158 /// check to see if the divide was folded.
159 static bool FactorOutConstant(const SCEV *&S,
160 const SCEV *&Remainder,
161 const APInt &Factor,
162 ScalarEvolution &SE) {
163 // Everything is divisible by one.
164 if (Factor == 1)
165 return true;
167 // For a Constant, check for a multiple of the given factor.
168 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S)) {
169 ConstantInt *CI =
170 ConstantInt::get(SE.getContext(), C->getValue()->getValue().sdiv(Factor));
171 // If the quotient is zero and the remainder is non-zero, reject
172 // the value at this scale. It will be considered for subsequent
173 // smaller scales.
174 if (C->isZero() || !CI->isZero()) {
175 const SCEV *Div = SE.getConstant(CI);
176 S = Div;
177 Remainder =
178 SE.getAddExpr(Remainder,
179 SE.getConstant(C->getValue()->getValue().srem(Factor)));
180 return true;
184 // In a Mul, check if there is a constant operand which is a multiple
185 // of the given factor.
186 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S))
187 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(M->getOperand(0)))
188 if (!C->getValue()->getValue().srem(Factor)) {
189 const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
190 SmallVector<const SCEV *, 4> NewMulOps(MOperands.begin(),
191 MOperands.end());
192 NewMulOps[0] =
193 SE.getConstant(C->getValue()->getValue().sdiv(Factor));
194 S = SE.getMulExpr(NewMulOps);
195 return true;
198 // In an AddRec, check if both start and step are divisible.
199 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
200 const SCEV *Step = A->getStepRecurrence(SE);
201 const SCEV *StepRem = SE.getIntegerSCEV(0, Step->getType());
202 if (!FactorOutConstant(Step, StepRem, Factor, SE))
203 return false;
204 if (!StepRem->isZero())
205 return false;
206 const SCEV *Start = A->getStart();
207 if (!FactorOutConstant(Start, Remainder, Factor, SE))
208 return false;
209 S = SE.getAddRecExpr(Start, Step, A->getLoop());
210 return true;
213 return false;
216 /// expandAddToGEP - Expand a SCEVAddExpr with a pointer type into a GEP
217 /// instead of using ptrtoint+arithmetic+inttoptr. This helps
218 /// BasicAliasAnalysis analyze the result.
220 /// Design note: This depends on ScalarEvolution not recognizing inttoptr
221 /// and ptrtoint operators, as they may introduce pointer arithmetic
222 /// which may not be safely converted into getelementptr.
224 /// Design note: It might seem desirable for this function to be more
225 /// loop-aware. If some of the indices are loop-invariant while others
226 /// aren't, it might seem desirable to emit multiple GEPs, keeping the
227 /// loop-invariant portions of the overall computation outside the loop.
228 /// However, there are a few reasons this is not done here. Hoisting simple
229 /// arithmetic is a low-level optimization that often isn't very
230 /// important until late in the optimization process. In fact, passes
231 /// like InstructionCombining will combine GEPs, even if it means
232 /// pushing loop-invariant computation down into loops, so even if the
233 /// GEPs were split here, the work would quickly be undone. The
234 /// LoopStrengthReduction pass, which is usually run quite late (and
235 /// after the last InstructionCombining pass), takes care of hoisting
236 /// loop-invariant portions of expressions, after considering what
237 /// can be folded using target addressing modes.
239 Value *SCEVExpander::expandAddToGEP(const SCEV *const *op_begin,
240 const SCEV *const *op_end,
241 const PointerType *PTy,
242 const Type *Ty,
243 Value *V) {
244 const Type *ElTy = PTy->getElementType();
245 SmallVector<Value *, 4> GepIndices;
246 SmallVector<const SCEV *, 8> Ops(op_begin, op_end);
247 bool AnyNonZeroIndices = false;
249 // Decend down the pointer's type and attempt to convert the other
250 // operands into GEP indices, at each level. The first index in a GEP
251 // indexes into the array implied by the pointer operand; the rest of
252 // the indices index into the element or field type selected by the
253 // preceding index.
254 for (;;) {
255 APInt ElSize = APInt(SE.getTypeSizeInBits(Ty),
256 ElTy->isSized() ? SE.TD->getTypeAllocSize(ElTy) : 0);
257 SmallVector<const SCEV *, 8> NewOps;
258 SmallVector<const SCEV *, 8> ScaledOps;
259 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
260 // Split AddRecs up into parts as either of the parts may be usable
261 // without the other.
262 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Ops[i]))
263 if (!A->getStart()->isZero()) {
264 const SCEV *Start = A->getStart();
265 Ops.push_back(SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
266 A->getStepRecurrence(SE),
267 A->getLoop()));
268 Ops[i] = Start;
269 ++e;
271 // If the scale size is not 0, attempt to factor out a scale.
272 if (ElSize != 0) {
273 const SCEV *Op = Ops[i];
274 const SCEV *Remainder = SE.getIntegerSCEV(0, Op->getType());
275 if (FactorOutConstant(Op, Remainder, ElSize, SE)) {
276 ScaledOps.push_back(Op); // Op now has ElSize factored out.
277 NewOps.push_back(Remainder);
278 continue;
281 // If the operand was not divisible, add it to the list of operands
282 // we'll scan next iteration.
283 NewOps.push_back(Ops[i]);
285 Ops = NewOps;
286 AnyNonZeroIndices |= !ScaledOps.empty();
287 Value *Scaled = ScaledOps.empty() ?
288 getContext().getNullValue(Ty) :
289 expandCodeFor(SE.getAddExpr(ScaledOps), Ty);
290 GepIndices.push_back(Scaled);
292 // Collect struct field index operands.
293 if (!Ops.empty())
294 while (const StructType *STy = dyn_cast<StructType>(ElTy)) {
295 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[0]))
296 if (SE.getTypeSizeInBits(C->getType()) <= 64) {
297 const StructLayout &SL = *SE.TD->getStructLayout(STy);
298 uint64_t FullOffset = C->getValue()->getZExtValue();
299 if (FullOffset < SL.getSizeInBytes()) {
300 unsigned ElIdx = SL.getElementContainingOffset(FullOffset);
301 GepIndices.push_back(ConstantInt::get(Type::Int32Ty, ElIdx));
302 ElTy = STy->getTypeAtIndex(ElIdx);
303 Ops[0] =
304 SE.getConstant(Ty, FullOffset - SL.getElementOffset(ElIdx));
305 AnyNonZeroIndices = true;
306 continue;
309 break;
312 if (const ArrayType *ATy = dyn_cast<ArrayType>(ElTy)) {
313 ElTy = ATy->getElementType();
314 continue;
316 break;
319 // If none of the operands were convertable to proper GEP indices, cast
320 // the base to i8* and do an ugly getelementptr with that. It's still
321 // better than ptrtoint+arithmetic+inttoptr at least.
322 if (!AnyNonZeroIndices) {
323 V = InsertNoopCastOfTo(V,
324 Type::Int8Ty->getPointerTo(PTy->getAddressSpace()));
325 Value *Idx = expandCodeFor(SE.getAddExpr(Ops), Ty);
327 // Fold a GEP with constant operands.
328 if (Constant *CLHS = dyn_cast<Constant>(V))
329 if (Constant *CRHS = dyn_cast<Constant>(Idx))
330 return getContext().getConstantExprGetElementPtr(CLHS, &CRHS, 1);
332 // Do a quick scan to see if we have this GEP nearby. If so, reuse it.
333 unsigned ScanLimit = 6;
334 BasicBlock::iterator BlockBegin = Builder.GetInsertBlock()->begin();
335 // Scanning starts from the last instruction before the insertion point.
336 BasicBlock::iterator IP = Builder.GetInsertPoint();
337 if (IP != BlockBegin) {
338 --IP;
339 for (; ScanLimit; --IP, --ScanLimit) {
340 if (IP->getOpcode() == Instruction::GetElementPtr &&
341 IP->getOperand(0) == V && IP->getOperand(1) == Idx)
342 return IP;
343 if (IP == BlockBegin) break;
347 Value *GEP = Builder.CreateGEP(V, Idx, "scevgep");
348 InsertedValues.insert(GEP);
349 return GEP;
352 // Insert a pretty getelementptr.
353 Value *GEP = Builder.CreateGEP(V,
354 GepIndices.begin(),
355 GepIndices.end(),
356 "scevgep");
357 Ops.push_back(SE.getUnknown(GEP));
358 InsertedValues.insert(GEP);
359 return expand(SE.getAddExpr(Ops));
362 Value *SCEVExpander::visitAddExpr(const SCEVAddExpr *S) {
363 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
364 Value *V = expand(S->getOperand(S->getNumOperands()-1));
366 // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
367 // comments on expandAddToGEP for details.
368 if (SE.TD)
369 if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) {
370 const SmallVectorImpl<const SCEV *> &Ops = S->getOperands();
371 return expandAddToGEP(&Ops[0], &Ops[Ops.size() - 1], PTy, Ty, V);
374 V = InsertNoopCastOfTo(V, Ty);
376 // Emit a bunch of add instructions
377 for (int i = S->getNumOperands()-2; i >= 0; --i) {
378 Value *W = expandCodeFor(S->getOperand(i), Ty);
379 V = InsertBinop(Instruction::Add, V, W);
381 return V;
384 Value *SCEVExpander::visitMulExpr(const SCEVMulExpr *S) {
385 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
386 int FirstOp = 0; // Set if we should emit a subtract.
387 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0)))
388 if (SC->getValue()->isAllOnesValue())
389 FirstOp = 1;
391 int i = S->getNumOperands()-2;
392 Value *V = expandCodeFor(S->getOperand(i+1), Ty);
394 // Emit a bunch of multiply instructions
395 for (; i >= FirstOp; --i) {
396 Value *W = expandCodeFor(S->getOperand(i), Ty);
397 V = InsertBinop(Instruction::Mul, V, W);
400 // -1 * ... ---> 0 - ...
401 if (FirstOp == 1)
402 V = InsertBinop(Instruction::Sub, getContext().getNullValue(Ty), V);
403 return V;
406 Value *SCEVExpander::visitUDivExpr(const SCEVUDivExpr *S) {
407 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
409 Value *LHS = expandCodeFor(S->getLHS(), Ty);
410 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getRHS())) {
411 const APInt &RHS = SC->getValue()->getValue();
412 if (RHS.isPowerOf2())
413 return InsertBinop(Instruction::LShr, LHS,
414 ConstantInt::get(Ty, RHS.logBase2()));
417 Value *RHS = expandCodeFor(S->getRHS(), Ty);
418 return InsertBinop(Instruction::UDiv, LHS, RHS);
421 /// Move parts of Base into Rest to leave Base with the minimal
422 /// expression that provides a pointer operand suitable for a
423 /// GEP expansion.
424 static void ExposePointerBase(const SCEV *&Base, const SCEV *&Rest,
425 ScalarEvolution &SE) {
426 while (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(Base)) {
427 Base = A->getStart();
428 Rest = SE.getAddExpr(Rest,
429 SE.getAddRecExpr(SE.getIntegerSCEV(0, A->getType()),
430 A->getStepRecurrence(SE),
431 A->getLoop()));
433 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Base)) {
434 Base = A->getOperand(A->getNumOperands()-1);
435 SmallVector<const SCEV *, 8> NewAddOps(A->op_begin(), A->op_end());
436 NewAddOps.back() = Rest;
437 Rest = SE.getAddExpr(NewAddOps);
438 ExposePointerBase(Base, Rest, SE);
442 Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
443 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
444 const Loop *L = S->getLoop();
446 // First check for an existing canonical IV in a suitable type.
447 PHINode *CanonicalIV = 0;
448 if (PHINode *PN = L->getCanonicalInductionVariable())
449 if (SE.isSCEVable(PN->getType()) &&
450 isa<IntegerType>(SE.getEffectiveSCEVType(PN->getType())) &&
451 SE.getTypeSizeInBits(PN->getType()) >= SE.getTypeSizeInBits(Ty))
452 CanonicalIV = PN;
454 // Rewrite an AddRec in terms of the canonical induction variable, if
455 // its type is more narrow.
456 if (CanonicalIV &&
457 SE.getTypeSizeInBits(CanonicalIV->getType()) >
458 SE.getTypeSizeInBits(Ty)) {
459 const SCEV *Start = SE.getAnyExtendExpr(S->getStart(),
460 CanonicalIV->getType());
461 const SCEV *Step = SE.getAnyExtendExpr(S->getStepRecurrence(SE),
462 CanonicalIV->getType());
463 Value *V = expand(SE.getAddRecExpr(Start, Step, S->getLoop()));
464 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
465 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
466 BasicBlock::iterator NewInsertPt =
467 next(BasicBlock::iterator(cast<Instruction>(V)));
468 while (isa<PHINode>(NewInsertPt)) ++NewInsertPt;
469 V = expandCodeFor(SE.getTruncateExpr(SE.getUnknown(V), Ty), 0,
470 NewInsertPt);
471 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
472 return V;
475 // {X,+,F} --> X + {0,+,F}
476 if (!S->getStart()->isZero()) {
477 const SmallVectorImpl<const SCEV *> &SOperands = S->getOperands();
478 SmallVector<const SCEV *, 4> NewOps(SOperands.begin(), SOperands.end());
479 NewOps[0] = SE.getIntegerSCEV(0, Ty);
480 const SCEV *Rest = SE.getAddRecExpr(NewOps, L);
482 // Turn things like ptrtoint+arithmetic+inttoptr into GEP. See the
483 // comments on expandAddToGEP for details.
484 if (SE.TD) {
485 const SCEV *Base = S->getStart();
486 const SCEV *RestArray[1] = { Rest };
487 // Dig into the expression to find the pointer base for a GEP.
488 ExposePointerBase(Base, RestArray[0], SE);
489 // If we found a pointer, expand the AddRec with a GEP.
490 if (const PointerType *PTy = dyn_cast<PointerType>(Base->getType())) {
491 // Make sure the Base isn't something exotic, such as a multiplied
492 // or divided pointer value. In those cases, the result type isn't
493 // actually a pointer type.
494 if (!isa<SCEVMulExpr>(Base) && !isa<SCEVUDivExpr>(Base)) {
495 Value *StartV = expand(Base);
496 assert(StartV->getType() == PTy && "Pointer type mismatch for GEP!");
497 return expandAddToGEP(RestArray, RestArray+1, PTy, Ty, StartV);
502 // Just do a normal add. Pre-expand the operands to suppress folding.
503 return expand(SE.getAddExpr(SE.getUnknown(expand(S->getStart())),
504 SE.getUnknown(expand(Rest))));
507 // {0,+,1} --> Insert a canonical induction variable into the loop!
508 if (S->isAffine() &&
509 S->getOperand(1) == SE.getIntegerSCEV(1, Ty)) {
510 // If there's a canonical IV, just use it.
511 if (CanonicalIV) {
512 assert(Ty == SE.getEffectiveSCEVType(CanonicalIV->getType()) &&
513 "IVs with types different from the canonical IV should "
514 "already have been handled!");
515 return CanonicalIV;
518 // Create and insert the PHI node for the induction variable in the
519 // specified loop.
520 BasicBlock *Header = L->getHeader();
521 BasicBlock *Preheader = L->getLoopPreheader();
522 PHINode *PN = PHINode::Create(Ty, "indvar", Header->begin());
523 InsertedValues.insert(PN);
524 PN->addIncoming(getContext().getNullValue(Ty), Preheader);
526 pred_iterator HPI = pred_begin(Header);
527 assert(HPI != pred_end(Header) && "Loop with zero preds???");
528 if (!L->contains(*HPI)) ++HPI;
529 assert(HPI != pred_end(Header) && L->contains(*HPI) &&
530 "No backedge in loop?");
532 // Insert a unit add instruction right before the terminator corresponding
533 // to the back-edge.
534 Constant *One = ConstantInt::get(Ty, 1);
535 Instruction *Add = BinaryOperator::CreateAdd(PN, One, "indvar.next",
536 (*HPI)->getTerminator());
537 InsertedValues.insert(Add);
539 pred_iterator PI = pred_begin(Header);
540 if (*PI == Preheader)
541 ++PI;
542 PN->addIncoming(Add, *PI);
543 return PN;
546 // {0,+,F} --> {0,+,1} * F
547 // Get the canonical induction variable I for this loop.
548 Value *I = CanonicalIV ?
549 CanonicalIV :
550 getOrInsertCanonicalInductionVariable(L, Ty);
552 // If this is a simple linear addrec, emit it now as a special case.
553 if (S->isAffine()) // {0,+,F} --> i*F
554 return
555 expand(SE.getTruncateOrNoop(
556 SE.getMulExpr(SE.getUnknown(I),
557 SE.getNoopOrAnyExtend(S->getOperand(1),
558 I->getType())),
559 Ty));
561 // If this is a chain of recurrences, turn it into a closed form, using the
562 // folders, then expandCodeFor the closed form. This allows the folders to
563 // simplify the expression without having to build a bunch of special code
564 // into this folder.
565 const SCEV *IH = SE.getUnknown(I); // Get I as a "symbolic" SCEV.
567 // Promote S up to the canonical IV type, if the cast is foldable.
568 const SCEV *NewS = S;
569 const SCEV *Ext = SE.getNoopOrAnyExtend(S, I->getType());
570 if (isa<SCEVAddRecExpr>(Ext))
571 NewS = Ext;
573 const SCEV *V = cast<SCEVAddRecExpr>(NewS)->evaluateAtIteration(IH, SE);
574 //cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";
576 // Truncate the result down to the original type, if needed.
577 const SCEV *T = SE.getTruncateOrNoop(V, Ty);
578 return expand(T);
581 Value *SCEVExpander::visitTruncateExpr(const SCEVTruncateExpr *S) {
582 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
583 Value *V = expandCodeFor(S->getOperand(),
584 SE.getEffectiveSCEVType(S->getOperand()->getType()));
585 Value *I = Builder.CreateTrunc(V, Ty, "tmp");
586 InsertedValues.insert(I);
587 return I;
590 Value *SCEVExpander::visitZeroExtendExpr(const SCEVZeroExtendExpr *S) {
591 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
592 Value *V = expandCodeFor(S->getOperand(),
593 SE.getEffectiveSCEVType(S->getOperand()->getType()));
594 Value *I = Builder.CreateZExt(V, Ty, "tmp");
595 InsertedValues.insert(I);
596 return I;
599 Value *SCEVExpander::visitSignExtendExpr(const SCEVSignExtendExpr *S) {
600 const Type *Ty = SE.getEffectiveSCEVType(S->getType());
601 Value *V = expandCodeFor(S->getOperand(),
602 SE.getEffectiveSCEVType(S->getOperand()->getType()));
603 Value *I = Builder.CreateSExt(V, Ty, "tmp");
604 InsertedValues.insert(I);
605 return I;
608 Value *SCEVExpander::visitSMaxExpr(const SCEVSMaxExpr *S) {
609 Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
610 const Type *Ty = LHS->getType();
611 for (int i = S->getNumOperands()-2; i >= 0; --i) {
612 // In the case of mixed integer and pointer types, do the
613 // rest of the comparisons as integer.
614 if (S->getOperand(i)->getType() != Ty) {
615 Ty = SE.getEffectiveSCEVType(Ty);
616 LHS = InsertNoopCastOfTo(LHS, Ty);
618 Value *RHS = expandCodeFor(S->getOperand(i), Ty);
619 Value *ICmp = Builder.CreateICmpSGT(LHS, RHS, "tmp");
620 InsertedValues.insert(ICmp);
621 Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "smax");
622 InsertedValues.insert(Sel);
623 LHS = Sel;
625 // In the case of mixed integer and pointer types, cast the
626 // final result back to the pointer type.
627 if (LHS->getType() != S->getType())
628 LHS = InsertNoopCastOfTo(LHS, S->getType());
629 return LHS;
632 Value *SCEVExpander::visitUMaxExpr(const SCEVUMaxExpr *S) {
633 Value *LHS = expand(S->getOperand(S->getNumOperands()-1));
634 const Type *Ty = LHS->getType();
635 for (int i = S->getNumOperands()-2; i >= 0; --i) {
636 // In the case of mixed integer and pointer types, do the
637 // rest of the comparisons as integer.
638 if (S->getOperand(i)->getType() != Ty) {
639 Ty = SE.getEffectiveSCEVType(Ty);
640 LHS = InsertNoopCastOfTo(LHS, Ty);
642 Value *RHS = expandCodeFor(S->getOperand(i), Ty);
643 Value *ICmp = Builder.CreateICmpUGT(LHS, RHS, "tmp");
644 InsertedValues.insert(ICmp);
645 Value *Sel = Builder.CreateSelect(ICmp, LHS, RHS, "umax");
646 InsertedValues.insert(Sel);
647 LHS = Sel;
649 // In the case of mixed integer and pointer types, cast the
650 // final result back to the pointer type.
651 if (LHS->getType() != S->getType())
652 LHS = InsertNoopCastOfTo(LHS, S->getType());
653 return LHS;
656 Value *SCEVExpander::expandCodeFor(const SCEV *SH, const Type *Ty) {
657 // Expand the code for this SCEV.
658 Value *V = expand(SH);
659 if (Ty) {
660 assert(SE.getTypeSizeInBits(Ty) == SE.getTypeSizeInBits(SH->getType()) &&
661 "non-trivial casts should be done with the SCEVs directly!");
662 V = InsertNoopCastOfTo(V, Ty);
664 return V;
667 Value *SCEVExpander::expand(const SCEV *S) {
668 // Compute an insertion point for this SCEV object. Hoist the instructions
669 // as far out in the loop nest as possible.
670 Instruction *InsertPt = Builder.GetInsertPoint();
671 for (Loop *L = SE.LI->getLoopFor(Builder.GetInsertBlock()); ;
672 L = L->getParentLoop())
673 if (S->isLoopInvariant(L)) {
674 if (!L) break;
675 if (BasicBlock *Preheader = L->getLoopPreheader())
676 InsertPt = Preheader->getTerminator();
677 } else {
678 // If the SCEV is computable at this level, insert it into the header
679 // after the PHIs (and after any other instructions that we've inserted
680 // there) so that it is guaranteed to dominate any user inside the loop.
681 if (L && S->hasComputableLoopEvolution(L))
682 InsertPt = L->getHeader()->getFirstNonPHI();
683 while (isInsertedInstruction(InsertPt))
684 InsertPt = next(BasicBlock::iterator(InsertPt));
685 break;
688 // Check to see if we already expanded this here.
689 std::map<std::pair<const SCEV *, Instruction *>,
690 AssertingVH<Value> >::iterator I =
691 InsertedExpressions.find(std::make_pair(S, InsertPt));
692 if (I != InsertedExpressions.end())
693 return I->second;
695 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
696 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
697 Builder.SetInsertPoint(InsertPt->getParent(), InsertPt);
699 // Expand the expression into instructions.
700 Value *V = visit(S);
702 // Remember the expanded value for this SCEV at this location.
703 InsertedExpressions[std::make_pair(S, InsertPt)] = V;
705 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
706 return V;
709 /// getOrInsertCanonicalInductionVariable - This method returns the
710 /// canonical induction variable of the specified type for the specified
711 /// loop (inserting one if there is none). A canonical induction variable
712 /// starts at zero and steps by one on each iteration.
713 Value *
714 SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
715 const Type *Ty) {
716 assert(Ty->isInteger() && "Can only insert integer induction variables!");
717 const SCEV *H = SE.getAddRecExpr(SE.getIntegerSCEV(0, Ty),
718 SE.getIntegerSCEV(1, Ty), L);
719 BasicBlock *SaveInsertBB = Builder.GetInsertBlock();
720 BasicBlock::iterator SaveInsertPt = Builder.GetInsertPoint();
721 Value *V = expandCodeFor(H, 0, L->getHeader()->begin());
722 if (SaveInsertBB)
723 Builder.SetInsertPoint(SaveInsertBB, SaveInsertPt);
724 return V;