1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of the scalar evolution analysis
11 // engine, which is used primarily to analyze expressions involving induction
12 // variables in loops.
14 // There are several aspects to this library. First is the representation of
15 // scalar expressions, which are represented as subclasses of the SCEV class.
16 // These classes are used to represent certain types of subexpressions that we
17 // can handle. These classes are reference counted, managed by the SCEVHandle
18 // class. We only create one SCEV of a particular shape, so pointer-comparisons
19 // for equality are legal.
21 // One important aspect of the SCEV objects is that they are never cyclic, even
22 // if there is a cycle in the dataflow for an expression (ie, a PHI node). If
23 // the PHI node is one of the idioms that we can represent (e.g., a polynomial
24 // recurrence) then we represent it directly as a recurrence node, otherwise we
25 // represent it as a SCEVUnknown node.
27 // In addition to being able to represent expressions of various types, we also
28 // have folders that are used to build the *canonical* representation for a
29 // particular expression. These folders are capable of using a variety of
30 // rewrite rules to simplify the expressions.
32 // Once the folders are defined, we can implement the more interesting
33 // higher-level code, such as the code that recognizes PHI nodes of various
34 // types, computes the execution count of a loop, etc.
36 // Orthogonal to the analysis of code above, this file also implements the
37 // ScalarEvolutionRewriter class, which is used to emit code that represents the
38 // various recurrences present in a loop, in canonical forms.
40 // TODO: We should use these routines and value representations to implement
41 // dependence analysis!
43 //===----------------------------------------------------------------------===//
45 // There are several good references for the techniques used in this analysis.
47 // Chains of recurrences -- a method to expedite the evaluation
48 // of closed-form functions
49 // Olaf Bachmann, Paul S. Wang, Eugene V. Zima
51 // On computational properties of chains of recurrences
54 // Symbolic Evaluation of Chains of Recurrences for Loop Optimization
55 // Robert A. van Engelen
57 // Efficient Symbolic Analysis for Optimizing Compilers
58 // Robert A. van Engelen
60 // Using the chains of recurrences algebra for data dependence testing and
61 // induction variable substitution
62 // MS Thesis, Johnie Birch
64 //===----------------------------------------------------------------------===//
66 #include "llvm/Analysis/ScalarEvolution.h"
67 #include "llvm/Constants.h"
68 #include "llvm/DerivedTypes.h"
69 #include "llvm/Instructions.h"
70 #include "llvm/Type.h"
71 #include "llvm/Value.h"
72 #include "llvm/Analysis/LoopInfo.h"
73 #include "llvm/Assembly/Writer.h"
74 #include "llvm/Transforms/Scalar.h"
75 #include "llvm/Support/CFG.h"
76 #include "llvm/Support/ConstantRange.h"
77 #include "llvm/Support/InstIterator.h"
78 #include "Support/Statistic.h"
82 RegisterAnalysis<ScalarEvolution>
83 R("scalar-evolution", "Scalar Evolution Analysis Printer");
86 NumBruteForceEvaluations("scalar-evolution",
87 "Number of brute force evaluations needed to calculate high-order polynomial exit values");
89 NumTripCountsComputed("scalar-evolution",
90 "Number of loops with predictable loop counts");
92 NumTripCountsNotComputed("scalar-evolution",
93 "Number of loops without predictable loop counts");
96 //===----------------------------------------------------------------------===//
97 // SCEV class definitions
98 //===----------------------------------------------------------------------===//
100 //===----------------------------------------------------------------------===//
101 // Implementation of the SCEV class.
105 // These should be ordered in terms of increasing complexity to make the
107 scConstant, scTruncate, scZeroExtend, scAddExpr, scMulExpr, scUDivExpr,
108 scAddRecExpr, scUnknown, scCouldNotCompute
111 /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
112 /// than the complexity of the RHS. If the SCEVs have identical complexity,
113 /// order them by their addresses. This comparator is used to canonicalize
115 struct SCEVComplexityCompare {
116 bool operator()(SCEV *LHS, SCEV *RHS) {
117 if (LHS->getSCEVType() < RHS->getSCEVType())
119 if (LHS->getSCEVType() == RHS->getSCEVType())
127 void SCEV::dump() const {
131 /// getValueRange - Return the tightest constant bounds that this value is
132 /// known to have. This method is only valid on integer SCEV objects.
133 ConstantRange SCEV::getValueRange() const {
134 const Type *Ty = getType();
135 assert(Ty->isInteger() && "Can't get range for a non-integer SCEV!");
136 Ty = Ty->getUnsignedVersion();
137 // Default to a full range if no better information is available.
138 return ConstantRange(getType());
142 SCEVCouldNotCompute::SCEVCouldNotCompute() : SCEV(scCouldNotCompute) {}
144 bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
145 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
149 const Type *SCEVCouldNotCompute::getType() const {
150 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
154 bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
155 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
159 Value *SCEVCouldNotCompute::expandCodeFor(ScalarEvolutionRewriter &SER,
160 Instruction *InsertPt) {
161 assert(0 && "Attempt to use a SCEVCouldNotCompute object!");
166 void SCEVCouldNotCompute::print(std::ostream &OS) const {
167 OS << "***COULDNOTCOMPUTE***";
170 bool SCEVCouldNotCompute::classof(const SCEV *S) {
171 return S->getSCEVType() == scCouldNotCompute;
175 //===----------------------------------------------------------------------===//
176 // SCEVConstant - This class represents a constant integer value.
180 // SCEVConstants - Only allow the creation of one SCEVConstant for any
181 // particular value. Don't use a SCEVHandle here, or else the object will
183 std::map<ConstantInt*, SCEVConstant*> SCEVConstants;
185 class SCEVConstant : public SCEV {
187 SCEVConstant(ConstantInt *v) : SCEV(scConstant), V(v) {}
189 virtual ~SCEVConstant() {
190 SCEVConstants.erase(V);
193 /// get method - This just gets and returns a new SCEVConstant object.
195 static SCEVHandle get(ConstantInt *V) {
196 // Make sure that SCEVConstant instances are all unsigned.
197 if (V->getType()->isSigned()) {
198 const Type *NewTy = V->getType()->getUnsignedVersion();
199 V = cast<ConstantUInt>(ConstantExpr::getCast(V, NewTy));
202 SCEVConstant *&R = SCEVConstants[V];
203 if (R == 0) R = new SCEVConstant(V);
207 ConstantInt *getValue() const { return V; }
209 /// getValueRange - Return the tightest constant bounds that this value is
210 /// known to have. This method is only valid on integer SCEV objects.
211 virtual ConstantRange getValueRange() const {
212 return ConstantRange(V);
215 virtual bool isLoopInvariant(const Loop *L) const {
219 virtual bool hasComputableLoopEvolution(const Loop *L) const {
220 return false; // Not loop variant
223 virtual const Type *getType() const { return V->getType(); }
225 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
226 Instruction *InsertPt) {
230 virtual void print(std::ostream &OS) const {
231 WriteAsOperand(OS, V, false);
234 /// Methods for support type inquiry through isa, cast, and dyn_cast:
235 static inline bool classof(const SCEVConstant *S) { return true; }
236 static inline bool classof(const SCEV *S) {
237 return S->getSCEVType() == scConstant;
243 //===----------------------------------------------------------------------===//
244 // SCEVTruncateExpr - This class represents a truncation of an integer value to
245 // a smaller integer value.
248 class SCEVTruncateExpr;
249 // SCEVTruncates - Only allow the creation of one SCEVTruncateExpr for any
250 // particular input. Don't use a SCEVHandle here, or else the object will
252 std::map<std::pair<SCEV*, const Type*>, SCEVTruncateExpr*> SCEVTruncates;
254 class SCEVTruncateExpr : public SCEV {
257 SCEVTruncateExpr(const SCEVHandle &op, const Type *ty)
258 : SCEV(scTruncate), Op(op), Ty(ty) {
259 assert(Op->getType()->isInteger() && Ty->isInteger() &&
261 "Cannot truncate non-integer value!");
262 assert(Op->getType()->getPrimitiveSize() > Ty->getPrimitiveSize() &&
263 "This is not a truncating conversion!");
266 virtual ~SCEVTruncateExpr() {
267 SCEVTruncates.erase(std::make_pair(Op, Ty));
270 /// get method - This just gets and returns a new SCEVTruncate object
272 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
274 const SCEVHandle &getOperand() const { return Op; }
275 virtual const Type *getType() const { return Ty; }
277 virtual bool isLoopInvariant(const Loop *L) const {
278 return Op->isLoopInvariant(L);
281 virtual bool hasComputableLoopEvolution(const Loop *L) const {
282 return Op->hasComputableLoopEvolution(L);
285 /// getValueRange - Return the tightest constant bounds that this value is
286 /// known to have. This method is only valid on integer SCEV objects.
287 virtual ConstantRange getValueRange() const {
288 return getOperand()->getValueRange().truncate(getType());
291 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
292 Instruction *InsertPt);
294 virtual void print(std::ostream &OS) const {
295 OS << "(truncate " << *Op << " to " << *Ty << ")";
298 /// Methods for support type inquiry through isa, cast, and dyn_cast:
299 static inline bool classof(const SCEVTruncateExpr *S) { return true; }
300 static inline bool classof(const SCEV *S) {
301 return S->getSCEVType() == scTruncate;
307 //===----------------------------------------------------------------------===//
308 // SCEVZeroExtendExpr - This class represents a zero extension of a small
309 // integer value to a larger integer value.
312 class SCEVZeroExtendExpr;
313 // SCEVZeroExtends - Only allow the creation of one SCEVZeroExtendExpr for any
314 // particular input. Don't use a SCEVHandle here, or else the object will
316 std::map<std::pair<SCEV*, const Type*>, SCEVZeroExtendExpr*> SCEVZeroExtends;
318 class SCEVZeroExtendExpr : public SCEV {
321 SCEVZeroExtendExpr(const SCEVHandle &op, const Type *ty)
322 : SCEV(scTruncate), Op(Op), Ty(ty) {
323 assert(Op->getType()->isInteger() && Ty->isInteger() &&
325 "Cannot zero extend non-integer value!");
326 assert(Op->getType()->getPrimitiveSize() < Ty->getPrimitiveSize() &&
327 "This is not an extending conversion!");
330 virtual ~SCEVZeroExtendExpr() {
331 SCEVZeroExtends.erase(std::make_pair(Op, Ty));
334 /// get method - This just gets and returns a new SCEVZeroExtend object
336 static SCEVHandle get(const SCEVHandle &Op, const Type *Ty);
338 const SCEVHandle &getOperand() const { return Op; }
339 virtual const Type *getType() const { return Ty; }
341 virtual bool isLoopInvariant(const Loop *L) const {
342 return Op->isLoopInvariant(L);
345 virtual bool hasComputableLoopEvolution(const Loop *L) const {
346 return Op->hasComputableLoopEvolution(L);
349 /// getValueRange - Return the tightest constant bounds that this value is
350 /// known to have. This method is only valid on integer SCEV objects.
351 virtual ConstantRange getValueRange() const {
352 return getOperand()->getValueRange().zeroExtend(getType());
355 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
356 Instruction *InsertPt);
358 virtual void print(std::ostream &OS) const {
359 OS << "(zeroextend " << *Op << " to " << *Ty << ")";
362 /// Methods for support type inquiry through isa, cast, and dyn_cast:
363 static inline bool classof(const SCEVZeroExtendExpr *S) { return true; }
364 static inline bool classof(const SCEV *S) {
365 return S->getSCEVType() == scZeroExtend;
371 //===----------------------------------------------------------------------===//
372 // SCEVCommutativeExpr - This node is the base class for n'ary commutative
376 class SCEVCommutativeExpr;
377 // SCEVCommExprs - Only allow the creation of one SCEVCommutativeExpr for any
378 // particular input. Don't use a SCEVHandle here, or else the object will
380 std::map<std::pair<unsigned, std::vector<SCEV*> >,
381 SCEVCommutativeExpr*> SCEVCommExprs;
383 class SCEVCommutativeExpr : public SCEV {
384 std::vector<SCEVHandle> Operands;
387 SCEVCommutativeExpr(enum SCEVTypes T, const std::vector<SCEVHandle> &ops)
389 Operands.reserve(ops.size());
390 Operands.insert(Operands.end(), ops.begin(), ops.end());
393 ~SCEVCommutativeExpr() {
394 SCEVCommExprs.erase(std::make_pair(getSCEVType(),
395 std::vector<SCEV*>(Operands.begin(),
400 unsigned getNumOperands() const { return Operands.size(); }
401 const SCEVHandle &getOperand(unsigned i) const {
402 assert(i < Operands.size() && "Operand index out of range!");
406 const std::vector<SCEVHandle> &getOperands() const { return Operands; }
407 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
408 op_iterator op_begin() const { return Operands.begin(); }
409 op_iterator op_end() const { return Operands.end(); }
412 virtual bool isLoopInvariant(const Loop *L) const {
413 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
414 if (!getOperand(i)->isLoopInvariant(L)) return false;
418 virtual bool hasComputableLoopEvolution(const Loop *L) const {
419 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
420 if (getOperand(i)->hasComputableLoopEvolution(L)) return true;
424 virtual const Type *getType() const { return getOperand(0)->getType(); }
426 virtual const char *getOperationStr() const = 0;
428 virtual void print(std::ostream &OS) const {
429 assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
430 const char *OpStr = getOperationStr();
431 OS << "(" << *Operands[0];
432 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
433 OS << OpStr << *Operands[i];
437 /// Methods for support type inquiry through isa, cast, and dyn_cast:
438 static inline bool classof(const SCEVCommutativeExpr *S) { return true; }
439 static inline bool classof(const SCEV *S) {
440 return S->getSCEVType() == scAddExpr ||
441 S->getSCEVType() == scMulExpr;
446 //===----------------------------------------------------------------------===//
447 // SCEVAddExpr - This node represents an addition of some number of SCEV's.
450 class SCEVAddExpr : public SCEVCommutativeExpr {
451 SCEVAddExpr(const std::vector<SCEVHandle> &ops)
452 : SCEVCommutativeExpr(scAddExpr, ops) {
456 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
458 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
459 std::vector<SCEVHandle> Ops;
465 static SCEVHandle get(const SCEVHandle &Op0, const SCEVHandle &Op1,
466 const SCEVHandle &Op2) {
467 std::vector<SCEVHandle> Ops;
474 virtual const char *getOperationStr() const { return " + "; }
476 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
477 Instruction *InsertPt);
479 /// Methods for support type inquiry through isa, cast, and dyn_cast:
480 static inline bool classof(const SCEVAddExpr *S) { return true; }
481 static inline bool classof(const SCEV *S) {
482 return S->getSCEVType() == scAddExpr;
487 //===----------------------------------------------------------------------===//
488 // SCEVMulExpr - This node represents multiplication of some number of SCEV's.
491 class SCEVMulExpr : public SCEVCommutativeExpr {
492 SCEVMulExpr(const std::vector<SCEVHandle> &ops)
493 : SCEVCommutativeExpr(scMulExpr, ops) {
497 static SCEVHandle get(std::vector<SCEVHandle> &Ops);
499 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
500 std::vector<SCEVHandle> Ops;
506 virtual const char *getOperationStr() const { return " * "; }
508 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
509 Instruction *InsertPt);
511 /// Methods for support type inquiry through isa, cast, and dyn_cast:
512 static inline bool classof(const SCEVMulExpr *S) { return true; }
513 static inline bool classof(const SCEV *S) {
514 return S->getSCEVType() == scMulExpr;
520 //===----------------------------------------------------------------------===//
521 // SCEVUDivExpr - This class represents a binary unsigned division operation.
525 // SCEVUDivs - Only allow the creation of one SCEVUDivExpr for any particular
526 // input. Don't use a SCEVHandle here, or else the object will never be
528 std::map<std::pair<SCEV*, SCEV*>, SCEVUDivExpr*> SCEVUDivs;
530 class SCEVUDivExpr : public SCEV {
532 SCEVUDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
533 : SCEV(scUDivExpr), LHS(lhs), RHS(rhs) {}
535 virtual ~SCEVUDivExpr() {
536 SCEVUDivs.erase(std::make_pair(LHS, RHS));
539 /// get method - This just gets and returns a new SCEVUDiv object.
541 static SCEVHandle get(const SCEVHandle &LHS, const SCEVHandle &RHS);
543 const SCEVHandle &getLHS() const { return LHS; }
544 const SCEVHandle &getRHS() const { return RHS; }
546 virtual bool isLoopInvariant(const Loop *L) const {
547 return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
550 virtual bool hasComputableLoopEvolution(const Loop *L) const {
551 return LHS->hasComputableLoopEvolution(L) &&
552 RHS->hasComputableLoopEvolution(L);
555 virtual const Type *getType() const {
556 const Type *Ty = LHS->getType();
557 if (Ty->isSigned()) Ty = Ty->getUnsignedVersion();
561 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
562 Instruction *InsertPt);
564 virtual void print(std::ostream &OS) const {
565 OS << "(" << *LHS << " /u " << *RHS << ")";
568 /// Methods for support type inquiry through isa, cast, and dyn_cast:
569 static inline bool classof(const SCEVUDivExpr *S) { return true; }
570 static inline bool classof(const SCEV *S) {
571 return S->getSCEVType() == scUDivExpr;
577 //===----------------------------------------------------------------------===//
579 // SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
580 // count of the specified loop.
582 // All operands of an AddRec are required to be loop invariant.
585 class SCEVAddRecExpr;
586 // SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any
587 // particular input. Don't use a SCEVHandle here, or else the object will
589 std::map<std::pair<const Loop *, std::vector<SCEV*> >,
590 SCEVAddRecExpr*> SCEVAddRecExprs;
592 class SCEVAddRecExpr : public SCEV {
593 std::vector<SCEVHandle> Operands;
596 SCEVAddRecExpr(const std::vector<SCEVHandle> &ops, const Loop *l)
597 : SCEV(scAddRecExpr), Operands(ops), L(l) {
598 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
599 assert(Operands[i]->isLoopInvariant(l) &&
600 "Operands of AddRec must be loop-invariant!");
603 SCEVAddRecExprs.erase(std::make_pair(L,
604 std::vector<SCEV*>(Operands.begin(),
608 static SCEVHandle get(const SCEVHandle &Start, const SCEVHandle &Step,
610 static SCEVHandle get(std::vector<SCEVHandle> &Operands,
612 static SCEVHandle get(const std::vector<SCEVHandle> &Operands,
614 std::vector<SCEVHandle> NewOp(Operands);
615 return get(NewOp, L);
618 typedef std::vector<SCEVHandle>::const_iterator op_iterator;
619 op_iterator op_begin() const { return Operands.begin(); }
620 op_iterator op_end() const { return Operands.end(); }
622 unsigned getNumOperands() const { return Operands.size(); }
623 const SCEVHandle &getOperand(unsigned i) const { return Operands[i]; }
624 const SCEVHandle &getStart() const { return Operands[0]; }
625 const Loop *getLoop() const { return L; }
628 /// getStepRecurrence - This method constructs and returns the recurrence
629 /// indicating how much this expression steps by. If this is a polynomial
630 /// of degree N, it returns a chrec of degree N-1.
631 SCEVHandle getStepRecurrence() const {
632 if (getNumOperands() == 2) return getOperand(1);
633 return SCEVAddRecExpr::get(std::vector<SCEVHandle>(op_begin()+1,op_end()),
637 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
638 if (L == QL) return true;
639 /// FIXME: What if the start or step value a recurrence for the specified
645 virtual bool isLoopInvariant(const Loop *QueryLoop) const {
646 // This recurrence is invariant w.r.t to QueryLoop iff QueryLoop doesn't
648 return !QueryLoop->contains(L->getHeader());
651 virtual const Type *getType() const { return Operands[0]->getType(); }
653 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
654 Instruction *InsertPt);
657 /// isAffine - Return true if this is an affine AddRec (i.e., it represents
658 /// an expressions A+B*x where A and B are loop invariant values.
659 bool isAffine() const {
660 // We know that the start value is invariant. This expression is thus
661 // affine iff the step is also invariant.
662 return getNumOperands() == 2;
665 /// isQuadratic - Return true if this is an quadratic AddRec (i.e., it
666 /// represents an expressions A+B*x+C*x^2 where A, B and C are loop
667 /// invariant values. This corresponds to an addrec of the form {L,+,M,+,N}
668 bool isQuadratic() const {
669 return getNumOperands() == 3;
672 /// evaluateAtIteration - Return the value of this chain of recurrences at
673 /// the specified iteration number.
674 SCEVHandle evaluateAtIteration(SCEVHandle It) const;
676 /// getNumIterationsInRange - Return the number of iterations of this loop
677 /// that produce values in the specified constant range. Another way of
678 /// looking at this is that it returns the first iteration number where the
679 /// value is not in the condition, thus computing the exit count. If the
680 /// iteration count can't be computed, an instance of SCEVCouldNotCompute is
682 SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
685 virtual void print(std::ostream &OS) const {
686 OS << "{" << *Operands[0];
687 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
688 OS << ",+," << *Operands[i];
689 OS << "}<" << L->getHeader()->getName() + ">";
692 /// Methods for support type inquiry through isa, cast, and dyn_cast:
693 static inline bool classof(const SCEVAddRecExpr *S) { return true; }
694 static inline bool classof(const SCEV *S) {
695 return S->getSCEVType() == scAddRecExpr;
701 //===----------------------------------------------------------------------===//
702 // SCEVUnknown - This means that we are dealing with an entirely unknown SCEV
703 // value, and only represent it as it's LLVM Value. This is the "bottom" value
708 // SCEVUnknowns - Only allow the creation of one SCEVUnknown for any
709 // particular value. Don't use a SCEVHandle here, or else the object will
711 std::map<Value*, SCEVUnknown*> SCEVUnknowns;
713 class SCEVUnknown : public SCEV {
715 SCEVUnknown(Value *v) : SCEV(scUnknown), V(v) {}
718 ~SCEVUnknown() { SCEVUnknowns.erase(V); }
720 /// get method - For SCEVUnknown, this just gets and returns a new
722 static SCEVHandle get(Value *V) {
723 if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
724 return SCEVConstant::get(CI);
725 SCEVUnknown *&Result = SCEVUnknowns[V];
726 if (Result == 0) Result = new SCEVUnknown(V);
730 Value *getValue() const { return V; }
732 Value *expandCodeFor(ScalarEvolutionRewriter &SER,
733 Instruction *InsertPt) {
737 virtual bool isLoopInvariant(const Loop *L) const {
738 // All non-instruction values are loop invariant. All instructions are
739 // loop invariant if they are not contained in the specified loop.
740 if (Instruction *I = dyn_cast<Instruction>(V))
741 return !L->contains(I->getParent());
745 virtual bool hasComputableLoopEvolution(const Loop *QL) const {
746 return false; // not computable
749 virtual const Type *getType() const { return V->getType(); }
751 virtual void print(std::ostream &OS) const {
752 WriteAsOperand(OS, V, false);
755 /// Methods for support type inquiry through isa, cast, and dyn_cast:
756 static inline bool classof(const SCEVUnknown *S) { return true; }
757 static inline bool classof(const SCEV *S) {
758 return S->getSCEVType() == scUnknown;
763 //===----------------------------------------------------------------------===//
764 // Simple SCEV method implementations
765 //===----------------------------------------------------------------------===//
767 /// getIntegerSCEV - Given an integer or FP type, create a constant for the
768 /// specified signed integer value and return a SCEV for the constant.
769 static SCEVHandle getIntegerSCEV(int Val, const Type *Ty) {
772 C = Constant::getNullValue(Ty);
773 else if (Ty->isFloatingPoint())
774 C = ConstantFP::get(Ty, Val);
775 else if (Ty->isSigned())
776 C = ConstantSInt::get(Ty, Val);
778 C = ConstantSInt::get(Ty->getSignedVersion(), Val);
779 C = ConstantExpr::getCast(C, Ty);
781 return SCEVUnknown::get(C);
784 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
785 /// input value to the specified type. If the type must be extended, it is zero
787 static SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty) {
788 const Type *SrcTy = V->getType();
789 assert(SrcTy->isInteger() && Ty->isInteger() &&
790 "Cannot truncate or zero extend with non-integer arguments!");
791 if (SrcTy->getPrimitiveSize() == Ty->getPrimitiveSize())
792 return V; // No conversion
793 if (SrcTy->getPrimitiveSize() > Ty->getPrimitiveSize())
794 return SCEVTruncateExpr::get(V, Ty);
795 return SCEVZeroExtendExpr::get(V, Ty);
798 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
800 static SCEVHandle getNegativeSCEV(const SCEVHandle &V) {
801 if (SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
802 return SCEVUnknown::get(ConstantExpr::getNeg(VC->getValue()));
804 return SCEVMulExpr::get(V, getIntegerSCEV(-1, V->getType()));
807 /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
809 static SCEVHandle getMinusSCEV(const SCEVHandle &LHS, const SCEVHandle &RHS) {
811 return SCEVAddExpr::get(LHS, getNegativeSCEV(RHS));
815 /// Binomial - Evaluate N!/((N-M)!*M!) . Note that N is often large and M is
816 /// often very small, so we try to reduce the number of N! terms we need to
817 /// evaluate by evaluating this as (N!/(N-M)!)/M!
818 static ConstantInt *Binomial(ConstantInt *N, unsigned M) {
819 uint64_t NVal = N->getRawValue();
820 uint64_t FirstTerm = 1;
821 for (unsigned i = 0; i != M; ++i)
824 unsigned MFactorial = 1;
828 Constant *Result = ConstantUInt::get(Type::ULongTy, FirstTerm/MFactorial);
829 Result = ConstantExpr::getCast(Result, N->getType());
830 assert(isa<ConstantInt>(Result) && "Cast of integer not folded??");
831 return cast<ConstantInt>(Result);
834 /// PartialFact - Compute V!/(V-NumSteps)!
835 static SCEVHandle PartialFact(SCEVHandle V, unsigned NumSteps) {
836 // Handle this case efficiently, it is common to have constant iteration
837 // counts while computing loop exit values.
838 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(V)) {
839 uint64_t Val = SC->getValue()->getRawValue();
841 for (; NumSteps; --NumSteps)
842 Result *= Val-(NumSteps-1);
843 Constant *Res = ConstantUInt::get(Type::ULongTy, Result);
844 return SCEVUnknown::get(ConstantExpr::getCast(Res, V->getType()));
847 const Type *Ty = V->getType();
849 return getIntegerSCEV(1, Ty);
851 SCEVHandle Result = V;
852 for (unsigned i = 1; i != NumSteps; ++i)
853 Result = SCEVMulExpr::get(Result, getMinusSCEV(V, getIntegerSCEV(i, Ty)));
858 /// evaluateAtIteration - Return the value of this chain of recurrences at
859 /// the specified iteration number. We can evaluate this recurrence by
860 /// multiplying each element in the chain by the binomial coefficient
861 /// corresponding to it. In other words, we can evaluate {A,+,B,+,C,+,D} as:
863 /// A*choose(It, 0) + B*choose(It, 1) + C*choose(It, 2) + D*choose(It, 3)
865 /// FIXME/VERIFY: I don't trust that this is correct in the face of overflow.
866 /// Is the binomial equation safe using modular arithmetic??
868 SCEVHandle SCEVAddRecExpr::evaluateAtIteration(SCEVHandle It) const {
869 SCEVHandle Result = getStart();
871 const Type *Ty = It->getType();
872 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
873 SCEVHandle BC = PartialFact(It, i);
875 SCEVHandle Val = SCEVUDivExpr::get(SCEVMulExpr::get(BC, getOperand(i)),
876 getIntegerSCEV(Divisor, Ty));
877 Result = SCEVAddExpr::get(Result, Val);
883 //===----------------------------------------------------------------------===//
884 // SCEV Expression folder implementations
885 //===----------------------------------------------------------------------===//
887 SCEVHandle SCEVTruncateExpr::get(const SCEVHandle &Op, const Type *Ty) {
888 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
889 return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty));
891 // If the input value is a chrec scev made out of constants, truncate
892 // all of the constants.
893 if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
894 std::vector<SCEVHandle> Operands;
895 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
896 // FIXME: This should allow truncation of other expression types!
897 if (isa<SCEVConstant>(AddRec->getOperand(i)))
898 Operands.push_back(get(AddRec->getOperand(i), Ty));
901 if (Operands.size() == AddRec->getNumOperands())
902 return SCEVAddRecExpr::get(Operands, AddRec->getLoop());
905 SCEVTruncateExpr *&Result = SCEVTruncates[std::make_pair(Op, Ty)];
906 if (Result == 0) Result = new SCEVTruncateExpr(Op, Ty);
910 SCEVHandle SCEVZeroExtendExpr::get(const SCEVHandle &Op, const Type *Ty) {
911 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
912 return SCEVUnknown::get(ConstantExpr::getCast(SC->getValue(), Ty));
914 // FIXME: If the input value is a chrec scev, and we can prove that the value
915 // did not overflow the old, smaller, value, we can zero extend all of the
916 // operands (often constants). This would allow analysis of something like
917 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
919 SCEVZeroExtendExpr *&Result = SCEVZeroExtends[std::make_pair(Op, Ty)];
920 if (Result == 0) Result = new SCEVZeroExtendExpr(Op, Ty);
924 // get - Get a canonical add expression, or something simpler if possible.
925 SCEVHandle SCEVAddExpr::get(std::vector<SCEVHandle> &Ops) {
926 assert(!Ops.empty() && "Cannot get empty add!");
927 if (Ops.size() == 1) return Ops[0];
929 // Sort by complexity, this groups all similar expression types together.
930 std::sort(Ops.begin(), Ops.end(), SCEVComplexityCompare());
932 // If there are any constants, fold them together.
934 if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
936 assert(Idx < Ops.size());
937 while (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
938 // We found two constants, fold them together!
939 Constant *Fold = ConstantExpr::getAdd(LHSC->getValue(), RHSC->getValue());
940 if (ConstantInt *CI = dyn_cast<ConstantInt>(Fold)) {
941 Ops[0] = SCEVConstant::get(CI);
942 Ops.erase(Ops.begin()+1); // Erase the folded element
943 if (Ops.size() == 1) return Ops[0];
945 // If we couldn't fold the expression, move to the next constant. Note
946 // that this is impossible to happen in practice because we always
947 // constant fold constant ints to constant ints.
952 // If we are left with a constant zero being added, strip it off.
953 if (cast<SCEVConstant>(Ops[0])->getValue()->isNullValue()) {
954 Ops.erase(Ops.begin());
959 if (Ops.size() == 1) return Ops[0];
961 // Okay, check to see if the same value occurs in the operand list twice. If
962 // so, merge them together into an multiply expression. Since we sorted the
963 // list, these values are required to be adjacent.
964 const Type *Ty = Ops[0]->getType();
965 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
966 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
967 // Found a match, merge the two values into a multiply, and add any
968 // remaining values to the result.
969 SCEVHandle Two = getIntegerSCEV(2, Ty);
970 SCEVHandle Mul = SCEVMulExpr::get(Ops[i], Two);
973 Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
975 return SCEVAddExpr::get(Ops);
978 // Okay, now we know the first non-constant operand. If there are add
979 // operands they would be next.
980 if (Idx < Ops.size()) {
981 bool DeletedAdd = false;
982 while (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
983 // If we have an add, expand the add operands onto the end of the operands
985 Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
986 Ops.erase(Ops.begin()+Idx);
990 // If we deleted at least one add, we added operands to the end of the list,
991 // and they are not necessarily sorted. Recurse to resort and resimplify
992 // any operands we just aquired.
997 // Skip over the add expression until we get to a multiply.
998 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1001 // If we are adding something to a multiply expression, make sure the
1002 // something is not already an operand of the multiply. If so, merge it into
1004 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
1005 SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
1006 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
1007 SCEV *MulOpSCEV = Mul->getOperand(MulOp);
1008 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
1009 if (MulOpSCEV == Ops[AddOp] &&
1010 (Mul->getNumOperands() != 2 || !isa<SCEVConstant>(MulOpSCEV))) {
1011 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
1012 SCEVHandle InnerMul = Mul->getOperand(MulOp == 0);
1013 if (Mul->getNumOperands() != 2) {
1014 // If the multiply has more than two operands, we must get the
1016 std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end());
1017 MulOps.erase(MulOps.begin()+MulOp);
1018 InnerMul = SCEVMulExpr::get(MulOps);
1020 SCEVHandle One = getIntegerSCEV(1, Ty);
1021 SCEVHandle AddOne = SCEVAddExpr::get(InnerMul, One);
1022 SCEVHandle OuterMul = SCEVMulExpr::get(AddOne, Ops[AddOp]);
1023 if (Ops.size() == 2) return OuterMul;
1025 Ops.erase(Ops.begin()+AddOp);
1026 Ops.erase(Ops.begin()+Idx-1);
1028 Ops.erase(Ops.begin()+Idx);
1029 Ops.erase(Ops.begin()+AddOp-1);
1031 Ops.push_back(OuterMul);
1032 return SCEVAddExpr::get(Ops);
1035 // Check this multiply against other multiplies being added together.
1036 for (unsigned OtherMulIdx = Idx+1;
1037 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1039 SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
1040 // If MulOp occurs in OtherMul, we can fold the two multiplies
1042 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1043 OMulOp != e; ++OMulOp)
1044 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1045 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
1046 SCEVHandle InnerMul1 = Mul->getOperand(MulOp == 0);
1047 if (Mul->getNumOperands() != 2) {
1048 std::vector<SCEVHandle> MulOps(Mul->op_begin(), Mul->op_end());
1049 MulOps.erase(MulOps.begin()+MulOp);
1050 InnerMul1 = SCEVMulExpr::get(MulOps);
1052 SCEVHandle InnerMul2 = OtherMul->getOperand(OMulOp == 0);
1053 if (OtherMul->getNumOperands() != 2) {
1054 std::vector<SCEVHandle> MulOps(OtherMul->op_begin(),
1055 OtherMul->op_end());
1056 MulOps.erase(MulOps.begin()+OMulOp);
1057 InnerMul2 = SCEVMulExpr::get(MulOps);
1059 SCEVHandle InnerMulSum = SCEVAddExpr::get(InnerMul1,InnerMul2);
1060 SCEVHandle OuterMul = SCEVMulExpr::get(MulOpSCEV, InnerMulSum);
1061 if (Ops.size() == 2) return OuterMul;
1062 Ops.erase(Ops.begin()+Idx);
1063 Ops.erase(Ops.begin()+OtherMulIdx-1);
1064 Ops.push_back(OuterMul);
1065 return SCEVAddExpr::get(Ops);
1071 // If there are any add recurrences in the operands list, see if any other
1072 // added values are loop invariant. If so, we can fold them into the
1074 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1077 // Scan over all recurrences, trying to fold loop invariants into them.
1078 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1079 // Scan all of the other operands to this add and add them to the vector if
1080 // they are loop invariant w.r.t. the recurrence.
1081 std::vector<SCEVHandle> LIOps;
1082 SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
1083 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1084 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1085 LIOps.push_back(Ops[i]);
1086 Ops.erase(Ops.begin()+i);
1090 // If we found some loop invariants, fold them into the recurrence.
1091 if (!LIOps.empty()) {
1092 // NLI + LI + { Start,+,Step} --> NLI + { LI+Start,+,Step }
1093 LIOps.push_back(AddRec->getStart());
1095 std::vector<SCEVHandle> AddRecOps(AddRec->op_begin(), AddRec->op_end());
1096 AddRecOps[0] = SCEVAddExpr::get(LIOps);
1098 SCEVHandle NewRec = SCEVAddRecExpr::get(AddRecOps, AddRec->getLoop());
1099 // If all of the other operands were loop invariant, we are done.
1100 if (Ops.size() == 1) return NewRec;
1102 // Otherwise, add the folded AddRec by the non-liv parts.
1103 for (unsigned i = 0;; ++i)
1104 if (Ops[i] == AddRec) {
1108 return SCEVAddExpr::get(Ops);
1111 // Okay, if there weren't any loop invariants to be folded, check to see if
1112 // there are multiple AddRec's with the same loop induction variable being
1113 // added together. If so, we can fold them.
1114 for (unsigned OtherIdx = Idx+1;
1115 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1116 if (OtherIdx != Idx) {
1117 SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
1118 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1119 // Other + {A,+,B} + {C,+,D} --> Other + {A+C,+,B+D}
1120 std::vector<SCEVHandle> NewOps(AddRec->op_begin(), AddRec->op_end());
1121 for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
1122 if (i >= NewOps.size()) {
1123 NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
1124 OtherAddRec->op_end());
1127 NewOps[i] = SCEVAddExpr::get(NewOps[i], OtherAddRec->getOperand(i));
1129 SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewOps, AddRec->getLoop());
1131 if (Ops.size() == 2) return NewAddRec;
1133 Ops.erase(Ops.begin()+Idx);
1134 Ops.erase(Ops.begin()+OtherIdx-1);
1135 Ops.push_back(NewAddRec);
1136 return SCEVAddExpr::get(Ops);
1140 // Otherwise couldn't fold anything into this recurrence. Move onto the
1144 // Okay, it looks like we really DO need an add expr. Check to see if we
1145 // already have one, otherwise create a new one.
1146 std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end());
1147 SCEVCommutativeExpr *&Result = SCEVCommExprs[std::make_pair(scAddExpr,
1149 if (Result == 0) Result = new SCEVAddExpr(Ops);
1154 SCEVHandle SCEVMulExpr::get(std::vector<SCEVHandle> &Ops) {
1155 assert(!Ops.empty() && "Cannot get empty mul!");
1157 // Sort by complexity, this groups all similar expression types together.
1158 std::sort(Ops.begin(), Ops.end(), SCEVComplexityCompare());
1160 // If there are any constants, fold them together.
1162 if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1164 // C1*(C2+V) -> C1*C2 + C1*V
1165 if (Ops.size() == 2)
1166 if (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
1167 if (Add->getNumOperands() == 2 &&
1168 isa<SCEVConstant>(Add->getOperand(0)))
1169 return SCEVAddExpr::get(SCEVMulExpr::get(LHSC, Add->getOperand(0)),
1170 SCEVMulExpr::get(LHSC, Add->getOperand(1)));
1174 while (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1175 // We found two constants, fold them together!
1176 Constant *Fold = ConstantExpr::getMul(LHSC->getValue(), RHSC->getValue());
1177 if (ConstantInt *CI = dyn_cast<ConstantInt>(Fold)) {
1178 Ops[0] = SCEVConstant::get(CI);
1179 Ops.erase(Ops.begin()+1); // Erase the folded element
1180 if (Ops.size() == 1) return Ops[0];
1182 // If we couldn't fold the expression, move to the next constant. Note
1183 // that this is impossible to happen in practice because we always
1184 // constant fold constant ints to constant ints.
1189 // If we are left with a constant one being multiplied, strip it off.
1190 if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1191 Ops.erase(Ops.begin());
1193 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isNullValue()) {
1194 // If we have a multiply of zero, it will always be zero.
1199 // Skip over the add expression until we get to a multiply.
1200 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1203 if (Ops.size() == 1)
1206 // If there are mul operands inline them all into this expression.
1207 if (Idx < Ops.size()) {
1208 bool DeletedMul = false;
1209 while (SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
1210 // If we have an mul, expand the mul operands onto the end of the operands
1212 Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
1213 Ops.erase(Ops.begin()+Idx);
1217 // If we deleted at least one mul, we added operands to the end of the list,
1218 // and they are not necessarily sorted. Recurse to resort and resimplify
1219 // any operands we just aquired.
1224 // If there are any add recurrences in the operands list, see if any other
1225 // added values are loop invariant. If so, we can fold them into the
1227 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1230 // Scan over all recurrences, trying to fold loop invariants into them.
1231 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1232 // Scan all of the other operands to this mul and add them to the vector if
1233 // they are loop invariant w.r.t. the recurrence.
1234 std::vector<SCEVHandle> LIOps;
1235 SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
1236 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1237 if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1238 LIOps.push_back(Ops[i]);
1239 Ops.erase(Ops.begin()+i);
1243 // If we found some loop invariants, fold them into the recurrence.
1244 if (!LIOps.empty()) {
1245 // NLI * LI * { Start,+,Step} --> NLI * { LI*Start,+,LI*Step }
1246 std::vector<SCEVHandle> NewOps;
1247 NewOps.reserve(AddRec->getNumOperands());
1248 if (LIOps.size() == 1) {
1249 SCEV *Scale = LIOps[0];
1250 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
1251 NewOps.push_back(SCEVMulExpr::get(Scale, AddRec->getOperand(i)));
1253 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
1254 std::vector<SCEVHandle> MulOps(LIOps);
1255 MulOps.push_back(AddRec->getOperand(i));
1256 NewOps.push_back(SCEVMulExpr::get(MulOps));
1260 SCEVHandle NewRec = SCEVAddRecExpr::get(NewOps, AddRec->getLoop());
1262 // If all of the other operands were loop invariant, we are done.
1263 if (Ops.size() == 1) return NewRec;
1265 // Otherwise, multiply the folded AddRec by the non-liv parts.
1266 for (unsigned i = 0;; ++i)
1267 if (Ops[i] == AddRec) {
1271 return SCEVMulExpr::get(Ops);
1274 // Okay, if there weren't any loop invariants to be folded, check to see if
1275 // there are multiple AddRec's with the same loop induction variable being
1276 // multiplied together. If so, we can fold them.
1277 for (unsigned OtherIdx = Idx+1;
1278 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1279 if (OtherIdx != Idx) {
1280 SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
1281 if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1282 // F * G --> {A,+,B} * {C,+,D} --> {A*C,+,F*D + G*B + B*D}
1283 SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
1284 SCEVHandle NewStart = SCEVMulExpr::get(F->getStart(),
1286 SCEVHandle B = F->getStepRecurrence();
1287 SCEVHandle D = G->getStepRecurrence();
1288 SCEVHandle NewStep = SCEVAddExpr::get(SCEVMulExpr::get(F, D),
1289 SCEVMulExpr::get(G, B),
1290 SCEVMulExpr::get(B, D));
1291 SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewStart, NewStep,
1293 if (Ops.size() == 2) return NewAddRec;
1295 Ops.erase(Ops.begin()+Idx);
1296 Ops.erase(Ops.begin()+OtherIdx-1);
1297 Ops.push_back(NewAddRec);
1298 return SCEVMulExpr::get(Ops);
1302 // Otherwise couldn't fold anything into this recurrence. Move onto the
1306 // Okay, it looks like we really DO need an mul expr. Check to see if we
1307 // already have one, otherwise create a new one.
1308 std::vector<SCEV*> SCEVOps(Ops.begin(), Ops.end());
1309 SCEVCommutativeExpr *&Result = SCEVCommExprs[std::make_pair(scMulExpr,
1311 if (Result == 0) Result = new SCEVMulExpr(Ops);
1315 SCEVHandle SCEVUDivExpr::get(const SCEVHandle &LHS, const SCEVHandle &RHS) {
1316 if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
1317 if (RHSC->getValue()->equalsInt(1))
1318 return LHS; // X /u 1 --> x
1319 if (RHSC->getValue()->isAllOnesValue())
1320 return getNegativeSCEV(LHS); // X /u -1 --> -x
1322 if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
1323 Constant *LHSCV = LHSC->getValue();
1324 Constant *RHSCV = RHSC->getValue();
1325 if (LHSCV->getType()->isSigned())
1326 LHSCV = ConstantExpr::getCast(LHSCV,
1327 LHSCV->getType()->getUnsignedVersion());
1328 if (RHSCV->getType()->isSigned())
1329 RHSCV = ConstantExpr::getCast(RHSCV, LHSCV->getType());
1330 return SCEVUnknown::get(ConstantExpr::getDiv(LHSCV, RHSCV));
1334 // FIXME: implement folding of (X*4)/4 when we know X*4 doesn't overflow.
1336 SCEVUDivExpr *&Result = SCEVUDivs[std::make_pair(LHS, RHS)];
1337 if (Result == 0) Result = new SCEVUDivExpr(LHS, RHS);
1342 /// SCEVAddRecExpr::get - Get a add recurrence expression for the
1343 /// specified loop. Simplify the expression as much as possible.
1344 SCEVHandle SCEVAddRecExpr::get(const SCEVHandle &Start,
1345 const SCEVHandle &Step, const Loop *L) {
1346 std::vector<SCEVHandle> Operands;
1347 Operands.push_back(Start);
1348 if (SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
1349 if (StepChrec->getLoop() == L) {
1350 Operands.insert(Operands.end(), StepChrec->op_begin(),
1351 StepChrec->op_end());
1352 return get(Operands, L);
1355 Operands.push_back(Step);
1356 return get(Operands, L);
1359 /// SCEVAddRecExpr::get - Get a add recurrence expression for the
1360 /// specified loop. Simplify the expression as much as possible.
1361 SCEVHandle SCEVAddRecExpr::get(std::vector<SCEVHandle> &Operands,
1363 if (Operands.size() == 1) return Operands[0];
1365 if (SCEVConstant *StepC = dyn_cast<SCEVConstant>(Operands.back()))
1366 if (StepC->getValue()->isNullValue()) {
1367 Operands.pop_back();
1368 return get(Operands, L); // { X,+,0 } --> X
1371 SCEVAddRecExpr *&Result =
1372 SCEVAddRecExprs[std::make_pair(L, std::vector<SCEV*>(Operands.begin(),
1374 if (Result == 0) Result = new SCEVAddRecExpr(Operands, L);
1379 //===----------------------------------------------------------------------===//
1380 // Non-trivial closed-form SCEV Expanders
1381 //===----------------------------------------------------------------------===//
1383 Value *SCEVTruncateExpr::expandCodeFor(ScalarEvolutionRewriter &SER,
1384 Instruction *InsertPt) {
1385 Value *V = SER.ExpandCodeFor(getOperand(), InsertPt);
1386 return new CastInst(V, getType(), "tmp.", InsertPt);
1389 Value *SCEVZeroExtendExpr::expandCodeFor(ScalarEvolutionRewriter &SER,
1390 Instruction *InsertPt) {
1391 Value *V = SER.ExpandCodeFor(getOperand(), InsertPt,
1392 getOperand()->getType()->getUnsignedVersion());
1393 return new CastInst(V, getType(), "tmp.", InsertPt);
1396 Value *SCEVAddExpr::expandCodeFor(ScalarEvolutionRewriter &SER,
1397 Instruction *InsertPt) {
1398 const Type *Ty = getType();
1399 Value *V = SER.ExpandCodeFor(getOperand(getNumOperands()-1), InsertPt, Ty);
1401 // Emit a bunch of add instructions
1402 for (int i = getNumOperands()-2; i >= 0; --i)
1403 V = BinaryOperator::create(Instruction::Add, V,
1404 SER.ExpandCodeFor(getOperand(i), InsertPt, Ty),
1409 Value *SCEVMulExpr::expandCodeFor(ScalarEvolutionRewriter &SER,
1410 Instruction *InsertPt) {
1411 const Type *Ty = getType();
1412 int FirstOp = 0; // Set if we should emit a subtract.
1413 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(getOperand(0)))
1414 if (SC->getValue()->isAllOnesValue())
1417 int i = getNumOperands()-2;
1418 Value *V = SER.ExpandCodeFor(getOperand(i+1), InsertPt, Ty);
1420 // Emit a bunch of multiply instructions
1421 for (; i >= FirstOp; --i)
1422 V = BinaryOperator::create(Instruction::Mul, V,
1423 SER.ExpandCodeFor(getOperand(i), InsertPt, Ty),
1425 // -1 * ... ---> 0 - ...
1427 V = BinaryOperator::create(Instruction::Sub, Constant::getNullValue(Ty), V,
1432 Value *SCEVUDivExpr::expandCodeFor(ScalarEvolutionRewriter &SER,
1433 Instruction *InsertPt) {
1434 const Type *Ty = getType();
1435 Value *LHS = SER.ExpandCodeFor(getLHS(), InsertPt, Ty);
1436 Value *RHS = SER.ExpandCodeFor(getRHS(), InsertPt, Ty);
1437 return BinaryOperator::create(Instruction::Div, LHS, RHS, "tmp.", InsertPt);
1440 Value *SCEVAddRecExpr::expandCodeFor(ScalarEvolutionRewriter &SER,
1441 Instruction *InsertPt) {
1442 const Type *Ty = getType();
1443 // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F}
1444 assert(Ty->isIntegral() && "Cannot expand fp recurrences yet!");
1446 // {X,+,F} --> X + {0,+,F}
1447 if (!isa<SCEVConstant>(getStart()) ||
1448 !cast<SCEVConstant>(getStart())->getValue()->isNullValue()) {
1449 Value *Start = SER.ExpandCodeFor(getStart(), InsertPt, Ty);
1450 std::vector<SCEVHandle> NewOps(op_begin(), op_end());
1451 NewOps[0] = getIntegerSCEV(0, getType());
1452 Value *Rest = SER.ExpandCodeFor(SCEVAddRecExpr::get(NewOps, getLoop()),
1453 InsertPt, getType());
1455 // FIXME: look for an existing add to use.
1456 return BinaryOperator::create(Instruction::Add, Rest, Start, "tmp.",
1460 // {0,+,1} --> Insert a canonical induction variable into the loop!
1461 if (getNumOperands() == 2 && getOperand(1) == getIntegerSCEV(1, getType())) {
1462 // Create and insert the PHI node for the induction variable in the
1464 BasicBlock *Header = getLoop()->getHeader();
1465 PHINode *PN = new PHINode(Ty, "indvar", Header->begin());
1466 PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader());
1468 pred_iterator HPI = pred_begin(Header);
1469 assert(HPI != pred_end(Header) && "Loop with zero preds???");
1470 if (!getLoop()->contains(*HPI)) ++HPI;
1471 assert(HPI != pred_end(Header) && getLoop()->contains(*HPI) &&
1472 "No backedge in loop?");
1474 // Insert a unit add instruction right before the terminator corresponding
1475 // to the back-edge.
1476 Constant *One = Ty->isFloatingPoint() ? (Constant*)ConstantFP::get(Ty, 1.0)
1477 : (Constant*)ConstantInt::get(Ty, 1);
1478 Instruction *Add = BinaryOperator::create(Instruction::Add, PN, One,
1480 (*HPI)->getTerminator());
1482 pred_iterator PI = pred_begin(Header);
1483 if (*PI == L->getLoopPreheader())
1485 PN->addIncoming(Add, *PI);
1489 // Get the canonical induction variable I for this loop.
1490 Value *I = SER.GetOrInsertCanonicalInductionVariable(getLoop(), Ty);
1492 if (getNumOperands() == 2) { // {0,+,F} --> i*F
1493 Value *F = SER.ExpandCodeFor(getOperand(1), InsertPt, Ty);
1494 return BinaryOperator::create(Instruction::Mul, I, F, "tmp.", InsertPt);
1497 // If this is a chain of recurrences, turn it into a closed form, using the
1498 // folders, then expandCodeFor the closed form. This allows the folders to
1499 // simplify the expression without having to build a bunch of special code
1500 // into this folder.
1501 SCEVHandle IH = SCEVUnknown::get(I); // Get I as a "symbolic" SCEV.
1503 SCEVHandle V = evaluateAtIteration(IH);
1504 //std::cerr << "Evaluated: " << *this << "\n to: " << *V << "\n";
1506 return SER.ExpandCodeFor(V, InsertPt, Ty);
1510 //===----------------------------------------------------------------------===//
1511 // ScalarEvolutionsImpl Definition and Implementation
1512 //===----------------------------------------------------------------------===//
1514 /// ScalarEvolutionsImpl - This class implements the main driver for the scalar
1518 struct ScalarEvolutionsImpl {
1519 /// F - The function we are analyzing.
1523 /// LI - The loop information for the function we are currently analyzing.
1527 /// UnknownValue - This SCEV is used to represent unknown trip counts and
1529 SCEVHandle UnknownValue;
1531 /// Scalars - This is a cache of the scalars we have analyzed so far.
1533 std::map<Value*, SCEVHandle> Scalars;
1535 /// IterationCounts - Cache the iteration count of the loops for this
1536 /// function as they are computed.
1537 std::map<const Loop*, SCEVHandle> IterationCounts;
1540 ScalarEvolutionsImpl(Function &f, LoopInfo &li)
1541 : F(f), LI(li), UnknownValue(new SCEVCouldNotCompute()) {}
1543 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
1544 /// expression and create a new one.
1545 SCEVHandle getSCEV(Value *V);
1547 /// getSCEVAtScope - Compute the value of the specified expression within
1548 /// the indicated loop (which may be null to indicate in no loop). If the
1549 /// expression cannot be evaluated, return UnknownValue itself.
1550 SCEVHandle getSCEVAtScope(SCEV *V, const Loop *L);
1553 /// hasLoopInvariantIterationCount - Return true if the specified loop has
1554 /// an analyzable loop-invariant iteration count.
1555 bool hasLoopInvariantIterationCount(const Loop *L);
1557 /// getIterationCount - If the specified loop has a predictable iteration
1558 /// count, return it. Note that it is not valid to call this method on a
1559 /// loop without a loop-invariant iteration count.
1560 SCEVHandle getIterationCount(const Loop *L);
1562 /// deleteInstructionFromRecords - This method should be called by the
1563 /// client before it removes an instruction from the program, to make sure
1564 /// that no dangling references are left around.
1565 void deleteInstructionFromRecords(Instruction *I);
1568 /// createSCEV - We know that there is no SCEV for the specified value.
1569 /// Analyze the expression.
1570 SCEVHandle createSCEV(Value *V);
1571 SCEVHandle createNodeForCast(CastInst *CI);
1573 /// createNodeForPHI - Provide the special handling we need to analyze PHI
1575 SCEVHandle createNodeForPHI(PHINode *PN);
1576 void UpdatePHIUserScalarEntries(Instruction *I, PHINode *PN,
1577 std::set<Instruction*> &UpdatedInsts);
1579 /// ComputeIterationCount - Compute the number of times the specified loop
1581 SCEVHandle ComputeIterationCount(const Loop *L);
1583 /// HowFarToZero - Return the number of times a backedge comparing the
1584 /// specified value to zero will execute. If not computable, return
1586 SCEVHandle HowFarToZero(SCEV *V, const Loop *L);
1588 /// HowFarToNonZero - Return the number of times a backedge checking the
1589 /// specified value for nonzero will execute. If not computable, return
1591 SCEVHandle HowFarToNonZero(SCEV *V, const Loop *L);
1595 //===----------------------------------------------------------------------===//
1596 // Basic SCEV Analysis and PHI Idiom Recognition Code
1599 /// deleteInstructionFromRecords - This method should be called by the
1600 /// client before it removes an instruction from the program, to make sure
1601 /// that no dangling references are left around.
1602 void ScalarEvolutionsImpl::deleteInstructionFromRecords(Instruction *I) {
1607 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
1608 /// expression and create a new one.
1609 SCEVHandle ScalarEvolutionsImpl::getSCEV(Value *V) {
1610 assert(V->getType() != Type::VoidTy && "Can't analyze void expressions!");
1612 std::map<Value*, SCEVHandle>::iterator I = Scalars.find(V);
1613 if (I != Scalars.end()) return I->second;
1614 SCEVHandle S = createSCEV(V);
1615 Scalars.insert(std::make_pair(V, S));
1620 /// UpdatePHIUserScalarEntries - After PHI node analysis, we have a bunch of
1621 /// entries in the scalar map that refer to the "symbolic" PHI value instead of
1622 /// the recurrence value. After we resolve the PHI we must loop over all of the
1623 /// using instructions that have scalar map entries and update them.
1624 void ScalarEvolutionsImpl::UpdatePHIUserScalarEntries(Instruction *I,
1626 std::set<Instruction*> &UpdatedInsts) {
1627 std::map<Value*, SCEVHandle>::iterator SI = Scalars.find(I);
1628 if (SI == Scalars.end()) return; // This scalar wasn't previous processed.
1629 if (UpdatedInsts.insert(I).second) {
1630 Scalars.erase(SI); // Remove the old entry
1631 getSCEV(I); // Calculate the new entry
1633 for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
1635 UpdatePHIUserScalarEntries(cast<Instruction>(*UI), PN, UpdatedInsts);
1640 /// createNodeForPHI - PHI nodes have two cases. Either the PHI node exists in
1641 /// a loop header, making it a potential recurrence, or it doesn't.
1643 SCEVHandle ScalarEvolutionsImpl::createNodeForPHI(PHINode *PN) {
1644 if (PN->getNumIncomingValues() == 2) // The loops have been canonicalized.
1645 if (const Loop *L = LI.getLoopFor(PN->getParent()))
1646 if (L->getHeader() == PN->getParent()) {
1647 // If it lives in the loop header, it has two incoming values, one
1648 // from outside the loop, and one from inside.
1649 unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
1650 unsigned BackEdge = IncomingEdge^1;
1652 // While we are analyzing this PHI node, handle its value symbolically.
1653 SCEVHandle SymbolicName = SCEVUnknown::get(PN);
1654 assert(Scalars.find(PN) == Scalars.end() &&
1655 "PHI node already processed?");
1656 Scalars.insert(std::make_pair(PN, SymbolicName));
1658 // Using this symbolic name for the PHI, analyze the value coming around
1660 SCEVHandle BEValue = getSCEV(PN->getIncomingValue(BackEdge));
1662 // NOTE: If BEValue is loop invariant, we know that the PHI node just
1663 // has a special value for the first iteration of the loop.
1665 // If the value coming around the backedge is an add with the symbolic
1666 // value we just inserted, then we found a simple induction variable!
1667 if (SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
1668 // If there is a single occurrence of the symbolic value, replace it
1669 // with a recurrence.
1670 unsigned FoundIndex = Add->getNumOperands();
1671 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
1672 if (Add->getOperand(i) == SymbolicName)
1673 if (FoundIndex == e) {
1678 if (FoundIndex != Add->getNumOperands()) {
1679 // Create an add with everything but the specified operand.
1680 std::vector<SCEVHandle> Ops;
1681 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
1682 if (i != FoundIndex)
1683 Ops.push_back(Add->getOperand(i));
1684 SCEVHandle Accum = SCEVAddExpr::get(Ops);
1686 // This is not a valid addrec if the step amount is varying each
1687 // loop iteration, but is not itself an addrec in this loop.
1688 if (Accum->isLoopInvariant(L) ||
1689 (isa<SCEVAddRecExpr>(Accum) &&
1690 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
1691 SCEVHandle StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
1692 SCEVHandle PHISCEV = SCEVAddRecExpr::get(StartVal, Accum, L);
1694 // Okay, for the entire analysis of this edge we assumed the PHI
1695 // to be symbolic. We now need to go back and update all of the
1696 // entries for the scalars that use the PHI (except for the PHI
1697 // itself) to use the new analyzed value instead of the "symbolic"
1699 Scalars.find(PN)->second = PHISCEV; // Update the PHI value
1700 std::set<Instruction*> UpdatedInsts;
1701 UpdatedInsts.insert(PN);
1702 for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end();
1704 UpdatePHIUserScalarEntries(cast<Instruction>(*UI), PN,
1711 return SymbolicName;
1714 // If it's not a loop phi, we can't handle it yet.
1715 return SCEVUnknown::get(PN);
1718 /// createNodeForCast - Handle the various forms of casts that we support.
1720 SCEVHandle ScalarEvolutionsImpl::createNodeForCast(CastInst *CI) {
1721 const Type *SrcTy = CI->getOperand(0)->getType();
1722 const Type *DestTy = CI->getType();
1724 // If this is a noop cast (ie, conversion from int to uint), ignore it.
1725 if (SrcTy->isLosslesslyConvertibleTo(DestTy))
1726 return getSCEV(CI->getOperand(0));
1728 if (SrcTy->isInteger() && DestTy->isInteger()) {
1729 // Otherwise, if this is a truncating integer cast, we can represent this
1731 if (SrcTy->getPrimitiveSize() > DestTy->getPrimitiveSize())
1732 return SCEVTruncateExpr::get(getSCEV(CI->getOperand(0)),
1733 CI->getType()->getUnsignedVersion());
1734 if (SrcTy->isUnsigned() &&
1735 SrcTy->getPrimitiveSize() > DestTy->getPrimitiveSize())
1736 return SCEVZeroExtendExpr::get(getSCEV(CI->getOperand(0)),
1737 CI->getType()->getUnsignedVersion());
1740 // If this is an sign or zero extending cast and we can prove that the value
1741 // will never overflow, we could do similar transformations.
1743 // Otherwise, we can't handle this cast!
1744 return SCEVUnknown::get(CI);
1748 /// createSCEV - We know that there is no SCEV for the specified value.
1749 /// Analyze the expression.
1751 SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) {
1752 if (Instruction *I = dyn_cast<Instruction>(V)) {
1753 switch (I->getOpcode()) {
1754 case Instruction::Add:
1755 return SCEVAddExpr::get(getSCEV(I->getOperand(0)),
1756 getSCEV(I->getOperand(1)));
1757 case Instruction::Mul:
1758 return SCEVMulExpr::get(getSCEV(I->getOperand(0)),
1759 getSCEV(I->getOperand(1)));
1760 case Instruction::Div:
1761 if (V->getType()->isInteger() && V->getType()->isUnsigned())
1762 return SCEVUDivExpr::get(getSCEV(I->getOperand(0)),
1763 getSCEV(I->getOperand(1)));
1766 case Instruction::Sub:
1767 return getMinusSCEV(getSCEV(I->getOperand(0)), getSCEV(I->getOperand(1)));
1769 case Instruction::Shl:
1770 // Turn shift left of a constant amount into a multiply.
1771 if (ConstantInt *SA = dyn_cast<ConstantInt>(I->getOperand(1))) {
1772 Constant *X = ConstantInt::get(V->getType(), 1);
1773 X = ConstantExpr::getShl(X, SA);
1774 return SCEVMulExpr::get(getSCEV(I->getOperand(0)), getSCEV(X));
1778 case Instruction::Shr:
1779 if (ConstantUInt *SA = dyn_cast<ConstantUInt>(I->getOperand(1)))
1780 if (V->getType()->isUnsigned()) {
1781 Constant *X = ConstantInt::get(V->getType(), 1);
1782 X = ConstantExpr::getShl(X, SA);
1783 return SCEVUDivExpr::get(getSCEV(I->getOperand(0)), getSCEV(X));
1787 case Instruction::Cast:
1788 return createNodeForCast(cast<CastInst>(I));
1790 case Instruction::PHI:
1791 return createNodeForPHI(cast<PHINode>(I));
1793 default: // We cannot analyze this expression.
1798 return SCEVUnknown::get(V);
1803 //===----------------------------------------------------------------------===//
1804 // Iteration Count Computation Code
1807 /// getIterationCount - If the specified loop has a predictable iteration
1808 /// count, return it. Note that it is not valid to call this method on a
1809 /// loop without a loop-invariant iteration count.
1810 SCEVHandle ScalarEvolutionsImpl::getIterationCount(const Loop *L) {
1811 std::map<const Loop*, SCEVHandle>::iterator I = IterationCounts.find(L);
1812 if (I == IterationCounts.end()) {
1813 SCEVHandle ItCount = ComputeIterationCount(L);
1814 I = IterationCounts.insert(std::make_pair(L, ItCount)).first;
1815 if (ItCount != UnknownValue) {
1816 assert(ItCount->isLoopInvariant(L) &&
1817 "Computed trip count isn't loop invariant for loop!");
1818 ++NumTripCountsComputed;
1819 } else if (isa<PHINode>(L->getHeader()->begin())) {
1820 // Only count loops that have phi nodes as not being computable.
1821 ++NumTripCountsNotComputed;
1827 /// ComputeIterationCount - Compute the number of times the specified loop
1829 SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
1830 // If the loop has a non-one exit block count, we can't analyze it.
1831 if (L->getExitBlocks().size() != 1) return UnknownValue;
1833 // Okay, there is one exit block. Try to find the condition that causes the
1834 // loop to be exited.
1835 BasicBlock *ExitBlock = L->getExitBlocks()[0];
1837 BasicBlock *ExitingBlock = 0;
1838 for (pred_iterator PI = pred_begin(ExitBlock), E = pred_end(ExitBlock);
1840 if (L->contains(*PI)) {
1841 if (ExitingBlock == 0)
1844 return UnknownValue; // More than one block exiting!
1846 assert(ExitingBlock && "No exits from loop, something is broken!");
1848 // Okay, we've computed the exiting block. See what condition causes us to
1851 // FIXME: we should be able to handle switch instructions (with a single exit)
1852 // FIXME: We should handle cast of int to bool as well
1853 BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
1854 if (ExitBr == 0) return UnknownValue;
1855 assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
1856 SetCondInst *ExitCond = dyn_cast<SetCondInst>(ExitBr->getCondition());
1857 if (ExitCond == 0) return UnknownValue;
1859 SCEVHandle LHS = getSCEV(ExitCond->getOperand(0));
1860 SCEVHandle RHS = getSCEV(ExitCond->getOperand(1));
1862 // Try to evaluate any dependencies out of the loop.
1863 SCEVHandle Tmp = getSCEVAtScope(LHS, L);
1864 if (!isa<SCEVCouldNotCompute>(Tmp)) LHS = Tmp;
1865 Tmp = getSCEVAtScope(RHS, L);
1866 if (!isa<SCEVCouldNotCompute>(Tmp)) RHS = Tmp;
1868 // If the condition was exit on true, convert the condition to exit on false.
1869 Instruction::BinaryOps Cond;
1870 if (ExitBr->getSuccessor(1) == ExitBlock)
1871 Cond = ExitCond->getOpcode();
1873 Cond = ExitCond->getInverseCondition();
1875 // At this point, we would like to compute how many iterations of the loop the
1876 // predicate will return true for these inputs.
1877 if (isa<SCEVConstant>(LHS) && !isa<SCEVConstant>(RHS)) {
1878 // If there is a constant, force it into the RHS.
1879 std::swap(LHS, RHS);
1880 Cond = SetCondInst::getSwappedCondition(Cond);
1883 // FIXME: think about handling pointer comparisons! i.e.:
1884 // while (P != P+100) ++P;
1886 // If we have a comparison of a chrec against a constant, try to use value
1887 // ranges to answer this query.
1888 if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
1889 if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
1890 if (AddRec->getLoop() == L) {
1891 // Form the comparison range using the constant of the correct type so
1892 // that the ConstantRange class knows to do a signed or unsigned
1894 ConstantInt *CompVal = RHSC->getValue();
1895 const Type *RealTy = ExitCond->getOperand(0)->getType();
1896 CompVal = dyn_cast<ConstantInt>(ConstantExpr::getCast(CompVal, RealTy));
1898 // Form the constant range.
1899 ConstantRange CompRange(Cond, CompVal);
1901 // Now that we have it, if it's signed, convert it to an unsigned
1903 if (CompRange.getLower()->getType()->isSigned()) {
1904 const Type *NewTy = RHSC->getValue()->getType();
1905 Constant *NewL = ConstantExpr::getCast(CompRange.getLower(), NewTy);
1906 Constant *NewU = ConstantExpr::getCast(CompRange.getUpper(), NewTy);
1907 CompRange = ConstantRange(NewL, NewU);
1910 SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange);
1911 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
1916 case Instruction::SetNE: // while (X != Y)
1917 // Convert to: while (X-Y != 0)
1918 if (LHS->getType()->isInteger())
1919 return HowFarToZero(getMinusSCEV(LHS, RHS), L);
1921 case Instruction::SetEQ:
1922 // Convert to: while (X-Y == 0) // while (X == Y)
1923 if (LHS->getType()->isInteger())
1924 return HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
1928 std::cerr << "ComputeIterationCount ";
1929 if (ExitCond->getOperand(0)->getType()->isUnsigned())
1930 std::cerr << "[unsigned] ";
1931 std::cerr << *LHS << " "
1932 << Instruction::getOpcodeName(Cond) << " " << *RHS << "\n";
1936 return UnknownValue;
1939 /// getSCEVAtScope - Compute the value of the specified expression within the
1940 /// indicated loop (which may be null to indicate in no loop). If the
1941 /// expression cannot be evaluated, return UnknownValue.
1942 SCEVHandle ScalarEvolutionsImpl::getSCEVAtScope(SCEV *V, const Loop *L) {
1943 // FIXME: this should be turned into a virtual method on SCEV!
1945 if (isa<SCEVConstant>(V) || isa<SCEVUnknown>(V)) return V;
1946 if (SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
1947 // Avoid performing the look-up in the common case where the specified
1948 // expression has no loop-variant portions.
1949 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
1950 SCEVHandle OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
1951 if (OpAtScope != Comm->getOperand(i)) {
1952 if (OpAtScope == UnknownValue) return UnknownValue;
1953 // Okay, at least one of these operands is loop variant but might be
1954 // foldable. Build a new instance of the folded commutative expression.
1955 std::vector<SCEVHandle> NewOps(Comm->op_begin(), Comm->op_begin()+i-1);
1956 NewOps.push_back(OpAtScope);
1958 for (++i; i != e; ++i) {
1959 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
1960 if (OpAtScope == UnknownValue) return UnknownValue;
1961 NewOps.push_back(OpAtScope);
1963 if (isa<SCEVAddExpr>(Comm))
1964 return SCEVAddExpr::get(NewOps);
1965 assert(isa<SCEVMulExpr>(Comm) && "Only know about add and mul!");
1966 return SCEVMulExpr::get(NewOps);
1969 // If we got here, all operands are loop invariant.
1973 if (SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(V)) {
1974 SCEVHandle LHS = getSCEVAtScope(UDiv->getLHS(), L);
1975 if (LHS == UnknownValue) return LHS;
1976 SCEVHandle RHS = getSCEVAtScope(UDiv->getRHS(), L);
1977 if (RHS == UnknownValue) return RHS;
1978 if (LHS == UDiv->getLHS() && RHS == UDiv->getRHS())
1979 return UDiv; // must be loop invariant
1980 return SCEVUDivExpr::get(LHS, RHS);
1983 // If this is a loop recurrence for a loop that does not contain L, then we
1984 // are dealing with the final value computed by the loop.
1985 if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
1986 if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
1987 // To evaluate this recurrence, we need to know how many times the AddRec
1988 // loop iterates. Compute this now.
1989 SCEVHandle IterationCount = getIterationCount(AddRec->getLoop());
1990 if (IterationCount == UnknownValue) return UnknownValue;
1991 IterationCount = getTruncateOrZeroExtend(IterationCount,
1994 // If the value is affine, simplify the expression evaluation to just
1995 // Start + Step*IterationCount.
1996 if (AddRec->isAffine())
1997 return SCEVAddExpr::get(AddRec->getStart(),
1998 SCEVMulExpr::get(IterationCount,
1999 AddRec->getOperand(1)));
2001 // Otherwise, evaluate it the hard way.
2002 return AddRec->evaluateAtIteration(IterationCount);
2004 return UnknownValue;
2007 //assert(0 && "Unknown SCEV type!");
2008 return UnknownValue;
2012 /// SolveQuadraticEquation - Find the roots of the quadratic equation for the
2013 /// given quadratic chrec {L,+,M,+,N}. This returns either the two roots (which
2014 /// might be the same) or two SCEVCouldNotCompute objects.
2016 static std::pair<SCEVHandle,SCEVHandle>
2017 SolveQuadraticEquation(const SCEVAddRecExpr *AddRec) {
2018 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
2019 SCEVConstant *L = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
2020 SCEVConstant *M = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
2021 SCEVConstant *N = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
2023 // We currently can only solve this if the coefficients are constants.
2024 if (!L || !M || !N) {
2025 SCEV *CNC = new SCEVCouldNotCompute();
2026 return std::make_pair(CNC, CNC);
2029 Constant *Two = ConstantInt::get(L->getValue()->getType(), 2);
2031 // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
2032 Constant *C = L->getValue();
2033 // The B coefficient is M-N/2
2034 Constant *B = ConstantExpr::getSub(M->getValue(),
2035 ConstantExpr::getDiv(N->getValue(),
2037 // The A coefficient is N/2
2038 Constant *A = ConstantExpr::getDiv(N->getValue(), Two);
2040 // Compute the B^2-4ac term.
2041 Constant *SqrtTerm =
2042 ConstantExpr::getMul(ConstantInt::get(C->getType(), 4),
2043 ConstantExpr::getMul(A, C));
2044 SqrtTerm = ConstantExpr::getSub(ConstantExpr::getMul(B, B), SqrtTerm);
2046 // Compute floor(sqrt(B^2-4ac))
2047 ConstantUInt *SqrtVal =
2048 cast<ConstantUInt>(ConstantExpr::getCast(SqrtTerm,
2049 SqrtTerm->getType()->getUnsignedVersion()));
2050 uint64_t SqrtValV = SqrtVal->getValue();
2051 uint64_t SqrtValV2 = (uint64_t)sqrt(SqrtValV);
2052 // The square root might not be precise for arbitrary 64-bit integer
2053 // values. Do some sanity checks to ensure it's correct.
2054 if (SqrtValV2*SqrtValV2 > SqrtValV ||
2055 (SqrtValV2+1)*(SqrtValV2+1) <= SqrtValV) {
2056 SCEV *CNC = new SCEVCouldNotCompute();
2057 return std::make_pair(CNC, CNC);
2060 SqrtVal = ConstantUInt::get(Type::ULongTy, SqrtValV2);
2061 SqrtTerm = ConstantExpr::getCast(SqrtVal, SqrtTerm->getType());
2063 Constant *NegB = ConstantExpr::getNeg(B);
2064 Constant *TwoA = ConstantExpr::getMul(A, Two);
2066 // The divisions must be performed as signed divisions.
2067 const Type *SignedTy = NegB->getType()->getSignedVersion();
2068 NegB = ConstantExpr::getCast(NegB, SignedTy);
2069 TwoA = ConstantExpr::getCast(TwoA, SignedTy);
2070 SqrtTerm = ConstantExpr::getCast(SqrtTerm, SignedTy);
2072 Constant *Solution1 =
2073 ConstantExpr::getDiv(ConstantExpr::getAdd(NegB, SqrtTerm), TwoA);
2074 Constant *Solution2 =
2075 ConstantExpr::getDiv(ConstantExpr::getSub(NegB, SqrtTerm), TwoA);
2076 return std::make_pair(SCEVUnknown::get(Solution1),
2077 SCEVUnknown::get(Solution2));
2080 /// HowFarToZero - Return the number of times a backedge comparing the specified
2081 /// value to zero will execute. If not computable, return UnknownValue
2082 SCEVHandle ScalarEvolutionsImpl::HowFarToZero(SCEV *V, const Loop *L) {
2083 // If the value is a constant
2084 if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
2085 // If the value is already zero, the branch will execute zero times.
2086 if (C->getValue()->isNullValue()) return C;
2087 return UnknownValue; // Otherwise it will loop infinitely.
2090 SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
2091 if (!AddRec || AddRec->getLoop() != L)
2092 return UnknownValue;
2094 if (AddRec->isAffine()) {
2095 // If this is an affine expression the execution count of this branch is
2098 // (0 - Start/Step) iff Start % Step == 0
2100 // Get the initial value for the loop.
2101 SCEVHandle Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
2102 SCEVHandle Step = AddRec->getOperand(1);
2104 Step = getSCEVAtScope(Step, L->getParentLoop());
2106 // Figure out if Start % Step == 0.
2107 // FIXME: We should add DivExpr and RemExpr operations to our AST.
2108 if (SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
2109 if (StepC->getValue()->equalsInt(1)) // N % 1 == 0
2110 return getNegativeSCEV(Start); // 0 - Start/1 == -Start
2111 if (StepC->getValue()->isAllOnesValue()) // N % -1 == 0
2112 return Start; // 0 - Start/-1 == Start
2114 // Check to see if Start is divisible by SC with no remainder.
2115 if (SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) {
2116 ConstantInt *StartCC = StartC->getValue();
2117 Constant *StartNegC = ConstantExpr::getNeg(StartCC);
2118 Constant *Rem = ConstantExpr::getRem(StartNegC, StepC->getValue());
2119 if (Rem->isNullValue()) {
2120 Constant *Result =ConstantExpr::getDiv(StartNegC,StepC->getValue());
2121 return SCEVUnknown::get(Result);
2125 } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
2126 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
2127 // the quadratic equation to solve it.
2128 std::pair<SCEVHandle,SCEVHandle> Roots = SolveQuadraticEquation(AddRec);
2129 SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
2130 SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
2133 std::cerr << "HFTZ: " << *V << " - sol#1: " << *R1
2134 << " sol#2: " << *R2 << "\n";
2136 // Pick the smallest positive root value.
2137 assert(R1->getType()->isUnsigned()&&"Didn't canonicalize to unsigned?");
2138 if (ConstantBool *CB =
2139 dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(),
2141 if (CB != ConstantBool::True)
2142 std::swap(R1, R2); // R1 is the minimum root now.
2144 // We can only use this value if the chrec ends up with an exact zero
2145 // value at this index. When solving for "X*X != 5", for example, we
2146 // should not accept a root of 2.
2147 SCEVHandle Val = AddRec->evaluateAtIteration(R1);
2148 if (SCEVConstant *EvalVal = dyn_cast<SCEVConstant>(Val))
2149 if (EvalVal->getValue()->isNullValue())
2150 return R1; // We found a quadratic root!
2155 return UnknownValue;
2158 /// HowFarToNonZero - Return the number of times a backedge checking the
2159 /// specified value for nonzero will execute. If not computable, return
2161 SCEVHandle ScalarEvolutionsImpl::HowFarToNonZero(SCEV *V, const Loop *L) {
2162 // Loops that look like: while (X == 0) are very strange indeed. We don't
2163 // handle them yet except for the trivial case. This could be expanded in the
2164 // future as needed.
2166 // If the value is a constant, check to see if it is known to be non-zero
2167 // already. If so, the backedge will execute zero times.
2168 if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
2169 Constant *Zero = Constant::getNullValue(C->getValue()->getType());
2170 Constant *NonZero = ConstantExpr::getSetNE(C->getValue(), Zero);
2171 if (NonZero == ConstantBool::True)
2172 return getSCEV(Zero);
2173 return UnknownValue; // Otherwise it will loop infinitely.
2176 // We could implement others, but I really doubt anyone writes loops like
2177 // this, and if they did, they would already be constant folded.
2178 return UnknownValue;
2181 static ConstantInt *
2182 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, Constant *C) {
2183 SCEVHandle InVal = SCEVConstant::get(cast<ConstantInt>(C));
2184 SCEVHandle Val = AddRec->evaluateAtIteration(InVal);
2185 assert(isa<SCEVConstant>(Val) &&
2186 "Evaluation of SCEV at constant didn't fold correctly?");
2187 return cast<SCEVConstant>(Val)->getValue();
2191 /// getNumIterationsInRange - Return the number of iterations of this loop that
2192 /// produce values in the specified constant range. Another way of looking at
2193 /// this is that it returns the first iteration number where the value is not in
2194 /// the condition, thus computing the exit count. If the iteration count can't
2195 /// be computed, an instance of SCEVCouldNotCompute is returned.
2196 SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range) const {
2197 if (Range.isFullSet()) // Infinite loop.
2198 return new SCEVCouldNotCompute();
2200 // If the start is a non-zero constant, shift the range to simplify things.
2201 if (SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
2202 if (!SC->getValue()->isNullValue()) {
2203 std::vector<SCEVHandle> Operands(op_begin(), op_end());
2204 Operands[0] = getIntegerSCEV(0, SC->getType());
2205 SCEVHandle Shifted = SCEVAddRecExpr::get(Operands, getLoop());
2206 if (SCEVAddRecExpr *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
2207 return ShiftedAddRec->getNumIterationsInRange(
2208 Range.subtract(SC->getValue()));
2209 // This is strange and shouldn't happen.
2210 return new SCEVCouldNotCompute();
2213 // The only time we can solve this is when we have all constant indices.
2214 // Otherwise, we cannot determine the overflow conditions.
2215 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2216 if (!isa<SCEVConstant>(getOperand(i)))
2217 return new SCEVCouldNotCompute();
2220 // Okay at this point we know that all elements of the chrec are constants and
2221 // that the start element is zero.
2223 // First check to see if the range contains zero. If not, the first
2225 ConstantInt *Zero = ConstantInt::get(getType(), 0);
2226 if (!Range.contains(Zero)) return SCEVConstant::get(Zero);
2229 // If this is an affine expression then we have this situation:
2230 // Solve {0,+,A} in Range === Ax in Range
2232 // Since we know that zero is in the range, we know that the upper value of
2233 // the range must be the first possible exit value. Also note that we
2234 // already checked for a full range.
2235 ConstantInt *Upper = cast<ConstantInt>(Range.getUpper());
2236 ConstantInt *A = cast<SCEVConstant>(getOperand(1))->getValue();
2237 ConstantInt *One = ConstantInt::get(getType(), 1);
2239 // The exit value should be (Upper+A-1)/A.
2240 Constant *ExitValue = Upper;
2242 ExitValue = ConstantExpr::getSub(ConstantExpr::getAdd(Upper, A), One);
2243 ExitValue = ConstantExpr::getDiv(ExitValue, A);
2245 assert(isa<ConstantInt>(ExitValue) &&
2246 "Constant folding of integers not implemented?");
2248 // Evaluate at the exit value. If we really did fall out of the valid
2249 // range, then we computed our trip count, otherwise wrap around or other
2250 // things must have happened.
2251 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue);
2252 if (Range.contains(Val))
2253 return new SCEVCouldNotCompute(); // Something strange happened
2255 // Ensure that the previous value is in the range. This is a sanity check.
2256 assert(Range.contains(EvaluateConstantChrecAtConstant(this,
2257 ConstantExpr::getSub(ExitValue, One))) &&
2258 "Linear scev computation is off in a bad way!");
2259 return SCEVConstant::get(cast<ConstantInt>(ExitValue));
2260 } else if (isQuadratic()) {
2261 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
2262 // quadratic equation to solve it. To do this, we must frame our problem in
2263 // terms of figuring out when zero is crossed, instead of when
2264 // Range.getUpper() is crossed.
2265 std::vector<SCEVHandle> NewOps(op_begin(), op_end());
2266 NewOps[0] = getNegativeSCEV(SCEVUnknown::get(Range.getUpper()));
2267 SCEVHandle NewAddRec = SCEVAddRecExpr::get(NewOps, getLoop());
2269 // Next, solve the constructed addrec
2270 std::pair<SCEVHandle,SCEVHandle> Roots =
2271 SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec));
2272 SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
2273 SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
2275 // Pick the smallest positive root value.
2276 assert(R1->getType()->isUnsigned() && "Didn't canonicalize to unsigned?");
2277 if (ConstantBool *CB =
2278 dyn_cast<ConstantBool>(ConstantExpr::getSetLT(R1->getValue(),
2280 if (CB != ConstantBool::True)
2281 std::swap(R1, R2); // R1 is the minimum root now.
2283 // Make sure the root is not off by one. The returned iteration should
2284 // not be in the range, but the previous one should be. When solving
2285 // for "X*X < 5", for example, we should not return a root of 2.
2286 ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
2288 if (Range.contains(R1Val)) {
2289 // The next iteration must be out of the range...
2291 ConstantExpr::getAdd(R1->getValue(),
2292 ConstantInt::get(R1->getType(), 1));
2294 R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
2295 if (!Range.contains(R1Val))
2296 return SCEVUnknown::get(NextVal);
2297 return new SCEVCouldNotCompute(); // Something strange happened
2300 // If R1 was not in the range, then it is a good return value. Make
2301 // sure that R1-1 WAS in the range though, just in case.
2303 ConstantExpr::getSub(R1->getValue(),
2304 ConstantInt::get(R1->getType(), 1));
2305 R1Val = EvaluateConstantChrecAtConstant(this, NextVal);
2306 if (Range.contains(R1Val))
2308 return new SCEVCouldNotCompute(); // Something strange happened
2313 // Fallback, if this is a general polynomial, figure out the progression
2314 // through brute force: evaluate until we find an iteration that fails the
2315 // test. This is likely to be slow, but getting an accurate trip count is
2316 // incredibly important, we will be able to simplify the exit test a lot, and
2317 // we are almost guaranteed to get a trip count in this case.
2318 ConstantInt *TestVal = ConstantInt::get(getType(), 0);
2319 ConstantInt *One = ConstantInt::get(getType(), 1);
2320 ConstantInt *EndVal = TestVal; // Stop when we wrap around.
2322 ++NumBruteForceEvaluations;
2323 SCEVHandle Val = evaluateAtIteration(SCEVConstant::get(TestVal));
2324 if (!isa<SCEVConstant>(Val)) // This shouldn't happen.
2325 return new SCEVCouldNotCompute();
2327 // Check to see if we found the value!
2328 if (!Range.contains(cast<SCEVConstant>(Val)->getValue()))
2329 return SCEVConstant::get(TestVal);
2331 // Increment to test the next index.
2332 TestVal = cast<ConstantInt>(ConstantExpr::getAdd(TestVal, One));
2333 } while (TestVal != EndVal);
2335 return new SCEVCouldNotCompute();
2340 //===----------------------------------------------------------------------===//
2341 // ScalarEvolution Class Implementation
2342 //===----------------------------------------------------------------------===//
2344 bool ScalarEvolution::runOnFunction(Function &F) {
2345 Impl = new ScalarEvolutionsImpl(F, getAnalysis<LoopInfo>());
2349 void ScalarEvolution::releaseMemory() {
2350 delete (ScalarEvolutionsImpl*)Impl;
2354 void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
2355 AU.setPreservesAll();
2356 AU.addRequiredID(LoopSimplifyID);
2357 AU.addRequiredTransitive<LoopInfo>();
2360 SCEVHandle ScalarEvolution::getSCEV(Value *V) const {
2361 return ((ScalarEvolutionsImpl*)Impl)->getSCEV(V);
2364 SCEVHandle ScalarEvolution::getIterationCount(const Loop *L) const {
2365 return ((ScalarEvolutionsImpl*)Impl)->getIterationCount(L);
2368 bool ScalarEvolution::hasLoopInvariantIterationCount(const Loop *L) const {
2369 return !isa<SCEVCouldNotCompute>(getIterationCount(L));
2372 SCEVHandle ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) const {
2373 return ((ScalarEvolutionsImpl*)Impl)->getSCEVAtScope(getSCEV(V), L);
2376 void ScalarEvolution::deleteInstructionFromRecords(Instruction *I) const {
2377 return ((ScalarEvolutionsImpl*)Impl)->deleteInstructionFromRecords(I);
2381 /// shouldSubstituteIndVar - Return true if we should perform induction variable
2382 /// substitution for this variable. This is a hack because we don't have a
2383 /// strength reduction pass yet. When we do we will promote all vars, because
2384 /// we can strength reduce them later as desired.
2385 bool ScalarEvolution::shouldSubstituteIndVar(const SCEV *S) const {
2386 // Don't substitute high degree polynomials.
2387 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S))
2388 if (AddRec->getNumOperands() > 3) return false;
2393 static void PrintLoopInfo(std::ostream &OS, const ScalarEvolution *SE,
2395 // Print all inner loops first
2396 for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
2397 PrintLoopInfo(OS, SE, *I);
2399 std::cerr << "Loop " << L->getHeader()->getName() << ": ";
2400 if (L->getExitBlocks().size() != 1)
2401 std::cerr << "<multiple exits> ";
2403 if (SE->hasLoopInvariantIterationCount(L)) {
2404 std::cerr << *SE->getIterationCount(L) << " iterations! ";
2406 std::cerr << "Unpredictable iteration count. ";
2412 void ScalarEvolution::print(std::ostream &OS) const {
2413 Function &F = ((ScalarEvolutionsImpl*)Impl)->F;
2414 LoopInfo &LI = ((ScalarEvolutionsImpl*)Impl)->LI;
2416 OS << "Classifying expressions for: " << F.getName() << "\n";
2417 for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
2418 if ((*I)->getType()->isInteger()) {
2421 SCEVHandle SV = getSCEV(*I);
2425 if ((*I)->getType()->isIntegral()) {
2426 ConstantRange Bounds = SV->getValueRange();
2427 if (!Bounds.isFullSet())
2428 OS << "Bounds: " << Bounds << " ";
2431 if (const Loop *L = LI.getLoopFor((*I)->getParent())) {
2433 SCEVHandle ExitValue = getSCEVAtScope(*I, L->getParentLoop());
2434 if (isa<SCEVCouldNotCompute>(ExitValue)) {
2435 OS << "<<Unknown>>";
2445 OS << "Determining loop execution counts for: " << F.getName() << "\n";
2446 for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
2447 PrintLoopInfo(OS, this, *I);
2450 //===----------------------------------------------------------------------===//
2451 // ScalarEvolutionRewriter Class Implementation
2452 //===----------------------------------------------------------------------===//
2454 Value *ScalarEvolutionRewriter::
2455 GetOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty) {
2456 assert((Ty->isInteger() || Ty->isFloatingPoint()) &&
2457 "Can only insert integer or floating point induction variables!");
2459 // Check to see if we already inserted one.
2460 SCEVHandle H = SCEVAddRecExpr::get(getIntegerSCEV(0, Ty),
2461 getIntegerSCEV(1, Ty), L);
2462 return ExpandCodeFor(H, 0, Ty);
2465 /// ExpandCodeFor - Insert code to directly compute the specified SCEV
2466 /// expression into the program. The inserted code is inserted into the
2467 /// specified block.
2468 Value *ScalarEvolutionRewriter::ExpandCodeFor(SCEVHandle SH,
2469 Instruction *InsertPt,
2471 std::map<SCEVHandle, Value*>::iterator ExistVal =InsertedExpressions.find(SH);
2473 if (ExistVal != InsertedExpressions.end()) {
2474 V = ExistVal->second;
2476 // Ask the recurrence object to expand the code for itself.
2477 V = SH->expandCodeFor(*this, InsertPt);
2478 // Cache the generated result.
2479 InsertedExpressions.insert(std::make_pair(SH, V));
2482 if (Ty == 0 || V->getType() == Ty)
2484 if (Constant *C = dyn_cast<Constant>(V))
2485 return ConstantExpr::getCast(C, Ty);
2486 else if (Instruction *I = dyn_cast<Instruction>(V)) {
2487 // FIXME: check to see if there is already a cast!
2488 BasicBlock::iterator IP = I; ++IP;
2489 if (InvokeInst *II = dyn_cast<InvokeInst>(I))
2490 IP = II->getNormalDest()->begin();
2491 while (isa<PHINode>(IP)) ++IP;
2492 return new CastInst(V, Ty, V->getName(), IP);
2494 // FIXME: check to see if there is already a cast!
2495 return new CastInst(V, Ty, V->getName(), InsertPt);