5f610a1515394d7735d88572c1eead8d57129322
[oota-llvm.git] / lib / Analysis / ScalarEvolution.cpp
1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
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 #include "llvm/Analysis/ScalarEvolution.h"
62 #include "llvm/ADT/Optional.h"
63 #include "llvm/ADT/STLExtras.h"
64 #include "llvm/ADT/SmallPtrSet.h"
65 #include "llvm/ADT/Statistic.h"
66 #include "llvm/Analysis/AssumptionCache.h"
67 #include "llvm/Analysis/ConstantFolding.h"
68 #include "llvm/Analysis/InstructionSimplify.h"
69 #include "llvm/Analysis/LoopInfo.h"
70 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
71 #include "llvm/Analysis/TargetLibraryInfo.h"
72 #include "llvm/Analysis/ValueTracking.h"
73 #include "llvm/IR/ConstantRange.h"
74 #include "llvm/IR/Constants.h"
75 #include "llvm/IR/DataLayout.h"
76 #include "llvm/IR/DerivedTypes.h"
77 #include "llvm/IR/Dominators.h"
78 #include "llvm/IR/GetElementPtrTypeIterator.h"
79 #include "llvm/IR/GlobalAlias.h"
80 #include "llvm/IR/GlobalVariable.h"
81 #include "llvm/IR/InstIterator.h"
82 #include "llvm/IR/Instructions.h"
83 #include "llvm/IR/LLVMContext.h"
84 #include "llvm/IR/Metadata.h"
85 #include "llvm/IR/Operator.h"
86 #include "llvm/Support/CommandLine.h"
87 #include "llvm/Support/Debug.h"
88 #include "llvm/Support/ErrorHandling.h"
89 #include "llvm/Support/MathExtras.h"
90 #include "llvm/Support/raw_ostream.h"
91 #include <algorithm>
92 using namespace llvm;
93
94 #define DEBUG_TYPE "scalar-evolution"
95
96 STATISTIC(NumArrayLenItCounts,
97           "Number of trip counts computed with array length");
98 STATISTIC(NumTripCountsComputed,
99           "Number of loops with predictable loop counts");
100 STATISTIC(NumTripCountsNotComputed,
101           "Number of loops without predictable loop counts");
102 STATISTIC(NumBruteForceTripCountsComputed,
103           "Number of loops with trip counts computed by force");
104
105 static cl::opt<unsigned>
106 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
107                         cl::desc("Maximum number of iterations SCEV will "
108                                  "symbolically execute a constant "
109                                  "derived loop"),
110                         cl::init(100));
111
112 // FIXME: Enable this with XDEBUG when the test suite is clean.
113 static cl::opt<bool>
114 VerifySCEV("verify-scev",
115            cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
116
117 INITIALIZE_PASS_BEGIN(ScalarEvolution, "scalar-evolution",
118                 "Scalar Evolution Analysis", false, true)
119 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
120 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
121 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
122 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
123 INITIALIZE_PASS_END(ScalarEvolution, "scalar-evolution",
124                 "Scalar Evolution Analysis", false, true)
125 char ScalarEvolution::ID = 0;
126
127 //===----------------------------------------------------------------------===//
128 //                           SCEV class definitions
129 //===----------------------------------------------------------------------===//
130
131 //===----------------------------------------------------------------------===//
132 // Implementation of the SCEV class.
133 //
134
135 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
136 void SCEV::dump() const {
137   print(dbgs());
138   dbgs() << '\n';
139 }
140 #endif
141
142 void SCEV::print(raw_ostream &OS) const {
143   switch (static_cast<SCEVTypes>(getSCEVType())) {
144   case scConstant:
145     cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
146     return;
147   case scTruncate: {
148     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
149     const SCEV *Op = Trunc->getOperand();
150     OS << "(trunc " << *Op->getType() << " " << *Op << " to "
151        << *Trunc->getType() << ")";
152     return;
153   }
154   case scZeroExtend: {
155     const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
156     const SCEV *Op = ZExt->getOperand();
157     OS << "(zext " << *Op->getType() << " " << *Op << " to "
158        << *ZExt->getType() << ")";
159     return;
160   }
161   case scSignExtend: {
162     const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
163     const SCEV *Op = SExt->getOperand();
164     OS << "(sext " << *Op->getType() << " " << *Op << " to "
165        << *SExt->getType() << ")";
166     return;
167   }
168   case scAddRecExpr: {
169     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
170     OS << "{" << *AR->getOperand(0);
171     for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
172       OS << ",+," << *AR->getOperand(i);
173     OS << "}<";
174     if (AR->getNoWrapFlags(FlagNUW))
175       OS << "nuw><";
176     if (AR->getNoWrapFlags(FlagNSW))
177       OS << "nsw><";
178     if (AR->getNoWrapFlags(FlagNW) &&
179         !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW)))
180       OS << "nw><";
181     AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false);
182     OS << ">";
183     return;
184   }
185   case scAddExpr:
186   case scMulExpr:
187   case scUMaxExpr:
188   case scSMaxExpr: {
189     const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
190     const char *OpStr = nullptr;
191     switch (NAry->getSCEVType()) {
192     case scAddExpr: OpStr = " + "; break;
193     case scMulExpr: OpStr = " * "; break;
194     case scUMaxExpr: OpStr = " umax "; break;
195     case scSMaxExpr: OpStr = " smax "; break;
196     }
197     OS << "(";
198     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
199          I != E; ++I) {
200       OS << **I;
201       if (std::next(I) != E)
202         OS << OpStr;
203     }
204     OS << ")";
205     switch (NAry->getSCEVType()) {
206     case scAddExpr:
207     case scMulExpr:
208       if (NAry->getNoWrapFlags(FlagNUW))
209         OS << "<nuw>";
210       if (NAry->getNoWrapFlags(FlagNSW))
211         OS << "<nsw>";
212     }
213     return;
214   }
215   case scUDivExpr: {
216     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
217     OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
218     return;
219   }
220   case scUnknown: {
221     const SCEVUnknown *U = cast<SCEVUnknown>(this);
222     Type *AllocTy;
223     if (U->isSizeOf(AllocTy)) {
224       OS << "sizeof(" << *AllocTy << ")";
225       return;
226     }
227     if (U->isAlignOf(AllocTy)) {
228       OS << "alignof(" << *AllocTy << ")";
229       return;
230     }
231
232     Type *CTy;
233     Constant *FieldNo;
234     if (U->isOffsetOf(CTy, FieldNo)) {
235       OS << "offsetof(" << *CTy << ", ";
236       FieldNo->printAsOperand(OS, false);
237       OS << ")";
238       return;
239     }
240
241     // Otherwise just print it normally.
242     U->getValue()->printAsOperand(OS, false);
243     return;
244   }
245   case scCouldNotCompute:
246     OS << "***COULDNOTCOMPUTE***";
247     return;
248   }
249   llvm_unreachable("Unknown SCEV kind!");
250 }
251
252 Type *SCEV::getType() const {
253   switch (static_cast<SCEVTypes>(getSCEVType())) {
254   case scConstant:
255     return cast<SCEVConstant>(this)->getType();
256   case scTruncate:
257   case scZeroExtend:
258   case scSignExtend:
259     return cast<SCEVCastExpr>(this)->getType();
260   case scAddRecExpr:
261   case scMulExpr:
262   case scUMaxExpr:
263   case scSMaxExpr:
264     return cast<SCEVNAryExpr>(this)->getType();
265   case scAddExpr:
266     return cast<SCEVAddExpr>(this)->getType();
267   case scUDivExpr:
268     return cast<SCEVUDivExpr>(this)->getType();
269   case scUnknown:
270     return cast<SCEVUnknown>(this)->getType();
271   case scCouldNotCompute:
272     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
273   }
274   llvm_unreachable("Unknown SCEV kind!");
275 }
276
277 bool SCEV::isZero() const {
278   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
279     return SC->getValue()->isZero();
280   return false;
281 }
282
283 bool SCEV::isOne() const {
284   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
285     return SC->getValue()->isOne();
286   return false;
287 }
288
289 bool SCEV::isAllOnesValue() const {
290   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
291     return SC->getValue()->isAllOnesValue();
292   return false;
293 }
294
295 /// isNonConstantNegative - Return true if the specified scev is negated, but
296 /// not a constant.
297 bool SCEV::isNonConstantNegative() const {
298   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
299   if (!Mul) return false;
300
301   // If there is a constant factor, it will be first.
302   const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
303   if (!SC) return false;
304
305   // Return true if the value is negative, this matches things like (-42 * V).
306   return SC->getValue()->getValue().isNegative();
307 }
308
309 SCEVCouldNotCompute::SCEVCouldNotCompute() :
310   SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {}
311
312 bool SCEVCouldNotCompute::classof(const SCEV *S) {
313   return S->getSCEVType() == scCouldNotCompute;
314 }
315
316 const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
317   FoldingSetNodeID ID;
318   ID.AddInteger(scConstant);
319   ID.AddPointer(V);
320   void *IP = nullptr;
321   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
322   SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
323   UniqueSCEVs.InsertNode(S, IP);
324   return S;
325 }
326
327 const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
328   return getConstant(ConstantInt::get(getContext(), Val));
329 }
330
331 const SCEV *
332 ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
333   IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
334   return getConstant(ConstantInt::get(ITy, V, isSigned));
335 }
336
337 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID,
338                            unsigned SCEVTy, const SCEV *op, Type *ty)
339   : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
340
341 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
342                                    const SCEV *op, Type *ty)
343   : SCEVCastExpr(ID, scTruncate, op, ty) {
344   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
345          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
346          "Cannot truncate non-integer value!");
347 }
348
349 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
350                                        const SCEV *op, Type *ty)
351   : SCEVCastExpr(ID, scZeroExtend, op, ty) {
352   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
353          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
354          "Cannot zero extend non-integer value!");
355 }
356
357 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
358                                        const SCEV *op, Type *ty)
359   : SCEVCastExpr(ID, scSignExtend, op, ty) {
360   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
361          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
362          "Cannot sign extend non-integer value!");
363 }
364
365 void SCEVUnknown::deleted() {
366   // Clear this SCEVUnknown from various maps.
367   SE->forgetMemoizedResults(this);
368
369   // Remove this SCEVUnknown from the uniquing map.
370   SE->UniqueSCEVs.RemoveNode(this);
371
372   // Release the value.
373   setValPtr(nullptr);
374 }
375
376 void SCEVUnknown::allUsesReplacedWith(Value *New) {
377   // Clear this SCEVUnknown from various maps.
378   SE->forgetMemoizedResults(this);
379
380   // Remove this SCEVUnknown from the uniquing map.
381   SE->UniqueSCEVs.RemoveNode(this);
382
383   // Update this SCEVUnknown to point to the new value. This is needed
384   // because there may still be outstanding SCEVs which still point to
385   // this SCEVUnknown.
386   setValPtr(New);
387 }
388
389 bool SCEVUnknown::isSizeOf(Type *&AllocTy) const {
390   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
391     if (VCE->getOpcode() == Instruction::PtrToInt)
392       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
393         if (CE->getOpcode() == Instruction::GetElementPtr &&
394             CE->getOperand(0)->isNullValue() &&
395             CE->getNumOperands() == 2)
396           if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
397             if (CI->isOne()) {
398               AllocTy = cast<PointerType>(CE->getOperand(0)->getType())
399                                  ->getElementType();
400               return true;
401             }
402
403   return false;
404 }
405
406 bool SCEVUnknown::isAlignOf(Type *&AllocTy) const {
407   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
408     if (VCE->getOpcode() == Instruction::PtrToInt)
409       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
410         if (CE->getOpcode() == Instruction::GetElementPtr &&
411             CE->getOperand(0)->isNullValue()) {
412           Type *Ty =
413             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
414           if (StructType *STy = dyn_cast<StructType>(Ty))
415             if (!STy->isPacked() &&
416                 CE->getNumOperands() == 3 &&
417                 CE->getOperand(1)->isNullValue()) {
418               if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
419                 if (CI->isOne() &&
420                     STy->getNumElements() == 2 &&
421                     STy->getElementType(0)->isIntegerTy(1)) {
422                   AllocTy = STy->getElementType(1);
423                   return true;
424                 }
425             }
426         }
427
428   return false;
429 }
430
431 bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const {
432   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
433     if (VCE->getOpcode() == Instruction::PtrToInt)
434       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
435         if (CE->getOpcode() == Instruction::GetElementPtr &&
436             CE->getNumOperands() == 3 &&
437             CE->getOperand(0)->isNullValue() &&
438             CE->getOperand(1)->isNullValue()) {
439           Type *Ty =
440             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
441           // Ignore vector types here so that ScalarEvolutionExpander doesn't
442           // emit getelementptrs that index into vectors.
443           if (Ty->isStructTy() || Ty->isArrayTy()) {
444             CTy = Ty;
445             FieldNo = CE->getOperand(2);
446             return true;
447           }
448         }
449
450   return false;
451 }
452
453 //===----------------------------------------------------------------------===//
454 //                               SCEV Utilities
455 //===----------------------------------------------------------------------===//
456
457 namespace {
458   /// SCEVComplexityCompare - Return true if the complexity of the LHS is less
459   /// than the complexity of the RHS.  This comparator is used to canonicalize
460   /// expressions.
461   class SCEVComplexityCompare {
462     const LoopInfo *const LI;
463   public:
464     explicit SCEVComplexityCompare(const LoopInfo *li) : LI(li) {}
465
466     // Return true or false if LHS is less than, or at least RHS, respectively.
467     bool operator()(const SCEV *LHS, const SCEV *RHS) const {
468       return compare(LHS, RHS) < 0;
469     }
470
471     // Return negative, zero, or positive, if LHS is less than, equal to, or
472     // greater than RHS, respectively. A three-way result allows recursive
473     // comparisons to be more efficient.
474     int compare(const SCEV *LHS, const SCEV *RHS) const {
475       // Fast-path: SCEVs are uniqued so we can do a quick equality check.
476       if (LHS == RHS)
477         return 0;
478
479       // Primarily, sort the SCEVs by their getSCEVType().
480       unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
481       if (LType != RType)
482         return (int)LType - (int)RType;
483
484       // Aside from the getSCEVType() ordering, the particular ordering
485       // isn't very important except that it's beneficial to be consistent,
486       // so that (a + b) and (b + a) don't end up as different expressions.
487       switch (static_cast<SCEVTypes>(LType)) {
488       case scUnknown: {
489         const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
490         const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
491
492         // Sort SCEVUnknown values with some loose heuristics. TODO: This is
493         // not as complete as it could be.
494         const Value *LV = LU->getValue(), *RV = RU->getValue();
495
496         // Order pointer values after integer values. This helps SCEVExpander
497         // form GEPs.
498         bool LIsPointer = LV->getType()->isPointerTy(),
499              RIsPointer = RV->getType()->isPointerTy();
500         if (LIsPointer != RIsPointer)
501           return (int)LIsPointer - (int)RIsPointer;
502
503         // Compare getValueID values.
504         unsigned LID = LV->getValueID(),
505                  RID = RV->getValueID();
506         if (LID != RID)
507           return (int)LID - (int)RID;
508
509         // Sort arguments by their position.
510         if (const Argument *LA = dyn_cast<Argument>(LV)) {
511           const Argument *RA = cast<Argument>(RV);
512           unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
513           return (int)LArgNo - (int)RArgNo;
514         }
515
516         // For instructions, compare their loop depth, and their operand
517         // count.  This is pretty loose.
518         if (const Instruction *LInst = dyn_cast<Instruction>(LV)) {
519           const Instruction *RInst = cast<Instruction>(RV);
520
521           // Compare loop depths.
522           const BasicBlock *LParent = LInst->getParent(),
523                            *RParent = RInst->getParent();
524           if (LParent != RParent) {
525             unsigned LDepth = LI->getLoopDepth(LParent),
526                      RDepth = LI->getLoopDepth(RParent);
527             if (LDepth != RDepth)
528               return (int)LDepth - (int)RDepth;
529           }
530
531           // Compare the number of operands.
532           unsigned LNumOps = LInst->getNumOperands(),
533                    RNumOps = RInst->getNumOperands();
534           return (int)LNumOps - (int)RNumOps;
535         }
536
537         return 0;
538       }
539
540       case scConstant: {
541         const SCEVConstant *LC = cast<SCEVConstant>(LHS);
542         const SCEVConstant *RC = cast<SCEVConstant>(RHS);
543
544         // Compare constant values.
545         const APInt &LA = LC->getValue()->getValue();
546         const APInt &RA = RC->getValue()->getValue();
547         unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
548         if (LBitWidth != RBitWidth)
549           return (int)LBitWidth - (int)RBitWidth;
550         return LA.ult(RA) ? -1 : 1;
551       }
552
553       case scAddRecExpr: {
554         const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
555         const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
556
557         // Compare addrec loop depths.
558         const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
559         if (LLoop != RLoop) {
560           unsigned LDepth = LLoop->getLoopDepth(),
561                    RDepth = RLoop->getLoopDepth();
562           if (LDepth != RDepth)
563             return (int)LDepth - (int)RDepth;
564         }
565
566         // Addrec complexity grows with operand count.
567         unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands();
568         if (LNumOps != RNumOps)
569           return (int)LNumOps - (int)RNumOps;
570
571         // Lexicographically compare.
572         for (unsigned i = 0; i != LNumOps; ++i) {
573           long X = compare(LA->getOperand(i), RA->getOperand(i));
574           if (X != 0)
575             return X;
576         }
577
578         return 0;
579       }
580
581       case scAddExpr:
582       case scMulExpr:
583       case scSMaxExpr:
584       case scUMaxExpr: {
585         const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS);
586         const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
587
588         // Lexicographically compare n-ary expressions.
589         unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands();
590         if (LNumOps != RNumOps)
591           return (int)LNumOps - (int)RNumOps;
592
593         for (unsigned i = 0; i != LNumOps; ++i) {
594           if (i >= RNumOps)
595             return 1;
596           long X = compare(LC->getOperand(i), RC->getOperand(i));
597           if (X != 0)
598             return X;
599         }
600         return (int)LNumOps - (int)RNumOps;
601       }
602
603       case scUDivExpr: {
604         const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS);
605         const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
606
607         // Lexicographically compare udiv expressions.
608         long X = compare(LC->getLHS(), RC->getLHS());
609         if (X != 0)
610           return X;
611         return compare(LC->getRHS(), RC->getRHS());
612       }
613
614       case scTruncate:
615       case scZeroExtend:
616       case scSignExtend: {
617         const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS);
618         const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
619
620         // Compare cast expressions by operand.
621         return compare(LC->getOperand(), RC->getOperand());
622       }
623
624       case scCouldNotCompute:
625         llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
626       }
627       llvm_unreachable("Unknown SCEV kind!");
628     }
629   };
630 }
631
632 /// GroupByComplexity - Given a list of SCEV objects, order them by their
633 /// complexity, and group objects of the same complexity together by value.
634 /// When this routine is finished, we know that any duplicates in the vector are
635 /// consecutive and that complexity is monotonically increasing.
636 ///
637 /// Note that we go take special precautions to ensure that we get deterministic
638 /// results from this routine.  In other words, we don't want the results of
639 /// this to depend on where the addresses of various SCEV objects happened to
640 /// land in memory.
641 ///
642 static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
643                               LoopInfo *LI) {
644   if (Ops.size() < 2) return;  // Noop
645   if (Ops.size() == 2) {
646     // This is the common case, which also happens to be trivially simple.
647     // Special case it.
648     const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
649     if (SCEVComplexityCompare(LI)(RHS, LHS))
650       std::swap(LHS, RHS);
651     return;
652   }
653
654   // Do the rough sort by complexity.
655   std::stable_sort(Ops.begin(), Ops.end(), SCEVComplexityCompare(LI));
656
657   // Now that we are sorted by complexity, group elements of the same
658   // complexity.  Note that this is, at worst, N^2, but the vector is likely to
659   // be extremely short in practice.  Note that we take this approach because we
660   // do not want to depend on the addresses of the objects we are grouping.
661   for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
662     const SCEV *S = Ops[i];
663     unsigned Complexity = S->getSCEVType();
664
665     // If there are any objects of the same complexity and same value as this
666     // one, group them.
667     for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
668       if (Ops[j] == S) { // Found a duplicate.
669         // Move it to immediately after i'th element.
670         std::swap(Ops[i+1], Ops[j]);
671         ++i;   // no need to rescan it.
672         if (i == e-2) return;  // Done!
673       }
674     }
675   }
676 }
677
678 namespace {
679 struct FindSCEVSize {
680   int Size;
681   FindSCEVSize() : Size(0) {}
682
683   bool follow(const SCEV *S) {
684     ++Size;
685     // Keep looking at all operands of S.
686     return true;
687   }
688   bool isDone() const {
689     return false;
690   }
691 };
692 }
693
694 // Returns the size of the SCEV S.
695 static inline int sizeOfSCEV(const SCEV *S) {
696   FindSCEVSize F;
697   SCEVTraversal<FindSCEVSize> ST(F);
698   ST.visitAll(S);
699   return F.Size;
700 }
701
702 namespace {
703
704 struct SCEVDivision : public SCEVVisitor<SCEVDivision, void> {
705 public:
706   // Computes the Quotient and Remainder of the division of Numerator by
707   // Denominator.
708   static void divide(ScalarEvolution &SE, const SCEV *Numerator,
709                      const SCEV *Denominator, const SCEV **Quotient,
710                      const SCEV **Remainder) {
711     assert(Numerator && Denominator && "Uninitialized SCEV");
712
713     SCEVDivision D(SE, Numerator, Denominator);
714
715     // Check for the trivial case here to avoid having to check for it in the
716     // rest of the code.
717     if (Numerator == Denominator) {
718       *Quotient = D.One;
719       *Remainder = D.Zero;
720       return;
721     }
722
723     if (Numerator->isZero()) {
724       *Quotient = D.Zero;
725       *Remainder = D.Zero;
726       return;
727     }
728
729     // Split the Denominator when it is a product.
730     if (const SCEVMulExpr *T = dyn_cast<const SCEVMulExpr>(Denominator)) {
731       const SCEV *Q, *R;
732       *Quotient = Numerator;
733       for (const SCEV *Op : T->operands()) {
734         divide(SE, *Quotient, Op, &Q, &R);
735         *Quotient = Q;
736
737         // Bail out when the Numerator is not divisible by one of the terms of
738         // the Denominator.
739         if (!R->isZero()) {
740           *Quotient = D.Zero;
741           *Remainder = Numerator;
742           return;
743         }
744       }
745       *Remainder = D.Zero;
746       return;
747     }
748
749     D.visit(Numerator);
750     *Quotient = D.Quotient;
751     *Remainder = D.Remainder;
752   }
753
754   // Except in the trivial case described above, we do not know how to divide
755   // Expr by Denominator for the following functions with empty implementation.
756   void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {}
757   void visitZeroExtendExpr(const SCEVZeroExtendExpr *Numerator) {}
758   void visitSignExtendExpr(const SCEVSignExtendExpr *Numerator) {}
759   void visitUDivExpr(const SCEVUDivExpr *Numerator) {}
760   void visitSMaxExpr(const SCEVSMaxExpr *Numerator) {}
761   void visitUMaxExpr(const SCEVUMaxExpr *Numerator) {}
762   void visitUnknown(const SCEVUnknown *Numerator) {}
763   void visitCouldNotCompute(const SCEVCouldNotCompute *Numerator) {}
764
765   void visitConstant(const SCEVConstant *Numerator) {
766     if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
767       APInt NumeratorVal = Numerator->getValue()->getValue();
768       APInt DenominatorVal = D->getValue()->getValue();
769       uint32_t NumeratorBW = NumeratorVal.getBitWidth();
770       uint32_t DenominatorBW = DenominatorVal.getBitWidth();
771
772       if (NumeratorBW > DenominatorBW)
773         DenominatorVal = DenominatorVal.sext(NumeratorBW);
774       else if (NumeratorBW < DenominatorBW)
775         NumeratorVal = NumeratorVal.sext(DenominatorBW);
776
777       APInt QuotientVal(NumeratorVal.getBitWidth(), 0);
778       APInt RemainderVal(NumeratorVal.getBitWidth(), 0);
779       APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal);
780       Quotient = SE.getConstant(QuotientVal);
781       Remainder = SE.getConstant(RemainderVal);
782       return;
783     }
784   }
785
786   void visitAddRecExpr(const SCEVAddRecExpr *Numerator) {
787     const SCEV *StartQ, *StartR, *StepQ, *StepR;
788     assert(Numerator->isAffine() && "Numerator should be affine");
789     divide(SE, Numerator->getStart(), Denominator, &StartQ, &StartR);
790     divide(SE, Numerator->getStepRecurrence(SE), Denominator, &StepQ, &StepR);
791     Quotient = SE.getAddRecExpr(StartQ, StepQ, Numerator->getLoop(),
792                                 Numerator->getNoWrapFlags());
793     Remainder = SE.getAddRecExpr(StartR, StepR, Numerator->getLoop(),
794                                  Numerator->getNoWrapFlags());
795   }
796
797   void visitAddExpr(const SCEVAddExpr *Numerator) {
798     SmallVector<const SCEV *, 2> Qs, Rs;
799     Type *Ty = Denominator->getType();
800
801     for (const SCEV *Op : Numerator->operands()) {
802       const SCEV *Q, *R;
803       divide(SE, Op, Denominator, &Q, &R);
804
805       // Bail out if types do not match.
806       if (Ty != Q->getType() || Ty != R->getType()) {
807         Quotient = Zero;
808         Remainder = Numerator;
809         return;
810       }
811
812       Qs.push_back(Q);
813       Rs.push_back(R);
814     }
815
816     if (Qs.size() == 1) {
817       Quotient = Qs[0];
818       Remainder = Rs[0];
819       return;
820     }
821
822     Quotient = SE.getAddExpr(Qs);
823     Remainder = SE.getAddExpr(Rs);
824   }
825
826   void visitMulExpr(const SCEVMulExpr *Numerator) {
827     SmallVector<const SCEV *, 2> Qs;
828     Type *Ty = Denominator->getType();
829
830     bool FoundDenominatorTerm = false;
831     for (const SCEV *Op : Numerator->operands()) {
832       // Bail out if types do not match.
833       if (Ty != Op->getType()) {
834         Quotient = Zero;
835         Remainder = Numerator;
836         return;
837       }
838
839       if (FoundDenominatorTerm) {
840         Qs.push_back(Op);
841         continue;
842       }
843
844       // Check whether Denominator divides one of the product operands.
845       const SCEV *Q, *R;
846       divide(SE, Op, Denominator, &Q, &R);
847       if (!R->isZero()) {
848         Qs.push_back(Op);
849         continue;
850       }
851
852       // Bail out if types do not match.
853       if (Ty != Q->getType()) {
854         Quotient = Zero;
855         Remainder = Numerator;
856         return;
857       }
858
859       FoundDenominatorTerm = true;
860       Qs.push_back(Q);
861     }
862
863     if (FoundDenominatorTerm) {
864       Remainder = Zero;
865       if (Qs.size() == 1)
866         Quotient = Qs[0];
867       else
868         Quotient = SE.getMulExpr(Qs);
869       return;
870     }
871
872     if (!isa<SCEVUnknown>(Denominator)) {
873       Quotient = Zero;
874       Remainder = Numerator;
875       return;
876     }
877
878     // The Remainder is obtained by replacing Denominator by 0 in Numerator.
879     ValueToValueMap RewriteMap;
880     RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
881         cast<SCEVConstant>(Zero)->getValue();
882     Remainder = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
883
884     if (Remainder->isZero()) {
885       // The Quotient is obtained by replacing Denominator by 1 in Numerator.
886       RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
887           cast<SCEVConstant>(One)->getValue();
888       Quotient =
889           SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
890       return;
891     }
892
893     // Quotient is (Numerator - Remainder) divided by Denominator.
894     const SCEV *Q, *R;
895     const SCEV *Diff = SE.getMinusSCEV(Numerator, Remainder);
896     if (sizeOfSCEV(Diff) > sizeOfSCEV(Numerator)) {
897       // This SCEV does not seem to simplify: fail the division here.
898       Quotient = Zero;
899       Remainder = Numerator;
900       return;
901     }
902     divide(SE, Diff, Denominator, &Q, &R);
903     assert(R == Zero &&
904            "(Numerator - Remainder) should evenly divide Denominator");
905     Quotient = Q;
906   }
907
908 private:
909   SCEVDivision(ScalarEvolution &S, const SCEV *Numerator,
910                const SCEV *Denominator)
911       : SE(S), Denominator(Denominator) {
912     Zero = SE.getConstant(Denominator->getType(), 0);
913     One = SE.getConstant(Denominator->getType(), 1);
914
915     // By default, we don't know how to divide Expr by Denominator.
916     // Providing the default here simplifies the rest of the code.
917     Quotient = Zero;
918     Remainder = Numerator;
919   }
920
921   ScalarEvolution &SE;
922   const SCEV *Denominator, *Quotient, *Remainder, *Zero, *One;
923 };
924
925 }
926
927 //===----------------------------------------------------------------------===//
928 //                      Simple SCEV method implementations
929 //===----------------------------------------------------------------------===//
930
931 /// BinomialCoefficient - Compute BC(It, K).  The result has width W.
932 /// Assume, K > 0.
933 static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
934                                        ScalarEvolution &SE,
935                                        Type *ResultTy) {
936   // Handle the simplest case efficiently.
937   if (K == 1)
938     return SE.getTruncateOrZeroExtend(It, ResultTy);
939
940   // We are using the following formula for BC(It, K):
941   //
942   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
943   //
944   // Suppose, W is the bitwidth of the return value.  We must be prepared for
945   // overflow.  Hence, we must assure that the result of our computation is
946   // equal to the accurate one modulo 2^W.  Unfortunately, division isn't
947   // safe in modular arithmetic.
948   //
949   // However, this code doesn't use exactly that formula; the formula it uses
950   // is something like the following, where T is the number of factors of 2 in
951   // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
952   // exponentiation:
953   //
954   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
955   //
956   // This formula is trivially equivalent to the previous formula.  However,
957   // this formula can be implemented much more efficiently.  The trick is that
958   // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
959   // arithmetic.  To do exact division in modular arithmetic, all we have
960   // to do is multiply by the inverse.  Therefore, this step can be done at
961   // width W.
962   //
963   // The next issue is how to safely do the division by 2^T.  The way this
964   // is done is by doing the multiplication step at a width of at least W + T
965   // bits.  This way, the bottom W+T bits of the product are accurate. Then,
966   // when we perform the division by 2^T (which is equivalent to a right shift
967   // by T), the bottom W bits are accurate.  Extra bits are okay; they'll get
968   // truncated out after the division by 2^T.
969   //
970   // In comparison to just directly using the first formula, this technique
971   // is much more efficient; using the first formula requires W * K bits,
972   // but this formula less than W + K bits. Also, the first formula requires
973   // a division step, whereas this formula only requires multiplies and shifts.
974   //
975   // It doesn't matter whether the subtraction step is done in the calculation
976   // width or the input iteration count's width; if the subtraction overflows,
977   // the result must be zero anyway.  We prefer here to do it in the width of
978   // the induction variable because it helps a lot for certain cases; CodeGen
979   // isn't smart enough to ignore the overflow, which leads to much less
980   // efficient code if the width of the subtraction is wider than the native
981   // register width.
982   //
983   // (It's possible to not widen at all by pulling out factors of 2 before
984   // the multiplication; for example, K=2 can be calculated as
985   // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
986   // extra arithmetic, so it's not an obvious win, and it gets
987   // much more complicated for K > 3.)
988
989   // Protection from insane SCEVs; this bound is conservative,
990   // but it probably doesn't matter.
991   if (K > 1000)
992     return SE.getCouldNotCompute();
993
994   unsigned W = SE.getTypeSizeInBits(ResultTy);
995
996   // Calculate K! / 2^T and T; we divide out the factors of two before
997   // multiplying for calculating K! / 2^T to avoid overflow.
998   // Other overflow doesn't matter because we only care about the bottom
999   // W bits of the result.
1000   APInt OddFactorial(W, 1);
1001   unsigned T = 1;
1002   for (unsigned i = 3; i <= K; ++i) {
1003     APInt Mult(W, i);
1004     unsigned TwoFactors = Mult.countTrailingZeros();
1005     T += TwoFactors;
1006     Mult = Mult.lshr(TwoFactors);
1007     OddFactorial *= Mult;
1008   }
1009
1010   // We need at least W + T bits for the multiplication step
1011   unsigned CalculationBits = W + T;
1012
1013   // Calculate 2^T, at width T+W.
1014   APInt DivFactor = APInt::getOneBitSet(CalculationBits, T);
1015
1016   // Calculate the multiplicative inverse of K! / 2^T;
1017   // this multiplication factor will perform the exact division by
1018   // K! / 2^T.
1019   APInt Mod = APInt::getSignedMinValue(W+1);
1020   APInt MultiplyFactor = OddFactorial.zext(W+1);
1021   MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
1022   MultiplyFactor = MultiplyFactor.trunc(W);
1023
1024   // Calculate the product, at width T+W
1025   IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
1026                                                       CalculationBits);
1027   const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
1028   for (unsigned i = 1; i != K; ++i) {
1029     const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
1030     Dividend = SE.getMulExpr(Dividend,
1031                              SE.getTruncateOrZeroExtend(S, CalculationTy));
1032   }
1033
1034   // Divide by 2^T
1035   const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
1036
1037   // Truncate the result, and divide by K! / 2^T.
1038
1039   return SE.getMulExpr(SE.getConstant(MultiplyFactor),
1040                        SE.getTruncateOrZeroExtend(DivResult, ResultTy));
1041 }
1042
1043 /// evaluateAtIteration - Return the value of this chain of recurrences at
1044 /// the specified iteration number.  We can evaluate this recurrence by
1045 /// multiplying each element in the chain by the binomial coefficient
1046 /// corresponding to it.  In other words, we can evaluate {A,+,B,+,C,+,D} as:
1047 ///
1048 ///   A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
1049 ///
1050 /// where BC(It, k) stands for binomial coefficient.
1051 ///
1052 const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
1053                                                 ScalarEvolution &SE) const {
1054   const SCEV *Result = getStart();
1055   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1056     // The computation is correct in the face of overflow provided that the
1057     // multiplication is performed _after_ the evaluation of the binomial
1058     // coefficient.
1059     const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
1060     if (isa<SCEVCouldNotCompute>(Coeff))
1061       return Coeff;
1062
1063     Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
1064   }
1065   return Result;
1066 }
1067
1068 //===----------------------------------------------------------------------===//
1069 //                    SCEV Expression folder implementations
1070 //===----------------------------------------------------------------------===//
1071
1072 const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
1073                                              Type *Ty) {
1074   assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
1075          "This is not a truncating conversion!");
1076   assert(isSCEVable(Ty) &&
1077          "This is not a conversion to a SCEVable type!");
1078   Ty = getEffectiveSCEVType(Ty);
1079
1080   FoldingSetNodeID ID;
1081   ID.AddInteger(scTruncate);
1082   ID.AddPointer(Op);
1083   ID.AddPointer(Ty);
1084   void *IP = nullptr;
1085   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1086
1087   // Fold if the operand is constant.
1088   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1089     return getConstant(
1090       cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
1091
1092   // trunc(trunc(x)) --> trunc(x)
1093   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
1094     return getTruncateExpr(ST->getOperand(), Ty);
1095
1096   // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
1097   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1098     return getTruncateOrSignExtend(SS->getOperand(), Ty);
1099
1100   // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
1101   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1102     return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
1103
1104   // trunc(x1+x2+...+xN) --> trunc(x1)+trunc(x2)+...+trunc(xN) if we can
1105   // eliminate all the truncates.
1106   if (const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) {
1107     SmallVector<const SCEV *, 4> Operands;
1108     bool hasTrunc = false;
1109     for (unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) {
1110       const SCEV *S = getTruncateExpr(SA->getOperand(i), Ty);
1111       hasTrunc = isa<SCEVTruncateExpr>(S);
1112       Operands.push_back(S);
1113     }
1114     if (!hasTrunc)
1115       return getAddExpr(Operands);
1116     UniqueSCEVs.FindNodeOrInsertPos(ID, IP);  // Mutates IP, returns NULL.
1117   }
1118
1119   // trunc(x1*x2*...*xN) --> trunc(x1)*trunc(x2)*...*trunc(xN) if we can
1120   // eliminate all the truncates.
1121   if (const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) {
1122     SmallVector<const SCEV *, 4> Operands;
1123     bool hasTrunc = false;
1124     for (unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) {
1125       const SCEV *S = getTruncateExpr(SM->getOperand(i), Ty);
1126       hasTrunc = isa<SCEVTruncateExpr>(S);
1127       Operands.push_back(S);
1128     }
1129     if (!hasTrunc)
1130       return getMulExpr(Operands);
1131     UniqueSCEVs.FindNodeOrInsertPos(ID, IP);  // Mutates IP, returns NULL.
1132   }
1133
1134   // If the input value is a chrec scev, truncate the chrec's operands.
1135   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
1136     SmallVector<const SCEV *, 4> Operands;
1137     for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
1138       Operands.push_back(getTruncateExpr(AddRec->getOperand(i), Ty));
1139     return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
1140   }
1141
1142   // The cast wasn't folded; create an explicit cast node. We can reuse
1143   // the existing insert position since if we get here, we won't have
1144   // made any changes which would invalidate it.
1145   SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
1146                                                  Op, Ty);
1147   UniqueSCEVs.InsertNode(S, IP);
1148   return S;
1149 }
1150
1151 // Get the limit of a recurrence such that incrementing by Step cannot cause
1152 // signed overflow as long as the value of the recurrence within the
1153 // loop does not exceed this limit before incrementing.
1154 static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
1155                                                  ICmpInst::Predicate *Pred,
1156                                                  ScalarEvolution *SE) {
1157   unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1158   if (SE->isKnownPositive(Step)) {
1159     *Pred = ICmpInst::ICMP_SLT;
1160     return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
1161                            SE->getSignedRange(Step).getSignedMax());
1162   }
1163   if (SE->isKnownNegative(Step)) {
1164     *Pred = ICmpInst::ICMP_SGT;
1165     return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
1166                            SE->getSignedRange(Step).getSignedMin());
1167   }
1168   return nullptr;
1169 }
1170
1171 // Get the limit of a recurrence such that incrementing by Step cannot cause
1172 // unsigned overflow as long as the value of the recurrence within the loop does
1173 // not exceed this limit before incrementing.
1174 static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
1175                                                    ICmpInst::Predicate *Pred,
1176                                                    ScalarEvolution *SE) {
1177   unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1178   *Pred = ICmpInst::ICMP_ULT;
1179
1180   return SE->getConstant(APInt::getMinValue(BitWidth) -
1181                          SE->getUnsignedRange(Step).getUnsignedMax());
1182 }
1183
1184 namespace {
1185
1186 struct ExtendOpTraitsBase {
1187   typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *);
1188 };
1189
1190 // Used to make code generic over signed and unsigned overflow.
1191 template <typename ExtendOp> struct ExtendOpTraits {
1192   // Members present:
1193   //
1194   // static const SCEV::NoWrapFlags WrapType;
1195   //
1196   // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1197   //
1198   // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1199   //                                           ICmpInst::Predicate *Pred,
1200   //                                           ScalarEvolution *SE);
1201 };
1202
1203 template <>
1204 struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
1205   static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
1206
1207   static const GetExtendExprTy GetExtendExpr;
1208
1209   static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1210                                              ICmpInst::Predicate *Pred,
1211                                              ScalarEvolution *SE) {
1212     return getSignedOverflowLimitForStep(Step, Pred, SE);
1213   }
1214 };
1215
1216 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1217     SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
1218
1219 template <>
1220 struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
1221   static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
1222
1223   static const GetExtendExprTy GetExtendExpr;
1224
1225   static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1226                                              ICmpInst::Predicate *Pred,
1227                                              ScalarEvolution *SE) {
1228     return getUnsignedOverflowLimitForStep(Step, Pred, SE);
1229   }
1230 };
1231
1232 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1233     SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
1234 }
1235
1236 // The recurrence AR has been shown to have no signed/unsigned wrap or something
1237 // close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1238 // easily prove NSW/NUW for its preincrement or postincrement sibling. This
1239 // allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1240 // Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1241 // expression "Step + sext/zext(PreIncAR)" is congruent with
1242 // "sext/zext(PostIncAR)"
1243 template <typename ExtendOpTy>
1244 static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty,
1245                                         ScalarEvolution *SE) {
1246   auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1247   auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1248
1249   const Loop *L = AR->getLoop();
1250   const SCEV *Start = AR->getStart();
1251   const SCEV *Step = AR->getStepRecurrence(*SE);
1252
1253   // Check for a simple looking step prior to loop entry.
1254   const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1255   if (!SA)
1256     return nullptr;
1257
1258   // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1259   // subtraction is expensive. For this purpose, perform a quick and dirty
1260   // difference, by checking for Step in the operand list.
1261   SmallVector<const SCEV *, 4> DiffOps;
1262   for (const SCEV *Op : SA->operands())
1263     if (Op != Step)
1264       DiffOps.push_back(Op);
1265
1266   if (DiffOps.size() == SA->getNumOperands())
1267     return nullptr;
1268
1269   // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1270   // `Step`:
1271
1272   // 1. NSW/NUW flags on the step increment.
1273   const SCEV *PreStart = SE->getAddExpr(DiffOps, SA->getNoWrapFlags());
1274   const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1275       SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
1276
1277   // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1278   // "S+X does not sign/unsign-overflow".
1279   //
1280
1281   const SCEV *BECount = SE->getBackedgeTakenCount(L);
1282   if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
1283       !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
1284     return PreStart;
1285
1286   // 2. Direct overflow check on the step operation's expression.
1287   unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
1288   Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
1289   const SCEV *OperandExtendedStart =
1290       SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy),
1291                      (SE->*GetExtendExpr)(Step, WideTy));
1292   if ((SE->*GetExtendExpr)(Start, WideTy) == OperandExtendedStart) {
1293     if (PreAR && AR->getNoWrapFlags(WrapType)) {
1294       // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1295       // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1296       // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`.  Cache this fact.
1297       const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(WrapType);
1298     }
1299     return PreStart;
1300   }
1301
1302   // 3. Loop precondition.
1303   ICmpInst::Predicate Pred;
1304   const SCEV *OverflowLimit =
1305       ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1306
1307   if (OverflowLimit &&
1308       SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit)) {
1309     return PreStart;
1310   }
1311   return nullptr;
1312 }
1313
1314 // Get the normalized zero or sign extended expression for this AddRec's Start.
1315 template <typename ExtendOpTy>
1316 static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
1317                                         ScalarEvolution *SE) {
1318   auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1319
1320   const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE);
1321   if (!PreStart)
1322     return (SE->*GetExtendExpr)(AR->getStart(), Ty);
1323
1324   return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty),
1325                         (SE->*GetExtendExpr)(PreStart, Ty));
1326 }
1327
1328 // Try to prove away overflow by looking at "nearby" add recurrences.  A
1329 // motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1330 // does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1331 //
1332 // Formally:
1333 //
1334 //     {S,+,X} == {S-T,+,X} + T
1335 //  => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1336 //
1337 // If ({S-T,+,X} + T) does not overflow  ... (1)
1338 //
1339 //  RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1340 //
1341 // If {S-T,+,X} does not overflow  ... (2)
1342 //
1343 //  RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1344 //      == {Ext(S-T)+Ext(T),+,Ext(X)}
1345 //
1346 // If (S-T)+T does not overflow  ... (3)
1347 //
1348 //  RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1349 //      == {Ext(S),+,Ext(X)} == LHS
1350 //
1351 // Thus, if (1), (2) and (3) are true for some T, then
1352 //   Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1353 //
1354 // (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1355 // does not overflow" restricted to the 0th iteration.  Therefore we only need
1356 // to check for (1) and (2).
1357 //
1358 // In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1359 // is `Delta` (defined below).
1360 //
1361 template <typename ExtendOpTy>
1362 bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
1363                                                 const SCEV *Step,
1364                                                 const Loop *L) {
1365   auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1366
1367   // We restrict `Start` to a constant to prevent SCEV from spending too much
1368   // time here.  It is correct (but more expensive) to continue with a
1369   // non-constant `Start` and do a general SCEV subtraction to compute
1370   // `PreStart` below.
1371   //
1372   const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
1373   if (!StartC)
1374     return false;
1375
1376   APInt StartAI = StartC->getValue()->getValue();
1377
1378   for (unsigned Delta : {-2, -1, 1, 2}) {
1379     const SCEV *PreStart = getConstant(StartAI - Delta);
1380
1381     // Give up if we don't already have the add recurrence we need because
1382     // actually constructing an add recurrence is relatively expensive.
1383     const SCEVAddRecExpr *PreAR = [&]() {
1384       FoldingSetNodeID ID;
1385       ID.AddInteger(scAddRecExpr);
1386       ID.AddPointer(PreStart);
1387       ID.AddPointer(Step);
1388       ID.AddPointer(L);
1389       void *IP = nullptr;
1390       return static_cast<SCEVAddRecExpr *>(
1391         UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1392     }();
1393
1394     if (PreAR && PreAR->getNoWrapFlags(WrapType)) {  // proves (2)
1395       const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
1396       ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1397       const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1398           DeltaS, &Pred, this);
1399       if (Limit && isKnownPredicate(Pred, PreAR, Limit))  // proves (1)
1400         return true;
1401     }
1402   }
1403
1404   return false;
1405 }
1406
1407 const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op,
1408                                                Type *Ty) {
1409   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1410          "This is not an extending conversion!");
1411   assert(isSCEVable(Ty) &&
1412          "This is not a conversion to a SCEVable type!");
1413   Ty = getEffectiveSCEVType(Ty);
1414
1415   // Fold if the operand is constant.
1416   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1417     return getConstant(
1418       cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty)));
1419
1420   // zext(zext(x)) --> zext(x)
1421   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1422     return getZeroExtendExpr(SZ->getOperand(), Ty);
1423
1424   // Before doing any expensive analysis, check to see if we've already
1425   // computed a SCEV for this Op and Ty.
1426   FoldingSetNodeID ID;
1427   ID.AddInteger(scZeroExtend);
1428   ID.AddPointer(Op);
1429   ID.AddPointer(Ty);
1430   void *IP = nullptr;
1431   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1432
1433   // zext(trunc(x)) --> zext(x) or x or trunc(x)
1434   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1435     // It's possible the bits taken off by the truncate were all zero bits. If
1436     // so, we should be able to simplify this further.
1437     const SCEV *X = ST->getOperand();
1438     ConstantRange CR = getUnsignedRange(X);
1439     unsigned TruncBits = getTypeSizeInBits(ST->getType());
1440     unsigned NewBits = getTypeSizeInBits(Ty);
1441     if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
1442             CR.zextOrTrunc(NewBits)))
1443       return getTruncateOrZeroExtend(X, Ty);
1444   }
1445
1446   // If the input value is a chrec scev, and we can prove that the value
1447   // did not overflow the old, smaller, value, we can zero extend all of the
1448   // operands (often constants).  This allows analysis of something like
1449   // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
1450   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1451     if (AR->isAffine()) {
1452       const SCEV *Start = AR->getStart();
1453       const SCEV *Step = AR->getStepRecurrence(*this);
1454       unsigned BitWidth = getTypeSizeInBits(AR->getType());
1455       const Loop *L = AR->getLoop();
1456
1457       // If we have special knowledge that this addrec won't overflow,
1458       // we don't need to do any further analysis.
1459       if (AR->getNoWrapFlags(SCEV::FlagNUW))
1460         return getAddRecExpr(
1461             getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this),
1462             getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1463
1464       // Check whether the backedge-taken count is SCEVCouldNotCompute.
1465       // Note that this serves two purposes: It filters out loops that are
1466       // simply not analyzable, and it covers the case where this code is
1467       // being called from within backedge-taken count analysis, such that
1468       // attempting to ask for the backedge-taken count would likely result
1469       // in infinite recursion. In the later case, the analysis code will
1470       // cope with a conservative value, and it will take care to purge
1471       // that value once it has finished.
1472       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
1473       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1474         // Manually compute the final value for AR, checking for
1475         // overflow.
1476
1477         // Check whether the backedge-taken count can be losslessly casted to
1478         // the addrec's type. The count is always unsigned.
1479         const SCEV *CastedMaxBECount =
1480           getTruncateOrZeroExtend(MaxBECount, Start->getType());
1481         const SCEV *RecastedMaxBECount =
1482           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
1483         if (MaxBECount == RecastedMaxBECount) {
1484           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1485           // Check whether Start+Step*MaxBECount has no unsigned overflow.
1486           const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step);
1487           const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul), WideTy);
1488           const SCEV *WideStart = getZeroExtendExpr(Start, WideTy);
1489           const SCEV *WideMaxBECount =
1490             getZeroExtendExpr(CastedMaxBECount, WideTy);
1491           const SCEV *OperandExtendedAdd =
1492             getAddExpr(WideStart,
1493                        getMulExpr(WideMaxBECount,
1494                                   getZeroExtendExpr(Step, WideTy)));
1495           if (ZAdd == OperandExtendedAdd) {
1496             // Cache knowledge of AR NUW, which is propagated to this AddRec.
1497             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1498             // Return the expression with the addrec on the outside.
1499             return getAddRecExpr(
1500                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this),
1501                 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1502           }
1503           // Similar to above, only this time treat the step value as signed.
1504           // This covers loops that count down.
1505           OperandExtendedAdd =
1506             getAddExpr(WideStart,
1507                        getMulExpr(WideMaxBECount,
1508                                   getSignExtendExpr(Step, WideTy)));
1509           if (ZAdd == OperandExtendedAdd) {
1510             // Cache knowledge of AR NW, which is propagated to this AddRec.
1511             // Negative step causes unsigned wrap, but it still can't self-wrap.
1512             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1513             // Return the expression with the addrec on the outside.
1514             return getAddRecExpr(
1515                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this),
1516                 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1517           }
1518         }
1519
1520         // If the backedge is guarded by a comparison with the pre-inc value
1521         // the addrec is safe. Also, if the entry is guarded by a comparison
1522         // with the start value and the backedge is guarded by a comparison
1523         // with the post-inc value, the addrec is safe.
1524         if (isKnownPositive(Step)) {
1525           const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
1526                                       getUnsignedRange(Step).getUnsignedMax());
1527           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
1528               (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
1529                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
1530                                            AR->getPostIncExpr(*this), N))) {
1531             // Cache knowledge of AR NUW, which is propagated to this AddRec.
1532             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1533             // Return the expression with the addrec on the outside.
1534             return getAddRecExpr(
1535                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this),
1536                 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1537           }
1538         } else if (isKnownNegative(Step)) {
1539           const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
1540                                       getSignedRange(Step).getSignedMin());
1541           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
1542               (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) &&
1543                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
1544                                            AR->getPostIncExpr(*this), N))) {
1545             // Cache knowledge of AR NW, which is propagated to this AddRec.
1546             // Negative step causes unsigned wrap, but it still can't self-wrap.
1547             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1548             // Return the expression with the addrec on the outside.
1549             return getAddRecExpr(
1550                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this),
1551                 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1552           }
1553         }
1554       }
1555
1556       if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1557         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1558         return getAddRecExpr(
1559             getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this),
1560             getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1561       }
1562     }
1563
1564   // The cast wasn't folded; create an explicit cast node.
1565   // Recompute the insert position, as it may have been invalidated.
1566   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1567   SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1568                                                    Op, Ty);
1569   UniqueSCEVs.InsertNode(S, IP);
1570   return S;
1571 }
1572
1573 const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op,
1574                                                Type *Ty) {
1575   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1576          "This is not an extending conversion!");
1577   assert(isSCEVable(Ty) &&
1578          "This is not a conversion to a SCEVable type!");
1579   Ty = getEffectiveSCEVType(Ty);
1580
1581   // Fold if the operand is constant.
1582   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1583     return getConstant(
1584       cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty)));
1585
1586   // sext(sext(x)) --> sext(x)
1587   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1588     return getSignExtendExpr(SS->getOperand(), Ty);
1589
1590   // sext(zext(x)) --> zext(x)
1591   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1592     return getZeroExtendExpr(SZ->getOperand(), Ty);
1593
1594   // Before doing any expensive analysis, check to see if we've already
1595   // computed a SCEV for this Op and Ty.
1596   FoldingSetNodeID ID;
1597   ID.AddInteger(scSignExtend);
1598   ID.AddPointer(Op);
1599   ID.AddPointer(Ty);
1600   void *IP = nullptr;
1601   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1602
1603   // If the input value is provably positive, build a zext instead.
1604   if (isKnownNonNegative(Op))
1605     return getZeroExtendExpr(Op, Ty);
1606
1607   // sext(trunc(x)) --> sext(x) or x or trunc(x)
1608   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1609     // It's possible the bits taken off by the truncate were all sign bits. If
1610     // so, we should be able to simplify this further.
1611     const SCEV *X = ST->getOperand();
1612     ConstantRange CR = getSignedRange(X);
1613     unsigned TruncBits = getTypeSizeInBits(ST->getType());
1614     unsigned NewBits = getTypeSizeInBits(Ty);
1615     if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1616             CR.sextOrTrunc(NewBits)))
1617       return getTruncateOrSignExtend(X, Ty);
1618   }
1619
1620   // sext(C1 + (C2 * x)) --> C1 + sext(C2 * x) if C1 < C2
1621   if (auto SA = dyn_cast<SCEVAddExpr>(Op)) {
1622     if (SA->getNumOperands() == 2) {
1623       auto SC1 = dyn_cast<SCEVConstant>(SA->getOperand(0));
1624       auto SMul = dyn_cast<SCEVMulExpr>(SA->getOperand(1));
1625       if (SMul && SC1) {
1626         if (auto SC2 = dyn_cast<SCEVConstant>(SMul->getOperand(0))) {
1627           const APInt &C1 = SC1->getValue()->getValue();
1628           const APInt &C2 = SC2->getValue()->getValue();
1629           if (C1.isStrictlyPositive() && C2.isStrictlyPositive() &&
1630               C2.ugt(C1) && C2.isPowerOf2())
1631             return getAddExpr(getSignExtendExpr(SC1, Ty),
1632                               getSignExtendExpr(SMul, Ty));
1633         }
1634       }
1635     }
1636   }
1637   // If the input value is a chrec scev, and we can prove that the value
1638   // did not overflow the old, smaller, value, we can sign extend all of the
1639   // operands (often constants).  This allows analysis of something like
1640   // this:  for (signed char X = 0; X < 100; ++X) { int Y = X; }
1641   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1642     if (AR->isAffine()) {
1643       const SCEV *Start = AR->getStart();
1644       const SCEV *Step = AR->getStepRecurrence(*this);
1645       unsigned BitWidth = getTypeSizeInBits(AR->getType());
1646       const Loop *L = AR->getLoop();
1647
1648       // If we have special knowledge that this addrec won't overflow,
1649       // we don't need to do any further analysis.
1650       if (AR->getNoWrapFlags(SCEV::FlagNSW))
1651         return getAddRecExpr(
1652             getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this),
1653             getSignExtendExpr(Step, Ty), L, SCEV::FlagNSW);
1654
1655       // Check whether the backedge-taken count is SCEVCouldNotCompute.
1656       // Note that this serves two purposes: It filters out loops that are
1657       // simply not analyzable, and it covers the case where this code is
1658       // being called from within backedge-taken count analysis, such that
1659       // attempting to ask for the backedge-taken count would likely result
1660       // in infinite recursion. In the later case, the analysis code will
1661       // cope with a conservative value, and it will take care to purge
1662       // that value once it has finished.
1663       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
1664       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1665         // Manually compute the final value for AR, checking for
1666         // overflow.
1667
1668         // Check whether the backedge-taken count can be losslessly casted to
1669         // the addrec's type. The count is always unsigned.
1670         const SCEV *CastedMaxBECount =
1671           getTruncateOrZeroExtend(MaxBECount, Start->getType());
1672         const SCEV *RecastedMaxBECount =
1673           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
1674         if (MaxBECount == RecastedMaxBECount) {
1675           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1676           // Check whether Start+Step*MaxBECount has no signed overflow.
1677           const SCEV *SMul = getMulExpr(CastedMaxBECount, Step);
1678           const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul), WideTy);
1679           const SCEV *WideStart = getSignExtendExpr(Start, WideTy);
1680           const SCEV *WideMaxBECount =
1681             getZeroExtendExpr(CastedMaxBECount, WideTy);
1682           const SCEV *OperandExtendedAdd =
1683             getAddExpr(WideStart,
1684                        getMulExpr(WideMaxBECount,
1685                                   getSignExtendExpr(Step, WideTy)));
1686           if (SAdd == OperandExtendedAdd) {
1687             // Cache knowledge of AR NSW, which is propagated to this AddRec.
1688             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1689             // Return the expression with the addrec on the outside.
1690             return getAddRecExpr(
1691                 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this),
1692                 getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1693           }
1694           // Similar to above, only this time treat the step value as unsigned.
1695           // This covers loops that count up with an unsigned step.
1696           OperandExtendedAdd =
1697             getAddExpr(WideStart,
1698                        getMulExpr(WideMaxBECount,
1699                                   getZeroExtendExpr(Step, WideTy)));
1700           if (SAdd == OperandExtendedAdd) {
1701             // If AR wraps around then
1702             //
1703             //    abs(Step) * MaxBECount > unsigned-max(AR->getType())
1704             // => SAdd != OperandExtendedAdd
1705             //
1706             // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
1707             // (SAdd == OperandExtendedAdd => AR is NW)
1708
1709             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1710
1711             // Return the expression with the addrec on the outside.
1712             return getAddRecExpr(
1713                 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this),
1714                 getZeroExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1715           }
1716         }
1717
1718         // If the backedge is guarded by a comparison with the pre-inc value
1719         // the addrec is safe. Also, if the entry is guarded by a comparison
1720         // with the start value and the backedge is guarded by a comparison
1721         // with the post-inc value, the addrec is safe.
1722         ICmpInst::Predicate Pred;
1723         const SCEV *OverflowLimit =
1724             getSignedOverflowLimitForStep(Step, &Pred, this);
1725         if (OverflowLimit &&
1726             (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
1727              (isLoopEntryGuardedByCond(L, Pred, Start, OverflowLimit) &&
1728               isLoopBackedgeGuardedByCond(L, Pred, AR->getPostIncExpr(*this),
1729                                           OverflowLimit)))) {
1730           // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec.
1731           const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1732           return getAddRecExpr(
1733               getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this),
1734               getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1735         }
1736       }
1737       // If Start and Step are constants, check if we can apply this
1738       // transformation:
1739       // sext{C1,+,C2} --> C1 + sext{0,+,C2} if C1 < C2
1740       auto SC1 = dyn_cast<SCEVConstant>(Start);
1741       auto SC2 = dyn_cast<SCEVConstant>(Step);
1742       if (SC1 && SC2) {
1743         const APInt &C1 = SC1->getValue()->getValue();
1744         const APInt &C2 = SC2->getValue()->getValue();
1745         if (C1.isStrictlyPositive() && C2.isStrictlyPositive() && C2.ugt(C1) &&
1746             C2.isPowerOf2()) {
1747           Start = getSignExtendExpr(Start, Ty);
1748           const SCEV *NewAR = getAddRecExpr(getConstant(AR->getType(), 0), Step,
1749                                             L, AR->getNoWrapFlags());
1750           return getAddExpr(Start, getSignExtendExpr(NewAR, Ty));
1751         }
1752       }
1753
1754       if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
1755         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1756         return getAddRecExpr(
1757             getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this),
1758             getSignExtendExpr(Step, Ty), L, AR->getNoWrapFlags());
1759       }
1760     }
1761
1762   // The cast wasn't folded; create an explicit cast node.
1763   // Recompute the insert position, as it may have been invalidated.
1764   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1765   SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1766                                                    Op, Ty);
1767   UniqueSCEVs.InsertNode(S, IP);
1768   return S;
1769 }
1770
1771 /// getAnyExtendExpr - Return a SCEV for the given operand extended with
1772 /// unspecified bits out to the given type.
1773 ///
1774 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
1775                                               Type *Ty) {
1776   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1777          "This is not an extending conversion!");
1778   assert(isSCEVable(Ty) &&
1779          "This is not a conversion to a SCEVable type!");
1780   Ty = getEffectiveSCEVType(Ty);
1781
1782   // Sign-extend negative constants.
1783   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1784     if (SC->getValue()->getValue().isNegative())
1785       return getSignExtendExpr(Op, Ty);
1786
1787   // Peel off a truncate cast.
1788   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
1789     const SCEV *NewOp = T->getOperand();
1790     if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
1791       return getAnyExtendExpr(NewOp, Ty);
1792     return getTruncateOrNoop(NewOp, Ty);
1793   }
1794
1795   // Next try a zext cast. If the cast is folded, use it.
1796   const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
1797   if (!isa<SCEVZeroExtendExpr>(ZExt))
1798     return ZExt;
1799
1800   // Next try a sext cast. If the cast is folded, use it.
1801   const SCEV *SExt = getSignExtendExpr(Op, Ty);
1802   if (!isa<SCEVSignExtendExpr>(SExt))
1803     return SExt;
1804
1805   // Force the cast to be folded into the operands of an addrec.
1806   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
1807     SmallVector<const SCEV *, 4> Ops;
1808     for (const SCEV *Op : AR->operands())
1809       Ops.push_back(getAnyExtendExpr(Op, Ty));
1810     return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
1811   }
1812
1813   // If the expression is obviously signed, use the sext cast value.
1814   if (isa<SCEVSMaxExpr>(Op))
1815     return SExt;
1816
1817   // Absent any other information, use the zext cast value.
1818   return ZExt;
1819 }
1820
1821 /// CollectAddOperandsWithScales - Process the given Ops list, which is
1822 /// a list of operands to be added under the given scale, update the given
1823 /// map. This is a helper function for getAddRecExpr. As an example of
1824 /// what it does, given a sequence of operands that would form an add
1825 /// expression like this:
1826 ///
1827 ///    m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
1828 ///
1829 /// where A and B are constants, update the map with these values:
1830 ///
1831 ///    (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
1832 ///
1833 /// and add 13 + A*B*29 to AccumulatedConstant.
1834 /// This will allow getAddRecExpr to produce this:
1835 ///
1836 ///    13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
1837 ///
1838 /// This form often exposes folding opportunities that are hidden in
1839 /// the original operand list.
1840 ///
1841 /// Return true iff it appears that any interesting folding opportunities
1842 /// may be exposed. This helps getAddRecExpr short-circuit extra work in
1843 /// the common case where no interesting opportunities are present, and
1844 /// is also used as a check to avoid infinite recursion.
1845 ///
1846 static bool
1847 CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
1848                              SmallVectorImpl<const SCEV *> &NewOps,
1849                              APInt &AccumulatedConstant,
1850                              const SCEV *const *Ops, size_t NumOperands,
1851                              const APInt &Scale,
1852                              ScalarEvolution &SE) {
1853   bool Interesting = false;
1854
1855   // Iterate over the add operands. They are sorted, with constants first.
1856   unsigned i = 0;
1857   while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
1858     ++i;
1859     // Pull a buried constant out to the outside.
1860     if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
1861       Interesting = true;
1862     AccumulatedConstant += Scale * C->getValue()->getValue();
1863   }
1864
1865   // Next comes everything else. We're especially interested in multiplies
1866   // here, but they're in the middle, so just visit the rest with one loop.
1867   for (; i != NumOperands; ++i) {
1868     const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
1869     if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
1870       APInt NewScale =
1871         Scale * cast<SCEVConstant>(Mul->getOperand(0))->getValue()->getValue();
1872       if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
1873         // A multiplication of a constant with another add; recurse.
1874         const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
1875         Interesting |=
1876           CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
1877                                        Add->op_begin(), Add->getNumOperands(),
1878                                        NewScale, SE);
1879       } else {
1880         // A multiplication of a constant with some other value. Update
1881         // the map.
1882         SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
1883         const SCEV *Key = SE.getMulExpr(MulOps);
1884         std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
1885           M.insert(std::make_pair(Key, NewScale));
1886         if (Pair.second) {
1887           NewOps.push_back(Pair.first->first);
1888         } else {
1889           Pair.first->second += NewScale;
1890           // The map already had an entry for this value, which may indicate
1891           // a folding opportunity.
1892           Interesting = true;
1893         }
1894       }
1895     } else {
1896       // An ordinary operand. Update the map.
1897       std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
1898         M.insert(std::make_pair(Ops[i], Scale));
1899       if (Pair.second) {
1900         NewOps.push_back(Pair.first->first);
1901       } else {
1902         Pair.first->second += Scale;
1903         // The map already had an entry for this value, which may indicate
1904         // a folding opportunity.
1905         Interesting = true;
1906       }
1907     }
1908   }
1909
1910   return Interesting;
1911 }
1912
1913 namespace {
1914   struct APIntCompare {
1915     bool operator()(const APInt &LHS, const APInt &RHS) const {
1916       return LHS.ult(RHS);
1917     }
1918   };
1919 }
1920
1921 // We're trying to construct a SCEV of type `Type' with `Ops' as operands and
1922 // `OldFlags' as can't-wrap behavior.  Infer a more aggressive set of
1923 // can't-overflow flags for the operation if possible.
1924 static SCEV::NoWrapFlags
1925 StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
1926                       const SmallVectorImpl<const SCEV *> &Ops,
1927                       SCEV::NoWrapFlags OldFlags) {
1928   using namespace std::placeholders;
1929
1930   bool CanAnalyze =
1931       Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr;
1932   (void)CanAnalyze;
1933   assert(CanAnalyze && "don't call from other places!");
1934
1935   int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
1936   SCEV::NoWrapFlags SignOrUnsignWrap =
1937       ScalarEvolution::maskFlags(OldFlags, SignOrUnsignMask);
1938
1939   // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
1940   auto IsKnownNonNegative =
1941     std::bind(std::mem_fn(&ScalarEvolution::isKnownNonNegative), SE, _1);
1942
1943   if (SignOrUnsignWrap == SCEV::FlagNSW &&
1944       std::all_of(Ops.begin(), Ops.end(), IsKnownNonNegative))
1945     return ScalarEvolution::setFlags(OldFlags,
1946                                      (SCEV::NoWrapFlags)SignOrUnsignMask);
1947
1948   return OldFlags;
1949 }
1950
1951 /// getAddExpr - Get a canonical add expression, or something simpler if
1952 /// possible.
1953 const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
1954                                         SCEV::NoWrapFlags Flags) {
1955   assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
1956          "only nuw or nsw allowed");
1957   assert(!Ops.empty() && "Cannot get empty add!");
1958   if (Ops.size() == 1) return Ops[0];
1959 #ifndef NDEBUG
1960   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
1961   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
1962     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
1963            "SCEVAddExpr operand types don't match!");
1964 #endif
1965
1966   Flags = StrengthenNoWrapFlags(this, scAddExpr, Ops, Flags);
1967
1968   // Sort by complexity, this groups all similar expression types together.
1969   GroupByComplexity(Ops, LI);
1970
1971   // If there are any constants, fold them together.
1972   unsigned Idx = 0;
1973   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
1974     ++Idx;
1975     assert(Idx < Ops.size());
1976     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
1977       // We found two constants, fold them together!
1978       Ops[0] = getConstant(LHSC->getValue()->getValue() +
1979                            RHSC->getValue()->getValue());
1980       if (Ops.size() == 2) return Ops[0];
1981       Ops.erase(Ops.begin()+1);  // Erase the folded element
1982       LHSC = cast<SCEVConstant>(Ops[0]);
1983     }
1984
1985     // If we are left with a constant zero being added, strip it off.
1986     if (LHSC->getValue()->isZero()) {
1987       Ops.erase(Ops.begin());
1988       --Idx;
1989     }
1990
1991     if (Ops.size() == 1) return Ops[0];
1992   }
1993
1994   // Okay, check to see if the same value occurs in the operand list more than
1995   // once.  If so, merge them together into an multiply expression.  Since we
1996   // sorted the list, these values are required to be adjacent.
1997   Type *Ty = Ops[0]->getType();
1998   bool FoundMatch = false;
1999   for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
2000     if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2
2001       // Scan ahead to count how many equal operands there are.
2002       unsigned Count = 2;
2003       while (i+Count != e && Ops[i+Count] == Ops[i])
2004         ++Count;
2005       // Merge the values into a multiply.
2006       const SCEV *Scale = getConstant(Ty, Count);
2007       const SCEV *Mul = getMulExpr(Scale, Ops[i]);
2008       if (Ops.size() == Count)
2009         return Mul;
2010       Ops[i] = Mul;
2011       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
2012       --i; e -= Count - 1;
2013       FoundMatch = true;
2014     }
2015   if (FoundMatch)
2016     return getAddExpr(Ops, Flags);
2017
2018   // Check for truncates. If all the operands are truncated from the same
2019   // type, see if factoring out the truncate would permit the result to be
2020   // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
2021   // if the contents of the resulting outer trunc fold to something simple.
2022   for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
2023     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
2024     Type *DstType = Trunc->getType();
2025     Type *SrcType = Trunc->getOperand()->getType();
2026     SmallVector<const SCEV *, 8> LargeOps;
2027     bool Ok = true;
2028     // Check all the operands to see if they can be represented in the
2029     // source type of the truncate.
2030     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
2031       if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
2032         if (T->getOperand()->getType() != SrcType) {
2033           Ok = false;
2034           break;
2035         }
2036         LargeOps.push_back(T->getOperand());
2037       } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2038         LargeOps.push_back(getAnyExtendExpr(C, SrcType));
2039       } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
2040         SmallVector<const SCEV *, 8> LargeMulOps;
2041         for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2042           if (const SCEVTruncateExpr *T =
2043                 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2044             if (T->getOperand()->getType() != SrcType) {
2045               Ok = false;
2046               break;
2047             }
2048             LargeMulOps.push_back(T->getOperand());
2049           } else if (const SCEVConstant *C =
2050                        dyn_cast<SCEVConstant>(M->getOperand(j))) {
2051             LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
2052           } else {
2053             Ok = false;
2054             break;
2055           }
2056         }
2057         if (Ok)
2058           LargeOps.push_back(getMulExpr(LargeMulOps));
2059       } else {
2060         Ok = false;
2061         break;
2062       }
2063     }
2064     if (Ok) {
2065       // Evaluate the expression in the larger type.
2066       const SCEV *Fold = getAddExpr(LargeOps, Flags);
2067       // If it folds to something simple, use it. Otherwise, don't.
2068       if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
2069         return getTruncateExpr(Fold, DstType);
2070     }
2071   }
2072
2073   // Skip past any other cast SCEVs.
2074   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
2075     ++Idx;
2076
2077   // If there are add operands they would be next.
2078   if (Idx < Ops.size()) {
2079     bool DeletedAdd = false;
2080     while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
2081       // If we have an add, expand the add operands onto the end of the operands
2082       // list.
2083       Ops.erase(Ops.begin()+Idx);
2084       Ops.append(Add->op_begin(), Add->op_end());
2085       DeletedAdd = true;
2086     }
2087
2088     // If we deleted at least one add, we added operands to the end of the list,
2089     // and they are not necessarily sorted.  Recurse to resort and resimplify
2090     // any operands we just acquired.
2091     if (DeletedAdd)
2092       return getAddExpr(Ops);
2093   }
2094
2095   // Skip over the add expression until we get to a multiply.
2096   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2097     ++Idx;
2098
2099   // Check to see if there are any folding opportunities present with
2100   // operands multiplied by constant values.
2101   if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
2102     uint64_t BitWidth = getTypeSizeInBits(Ty);
2103     DenseMap<const SCEV *, APInt> M;
2104     SmallVector<const SCEV *, 8> NewOps;
2105     APInt AccumulatedConstant(BitWidth, 0);
2106     if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2107                                      Ops.data(), Ops.size(),
2108                                      APInt(BitWidth, 1), *this)) {
2109       // Some interesting folding opportunity is present, so its worthwhile to
2110       // re-generate the operands list. Group the operands by constant scale,
2111       // to avoid multiplying by the same constant scale multiple times.
2112       std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
2113       for (SmallVectorImpl<const SCEV *>::const_iterator I = NewOps.begin(),
2114            E = NewOps.end(); I != E; ++I)
2115         MulOpLists[M.find(*I)->second].push_back(*I);
2116       // Re-generate the operands list.
2117       Ops.clear();
2118       if (AccumulatedConstant != 0)
2119         Ops.push_back(getConstant(AccumulatedConstant));
2120       for (std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare>::iterator
2121            I = MulOpLists.begin(), E = MulOpLists.end(); I != E; ++I)
2122         if (I->first != 0)
2123           Ops.push_back(getMulExpr(getConstant(I->first),
2124                                    getAddExpr(I->second)));
2125       if (Ops.empty())
2126         return getConstant(Ty, 0);
2127       if (Ops.size() == 1)
2128         return Ops[0];
2129       return getAddExpr(Ops);
2130     }
2131   }
2132
2133   // If we are adding something to a multiply expression, make sure the
2134   // something is not already an operand of the multiply.  If so, merge it into
2135   // the multiply.
2136   for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
2137     const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
2138     for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
2139       const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
2140       if (isa<SCEVConstant>(MulOpSCEV))
2141         continue;
2142       for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
2143         if (MulOpSCEV == Ops[AddOp]) {
2144           // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1))
2145           const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
2146           if (Mul->getNumOperands() != 2) {
2147             // If the multiply has more than two operands, we must get the
2148             // Y*Z term.
2149             SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2150                                                 Mul->op_begin()+MulOp);
2151             MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2152             InnerMul = getMulExpr(MulOps);
2153           }
2154           const SCEV *One = getConstant(Ty, 1);
2155           const SCEV *AddOne = getAddExpr(One, InnerMul);
2156           const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV);
2157           if (Ops.size() == 2) return OuterMul;
2158           if (AddOp < Idx) {
2159             Ops.erase(Ops.begin()+AddOp);
2160             Ops.erase(Ops.begin()+Idx-1);
2161           } else {
2162             Ops.erase(Ops.begin()+Idx);
2163             Ops.erase(Ops.begin()+AddOp-1);
2164           }
2165           Ops.push_back(OuterMul);
2166           return getAddExpr(Ops);
2167         }
2168
2169       // Check this multiply against other multiplies being added together.
2170       for (unsigned OtherMulIdx = Idx+1;
2171            OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
2172            ++OtherMulIdx) {
2173         const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
2174         // If MulOp occurs in OtherMul, we can fold the two multiplies
2175         // together.
2176         for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
2177              OMulOp != e; ++OMulOp)
2178           if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
2179             // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
2180             const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
2181             if (Mul->getNumOperands() != 2) {
2182               SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2183                                                   Mul->op_begin()+MulOp);
2184               MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2185               InnerMul1 = getMulExpr(MulOps);
2186             }
2187             const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
2188             if (OtherMul->getNumOperands() != 2) {
2189               SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
2190                                                   OtherMul->op_begin()+OMulOp);
2191               MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end());
2192               InnerMul2 = getMulExpr(MulOps);
2193             }
2194             const SCEV *InnerMulSum = getAddExpr(InnerMul1,InnerMul2);
2195             const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum);
2196             if (Ops.size() == 2) return OuterMul;
2197             Ops.erase(Ops.begin()+Idx);
2198             Ops.erase(Ops.begin()+OtherMulIdx-1);
2199             Ops.push_back(OuterMul);
2200             return getAddExpr(Ops);
2201           }
2202       }
2203     }
2204   }
2205
2206   // If there are any add recurrences in the operands list, see if any other
2207   // added values are loop invariant.  If so, we can fold them into the
2208   // recurrence.
2209   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2210     ++Idx;
2211
2212   // Scan over all recurrences, trying to fold loop invariants into them.
2213   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2214     // Scan all of the other operands to this add and add them to the vector if
2215     // they are loop invariant w.r.t. the recurrence.
2216     SmallVector<const SCEV *, 8> LIOps;
2217     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2218     const Loop *AddRecLoop = AddRec->getLoop();
2219     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2220       if (isLoopInvariant(Ops[i], AddRecLoop)) {
2221         LIOps.push_back(Ops[i]);
2222         Ops.erase(Ops.begin()+i);
2223         --i; --e;
2224       }
2225
2226     // If we found some loop invariants, fold them into the recurrence.
2227     if (!LIOps.empty()) {
2228       //  NLI + LI + {Start,+,Step}  -->  NLI + {LI+Start,+,Step}
2229       LIOps.push_back(AddRec->getStart());
2230
2231       SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
2232                                              AddRec->op_end());
2233       AddRecOps[0] = getAddExpr(LIOps);
2234
2235       // Build the new addrec. Propagate the NUW and NSW flags if both the
2236       // outer add and the inner addrec are guaranteed to have no overflow.
2237       // Always propagate NW.
2238       Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
2239       const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
2240
2241       // If all of the other operands were loop invariant, we are done.
2242       if (Ops.size() == 1) return NewRec;
2243
2244       // Otherwise, add the folded AddRec by the non-invariant parts.
2245       for (unsigned i = 0;; ++i)
2246         if (Ops[i] == AddRec) {
2247           Ops[i] = NewRec;
2248           break;
2249         }
2250       return getAddExpr(Ops);
2251     }
2252
2253     // Okay, if there weren't any loop invariants to be folded, check to see if
2254     // there are multiple AddRec's with the same loop induction variable being
2255     // added together.  If so, we can fold them.
2256     for (unsigned OtherIdx = Idx+1;
2257          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2258          ++OtherIdx)
2259       if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2260         // Other + {A,+,B}<L> + {C,+,D}<L>  -->  Other + {A+C,+,B+D}<L>
2261         SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
2262                                                AddRec->op_end());
2263         for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2264              ++OtherIdx)
2265           if (const SCEVAddRecExpr *OtherAddRec =
2266                 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]))
2267             if (OtherAddRec->getLoop() == AddRecLoop) {
2268               for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2269                    i != e; ++i) {
2270                 if (i >= AddRecOps.size()) {
2271                   AddRecOps.append(OtherAddRec->op_begin()+i,
2272                                    OtherAddRec->op_end());
2273                   break;
2274                 }
2275                 AddRecOps[i] = getAddExpr(AddRecOps[i],
2276                                           OtherAddRec->getOperand(i));
2277               }
2278               Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2279             }
2280         // Step size has changed, so we cannot guarantee no self-wraparound.
2281         Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
2282         return getAddExpr(Ops);
2283       }
2284
2285     // Otherwise couldn't fold anything into this recurrence.  Move onto the
2286     // next one.
2287   }
2288
2289   // Okay, it looks like we really DO need an add expr.  Check to see if we
2290   // already have one, otherwise create a new one.
2291   FoldingSetNodeID ID;
2292   ID.AddInteger(scAddExpr);
2293   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2294     ID.AddPointer(Ops[i]);
2295   void *IP = nullptr;
2296   SCEVAddExpr *S =
2297     static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2298   if (!S) {
2299     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2300     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2301     S = new (SCEVAllocator) SCEVAddExpr(ID.Intern(SCEVAllocator),
2302                                         O, Ops.size());
2303     UniqueSCEVs.InsertNode(S, IP);
2304   }
2305   S->setNoWrapFlags(Flags);
2306   return S;
2307 }
2308
2309 static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
2310   uint64_t k = i*j;
2311   if (j > 1 && k / j != i) Overflow = true;
2312   return k;
2313 }
2314
2315 /// Compute the result of "n choose k", the binomial coefficient.  If an
2316 /// intermediate computation overflows, Overflow will be set and the return will
2317 /// be garbage. Overflow is not cleared on absence of overflow.
2318 static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
2319   // We use the multiplicative formula:
2320   //     n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
2321   // At each iteration, we take the n-th term of the numeral and divide by the
2322   // (k-n)th term of the denominator.  This division will always produce an
2323   // integral result, and helps reduce the chance of overflow in the
2324   // intermediate computations. However, we can still overflow even when the
2325   // final result would fit.
2326
2327   if (n == 0 || n == k) return 1;
2328   if (k > n) return 0;
2329
2330   if (k > n/2)
2331     k = n-k;
2332
2333   uint64_t r = 1;
2334   for (uint64_t i = 1; i <= k; ++i) {
2335     r = umul_ov(r, n-(i-1), Overflow);
2336     r /= i;
2337   }
2338   return r;
2339 }
2340
2341 /// Determine if any of the operands in this SCEV are a constant or if
2342 /// any of the add or multiply expressions in this SCEV contain a constant.
2343 static bool containsConstantSomewhere(const SCEV *StartExpr) {
2344   SmallVector<const SCEV *, 4> Ops;
2345   Ops.push_back(StartExpr);
2346   while (!Ops.empty()) {
2347     const SCEV *CurrentExpr = Ops.pop_back_val();
2348     if (isa<SCEVConstant>(*CurrentExpr))
2349       return true;
2350
2351     if (isa<SCEVAddExpr>(*CurrentExpr) || isa<SCEVMulExpr>(*CurrentExpr)) {
2352       const auto *CurrentNAry = cast<SCEVNAryExpr>(CurrentExpr);
2353       Ops.append(CurrentNAry->op_begin(), CurrentNAry->op_end());
2354     }
2355   }
2356   return false;
2357 }
2358
2359 /// getMulExpr - Get a canonical multiply expression, or something simpler if
2360 /// possible.
2361 const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
2362                                         SCEV::NoWrapFlags Flags) {
2363   assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) &&
2364          "only nuw or nsw allowed");
2365   assert(!Ops.empty() && "Cannot get empty mul!");
2366   if (Ops.size() == 1) return Ops[0];
2367 #ifndef NDEBUG
2368   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2369   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2370     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2371            "SCEVMulExpr operand types don't match!");
2372 #endif
2373
2374   Flags = StrengthenNoWrapFlags(this, scMulExpr, Ops, Flags);
2375
2376   // Sort by complexity, this groups all similar expression types together.
2377   GroupByComplexity(Ops, LI);
2378
2379   // If there are any constants, fold them together.
2380   unsigned Idx = 0;
2381   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2382
2383     // C1*(C2+V) -> C1*C2 + C1*V
2384     if (Ops.size() == 2)
2385         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
2386           // If any of Add's ops are Adds or Muls with a constant,
2387           // apply this transformation as well.
2388           if (Add->getNumOperands() == 2)
2389             if (containsConstantSomewhere(Add))
2390               return getAddExpr(getMulExpr(LHSC, Add->getOperand(0)),
2391                                 getMulExpr(LHSC, Add->getOperand(1)));
2392
2393     ++Idx;
2394     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2395       // We found two constants, fold them together!
2396       ConstantInt *Fold = ConstantInt::get(getContext(),
2397                                            LHSC->getValue()->getValue() *
2398                                            RHSC->getValue()->getValue());
2399       Ops[0] = getConstant(Fold);
2400       Ops.erase(Ops.begin()+1);  // Erase the folded element
2401       if (Ops.size() == 1) return Ops[0];
2402       LHSC = cast<SCEVConstant>(Ops[0]);
2403     }
2404
2405     // If we are left with a constant one being multiplied, strip it off.
2406     if (cast<SCEVConstant>(Ops[0])->getValue()->equalsInt(1)) {
2407       Ops.erase(Ops.begin());
2408       --Idx;
2409     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
2410       // If we have a multiply of zero, it will always be zero.
2411       return Ops[0];
2412     } else if (Ops[0]->isAllOnesValue()) {
2413       // If we have a mul by -1 of an add, try distributing the -1 among the
2414       // add operands.
2415       if (Ops.size() == 2) {
2416         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
2417           SmallVector<const SCEV *, 4> NewOps;
2418           bool AnyFolded = false;
2419           for (SCEVAddRecExpr::op_iterator I = Add->op_begin(),
2420                  E = Add->op_end(); I != E; ++I) {
2421             const SCEV *Mul = getMulExpr(Ops[0], *I);
2422             if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
2423             NewOps.push_back(Mul);
2424           }
2425           if (AnyFolded)
2426             return getAddExpr(NewOps);
2427         }
2428         else if (const SCEVAddRecExpr *
2429                  AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
2430           // Negation preserves a recurrence's no self-wrap property.
2431           SmallVector<const SCEV *, 4> Operands;
2432           for (SCEVAddRecExpr::op_iterator I = AddRec->op_begin(),
2433                  E = AddRec->op_end(); I != E; ++I) {
2434             Operands.push_back(getMulExpr(Ops[0], *I));
2435           }
2436           return getAddRecExpr(Operands, AddRec->getLoop(),
2437                                AddRec->getNoWrapFlags(SCEV::FlagNW));
2438         }
2439       }
2440     }
2441
2442     if (Ops.size() == 1)
2443       return Ops[0];
2444   }
2445
2446   // Skip over the add expression until we get to a multiply.
2447   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2448     ++Idx;
2449
2450   // If there are mul operands inline them all into this expression.
2451   if (Idx < Ops.size()) {
2452     bool DeletedMul = false;
2453     while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2454       // If we have an mul, expand the mul operands onto the end of the operands
2455       // list.
2456       Ops.erase(Ops.begin()+Idx);
2457       Ops.append(Mul->op_begin(), Mul->op_end());
2458       DeletedMul = true;
2459     }
2460
2461     // If we deleted at least one mul, we added operands to the end of the list,
2462     // and they are not necessarily sorted.  Recurse to resort and resimplify
2463     // any operands we just acquired.
2464     if (DeletedMul)
2465       return getMulExpr(Ops);
2466   }
2467
2468   // If there are any add recurrences in the operands list, see if any other
2469   // added values are loop invariant.  If so, we can fold them into the
2470   // recurrence.
2471   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2472     ++Idx;
2473
2474   // Scan over all recurrences, trying to fold loop invariants into them.
2475   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2476     // Scan all of the other operands to this mul and add them to the vector if
2477     // they are loop invariant w.r.t. the recurrence.
2478     SmallVector<const SCEV *, 8> LIOps;
2479     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2480     const Loop *AddRecLoop = AddRec->getLoop();
2481     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2482       if (isLoopInvariant(Ops[i], AddRecLoop)) {
2483         LIOps.push_back(Ops[i]);
2484         Ops.erase(Ops.begin()+i);
2485         --i; --e;
2486       }
2487
2488     // If we found some loop invariants, fold them into the recurrence.
2489     if (!LIOps.empty()) {
2490       //  NLI * LI * {Start,+,Step}  -->  NLI * {LI*Start,+,LI*Step}
2491       SmallVector<const SCEV *, 4> NewOps;
2492       NewOps.reserve(AddRec->getNumOperands());
2493       const SCEV *Scale = getMulExpr(LIOps);
2494       for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
2495         NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i)));
2496
2497       // Build the new addrec. Propagate the NUW and NSW flags if both the
2498       // outer mul and the inner addrec are guaranteed to have no overflow.
2499       //
2500       // No self-wrap cannot be guaranteed after changing the step size, but
2501       // will be inferred if either NUW or NSW is true.
2502       Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW));
2503       const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags);
2504
2505       // If all of the other operands were loop invariant, we are done.
2506       if (Ops.size() == 1) return NewRec;
2507
2508       // Otherwise, multiply the folded AddRec by the non-invariant parts.
2509       for (unsigned i = 0;; ++i)
2510         if (Ops[i] == AddRec) {
2511           Ops[i] = NewRec;
2512           break;
2513         }
2514       return getMulExpr(Ops);
2515     }
2516
2517     // Okay, if there weren't any loop invariants to be folded, check to see if
2518     // there are multiple AddRec's with the same loop induction variable being
2519     // multiplied together.  If so, we can fold them.
2520
2521     // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
2522     // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
2523     //       choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
2524     //   ]]],+,...up to x=2n}.
2525     // Note that the arguments to choose() are always integers with values
2526     // known at compile time, never SCEV objects.
2527     //
2528     // The implementation avoids pointless extra computations when the two
2529     // addrec's are of different length (mathematically, it's equivalent to
2530     // an infinite stream of zeros on the right).
2531     bool OpsModified = false;
2532     for (unsigned OtherIdx = Idx+1;
2533          OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2534          ++OtherIdx) {
2535       const SCEVAddRecExpr *OtherAddRec =
2536         dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2537       if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop)
2538         continue;
2539
2540       bool Overflow = false;
2541       Type *Ty = AddRec->getType();
2542       bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
2543       SmallVector<const SCEV*, 7> AddRecOps;
2544       for (int x = 0, xe = AddRec->getNumOperands() +
2545              OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
2546         const SCEV *Term = getConstant(Ty, 0);
2547         for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
2548           uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
2549           for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
2550                  ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
2551                z < ze && !Overflow; ++z) {
2552             uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
2553             uint64_t Coeff;
2554             if (LargerThan64Bits)
2555               Coeff = umul_ov(Coeff1, Coeff2, Overflow);
2556             else
2557               Coeff = Coeff1*Coeff2;
2558             const SCEV *CoeffTerm = getConstant(Ty, Coeff);
2559             const SCEV *Term1 = AddRec->getOperand(y-z);
2560             const SCEV *Term2 = OtherAddRec->getOperand(z);
2561             Term = getAddExpr(Term, getMulExpr(CoeffTerm, Term1,Term2));
2562           }
2563         }
2564         AddRecOps.push_back(Term);
2565       }
2566       if (!Overflow) {
2567         const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(),
2568                                               SCEV::FlagAnyWrap);
2569         if (Ops.size() == 2) return NewAddRec;
2570         Ops[Idx] = NewAddRec;
2571         Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2572         OpsModified = true;
2573         AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
2574         if (!AddRec)
2575           break;
2576       }
2577     }
2578     if (OpsModified)
2579       return getMulExpr(Ops);
2580
2581     // Otherwise couldn't fold anything into this recurrence.  Move onto the
2582     // next one.
2583   }
2584
2585   // Okay, it looks like we really DO need an mul expr.  Check to see if we
2586   // already have one, otherwise create a new one.
2587   FoldingSetNodeID ID;
2588   ID.AddInteger(scMulExpr);
2589   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2590     ID.AddPointer(Ops[i]);
2591   void *IP = nullptr;
2592   SCEVMulExpr *S =
2593     static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2594   if (!S) {
2595     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2596     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2597     S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
2598                                         O, Ops.size());
2599     UniqueSCEVs.InsertNode(S, IP);
2600   }
2601   S->setNoWrapFlags(Flags);
2602   return S;
2603 }
2604
2605 /// getUDivExpr - Get a canonical unsigned division expression, or something
2606 /// simpler if possible.
2607 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
2608                                          const SCEV *RHS) {
2609   assert(getEffectiveSCEVType(LHS->getType()) ==
2610          getEffectiveSCEVType(RHS->getType()) &&
2611          "SCEVUDivExpr operand types don't match!");
2612
2613   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
2614     if (RHSC->getValue()->equalsInt(1))
2615       return LHS;                               // X udiv 1 --> x
2616     // If the denominator is zero, the result of the udiv is undefined. Don't
2617     // try to analyze it, because the resolution chosen here may differ from
2618     // the resolution chosen in other parts of the compiler.
2619     if (!RHSC->getValue()->isZero()) {
2620       // Determine if the division can be folded into the operands of
2621       // its operands.
2622       // TODO: Generalize this to non-constants by using known-bits information.
2623       Type *Ty = LHS->getType();
2624       unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
2625       unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
2626       // For non-power-of-two values, effectively round the value up to the
2627       // nearest power of two.
2628       if (!RHSC->getValue()->getValue().isPowerOf2())
2629         ++MaxShiftAmt;
2630       IntegerType *ExtTy =
2631         IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
2632       if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
2633         if (const SCEVConstant *Step =
2634             dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
2635           // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
2636           const APInt &StepInt = Step->getValue()->getValue();
2637           const APInt &DivInt = RHSC->getValue()->getValue();
2638           if (!StepInt.urem(DivInt) &&
2639               getZeroExtendExpr(AR, ExtTy) ==
2640               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
2641                             getZeroExtendExpr(Step, ExtTy),
2642                             AR->getLoop(), SCEV::FlagAnyWrap)) {
2643             SmallVector<const SCEV *, 4> Operands;
2644             for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
2645               Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
2646             return getAddRecExpr(Operands, AR->getLoop(),
2647                                  SCEV::FlagNW);
2648           }
2649           /// Get a canonical UDivExpr for a recurrence.
2650           /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
2651           // We can currently only fold X%N if X is constant.
2652           const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
2653           if (StartC && !DivInt.urem(StepInt) &&
2654               getZeroExtendExpr(AR, ExtTy) ==
2655               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
2656                             getZeroExtendExpr(Step, ExtTy),
2657                             AR->getLoop(), SCEV::FlagAnyWrap)) {
2658             const APInt &StartInt = StartC->getValue()->getValue();
2659             const APInt &StartRem = StartInt.urem(StepInt);
2660             if (StartRem != 0)
2661               LHS = getAddRecExpr(getConstant(StartInt - StartRem), Step,
2662                                   AR->getLoop(), SCEV::FlagNW);
2663           }
2664         }
2665       // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
2666       if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
2667         SmallVector<const SCEV *, 4> Operands;
2668         for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
2669           Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
2670         if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
2671           // Find an operand that's safely divisible.
2672           for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
2673             const SCEV *Op = M->getOperand(i);
2674             const SCEV *Div = getUDivExpr(Op, RHSC);
2675             if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
2676               Operands = SmallVector<const SCEV *, 4>(M->op_begin(),
2677                                                       M->op_end());
2678               Operands[i] = Div;
2679               return getMulExpr(Operands);
2680             }
2681           }
2682       }
2683       // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
2684       if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
2685         SmallVector<const SCEV *, 4> Operands;
2686         for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
2687           Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
2688         if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
2689           Operands.clear();
2690           for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
2691             const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
2692             if (isa<SCEVUDivExpr>(Op) ||
2693                 getMulExpr(Op, RHS) != A->getOperand(i))
2694               break;
2695             Operands.push_back(Op);
2696           }
2697           if (Operands.size() == A->getNumOperands())
2698             return getAddExpr(Operands);
2699         }
2700       }
2701
2702       // Fold if both operands are constant.
2703       if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
2704         Constant *LHSCV = LHSC->getValue();
2705         Constant *RHSCV = RHSC->getValue();
2706         return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
2707                                                                    RHSCV)));
2708       }
2709     }
2710   }
2711
2712   FoldingSetNodeID ID;
2713   ID.AddInteger(scUDivExpr);
2714   ID.AddPointer(LHS);
2715   ID.AddPointer(RHS);
2716   void *IP = nullptr;
2717   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2718   SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
2719                                              LHS, RHS);
2720   UniqueSCEVs.InsertNode(S, IP);
2721   return S;
2722 }
2723
2724 static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) {
2725   APInt A = C1->getValue()->getValue().abs();
2726   APInt B = C2->getValue()->getValue().abs();
2727   uint32_t ABW = A.getBitWidth();
2728   uint32_t BBW = B.getBitWidth();
2729
2730   if (ABW > BBW)
2731     B = B.zext(ABW);
2732   else if (ABW < BBW)
2733     A = A.zext(BBW);
2734
2735   return APIntOps::GreatestCommonDivisor(A, B);
2736 }
2737
2738 /// getUDivExactExpr - Get a canonical unsigned division expression, or
2739 /// something simpler if possible. There is no representation for an exact udiv
2740 /// in SCEV IR, but we can attempt to remove factors from the LHS and RHS.
2741 /// We can't do this when it's not exact because the udiv may be clearing bits.
2742 const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS,
2743                                               const SCEV *RHS) {
2744   // TODO: we could try to find factors in all sorts of things, but for now we
2745   // just deal with u/exact (multiply, constant). See SCEVDivision towards the
2746   // end of this file for inspiration.
2747
2748   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS);
2749   if (!Mul)
2750     return getUDivExpr(LHS, RHS);
2751
2752   if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
2753     // If the mulexpr multiplies by a constant, then that constant must be the
2754     // first element of the mulexpr.
2755     if (const SCEVConstant *LHSCst =
2756             dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
2757       if (LHSCst == RHSCst) {
2758         SmallVector<const SCEV *, 2> Operands;
2759         Operands.append(Mul->op_begin() + 1, Mul->op_end());
2760         return getMulExpr(Operands);
2761       }
2762
2763       // We can't just assume that LHSCst divides RHSCst cleanly, it could be
2764       // that there's a factor provided by one of the other terms. We need to
2765       // check.
2766       APInt Factor = gcd(LHSCst, RHSCst);
2767       if (!Factor.isIntN(1)) {
2768         LHSCst = cast<SCEVConstant>(
2769             getConstant(LHSCst->getValue()->getValue().udiv(Factor)));
2770         RHSCst = cast<SCEVConstant>(
2771             getConstant(RHSCst->getValue()->getValue().udiv(Factor)));
2772         SmallVector<const SCEV *, 2> Operands;
2773         Operands.push_back(LHSCst);
2774         Operands.append(Mul->op_begin() + 1, Mul->op_end());
2775         LHS = getMulExpr(Operands);
2776         RHS = RHSCst;
2777         Mul = dyn_cast<SCEVMulExpr>(LHS);
2778         if (!Mul)
2779           return getUDivExactExpr(LHS, RHS);
2780       }
2781     }
2782   }
2783
2784   for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
2785     if (Mul->getOperand(i) == RHS) {
2786       SmallVector<const SCEV *, 2> Operands;
2787       Operands.append(Mul->op_begin(), Mul->op_begin() + i);
2788       Operands.append(Mul->op_begin() + i + 1, Mul->op_end());
2789       return getMulExpr(Operands);
2790     }
2791   }
2792
2793   return getUDivExpr(LHS, RHS);
2794 }
2795
2796 /// getAddRecExpr - Get an add recurrence expression for the specified loop.
2797 /// Simplify the expression as much as possible.
2798 const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step,
2799                                            const Loop *L,
2800                                            SCEV::NoWrapFlags Flags) {
2801   SmallVector<const SCEV *, 4> Operands;
2802   Operands.push_back(Start);
2803   if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
2804     if (StepChrec->getLoop() == L) {
2805       Operands.append(StepChrec->op_begin(), StepChrec->op_end());
2806       return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW));
2807     }
2808
2809   Operands.push_back(Step);
2810   return getAddRecExpr(Operands, L, Flags);
2811 }
2812
2813 /// getAddRecExpr - Get an add recurrence expression for the specified loop.
2814 /// Simplify the expression as much as possible.
2815 const SCEV *
2816 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
2817                                const Loop *L, SCEV::NoWrapFlags Flags) {
2818   if (Operands.size() == 1) return Operands[0];
2819 #ifndef NDEBUG
2820   Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
2821   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
2822     assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy &&
2823            "SCEVAddRecExpr operand types don't match!");
2824   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
2825     assert(isLoopInvariant(Operands[i], L) &&
2826            "SCEVAddRecExpr operand is not loop-invariant!");
2827 #endif
2828
2829   if (Operands.back()->isZero()) {
2830     Operands.pop_back();
2831     return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0}  -->  X
2832   }
2833
2834   // It's tempting to want to call getMaxBackedgeTakenCount count here and
2835   // use that information to infer NUW and NSW flags. However, computing a
2836   // BE count requires calling getAddRecExpr, so we may not yet have a
2837   // meaningful BE count at this point (and if we don't, we'd be stuck
2838   // with a SCEVCouldNotCompute as the cached BE count).
2839
2840   Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
2841
2842   // Canonicalize nested AddRecs in by nesting them in order of loop depth.
2843   if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
2844     const Loop *NestedLoop = NestedAR->getLoop();
2845     if (L->contains(NestedLoop) ?
2846         (L->getLoopDepth() < NestedLoop->getLoopDepth()) :
2847         (!NestedLoop->contains(L) &&
2848          DT->dominates(L->getHeader(), NestedLoop->getHeader()))) {
2849       SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
2850                                                   NestedAR->op_end());
2851       Operands[0] = NestedAR->getStart();
2852       // AddRecs require their operands be loop-invariant with respect to their
2853       // loops. Don't perform this transformation if it would break this
2854       // requirement.
2855       bool AllInvariant = true;
2856       for (unsigned i = 0, e = Operands.size(); i != e; ++i)
2857         if (!isLoopInvariant(Operands[i], L)) {
2858           AllInvariant = false;
2859           break;
2860         }
2861       if (AllInvariant) {
2862         // Create a recurrence for the outer loop with the same step size.
2863         //
2864         // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
2865         // inner recurrence has the same property.
2866         SCEV::NoWrapFlags OuterFlags =
2867           maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
2868
2869         NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
2870         AllInvariant = true;
2871         for (unsigned i = 0, e = NestedOperands.size(); i != e; ++i)
2872           if (!isLoopInvariant(NestedOperands[i], NestedLoop)) {
2873             AllInvariant = false;
2874             break;
2875           }
2876         if (AllInvariant) {
2877           // Ok, both add recurrences are valid after the transformation.
2878           //
2879           // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
2880           // the outer recurrence has the same property.
2881           SCEV::NoWrapFlags InnerFlags =
2882             maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
2883           return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
2884         }
2885       }
2886       // Reset Operands to its original state.
2887       Operands[0] = NestedAR;
2888     }
2889   }
2890
2891   // Okay, it looks like we really DO need an addrec expr.  Check to see if we
2892   // already have one, otherwise create a new one.
2893   FoldingSetNodeID ID;
2894   ID.AddInteger(scAddRecExpr);
2895   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
2896     ID.AddPointer(Operands[i]);
2897   ID.AddPointer(L);
2898   void *IP = nullptr;
2899   SCEVAddRecExpr *S =
2900     static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2901   if (!S) {
2902     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size());
2903     std::uninitialized_copy(Operands.begin(), Operands.end(), O);
2904     S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator),
2905                                            O, Operands.size(), L);
2906     UniqueSCEVs.InsertNode(S, IP);
2907   }
2908   S->setNoWrapFlags(Flags);
2909   return S;
2910 }
2911
2912 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
2913                                          const SCEV *RHS) {
2914   SmallVector<const SCEV *, 2> Ops;
2915   Ops.push_back(LHS);
2916   Ops.push_back(RHS);
2917   return getSMaxExpr(Ops);
2918 }
2919
2920 const SCEV *
2921 ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
2922   assert(!Ops.empty() && "Cannot get empty smax!");
2923   if (Ops.size() == 1) return Ops[0];
2924 #ifndef NDEBUG
2925   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2926   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2927     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2928            "SCEVSMaxExpr operand types don't match!");
2929 #endif
2930
2931   // Sort by complexity, this groups all similar expression types together.
2932   GroupByComplexity(Ops, LI);
2933
2934   // If there are any constants, fold them together.
2935   unsigned Idx = 0;
2936   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2937     ++Idx;
2938     assert(Idx < Ops.size());
2939     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2940       // We found two constants, fold them together!
2941       ConstantInt *Fold = ConstantInt::get(getContext(),
2942                               APIntOps::smax(LHSC->getValue()->getValue(),
2943                                              RHSC->getValue()->getValue()));
2944       Ops[0] = getConstant(Fold);
2945       Ops.erase(Ops.begin()+1);  // Erase the folded element
2946       if (Ops.size() == 1) return Ops[0];
2947       LHSC = cast<SCEVConstant>(Ops[0]);
2948     }
2949
2950     // If we are left with a constant minimum-int, strip it off.
2951     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
2952       Ops.erase(Ops.begin());
2953       --Idx;
2954     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
2955       // If we have an smax with a constant maximum-int, it will always be
2956       // maximum-int.
2957       return Ops[0];
2958     }
2959
2960     if (Ops.size() == 1) return Ops[0];
2961   }
2962
2963   // Find the first SMax
2964   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
2965     ++Idx;
2966
2967   // Check to see if one of the operands is an SMax. If so, expand its operands
2968   // onto our operand list, and recurse to simplify.
2969   if (Idx < Ops.size()) {
2970     bool DeletedSMax = false;
2971     while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
2972       Ops.erase(Ops.begin()+Idx);
2973       Ops.append(SMax->op_begin(), SMax->op_end());
2974       DeletedSMax = true;
2975     }
2976
2977     if (DeletedSMax)
2978       return getSMaxExpr(Ops);
2979   }
2980
2981   // Okay, check to see if the same value occurs in the operand list twice.  If
2982   // so, delete one.  Since we sorted the list, these values are required to
2983   // be adjacent.
2984   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
2985     //  X smax Y smax Y  -->  X smax Y
2986     //  X smax Y         -->  X, if X is always greater than Y
2987     if (Ops[i] == Ops[i+1] ||
2988         isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) {
2989       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
2990       --i; --e;
2991     } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) {
2992       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
2993       --i; --e;
2994     }
2995
2996   if (Ops.size() == 1) return Ops[0];
2997
2998   assert(!Ops.empty() && "Reduced smax down to nothing!");
2999
3000   // Okay, it looks like we really DO need an smax expr.  Check to see if we
3001   // already have one, otherwise create a new one.
3002   FoldingSetNodeID ID;
3003   ID.AddInteger(scSMaxExpr);
3004   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3005     ID.AddPointer(Ops[i]);
3006   void *IP = nullptr;
3007   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
3008   const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3009   std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3010   SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator),
3011                                              O, Ops.size());
3012   UniqueSCEVs.InsertNode(S, IP);
3013   return S;
3014 }
3015
3016 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
3017                                          const SCEV *RHS) {
3018   SmallVector<const SCEV *, 2> Ops;
3019   Ops.push_back(LHS);
3020   Ops.push_back(RHS);
3021   return getUMaxExpr(Ops);
3022 }
3023
3024 const SCEV *
3025 ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
3026   assert(!Ops.empty() && "Cannot get empty umax!");
3027   if (Ops.size() == 1) return Ops[0];
3028 #ifndef NDEBUG
3029   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
3030   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
3031     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
3032            "SCEVUMaxExpr operand types don't match!");
3033 #endif
3034
3035   // Sort by complexity, this groups all similar expression types together.
3036   GroupByComplexity(Ops, LI);
3037
3038   // If there are any constants, fold them together.
3039   unsigned Idx = 0;
3040   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3041     ++Idx;
3042     assert(Idx < Ops.size());
3043     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3044       // We found two constants, fold them together!
3045       ConstantInt *Fold = ConstantInt::get(getContext(),
3046                               APIntOps::umax(LHSC->getValue()->getValue(),
3047                                              RHSC->getValue()->getValue()));
3048       Ops[0] = getConstant(Fold);
3049       Ops.erase(Ops.begin()+1);  // Erase the folded element
3050       if (Ops.size() == 1) return Ops[0];
3051       LHSC = cast<SCEVConstant>(Ops[0]);
3052     }
3053
3054     // If we are left with a constant minimum-int, strip it off.
3055     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
3056       Ops.erase(Ops.begin());
3057       --Idx;
3058     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
3059       // If we have an umax with a constant maximum-int, it will always be
3060       // maximum-int.
3061       return Ops[0];
3062     }
3063
3064     if (Ops.size() == 1) return Ops[0];
3065   }
3066
3067   // Find the first UMax
3068   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
3069     ++Idx;
3070
3071   // Check to see if one of the operands is a UMax. If so, expand its operands
3072   // onto our operand list, and recurse to simplify.
3073   if (Idx < Ops.size()) {
3074     bool DeletedUMax = false;
3075     while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
3076       Ops.erase(Ops.begin()+Idx);
3077       Ops.append(UMax->op_begin(), UMax->op_end());
3078       DeletedUMax = true;
3079     }
3080
3081     if (DeletedUMax)
3082       return getUMaxExpr(Ops);
3083   }
3084
3085   // Okay, check to see if the same value occurs in the operand list twice.  If
3086   // so, delete one.  Since we sorted the list, these values are required to
3087   // be adjacent.
3088   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
3089     //  X umax Y umax Y  -->  X umax Y
3090     //  X umax Y         -->  X, if X is always greater than Y
3091     if (Ops[i] == Ops[i+1] ||
3092         isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) {
3093       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
3094       --i; --e;
3095     } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) {
3096       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
3097       --i; --e;
3098     }
3099
3100   if (Ops.size() == 1) return Ops[0];
3101
3102   assert(!Ops.empty() && "Reduced umax down to nothing!");
3103
3104   // Okay, it looks like we really DO need a umax expr.  Check to see if we
3105   // already have one, otherwise create a new one.
3106   FoldingSetNodeID ID;
3107   ID.AddInteger(scUMaxExpr);
3108   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3109     ID.AddPointer(Ops[i]);
3110   void *IP = nullptr;
3111   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
3112   const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3113   std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3114   SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator),
3115                                              O, Ops.size());
3116   UniqueSCEVs.InsertNode(S, IP);
3117   return S;
3118 }
3119
3120 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
3121                                          const SCEV *RHS) {
3122   // ~smax(~x, ~y) == smin(x, y).
3123   return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
3124 }
3125
3126 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
3127                                          const SCEV *RHS) {
3128   // ~umax(~x, ~y) == umin(x, y)
3129   return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
3130 }
3131
3132 const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
3133   // If we have DataLayout, we can bypass creating a target-independent
3134   // constant expression and then folding it back into a ConstantInt.
3135   // This is just a compile-time optimization.
3136   if (DL)
3137     return getConstant(IntTy, DL->getTypeAllocSize(AllocTy));
3138
3139   Constant *C = ConstantExpr::getSizeOf(AllocTy);
3140   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
3141     if (Constant *Folded = ConstantFoldConstantExpression(CE, DL, TLI))
3142       C = Folded;
3143   Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(AllocTy));
3144   assert(Ty == IntTy && "Effective SCEV type doesn't match");
3145   return getTruncateOrZeroExtend(getSCEV(C), Ty);
3146 }
3147
3148 const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
3149                                              StructType *STy,
3150                                              unsigned FieldNo) {
3151   // If we have DataLayout, we can bypass creating a target-independent
3152   // constant expression and then folding it back into a ConstantInt.
3153   // This is just a compile-time optimization.
3154   if (DL) {
3155     return getConstant(IntTy,
3156                        DL->getStructLayout(STy)->getElementOffset(FieldNo));
3157   }
3158
3159   Constant *C = ConstantExpr::getOffsetOf(STy, FieldNo);
3160   if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C))
3161     if (Constant *Folded = ConstantFoldConstantExpression(CE, DL, TLI))
3162       C = Folded;
3163
3164   Type *Ty = getEffectiveSCEVType(PointerType::getUnqual(STy));
3165   return getTruncateOrZeroExtend(getSCEV(C), Ty);
3166 }
3167
3168 const SCEV *ScalarEvolution::getUnknown(Value *V) {
3169   // Don't attempt to do anything other than create a SCEVUnknown object
3170   // here.  createSCEV only calls getUnknown after checking for all other
3171   // interesting possibilities, and any other code that calls getUnknown
3172   // is doing so in order to hide a value from SCEV canonicalization.
3173
3174   FoldingSetNodeID ID;
3175   ID.AddInteger(scUnknown);
3176   ID.AddPointer(V);
3177   void *IP = nullptr;
3178   if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
3179     assert(cast<SCEVUnknown>(S)->getValue() == V &&
3180            "Stale SCEVUnknown in uniquing map!");
3181     return S;
3182   }
3183   SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
3184                                             FirstUnknown);
3185   FirstUnknown = cast<SCEVUnknown>(S);
3186   UniqueSCEVs.InsertNode(S, IP);
3187   return S;
3188 }
3189
3190 //===----------------------------------------------------------------------===//
3191 //            Basic SCEV Analysis and PHI Idiom Recognition Code
3192 //
3193
3194 /// isSCEVable - Test if values of the given type are analyzable within
3195 /// the SCEV framework. This primarily includes integer types, and it
3196 /// can optionally include pointer types if the ScalarEvolution class
3197 /// has access to target-specific information.
3198 bool ScalarEvolution::isSCEVable(Type *Ty) const {
3199   // Integers and pointers are always SCEVable.
3200   return Ty->isIntegerTy() || Ty->isPointerTy();
3201 }
3202
3203 /// getTypeSizeInBits - Return the size in bits of the specified type,
3204 /// for which isSCEVable must return true.
3205 uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
3206   assert(isSCEVable(Ty) && "Type is not SCEVable!");
3207
3208   // If we have a DataLayout, use it!
3209   if (DL)
3210     return DL->getTypeSizeInBits(Ty);
3211
3212   // Integer types have fixed sizes.
3213   if (Ty->isIntegerTy())
3214     return Ty->getPrimitiveSizeInBits();
3215
3216   // The only other support type is pointer. Without DataLayout, conservatively
3217   // assume pointers are 64-bit.
3218   assert(Ty->isPointerTy() && "isSCEVable permitted a non-SCEVable type!");
3219   return 64;
3220 }
3221
3222 /// getEffectiveSCEVType - Return a type with the same bitwidth as
3223 /// the given type and which represents how SCEV will treat the given
3224 /// type, for which isSCEVable must return true. For pointer types,
3225 /// this is the pointer-sized integer type.
3226 Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
3227   assert(isSCEVable(Ty) && "Type is not SCEVable!");
3228
3229   if (Ty->isIntegerTy()) {
3230     return Ty;
3231   }
3232
3233   // The only other support type is pointer.
3234   assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
3235
3236   if (DL)
3237     return DL->getIntPtrType(Ty);
3238
3239   // Without DataLayout, conservatively assume pointers are 64-bit.
3240   return Type::getInt64Ty(getContext());
3241 }
3242
3243 const SCEV *ScalarEvolution::getCouldNotCompute() {
3244   return &CouldNotCompute;
3245 }
3246
3247 namespace {
3248   // Helper class working with SCEVTraversal to figure out if a SCEV contains
3249   // a SCEVUnknown with null value-pointer. FindInvalidSCEVUnknown::FindOne
3250   // is set iff if find such SCEVUnknown.
3251   //
3252   struct FindInvalidSCEVUnknown {
3253     bool FindOne;
3254     FindInvalidSCEVUnknown() { FindOne = false; }
3255     bool follow(const SCEV *S) {
3256       switch (static_cast<SCEVTypes>(S->getSCEVType())) {
3257       case scConstant:
3258         return false;
3259       case scUnknown:
3260         if (!cast<SCEVUnknown>(S)->getValue())
3261           FindOne = true;
3262         return false;
3263       default:
3264         return true;
3265       }
3266     }
3267     bool isDone() const { return FindOne; }
3268   };
3269 }
3270
3271 bool ScalarEvolution::checkValidity(const SCEV *S) const {
3272   FindInvalidSCEVUnknown F;
3273   SCEVTraversal<FindInvalidSCEVUnknown> ST(F);
3274   ST.visitAll(S);
3275
3276   return !F.FindOne;
3277 }
3278
3279 /// getSCEV - Return an existing SCEV if it exists, otherwise analyze the
3280 /// expression and create a new one.
3281 const SCEV *ScalarEvolution::getSCEV(Value *V) {
3282   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
3283
3284   ValueExprMapType::iterator I = ValueExprMap.find_as(V);
3285   if (I != ValueExprMap.end()) {
3286     const SCEV *S = I->second;
3287     if (checkValidity(S))
3288       return S;
3289     else
3290       ValueExprMap.erase(I);
3291   }
3292   const SCEV *S = createSCEV(V);
3293
3294   // The process of creating a SCEV for V may have caused other SCEVs
3295   // to have been created, so it's necessary to insert the new entry
3296   // from scratch, rather than trying to remember the insert position
3297   // above.
3298   ValueExprMap.insert(std::make_pair(SCEVCallbackVH(V, this), S));
3299   return S;
3300 }
3301
3302 /// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
3303 ///
3304 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V) {
3305   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
3306     return getConstant(
3307                cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
3308
3309   Type *Ty = V->getType();
3310   Ty = getEffectiveSCEVType(Ty);
3311   return getMulExpr(V,
3312                   getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))));
3313 }
3314
3315 /// getNotSCEV - Return a SCEV corresponding to ~V = -1-V
3316 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
3317   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
3318     return getConstant(
3319                 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
3320
3321   Type *Ty = V->getType();
3322   Ty = getEffectiveSCEVType(Ty);
3323   const SCEV *AllOnes =
3324                    getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
3325   return getMinusSCEV(AllOnes, V);
3326 }
3327
3328 /// getMinusSCEV - Return LHS-RHS.  Minus is represented in SCEV as A+B*-1.
3329 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
3330                                           SCEV::NoWrapFlags Flags) {
3331   assert(!maskFlags(Flags, SCEV::FlagNUW) && "subtraction does not have NUW");
3332
3333   // Fast path: X - X --> 0.
3334   if (LHS == RHS)
3335     return getConstant(LHS->getType(), 0);
3336
3337   // X - Y --> X + -Y.
3338   // X -(nsw || nuw) Y --> X + -Y.
3339   return getAddExpr(LHS, getNegativeSCEV(RHS));
3340 }
3341
3342 /// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
3343 /// input value to the specified type.  If the type must be extended, it is zero
3344 /// extended.
3345 const SCEV *
3346 ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty) {
3347   Type *SrcTy = V->getType();
3348   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3349          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
3350          "Cannot truncate or zero extend with non-integer arguments!");
3351   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3352     return V;  // No conversion
3353   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
3354     return getTruncateExpr(V, Ty);
3355   return getZeroExtendExpr(V, Ty);
3356 }
3357
3358 /// getTruncateOrSignExtend - Return a SCEV corresponding to a conversion of the
3359 /// input value to the specified type.  If the type must be extended, it is sign
3360 /// extended.
3361 const SCEV *
3362 ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
3363                                          Type *Ty) {
3364   Type *SrcTy = V->getType();
3365   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3366          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
3367          "Cannot truncate or zero extend with non-integer arguments!");
3368   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3369     return V;  // No conversion
3370   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
3371     return getTruncateExpr(V, Ty);
3372   return getSignExtendExpr(V, Ty);
3373 }
3374
3375 /// getNoopOrZeroExtend - Return a SCEV corresponding to a conversion of the
3376 /// input value to the specified type.  If the type must be extended, it is zero
3377 /// extended.  The conversion must not be narrowing.
3378 const SCEV *
3379 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
3380   Type *SrcTy = V->getType();
3381   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3382          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
3383          "Cannot noop or zero extend with non-integer arguments!");
3384   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3385          "getNoopOrZeroExtend cannot truncate!");
3386   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3387     return V;  // No conversion
3388   return getZeroExtendExpr(V, Ty);
3389 }
3390
3391 /// getNoopOrSignExtend - Return a SCEV corresponding to a conversion of the
3392 /// input value to the specified type.  If the type must be extended, it is sign
3393 /// extended.  The conversion must not be narrowing.
3394 const SCEV *
3395 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
3396   Type *SrcTy = V->getType();
3397   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3398          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
3399          "Cannot noop or sign extend with non-integer arguments!");
3400   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3401          "getNoopOrSignExtend cannot truncate!");
3402   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3403     return V;  // No conversion
3404   return getSignExtendExpr(V, Ty);
3405 }
3406
3407 /// getNoopOrAnyExtend - Return a SCEV corresponding to a conversion of
3408 /// the input value to the specified type. If the type must be extended,
3409 /// it is extended with unspecified bits. The conversion must not be
3410 /// narrowing.
3411 const SCEV *
3412 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
3413   Type *SrcTy = V->getType();
3414   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3415          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
3416          "Cannot noop or any extend with non-integer arguments!");
3417   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3418          "getNoopOrAnyExtend cannot truncate!");
3419   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3420     return V;  // No conversion
3421   return getAnyExtendExpr(V, Ty);
3422 }
3423
3424 /// getTruncateOrNoop - Return a SCEV corresponding to a conversion of the
3425 /// input value to the specified type.  The conversion must not be widening.
3426 const SCEV *
3427 ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
3428   Type *SrcTy = V->getType();
3429   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
3430          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
3431          "Cannot truncate or noop with non-integer arguments!");
3432   assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
3433          "getTruncateOrNoop cannot extend!");
3434   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3435     return V;  // No conversion
3436   return getTruncateExpr(V, Ty);
3437 }
3438
3439 /// getUMaxFromMismatchedTypes - Promote the operands to the wider of
3440 /// the types using zero-extension, and then perform a umax operation
3441 /// with them.
3442 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
3443                                                         const SCEV *RHS) {
3444   const SCEV *PromotedLHS = LHS;
3445   const SCEV *PromotedRHS = RHS;
3446
3447   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
3448     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
3449   else
3450     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
3451
3452   return getUMaxExpr(PromotedLHS, PromotedRHS);
3453 }
3454
3455 /// getUMinFromMismatchedTypes - Promote the operands to the wider of
3456 /// the types using zero-extension, and then perform a umin operation
3457 /// with them.
3458 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
3459                                                         const SCEV *RHS) {
3460   const SCEV *PromotedLHS = LHS;
3461   const SCEV *PromotedRHS = RHS;
3462
3463   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
3464     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
3465   else
3466     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
3467
3468   return getUMinExpr(PromotedLHS, PromotedRHS);
3469 }
3470
3471 /// getPointerBase - Transitively follow the chain of pointer-type operands
3472 /// until reaching a SCEV that does not have a single pointer operand. This
3473 /// returns a SCEVUnknown pointer for well-formed pointer-type expressions,
3474 /// but corner cases do exist.
3475 const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
3476   // A pointer operand may evaluate to a nonpointer expression, such as null.
3477   if (!V->getType()->isPointerTy())
3478     return V;
3479
3480   if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
3481     return getPointerBase(Cast->getOperand());
3482   }
3483   else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
3484     const SCEV *PtrOp = nullptr;
3485     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
3486          I != E; ++I) {
3487       if ((*I)->getType()->isPointerTy()) {
3488         // Cannot find the base of an expression with multiple pointer operands.
3489         if (PtrOp)
3490           return V;
3491         PtrOp = *I;
3492       }
3493     }
3494     if (!PtrOp)
3495       return V;
3496     return getPointerBase(PtrOp);
3497   }
3498   return V;
3499 }
3500
3501 /// PushDefUseChildren - Push users of the given Instruction
3502 /// onto the given Worklist.
3503 static void
3504 PushDefUseChildren(Instruction *I,
3505                    SmallVectorImpl<Instruction *> &Worklist) {
3506   // Push the def-use children onto the Worklist stack.
3507   for (User *U : I->users())
3508     Worklist.push_back(cast<Instruction>(U));
3509 }
3510
3511 /// ForgetSymbolicValue - This looks up computed SCEV values for all
3512 /// instructions that depend on the given instruction and removes them from
3513 /// the ValueExprMapType map if they reference SymName. This is used during PHI
3514 /// resolution.
3515 void
3516 ScalarEvolution::ForgetSymbolicName(Instruction *PN, const SCEV *SymName) {
3517   SmallVector<Instruction *, 16> Worklist;
3518   PushDefUseChildren(PN, Worklist);
3519
3520   SmallPtrSet<Instruction *, 8> Visited;
3521   Visited.insert(PN);
3522   while (!Worklist.empty()) {
3523     Instruction *I = Worklist.pop_back_val();
3524     if (!Visited.insert(I).second)
3525       continue;
3526
3527     ValueExprMapType::iterator It =
3528       ValueExprMap.find_as(static_cast<Value *>(I));
3529     if (It != ValueExprMap.end()) {
3530       const SCEV *Old = It->second;
3531
3532       // Short-circuit the def-use traversal if the symbolic name
3533       // ceases to appear in expressions.
3534       if (Old != SymName && !hasOperand(Old, SymName))
3535         continue;
3536
3537       // SCEVUnknown for a PHI either means that it has an unrecognized
3538       // structure, it's a PHI that's in the progress of being computed
3539       // by createNodeForPHI, or it's a single-value PHI. In the first case,
3540       // additional loop trip count information isn't going to change anything.
3541       // In the second case, createNodeForPHI will perform the necessary
3542       // updates on its own when it gets to that point. In the third, we do
3543       // want to forget the SCEVUnknown.
3544       if (!isa<PHINode>(I) ||
3545           !isa<SCEVUnknown>(Old) ||
3546           (I != PN && Old == SymName)) {
3547         forgetMemoizedResults(Old);
3548         ValueExprMap.erase(It);
3549       }
3550     }
3551
3552     PushDefUseChildren(I, Worklist);
3553   }
3554 }
3555
3556 /// createNodeForPHI - PHI nodes have two cases.  Either the PHI node exists in
3557 /// a loop header, making it a potential recurrence, or it doesn't.
3558 ///
3559 const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
3560   if (const Loop *L = LI->getLoopFor(PN->getParent()))
3561     if (L->getHeader() == PN->getParent()) {
3562       // The loop may have multiple entrances or multiple exits; we can analyze
3563       // this phi as an addrec if it has a unique entry value and a unique
3564       // backedge value.
3565       Value *BEValueV = nullptr, *StartValueV = nullptr;
3566       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
3567         Value *V = PN->getIncomingValue(i);
3568         if (L->contains(PN->getIncomingBlock(i))) {
3569           if (!BEValueV) {
3570             BEValueV = V;
3571           } else if (BEValueV != V) {
3572             BEValueV = nullptr;
3573             break;
3574           }
3575         } else if (!StartValueV) {
3576           StartValueV = V;
3577         } else if (StartValueV != V) {
3578           StartValueV = nullptr;
3579           break;
3580         }
3581       }
3582       if (BEValueV && StartValueV) {
3583         // While we are analyzing this PHI node, handle its value symbolically.
3584         const SCEV *SymbolicName = getUnknown(PN);
3585         assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
3586                "PHI node already processed?");
3587         ValueExprMap.insert(std::make_pair(SCEVCallbackVH(PN, this), SymbolicName));
3588
3589         // Using this symbolic name for the PHI, analyze the value coming around
3590         // the back-edge.
3591         const SCEV *BEValue = getSCEV(BEValueV);
3592
3593         // NOTE: If BEValue is loop invariant, we know that the PHI node just
3594         // has a special value for the first iteration of the loop.
3595
3596         // If the value coming around the backedge is an add with the symbolic
3597         // value we just inserted, then we found a simple induction variable!
3598         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
3599           // If there is a single occurrence of the symbolic value, replace it
3600           // with a recurrence.
3601           unsigned FoundIndex = Add->getNumOperands();
3602           for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
3603             if (Add->getOperand(i) == SymbolicName)
3604               if (FoundIndex == e) {
3605                 FoundIndex = i;
3606                 break;
3607               }
3608
3609           if (FoundIndex != Add->getNumOperands()) {
3610             // Create an add with everything but the specified operand.
3611             SmallVector<const SCEV *, 8> Ops;
3612             for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
3613               if (i != FoundIndex)
3614                 Ops.push_back(Add->getOperand(i));
3615             const SCEV *Accum = getAddExpr(Ops);
3616
3617             // This is not a valid addrec if the step amount is varying each
3618             // loop iteration, but is not itself an addrec in this loop.
3619             if (isLoopInvariant(Accum, L) ||
3620                 (isa<SCEVAddRecExpr>(Accum) &&
3621                  cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
3622               SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
3623
3624               // If the increment doesn't overflow, then neither the addrec nor
3625               // the post-increment will overflow.
3626               if (const AddOperator *OBO = dyn_cast<AddOperator>(BEValueV)) {
3627                 if (OBO->hasNoUnsignedWrap())
3628                   Flags = setFlags(Flags, SCEV::FlagNUW);
3629                 if (OBO->hasNoSignedWrap())
3630                   Flags = setFlags(Flags, SCEV::FlagNSW);
3631               } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
3632                 // If the increment is an inbounds GEP, then we know the address
3633                 // space cannot be wrapped around. We cannot make any guarantee
3634                 // about signed or unsigned overflow because pointers are
3635                 // unsigned but we may have a negative index from the base
3636                 // pointer. We can guarantee that no unsigned wrap occurs if the
3637                 // indices form a positive value.
3638                 if (GEP->isInBounds()) {
3639                   Flags = setFlags(Flags, SCEV::FlagNW);
3640
3641                   const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
3642                   if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
3643                     Flags = setFlags(Flags, SCEV::FlagNUW);
3644                 }
3645
3646                 // We cannot transfer nuw and nsw flags from subtraction
3647                 // operations -- sub nuw X, Y is not the same as add nuw X, -Y
3648                 // for instance.
3649               }
3650
3651               const SCEV *StartVal = getSCEV(StartValueV);
3652               const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
3653
3654               // Since the no-wrap flags are on the increment, they apply to the
3655               // post-incremented value as well.
3656               if (isLoopInvariant(Accum, L))
3657                 (void)getAddRecExpr(getAddExpr(StartVal, Accum),
3658                                     Accum, L, Flags);
3659
3660               // Okay, for the entire analysis of this edge we assumed the PHI
3661               // to be symbolic.  We now need to go back and purge all of the
3662               // entries for the scalars that use the symbolic expression.
3663               ForgetSymbolicName(PN, SymbolicName);
3664               ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
3665               return PHISCEV;
3666             }
3667           }
3668         } else if (const SCEVAddRecExpr *AddRec =
3669                      dyn_cast<SCEVAddRecExpr>(BEValue)) {
3670           // Otherwise, this could be a loop like this:
3671           //     i = 0;  for (j = 1; ..; ++j) { ....  i = j; }
3672           // In this case, j = {1,+,1}  and BEValue is j.
3673           // Because the other in-value of i (0) fits the evolution of BEValue
3674           // i really is an addrec evolution.
3675           if (AddRec->getLoop() == L && AddRec->isAffine()) {
3676             const SCEV *StartVal = getSCEV(StartValueV);
3677
3678             // If StartVal = j.start - j.stride, we can use StartVal as the
3679             // initial step of the addrec evolution.
3680             if (StartVal == getMinusSCEV(AddRec->getOperand(0),
3681                                          AddRec->getOperand(1))) {
3682               // FIXME: For constant StartVal, we should be able to infer
3683               // no-wrap flags.
3684               const SCEV *PHISCEV =
3685                 getAddRecExpr(StartVal, AddRec->getOperand(1), L,
3686                               SCEV::FlagAnyWrap);
3687
3688               // Okay, for the entire analysis of this edge we assumed the PHI
3689               // to be symbolic.  We now need to go back and purge all of the
3690               // entries for the scalars that use the symbolic expression.
3691               ForgetSymbolicName(PN, SymbolicName);
3692               ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
3693               return PHISCEV;
3694             }
3695           }
3696         }
3697       }
3698     }
3699
3700   // If the PHI has a single incoming value, follow that value, unless the
3701   // PHI's incoming blocks are in a different loop, in which case doing so
3702   // risks breaking LCSSA form. Instcombine would normally zap these, but
3703   // it doesn't have DominatorTree information, so it may miss cases.
3704   if (Value *V = SimplifyInstruction(PN, DL, TLI, DT, AC))
3705     if (LI->replacementPreservesLCSSAForm(PN, V))
3706       return getSCEV(V);
3707
3708   // If it's not a loop phi, we can't handle it yet.
3709   return getUnknown(PN);
3710 }
3711
3712 /// createNodeForGEP - Expand GEP instructions into add and multiply
3713 /// operations. This allows them to be analyzed by regular SCEV code.
3714 ///
3715 const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
3716   Type *IntPtrTy = getEffectiveSCEVType(GEP->getType());
3717   Value *Base = GEP->getOperand(0);
3718   // Don't attempt to analyze GEPs over unsized objects.
3719   if (!Base->getType()->getPointerElementType()->isSized())
3720     return getUnknown(GEP);
3721
3722   // Don't blindly transfer the inbounds flag from the GEP instruction to the
3723   // Add expression, because the Instruction may be guarded by control flow
3724   // and the no-overflow bits may not be valid for the expression in any
3725   // context.
3726   SCEV::NoWrapFlags Wrap = GEP->isInBounds() ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
3727
3728   const SCEV *TotalOffset = getConstant(IntPtrTy, 0);
3729   gep_type_iterator GTI = gep_type_begin(GEP);
3730   for (GetElementPtrInst::op_iterator I = std::next(GEP->op_begin()),
3731                                       E = GEP->op_end();
3732        I != E; ++I) {
3733     Value *Index = *I;
3734     // Compute the (potentially symbolic) offset in bytes for this index.
3735     if (StructType *STy = dyn_cast<StructType>(*GTI++)) {
3736       // For a struct, add the member offset.
3737       unsigned FieldNo = cast<ConstantInt>(Index)->getZExtValue();
3738       const SCEV *FieldOffset = getOffsetOfExpr(IntPtrTy, STy, FieldNo);
3739
3740       // Add the field offset to the running total offset.
3741       TotalOffset = getAddExpr(TotalOffset, FieldOffset);
3742     } else {
3743       // For an array, add the element offset, explicitly scaled.
3744       const SCEV *ElementSize = getSizeOfExpr(IntPtrTy, *GTI);
3745       const SCEV *IndexS = getSCEV(Index);
3746       // Getelementptr indices are signed.
3747       IndexS = getTruncateOrSignExtend(IndexS, IntPtrTy);
3748
3749       // Multiply the index by the element size to compute the element offset.
3750       const SCEV *LocalOffset = getMulExpr(IndexS, ElementSize, Wrap);
3751
3752       // Add the element offset to the running total offset.
3753       TotalOffset = getAddExpr(TotalOffset, LocalOffset);
3754     }
3755   }
3756
3757   // Get the SCEV for the GEP base.
3758   const SCEV *BaseS = getSCEV(Base);
3759
3760   // Add the total offset from all the GEP indices to the base.
3761   return getAddExpr(BaseS, TotalOffset, Wrap);
3762 }
3763
3764 /// GetMinTrailingZeros - Determine the minimum number of zero bits that S is
3765 /// guaranteed to end in (at every loop iteration).  It is, at the same time,
3766 /// the minimum number of times S is divisible by 2.  For example, given {4,+,8}
3767 /// it returns 2.  If S is guaranteed to be 0, it returns the bitwidth of S.
3768 uint32_t
3769 ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
3770   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
3771     return C->getValue()->getValue().countTrailingZeros();
3772
3773   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
3774     return std::min(GetMinTrailingZeros(T->getOperand()),
3775                     (uint32_t)getTypeSizeInBits(T->getType()));
3776
3777   if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
3778     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
3779     return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
3780              getTypeSizeInBits(E->getType()) : OpRes;
3781   }
3782
3783   if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
3784     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
3785     return OpRes == getTypeSizeInBits(E->getOperand()->getType()) ?
3786              getTypeSizeInBits(E->getType()) : OpRes;
3787   }
3788
3789   if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
3790     // The result is the min of all operands results.
3791     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
3792     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
3793       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
3794     return MinOpRes;
3795   }
3796
3797   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
3798     // The result is the sum of all operands results.
3799     uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
3800     uint32_t BitWidth = getTypeSizeInBits(M->getType());
3801     for (unsigned i = 1, e = M->getNumOperands();
3802          SumOpRes != BitWidth && i != e; ++i)
3803       SumOpRes = std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)),
3804                           BitWidth);
3805     return SumOpRes;
3806   }
3807
3808   if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
3809     // The result is the min of all operands results.
3810     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
3811     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
3812       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
3813     return MinOpRes;
3814   }
3815
3816   if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
3817     // The result is the min of all operands results.
3818     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
3819     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
3820       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
3821     return MinOpRes;
3822   }
3823
3824   if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
3825     // The result is the min of all operands results.
3826     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
3827     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
3828       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
3829     return MinOpRes;
3830   }
3831
3832   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
3833     // For a SCEVUnknown, ask ValueTracking.
3834     unsigned BitWidth = getTypeSizeInBits(U->getType());
3835     APInt Zeros(BitWidth, 0), Ones(BitWidth, 0);
3836     computeKnownBits(U->getValue(), Zeros, Ones, DL, 0, AC, nullptr, DT);
3837     return Zeros.countTrailingOnes();
3838   }
3839
3840   // SCEVUDivExpr
3841   return 0;
3842 }
3843
3844 /// GetRangeFromMetadata - Helper method to assign a range to V from
3845 /// metadata present in the IR.
3846 static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
3847   if (Instruction *I = dyn_cast<Instruction>(V)) {
3848     if (MDNode *MD = I->getMetadata(LLVMContext::MD_range)) {
3849       ConstantRange TotalRange(
3850           cast<IntegerType>(I->getType())->getBitWidth(), false);
3851
3852       unsigned NumRanges = MD->getNumOperands() / 2;
3853       assert(NumRanges >= 1);
3854
3855       for (unsigned i = 0; i < NumRanges; ++i) {
3856         ConstantInt *Lower =
3857             mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 0));
3858         ConstantInt *Upper =
3859             mdconst::extract<ConstantInt>(MD->getOperand(2 * i + 1));
3860         ConstantRange Range(Lower->getValue(), Upper->getValue());
3861         TotalRange = TotalRange.unionWith(Range);
3862       }
3863
3864       return TotalRange;
3865     }
3866   }
3867
3868   return None;
3869 }
3870
3871 /// getUnsignedRange - Determine the unsigned range for a particular SCEV.
3872 ///
3873 ConstantRange
3874 ScalarEvolution::getUnsignedRange(const SCEV *S) {
3875   // See if we've computed this range already.
3876   DenseMap<const SCEV *, ConstantRange>::iterator I = UnsignedRanges.find(S);
3877   if (I != UnsignedRanges.end())
3878     return I->second;
3879
3880   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
3881     return setUnsignedRange(C, ConstantRange(C->getValue()->getValue()));
3882
3883   unsigned BitWidth = getTypeSizeInBits(S->getType());
3884   ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
3885
3886   // If the value has known zeros, the maximum unsigned value will have those
3887   // known zeros as well.
3888   uint32_t TZ = GetMinTrailingZeros(S);
3889   if (TZ != 0)
3890     ConservativeResult =
3891       ConstantRange(APInt::getMinValue(BitWidth),
3892                     APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
3893
3894   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
3895     ConstantRange X = getUnsignedRange(Add->getOperand(0));
3896     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
3897       X = X.add(getUnsignedRange(Add->getOperand(i)));
3898     return setUnsignedRange(Add, ConservativeResult.intersectWith(X));
3899   }
3900
3901   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
3902     ConstantRange X = getUnsignedRange(Mul->getOperand(0));
3903     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
3904       X = X.multiply(getUnsignedRange(Mul->getOperand(i)));
3905     return setUnsignedRange(Mul, ConservativeResult.intersectWith(X));