Add a properlyDominates member function to ScalarEvolution.
[oota-llvm.git] / lib / Analysis / ScalarEvolution.cpp
1 //===- ScalarEvolution.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 analysis
11 // engine, which is used primarily to analyze expressions involving induction
12 // variables in loops.
13 //
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. We only create one SCEV of a particular shape, so
18 // pointer-comparisons for equality are legal.
19 //
20 // One important aspect of the SCEV objects is that they are never cyclic, even
21 // if there is a cycle in the dataflow for an expression (ie, a PHI node).  If
22 // the PHI node is one of the idioms that we can represent (e.g., a polynomial
23 // recurrence) then we represent it directly as a recurrence node, otherwise we
24 // represent it as a SCEVUnknown node.
25 //
26 // In addition to being able to represent expressions of various types, we also
27 // have folders that are used to build the *canonical* representation for a
28 // particular expression.  These folders are capable of using a variety of
29 // rewrite rules to simplify the expressions.
30 //
31 // Once the folders are defined, we can implement the more interesting
32 // higher-level code, such as the code that recognizes PHI nodes of various
33 // types, computes the execution count of a loop, etc.
34 //
35 // TODO: We should use these routines and value representations to implement
36 // dependence analysis!
37 //
38 //===----------------------------------------------------------------------===//
39 //
40 // There are several good references for the techniques used in this analysis.
41 //
42 //  Chains of recurrences -- a method to expedite the evaluation
43 //  of closed-form functions
44 //  Olaf Bachmann, Paul S. Wang, Eugene V. Zima
45 //
46 //  On computational properties of chains of recurrences
47 //  Eugene V. Zima
48 //
49 //  Symbolic Evaluation of Chains of Recurrences for Loop Optimization
50 //  Robert A. van Engelen
51 //
52 //  Efficient Symbolic Analysis for Optimizing Compilers
53 //  Robert A. van Engelen
54 //
55 //  Using the chains of recurrences algebra for data dependence testing and
56 //  induction variable substitution
57 //  MS Thesis, Johnie Birch
58 //
59 //===----------------------------------------------------------------------===//
60
61 #define DEBUG_TYPE "scalar-evolution"
62 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
63 #include "llvm/Constants.h"
64 #include "llvm/DerivedTypes.h"
65 #include "llvm/GlobalVariable.h"
66 #include "llvm/GlobalAlias.h"
67 #include "llvm/Instructions.h"
68 #include "llvm/LLVMContext.h"
69 #include "llvm/Operator.h"
70 #include "llvm/Analysis/ConstantFolding.h"
71 #include "llvm/Analysis/Dominators.h"
72 #include "llvm/Analysis/LoopInfo.h"
73 #include "llvm/Analysis/ValueTracking.h"
74 #include "llvm/Assembly/Writer.h"
75 #include "llvm/Target/TargetData.h"
76 #include "llvm/Support/CommandLine.h"
77 #include "llvm/Support/Compiler.h"
78 #include "llvm/Support/ConstantRange.h"
79 #include "llvm/Support/ErrorHandling.h"
80 #include "llvm/Support/GetElementPtrTypeIterator.h"
81 #include "llvm/Support/InstIterator.h"
82 #include "llvm/Support/MathExtras.h"
83 #include "llvm/Support/raw_ostream.h"
84 #include "llvm/ADT/Statistic.h"
85 #include "llvm/ADT/STLExtras.h"
86 #include "llvm/ADT/SmallPtrSet.h"
87 #include <algorithm>
88 using namespace llvm;
89
90 STATISTIC(NumArrayLenItCounts,
91           "Number of trip counts computed with array length");
92 STATISTIC(NumTripCountsComputed,
93           "Number of loops with predictable loop counts");
94 STATISTIC(NumTripCountsNotComputed,
95           "Number of loops without predictable loop counts");
96 STATISTIC(NumBruteForceTripCountsComputed,
97           "Number of loops with trip counts computed by force");
98
99 static cl::opt<unsigned>
100 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
101                         cl::desc("Maximum number of iterations SCEV will "
102                                  "symbolically execute a constant "
103                                  "derived loop"),
104                         cl::init(100));
105
106 static RegisterPass<ScalarEvolution>
107 R("scalar-evolution", "Scalar Evolution Analysis", false, true);
108 char ScalarEvolution::ID = 0;
109
110 //===----------------------------------------------------------------------===//
111 //                           SCEV class definitions
112 //===----------------------------------------------------------------------===//
113
114 //===----------------------------------------------------------------------===//
115 // Implementation of the SCEV class.
116 //
117
118 SCEV::~SCEV() {}
119
120 void SCEV::dump() const {
121   print(errs());
122   errs() << '\n';
123 }
124
125 bool SCEV::isZero() const {
126   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
127     return SC->getValue()->isZero();
128   return false;
129 }
130
131 bool SCEV::isOne() const {
132   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
133     return SC->getValue()->isOne();
134   return false;
135 }
136
137 bool SCEV::isAllOnesValue() const {
138   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
139     return SC->getValue()->isAllOnesValue();
140   return false;
141 }
142
143 SCEVCouldNotCompute::SCEVCouldNotCompute() :
144   SCEV(FoldingSetNodeID(), scCouldNotCompute) {}
145
146 bool SCEVCouldNotCompute::isLoopInvariant(const Loop *L) const {
147   llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
148   return false;
149 }
150
151 const Type *SCEVCouldNotCompute::getType() const {
152   llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
153   return 0;
154 }
155
156 bool SCEVCouldNotCompute::hasComputableLoopEvolution(const Loop *L) const {
157   llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
158   return false;
159 }
160
161 bool SCEVCouldNotCompute::hasOperand(const SCEV *) const {
162   llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
163   return false;
164 }
165
166 void SCEVCouldNotCompute::print(raw_ostream &OS) const {
167   OS << "***COULDNOTCOMPUTE***";
168 }
169
170 bool SCEVCouldNotCompute::classof(const SCEV *S) {
171   return S->getSCEVType() == scCouldNotCompute;
172 }
173
174 const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
175   FoldingSetNodeID ID;
176   ID.AddInteger(scConstant);
177   ID.AddPointer(V);
178   void *IP = 0;
179   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
180   SCEV *S = SCEVAllocator.Allocate<SCEVConstant>();
181   new (S) SCEVConstant(ID, V);
182   UniqueSCEVs.InsertNode(S, IP);
183   return S;
184 }
185
186 const SCEV *ScalarEvolution::getConstant(const APInt& Val) {
187   return getConstant(ConstantInt::get(getContext(), Val));
188 }
189
190 const SCEV *
191 ScalarEvolution::getConstant(const Type *Ty, uint64_t V, bool isSigned) {
192   return getConstant(
193     ConstantInt::get(cast<IntegerType>(Ty), V, isSigned));
194 }
195
196 const Type *SCEVConstant::getType() const { return V->getType(); }
197
198 void SCEVConstant::print(raw_ostream &OS) const {
199   WriteAsOperand(OS, V, false);
200 }
201
202 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeID &ID,
203                            unsigned SCEVTy, const SCEV *op, const Type *ty)
204   : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
205
206 bool SCEVCastExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
207   return Op->dominates(BB, DT);
208 }
209
210 bool SCEVCastExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
211   return Op->properlyDominates(BB, DT);
212 }
213
214 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeID &ID,
215                                    const SCEV *op, const Type *ty)
216   : SCEVCastExpr(ID, scTruncate, op, ty) {
217   assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
218          (Ty->isInteger() || isa<PointerType>(Ty)) &&
219          "Cannot truncate non-integer value!");
220 }
221
222 void SCEVTruncateExpr::print(raw_ostream &OS) const {
223   OS << "(trunc " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
224 }
225
226 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeID &ID,
227                                        const SCEV *op, const Type *ty)
228   : SCEVCastExpr(ID, scZeroExtend, op, ty) {
229   assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
230          (Ty->isInteger() || isa<PointerType>(Ty)) &&
231          "Cannot zero extend non-integer value!");
232 }
233
234 void SCEVZeroExtendExpr::print(raw_ostream &OS) const {
235   OS << "(zext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
236 }
237
238 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeID &ID,
239                                        const SCEV *op, const Type *ty)
240   : SCEVCastExpr(ID, scSignExtend, op, ty) {
241   assert((Op->getType()->isInteger() || isa<PointerType>(Op->getType())) &&
242          (Ty->isInteger() || isa<PointerType>(Ty)) &&
243          "Cannot sign extend non-integer value!");
244 }
245
246 void SCEVSignExtendExpr::print(raw_ostream &OS) const {
247   OS << "(sext " << *Op->getType() << " " << *Op << " to " << *Ty << ")";
248 }
249
250 void SCEVCommutativeExpr::print(raw_ostream &OS) const {
251   assert(Operands.size() > 1 && "This plus expr shouldn't exist!");
252   const char *OpStr = getOperationStr();
253   OS << "(" << *Operands[0];
254   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
255     OS << OpStr << *Operands[i];
256   OS << ")";
257 }
258
259 bool SCEVNAryExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
260   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
261     if (!getOperand(i)->dominates(BB, DT))
262       return false;
263   }
264   return true;
265 }
266
267 bool SCEVNAryExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
268   for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
269     if (!getOperand(i)->properlyDominates(BB, DT))
270       return false;
271   }
272   return true;
273 }
274
275 bool SCEVUDivExpr::dominates(BasicBlock *BB, DominatorTree *DT) const {
276   return LHS->dominates(BB, DT) && RHS->dominates(BB, DT);
277 }
278
279 bool SCEVUDivExpr::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
280   return LHS->properlyDominates(BB, DT) && RHS->properlyDominates(BB, DT);
281 }
282
283 void SCEVUDivExpr::print(raw_ostream &OS) const {
284   OS << "(" << *LHS << " /u " << *RHS << ")";
285 }
286
287 const Type *SCEVUDivExpr::getType() const {
288   // In most cases the types of LHS and RHS will be the same, but in some
289   // crazy cases one or the other may be a pointer. ScalarEvolution doesn't
290   // depend on the type for correctness, but handling types carefully can
291   // avoid extra casts in the SCEVExpander. The LHS is more likely to be
292   // a pointer type than the RHS, so use the RHS' type here.
293   return RHS->getType();
294 }
295
296 bool SCEVAddRecExpr::isLoopInvariant(const Loop *QueryLoop) const {
297   // Add recurrences are never invariant in the function-body (null loop).
298   if (!QueryLoop)
299     return false;
300
301   // This recurrence is variant w.r.t. QueryLoop if QueryLoop contains L.
302   if (QueryLoop->contains(L->getHeader()))
303     return false;
304
305   // This recurrence is variant w.r.t. QueryLoop if any of its operands
306   // are variant.
307   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
308     if (!getOperand(i)->isLoopInvariant(QueryLoop))
309       return false;
310
311   // Otherwise it's loop-invariant.
312   return true;
313 }
314
315 void SCEVAddRecExpr::print(raw_ostream &OS) const {
316   OS << "{" << *Operands[0];
317   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
318     OS << ",+," << *Operands[i];
319   OS << "}<" << L->getHeader()->getName() + ">";
320 }
321
322 void SCEVFieldOffsetExpr::print(raw_ostream &OS) const {
323   // LLVM struct fields don't have names, so just print the field number.
324   OS << "offsetof(" << *STy << ", " << FieldNo << ")";
325 }
326
327 void SCEVAllocSizeExpr::print(raw_ostream &OS) const {
328   OS << "sizeof(" << *AllocTy << ")";
329 }
330
331 bool SCEVUnknown::isLoopInvariant(const Loop *L) const {
332   // All non-instruction values are loop invariant.  All instructions are loop
333   // invariant if they are not contained in the specified loop.
334   // Instructions are never considered invariant in the function body
335   // (null loop) because they are defined within the "loop".
336   if (Instruction *I = dyn_cast<Instruction>(V))
337     return L && !L->contains(I->getParent());
338   return true;
339 }
340
341 bool SCEVUnknown::dominates(BasicBlock *BB, DominatorTree *DT) const {
342   if (Instruction *I = dyn_cast<Instruction>(getValue()))
343     return DT->dominates(I->getParent(), BB);
344   return true;
345 }
346
347 bool SCEVUnknown::properlyDominates(BasicBlock *BB, DominatorTree *DT) const {
348   if (Instruction *I = dyn_cast<Instruction>(getValue()))
349     return DT->properlyDominates(I->getParent(), BB);
350   return true;
351 }
352
353 const Type *SCEVUnknown::getType() const {
354   return V->getType();
355 }
356
357 void SCEVUnknown::print(raw_ostream &OS) const {
358   WriteAsOperand(OS, V, false);
359 }
360
361 //===----------------------------------------------------------------------===//
362 //                               SCEV Utilities
363 //===----------------------------------------------------------------------===//
364
365 static bool CompareTypes(const Type *A, const Type *B) {
366   if (A->getTypeID() != B->getTypeID())
367     return A->getTypeID() < B->getTypeID();
368   if (const IntegerType *AI = dyn_cast<IntegerType>(A)) {
369     const IntegerType *BI = cast<IntegerType>(B);
370     return AI->getBitWidth() < BI->getBitWidth();
371   }
372   if (const PointerType *AI = dyn_cast<PointerType>(A)) {
373     const PointerType *BI = cast<PointerType>(B);
374     return CompareTypes(AI->getElementType(), BI->getElementType());
375   }
376   if (const ArrayType *AI = dyn_cast<ArrayType>(A)) {
377     const ArrayType *BI = cast<ArrayType>(B);
378     if (AI->getNumElements() != BI->getNumElements())
379       return AI->getNumElements() < BI->getNumElements();
380     return CompareTypes(AI->getElementType(), BI->getElementType());
381   }
382   if (const VectorType *AI = dyn_cast<VectorType>(A)) {
383     const VectorType *BI = cast<VectorType>(B);
384     if (AI->getNumElements() != BI->getNumElements())
385       return AI->getNumElements() < BI->getNumElements();
386     return CompareTypes(AI->getElementType(), BI->getElementType());
387   }
388   if (const StructType *AI = dyn_cast<StructType>(A)) {
389     const StructType *BI = cast<StructType>(B);
390     if (AI->getNumElements() != BI->getNumElements())
391       return AI->getNumElements() < BI->getNumElements();
392     for (unsigned i = 0, e = AI->getNumElements(); i != e; ++i)
393       if (CompareTypes(AI->getElementType(i), BI->getElementType(i)) ||
394           CompareTypes(BI->getElementType(i), AI->getElementType(i)))
395         return CompareTypes(AI->getElementType(i), BI->getElementType(i));
396   }
397   return false;
398 }
399
400 namespace {
401   /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
402   /// than the complexity of the RHS.  This comparator is used to canonicalize
403   /// expressions.
404   class VISIBILITY_HIDDEN SCEVComplexityCompare {
405     LoopInfo *LI;
406   public:
407     explicit SCEVComplexityCompare(LoopInfo *li) : LI(li) {}
408
409     bool operator()(const SCEV *LHS, const SCEV *RHS) const {
410       // Fast-path: SCEVs are uniqued so we can do a quick equality check.
411       if (LHS == RHS)
412         return false;
413
414       // Primarily, sort the SCEVs by their getSCEVType().
415       if (LHS->getSCEVType() != RHS->getSCEVType())
416         return LHS->getSCEVType() < RHS->getSCEVType();
417
418       // Aside from the getSCEVType() ordering, the particular ordering
419       // isn't very important except that it's beneficial to be consistent,
420       // so that (a + b) and (b + a) don't end up as different expressions.
421
422       // Sort SCEVUnknown values with some loose heuristics. TODO: This is
423       // not as complete as it could be.
424       if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS)) {
425         const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
426
427         // Order pointer values after integer values. This helps SCEVExpander
428         // form GEPs.
429         if (isa<PointerType>(LU->getType()) && !isa<PointerType>(RU->getType()))
430           return false;
431         if (isa<PointerType>(RU->getType()) && !isa<PointerType>(LU->getType()))
432           return true;
433
434         // Compare getValueID values.
435         if (LU->getValue()->getValueID() != RU->getValue()->getValueID())
436           return LU->getValue()->getValueID() < RU->getValue()->getValueID();
437
438         // Sort arguments by their position.
439         if (const Argument *LA = dyn_cast<Argument>(LU->getValue())) {
440           const Argument *RA = cast<Argument>(RU->getValue());
441           return LA->getArgNo() < RA->getArgNo();
442         }
443
444         // For instructions, compare their loop depth, and their opcode.
445         // This is pretty loose.
446         if (Instruction *LV = dyn_cast<Instruction>(LU->getValue())) {
447           Instruction *RV = cast<Instruction>(RU->getValue());
448
449           // Compare loop depths.
450           if (LI->getLoopDepth(LV->getParent()) !=
451               LI->getLoopDepth(RV->getParent()))
452             return LI->getLoopDepth(LV->getParent()) <
453                    LI->getLoopDepth(RV->getParent());
454
455           // Compare opcodes.
456           if (LV->getOpcode() != RV->getOpcode())
457             return LV->getOpcode() < RV->getOpcode();
458
459           // Compare the number of operands.
460           if (LV->getNumOperands() != RV->getNumOperands())
461             return LV->getNumOperands() < RV->getNumOperands();
462         }
463
464         return false;
465       }
466
467       // Compare constant values.
468       if (const SCEVConstant *LC = dyn_cast<SCEVConstant>(LHS)) {
469         const SCEVConstant *RC = cast<SCEVConstant>(RHS);
470         if (LC->getValue()->getBitWidth() != RC->getValue()->getBitWidth())
471           return LC->getValue()->getBitWidth() < RC->getValue()->getBitWidth();
472         return LC->getValue()->getValue().ult(RC->getValue()->getValue());
473       }
474
475       // Compare addrec loop depths.
476       if (const SCEVAddRecExpr *LA = dyn_cast<SCEVAddRecExpr>(LHS)) {
477         const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
478         if (LA->getLoop()->getLoopDepth() != RA->getLoop()->getLoopDepth())
479           return LA->getLoop()->getLoopDepth() < RA->getLoop()->getLoopDepth();
480       }
481
482       // Lexicographically compare n-ary expressions.
483       if (const SCEVNAryExpr *LC = dyn_cast<SCEVNAryExpr>(LHS)) {
484         const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
485         for (unsigned i = 0, e = LC->getNumOperands(); i != e; ++i) {
486           if (i >= RC->getNumOperands())
487             return false;
488           if (operator()(LC->getOperand(i), RC->getOperand(i)))
489             return true;
490           if (operator()(RC->getOperand(i), LC->getOperand(i)))
491             return false;
492         }
493         return LC->getNumOperands() < RC->getNumOperands();
494       }
495
496       // Lexicographically compare udiv expressions.
497       if (const SCEVUDivExpr *LC = dyn_cast<SCEVUDivExpr>(LHS)) {
498         const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
499         if (operator()(LC->getLHS(), RC->getLHS()))
500           return true;
501         if (operator()(RC->getLHS(), LC->getLHS()))
502           return false;
503         if (operator()(LC->getRHS(), RC->getRHS()))
504           return true;
505         if (operator()(RC->getRHS(), LC->getRHS()))
506           return false;
507         return false;
508       }
509
510       // Compare cast expressions by operand.
511       if (const SCEVCastExpr *LC = dyn_cast<SCEVCastExpr>(LHS)) {
512         const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
513         return operator()(LC->getOperand(), RC->getOperand());
514       }
515
516       // Compare offsetof expressions.
517       if (const SCEVFieldOffsetExpr *LA = dyn_cast<SCEVFieldOffsetExpr>(LHS)) {
518         const SCEVFieldOffsetExpr *RA = cast<SCEVFieldOffsetExpr>(RHS);
519         if (CompareTypes(LA->getStructType(), RA->getStructType()) ||
520             CompareTypes(RA->getStructType(), LA->getStructType()))
521           return CompareTypes(LA->getStructType(), RA->getStructType());
522         return LA->getFieldNo() < RA->getFieldNo();
523       }
524
525       // Compare sizeof expressions by the allocation type.
526       if (const SCEVAllocSizeExpr *LA = dyn_cast<SCEVAllocSizeExpr>(LHS)) {
527         const SCEVAllocSizeExpr *RA = cast<SCEVAllocSizeExpr>(RHS);
528         return CompareTypes(LA->getAllocType(), RA->getAllocType());
529       }
530
531       llvm_unreachable("Unknown SCEV kind!");
532       return false;
533     }
534   };
535 }
536
537 /// GroupByComplexity - Given a list of SCEV objects, order them by their
538 /// complexity, and group objects of the same complexity together by value.
539 /// When this routine is finished, we know that any duplicates in the vector are
540 /// consecutive and that complexity is monotonically increasing.
541 ///
542 /// Note that we go take special precautions to ensure that we get determinstic
543 /// results from this routine.  In other words, we don't want the results of
544 /// this to depend on where the addresses of various SCEV objects happened to
545 /// land in memory.
546 ///
547 static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
548                               LoopInfo *LI) {
549   if (Ops.size() < 2) return;  // Noop
550   if (Ops.size() == 2) {
551     // This is the common case, which also happens to be trivially simple.
552     // Special case it.
553     if (SCEVComplexityCompare(LI)(Ops[1], Ops[0]))
554       std::swap(Ops[0], Ops[1]);
555     return;
556   }
557
558   // Do the rough sort by complexity.
559   std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
560
561   // Now that we are sorted by complexity, group elements of the same
562   // complexity.  Note that this is, at worst, N^2, but the vector is likely to
563   // be extremely short in practice.  Note that we take this approach because we
564   // do not want to depend on the addresses of the objects we are grouping.
565   for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
566     const SCEV *S = Ops[i];
567     unsigned Complexity = S->getSCEVType();
568
569     // If there are any objects of the same complexity and same value as this
570     // one, group them.
571     for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
572       if (Ops[j] == S) { // Found a duplicate.
573         // Move it to immediately after i'th element.
574         std::swap(Ops[i+1], Ops[j]);
575         ++i;   // no need to rescan it.
576         if (i == e-2) return;  // Done!
577       }
578     }
579   }
580 }
581
582
583
584 //===----------------------------------------------------------------------===//
585 //                      Simple SCEV method implementations
586 //===----------------------------------------------------------------------===//
587
588 /// BinomialCoefficient - Compute BC(It, K).  The result has width W.
589 /// Assume, K > 0.
590 static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
591                                        ScalarEvolution &SE,
592                                        const Type* ResultTy) {
593   // Handle the simplest case efficiently.
594   if (K == 1)
595     return SE.getTruncateOrZeroExtend(It, ResultTy);
596
597   // We are using the following formula for BC(It, K):
598   //
599   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
600   //
601   // Suppose, W is the bitwidth of the return value.  We must be prepared for
602   // overflow.  Hence, we must assure that the result of our computation is
603   // equal to the accurate one modulo 2^W.  Unfortunately, division isn't
604   // safe in modular arithmetic.
605   //
606   // However, this code doesn't use exactly that formula; the formula it uses
607   // is something like the following, where T is the number of factors of 2 in
608   // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
609   // exponentiation:
610   //
611   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
612   //
613   // This formula is trivially equivalent to the previous formula.  However,
614   // this formula can be implemented much more efficiently.  The trick is that
615   // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
616   // arithmetic.  To do exact division in modular arithmetic, all we have
617   // to do is multiply by the inverse.  Therefore, this step can be done at
618   // width W.
619   //
620   // The next issue is how to safely do the division by 2^T.  The way this
621   // is done is by doing the multiplication step at a width of at least W + T
622   // bits.  This way, the bottom W+T bits of the product are accurate. Then,
623   // when we perform the division by 2^T (which is equivalent to a right shift
624   // by T), the bottom W bits are accurate.  Extra bits are okay; they'll get
625   // truncated out after the division by 2^T.
626   //
627   // In comparison to just directly using the first formula, this technique
628   // is much more efficient; using the first formula requires W * K bits,
629   // but this formula less than W + K bits. Also, the first formula requires
630   // a division step, whereas this formula only requires multiplies and shifts.
631   //
632   // It doesn't matter whether the subtraction step is done in the calculation
633   // width or the input iteration count's width; if the subtraction overflows,
634   // the result must be zero anyway.  We prefer here to do it in the width of
635   // the induction variable because it helps a lot for certain cases; CodeGen
636   // isn't smart enough to ignore the overflow, which leads to much less
637   // efficient code if the width of the subtraction is wider than the native
638   // register width.
639   //
640   // (It's possible to not widen at all by pulling out factors of 2 before
641   // the multiplication; for example, K=2 can be calculated as
642   // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
643   // extra arithmetic, so it's not an obvious win, and it gets
644   // much more complicated for K > 3.)
645
646   // Protection from insane SCEVs; this bound is conservative,
647   // but it probably doesn't matter.
648   if (K > 1000)
649     return SE.getCouldNotCompute();
650
651   unsigned W = SE.getTypeSizeInBits(ResultTy);
652
653   // Calculate K! / 2^T and T; we divide out the factors of two before
654   // multiplying for calculating K! / 2^T to avoid overflow.
655   // Other overflow doesn't matter because we only care about the bottom
656   // W bits of the result.
657   APInt OddFactorial(W, 1);
658   unsigned T = 1;
659   for (unsigned i = 3; i <= K; ++i) {
660     APInt Mult(W, i);
661     unsigned TwoFactors = Mult.countTrailingZeros();
662     T += TwoFactors;
663     Mult = Mult.lshr(TwoFactors);
664     OddFactorial *= Mult;
665   }
666
667   // We need at least W + T bits for the multiplication step
668   unsigned CalculationBits = W + T;
669
670   // Calcuate 2^T, at width T+W.
671   APInt DivFactor = APInt(CalculationBits, 1).shl(T);
672
673   // Calculate the multiplicative inverse of K! / 2^T;
674   // this multiplication factor will perform the exact division by
675   // K! / 2^T.
676   APInt Mod = APInt::getSignedMinValue(W+1);
677   APInt MultiplyFactor = OddFactorial.zext(W+1);
678   MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
679   MultiplyFactor = MultiplyFactor.trunc(W);
680
681   // Calculate the product, at width T+W
682   const IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
683                                                       CalculationBits);
684   const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
685   for (unsigned i = 1; i != K; ++i) {
686     const SCEV *S = SE.getMinusSCEV(It, SE.getIntegerSCEV(i, It->getType()));
687     Dividend = SE.getMulExpr(Dividend,
688                              SE.getTruncateOrZeroExtend(S, CalculationTy));
689   }
690
691   // Divide by 2^T
692   const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
693
694   // Truncate the result, and divide by K! / 2^T.
695
696   return SE.getMulExpr(SE.getConstant(MultiplyFactor),
697                        SE.getTruncateOrZeroExtend(DivResult, ResultTy));
698 }
699
700 /// evaluateAtIteration - Return the value of this chain of recurrences at
701 /// the specified iteration number.  We can evaluate this recurrence by
702 /// multiplying each element in the chain by the binomial coefficient
703 /// corresponding to it.  In other words, we can evaluate {A,+,B,+,C,+,D} as:
704 ///
705 ///   A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
706 ///
707 /// where BC(It, k) stands for binomial coefficient.
708 ///
709 const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
710                                                 ScalarEvolution &SE) const {
711   const SCEV *Result = getStart();
712   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
713     // The computation is correct in the face of overflow provided that the
714     // multiplication is performed _after_ the evaluation of the binomial
715     // coefficient.
716     const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
717     if (isa<SCEVCouldNotCompute>(Coeff))
718       return Coeff;
719
720     Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
721   }
722   return Result;
723 }
724
725 //===----------------------------------------------------------------------===//
726 //                    SCEV Expression folder implementations
727 //===----------------------------------------------------------------------===//
728
729 const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
730                                              const Type *Ty) {
731   assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
732          "This is not a truncating conversion!");
733   assert(isSCEVable(Ty) &&
734          "This is not a conversion to a SCEVable type!");
735   Ty = getEffectiveSCEVType(Ty);
736
737   FoldingSetNodeID ID;
738   ID.AddInteger(scTruncate);
739   ID.AddPointer(Op);
740   ID.AddPointer(Ty);
741   void *IP = 0;
742   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
743
744   // Fold if the operand is constant.
745   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
746     return getConstant(
747       cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
748
749   // trunc(trunc(x)) --> trunc(x)
750   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
751     return getTruncateExpr(ST->getOperand(), Ty);
752
753   // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
754   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
755     return getTruncateOrSignExtend(SS->getOperand(), Ty);
756
757   // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
758   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
759     return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
760
761   // If the input value is a chrec scev, truncate the chrec's operands.
762   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
763     SmallVector<const SCEV *, 4> Operands;
764     for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
765       Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
766     return getAddRecExpr(Operands, AddRec->getLoop());
767   }
768
769   // The cast wasn't folded; create an explicit cast node.
770   // Recompute the insert position, as it may have been invalidated.
771   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
772   SCEV *S = SCEVAllocator.Allocate<SCEVTruncateExpr>();
773   new (S) SCEVTruncateExpr(ID, Op, Ty);
774   UniqueSCEVs.InsertNode(S, IP);
775   return S;
776 }
777
778 const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op,
779                                                const Type *Ty) {
780   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
781          "This is not an extending conversion!");
782   assert(isSCEVable(Ty) &&
783          "This is not a conversion to a SCEVable type!");
784   Ty = getEffectiveSCEVType(Ty);
785
786   // Fold if the operand is constant.
787   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
788     const Type *IntTy = getEffectiveSCEVType(Ty);
789     Constant *C = ConstantExpr::getZExt(SC->getValue(), IntTy);
790     if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
791     return getConstant(cast<ConstantInt>(C));
792   }
793
794   // zext(zext(x)) --> zext(x)
795   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
796     return getZeroExtendExpr(SZ->getOperand(), Ty);
797
798   // Before doing any expensive analysis, check to see if we've already
799   // computed a SCEV for this Op and Ty.
800   FoldingSetNodeID ID;
801   ID.AddInteger(scZeroExtend);
802   ID.AddPointer(Op);
803   ID.AddPointer(Ty);
804   void *IP = 0;
805   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
806
807   // If the input value is a chrec scev, and we can prove that the value
808   // did not overflow the old, smaller, value, we can zero extend all of the
809   // operands (often constants).  This allows analysis of something like
810   // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
811   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
812     if (AR->isAffine()) {
813       const SCEV *Start = AR->getStart();
814       const SCEV *Step = AR->getStepRecurrence(*this);
815       unsigned BitWidth = getTypeSizeInBits(AR->getType());
816       const Loop *L = AR->getLoop();
817
818       // If we have special knowledge that this addrec won't overflow,
819       // we don't need to do any further analysis.
820       if (AR->hasNoUnsignedWrap())
821         return getAddRecExpr(getZeroExtendExpr(Start, Ty),
822                              getZeroExtendExpr(Step, Ty),
823                              L);
824
825       // Check whether the backedge-taken count is SCEVCouldNotCompute.
826       // Note that this serves two purposes: It filters out loops that are
827       // simply not analyzable, and it covers the case where this code is
828       // being called from within backedge-taken count analysis, such that
829       // attempting to ask for the backedge-taken count would likely result
830       // in infinite recursion. In the later case, the analysis code will
831       // cope with a conservative value, and it will take care to purge
832       // that value once it has finished.
833       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
834       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
835         // Manually compute the final value for AR, checking for
836         // overflow.
837
838         // Check whether the backedge-taken count can be losslessly casted to
839         // the addrec's type. The count is always unsigned.
840         const SCEV *CastedMaxBECount =
841           getTruncateOrZeroExtend(MaxBECount, Start->getType());
842         const SCEV *RecastedMaxBECount =
843           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
844         if (MaxBECount == RecastedMaxBECount) {
845           const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
846           // Check whether Start+Step*MaxBECount has no unsigned overflow.
847           const SCEV *ZMul =
848             getMulExpr(CastedMaxBECount,
849                        getTruncateOrZeroExtend(Step, Start->getType()));
850           const SCEV *Add = getAddExpr(Start, ZMul);
851           const SCEV *OperandExtendedAdd =
852             getAddExpr(getZeroExtendExpr(Start, WideTy),
853                        getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
854                                   getZeroExtendExpr(Step, WideTy)));
855           if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
856             // Return the expression with the addrec on the outside.
857             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
858                                  getZeroExtendExpr(Step, Ty),
859                                  L);
860
861           // Similar to above, only this time treat the step value as signed.
862           // This covers loops that count down.
863           const SCEV *SMul =
864             getMulExpr(CastedMaxBECount,
865                        getTruncateOrSignExtend(Step, Start->getType()));
866           Add = getAddExpr(Start, SMul);
867           OperandExtendedAdd =
868             getAddExpr(getZeroExtendExpr(Start, WideTy),
869                        getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
870                                   getSignExtendExpr(Step, WideTy)));
871           if (getZeroExtendExpr(Add, WideTy) == OperandExtendedAdd)
872             // Return the expression with the addrec on the outside.
873             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
874                                  getSignExtendExpr(Step, Ty),
875                                  L);
876         }
877
878         // If the backedge is guarded by a comparison with the pre-inc value
879         // the addrec is safe. Also, if the entry is guarded by a comparison
880         // with the start value and the backedge is guarded by a comparison
881         // with the post-inc value, the addrec is safe.
882         if (isKnownPositive(Step)) {
883           const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
884                                       getUnsignedRange(Step).getUnsignedMax());
885           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
886               (isLoopGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
887                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
888                                            AR->getPostIncExpr(*this), N)))
889             // Return the expression with the addrec on the outside.
890             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
891                                  getZeroExtendExpr(Step, Ty),
892                                  L);
893         } else if (isKnownNegative(Step)) {
894           const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
895                                       getSignedRange(Step).getSignedMin());
896           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) &&
897               (isLoopGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) ||
898                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
899                                            AR->getPostIncExpr(*this), N)))
900             // Return the expression with the addrec on the outside.
901             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
902                                  getSignExtendExpr(Step, Ty),
903                                  L);
904         }
905       }
906     }
907
908   // The cast wasn't folded; create an explicit cast node.
909   // Recompute the insert position, as it may have been invalidated.
910   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
911   SCEV *S = SCEVAllocator.Allocate<SCEVZeroExtendExpr>();
912   new (S) SCEVZeroExtendExpr(ID, Op, Ty);
913   UniqueSCEVs.InsertNode(S, IP);
914   return S;
915 }
916
917 const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op,
918                                                const Type *Ty) {
919   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
920          "This is not an extending conversion!");
921   assert(isSCEVable(Ty) &&
922          "This is not a conversion to a SCEVable type!");
923   Ty = getEffectiveSCEVType(Ty);
924
925   // Fold if the operand is constant.
926   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op)) {
927     const Type *IntTy = getEffectiveSCEVType(Ty);
928     Constant *C = ConstantExpr::getSExt(SC->getValue(), IntTy);
929     if (IntTy != Ty) C = ConstantExpr::getIntToPtr(C, Ty);
930     return getConstant(cast<ConstantInt>(C));
931   }
932
933   // sext(sext(x)) --> sext(x)
934   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
935     return getSignExtendExpr(SS->getOperand(), Ty);
936
937   // Before doing any expensive analysis, check to see if we've already
938   // computed a SCEV for this Op and Ty.
939   FoldingSetNodeID ID;
940   ID.AddInteger(scSignExtend);
941   ID.AddPointer(Op);
942   ID.AddPointer(Ty);
943   void *IP = 0;
944   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
945
946   // If the input value is a chrec scev, and we can prove that the value
947   // did not overflow the old, smaller, value, we can sign extend all of the
948   // operands (often constants).  This allows analysis of something like
949   // this:  for (signed char X = 0; X < 100; ++X) { int Y = X; }
950   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
951     if (AR->isAffine()) {
952       const SCEV *Start = AR->getStart();
953       const SCEV *Step = AR->getStepRecurrence(*this);
954       unsigned BitWidth = getTypeSizeInBits(AR->getType());
955       const Loop *L = AR->getLoop();
956
957       // If we have special knowledge that this addrec won't overflow,
958       // we don't need to do any further analysis.
959       if (AR->hasNoSignedWrap())
960         return getAddRecExpr(getSignExtendExpr(Start, Ty),
961                              getSignExtendExpr(Step, Ty),
962                              L);
963
964       // Check whether the backedge-taken count is SCEVCouldNotCompute.
965       // Note that this serves two purposes: It filters out loops that are
966       // simply not analyzable, and it covers the case where this code is
967       // being called from within backedge-taken count analysis, such that
968       // attempting to ask for the backedge-taken count would likely result
969       // in infinite recursion. In the later case, the analysis code will
970       // cope with a conservative value, and it will take care to purge
971       // that value once it has finished.
972       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
973       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
974         // Manually compute the final value for AR, checking for
975         // overflow.
976
977         // Check whether the backedge-taken count can be losslessly casted to
978         // the addrec's type. The count is always unsigned.
979         const SCEV *CastedMaxBECount =
980           getTruncateOrZeroExtend(MaxBECount, Start->getType());
981         const SCEV *RecastedMaxBECount =
982           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
983         if (MaxBECount == RecastedMaxBECount) {
984           const Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
985           // Check whether Start+Step*MaxBECount has no signed overflow.
986           const SCEV *SMul =
987             getMulExpr(CastedMaxBECount,
988                        getTruncateOrSignExtend(Step, Start->getType()));
989           const SCEV *Add = getAddExpr(Start, SMul);
990           const SCEV *OperandExtendedAdd =
991             getAddExpr(getSignExtendExpr(Start, WideTy),
992                        getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
993                                   getSignExtendExpr(Step, WideTy)));
994           if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
995             // Return the expression with the addrec on the outside.
996             return getAddRecExpr(getSignExtendExpr(Start, Ty),
997                                  getSignExtendExpr(Step, Ty),
998                                  L);
999
1000           // Similar to above, only this time treat the step value as unsigned.
1001           // This covers loops that count up with an unsigned step.
1002           const SCEV *UMul =
1003             getMulExpr(CastedMaxBECount,
1004                        getTruncateOrZeroExtend(Step, Start->getType()));
1005           Add = getAddExpr(Start, UMul);
1006           OperandExtendedAdd =
1007             getAddExpr(getSignExtendExpr(Start, WideTy),
1008                        getMulExpr(getZeroExtendExpr(CastedMaxBECount, WideTy),
1009                                   getZeroExtendExpr(Step, WideTy)));
1010           if (getSignExtendExpr(Add, WideTy) == OperandExtendedAdd)
1011             // Return the expression with the addrec on the outside.
1012             return getAddRecExpr(getSignExtendExpr(Start, Ty),
1013                                  getZeroExtendExpr(Step, Ty),
1014                                  L);
1015         }
1016
1017         // If the backedge is guarded by a comparison with the pre-inc value
1018         // the addrec is safe. Also, if the entry is guarded by a comparison
1019         // with the start value and the backedge is guarded by a comparison
1020         // with the post-inc value, the addrec is safe.
1021         if (isKnownPositive(Step)) {
1022           const SCEV *N = getConstant(APInt::getSignedMinValue(BitWidth) -
1023                                       getSignedRange(Step).getSignedMax());
1024           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT, AR, N) ||
1025               (isLoopGuardedByCond(L, ICmpInst::ICMP_SLT, Start, N) &&
1026                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SLT,
1027                                            AR->getPostIncExpr(*this), N)))
1028             // Return the expression with the addrec on the outside.
1029             return getAddRecExpr(getSignExtendExpr(Start, Ty),
1030                                  getSignExtendExpr(Step, Ty),
1031                                  L);
1032         } else if (isKnownNegative(Step)) {
1033           const SCEV *N = getConstant(APInt::getSignedMaxValue(BitWidth) -
1034                                       getSignedRange(Step).getSignedMin());
1035           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT, AR, N) ||
1036               (isLoopGuardedByCond(L, ICmpInst::ICMP_SGT, Start, N) &&
1037                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_SGT,
1038                                            AR->getPostIncExpr(*this), N)))
1039             // Return the expression with the addrec on the outside.
1040             return getAddRecExpr(getSignExtendExpr(Start, Ty),
1041                                  getSignExtendExpr(Step, Ty),
1042                                  L);
1043         }
1044       }
1045     }
1046
1047   // The cast wasn't folded; create an explicit cast node.
1048   // Recompute the insert position, as it may have been invalidated.
1049   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1050   SCEV *S = SCEVAllocator.Allocate<SCEVSignExtendExpr>();
1051   new (S) SCEVSignExtendExpr(ID, Op, Ty);
1052   UniqueSCEVs.InsertNode(S, IP);
1053   return S;
1054 }
1055
1056 /// getAnyExtendExpr - Return a SCEV for the given operand extended with
1057 /// unspecified bits out to the given type.
1058 ///
1059 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
1060                                               const Type *Ty) {
1061   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1062          "This is not an extending conversion!");
1063   assert(isSCEVable(Ty) &&
1064          "This is not a conversion to a SCEVable type!");
1065   Ty = getEffectiveSCEVType(Ty);
1066
1067   // Sign-extend negative constants.
1068   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1069     if (SC->getValue()->getValue().isNegative())
1070       return getSignExtendExpr(Op, Ty);
1071
1072   // Peel off a truncate cast.
1073   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
1074     const SCEV *NewOp = T->getOperand();
1075     if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
1076       return getAnyExtendExpr(NewOp, Ty);
1077     return getTruncateOrNoop(NewOp, Ty);
1078   }
1079
1080   // Next try a zext cast. If the cast is folded, use it.
1081   const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
1082   if (!isa<SCEVZeroExtendExpr>(ZExt))
1083     return ZExt;
1084
1085   // Next try a sext cast. If the cast is folded, use it.
1086   const SCEV *SExt = getSignExtendExpr(Op, Ty);
1087   if (!isa<SCEVSignExtendExpr>(SExt))
1088     return SExt;
1089
1090   // If the expression is obviously signed, use the sext cast value.
1091   if (isa<SCEVSMaxExpr>(Op))
1092     return SExt;
1093
1094   // Absent any other information, use the zext cast value.
1095   return ZExt;
1096 }
1097
1098 /// CollectAddOperandsWithScales - Process the given Ops list, which is
1099 /// a list of operands to be added under the given scale, update the given
1100 /// map. This is a helper function for getAddRecExpr. As an example of
1101 /// what it does, given a sequence of operands that would form an add
1102 /// expression like this:
1103 ///
1104 ///    m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r)
1105 ///
1106 /// where A and B are constants, update the map with these values:
1107 ///
1108 ///    (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1109 ///
1110 /// and add 13 + A*B*29 to AccumulatedConstant.
1111 /// This will allow getAddRecExpr to produce this:
1112 ///
1113 ///    13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1114 ///
1115 /// This form often exposes folding opportunities that are hidden in
1116 /// the original operand list.
1117 ///
1118 /// Return true iff it appears that any interesting folding opportunities
1119 /// may be exposed. This helps getAddRecExpr short-circuit extra work in
1120 /// the common case where no interesting opportunities are present, and
1121 /// is also used as a check to avoid infinite recursion.
1122 ///
1123 static bool
1124 CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
1125                              SmallVector<const SCEV *, 8> &NewOps,
1126                              APInt &AccumulatedConstant,
1127                              const SmallVectorImpl<const SCEV *> &Ops,
1128                              const APInt &Scale,
1129                              ScalarEvolution &SE) {
1130   bool Interesting = false;
1131
1132   // Iterate over the add operands.
1133   for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1134     const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1135     if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1136       APInt NewScale =
1137         Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1138       if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1139         // A multiplication of a constant with another add; recurse.
1140         Interesting |=
1141           CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1142                                        cast<SCEVAddExpr>(Mul->getOperand(1))
1143                                          ->getOperands(),
1144                                        NewScale, SE);
1145       } else {
1146         // A multiplication of a constant with some other value. Update
1147         // the map.
1148         SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1149         const SCEV *Key = SE.getMulExpr(MulOps);
1150         std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
1151           M.insert(std::make_pair(Key, NewScale));
1152         if (Pair.second) {
1153           NewOps.push_back(Pair.first->first);
1154         } else {
1155           Pair.first->second += NewScale;
1156           // The map already had an entry for this value, which may indicate
1157           // a folding opportunity.
1158           Interesting = true;
1159         }
1160       }
1161     } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1162       // Pull a buried constant out to the outside.
1163       if (Scale != 1 || AccumulatedConstant != 0 || C->isZero())
1164         Interesting = true;
1165       AccumulatedConstant += Scale * C->getValue()->getValue();
1166     } else {
1167       // An ordinary operand. Update the map.
1168       std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
1169         M.insert(std::make_pair(Ops[i], Scale));
1170       if (Pair.second) {
1171         NewOps.push_back(Pair.first->first);
1172       } else {
1173         Pair.first->second += Scale;
1174         // The map already had an entry for this value, which may indicate
1175         // a folding opportunity.
1176         Interesting = true;
1177       }
1178     }
1179   }
1180
1181   return Interesting;
1182 }
1183
1184 namespace {
1185   struct APIntCompare {
1186     bool operator()(const APInt &LHS, const APInt &RHS) const {
1187       return LHS.ult(RHS);
1188     }
1189   };
1190 }
1191
1192 /// getAddExpr - Get a canonical add expression, or something simpler if
1193 /// possible.
1194 const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops) {
1195   assert(!Ops.empty() && "Cannot get empty add!");
1196   if (Ops.size() == 1) return Ops[0];
1197 #ifndef NDEBUG
1198   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1199     assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1200            getEffectiveSCEVType(Ops[0]->getType()) &&
1201            "SCEVAddExpr operand types don't match!");
1202 #endif
1203
1204   // Sort by complexity, this groups all similar expression types together.
1205   GroupByComplexity(Ops, LI);
1206
1207   // If there are any constants, fold them together.
1208   unsigned Idx = 0;
1209   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1210     ++Idx;
1211     assert(Idx < Ops.size());
1212     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1213       // We found two constants, fold them together!
1214       Ops[0] = getConstant(LHSC->getValue()->getValue() +
1215                            RHSC->getValue()->getValue());
1216       if (Ops.size() == 2) return Ops[0];
1217       Ops.erase(Ops.begin()+1);  // Erase the folded element
1218       LHSC = cast<SCEVConstant>(Ops[0]);
1219     }
1220
1221     // If we are left with a constant zero being added, strip it off.
1222     if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
1223       Ops.erase(Ops.begin());
1224       --Idx;
1225     }
1226   }
1227
1228   if (Ops.size() == 1) return Ops[0];
1229
1230   // Okay, check to see if the same value occurs in the operand list twice.  If
1231   // so, merge them together into an multiply expression.  Since we sorted the
1232   // list, these values are required to be adjacent.
1233   const Type *Ty = Ops[0]->getType();
1234   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1235     if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2
1236       // Found a match, merge the two values into a multiply, and add any
1237       // remaining values to the result.
1238       const SCEV *Two = getIntegerSCEV(2, Ty);
1239       const SCEV *Mul = getMulExpr(Ops[i], Two);
1240       if (Ops.size() == 2)
1241         return Mul;
1242       Ops.erase(Ops.begin()+i, Ops.begin()+i+2);
1243       Ops.push_back(Mul);
1244       return getAddExpr(Ops);
1245     }
1246
1247   // Check for truncates. If all the operands are truncated from the same
1248   // type, see if factoring out the truncate would permit the result to be
1249   // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
1250   // if the contents of the resulting outer trunc fold to something simple.
1251   for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
1252     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
1253     const Type *DstType = Trunc->getType();
1254     const Type *SrcType = Trunc->getOperand()->getType();
1255     SmallVector<const SCEV *, 8> LargeOps;
1256     bool Ok = true;
1257     // Check all the operands to see if they can be represented in the
1258     // source type of the truncate.
1259     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1260       if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
1261         if (T->getOperand()->getType() != SrcType) {
1262           Ok = false;
1263           break;
1264         }
1265         LargeOps.push_back(T->getOperand());
1266       } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1267         // This could be either sign or zero extension, but sign extension
1268         // is much more likely to be foldable here.
1269         LargeOps.push_back(getSignExtendExpr(C, SrcType));
1270       } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
1271         SmallVector<const SCEV *, 8> LargeMulOps;
1272         for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
1273           if (const SCEVTruncateExpr *T =
1274                 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
1275             if (T->getOperand()->getType() != SrcType) {
1276               Ok = false;
1277               break;
1278             }
1279             LargeMulOps.push_back(T->getOperand());
1280           } else if (const SCEVConstant *C =
1281                        dyn_cast<SCEVConstant>(M->getOperand(j))) {
1282             // This could be either sign or zero extension, but sign extension
1283             // is much more likely to be foldable here.
1284             LargeMulOps.push_back(getSignExtendExpr(C, SrcType));
1285           } else {
1286             Ok = false;
1287             break;
1288           }
1289         }
1290         if (Ok)
1291           LargeOps.push_back(getMulExpr(LargeMulOps));
1292       } else {
1293         Ok = false;
1294         break;
1295       }
1296     }
1297     if (Ok) {
1298       // Evaluate the expression in the larger type.
1299       const SCEV *Fold = getAddExpr(LargeOps);
1300       // If it folds to something simple, use it. Otherwise, don't.
1301       if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
1302         return getTruncateExpr(Fold, DstType);
1303     }
1304   }
1305
1306   // Skip past any other cast SCEVs.
1307   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
1308     ++Idx;
1309
1310   // If there are add operands they would be next.
1311   if (Idx < Ops.size()) {
1312     bool DeletedAdd = false;
1313     while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
1314       // If we have an add, expand the add operands onto the end of the operands
1315       // list.
1316       Ops.insert(Ops.end(), Add->op_begin(), Add->op_end());
1317       Ops.erase(Ops.begin()+Idx);
1318       DeletedAdd = true;
1319     }
1320
1321     // If we deleted at least one add, we added operands to the end of the list,
1322     // and they are not necessarily sorted.  Recurse to resort and resimplify
1323     // any operands we just aquired.
1324     if (DeletedAdd)
1325       return getAddExpr(Ops);
1326   }
1327
1328   // Skip over the add expression until we get to a multiply.
1329   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1330     ++Idx;
1331
1332   // Check to see if there are any folding opportunities present with
1333   // operands multiplied by constant values.
1334   if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
1335     uint64_t BitWidth = getTypeSizeInBits(Ty);
1336     DenseMap<const SCEV *, APInt> M;
1337     SmallVector<const SCEV *, 8> NewOps;
1338     APInt AccumulatedConstant(BitWidth, 0);
1339     if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1340                                      Ops, APInt(BitWidth, 1), *this)) {
1341       // Some interesting folding opportunity is present, so its worthwhile to
1342       // re-generate the operands list. Group the operands by constant scale,
1343       // to avoid multiplying by the same constant scale multiple times.
1344       std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
1345       for (SmallVector<const SCEV *, 8>::iterator I = NewOps.begin(),
1346            E = NewOps.end(); I != E; ++I)
1347         MulOpLists[M.find(*I)->second].push_back(*I);
1348       // Re-generate the operands list.
1349       Ops.clear();
1350       if (AccumulatedConstant != 0)
1351         Ops.push_back(getConstant(AccumulatedConstant));
1352       for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator
1353            I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
1354         if (I->first != 0)
1355           Ops.push_back(getMulExpr(getConstant(I->first),
1356                                    getAddExpr(I->second)));
1357       if (Ops.empty())
1358         return getIntegerSCEV(0, Ty);
1359       if (Ops.size() == 1)
1360         return Ops[0];
1361       return getAddExpr(Ops);
1362     }
1363   }
1364
1365   // If we are adding something to a multiply expression, make sure the
1366   // something is not already an operand of the multiply.  If so, merge it into
1367   // the multiply.
1368   for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
1369     const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
1370     for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
1371       const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
1372       for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
1373         if (MulOpSCEV == Ops[AddOp] && !isa<SCEVConstant>(Ops[AddOp])) {
1374           // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1))
1375           const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
1376           if (Mul->getNumOperands() != 2) {
1377             // If the multiply has more than two operands, we must get the
1378             // Y*Z term.
1379             SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(), Mul->op_end());
1380             MulOps.erase(MulOps.begin()+MulOp);
1381             InnerMul = getMulExpr(MulOps);
1382           }
1383           const SCEV *One = getIntegerSCEV(1, Ty);
1384           const SCEV *AddOne = getAddExpr(InnerMul, One);
1385           const SCEV *OuterMul = getMulExpr(AddOne, Ops[AddOp]);
1386           if (Ops.size() == 2) return OuterMul;
1387           if (AddOp < Idx) {
1388             Ops.erase(Ops.begin()+AddOp);
1389             Ops.erase(Ops.begin()+Idx-1);
1390           } else {
1391             Ops.erase(Ops.begin()+Idx);
1392             Ops.erase(Ops.begin()+AddOp-1);
1393           }
1394           Ops.push_back(OuterMul);
1395           return getAddExpr(Ops);
1396         }
1397
1398       // Check this multiply against other multiplies being added together.
1399       for (unsigned OtherMulIdx = Idx+1;
1400            OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1401            ++OtherMulIdx) {
1402         const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
1403         // If MulOp occurs in OtherMul, we can fold the two multiplies
1404         // together.
1405         for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1406              OMulOp != e; ++OMulOp)
1407           if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1408             // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
1409             const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
1410             if (Mul->getNumOperands() != 2) {
1411               SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
1412                                                   Mul->op_end());
1413               MulOps.erase(MulOps.begin()+MulOp);
1414               InnerMul1 = getMulExpr(MulOps);
1415             }
1416             const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
1417             if (OtherMul->getNumOperands() != 2) {
1418               SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
1419                                                   OtherMul->op_end());
1420               MulOps.erase(MulOps.begin()+OMulOp);
1421               InnerMul2 = getMulExpr(MulOps);
1422             }
1423             const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
1424             const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
1425             if (Ops.size() == 2) return OuterMul;
1426             Ops.erase(Ops.begin()+Idx);
1427             Ops.erase(Ops.begin()+OtherMulIdx-1);
1428             Ops.push_back(OuterMul);
1429             return getAddExpr(Ops);
1430           }
1431       }
1432     }
1433   }
1434
1435   // If there are any add recurrences in the operands list, see if any other
1436   // added values are loop invariant.  If so, we can fold them into the
1437   // recurrence.
1438   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1439     ++Idx;
1440
1441   // Scan over all recurrences, trying to fold loop invariants into them.
1442   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1443     // Scan all of the other operands to this add and add them to the vector if
1444     // they are loop invariant w.r.t. the recurrence.
1445     SmallVector<const SCEV *, 8> LIOps;
1446     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
1447     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1448       if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1449         LIOps.push_back(Ops[i]);
1450         Ops.erase(Ops.begin()+i);
1451         --i; --e;
1452       }
1453
1454     // If we found some loop invariants, fold them into the recurrence.
1455     if (!LIOps.empty()) {
1456       //  NLI + LI + {Start,+,Step}  -->  NLI + {LI+Start,+,Step}
1457       LIOps.push_back(AddRec->getStart());
1458
1459       SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
1460                                            AddRec->op_end());
1461       AddRecOps[0] = getAddExpr(LIOps);
1462
1463       const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRec->getLoop());
1464       // If all of the other operands were loop invariant, we are done.
1465       if (Ops.size() == 1) return NewRec;
1466
1467       // Otherwise, add the folded AddRec by the non-liv parts.
1468       for (unsigned i = 0;; ++i)
1469         if (Ops[i] == AddRec) {
1470           Ops[i] = NewRec;
1471           break;
1472         }
1473       return getAddExpr(Ops);
1474     }
1475
1476     // Okay, if there weren't any loop invariants to be folded, check to see if
1477     // there are multiple AddRec's with the same loop induction variable being
1478     // added together.  If so, we can fold them.
1479     for (unsigned OtherIdx = Idx+1;
1480          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1481       if (OtherIdx != Idx) {
1482         const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
1483         if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1484           // Other + {A,+,B} + {C,+,D}  -->  Other + {A+C,+,B+D}
1485           SmallVector<const SCEV *, 4> NewOps(AddRec->op_begin(),
1486                                               AddRec->op_end());
1487           for (unsigned i = 0, e = OtherAddRec->getNumOperands(); i != e; ++i) {
1488             if (i >= NewOps.size()) {
1489               NewOps.insert(NewOps.end(), OtherAddRec->op_begin()+i,
1490                             OtherAddRec->op_end());
1491               break;
1492             }
1493             NewOps[i] = getAddExpr(NewOps[i], OtherAddRec->getOperand(i));
1494           }
1495           const SCEV *NewAddRec = getAddRecExpr(NewOps, AddRec->getLoop());
1496
1497           if (Ops.size() == 2) return NewAddRec;
1498
1499           Ops.erase(Ops.begin()+Idx);
1500           Ops.erase(Ops.begin()+OtherIdx-1);
1501           Ops.push_back(NewAddRec);
1502           return getAddExpr(Ops);
1503         }
1504       }
1505
1506     // Otherwise couldn't fold anything into this recurrence.  Move onto the
1507     // next one.
1508   }
1509
1510   // Okay, it looks like we really DO need an add expr.  Check to see if we
1511   // already have one, otherwise create a new one.
1512   FoldingSetNodeID ID;
1513   ID.AddInteger(scAddExpr);
1514   ID.AddInteger(Ops.size());
1515   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1516     ID.AddPointer(Ops[i]);
1517   void *IP = 0;
1518   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1519   SCEV *S = SCEVAllocator.Allocate<SCEVAddExpr>();
1520   new (S) SCEVAddExpr(ID, Ops);
1521   UniqueSCEVs.InsertNode(S, IP);
1522   return S;
1523 }
1524
1525
1526 /// getMulExpr - Get a canonical multiply expression, or something simpler if
1527 /// possible.
1528 const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops) {
1529   assert(!Ops.empty() && "Cannot get empty mul!");
1530 #ifndef NDEBUG
1531   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1532     assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1533            getEffectiveSCEVType(Ops[0]->getType()) &&
1534            "SCEVMulExpr operand types don't match!");
1535 #endif
1536
1537   // Sort by complexity, this groups all similar expression types together.
1538   GroupByComplexity(Ops, LI);
1539
1540   // If there are any constants, fold them together.
1541   unsigned Idx = 0;
1542   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1543
1544     // C1*(C2+V) -> C1*C2 + C1*V
1545     if (Ops.size() == 2)
1546       if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
1547         if (Add->getNumOperands() == 2 &&
1548             isa<SCEVConstant>(Add->getOperand(0)))
1549           return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
1550                             getMulExpr(LHSC, Add->getOperand(1)));
1551
1552
1553     ++Idx;
1554     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1555       // We found two constants, fold them together!
1556       ConstantInt *Fold = ConstantInt::get(getContext(),
1557                                            LHSC->getValue()->getValue() *
1558                                            RHSC->getValue()->getValue());
1559       Ops[0] = getConstant(Fold);
1560       Ops.erase(Ops.begin()+1);  // Erase the folded element
1561       if (Ops.size() == 1) return Ops[0];
1562       LHSC = cast<SCEVConstant>(Ops[0]);
1563     }
1564
1565     // If we are left with a constant one being multiplied, strip it off.
1566     if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1567       Ops.erase(Ops.begin());
1568       --Idx;
1569     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
1570       // If we have a multiply of zero, it will always be zero.
1571       return Ops[0];
1572     }
1573   }
1574
1575   // Skip over the add expression until we get to a multiply.
1576   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1577     ++Idx;
1578
1579   if (Ops.size() == 1)
1580     return Ops[0];
1581
1582   // If there are mul operands inline them all into this expression.
1583   if (Idx < Ops.size()) {
1584     bool DeletedMul = false;
1585     while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
1586       // If we have an mul, expand the mul operands onto the end of the operands
1587       // list.
1588       Ops.insert(Ops.end(), Mul->op_begin(), Mul->op_end());
1589       Ops.erase(Ops.begin()+Idx);
1590       DeletedMul = true;
1591     }
1592
1593     // If we deleted at least one mul, we added operands to the end of the list,
1594     // and they are not necessarily sorted.  Recurse to resort and resimplify
1595     // any operands we just aquired.
1596     if (DeletedMul)
1597       return getMulExpr(Ops);
1598   }
1599
1600   // If there are any add recurrences in the operands list, see if any other
1601   // added values are loop invariant.  If so, we can fold them into the
1602   // recurrence.
1603   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1604     ++Idx;
1605
1606   // Scan over all recurrences, trying to fold loop invariants into them.
1607   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1608     // Scan all of the other operands to this mul and add them to the vector if
1609     // they are loop invariant w.r.t. the recurrence.
1610     SmallVector<const SCEV *, 8> LIOps;
1611     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
1612     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1613       if (Ops[i]->isLoopInvariant(AddRec->getLoop())) {
1614         LIOps.push_back(Ops[i]);
1615         Ops.erase(Ops.begin()+i);
1616         --i; --e;
1617       }
1618
1619     // If we found some loop invariants, fold them into the recurrence.
1620     if (!LIOps.empty()) {
1621       //  NLI * LI * {Start,+,Step}  -->  NLI * {LI*Start,+,LI*Step}
1622       SmallVector<const SCEV *, 4> NewOps;
1623       NewOps.reserve(AddRec->getNumOperands());
1624       if (LIOps.size() == 1) {
1625         const SCEV *Scale = LIOps[0];
1626         for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
1627           NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
1628       } else {
1629         for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
1630           SmallVector<const SCEV *, 4> MulOps(LIOps.begin(), LIOps.end());
1631           MulOps.push_back(AddRec->getOperand(i));
1632           NewOps.push_back(getMulExpr(MulOps));
1633         }
1634       }
1635
1636       const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop());
1637
1638       // If all of the other operands were loop invariant, we are done.
1639       if (Ops.size() == 1) return NewRec;
1640
1641       // Otherwise, multiply the folded AddRec by the non-liv parts.
1642       for (unsigned i = 0;; ++i)
1643         if (Ops[i] == AddRec) {
1644           Ops[i] = NewRec;
1645           break;
1646         }
1647       return getMulExpr(Ops);
1648     }
1649
1650     // Okay, if there weren't any loop invariants to be folded, check to see if
1651     // there are multiple AddRec's with the same loop induction variable being
1652     // multiplied together.  If so, we can fold them.
1653     for (unsigned OtherIdx = Idx+1;
1654          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);++OtherIdx)
1655       if (OtherIdx != Idx) {
1656         const SCEVAddRecExpr *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
1657         if (AddRec->getLoop() == OtherAddRec->getLoop()) {
1658           // F * G  -->  {A,+,B} * {C,+,D}  -->  {A*C,+,F*D + G*B + B*D}
1659           const SCEVAddRecExpr *F = AddRec, *G = OtherAddRec;
1660           const SCEV *NewStart = getMulExpr(F->getStart(),
1661                                                  G->getStart());
1662           const SCEV *B = F->getStepRecurrence(*this);
1663           const SCEV *D = G->getStepRecurrence(*this);
1664           const SCEV *NewStep = getAddExpr(getMulExpr(F, D),
1665                                           getMulExpr(G, B),
1666                                           getMulExpr(B, D));
1667           const SCEV *NewAddRec = getAddRecExpr(NewStart, NewStep,
1668                                                F->getLoop());
1669           if (Ops.size() == 2) return NewAddRec;
1670
1671           Ops.erase(Ops.begin()+Idx);
1672           Ops.erase(Ops.begin()+OtherIdx-1);
1673           Ops.push_back(NewAddRec);
1674           return getMulExpr(Ops);
1675         }
1676       }
1677
1678     // Otherwise couldn't fold anything into this recurrence.  Move onto the
1679     // next one.
1680   }
1681
1682   // Okay, it looks like we really DO need an mul expr.  Check to see if we
1683   // already have one, otherwise create a new one.
1684   FoldingSetNodeID ID;
1685   ID.AddInteger(scMulExpr);
1686   ID.AddInteger(Ops.size());
1687   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1688     ID.AddPointer(Ops[i]);
1689   void *IP = 0;
1690   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1691   SCEV *S = SCEVAllocator.Allocate<SCEVMulExpr>();
1692   new (S) SCEVMulExpr(ID, Ops);
1693   UniqueSCEVs.InsertNode(S, IP);
1694   return S;
1695 }
1696
1697 /// getUDivExpr - Get a canonical unsigned division expression, or something
1698 /// simpler if possible.
1699 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
1700                                          const SCEV *RHS) {
1701   assert(getEffectiveSCEVType(LHS->getType()) ==
1702          getEffectiveSCEVType(RHS->getType()) &&
1703          "SCEVUDivExpr operand types don't match!");
1704
1705   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
1706     if (RHSC->getValue()->equalsInt(1))
1707       return LHS;                               // X udiv 1 --> x
1708     if (RHSC->isZero())
1709       return getIntegerSCEV(0, LHS->getType()); // value is undefined
1710
1711     // Determine if the division can be folded into the operands of
1712     // its operands.
1713     // TODO: Generalize this to non-constants by using known-bits information.
1714     const Type *Ty = LHS->getType();
1715     unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1716     unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1717     // For non-power-of-two values, effectively round the value up to the
1718     // nearest power of two.
1719     if (!RHSC->getValue()->getValue().isPowerOf2())
1720       ++MaxShiftAmt;
1721     const IntegerType *ExtTy =
1722       IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
1723     // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1724     if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1725       if (const SCEVConstant *Step =
1726             dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1727         if (!Step->getValue()->getValue()
1728               .urem(RHSC->getValue()->getValue()) &&
1729             getZeroExtendExpr(AR, ExtTy) ==
1730             getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1731                           getZeroExtendExpr(Step, ExtTy),
1732                           AR->getLoop())) {
1733           SmallVector<const SCEV *, 4> Operands;
1734           for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1735             Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1736           return getAddRecExpr(Operands, AR->getLoop());
1737         }
1738     // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
1739     if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
1740       SmallVector<const SCEV *, 4> Operands;
1741       for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1742         Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1743       if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
1744         // Find an operand that's safely divisible.
1745         for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
1746           const SCEV *Op = M->getOperand(i);
1747           const SCEV *Div = getUDivExpr(Op, RHSC);
1748           if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
1749             const SmallVectorImpl<const SCEV *> &MOperands = M->getOperands();
1750             Operands = SmallVector<const SCEV *, 4>(MOperands.begin(),
1751                                                   MOperands.end());
1752             Operands[i] = Div;
1753             return getMulExpr(Operands);
1754           }
1755         }
1756     }
1757     // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
1758     if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
1759       SmallVector<const SCEV *, 4> Operands;
1760       for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1761         Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1762       if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1763         Operands.clear();
1764         for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
1765           const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
1766           if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i))
1767             break;
1768           Operands.push_back(Op);
1769         }
1770         if (Operands.size() == A->getNumOperands())
1771           return getAddExpr(Operands);
1772       }
1773     }
1774
1775     // Fold if both operands are constant.
1776     if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
1777       Constant *LHSCV = LHSC->getValue();
1778       Constant *RHSCV = RHSC->getValue();
1779       return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
1780                                                                  RHSCV)));
1781     }
1782   }
1783
1784   FoldingSetNodeID ID;
1785   ID.AddInteger(scUDivExpr);
1786   ID.AddPointer(LHS);
1787   ID.AddPointer(RHS);
1788   void *IP = 0;
1789   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1790   SCEV *S = SCEVAllocator.Allocate<SCEVUDivExpr>();
1791   new (S) SCEVUDivExpr(ID, LHS, RHS);
1792   UniqueSCEVs.InsertNode(S, IP);
1793   return S;
1794 }
1795
1796
1797 /// getAddRecExpr - Get an add recurrence expression for the specified loop.
1798 /// Simplify the expression as much as possible.
1799 const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start,
1800                                            const SCEV *Step, const Loop *L) {
1801   SmallVector<const SCEV *, 4> Operands;
1802   Operands.push_back(Start);
1803   if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
1804     if (StepChrec->getLoop() == L) {
1805       Operands.insert(Operands.end(), StepChrec->op_begin(),
1806                       StepChrec->op_end());
1807       return getAddRecExpr(Operands, L);
1808     }
1809
1810   Operands.push_back(Step);
1811   return getAddRecExpr(Operands, L);
1812 }
1813
1814 /// getAddRecExpr - Get an add recurrence expression for the specified loop.
1815 /// Simplify the expression as much as possible.
1816 const SCEV *
1817 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
1818                                const Loop *L) {
1819   if (Operands.size() == 1) return Operands[0];
1820 #ifndef NDEBUG
1821   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
1822     assert(getEffectiveSCEVType(Operands[i]->getType()) ==
1823            getEffectiveSCEVType(Operands[0]->getType()) &&
1824            "SCEVAddRecExpr operand types don't match!");
1825 #endif
1826
1827   if (Operands.back()->isZero()) {
1828     Operands.pop_back();
1829     return getAddRecExpr(Operands, L);             // {X,+,0}  -->  X
1830   }
1831
1832   // Canonicalize nested AddRecs in by nesting them in order of loop depth.
1833   if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
1834     const Loop* NestedLoop = NestedAR->getLoop();
1835     if (L->getLoopDepth() < NestedLoop->getLoopDepth()) {
1836       SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
1837                                                 NestedAR->op_end());
1838       Operands[0] = NestedAR->getStart();
1839       // AddRecs require their operands be loop-invariant with respect to their
1840       // loops. Don't perform this transformation if it would break this
1841       // requirement.
1842       bool AllInvariant = true;
1843       for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1844         if (!Operands[i]->isLoopInvariant(L)) {
1845           AllInvariant = false;
1846           break;
1847         }
1848       if (AllInvariant) {
1849         NestedOperands[0] = getAddRecExpr(Operands, L);
1850         AllInvariant = true;
1851         for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
1852           if (!NestedOperands[i]->isLoopInvariant(NestedLoop)) {
1853             AllInvariant = false;
1854             break;
1855           }
1856         if (AllInvariant)
1857           // Ok, both add recurrences are valid after the transformation.
1858           return getAddRecExpr(NestedOperands, NestedLoop);
1859       }
1860       // Reset Operands to its original state.
1861       Operands[0] = NestedAR;
1862     }
1863   }
1864
1865   FoldingSetNodeID ID;
1866   ID.AddInteger(scAddRecExpr);
1867   ID.AddInteger(Operands.size());
1868   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
1869     ID.AddPointer(Operands[i]);
1870   ID.AddPointer(L);
1871   void *IP = 0;
1872   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1873   SCEV *S = SCEVAllocator.Allocate<SCEVAddRecExpr>();
1874   new (S) SCEVAddRecExpr(ID, Operands, L);
1875   UniqueSCEVs.InsertNode(S, IP);
1876   return S;
1877 }
1878
1879 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
1880                                          const SCEV *RHS) {
1881   SmallVector<const SCEV *, 2> Ops;
1882   Ops.push_back(LHS);
1883   Ops.push_back(RHS);
1884   return getSMaxExpr(Ops);
1885 }
1886
1887 const SCEV *
1888 ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
1889   assert(!Ops.empty() && "Cannot get empty smax!");
1890   if (Ops.size() == 1) return Ops[0];
1891 #ifndef NDEBUG
1892   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1893     assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1894            getEffectiveSCEVType(Ops[0]->getType()) &&
1895            "SCEVSMaxExpr operand types don't match!");
1896 #endif
1897
1898   // Sort by complexity, this groups all similar expression types together.
1899   GroupByComplexity(Ops, LI);
1900
1901   // If there are any constants, fold them together.
1902   unsigned Idx = 0;
1903   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1904     ++Idx;
1905     assert(Idx < Ops.size());
1906     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1907       // We found two constants, fold them together!
1908       ConstantInt *Fold = ConstantInt::get(getContext(),
1909                               APIntOps::smax(LHSC->getValue()->getValue(),
1910                                              RHSC->getValue()->getValue()));
1911       Ops[0] = getConstant(Fold);
1912       Ops.erase(Ops.begin()+1);  // Erase the folded element
1913       if (Ops.size() == 1) return Ops[0];
1914       LHSC = cast<SCEVConstant>(Ops[0]);
1915     }
1916
1917     // If we are left with a constant minimum-int, strip it off.
1918     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
1919       Ops.erase(Ops.begin());
1920       --Idx;
1921     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
1922       // If we have an smax with a constant maximum-int, it will always be
1923       // maximum-int.
1924       return Ops[0];
1925     }
1926   }
1927
1928   if (Ops.size() == 1) return Ops[0];
1929
1930   // Find the first SMax
1931   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
1932     ++Idx;
1933
1934   // Check to see if one of the operands is an SMax. If so, expand its operands
1935   // onto our operand list, and recurse to simplify.
1936   if (Idx < Ops.size()) {
1937     bool DeletedSMax = false;
1938     while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
1939       Ops.insert(Ops.end(), SMax->op_begin(), SMax->op_end());
1940       Ops.erase(Ops.begin()+Idx);
1941       DeletedSMax = true;
1942     }
1943
1944     if (DeletedSMax)
1945       return getSMaxExpr(Ops);
1946   }
1947
1948   // Okay, check to see if the same value occurs in the operand list twice.  If
1949   // so, delete one.  Since we sorted the list, these values are required to
1950   // be adjacent.
1951   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
1952     if (Ops[i] == Ops[i+1]) {      //  X smax Y smax Y  -->  X smax Y
1953       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
1954       --i; --e;
1955     }
1956
1957   if (Ops.size() == 1) return Ops[0];
1958
1959   assert(!Ops.empty() && "Reduced smax down to nothing!");
1960
1961   // Okay, it looks like we really DO need an smax expr.  Check to see if we
1962   // already have one, otherwise create a new one.
1963   FoldingSetNodeID ID;
1964   ID.AddInteger(scSMaxExpr);
1965   ID.AddInteger(Ops.size());
1966   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1967     ID.AddPointer(Ops[i]);
1968   void *IP = 0;
1969   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1970   SCEV *S = SCEVAllocator.Allocate<SCEVSMaxExpr>();
1971   new (S) SCEVSMaxExpr(ID, Ops);
1972   UniqueSCEVs.InsertNode(S, IP);
1973   return S;
1974 }
1975
1976 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
1977                                          const SCEV *RHS) {
1978   SmallVector<const SCEV *, 2> Ops;
1979   Ops.push_back(LHS);
1980   Ops.push_back(RHS);
1981   return getUMaxExpr(Ops);
1982 }
1983
1984 const SCEV *
1985 ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
1986   assert(!Ops.empty() && "Cannot get empty umax!");
1987   if (Ops.size() == 1) return Ops[0];
1988 #ifndef NDEBUG
1989   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1990     assert(getEffectiveSCEVType(Ops[i]->getType()) ==
1991            getEffectiveSCEVType(Ops[0]->getType()) &&
1992            "SCEVUMaxExpr operand types don't match!");
1993 #endif
1994
1995   // Sort by complexity, this groups all similar expression types together.
1996   GroupByComplexity(Ops, LI);
1997
1998   // If there are any constants, fold them together.
1999   unsigned Idx = 0;
2000   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2001     ++Idx;
2002     assert(Idx < Ops.size());
2003     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2004       // We found two constants, fold them together!
2005       ConstantInt *Fold = ConstantInt::get(getContext(),
2006                               APIntOps::umax(LHSC->getValue()->getValue(),
2007                                              RHSC->getValue()->getValue()));
2008       Ops[0] = getConstant(Fold);
2009       Ops.erase(Ops.begin()+1);  // Erase the folded element
2010       if (Ops.size() == 1) return Ops[0];
2011       LHSC = cast<SCEVConstant>(Ops[0]);
2012     }
2013
2014     // If we are left with a constant minimum-int, strip it off.
2015     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
2016       Ops.erase(Ops.begin());
2017       --Idx;
2018     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
2019       // If we have an umax with a constant maximum-int, it will always be
2020       // maximum-int.
2021       return Ops[0];
2022     }
2023   }
2024
2025   if (Ops.size() == 1) return Ops[0];
2026
2027   // Find the first UMax
2028   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
2029     ++Idx;
2030
2031   // Check to see if one of the operands is a UMax. If so, expand its operands
2032   // onto our operand list, and recurse to simplify.
2033   if (Idx < Ops.size()) {
2034     bool DeletedUMax = false;
2035     while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
2036       Ops.insert(Ops.end(), UMax->op_begin(), UMax->op_end());
2037       Ops.erase(Ops.begin()+Idx);
2038       DeletedUMax = true;
2039     }
2040
2041     if (DeletedUMax)
2042       return getUMaxExpr(Ops);
2043   }
2044
2045   // Okay, check to see if the same value occurs in the operand list twice.  If
2046   // so, delete one.  Since we sorted the list, these values are required to
2047   // be adjacent.
2048   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
2049     if (Ops[i] == Ops[i+1]) {      //  X umax Y umax Y  -->  X umax Y
2050       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
2051       --i; --e;
2052     }
2053
2054   if (Ops.size() == 1) return Ops[0];
2055
2056   assert(!Ops.empty() && "Reduced umax down to nothing!");
2057
2058   // Okay, it looks like we really DO need a umax expr.  Check to see if we
2059   // already have one, otherwise create a new one.
2060   FoldingSetNodeID ID;
2061   ID.AddInteger(scUMaxExpr);
2062   ID.AddInteger(Ops.size());
2063   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2064     ID.AddPointer(Ops[i]);
2065   void *IP = 0;
2066   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2067   SCEV *S = SCEVAllocator.Allocate<SCEVUMaxExpr>();
2068   new (S) SCEVUMaxExpr(ID, Ops);
2069   UniqueSCEVs.InsertNode(S, IP);
2070   return S;
2071 }
2072
2073 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
2074                                          const SCEV *RHS) {
2075   // ~smax(~x, ~y) == smin(x, y).
2076   return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2077 }
2078
2079 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
2080                                          const SCEV *RHS) {
2081   // ~umax(~x, ~y) == umin(x, y)
2082   return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2083 }
2084
2085 const SCEV *ScalarEvolution::getFieldOffsetExpr(const StructType *STy,
2086                                                 unsigned FieldNo) {
2087   // If we have TargetData we can determine the constant offset.
2088   if (TD) {
2089     const Type *IntPtrTy = TD->getIntPtrType(getContext());
2090     const StructLayout &SL = *TD->getStructLayout(STy);
2091     uint64_t Offset = SL.getElementOffset(FieldNo);
2092     return getIntegerSCEV(Offset, IntPtrTy);
2093   }
2094
2095   // Field 0 is always at offset 0.
2096   if (FieldNo == 0) {
2097     const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
2098     return getIntegerSCEV(0, Ty);
2099   }
2100
2101   // Okay, it looks like we really DO need an offsetof expr.  Check to see if we
2102   // already have one, otherwise create a new one.
2103   FoldingSetNodeID ID;
2104   ID.AddInteger(scFieldOffset);
2105   ID.AddPointer(STy);
2106   ID.AddInteger(FieldNo);
2107   void *IP = 0;
2108   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2109   SCEV *S = SCEVAllocator.Allocate<SCEVFieldOffsetExpr>();
2110   const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
2111   new (S) SCEVFieldOffsetExpr(ID, Ty, STy, FieldNo);
2112   UniqueSCEVs.InsertNode(S, IP);
2113   return S;
2114 }
2115
2116 const SCEV *ScalarEvolution::getAllocSizeExpr(const Type *AllocTy) {
2117   // If we have TargetData we can determine the constant size.
2118   if (TD && AllocTy->isSized()) {
2119     const Type *IntPtrTy = TD->getIntPtrType(getContext());
2120     return getIntegerSCEV(TD->getTypeAllocSize(AllocTy), IntPtrTy);
2121   }
2122
2123   // Expand an array size into the element size times the number
2124   // of elements.
2125   if (const ArrayType *ATy = dyn_cast<ArrayType>(AllocTy)) {
2126     const SCEV *E = getAllocSizeExpr(ATy->getElementType());
2127     return getMulExpr(
2128       E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()),
2129                                       ATy->getNumElements())));
2130   }
2131
2132   // Expand a vector size into the element size times the number
2133   // of elements.
2134   if (const VectorType *VTy = dyn_cast<VectorType>(AllocTy)) {
2135     const SCEV *E = getAllocSizeExpr(VTy->getElementType());
2136     return getMulExpr(
2137       E, getConstant(ConstantInt::get(cast<IntegerType>(E->getType()),
2138                                       VTy->getNumElements())));
2139   }
2140
2141   // Okay, it looks like we really DO need a sizeof expr.  Check to see if we
2142   // already have one, otherwise create a new one.
2143   FoldingSetNodeID ID;
2144   ID.AddInteger(scAllocSize);
2145   ID.AddPointer(AllocTy);
2146   void *IP = 0;
2147   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2148   SCEV *S = SCEVAllocator.Allocate<SCEVAllocSizeExpr>();
2149   const Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
2150   new (S) SCEVAllocSizeExpr(ID, Ty, AllocTy);
2151   UniqueSCEVs.InsertNode(S, IP);
2152   return S;
2153 }
2154
2155 const SCEV *ScalarEvolution::getUnknown(Value *V) {
2156   // Don't attempt to do anything other than create a SCEVUnknown object
2157   // here.  createSCEV only calls getUnknown after checking for all other
2158   // interesting possibilities, and any other code that calls getUnknown
2159   // is doing so in order to hide a value from SCEV canonicalization.
2160
2161   FoldingSetNodeID ID;
2162   ID.AddInteger(scUnknown);
2163   ID.AddPointer(V);
2164   void *IP = 0;
2165   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2166   SCEV *S = SCEVAllocator.Allocate<SCEVUnknown>();
2167   new (S) SCEVUnknown(ID, V);
2168   UniqueSCEVs.InsertNode(S, IP);
2169   return S;
2170 }
2171
2172 //===----------------------------------------------------------------------===//
2173 //            Basic SCEV Analysis and PHI Idiom Recognition Code
2174 //
2175
2176 /// isSCEVable - Test if values of the given type are analyzable within
2177 /// the SCEV framework. This primarily includes integer types, and it
2178 /// can optionally include pointer types if the ScalarEvolution class
2179 /// has access to target-specific information.
2180 bool ScalarEvolution::isSCEVable(const Type *Ty) const {
2181   // Integers and pointers are always SCEVable.
2182   return Ty->isInteger() || isa<PointerType>(Ty);
2183 }
2184
2185 /// getTypeSizeInBits - Return the size in bits of the specified type,
2186 /// for which isSCEVable must return true.
2187 uint64_t ScalarEvolution::getTypeSizeInBits(const Type *Ty) const {
2188   assert(isSCEVable(Ty) && "Type is not SCEVable!");
2189
2190   // If we have a TargetData, use it!
2191   if (TD)
2192     return TD->getTypeSizeInBits(Ty);
2193
2194   // Integer types have fixed sizes.
2195   if (Ty->isInteger())
2196     return Ty->getPrimitiveSizeInBits();
2197
2198   // The only other support type is pointer. Without TargetData, conservatively
2199   // assume pointers are 64-bit.
2200   assert(isa<PointerType>(Ty) && "isSCEVable permitted a non-SCEVable type!");
2201   return 64;
2202 }
2203
2204 /// getEffectiveSCEVType - Return a type with the same bitwidth as
2205 /// the given type and which represents how SCEV will treat the given
2206 /// type, for which isSCEVable must return true. For pointer types,
2207 /// this is the pointer-sized integer type.
2208 const Type *ScalarEvolution::getEffectiveSCEVType(const Type *Ty) const {
2209   assert(isSCEVable(Ty) && "Type is not SCEVable!");
2210
2211   if (Ty->isInteger())
2212     return Ty;
2213
2214   // The only other support type is pointer.
2215   assert(isa<PointerType>(Ty) && "Unexpected non-pointer non-integer type!");
2216   if (TD) return TD->getIntPtrType(getContext());
2217
2218   // Without TargetData, conservatively assume pointers are 64-bit.
2219   return Type::getInt64Ty(getContext());
2220 }
2221
2222 const SCEV *ScalarEvolution::getCouldNotCompute() {
2223   return &CouldNotCompute;
2224 }
2225
2226 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
2227 /// expression and create a new one.
2228 const SCEV *ScalarEvolution::getSCEV(Value *V) {
2229   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
2230
2231   std::map<SCEVCallbackVH, const SCEV *>::iterator I = Scalars.find(V);
2232   if (I != Scalars.end()) return I->second;
2233   const SCEV *S = createSCEV(V);
2234   Scalars.insert(std::make_pair(SCEVCallbackVH(V, this), S));
2235   return S;
2236 }
2237
2238 /// getIntegerSCEV - Given a SCEVable type, create a constant for the
2239 /// specified signed integer value and return a SCEV for the constant.
2240 const SCEV *ScalarEvolution::getIntegerSCEV(int Val, const Type *Ty) {
2241   const IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
2242   return getConstant(ConstantInt::get(ITy, Val));
2243 }
2244
2245 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
2246 ///
2247 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
2248   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
2249     return getConstant(
2250                cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
2251
2252   const Type *Ty = V->getType();
2253   Ty = getEffectiveSCEVType(Ty);
2254   return getMulExpr(V,
2255                   getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))));
2256 }
2257
2258 /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
2259 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
2260   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
2261     return getConstant(
2262                 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
2263
2264   const Type *Ty = V->getType();
2265   Ty = getEffectiveSCEVType(Ty);
2266   const SCEV *AllOnes =
2267                    getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
2268   return getMinusSCEV(AllOnes, V);
2269 }
2270
2271 /// getMinusSCEV - Return a SCEV corresponding to LHS - RHS.
2272 ///
2273 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS,
2274                                           const SCEV *RHS) {
2275   // X - Y --> X + -Y
2276   return getAddExpr(LHS, getNegativeSCEV(RHS));
2277 }
2278
2279 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
2280 /// input value to the specified type.  If the type must be extended, it is zero
2281 /// extended.
2282 const SCEV *
2283 ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V,
2284                                          const Type *Ty) {
2285   const Type *SrcTy = V->getType();
2286   assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2287          (Ty->isInteger() || isa<PointerType>(Ty)) &&
2288          "Cannot truncate or zero extend with non-integer arguments!");
2289   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2290     return V;  // No conversion
2291   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
2292     return getTruncateExpr(V, Ty);
2293   return getZeroExtendExpr(V, Ty);
2294 }
2295
2296 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
2297 /// input value to the specified type.  If the type must be extended, it is sign
2298 /// extended.
2299 const SCEV *
2300 ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
2301                                          const Type *Ty) {
2302   const Type *SrcTy = V->getType();
2303   assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2304          (Ty->isInteger() || isa<PointerType>(Ty)) &&
2305          "Cannot truncate or zero extend with non-integer arguments!");
2306   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2307     return V;  // No conversion
2308   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
2309     return getTruncateExpr(V, Ty);
2310   return getSignExtendExpr(V, Ty);
2311 }
2312
2313 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
2314 /// input value to the specified type.  If the type must be extended, it is zero
2315 /// extended.  The conversion must not be narrowing.
2316 const SCEV *
2317 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, const Type *Ty) {
2318   const Type *SrcTy = V->getType();
2319   assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2320          (Ty->isInteger() || isa<PointerType>(Ty)) &&
2321          "Cannot noop or zero extend with non-integer arguments!");
2322   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2323          "getNoopOrZeroExtend cannot truncate!");
2324   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2325     return V;  // No conversion
2326   return getZeroExtendExpr(V, Ty);
2327 }
2328
2329 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
2330 /// input value to the specified type.  If the type must be extended, it is sign
2331 /// extended.  The conversion must not be narrowing.
2332 const SCEV *
2333 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, const Type *Ty) {
2334   const Type *SrcTy = V->getType();
2335   assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2336          (Ty->isInteger() || isa<PointerType>(Ty)) &&
2337          "Cannot noop or sign extend with non-integer arguments!");
2338   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2339          "getNoopOrSignExtend cannot truncate!");
2340   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2341     return V;  // No conversion
2342   return getSignExtendExpr(V, Ty);
2343 }
2344
2345 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
2346 /// the input value to the specified type. If the type must be extended,
2347 /// it is extended with unspecified bits. The conversion must not be
2348 /// narrowing.
2349 const SCEV *
2350 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, const Type *Ty) {
2351   const Type *SrcTy = V->getType();
2352   assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2353          (Ty->isInteger() || isa<PointerType>(Ty)) &&
2354          "Cannot noop or any extend with non-integer arguments!");
2355   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2356          "getNoopOrAnyExtend cannot truncate!");
2357   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2358     return V;  // No conversion
2359   return getAnyExtendExpr(V, Ty);
2360 }
2361
2362 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
2363 /// input value to the specified type.  The conversion must not be widening.
2364 const SCEV *
2365 ScalarEvolution::getTruncateOrNoop(const SCEV *V, const Type *Ty) {
2366   const Type *SrcTy = V->getType();
2367   assert((SrcTy->isInteger() || isa<PointerType>(SrcTy)) &&
2368          (Ty->isInteger() || isa<PointerType>(Ty)) &&
2369          "Cannot truncate or noop with non-integer arguments!");
2370   assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
2371          "getTruncateOrNoop cannot extend!");
2372   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2373     return V;  // No conversion
2374   return getTruncateExpr(V, Ty);
2375 }
2376
2377 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2378 /// the types using zero-extension, and then perform a umax operation
2379 /// with them.
2380 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
2381                                                         const SCEV *RHS) {
2382   const SCEV *PromotedLHS = LHS;
2383   const SCEV *PromotedRHS = RHS;
2384
2385   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2386     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2387   else
2388     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2389
2390   return getUMaxExpr(PromotedLHS, PromotedRHS);
2391 }
2392
2393 /// getUMinFromMismatchedTypes - Promote the operands to the wider of
2394 /// the types using zero-extension, and then perform a umin operation
2395 /// with them.
2396 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
2397                                                         const SCEV *RHS) {
2398   const SCEV *PromotedLHS = LHS;
2399   const SCEV *PromotedRHS = RHS;
2400
2401   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2402     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2403   else
2404     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2405
2406   return getUMinExpr(PromotedLHS, PromotedRHS);
2407 }
2408
2409 /// PushDefUseChildren - Push users of the given Instruction
2410 /// onto the given Worklist.
2411 static void
2412 PushDefUseChildren(Instruction *I,
2413                    SmallVectorImpl<Instruction *> &Worklist) {
2414   // Push the def-use children onto the Worklist stack.
2415   for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
2416        UI != UE; ++UI)
2417     Worklist.push_back(cast<Instruction>(UI));
2418 }
2419
2420 /// ForgetSymbolicValue - This looks up computed SCEV values for all
2421 /// instructions that depend on the given instruction and removes them from
2422 /// the Scalars map if they reference SymName. This is used during PHI
2423 /// resolution.
2424 void
2425 ScalarEvolution::ForgetSymbolicName(Instruction *I, const SCEV *SymName) {
2426   SmallVector<Instruction *, 16> Worklist;
2427   PushDefUseChildren(I, Worklist);
2428
2429   SmallPtrSet<Instruction *, 8> Visited;
2430   Visited.insert(I);
2431   while (!Worklist.empty()) {
2432     Instruction *I = Worklist.pop_back_val();
2433     if (!Visited.insert(I)) continue;
2434
2435     std::map<SCEVCallbackVH, const SCEV*>::iterator It =
2436       Scalars.find(static_cast<Value *>(I));
2437     if (It != Scalars.end()) {
2438       // Short-circuit the def-use traversal if the symbolic name
2439       // ceases to appear in expressions.
2440       if (!It->second->hasOperand(SymName))
2441         continue;
2442
2443       // SCEVUnknown for a PHI either means that it has an unrecognized
2444       // structure, or it's a PHI that's in the progress of being computed
2445       // by createNodeForPHI.  In the former case, additional loop trip
2446       // count information isn't going to change anything. In the later
2447       // case, createNodeForPHI will perform the necessary updates on its
2448       // own when it gets to that point.
2449       if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) {
2450         ValuesAtScopes.erase(It->second);
2451         Scalars.erase(It);
2452       }
2453     }
2454
2455     PushDefUseChildren(I, Worklist);
2456   }
2457 }
2458
2459 /// createNodeForPHI - PHI nodes have two cases.  Either the PHI node exists in
2460 /// a loop header, making it a potential recurrence, or it doesn't.
2461 ///
2462 const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
2463   if (PN->getNumIncomingValues() == 2)  // The loops have been canonicalized.
2464     if (const Loop *L = LI->getLoopFor(PN->getParent()))
2465       if (L->getHeader() == PN->getParent()) {
2466         // If it lives in the loop header, it has two incoming values, one
2467         // from outside the loop, and one from inside.
2468         unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0));
2469         unsigned BackEdge     = IncomingEdge^1;
2470
2471         // While we are analyzing this PHI node, handle its value symbolically.
2472         const SCEV *SymbolicName = getUnknown(PN);
2473         assert(Scalars.find(PN) == Scalars.end() &&
2474                "PHI node already processed?");
2475         Scalars.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
2476
2477         // Using this symbolic name for the PHI, analyze the value coming around
2478         // the back-edge.
2479         Value *BEValueV = PN->getIncomingValue(BackEdge);
2480         const SCEV *BEValue = getSCEV(BEValueV);
2481
2482         // NOTE: If BEValue is loop invariant, we know that the PHI node just
2483         // has a special value for the first iteration of the loop.
2484
2485         // If the value coming around the backedge is an add with the symbolic
2486         // value we just inserted, then we found a simple induction variable!
2487         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
2488           // If there is a single occurrence of the symbolic value, replace it
2489           // with a recurrence.
2490           unsigned FoundIndex = Add->getNumOperands();
2491           for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2492             if (Add->getOperand(i) == SymbolicName)
2493               if (FoundIndex == e) {
2494                 FoundIndex = i;
2495                 break;
2496               }
2497
2498           if (FoundIndex != Add->getNumOperands()) {
2499             // Create an add with everything but the specified operand.
2500             SmallVector<const SCEV *, 8> Ops;
2501             for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
2502               if (i != FoundIndex)
2503                 Ops.push_back(Add->getOperand(i));
2504             const SCEV *Accum = getAddExpr(Ops);
2505
2506             // This is not a valid addrec if the step amount is varying each
2507             // loop iteration, but is not itself an addrec in this loop.
2508             if (Accum->isLoopInvariant(L) ||
2509                 (isa<SCEVAddRecExpr>(Accum) &&
2510                  cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
2511               const SCEV *StartVal =
2512                 getSCEV(PN->getIncomingValue(IncomingEdge));
2513               const SCEVAddRecExpr *PHISCEV =
2514                 cast<SCEVAddRecExpr>(getAddRecExpr(StartVal, Accum, L));
2515
2516               // If the increment doesn't overflow, then neither the addrec nor the
2517               // post-increment will overflow.
2518               if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV))
2519                 if (OBO->getOperand(0) == PN &&
2520                     getSCEV(OBO->getOperand(1)) ==
2521                       PHISCEV->getStepRecurrence(*this)) {
2522                   const SCEVAddRecExpr *PostInc = PHISCEV->getPostIncExpr(*this);
2523                   if (OBO->hasNoUnsignedWrap()) {
2524                     const_cast<SCEVAddRecExpr *>(PHISCEV)
2525                       ->setHasNoUnsignedWrap(true);
2526                     const_cast<SCEVAddRecExpr *>(PostInc)
2527                       ->setHasNoUnsignedWrap(true);
2528                   }
2529                   if (OBO->hasNoSignedWrap()) {
2530                     const_cast<SCEVAddRecExpr *>(PHISCEV)
2531                       ->setHasNoSignedWrap(true);
2532                     const_cast<SCEVAddRecExpr *>(PostInc)
2533                       ->setHasNoSignedWrap(true);
2534                   }
2535                 }
2536
2537               // Okay, for the entire analysis of this edge we assumed the PHI
2538               // to be symbolic.  We now need to go back and purge all of the
2539               // entries for the scalars that use the symbolic expression.
2540               ForgetSymbolicName(PN, SymbolicName);
2541               Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
2542               return PHISCEV;
2543             }
2544           }
2545         } else if (const SCEVAddRecExpr *AddRec =
2546                      dyn_cast<SCEVAddRecExpr>(BEValue)) {
2547           // Otherwise, this could be a loop like this:
2548           //     i = 0;  for (j = 1; ..; ++j) { ....  i = j; }
2549           // In this case, j = {1,+,1}  and BEValue is j.
2550           // Because the other in-value of i (0) fits the evolution of BEValue
2551           // i really is an addrec evolution.
2552           if (AddRec->getLoop() == L && AddRec->isAffine()) {
2553             const SCEV *StartVal = getSCEV(PN->getIncomingValue(IncomingEdge));
2554
2555             // If StartVal = j.start - j.stride, we can use StartVal as the
2556             // initial step of the addrec evolution.
2557             if (StartVal == getMinusSCEV(AddRec->getOperand(0),
2558                                             AddRec->getOperand(1))) {
2559               const SCEV *PHISCEV =
2560                  getAddRecExpr(StartVal, AddRec->getOperand(1), L);
2561
2562               // Okay, for the entire analysis of this edge we assumed the PHI
2563               // to be symbolic.  We now need to go back and purge all of the
2564               // entries for the scalars that use the symbolic expression.
2565               ForgetSymbolicName(PN, SymbolicName);
2566               Scalars[SCEVCallbackVH(PN, this)] = PHISCEV;
2567               return PHISCEV;
2568             }
2569           }
2570         }
2571
2572         return SymbolicName;
2573       }
2574
2575   // It's tempting to recognize PHIs with a unique incoming value, however
2576   // this leads passes like indvars to break LCSSA form. Fortunately, such
2577   // PHIs are rare, as instcombine zaps them.
2578
2579   // If it's not a loop phi, we can't handle it yet.
2580   return getUnknown(PN);
2581 }
2582
2583 /// createNodeForGEP - Expand GEP instructions into add and multiply
2584 /// operations. This allows them to be analyzed by regular SCEV code.
2585 ///
2586 const SCEV *ScalarEvolution::createNodeForGEP(Operator *GEP) {
2587
2588   const Type *IntPtrTy = getEffectiveSCEVType(GEP->getType());
2589   Value *Base = GEP->getOperand(0);
2590   // Don't attempt to analyze GEPs over unsized objects.
2591   if (!cast<PointerType>(Base->getType())->getElementType()->isSized())
2592     return getUnknown(GEP);
2593   const SCEV *TotalOffset = getIntegerSCEV(0, IntPtrTy);
2594   gep_type_iterator GTI = gep_type_begin(GEP);
2595   for (GetElementPtrInst::op_iterator I = next(GEP->op_begin()),
2596                                       E = GEP->op_end();
2597        I != E; ++I) {
2598     Value *Index = *I;
2599     // Compute the (potentially symbolic) offset in bytes for this index.
2600     if (const StructType *STy = dyn_cast<StructType>(*GTI++)) {
2601       // For a struct, add the member offset.
2602       unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
2603       TotalOffset = getAddExpr(TotalOffset,
2604                                getFieldOffsetExpr(STy, FieldNo));
2605     } else {
2606       // For an array, add the element offset, explicitly scaled.
2607       const SCEV *LocalOffset = getSCEV(Index);
2608       if (!isa<PointerType>(LocalOffset->getType()))
2609         // Getelementptr indicies are signed.
2610         LocalOffset = getTruncateOrSignExtend(LocalOffset, IntPtrTy);
2611       LocalOffset = getMulExpr(LocalOffset, getAllocSizeExpr(*GTI));
2612       TotalOffset = getAddExpr(TotalOffset, LocalOffset);
2613     }
2614   }
2615   return getAddExpr(getSCEV(Base), TotalOffset);
2616 }
2617
2618 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
2619 /// guaranteed to end in (at every loop iteration).  It is, at the same time,
2620 /// the minimum number of times S is divisible by 2.  For example, given {4,+,8}
2621 /// it returns 2.  If S is guaranteed to be 0, it returns the bitwidth of S.
2622 uint32_t
2623 ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
2624   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2625     return C->getValue()->getValue().countTrailingZeros();
2626
2627   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
2628     return std::min(GetMinTrailingZeros(T->getOperand()),
2629                     (uint32_t)getTypeSizeInBits(T->getType()));
2630
2631   if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
2632     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2633     return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2634              getTypeSizeInBits(E->getType()) : OpRes;
2635   }
2636
2637   if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
2638     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
2639     return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
2640              getTypeSizeInBits(E->getType()) : OpRes;
2641   }
2642
2643   if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
2644     // The result is the min of all operands results.
2645     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
2646     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
2647       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
2648     return MinOpRes;
2649   }
2650
2651   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
2652     // The result is the sum of all operands results.
2653     uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
2654     uint32_t BitWidth = getTypeSizeInBits(M->getType());
2655     for (unsigned i = 1, e = M->getNumOperands();
2656          SumOpRes != BitWidth && i != e; ++i)
2657       SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
2658                           BitWidth);
2659     return SumOpRes;
2660   }
2661
2662   if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
2663     // The result is the min of all operands results.
2664     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
2665     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
2666       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
2667     return MinOpRes;
2668   }
2669
2670   if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
2671     // The result is the min of all operands results.
2672     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
2673     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
2674       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
2675     return MinOpRes;
2676   }
2677
2678   if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
2679     // The result is the min of all operands results.
2680     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
2681     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
2682       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
2683     return MinOpRes;
2684   }
2685
2686   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2687     // For a SCEVUnknown, ask ValueTracking.
2688     unsigned BitWidth = getTypeSizeInBits(U->getType());
2689     APInt Mask = APInt::getAllOnesValue(BitWidth);
2690     APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2691     ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones);
2692     return Zeros.countTrailingOnes();
2693   }
2694
2695   // SCEVUDivExpr
2696   return 0;
2697 }
2698
2699 /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
2700 ///
2701 ConstantRange
2702 ScalarEvolution::getUnsignedRange(const SCEV *S) {
2703
2704   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2705     return ConstantRange(C->getValue()->getValue());
2706
2707   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2708     ConstantRange X = getUnsignedRange(Add->getOperand(0));
2709     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2710       X = X.add(getUnsignedRange(Add->getOperand(i)));
2711     return X;
2712   }
2713
2714   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2715     ConstantRange X = getUnsignedRange(Mul->getOperand(0));
2716     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2717       X = X.multiply(getUnsignedRange(Mul->getOperand(i)));
2718     return X;
2719   }
2720
2721   if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2722     ConstantRange X = getUnsignedRange(SMax->getOperand(0));
2723     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2724       X = X.smax(getUnsignedRange(SMax->getOperand(i)));
2725     return X;
2726   }
2727
2728   if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2729     ConstantRange X = getUnsignedRange(UMax->getOperand(0));
2730     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2731       X = X.umax(getUnsignedRange(UMax->getOperand(i)));
2732     return X;
2733   }
2734
2735   if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2736     ConstantRange X = getUnsignedRange(UDiv->getLHS());
2737     ConstantRange Y = getUnsignedRange(UDiv->getRHS());
2738     return X.udiv(Y);
2739   }
2740
2741   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2742     ConstantRange X = getUnsignedRange(ZExt->getOperand());
2743     return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
2744   }
2745
2746   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2747     ConstantRange X = getUnsignedRange(SExt->getOperand());
2748     return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
2749   }
2750
2751   if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
2752     ConstantRange X = getUnsignedRange(Trunc->getOperand());
2753     return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
2754   }
2755
2756   ConstantRange FullSet(getTypeSizeInBits(S->getType()), true);
2757
2758   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
2759     const SCEV *T = getBackedgeTakenCount(AddRec->getLoop());
2760     const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
2761     if (!Trip) return FullSet;
2762
2763     // TODO: non-affine addrec
2764     if (AddRec->isAffine()) {
2765       const Type *Ty = AddRec->getType();
2766       const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
2767       if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) {
2768         MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
2769
2770         const SCEV *Start = AddRec->getStart();
2771         const SCEV *Step = AddRec->getStepRecurrence(*this);
2772         const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
2773
2774         // Check for overflow.
2775         // TODO: This is very conservative.
2776         if (!(Step->isOne() &&
2777               isKnownPredicate(ICmpInst::ICMP_ULT, Start, End)) &&
2778             !(Step->isAllOnesValue() &&
2779               isKnownPredicate(ICmpInst::ICMP_UGT, Start, End)))
2780           return FullSet;
2781
2782         ConstantRange StartRange = getUnsignedRange(Start);
2783         ConstantRange EndRange = getUnsignedRange(End);
2784         APInt Min = APIntOps::umin(StartRange.getUnsignedMin(),
2785                                    EndRange.getUnsignedMin());
2786         APInt Max = APIntOps::umax(StartRange.getUnsignedMax(),
2787                                    EndRange.getUnsignedMax());
2788         if (Min.isMinValue() && Max.isMaxValue())
2789           return FullSet;
2790         return ConstantRange(Min, Max+1);
2791       }
2792     }
2793   }
2794
2795   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2796     // For a SCEVUnknown, ask ValueTracking.
2797     unsigned BitWidth = getTypeSizeInBits(U->getType());
2798     APInt Mask = APInt::getAllOnesValue(BitWidth);
2799     APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
2800     ComputeMaskedBits(U->getValue(), Mask, Zeros, Ones, TD);
2801     if (Ones == ~Zeros + 1)
2802       return FullSet;
2803     return ConstantRange(Ones, ~Zeros + 1);
2804   }
2805
2806   return FullSet;
2807 }
2808
2809 /// getSignedRange - Determine the signed range for a particular SCEV.
2810 ///
2811 ConstantRange
2812 ScalarEvolution::getSignedRange(const SCEV *S) {
2813
2814   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
2815     return ConstantRange(C->getValue()->getValue());
2816
2817   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
2818     ConstantRange X = getSignedRange(Add->getOperand(0));
2819     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
2820       X = X.add(getSignedRange(Add->getOperand(i)));
2821     return X;
2822   }
2823
2824   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
2825     ConstantRange X = getSignedRange(Mul->getOperand(0));
2826     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
2827       X = X.multiply(getSignedRange(Mul->getOperand(i)));
2828     return X;
2829   }
2830
2831   if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
2832     ConstantRange X = getSignedRange(SMax->getOperand(0));
2833     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
2834       X = X.smax(getSignedRange(SMax->getOperand(i)));
2835     return X;
2836   }
2837
2838   if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
2839     ConstantRange X = getSignedRange(UMax->getOperand(0));
2840     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
2841       X = X.umax(getSignedRange(UMax->getOperand(i)));
2842     return X;
2843   }
2844
2845   if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
2846     ConstantRange X = getSignedRange(UDiv->getLHS());
2847     ConstantRange Y = getSignedRange(UDiv->getRHS());
2848     return X.udiv(Y);
2849   }
2850
2851   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
2852     ConstantRange X = getSignedRange(ZExt->getOperand());
2853     return X.zeroExtend(cast<IntegerType>(ZExt->getType())->getBitWidth());
2854   }
2855
2856   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
2857     ConstantRange X = getSignedRange(SExt->getOperand());
2858     return X.signExtend(cast<IntegerType>(SExt->getType())->getBitWidth());
2859   }
2860
2861   if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
2862     ConstantRange X = getSignedRange(Trunc->getOperand());
2863     return X.truncate(cast<IntegerType>(Trunc->getType())->getBitWidth());
2864   }
2865
2866   ConstantRange FullSet(getTypeSizeInBits(S->getType()), true);
2867
2868   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
2869     const SCEV *T = getBackedgeTakenCount(AddRec->getLoop());
2870     const SCEVConstant *Trip = dyn_cast<SCEVConstant>(T);
2871     if (!Trip) return FullSet;
2872
2873     // TODO: non-affine addrec
2874     if (AddRec->isAffine()) {
2875       const Type *Ty = AddRec->getType();
2876       const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
2877       if (getTypeSizeInBits(MaxBECount->getType()) <= getTypeSizeInBits(Ty)) {
2878         MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
2879
2880         const SCEV *Start = AddRec->getStart();
2881         const SCEV *Step = AddRec->getStepRecurrence(*this);
2882         const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
2883
2884         // Check for overflow.
2885         // TODO: This is very conservative.
2886         if (!(Step->isOne() &&
2887               isKnownPredicate(ICmpInst::ICMP_SLT, Start, End)) &&
2888             !(Step->isAllOnesValue() &&
2889               isKnownPredicate(ICmpInst::ICMP_SGT, Start, End)))
2890           return FullSet;
2891
2892         ConstantRange StartRange = getSignedRange(Start);
2893         ConstantRange EndRange = getSignedRange(End);
2894         APInt Min = APIntOps::smin(StartRange.getSignedMin(),
2895                                    EndRange.getSignedMin());
2896         APInt Max = APIntOps::smax(StartRange.getSignedMax(),
2897                                    EndRange.getSignedMax());
2898         if (Min.isMinSignedValue() && Max.isMaxSignedValue())
2899           return FullSet;
2900         return ConstantRange(Min, Max+1);
2901       }
2902     }
2903   }
2904
2905   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
2906     // For a SCEVUnknown, ask ValueTracking.
2907     unsigned BitWidth = getTypeSizeInBits(U->getType());
2908     unsigned NS = ComputeNumSignBits(U->getValue(), TD);
2909     if (NS == 1)
2910       return FullSet;
2911     return
2912       ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
2913                     APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1);
2914   }
2915
2916   return FullSet;
2917 }
2918
2919 /// createSCEV - We know that there is no SCEV for the specified value.
2920 /// Analyze the expression.
2921 ///
2922 const SCEV *ScalarEvolution::createSCEV(Value *V) {
2923   if (!isSCEVable(V->getType()))
2924     return getUnknown(V);
2925
2926   unsigned Opcode = Instruction::UserOp1;
2927   if (Instruction *I = dyn_cast<Instruction>(V))
2928     Opcode = I->getOpcode();
2929   else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
2930     Opcode = CE->getOpcode();
2931   else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
2932     return getConstant(CI);
2933   else if (isa<ConstantPointerNull>(V))
2934     return getIntegerSCEV(0, V->getType());
2935   else if (isa<UndefValue>(V))
2936     return getIntegerSCEV(0, V->getType());
2937   else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
2938     return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee());
2939   else
2940     return getUnknown(V);
2941
2942   Operator *U = cast<Operator>(V);
2943   switch (Opcode) {
2944   case Instruction::Add:
2945     return getAddExpr(getSCEV(U->getOperand(0)),
2946                       getSCEV(U->getOperand(1)));
2947   case Instruction::Mul:
2948     return getMulExpr(getSCEV(U->getOperand(0)),
2949                       getSCEV(U->getOperand(1)));
2950   case Instruction::UDiv:
2951     return getUDivExpr(getSCEV(U->getOperand(0)),
2952                        getSCEV(U->getOperand(1)));
2953   case Instruction::Sub:
2954     return getMinusSCEV(getSCEV(U->getOperand(0)),
2955                         getSCEV(U->getOperand(1)));
2956   case Instruction::And:
2957     // For an expression like x&255 that merely masks off the high bits,
2958     // use zext(trunc(x)) as the SCEV expression.
2959     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
2960       if (CI->isNullValue())
2961         return getSCEV(U->getOperand(1));
2962       if (CI->isAllOnesValue())
2963         return getSCEV(U->getOperand(0));
2964       const APInt &A = CI->getValue();
2965
2966       // Instcombine's ShrinkDemandedConstant may strip bits out of
2967       // constants, obscuring what would otherwise be a low-bits mask.
2968       // Use ComputeMaskedBits to compute what ShrinkDemandedConstant
2969       // knew about to reconstruct a low-bits mask value.
2970       unsigned LZ = A.countLeadingZeros();
2971       unsigned BitWidth = A.getBitWidth();
2972       APInt AllOnes = APInt::getAllOnesValue(BitWidth);
2973       APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
2974       ComputeMaskedBits(U->getOperand(0), AllOnes, KnownZero, KnownOne, TD);
2975
2976       APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ);
2977
2978       if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask))
2979         return
2980           getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)),
2981                                 IntegerType::get(getContext(), BitWidth - LZ)),
2982                             U->getType());
2983     }
2984     break;
2985
2986   case Instruction::Or:
2987     // If the RHS of the Or is a constant, we may have something like:
2988     // X*4+1 which got turned into X*4|1.  Handle this as an Add so loop
2989     // optimizations will transparently handle this case.
2990     //
2991     // In order for this transformation to be safe, the LHS must be of the
2992     // form X*(2^n) and the Or constant must be less than 2^n.
2993     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
2994       const SCEV *LHS = getSCEV(U->getOperand(0));
2995       const APInt &CIVal = CI->getValue();
2996       if (GetMinTrailingZeros(LHS) >=
2997           (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
2998         // Build a plain add SCEV.
2999         const SCEV *S = getAddExpr(LHS, getSCEV(CI));
3000         // If the LHS of the add was an addrec and it has no-wrap flags,
3001         // transfer the no-wrap flags, since an or won't introduce a wrap.
3002         if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
3003           const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
3004           if (OldAR->hasNoUnsignedWrap())
3005             const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoUnsignedWrap(true);
3006           if (OldAR->hasNoSignedWrap())
3007             const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoSignedWrap(true);
3008         }
3009         return S;
3010       }
3011     }
3012     break;
3013   case Instruction::Xor:
3014     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
3015       // If the RHS of the xor is a signbit, then this is just an add.
3016       // Instcombine turns add of signbit into xor as a strength reduction step.
3017       if (CI->getValue().isSignBit())
3018         return getAddExpr(getSCEV(U->getOperand(0)),
3019                           getSCEV(U->getOperand(1)));
3020
3021       // If the RHS of xor is -1, then this is a not operation.
3022       if (CI->isAllOnesValue())
3023         return getNotSCEV(getSCEV(U->getOperand(0)));
3024
3025       // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
3026       // This is a variant of the check for xor with -1, and it handles
3027       // the case where instcombine has trimmed non-demanded bits out
3028       // of an xor with -1.
3029       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0)))
3030         if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
3031           if (BO->getOpcode() == Instruction::And &&
3032               LCI->getValue() == CI->getValue())
3033             if (const SCEVZeroExtendExpr *Z =
3034                   dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
3035               const Type *UTy = U->getType();
3036               const SCEV *Z0 = Z->getOperand();
3037               const Type *Z0Ty = Z0->getType();
3038               unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
3039
3040               // If C is a low-bits mask, the zero extend is zerving to
3041               // mask off the high bits. Complement the operand and
3042               // re-apply the zext.
3043               if (APIntOps::isMask(Z0TySize, CI->getValue()))
3044                 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
3045
3046               // If C is a single bit, it may be in the sign-bit position
3047               // before the zero-extend. In this case, represent the xor
3048               // using an add, which is equivalent, and re-apply the zext.
3049               APInt Trunc = APInt(CI->getValue()).trunc(Z0TySize);
3050               if (APInt(Trunc).zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
3051                   Trunc.isSignBit())
3052                 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
3053                                          UTy);
3054             }
3055     }
3056     break;
3057
3058   case Instruction::Shl:
3059     // Turn shift left of a constant amount into a multiply.
3060     if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
3061       uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
3062       Constant *X = ConstantInt::get(getContext(),
3063         APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
3064       return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
3065     }
3066     break;
3067
3068   case Instruction::LShr:
3069     // Turn logical shift right of a constant into a unsigned divide.
3070     if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
3071       uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
3072       Constant *X = ConstantInt::get(getContext(),
3073         APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
3074       return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
3075     }
3076     break;
3077
3078   case Instruction::AShr:
3079     // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
3080     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
3081       if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0)))
3082         if (L->getOpcode() == Instruction::Shl &&
3083             L->getOperand(1) == U->getOperand(1)) {
3084           unsigned BitWidth = getTypeSizeInBits(U->getType());
3085           uint64_t Amt = BitWidth - CI->getZExtValue();
3086           if (Amt == BitWidth)
3087             return getSCEV(L->getOperand(0));       // shift by zero --> noop
3088           if (Amt > BitWidth)
3089             return getIntegerSCEV(0, U->getType()); // value is undefined
3090           return
3091             getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
3092                                            IntegerType::get(getContext(), Amt)),
3093                                  U->getType());
3094         }
3095     break;
3096
3097   case Instruction::Trunc:
3098     return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
3099
3100   case Instruction::ZExt:
3101     return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
3102
3103   case Instruction::SExt:
3104     return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
3105
3106   case Instruction::BitCast:
3107     // BitCasts are no-op casts so we just eliminate the cast.
3108     if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
3109       return getSCEV(U->getOperand(0));
3110     break;
3111
3112     // It's tempting to handle inttoptr and ptrtoint, however this can
3113     // lead to pointer expressions which cannot be expanded to GEPs
3114     // (because they may overflow). For now, the only pointer-typed
3115     // expressions we handle are GEPs and address literals.
3116
3117   case Instruction::GetElementPtr:
3118     return createNodeForGEP(U);
3119
3120   case Instruction::PHI:
3121     return createNodeForPHI(cast<PHINode>(U));
3122
3123   case Instruction::Select:
3124     // This could be a smax or umax that was lowered earlier.
3125     // Try to recover it.
3126     if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) {
3127       Value *LHS = ICI->getOperand(0);
3128       Value *RHS = ICI->getOperand(1);
3129       switch (ICI->getPredicate()) {
3130       case ICmpInst::ICMP_SLT:
3131       case ICmpInst::ICMP_SLE:
3132         std::swap(LHS, RHS);
3133         // fall through
3134       case ICmpInst::ICMP_SGT:
3135       case ICmpInst::ICMP_SGE:
3136         if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
3137           return getSMaxExpr(getSCEV(LHS), getSCEV(RHS));
3138         else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
3139           return getSMinExpr(getSCEV(LHS), getSCEV(RHS));
3140         break;
3141       case ICmpInst::ICMP_ULT:
3142       case ICmpInst::ICMP_ULE:
3143         std::swap(LHS, RHS);
3144         // fall through
3145       case ICmpInst::ICMP_UGT:
3146       case ICmpInst::ICMP_UGE:
3147         if (LHS == U->getOperand(1) && RHS == U->getOperand(2))
3148           return getUMaxExpr(getSCEV(LHS), getSCEV(RHS));
3149         else if (LHS == U->getOperand(2) && RHS == U->getOperand(1))
3150           return getUMinExpr(getSCEV(LHS), getSCEV(RHS));
3151         break;
3152       case ICmpInst::ICMP_NE:
3153         // n != 0 ? n : 1  ->  umax(n, 1)
3154         if (LHS == U->getOperand(1) &&
3155             isa<ConstantInt>(U->getOperand(2)) &&
3156             cast<ConstantInt>(U->getOperand(2))->isOne() &&
3157             isa<ConstantInt>(RHS) &&
3158             cast<ConstantInt>(RHS)->isZero())
3159           return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(2)));
3160         break;
3161       case ICmpInst::ICMP_EQ:
3162         // n == 0 ? 1 : n  ->  umax(n, 1)
3163         if (LHS == U->getOperand(2) &&
3164             isa<ConstantInt>(U->getOperand(1)) &&
3165             cast<ConstantInt>(U->getOperand(1))->isOne() &&
3166             isa<ConstantInt>(RHS) &&
3167             cast<ConstantInt>(RHS)->isZero())
3168           return getUMaxExpr(getSCEV(LHS), getSCEV(U->getOperand(1)));
3169         break;
3170       default:
3171         break;
3172       }
3173     }
3174
3175   default: // We cannot analyze this expression.
3176     break;
3177   }
3178
3179   return getUnknown(V);
3180 }
3181
3182
3183
3184 //===----------------------------------------------------------------------===//
3185 //                   Iteration Count Computation Code
3186 //
3187
3188 /// getBackedgeTakenCount - If the specified loop has a predictable
3189 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
3190 /// object. The backedge-taken count is the number of times the loop header
3191 /// will be branched to from within the loop. This is one less than the
3192 /// trip count of the loop, since it doesn't count the first iteration,
3193 /// when the header is branched to from outside the loop.
3194 ///
3195 /// Note that it is not valid to call this method on a loop without a
3196 /// loop-invariant backedge-taken count (see
3197 /// hasLoopInvariantBackedgeTakenCount).
3198 ///
3199 const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
3200   return getBackedgeTakenInfo(L).Exact;
3201 }
3202
3203 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
3204 /// return the least SCEV value that is known never to be less than the
3205 /// actual backedge taken count.
3206 const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
3207   return getBackedgeTakenInfo(L).Max;
3208 }
3209
3210 /// PushLoopPHIs - Push PHI nodes in the header of the given loop
3211 /// onto the given Worklist.
3212 static void
3213 PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
3214   BasicBlock *Header = L->getHeader();
3215
3216   // Push all Loop-header PHIs onto the Worklist stack.
3217   for (BasicBlock::iterator I = Header->begin();
3218        PHINode *PN = dyn_cast<PHINode>(I); ++I)
3219     Worklist.push_back(PN);
3220 }
3221
3222 const ScalarEvolution::BackedgeTakenInfo &
3223 ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
3224   // Initially insert a CouldNotCompute for this loop. If the insertion
3225   // succeeds, procede to actually compute a backedge-taken count and
3226   // update the value. The temporary CouldNotCompute value tells SCEV
3227   // code elsewhere that it shouldn't attempt to request a new
3228   // backedge-taken count, which could result in infinite recursion.
3229   std::pair<std::map<const Loop*, BackedgeTakenInfo>::iterator, bool> Pair =
3230     BackedgeTakenCounts.insert(std::make_pair(L, getCouldNotCompute()));
3231   if (Pair.second) {
3232     BackedgeTakenInfo ItCount = ComputeBackedgeTakenCount(L);
3233     if (ItCount.Exact != getCouldNotCompute()) {
3234       assert(ItCount.Exact->isLoopInvariant(L) &&
3235              ItCount.Max->isLoopInvariant(L) &&
3236              "Computed trip count isn't loop invariant for loop!");
3237       ++NumTripCountsComputed;
3238
3239       // Update the value in the map.
3240       Pair.first->second = ItCount;
3241     } else {
3242       if (ItCount.Max != getCouldNotCompute())
3243         // Update the value in the map.
3244         Pair.first->second = ItCount;
3245       if (isa<PHINode>(L->getHeader()->begin()))
3246         // Only count loops that have phi nodes as not being computable.
3247         ++NumTripCountsNotComputed;
3248     }
3249
3250     // Now that we know more about the trip count for this loop, forget any
3251     // existing SCEV values for PHI nodes in this loop since they are only
3252     // conservative estimates made without the benefit of trip count
3253     // information. This is similar to the code in
3254     // forgetLoopBackedgeTakenCount, except that it handles SCEVUnknown PHI
3255     // nodes specially.
3256     if (ItCount.hasAnyInfo()) {
3257       SmallVector<Instruction *, 16> Worklist;
3258       PushLoopPHIs(L, Worklist);
3259
3260       SmallPtrSet<Instruction *, 8> Visited;
3261       while (!Worklist.empty()) {
3262         Instruction *I = Worklist.pop_back_val();
3263         if (!Visited.insert(I)) continue;
3264
3265         std::map<SCEVCallbackVH, const SCEV*>::iterator It =
3266           Scalars.find(static_cast<Value *>(I));
3267         if (It != Scalars.end()) {
3268           // SCEVUnknown for a PHI either means that it has an unrecognized
3269           // structure, or it's a PHI that's in the progress of being computed
3270           // by createNodeForPHI.  In the former case, additional loop trip
3271           // count information isn't going to change anything. In the later
3272           // case, createNodeForPHI will perform the necessary updates on its
3273           // own when it gets to that point.
3274           if (!isa<PHINode>(I) || !isa<SCEVUnknown>(It->second)) {
3275             ValuesAtScopes.erase(It->second);
3276             Scalars.erase(It);
3277           }
3278           if (PHINode *PN = dyn_cast<PHINode>(I))
3279             ConstantEvolutionLoopExitValue.erase(PN);
3280         }
3281
3282         PushDefUseChildren(I, Worklist);
3283       }
3284     }
3285   }
3286   return Pair.first->second;
3287 }
3288
3289 /// forgetLoopBackedgeTakenCount - This method should be called by the
3290 /// client when it has changed a loop in a way that may effect
3291 /// ScalarEvolution's ability to compute a trip count, or if the loop
3292 /// is deleted.
3293 void ScalarEvolution::forgetLoopBackedgeTakenCount(const Loop *L) {
3294   BackedgeTakenCounts.erase(L);
3295
3296   SmallVector<Instruction *, 16> Worklist;
3297   PushLoopPHIs(L, Worklist);
3298
3299   SmallPtrSet<Instruction *, 8> Visited;
3300   while (!Worklist.empty()) {
3301     Instruction *I = Worklist.pop_back_val();
3302     if (!Visited.insert(I)) continue;
3303
3304     std::map<SCEVCallbackVH, const SCEV*>::iterator It =
3305       Scalars.find(static_cast<Value *>(I));
3306     if (It != Scalars.end()) {
3307       ValuesAtScopes.erase(It->second);
3308       Scalars.erase(It);
3309       if (PHINode *PN = dyn_cast<PHINode>(I))
3310         ConstantEvolutionLoopExitValue.erase(PN);
3311     }
3312
3313     PushDefUseChildren(I, Worklist);
3314   }
3315 }
3316
3317 /// ComputeBackedgeTakenCount - Compute the number of times the backedge
3318 /// of the specified loop will execute.
3319 ScalarEvolution::BackedgeTakenInfo
3320 ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
3321   SmallVector<BasicBlock*, 8> ExitingBlocks;
3322   L->getExitingBlocks(ExitingBlocks);
3323
3324   // Examine all exits and pick the most conservative values.
3325   const SCEV *BECount = getCouldNotCompute();
3326   const SCEV *MaxBECount = getCouldNotCompute();
3327   bool CouldNotComputeBECount = false;
3328   for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
3329     BackedgeTakenInfo NewBTI =
3330       ComputeBackedgeTakenCountFromExit(L, ExitingBlocks[i]);
3331
3332     if (NewBTI.Exact == getCouldNotCompute()) {
3333       // We couldn't compute an exact value for this exit, so
3334       // we won't be able to compute an exact value for the loop.
3335       CouldNotComputeBECount = true;
3336       BECount = getCouldNotCompute();
3337     } else if (!CouldNotComputeBECount) {
3338       if (BECount == getCouldNotCompute())
3339         BECount = NewBTI.Exact;
3340       else
3341         BECount = getUMinFromMismatchedTypes(BECount, NewBTI.Exact);
3342     }
3343     if (MaxBECount == getCouldNotCompute())
3344       MaxBECount = NewBTI.Max;
3345     else if (NewBTI.Max != getCouldNotCompute())
3346       MaxBECount = getUMinFromMismatchedTypes(MaxBECount, NewBTI.Max);
3347   }
3348
3349   return BackedgeTakenInfo(BECount, MaxBECount);
3350 }
3351
3352 /// ComputeBackedgeTakenCountFromExit - Compute the number of times the backedge
3353 /// of the specified loop will execute if it exits via the specified block.
3354 ScalarEvolution::BackedgeTakenInfo
3355 ScalarEvolution::ComputeBackedgeTakenCountFromExit(const Loop *L,
3356                                                    BasicBlock *ExitingBlock) {
3357
3358   // Okay, we've chosen an exiting block.  See what condition causes us to
3359   // exit at this block.
3360   //
3361   // FIXME: we should be able to handle switch instructions (with a single exit)
3362   BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
3363   if (ExitBr == 0) return getCouldNotCompute();
3364   assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
3365
3366   // At this point, we know we have a conditional branch that determines whether
3367   // the loop is exited.  However, we don't know if the branch is executed each
3368   // time through the loop.  If not, then the execution count of the branch will
3369   // not be equal to the trip count of the loop.
3370   //
3371   // Currently we check for this by checking to see if the Exit branch goes to
3372   // the loop header.  If so, we know it will always execute the same number of
3373   // times as the loop.  We also handle the case where the exit block *is* the
3374   // loop header.  This is common for un-rotated loops.
3375   //
3376   // If both of those tests fail, walk up the unique predecessor chain to the
3377   // header, stopping if there is an edge that doesn't exit the loop. If the
3378   // header is reached, the execution count of the branch will be equal to the
3379   // trip count of the loop.
3380   //
3381   //  More extensive analysis could be done to handle more cases here.
3382   //
3383   if (ExitBr->getSuccessor(0) != L->getHeader() &&
3384       ExitBr->getSuccessor(1) != L->getHeader() &&
3385       ExitBr->getParent() != L->getHeader()) {
3386     // The simple checks failed, try climbing the unique predecessor chain
3387     // up to the header.
3388     bool Ok = false;
3389     for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
3390       BasicBlock *Pred = BB->getUniquePredecessor();
3391       if (!Pred)
3392         return getCouldNotCompute();
3393       TerminatorInst *PredTerm = Pred->getTerminator();
3394       for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
3395         BasicBlock *PredSucc = PredTerm->getSuccessor(i);
3396         if (PredSucc == BB)
3397           continue;
3398         // If the predecessor has a successor that isn't BB and isn't
3399         // outside the loop, assume the worst.
3400         if (L->contains(PredSucc))
3401           return getCouldNotCompute();
3402       }
3403       if (Pred == L->getHeader()) {
3404         Ok = true;
3405         break;
3406       }
3407       BB = Pred;
3408     }
3409     if (!Ok)
3410       return getCouldNotCompute();
3411   }
3412
3413   // Procede to the next level to examine the exit condition expression.
3414   return ComputeBackedgeTakenCountFromExitCond(L, ExitBr->getCondition(),
3415                                                ExitBr->getSuccessor(0),
3416                                                ExitBr->getSuccessor(1));
3417 }
3418
3419 /// ComputeBackedgeTakenCountFromExitCond - Compute the number of times the
3420 /// backedge of the specified loop will execute if its exit condition
3421 /// were a conditional branch of ExitCond, TBB, and FBB.
3422 ScalarEvolution::BackedgeTakenInfo
3423 ScalarEvolution::ComputeBackedgeTakenCountFromExitCond(const Loop *L,
3424                                                        Value *ExitCond,
3425                                                        BasicBlock *TBB,
3426                                                        BasicBlock *FBB) {
3427   // Check if the controlling expression for this loop is an And or Or.
3428   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
3429     if (BO->getOpcode() == Instruction::And) {
3430       // Recurse on the operands of the and.
3431       BackedgeTakenInfo BTI0 =
3432         ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3433       BackedgeTakenInfo BTI1 =
3434         ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
3435       const SCEV *BECount = getCouldNotCompute();
3436       const SCEV *MaxBECount = getCouldNotCompute();
3437       if (L->contains(TBB)) {
3438         // Both conditions must be true for the loop to continue executing.
3439         // Choose the less conservative count.
3440         if (BTI0.Exact == getCouldNotCompute() ||
3441             BTI1.Exact == getCouldNotCompute())
3442           BECount = getCouldNotCompute();
3443         else
3444           BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
3445         if (BTI0.Max == getCouldNotCompute())
3446           MaxBECount = BTI1.Max;
3447         else if (BTI1.Max == getCouldNotCompute())
3448           MaxBECount = BTI0.Max;
3449         else
3450           MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
3451       } else {
3452         // Both conditions must be true for the loop to exit.
3453         assert(L->contains(FBB) && "Loop block has no successor in loop!");
3454         if (BTI0.Exact != getCouldNotCompute() &&
3455             BTI1.Exact != getCouldNotCompute())
3456           BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
3457         if (BTI0.Max != getCouldNotCompute() &&
3458             BTI1.Max != getCouldNotCompute())
3459           MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3460       }
3461
3462       return BackedgeTakenInfo(BECount, MaxBECount);
3463     }
3464     if (BO->getOpcode() == Instruction::Or) {
3465       // Recurse on the operands of the or.
3466       BackedgeTakenInfo BTI0 =
3467         ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(0), TBB, FBB);
3468       BackedgeTakenInfo BTI1 =
3469         ComputeBackedgeTakenCountFromExitCond(L, BO->getOperand(1), TBB, FBB);
3470       const SCEV *BECount = getCouldNotCompute();
3471       const SCEV *MaxBECount = getCouldNotCompute();
3472       if (L->contains(FBB)) {
3473         // Both conditions must be false for the loop to continue executing.
3474         // Choose the less conservative count.
3475         if (BTI0.Exact == getCouldNotCompute() ||
3476             BTI1.Exact == getCouldNotCompute())
3477           BECount = getCouldNotCompute();
3478         else
3479           BECount = getUMinFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
3480         if (BTI0.Max == getCouldNotCompute())
3481           MaxBECount = BTI1.Max;
3482         else if (BTI1.Max == getCouldNotCompute())
3483           MaxBECount = BTI0.Max;
3484         else
3485           MaxBECount = getUMinFromMismatchedTypes(BTI0.Max, BTI1.Max);
3486       } else {
3487         // Both conditions must be false for the loop to exit.
3488         assert(L->contains(TBB) && "Loop block has no successor in loop!");
3489         if (BTI0.Exact != getCouldNotCompute() &&
3490             BTI1.Exact != getCouldNotCompute())
3491           BECount = getUMaxFromMismatchedTypes(BTI0.Exact, BTI1.Exact);
3492         if (BTI0.Max != getCouldNotCompute() &&
3493             BTI1.Max != getCouldNotCompute())
3494           MaxBECount = getUMaxFromMismatchedTypes(BTI0.Max, BTI1.Max);
3495       }
3496
3497       return BackedgeTakenInfo(BECount, MaxBECount);
3498     }
3499   }
3500
3501   // With an icmp, it may be feasible to compute an exact backedge-taken count.
3502   // Procede to the next level to examine the icmp.
3503   if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
3504     return ComputeBackedgeTakenCountFromExitCondICmp(L, ExitCondICmp, TBB, FBB);
3505
3506   // If it's not an integer or pointer comparison then compute it the hard way.
3507   return ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3508 }
3509
3510 /// ComputeBackedgeTakenCountFromExitCondICmp - Compute the number of times the
3511 /// backedge of the specified loop will execute if its exit condition
3512 /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
3513 ScalarEvolution::BackedgeTakenInfo
3514 ScalarEvolution::ComputeBackedgeTakenCountFromExitCondICmp(const Loop *L,
3515                                                            ICmpInst *ExitCond,
3516                                                            BasicBlock *TBB,
3517                                                            BasicBlock *FBB) {
3518
3519   // If the condition was exit on true, convert the condition to exit on false
3520   ICmpInst::Predicate Cond;
3521   if (!L->contains(FBB))
3522     Cond = ExitCond->getPredicate();
3523   else
3524     Cond = ExitCond->getInversePredicate();
3525
3526   // Handle common loops like: for (X = "string"; *X; ++X)
3527   if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
3528     if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
3529       const SCEV *ItCnt =
3530         ComputeLoadConstantCompareBackedgeTakenCount(LI, RHS, L, Cond);
3531       if (!isa<SCEVCouldNotCompute>(ItCnt)) {
3532         unsigned BitWidth = getTypeSizeInBits(ItCnt->getType());
3533         return BackedgeTakenInfo(ItCnt,
3534                                  isa<SCEVConstant>(ItCnt) ? ItCnt :
3535                                    getConstant(APInt::getMaxValue(BitWidth)-1));
3536       }
3537     }
3538
3539   const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
3540   const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
3541
3542   // Try to evaluate any dependencies out of the loop.
3543   LHS = getSCEVAtScope(LHS, L);
3544   RHS = getSCEVAtScope(RHS, L);
3545
3546   // At this point, we would like to compute how many iterations of the
3547   // loop the predicate will return true for these inputs.
3548   if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) {
3549     // If there is a loop-invariant, force it into the RHS.
3550     std::swap(LHS, RHS);
3551     Cond = ICmpInst::getSwappedPredicate(Cond);
3552   }
3553
3554   // If we have a comparison of a chrec against a constant, try to use value
3555   // ranges to answer this query.
3556   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
3557     if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
3558       if (AddRec->getLoop() == L) {
3559         // Form the constant range.
3560         ConstantRange CompRange(
3561             ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue()));
3562
3563         const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
3564         if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
3565       }
3566
3567   switch (Cond) {
3568   case ICmpInst::ICMP_NE: {                     // while (X != Y)
3569     // Convert to: while (X-Y != 0)
3570     const SCEV *TC = HowFarToZero(getMinusSCEV(LHS, RHS), L);
3571     if (!isa<SCEVCouldNotCompute>(TC)) return TC;
3572     break;
3573   }
3574   case ICmpInst::ICMP_EQ: {                     // while (X == Y)
3575     // Convert to: while (X-Y == 0)
3576     const SCEV *TC = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
3577     if (!isa<SCEVCouldNotCompute>(TC)) return TC;
3578     break;
3579   }
3580   case ICmpInst::ICMP_SLT: {
3581     BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, true);
3582     if (BTI.hasAnyInfo()) return BTI;
3583     break;
3584   }
3585   case ICmpInst::ICMP_SGT: {
3586     BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3587                                              getNotSCEV(RHS), L, true);
3588     if (BTI.hasAnyInfo()) return BTI;
3589     break;
3590   }
3591   case ICmpInst::ICMP_ULT: {
3592     BackedgeTakenInfo BTI = HowManyLessThans(LHS, RHS, L, false);
3593     if (BTI.hasAnyInfo()) return BTI;
3594     break;
3595   }
3596   case ICmpInst::ICMP_UGT: {
3597     BackedgeTakenInfo BTI = HowManyLessThans(getNotSCEV(LHS),
3598                                              getNotSCEV(RHS), L, false);
3599     if (BTI.hasAnyInfo()) return BTI;
3600     break;
3601   }
3602   default:
3603 #if 0
3604     errs() << "ComputeBackedgeTakenCount ";
3605     if (ExitCond->getOperand(0)->getType()->isUnsigned())
3606       errs() << "[unsigned] ";
3607     errs() << *LHS << "   "
3608          << Instruction::getOpcodeName(Instruction::ICmp)
3609          << "   " << *RHS << "\n";
3610 #endif
3611     break;
3612   }
3613   return
3614     ComputeBackedgeTakenCountExhaustively(L, ExitCond, !L->contains(TBB));
3615 }
3616
3617 static ConstantInt *
3618 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
3619                                 ScalarEvolution &SE) {
3620   const SCEV *InVal = SE.getConstant(C);
3621   const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
3622   assert(isa<SCEVConstant>(Val) &&
3623          "Evaluation of SCEV at constant didn't fold correctly?");
3624   return cast<SCEVConstant>(Val)->getValue();
3625 }
3626
3627 /// GetAddressedElementFromGlobal - Given a global variable with an initializer
3628 /// and a GEP expression (missing the pointer index) indexing into it, return
3629 /// the addressed element of the initializer or null if the index expression is
3630 /// invalid.
3631 static Constant *
3632 GetAddressedElementFromGlobal(LLVMContext &Context, GlobalVariable *GV,
3633                               const std::vector<ConstantInt*> &Indices) {
3634   Constant *Init = GV->getInitializer();
3635   for (unsigned i = 0, e = Indices.size(); i != e; ++i) {
3636     uint64_t Idx = Indices[i]->getZExtValue();
3637     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Init)) {
3638       assert(Idx < CS->getNumOperands() && "Bad struct index!");
3639       Init = cast<Constant>(CS->getOperand(Idx));
3640     } else if (ConstantArray *CA = dyn_cast<ConstantArray>(Init)) {
3641       if (Idx >= CA->getNumOperands()) return 0;  // Bogus program
3642       Init = cast<Constant>(CA->getOperand(Idx));
3643     } else if (isa<ConstantAggregateZero>(Init)) {
3644       if (const StructType *STy = dyn_cast<StructType>(Init->getType())) {
3645         assert(Idx < STy->getNumElements() && "Bad struct index!");
3646         Init = Constant::getNullValue(STy->getElementType(Idx));
3647       } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Init->getType())) {
3648         if (Idx >= ATy->getNumElements()) return 0;  // Bogus program
3649         Init = Constant::getNullValue(ATy->getElementType());
3650       } else {
3651         llvm_unreachable("Unknown constant aggregate type!");
3652       }
3653       return 0;
3654     } else {
3655       return 0; // Unknown initializer type
3656     }
3657   }
3658   return Init;
3659 }
3660
3661 /// ComputeLoadConstantCompareBackedgeTakenCount - Given an exit condition of
3662 /// 'icmp op load X, cst', try to see if we can compute the backedge
3663 /// execution count.
3664 const SCEV *
3665 ScalarEvolution::ComputeLoadConstantCompareBackedgeTakenCount(
3666                                                 LoadInst *LI,
3667                                                 Constant *RHS,
3668                                                 const Loop *L,
3669                                                 ICmpInst::Predicate predicate) {
3670   if (LI->isVolatile()) return getCouldNotCompute();
3671
3672   // Check to see if the loaded pointer is a getelementptr of a global.
3673   GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
3674   if (!GEP) return getCouldNotCompute();
3675
3676   // Make sure that it is really a constant global we are gepping, with an
3677   // initializer, and make sure the first IDX is really 0.
3678   GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
3679   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
3680       GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
3681       !cast<Constant>(GEP->getOperand(1))->isNullValue())
3682     return getCouldNotCompute();
3683
3684   // Okay, we allow one non-constant index into the GEP instruction.
3685   Value *VarIdx = 0;
3686   std::vector<ConstantInt*> Indexes;
3687   unsigned VarIdxNum = 0;
3688   for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
3689     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
3690       Indexes.push_back(CI);
3691     } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
3692       if (VarIdx) return getCouldNotCompute();  // Multiple non-constant idx's.
3693       VarIdx = GEP->getOperand(i);
3694       VarIdxNum = i-2;
3695       Indexes.push_back(0);
3696     }
3697
3698   // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
3699   // Check to see if X is a loop variant variable value now.
3700   const SCEV *Idx = getSCEV(VarIdx);
3701   Idx = getSCEVAtScope(Idx, L);
3702
3703   // We can only recognize very limited forms of loop index expressions, in
3704   // particular, only affine AddRec's like {C1,+,C2}.
3705   const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
3706   if (!IdxExpr || !IdxExpr->isAffine() || IdxExpr->isLoopInvariant(L) ||
3707       !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
3708       !isa<SCEVConstant>(IdxExpr->getOperand(1)))
3709     return getCouldNotCompute();
3710
3711   unsigned MaxSteps = MaxBruteForceIterations;
3712   for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
3713     ConstantInt *ItCst = ConstantInt::get(
3714                            cast<IntegerType>(IdxExpr->getType()), IterationNum);
3715     ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
3716
3717     // Form the GEP offset.
3718     Indexes[VarIdxNum] = Val;
3719
3720     Constant *Result = GetAddressedElementFromGlobal(getContext(), GV, Indexes);
3721     if (Result == 0) break;  // Cannot compute!
3722
3723     // Evaluate the condition for this iteration.
3724     Result = ConstantExpr::getICmp(predicate, Result, RHS);
3725     if (!isa<ConstantInt>(Result)) break;  // Couldn't decide for sure
3726     if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
3727 #if 0
3728       errs() << "\n***\n*** Computed loop count " << *ItCst
3729              << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
3730              << "***\n";
3731 #endif
3732       ++NumArrayLenItCounts;
3733       return getConstant(ItCst);   // Found terminating iteration!
3734     }
3735   }
3736   return getCouldNotCompute();
3737 }
3738
3739
3740 /// CanConstantFold - Return true if we can constant fold an instruction of the
3741 /// specified type, assuming that all operands were constants.
3742 static bool CanConstantFold(const Instruction *I) {
3743   if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
3744       isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I))
3745     return true;
3746
3747   if (const CallInst *CI = dyn_cast<CallInst>(I))
3748     if (const Function *F = CI->getCalledFunction())
3749       return canConstantFoldCallTo(F);
3750   return false;
3751 }
3752
3753 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
3754 /// in the loop that V is derived from.  We allow arbitrary operations along the
3755 /// way, but the operands of an operation must either be constants or a value
3756 /// derived from a constant PHI.  If this expression does not fit with these
3757 /// constraints, return null.
3758 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
3759   // If this is not an instruction, or if this is an instruction outside of the
3760   // loop, it can't be derived from a loop PHI.
3761   Instruction *I = dyn_cast<Instruction>(V);
3762   if (I == 0 || !L->contains(I->getParent())) return 0;
3763
3764   if (PHINode *PN = dyn_cast<PHINode>(I)) {
3765     if (L->getHeader() == I->getParent())
3766       return PN;
3767     else
3768       // We don't currently keep track of the control flow needed to evaluate
3769       // PHIs, so we cannot handle PHIs inside of loops.
3770       return 0;
3771   }
3772
3773   // If we won't be able to constant fold this expression even if the operands
3774   // are constants, return early.
3775   if (!CanConstantFold(I)) return 0;
3776
3777   // Otherwise, we can evaluate this instruction if all of its operands are
3778   // constant or derived from a PHI node themselves.
3779   PHINode *PHI = 0;
3780   for (unsigned Op = 0, e = I->getNumOperands(); Op != e; ++Op)
3781     if (!(isa<Constant>(I->getOperand(Op)) ||
3782           isa<GlobalValue>(I->getOperand(Op)))) {
3783       PHINode *P = getConstantEvolvingPHI(I->getOperand(Op), L);
3784       if (P == 0) return 0;  // Not evolving from PHI
3785       if (PHI == 0)
3786         PHI = P;
3787       else if (PHI != P)
3788         return 0;  // Evolving from multiple different PHIs.
3789     }
3790
3791   // This is a expression evolving from a constant PHI!
3792   return PHI;
3793 }
3794
3795 /// EvaluateExpression - Given an expression that passes the
3796 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
3797 /// in the loop has the value PHIVal.  If we can't fold this expression for some
3798 /// reason, return null.
3799 static Constant *EvaluateExpression(Value *V, Constant *PHIVal) {
3800   if (isa<PHINode>(V)) return PHIVal;
3801   if (Constant *C = dyn_cast<Constant>(V)) return C;
3802   if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) return GV;
3803   Instruction *I = cast<Instruction>(V);
3804   LLVMContext &Context = I->getParent()->getContext();
3805
3806   std::vector<Constant*> Operands;
3807   Operands.resize(I->getNumOperands());
3808
3809   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3810     Operands[i] = EvaluateExpression(I->getOperand(i), PHIVal);
3811     if (Operands[i] == 0) return 0;
3812   }
3813
3814   if (const CmpInst *CI = dyn_cast<CmpInst>(I))
3815     return ConstantFoldCompareInstOperands(CI->getPredicate(),
3816                                            &Operands[0], Operands.size(),
3817                                            Context);
3818   else
3819     return ConstantFoldInstOperands(I->getOpcode(), I->getType(),
3820                                     &Operands[0], Operands.size(),
3821                                     Context);
3822 }
3823
3824 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
3825 /// in the header of its containing loop, we know the loop executes a
3826 /// constant number of times, and the PHI node is just a recurrence
3827 /// involving constants, fold it.
3828 Constant *
3829 ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
3830                                                    const APInt& BEs,
3831                                                    const Loop *L) {
3832   std::map<PHINode*, Constant*>::iterator I =
3833     ConstantEvolutionLoopExitValue.find(PN);
3834   if (I != ConstantEvolutionLoopExitValue.end())
3835     return I->second;
3836
3837   if (BEs.ugt(APInt(BEs.getBitWidth(),MaxBruteForceIterations)))
3838     return ConstantEvolutionLoopExitValue[PN] = 0;  // Not going to evaluate it.
3839
3840   Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
3841
3842   // Since the loop is canonicalized, the PHI node must have two entries.  One
3843   // entry must be a constant (coming in from outside of the loop), and the
3844   // second must be derived from the same PHI.
3845   bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3846   Constant *StartCST =
3847     dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
3848   if (StartCST == 0)
3849     return RetVal = 0;  // Must be a constant.
3850
3851   Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3852   PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
3853   if (PN2 != PN)
3854     return RetVal = 0;  // Not derived from same PHI.
3855
3856   // Execute the loop symbolically to determine the exit value.
3857   if (BEs.getActiveBits() >= 32)
3858     return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
3859
3860   unsigned NumIterations = BEs.getZExtValue(); // must be in range
3861   unsigned IterationNum = 0;
3862   for (Constant *PHIVal = StartCST; ; ++IterationNum) {
3863     if (IterationNum == NumIterations)
3864       return RetVal = PHIVal;  // Got exit value!
3865
3866     // Compute the value of the PHI node for the next iteration.
3867     Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3868     if (NextPHI == PHIVal)
3869       return RetVal = NextPHI;  // Stopped evolving!
3870     if (NextPHI == 0)
3871       return 0;        // Couldn't evaluate!
3872     PHIVal = NextPHI;
3873   }
3874 }
3875
3876 /// ComputeBackedgeTakenCountExhaustively - If the loop is known to execute a
3877 /// constant number of times (the condition evolves only from constants),
3878 /// try to evaluate a few iterations of the loop until we get the exit
3879 /// condition gets a value of ExitWhen (true or false).  If we cannot
3880 /// evaluate the trip count of the loop, return getCouldNotCompute().
3881 const SCEV *
3882 ScalarEvolution::ComputeBackedgeTakenCountExhaustively(const Loop *L,
3883                                                        Value *Cond,
3884                                                        bool ExitWhen) {
3885   PHINode *PN = getConstantEvolvingPHI(Cond, L);
3886   if (PN == 0) return getCouldNotCompute();
3887
3888   // Since the loop is canonicalized, the PHI node must have two entries.  One
3889   // entry must be a constant (coming in from outside of the loop), and the
3890   // second must be derived from the same PHI.
3891   bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
3892   Constant *StartCST =
3893     dyn_cast<Constant>(PN->getIncomingValue(!SecondIsBackedge));
3894   if (StartCST == 0) return getCouldNotCompute();  // Must be a constant.
3895
3896   Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
3897   PHINode *PN2 = getConstantEvolvingPHI(BEValue, L);
3898   if (PN2 != PN) return getCouldNotCompute();  // Not derived from same PHI.
3899
3900   // Okay, we find a PHI node that defines the trip count of this loop.  Execute
3901   // the loop symbolically to determine when the condition gets a value of
3902   // "ExitWhen".
3903   unsigned IterationNum = 0;
3904   unsigned MaxIterations = MaxBruteForceIterations;   // Limit analysis.
3905   for (Constant *PHIVal = StartCST;
3906        IterationNum != MaxIterations; ++IterationNum) {
3907     ConstantInt *CondVal =
3908       dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, PHIVal));
3909
3910     // Couldn't symbolically evaluate.
3911     if (!CondVal) return getCouldNotCompute();
3912
3913     if (CondVal->getValue() == uint64_t(ExitWhen)) {
3914       ++NumBruteForceTripCountsComputed;
3915       return getConstant(Type::getInt32Ty(getContext()), IterationNum);
3916     }
3917
3918     // Compute the value of the PHI node for the next iteration.
3919     Constant *NextPHI = EvaluateExpression(BEValue, PHIVal);
3920     if (NextPHI == 0 || NextPHI == PHIVal)
3921       return getCouldNotCompute();// Couldn't evaluate or not making progress...
3922     PHIVal = NextPHI;
3923   }
3924
3925   // Too many iterations were needed to evaluate.
3926   return getCouldNotCompute();
3927 }
3928
3929 /// getSCEVAtScope - Return a SCEV expression for the specified value
3930 /// at the specified scope in the program.  The L value specifies a loop
3931 /// nest to evaluate the expression at, where null is the top-level or a
3932 /// specified loop is immediately inside of the loop.
3933 ///
3934 /// This method can be used to compute the exit value for a variable defined
3935 /// in a loop by querying what the value will hold in the parent loop.
3936 ///
3937 /// In the case that a relevant loop exit value cannot be computed, the
3938 /// original value V is returned.
3939 const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
3940   // Check to see if we've folded this expression at this loop before.
3941   std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V];
3942   std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair =
3943     Values.insert(std::make_pair(L, static_cast<const SCEV *>(0)));
3944   if (!Pair.second)
3945     return Pair.first->second ? Pair.first->second : V;
3946
3947   // Otherwise compute it.
3948   const SCEV *C = computeSCEVAtScope(V, L);
3949   ValuesAtScopes[V][L] = C;
3950   return C;
3951 }
3952
3953 const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
3954   if (isa<SCEVConstant>(V)) return V;
3955
3956   // If this instruction is evolved from a constant-evolving PHI, compute the
3957   // exit value from the loop without using SCEVs.
3958   if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
3959     if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
3960       const Loop *LI = (*this->LI)[I->getParent()];
3961       if (LI && LI->getParentLoop() == L)  // Looking for loop exit value.
3962         if (PHINode *PN = dyn_cast<PHINode>(I))
3963           if (PN->getParent() == LI->getHeader()) {
3964             // Okay, there is no closed form solution for the PHI node.  Check
3965             // to see if the loop that contains it has a known backedge-taken
3966             // count.  If so, we may be able to force computation of the exit
3967             // value.
3968             const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
3969             if (const SCEVConstant *BTCC =
3970                   dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
3971               // Okay, we know how many times the containing loop executes.  If
3972               // this is a constant evolving PHI node, get the final value at
3973               // the specified iteration number.
3974               Constant *RV = getConstantEvolutionLoopExitValue(PN,
3975                                                    BTCC->getValue()->getValue(),
3976                                                                LI);
3977               if (RV) return getSCEV(RV);
3978             }
3979           }
3980
3981       // Okay, this is an expression that we cannot symbolically evaluate
3982       // into a SCEV.  Check to see if it's possible to symbolically evaluate
3983       // the arguments into constants, and if so, try to constant propagate the
3984       // result.  This is particularly useful for computing loop exit values.
3985       if (CanConstantFold(I)) {
3986         std::vector<Constant*> Operands;
3987         Operands.reserve(I->getNumOperands());
3988         for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
3989           Value *Op = I->getOperand(i);
3990           if (Constant *C = dyn_cast<Constant>(Op)) {
3991             Operands.push_back(C);
3992           } else {
3993             // If any of the operands is non-constant and if they are
3994             // non-integer and non-pointer, don't even try to analyze them
3995             // with scev techniques.
3996             if (!isSCEVable(Op->getType()))
3997               return V;
3998
3999             const SCEV* OpV = getSCEVAtScope(Op, L);
4000             if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(OpV)) {
4001               Constant *C = SC->getValue();
4002               if (C->getType() != Op->getType())
4003                 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4004                                                                   Op->getType(),
4005                                                                   false),
4006                                           C, Op->getType());
4007               Operands.push_back(C);
4008             } else if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(OpV)) {
4009               if (Constant *C = dyn_cast<Constant>(SU->getValue())) {
4010                 if (C->getType() != Op->getType())
4011                   C =
4012                     ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
4013                                                                   Op->getType(),
4014                                                                   false),
4015                                           C, Op->getType());
4016                 Operands.push_back(C);
4017               } else
4018                 return V;
4019             } else {
4020               return V;
4021             }
4022           }
4023         }
4024
4025         Constant *C;
4026         if (const CmpInst *CI = dyn_cast<CmpInst>(I))
4027           C = ConstantFoldCompareInstOperands(CI->getPredicate(),
4028                                               &Operands[0], Operands.size(),
4029                                               getContext());
4030         else
4031           C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
4032                                        &Operands[0], Operands.size(),
4033                                        getContext());
4034         return getSCEV(C);
4035       }
4036     }
4037
4038     // This is some other type of SCEVUnknown, just return it.
4039     return V;
4040   }
4041
4042   if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
4043     // Avoid performing the look-up in the common case where the specified
4044     // expression has no loop-variant portions.
4045     for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
4046       const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
4047       if (OpAtScope != Comm->getOperand(i)) {
4048         // Okay, at least one of these operands is loop variant but might be
4049         // foldable.  Build a new instance of the folded commutative expression.
4050         SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
4051                                             Comm->op_begin()+i);
4052         NewOps.push_back(OpAtScope);
4053
4054         for (++i; i != e; ++i) {
4055           OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
4056           NewOps.push_back(OpAtScope);
4057         }
4058         if (isa<SCEVAddExpr>(Comm))
4059           return getAddExpr(NewOps);
4060         if (isa<SCEVMulExpr>(Comm))
4061           return getMulExpr(NewOps);
4062         if (isa<SCEVSMaxExpr>(Comm))
4063           return getSMaxExpr(NewOps);
4064         if (isa<SCEVUMaxExpr>(Comm))
4065           return getUMaxExpr(NewOps);
4066         llvm_unreachable("Unknown commutative SCEV type!");
4067       }
4068     }
4069     // If we got here, all operands are loop invariant.
4070     return Comm;
4071   }
4072
4073   if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
4074     const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
4075     const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
4076     if (LHS == Div->getLHS() && RHS == Div->getRHS())
4077       return Div;   // must be loop invariant
4078     return getUDivExpr(LHS, RHS);
4079   }
4080
4081   // If this is a loop recurrence for a loop that does not contain L, then we
4082   // are dealing with the final value computed by the loop.
4083   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
4084     if (!L || !AddRec->getLoop()->contains(L->getHeader())) {
4085       // To evaluate this recurrence, we need to know how many times the AddRec
4086       // loop iterates.  Compute this now.
4087       const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
4088       if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
4089
4090       // Then, evaluate the AddRec.
4091       return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
4092     }
4093     return AddRec;
4094   }
4095
4096   if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
4097     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
4098     if (Op == Cast->getOperand())
4099       return Cast;  // must be loop invariant
4100     return getZeroExtendExpr(Op, Cast->getType());
4101   }
4102
4103   if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
4104     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
4105     if (Op == Cast->getOperand())
4106       return Cast;  // must be loop invariant
4107     return getSignExtendExpr(Op, Cast->getType());
4108   }
4109
4110   if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
4111     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
4112     if (Op == Cast->getOperand())
4113       return Cast;  // must be loop invariant
4114     return getTruncateExpr(Op, Cast->getType());
4115   }
4116
4117   if (isa<SCEVTargetDataConstant>(V))
4118     return V;
4119
4120   llvm_unreachable("Unknown SCEV type!");
4121   return 0;
4122 }
4123
4124 /// getSCEVAtScope - This is a convenience function which does
4125 /// getSCEVAtScope(getSCEV(V), L).
4126 const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
4127   return getSCEVAtScope(getSCEV(V), L);
4128 }
4129
4130 /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the
4131 /// following equation:
4132 ///
4133 ///     A * X = B (mod N)
4134 ///
4135 /// where N = 2^BW and BW is the common bit width of A and B. The signedness of
4136 /// A and B isn't important.
4137 ///
4138 /// If the equation does not have a solution, SCEVCouldNotCompute is returned.
4139 static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
4140                                                ScalarEvolution &SE) {
4141   uint32_t BW = A.getBitWidth();
4142   assert(BW == B.getBitWidth() && "Bit widths must be the same.");
4143   assert(A != 0 && "A must be non-zero.");
4144
4145   // 1. D = gcd(A, N)
4146   //
4147   // The gcd of A and N may have only one prime factor: 2. The number of
4148   // trailing zeros in A is its multiplicity
4149   uint32_t Mult2 = A.countTrailingZeros();
4150   // D = 2^Mult2
4151
4152   // 2. Check if B is divisible by D.
4153   //
4154   // B is divisible by D if and only if the multiplicity of prime factor 2 for B
4155   // is not less than multiplicity of this prime factor for D.
4156   if (B.countTrailingZeros() < Mult2)
4157     return SE.getCouldNotCompute();
4158
4159   // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
4160   // modulo (N / D).
4161   //
4162   // (N / D) may need BW+1 bits in its representation.  Hence, we'll use this
4163   // bit width during computations.
4164   APInt AD = A.lshr(Mult2).zext(BW + 1);  // AD = A / D
4165   APInt Mod(BW + 1, 0);
4166   Mod.set(BW - Mult2);  // Mod = N / D
4167   APInt I = AD.multiplicativeInverse(Mod);
4168
4169   // 4. Compute the minimum unsigned root of the equation:
4170   // I * (B / D) mod (N / D)
4171   APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod);
4172
4173   // The result is guaranteed to be less than 2^BW so we may truncate it to BW
4174   // bits.
4175   return SE.getConstant(Result.trunc(BW));
4176 }
4177
4178 /// SolveQuadraticEquation - Find the roots of the quadratic equation for the
4179 /// given quadratic chrec {L,+,M,+,N}.  This returns either the two roots (which
4180 /// might be the same) or two SCEVCouldNotCompute objects.
4181 ///
4182 static std::pair<const SCEV *,const SCEV *>
4183 SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
4184   assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
4185   const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
4186   const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
4187   const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
4188
4189   // We currently can only solve this if the coefficients are constants.
4190   if (!LC || !MC || !NC) {
4191     const SCEV *CNC = SE.getCouldNotCompute();
4192     return std::make_pair(CNC, CNC);
4193   }
4194
4195   uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
4196   const APInt &L = LC->getValue()->getValue();
4197   const APInt &M = MC->getValue()->getValue();
4198   const APInt &N = NC->getValue()->getValue();
4199   APInt Two(BitWidth, 2);
4200   APInt Four(BitWidth, 4);
4201
4202   {
4203     using namespace APIntOps;
4204     const APInt& C = L;
4205     // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
4206     // The B coefficient is M-N/2
4207     APInt B(M);
4208     B -= sdiv(N,Two);
4209
4210     // The A coefficient is N/2
4211     APInt A(N.sdiv(Two));
4212
4213     // Compute the B^2-4ac term.
4214     APInt SqrtTerm(B);
4215     SqrtTerm *= B;
4216     SqrtTerm -= Four * (A * C);
4217
4218     // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
4219     // integer value or else APInt::sqrt() will assert.
4220     APInt SqrtVal(SqrtTerm.sqrt());
4221
4222     // Compute the two solutions for the quadratic formula.
4223     // The divisions must be performed as signed divisions.
4224     APInt NegB(-B);
4225     APInt TwoA( A << 1 );
4226     if (TwoA.isMinValue()) {
4227       const SCEV *CNC = SE.getCouldNotCompute();
4228       return std::make_pair(CNC, CNC);
4229     }
4230
4231     LLVMContext &Context = SE.getContext();
4232
4233     ConstantInt *Solution1 =
4234       ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
4235     ConstantInt *Solution2 =
4236       ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
4237
4238     return std::make_pair(SE.getConstant(Solution1),
4239                           SE.getConstant(Solution2));
4240     } // end APIntOps namespace
4241 }
4242
4243 /// HowFarToZero - Return the number of times a backedge comparing the specified
4244 /// value to zero will execute.  If not computable, return CouldNotCompute.
4245 const SCEV *ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) {
4246   // If the value is a constant
4247   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
4248     // If the value is already zero, the branch will execute zero times.
4249     if (C->getValue()->isZero()) return C;
4250     return getCouldNotCompute();  // Otherwise it will loop infinitely.
4251   }
4252
4253   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
4254   if (!AddRec || AddRec->getLoop() != L)
4255     return getCouldNotCompute();
4256
4257   if (AddRec->isAffine()) {
4258     // If this is an affine expression, the execution count of this branch is
4259     // the minimum unsigned root of the following equation:
4260     //
4261     //     Start + Step*N = 0 (mod 2^BW)
4262     //
4263     // equivalent to:
4264     //
4265     //             Step*N = -Start (mod 2^BW)
4266     //
4267     // where BW is the common bit width of Start and Step.
4268
4269     // Get the initial value for the loop.
4270     const SCEV *Start = getSCEVAtScope(AddRec->getStart(),
4271                                        L->getParentLoop());
4272     const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1),
4273                                       L->getParentLoop());
4274
4275     if (const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step)) {
4276       // For now we handle only constant steps.
4277
4278       // First, handle unitary steps.
4279       if (StepC->getValue()->equalsInt(1))      // 1*N = -Start (mod 2^BW), so:
4280         return getNegativeSCEV(Start);          //   N = -Start (as unsigned)
4281       if (StepC->getValue()->isAllOnesValue())  // -1*N = -Start (mod 2^BW), so:
4282         return Start;                           //    N = Start (as unsigned)
4283
4284       // Then, try to solve the above equation provided that Start is constant.
4285       if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
4286         return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
4287                                             -StartC->getValue()->getValue(),
4288                                             *this);
4289     }
4290   } else if (AddRec->isQuadratic() && AddRec->getType()->isInteger()) {
4291     // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
4292     // the quadratic equation to solve it.
4293     std::pair<const SCEV *,const SCEV *> Roots = SolveQuadraticEquation(AddRec,
4294                                                                     *this);
4295     const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
4296     const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
4297     if (R1) {
4298 #if 0
4299       errs() << "HFTZ: " << *V << " - sol#1: " << *R1
4300              << "  sol#2: " << *R2 << "\n";
4301 #endif
4302       // Pick the smallest positive root value.
4303       if (ConstantInt *CB =
4304           dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
4305                                    R1->getValue(), R2->getValue()))) {
4306         if (CB->getZExtValue() == false)
4307           std::swap(R1, R2);   // R1 is the minimum root now.
4308
4309         // We can only use this value if the chrec ends up with an exact zero
4310         // value at this index.  When solving for "X*X != 5", for example, we
4311         // should not accept a root of 2.
4312         const SCEV *Val = AddRec->evaluateAtIteration(R1, *this);
4313         if (Val->isZero())
4314           return R1;  // We found a quadratic root!
4315       }
4316     }
4317   }
4318
4319   return getCouldNotCompute();
4320 }
4321
4322 /// HowFarToNonZero - Return the number of times a backedge checking the
4323 /// specified value for nonzero will execute.  If not computable, return
4324 /// CouldNotCompute
4325 const SCEV *ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
4326   // Loops that look like: while (X == 0) are very strange indeed.  We don't
4327   // handle them yet except for the trivial case.  This could be expanded in the
4328   // future as needed.
4329
4330   // If the value is a constant, check to see if it is known to be non-zero
4331   // already.  If so, the backedge will execute zero times.
4332   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
4333     if (!C->getValue()->isNullValue())
4334       return getIntegerSCEV(0, C->getType());
4335     return getCouldNotCompute();  // Otherwise it will loop infinitely.
4336   }
4337
4338   // We could implement others, but I really doubt anyone writes loops like
4339   // this, and if they did, they would already be constant folded.
4340   return getCouldNotCompute();
4341 }
4342
4343 /// getLoopPredecessor - If the given loop's header has exactly one unique
4344 /// predecessor outside the loop, return it. Otherwise return null.
4345 ///
4346 BasicBlock *ScalarEvolution::getLoopPredecessor(const Loop *L) {
4347   BasicBlock *Header = L->getHeader();
4348   BasicBlock *Pred = 0;
4349   for (pred_iterator PI = pred_begin(Header), E = pred_end(Header);
4350        PI != E; ++PI)
4351     if (!L->contains(*PI)) {
4352       if (Pred && Pred != *PI) return 0; // Multiple predecessors.
4353       Pred = *PI;
4354     }
4355   return Pred;
4356 }
4357
4358 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
4359 /// (which may not be an immediate predecessor) which has exactly one
4360 /// successor from which BB is reachable, or null if no such block is
4361 /// found.
4362 ///
4363 BasicBlock *
4364 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
4365   // If the block has a unique predecessor, then there is no path from the
4366   // predecessor to the block that does not go through the direct edge
4367   // from the predecessor to the block.
4368   if (BasicBlock *Pred = BB->getSinglePredecessor())
4369     return Pred;
4370
4371   // A loop's header is defined to be a block that dominates the loop.
4372   // If the header has a unique predecessor outside the loop, it must be
4373   // a block that has exactly one successor that can reach the loop.
4374   if (Loop *L = LI->getLoopFor(BB))
4375     return getLoopPredecessor(L);
4376
4377   return 0;
4378 }
4379
4380 /// HasSameValue - SCEV structural equivalence is usually sufficient for
4381 /// testing whether two expressions are equal, however for the purposes of
4382 /// looking for a condition guarding a loop, it can be useful to be a little
4383 /// more general, since a front-end may have replicated the controlling
4384 /// expression.
4385 ///
4386 static bool HasSameValue(const SCEV *A, const SCEV *B) {
4387   // Quick check to see if they are the same SCEV.
4388   if (A == B) return true;
4389
4390   // Otherwise, if they're both SCEVUnknown, it's possible that they hold
4391   // two different instructions with the same value. Check for this case.
4392   if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
4393     if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
4394       if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
4395         if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
4396           if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory())
4397             return true;
4398
4399   // Otherwise assume they may have a different value.
4400   return false;
4401 }
4402
4403 bool ScalarEvolution::isKnownNegative(const SCEV *S) {
4404   return getSignedRange(S).getSignedMax().isNegative();
4405 }
4406
4407 bool ScalarEvolution::isKnownPositive(const SCEV *S) {
4408   return getSignedRange(S).getSignedMin().isStrictlyPositive();
4409 }
4410
4411 bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
4412   return !getSignedRange(S).getSignedMin().isNegative();
4413 }
4414
4415 bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
4416   return !getSignedRange(S).getSignedMax().isStrictlyPositive();
4417 }
4418
4419 bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
4420   return isKnownNegative(S) || isKnownPositive(S);
4421 }
4422
4423 bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
4424                                        const SCEV *LHS, const SCEV *RHS) {
4425
4426   if (HasSameValue(LHS, RHS))
4427     return ICmpInst::isTrueWhenEqual(Pred);
4428
4429   switch (Pred) {
4430   default:
4431     llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4432     break;
4433   case ICmpInst::ICMP_SGT:
4434     Pred = ICmpInst::ICMP_SLT;
4435     std::swap(LHS, RHS);
4436   case ICmpInst::ICMP_SLT: {
4437     ConstantRange LHSRange = getSignedRange(LHS);
4438     ConstantRange RHSRange = getSignedRange(RHS);
4439     if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin()))
4440       return true;
4441     if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax()))
4442       return false;
4443     break;
4444   }
4445   case ICmpInst::ICMP_SGE:
4446     Pred = ICmpInst::ICMP_SLE;
4447     std::swap(LHS, RHS);
4448   case ICmpInst::ICMP_SLE: {
4449     ConstantRange LHSRange = getSignedRange(LHS);
4450     ConstantRange RHSRange = getSignedRange(RHS);
4451     if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin()))
4452       return true;
4453     if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax()))
4454       return false;
4455     break;
4456   }
4457   case ICmpInst::ICMP_UGT:
4458     Pred = ICmpInst::ICMP_ULT;
4459     std::swap(LHS, RHS);
4460   case ICmpInst::ICMP_ULT: {
4461     ConstantRange LHSRange = getUnsignedRange(LHS);
4462     ConstantRange RHSRange = getUnsignedRange(RHS);
4463     if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin()))
4464       return true;
4465     if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax()))
4466       return false;
4467     break;
4468   }
4469   case ICmpInst::ICMP_UGE:
4470     Pred = ICmpInst::ICMP_ULE;
4471     std::swap(LHS, RHS);
4472   case ICmpInst::ICMP_ULE: {
4473     ConstantRange LHSRange = getUnsignedRange(LHS);
4474     ConstantRange RHSRange = getUnsignedRange(RHS);
4475     if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin()))
4476       return true;
4477     if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax()))
4478       return false;
4479     break;
4480   }
4481   case ICmpInst::ICMP_NE: {
4482     if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet())
4483       return true;
4484     if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet())
4485       return true;
4486
4487     const SCEV *Diff = getMinusSCEV(LHS, RHS);
4488     if (isKnownNonZero(Diff))
4489       return true;
4490     break;
4491   }
4492   case ICmpInst::ICMP_EQ:
4493     // The check at the top of the function catches the case where
4494     // the values are known to be equal.
4495     break;
4496   }
4497   return false;
4498 }
4499
4500 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
4501 /// protected by a conditional between LHS and RHS.  This is used to
4502 /// to eliminate casts.
4503 bool
4504 ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
4505                                              ICmpInst::Predicate Pred,
4506                                              const SCEV *LHS, const SCEV *RHS) {
4507   // Interpret a null as meaning no loop, where there is obviously no guard
4508   // (interprocedural conditions notwithstanding).
4509   if (!L) return true;
4510
4511   BasicBlock *Latch = L->getLoopLatch();
4512   if (!Latch)
4513     return false;
4514
4515   BranchInst *LoopContinuePredicate =
4516     dyn_cast<BranchInst>(Latch->getTerminator());
4517   if (!LoopContinuePredicate ||
4518       LoopContinuePredicate->isUnconditional())
4519     return false;
4520
4521   return isImpliedCond(LoopContinuePredicate->getCondition(), Pred, LHS, RHS,
4522                        LoopContinuePredicate->getSuccessor(0) != L->getHeader());
4523 }
4524
4525 /// isLoopGuardedByCond - Test whether entry to the loop is protected
4526 /// by a conditional between LHS and RHS.  This is used to help avoid max
4527 /// expressions in loop trip counts, and to eliminate casts.
4528 bool
4529 ScalarEvolution::isLoopGuardedByCond(const Loop *L,
4530                                      ICmpInst::Predicate Pred,
4531                                      const SCEV *LHS, const SCEV *RHS) {
4532   // Interpret a null as meaning no loop, where there is obviously no guard
4533   // (interprocedural conditions notwithstanding).
4534   if (!L) return false;
4535
4536   BasicBlock *Predecessor = getLoopPredecessor(L);
4537   BasicBlock *PredecessorDest = L->getHeader();
4538
4539   // Starting at the loop predecessor, climb up the predecessor chain, as long
4540   // as there are predecessors that can be found that have unique successors
4541   // leading to the original header.
4542   for (; Predecessor;
4543        PredecessorDest = Predecessor,
4544        Predecessor = getPredecessorWithUniqueSuccessorForBB(Predecessor)) {
4545
4546     BranchInst *LoopEntryPredicate =
4547       dyn_cast<BranchInst>(Predecessor->getTerminator());
4548     if (!LoopEntryPredicate ||
4549         LoopEntryPredicate->isUnconditional())
4550       continue;
4551
4552     if (isImpliedCond(LoopEntryPredicate->getCondition(), Pred, LHS, RHS,
4553                       LoopEntryPredicate->getSuccessor(0) != PredecessorDest))
4554       return true;
4555   }
4556
4557   return false;
4558 }
4559
4560 /// isImpliedCond - Test whether the condition described by Pred, LHS,
4561 /// and RHS is true whenever the given Cond value evaluates to true.
4562 bool ScalarEvolution::isImpliedCond(Value *CondValue,
4563                                     ICmpInst::Predicate Pred,
4564                                     const SCEV *LHS, const SCEV *RHS,
4565                                     bool Inverse) {
4566   // Recursivly handle And and Or conditions.
4567   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CondValue)) {
4568     if (BO->getOpcode() == Instruction::And) {
4569       if (!Inverse)
4570         return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4571                isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
4572     } else if (BO->getOpcode() == Instruction::Or) {
4573       if (Inverse)
4574         return isImpliedCond(BO->getOperand(0), Pred, LHS, RHS, Inverse) ||
4575                isImpliedCond(BO->getOperand(1), Pred, LHS, RHS, Inverse);
4576     }
4577   }
4578
4579   ICmpInst *ICI = dyn_cast<ICmpInst>(CondValue);
4580   if (!ICI) return false;
4581
4582   // Bail if the ICmp's operands' types are wider than the needed type
4583   // before attempting to call getSCEV on them. This avoids infinite
4584   // recursion, since the analysis of widening casts can require loop
4585   // exit condition information for overflow checking, which would
4586   // lead back here.
4587   if (getTypeSizeInBits(LHS->getType()) <
4588       getTypeSizeInBits(ICI->getOperand(0)->getType()))
4589     return false;
4590
4591   // Now that we found a conditional branch that dominates the loop, check to
4592   // see if it is the comparison we are looking for.
4593   ICmpInst::Predicate FoundPred;
4594   if (Inverse)
4595     FoundPred = ICI->getInversePredicate();
4596   else
4597     FoundPred = ICI->getPredicate();
4598
4599   const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
4600   const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
4601
4602   // Balance the types. The case where FoundLHS' type is wider than
4603   // LHS' type is checked for above.
4604   if (getTypeSizeInBits(LHS->getType()) >
4605       getTypeSizeInBits(FoundLHS->getType())) {
4606     if (CmpInst::isSigned(Pred)) {
4607       FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
4608       FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
4609     } else {
4610       FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
4611       FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
4612     }
4613   }
4614
4615   // Canonicalize the query to match the way instcombine will have
4616   // canonicalized the comparison.
4617   // First, put a constant operand on the right.
4618   if (isa<SCEVConstant>(LHS)) {
4619     std::swap(LHS, RHS);
4620     Pred = ICmpInst::getSwappedPredicate(Pred);
4621   }
4622   // Then, canonicalize comparisons with boundary cases.
4623   if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
4624     const APInt &RA = RC->getValue()->getValue();
4625     switch (Pred) {
4626     default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4627     case ICmpInst::ICMP_EQ:
4628     case ICmpInst::ICMP_NE:
4629       break;
4630     case ICmpInst::ICMP_UGE:
4631       if ((RA - 1).isMinValue()) {
4632         Pred = ICmpInst::ICMP_NE;
4633         RHS = getConstant(RA - 1);
4634         break;
4635       }
4636       if (RA.isMaxValue()) {
4637         Pred = ICmpInst::ICMP_EQ;
4638         break;
4639       }
4640       if (RA.isMinValue()) return true;
4641       break;
4642     case ICmpInst::ICMP_ULE:
4643       if ((RA + 1).isMaxValue()) {
4644         Pred = ICmpInst::ICMP_NE;
4645         RHS = getConstant(RA + 1);
4646         break;
4647       }
4648       if (RA.isMinValue()) {
4649         Pred = ICmpInst::ICMP_EQ;
4650         break;
4651       }
4652       if (RA.isMaxValue()) return true;
4653       break;
4654     case ICmpInst::ICMP_SGE:
4655       if ((RA - 1).isMinSignedValue()) {
4656         Pred = ICmpInst::ICMP_NE;
4657         RHS = getConstant(RA - 1);
4658         break;
4659       }
4660       if (RA.isMaxSignedValue()) {
4661         Pred = ICmpInst::ICMP_EQ;
4662         break;
4663       }
4664       if (RA.isMinSignedValue()) return true;
4665       break;
4666     case ICmpInst::ICMP_SLE:
4667       if ((RA + 1).isMaxSignedValue()) {
4668         Pred = ICmpInst::ICMP_NE;
4669         RHS = getConstant(RA + 1);
4670         break;
4671       }
4672       if (RA.isMinSignedValue()) {
4673         Pred = ICmpInst::ICMP_EQ;
4674         break;
4675       }
4676       if (RA.isMaxSignedValue()) return true;
4677       break;
4678     case ICmpInst::ICMP_UGT:
4679       if (RA.isMinValue()) {
4680         Pred = ICmpInst::ICMP_NE;
4681         break;
4682       }
4683       if ((RA + 1).isMaxValue()) {
4684         Pred = ICmpInst::ICMP_EQ;
4685         RHS = getConstant(RA + 1);
4686         break;
4687       }
4688       if (RA.isMaxValue()) return false;
4689       break;
4690     case ICmpInst::ICMP_ULT:
4691       if (RA.isMaxValue()) {
4692         Pred = ICmpInst::ICMP_NE;
4693         break;
4694       }
4695       if ((RA - 1).isMinValue()) {
4696         Pred = ICmpInst::ICMP_EQ;
4697         RHS = getConstant(RA - 1);
4698         break;
4699       }
4700       if (RA.isMinValue()) return false;
4701       break;
4702     case ICmpInst::ICMP_SGT:
4703       if (RA.isMinSignedValue()) {
4704         Pred = ICmpInst::ICMP_NE;
4705         break;
4706       }
4707       if ((RA + 1).isMaxSignedValue()) {
4708         Pred = ICmpInst::ICMP_EQ;
4709         RHS = getConstant(RA + 1);
4710         break;
4711       }
4712       if (RA.isMaxSignedValue()) return false;
4713       break;
4714     case ICmpInst::ICMP_SLT:
4715       if (RA.isMaxSignedValue()) {
4716         Pred = ICmpInst::ICMP_NE;
4717         break;
4718       }
4719       if ((RA - 1).isMinSignedValue()) {
4720        Pred = ICmpInst::ICMP_EQ;
4721        RHS = getConstant(RA - 1);
4722        break;
4723       }
4724       if (RA.isMinSignedValue()) return false;
4725       break;
4726     }
4727   }
4728
4729   // Check to see if we can make the LHS or RHS match.
4730   if (LHS == FoundRHS || RHS == FoundLHS) {
4731     if (isa<SCEVConstant>(RHS)) {
4732       std::swap(FoundLHS, FoundRHS);
4733       FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
4734     } else {
4735       std::swap(LHS, RHS);
4736       Pred = ICmpInst::getSwappedPredicate(Pred);
4737     }
4738   }
4739
4740   // Check whether the found predicate is the same as the desired predicate.
4741   if (FoundPred == Pred)
4742     return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
4743
4744   // Check whether swapping the found predicate makes it the same as the
4745   // desired predicate.
4746   if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
4747     if (isa<SCEVConstant>(RHS))
4748       return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
4749     else
4750       return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
4751                                    RHS, LHS, FoundLHS, FoundRHS);
4752   }
4753
4754   // Check whether the actual condition is beyond sufficient.
4755   if (FoundPred == ICmpInst::ICMP_EQ)
4756     if (ICmpInst::isTrueWhenEqual(Pred))
4757       if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
4758         return true;
4759   if (Pred == ICmpInst::ICMP_NE)
4760     if (!ICmpInst::isTrueWhenEqual(FoundPred))
4761       if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
4762         return true;
4763
4764   // Otherwise assume the worst.
4765   return false;
4766 }
4767
4768 /// isImpliedCondOperands - Test whether the condition described by Pred,
4769 /// LHS, and RHS is true whenever the condition desribed by Pred, FoundLHS,
4770 /// and FoundRHS is true.
4771 bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
4772                                             const SCEV *LHS, const SCEV *RHS,
4773                                             const SCEV *FoundLHS,
4774                                             const SCEV *FoundRHS) {
4775   return isImpliedCondOperandsHelper(Pred, LHS, RHS,
4776                                      FoundLHS, FoundRHS) ||
4777          // ~x < ~y --> x > y
4778          isImpliedCondOperandsHelper(Pred, LHS, RHS,
4779                                      getNotSCEV(FoundRHS),
4780                                      getNotSCEV(FoundLHS));
4781 }
4782
4783 /// isImpliedCondOperandsHelper - Test whether the condition described by
4784 /// Pred, LHS, and RHS is true whenever the condition desribed by Pred,
4785 /// FoundLHS, and FoundRHS is true.
4786 bool
4787 ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
4788                                              const SCEV *LHS, const SCEV *RHS,
4789                                              const SCEV *FoundLHS,
4790                                              const SCEV *FoundRHS) {
4791   switch (Pred) {
4792   default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
4793   case ICmpInst::ICMP_EQ:
4794   case ICmpInst::ICMP_NE:
4795     if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
4796       return true;
4797     break;
4798   case ICmpInst::ICMP_SLT:
4799   case ICmpInst::ICMP_SLE:
4800     if (isKnownPredicate(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
4801         isKnownPredicate(ICmpInst::ICMP_SGE, RHS, FoundRHS))
4802       return true;
4803     break;
4804   case ICmpInst::ICMP_SGT:
4805   case ICmpInst::ICMP_SGE:
4806     if (isKnownPredicate(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
4807         isKnownPredicate(ICmpInst::ICMP_SLE, RHS, FoundRHS))
4808       return true;
4809     break;
4810   case ICmpInst::ICMP_ULT:
4811   case ICmpInst::ICMP_ULE:
4812     if (isKnownPredicate(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
4813         isKnownPredicate(ICmpInst::ICMP_UGE, RHS, FoundRHS))
4814       return true;
4815     break;
4816   case ICmpInst::ICMP_UGT:
4817   case ICmpInst::ICMP_UGE:
4818     if (isKnownPredicate(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
4819         isKnownPredicate(ICmpInst::ICMP_ULE, RHS, FoundRHS))
4820       return true;
4821     break;
4822   }
4823
4824   return false;
4825 }
4826
4827 /// getBECount - Subtract the end and start values and divide by the step,
4828 /// rounding up, to get the number of times the backedge is executed. Return
4829 /// CouldNotCompute if an intermediate computation overflows.
4830 const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
4831                                         const SCEV *End,
4832                                         const SCEV *Step,
4833                                         bool NoWrap) {
4834   const Type *Ty = Start->getType();
4835   const SCEV *NegOne = getIntegerSCEV(-1, Ty);
4836   const SCEV *Diff = getMinusSCEV(End, Start);
4837   const SCEV *RoundUp = getAddExpr(Step, NegOne);
4838
4839   // Add an adjustment to the difference between End and Start so that
4840   // the division will effectively round up.
4841   const SCEV *Add = getAddExpr(Diff, RoundUp);
4842
4843   if (!NoWrap) {
4844     // Check Add for unsigned overflow.
4845     // TODO: More sophisticated things could be done here.
4846     const Type *WideTy = IntegerType::get(getContext(),
4847                                           getTypeSizeInBits(Ty) + 1);
4848     const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
4849     const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
4850     const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
4851     if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
4852       return getCouldNotCompute();
4853   }
4854
4855   return getUDivExpr(Add, Step);
4856 }
4857
4858 /// HowManyLessThans - Return the number of times a backedge containing the
4859 /// specified less-than comparison will execute.  If not computable, return
4860 /// CouldNotCompute.
4861 ScalarEvolution::BackedgeTakenInfo
4862 ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
4863                                   const Loop *L, bool isSigned) {
4864   // Only handle:  "ADDREC < LoopInvariant".
4865   if (!RHS->isLoopInvariant(L)) return getCouldNotCompute();
4866
4867   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
4868   if (!AddRec || AddRec->getLoop() != L)
4869     return getCouldNotCompute();
4870
4871   // Check to see if we have a flag which makes analysis easy.
4872   bool NoWrap = isSigned ? AddRec->hasNoSignedWrap() :
4873                            AddRec->hasNoUnsignedWrap();
4874
4875   if (AddRec->isAffine()) {
4876     // FORNOW: We only support unit strides.
4877     unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
4878     const SCEV *Step = AddRec->getStepRecurrence(*this);
4879
4880     // TODO: handle non-constant strides.
4881     const SCEVConstant *CStep = dyn_cast<SCEVConstant>(Step);
4882     if (!CStep || CStep->isZero())
4883       return getCouldNotCompute();
4884     if (CStep->isOne()) {
4885       // With unit stride, the iteration never steps past the limit value.
4886     } else if (CStep->getValue()->getValue().isStrictlyPositive()) {
4887       if (NoWrap) {
4888         // We know the iteration won't step past the maximum value for its type.
4889         ;
4890       } else if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) {
4891         // Test whether a positive iteration iteration can step past the limit
4892         // value and past the maximum value for its type in a single step.
4893         if (isSigned) {
4894           APInt Max = APInt::getSignedMaxValue(BitWidth);
4895           if ((Max - CStep->getValue()->getValue())
4896                 .slt(CLimit->getValue()->getValue()))
4897             return getCouldNotCompute();
4898         } else {
4899           APInt Max = APInt::getMaxValue(BitWidth);
4900           if ((Max - CStep->getValue()->getValue())
4901                 .ult(CLimit->getValue()->getValue()))
4902             return getCouldNotCompute();
4903         }
4904       } else
4905         // TODO: handle non-constant limit values below.
4906         return getCouldNotCompute();
4907     } else
4908       // TODO: handle negative strides below.
4909       return getCouldNotCompute();
4910
4911     // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
4912     // m.  So, we count the number of iterations in which {n,+,s} < m is true.
4913     // Note that we cannot simply return max(m-n,0)/s because it's not safe to
4914     // treat m-n as signed nor unsigned due to overflow possibility.
4915
4916     // First, we get the value of the LHS in the first iteration: n
4917     const SCEV *Start = AddRec->getOperand(0);
4918
4919     // Determine the minimum constant start value.
4920     const SCEV *MinStart = getConstant(isSigned ?
4921       getSignedRange(Start).getSignedMin() :
4922       getUnsignedRange(Start).getUnsignedMin());
4923
4924     // If we know that the condition is true in order to enter the loop,
4925     // then we know that it will run exactly (m-n)/s times. Otherwise, we
4926     // only know that it will execute (max(m,n)-n)/s times. In both cases,
4927     // the division must round up.
4928     const SCEV *End = RHS;
4929     if (!isLoopGuardedByCond(L,
4930                              isSigned ? ICmpInst::ICMP_SLT :
4931                                         ICmpInst::ICMP_ULT,
4932                              getMinusSCEV(Start, Step), RHS))
4933       End = isSigned ? getSMaxExpr(RHS, Start)
4934                      : getUMaxExpr(RHS, Start);
4935
4936     // Determine the maximum constant end value.
4937     const SCEV *MaxEnd = getConstant(isSigned ?
4938       getSignedRange(End).getSignedMax() :
4939       getUnsignedRange(End).getUnsignedMax());
4940
4941     // Finally, we subtract these two values and divide, rounding up, to get
4942     // the number of times the backedge is executed.
4943     const SCEV *BECount = getBECount(Start, End, Step, NoWrap);
4944
4945     // The maximum backedge count is similar, except using the minimum start
4946     // value and the maximum end value.
4947     const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap);
4948
4949     return BackedgeTakenInfo(BECount, MaxBECount);
4950   }
4951
4952   return getCouldNotCompute();
4953 }
4954
4955 /// getNumIterationsInRange - Return the number of iterations of this loop that
4956 /// produce values in the specified constant range.  Another way of looking at
4957 /// this is that it returns the first iteration number where the value is not in
4958 /// the condition, thus computing the exit count. If the iteration count can't
4959 /// be computed, an instance of SCEVCouldNotCompute is returned.
4960 const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
4961                                                     ScalarEvolution &SE) const {
4962   if (Range.isFullSet())  // Infinite loop.
4963     return SE.getCouldNotCompute();
4964
4965   // If the start is a non-zero constant, shift the range to simplify things.
4966   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
4967     if (!SC->getValue()->isZero()) {
4968       SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
4969       Operands[0] = SE.getIntegerSCEV(0, SC->getType());
4970       const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop());
4971       if (const SCEVAddRecExpr *ShiftedAddRec =
4972             dyn_cast<SCEVAddRecExpr>(Shifted))
4973         return ShiftedAddRec->getNumIterationsInRange(
4974                            Range.subtract(SC->getValue()->getValue()), SE);
4975       // This is strange and shouldn't happen.
4976       return SE.getCouldNotCompute();
4977     }
4978
4979   // The only time we can solve this is when we have all constant indices.
4980   // Otherwise, we cannot determine the overflow conditions.
4981   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
4982     if (!isa<SCEVConstant>(getOperand(i)))
4983       return SE.getCouldNotCompute();
4984
4985
4986   // Okay at this point we know that all elements of the chrec are constants and
4987   // that the start element is zero.
4988
4989   // First check to see if the range contains zero.  If not, the first
4990   // iteration exits.
4991   unsigned BitWidth = SE.getTypeSizeInBits(getType());
4992   if (!Range.contains(APInt(BitWidth, 0)))
4993     return SE.getIntegerSCEV(0, getType());
4994
4995   if (isAffine()) {
4996     // If this is an affine expression then we have this situation:
4997     //   Solve {0,+,A} in Range  ===  Ax in Range
4998
4999     // We know that zero is in the range.  If A is positive then we know that
5000     // the upper value of the range must be the first possible exit value.
5001     // If A is negative then the lower of the range is the last possible loop
5002     // value.  Also note that we already checked for a full range.
5003     APInt One(BitWidth,1);
5004     APInt A     = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
5005     APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
5006
5007     // The exit value should be (End+A)/A.
5008     APInt ExitVal = (End + A).udiv(A);
5009     ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
5010
5011     // Evaluate at the exit value.  If we really did fall out of the valid
5012     // range, then we computed our trip count, otherwise wrap around or other
5013     // things must have happened.
5014     ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
5015     if (Range.contains(Val->getValue()))
5016       return SE.getCouldNotCompute();  // Something strange happened
5017
5018     // Ensure that the previous value is in the range.  This is a sanity check.
5019     assert(Range.contains(
5020            EvaluateConstantChrecAtConstant(this,
5021            ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) &&
5022            "Linear scev computation is off in a bad way!");
5023     return SE.getConstant(ExitValue);
5024   } else if (isQuadratic()) {
5025     // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
5026     // quadratic equation to solve it.  To do this, we must frame our problem in
5027     // terms of figuring out when zero is crossed, instead of when
5028     // Range.getUpper() is crossed.
5029     SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end());
5030     NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
5031     const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop());
5032
5033     // Next, solve the constructed addrec
5034     std::pair<const SCEV *,const SCEV *> Roots =
5035       SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
5036     const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
5037     const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
5038     if (R1) {
5039       // Pick the smallest positive root value.
5040       if (ConstantInt *CB =
5041           dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
5042                          R1->getValue(), R2->getValue()))) {
5043         if (CB->getZExtValue() == false)
5044           std::swap(R1, R2);   // R1 is the minimum root now.
5045
5046         // Make sure the root is not off by one.  The returned iteration should
5047         // not be in the range, but the previous one should be.  When solving
5048         // for "X*X < 5", for example, we should not return a root of 2.
5049         ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
5050                                                              R1->getValue(),
5051                                                              SE);
5052         if (Range.contains(R1Val->getValue())) {
5053           // The next iteration must be out of the range...
5054           ConstantInt *NextVal =
5055                 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1);
5056
5057           R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
5058           if (!Range.contains(R1Val->getValue()))
5059             return SE.getConstant(NextVal);
5060           return SE.getCouldNotCompute();  // Something strange happened
5061         }
5062
5063         // If R1 was not in the range, then it is a good return value.  Make
5064         // sure that R1-1 WAS in the range though, just in case.
5065         ConstantInt *NextVal =
5066                ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1);
5067         R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
5068         if (Range.contains(R1Val->getValue()))
5069           return R1;
5070         return SE.getCouldNotCompute();  // Something strange happened
5071       }
5072     }
5073   }
5074
5075   return SE.getCouldNotCompute();
5076 }
5077
5078
5079
5080 //===----------------------------------------------------------------------===//
5081 //                   SCEVCallbackVH Class Implementation
5082 //===----------------------------------------------------------------------===//
5083
5084 void ScalarEvolution::SCEVCallbackVH::deleted() {
5085   assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
5086   if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
5087     SE->ConstantEvolutionLoopExitValue.erase(PN);
5088   SE->Scalars.erase(getValPtr());
5089   // this now dangles!
5090 }
5091
5092 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *) {
5093   assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
5094
5095   // Forget all the expressions associated with users of the old value,
5096   // so that future queries will recompute the expressions using the new
5097   // value.
5098   SmallVector<User *, 16> Worklist;
5099   SmallPtrSet<User *, 8> Visited;
5100   Value *Old = getValPtr();
5101   bool DeleteOld = false;
5102   for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end();
5103        UI != UE; ++UI)
5104     Worklist.push_back(*UI);
5105   while (!Worklist.empty()) {
5106     User *U = Worklist.pop_back_val();
5107     // Deleting the Old value will cause this to dangle. Postpone
5108     // that until everything else is done.
5109     if (U == Old) {
5110       DeleteOld = true;
5111       continue;
5112     }
5113     if (!Visited.insert(U))
5114       continue;
5115     if (PHINode *PN = dyn_cast<PHINode>(U))
5116       SE->ConstantEvolutionLoopExitValue.erase(PN);
5117     SE->Scalars.erase(U);
5118     for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
5119          UI != UE; ++UI)
5120       Worklist.push_back(*UI);
5121   }
5122   // Delete the Old value if it (indirectly) references itself.
5123   if (DeleteOld) {
5124     if (PHINode *PN = dyn_cast<PHINode>(Old))
5125       SE->ConstantEvolutionLoopExitValue.erase(PN);
5126     SE->Scalars.erase(Old);
5127     // this now dangles!
5128   }
5129   // this may dangle!
5130 }
5131
5132 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
5133   : CallbackVH(V), SE(se) {}
5134
5135 //===----------------------------------------------------------------------===//
5136 //                   ScalarEvolution Class Implementation
5137 //===----------------------------------------------------------------------===//
5138
5139 ScalarEvolution::ScalarEvolution()
5140   : FunctionPass(&ID) {
5141 }
5142
5143 bool ScalarEvolution::runOnFunction(Function &F) {
5144   this->F = &F;
5145   LI = &getAnalysis<LoopInfo>();
5146   TD = getAnalysisIfAvailable<TargetData>();
5147   return false;
5148 }
5149
5150 void ScalarEvolution::releaseMemory() {
5151   Scalars.clear();
5152   BackedgeTakenCounts.clear();
5153   ConstantEvolutionLoopExitValue.clear();
5154   ValuesAtScopes.clear();
5155   UniqueSCEVs.clear();
5156   SCEVAllocator.Reset();
5157 }
5158
5159 void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
5160   AU.setPreservesAll();
5161   AU.addRequiredTransitive<LoopInfo>();
5162 }
5163
5164 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
5165   return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
5166 }
5167
5168 static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
5169                           const Loop *L) {
5170   // Print all inner loops first
5171   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
5172     PrintLoopInfo(OS, SE, *I);
5173
5174   OS << "Loop " << L->getHeader()->getName() << ": ";
5175
5176   SmallVector<BasicBlock*, 8> ExitBlocks;
5177   L->getExitBlocks(ExitBlocks);
5178   if (ExitBlocks.size() != 1)
5179     OS << "<multiple exits> ";
5180
5181   if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
5182     OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
5183   } else {
5184     OS << "Unpredictable backedge-taken count. ";
5185   }
5186
5187   OS << "\n";
5188   OS << "Loop " << L->getHeader()->getName() << ": ";
5189
5190   if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
5191     OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
5192   } else {
5193     OS << "Unpredictable max backedge-taken count. ";
5194   }
5195
5196   OS << "\n";
5197 }
5198
5199 void ScalarEvolution::print(raw_ostream &OS, const Module* ) const {
5200   // ScalarEvolution's implementaiton of the print method is to print
5201   // out SCEV values of all instructions that are interesting. Doing
5202   // this potentially causes it to create new SCEV objects though,
5203   // which technically conflicts with the const qualifier. This isn't
5204   // observable from outside the class though, so casting away the
5205   // const isn't dangerous.
5206   ScalarEvolution &SE = *const_cast<ScalarEvolution*>(this);
5207
5208   OS << "Classifying expressions for: " << F->getName() << "\n";
5209   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
5210     if (isSCEVable(I->getType())) {
5211       OS << *I << '\n';
5212       OS << "  -->  ";
5213       const SCEV *SV = SE.getSCEV(&*I);
5214       SV->print(OS);
5215
5216       const Loop *L = LI->getLoopFor((*I).getParent());
5217
5218       const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
5219       if (AtUse != SV) {
5220         OS << "  -->  ";
5221         AtUse->print(OS);
5222       }
5223
5224       if (L) {
5225         OS << "\t\t" "Exits: ";
5226         const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
5227         if (!ExitValue->isLoopInvariant(L)) {
5228           OS << "<<Unknown>>";
5229         } else {
5230           OS << *ExitValue;
5231         }
5232       }
5233
5234       OS << "\n";
5235     }
5236
5237   OS << "Determining loop execution counts for: " << F->getName() << "\n";
5238   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
5239     PrintLoopInfo(OS, &SE, *I);
5240 }
5241