Fix SCEV forgetMemoizedResults should search and destroy backedge exprs.
[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/ScalarEvolution.h"
63 #include "llvm/ADT/STLExtras.h"
64 #include "llvm/ADT/SmallPtrSet.h"
65 #include "llvm/ADT/Statistic.h"
66 #include "llvm/Analysis/ConstantFolding.h"
67 #include "llvm/Analysis/Dominators.h"
68 #include "llvm/Analysis/InstructionSimplify.h"
69 #include "llvm/Analysis/LoopInfo.h"
70 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
71 #include "llvm/Analysis/ValueTracking.h"
72 #include "llvm/Assembly/Writer.h"
73 #include "llvm/IR/Constants.h"
74 #include "llvm/IR/DataLayout.h"
75 #include "llvm/IR/DerivedTypes.h"
76 #include "llvm/IR/GlobalAlias.h"
77 #include "llvm/IR/GlobalVariable.h"
78 #include "llvm/IR/Instructions.h"
79 #include "llvm/IR/LLVMContext.h"
80 #include "llvm/IR/Operator.h"
81 #include "llvm/Support/CommandLine.h"
82 #include "llvm/Support/ConstantRange.h"
83 #include "llvm/Support/Debug.h"
84 #include "llvm/Support/ErrorHandling.h"
85 #include "llvm/Support/GetElementPtrTypeIterator.h"
86 #include "llvm/Support/InstIterator.h"
87 #include "llvm/Support/MathExtras.h"
88 #include "llvm/Support/raw_ostream.h"
89 #include "llvm/Target/TargetLibraryInfo.h"
90 #include <algorithm>
91 using namespace llvm;
92
93 STATISTIC(NumArrayLenItCounts,
94           "Number of trip counts computed with array length");
95 STATISTIC(NumTripCountsComputed,
96           "Number of loops with predictable loop counts");
97 STATISTIC(NumTripCountsNotComputed,
98           "Number of loops without predictable loop counts");
99 STATISTIC(NumBruteForceTripCountsComputed,
100           "Number of loops with trip counts computed by force");
101
102 static cl::opt<unsigned>
103 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
104                         cl::desc("Maximum number of iterations SCEV will "
105                                  "symbolically execute a constant "
106                                  "derived loop"),
107                         cl::init(100));
108
109 // FIXME: Enable this with XDEBUG when the test suite is clean.
110 static cl::opt<bool>
111 VerifySCEV("verify-scev",
112            cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
113
114 INITIALIZE_PASS_BEGIN(ScalarEvolution, "scalar-evolution",
115                 "Scalar Evolution Analysis", false, true)
116 INITIALIZE_PASS_DEPENDENCY(LoopInfo)
117 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
118 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
119 INITIALIZE_PASS_END(ScalarEvolution, "scalar-evolution",
120                 "Scalar Evolution Analysis", false, true)
121 char ScalarEvolution::ID = 0;
122
123 //===----------------------------------------------------------------------===//
124 //                           SCEV class definitions
125 //===----------------------------------------------------------------------===//
126
127 //===----------------------------------------------------------------------===//
128 // Implementation of the SCEV class.
129 //
130
131 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
132 void SCEV::dump() const {
133   print(dbgs());
134   dbgs() << '\n';
135 }
136 #endif
137
138 void SCEV::print(raw_ostream &OS) const {
139   switch (getSCEVType()) {
140   case scConstant:
141     WriteAsOperand(OS, cast<SCEVConstant>(this)->getValue(), false);
142     return;
143   case scTruncate: {
144     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
145     const SCEV *Op = Trunc->getOperand();
146     OS << "(trunc " << *Op->getType() << " " << *Op << " to "
147        << *Trunc->getType() << ")";
148     return;
149   }
150   case scZeroExtend: {
151     const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
152     const SCEV *Op = ZExt->getOperand();
153     OS << "(zext " << *Op->getType() << " " << *Op << " to "
154        << *ZExt->getType() << ")";
155     return;
156   }
157   case scSignExtend: {
158     const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
159     const SCEV *Op = SExt->getOperand();
160     OS << "(sext " << *Op->getType() << " " << *Op << " to "
161        << *SExt->getType() << ")";
162     return;
163   }
164   case scAddRecExpr: {
165     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
166     OS << "{" << *AR->getOperand(0);
167     for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
168       OS << ",+," << *AR->getOperand(i);
169     OS << "}<";
170     if (AR->getNoWrapFlags(FlagNUW))
171       OS << "nuw><";
172     if (AR->getNoWrapFlags(FlagNSW))
173       OS << "nsw><";
174     if (AR->getNoWrapFlags(FlagNW) &&
175         !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW)))
176       OS << "nw><";
177     WriteAsOperand(OS, AR->getLoop()->getHeader(), /*PrintType=*/false);
178     OS << ">";
179     return;
180   }
181   case scAddExpr:
182   case scMulExpr:
183   case scUMaxExpr:
184   case scSMaxExpr: {
185     const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
186     const char *OpStr = 0;
187     switch (NAry->getSCEVType()) {
188     case scAddExpr: OpStr = " + "; break;
189     case scMulExpr: OpStr = " * "; break;
190     case scUMaxExpr: OpStr = " umax "; break;
191     case scSMaxExpr: OpStr = " smax "; break;
192     }
193     OS << "(";
194     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
195          I != E; ++I) {
196       OS << **I;
197       if (llvm::next(I) != E)
198         OS << OpStr;
199     }
200     OS << ")";
201     switch (NAry->getSCEVType()) {
202     case scAddExpr:
203     case scMulExpr:
204       if (NAry->getNoWrapFlags(FlagNUW))
205         OS << "<nuw>";
206       if (NAry->getNoWrapFlags(FlagNSW))
207         OS << "<nsw>";
208     }
209     return;
210   }
211   case scUDivExpr: {
212     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
213     OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
214     return;
215   }
216   case scUnknown: {
217     const SCEVUnknown *U = cast<SCEVUnknown>(this);
218     Type *AllocTy;
219     if (U->isSizeOf(AllocTy)) {
220       OS << "sizeof(" << *AllocTy << ")";
221       return;
222     }
223     if (U->isAlignOf(AllocTy)) {
224       OS << "alignof(" << *AllocTy << ")";
225       return;
226     }
227
228     Type *CTy;
229     Constant *FieldNo;
230     if (U->isOffsetOf(CTy, FieldNo)) {
231       OS << "offsetof(" << *CTy << ", ";
232       WriteAsOperand(OS, FieldNo, false);
233       OS << ")";
234       return;
235     }
236
237     // Otherwise just print it normally.
238     WriteAsOperand(OS, U->getValue(), false);
239     return;
240   }
241   case scCouldNotCompute:
242     OS << "***COULDNOTCOMPUTE***";
243     return;
244   default: break;
245   }
246   llvm_unreachable("Unknown SCEV kind!");
247 }
248
249 Type *SCEV::getType() const {
250   switch (getSCEVType()) {
251   case scConstant:
252     return cast<SCEVConstant>(this)->getType();
253   case scTruncate:
254   case scZeroExtend:
255   case scSignExtend:
256     return cast<SCEVCastExpr>(this)->getType();
257   case scAddRecExpr:
258   case scMulExpr:
259   case scUMaxExpr:
260   case scSMaxExpr:
261     return cast<SCEVNAryExpr>(this)->getType();
262   case scAddExpr:
263     return cast<SCEVAddExpr>(this)->getType();
264   case scUDivExpr:
265     return cast<SCEVUDivExpr>(this)->getType();
266   case scUnknown:
267     return cast<SCEVUnknown>(this)->getType();
268   case scCouldNotCompute:
269     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
270   default:
271     llvm_unreachable("Unknown SCEV kind!");
272   }
273 }
274
275 bool SCEV::isZero() const {
276   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
277     return SC->getValue()->isZero();
278   return false;
279 }
280
281 bool SCEV::isOne() const {
282   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
283     return SC->getValue()->isOne();
284   return false;
285 }
286
287 bool SCEV::isAllOnesValue() const {
288   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
289     return SC->getValue()->isAllOnesValue();
290   return false;
291 }
292
293 /// isNonConstantNegative - Return true if the specified scev is negated, but
294 /// not a constant.
295 bool SCEV::isNonConstantNegative() const {
296   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
297   if (!Mul) return false;
298
299   // If there is a constant factor, it will be first.
300   const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
301   if (!SC) return false;
302
303   // Return true if the value is negative, this matches things like (-42 * V).
304   return SC->getValue()->getValue().isNegative();
305 }
306
307 SCEVCouldNotCompute::SCEVCouldNotCompute() :
308   SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {}
309
310 bool SCEVCouldNotCompute::classof(const SCEV *S) {
311   return S->getSCEVType() == scCouldNotCompute;
312 }
313
314 const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
315   FoldingSetNodeID ID;
316   ID.AddInteger(scConstant);
317   ID.AddPointer(V);
318   void *IP = 0;
319   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
320   SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
321   UniqueSCEVs.InsertNode(S, IP);
322   return S;
323 }
324
325 const SCEV *ScalarEvolution::getConstant(const APInt& Val) {
326   return getConstant(ConstantInt::get(getContext(), Val));
327 }
328
329 const SCEV *
330 ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
331   IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
332   return getConstant(ConstantInt::get(ITy, V, isSigned));
333 }
334
335 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID,
336                            unsigned SCEVTy, const SCEV *op, Type *ty)
337   : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
338
339 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
340                                    const SCEV *op, Type *ty)
341   : SCEVCastExpr(ID, scTruncate, op, ty) {
342   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
343          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
344          "Cannot truncate non-integer value!");
345 }
346
347 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
348                                        const SCEV *op, Type *ty)
349   : SCEVCastExpr(ID, scZeroExtend, op, ty) {
350   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
351          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
352          "Cannot zero extend non-integer value!");
353 }
354
355 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
356                                        const SCEV *op, Type *ty)
357   : SCEVCastExpr(ID, scSignExtend, op, ty) {
358   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
359          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
360          "Cannot sign extend non-integer value!");
361 }
362
363 void SCEVUnknown::deleted() {
364   // Clear this SCEVUnknown from various maps.
365   SE->forgetMemoizedResults(this);
366
367   // Remove this SCEVUnknown from the uniquing map.
368   SE->UniqueSCEVs.RemoveNode(this);
369
370   // Release the value.
371   setValPtr(0);
372 }
373
374 void SCEVUnknown::allUsesReplacedWith(Value *New) {
375   // Clear this SCEVUnknown from various maps.
376   SE->forgetMemoizedResults(this);
377
378   // Remove this SCEVUnknown from the uniquing map.
379   SE->UniqueSCEVs.RemoveNode(this);
380
381   // Update this SCEVUnknown to point to the new value. This is needed
382   // because there may still be outstanding SCEVs which still point to
383   // this SCEVUnknown.
384   setValPtr(New);
385 }
386
387 bool SCEVUnknown::isSizeOf(Type *&AllocTy) const {
388   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
389     if (VCE->getOpcode() == Instruction::PtrToInt)
390       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
391         if (CE->getOpcode() == Instruction::GetElementPtr &&
392             CE->getOperand(0)->isNullValue() &&
393             CE->getNumOperands() == 2)
394           if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
395             if (CI->isOne()) {
396               AllocTy = cast<PointerType>(CE->getOperand(0)->getType())
397                                  ->getElementType();
398               return true;
399             }
400
401   return false;
402 }
403
404 bool SCEVUnknown::isAlignOf(Type *&AllocTy) const {
405   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
406     if (VCE->getOpcode() == Instruction::PtrToInt)
407       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
408         if (CE->getOpcode() == Instruction::GetElementPtr &&
409             CE->getOperand(0)->isNullValue()) {
410           Type *Ty =
411             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
412           if (StructType *STy = dyn_cast<StructType>(Ty))
413             if (!STy->isPacked() &&
414                 CE->getNumOperands() == 3 &&
415                 CE->getOperand(1)->isNullValue()) {
416               if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
417                 if (CI->isOne() &&
418                     STy->getNumElements() == 2 &&
419                     STy->getElementType(0)->isIntegerTy(1)) {
420                   AllocTy = STy->getElementType(1);
421                   return true;
422                 }
423             }
424         }
425
426   return false;
427 }
428
429 bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const {
430   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
431     if (VCE->getOpcode() == Instruction::PtrToInt)
432       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
433         if (CE->getOpcode() == Instruction::GetElementPtr &&
434             CE->getNumOperands() == 3 &&
435             CE->getOperand(0)->isNullValue() &&
436             CE->getOperand(1)->isNullValue()) {
437           Type *Ty =
438             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
439           // Ignore vector types here so that ScalarEvolutionExpander doesn't
440           // emit getelementptrs that index into vectors.
441           if (Ty->isStructTy() || Ty->isArrayTy()) {
442             CTy = Ty;
443             FieldNo = CE->getOperand(2);
444             return true;
445           }
446         }
447
448   return false;
449 }
450
451 //===----------------------------------------------------------------------===//
452 //                               SCEV Utilities
453 //===----------------------------------------------------------------------===//
454
455 namespace {
456   /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
457   /// than the complexity of the RHS.  This comparator is used to canonicalize
458   /// expressions.
459   class SCEVComplexityCompare {
460     const LoopInfo *const LI;
461   public:
462     explicit SCEVComplexityCompare(const LoopInfo *li) : LI(li) {}
463
464     // Return true or false if LHS is less than, or at least RHS, respectively.
465     bool operator()(const SCEV *LHS, const SCEV *RHS) const {
466       return compare(LHS, RHS) < 0;
467     }
468
469     // Return negative, zero, or positive, if LHS is less than, equal to, or
470     // greater than RHS, respectively. A three-way result allows recursive
471     // comparisons to be more efficient.
472     int compare(const SCEV *LHS, const SCEV *RHS) const {
473       // Fast-path: SCEVs are uniqued so we can do a quick equality check.
474       if (LHS == RHS)
475         return 0;
476
477       // Primarily, sort the SCEVs by their getSCEVType().
478       unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
479       if (LType != RType)
480         return (int)LType - (int)RType;
481
482       // Aside from the getSCEVType() ordering, the particular ordering
483       // isn't very important except that it's beneficial to be consistent,
484       // so that (a + b) and (b + a) don't end up as different expressions.
485       switch (LType) {
486       case scUnknown: {
487         const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
488         const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
489
490         // Sort SCEVUnknown values with some loose heuristics. TODO: This is
491         // not as complete as it could be.
492         const Value *LV = LU->getValue(), *RV = RU->getValue();
493
494         // Order pointer values after integer values. This helps SCEVExpander
495         // form GEPs.
496         bool LIsPointer = LV->getType()->isPointerTy(),
497              RIsPointer = RV->getType()->isPointerTy();
498         if (LIsPointer != RIsPointer)
499           return (int)LIsPointer - (int)RIsPointer;
500
501         // Compare getValueID values.
502         unsigned LID = LV->getValueID(),
503                  RID = RV->getValueID();
504         if (LID != RID)
505           return (int)LID - (int)RID;
506
507         // Sort arguments by their position.
508         if (const Argument *LA = dyn_cast<Argument>(LV)) {
509           const Argument *RA = cast<Argument>(RV);
510           unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
511           return (int)LArgNo - (int)RArgNo;
512         }
513
514         // For instructions, compare their loop depth, and their operand
515         // count.  This is pretty loose.
516         if (const Instruction *LInst = dyn_cast<Instruction>(LV)) {
517           const Instruction *RInst = cast<Instruction>(RV);
518
519           // Compare loop depths.
520           const BasicBlock *LParent = LInst->getParent(),
521                            *RParent = RInst->getParent();
522           if (LParent != RParent) {
523             unsigned LDepth = LI->getLoopDepth(LParent),
524                      RDepth = LI->getLoopDepth(RParent);
525             if (LDepth != RDepth)
526               return (int)LDepth - (int)RDepth;
527           }
528
529           // Compare the number of operands.
530           unsigned LNumOps = LInst->getNumOperands(),
531                    RNumOps = RInst->getNumOperands();
532           return (int)LNumOps - (int)RNumOps;
533         }
534
535         return 0;
536       }
537
538       case scConstant: {
539         const SCEVConstant *LC = cast<SCEVConstant>(LHS);
540         const SCEVConstant *RC = cast<SCEVConstant>(RHS);
541
542         // Compare constant values.
543         const APInt &LA = LC->getValue()->getValue();
544         const APInt &RA = RC->getValue()->getValue();
545         unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
546         if (LBitWidth != RBitWidth)
547           return (int)LBitWidth - (int)RBitWidth;
548         return LA.ult(RA) ? -1 : 1;
549       }
550
551       case scAddRecExpr: {
552         const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
553         const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
554
555         // Compare addrec loop depths.
556         const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
557         if (LLoop != RLoop) {
558           unsigned LDepth = LLoop->getLoopDepth(),
559                    RDepth = RLoop->getLoopDepth();
560           if (LDepth != RDepth)
561             return (int)LDepth - (int)RDepth;
562         }
563
564         // Addrec complexity grows with operand count.
565         unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands();
566         if (LNumOps != RNumOps)
567           return (int)LNumOps - (int)RNumOps;
568
569         // Lexicographically compare.
570         for (unsigned i = 0; i != LNumOps; ++i) {
571           long X = compare(LA->getOperand(i), RA->getOperand(i));
572           if (X != 0)
573             return X;
574         }
575
576         return 0;
577       }
578
579       case scAddExpr:
580       case scMulExpr:
581       case scSMaxExpr:
582       case scUMaxExpr: {
583         const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS);
584         const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
585
586         // Lexicographically compare n-ary expressions.
587         unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands();
588         for (unsigned i = 0; i != LNumOps; ++i) {
589           if (i >= RNumOps)
590             return 1;
591           long X = compare(LC->getOperand(i), RC->getOperand(i));
592           if (X != 0)
593             return X;
594         }
595         return (int)LNumOps - (int)RNumOps;
596       }
597
598       case scUDivExpr: {
599         const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS);
600         const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
601
602         // Lexicographically compare udiv expressions.
603         long X = compare(LC->getLHS(), RC->getLHS());
604         if (X != 0)
605           return X;
606         return compare(LC->getRHS(), RC->getRHS());
607       }
608
609       case scTruncate:
610       case scZeroExtend:
611       case scSignExtend: {
612         const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS);
613         const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
614
615         // Compare cast expressions by operand.
616         return compare(LC->getOperand(), RC->getOperand());
617       }
618
619       default:
620         llvm_unreachable("Unknown SCEV kind!");
621       }
622     }
623   };
624 }
625
626 /// GroupByComplexity - Given a list of SCEV objects, order them by their
627 /// complexity, and group objects of the same complexity together by value.
628 /// When this routine is finished, we know that any duplicates in the vector are
629 /// consecutive and that complexity is monotonically increasing.
630 ///
631 /// Note that we go take special precautions to ensure that we get deterministic
632 /// results from this routine.  In other words, we don't want the results of
633 /// this to depend on where the addresses of various SCEV objects happened to
634 /// land in memory.
635 ///
636 static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
637                               LoopInfo *LI) {
638   if (Ops.size() < 2) return;  // Noop
639   if (Ops.size() == 2) {
640     // This is the common case, which also happens to be trivially simple.
641     // Special case it.
642     const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
643     if (SCEVComplexityCompare(LI)(RHS, LHS))
644       std::swap(LHS, RHS);
645     return;
646   }
647
648   // Do the rough sort by complexity.
649   std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
650
651   // Now that we are sorted by complexity, group elements of the same
652   // complexity.  Note that this is, at worst, N^2, but the vector is likely to
653   // be extremely short in practice.  Note that we take this approach because we
654   // do not want to depend on the addresses of the objects we are grouping.
655   for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
656     const SCEV *S = Ops[i];
657     unsigned Complexity = S->getSCEVType();
658
659     // If there are any objects of the same complexity and same value as this
660     // one, group them.
661     for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
662       if (Ops[j] == S) { // Found a duplicate.
663         // Move it to immediately after i'th element.
664         std::swap(Ops[i+1], Ops[j]);
665         ++i;   // no need to rescan it.
666         if (i == e-2) return;  // Done!
667       }
668     }
669   }
670 }
671
672
673
674 //===----------------------------------------------------------------------===//
675 //                      Simple SCEV method implementations
676 //===----------------------------------------------------------------------===//
677
678 /// BinomialCoefficient - Compute BC(It, K).  The result has width W.
679 /// Assume, K > 0.
680 static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
681                                        ScalarEvolution &SE,
682                                        Type *ResultTy) {
683   // Handle the simplest case efficiently.
684   if (K == 1)
685     return SE.getTruncateOrZeroExtend(It, ResultTy);
686
687   // We are using the following formula for BC(It, K):
688   //
689   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
690   //
691   // Suppose, W is the bitwidth of the return value.  We must be prepared for
692   // overflow.  Hence, we must assure that the result of our computation is
693   // equal to the accurate one modulo 2^W.  Unfortunately, division isn't
694   // safe in modular arithmetic.
695   //
696   // However, this code doesn't use exactly that formula; the formula it uses
697   // is something like the following, where T is the number of factors of 2 in
698   // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
699   // exponentiation:
700   //
701   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
702   //
703   // This formula is trivially equivalent to the previous formula.  However,
704   // this formula can be implemented much more efficiently.  The trick is that
705   // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
706   // arithmetic.  To do exact division in modular arithmetic, all we have
707   // to do is multiply by the inverse.  Therefore, this step can be done at
708   // width W.
709   //
710   // The next issue is how to safely do the division by 2^T.  The way this
711   // is done is by doing the multiplication step at a width of at least W + T
712   // bits.  This way, the bottom W+T bits of the product are accurate. Then,
713   // when we perform the division by 2^T (which is equivalent to a right shift
714   // by T), the bottom W bits are accurate.  Extra bits are okay; they'll get
715   // truncated out after the division by 2^T.
716   //
717   // In comparison to just directly using the first formula, this technique
718   // is much more efficient; using the first formula requires W * K bits,
719   // but this formula less than W + K bits. Also, the first formula requires
720   // a division step, whereas this formula only requires multiplies and shifts.
721   //
722   // It doesn't matter whether the subtraction step is done in the calculation
723   // width or the input iteration count's width; if the subtraction overflows,
724   // the result must be zero anyway.  We prefer here to do it in the width of
725   // the induction variable because it helps a lot for certain cases; CodeGen
726   // isn't smart enough to ignore the overflow, which leads to much less
727   // efficient code if the width of the subtraction is wider than the native
728   // register width.
729   //
730   // (It's possible to not widen at all by pulling out factors of 2 before
731   // the multiplication; for example, K=2 can be calculated as
732   // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
733   // extra arithmetic, so it's not an obvious win, and it gets
734   // much more complicated for K > 3.)
735
736   // Protection from insane SCEVs; this bound is conservative,
737   // but it probably doesn't matter.
738   if (K > 1000)
739     return SE.getCouldNotCompute();
740
741   unsigned W = SE.getTypeSizeInBits(ResultTy);
742
743   // Calculate K! / 2^T and T; we divide out the factors of two before
744   // multiplying for calculating K! / 2^T to avoid overflow.
745   // Other overflow doesn't matter because we only care about the bottom
746   // W bits of the result.
747   APInt OddFactorial(W, 1);
748   unsigned T = 1;
749   for (unsigned i = 3; i <= K; ++i) {
750     APInt Mult(W, i);
751     unsigned TwoFactors = Mult.countTrailingZeros();
752     T += TwoFactors;
753     Mult = Mult.lshr(TwoFactors);
754     OddFactorial *= Mult;
755   }
756
757   // We need at least W + T bits for the multiplication step
758   unsigned CalculationBits = W + T;
759
760   // Calculate 2^T, at width T+W.
761   APInt DivFactor = APInt(CalculationBits, 1).shl(T);
762
763   // Calculate the multiplicative inverse of K! / 2^T;
764   // this multiplication factor will perform the exact division by
765   // K! / 2^T.
766   APInt Mod = APInt::getSignedMinValue(W+1);
767   APInt MultiplyFactor = OddFactorial.zext(W+1);
768   MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
769   MultiplyFactor = MultiplyFactor.trunc(W);
770
771   // Calculate the product, at width T+W
772   IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
773                                                       CalculationBits);
774   const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
775   for (unsigned i = 1; i != K; ++i) {
776     const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
777     Dividend = SE.getMulExpr(Dividend,
778                              SE.getTruncateOrZeroExtend(S, CalculationTy));
779   }
780
781   // Divide by 2^T
782   const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
783
784   // Truncate the result, and divide by K! / 2^T.
785
786   return SE.getMulExpr(SE.getConstant(MultiplyFactor),
787                        SE.getTruncateOrZeroExtend(DivResult, ResultTy));
788 }
789
790 /// evaluateAtIteration - Return the value of this chain of recurrences at
791 /// the specified iteration number.  We can evaluate this recurrence by
792 /// multiplying each element in the chain by the binomial coefficient
793 /// corresponding to it.  In other words, we can evaluate {A,+,B,+,C,+,D} as:
794 ///
795 ///   A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
796 ///
797 /// where BC(It, k) stands for binomial coefficient.
798 ///
799 const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
800                                                 ScalarEvolution &SE) const {
801   const SCEV *Result = getStart();
802   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
803     // The computation is correct in the face of overflow provided that the
804     // multiplication is performed _after_ the evaluation of the binomial
805     // coefficient.
806     const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
807     if (isa<SCEVCouldNotCompute>(Coeff))
808       return Coeff;
809
810     Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
811   }
812   return Result;
813 }
814
815 //===----------------------------------------------------------------------===//
816 //                    SCEV Expression folder implementations
817 //===----------------------------------------------------------------------===//
818
819 const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
820                                              Type *Ty) {
821   assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
822          "This is not a truncating conversion!");
823   assert(isSCEVable(Ty) &&
824          "This is not a conversion to a SCEVable type!");
825   Ty = getEffectiveSCEVType(Ty);
826
827   FoldingSetNodeID ID;
828   ID.AddInteger(scTruncate);
829   ID.AddPointer(Op);
830   ID.AddPointer(Ty);
831   void *IP = 0;
832   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
833
834   // Fold if the operand is constant.
835   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
836     return getConstant(
837       cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
838
839   // trunc(trunc(x)) --> trunc(x)
840   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
841     return getTruncateExpr(ST->getOperand(), Ty);
842
843   // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
844   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
845     return getTruncateOrSignExtend(SS->getOperand(), Ty);
846
847   // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
848   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
849     return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
850
851   // trunc(x1+x2+...+xN) --> trunc(x1)+trunc(x2)+...+trunc(xN) if we can
852   // eliminate all the truncates.
853   if (const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) {
854     SmallVector<const SCEV *, 4> Operands;
855     bool hasTrunc = false;
856     for (unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) {
857       const SCEV *S = getTruncateExpr(SA->getOperand(i), Ty);
858       hasTrunc = isa<SCEVTruncateExpr>(S);
859       Operands.push_back(S);
860     }
861     if (!hasTrunc)
862       return getAddExpr(Operands);
863     UniqueSCEVs.FindNodeOrInsertPos(ID, IP);  // Mutates IP, returns NULL.
864   }
865
866   // trunc(x1*x2*...*xN) --> trunc(x1)*trunc(x2)*...*trunc(xN) if we can
867   // eliminate all the truncates.
868   if (const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) {
869     SmallVector<const SCEV *, 4> Operands;
870     bool hasTrunc = false;
871     for (unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) {
872       const SCEV *S = getTruncateExpr(SM->getOperand(i), Ty);
873       hasTrunc = isa<SCEVTruncateExpr>(S);
874       Operands.push_back(S);
875     }
876     if (!hasTrunc)
877       return getMulExpr(Operands);
878     UniqueSCEVs.FindNodeOrInsertPos(ID, IP);  // Mutates IP, returns NULL.
879   }
880
881   // If the input value is a chrec scev, truncate the chrec's operands.
882   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
883     SmallVector<const SCEV *, 4> Operands;
884     for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
885       Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
886     return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
887   }
888
889   // The cast wasn't folded; create an explicit cast node. We can reuse
890   // the existing insert position since if we get here, we won't have
891   // made any changes which would invalidate it.
892   SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
893                                                  Op, Ty);
894   UniqueSCEVs.InsertNode(S, IP);
895   return S;
896 }
897
898 const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op,
899                                                Type *Ty) {
900   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
901          "This is not an extending conversion!");
902   assert(isSCEVable(Ty) &&
903          "This is not a conversion to a SCEVable type!");
904   Ty = getEffectiveSCEVType(Ty);
905
906   // Fold if the operand is constant.
907   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
908     return getConstant(
909       cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty)));
910
911   // zext(zext(x)) --> zext(x)
912   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
913     return getZeroExtendExpr(SZ->getOperand(), Ty);
914
915   // Before doing any expensive analysis, check to see if we've already
916   // computed a SCEV for this Op and Ty.
917   FoldingSetNodeID ID;
918   ID.AddInteger(scZeroExtend);
919   ID.AddPointer(Op);
920   ID.AddPointer(Ty);
921   void *IP = 0;
922   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
923
924   // zext(trunc(x)) --> zext(x) or x or trunc(x)
925   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
926     // It's possible the bits taken off by the truncate were all zero bits. If
927     // so, we should be able to simplify this further.
928     const SCEV *X = ST->getOperand();
929     ConstantRange CR = getUnsignedRange(X);
930     unsigned TruncBits = getTypeSizeInBits(ST->getType());
931     unsigned NewBits = getTypeSizeInBits(Ty);
932     if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
933             CR.zextOrTrunc(NewBits)))
934       return getTruncateOrZeroExtend(X, Ty);
935   }
936
937   // If the input value is a chrec scev, and we can prove that the value
938   // did not overflow the old, smaller, value, we can zero extend all of the
939   // operands (often constants).  This allows analysis of something like
940   // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
941   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
942     if (AR->isAffine()) {
943       const SCEV *Start = AR->getStart();
944       const SCEV *Step = AR->getStepRecurrence(*this);
945       unsigned BitWidth = getTypeSizeInBits(AR->getType());
946       const Loop *L = AR->getLoop();
947
948       // If we have special knowledge that this addrec won't overflow,
949       // we don't need to do any further analysis.
950       if (AR->getNoWrapFlags(SCEV::FlagNUW))
951         return getAddRecExpr(getZeroExtendExpr(Start, Ty),
952                              getZeroExtendExpr(Step, Ty),
953                              L, AR->getNoWrapFlags());
954
955       // Check whether the backedge-taken count is SCEVCouldNotCompute.
956       // Note that this serves two purposes: It filters out loops that are
957       // simply not analyzable, and it covers the case where this code is
958       // being called from within backedge-taken count analysis, such that
959       // attempting to ask for the backedge-taken count would likely result
960       // in infinite recursion. In the later case, the analysis code will
961       // cope with a conservative value, and it will take care to purge
962       // that value once it has finished.
963       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
964       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
965         // Manually compute the final value for AR, checking for
966         // overflow.
967
968         // Check whether the backedge-taken count can be losslessly casted to
969         // the addrec's type. The count is always unsigned.
970         const SCEV *CastedMaxBECount =
971           getTruncateOrZeroExtend(MaxBECount, Start->getType());
972         const SCEV *RecastedMaxBECount =
973           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
974         if (MaxBECount == RecastedMaxBECount) {
975           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
976           // Check whether Start+Step*MaxBECount has no unsigned overflow.
977           const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step);
978           const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul), WideTy);
979           const SCEV *WideStart = getZeroExtendExpr(Start, WideTy);
980           const SCEV *WideMaxBECount =
981             getZeroExtendExpr(CastedMaxBECount, WideTy);
982           const SCEV *OperandExtendedAdd =
983             getAddExpr(WideStart,
984                        getMulExpr(WideMaxBECount,
985                                   getZeroExtendExpr(Step, WideTy)));
986           if (ZAdd == OperandExtendedAdd) {
987             // Cache knowledge of AR NUW, which is propagated to this AddRec.
988             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
989             // Return the expression with the addrec on the outside.
990             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
991                                  getZeroExtendExpr(Step, Ty),
992                                  L, AR->getNoWrapFlags());
993           }
994           // Similar to above, only this time treat the step value as signed.
995           // This covers loops that count down.
996           OperandExtendedAdd =
997             getAddExpr(WideStart,
998                        getMulExpr(WideMaxBECount,
999                                   getSignExtendExpr(Step, WideTy)));
1000           if (ZAdd == OperandExtendedAdd) {
1001             // Cache knowledge of AR NW, which is propagated to this AddRec.
1002             // Negative step causes unsigned wrap, but it still can't self-wrap.
1003             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1004             // Return the expression with the addrec on the outside.
1005             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
1006                                  getSignExtendExpr(Step, Ty),
1007                                  L, AR->getNoWrapFlags());
1008           }
1009         }
1010
1011         // If the backedge is guarded by a comparison with the pre-inc value
1012         // the addrec is safe. Also, if the entry is guarded by a comparison
1013         // with the start value and the backedge is guarded by a comparison
1014         // with the post-inc value, the addrec is safe.
1015         if (isKnownPositive(Step)) {
1016           const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
1017                                       getUnsignedRange(Step).getUnsignedMax());
1018           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
1019               (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
1020                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
1021                                            AR->getPostIncExpr(*this), N))) {
1022             // Cache knowledge of AR NUW, which is propagated to this AddRec.
1023             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1024             // Return the expression with the addrec on the outside.
1025             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
1026                                  getZeroExtendExpr(Step, Ty),
1027                                  L, AR->getNoWrapFlags());
1028           }
1029         } else if (isKnownNegative(Step)) {
1030           const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
1031                                       getSignedRange(Step).getSignedMin());
1032           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
1033               (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) &&
1034                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
1035                                            AR->getPostIncExpr(*this), N))) {
1036             // Cache knowledge of AR NW, which is propagated to this AddRec.
1037             // Negative step causes unsigned wrap, but it still can't self-wrap.
1038             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1039             // Return the expression with the addrec on the outside.
1040             return getAddRecExpr(getZeroExtendExpr(Start, Ty),
1041                                  getSignExtendExpr(Step, Ty),
1042                                  L, AR->getNoWrapFlags());
1043           }
1044         }
1045       }
1046     }
1047
1048   // The cast wasn't folded; create an explicit cast node.
1049   // Recompute the insert position, as it may have been invalidated.
1050   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1051   SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1052                                                    Op, Ty);
1053   UniqueSCEVs.InsertNode(S, IP);
1054   return S;
1055 }
1056
1057 // Get the limit of a recurrence such that incrementing by Step cannot cause
1058 // signed overflow as long as the value of the recurrence within the loop does
1059 // not exceed this limit before incrementing.
1060 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1061                                            ICmpInst::Predicate *Pred,
1062                                            ScalarEvolution *SE) {
1063   unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1064   if (SE->isKnownPositive(Step)) {
1065     *Pred = ICmpInst::ICMP_SLT;
1066     return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
1067                            SE->getSignedRange(Step).getSignedMax());
1068   }
1069   if (SE->isKnownNegative(Step)) {
1070     *Pred = ICmpInst::ICMP_SGT;
1071     return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
1072                        SE->getSignedRange(Step).getSignedMin());
1073   }
1074   return 0;
1075 }
1076
1077 // The recurrence AR has been shown to have no signed wrap. Typically, if we can
1078 // prove NSW for AR, then we can just as easily prove NSW for its preincrement
1079 // or postincrement sibling. This allows normalizing a sign extended AddRec as
1080 // such: {sext(Step + Start),+,Step} => {(Step + sext(Start),+,Step} As a
1081 // result, the expression "Step + sext(PreIncAR)" is congruent with
1082 // "sext(PostIncAR)"
1083 static const SCEV *getPreStartForSignExtend(const SCEVAddRecExpr *AR,
1084                                             Type *Ty,
1085                                             ScalarEvolution *SE) {
1086   const Loop *L = AR->getLoop();
1087   const SCEV *Start = AR->getStart();
1088   const SCEV *Step = AR->getStepRecurrence(*SE);
1089
1090   // Check for a simple looking step prior to loop entry.
1091   const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1092   if (!SA)
1093     return 0;
1094
1095   // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1096   // subtraction is expensive. For this purpose, perform a quick and dirty
1097   // difference, by checking for Step in the operand list.
1098   SmallVector<const SCEV *, 4> DiffOps;
1099   for (SCEVAddExpr::op_iterator I = SA->op_begin(), E = SA->op_end();
1100        I != E; ++I) {
1101     if (*I != Step)
1102       DiffOps.push_back(*I);
1103   }
1104   if (DiffOps.size() == SA->getNumOperands())
1105     return 0;
1106
1107   // This is a postinc AR. Check for overflow on the preinc recurrence using the
1108   // same three conditions that getSignExtendedExpr checks.
1109
1110   // 1. NSW flags on the step increment.
1111   const SCEV *PreStart = SE->getAddExpr(DiffOps, SA->getNoWrapFlags());
1112   const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1113     SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
1114
1115   if (PreAR && PreAR->getNoWrapFlags(SCEV::FlagNSW))
1116     return PreStart;
1117
1118   // 2. Direct overflow check on the step operation's expression.
1119   unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
1120   Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
1121   const SCEV *OperandExtendedStart =
1122     SE->getAddExpr(SE->getSignExtendExpr(PreStart, WideTy),
1123                    SE->getSignExtendExpr(Step, WideTy));
1124   if (SE->getSignExtendExpr(Start, WideTy) == OperandExtendedStart) {
1125     // Cache knowledge of PreAR NSW.
1126     if (PreAR)
1127       const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(SCEV::FlagNSW);
1128     // FIXME: this optimization needs a unit test
1129     DEBUG(dbgs() << "SCEV: untested prestart overflow check\n");
1130     return PreStart;
1131   }
1132
1133   // 3. Loop precondition.
1134   ICmpInst::Predicate Pred;
1135   const SCEV *OverflowLimit = getOverflowLimitForStep(Step, &Pred, SE);
1136
1137   if (OverflowLimit &&
1138       SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit)) {
1139     return PreStart;
1140   }
1141   return 0;
1142 }
1143
1144 // Get the normalized sign-extended expression for this AddRec's Start.
1145 static const SCEV *getSignExtendAddRecStart(const SCEVAddRecExpr *AR,
1146                                             Type *Ty,
1147                                             ScalarEvolution *SE) {
1148   const SCEV *PreStart = getPreStartForSignExtend(AR, Ty, SE);
1149   if (!PreStart)
1150     return SE->getSignExtendExpr(AR->getStart(), Ty);
1151
1152   return SE->getAddExpr(SE->getSignExtendExpr(AR->getStepRecurrence(*SE), Ty),
1153                         SE->getSignExtendExpr(PreStart, Ty));
1154 }
1155
1156 const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op,
1157                                                Type *Ty) {
1158   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1159          "This is not an extending conversion!");
1160   assert(isSCEVable(Ty) &&
1161          "This is not a conversion to a SCEVable type!");
1162   Ty = getEffectiveSCEVType(Ty);
1163
1164   // Fold if the operand is constant.
1165   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1166     return getConstant(
1167       cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty)));
1168
1169   // sext(sext(x)) --> sext(x)
1170   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1171     return getSignExtendExpr(SS->getOperand(), Ty);
1172
1173   // sext(zext(x)) --> zext(x)
1174   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1175     return getZeroExtendExpr(SZ->getOperand(), Ty);
1176
1177   // Before doing any expensive analysis, check to see if we've already
1178   // computed a SCEV for this Op and Ty.
1179   FoldingSetNodeID ID;
1180   ID.AddInteger(scSignExtend);
1181   ID.AddPointer(Op);
1182   ID.AddPointer(Ty);
1183   void *IP = 0;
1184   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1185
1186   // If the input value is provably positive, build a zext instead.
1187   if (isKnownNonNegative(Op))
1188     return getZeroExtendExpr(Op, Ty);
1189
1190   // sext(trunc(x)) --> sext(x) or x or trunc(x)
1191   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1192     // It's possible the bits taken off by the truncate were all sign bits. If
1193     // so, we should be able to simplify this further.
1194     const SCEV *X = ST->getOperand();
1195     ConstantRange CR = getSignedRange(X);
1196     unsigned TruncBits = getTypeSizeInBits(ST->getType());
1197     unsigned NewBits = getTypeSizeInBits(Ty);
1198     if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1199             CR.sextOrTrunc(NewBits)))
1200       return getTruncateOrSignExtend(X, Ty);
1201   }
1202
1203   // If the input value is a chrec scev, and we can prove that the value
1204   // did not overflow the old, smaller, value, we can sign extend all of the
1205   // operands (often constants).  This allows analysis of something like
1206   // this:  for (signed char X = 0; X < 100; ++X) { int Y = X; }
1207   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1208     if (AR->isAffine()) {
1209       const SCEV *Start = AR->getStart();
1210       const SCEV *Step = AR->getStepRecurrence(*this);
1211       unsigned BitWidth = getTypeSizeInBits(AR->getType());
1212       const Loop *L = AR->getLoop();
1213
1214       // If we have special knowledge that this addrec won't overflow,
1215       // we don't need to do any further analysis.
1216       if (AR->getNoWrapFlags(SCEV::FlagNSW))
1217         return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this),
1218                              getSignExtendExpr(Step, Ty),
1219                              L, SCEV::FlagNSW);
1220
1221       // Check whether the backedge-taken count is SCEVCouldNotCompute.
1222       // Note that this serves two purposes: It filters out loops that are
1223       // simply not analyzable, and it covers the case where this code is
1224       // being called from within backedge-taken count analysis, such that
1225       // attempting to ask for the backedge-taken count would likely result
1226       // in infinite recursion. In the later case, the analysis code will
1227       // cope with a conservative value, and it will take care to purge
1228       // that value once it has finished.
1229       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
1230       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1231         // Manually compute the final value for AR, checking for
1232         // overflow.
1233
1234         // Check whether the backedge-taken count can be losslessly casted to
1235         // the addrec's type. The count is always unsigned.
1236         const SCEV *CastedMaxBECount =
1237           getTruncateOrZeroExtend(MaxBECount, Start->getType());
1238         const SCEV *RecastedMaxBECount =
1239           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
1240         if (MaxBECount == RecastedMaxBECount) {
1241           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1242           // Check whether Start+Step*MaxBECount has no signed overflow.
1243           const SCEV *SMul = getMulExpr(CastedMaxBECount, Step);
1244           const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul), WideTy);
1245           const SCEV *WideStart = getSignExtendExpr(Start, WideTy);
1246           const SCEV *WideMaxBECount =
1247             getZeroExtendExpr(CastedMaxBECount, WideTy);
1248           const SCEV *OperandExtendedAdd =
1249             getAddExpr(WideStart,
1250                        getMulExpr(WideMaxBECount,
1251                                   getSignExtendExpr(Step, WideTy)));
1252           if (SAdd == OperandExtendedAdd) {
1253             // Cache knowledge of AR NSW, which is propagated to this AddRec.
1254             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1255             // Return the expression with the addrec on the outside.
1256             return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this),
1257                                  getSignExtendExpr(Step, Ty),
1258                                  L, AR->getNoWrapFlags());
1259           }
1260           // Similar to above, only this time treat the step value as unsigned.
1261           // This covers loops that count up with an unsigned step.
1262           OperandExtendedAdd =
1263             getAddExpr(WideStart,
1264                        getMulExpr(WideMaxBECount,
1265                                   getZeroExtendExpr(Step, WideTy)));
1266           if (SAdd == OperandExtendedAdd) {
1267             // Cache knowledge of AR NSW, which is propagated to this AddRec.
1268             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1269             // Return the expression with the addrec on the outside.
1270             return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this),
1271                                  getZeroExtendExpr(Step, Ty),
1272                                  L, AR->getNoWrapFlags());
1273           }
1274         }
1275
1276         // If the backedge is guarded by a comparison with the pre-inc value
1277         // the addrec is safe. Also, if the entry is guarded by a comparison
1278         // with the start value and the backedge is guarded by a comparison
1279         // with the post-inc value, the addrec is safe.
1280         ICmpInst::Predicate Pred;
1281         const SCEV *OverflowLimit = getOverflowLimitForStep(Step, &Pred, this);
1282         if (OverflowLimit &&
1283             (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
1284              (isLoopEntryGuardedByCond(L, Pred, Start, OverflowLimit) &&
1285               isLoopBackedgeGuardedByCond(L, Pred, AR->getPostIncExpr(*this),
1286                                           OverflowLimit)))) {
1287           // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec.
1288           const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1289           return getAddRecExpr(getSignExtendAddRecStart(AR, Ty, this),
1290                                getSignExtendExpr(Step, Ty),
1291                                L, AR->getNoWrapFlags());
1292         }
1293       }
1294     }
1295
1296   // The cast wasn't folded; create an explicit cast node.
1297   // Recompute the insert position, as it may have been invalidated.
1298   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1299   SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1300                                                    Op, Ty);
1301   UniqueSCEVs.InsertNode(S, IP);
1302   return S;
1303 }
1304
1305 /// getAnyExtendExpr - Return a SCEV for the given operand extended with
1306 /// unspecified bits out to the given type.
1307 ///
1308 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
1309                                               Type *Ty) {
1310   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1311          "This is not an extending conversion!");
1312   assert(isSCEVable(Ty) &&
1313          "This is not a conversion to a SCEVable type!");
1314   Ty = getEffectiveSCEVType(Ty);
1315
1316   // Sign-extend negative constants.
1317   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1318     if (SC->getValue()->getValue().isNegative())
1319       return getSignExtendExpr(Op, Ty);
1320
1321   // Peel off a truncate cast.
1322   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
1323     const SCEV *NewOp = T->getOperand();
1324     if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
1325       return getAnyExtendExpr(NewOp, Ty);
1326     return getTruncateOrNoop(NewOp, Ty);
1327   }
1328
1329   // Next try a zext cast. If the cast is folded, use it.
1330   const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
1331   if (!isa<SCEVZeroExtendExpr>(ZExt))
1332     return ZExt;
1333
1334   // Next try a sext cast. If the cast is folded, use it.
1335   const SCEV *SExt = getSignExtendExpr(Op, Ty);
1336   if (!isa<SCEVSignExtendExpr>(SExt))
1337     return SExt;
1338
1339   // Force the cast to be folded into the operands of an addrec.
1340   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
1341     SmallVector<const SCEV *, 4> Ops;
1342     for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end();
1343          I != E; ++I)
1344       Ops.push_back(getAnyExtendExpr(*I, Ty));
1345     return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
1346   }
1347
1348   // If the expression is obviously signed, use the sext cast value.
1349   if (isa<SCEVSMaxExpr>(Op))
1350     return SExt;
1351
1352   // Absent any other information, use the zext cast value.
1353   return ZExt;
1354 }
1355
1356 /// CollectAddOperandsWithScales - Process the given Ops list, which is
1357 /// a list of operands to be added under the given scale, update the given
1358 /// map. This is a helper function for getAddRecExpr. As an example of
1359 /// what it does, given a sequence of operands that would form an add
1360 /// expression like this:
1361 ///
1362 ///    m + n + 13 + (A * (o + p + (B * q + m + 29))) + r + (-1 * r)
1363 ///
1364 /// where A and B are constants, update the map with these values:
1365 ///
1366 ///    (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1367 ///
1368 /// and add 13 + A*B*29 to AccumulatedConstant.
1369 /// This will allow getAddRecExpr to produce this:
1370 ///
1371 ///    13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1372 ///
1373 /// This form often exposes folding opportunities that are hidden in
1374 /// the original operand list.
1375 ///
1376 /// Return true iff it appears that any interesting folding opportunities
1377 /// may be exposed. This helps getAddRecExpr short-circuit extra work in
1378 /// the common case where no interesting opportunities are present, and
1379 /// is also used as a check to avoid infinite recursion.
1380 ///
1381 static bool
1382 CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
1383                              SmallVector<const SCEV *, 8> &NewOps,
1384                              APInt &AccumulatedConstant,
1385                              const SCEV *const *Ops, size_t NumOperands,
1386                              const APInt &Scale,
1387                              ScalarEvolution &SE) {
1388   bool Interesting = false;
1389
1390   // Iterate over the add operands. They are sorted, with constants first.
1391   unsigned i = 0;
1392   while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1393     ++i;
1394     // Pull a buried constant out to the outside.
1395     if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
1396       Interesting = true;
1397     AccumulatedConstant += Scale * C->getValue()->getValue();
1398   }
1399
1400   // Next comes everything else. We're especially interested in multiplies
1401   // here, but they're in the middle, so just visit the rest with one loop.
1402   for (; i != NumOperands; ++i) {
1403     const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1404     if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1405       APInt NewScale =
1406         Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1407       if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1408         // A multiplication of a constant with another add; recurse.
1409         const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
1410         Interesting |=
1411           CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1412                                        Add->op_begin(), Add->getNumOperands(),
1413                                        NewScale, SE);
1414       } else {
1415         // A multiplication of a constant with some other value. Update
1416         // the map.
1417         SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1418         const SCEV *Key = SE.getMulExpr(MulOps);
1419         std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
1420           M.insert(std::make_pair(Key, NewScale));
1421         if (Pair.second) {
1422           NewOps.push_back(Pair.first->first);
1423         } else {
1424           Pair.first->second += NewScale;
1425           // The map already had an entry for this value, which may indicate
1426           // a folding opportunity.
1427           Interesting = true;
1428         }
1429       }
1430     } else {
1431       // An ordinary operand. Update the map.
1432       std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
1433         M.insert(std::make_pair(Ops[i], Scale));
1434       if (Pair.second) {
1435         NewOps.push_back(Pair.first->first);
1436       } else {
1437         Pair.first->second += Scale;
1438         // The map already had an entry for this value, which may indicate
1439         // a folding opportunity.
1440         Interesting = true;
1441       }
1442     }
1443   }
1444
1445   return Interesting;
1446 }
1447
1448 namespace {
1449   struct APIntCompare {
1450     bool operator()(const APInt &LHS, const APInt &RHS) const {
1451       return LHS.ult(RHS);
1452     }
1453   };
1454 }
1455
1456 /// getAddExpr - Get a canonical add expression, or something simpler if
1457 /// possible.
1458 const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
1459                                         SCEV::NoWrapFlags Flags) {
1460   assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
1461          "only nuw or nsw allowed");
1462   assert(!Ops.empty() && "Cannot get empty add!");
1463   if (Ops.size() == 1) return Ops[0];
1464 #ifndef NDEBUG
1465   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
1466   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1467     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
1468            "SCEVAddExpr operand types don't match!");
1469 #endif
1470
1471   // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
1472   // And vice-versa.
1473   int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
1474   SCEV::NoWrapFlags SignOrUnsignWrap = maskFlags(Flags, SignOrUnsignMask);
1475   if (SignOrUnsignWrap && (SignOrUnsignWrap != SignOrUnsignMask)) {
1476     bool All = true;
1477     for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(),
1478          E = Ops.end(); I != E; ++I)
1479       if (!isKnownNonNegative(*I)) {
1480         All = false;
1481         break;
1482       }
1483     if (All) Flags = setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
1484   }
1485
1486   // Sort by complexity, this groups all similar expression types together.
1487   GroupByComplexity(Ops, LI);
1488
1489   // If there are any constants, fold them together.
1490   unsigned Idx = 0;
1491   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1492     ++Idx;
1493     assert(Idx < Ops.size());
1494     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1495       // We found two constants, fold them together!
1496       Ops[0] = getConstant(LHSC->getValue()->getValue() +
1497                            RHSC->getValue()->getValue());
1498       if (Ops.size() == 2) return Ops[0];
1499       Ops.erase(Ops.begin()+1);  // Erase the folded element
1500       LHSC = cast<SCEVConstant>(Ops[0]);
1501     }
1502
1503     // If we are left with a constant zero being added, strip it off.
1504     if (LHSC->getValue()->isZero()) {
1505       Ops.erase(Ops.begin());
1506       --Idx;
1507     }
1508
1509     if (Ops.size() == 1) return Ops[0];
1510   }
1511
1512   // Okay, check to see if the same value occurs in the operand list more than
1513   // once.  If so, merge them together into an multiply expression.  Since we
1514   // sorted the list, these values are required to be adjacent.
1515   Type *Ty = Ops[0]->getType();
1516   bool FoundMatch = false;
1517   for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
1518     if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2
1519       // Scan ahead to count how many equal operands there are.
1520       unsigned Count = 2;
1521       while (i+Count != e && Ops[i+Count] == Ops[i])
1522         ++Count;
1523       // Merge the values into a multiply.
1524       const SCEV *Scale = getConstant(Ty, Count);
1525       const SCEV *Mul = getMulExpr(Scale, Ops[i]);
1526       if (Ops.size() == Count)
1527         return Mul;
1528       Ops[i] = Mul;
1529       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
1530       --i; e -= Count - 1;
1531       FoundMatch = true;
1532     }
1533   if (FoundMatch)
1534     return getAddExpr(Ops, Flags);
1535
1536   // Check for truncates. If all the operands are truncated from the same
1537   // type, see if factoring out the truncate would permit the result to be
1538   // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
1539   // if the contents of the resulting outer trunc fold to something simple.
1540   for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
1541     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
1542     Type *DstType = Trunc->getType();
1543     Type *SrcType = Trunc->getOperand()->getType();
1544     SmallVector<const SCEV *, 8> LargeOps;
1545     bool Ok = true;
1546     // Check all the operands to see if they can be represented in the
1547     // source type of the truncate.
1548     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
1549       if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
1550         if (T->getOperand()->getType() != SrcType) {
1551           Ok = false;
1552           break;
1553         }
1554         LargeOps.push_back(T->getOperand());
1555       } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1556         LargeOps.push_back(getAnyExtendExpr(C, SrcType));
1557       } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
1558         SmallVector<const SCEV *, 8> LargeMulOps;
1559         for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
1560           if (const SCEVTruncateExpr *T =
1561                 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
1562             if (T->getOperand()->getType() != SrcType) {
1563               Ok = false;
1564               break;
1565             }
1566             LargeMulOps.push_back(T->getOperand());
1567           } else if (const SCEVConstant *C =
1568                        dyn_cast<SCEVConstant>(M->getOperand(j))) {
1569             LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
1570           } else {
1571             Ok = false;
1572             break;
1573           }
1574         }
1575         if (Ok)
1576           LargeOps.push_back(getMulExpr(LargeMulOps));
1577       } else {
1578         Ok = false;
1579         break;
1580       }
1581     }
1582     if (Ok) {
1583       // Evaluate the expression in the larger type.
1584       const SCEV *Fold = getAddExpr(LargeOps, Flags);
1585       // If it folds to something simple, use it. Otherwise, don't.
1586       if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
1587         return getTruncateExpr(Fold, DstType);
1588     }
1589   }
1590
1591   // Skip past any other cast SCEVs.
1592   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
1593     ++Idx;
1594
1595   // If there are add operands they would be next.
1596   if (Idx < Ops.size()) {
1597     bool DeletedAdd = false;
1598     while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
1599       // If we have an add, expand the add operands onto the end of the operands
1600       // list.
1601       Ops.erase(Ops.begin()+Idx);
1602       Ops.append(Add->op_begin(), Add->op_end());
1603       DeletedAdd = true;
1604     }
1605
1606     // If we deleted at least one add, we added operands to the end of the list,
1607     // and they are not necessarily sorted.  Recurse to resort and resimplify
1608     // any operands we just acquired.
1609     if (DeletedAdd)
1610       return getAddExpr(Ops);
1611   }
1612
1613   // Skip over the add expression until we get to a multiply.
1614   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1615     ++Idx;
1616
1617   // Check to see if there are any folding opportunities present with
1618   // operands multiplied by constant values.
1619   if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
1620     uint64_t BitWidth = getTypeSizeInBits(Ty);
1621     DenseMap<const SCEV *, APInt> M;
1622     SmallVector<const SCEV *, 8> NewOps;
1623     APInt AccumulatedConstant(BitWidth, 0);
1624     if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1625                                      Ops.data(), Ops.size(),
1626                                      APInt(BitWidth, 1), *this)) {
1627       // Some interesting folding opportunity is present, so its worthwhile to
1628       // re-generate the operands list. Group the operands by constant scale,
1629       // to avoid multiplying by the same constant scale multiple times.
1630       std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
1631       for (SmallVector<const SCEV *, 8>::const_iterator I = NewOps.begin(),
1632            E = NewOps.end(); I != E; ++I)
1633         MulOpLists[M.find(*I)->second].push_back(*I);
1634       // Re-generate the operands list.
1635       Ops.clear();
1636       if (AccumulatedConstant != 0)
1637         Ops.push_back(getConstant(AccumulatedConstant));
1638       for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator
1639            I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
1640         if (I->first != 0)
1641           Ops.push_back(getMulExpr(getConstant(I->first),
1642                                    getAddExpr(I->second)));
1643       if (Ops.empty())
1644         return getConstant(Ty, 0);
1645       if (Ops.size() == 1)
1646         return Ops[0];
1647       return getAddExpr(Ops);
1648     }
1649   }
1650
1651   // If we are adding something to a multiply expression, make sure the
1652   // something is not already an operand of the multiply.  If so, merge it into
1653   // the multiply.
1654   for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
1655     const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
1656     for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
1657       const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
1658       if (isa<SCEVConstant>(MulOpSCEV))
1659         continue;
1660       for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
1661         if (MulOpSCEV == Ops[AddOp]) {
1662           // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1))
1663           const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
1664           if (Mul->getNumOperands() != 2) {
1665             // If the multiply has more than two operands, we must get the
1666             // Y*Z term.
1667             SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
1668                                                 Mul->op_begin()+MulOp);
1669             MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
1670             InnerMul = getMulExpr(MulOps);
1671           }
1672           const SCEV *One = getConstant(Ty, 1);
1673           const SCEV *AddOne = getAddExpr(One, InnerMul);
1674           const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV);
1675           if (Ops.size() == 2) return OuterMul;
1676           if (AddOp < Idx) {
1677             Ops.erase(Ops.begin()+AddOp);
1678             Ops.erase(Ops.begin()+Idx-1);
1679           } else {
1680             Ops.erase(Ops.begin()+Idx);
1681             Ops.erase(Ops.begin()+AddOp-1);
1682           }
1683           Ops.push_back(OuterMul);
1684           return getAddExpr(Ops);
1685         }
1686
1687       // Check this multiply against other multiplies being added together.
1688       for (unsigned OtherMulIdx = Idx+1;
1689            OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
1690            ++OtherMulIdx) {
1691         const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
1692         // If MulOp occurs in OtherMul, we can fold the two multiplies
1693         // together.
1694         for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
1695              OMulOp != e; ++OMulOp)
1696           if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
1697             // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
1698             const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
1699             if (Mul->getNumOperands() != 2) {
1700               SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
1701                                                   Mul->op_begin()+MulOp);
1702               MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
1703               InnerMul1 = getMulExpr(MulOps);
1704             }
1705             const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
1706             if (OtherMul->getNumOperands() != 2) {
1707               SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
1708                                                   OtherMul->op_begin()+OMulOp);
1709               MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end());
1710               InnerMul2 = getMulExpr(MulOps);
1711             }
1712             const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
1713             const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
1714             if (Ops.size() == 2) return OuterMul;
1715             Ops.erase(Ops.begin()+Idx);
1716             Ops.erase(Ops.begin()+OtherMulIdx-1);
1717             Ops.push_back(OuterMul);
1718             return getAddExpr(Ops);
1719           }
1720       }
1721     }
1722   }
1723
1724   // If there are any add recurrences in the operands list, see if any other
1725   // added values are loop invariant.  If so, we can fold them into the
1726   // recurrence.
1727   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1728     ++Idx;
1729
1730   // Scan over all recurrences, trying to fold loop invariants into them.
1731   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1732     // Scan all of the other operands to this add and add them to the vector if
1733     // they are loop invariant w.r.t. the recurrence.
1734     SmallVector<const SCEV *, 8> LIOps;
1735     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
1736     const Loop *AddRecLoop = AddRec->getLoop();
1737     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1738       if (isLoopInvariant(Ops[i], AddRecLoop)) {
1739         LIOps.push_back(Ops[i]);
1740         Ops.erase(Ops.begin()+i);
1741         --i; --e;
1742       }
1743
1744     // If we found some loop invariants, fold them into the recurrence.
1745     if (!LIOps.empty()) {
1746       //  NLI + LI + {Start,+,Step}  -->  NLI + {LI+Start,+,Step}
1747       LIOps.push_back(AddRec->getStart());
1748
1749       SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
1750                                              AddRec->op_end());
1751       AddRecOps[0] = getAddExpr(LIOps);
1752
1753       // Build the new addrec. Propagate the NUW and NSW flags if both the
1754       // outer add and the inner addrec are guaranteed to have no overflow.
1755       // Always propagate NW.
1756       Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
1757       const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
1758
1759       // If all of the other operands were loop invariant, we are done.
1760       if (Ops.size() == 1) return NewRec;
1761
1762       // Otherwise, add the folded AddRec by the non-invariant parts.
1763       for (unsigned i = 0;; ++i)
1764         if (Ops[i] == AddRec) {
1765           Ops[i] = NewRec;
1766           break;
1767         }
1768       return getAddExpr(Ops);
1769     }
1770
1771     // Okay, if there weren't any loop invariants to be folded, check to see if
1772     // there are multiple AddRec's with the same loop induction variable being
1773     // added together.  If so, we can fold them.
1774     for (unsigned OtherIdx = Idx+1;
1775          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
1776          ++OtherIdx)
1777       if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
1778         // Other + {A,+,B}<L> + {C,+,D}<L>  -->  Other + {A+C,+,B+D}<L>
1779         SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
1780                                                AddRec->op_end());
1781         for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
1782              ++OtherIdx)
1783           if (const SCEVAddRecExpr *OtherAddRec =
1784                 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]))
1785             if (OtherAddRec->getLoop() == AddRecLoop) {
1786               for (unsigned i = 0, e = OtherAddRec->getNumOperands();
1787                    i != e; ++i) {
1788                 if (i >= AddRecOps.size()) {
1789                   AddRecOps.append(OtherAddRec->op_begin()+i,
1790                                    OtherAddRec->op_end());
1791                   break;
1792                 }
1793                 AddRecOps[i] = getAddExpr(AddRecOps[i],
1794                                           OtherAddRec->getOperand(i));
1795               }
1796               Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
1797             }
1798         // Step size has changed, so we cannot guarantee no self-wraparound.
1799         Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
1800         return getAddExpr(Ops);
1801       }
1802
1803     // Otherwise couldn't fold anything into this recurrence.  Move onto the
1804     // next one.
1805   }
1806
1807   // Okay, it looks like we really DO need an add expr.  Check to see if we
1808   // already have one, otherwise create a new one.
1809   FoldingSetNodeID ID;
1810   ID.AddInteger(scAddExpr);
1811   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1812     ID.AddPointer(Ops[i]);
1813   void *IP = 0;
1814   SCEVAddExpr *S =
1815     static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1816   if (!S) {
1817     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
1818     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
1819     S = new (SCEVAllocator) SCEVAddExpr(ID.Intern(SCEVAllocator),
1820                                         O, Ops.size());
1821     UniqueSCEVs.InsertNode(S, IP);
1822   }
1823   S->setNoWrapFlags(Flags);
1824   return S;
1825 }
1826
1827 static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
1828   uint64_t k = i*j;
1829   if (j > 1 && k / j != i) Overflow = true;
1830   return k;
1831 }
1832
1833 /// Compute the result of "n choose k", the binomial coefficient.  If an
1834 /// intermediate computation overflows, Overflow will be set and the return will
1835 /// be garbage. Overflow is not cleared on absence of overflow.
1836 static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
1837   // We use the multiplicative formula:
1838   //     n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
1839   // At each iteration, we take the n-th term of the numeral and divide by the
1840   // (k-n)th term of the denominator.  This division will always produce an
1841   // integral result, and helps reduce the chance of overflow in the
1842   // intermediate computations. However, we can still overflow even when the
1843   // final result would fit.
1844
1845   if (n == 0 || n == k) return 1;
1846   if (k > n) return 0;
1847
1848   if (k > n/2)
1849     k = n-k;
1850
1851   uint64_t r = 1;
1852   for (uint64_t i = 1; i <= k; ++i) {
1853     r = umul_ov(r, n-(i-1), Overflow);
1854     r /= i;
1855   }
1856   return r;
1857 }
1858
1859 /// getMulExpr - Get a canonical multiply expression, or something simpler if
1860 /// possible.
1861 const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
1862                                         SCEV::NoWrapFlags Flags) {
1863   assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) &&
1864          "only nuw or nsw allowed");
1865   assert(!Ops.empty() && "Cannot get empty mul!");
1866   if (Ops.size() == 1) return Ops[0];
1867 #ifndef NDEBUG
1868   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
1869   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1870     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
1871            "SCEVMulExpr operand types don't match!");
1872 #endif
1873
1874   // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
1875   // And vice-versa.
1876   int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
1877   SCEV::NoWrapFlags SignOrUnsignWrap = maskFlags(Flags, SignOrUnsignMask);
1878   if (SignOrUnsignWrap && (SignOrUnsignWrap != SignOrUnsignMask)) {
1879     bool All = true;
1880     for (SmallVectorImpl<const SCEV *>::const_iterator I = Ops.begin(),
1881          E = Ops.end(); I != E; ++I)
1882       if (!isKnownNonNegative(*I)) {
1883         All = false;
1884         break;
1885       }
1886     if (All) Flags = setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
1887   }
1888
1889   // Sort by complexity, this groups all similar expression types together.
1890   GroupByComplexity(Ops, LI);
1891
1892   // If there are any constants, fold them together.
1893   unsigned Idx = 0;
1894   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1895
1896     // C1*(C2+V) -> C1*C2 + C1*V
1897     if (Ops.size() == 2)
1898       if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
1899         if (Add->getNumOperands() == 2 &&
1900             isa<SCEVConstant>(Add->getOperand(0)))
1901           return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
1902                             getMulExpr(LHSC, Add->getOperand(1)));
1903
1904     ++Idx;
1905     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1906       // We found two constants, fold them together!
1907       ConstantInt *Fold = ConstantInt::get(getContext(),
1908                                            LHSC->getValue()->getValue() *
1909                                            RHSC->getValue()->getValue());
1910       Ops[0] = getConstant(Fold);
1911       Ops.erase(Ops.begin()+1);  // Erase the folded element
1912       if (Ops.size() == 1) return Ops[0];
1913       LHSC = cast<SCEVConstant>(Ops[0]);
1914     }
1915
1916     // If we are left with a constant one being multiplied, strip it off.
1917     if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
1918       Ops.erase(Ops.begin());
1919       --Idx;
1920     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
1921       // If we have a multiply of zero, it will always be zero.
1922       return Ops[0];
1923     } else if (Ops[0]->isAllOnesValue()) {
1924       // If we have a mul by -1 of an add, try distributing the -1 among the
1925       // add operands.
1926       if (Ops.size() == 2) {
1927         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
1928           SmallVector<const SCEV *, 4> NewOps;
1929           bool AnyFolded = false;
1930           for (SCEVAddRecExpr::op_iterator I = Add->op_begin(),
1931                  E = Add->op_end(); I != E; ++I) {
1932             const SCEV *Mul = getMulExpr(Ops[0], *I);
1933             if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
1934             NewOps.push_back(Mul);
1935           }
1936           if (AnyFolded)
1937             return getAddExpr(NewOps);
1938         }
1939         else if (const SCEVAddRecExpr *
1940                  AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
1941           // Negation preserves a recurrence's no self-wrap property.
1942           SmallVector<const SCEV *, 4> Operands;
1943           for (SCEVAddRecExpr::op_iterator I = AddRec->op_begin(),
1944                  E = AddRec->op_end(); I != E; ++I) {
1945             Operands.push_back(getMulExpr(Ops[0], *I));
1946           }
1947           return getAddRecExpr(Operands, AddRec->getLoop(),
1948                                AddRec->getNoWrapFlags(SCEV::FlagNW));
1949         }
1950       }
1951     }
1952
1953     if (Ops.size() == 1)
1954       return Ops[0];
1955   }
1956
1957   // Skip over the add expression until we get to a multiply.
1958   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
1959     ++Idx;
1960
1961   // If there are mul operands inline them all into this expression.
1962   if (Idx < Ops.size()) {
1963     bool DeletedMul = false;
1964     while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
1965       // If we have an mul, expand the mul operands onto the end of the operands
1966       // list.
1967       Ops.erase(Ops.begin()+Idx);
1968       Ops.append(Mul->op_begin(), Mul->op_end());
1969       DeletedMul = true;
1970     }
1971
1972     // If we deleted at least one mul, we added operands to the end of the list,
1973     // and they are not necessarily sorted.  Recurse to resort and resimplify
1974     // any operands we just acquired.
1975     if (DeletedMul)
1976       return getMulExpr(Ops);
1977   }
1978
1979   // If there are any add recurrences in the operands list, see if any other
1980   // added values are loop invariant.  If so, we can fold them into the
1981   // recurrence.
1982   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
1983     ++Idx;
1984
1985   // Scan over all recurrences, trying to fold loop invariants into them.
1986   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
1987     // Scan all of the other operands to this mul and add them to the vector if
1988     // they are loop invariant w.r.t. the recurrence.
1989     SmallVector<const SCEV *, 8> LIOps;
1990     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
1991     const Loop *AddRecLoop = AddRec->getLoop();
1992     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
1993       if (isLoopInvariant(Ops[i], AddRecLoop)) {
1994         LIOps.push_back(Ops[i]);
1995         Ops.erase(Ops.begin()+i);
1996         --i; --e;
1997       }
1998
1999     // If we found some loop invariants, fold them into the recurrence.
2000     if (!LIOps.empty()) {
2001       //  NLI * LI * {Start,+,Step}  -->  NLI * {LI*Start,+,LI*Step}
2002       SmallVector<const SCEV *, 4> NewOps;
2003       NewOps.reserve(AddRec->getNumOperands());
2004       const SCEV *Scale = getMulExpr(LIOps);
2005       for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
2006         NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
2007
2008       // Build the new addrec. Propagate the NUW and NSW flags if both the
2009       // outer mul and the inner addrec are guaranteed to have no overflow.
2010       //
2011       // No self-wrap cannot be guaranteed after changing the step size, but
2012       // will be inferred if either NUW or NSW is true.
2013       Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW));
2014       const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags);
2015
2016       // If all of the other operands were loop invariant, we are done.
2017       if (Ops.size() == 1) return NewRec;
2018
2019       // Otherwise, multiply the folded AddRec by the non-invariant parts.
2020       for (unsigned i = 0;; ++i)
2021         if (Ops[i] == AddRec) {
2022           Ops[i] = NewRec;
2023           break;
2024         }
2025       return getMulExpr(Ops);
2026     }
2027
2028     // Okay, if there weren't any loop invariants to be folded, check to see if
2029     // there are multiple AddRec's with the same loop induction variable being
2030     // multiplied together.  If so, we can fold them.
2031     for (unsigned OtherIdx = Idx+1;
2032          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2033          ++OtherIdx) {
2034       if (AddRecLoop != cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop())
2035         continue;
2036
2037       // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
2038       // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
2039       //       choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
2040       //   ]]],+,...up to x=2n}.
2041       // Note that the arguments to choose() are always integers with values
2042       // known at compile time, never SCEV objects.
2043       //
2044       // The implementation avoids pointless extra computations when the two
2045       // addrec's are of different length (mathematically, it's equivalent to
2046       // an infinite stream of zeros on the right).
2047       bool OpsModified = false;
2048       for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2049            ++OtherIdx) {
2050         const SCEVAddRecExpr *OtherAddRec =
2051           dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2052         if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop)
2053           continue;
2054
2055         bool Overflow = false;
2056         Type *Ty = AddRec->getType();
2057         bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
2058         SmallVector<const SCEV*, 7> AddRecOps;
2059         for (int x = 0, xe = AddRec->getNumOperands() +
2060                OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
2061           const SCEV *Term = getConstant(Ty, 0);
2062           for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
2063             uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
2064             for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
2065                    ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
2066                  z < ze && !Overflow; ++z) {
2067               uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
2068               uint64_t Coeff;
2069               if (LargerThan64Bits)
2070                 Coeff = umul_ov(Coeff1, Coeff2, Overflow);
2071               else
2072                 Coeff = Coeff1*Coeff2;
2073               const SCEV *CoeffTerm = getConstant(Ty, Coeff);
2074               const SCEV *Term1 = AddRec->getOperand(y-z);
2075               const SCEV *Term2 = OtherAddRec->getOperand(z);
2076               Term = getAddExpr(Term, getMulExpr(CoeffTerm, Term1,Term2));
2077             }
2078           }
2079           AddRecOps.push_back(Term);
2080         }
2081         if (!Overflow) {
2082           const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(),
2083                                                 SCEV::FlagAnyWrap);
2084           if (Ops.size() == 2) return NewAddRec;
2085           Ops[Idx] = NewAddRec;
2086           Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2087           OpsModified = true;
2088           AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
2089           if (!AddRec)
2090             break;
2091         }
2092       }
2093       if (OpsModified)
2094         return getMulExpr(Ops);
2095     }
2096
2097     // Otherwise couldn't fold anything into this recurrence.  Move onto the
2098     // next one.
2099   }
2100
2101   // Okay, it looks like we really DO need an mul expr.  Check to see if we
2102   // already have one, otherwise create a new one.
2103   FoldingSetNodeID ID;
2104   ID.AddInteger(scMulExpr);
2105   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2106     ID.AddPointer(Ops[i]);
2107   void *IP = 0;
2108   SCEVMulExpr *S =
2109     static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2110   if (!S) {
2111     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2112     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2113     S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
2114                                         O, Ops.size());
2115     UniqueSCEVs.InsertNode(S, IP);
2116   }
2117   S->setNoWrapFlags(Flags);
2118   return S;
2119 }
2120
2121 /// getUDivExpr - Get a canonical unsigned division expression, or something
2122 /// simpler if possible.
2123 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
2124                                          const SCEV *RHS) {
2125   assert(getEffectiveSCEVType(LHS->getType()) ==
2126          getEffectiveSCEVType(RHS->getType()) &&
2127          "SCEVUDivExpr operand types don't match!");
2128
2129   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
2130     if (RHSC->getValue()->equalsInt(1))
2131       return LHS;                               // X udiv 1 --> x
2132     // If the denominator is zero, the result of the udiv is undefined. Don't
2133     // try to analyze it, because the resolution chosen here may differ from
2134     // the resolution chosen in other parts of the compiler.
2135     if (!RHSC->getValue()->isZero()) {
2136       // Determine if the division can be folded into the operands of
2137       // its operands.
2138       // TODO: Generalize this to non-constants by using known-bits information.
2139       Type *Ty = LHS->getType();
2140       unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
2141       unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
2142       // For non-power-of-two values, effectively round the value up to the
2143       // nearest power of two.
2144       if (!RHSC->getValue()->getValue().isPowerOf2())
2145         ++MaxShiftAmt;
2146       IntegerType *ExtTy =
2147         IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
2148       if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
2149         if (const SCEVConstant *Step =
2150             dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
2151           // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
2152           const APInt &StepInt = Step->getValue()->getValue();
2153           const APInt &DivInt = RHSC->getValue()->getValue();
2154           if (!StepInt.urem(DivInt) &&
2155               getZeroExtendExpr(AR, ExtTy) ==
2156               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
2157                             getZeroExtendExpr(Step, ExtTy),
2158                             AR->getLoop(), SCEV::FlagAnyWrap)) {
2159             SmallVector<const SCEV *, 4> Operands;
2160             for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
2161               Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
2162             return getAddRecExpr(Operands, AR->getLoop(),
2163                                  SCEV::FlagNW);
2164           }
2165           /// Get a canonical UDivExpr for a recurrence.
2166           /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
2167           // We can currently only fold X%N if X is constant.
2168           const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
2169           if (StartC && !DivInt.urem(StepInt) &&
2170               getZeroExtendExpr(AR, ExtTy) ==
2171               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
2172                             getZeroExtendExpr(Step, ExtTy),
2173                             AR->getLoop(), SCEV::FlagAnyWrap)) {
2174             const APInt &StartInt = StartC->getValue()->getValue();
2175             const APInt &StartRem = StartInt.urem(StepInt);
2176             if (StartRem != 0)
2177               LHS = getAddRecExpr(getConstant(StartInt - StartRem), Step,
2178                                   AR->getLoop(), SCEV::FlagNW);
2179           }
2180         }
2181       // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
2182       if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
2183         SmallVector<const SCEV *, 4> Operands;
2184         for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
2185           Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
2186         if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
2187           // Find an operand that's safely divisible.
2188           for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
2189             const SCEV *Op = M->getOperand(i);
2190             const SCEV *Div = getUDivExpr(Op, RHSC);
2191             if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
2192               Operands = SmallVector<const SCEV *, 4>(M->op_begin(),
2193                                                       M->op_end());
2194               Operands[i] = Div;
2195               return getMulExpr(Operands);
2196             }
2197           }
2198       }
2199       // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
2200       if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
2201         SmallVector<const SCEV *, 4> Operands;
2202         for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
2203           Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
2204         if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
2205           Operands.clear();
2206           for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
2207             const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
2208             if (isa<SCEVUDivExpr>(Op) ||
2209                 getMulExpr(Op, RHS) != A->getOperand(i))
2210               break;
2211             Operands.push_back(Op);
2212           }
2213           if (Operands.size() == A->getNumOperands())
2214             return getAddExpr(Operands);
2215         }
2216       }
2217
2218       // Fold if both operands are constant.
2219       if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
2220         Constant *LHSCV = LHSC->getValue();
2221         Constant *RHSCV = RHSC->getValue();
2222         return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
2223                                                                    RHSCV)));
2224       }
2225     }
2226   }
2227
2228   FoldingSetNodeID ID;
2229   ID.AddInteger(scUDivExpr);
2230   ID.AddPointer(LHS);
2231   ID.AddPointer(RHS);
2232   void *IP = 0;
2233   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2234   SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
2235                                              LHS, RHS);
2236   UniqueSCEVs.InsertNode(S, IP);
2237   return S;
2238 }
2239
2240
2241 /// getAddRecExpr - Get an add recurrence expression for the specified loop.
2242 /// Simplify the expression as much as possible.
2243 const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step,
2244                                            const Loop *L,
2245                                            SCEV::NoWrapFlags Flags) {
2246   SmallVector<const SCEV *, 4> Operands;
2247   Operands.push_back(Start);
2248   if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
2249     if (StepChrec->getLoop() == L) {
2250       Operands.append(StepChrec->op_begin(), StepChrec->op_end());
2251       return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW));
2252     }
2253
2254   Operands.push_back(Step);
2255   return getAddRecExpr(Operands, L, Flags);
2256 }
2257
2258 /// getAddRecExpr - Get an add recurrence expression for the specified loop.
2259 /// Simplify the expression as much as possible.
2260 const SCEV *
2261 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
2262                                const Loop *L, SCEV::NoWrapFlags Flags) {
2263   if (Operands.size() == 1) return Operands[0];
2264 #ifndef NDEBUG
2265   Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
2266   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
2267     assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy &&
2268            "SCEVAddRecExpr operand types don't match!");
2269   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
2270     assert(isLoopInvariant(Operands[i], L) &&
2271            "SCEVAddRecExpr operand is not loop-invariant!");
2272 #endif
2273
2274   if (Operands.back()->isZero()) {
2275     Operands.pop_back();
2276     return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0}  -->  X
2277   }
2278
2279   // It's tempting to want to call getMaxBackedgeTakenCount count here and
2280   // use that information to infer NUW and NSW flags. However, computing a
2281   // BE count requires calling getAddRecExpr, so we may not yet have a
2282   // meaningful BE count at this point (and if we don't, we'd be stuck
2283   // with a SCEVCouldNotCompute as the cached BE count).
2284
2285   // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
2286   // And vice-versa.
2287   int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
2288   SCEV::NoWrapFlags SignOrUnsignWrap = maskFlags(Flags, SignOrUnsignMask);
2289   if (SignOrUnsignWrap && (SignOrUnsignWrap != SignOrUnsignMask)) {
2290     bool All = true;
2291     for (SmallVectorImpl<const SCEV *>::const_iterator I = Operands.begin(),
2292          E = Operands.end(); I != E; ++I)
2293       if (!isKnownNonNegative(*I)) {
2294         All = false;
2295         break;
2296       }
2297     if (All) Flags = setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
2298   }
2299
2300   // Canonicalize nested AddRecs in by nesting them in order of loop depth.
2301   if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
2302     const Loop *NestedLoop = NestedAR->getLoop();
2303     if (L->contains(NestedLoop) ?
2304         (L->getLoopDepth() < NestedLoop->getLoopDepth()) :
2305         (!NestedLoop->contains(L) &&
2306          DT->dominates(L->getHeader(), NestedLoop->getHeader()))) {
2307       SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
2308                                                   NestedAR->op_end());
2309       Operands[0] = NestedAR->getStart();
2310       // AddRecs require their operands be loop-invariant with respect to their
2311       // loops. Don't perform this transformation if it would break this
2312       // requirement.
2313       bool AllInvariant = true;
2314       for (unsigned i = 0, e = Operands.size(); i != e; ++i)
2315         if (!isLoopInvariant(Operands[i], L)) {
2316           AllInvariant = false;
2317           break;
2318         }
2319       if (AllInvariant) {
2320         // Create a recurrence for the outer loop with the same step size.
2321         //
2322         // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
2323         // inner recurrence has the same property.
2324         SCEV::NoWrapFlags OuterFlags =
2325           maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
2326
2327         NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
2328         AllInvariant = true;
2329         for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
2330           if (!isLoopInvariant(NestedOperands[i], NestedLoop)) {
2331             AllInvariant = false;
2332             break;
2333           }
2334         if (AllInvariant) {
2335           // Ok, both add recurrences are valid after the transformation.
2336           //
2337           // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
2338           // the outer recurrence has the same property.
2339           SCEV::NoWrapFlags InnerFlags =
2340             maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
2341           return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
2342         }
2343       }
2344       // Reset Operands to its original state.
2345       Operands[0] = NestedAR;
2346     }
2347   }
2348
2349   // Okay, it looks like we really DO need an addrec expr.  Check to see if we
2350   // already have one, otherwise create a new one.
2351   FoldingSetNodeID ID;
2352   ID.AddInteger(scAddRecExpr);
2353   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
2354     ID.AddPointer(Operands[i]);
2355   ID.AddPointer(L);
2356   void *IP = 0;
2357   SCEVAddRecExpr *S =
2358     static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2359   if (!S) {
2360     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size());
2361     std::uninitialized_copy(Operands.begin(), Operands.end(), O);
2362     S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator),
2363                                            O, Operands.size(), L);
2364     UniqueSCEVs.InsertNode(S, IP);
2365   }
2366   S->setNoWrapFlags(Flags);
2367   return S;
2368 }
2369
2370 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
2371                                          const SCEV *RHS) {
2372   SmallVector<const SCEV *, 2> Ops;
2373   Ops.push_back(LHS);
2374   Ops.push_back(RHS);
2375   return getSMaxExpr(Ops);
2376 }
2377
2378 const SCEV *
2379 ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
2380   assert(!Ops.empty() && "Cannot get empty smax!");
2381   if (Ops.size() == 1) return Ops[0];
2382 #ifndef NDEBUG
2383   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2384   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2385     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2386            "SCEVSMaxExpr operand types don't match!");
2387 #endif
2388
2389   // Sort by complexity, this groups all similar expression types together.
2390   GroupByComplexity(Ops, LI);
2391
2392   // If there are any constants, fold them together.
2393   unsigned Idx = 0;
2394   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2395     ++Idx;
2396     assert(Idx < Ops.size());
2397     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2398       // We found two constants, fold them together!
2399       ConstantInt *Fold = ConstantInt::get(getContext(),
2400                               APIntOps::smax(LHSC->getValue()->getValue(),
2401                                              RHSC->getValue()->getValue()));
2402       Ops[0] = getConstant(Fold);
2403       Ops.erase(Ops.begin()+1);  // Erase the folded element
2404       if (Ops.size() == 1) return Ops[0];
2405       LHSC = cast<SCEVConstant>(Ops[0]);
2406     }
2407
2408     // If we are left with a constant minimum-int, strip it off.
2409     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
2410       Ops.erase(Ops.begin());
2411       --Idx;
2412     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
2413       // If we have an smax with a constant maximum-int, it will always be
2414       // maximum-int.
2415       return Ops[0];
2416     }
2417
2418     if (Ops.size() == 1) return Ops[0];
2419   }
2420
2421   // Find the first SMax
2422   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
2423     ++Idx;
2424
2425   // Check to see if one of the operands is an SMax. If so, expand its operands
2426   // onto our operand list, and recurse to simplify.
2427   if (Idx < Ops.size()) {
2428     bool DeletedSMax = false;
2429     while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
2430       Ops.erase(Ops.begin()+Idx);
2431       Ops.append(SMax->op_begin(), SMax->op_end());
2432       DeletedSMax = true;
2433     }
2434
2435     if (DeletedSMax)
2436       return getSMaxExpr(Ops);
2437   }
2438
2439   // Okay, check to see if the same value occurs in the operand list twice.  If
2440   // so, delete one.  Since we sorted the list, these values are required to
2441   // be adjacent.
2442   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
2443     //  X smax Y smax Y  -->  X smax Y
2444     //  X smax Y         -->  X, if X is always greater than Y
2445     if (Ops[i] == Ops[i+1] ||
2446         isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) {
2447       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
2448       --i; --e;
2449     } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) {
2450       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
2451       --i; --e;
2452     }
2453
2454   if (Ops.size() == 1) return Ops[0];
2455
2456   assert(!Ops.empty() && "Reduced smax down to nothing!");
2457
2458   // Okay, it looks like we really DO need an smax expr.  Check to see if we
2459   // already have one, otherwise create a new one.
2460   FoldingSetNodeID ID;
2461   ID.AddInteger(scSMaxExpr);
2462   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2463     ID.AddPointer(Ops[i]);
2464   void *IP = 0;
2465   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2466   const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2467   std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2468   SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator),
2469                                              O, Ops.size());
2470   UniqueSCEVs.InsertNode(S, IP);
2471   return S;
2472 }
2473
2474 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
2475                                          const SCEV *RHS) {
2476   SmallVector<const SCEV *, 2> Ops;
2477   Ops.push_back(LHS);
2478   Ops.push_back(RHS);
2479   return getUMaxExpr(Ops);
2480 }
2481
2482 const SCEV *
2483 ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
2484   assert(!Ops.empty() && "Cannot get empty umax!");
2485   if (Ops.size() == 1) return Ops[0];
2486 #ifndef NDEBUG
2487   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2488   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2489     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2490            "SCEVUMaxExpr operand types don't match!");
2491 #endif
2492
2493   // Sort by complexity, this groups all similar expression types together.
2494   GroupByComplexity(Ops, LI);
2495
2496   // If there are any constants, fold them together.
2497   unsigned Idx = 0;
2498   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2499     ++Idx;
2500     assert(Idx < Ops.size());
2501     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2502       // We found two constants, fold them together!
2503       ConstantInt *Fold = ConstantInt::get(getContext(),
2504                               APIntOps::umax(LHSC->getValue()->getValue(),
2505                                              RHSC->getValue()->getValue()));
2506       Ops[0] = getConstant(Fold);
2507       Ops.erase(Ops.begin()+1);  // Erase the folded element
2508       if (Ops.size() == 1) return Ops[0];
2509       LHSC = cast<SCEVConstant>(Ops[0]);
2510     }
2511
2512     // If we are left with a constant minimum-int, strip it off.
2513     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
2514       Ops.erase(Ops.begin());
2515       --Idx;
2516     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
2517       // If we have an umax with a constant maximum-int, it will always be
2518       // maximum-int.
2519       return Ops[0];
2520     }
2521
2522     if (Ops.size() == 1) return Ops[0];
2523   }
2524
2525   // Find the first UMax
2526   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
2527     ++Idx;
2528
2529   // Check to see if one of the operands is a UMax. If so, expand its operands
2530   // onto our operand list, and recurse to simplify.
2531   if (Idx < Ops.size()) {
2532     bool DeletedUMax = false;
2533     while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
2534       Ops.erase(Ops.begin()+Idx);
2535       Ops.append(UMax->op_begin(), UMax->op_end());
2536       DeletedUMax = true;
2537     }
2538
2539     if (DeletedUMax)
2540       return getUMaxExpr(Ops);
2541   }
2542
2543   // Okay, check to see if the same value occurs in the operand list twice.  If
2544   // so, delete one.  Since we sorted the list, these values are required to
2545   // be adjacent.
2546   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
2547     //  X umax Y umax Y  -->  X umax Y
2548     //  X umax Y         -->  X, if X is always greater than Y
2549     if (Ops[i] == Ops[i+1] ||
2550         isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) {
2551       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
2552       --i; --e;
2553     } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) {
2554       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
2555       --i; --e;
2556     }
2557
2558   if (Ops.size() == 1) return Ops[0];
2559
2560   assert(!Ops.empty() && "Reduced umax down to nothing!");
2561
2562   // Okay, it looks like we really DO need a umax expr.  Check to see if we
2563   // already have one, otherwise create a new one.
2564   FoldingSetNodeID ID;
2565   ID.AddInteger(scUMaxExpr);
2566   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2567     ID.AddPointer(Ops[i]);
2568   void *IP = 0;
2569   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2570   const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2571   std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2572   SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator),
2573                                              O, Ops.size());
2574   UniqueSCEVs.InsertNode(S, IP);
2575   return S;
2576 }
2577
2578 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
2579                                          const SCEV *RHS) {
2580   // ~smax(~x, ~y) == smin(x, y).
2581   return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2582 }
2583
2584 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
2585                                          const SCEV *RHS) {
2586   // ~umax(~x, ~y) == umin(x, y)
2587   return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
2588 }
2589
2590 const SCEV *ScalarEvolution::getSizeOfExpr(Type *AllocTy) {
2591   // If we have DataLayout, we can bypass creating a target-independent
2592   // constant expression and then folding it back into a ConstantInt.
2593   // This is just a compile-time optimization.
2594   if (TD)
2595     return getConstant(TD->getIntPtrType(getContext()),
2596                        TD->getTypeAllocSize(AllocTy));
2597
2598   Constant *C = ConstantExpr::getSizeOf(AllocTy);
2599   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2600     if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI))
2601       C = Folded;
2602   Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
2603   return getTruncateOrZeroExtend(getSCEV(C), Ty);
2604 }
2605
2606 const SCEV *ScalarEvolution::getAlignOfExpr(Type *AllocTy) {
2607   Constant *C = ConstantExpr::getAlignOf(AllocTy);
2608   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2609     if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI))
2610       C = Folded;
2611   Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
2612   return getTruncateOrZeroExtend(getSCEV(C), Ty);
2613 }
2614
2615 const SCEV *ScalarEvolution::getOffsetOfExpr(StructType *STy,
2616                                              unsigned FieldNo) {
2617   // If we have DataLayout, we can bypass creating a target-independent
2618   // constant expression and then folding it back into a ConstantInt.
2619   // This is just a compile-time optimization.
2620   if (TD)
2621     return getConstant(TD->getIntPtrType(getContext()),
2622                        TD->getStructLayout(STy)->getElementOffset(FieldNo));
2623
2624   Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo);
2625   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2626     if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI))
2627       C = Folded;
2628   Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
2629   return getTruncateOrZeroExtend(getSCEV(C), Ty);
2630 }
2631
2632 const SCEV *ScalarEvolution::getOffsetOfExpr(Type *CTy,
2633                                              Constant *FieldNo) {
2634   Constant *C = ConstantExpr::getOffsetOf(CTy, FieldNo);
2635   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
2636     if (Constant *Folded = ConstantFoldConstantExpression(CE, TD, TLI))
2637       C = Folded;
2638   Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(CTy));
2639   return getTruncateOrZeroExtend(getSCEV(C), Ty);
2640 }
2641
2642 const SCEV *ScalarEvolution::getUnknown(Value *V) {
2643   // Don't attempt to do anything other than create a SCEVUnknown object
2644   // here.  createSCEV only calls getUnknown after checking for all other
2645   // interesting possibilities, and any other code that calls getUnknown
2646   // is doing so in order to hide a value from SCEV canonicalization.
2647
2648   FoldingSetNodeID ID;
2649   ID.AddInteger(scUnknown);
2650   ID.AddPointer(V);
2651   void *IP = 0;
2652   if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
2653     assert(cast<SCEVUnknown>(S)->getValue() == V &&
2654            "Stale SCEVUnknown in uniquing map!");
2655     return S;
2656   }
2657   SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
2658                                             FirstUnknown);
2659   FirstUnknown = cast<SCEVUnknown>(S);
2660   UniqueSCEVs.InsertNode(S, IP);
2661   return S;
2662 }
2663
2664 //===----------------------------------------------------------------------===//
2665 //            Basic SCEV Analysis and PHI Idiom Recognition Code
2666 //
2667
2668 /// isSCEVable - Test if values of the given type are analyzable within
2669 /// the SCEV framework. This primarily includes integer types, and it
2670 /// can optionally include pointer types if the ScalarEvolution class
2671 /// has access to target-specific information.
2672 bool ScalarEvolution::isSCEVable(Type *Ty) const {
2673   // Integers and pointers are always SCEVable.
2674   return Ty->isIntegerTy() || Ty->isPointerTy();
2675 }
2676
2677 /// getTypeSizeInBits - Return the size in bits of the specified type,
2678 /// for which isSCEVable must return true.
2679 uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
2680   assert(isSCEVable(Ty) && "Type is not SCEVable!");
2681
2682   // If we have a DataLayout, use it!
2683   if (TD)
2684     return TD->getTypeSizeInBits(Ty);
2685
2686   // Integer types have fixed sizes.
2687   if (Ty->isIntegerTy())
2688     return Ty->getPrimitiveSizeInBits();
2689
2690   // The only other support type is pointer. Without DataLayout, conservatively
2691   // assume pointers are 64-bit.
2692   assert(Ty->isPointerTy() && "isSCEVable permitted a non-SCEVable type!");
2693   return 64;
2694 }
2695
2696 /// getEffectiveSCEVType - Return a type with the same bitwidth as
2697 /// the given type and which represents how SCEV will treat the given
2698 /// type, for which isSCEVable must return true. For pointer types,
2699 /// this is the pointer-sized integer type.
2700 Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
2701   assert(isSCEVable(Ty) && "Type is not SCEVable!");
2702
2703   if (Ty->isIntegerTy())
2704     return Ty;
2705
2706   // The only other support type is pointer.
2707   assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
2708   if (TD) return TD->getIntPtrType(getContext());
2709
2710   // Without DataLayout, conservatively assume pointers are 64-bit.
2711   return Type::getInt64Ty(getContext());
2712 }
2713
2714 const SCEV *ScalarEvolution::getCouldNotCompute() {
2715   return &CouldNotCompute;
2716 }
2717
2718 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
2719 /// expression and create a new one.
2720 const SCEV *ScalarEvolution::getSCEV(Value *V) {
2721   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
2722
2723   ValueExprMapType::const_iterator I = ValueExprMap.find_as(V);
2724   if (I != ValueExprMap.end()) return I->second;
2725   const SCEV *S = createSCEV(V);
2726
2727   // The process of creating a SCEV for V may have caused other SCEVs
2728   // to have been created, so it's necessary to insert the new entry
2729   // from scratch, rather than trying to remember the insert position
2730   // above.
2731   ValueExprMap.insert(std::make_pair(SCEVCallbackVH(V, this), S));
2732   return S;
2733 }
2734
2735 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
2736 ///
2737 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
2738   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
2739     return getConstant(
2740                cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
2741
2742   Type *Ty = V->getType();
2743   Ty = getEffectiveSCEVType(Ty);
2744   return getMulExpr(V,
2745                   getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))));
2746 }
2747
2748 /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
2749 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
2750   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
2751     return getConstant(
2752                 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
2753
2754   Type *Ty = V->getType();
2755   Ty = getEffectiveSCEVType(Ty);
2756   const SCEV *AllOnes =
2757                    getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
2758   return getMinusSCEV(AllOnes, V);
2759 }
2760
2761 /// getMinusSCEV - Return LHS-RHS.  Minus is represented in SCEV as A+B*-1.
2762 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
2763                                           SCEV::NoWrapFlags Flags) {
2764   assert(!maskFlags(Flags, SCEV::FlagNUW) && "subtraction does not have NUW");
2765
2766   // Fast path: X - X --> 0.
2767   if (LHS == RHS)
2768     return getConstant(LHS->getType(), 0);
2769
2770   // X - Y --> X + -Y
2771   return getAddExpr(LHS, getNegativeSCEV(RHS), Flags);
2772 }
2773
2774 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
2775 /// input value to the specified type.  If the type must be extended, it is zero
2776 /// extended.
2777 const SCEV *
2778 ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty) {
2779   Type *SrcTy = V->getType();
2780   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
2781          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
2782          "Cannot truncate or zero extend with non-integer arguments!");
2783   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2784     return V;  // No conversion
2785   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
2786     return getTruncateExpr(V, Ty);
2787   return getZeroExtendExpr(V, Ty);
2788 }
2789
2790 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
2791 /// input value to the specified type.  If the type must be extended, it is sign
2792 /// extended.
2793 const SCEV *
2794 ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
2795                                          Type *Ty) {
2796   Type *SrcTy = V->getType();
2797   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
2798          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
2799          "Cannot truncate or zero extend with non-integer arguments!");
2800   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2801     return V;  // No conversion
2802   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
2803     return getTruncateExpr(V, Ty);
2804   return getSignExtendExpr(V, Ty);
2805 }
2806
2807 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
2808 /// input value to the specified type.  If the type must be extended, it is zero
2809 /// extended.  The conversion must not be narrowing.
2810 const SCEV *
2811 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
2812   Type *SrcTy = V->getType();
2813   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
2814          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
2815          "Cannot noop or zero extend with non-integer arguments!");
2816   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2817          "getNoopOrZeroExtend cannot truncate!");
2818   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2819     return V;  // No conversion
2820   return getZeroExtendExpr(V, Ty);
2821 }
2822
2823 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
2824 /// input value to the specified type.  If the type must be extended, it is sign
2825 /// extended.  The conversion must not be narrowing.
2826 const SCEV *
2827 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
2828   Type *SrcTy = V->getType();
2829   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
2830          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
2831          "Cannot noop or sign extend with non-integer arguments!");
2832   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2833          "getNoopOrSignExtend cannot truncate!");
2834   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2835     return V;  // No conversion
2836   return getSignExtendExpr(V, Ty);
2837 }
2838
2839 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
2840 /// the input value to the specified type. If the type must be extended,
2841 /// it is extended with unspecified bits. The conversion must not be
2842 /// narrowing.
2843 const SCEV *
2844 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
2845   Type *SrcTy = V->getType();
2846   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
2847          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
2848          "Cannot noop or any extend with non-integer arguments!");
2849   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
2850          "getNoopOrAnyExtend cannot truncate!");
2851   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2852     return V;  // No conversion
2853   return getAnyExtendExpr(V, Ty);
2854 }
2855
2856 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
2857 /// input value to the specified type.  The conversion must not be widening.
2858 const SCEV *
2859 ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
2860   Type *SrcTy = V->getType();
2861   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
2862          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
2863          "Cannot truncate or noop with non-integer arguments!");
2864   assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
2865          "getTruncateOrNoop cannot extend!");
2866   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
2867     return V;  // No conversion
2868   return getTruncateExpr(V, Ty);
2869 }
2870
2871 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
2872 /// the types using zero-extension, and then perform a umax operation
2873 /// with them.
2874 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
2875                                                         const SCEV *RHS) {
2876   const SCEV *PromotedLHS = LHS;
2877   const SCEV *PromotedRHS = RHS;
2878
2879   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2880     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2881   else
2882     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2883
2884   return getUMaxExpr(PromotedLHS, PromotedRHS);
2885 }
2886
2887 /// getUMinFromMismatchedTypes - Promote the operands to the wider of
2888 /// the types using zero-extension, and then perform a umin operation
2889 /// with them.
2890 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
2891                                                         const SCEV *RHS) {
2892   const SCEV *PromotedLHS = LHS;
2893   const SCEV *PromotedRHS = RHS;
2894
2895   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
2896     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
2897   else
2898     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
2899
2900   return getUMinExpr(PromotedLHS, PromotedRHS);
2901 }
2902
2903 /// getPointerBase - Transitively follow the chain of pointer-type operands
2904 /// until reaching a SCEV that does not have a single pointer operand. This
2905 /// returns a SCEVUnknown pointer for well-formed pointer-type expressions,
2906 /// but corner cases do exist.
2907 const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
2908   // A pointer operand may evaluate to a nonpointer expression, such as null.
2909   if (!V->getType()->isPointerTy())
2910     return V;
2911
2912   if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
2913     return getPointerBase(Cast->getOperand());
2914   }
2915   else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
2916     const SCEV *PtrOp = 0;
2917     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
2918          I != E; ++I) {
2919       if ((*I)->getType()->isPointerTy()) {
2920         // Cannot find the base of an expression with multiple pointer operands.
2921         if (PtrOp)
2922           return V;
2923         PtrOp = *I;
2924       }
2925     }
2926     if (!PtrOp)
2927       return V;
2928     return getPointerBase(PtrOp);
2929   }
2930   return V;
2931 }
2932
2933 /// PushDefUseChildren - Push users of the given Instruction
2934 /// onto the given Worklist.
2935 static void
2936 PushDefUseChildren(Instruction *I,
2937                    SmallVectorImpl<Instruction *> &Worklist) {
2938   // Push the def-use children onto the Worklist stack.
2939   for (Value::use_iterator UI = I->use_begin(), UE = I->use_end();
2940        UI != UE; ++UI)
2941     Worklist.push_back(cast<Instruction>(*UI));
2942 }
2943
2944 /// ForgetSymbolicValue - This looks up computed SCEV values for all
2945 /// instructions that depend on the given instruction and removes them from
2946 /// the ValueExprMapType map if they reference SymName. This is used during PHI
2947 /// resolution.
2948 void
2949 ScalarEvolution::ForgetSymbolicName(Instruction *PN, const SCEV *SymName) {
2950   SmallVector<Instruction *, 16> Worklist;
2951   PushDefUseChildren(PN, Worklist);
2952
2953   SmallPtrSet<Instruction *, 8> Visited;
2954   Visited.insert(PN);
2955   while (!Worklist.empty()) {
2956     Instruction *I = Worklist.pop_back_val();
2957     if (!Visited.insert(I)) continue;
2958
2959     ValueExprMapType::iterator It =
2960       ValueExprMap.find_as(static_cast<Value *>(I));
2961     if (It != ValueExprMap.end()) {
2962       const SCEV *Old = It->second;
2963
2964       // Short-circuit the def-use traversal if the symbolic name
2965       // ceases to appear in expressions.
2966       if (Old != SymName && !hasOperand(Old, SymName))
2967         continue;
2968
2969       // SCEVUnknown for a PHI either means that it has an unrecognized
2970       // structure, it's a PHI that's in the progress of being computed
2971       // by createNodeForPHI, or it's a single-value PHI. In the first case,
2972       // additional loop trip count information isn't going to change anything.
2973       // In the second case, createNodeForPHI will perform the necessary
2974       // updates on its own when it gets to that point. In the third, we do
2975       // want to forget the SCEVUnknown.
2976       if (!isa<PHINode>(I) ||
2977           !isa<SCEVUnknown>(Old) ||
2978           (I != PN && Old == SymName)) {
2979         forgetMemoizedResults(Old);
2980         ValueExprMap.erase(It);
2981       }
2982     }
2983
2984     PushDefUseChildren(I, Worklist);
2985   }
2986 }
2987
2988 /// createNodeForPHI - PHI nodes have two cases.  Either the PHI node exists in
2989 /// a loop header, making it a potential recurrence, or it doesn't.
2990 ///
2991 const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
2992   if (const Loop *L = LI->getLoopFor(PN->getParent()))
2993     if (L->getHeader() == PN->getParent()) {
2994       // The loop may have multiple entrances or multiple exits; we can analyze
2995       // this phi as an addrec if it has a unique entry value and a unique
2996       // backedge value.
2997       Value *BEValueV = 0, *StartValueV = 0;
2998       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2999         Value *V = PN->getIncomingValue(i);
3000         if (L->contains(PN->getIncomingBlock(i))) {
3001           if (!BEValueV) {
3002             BEValueV = V;
3003           } else if (BEValueV != V) {
3004             BEValueV = 0;
3005             break;
3006           }
3007         } else if (!StartValueV) {
3008           StartValueV = V;
3009         } else if (StartValueV != V) {
3010           StartValueV = 0;
3011           break;
3012         }
3013       }
3014       if (BEValueV && StartValueV) {
3015         // While we are analyzing this PHI node, handle its value symbolically.
3016         const SCEV *SymbolicName = getUnknown(PN);
3017         assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
3018                "PHI node already processed?");
3019         ValueExprMap.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
3020
3021         // Using this symbolic name for the PHI, analyze the value coming around
3022         // the back-edge.
3023         const SCEV *BEValue = getSCEV(BEValueV);
3024
3025         // NOTE: If BEValue is loop invariant, we know that the PHI node just
3026         // has a special value for the first iteration of the loop.
3027
3028         // If the value coming around the backedge is an add with the symbolic
3029         // value we just inserted, then we found a simple induction variable!
3030         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
3031           // If there is a single occurrence of the symbolic value, replace it
3032           // with a recurrence.
3033           unsigned FoundIndex = Add->getNumOperands();
3034           for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
3035             if (Add->getOperand(i) == SymbolicName)
3036               if (FoundIndex == e) {
3037                 FoundIndex = i;
3038                 break;
3039               }
3040
3041           if (FoundIndex != Add->getNumOperands()) {
3042             // Create an add with everything but the specified operand.
3043             SmallVector<const SCEV *, 8> Ops;
3044             for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
3045               if (i != FoundIndex)
3046                 Ops.push_back(Add->getOperand(i));
3047             const SCEV *Accum = getAddExpr(Ops);
3048
3049             // This is not a valid addrec if the step amount is varying each
3050             // loop iteration, but is not itself an addrec in this loop.
3051             if (isLoopInvariant(Accum, L) ||
3052                 (isa<SCEVAddRecExpr>(Accum) &&
3053                  cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
3054               SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
3055
3056               // If the increment doesn't overflow, then neither the addrec nor
3057               // the post-increment will overflow.
3058               if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) {
3059                 if (OBO->hasNoUnsignedWrap())
3060                   Flags = setFlags(Flags, SCEV::FlagNUW);
3061                 if (OBO->hasNoSignedWrap())
3062                   Flags = setFlags(Flags, SCEV::FlagNSW);
3063               } else if (const GEPOperator *GEP =
3064                          dyn_cast<GEPOperator>(BEValueV)) {
3065                 // If the increment is an inbounds GEP, then we know the address
3066                 // space cannot be wrapped around. We cannot make any guarantee
3067                 // about signed or unsigned overflow because pointers are
3068                 // unsigned but we may have a negative index from the base
3069                 // pointer.
3070                 if (GEP->isInBounds())
3071                   Flags = setFlags(Flags, SCEV::FlagNW);
3072               }
3073
3074               const SCEV *StartVal = getSCEV(StartValueV);
3075               const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
3076
3077               // Since the no-wrap flags are on the increment, they apply to the
3078               // post-incremented value as well.
3079               if (isLoopInvariant(Accum, L))
3080                 (void)getAddRecExpr(getAddExpr(StartVal, Accum),
3081                                     Accum, L, Flags);
3082
3083               // Okay, for the entire analysis of this edge we assumed the PHI
3084               // to be symbolic.  We now need to go back and purge all of the
3085               // entries for the scalars that use the symbolic expression.
3086               ForgetSymbolicName(PN, SymbolicName);
3087               ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
3088               return PHISCEV;
3089             }
3090           }
3091         } else if (const SCEVAddRecExpr *AddRec =
3092                      dyn_cast<SCEVAddRecExpr>(BEValue)) {
3093           // Otherwise, this could be a loop like this:
3094           //     i = 0;  for (j = 1; ..; ++j) { ....  i = j; }
3095           // In this case, j = {1,+,1}  and BEValue is j.
3096           // Because the other in-value of i (0) fits the evolution of BEValue
3097           // i really is an addrec evolution.
3098           if (AddRec->getLoop() == L && AddRec->isAffine()) {
3099             const SCEV *StartVal = getSCEV(StartValueV);
3100
3101             // If StartVal = j.start - j.stride, we can use StartVal as the
3102             // initial step of the addrec evolution.
3103             if (StartVal == getMinusSCEV(AddRec->getOperand(0),
3104                                          AddRec->getOperand(1))) {
3105               // FIXME: For constant StartVal, we should be able to infer
3106               // no-wrap flags.
3107               const SCEV *PHISCEV =
3108                 getAddRecExpr(StartVal, AddRec->getOperand(1), L,
3109                               SCEV::FlagAnyWrap);
3110
3111               // Okay, for the entire analysis of this edge we assumed the PHI
3112               // to be symbolic.  We now need to go back and purge all of the
3113               // entries for the scalars that use the symbolic expression.
3114               ForgetSymbolicName(PN, SymbolicName);
3115               ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
3116               return PHISCEV;
3117             }
3118           }
3119         }
3120       }
3121     }
3122
3123   // If the PHI has a single incoming value, follow that value, unless the
3124   // PHI's incoming blocks are in a different loop, in which case doing so
3125   // risks breaking LCSSA form. Instcombine would normally zap these, but
3126   // it doesn't have DominatorTree information, so it may miss cases.
3127   if (Value *V = SimplifyInstruction(PN, TD, TLI, DT))
3128     if (LI->replacementPreservesLCSSAForm(PN, V))
3129       return getSCEV(V);
3130
3131   // If it's not a loop phi, we can't handle it yet.
3132   return getUnknown(PN);
3133 }
3134
3135 /// createNodeForGEP - Expand GEP instructions into add and multiply
3136 /// operations. This allows them to be analyzed by regular SCEV code.
3137 ///
3138 const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
3139
3140   // Don't blindly transfer the inbounds flag from the GEP instruction to the
3141   // Add expression, because the Instruction may be guarded by control flow
3142   // and the no-overflow bits may not be valid for the expression in any
3143   // context.
3144   bool isInBounds = GEP->isInBounds();
3145
3146   Type *IntPtrTy = getEffectiveSCEVType(GEP->getType());
3147   Value *Base = GEP->getOperand(0);
3148   // Don't attempt to analyze GEPs over unsized objects.
3149   if (!cast<PointerType>(Base->getType())->getElementType()->isSized())
3150     return getUnknown(GEP);
3151   const SCEV *TotalOffset = getConstant(IntPtrTy, 0);
3152   gep_type_iterator GTI = gep_type_begin(GEP);
3153   for (GetElementPtrInst::op_iterator I = llvm::next(GEP->op_begin()),
3154                                       E = GEP->op_end();
3155        I != E; ++I) {
3156     Value *Index = *I;
3157     // Compute the (potentially symbolic) offset in bytes for this index.
3158     if (StructType *STy = dyn_cast<StructType>(*GTI++)) {
3159       // For a struct, add the member offset.
3160       unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
3161       const SCEV *FieldOffset = getOffsetOfExpr(STy, FieldNo);
3162
3163       // Add the field offset to the running total offset.
3164       TotalOffset = getAddExpr(TotalOffset, FieldOffset);
3165     } else {
3166       // For an array, add the element offset, explicitly scaled.
3167       const SCEV *ElementSize = getSizeOfExpr(*GTI);
3168       const SCEV *IndexS = getSCEV(Index);
3169       // Getelementptr indices are signed.
3170       IndexS = getTruncateOrSignExtend(IndexS, IntPtrTy);
3171
3172       // Multiply the index by the element size to compute the element offset.
3173       const SCEV *LocalOffset = getMulExpr(IndexS, ElementSize,
3174                                            isInBounds ? SCEV::FlagNSW :
3175                                            SCEV::FlagAnyWrap);
3176
3177       // Add the element offset to the running total offset.
3178       TotalOffset = getAddExpr(TotalOffset, LocalOffset);
3179     }
3180   }
3181
3182   // Get the SCEV for the GEP base.
3183   const SCEV *BaseS = getSCEV(Base);
3184
3185   // Add the total offset from all the GEP indices to the base.
3186   return getAddExpr(BaseS, TotalOffset,
3187                     isInBounds ? SCEV::FlagNSW : SCEV::FlagAnyWrap);
3188 }
3189
3190 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
3191 /// guaranteed to end in (at every loop iteration).  It is, at the same time,
3192 /// the minimum number of times S is divisible by 2.  For example, given {4,+,8}
3193 /// it returns 2.  If S is guaranteed to be 0, it returns the bitwidth of S.
3194 uint32_t
3195 ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
3196   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
3197     return C->getValue()->getValue().countTrailingZeros();
3198
3199   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
3200     return std::min(GetMinTrailingZeros(T->getOperand()),
3201                     (uint32_t)getTypeSizeInBits(T->getType()));
3202
3203   if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
3204     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
3205     return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
3206              getTypeSizeInBits(E->getType()) : OpRes;
3207   }
3208
3209   if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
3210     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
3211     return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
3212              getTypeSizeInBits(E->getType()) : OpRes;
3213   }
3214
3215   if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
3216     // The result is the min of all operands results.
3217     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
3218     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
3219       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
3220     return MinOpRes;
3221   }
3222
3223   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
3224     // The result is the sum of all operands results.
3225     uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
3226     uint32_t BitWidth = getTypeSizeInBits(M->getType());
3227     for (unsigned i = 1, e = M->getNumOperands();
3228          SumOpRes != BitWidth && i != e; ++i)
3229       SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
3230                           BitWidth);
3231     return SumOpRes;
3232   }
3233
3234   if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
3235     // The result is the min of all operands results.
3236     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
3237     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
3238       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
3239     return MinOpRes;
3240   }
3241
3242   if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
3243     // The result is the min of all operands results.
3244     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
3245     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
3246       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
3247     return MinOpRes;
3248   }
3249
3250   if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
3251     // The result is the min of all operands results.
3252     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
3253     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
3254       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
3255     return MinOpRes;
3256   }
3257
3258   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
3259     // For a SCEVUnknown, ask ValueTracking.
3260     unsigned BitWidth = getTypeSizeInBits(U->getType());
3261     APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
3262     ComputeMaskedBits(U->getValue(), Zeros, Ones);
3263     return Zeros.countTrailingOnes();
3264   }
3265
3266   // SCEVUDivExpr
3267   return 0;
3268 }
3269
3270 /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
3271 ///
3272 ConstantRange
3273 ScalarEvolution::getUnsignedRange(const SCEV *S) {
3274   // See if we've computed this range already.
3275   DenseMap<const SCEV *, ConstantRange>::iterator I = UnsignedRanges.find(S);
3276   if (I != UnsignedRanges.end())
3277     return I->second;
3278
3279   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
3280     return setUnsignedRange(C, ConstantRange(C->getValue()->getValue()));
3281
3282   unsigned BitWidth = getTypeSizeInBits(S->getType());
3283   ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
3284
3285   // If the value has known zeros, the maximum unsigned value will have those
3286   // known zeros as well.
3287   uint32_t TZ = GetMinTrailingZeros(S);
3288   if (TZ != 0)
3289     ConservativeResult =
3290       ConstantRange(APInt::getMinValue(BitWidth),
3291                     APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
3292
3293   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3294     ConstantRange X = getUnsignedRange(Add->getOperand(0));
3295     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
3296       X = X.add(getUnsignedRange(Add->getOperand(i)));
3297     return setUnsignedRange(Add, ConservativeResult.intersectWith(X));
3298   }
3299
3300   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3301     ConstantRange X = getUnsignedRange(Mul->getOperand(0));
3302     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
3303       X = X.multiply(getUnsignedRange(Mul->getOperand(i)));
3304     return setUnsignedRange(Mul, ConservativeResult.intersectWith(X));
3305   }
3306
3307   if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
3308     ConstantRange X = getUnsignedRange(SMax->getOperand(0));
3309     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
3310       X = X.smax(getUnsignedRange(SMax->getOperand(i)));
3311     return setUnsignedRange(SMax, ConservativeResult.intersectWith(X));
3312   }
3313
3314   if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
3315     ConstantRange X = getUnsignedRange(UMax->getOperand(0));
3316     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
3317       X = X.umax(getUnsignedRange(UMax->getOperand(i)));
3318     return setUnsignedRange(UMax, ConservativeResult.intersectWith(X));
3319   }
3320
3321   if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
3322     ConstantRange X = getUnsignedRange(UDiv->getLHS());
3323     ConstantRange Y = getUnsignedRange(UDiv->getRHS());
3324     return setUnsignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y)));
3325   }
3326
3327   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
3328     ConstantRange X = getUnsignedRange(ZExt->getOperand());
3329     return setUnsignedRange(ZExt,
3330       ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
3331   }
3332
3333   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
3334     ConstantRange X = getUnsignedRange(SExt->getOperand());
3335     return setUnsignedRange(SExt,
3336       ConservativeResult.intersectWith(X.signExtend(BitWidth)));
3337   }
3338
3339   if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
3340     ConstantRange X = getUnsignedRange(Trunc->getOperand());
3341     return setUnsignedRange(Trunc,
3342       ConservativeResult.intersectWith(X.truncate(BitWidth)));
3343   }
3344
3345   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
3346     // If there's no unsigned wrap, the value will never be less than its
3347     // initial value.
3348     if (AddRec->getNoWrapFlags(SCEV::FlagNUW))
3349       if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart()))
3350         if (!C->getValue()->isZero())
3351           ConservativeResult =
3352             ConservativeResult.intersectWith(
3353               ConstantRange(C->getValue()->getValue(), APInt(BitWidth, 0)));
3354
3355     // TODO: non-affine addrec
3356     if (AddRec->isAffine()) {
3357       Type *Ty = AddRec->getType();
3358       const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
3359       if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
3360           getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
3361         MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
3362
3363         const SCEV *Start = AddRec->getStart();
3364         const SCEV *Step = AddRec->getStepRecurrence(*this);
3365
3366         ConstantRange StartRange = getUnsignedRange(Start);
3367         ConstantRange StepRange = getSignedRange(Step);
3368         ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount);
3369         ConstantRange EndRange =
3370           StartRange.add(MaxBECountRange.multiply(StepRange));
3371
3372         // Check for overflow. This must be done with ConstantRange arithmetic
3373         // because we could be called from within the ScalarEvolution overflow
3374         // checking code.
3375         ConstantRange ExtStartRange = StartRange.zextOrTrunc(BitWidth*2+1);
3376         ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1);
3377         ConstantRange ExtMaxBECountRange =
3378           MaxBECountRange.zextOrTrunc(BitWidth*2+1);
3379         ConstantRange ExtEndRange = EndRange.zextOrTrunc(BitWidth*2+1);
3380         if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) !=
3381             ExtEndRange)
3382           return setUnsignedRange(AddRec, ConservativeResult);
3383
3384         APInt Min = APIntOps::umin(StartRange.getUnsignedMin(),
3385                                    EndRange.getUnsignedMin());
3386         APInt Max = APIntOps::umax(StartRange.getUnsignedMax(),
3387                                    EndRange.getUnsignedMax());
3388         if (Min.isMinValue() && Max.isMaxValue())
3389           return setUnsignedRange(AddRec, ConservativeResult);
3390         return setUnsignedRange(AddRec,
3391           ConservativeResult.intersectWith(ConstantRange(Min, Max+1)));
3392       }
3393     }
3394
3395     return setUnsignedRange(AddRec, ConservativeResult);
3396   }
3397
3398   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
3399     // For a SCEVUnknown, ask ValueTracking.
3400     APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
3401     ComputeMaskedBits(U->getValue(), Zeros, Ones, TD);
3402     if (Ones == ~Zeros + 1)
3403       return setUnsignedRange(U, ConservativeResult);
3404     return setUnsignedRange(U,
3405       ConservativeResult.intersectWith(ConstantRange(Ones, ~Zeros + 1)));
3406   }
3407
3408   return setUnsignedRange(S, ConservativeResult);
3409 }
3410
3411 /// getSignedRange - Determine the signed range for a particular SCEV.
3412 ///
3413 ConstantRange
3414 ScalarEvolution::getSignedRange(const SCEV *S) {
3415   // See if we've computed this range already.
3416   DenseMap<const SCEV *, ConstantRange>::iterator I = SignedRanges.find(S);
3417   if (I != SignedRanges.end())
3418     return I->second;
3419
3420   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
3421     return setSignedRange(C, ConstantRange(C->getValue()->getValue()));
3422
3423   unsigned BitWidth = getTypeSizeInBits(S->getType());
3424   ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
3425
3426   // If the value has known zeros, the maximum signed value will have those
3427   // known zeros as well.
3428   uint32_t TZ = GetMinTrailingZeros(S);
3429   if (TZ != 0)
3430     ConservativeResult =
3431       ConstantRange(APInt::getSignedMinValue(BitWidth),
3432                     APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
3433
3434   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3435     ConstantRange X = getSignedRange(Add->getOperand(0));
3436     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
3437       X = X.add(getSignedRange(Add->getOperand(i)));
3438     return setSignedRange(Add, ConservativeResult.intersectWith(X));
3439   }
3440
3441   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3442     ConstantRange X = getSignedRange(Mul->getOperand(0));
3443     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
3444       X = X.multiply(getSignedRange(Mul->getOperand(i)));
3445     return setSignedRange(Mul, ConservativeResult.intersectWith(X));
3446   }
3447
3448   if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
3449     ConstantRange X = getSignedRange(SMax->getOperand(0));
3450     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
3451       X = X.smax(getSignedRange(SMax->getOperand(i)));
3452     return setSignedRange(SMax, ConservativeResult.intersectWith(X));
3453   }
3454
3455   if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
3456     ConstantRange X = getSignedRange(UMax->getOperand(0));
3457     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
3458       X = X.umax(getSignedRange(UMax->getOperand(i)));
3459     return setSignedRange(UMax, ConservativeResult.intersectWith(X));
3460   }
3461
3462   if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
3463     ConstantRange X = getSignedRange(UDiv->getLHS());
3464     ConstantRange Y = getSignedRange(UDiv->getRHS());
3465     return setSignedRange(UDiv, ConservativeResult.intersectWith(X.udiv(Y)));
3466   }
3467
3468   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
3469     ConstantRange X = getSignedRange(ZExt->getOperand());
3470     return setSignedRange(ZExt,
3471       ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
3472   }
3473
3474   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
3475     ConstantRange X = getSignedRange(SExt->getOperand());
3476     return setSignedRange(SExt,
3477       ConservativeResult.intersectWith(X.signExtend(BitWidth)));
3478   }
3479
3480   if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
3481     ConstantRange X = getSignedRange(Trunc->getOperand());
3482     return setSignedRange(Trunc,
3483       ConservativeResult.intersectWith(X.truncate(BitWidth)));
3484   }
3485
3486   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
3487     // If there's no signed wrap, and all the operands have the same sign or
3488     // zero, the value won't ever change sign.
3489     if (AddRec->getNoWrapFlags(SCEV::FlagNSW)) {
3490       bool AllNonNeg = true;
3491       bool AllNonPos = true;
3492       for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
3493         if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
3494         if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
3495       }
3496       if (AllNonNeg)
3497         ConservativeResult = ConservativeResult.intersectWith(
3498           ConstantRange(APInt(BitWidth, 0),
3499                         APInt::getSignedMinValue(BitWidth)));
3500       else if (AllNonPos)
3501         ConservativeResult = ConservativeResult.intersectWith(
3502           ConstantRange(APInt::getSignedMinValue(BitWidth),
3503                         APInt(BitWidth, 1)));
3504     }
3505
3506     // TODO: non-affine addrec
3507     if (AddRec->isAffine()) {
3508       Type *Ty = AddRec->getType();
3509       const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
3510       if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
3511           getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
3512         MaxBECount = getNoopOrZeroExtend(MaxBECount, Ty);
3513
3514         const SCEV *Start = AddRec->getStart();
3515         const SCEV *Step = AddRec->getStepRecurrence(*this);
3516
3517         ConstantRange StartRange = getSignedRange(Start);
3518         ConstantRange StepRange = getSignedRange(Step);
3519         ConstantRange MaxBECountRange = getUnsignedRange(MaxBECount);
3520         ConstantRange EndRange =
3521           StartRange.add(MaxBECountRange.multiply(StepRange));
3522
3523         // Check for overflow. This must be done with ConstantRange arithmetic
3524         // because we could be called from within the ScalarEvolution overflow
3525         // checking code.
3526         ConstantRange ExtStartRange = StartRange.sextOrTrunc(BitWidth*2+1);
3527         ConstantRange ExtStepRange = StepRange.sextOrTrunc(BitWidth*2+1);
3528         ConstantRange ExtMaxBECountRange =
3529           MaxBECountRange.zextOrTrunc(BitWidth*2+1);
3530         ConstantRange ExtEndRange = EndRange.sextOrTrunc(BitWidth*2+1);
3531         if (ExtStartRange.add(ExtMaxBECountRange.multiply(ExtStepRange)) !=
3532             ExtEndRange)
3533           return setSignedRange(AddRec, ConservativeResult);
3534
3535         APInt Min = APIntOps::smin(StartRange.getSignedMin(),
3536                                    EndRange.getSignedMin());
3537         APInt Max = APIntOps::smax(StartRange.getSignedMax(),
3538                                    EndRange.getSignedMax());
3539         if (Min.isMinSignedValue() && Max.isMaxSignedValue())
3540           return setSignedRange(AddRec, ConservativeResult);
3541         return setSignedRange(AddRec,
3542           ConservativeResult.intersectWith(ConstantRange(Min, Max+1)));
3543       }
3544     }
3545
3546     return setSignedRange(AddRec, ConservativeResult);
3547   }
3548
3549   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
3550     // For a SCEVUnknown, ask ValueTracking.
3551     if (!U->getValue()->getType()->isIntegerTy() && !TD)
3552       return setSignedRange(U, ConservativeResult);
3553     unsigned NS = ComputeNumSignBits(U->getValue(), TD);
3554     if (NS == 1)
3555       return setSignedRange(U, ConservativeResult);
3556     return setSignedRange(U, ConservativeResult.intersectWith(
3557       ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
3558                     APInt::getSignedMaxValue(BitWidth).ashr(NS - 1)+1)));
3559   }
3560
3561   return setSignedRange(S, ConservativeResult);
3562 }
3563
3564 /// createSCEV - We know that there is no SCEV for the specified value.
3565 /// Analyze the expression.
3566 ///
3567 const SCEV *ScalarEvolution::createSCEV(Value *V) {
3568   if (!isSCEVable(V->getType()))
3569     return getUnknown(V);
3570
3571   unsigned Opcode = Instruction::UserOp1;
3572   if (Instruction *I = dyn_cast<Instruction>(V)) {
3573     Opcode = I->getOpcode();
3574
3575     // Don't attempt to analyze instructions in blocks that aren't
3576     // reachable. Such instructions don't matter, and they aren't required
3577     // to obey basic rules for definitions dominating uses which this
3578     // analysis depends on.
3579     if (!DT->isReachableFromEntry(I->getParent()))
3580       return getUnknown(V);
3581   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
3582     Opcode = CE->getOpcode();
3583   else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
3584     return getConstant(CI);
3585   else if (isa<ConstantPointerNull>(V))
3586     return getConstant(V->getType(), 0);
3587   else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
3588     return GA->mayBeOverridden() ? getUnknown(V) : getSCEV(GA->getAliasee());
3589   else
3590     return getUnknown(V);
3591
3592   Operator *U = cast<Operator>(V);
3593   switch (Opcode) {
3594   case Instruction::Add: {
3595     // The simple thing to do would be to just call getSCEV on both operands
3596     // and call getAddExpr with the result. However if we're looking at a
3597     // bunch of things all added together, this can be quite inefficient,
3598     // because it leads to N-1 getAddExpr calls for N ultimate operands.
3599     // Instead, gather up all the operands and make a single getAddExpr call.
3600     // LLVM IR canonical form means we need only traverse the left operands.
3601     //
3602     // Don't apply this instruction's NSW or NUW flags to the new
3603     // expression. The instruction may be guarded by control flow that the
3604     // no-wrap behavior depends on. Non-control-equivalent instructions can be
3605     // mapped to the same SCEV expression, and it would be incorrect to transfer
3606     // NSW/NUW semantics to those operations.
3607     SmallVector<const SCEV *, 4> AddOps;
3608     AddOps.push_back(getSCEV(U->getOperand(1)));
3609     for (Value *Op = U->getOperand(0); ; Op = U->getOperand(0)) {
3610       unsigned Opcode = Op->getValueID() - Value::InstructionVal;
3611       if (Opcode != Instruction::Add && Opcode != Instruction::Sub)
3612         break;
3613       U = cast<Operator>(Op);
3614       const SCEV *Op1 = getSCEV(U->getOperand(1));
3615       if (Opcode == Instruction::Sub)
3616         AddOps.push_back(getNegativeSCEV(Op1));
3617       else
3618         AddOps.push_back(Op1);
3619     }
3620     AddOps.push_back(getSCEV(U->getOperand(0)));
3621     return getAddExpr(AddOps);
3622   }
3623   case Instruction::Mul: {
3624     // Don't transfer NSW/NUW for the same reason as AddExpr.
3625     SmallVector<const SCEV *, 4> MulOps;
3626     MulOps.push_back(getSCEV(U->getOperand(1)));
3627     for (Value *Op = U->getOperand(0);
3628          Op->getValueID() == Instruction::Mul + Value::InstructionVal;
3629          Op = U->getOperand(0)) {
3630       U = cast<Operator>(Op);
3631       MulOps.push_back(getSCEV(U->getOperand(1)));
3632     }
3633     MulOps.push_back(getSCEV(U->getOperand(0)));
3634     return getMulExpr(MulOps);
3635   }
3636   case Instruction::UDiv:
3637     return getUDivExpr(getSCEV(U->getOperand(0)),
3638                        getSCEV(U->getOperand(1)));
3639   case Instruction::Sub:
3640     return getMinusSCEV(getSCEV(U->getOperand(0)),
3641                         getSCEV(U->getOperand(1)));
3642   case Instruction::And:
3643     // For an expression like x&255 that merely masks off the high bits,
3644     // use zext(trunc(x)) as the SCEV expression.
3645     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
3646       if (CI->isNullValue())
3647         return getSCEV(U->getOperand(1));
3648       if (CI->isAllOnesValue())
3649         return getSCEV(U->getOperand(0));
3650       const APInt &A = CI->getValue();
3651
3652       // Instcombine's ShrinkDemandedConstant may strip bits out of
3653       // constants, obscuring what would otherwise be a low-bits mask.
3654       // Use ComputeMaskedBits to compute what ShrinkDemandedConstant
3655       // knew about to reconstruct a low-bits mask value.
3656       unsigned LZ = A.countLeadingZeros();
3657       unsigned BitWidth = A.getBitWidth();
3658       APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
3659       ComputeMaskedBits(U->getOperand(0), KnownZero, KnownOne, TD);
3660
3661       APInt EffectiveMask = APInt::getLowBitsSet(BitWidth, BitWidth - LZ);
3662
3663       if (LZ != 0 && !((~A & ~KnownZero) & EffectiveMask))
3664         return
3665           getZeroExtendExpr(getTruncateExpr(getSCEV(U->getOperand(0)),
3666                                 IntegerType::get(getContext(), BitWidth - LZ)),
3667                             U->getType());
3668     }
3669     break;
3670
3671   case Instruction::Or:
3672     // If the RHS of the Or is a constant, we may have something like:
3673     // X*4+1 which got turned into X*4|1.  Handle this as an Add so loop
3674     // optimizations will transparently handle this case.
3675     //
3676     // In order for this transformation to be safe, the LHS must be of the
3677     // form X*(2^n) and the Or constant must be less than 2^n.
3678     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
3679       const SCEV *LHS = getSCEV(U->getOperand(0));
3680       const APInt &CIVal = CI->getValue();
3681       if (GetMinTrailingZeros(LHS) >=
3682           (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
3683         // Build a plain add SCEV.
3684         const SCEV *S = getAddExpr(LHS, getSCEV(CI));
3685         // If the LHS of the add was an addrec and it has no-wrap flags,
3686         // transfer the no-wrap flags, since an or won't introduce a wrap.
3687         if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
3688           const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
3689           const_cast<SCEVAddRecExpr *>(NewAR)->setNoWrapFlags(
3690             OldAR->getNoWrapFlags());
3691         }
3692         return S;
3693       }
3694     }
3695     break;
3696   case Instruction::Xor:
3697     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
3698       // If the RHS of the xor is a signbit, then this is just an add.
3699       // Instcombine turns add of signbit into xor as a strength reduction step.
3700       if (CI->getValue().isSignBit())
3701         return getAddExpr(getSCEV(U->getOperand(0)),
3702                           getSCEV(U->getOperand(1)));
3703
3704       // If the RHS of xor is -1, then this is a not operation.
3705       if (CI->isAllOnesValue())
3706         return getNotSCEV(getSCEV(U->getOperand(0)));
3707
3708       // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
3709       // This is a variant of the check for xor with -1, and it handles
3710       // the case where instcombine has trimmed non-demanded bits out
3711       // of an xor with -1.
3712       if (BinaryOperator *BO = dyn_cast<BinaryOperator>(U->getOperand(0)))
3713         if (ConstantInt *LCI = dyn_cast<ConstantInt>(BO->getOperand(1)))
3714           if (BO->getOpcode() == Instruction::And &&
3715               LCI->getValue() == CI->getValue())
3716             if (const SCEVZeroExtendExpr *Z =
3717                   dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
3718               Type *UTy = U->getType();
3719               const SCEV *Z0 = Z->getOperand();
3720               Type *Z0Ty = Z0->getType();
3721               unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
3722
3723               // If C is a low-bits mask, the zero extend is serving to
3724               // mask off the high bits. Complement the operand and
3725               // re-apply the zext.
3726               if (APIntOps::isMask(Z0TySize, CI->getValue()))
3727                 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
3728
3729               // If C is a single bit, it may be in the sign-bit position
3730               // before the zero-extend. In this case, represent the xor
3731               // using an add, which is equivalent, and re-apply the zext.
3732               APInt Trunc = CI->getValue().trunc(Z0TySize);
3733               if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
3734                   Trunc.isSignBit())
3735                 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
3736                                          UTy);
3737             }
3738     }
3739     break;
3740
3741   case Instruction::Shl:
3742     // Turn shift left of a constant amount into a multiply.
3743     if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
3744       uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth();
3745
3746       // If the shift count is not less than the bitwidth, the result of
3747       // the shift is undefined. Don't try to analyze it, because the
3748       // resolution chosen here may differ from the resolution chosen in
3749       // other parts of the compiler.
3750       if (SA->getValue().uge(BitWidth))
3751         break;
3752
3753       Constant *X = ConstantInt::get(getContext(),
3754         APInt(BitWidth, 1).shl(SA->getZExtValue()));
3755       return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
3756     }
3757     break;
3758
3759   case Instruction::LShr:
3760     // Turn logical shift right of a constant into a unsigned divide.
3761     if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
3762       uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth();
3763
3764       // If the shift count is not less than the bitwidth, the result of
3765       // the shift is undefined. Don't try to analyze it, because the
3766       // resolution chosen here may differ from the resolution chosen in
3767       // other parts of the compiler.
3768       if (SA->getValue().uge(BitWidth))
3769         break;
3770
3771       Constant *X = ConstantInt::get(getContext(),
3772         APInt(BitWidth, 1).shl(SA->getZExtValue()));
3773       return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
3774     }
3775     break;
3776
3777   case Instruction::AShr:
3778     // For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
3779     if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
3780       if (Operator *L = dyn_cast<Operator>(U->getOperand(0)))
3781         if (L->getOpcode() == Instruction::Shl &&
3782             L->getOperand(1) == U->getOperand(1)) {
3783           uint64_t BitWidth = getTypeSizeInBits(U->getType());
3784
3785           // If the shift count is not less than the bitwidth, the result of
3786           // the shift is undefined. Don't try to analyze it, because the
3787           // resolution chosen here may differ from the resolution chosen in
3788           // other parts of the compiler.
3789           if (CI->getValue().uge(BitWidth))
3790             break;
3791
3792           uint64_t Amt = BitWidth - CI->getZExtValue();
3793           if (Amt == BitWidth)
3794             return getSCEV(L->getOperand(0));       // shift by zero --> noop
3795           return
3796             getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
3797                                               IntegerType::get(getContext(),
3798                                                                Amt)),
3799                               U->getType());
3800         }
3801     break;
3802
3803   case Instruction::Trunc:
3804     return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
3805
3806   case Instruction::ZExt:
3807     return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
3808
3809   case Instruction::SExt:
3810     return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
3811
3812   case Instruction::BitCast:
3813     // BitCasts are no-op casts so we just eliminate the cast.
3814     if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
3815       return getSCEV(U->getOperand(0));
3816     break;
3817
3818   // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can
3819   // lead to pointer expressions which cannot safely be expanded to GEPs,
3820   // because ScalarEvolution doesn't respect the GEP aliasing rules when
3821   // simplifying integer expressions.
3822
3823   case Instruction::GetElementPtr:
3824     return createNodeForGEP(cast<GEPOperator>(U));
3825
3826   case Instruction::PHI:
3827     return createNodeForPHI(cast<PHINode>(U));
3828
3829   case Instruction::Select:
3830     // This could be a smax or umax that was lowered earlier.
3831     // Try to recover it.
3832     if (ICmpInst *ICI = dyn_cast<ICmpInst>(U->getOperand(0))) {
3833       Value *LHS = ICI->getOperand(0);
3834       Value *RHS = ICI->getOperand(1);
3835       switch (ICI->getPredicate()) {
3836       case ICmpInst::ICMP_SLT:
3837       case ICmpInst::ICMP_SLE:
3838         std::swap(LHS, RHS);
3839         // fall through
3840       case ICmpInst::ICMP_SGT:
3841       case ICmpInst::ICMP_SGE:
3842         // a >s b ? a+x : b+x  ->  smax(a, b)+x
3843         // a >s b ? b+x : a+x  ->  smin(a, b)+x
3844         if (LHS->getType() == U->getType()) {
3845           const SCEV *LS = getSCEV(LHS);
3846           const SCEV *RS = getSCEV(RHS);
3847           const SCEV *LA = getSCEV(U->getOperand(1));
3848           const SCEV *RA = getSCEV(U->getOperand(2));
3849           const SCEV *LDiff = getMinusSCEV(LA, LS);
3850           const SCEV *RDiff = getMinusSCEV(RA, RS);
3851           if (LDiff == RDiff)
3852             return getAddExpr(getSMaxExpr(LS, RS), LDiff);
3853           LDiff = getMinusSCEV(LA, RS);
3854           RDiff = getMinusSCEV(RA, LS);
3855           if (LDiff == RDiff)
3856             return getAddExpr(getSMinExpr(LS, RS), LDiff);
3857         }
3858         break;
3859       case ICmpInst::ICMP_ULT:
3860       case ICmpInst::ICMP_ULE:
3861         std::swap(LHS, RHS);
3862         // fall through
3863       case ICmpInst::ICMP_UGT:
3864       case ICmpInst::ICMP_UGE:
3865         // a >u b ? a+x : b+x  ->  umax(a, b)+x
3866         // a >u b ? b+x : a+x  ->  umin(a, b)+x
3867         if (LHS->getType() == U->getType()) {
3868           const SCEV *LS = getSCEV(LHS);
3869           const SCEV *RS = getSCEV(RHS);
3870           const SCEV *LA = getSCEV(U->getOperand(1));
3871           const SCEV *RA = getSCEV(U->getOperand(2));
3872           const SCEV *LDiff = getMinusSCEV(LA, LS);
3873           const SCEV *RDiff = getMinusSCEV(RA, RS);
3874           if (LDiff == RDiff)
3875             return getAddExpr(getUMaxExpr(LS, RS), LDiff);
3876           LDiff = getMinusSCEV(LA, RS);
3877           RDiff = getMinusSCEV(RA, LS);
3878           if (LDiff == RDiff)
3879             return getAddExpr(getUMinExpr(LS, RS), LDiff);
3880         }
3881         break;
3882       case ICmpInst::ICMP_NE:
3883         // n != 0 ? n+x : 1+x  ->  umax(n, 1)+x
3884         if (LHS->getType() == U->getType() &&
3885             isa<ConstantInt>(RHS) &&
3886             cast<ConstantInt>(RHS)->isZero()) {
3887           const SCEV *One = getConstant(LHS->getType(), 1);
3888           const SCEV *LS = getSCEV(LHS);
3889           const SCEV *LA = getSCEV(U->getOperand(1));
3890           const SCEV *RA = getSCEV(U->getOperand(2));
3891           const SCEV *LDiff = getMinusSCEV(LA, LS);
3892           const SCEV *RDiff = getMinusSCEV(RA, One);
3893           if (LDiff == RDiff)
3894             return getAddExpr(getUMaxExpr(One, LS), LDiff);
3895         }
3896         break;
3897       case ICmpInst::ICMP_EQ:
3898         // n == 0 ? 1+x : n+x  ->  umax(n, 1)+x
3899         if (LHS->getType() == U->getType() &&
3900             isa<ConstantInt>(RHS) &&
3901             cast<ConstantInt>(RHS)->isZero()) {
3902           const SCEV *One = getConstant(LHS->getType(), 1);
3903           const SCEV *LS = getSCEV(LHS);
3904           const SCEV *LA = getSCEV(U->getOperand(1));
3905           const SCEV *RA = getSCEV(U->getOperand(2));
3906           const SCEV *LDiff = getMinusSCEV(LA, One);
3907           const SCEV *RDiff = getMinusSCEV(RA, LS);
3908           if (LDiff == RDiff)
3909             return getAddExpr(getUMaxExpr(One, LS), LDiff);
3910         }
3911         break;
3912       default:
3913         break;
3914       }
3915     }
3916
3917   default: // We cannot analyze this expression.
3918     break;
3919   }
3920
3921   return getUnknown(V);
3922 }
3923
3924
3925
3926 //===----------------------------------------------------------------------===//
3927 //                   Iteration Count Computation Code
3928 //
3929
3930 /// getSmallConstantTripCount - Returns the maximum trip count of this loop as a
3931 /// normal unsigned value. Returns 0 if the trip count is unknown or not
3932 /// constant. Will also return 0 if the maximum trip count is very large (>=
3933 /// 2^32).
3934 ///
3935 /// This "trip count" assumes that control exits via ExitingBlock. More
3936 /// precisely, it is the number of times that control may reach ExitingBlock
3937 /// before taking the branch. For loops with multiple exits, it may not be the
3938 /// number times that the loop header executes because the loop may exit
3939 /// prematurely via another branch.
3940 unsigned ScalarEvolution::
3941 getSmallConstantTripCount(Loop *L, BasicBlock *ExitingBlock) {
3942   const SCEVConstant *ExitCount =
3943     dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock));
3944   if (!ExitCount)
3945     return 0;
3946
3947   ConstantInt *ExitConst = ExitCount->getValue();
3948
3949   // Guard against huge trip counts.
3950   if (ExitConst->getValue().getActiveBits() > 32)
3951     return 0;
3952
3953   // In case of integer overflow, this returns 0, which is correct.
3954   return ((unsigned)ExitConst->getZExtValue()) + 1;
3955 }
3956
3957 /// getSmallConstantTripMultiple - Returns the largest constant divisor of the
3958 /// trip count of this loop as a normal unsigned value, if possible. This
3959 /// means that the actual trip count is always a multiple of the returned
3960 /// value (don't forget the trip count could very well be zero as well!).
3961 ///
3962 /// Returns 1 if the trip count is unknown or not guaranteed to be the
3963 /// multiple of a constant (which is also the case if the trip count is simply
3964 /// constant, use getSmallConstantTripCount for that case), Will also return 1
3965 /// if the trip count is very large (>= 2^32).
3966 ///
3967 /// As explained in the comments for getSmallConstantTripCount, this assumes
3968 /// that control exits the loop via ExitingBlock.
3969 unsigned ScalarEvolution::
3970 getSmallConstantTripMultiple(Loop *L, BasicBlock *ExitingBlock) {
3971   const SCEV *ExitCount = getExitCount(L, ExitingBlock);
3972   if (ExitCount == getCouldNotCompute())
3973     return 1;
3974
3975   // Get the trip count from the BE count by adding 1.
3976   const SCEV *TCMul = getAddExpr(ExitCount,
3977                                  getConstant(ExitCount->getType(), 1));
3978   // FIXME: SCEV distributes multiplication as V1*C1 + V2*C1. We could attempt
3979   // to factor simple cases.
3980   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(TCMul))
3981     TCMul = Mul->getOperand(0);
3982
3983   const SCEVConstant *MulC = dyn_cast<SCEVConstant>(TCMul);
3984   if (!MulC)
3985     return 1;
3986
3987   ConstantInt *Result = MulC->getValue();
3988
3989   // Guard against huge trip counts (this requires checking
3990   // for zero to handle the case where the trip count == -1 and the
3991   // addition wraps).
3992   if (!Result || Result->getValue().getActiveBits() > 32 ||
3993       Result->getValue().getActiveBits() == 0)
3994     return 1;
3995
3996   return (unsigned)Result->getZExtValue();
3997 }
3998
3999 // getExitCount - Get the expression for the number of loop iterations for which
4000 // this loop is guaranteed not to exit via ExitintBlock. Otherwise return
4001 // SCEVCouldNotCompute.
4002 const SCEV *ScalarEvolution::getExitCount(Loop *L, BasicBlock *ExitingBlock) {
4003   return getBackedgeTakenInfo(L).getExact(ExitingBlock, this);
4004 }
4005
4006 /// getBackedgeTakenCount - If the specified loop has a predictable
4007 /// backedge-taken count, return it, otherwise return a SCEVCouldNotCompute
4008 /// object. The backedge-taken count is the number of times the loop header
4009 /// will be branched to from within the loop. This is one less than the
4010 /// trip count of the loop, since it doesn't count the first iteration,
4011 /// when the header is branched to from outside the loop.
4012 ///
4013 /// Note that it is not valid to call this method on a loop without a
4014 /// loop-invariant backedge-taken count (see
4015 /// hasLoopInvariantBackedgeTakenCount).
4016 ///
4017 const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
4018   return getBackedgeTakenInfo(L).getExact(this);
4019 }
4020
4021 /// getMaxBackedgeTakenCount - Similar to getBackedgeTakenCount, except
4022 /// return the least SCEV value that is known never to be less than the
4023 /// actual backedge taken count.
4024 const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
4025   return getBackedgeTakenInfo(L).getMax(this);
4026 }
4027
4028 /// PushLoopPHIs - Push PHI nodes in the header of the given loop
4029 /// onto the given Worklist.
4030 static void
4031 PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
4032   BasicBlock *Header = L->getHeader();
4033
4034   // Push all Loop-header PHIs onto the Worklist stack.
4035   for (BasicBlock::iterator I = Header->begin();
4036        PHINode *PN = dyn_cast<PHINode>(I); ++I)
4037     Worklist.push_back(PN);
4038 }
4039
4040 const ScalarEvolution::BackedgeTakenInfo &
4041 ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
4042   // Initially insert an invalid entry for this loop. If the insertion
4043   // succeeds, proceed to actually compute a backedge-taken count and
4044   // update the value. The temporary CouldNotCompute value tells SCEV
4045   // code elsewhere that it shouldn't attempt to request a new
4046   // backedge-taken count, which could result in infinite recursion.
4047   std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
4048     BackedgeTakenCounts.insert(std::make_pair(L, BackedgeTakenInfo()));
4049   if (!Pair.second)
4050     return Pair.first->second;
4051
4052   // ComputeBackedgeTakenCount may allocate memory for its result. Inserting it
4053   // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
4054   // must be cleared in this scope.
4055   BackedgeTakenInfo Result = ComputeBackedgeTakenCount(L);
4056
4057   if (Result.getExact(this) != getCouldNotCompute()) {
4058     assert(isLoopInvariant(Result.getExact(this), L) &&
4059            isLoopInvariant(Result.getMax(this), L) &&
4060            "Computed backedge-taken count isn't loop invariant for loop!");
4061     ++NumTripCountsComputed;
4062   }
4063   else if (Result.getMax(this) == getCouldNotCompute() &&
4064            isa<PHINode>(L->getHeader()->begin())) {
4065     // Only count loops that have phi nodes as not being computable.
4066     ++NumTripCountsNotComputed;
4067   }
4068
4069   // Now that we know more about the trip count for this loop, forget any
4070   // existing SCEV values for PHI nodes in this loop since they are only
4071   // conservative estimates made without the benefit of trip count
4072   // information. This is similar to the code in forgetLoop, except that
4073   // it handles SCEVUnknown PHI nodes specially.
4074   if (Result.hasAnyInfo()) {
4075     SmallVector<Instruction *, 16> Worklist;
4076     PushLoopPHIs(L, Worklist);
4077
4078     SmallPtrSet<Instruction *, 8> Visited;
4079     while (!Worklist.empty()) {
4080       Instruction *I = Worklist.pop_back_val();
4081       if (!Visited.insert(I)) continue;
4082
4083       ValueExprMapType::iterator It =
4084         ValueExprMap.find_as(static_cast<Value *>(I));
4085       if (It != ValueExprMap.end()) {
4086         const SCEV *Old = It->second;
4087
4088         // SCEVUnknown for a PHI either means that it has an unrecognized
4089         // structure, or it's a PHI that's in the progress of being computed
4090         // by createNodeForPHI.  In the former case, additional loop trip
4091         // count information isn't going to change anything. In the later
4092         // case, createNodeForPHI will perform the necessary updates on its
4093         // own when it gets to that point.
4094         if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) {
4095           forgetMemoizedResults(Old);
4096           ValueExprMap.erase(It);
4097         }
4098         if (PHINode *PN = dyn_cast<PHINode>(I))
4099           ConstantEvolutionLoopExitValue.erase(PN);
4100       }
4101
4102       PushDefUseChildren(I, Worklist);
4103     }
4104   }
4105
4106   // Re-lookup the insert position, since the call to
4107   // ComputeBackedgeTakenCount above could result in a
4108   // recusive call to getBackedgeTakenInfo (on a different
4109   // loop), which would invalidate the iterator computed
4110   // earlier.
4111   return BackedgeTakenCounts.find(L)->second = Result;
4112 }
4113
4114 /// forgetLoop - This method should be called by the client when it has
4115 /// changed a loop in a way that may effect ScalarEvolution's ability to
4116 /// compute a trip count, or if the loop is deleted.
4117 void ScalarEvolution::forgetLoop(const Loop *L) {
4118   // Drop any stored trip count value.
4119   DenseMap<const Loop*, BackedgeTakenInfo>::iterator BTCPos =
4120     BackedgeTakenCounts.find(L);
4121   if (BTCPos != BackedgeTakenCounts.end()) {
4122     BTCPos->second.clear();
4123     BackedgeTakenCounts.erase(BTCPos);
4124   }
4125
4126   // Drop information about expressions based on loop-header PHIs.
4127   SmallVector<Instruction *, 16> Worklist;
4128   PushLoopPHIs(L, Worklist);
4129
4130   SmallPtrSet<Instruction *, 8> Visited;
4131   while (!Worklist.empty()) {
4132     Instruction *I = Worklist.pop_back_val();
4133     if (!Visited.insert(I)) continue;
4134
4135     ValueExprMapType::iterator It =
4136       ValueExprMap.find_as(static_cast<Value *>(I));
4137     if (It != ValueExprMap.end()) {
4138       forgetMemoizedResults(It->second);
4139       ValueExprMap.erase(It);
4140       if (PHINode *PN = dyn_cast<PHINode>(I))
4141         ConstantEvolutionLoopExitValue.erase(PN);
4142     }
4143
4144     PushDefUseChildren(I, Worklist);
4145   }
4146
4147   // Forget all contained loops too, to avoid dangling entries in the
4148   // ValuesAtScopes map.
4149   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
4150     forgetLoop(*I);
4151 }
4152
4153 /// forgetValue - This method should be called by the client when it has
4154 /// changed a value in a way that may effect its value, or which may
4155 /// disconnect it from a def-use chain linking it to a loop.
4156 void ScalarEvolution::forgetValue(Value *V) {
4157   Instruction *I = dyn_cast<Instruction>(V);
4158   if (!I) return;
4159
4160   // Drop information about expressions based on loop-header PHIs.
4161   SmallVector<Instruction *, 16> Worklist;
4162   Worklist.push_back(I);
4163
4164   SmallPtrSet<Instruction *, 8> Visited;
4165   while (!Worklist.empty()) {
4166     I = Worklist.pop_back_val();
4167     if (!Visited.insert(I)) continue;
4168
4169     ValueExprMapType::iterator It =
4170       ValueExprMap.find_as(static_cast<Value *>(I));
4171     if (It != ValueExprMap.end()) {
4172       forgetMemoizedResults(It->second);
4173       ValueExprMap.erase(It);
4174       if (PHINode *PN = dyn_cast<PHINode>(I))
4175         ConstantEvolutionLoopExitValue.erase(PN);
4176     }
4177
4178     PushDefUseChildren(I, Worklist);
4179   }
4180 }
4181
4182 /// getExact - Get the exact loop backedge taken count considering all loop
4183 /// exits. A computable result can only be return for loops with a single exit.
4184 /// Returning the minimum taken count among all exits is incorrect because one
4185 /// of the loop's exit limit's may have been skipped. HowFarToZero assumes that
4186 /// the limit of each loop test is never skipped. This is a valid assumption as
4187 /// long as the loop exits via that test. For precise results, it is the
4188 /// caller's responsibility to specify the relevant loop exit using
4189 /// getExact(ExitingBlock, SE).
4190 const SCEV *
4191 ScalarEvolution::BackedgeTakenInfo::getExact(ScalarEvolution *SE) const {
4192   // If any exits were not computable, the loop is not computable.
4193   if (!ExitNotTaken.isCompleteList()) return SE->getCouldNotCompute();
4194
4195   // We need exactly one computable exit.
4196   if (!ExitNotTaken.ExitingBlock) return SE->getCouldNotCompute();
4197   assert(ExitNotTaken.ExactNotTaken && "uninitialized not-taken info");
4198
4199   const SCEV *BECount = 0;
4200   for (const ExitNotTakenInfo *ENT = &ExitNotTaken;
4201        ENT != 0; ENT = ENT->getNextExit()) {
4202
4203     assert(ENT->ExactNotTaken != SE->getCouldNotCompute() && "bad exit SCEV");
4204
4205     if (!BECount)
4206       BECount = ENT->ExactNotTaken;
4207     else if (BECount != ENT->ExactNotTaken)
4208       return SE->getCouldNotCompute();
4209   }
4210   assert(BECount && "Invalid not taken count for loop exit");
4211   return BECount;
4212 }
4213
4214 /// getExact - Get the exact not taken count for this loop exit.
4215 const SCEV *
4216 ScalarEvolution::BackedgeTakenInfo::getExact(BasicBlock *ExitingBlock,
4217                                              ScalarEvolution *SE) const {
4218   for (const ExitNotTakenInfo *ENT = &ExitNotTaken;
4219        ENT != 0; ENT = ENT->getNextExit()) {
4220
4221     if (ENT->ExitingBlock == ExitingBlock)
4222       return ENT->ExactNotTaken;
4223   }
4224   return SE->getCouldNotCompute();
4225 }
4226
4227 /// getMax - Get the max backedge taken count for the loop.
4228 const SCEV *
4229 ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE) const {
4230   return Max ? Max : SE->getCouldNotCompute();
4231 }
4232
4233 bool ScalarEvolution::BackedgeTakenInfo::hasOperand(const SCEV *S,
4234                                                     ScalarEvolution *SE) const {
4235   if (Max && Max != SE->getCouldNotCompute() && SE->hasOperand(Max, S))
4236     return true;
4237
4238   if (!ExitNotTaken.ExitingBlock)
4239     return false;
4240
4241   for (const ExitNotTakenInfo *ENT = &ExitNotTaken;
4242        ENT != 0; ENT = ENT->getNextExit()) {
4243
4244     if (ENT->ExactNotTaken != SE->getCouldNotCompute()
4245         && SE->hasOperand(ENT->ExactNotTaken, S)) {
4246       return true;
4247     }
4248   }
4249   return false;
4250 }
4251
4252 /// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
4253 /// computable exit into a persistent ExitNotTakenInfo array.
4254 ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
4255   SmallVectorImpl< std::pair<BasicBlock *, const SCEV *> > &ExitCounts,
4256   bool Complete, const SCEV *MaxCount) : Max(MaxCount) {
4257
4258   if (!Complete)
4259     ExitNotTaken.setIncomplete();
4260
4261   unsigned NumExits = ExitCounts.size();
4262   if (NumExits == 0) return;
4263
4264   ExitNotTaken.ExitingBlock = ExitCounts[0].first;
4265   ExitNotTaken.ExactNotTaken = ExitCounts[0].second;
4266   if (NumExits == 1) return;
4267
4268   // Handle the rare case of multiple computable exits.
4269   ExitNotTakenInfo *ENT = new ExitNotTakenInfo[NumExits-1];
4270
4271   ExitNotTakenInfo *PrevENT = &ExitNotTaken;
4272   for (unsigned i = 1; i < NumExits; ++i, PrevENT = ENT, ++ENT) {
4273     PrevENT->setNextExit(ENT);
4274     ENT->ExitingBlock = ExitCounts[i].first;
4275     ENT->ExactNotTaken = ExitCounts[i].second;
4276   }
4277 }
4278
4279 /// clear - Invalidate this result and free the ExitNotTakenInfo array.
4280 void ScalarEvolution::BackedgeTakenInfo::clear() {
4281   ExitNotTaken.ExitingBlock = 0;
4282   ExitNotTaken.ExactNotTaken = 0;
4283   delete[] ExitNotTaken.getNextExit();
4284 }
4285
4286 /// ComputeBackedgeTakenCount - Compute the number of times the backedge
4287 /// of the specified loop will execute.
4288 ScalarEvolution::BackedgeTakenInfo
4289 ScalarEvolution::ComputeBackedgeTakenCount(const Loop *L) {
4290   SmallVector<BasicBlock *, 8> ExitingBlocks;
4291   L->getExitingBlocks(ExitingBlocks);
4292
4293   // Examine all exits and pick the most conservative values.
4294   const SCEV *MaxBECount = getCouldNotCompute();
4295   bool CouldComputeBECount = true;
4296   SmallVector<std::pair<BasicBlock *, const SCEV *>, 4> ExitCounts;
4297   for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
4298     ExitLimit EL = ComputeExitLimit(L, ExitingBlocks[i]);
4299     if (EL.Exact == getCouldNotCompute())
4300       // We couldn't compute an exact value for this exit, so
4301       // we won't be able to compute an exact value for the loop.
4302       CouldComputeBECount = false;
4303     else
4304       ExitCounts.push_back(std::make_pair(ExitingBlocks[i], EL.Exact));
4305
4306     if (MaxBECount == getCouldNotCompute())
4307       MaxBECount = EL.Max;
4308     else if (EL.Max != getCouldNotCompute()) {
4309       // We cannot take the "min" MaxBECount, because non-unit stride loops may
4310       // skip some loop tests. Taking the max over the exits is sufficiently
4311       // conservative.  TODO: We could do better taking into consideration
4312       // that (1) the loop has unit stride (2) the last loop test is
4313       // less-than/greater-than (3) any loop test is less-than/greater-than AND
4314       // falls-through some constant times less then the other tests.
4315       MaxBECount = getUMaxFromMismatchedTypes(MaxBECount, EL.Max);
4316     }
4317   }
4318
4319   return BackedgeTakenInfo(ExitCounts, CouldComputeBECount, MaxBECount);
4320 }
4321
4322 /// ComputeExitLimit - Compute the number of times the backedge of the specified
4323 /// loop will execute if it exits via the specified block.
4324 ScalarEvolution::ExitLimit
4325 ScalarEvolution::ComputeExitLimit(const Loop *L, BasicBlock *ExitingBlock) {
4326
4327   // Okay, we've chosen an exiting block.  See what condition causes us to
4328   // exit at this block.
4329   //
4330   // FIXME: we should be able to handle switch instructions (with a single exit)
4331   BranchInst *ExitBr = dyn_cast<BranchInst>(ExitingBlock->getTerminator());
4332   if (ExitBr == 0) return getCouldNotCompute();
4333   assert(ExitBr->isConditional() && "If unconditional, it can't be in loop!");
4334
4335   // At this point, we know we have a conditional branch that determines whether
4336   // the loop is exited.  However, we don't know if the branch is executed each
4337   // time through the loop.  If not, then the execution count of the branch will
4338   // not be equal to the trip count of the loop.
4339   //
4340   // Currently we check for this by checking to see if the Exit branch goes to
4341   // the loop header.  If so, we know it will always execute the same number of
4342   // times as the loop.  We also handle the case where the exit block *is* the
4343   // loop header.  This is common for un-rotated loops.
4344   //
4345   // If both of those tests fail, walk up the unique predecessor chain to the
4346   // header, stopping if there is an edge that doesn't exit the loop. If the
4347   // header is reached, the execution count of the branch will be equal to the
4348   // trip count of the loop.
4349   //
4350   //  More extensive analysis could be done to handle more cases here.
4351   //
4352   if (ExitBr->getSuccessor(0) != L->getHeader() &&
4353       ExitBr->getSuccessor(1) != L->getHeader() &&
4354       ExitBr->getParent() != L->getHeader()) {
4355     // The simple checks failed, try climbing the unique predecessor chain
4356     // up to the header.
4357     bool Ok = false;
4358     for (BasicBlock *BB = ExitBr->getParent(); BB; ) {
4359       BasicBlock *Pred = BB->getUniquePredecessor();
4360       if (!Pred)
4361         return getCouldNotCompute();
4362       TerminatorInst *PredTerm = Pred->getTerminator();
4363       for (unsigned i = 0, e = PredTerm->getNumSuccessors(); i != e; ++i) {
4364         BasicBlock *PredSucc = PredTerm->getSuccessor(i);
4365         if (PredSucc == BB)
4366           continue;
4367         // If the predecessor has a successor that isn't BB and isn't
4368         // outside the loop, assume the worst.
4369         if (L->contains(PredSucc))
4370           return getCouldNotCompute();
4371       }
4372       if (Pred == L->getHeader()) {
4373         Ok = true;
4374         break;
4375       }
4376       BB = Pred;
4377     }
4378     if (!Ok)
4379       return getCouldNotCompute();
4380   }
4381
4382   // Proceed to the next level to examine the exit condition expression.
4383   return ComputeExitLimitFromCond(L, ExitBr->getCondition(),
4384                                   ExitBr->getSuccessor(0),
4385                                   ExitBr->getSuccessor(1));
4386 }
4387
4388 /// ComputeExitLimitFromCond - Compute the number of times the
4389 /// backedge of the specified loop will execute if its exit condition
4390 /// were a conditional branch of ExitCond, TBB, and FBB.
4391 ScalarEvolution::ExitLimit
4392 ScalarEvolution::ComputeExitLimitFromCond(const Loop *L,
4393                                           Value *ExitCond,
4394                                           BasicBlock *TBB,
4395                                           BasicBlock *FBB) {
4396   // Check if the controlling expression for this loop is an And or Or.
4397   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
4398     if (BO->getOpcode() == Instruction::And) {
4399       // Recurse on the operands of the and.
4400       ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB);
4401       ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB);
4402       const SCEV *BECount = getCouldNotCompute();
4403       const SCEV *MaxBECount = getCouldNotCompute();
4404       if (L->contains(TBB)) {
4405         // Both conditions must be true for the loop to continue executing.
4406         // Choose the less conservative count.
4407         if (EL0.Exact == getCouldNotCompute() ||
4408             EL1.Exact == getCouldNotCompute())
4409           BECount = getCouldNotCompute();
4410         else
4411           BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact);
4412         if (EL0.Max == getCouldNotCompute())
4413           MaxBECount = EL1.Max;
4414         else if (EL1.Max == getCouldNotCompute())
4415           MaxBECount = EL0.Max;
4416         else
4417           MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max);
4418       } else {
4419         // Both conditions must be true at the same time for the loop to exit.
4420         // For now, be conservative.
4421         assert(L->contains(FBB) && "Loop block has no successor in loop!");
4422         if (EL0.Max == EL1.Max)
4423           MaxBECount = EL0.Max;
4424         if (EL0.Exact == EL1.Exact)
4425           BECount = EL0.Exact;
4426       }
4427
4428       return ExitLimit(BECount, MaxBECount);
4429     }
4430     if (BO->getOpcode() == Instruction::Or) {
4431       // Recurse on the operands of the or.
4432       ExitLimit EL0 = ComputeExitLimitFromCond(L, BO->getOperand(0), TBB, FBB);
4433       ExitLimit EL1 = ComputeExitLimitFromCond(L, BO->getOperand(1), TBB, FBB);
4434       const SCEV *BECount = getCouldNotCompute();
4435       const SCEV *MaxBECount = getCouldNotCompute();
4436       if (L->contains(FBB)) {
4437         // Both conditions must be false for the loop to continue executing.
4438         // Choose the less conservative count.
4439         if (EL0.Exact == getCouldNotCompute() ||
4440             EL1.Exact == getCouldNotCompute())
4441           BECount = getCouldNotCompute();
4442         else
4443           BECount = getUMinFromMismatchedTypes(EL0.Exact, EL1.Exact);
4444         if (EL0.Max == getCouldNotCompute())
4445           MaxBECount = EL1.Max;
4446         else if (EL1.Max == getCouldNotCompute())
4447           MaxBECount = EL0.Max;
4448         else
4449           MaxBECount = getUMinFromMismatchedTypes(EL0.Max, EL1.Max);
4450       } else {
4451         // Both conditions must be false at the same time for the loop to exit.
4452         // For now, be conservative.
4453         assert(L->contains(TBB) && "Loop block has no successor in loop!");
4454         if (EL0.Max == EL1.Max)
4455           MaxBECount = EL0.Max;
4456         if (EL0.Exact == EL1.Exact)
4457           BECount = EL0.Exact;
4458       }
4459
4460       return ExitLimit(BECount, MaxBECount);
4461     }
4462   }
4463
4464   // With an icmp, it may be feasible to compute an exact backedge-taken count.
4465   // Proceed to the next level to examine the icmp.
4466   if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond))
4467     return ComputeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB);
4468
4469   // Check for a constant condition. These are normally stripped out by
4470   // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
4471   // preserve the CFG and is temporarily leaving constant conditions
4472   // in place.
4473   if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
4474     if (L->contains(FBB) == !CI->getZExtValue())
4475       // The backedge is always taken.
4476       return getCouldNotCompute();
4477     else
4478       // The backedge is never taken.
4479       return getConstant(CI->getType(), 0);
4480   }
4481
4482   // If it's not an integer or pointer comparison then compute it the hard way.
4483   return ComputeExitCountExhaustively(L, ExitCond, !L->contains(TBB));
4484 }
4485
4486 /// ComputeExitLimitFromICmp - Compute the number of times the
4487 /// backedge of the specified loop will execute if its exit condition
4488 /// were a conditional branch of the ICmpInst ExitCond, TBB, and FBB.
4489 ScalarEvolution::ExitLimit
4490 ScalarEvolution::ComputeExitLimitFromICmp(const Loop *L,
4491                                           ICmpInst *ExitCond,
4492                                           BasicBlock *TBB,
4493                                           BasicBlock *FBB) {
4494
4495   // If the condition was exit on true, convert the condition to exit on false
4496   ICmpInst::Predicate Cond;
4497   if (!L->contains(FBB))
4498     Cond = ExitCond->getPredicate();
4499   else
4500     Cond = ExitCond->getInversePredicate();
4501
4502   // Handle common loops like: for (X = "string"; *X; ++X)
4503   if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
4504     if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
4505       ExitLimit ItCnt =
4506         ComputeLoadConstantCompareExitLimit(LI, RHS, L, Cond);
4507       if (ItCnt.hasAnyInfo())
4508         return ItCnt;
4509     }
4510
4511   const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
4512   const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
4513
4514   // Try to evaluate any dependencies out of the loop.
4515   LHS = getSCEVAtScope(LHS, L);
4516   RHS = getSCEVAtScope(RHS, L);
4517
4518   // At this point, we would like to compute how many iterations of the
4519   // loop the predicate will return true for these inputs.
4520   if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) {
4521     // If there is a loop-invariant, force it into the RHS.
4522     std::swap(LHS, RHS);
4523     Cond = ICmpInst::getSwappedPredicate(Cond);
4524   }
4525
4526   // Simplify the operands before analyzing them.
4527   (void)SimplifyICmpOperands(Cond, LHS, RHS);
4528
4529   // If we have a comparison of a chrec against a constant, try to use value
4530   // ranges to answer this query.
4531   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
4532     if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
4533       if (AddRec->getLoop() == L) {
4534         // Form the constant range.
4535         ConstantRange CompRange(
4536             ICmpInst::makeConstantRange(Cond, RHSC->getValue()->getValue()));
4537
4538         const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
4539         if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
4540       }
4541
4542   switch (Cond) {
4543   case ICmpInst::ICMP_NE: {                     // while (X != Y)
4544     // Convert to: while (X-Y != 0)
4545     ExitLimit EL = HowFarToZero(getMinusSCEV(LHS, RHS), L);
4546     if (EL.hasAnyInfo()) return EL;
4547     break;
4548   }
4549   case ICmpInst::ICMP_EQ: {                     // while (X == Y)
4550     // Convert to: while (X-Y == 0)
4551     ExitLimit EL = HowFarToNonZero(getMinusSCEV(LHS, RHS), L);
4552     if (EL.hasAnyInfo()) return EL;
4553     break;
4554   }
4555   case ICmpInst::ICMP_SLT: {
4556     ExitLimit EL = HowManyLessThans(LHS, RHS, L, true);
4557     if (EL.hasAnyInfo()) return EL;
4558     break;
4559   }
4560   case ICmpInst::ICMP_SGT: {
4561     ExitLimit EL = HowManyLessThans(getNotSCEV(LHS),
4562                                              getNotSCEV(RHS), L, true);
4563     if (EL.hasAnyInfo()) return EL;
4564     break;
4565   }
4566   case ICmpInst::ICMP_ULT: {
4567     ExitLimit EL = HowManyLessThans(LHS, RHS, L, false);
4568     if (EL.hasAnyInfo()) return EL;
4569     break;
4570   }
4571   case ICmpInst::ICMP_UGT: {
4572     ExitLimit EL = HowManyLessThans(getNotSCEV(LHS),
4573                                              getNotSCEV(RHS), L, false);
4574     if (EL.hasAnyInfo()) return EL;
4575     break;
4576   }
4577   default:
4578 #if 0
4579     dbgs() << "ComputeBackedgeTakenCount ";
4580     if (ExitCond->getOperand(0)->getType()->isUnsigned())
4581       dbgs() << "[unsigned] ";
4582     dbgs() << *LHS << "   "
4583          << Instruction::getOpcodeName(Instruction::ICmp)
4584          << "   " << *RHS << "\n";
4585 #endif
4586     break;
4587   }
4588   return ComputeExitCountExhaustively(L, ExitCond, !L->contains(TBB));
4589 }
4590
4591 static ConstantInt *
4592 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
4593                                 ScalarEvolution &SE) {
4594   const SCEV *InVal = SE.getConstant(C);
4595   const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
4596   assert(isa<SCEVConstant>(Val) &&
4597          "Evaluation of SCEV at constant didn't fold correctly?");
4598   return cast<SCEVConstant>(Val)->getValue();
4599 }
4600
4601 /// ComputeLoadConstantCompareExitLimit - Given an exit condition of
4602 /// 'icmp op load X, cst', try to see if we can compute the backedge
4603 /// execution count.
4604 ScalarEvolution::ExitLimit
4605 ScalarEvolution::ComputeLoadConstantCompareExitLimit(
4606   LoadInst *LI,
4607   Constant *RHS,
4608   const Loop *L,
4609   ICmpInst::Predicate predicate) {
4610
4611   if (LI->isVolatile()) return getCouldNotCompute();
4612
4613   // Check to see if the loaded pointer is a getelementptr of a global.
4614   // TODO: Use SCEV instead of manually grubbing with GEPs.
4615   GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
4616   if (!GEP) return getCouldNotCompute();
4617
4618   // Make sure that it is really a constant global we are gepping, with an
4619   // initializer, and make sure the first IDX is really 0.
4620   GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
4621   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
4622       GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
4623       !cast<Constant>(GEP->getOperand(1))->isNullValue())
4624     return getCouldNotCompute();
4625
4626   // Okay, we allow one non-constant index into the GEP instruction.
4627   Value *VarIdx = 0;
4628   std::vector<Constant*> Indexes;
4629   unsigned VarIdxNum = 0;
4630   for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
4631     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
4632       Indexes.push_back(CI);
4633     } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
4634       if (VarIdx) return getCouldNotCompute();  // Multiple non-constant idx's.
4635       VarIdx = GEP->getOperand(i);
4636       VarIdxNum = i-2;
4637       Indexes.push_back(0);
4638     }
4639
4640   // Loop-invariant loads may be a byproduct of loop optimization. Skip them.
4641   if (!VarIdx)
4642     return getCouldNotCompute();
4643
4644   // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
4645   // Check to see if X is a loop variant variable value now.
4646   const SCEV *Idx = getSCEV(VarIdx);
4647   Idx = getSCEVAtScope(Idx, L);
4648
4649   // We can only recognize very limited forms of loop index expressions, in
4650   // particular, only affine AddRec's like {C1,+,C2}.
4651   const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
4652   if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) ||
4653       !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
4654       !isa<SCEVConstant>(IdxExpr->getOperand(1)))
4655     return getCouldNotCompute();
4656
4657   unsigned MaxSteps = MaxBruteForceIterations;
4658   for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
4659     ConstantInt *ItCst = ConstantInt::get(
4660                            cast<IntegerType>(IdxExpr->getType()), IterationNum);
4661     ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
4662
4663     // Form the GEP offset.
4664     Indexes[VarIdxNum] = Val;
4665
4666     Constant *Result = ConstantFoldLoadThroughGEPIndices(GV->getInitializer(),
4667                                                          Indexes);
4668     if (Result == 0) break;  // Cannot compute!
4669
4670     // Evaluate the condition for this iteration.
4671     Result = ConstantExpr::getICmp(predicate, Result, RHS);
4672     if (!isa<ConstantInt>(Result)) break;  // Couldn't decide for sure
4673     if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
4674 #if 0
4675       dbgs() << "\n***\n*** Computed loop count " << *ItCst
4676              << "\n*** From global " << *GV << "*** BB: " << *L->getHeader()
4677              << "***\n";
4678 #endif
4679       ++NumArrayLenItCounts;
4680       return getConstant(ItCst);   // Found terminating iteration!
4681     }
4682   }
4683   return getCouldNotCompute();
4684 }
4685
4686
4687 /// CanConstantFold - Return true if we can constant fold an instruction of the
4688 /// specified type, assuming that all operands were constants.
4689 static bool CanConstantFold(const Instruction *I) {
4690   if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
4691       isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
4692       isa<LoadInst>(I))
4693     return true;
4694
4695   if (const CallInst *CI = dyn_cast<CallInst>(I))
4696     if (const Function *F = CI->getCalledFunction())
4697       return canConstantFoldCallTo(F);
4698   return false;
4699 }
4700
4701 /// Determine whether this instruction can constant evolve within this loop
4702 /// assuming its operands can all constant evolve.
4703 static bool canConstantEvolve(Instruction *I, const Loop *L) {
4704   // An instruction outside of the loop can't be derived from a loop PHI.
4705   if (!L->contains(I)) return false;
4706
4707   if (isa<PHINode>(I)) {
4708     if (L->getHeader() == I->getParent())
4709       return true;
4710     else
4711       // We don't currently keep track of the control flow needed to evaluate
4712       // PHIs, so we cannot handle PHIs inside of loops.
4713       return false;
4714   }
4715
4716   // If we won't be able to constant fold this expression even if the operands
4717   // are constants, bail early.
4718   return CanConstantFold(I);
4719 }
4720
4721 /// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
4722 /// recursing through each instruction operand until reaching a loop header phi.
4723 static PHINode *
4724 getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L,
4725                                DenseMap<Instruction *, PHINode *> &PHIMap) {
4726
4727   // Otherwise, we can evaluate this instruction if all of its operands are
4728   // constant or derived from a PHI node themselves.
4729   PHINode *PHI = 0;
4730   for (Instruction::op_iterator OpI = UseInst->op_begin(),
4731          OpE = UseInst->op_end(); OpI != OpE; ++OpI) {
4732
4733     if (isa<Constant>(*OpI)) continue;
4734
4735     Instruction *OpInst = dyn_cast<Instruction>(*OpI);
4736     if (!OpInst || !canConstantEvolve(OpInst, L)) return 0;
4737
4738     PHINode *P = dyn_cast<PHINode>(OpInst);
4739     if (!P)
4740       // If this operand is already visited, reuse the prior result.
4741       // We may have P != PHI if this is the deepest point at which the
4742       // inconsistent paths meet.
4743       P = PHIMap.lookup(OpInst);
4744     if (!P) {
4745       // Recurse and memoize the results, whether a phi is found or not.
4746       // This recursive call invalidates pointers into PHIMap.
4747       P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap);
4748       PHIMap[OpInst] = P;
4749     }
4750     if (P == 0) return 0;        // Not evolving from PHI
4751     if (PHI && PHI != P) return 0;  // Evolving from multiple different PHIs.
4752     PHI = P;
4753   }
4754   // This is a expression evolving from a constant PHI!
4755   return PHI;
4756 }
4757
4758 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
4759 /// in the loop that V is derived from.  We allow arbitrary operations along the
4760 /// way, but the operands of an operation must either be constants or a value
4761 /// derived from a constant PHI.  If this expression does not fit with these
4762 /// constraints, return null.
4763 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
4764   Instruction *I = dyn_cast<Instruction>(V);
4765   if (I == 0 || !canConstantEvolve(I, L)) return 0;
4766
4767   if (PHINode *PN = dyn_cast<PHINode>(I)) {
4768     return PN;
4769   }
4770
4771   // Record non-constant instructions contained by the loop.
4772   DenseMap<Instruction *, PHINode *> PHIMap;
4773   return getConstantEvolvingPHIOperands(I, L, PHIMap);
4774 }
4775
4776 /// EvaluateExpression - Given an expression that passes the
4777 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
4778 /// in the loop has the value PHIVal.  If we can't fold this expression for some
4779 /// reason, return null.
4780 static Constant *EvaluateExpression(Value *V, const Loop *L,
4781                                     DenseMap<Instruction *, Constant *> &Vals,
4782                                     const DataLayout *TD,
4783                                     const TargetLibraryInfo *TLI) {
4784   // Convenient constant check, but redundant for recursive calls.
4785   if (Constant *C = dyn_cast<Constant>(V)) return C;
4786   Instruction *I = dyn_cast<Instruction>(V);
4787   if (!I) return 0;
4788
4789   if (Constant *C = Vals.lookup(I)) return C;
4790
4791   // An instruction inside the loop depends on a value outside the loop that we
4792   // weren't given a mapping for, or a value such as a call inside the loop.
4793   if (!canConstantEvolve(I, L)) return 0;
4794
4795   // An unmapped PHI can be due to a branch or another loop inside this loop,
4796   // or due to this not being the initial iteration through a loop where we
4797   // couldn't compute the evolution of this particular PHI last time.
4798   if (isa<PHINode>(I)) return 0;
4799
4800   std::vector<Constant*> Operands(I->getNumOperands());
4801
4802   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
4803     Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i));
4804     if (!Operand) {
4805       Operands[i] = dyn_cast<Constant>(I->getOperand(i));
4806       if (!Operands[i]) return 0;
4807       continue;
4808     }
4809     Constant *C = EvaluateExpression(Operand, L, Vals, TD, TLI);
4810     Vals[Operand] = C;
4811     if (!C) return 0;
4812     Operands[i] = C;
4813   }
4814
4815   if (CmpInst *CI = dyn_cast<CmpInst>(I))
4816     return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
4817                                            Operands[1], TD, TLI);
4818   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
4819     if (!LI->isVolatile())
4820       return ConstantFoldLoadFromConstPtr(Operands[0], TD);
4821   }
4822   return ConstantFoldInstOperands(I->getOpcode(), I->getType(), Operands, TD,
4823                                   TLI);
4824 }
4825
4826 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
4827 /// in the header of its containing loop, we know the loop executes a
4828 /// constant number of times, and the PHI node is just a recurrence
4829 /// involving constants, fold it.
4830 Constant *
4831 ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
4832                                                    const APInt &BEs,
4833                                                    const Loop *L) {
4834   DenseMap<PHINode*, Constant*>::const_iterator I =
4835     ConstantEvolutionLoopExitValue.find(PN);
4836   if (I != ConstantEvolutionLoopExitValue.end())
4837     return I->second;
4838
4839   if (BEs.ugt(MaxBruteForceIterations))
4840     return ConstantEvolutionLoopExitValue[PN] = 0;  // Not going to evaluate it.
4841
4842   Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
4843
4844   DenseMap<Instruction *, Constant *> CurrentIterVals;
4845   BasicBlock *Header = L->getHeader();
4846   assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
4847
4848   // Since the loop is canonicalized, the PHI node must have two entries.  One
4849   // entry must be a constant (coming in from outside of the loop), and the
4850   // second must be derived from the same PHI.
4851   bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
4852   PHINode *PHI = 0;
4853   for (BasicBlock::iterator I = Header->begin();
4854        (PHI = dyn_cast<PHINode>(I)); ++I) {
4855     Constant *StartCST =
4856       dyn_cast<Constant>(PHI->getIncomingValue(!SecondIsBackedge));
4857     if (StartCST == 0) continue;
4858     CurrentIterVals[PHI] = StartCST;
4859   }
4860   if (!CurrentIterVals.count(PN))
4861     return RetVal = 0;
4862
4863   Value *BEValue = PN->getIncomingValue(SecondIsBackedge);
4864
4865   // Execute the loop symbolically to determine the exit value.
4866   if (BEs.getActiveBits() >= 32)
4867     return RetVal = 0; // More than 2^32-1 iterations?? Not doing it!
4868
4869   unsigned NumIterations = BEs.getZExtValue(); // must be in range
4870   unsigned IterationNum = 0;
4871   for (; ; ++IterationNum) {
4872     if (IterationNum == NumIterations)
4873       return RetVal = CurrentIterVals[PN];  // Got exit value!
4874
4875     // Compute the value of the PHIs for the next iteration.
4876     // EvaluateExpression adds non-phi values to the CurrentIterVals map.
4877     DenseMap<Instruction *, Constant *> NextIterVals;
4878     Constant *NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD,
4879                                            TLI);
4880     if (NextPHI == 0)
4881       return 0;        // Couldn't evaluate!
4882     NextIterVals[PN] = NextPHI;
4883
4884     bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
4885
4886     // Also evaluate the other PHI nodes.  However, we don't get to stop if we
4887     // cease to be able to evaluate one of them or if they stop evolving,
4888     // because that doesn't necessarily prevent us from computing PN.
4889     SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute;
4890     for (DenseMap<Instruction *, Constant *>::const_iterator
4891            I = CurrentIterVals.begin(), E = CurrentIterVals.end(); I != E; ++I){
4892       PHINode *PHI = dyn_cast<PHINode>(I->first);
4893       if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
4894       PHIsToCompute.push_back(std::make_pair(PHI, I->second));
4895     }
4896     // We use two distinct loops because EvaluateExpression may invalidate any
4897     // iterators into CurrentIterVals.
4898     for (SmallVectorImpl<std::pair<PHINode *, Constant*> >::const_iterator
4899              I = PHIsToCompute.begin(), E = PHIsToCompute.end(); I != E; ++I) {
4900       PHINode *PHI = I->first;
4901       Constant *&NextPHI = NextIterVals[PHI];
4902       if (!NextPHI) {   // Not already computed.
4903         Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4904         NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD, TLI);
4905       }
4906       if (NextPHI != I->second)
4907         StoppedEvolving = false;
4908     }
4909
4910     // If all entries in CurrentIterVals == NextIterVals then we can stop
4911     // iterating, the loop can't continue to change.
4912     if (StoppedEvolving)
4913       return RetVal = CurrentIterVals[PN];
4914
4915     CurrentIterVals.swap(NextIterVals);
4916   }
4917 }
4918
4919 /// ComputeExitCountExhaustively - If the loop is known to execute a
4920 /// constant number of times (the condition evolves only from constants),
4921 /// try to evaluate a few iterations of the loop until we get the exit
4922 /// condition gets a value of ExitWhen (true or false).  If we cannot
4923 /// evaluate the trip count of the loop, return getCouldNotCompute().
4924 const SCEV *ScalarEvolution::ComputeExitCountExhaustively(const Loop *L,
4925                                                           Value *Cond,
4926                                                           bool ExitWhen) {
4927   PHINode *PN = getConstantEvolvingPHI(Cond, L);
4928   if (PN == 0) return getCouldNotCompute();
4929
4930   // If the loop is canonicalized, the PHI will have exactly two entries.
4931   // That's the only form we support here.
4932   if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
4933
4934   DenseMap<Instruction *, Constant *> CurrentIterVals;
4935   BasicBlock *Header = L->getHeader();
4936   assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
4937
4938   // One entry must be a constant (coming in from outside of the loop), and the
4939   // second must be derived from the same PHI.
4940   bool SecondIsBackedge = L->contains(PN->getIncomingBlock(1));
4941   PHINode *PHI = 0;
4942   for (BasicBlock::iterator I = Header->begin();
4943        (PHI = dyn_cast<PHINode>(I)); ++I) {
4944     Constant *StartCST =
4945       dyn_cast<Constant>(PHI->getIncomingValue(!SecondIsBackedge));
4946     if (StartCST == 0) continue;
4947     CurrentIterVals[PHI] = StartCST;
4948   }
4949   if (!CurrentIterVals.count(PN))
4950     return getCouldNotCompute();
4951
4952   // Okay, we find a PHI node that defines the trip count of this loop.  Execute
4953   // the loop symbolically to determine when the condition gets a value of
4954   // "ExitWhen".
4955
4956   unsigned MaxIterations = MaxBruteForceIterations;   // Limit analysis.
4957   for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
4958     ConstantInt *CondVal =
4959       dyn_cast_or_null<ConstantInt>(EvaluateExpression(Cond, L, CurrentIterVals,
4960                                                        TD, TLI));
4961
4962     // Couldn't symbolically evaluate.
4963     if (!CondVal) return getCouldNotCompute();
4964
4965     if (CondVal->getValue() == uint64_t(ExitWhen)) {
4966       ++NumBruteForceTripCountsComputed;
4967       return getConstant(Type::getInt32Ty(getContext()), IterationNum);
4968     }
4969
4970     // Update all the PHI nodes for the next iteration.
4971     DenseMap<Instruction *, Constant *> NextIterVals;
4972
4973     // Create a list of which PHIs we need to compute. We want to do this before
4974     // calling EvaluateExpression on them because that may invalidate iterators
4975     // into CurrentIterVals.
4976     SmallVector<PHINode *, 8> PHIsToCompute;
4977     for (DenseMap<Instruction *, Constant *>::const_iterator
4978            I = CurrentIterVals.begin(), E = CurrentIterVals.end(); I != E; ++I){
4979       PHINode *PHI = dyn_cast<PHINode>(I->first);
4980       if (!PHI || PHI->getParent() != Header) continue;
4981       PHIsToCompute.push_back(PHI);
4982     }
4983     for (SmallVectorImpl<PHINode *>::const_iterator I = PHIsToCompute.begin(),
4984              E = PHIsToCompute.end(); I != E; ++I) {
4985       PHINode *PHI = *I;
4986       Constant *&NextPHI = NextIterVals[PHI];
4987       if (NextPHI) continue;    // Already computed!
4988
4989       Value *BEValue = PHI->getIncomingValue(SecondIsBackedge);
4990       NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, TD, TLI);
4991     }
4992     CurrentIterVals.swap(NextIterVals);
4993   }
4994
4995   // Too many iterations were needed to evaluate.
4996   return getCouldNotCompute();
4997 }
4998
4999 /// getSCEVAtScope - Return a SCEV expression for the specified value
5000 /// at the specified scope in the program.  The L value specifies a loop
5001 /// nest to evaluate the expression at, where null is the top-level or a
5002 /// specified loop is immediately inside of the loop.
5003 ///
5004 /// This method can be used to compute the exit value for a variable defined
5005 /// in a loop by querying what the value will hold in the parent loop.
5006 ///
5007 /// In the case that a relevant loop exit value cannot be computed, the
5008 /// original value V is returned.
5009 const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
5010   // Check to see if we've folded this expression at this loop before.
5011   std::map<const Loop *, const SCEV *> &Values = ValuesAtScopes[V];
5012   std::pair<std::map<const Loop *, const SCEV *>::iterator, bool> Pair =
5013     Values.insert(std::make_pair(L, static_cast<const SCEV *>(0)));
5014   if (!Pair.second)
5015     return Pair.first->second ? Pair.first->second : V;
5016
5017   // Otherwise compute it.
5018   const SCEV *C = computeSCEVAtScope(V, L);
5019   ValuesAtScopes[V][L] = C;
5020   return C;
5021 }
5022
5023 /// This builds up a Constant using the ConstantExpr interface.  That way, we
5024 /// will return Constants for objects which aren't represented by a
5025 /// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
5026 /// Returns NULL if the SCEV isn't representable as a Constant.
5027 static Constant *BuildConstantFromSCEV(const SCEV *V) {
5028   switch (V->getSCEVType()) {
5029     default:  // TODO: smax, umax.
5030     case scCouldNotCompute:
5031     case scAddRecExpr:
5032       break;
5033     case scConstant:
5034       return cast<SCEVConstant>(V)->getValue();
5035     case scUnknown:
5036       return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
5037     case scSignExtend: {
5038       const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
5039       if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
5040         return ConstantExpr::getSExt(CastOp, SS->getType());
5041       break;
5042     }
5043     case scZeroExtend: {
5044       const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
5045       if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
5046         return ConstantExpr::getZExt(CastOp, SZ->getType());
5047       break;
5048     }
5049     case scTruncate: {
5050       const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
5051       if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
5052         return ConstantExpr::getTrunc(CastOp, ST->getType());
5053       break;
5054     }
5055     case scAddExpr: {
5056       const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
5057       if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
5058         if (C->getType()->isPointerTy())
5059           C = ConstantExpr::getBitCast(C, Type::getInt8PtrTy(C->getContext()));
5060         for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
5061           Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
5062           if (!C2) return 0;
5063
5064           // First pointer!
5065           if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
5066             std::swap(C, C2);
5067             // The offsets have been converted to bytes.  We can add bytes to an
5068             // i8* by GEP with the byte count in the first index.
5069             C = ConstantExpr::getBitCast(C,Type::getInt8PtrTy(C->getContext()));
5070           }
5071
5072           // Don't bother trying to sum two pointers. We probably can't
5073           // statically compute a load that results from it anyway.
5074           if (C2->getType()->isPointerTy())
5075             return 0;
5076
5077           if (C->getType()->isPointerTy()) {
5078             if (cast<PointerType>(C->getType())->getElementType()->isStructTy())
5079               C2 = ConstantExpr::getIntegerCast(
5080                   C2, Type::getInt32Ty(C->getContext()), true);
5081             C = ConstantExpr::getGetElementPtr(C, C2);
5082           } else
5083             C = ConstantExpr::getAdd(C, C2);
5084         }
5085         return C;
5086       }
5087       break;
5088     }
5089     case scMulExpr: {
5090       const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
5091       if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
5092         // Don't bother with pointers at all.
5093         if (C->getType()->isPointerTy()) return 0;
5094         for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
5095           Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
5096           if (!C2 || C2->getType()->isPointerTy()) return 0;
5097           C = ConstantExpr::getMul(C, C2);
5098         }
5099         return C;
5100       }
5101       break;
5102     }
5103     case scUDivExpr: {
5104       const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
5105       if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
5106         if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
5107           if (LHS->getType() == RHS->getType())
5108             return ConstantExpr::getUDiv(LHS, RHS);
5109       break;
5110     }
5111   }
5112   return 0;
5113 }
5114
5115 const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
5116   if (isa<SCEVConstant>(V)) return V;
5117
5118   // If this instruction is evolved from a constant-evolving PHI, compute the
5119   // exit value from the loop without using SCEVs.
5120   if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
5121     if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
5122       const Loop *LI = (*this->LI)[I->getParent()];
5123       if (LI && LI->getParentLoop() == L)  // Looking for loop exit value.
5124         if (PHINode *PN = dyn_cast<PHINode>(I))
5125           if (PN->getParent() == LI->getHeader()) {
5126             // Okay, there is no closed form solution for the PHI node.  Check
5127             // to see if the loop that contains it has a known backedge-taken
5128             // count.  If so, we may be able to force computation of the exit
5129             // value.
5130             const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
5131             if (const SCEVConstant *BTCC =
5132                   dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
5133               // Okay, we know how many times the containing loop executes.  If
5134               // this is a constant evolving PHI node, get the final value at
5135               // the specified iteration number.
5136               Constant *RV = getConstantEvolutionLoopExitValue(PN,
5137                                                    BTCC->getValue()->getValue(),
5138                                                                LI);
5139               if (RV) return getSCEV(RV);
5140             }
5141           }
5142
5143       // Okay, this is an expression that we cannot symbolically evaluate
5144       // into a SCEV.  Check to see if it's possible to symbolically evaluate
5145       // the arguments into constants, and if so, try to constant propagate the
5146       // result.  This is particularly useful for computing loop exit values.
5147       if (CanConstantFold(I)) {
5148         SmallVector<Constant *, 4> Operands;
5149         bool MadeImprovement = false;
5150         for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
5151           Value *Op = I->getOperand(i);
5152           if (Constant *C = dyn_cast<Constant>(Op)) {
5153             Operands.push_back(C);
5154             continue;
5155           }
5156
5157           // If any of the operands is non-constant and if they are
5158           // non-integer and non-pointer, don't even try to analyze them
5159           // with scev techniques.
5160           if (!isSCEVable(Op->getType()))
5161             return V;
5162
5163           const SCEV *OrigV = getSCEV(Op);
5164           const SCEV *OpV = getSCEVAtScope(OrigV, L);
5165           MadeImprovement |= OrigV != OpV;
5166
5167           Constant *C = BuildConstantFromSCEV(OpV);
5168           if (!C) return V;
5169           if (C->getType() != Op->getType())
5170             C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
5171                                                               Op->getType(),
5172                                                               false),
5173                                       C, Op->getType());
5174           Operands.push_back(C);
5175         }
5176
5177         // Check to see if getSCEVAtScope actually made an improvement.
5178         if (MadeImprovement) {
5179           Constant *C = 0;
5180           if (const CmpInst *CI = dyn_cast<CmpInst>(I))
5181             C = ConstantFoldCompareInstOperands(CI->getPredicate(),
5182                                                 Operands[0], Operands[1], TD,
5183                                                 TLI);
5184           else if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
5185             if (!LI->isVolatile())
5186               C = ConstantFoldLoadFromConstPtr(Operands[0], TD);
5187           } else
5188             C = ConstantFoldInstOperands(I->getOpcode(), I->getType(),
5189                                          Operands, TD, TLI);
5190           if (!C) return V;
5191           return getSCEV(C);
5192         }
5193       }
5194     }
5195
5196     // This is some other type of SCEVUnknown, just return it.
5197     return V;
5198   }
5199
5200   if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
5201     // Avoid performing the look-up in the common case where the specified
5202     // expression has no loop-variant portions.
5203     for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
5204       const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
5205       if (OpAtScope != Comm->getOperand(i)) {
5206         // Okay, at least one of these operands is loop variant but might be
5207         // foldable.  Build a new instance of the folded commutative expression.
5208         SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
5209                                             Comm->op_begin()+i);
5210         NewOps.push_back(OpAtScope);
5211
5212         for (++i; i != e; ++i) {
5213           OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
5214           NewOps.push_back(OpAtScope);
5215         }
5216         if (isa<SCEVAddExpr>(Comm))
5217           return getAddExpr(NewOps);
5218         if (isa<SCEVMulExpr>(Comm))
5219           return getMulExpr(NewOps);
5220         if (isa<SCEVSMaxExpr>(Comm))
5221           return getSMaxExpr(NewOps);
5222         if (isa<SCEVUMaxExpr>(Comm))
5223           return getUMaxExpr(NewOps);
5224         llvm_unreachable("Unknown commutative SCEV type!");
5225       }
5226     }
5227     // If we got here, all operands are loop invariant.
5228     return Comm;
5229   }
5230
5231   if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
5232     const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
5233     const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
5234     if (LHS == Div->getLHS() && RHS == Div->getRHS())
5235       return Div;   // must be loop invariant
5236     return getUDivExpr(LHS, RHS);
5237   }
5238
5239   // If this is a loop recurrence for a loop that does not contain L, then we
5240   // are dealing with the final value computed by the loop.
5241   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
5242     // First, attempt to evaluate each operand.
5243     // Avoid performing the look-up in the common case where the specified
5244     // expression has no loop-variant portions.
5245     for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
5246       const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L);
5247       if (OpAtScope == AddRec->getOperand(i))
5248         continue;
5249
5250       // Okay, at least one of these operands is loop variant but might be
5251       // foldable.  Build a new instance of the folded commutative expression.
5252       SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(),
5253                                           AddRec->op_begin()+i);
5254       NewOps.push_back(OpAtScope);
5255       for (++i; i != e; ++i)
5256         NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L));
5257
5258       const SCEV *FoldedRec =
5259         getAddRecExpr(NewOps, AddRec->getLoop(),
5260                       AddRec->getNoWrapFlags(SCEV::FlagNW));
5261       AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec);
5262       // The addrec may be folded to a nonrecurrence, for example, if the
5263       // induction variable is multiplied by zero after constant folding. Go
5264       // ahead and return the folded value.
5265       if (!AddRec)
5266         return FoldedRec;
5267       break;
5268     }
5269
5270     // If the scope is outside the addrec's loop, evaluate it by using the
5271     // loop exit value of the addrec.
5272     if (!AddRec->getLoop()->contains(L)) {
5273       // To evaluate this recurrence, we need to know how many times the AddRec
5274       // loop iterates.  Compute this now.
5275       const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
5276       if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
5277
5278       // Then, evaluate the AddRec.
5279       return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
5280     }
5281
5282     return AddRec;
5283   }
5284
5285   if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
5286     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
5287     if (Op == Cast->getOperand())
5288       return Cast;  // must be loop invariant
5289     return getZeroExtendExpr(Op, Cast->getType());
5290   }
5291
5292   if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
5293     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
5294     if (Op == Cast->getOperand())
5295       return Cast;  // must be loop invariant
5296     return getSignExtendExpr(Op, Cast->getType());
5297   }
5298
5299   if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
5300     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
5301     if (Op == Cast->getOperand())
5302       return Cast;  // must be loop invariant
5303     return getTruncateExpr(Op, Cast->getType());
5304   }
5305
5306   llvm_unreachable("Unknown SCEV type!");
5307 }
5308
5309 /// getSCEVAtScope - This is a convenience function which does
5310 /// getSCEVAtScope(getSCEV(V), L).
5311 const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
5312   return getSCEVAtScope(getSCEV(V), L);
5313 }
5314
5315 /// SolveLinEquationWithOverflow - Finds the minimum unsigned root of the
5316 /// following equation:
5317 ///
5318 ///     A * X = B (mod N)
5319 ///
5320 /// where N = 2^BW and BW is the common bit width of A and B. The signedness of
5321 /// A and B isn't important.
5322 ///
5323 /// If the equation does not have a solution, SCEVCouldNotCompute is returned.
5324 static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const APInt &B,
5325                                                ScalarEvolution &SE) {
5326   uint32_t BW = A.getBitWidth();
5327   assert(BW == B.getBitWidth() && "Bit widths must be the same.");
5328   assert(A != 0 && "A must be non-zero.");
5329
5330   // 1. D = gcd(A, N)
5331   //
5332   // The gcd of A and N may have only one prime factor: 2. The number of
5333   // trailing zeros in A is its multiplicity
5334   uint32_t Mult2 = A.countTrailingZeros();
5335   // D = 2^Mult2
5336
5337   // 2. Check if B is divisible by D.
5338   //
5339   // B is divisible by D if and only if the multiplicity of prime factor 2 for B
5340   // is not less than multiplicity of this prime factor for D.
5341   if (B.countTrailingZeros() < Mult2)
5342     return SE.getCouldNotCompute();
5343
5344   // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
5345   // modulo (N / D).
5346   //
5347   // (N / D) may need BW+1 bits in its representation.  Hence, we'll use this
5348   // bit width during computations.
5349   APInt AD = A.lshr(Mult2).zext(BW + 1);  // AD = A / D
5350   APInt Mod(BW + 1, 0);
5351   Mod.setBit(BW - Mult2);  // Mod = N / D
5352   APInt I = AD.multiplicativeInverse(Mod);
5353
5354   // 4. Compute the minimum unsigned root of the equation:
5355   // I * (B / D) mod (N / D)
5356   APInt Result = (I * B.lshr(Mult2).zext(BW + 1)).urem(Mod);
5357
5358   // The result is guaranteed to be less than 2^BW so we may truncate it to BW
5359   // bits.
5360   return SE.getConstant(Result.trunc(BW));
5361 }
5362
5363 /// SolveQuadraticEquation - Find the roots of the quadratic equation for the
5364 /// given quadratic chrec {L,+,M,+,N}.  This returns either the two roots (which
5365 /// might be the same) or two SCEVCouldNotCompute objects.
5366 ///
5367 static std::pair<const SCEV *,const SCEV *>
5368 SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
5369   assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
5370   const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
5371   const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
5372   const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
5373
5374   // We currently can only solve this if the coefficients are constants.
5375   if (!LC || !MC || !NC) {
5376     const SCEV *CNC = SE.getCouldNotCompute();
5377     return std::make_pair(CNC, CNC);
5378   }
5379
5380   uint32_t BitWidth = LC->getValue()->getValue().getBitWidth();
5381   const APInt &L = LC->getValue()->getValue();
5382   const APInt &M = MC->getValue()->getValue();
5383   const APInt &N = NC->getValue()->getValue();
5384   APInt Two(BitWidth, 2);
5385   APInt Four(BitWidth, 4);
5386
5387   {
5388     using namespace APIntOps;
5389     const APInt& C = L;
5390     // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
5391     // The B coefficient is M-N/2
5392     APInt B(M);
5393     B -= sdiv(N,Two);
5394
5395     // The A coefficient is N/2
5396     APInt A(N.sdiv(Two));
5397
5398     // Compute the B^2-4ac term.
5399     APInt SqrtTerm(B);
5400     SqrtTerm *= B;
5401     SqrtTerm -= Four * (A * C);
5402
5403     if (SqrtTerm.isNegative()) {
5404       // The loop is provably infinite.
5405       const SCEV *CNC = SE.getCouldNotCompute();
5406       return std::make_pair(CNC, CNC);
5407     }
5408
5409     // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
5410     // integer value or else APInt::sqrt() will assert.
5411     APInt SqrtVal(SqrtTerm.sqrt());
5412
5413     // Compute the two solutions for the quadratic formula.
5414     // The divisions must be performed as signed divisions.
5415     APInt NegB(-B);
5416     APInt TwoA(A << 1);
5417     if (TwoA.isMinValue()) {
5418       const SCEV *CNC = SE.getCouldNotCompute();
5419       return std::make_pair(CNC, CNC);
5420     }
5421
5422     LLVMContext &Context = SE.getContext();
5423
5424     ConstantInt *Solution1 =
5425       ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
5426     ConstantInt *Solution2 =
5427       ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
5428
5429     return std::make_pair(SE.getConstant(Solution1),
5430                           SE.getConstant(Solution2));
5431   } // end APIntOps namespace
5432 }
5433
5434 /// HowFarToZero - Return the number of times a backedge comparing the specified
5435 /// value to zero will execute.  If not computable, return CouldNotCompute.
5436 ///
5437 /// This is only used for loops with a "x != y" exit test. The exit condition is
5438 /// now expressed as a single expression, V = x-y. So the exit test is
5439 /// effectively V != 0.  We know and take advantage of the fact that this
5440 /// expression only being used in a comparison by zero context.
5441 ScalarEvolution::ExitLimit
5442 ScalarEvolution::HowFarToZero(const SCEV *V, const Loop *L) {
5443   // If the value is a constant
5444   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
5445     // If the value is already zero, the branch will execute zero times.
5446     if (C->getValue()->isZero()) return C;
5447     return getCouldNotCompute();  // Otherwise it will loop infinitely.
5448   }
5449
5450   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
5451   if (!AddRec || AddRec->getLoop() != L)
5452     return getCouldNotCompute();
5453
5454   // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
5455   // the quadratic equation to solve it.
5456   if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
5457     std::pair<const SCEV *,const SCEV *> Roots =
5458       SolveQuadraticEquation(AddRec, *this);
5459     const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
5460     const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
5461     if (R1 && R2) {
5462 #if 0
5463       dbgs() << "HFTZ: " << *V << " - sol#1: " << *R1
5464              << "  sol#2: " << *R2 << "\n";
5465 #endif
5466       // Pick the smallest positive root value.
5467       if (ConstantInt *CB =
5468           dyn_cast<ConstantInt>(ConstantExpr::getICmp(CmpInst::ICMP_ULT,
5469                                                       R1->getValue(),
5470                                                       R2->getValue()))) {
5471         if (CB->getZExtValue() == false)
5472           std::swap(R1, R2);   // R1 is the minimum root now.
5473
5474         // We can only use this value if the chrec ends up with an exact zero
5475         // value at this index.  When solving for "X*X != 5", for example, we
5476         // should not accept a root of 2.
5477         const SCEV *Val = AddRec->evaluateAtIteration(R1, *this);
5478         if (Val->isZero())
5479           return R1;  // We found a quadratic root!
5480       }
5481     }
5482     return getCouldNotCompute();
5483   }
5484
5485   // Otherwise we can only handle this if it is affine.
5486   if (!AddRec->isAffine())
5487     return getCouldNotCompute();
5488
5489   // If this is an affine expression, the execution count of this branch is
5490   // the minimum unsigned root of the following equation:
5491   //
5492   //     Start + Step*N = 0 (mod 2^BW)
5493   //
5494   // equivalent to:
5495   //
5496   //             Step*N = -Start (mod 2^BW)
5497   //
5498   // where BW is the common bit width of Start and Step.
5499
5500   // Get the initial value for the loop.
5501   const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
5502   const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
5503
5504   // For now we handle only constant steps.
5505   //
5506   // TODO: Handle a nonconstant Step given AddRec<NUW>. If the
5507   // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap
5508   // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step.
5509   // We have not yet seen any such cases.
5510   const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
5511   if (StepC == 0 || StepC->getValue()->equalsInt(0))
5512     return getCouldNotCompute();
5513
5514   // For positive steps (counting up until unsigned overflow):
5515   //   N = -Start/Step (as unsigned)
5516   // For negative steps (counting down to zero):
5517   //   N = Start/-Step
5518   // First compute the unsigned distance from zero in the direction of Step.
5519   bool CountDown = StepC->getValue()->getValue().isNegative();
5520   const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start);
5521
5522   // Handle unitary steps, which cannot wraparound.
5523   // 1*N = -Start; -1*N = Start (mod 2^BW), so:
5524   //   N = Distance (as unsigned)
5525   if (StepC->getValue()->equalsInt(1) || StepC->getValue()->isAllOnesValue()) {
5526     ConstantRange CR = getUnsignedRange(Start);
5527     const SCEV *MaxBECount;
5528     if (!CountDown && CR.getUnsignedMin().isMinValue())
5529       // When counting up, the worst starting value is 1, not 0.
5530       MaxBECount = CR.getUnsignedMax().isMinValue()
5531         ? getConstant(APInt::getMinValue(CR.getBitWidth()))
5532         : getConstant(APInt::getMaxValue(CR.getBitWidth()));
5533     else
5534       MaxBECount = getConstant(CountDown ? CR.getUnsignedMax()
5535                                          : -CR.getUnsignedMin());
5536     return ExitLimit(Distance, MaxBECount);
5537   }
5538
5539   // If the recurrence is known not to wraparound, unsigned divide computes the
5540   // back edge count. We know that the value will either become zero (and thus
5541   // the loop terminates), that the loop will terminate through some other exit
5542   // condition first, or that the loop has undefined behavior.  This means
5543   // we can't "miss" the exit value, even with nonunit stride.
5544   //
5545   // FIXME: Prove that loops always exhibits *acceptable* undefined
5546   // behavior. Loops must exhibit defined behavior until a wrapped value is
5547   // actually used. So the trip count computed by udiv could be smaller than the
5548   // number of well-defined iterations.
5549   if (AddRec->getNoWrapFlags(SCEV::FlagNW)) {
5550     // FIXME: We really want an "isexact" bit for udiv.
5551     return getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
5552   }
5553   // Then, try to solve the above equation provided that Start is constant.
5554   if (const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start))
5555     return SolveLinEquationWithOverflow(StepC->getValue()->getValue(),
5556                                         -StartC->getValue()->getValue(),
5557                                         *this);
5558   return getCouldNotCompute();
5559 }
5560
5561 /// HowFarToNonZero - Return the number of times a backedge checking the
5562 /// specified value for nonzero will execute.  If not computable, return
5563 /// CouldNotCompute
5564 ScalarEvolution::ExitLimit
5565 ScalarEvolution::HowFarToNonZero(const SCEV *V, const Loop *L) {
5566   // Loops that look like: while (X == 0) are very strange indeed.  We don't
5567   // handle them yet except for the trivial case.  This could be expanded in the
5568   // future as needed.
5569
5570   // If the value is a constant, check to see if it is known to be non-zero
5571   // already.  If so, the backedge will execute zero times.
5572   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
5573     if (!C->getValue()->isNullValue())
5574       return getConstant(C->getType(), 0);
5575     return getCouldNotCompute();  // Otherwise it will loop infinitely.
5576   }
5577
5578   // We could implement others, but I really doubt anyone writes loops like
5579   // this, and if they did, they would already be constant folded.
5580   return getCouldNotCompute();
5581 }
5582
5583 /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
5584 /// (which may not be an immediate predecessor) which has exactly one
5585 /// successor from which BB is reachable, or null if no such block is
5586 /// found.
5587 ///
5588 std::pair<BasicBlock *, BasicBlock *>
5589 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
5590   // If the block has a unique predecessor, then there is no path from the
5591   // predecessor to the block that does not go through the direct edge
5592   // from the predecessor to the block.
5593   if (BasicBlock *Pred = BB->getSinglePredecessor())
5594     return std::make_pair(Pred, BB);
5595
5596   // A loop's header is defined to be a block that dominates the loop.
5597   // If the header has a unique predecessor outside the loop, it must be
5598   // a block that has exactly one successor that can reach the loop.
5599   if (Loop *L = LI->getLoopFor(BB))
5600     return std::make_pair(L->getLoopPredecessor(), L->getHeader());
5601
5602   return std::pair<BasicBlock *, BasicBlock *>();
5603 }
5604
5605 /// HasSameValue - SCEV structural equivalence is usually sufficient for
5606 /// testing whether two expressions are equal, however for the purposes of
5607 /// looking for a condition guarding a loop, it can be useful to be a little
5608 /// more general, since a front-end may have replicated the controlling
5609 /// expression.
5610 ///
5611 static bool HasSameValue(const SCEV *A, const SCEV *B) {
5612   // Quick check to see if they are the same SCEV.
5613   if (A == B) return true;
5614
5615   // Otherwise, if they're both SCEVUnknown, it's possible that they hold
5616   // two different instructions with the same value. Check for this case.
5617   if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
5618     if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
5619       if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
5620         if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
5621           if (AI->isIdenticalTo(BI) && !AI->mayReadFromMemory())
5622             return true;
5623
5624   // Otherwise assume they may have a different value.
5625   return false;
5626 }
5627
5628 /// SimplifyICmpOperands - Simplify LHS and RHS in a comparison with
5629 /// predicate Pred. Return true iff any changes were made.
5630 ///
5631 bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
5632                                            const SCEV *&LHS, const SCEV *&RHS,
5633                                            unsigned Depth) {
5634   bool Changed = false;
5635
5636   // If we hit the max recursion limit bail out.
5637   if (Depth >= 3)
5638     return false;
5639
5640   // Canonicalize a constant to the right side.
5641   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
5642     // Check for both operands constant.
5643     if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
5644       if (ConstantExpr::getICmp(Pred,
5645                                 LHSC->getValue(),
5646                                 RHSC->getValue())->isNullValue())
5647         goto trivially_false;
5648       else
5649         goto trivially_true;
5650     }
5651     // Otherwise swap the operands to put the constant on the right.
5652     std::swap(LHS, RHS);
5653     Pred = ICmpInst::getSwappedPredicate(Pred);
5654     Changed = true;
5655   }
5656
5657   // If we're comparing an addrec with a value which is loop-invariant in the
5658   // addrec's loop, put the addrec on the left. Also make a dominance check,
5659   // as both operands could be addrecs loop-invariant in each other's loop.
5660   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) {
5661     const Loop *L = AR->getLoop();
5662     if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) {
5663       std::swap(LHS, RHS);
5664       Pred = ICmpInst::getSwappedPredicate(Pred);
5665       Changed = true;
5666     }
5667   }
5668
5669   // If there's a constant operand, canonicalize comparisons with boundary
5670   // cases, and canonicalize *-or-equal comparisons to regular comparisons.
5671   if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
5672     const APInt &RA = RC->getValue()->getValue();
5673     switch (Pred) {
5674     default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
5675     case ICmpInst::ICMP_EQ:
5676     case ICmpInst::ICMP_NE:
5677       // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
5678       if (!RA)
5679         if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
5680           if (const SCEVMulExpr *ME = dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
5681             if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
5682                 ME->getOperand(0)->isAllOnesValue()) {
5683               RHS = AE->getOperand(1);
5684               LHS = ME->getOperand(1);
5685               Changed = true;
5686             }
5687       break;
5688     case ICmpInst::ICMP_UGE:
5689       if ((RA - 1).isMinValue()) {
5690         Pred = ICmpInst::ICMP_NE;
5691         RHS = getConstant(RA - 1);
5692         Changed = true;
5693         break;
5694       }
5695       if (RA.isMaxValue()) {
5696         Pred = ICmpInst::ICMP_EQ;
5697         Changed = true;
5698         break;
5699       }
5700       if (RA.isMinValue()) goto trivially_true;
5701
5702       Pred = ICmpInst::ICMP_UGT;
5703       RHS = getConstant(RA - 1);
5704       Changed = true;
5705       break;
5706     case ICmpInst::ICMP_ULE:
5707       if ((RA + 1).isMaxValue()) {
5708         Pred = ICmpInst::ICMP_NE;
5709         RHS = getConstant(RA + 1);
5710         Changed = true;
5711         break;
5712       }
5713       if (RA.isMinValue()) {
5714         Pred = ICmpInst::ICMP_EQ;
5715         Changed = true;
5716         break;
5717       }
5718       if (RA.isMaxValue()) goto trivially_true;
5719
5720       Pred = ICmpInst::ICMP_ULT;
5721       RHS = getConstant(RA + 1);
5722       Changed = true;
5723       break;
5724     case ICmpInst::ICMP_SGE:
5725       if ((RA - 1).isMinSignedValue()) {
5726         Pred = ICmpInst::ICMP_NE;
5727         RHS = getConstant(RA - 1);
5728         Changed = true;
5729         break;
5730       }
5731       if (RA.isMaxSignedValue()) {
5732         Pred = ICmpInst::ICMP_EQ;
5733         Changed = true;
5734         break;
5735       }
5736       if (RA.isMinSignedValue()) goto trivially_true;
5737
5738       Pred = ICmpInst::ICMP_SGT;
5739       RHS = getConstant(RA - 1);
5740       Changed = true;
5741       break;
5742     case ICmpInst::ICMP_SLE:
5743       if ((RA + 1).isMaxSignedValue()) {
5744         Pred = ICmpInst::ICMP_NE;
5745         RHS = getConstant(RA + 1);
5746         Changed = true;
5747         break;
5748       }
5749       if (RA.isMinSignedValue()) {
5750         Pred = ICmpInst::ICMP_EQ;
5751         Changed = true;
5752         break;
5753       }
5754       if (RA.isMaxSignedValue()) goto trivially_true;
5755
5756       Pred = ICmpInst::ICMP_SLT;
5757       RHS = getConstant(RA + 1);
5758       Changed = true;
5759       break;
5760     case ICmpInst::ICMP_UGT:
5761       if (RA.isMinValue()) {
5762         Pred = ICmpInst::ICMP_NE;
5763         Changed = true;
5764         break;
5765       }
5766       if ((RA + 1).isMaxValue()) {
5767         Pred = ICmpInst::ICMP_EQ;
5768         RHS = getConstant(RA + 1);
5769         Changed = true;
5770         break;
5771       }
5772       if (RA.isMaxValue()) goto trivially_false;
5773       break;
5774     case ICmpInst::ICMP_ULT:
5775       if (RA.isMaxValue()) {
5776         Pred = ICmpInst::ICMP_NE;
5777         Changed = true;
5778         break;
5779       }
5780       if ((RA - 1).isMinValue()) {
5781         Pred = ICmpInst::ICMP_EQ;
5782         RHS = getConstant(RA - 1);
5783         Changed = true;
5784         break;
5785       }
5786       if (RA.isMinValue()) goto trivially_false;
5787       break;
5788     case ICmpInst::ICMP_SGT:
5789       if (RA.isMinSignedValue()) {
5790         Pred = ICmpInst::ICMP_NE;
5791         Changed = true;
5792         break;
5793       }
5794       if ((RA + 1).isMaxSignedValue()) {
5795         Pred = ICmpInst::ICMP_EQ;
5796         RHS = getConstant(RA + 1);
5797         Changed = true;
5798         break;
5799       }
5800       if (RA.isMaxSignedValue()) goto trivially_false;
5801       break;
5802     case ICmpInst::ICMP_SLT:
5803       if (RA.isMaxSignedValue()) {
5804         Pred = ICmpInst::ICMP_NE;
5805         Changed = true;
5806         break;
5807       }
5808       if ((RA - 1).isMinSignedValue()) {
5809        Pred = ICmpInst::ICMP_EQ;
5810        RHS = getConstant(RA - 1);
5811         Changed = true;
5812        break;
5813       }
5814       if (RA.isMinSignedValue()) goto trivially_false;
5815       break;
5816     }
5817   }
5818
5819   // Check for obvious equality.
5820   if (HasSameValue(LHS, RHS)) {
5821     if (ICmpInst::isTrueWhenEqual(Pred))
5822       goto trivially_true;
5823     if (ICmpInst::isFalseWhenEqual(Pred))
5824       goto trivially_false;
5825   }
5826
5827   // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
5828   // adding or subtracting 1 from one of the operands.
5829   switch (Pred) {
5830   case ICmpInst::ICMP_SLE:
5831     if (!getSignedRange(RHS).getSignedMax().isMaxSignedValue()) {
5832       RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
5833                        SCEV::FlagNSW);
5834       Pred = ICmpInst::ICMP_SLT;
5835       Changed = true;
5836     } else if (!getSignedRange(LHS).getSignedMin().isMinSignedValue()) {
5837       LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS,
5838                        SCEV::FlagNSW);
5839       Pred = ICmpInst::ICMP_SLT;
5840       Changed = true;
5841     }
5842     break;
5843   case ICmpInst::ICMP_SGE:
5844     if (!getSignedRange(RHS).getSignedMin().isMinSignedValue()) {
5845       RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS,
5846                        SCEV::FlagNSW);
5847       Pred = ICmpInst::ICMP_SGT;
5848       Changed = true;
5849     } else if (!getSignedRange(LHS).getSignedMax().isMaxSignedValue()) {
5850       LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
5851                        SCEV::FlagNSW);
5852       Pred = ICmpInst::ICMP_SGT;
5853       Changed = true;
5854     }
5855     break;
5856   case ICmpInst::ICMP_ULE:
5857     if (!getUnsignedRange(RHS).getUnsignedMax().isMaxValue()) {
5858       RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
5859                        SCEV::FlagNUW);
5860       Pred = ICmpInst::ICMP_ULT;
5861       Changed = true;
5862     } else if (!getUnsignedRange(LHS).getUnsignedMin().isMinValue()) {
5863       LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS,
5864                        SCEV::FlagNUW);
5865       Pred = ICmpInst::ICMP_ULT;
5866       Changed = true;
5867     }
5868     break;
5869   case ICmpInst::ICMP_UGE:
5870     if (!getUnsignedRange(RHS).getUnsignedMin().isMinValue()) {
5871       RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS,
5872                        SCEV::FlagNUW);
5873       Pred = ICmpInst::ICMP_UGT;
5874       Changed = true;
5875     } else if (!getUnsignedRange(LHS).getUnsignedMax().isMaxValue()) {
5876       LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
5877                        SCEV::FlagNUW);
5878       Pred = ICmpInst::ICMP_UGT;
5879       Changed = true;
5880     }
5881     break;
5882   default:
5883     break;
5884   }
5885
5886   // TODO: More simplifications are possible here.
5887
5888   // Recursively simplify until we either hit a recursion limit or nothing
5889   // changes.
5890   if (Changed)
5891     return SimplifyICmpOperands(Pred, LHS, RHS, Depth+1);
5892
5893   return Changed;
5894
5895 trivially_true:
5896   // Return 0 == 0.
5897   LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
5898   Pred = ICmpInst::ICMP_EQ;
5899   return true;
5900
5901 trivially_false:
5902   // Return 0 != 0.
5903   LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
5904   Pred = ICmpInst::ICMP_NE;
5905   return true;
5906 }
5907
5908 bool ScalarEvolution::isKnownNegative(const SCEV *S) {
5909   return getSignedRange(S).getSignedMax().isNegative();
5910 }
5911
5912 bool ScalarEvolution::isKnownPositive(const SCEV *S) {
5913   return getSignedRange(S).getSignedMin().isStrictlyPositive();
5914 }
5915
5916 bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
5917   return !getSignedRange(S).getSignedMin().isNegative();
5918 }
5919
5920 bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
5921   return !getSignedRange(S).getSignedMax().isStrictlyPositive();
5922 }
5923
5924 bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
5925   return isKnownNegative(S) || isKnownPositive(S);
5926 }
5927
5928 bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
5929                                        const SCEV *LHS, const SCEV *RHS) {
5930   // Canonicalize the inputs first.
5931   (void)SimplifyICmpOperands(Pred, LHS, RHS);
5932
5933   // If LHS or RHS is an addrec, check to see if the condition is true in
5934   // every iteration of the loop.
5935   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
5936     if (isLoopEntryGuardedByCond(
5937           AR->getLoop(), Pred, AR->getStart(), RHS) &&
5938         isLoopBackedgeGuardedByCond(
5939           AR->getLoop(), Pred, AR->getPostIncExpr(*this), RHS))
5940       return true;
5941   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS))
5942     if (isLoopEntryGuardedByCond(
5943           AR->getLoop(), Pred, LHS, AR->getStart()) &&
5944         isLoopBackedgeGuardedByCond(
5945           AR->getLoop(), Pred, LHS, AR->getPostIncExpr(*this)))
5946       return true;
5947
5948   // Otherwise see what can be done with known constant ranges.
5949   return isKnownPredicateWithRanges(Pred, LHS, RHS);
5950 }
5951
5952 bool
5953 ScalarEvolution::isKnownPredicateWithRanges(ICmpInst::Predicate Pred,
5954                                             const SCEV *LHS, const SCEV *RHS) {
5955   if (HasSameValue(LHS, RHS))
5956     return ICmpInst::isTrueWhenEqual(Pred);
5957
5958   // This code is split out from isKnownPredicate because it is called from
5959   // within isLoopEntryGuardedByCond.
5960   switch (Pred) {
5961   default:
5962     llvm_unreachable("Unexpected ICmpInst::Predicate value!");
5963   case ICmpInst::ICMP_SGT:
5964     Pred = ICmpInst::ICMP_SLT;
5965     std::swap(LHS, RHS);
5966   case ICmpInst::ICMP_SLT: {
5967     ConstantRange LHSRange = getSignedRange(LHS);
5968     ConstantRange RHSRange = getSignedRange(RHS);
5969     if (LHSRange.getSignedMax().slt(RHSRange.getSignedMin()))
5970       return true;
5971     if (LHSRange.getSignedMin().sge(RHSRange.getSignedMax()))
5972       return false;
5973     break;
5974   }
5975   case ICmpInst::ICMP_SGE:
5976     Pred = ICmpInst::ICMP_SLE;
5977     std::swap(LHS, RHS);
5978   case ICmpInst::ICMP_SLE: {
5979     ConstantRange LHSRange = getSignedRange(LHS);
5980     ConstantRange RHSRange = getSignedRange(RHS);
5981     if (LHSRange.getSignedMax().sle(RHSRange.getSignedMin()))
5982       return true;
5983     if (LHSRange.getSignedMin().sgt(RHSRange.getSignedMax()))
5984       return false;
5985     break;
5986   }
5987   case ICmpInst::ICMP_UGT:
5988     Pred = ICmpInst::ICMP_ULT;
5989     std::swap(LHS, RHS);
5990   case ICmpInst::ICMP_ULT: {
5991     ConstantRange LHSRange = getUnsignedRange(LHS);
5992     ConstantRange RHSRange = getUnsignedRange(RHS);
5993     if (LHSRange.getUnsignedMax().ult(RHSRange.getUnsignedMin()))
5994       return true;
5995     if (LHSRange.getUnsignedMin().uge(RHSRange.getUnsignedMax()))
5996       return false;
5997     break;
5998   }
5999   case ICmpInst::ICMP_UGE:
6000     Pred = ICmpInst::ICMP_ULE;
6001     std::swap(LHS, RHS);
6002   case ICmpInst::ICMP_ULE: {
6003     ConstantRange LHSRange = getUnsignedRange(LHS);
6004     ConstantRange RHSRange = getUnsignedRange(RHS);
6005     if (LHSRange.getUnsignedMax().ule(RHSRange.getUnsignedMin()))
6006       return true;
6007     if (LHSRange.getUnsignedMin().ugt(RHSRange.getUnsignedMax()))
6008       return false;
6009     break;
6010   }
6011   case ICmpInst::ICMP_NE: {
6012     if (getUnsignedRange(LHS).intersectWith(getUnsignedRange(RHS)).isEmptySet())
6013       return true;
6014     if (getSignedRange(LHS).intersectWith(getSignedRange(RHS)).isEmptySet())
6015       return true;
6016
6017     const SCEV *Diff = getMinusSCEV(LHS, RHS);
6018     if (isKnownNonZero(Diff))
6019       return true;
6020     break;
6021   }
6022   case ICmpInst::ICMP_EQ:
6023     // The check at the top of the function catches the case where
6024     // the values are known to be equal.
6025     break;
6026   }
6027   return false;
6028 }
6029
6030 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
6031 /// protected by a conditional between LHS and RHS.  This is used to
6032 /// to eliminate casts.
6033 bool
6034 ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
6035                                              ICmpInst::Predicate Pred,
6036                                              const SCEV *LHS, const SCEV *RHS) {
6037   // Interpret a null as meaning no loop, where there is obviously no guard
6038   // (interprocedural conditions notwithstanding).
6039   if (!L) return true;
6040
6041   BasicBlock *Latch = L->getLoopLatch();
6042   if (!Latch)
6043     return false;
6044
6045   BranchInst *LoopContinuePredicate =
6046     dyn_cast<BranchInst>(Latch->getTerminator());
6047   if (!LoopContinuePredicate ||
6048       LoopContinuePredicate->isUnconditional())
6049     return false;
6050
6051   return isImpliedCond(Pred, LHS, RHS,
6052                        LoopContinuePredicate->getCondition(),
6053                        LoopContinuePredicate->getSuccessor(0) != L->getHeader());
6054 }
6055
6056 /// isLoopEntryGuardedByCond - Test whether entry to the loop is protected
6057 /// by a conditional between LHS and RHS.  This is used to help avoid max
6058 /// expressions in loop trip counts, and to eliminate casts.
6059 bool
6060 ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
6061                                           ICmpInst::Predicate Pred,
6062                                           const SCEV *LHS, const SCEV *RHS) {
6063   // Interpret a null as meaning no loop, where there is obviously no guard
6064   // (interprocedural conditions notwithstanding).
6065   if (!L) return false;
6066
6067   // Starting at the loop predecessor, climb up the predecessor chain, as long
6068   // as there are predecessors that can be found that have unique successors
6069   // leading to the original header.
6070   for (std::pair<BasicBlock *, BasicBlock *>
6071          Pair(L->getLoopPredecessor(), L->getHeader());
6072        Pair.first;
6073        Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
6074
6075     BranchInst *LoopEntryPredicate =
6076       dyn_cast<BranchInst>(Pair.first->getTerminator());
6077     if (!LoopEntryPredicate ||
6078         LoopEntryPredicate->isUnconditional())
6079       continue;
6080
6081     if (isImpliedCond(Pred, LHS, RHS,
6082                       LoopEntryPredicate->getCondition(),
6083                       LoopEntryPredicate->getSuccessor(0) != Pair.second))
6084       return true;
6085   }
6086
6087   return false;
6088 }
6089
6090 /// RAII wrapper to prevent recursive application of isImpliedCond.
6091 /// ScalarEvolution's PendingLoopPredicates set must be empty unless we are
6092 /// currently evaluating isImpliedCond.
6093 struct MarkPendingLoopPredicate {
6094   Value *Cond;
6095   DenseSet<Value*> &LoopPreds;
6096   bool Pending;
6097
6098   MarkPendingLoopPredicate(Value *C, DenseSet<Value*> &LP)
6099     : Cond(C), LoopPreds(LP) {
6100     Pending = !LoopPreds.insert(Cond).second;
6101   }
6102   ~MarkPendingLoopPredicate() {
6103     if (!Pending)
6104       LoopPreds.erase(Cond);
6105   }
6106 };
6107
6108 /// isImpliedCond - Test whether the condition described by Pred, LHS,
6109 /// and RHS is true whenever the given Cond value evaluates to true.
6110 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred,
6111                                     const SCEV *LHS, const SCEV *RHS,
6112                                     Value *FoundCondValue,
6113                                     bool Inverse) {
6114   MarkPendingLoopPredicate Mark(FoundCondValue, PendingLoopPredicates);
6115   if (Mark.Pending)
6116     return false;
6117
6118   // Recursively handle And and Or conditions.
6119   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) {
6120     if (BO->getOpcode() == Instruction::And) {
6121       if (!Inverse)
6122         return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
6123                isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
6124     } else if (BO->getOpcode() == Instruction::Or) {
6125       if (Inverse)
6126         return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
6127                isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
6128     }
6129   }
6130
6131   ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue);
6132   if (!ICI) return false;
6133
6134   // Bail if the ICmp's operands' types are wider than the needed type
6135   // before attempting to call getSCEV on them. This avoids infinite
6136   // recursion, since the analysis of widening casts can require loop
6137   // exit condition information for overflow checking, which would
6138   // lead back here.
6139   if (getTypeSizeInBits(LHS->getType()) <
6140       getTypeSizeInBits(ICI->getOperand(0)->getType()))
6141     return false;
6142
6143   // Now that we found a conditional branch that dominates the loop or controls
6144   // the loop latch. Check to see if it is the comparison we are looking for.
6145   ICmpInst::Predicate FoundPred;
6146   if (Inverse)
6147     FoundPred = ICI->getInversePredicate();
6148   else
6149     FoundPred = ICI->getPredicate();
6150
6151   const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
6152   const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
6153
6154   // Balance the types. The case where FoundLHS' type is wider than
6155   // LHS' type is checked for above.
6156   if (getTypeSizeInBits(LHS->getType()) >
6157       getTypeSizeInBits(FoundLHS->getType())) {
6158     if (CmpInst::isSigned(Pred)) {
6159       FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
6160       FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
6161     } else {
6162       FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
6163       FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
6164     }
6165   }
6166
6167   // Canonicalize the query to match the way instcombine will have
6168   // canonicalized the comparison.
6169   if (SimplifyICmpOperands(Pred, LHS, RHS))
6170     if (LHS == RHS)
6171       return CmpInst::isTrueWhenEqual(Pred);
6172   if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS))
6173     if (FoundLHS == FoundRHS)
6174       return CmpInst::isFalseWhenEqual(FoundPred);
6175
6176   // Check to see if we can make the LHS or RHS match.
6177   if (LHS == FoundRHS || RHS == FoundLHS) {
6178     if (isa<SCEVConstant>(RHS)) {
6179       std::swap(FoundLHS, FoundRHS);
6180       FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
6181     } else {
6182       std::swap(LHS, RHS);
6183       Pred = ICmpInst::getSwappedPredicate(Pred);
6184     }
6185   }
6186
6187   // Check whether the found predicate is the same as the desired predicate.
6188   if (FoundPred == Pred)
6189     return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
6190
6191   // Check whether swapping the found predicate makes it the same as the
6192   // desired predicate.
6193   if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
6194     if (isa<SCEVConstant>(RHS))
6195       return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
6196     else
6197       return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
6198                                    RHS, LHS, FoundLHS, FoundRHS);
6199   }
6200
6201   // Check whether the actual condition is beyond sufficient.
6202   if (FoundPred == ICmpInst::ICMP_EQ)
6203     if (ICmpInst::isTrueWhenEqual(Pred))
6204       if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
6205         return true;
6206   if (Pred == ICmpInst::ICMP_NE)
6207     if (!ICmpInst::isTrueWhenEqual(FoundPred))
6208       if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
6209         return true;
6210
6211   // Otherwise assume the worst.
6212   return false;
6213 }
6214
6215 /// isImpliedCondOperands - Test whether the condition described by Pred,
6216 /// LHS, and RHS is true whenever the condition described by Pred, FoundLHS,
6217 /// and FoundRHS is true.
6218 bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
6219                                             const SCEV *LHS, const SCEV *RHS,
6220                                             const SCEV *FoundLHS,
6221                                             const SCEV *FoundRHS) {
6222   return isImpliedCondOperandsHelper(Pred, LHS, RHS,
6223                                      FoundLHS, FoundRHS) ||
6224          // ~x < ~y --> x > y
6225          isImpliedCondOperandsHelper(Pred, LHS, RHS,
6226                                      getNotSCEV(FoundRHS),
6227                                      getNotSCEV(FoundLHS));
6228 }
6229
6230 /// isImpliedCondOperandsHelper - Test whether the condition described by
6231 /// Pred, LHS, and RHS is true whenever the condition described by Pred,
6232 /// FoundLHS, and FoundRHS is true.
6233 bool
6234 ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
6235                                              const SCEV *LHS, const SCEV *RHS,
6236                                              const SCEV *FoundLHS,
6237                                              const SCEV *FoundRHS) {
6238   switch (Pred) {
6239   default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
6240   case ICmpInst::ICMP_EQ:
6241   case ICmpInst::ICMP_NE:
6242     if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
6243       return true;
6244     break;
6245   case ICmpInst::ICMP_SLT:
6246   case ICmpInst::ICMP_SLE:
6247     if (isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
6248         isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, RHS, FoundRHS))
6249       return true;
6250     break;
6251   case ICmpInst::ICMP_SGT:
6252   case ICmpInst::ICMP_SGE:
6253     if (isKnownPredicateWithRanges(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
6254         isKnownPredicateWithRanges(ICmpInst::ICMP_SLE, RHS, FoundRHS))
6255       return true;
6256     break;
6257   case ICmpInst::ICMP_ULT:
6258   case ICmpInst::ICMP_ULE:
6259     if (isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
6260         isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, RHS, FoundRHS))
6261       return true;
6262     break;
6263   case ICmpInst::ICMP_UGT:
6264   case ICmpInst::ICMP_UGE:
6265     if (isKnownPredicateWithRanges(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
6266         isKnownPredicateWithRanges(ICmpInst::ICMP_ULE, RHS, FoundRHS))
6267       return true;
6268     break;
6269   }
6270
6271   return false;
6272 }
6273
6274 /// getBECount - Subtract the end and start values and divide by the step,
6275 /// rounding up, to get the number of times the backedge is executed. Return
6276 /// CouldNotCompute if an intermediate computation overflows.
6277 const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
6278                                         const SCEV *End,
6279                                         const SCEV *Step,
6280                                         bool NoWrap) {
6281   assert(!isKnownNegative(Step) &&
6282          "This code doesn't handle negative strides yet!");
6283
6284   Type *Ty = Start->getType();
6285
6286   // When Start == End, we have an exact BECount == 0. Short-circuit this case
6287   // here because SCEV may not be able to determine that the unsigned division
6288   // after rounding is zero.
6289   if (Start == End)
6290     return getConstant(Ty, 0);
6291
6292   const SCEV *NegOne = getConstant(Ty, (uint64_t)-1);
6293   const SCEV *Diff = getMinusSCEV(End, Start);
6294   const SCEV *RoundUp = getAddExpr(Step, NegOne);
6295
6296   // Add an adjustment to the difference between End and Start so that
6297   // the division will effectively round up.
6298   const SCEV *Add = getAddExpr(Diff, RoundUp);
6299
6300   if (!NoWrap) {
6301     // Check Add for unsigned overflow.
6302     // TODO: More sophisticated things could be done here.
6303     Type *WideTy = IntegerType::get(getContext(),
6304                                           getTypeSizeInBits(Ty) + 1);
6305     const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
6306     const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
6307     const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
6308     if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
6309       return getCouldNotCompute();
6310   }
6311
6312   return getUDivExpr(Add, Step);
6313 }
6314
6315 /// HowManyLessThans - Return the number of times a backedge containing the
6316 /// specified less-than comparison will execute.  If not computable, return
6317 /// CouldNotCompute.
6318 ScalarEvolution::ExitLimit
6319 ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
6320                                   const Loop *L, bool isSigned) {
6321   // Only handle:  "ADDREC < LoopInvariant".
6322   if (!isLoopInvariant(RHS, L)) return getCouldNotCompute();
6323
6324   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS);
6325   if (!AddRec || AddRec->getLoop() != L)
6326     return getCouldNotCompute();
6327
6328   // Check to see if we have a flag which makes analysis easy.
6329   bool NoWrap = isSigned ?
6330     AddRec->getNoWrapFlags((SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNW)) :
6331     AddRec->getNoWrapFlags((SCEV::NoWrapFlags)(SCEV::FlagNUW | SCEV::FlagNW));
6332
6333   if (AddRec->isAffine()) {
6334     unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
6335     const SCEV *Step = AddRec->getStepRecurrence(*this);
6336
6337     if (Step->isZero())
6338       return getCouldNotCompute();
6339     if (Step->isOne()) {
6340       // With unit stride, the iteration never steps past the limit value.
6341     } else if (isKnownPositive(Step)) {
6342       // Test whether a positive iteration can step past the limit
6343       // value and past the maximum value for its type in a single step.
6344       // Note that it's not sufficient to check NoWrap here, because even
6345       // though the value after a wrap is undefined, it's not undefined
6346       // behavior, so if wrap does occur, the loop could either terminate or
6347       // loop infinitely, but in either case, the loop is guaranteed to
6348       // iterate at least until the iteration where the wrapping occurs.
6349       const SCEV *One = getConstant(Step->getType(), 1);
6350       if (isSigned) {
6351         APInt Max = APInt::getSignedMaxValue(BitWidth);
6352         if ((Max - getSignedRange(getMinusSCEV(Step, One)).getSignedMax())
6353               .slt(getSignedRange(RHS).getSignedMax()))
6354           return getCouldNotCompute();
6355       } else {
6356         APInt Max = APInt::getMaxValue(BitWidth);
6357         if ((Max - getUnsignedRange(getMinusSCEV(Step, One)).getUnsignedMax())
6358               .ult(getUnsignedRange(RHS).getUnsignedMax()))
6359           return getCouldNotCompute();
6360       }
6361     } else
6362       // TODO: Handle negative strides here and below.
6363       return getCouldNotCompute();
6364
6365     // We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
6366     // m.  So, we count the number of iterations in which {n,+,s} < m is true.
6367     // Note that we cannot simply return max(m-n,0)/s because it's not safe to
6368     // treat m-n as signed nor unsigned due to overflow possibility.
6369
6370     // First, we get the value of the LHS in the first iteration: n
6371     const SCEV *Start = AddRec->getOperand(0);
6372
6373     // Determine the minimum constant start value.
6374     const SCEV *MinStart = getConstant(isSigned ?
6375       getSignedRange(Start).getSignedMin() :
6376       getUnsignedRange(Start).getUnsignedMin());
6377
6378     // If we know that the condition is true in order to enter the loop,
6379     // then we know that it will run exactly (m-n)/s times. Otherwise, we
6380     // only know that it will execute (max(m,n)-n)/s times. In both cases,
6381     // the division must round up.
6382     const SCEV *End = RHS;
6383     if (!isLoopEntryGuardedByCond(L,
6384                                   isSigned ? ICmpInst::ICMP_SLT :
6385                                              ICmpInst::ICMP_ULT,
6386                                   getMinusSCEV(Start, Step), RHS))
6387       End = isSigned ? getSMaxExpr(RHS, Start)
6388                      : getUMaxExpr(RHS, Start);
6389
6390     // Determine the maximum constant end value.
6391     const SCEV *MaxEnd = getConstant(isSigned ?
6392       getSignedRange(End).getSignedMax() :
6393       getUnsignedRange(End).getUnsignedMax());
6394
6395     // If MaxEnd is within a step of the maximum integer value in its type,
6396     // adjust it down to the minimum value which would produce the same effect.
6397     // This allows the subsequent ceiling division of (N+(step-1))/step to
6398     // compute the correct value.
6399     const SCEV *StepMinusOne = getMinusSCEV(Step,
6400                                             getConstant(Step->getType(), 1));
6401     MaxEnd = isSigned ?
6402       getSMinExpr(MaxEnd,
6403                   getMinusSCEV(getConstant(APInt::getSignedMaxValue(BitWidth)),
6404                                StepMinusOne)) :
6405       getUMinExpr(MaxEnd,
6406                   getMinusSCEV(getConstant(APInt::getMaxValue(BitWidth)),
6407                                StepMinusOne));
6408
6409     // Finally, we subtract these two values and divide, rounding up, to get
6410     // the number of times the backedge is executed.
6411     const SCEV *BECount = getBECount(Start, End, Step, NoWrap);
6412
6413     // The maximum backedge count is similar, except using the minimum start
6414     // value and the maximum end value.
6415     // If we already have an exact constant BECount, use it instead.
6416     const SCEV *MaxBECount = isa<SCEVConstant>(BECount) ? BECount
6417       : getBECount(MinStart, MaxEnd, Step, NoWrap);
6418
6419     // If the stride is nonconstant, and NoWrap == true, then
6420     // getBECount(MinStart, MaxEnd) may not compute. This would result in an
6421     // exact BECount and invalid MaxBECount, which should be avoided to catch
6422     // more optimization opportunities.
6423     if (isa<SCEVCouldNotCompute>(MaxBECount))
6424       MaxBECount = BECount;
6425
6426     return ExitLimit(BECount, MaxBECount);
6427   }
6428
6429   return getCouldNotCompute();
6430 }
6431
6432 /// getNumIterationsInRange - Return the number of iterations of this loop that
6433 /// produce values in the specified constant range.  Another way of looking at
6434 /// this is that it returns the first iteration number where the value is not in
6435 /// the condition, thus computing the exit count. If the iteration count can't
6436 /// be computed, an instance of SCEVCouldNotCompute is returned.
6437 const SCEV *SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
6438                                                     ScalarEvolution &SE) const {
6439   if (Range.isFullSet())  // Infinite loop.
6440     return SE.getCouldNotCompute();
6441
6442   // If the start is a non-zero constant, shift the range to simplify things.
6443   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
6444     if (!SC->getValue()->isZero()) {
6445       SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
6446       Operands[0] = SE.getConstant(SC->getType(), 0);
6447       const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(),
6448                                              getNoWrapFlags(FlagNW));
6449       if (const SCEVAddRecExpr *ShiftedAddRec =
6450             dyn_cast<SCEVAddRecExpr>(Shifted))
6451         return ShiftedAddRec->getNumIterationsInRange(
6452                            Range.subtract(SC->getValue()->getValue()), SE);
6453       // This is strange and shouldn't happen.
6454       return SE.getCouldNotCompute();
6455     }
6456
6457   // The only time we can solve this is when we have all constant indices.
6458   // Otherwise, we cannot determine the overflow conditions.
6459   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
6460     if (!isa<SCEVConstant>(getOperand(i)))
6461       return SE.getCouldNotCompute();
6462
6463
6464   // Okay at this point we know that all elements of the chrec are constants and
6465   // that the start element is zero.
6466
6467   // First check to see if the range contains zero.  If not, the first
6468   // iteration exits.
6469   unsigned BitWidth = SE.getTypeSizeInBits(getType());
6470   if (!Range.contains(APInt(BitWidth, 0)))
6471     return SE.getConstant(getType(), 0);
6472
6473   if (isAffine()) {
6474     // If this is an affine expression then we have this situation:
6475     //   Solve {0,+,A} in Range  ===  Ax in Range
6476
6477     // We know that zero is in the range.  If A is positive then we know that
6478     // the upper value of the range must be the first possible exit value.
6479     // If A is negative then the lower of the range is the last possible loop
6480     // value.  Also note that we already checked for a full range.
6481     APInt One(BitWidth,1);
6482     APInt A     = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
6483     APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
6484
6485     // The exit value should be (End+A)/A.
6486     APInt ExitVal = (End + A).udiv(A);
6487     ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
6488
6489     // Evaluate at the exit value.  If we really did fall out of the valid
6490     // range, then we computed our trip count, otherwise wrap around or other
6491     // things must have happened.
6492     ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
6493     if (Range.contains(Val->getValue()))
6494       return SE.getCouldNotCompute();  // Something strange happened
6495
6496     // Ensure that the previous value is in the range.  This is a sanity check.
6497     assert(Range.contains(
6498            EvaluateConstantChrecAtConstant(this,
6499            ConstantInt::get(SE.getContext(), ExitVal - One), SE)->getValue()) &&
6500            "Linear scev computation is off in a bad way!");
6501     return SE.getConstant(ExitValue);
6502   } else if (isQuadratic()) {
6503     // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of the
6504     // quadratic equation to solve it.  To do this, we must frame our problem in
6505     // terms of figuring out when zero is crossed, instead of when
6506     // Range.getUpper() is crossed.
6507     SmallVector<const SCEV *, 4> NewOps(op_begin(), op_end());
6508     NewOps[0] = SE.getNegativeSCEV(SE.getConstant(Range.getUpper()));
6509     const SCEV *NewAddRec = SE.getAddRecExpr(NewOps, getLoop(),
6510                                              // getNoWrapFlags(FlagNW)
6511                                              FlagAnyWrap);
6512
6513     // Next, solve the constructed addrec
6514     std::pair<const SCEV *,const SCEV *> Roots =
6515       SolveQuadraticEquation(cast<SCEVAddRecExpr>(NewAddRec), SE);
6516     const SCEVConstant *R1 = dyn_cast<SCEVConstant>(Roots.first);
6517     const SCEVConstant *R2 = dyn_cast<SCEVConstant>(Roots.second);
6518     if (R1) {
6519       // Pick the smallest positive root value.
6520       if (ConstantInt *CB =
6521           dyn_cast<ConstantInt>(ConstantExpr::getICmp(ICmpInst::ICMP_ULT,
6522                          R1->getValue(), R2->getValue()))) {
6523         if (CB->getZExtValue() == false)
6524           std::swap(R1, R2);   // R1 is the minimum root now.
6525
6526         // Make sure the root is not off by one.  The returned iteration should
6527         // not be in the range, but the previous one should be.  When solving
6528         // for "X*X < 5", for example, we should not return a root of 2.
6529         ConstantInt *R1Val = EvaluateConstantChrecAtConstant(this,
6530                                                              R1->getValue(),
6531                                                              SE);
6532         if (Range.contains(R1Val->getValue())) {
6533           // The next iteration must be out of the range...
6534           ConstantInt *NextVal =
6535                 ConstantInt::get(SE.getContext(), R1->getValue()->getValue()+1);
6536
6537           R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
6538           if (!Range.contains(R1Val->getValue()))
6539             return SE.getConstant(NextVal);
6540           return SE.getCouldNotCompute();  // Something strange happened
6541         }
6542
6543         // If R1 was not in the range, then it is a good return value.  Make
6544         // sure that R1-1 WAS in the range though, just in case.
6545         ConstantInt *NextVal =
6546                ConstantInt::get(SE.getContext(), R1->getValue()->getValue()-1);
6547         R1Val = EvaluateConstantChrecAtConstant(this, NextVal, SE);
6548         if (Range.contains(R1Val->getValue()))
6549           return R1;
6550         return SE.getCouldNotCompute();  // Something strange happened
6551       }
6552     }
6553   }
6554
6555   return SE.getCouldNotCompute();
6556 }
6557
6558
6559
6560 //===----------------------------------------------------------------------===//
6561 //                   SCEVCallbackVH Class Implementation
6562 //===----------------------------------------------------------------------===//
6563
6564 void ScalarEvolution::SCEVCallbackVH::deleted() {
6565   assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
6566   if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
6567     SE->ConstantEvolutionLoopExitValue.erase(PN);
6568   SE->ValueExprMap.erase(getValPtr());
6569   // this now dangles!
6570 }
6571
6572 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) {
6573   assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
6574
6575   // Forget all the expressions associated with users of the old value,
6576   // so that future queries will recompute the expressions using the new
6577   // value.
6578   Value *Old = getValPtr();
6579   SmallVector<User *, 16> Worklist;
6580   SmallPtrSet<User *, 8> Visited;
6581   for (Value::use_iterator UI = Old->use_begin(), UE = Old->use_end();
6582        UI != UE; ++UI)
6583     Worklist.push_back(*UI);
6584   while (!Worklist.empty()) {
6585     User *U = Worklist.pop_back_val();
6586     // Deleting the Old value will cause this to dangle. Postpone
6587     // that until everything else is done.
6588     if (U == Old)
6589       continue;
6590     if (!Visited.insert(U))
6591       continue;
6592     if (PHINode *PN = dyn_cast<PHINode>(U))
6593       SE->ConstantEvolutionLoopExitValue.erase(PN);
6594     SE->ValueExprMap.erase(U);
6595     for (Value::use_iterator UI = U->use_begin(), UE = U->use_end();
6596          UI != UE; ++UI)
6597       Worklist.push_back(*UI);
6598   }
6599   // Delete the Old value.
6600   if (PHINode *PN = dyn_cast<PHINode>(Old))
6601     SE->ConstantEvolutionLoopExitValue.erase(PN);
6602   SE->ValueExprMap.erase(Old);
6603   // this now dangles!
6604 }
6605
6606 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
6607   : CallbackVH(V), SE(se) {}
6608
6609 //===----------------------------------------------------------------------===//
6610 //                   ScalarEvolution Class Implementation
6611 //===----------------------------------------------------------------------===//
6612
6613 ScalarEvolution::ScalarEvolution()
6614   : FunctionPass(ID), FirstUnknown(0) {
6615   initializeScalarEvolutionPass(*PassRegistry::getPassRegistry());
6616 }
6617
6618 bool ScalarEvolution::runOnFunction(Function &F) {
6619   this->F = &F;
6620   LI = &getAnalysis<LoopInfo>();
6621   TD = getAnalysisIfAvailable<DataLayout>();
6622   TLI = &getAnalysis<TargetLibraryInfo>();
6623   DT = &getAnalysis<DominatorTree>();
6624   return false;
6625 }
6626
6627 void ScalarEvolution::releaseMemory() {
6628   // Iterate through all the SCEVUnknown instances and call their
6629   // destructors, so that they release their references to their values.
6630   for (SCEVUnknown *U = FirstUnknown; U; U = U->Next)
6631     U->~SCEVUnknown();
6632   FirstUnknown = 0;
6633
6634   ValueExprMap.clear();
6635
6636   // Free any extra memory created for ExitNotTakenInfo in the unlikely event
6637   // that a loop had multiple computable exits.
6638   for (DenseMap<const Loop*, BackedgeTakenInfo>::iterator I =
6639          BackedgeTakenCounts.begin(), E = BackedgeTakenCounts.end();
6640        I != E; ++I) {
6641     I->second.clear();
6642   }
6643
6644   assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
6645
6646   BackedgeTakenCounts.clear();
6647   ConstantEvolutionLoopExitValue.clear();
6648   ValuesAtScopes.clear();
6649   LoopDispositions.clear();
6650   BlockDispositions.clear();
6651   UnsignedRanges.clear();
6652   SignedRanges.clear();
6653   UniqueSCEVs.clear();
6654   SCEVAllocator.Reset();
6655 }
6656
6657 void ScalarEvolution::getAnalysisUsage(AnalysisUsage &AU) const {
6658   AU.setPreservesAll();
6659   AU.addRequiredTransitive<LoopInfo>();
6660   AU.addRequiredTransitive<DominatorTree>();
6661   AU.addRequired<TargetLibraryInfo>();
6662 }
6663
6664 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
6665   return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
6666 }
6667
6668 static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
6669                           const Loop *L) {
6670   // Print all inner loops first
6671   for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
6672     PrintLoopInfo(OS, SE, *I);
6673
6674   OS << "Loop ";
6675   WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false);
6676   OS << ": ";
6677
6678   SmallVector<BasicBlock *, 8> ExitBlocks;
6679   L->getExitBlocks(ExitBlocks);
6680   if (ExitBlocks.size() != 1)
6681     OS << "<multiple exits> ";
6682
6683   if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
6684     OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
6685   } else {
6686     OS << "Unpredictable backedge-taken count. ";
6687   }
6688
6689   OS << "\n"
6690         "Loop ";
6691   WriteAsOperand(OS, L->getHeader(), /*PrintType=*/false);
6692   OS << ": ";
6693
6694   if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
6695     OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
6696   } else {
6697     OS << "Unpredictable max backedge-taken count. ";
6698   }
6699
6700   OS << "\n";
6701 }
6702
6703 void ScalarEvolution::print(raw_ostream &OS, const Module *) const {
6704   // ScalarEvolution's implementation of the print method is to print
6705   // out SCEV values of all instructions that are interesting. Doing
6706   // this potentially causes it to create new SCEV objects though,
6707   // which technically conflicts with the const qualifier. This isn't
6708   // observable from outside the class though, so casting away the
6709   // const isn't dangerous.
6710   ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
6711
6712   OS << "Classifying expressions for: ";
6713   WriteAsOperand(OS, F, /*PrintType=*/false);
6714   OS << "\n";
6715   for (inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I)
6716     if (isSCEVable(I->getType()) && !isa<CmpInst>(*I)) {
6717       OS << *I << '\n';
6718       OS << "  -->  ";
6719       const SCEV *SV = SE.getSCEV(&*I);
6720       SV->print(OS);
6721
6722       const Loop *L = LI->getLoopFor((*I).getParent());
6723
6724       const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
6725       if (AtUse != SV) {
6726         OS << "  -->  ";
6727         AtUse->print(OS);
6728       }
6729
6730       if (L) {
6731         OS << "\t\t" "Exits: ";
6732         const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
6733         if (!SE.isLoopInvariant(ExitValue, L)) {
6734           OS << "<<Unknown>>";
6735         } else {
6736           OS << *ExitValue;
6737         }
6738       }
6739
6740       OS << "\n";
6741     }
6742
6743   OS << "Determining loop execution counts for: ";
6744   WriteAsOperand(OS, F, /*PrintType=*/false);
6745   OS << "\n";
6746   for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I)
6747     PrintLoopInfo(OS, &SE, *I);
6748 }
6749
6750 ScalarEvolution::LoopDisposition
6751 ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) {
6752   std::map<const Loop *, LoopDisposition> &Values = LoopDispositions[S];
6753   std::pair<std::map<const Loop *, LoopDisposition>::iterator, bool> Pair =
6754     Values.insert(std::make_pair(L, LoopVariant));
6755   if (!Pair.second)
6756     return Pair.first->second;
6757
6758   LoopDisposition D = computeLoopDisposition(S, L);
6759   return LoopDispositions[S][L] = D;
6760 }
6761
6762 ScalarEvolution::LoopDisposition
6763 ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) {
6764   switch (S->getSCEVType()) {
6765   case scConstant:
6766     return LoopInvariant;
6767   case scTruncate:
6768   case scZeroExtend:
6769   case scSignExtend:
6770     return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L);
6771   case scAddRecExpr: {
6772     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
6773
6774     // If L is the addrec's loop, it's computable.
6775     if (AR->getLoop() == L)
6776       return LoopComputable;
6777
6778     // Add recurrences are never invariant in the function-body (null loop).
6779     if (!L)
6780       return LoopVariant;
6781
6782     // This recurrence is variant w.r.t. L if L contains AR's loop.
6783     if (L->contains(AR->getLoop()))
6784       return LoopVariant;
6785
6786     // This recurrence is invariant w.r.t. L if AR's loop contains L.
6787     if (AR->getLoop()->contains(L))
6788       return LoopInvariant;
6789
6790     // This recurrence is variant w.r.t. L if any of its operands
6791     // are variant.
6792     for (SCEVAddRecExpr::op_iterator I = AR->op_begin(), E = AR->op_end();
6793          I != E; ++I)
6794       if (!isLoopInvariant(*I, L))
6795         return LoopVariant;
6796
6797     // Otherwise it's loop-invariant.
6798     return LoopInvariant;
6799   }
6800   case scAddExpr:
6801   case scMulExpr:
6802   case scUMaxExpr:
6803   case scSMaxExpr: {
6804     const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
6805     bool HasVarying = false;
6806     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
6807          I != E; ++I) {
6808       LoopDisposition D = getLoopDisposition(*I, L);
6809       if (D == LoopVariant)
6810         return LoopVariant;
6811       if (D == LoopComputable)
6812         HasVarying = true;
6813     }
6814     return HasVarying ? LoopComputable : LoopInvariant;
6815   }
6816   case scUDivExpr: {
6817     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
6818     LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L);
6819     if (LD == LoopVariant)
6820       return LoopVariant;
6821     LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L);
6822     if (RD == LoopVariant)
6823       return LoopVariant;
6824     return (LD == LoopInvariant && RD == LoopInvariant) ?
6825            LoopInvariant : LoopComputable;
6826   }
6827   case scUnknown:
6828     // All non-instruction values are loop invariant.  All instructions are loop
6829     // invariant if they are not contained in the specified loop.
6830     // Instructions are never considered invariant in the function body
6831     // (null loop) because they are defined within the "loop".
6832     if (Instruction *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue()))
6833       return (L && !L->contains(I)) ? LoopInvariant : LoopVariant;
6834     return LoopInvariant;
6835   case scCouldNotCompute:
6836     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6837   default: llvm_unreachable("Unknown SCEV kind!");
6838   }
6839 }
6840
6841 bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) {
6842   return getLoopDisposition(S, L) == LoopInvariant;
6843 }
6844
6845 bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) {
6846   return getLoopDisposition(S, L) == LoopComputable;
6847 }
6848
6849 ScalarEvolution::BlockDisposition
6850 ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) {
6851   std::map<const BasicBlock *, BlockDisposition> &Values = BlockDispositions[S];
6852   std::pair<std::map<const BasicBlock *, BlockDisposition>::iterator, bool>
6853     Pair = Values.insert(std::make_pair(BB, DoesNotDominateBlock));
6854   if (!Pair.second)
6855     return Pair.first->second;
6856
6857   BlockDisposition D = computeBlockDisposition(S, BB);
6858   return BlockDispositions[S][BB] = D;
6859 }
6860
6861 ScalarEvolution::BlockDisposition
6862 ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
6863   switch (S->getSCEVType()) {
6864   case scConstant:
6865     return ProperlyDominatesBlock;
6866   case scTruncate:
6867   case scZeroExtend:
6868   case scSignExtend:
6869     return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB);
6870   case scAddRecExpr: {
6871     // This uses a "dominates" query instead of "properly dominates" query
6872     // to test for proper dominance too, because the instruction which
6873     // produces the addrec's value is a PHI, and a PHI effectively properly
6874     // dominates its entire containing block.
6875     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
6876     if (!DT->dominates(AR->getLoop()->getHeader(), BB))
6877       return DoesNotDominateBlock;
6878   }
6879   // FALL THROUGH into SCEVNAryExpr handling.
6880   case scAddExpr:
6881   case scMulExpr:
6882   case scUMaxExpr:
6883   case scSMaxExpr: {
6884     const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
6885     bool Proper = true;
6886     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
6887          I != E; ++I) {
6888       BlockDisposition D = getBlockDisposition(*I, BB);
6889       if (D == DoesNotDominateBlock)
6890         return DoesNotDominateBlock;
6891       if (D == DominatesBlock)
6892         Proper = false;
6893     }
6894     return Proper ? ProperlyDominatesBlock : DominatesBlock;
6895   }
6896   case scUDivExpr: {
6897     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
6898     const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS();
6899     BlockDisposition LD = getBlockDisposition(LHS, BB);
6900     if (LD == DoesNotDominateBlock)
6901       return DoesNotDominateBlock;
6902     BlockDisposition RD = getBlockDisposition(RHS, BB);
6903     if (RD == DoesNotDominateBlock)
6904       return DoesNotDominateBlock;
6905     return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ?
6906       ProperlyDominatesBlock : DominatesBlock;
6907   }
6908   case scUnknown:
6909     if (Instruction *I =
6910           dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) {
6911       if (I->getParent() == BB)
6912         return DominatesBlock;
6913       if (DT->properlyDominates(I->getParent(), BB))
6914         return ProperlyDominatesBlock;
6915       return DoesNotDominateBlock;
6916     }
6917     return ProperlyDominatesBlock;
6918   case scCouldNotCompute:
6919     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6920   default:
6921     llvm_unreachable("Unknown SCEV kind!");
6922   }
6923 }
6924
6925 bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) {
6926   return getBlockDisposition(S, BB) >= DominatesBlock;
6927 }
6928
6929 bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) {
6930   return getBlockDisposition(S, BB) == ProperlyDominatesBlock;
6931 }
6932
6933 namespace {
6934 // Search for a SCEV expression node within an expression tree.
6935 // Implements SCEVTraversal::Visitor.
6936 struct SCEVSearch {
6937   const SCEV *Node;
6938   bool IsFound;
6939
6940   SCEVSearch(const SCEV *N): Node(N), IsFound(false) {}
6941
6942   bool follow(const SCEV *S) {
6943     IsFound |= (S == Node);
6944     return !IsFound;
6945   }
6946   bool isDone() const { return IsFound; }
6947 };
6948 }
6949
6950 bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const {
6951   SCEVSearch Search(Op);
6952   visitAll(S, Search);
6953   return Search.IsFound;
6954 }
6955
6956 void ScalarEvolution::forgetMemoizedResults(const SCEV *S) {
6957   ValuesAtScopes.erase(S);
6958   LoopDispositions.erase(S);
6959   BlockDispositions.erase(S);
6960   UnsignedRanges.erase(S);
6961   SignedRanges.erase(S);
6962
6963   for (DenseMap<const Loop*, BackedgeTakenInfo>::iterator I =
6964          BackedgeTakenCounts.begin(), E = BackedgeTakenCounts.end(); I != E; ) {
6965     BackedgeTakenInfo &BEInfo = I->second;
6966     if (BEInfo.hasOperand(S, this)) {
6967       BEInfo.clear();
6968       BackedgeTakenCounts.erase(I++);
6969     }
6970     else
6971       ++I;
6972   }
6973 }
6974
6975 typedef DenseMap<const Loop *, std::string> VerifyMap;
6976
6977 /// replaceSubString - Replaces all occurences of From in Str with To.
6978 static void replaceSubString(std::string &Str, StringRef From, StringRef To) {
6979   size_t Pos = 0;
6980   while ((Pos = Str.find(From, Pos)) != std::string::npos) {
6981     Str.replace(Pos, From.size(), To.data(), To.size());
6982     Pos += To.size();
6983   }
6984 }
6985
6986 /// getLoopBackedgeTakenCounts - Helper method for verifyAnalysis.
6987 static void
6988 getLoopBackedgeTakenCounts(Loop *L, VerifyMap &Map, ScalarEvolution &SE) {
6989   for (Loop::reverse_iterator I = L->rbegin(), E = L->rend(); I != E; ++I) {
6990     getLoopBackedgeTakenCounts(*I, Map, SE); // recurse.
6991
6992     std::string &S = Map[L];
6993     if (S.empty()) {
6994       raw_string_ostream OS(S);
6995       SE.getBackedgeTakenCount(L)->print(OS);
6996
6997       // false and 0 are semantically equivalent. This can happen in dead loops.
6998       replaceSubString(OS.str(), "false", "0");
6999       // Remove wrap flags, their use in SCEV is highly fragile.
7000       // FIXME: Remove this when SCEV gets smarter about them.
7001       replaceSubString(OS.str(), "<nw>", "");
7002       replaceSubString(OS.str(), "<nsw>", "");
7003       replaceSubString(OS.str(), "<nuw>", "");
7004     }
7005   }
7006 }
7007
7008 void ScalarEvolution::verifyAnalysis() const {
7009   if (!VerifySCEV)
7010     return;
7011
7012   ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
7013
7014   // Gather stringified backedge taken counts for all loops using SCEV's caches.
7015   // FIXME: It would be much better to store actual values instead of strings,
7016   //        but SCEV pointers will change if we drop the caches.
7017   VerifyMap BackedgeDumpsOld, BackedgeDumpsNew;
7018   for (LoopInfo::reverse_iterator I = LI->rbegin(), E = LI->rend(); I != E; ++I)
7019     getLoopBackedgeTakenCounts(*I, BackedgeDumpsOld, SE);
7020
7021   // Gather stringified backedge taken counts for all loops without using
7022   // SCEV's caches.
7023   SE.releaseMemory();
7024   for (LoopInfo::reverse_iterator I = LI->rbegin(), E = LI->rend(); I != E; ++I)
7025     getLoopBackedgeTakenCounts(*I, BackedgeDumpsNew, SE);
7026
7027   // Now compare whether they're the same with and without caches. This allows
7028   // verifying that no pass changed the cache.
7029   assert(BackedgeDumpsOld.size() == BackedgeDumpsNew.size() &&
7030          "New loops suddenly appeared!");
7031
7032   for (VerifyMap::iterator OldI = BackedgeDumpsOld.begin(),
7033                            OldE = BackedgeDumpsOld.end(),
7034                            NewI = BackedgeDumpsNew.begin();
7035        OldI != OldE; ++OldI, ++NewI) {
7036     assert(OldI->first == NewI->first && "Loop order changed!");
7037
7038     // Compare the stringified SCEVs. We don't care if undef backedgetaken count
7039     // changes.
7040     // FIXME: We currently ignore SCEV changes from/to CouldNotCompute. This
7041     // means that a pass is buggy or SCEV has to learn a new pattern but is
7042     // usually not harmful.
7043     if (OldI->second != NewI->second &&
7044         OldI->second.find("undef") == std::string::npos &&
7045         NewI->second.find("undef") == std::string::npos &&
7046         OldI->second != "***COULDNOTCOMPUTE***" &&
7047         NewI->second != "***COULDNOTCOMPUTE***") {
7048       dbgs() << "SCEVValidator: SCEV for loop '"
7049              << OldI->first->getHeader()->getName()
7050              << "' changed from '" << OldI->second
7051              << "' to '" << NewI->second << "'!\n";
7052       std::abort();
7053     }
7054   }
7055
7056   // TODO: Verify more things.
7057 }