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