LCOV - code coverage report
Current view: top level - lib/Analysis - ScalarEvolution.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 4790 5037 95.1 %
Date: 2017-09-14 15:23:50 Functions: 323 328 98.5 %
Legend: Lines: hit not hit

          Line data    Source code
       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/APInt.h"
      63             : #include "llvm/ADT/ArrayRef.h"
      64             : #include "llvm/ADT/DenseMap.h"
      65             : #include "llvm/ADT/DepthFirstIterator.h"
      66             : #include "llvm/ADT/FoldingSet.h"
      67             : #include "llvm/ADT/None.h"
      68             : #include "llvm/ADT/Optional.h"
      69             : #include "llvm/ADT/STLExtras.h"
      70             : #include "llvm/ADT/ScopeExit.h"
      71             : #include "llvm/ADT/Sequence.h"
      72             : #include "llvm/ADT/SetVector.h"
      73             : #include "llvm/ADT/SmallPtrSet.h"
      74             : #include "llvm/ADT/SmallSet.h"
      75             : #include "llvm/ADT/SmallVector.h"
      76             : #include "llvm/ADT/Statistic.h"
      77             : #include "llvm/ADT/StringRef.h"
      78             : #include "llvm/Analysis/AssumptionCache.h"
      79             : #include "llvm/Analysis/ConstantFolding.h"
      80             : #include "llvm/Analysis/InstructionSimplify.h"
      81             : #include "llvm/Analysis/LoopInfo.h"
      82             : #include "llvm/Analysis/ScalarEvolutionExpressions.h"
      83             : #include "llvm/Analysis/TargetLibraryInfo.h"
      84             : #include "llvm/Analysis/ValueTracking.h"
      85             : #include "llvm/IR/Argument.h"
      86             : #include "llvm/IR/BasicBlock.h"
      87             : #include "llvm/IR/CFG.h"
      88             : #include "llvm/IR/CallSite.h"
      89             : #include "llvm/IR/Constant.h"
      90             : #include "llvm/IR/ConstantRange.h"
      91             : #include "llvm/IR/Constants.h"
      92             : #include "llvm/IR/DataLayout.h"
      93             : #include "llvm/IR/DerivedTypes.h"
      94             : #include "llvm/IR/Dominators.h"
      95             : #include "llvm/IR/Function.h"
      96             : #include "llvm/IR/GlobalAlias.h"
      97             : #include "llvm/IR/GlobalValue.h"
      98             : #include "llvm/IR/GlobalVariable.h"
      99             : #include "llvm/IR/InstIterator.h"
     100             : #include "llvm/IR/InstrTypes.h"
     101             : #include "llvm/IR/Instruction.h"
     102             : #include "llvm/IR/Instructions.h"
     103             : #include "llvm/IR/IntrinsicInst.h"
     104             : #include "llvm/IR/Intrinsics.h"
     105             : #include "llvm/IR/LLVMContext.h"
     106             : #include "llvm/IR/Metadata.h"
     107             : #include "llvm/IR/Operator.h"
     108             : #include "llvm/IR/PatternMatch.h"
     109             : #include "llvm/IR/Type.h"
     110             : #include "llvm/IR/Use.h"
     111             : #include "llvm/IR/User.h"
     112             : #include "llvm/IR/Value.h"
     113             : #include "llvm/Pass.h"
     114             : #include "llvm/Support/Casting.h"
     115             : #include "llvm/Support/CommandLine.h"
     116             : #include "llvm/Support/Compiler.h"
     117             : #include "llvm/Support/Debug.h"
     118             : #include "llvm/Support/ErrorHandling.h"
     119             : #include "llvm/Support/KnownBits.h"
     120             : #include "llvm/Support/SaveAndRestore.h"
     121             : #include "llvm/Support/raw_ostream.h"
     122             : #include <algorithm>
     123             : #include <cassert>
     124             : #include <climits>
     125             : #include <cstddef>
     126             : #include <cstdint>
     127             : #include <cstdlib>
     128             : #include <map>
     129             : #include <memory>
     130             : #include <tuple>
     131             : #include <utility>
     132             : #include <vector>
     133             : 
     134             : using namespace llvm;
     135             : 
     136             : #define DEBUG_TYPE "scalar-evolution"
     137             : 
     138             : STATISTIC(NumArrayLenItCounts,
     139             :           "Number of trip counts computed with array length");
     140             : STATISTIC(NumTripCountsComputed,
     141             :           "Number of loops with predictable loop counts");
     142             : STATISTIC(NumTripCountsNotComputed,
     143             :           "Number of loops without predictable loop counts");
     144             : STATISTIC(NumBruteForceTripCountsComputed,
     145             :           "Number of loops with trip counts computed by force");
     146             : 
     147             : static cl::opt<unsigned>
     148       72306 : MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
     149      216918 :                         cl::desc("Maximum number of iterations SCEV will "
     150             :                                  "symbolically execute a constant "
     151             :                                  "derived loop"),
     152      289224 :                         cl::init(100));
     153             : 
     154             : // FIXME: Enable this with EXPENSIVE_CHECKS when the test suite is clean.
     155             : static cl::opt<bool>
     156       72306 : VerifySCEV("verify-scev",
     157      144612 :            cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
     158             : static cl::opt<bool>
     159       72306 :     VerifySCEVMap("verify-scev-maps",
     160      216918 :                   cl::desc("Verify no dangling value in ScalarEvolution's "
     161       72306 :                            "ExprValueMap (slow)"));
     162             : 
     163       72306 : static cl::opt<unsigned> MulOpsInlineThreshold(
     164             :     "scev-mulops-inline-threshold", cl::Hidden,
     165      216918 :     cl::desc("Threshold for inlining multiplication operands into a SCEV"),
     166      289224 :     cl::init(32));
     167             : 
     168       72306 : static cl::opt<unsigned> AddOpsInlineThreshold(
     169             :     "scev-addops-inline-threshold", cl::Hidden,
     170      216918 :     cl::desc("Threshold for inlining addition operands into a SCEV"),
     171      289224 :     cl::init(500));
     172             : 
     173       72306 : static cl::opt<unsigned> MaxSCEVCompareDepth(
     174             :     "scalar-evolution-max-scev-compare-depth", cl::Hidden,
     175      216918 :     cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
     176      289224 :     cl::init(32));
     177             : 
     178       72306 : static cl::opt<unsigned> MaxSCEVOperationsImplicationDepth(
     179             :     "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden,
     180      216918 :     cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
     181      289224 :     cl::init(2));
     182             : 
     183       72306 : static cl::opt<unsigned> MaxValueCompareDepth(
     184             :     "scalar-evolution-max-value-compare-depth", cl::Hidden,
     185      216918 :     cl::desc("Maximum depth of recursive value complexity comparisons"),
     186      289224 :     cl::init(2));
     187             : 
     188             : static cl::opt<unsigned>
     189       72306 :     MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden,
     190      216918 :                   cl::desc("Maximum depth of recursive arithmetics"),
     191      289224 :                   cl::init(32));
     192             : 
     193       72306 : static cl::opt<unsigned> MaxConstantEvolvingDepth(
     194             :     "scalar-evolution-max-constant-evolving-depth", cl::Hidden,
     195      216918 :     cl::desc("Maximum depth of recursive constant evolving"), cl::init(32));
     196             : 
     197             : static cl::opt<unsigned>
     198       72306 :     MaxExtDepth("scalar-evolution-max-ext-depth", cl::Hidden,
     199      216918 :                 cl::desc("Maximum depth of recursive SExt/ZExt"),
     200      289224 :                 cl::init(8));
     201             : 
     202             : static cl::opt<unsigned>
     203       72306 :     MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
     204      216918 :                   cl::desc("Max coefficients in AddRec during evolving"),
     205      289224 :                   cl::init(16));
     206             : 
     207             : //===----------------------------------------------------------------------===//
     208             : //                           SCEV class definitions
     209             : //===----------------------------------------------------------------------===//
     210             : 
     211             : //===----------------------------------------------------------------------===//
     212             : // Implementation of the SCEV class.
     213             : //
     214             : 
     215             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
     216             : LLVM_DUMP_METHOD void SCEV::dump() const {
     217             :   print(dbgs());
     218             :   dbgs() << '\n';
     219             : }
     220             : #endif
     221             : 
     222       22134 : void SCEV::print(raw_ostream &OS) const {
     223       44268 :   switch (static_cast<SCEVTypes>(getSCEVType())) {
     224        8325 :   case scConstant:
     225        8325 :     cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
     226        8325 :     return;
     227         138 :   case scTruncate: {
     228         138 :     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
     229         138 :     const SCEV *Op = Trunc->getOperand();
     230         276 :     OS << "(trunc " << *Op->getType() << " " << *Op << " to "
     231         276 :        << *Trunc->getType() << ")";
     232         138 :     return;
     233             :   }
     234         311 :   case scZeroExtend: {
     235         311 :     const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
     236         311 :     const SCEV *Op = ZExt->getOperand();
     237         622 :     OS << "(zext " << *Op->getType() << " " << *Op << " to "
     238         622 :        << *ZExt->getType() << ")";
     239         311 :     return;
     240             :   }
     241         401 :   case scSignExtend: {
     242         401 :     const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
     243         401 :     const SCEV *Op = SExt->getOperand();
     244         802 :     OS << "(sext " << *Op->getType() << " " << *Op << " to "
     245         802 :        << *SExt->getType() << ")";
     246         401 :     return;
     247             :   }
     248        1865 :   case scAddRecExpr: {
     249        1865 :     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
     250        3730 :     OS << "{" << *AR->getOperand(0);
     251        4058 :     for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
     252        4386 :       OS << ",+," << *AR->getOperand(i);
     253        1865 :     OS << "}<";
     254        3730 :     if (AR->hasNoUnsignedWrap())
     255         417 :       OS << "nuw><";
     256        3730 :     if (AR->hasNoSignedWrap())
     257         586 :       OS << "nsw><";
     258        4554 :     if (AR->hasNoSelfWrap() &&
     259        1648 :         !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW)))
     260         124 :       OS << "nw><";
     261        3730 :     AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false);
     262        1865 :     OS << ">";
     263        1865 :     return;
     264             :   }
     265        4522 :   case scAddExpr:
     266             :   case scMulExpr:
     267             :   case scUMaxExpr:
     268             :   case scSMaxExpr: {
     269        4522 :     const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
     270        4522 :     const char *OpStr = nullptr;
     271        4522 :     switch (NAry->getSCEVType()) {
     272        2314 :     case scAddExpr: OpStr = " + "; break;
     273        1815 :     case scMulExpr: OpStr = " * "; break;
     274         142 :     case scUMaxExpr: OpStr = " umax "; break;
     275         251 :     case scSMaxExpr: OpStr = " smax "; break;
     276             :     }
     277        4522 :     OS << "(";
     278        9044 :     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
     279       14465 :          I != E; ++I) {
     280        9943 :       OS << **I;
     281        9943 :       if (std::next(I) != E)
     282        5421 :         OS << OpStr;
     283             :     }
     284        4522 :     OS << ")";
     285        9044 :     switch (NAry->getSCEVType()) {
     286        4129 :     case scAddExpr:
     287             :     case scMulExpr:
     288        4129 :       if (NAry->hasNoUnsignedWrap())
     289         107 :         OS << "<nuw>";
     290        4129 :       if (NAry->hasNoSignedWrap())
     291         436 :         OS << "<nsw>";
     292             :     }
     293             :     return;
     294             :   }
     295         377 :   case scUDivExpr: {
     296         377 :     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
     297         377 :     OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
     298         377 :     return;
     299             :   }
     300        6195 :   case scUnknown: {
     301        6195 :     const SCEVUnknown *U = cast<SCEVUnknown>(this);
     302             :     Type *AllocTy;
     303        6195 :     if (U->isSizeOf(AllocTy)) {
     304           8 :       OS << "sizeof(" << *AllocTy << ")";
     305           4 :       return;
     306             :     }
     307        6191 :     if (U->isAlignOf(AllocTy)) {
     308           6 :       OS << "alignof(" << *AllocTy << ")";
     309           3 :       return;
     310             :     }
     311             : 
     312             :     Type *CTy;
     313             :     Constant *FieldNo;
     314        6188 :     if (U->isOffsetOf(CTy, FieldNo)) {
     315           2 :       OS << "offsetof(" << *CTy << ", ";
     316           1 :       FieldNo->printAsOperand(OS, false);
     317           1 :       OS << ")";
     318           1 :       return;
     319             :     }
     320             : 
     321             :     // Otherwise just print it normally.
     322        6187 :     U->getValue()->printAsOperand(OS, false);
     323        6187 :     return;
     324             :   }
     325           0 :   case scCouldNotCompute:
     326           0 :     OS << "***COULDNOTCOMPUTE***";
     327           0 :     return;
     328             :   }
     329           0 :   llvm_unreachable("Unknown SCEV kind!");
     330             : }
     331             : 
     332     5016926 : Type *SCEV::getType() const {
     333     5045050 :   switch (static_cast<SCEVTypes>(getSCEVType())) {
     334     2192584 :   case scConstant:
     335     4385168 :     return cast<SCEVConstant>(this)->getType();
     336      106948 :   case scTruncate:
     337             :   case scZeroExtend:
     338             :   case scSignExtend:
     339      106948 :     return cast<SCEVCastExpr>(this)->getType();
     340     1291622 :   case scAddRecExpr:
     341             :   case scMulExpr:
     342             :   case scUMaxExpr:
     343             :   case scSMaxExpr:
     344     1291622 :     return cast<SCEVNAryExpr>(this)->getType();
     345      398584 :   case scAddExpr:
     346      398584 :     return cast<SCEVAddExpr>(this)->getType();
     347       28124 :   case scUDivExpr:
     348       56248 :     return cast<SCEVUDivExpr>(this)->getType();
     349     1027188 :   case scUnknown:
     350     2054376 :     return cast<SCEVUnknown>(this)->getType();
     351           0 :   case scCouldNotCompute:
     352           0 :     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
     353             :   }
     354           0 :   llvm_unreachable("Unknown SCEV kind!");
     355             : }
     356             : 
     357      951748 : bool SCEV::isZero() const {
     358      673548 :   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
     359      673548 :     return SC->getValue()->isZero();
     360             :   return false;
     361             : }
     362             : 
     363       35176 : bool SCEV::isOne() const {
     364       20310 :   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
     365       20310 :     return SC->getValue()->isOne();
     366             :   return false;
     367             : }
     368             : 
     369      284382 : bool SCEV::isAllOnesValue() const {
     370      283166 :   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
     371      283166 :     return SC->getValue()->isMinusOne();
     372             :   return false;
     373             : }
     374             : 
     375       15143 : bool SCEV::isNonConstantNegative() const {
     376        2109 :   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
     377             :   if (!Mul) return false;
     378             : 
     379             :   // If there is a constant factor, it will be first.
     380        5794 :   const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
     381             :   if (!SC) return false;
     382             : 
     383             :   // Return true if the value is negative, this matches things like (-42 * V).
     384        3152 :   return SC->getAPInt().isNegative();
     385             : }
     386             : 
     387      409193 : SCEVCouldNotCompute::SCEVCouldNotCompute() :
     388      818386 :   SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {}
     389             : 
     390      758083 : bool SCEVCouldNotCompute::classof(const SCEV *S) {
     391      758083 :   return S->getSCEVType() == scCouldNotCompute;
     392             : }
     393             : 
     394     3724719 : const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
     395     7449438 :   FoldingSetNodeID ID;
     396     3724719 :   ID.AddInteger(scConstant);
     397     3724719 :   ID.AddPointer(V);
     398     3724719 :   void *IP = nullptr;
     399     7449438 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
     400      885801 :   SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
     401      590534 :   UniqueSCEVs.InsertNode(S, IP);
     402      295267 :   return S;
     403             : }
     404             : 
     405     1680234 : const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
     406     3360468 :   return getConstant(ConstantInt::get(getContext(), Val));
     407             : }
     408             : 
     409             : const SCEV *
     410      740078 : ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
     411     1480156 :   IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
     412      740078 :   return getConstant(ConstantInt::get(ITy, V, isSigned));
     413             : }
     414             : 
     415       42708 : SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID,
     416       42708 :                            unsigned SCEVTy, const SCEV *op, Type *ty)
     417       85416 :   : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
     418             : 
     419        2390 : SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
     420        2390 :                                    const SCEV *op, Type *ty)
     421        2390 :   : SCEVCastExpr(ID, scTruncate, op, ty) {
     422             :   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
     423             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
     424             :          "Cannot truncate non-integer value!");
     425        2390 : }
     426             : 
     427       24626 : SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
     428       24626 :                                        const SCEV *op, Type *ty)
     429       24626 :   : SCEVCastExpr(ID, scZeroExtend, op, ty) {
     430             :   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
     431             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
     432             :          "Cannot zero extend non-integer value!");
     433       24626 : }
     434             : 
     435       15692 : SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
     436       15692 :                                        const SCEV *op, Type *ty)
     437       15692 :   : SCEVCastExpr(ID, scSignExtend, op, ty) {
     438             :   assert((Op->getType()->isIntegerTy() || Op->getType()->isPointerTy()) &&
     439             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
     440             :          "Cannot sign extend non-integer value!");
     441       15692 : }
     442             : 
     443         544 : void SCEVUnknown::deleted() {
     444             :   // Clear this SCEVUnknown from various maps.
     445         544 :   SE->forgetMemoizedResults(this);
     446             : 
     447             :   // Remove this SCEVUnknown from the uniquing map.
     448        1088 :   SE->UniqueSCEVs.RemoveNode(this);
     449             : 
     450             :   // Release the value.
     451        1088 :   setValPtr(nullptr);
     452         544 : }
     453             : 
     454        1208 : void SCEVUnknown::allUsesReplacedWith(Value *New) {
     455             :   // Remove this SCEVUnknown from the uniquing map.
     456        2416 :   SE->UniqueSCEVs.RemoveNode(this);
     457             : 
     458             :   // Update this SCEVUnknown to point to the new value. This is needed
     459             :   // because there may still be outstanding SCEVs which still point to
     460             :   // this SCEVUnknown.
     461        2416 :   setValPtr(New);
     462        1208 : }
     463             : 
     464        6195 : bool SCEVUnknown::isSizeOf(Type *&AllocTy) const {
     465        6208 :   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
     466          13 :     if (VCE->getOpcode() == Instruction::PtrToInt)
     467          16 :       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
     468          16 :         if (CE->getOpcode() == Instruction::GetElementPtr &&
     469          24 :             CE->getOperand(0)->isNullValue() &&
     470           8 :             CE->getNumOperands() == 2)
     471           8 :           if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
     472           4 :             if (CI->isOne()) {
     473           8 :               AllocTy = cast<PointerType>(CE->getOperand(0)->getType())
     474           8 :                                  ->getElementType();
     475           4 :               return true;
     476             :             }
     477             : 
     478             :   return false;
     479             : }
     480             : 
     481        6191 : bool SCEVUnknown::isAlignOf(Type *&AllocTy) const {
     482        6200 :   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
     483           9 :     if (VCE->getOpcode() == Instruction::PtrToInt)
     484           8 :       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
     485           8 :         if (CE->getOpcode() == Instruction::GetElementPtr &&
     486           4 :             CE->getOperand(0)->isNullValue()) {
     487             :           Type *Ty =
     488           8 :             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
     489           4 :           if (StructType *STy = dyn_cast<StructType>(Ty))
     490           8 :             if (!STy->isPacked() &&
     491          12 :                 CE->getNumOperands() == 3 &&
     492           4 :                 CE->getOperand(1)->isNullValue()) {
     493           8 :               if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
     494           7 :                 if (CI->isOne() &&
     495           7 :                     STy->getNumElements() == 2 &&
     496           6 :                     STy->getElementType(0)->isIntegerTy(1)) {
     497           6 :                   AllocTy = STy->getElementType(1);
     498           3 :                   return true;
     499             :                 }
     500             :             }
     501             :         }
     502             : 
     503             :   return false;
     504             : }
     505             : 
     506        6188 : bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const {
     507        6194 :   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
     508           6 :     if (VCE->getOpcode() == Instruction::PtrToInt)
     509           2 :       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
     510           2 :         if (CE->getOpcode() == Instruction::GetElementPtr &&
     511           2 :             CE->getNumOperands() == 3 &&
     512           3 :             CE->getOperand(0)->isNullValue() &&
     513           1 :             CE->getOperand(1)->isNullValue()) {
     514             :           Type *Ty =
     515           2 :             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
     516             :           // Ignore vector types here so that ScalarEvolutionExpander doesn't
     517             :           // emit getelementptrs that index into vectors.
     518           1 :           if (Ty->isStructTy() || Ty->isArrayTy()) {
     519           1 :             CTy = Ty;
     520           1 :             FieldNo = CE->getOperand(2);
     521           1 :             return true;
     522             :           }
     523             :         }
     524             : 
     525             :   return false;
     526             : }
     527             : 
     528             : //===----------------------------------------------------------------------===//
     529             : //                               SCEV Utilities
     530             : //===----------------------------------------------------------------------===//
     531             : 
     532             : /// Compare the two values \p LV and \p RV in terms of their "complexity" where
     533             : /// "complexity" is a partial (and somewhat ad-hoc) relation used to order
     534             : /// operands in SCEV expressions.  \p EqCache is a set of pairs of values that
     535             : /// have been previously deemed to be "equally complex" by this routine.  It is
     536             : /// intended to avoid exponential time complexity in cases like:
     537             : ///
     538             : ///   %a = f(%x, %y)
     539             : ///   %b = f(%a, %a)
     540             : ///   %c = f(%b, %b)
     541             : ///
     542             : ///   %d = f(%x, %y)
     543             : ///   %e = f(%d, %d)
     544             : ///   %f = f(%e, %e)
     545             : ///
     546             : ///   CompareValueComplexity(%f, %c)
     547             : ///
     548             : /// Since we do not continue running this routine on expression trees once we
     549             : /// have seen unequal values, there is no need to track them in the cache.
     550             : static int
     551       71074 : CompareValueComplexity(SmallSet<std::pair<Value *, Value *>, 8> &EqCache,
     552             :                        const LoopInfo *const LI, Value *LV, Value *RV,
     553             :                        unsigned Depth) {
     554      125515 :   if (Depth > MaxValueCompareDepth || EqCache.count({LV, RV}))
     555             :     return 0;
     556             : 
     557             :   // Order pointer values after integer values. This helps SCEVExpander form
     558             :   // GEPs.
     559      108882 :   bool LIsPointer = LV->getType()->isPointerTy(),
     560      108882 :        RIsPointer = RV->getType()->isPointerTy();
     561       54441 :   if (LIsPointer != RIsPointer)
     562        6075 :     return (int)LIsPointer - (int)RIsPointer;
     563             : 
     564             :   // Compare getValueID values.
     565      145098 :   unsigned LID = LV->getValueID(), RID = RV->getValueID();
     566       48366 :   if (LID != RID)
     567        7042 :     return (int)LID - (int)RID;
     568             : 
     569             :   // Sort arguments by their position.
     570       51077 :   if (const auto *LA = dyn_cast<Argument>(LV)) {
     571       19506 :     const auto *RA = cast<Argument>(RV);
     572        9753 :     unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
     573        9753 :     return (int)LArgNo - (int)RArgNo;
     574             :   }
     575             : 
     576       36985 :   if (const auto *LGV = dyn_cast<GlobalValue>(LV)) {
     577       10828 :     const auto *RGV = cast<GlobalValue>(RV);
     578             : 
     579             :     const auto IsGVNameSemantic = [&](const GlobalValue *GV) {
     580       10828 :       auto LT = GV->getLinkage();
     581       21656 :       return !(GlobalValue::isPrivateLinkage(LT) ||
     582       10828 :                GlobalValue::isInternalLinkage(LT));
     583             :     };
     584             : 
     585             :     // Use the names to distinguish the two values, but only if the
     586             :     // names are semantically important.
     587       16242 :     if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
     588       10828 :       return LGV->getName().compare(RGV->getName());
     589             :   }
     590             : 
     591             :   // For instructions, compare their loop depth, and their operand count.  This
     592             :   // is pretty loose.
     593       49416 :   if (const auto *LInst = dyn_cast<Instruction>(LV)) {
     594       46518 :     const auto *RInst = cast<Instruction>(RV);
     595             : 
     596             :     // Compare loop depths.
     597       23259 :     const BasicBlock *LParent = LInst->getParent(),
     598       23259 :                      *RParent = RInst->getParent();
     599       23259 :     if (LParent != RParent) {
     600        2482 :       unsigned LDepth = LI->getLoopDepth(LParent),
     601        2482 :                RDepth = LI->getLoopDepth(RParent);
     602        2482 :       if (LDepth != RDepth)
     603         189 :         return (int)LDepth - (int)RDepth;
     604             :     }
     605             : 
     606             :     // Compare the number of operands.
     607       46140 :     unsigned LNumOps = LInst->getNumOperands(),
     608       46140 :              RNumOps = RInst->getNumOperands();
     609       23070 :     if (LNumOps != RNumOps)
     610          82 :       return (int)LNumOps - (int)RNumOps;
     611             : 
     612       48842 :     for (unsigned Idx : seq(0u, LNumOps)) {
     613             :       int Result =
     614       95562 :           CompareValueComplexity(EqCache, LI, LInst->getOperand(Idx),
     615       31854 :                                  RInst->getOperand(Idx), Depth + 1);
     616       31854 :       if (Result != 0)
     617             :         return Result;
     618             :     }
     619             :   }
     620             : 
     621       19886 :   EqCache.insert({LV, RV});
     622       19886 :   return 0;
     623             : }
     624             : 
     625             : // Return negative, zero, or positive, if LHS is less than, equal to, or greater
     626             : // than RHS, respectively. A three-way result allows recursive comparisons to be
     627             : // more efficient.
     628     3303308 : static int CompareSCEVComplexity(
     629             :     SmallSet<std::pair<const SCEV *, const SCEV *>, 8> &EqCacheSCEV,
     630             :     const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS,
     631             :     DominatorTree &DT, unsigned Depth = 0) {
     632             :   // Fast-path: SCEVs are uniqued so we can do a quick equality check.
     633     3303308 :   if (LHS == RHS)
     634             :     return 0;
     635             : 
     636             :   // Primarily, sort the SCEVs by their getSCEVType().
     637     9054654 :   unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
     638     3018218 :   if (LType != RType)
     639     1590017 :     return (int)LType - (int)RType;
     640             : 
     641     2856402 :   if (Depth > MaxSCEVCompareDepth || EqCacheSCEV.count({LHS, RHS}))
     642             :     return 0;
     643             :   // Aside from the getSCEVType() ordering, the particular ordering
     644             :   // isn't very important except that it's beneficial to be consistent,
     645             :   // so that (a + b) and (b + a) don't end up as different expressions.
     646     1349521 :   switch (static_cast<SCEVTypes>(LType)) {
     647             :   case scUnknown: {
     648       78440 :     const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
     649       78440 :     const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
     650             : 
     651       78440 :     SmallSet<std::pair<Value *, Value *>, 8> EqCache;
     652      117660 :     int X = CompareValueComplexity(EqCache, LI, LU->getValue(), RU->getValue(),
     653       39220 :                                    Depth + 1);
     654       39220 :     if (X == 0)
     655       10723 :       EqCacheSCEV.insert({LHS, RHS});
     656             :     return X;
     657             :   }
     658             : 
     659     1009368 :   case scConstant: {
     660     2018736 :     const SCEVConstant *LC = cast<SCEVConstant>(LHS);
     661     2018736 :     const SCEVConstant *RC = cast<SCEVConstant>(RHS);
     662             : 
     663             :     // Compare constant values.
     664     1009368 :     const APInt &LA = LC->getAPInt();
     665     1009368 :     const APInt &RA = RC->getAPInt();
     666     1009368 :     unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
     667     1009368 :     if (LBitWidth != RBitWidth)
     668           1 :       return (int)LBitWidth - (int)RBitWidth;
     669     1009367 :     return LA.ult(RA) ? -1 : 1;
     670             :   }
     671             : 
     672       54829 :   case scAddRecExpr: {
     673      109658 :     const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
     674      109658 :     const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
     675             : 
     676             :     // There is always a dominance between two recs that are used by one SCEV,
     677             :     // so we can safely sort recs by loop header dominance. We require such
     678             :     // order in getAddExpr.
     679       54829 :     const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
     680       54829 :     if (LLoop != RLoop) {
     681       12684 :       const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
     682             :       assert(LHead != RHead && "Two loops share the same header?");
     683        4228 :       if (DT.dominates(LHead, RHead))
     684             :         return 1;
     685             :       else
     686             :         assert(DT.dominates(RHead, LHead) &&
     687             :                "No dominance between recurrences used by one SCEV?");
     688        1998 :       return -1;
     689             :     }
     690             : 
     691             :     // Addrec complexity grows with operand count.
     692       50601 :     unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands();
     693       50601 :     if (LNumOps != RNumOps)
     694       26169 :       return (int)LNumOps - (int)RNumOps;
     695             : 
     696             :     // Lexicographically compare.
     697       24780 :     for (unsigned i = 0; i != LNumOps; ++i) {
     698       73818 :       int X = CompareSCEVComplexity(EqCacheSCEV, LI, LA->getOperand(i),
     699       24606 :                                     RA->getOperand(i), DT,  Depth + 1);
     700       24606 :       if (X != 0)
     701             :         return X;
     702             :     }
     703           0 :     EqCacheSCEV.insert({LHS, RHS});
     704           0 :     return 0;
     705             :   }
     706             : 
     707      234678 :   case scAddExpr:
     708             :   case scMulExpr:
     709             :   case scSMaxExpr:
     710             :   case scUMaxExpr: {
     711      469356 :     const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS);
     712      469356 :     const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
     713             : 
     714             :     // Lexicographically compare n-ary expressions.
     715      234678 :     unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands();
     716      234678 :     if (LNumOps != RNumOps)
     717       87563 :       return (int)LNumOps - (int)RNumOps;
     718             : 
     719      392993 :     for (unsigned i = 0; i != LNumOps; ++i) {
     720      253531 :       if (i >= RNumOps)
     721             :         return 1;
     722      760593 :       int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getOperand(i),
     723      253531 :                                     RC->getOperand(i), DT, Depth + 1);
     724      253531 :       if (X != 0)
     725             :         return X;
     726             :     }
     727       16523 :     EqCacheSCEV.insert({LHS, RHS});
     728       16523 :     return 0;
     729             :   }
     730             : 
     731        3094 :   case scUDivExpr: {
     732        6188 :     const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS);
     733        6188 :     const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
     734             : 
     735             :     // Lexicographically compare udiv expressions.
     736        3094 :     int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getLHS(), RC->getLHS(),
     737        3094 :                                   DT, Depth + 1);
     738        3094 :     if (X != 0)
     739             :       return X;
     740          93 :     X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getRHS(), RC->getRHS(), DT,
     741             :                               Depth + 1);
     742          93 :     if (X == 0)
     743          90 :       EqCacheSCEV.insert({LHS, RHS});
     744             :     return X;
     745             :   }
     746             : 
     747        8332 :   case scTruncate:
     748             :   case scZeroExtend:
     749             :   case scSignExtend: {
     750       16664 :     const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS);
     751       16664 :     const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
     752             : 
     753             :     // Compare cast expressions by operand.
     754        8332 :     int X = CompareSCEVComplexity(EqCacheSCEV, LI, LC->getOperand(),
     755        8332 :                                   RC->getOperand(), DT, Depth + 1);
     756        8332 :     if (X == 0)
     757        1459 :       EqCacheSCEV.insert({LHS, RHS});
     758             :     return X;
     759             :   }
     760             : 
     761           0 :   case scCouldNotCompute:
     762           0 :     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
     763             :   }
     764           0 :   llvm_unreachable("Unknown SCEV kind!");
     765             : }
     766             : 
     767             : /// Given a list of SCEV objects, order them by their complexity, and group
     768             : /// objects of the same complexity together by value.  When this routine is
     769             : /// finished, we know that any duplicates in the vector are consecutive and that
     770             : /// complexity is monotonically increasing.
     771             : ///
     772             : /// Note that we go take special precautions to ensure that we get deterministic
     773             : /// results from this routine.  In other words, we don't want the results of
     774             : /// this to depend on where the addresses of various SCEV objects happened to
     775             : /// land in memory.
     776     2330185 : static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
     777             :                               LoopInfo *LI, DominatorTree &DT) {
     778     6801354 :   if (Ops.size() < 2) return;  // Noop
     779             : 
     780     2519386 :   SmallSet<std::pair<const SCEV *, const SCEV *>, 8> EqCache;
     781     4660370 :   if (Ops.size() == 2) {
     782             :     // This is the common case, which also happens to be trivially simple.
     783             :     // Special case it.
     784     6340314 :     const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
     785     2113438 :     if (CompareSCEVComplexity(EqCache, LI, RHS, LHS, DT) < 0)
     786             :       std::swap(LHS, RHS);
     787     2140984 :     return;
     788             :   }
     789             : 
     790             :   // Do the rough sort by complexity.
     791      433494 :   std::stable_sort(Ops.begin(), Ops.end(),
     792      595499 :                    [&EqCache, LI, &DT](const SCEV *LHS, const SCEV *RHS) {
     793             :                      return
     794      900214 :                          CompareSCEVComplexity(EqCache, LI, LHS, RHS, DT) < 0;
     795             :                    });
     796             : 
     797             :   // Now that we are sorted by complexity, group elements of the same
     798             :   // complexity.  Note that this is, at worst, N^2, but the vector is likely to
     799             :   // be extremely short in practice.  Note that we take this approach because we
     800             :   // do not want to depend on the addresses of the objects we are grouping.
     801      682723 :   for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
     802      553550 :     const SCEV *S = Ops[i];
     803      276775 :     unsigned Complexity = S->getSCEVType();
     804             : 
     805             :     // If there are any objects of the same complexity and same value as this
     806             :     // one, group them.
     807     1270349 :     for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
     808      798598 :       if (Ops[j] == S) { // Found a duplicate.
     809             :         // Move it to immediately after i'th element.
     810      232064 :         std::swap(Ops[i+1], Ops[j]);
     811       58016 :         ++i;   // no need to rescan it.
     812       58016 :         if (i == e-2) return;  // Done!
     813             :       }
     814             :     }
     815             :   }
     816             : }
     817             : 
     818             : // Returns the size of the SCEV S.
     819          36 : static inline int sizeOfSCEV(const SCEV *S) {
     820             :   struct FindSCEVSize {
     821             :     int Size = 0;
     822             : 
     823             :     FindSCEVSize() = default;
     824             : 
     825             :     bool follow(const SCEV *S) {
     826         101 :       ++Size;
     827             :       // Keep looking at all operands of S.
     828             :       return true;
     829             :     }
     830             : 
     831             :     bool isDone() const {
     832             :       return false;
     833             :     }
     834             :   };
     835             : 
     836          36 :   FindSCEVSize F;
     837          72 :   SCEVTraversal<FindSCEVSize> ST(F);
     838          36 :   ST.visitAll(S);
     839          72 :   return F.Size;
     840             : }
     841             : 
     842             : namespace {
     843             : 
     844             : struct SCEVDivision : public SCEVVisitor<SCEVDivision, void> {
     845             : public:
     846             :   // Computes the Quotient and Remainder of the division of Numerator by
     847             :   // Denominator.
     848       35394 :   static void divide(ScalarEvolution &SE, const SCEV *Numerator,
     849             :                      const SCEV *Denominator, const SCEV **Quotient,
     850             :                      const SCEV **Remainder) {
     851             :     assert(Numerator && Denominator && "Uninitialized SCEV");
     852             : 
     853       35394 :     SCEVDivision D(SE, Numerator, Denominator);
     854             : 
     855             :     // Check for the trivial case here to avoid having to check for it in the
     856             :     // rest of the code.
     857       35394 :     if (Numerator == Denominator) {
     858       10461 :       *Quotient = D.One;
     859       10461 :       *Remainder = D.Zero;
     860       23451 :       return;
     861             :     }
     862             : 
     863       24933 :     if (Numerator->isZero()) {
     864        2477 :       *Quotient = D.Zero;
     865        2477 :       *Remainder = D.Zero;
     866        2477 :       return;
     867             :     }
     868             : 
     869             :     // A simple case when N/1. The quotient is N.
     870       22456 :     if (Denominator->isOne()) {
     871          44 :       *Quotient = Numerator;
     872          44 :       *Remainder = D.Zero;
     873          44 :       return;
     874             :     }
     875             : 
     876             :     // Split the Denominator when it is a product.
     877           8 :     if (const SCEVMulExpr *T = dyn_cast<SCEVMulExpr>(Denominator)) {
     878             :       const SCEV *Q, *R;
     879           8 :       *Quotient = Numerator;
     880          16 :       for (const SCEV *Op : T->operands()) {
     881           8 :         divide(SE, *Quotient, Op, &Q, &R);
     882           8 :         *Quotient = Q;
     883             : 
     884             :         // Bail out when the Numerator is not divisible by one of the terms of
     885             :         // the Denominator.
     886           8 :         if (!R->isZero()) {
     887           8 :           *Quotient = D.Zero;
     888           8 :           *Remainder = Numerator;
     889           8 :           return;
     890             :         }
     891             :       }
     892           0 :       *Remainder = D.Zero;
     893           0 :       return;
     894             :     }
     895             : 
     896       22404 :     D.visit(Numerator);
     897       22404 :     *Quotient = D.Quotient;
     898       22404 :     *Remainder = D.Remainder;
     899             :   }
     900             : 
     901             :   // Except in the trivial case described above, we do not know how to divide
     902             :   // Expr by Denominator for the following functions with empty implementation.
     903             :   void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {}
     904             :   void visitZeroExtendExpr(const SCEVZeroExtendExpr *Numerator) {}
     905             :   void visitSignExtendExpr(const SCEVSignExtendExpr *Numerator) {}
     906             :   void visitUDivExpr(const SCEVUDivExpr *Numerator) {}
     907             :   void visitSMaxExpr(const SCEVSMaxExpr *Numerator) {}
     908             :   void visitUMaxExpr(const SCEVUMaxExpr *Numerator) {}
     909             :   void visitUnknown(const SCEVUnknown *Numerator) {}
     910             :   void visitCouldNotCompute(const SCEVCouldNotCompute *Numerator) {}
     911             : 
     912        3163 :   void visitConstant(const SCEVConstant *Numerator) {
     913        3428 :     if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
     914         795 :       APInt NumeratorVal = Numerator->getAPInt();
     915         795 :       APInt DenominatorVal = D->getAPInt();
     916         265 :       uint32_t NumeratorBW = NumeratorVal.getBitWidth();
     917         265 :       uint32_t DenominatorBW = DenominatorVal.getBitWidth();
     918             : 
     919         265 :       if (NumeratorBW > DenominatorBW)
     920           0 :         DenominatorVal = DenominatorVal.sext(NumeratorBW);
     921         265 :       else if (NumeratorBW < DenominatorBW)
     922           0 :         NumeratorVal = NumeratorVal.sext(DenominatorBW);
     923             : 
     924         795 :       APInt QuotientVal(NumeratorVal.getBitWidth(), 0);
     925         795 :       APInt RemainderVal(NumeratorVal.getBitWidth(), 0);
     926         265 :       APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal);
     927         265 :       Quotient = SE.getConstant(QuotientVal);
     928         265 :       Remainder = SE.getConstant(RemainderVal);
     929             :       return;
     930             :     }
     931             :   }
     932             : 
     933        8722 :   void visitAddRecExpr(const SCEVAddRecExpr *Numerator) {
     934             :     const SCEV *StartQ, *StartR, *StepQ, *StepR;
     935        8722 :     if (!Numerator->isAffine())
     936           9 :       return cannotDivide(Numerator);
     937       17442 :     divide(SE, Numerator->getStart(), Denominator, &StartQ, &StartR);
     938        8721 :     divide(SE, Numerator->getStepRecurrence(SE), Denominator, &StepQ, &StepR);
     939             :     // Bail out if the types do not match.
     940        8721 :     Type *Ty = Denominator->getType();
     941       26155 :     if (Ty != StartQ->getType() || Ty != StartR->getType() ||
     942       26147 :         Ty != StepQ->getType() || Ty != StepR->getType())
     943             :       return cannotDivide(Numerator);
     944       17426 :     Quotient = SE.getAddRecExpr(StartQ, StepQ, Numerator->getLoop(),
     945             :                                 Numerator->getNoWrapFlags());
     946       17426 :     Remainder = SE.getAddRecExpr(StartR, StepR, Numerator->getLoop(),
     947             :                                  Numerator->getNoWrapFlags());
     948             :   }
     949             : 
     950        1027 :   void visitAddExpr(const SCEVAddExpr *Numerator) {
     951        4108 :     SmallVector<const SCEV *, 2> Qs, Rs;
     952        1027 :     Type *Ty = Denominator->getType();
     953             : 
     954        4141 :     for (const SCEV *Op : Numerator->operands()) {
     955             :       const SCEV *Q, *R;
     956        2087 :       divide(SE, Op, Denominator, &Q, &R);
     957             : 
     958             :       // Bail out if types do not match.
     959        2087 :       if (Ty != Q->getType() || Ty != R->getType())
     960           0 :         return cannotDivide(Numerator);
     961             : 
     962        2087 :       Qs.push_back(Q);
     963        2087 :       Rs.push_back(R);
     964             :     }
     965             : 
     966        1027 :     if (Qs.size() == 1) {
     967           0 :       Quotient = Qs[0];
     968           0 :       Remainder = Rs[0];
     969           0 :       return;
     970             :     }
     971             : 
     972        1027 :     Quotient = SE.getAddExpr(Qs);
     973        1027 :     Remainder = SE.getAddExpr(Rs);
     974             :   }
     975             : 
     976        6564 :   void visitMulExpr(const SCEVMulExpr *Numerator) {
     977        6582 :     SmallVector<const SCEV *, 2> Qs;
     978        6564 :     Type *Ty = Denominator->getType();
     979             : 
     980        6564 :     bool FoundDenominatorTerm = false;
     981       29415 :     for (const SCEV *Op : Numerator->operands()) {
     982             :       // Bail out if types do not match.
     983       16287 :       if (Ty != Op->getType())
     984           0 :         return cannotDivide(Numerator);
     985             : 
     986       23089 :       if (FoundDenominatorTerm) {
     987        6802 :         Qs.push_back(Op);
     988       16567 :         continue;
     989             :       }
     990             : 
     991             :       // Check whether Denominator divides one of the product operands.
     992             :       const SCEV *Q, *R;
     993        9485 :       divide(SE, Op, Denominator, &Q, &R);
     994       12448 :       if (!R->isZero()) {
     995        2963 :         Qs.push_back(Op);
     996        2963 :         continue;
     997             :       }
     998             : 
     999             :       // Bail out if types do not match.
    1000        6522 :       if (Ty != Q->getType())
    1001             :         return cannotDivide(Numerator);
    1002             : 
    1003        6522 :       FoundDenominatorTerm = true;
    1004        6522 :       Qs.push_back(Q);
    1005             :     }
    1006             : 
    1007        6564 :     if (FoundDenominatorTerm) {
    1008        6522 :       Remainder = Zero;
    1009        6522 :       if (Qs.size() == 1)
    1010           0 :         Quotient = Qs[0];
    1011             :       else
    1012        6522 :         Quotient = SE.getMulExpr(Qs);
    1013             :       return;
    1014             :     }
    1015             : 
    1016          84 :     if (!isa<SCEVUnknown>(Denominator))
    1017             :       return cannotDivide(Numerator);
    1018             : 
    1019             :     // The Remainder is obtained by replacing Denominator by 0 in Numerator.
    1020          36 :     ValueToValueMap RewriteMap;
    1021          72 :     RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
    1022          36 :         cast<SCEVConstant>(Zero)->getValue();
    1023          18 :     Remainder = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
    1024             : 
    1025          18 :     if (Remainder->isZero()) {
    1026             :       // The Quotient is obtained by replacing Denominator by 1 in Numerator.
    1027           0 :       RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
    1028           0 :           cast<SCEVConstant>(One)->getValue();
    1029           0 :       Quotient =
    1030           0 :           SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
    1031           0 :       return;
    1032             :     }
    1033             : 
    1034             :     // Quotient is (Numerator - Remainder) divided by Denominator.
    1035             :     const SCEV *Q, *R;
    1036          18 :     const SCEV *Diff = SE.getMinusSCEV(Numerator, Remainder);
    1037             :     // This SCEV does not seem to simplify: fail the division here.
    1038          18 :     if (sizeOfSCEV(Diff) > sizeOfSCEV(Numerator))
    1039             :       return cannotDivide(Numerator);
    1040          18 :     divide(SE, Diff, Denominator, &Q, &R);
    1041          18 :     if (R != Zero)
    1042             :       return cannotDivide(Numerator);
    1043          18 :     Quotient = Q;
    1044             :   }
    1045             : 
    1046             : private:
    1047       35394 :   SCEVDivision(ScalarEvolution &S, const SCEV *Numerator,
    1048             :                const SCEV *Denominator)
    1049       35394 :       : SE(S), Denominator(Denominator) {
    1050       70788 :     Zero = SE.getZero(Denominator->getType());
    1051       70788 :     One = SE.getOne(Denominator->getType());
    1052             : 
    1053             :     // We generally do not know how to divide Expr by Denominator. We
    1054             :     // initialize the division to a "cannot divide" state to simplify the rest
    1055             :     // of the code.
    1056       35394 :     cannotDivide(Numerator);
    1057       35394 :   }
    1058             : 
    1059             :   // Convenience function for giving up on the division. We set the quotient to
    1060             :   // be equal to zero and the remainder to be equal to the numerator.
    1061             :   void cannotDivide(const SCEV *Numerator) {
    1062       35427 :     Quotient = Zero;
    1063       35427 :     Remainder = Numerator;
    1064             :   }
    1065             : 
    1066             :   ScalarEvolution &SE;
    1067             :   const SCEV *Denominator, *Quotient, *Remainder, *Zero, *One;
    1068             : };
    1069             : 
    1070             : } // end anonymous namespace
    1071             : 
    1072             : //===----------------------------------------------------------------------===//
    1073             : //                      Simple SCEV method implementations
    1074             : //===----------------------------------------------------------------------===//
    1075             : 
    1076             : /// Compute BC(It, K).  The result has width W.  Assume, K > 0.
    1077       27870 : static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
    1078             :                                        ScalarEvolution &SE,
    1079             :                                        Type *ResultTy) {
    1080             :   // Handle the simplest case efficiently.
    1081       27870 :   if (K == 1)
    1082       25754 :     return SE.getTruncateOrZeroExtend(It, ResultTy);
    1083             : 
    1084             :   // We are using the following formula for BC(It, K):
    1085             :   //
    1086             :   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
    1087             :   //
    1088             :   // Suppose, W is the bitwidth of the return value.  We must be prepared for
    1089             :   // overflow.  Hence, we must assure that the result of our computation is
    1090             :   // equal to the accurate one modulo 2^W.  Unfortunately, division isn't
    1091             :   // safe in modular arithmetic.
    1092             :   //
    1093             :   // However, this code doesn't use exactly that formula; the formula it uses
    1094             :   // is something like the following, where T is the number of factors of 2 in
    1095             :   // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
    1096             :   // exponentiation:
    1097             :   //
    1098             :   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
    1099             :   //
    1100             :   // This formula is trivially equivalent to the previous formula.  However,
    1101             :   // this formula can be implemented much more efficiently.  The trick is that
    1102             :   // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
    1103             :   // arithmetic.  To do exact division in modular arithmetic, all we have
    1104             :   // to do is multiply by the inverse.  Therefore, this step can be done at
    1105             :   // width W.
    1106             :   //
    1107             :   // The next issue is how to safely do the division by 2^T.  The way this
    1108             :   // is done is by doing the multiplication step at a width of at least W + T
    1109             :   // bits.  This way, the bottom W+T bits of the product are accurate. Then,
    1110             :   // when we perform the division by 2^T (which is equivalent to a right shift
    1111             :   // by T), the bottom W bits are accurate.  Extra bits are okay; they'll get
    1112             :   // truncated out after the division by 2^T.
    1113             :   //
    1114             :   // In comparison to just directly using the first formula, this technique
    1115             :   // is much more efficient; using the first formula requires W * K bits,
    1116             :   // but this formula less than W + K bits. Also, the first formula requires
    1117             :   // a division step, whereas this formula only requires multiplies and shifts.
    1118             :   //
    1119             :   // It doesn't matter whether the subtraction step is done in the calculation
    1120             :   // width or the input iteration count's width; if the subtraction overflows,
    1121             :   // the result must be zero anyway.  We prefer here to do it in the width of
    1122             :   // the induction variable because it helps a lot for certain cases; CodeGen
    1123             :   // isn't smart enough to ignore the overflow, which leads to much less
    1124             :   // efficient code if the width of the subtraction is wider than the native
    1125             :   // register width.
    1126             :   //
    1127             :   // (It's possible to not widen at all by pulling out factors of 2 before
    1128             :   // the multiplication; for example, K=2 can be calculated as
    1129             :   // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
    1130             :   // extra arithmetic, so it's not an obvious win, and it gets
    1131             :   // much more complicated for K > 3.)
    1132             : 
    1133             :   // Protection from insane SCEVs; this bound is conservative,
    1134             :   // but it probably doesn't matter.
    1135        2116 :   if (K > 1000)
    1136           0 :     return SE.getCouldNotCompute();
    1137             : 
    1138        2116 :   unsigned W = SE.getTypeSizeInBits(ResultTy);
    1139             : 
    1140             :   // Calculate K! / 2^T and T; we divide out the factors of two before
    1141             :   // multiplying for calculating K! / 2^T to avoid overflow.
    1142             :   // Other overflow doesn't matter because we only care about the bottom
    1143             :   // W bits of the result.
    1144             :   APInt OddFactorial(W, 1);
    1145             :   unsigned T = 1;
    1146        5458 :   for (unsigned i = 3; i <= K; ++i) {
    1147        5013 :     APInt Mult(W, i);
    1148        1671 :     unsigned TwoFactors = Mult.countTrailingZeros();
    1149        1671 :     T += TwoFactors;
    1150        1671 :     Mult.lshrInPlace(TwoFactors);
    1151        1671 :     OddFactorial *= Mult;
    1152             :   }
    1153             : 
    1154             :   // We need at least W + T bits for the multiplication step
    1155        2116 :   unsigned CalculationBits = W + T;
    1156             : 
    1157             :   // Calculate 2^T, at width T+W.
    1158        4232 :   APInt DivFactor = APInt::getOneBitSet(CalculationBits, T);
    1159             : 
    1160             :   // Calculate the multiplicative inverse of K! / 2^T;
    1161             :   // this multiplication factor will perform the exact division by
    1162             :   // K! / 2^T.
    1163        4232 :   APInt Mod = APInt::getSignedMinValue(W+1);
    1164        4232 :   APInt MultiplyFactor = OddFactorial.zext(W+1);
    1165        6348 :   MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
    1166        6348 :   MultiplyFactor = MultiplyFactor.trunc(W);
    1167             : 
    1168             :   // Calculate the product, at width T+W
    1169        2116 :   IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
    1170        2116 :                                                       CalculationBits);
    1171        2116 :   const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
    1172        5903 :   for (unsigned i = 1; i != K; ++i) {
    1173        3787 :     const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
    1174        3787 :     Dividend = SE.getMulExpr(Dividend,
    1175             :                              SE.getTruncateOrZeroExtend(S, CalculationTy));
    1176             :   }
    1177             : 
    1178             :   // Divide by 2^T
    1179        2116 :   const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
    1180             : 
    1181             :   // Truncate the result, and divide by K! / 2^T.
    1182             : 
    1183        2116 :   return SE.getMulExpr(SE.getConstant(MultiplyFactor),
    1184        2116 :                        SE.getTruncateOrZeroExtend(DivResult, ResultTy));
    1185             : }
    1186             : 
    1187             : /// Return the value of this chain of recurrences at the specified iteration
    1188             : /// number.  We can evaluate this recurrence by multiplying each element in the
    1189             : /// chain by the binomial coefficient corresponding to it.  In other words, we
    1190             : /// can evaluate {A,+,B,+,C,+,D} as:
    1191             : ///
    1192             : ///   A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
    1193             : ///
    1194             : /// where BC(It, k) stands for binomial coefficient.
    1195       25754 : const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
    1196             :                                                 ScalarEvolution &SE) const {
    1197       51508 :   const SCEV *Result = getStart();
    1198       53624 :   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
    1199             :     // The computation is correct in the face of overflow provided that the
    1200             :     // multiplication is performed _after_ the evaluation of the binomial
    1201             :     // coefficient.
    1202       55740 :     const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
    1203       55740 :     if (isa<SCEVCouldNotCompute>(Coeff))
    1204             :       return Coeff;
    1205             : 
    1206       55740 :     Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
    1207             :   }
    1208             :   return Result;
    1209             : }
    1210             : 
    1211             : //===----------------------------------------------------------------------===//
    1212             : //                    SCEV Expression folder implementations
    1213             : //===----------------------------------------------------------------------===//
    1214             : 
    1215       18523 : const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
    1216             :                                              Type *Ty) {
    1217             :   assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
    1218             :          "This is not a truncating conversion!");
    1219             :   assert(isSCEVable(Ty) &&
    1220             :          "This is not a conversion to a SCEVable type!");
    1221       18523 :   Ty = getEffectiveSCEVType(Ty);
    1222             : 
    1223       37046 :   FoldingSetNodeID ID;
    1224       18523 :   ID.AddInteger(scTruncate);
    1225       18523 :   ID.AddPointer(Op);
    1226       18523 :   ID.AddPointer(Ty);
    1227       18523 :   void *IP = nullptr;
    1228       37046 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    1229             : 
    1230             :   // Fold if the operand is constant.
    1231       10603 :   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
    1232       21206 :     return getConstant(
    1233       10603 :       cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
    1234             : 
    1235             :   // trunc(trunc(x)) --> trunc(x)
    1236          11 :   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
    1237          11 :     return getTruncateExpr(ST->getOperand(), Ty);
    1238             : 
    1239             :   // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
    1240         206 :   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
    1241         206 :     return getTruncateOrSignExtend(SS->getOperand(), Ty);
    1242             : 
    1243             :   // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
    1244         255 :   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
    1245         255 :     return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
    1246             : 
    1247             :   // trunc(x1+x2+...+xN) --> trunc(x1)+trunc(x2)+...+trunc(xN) if we can
    1248             :   // eliminate all the truncates, or we replace other casts with truncates.
    1249         635 :   if (const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) {
    1250         990 :     SmallVector<const SCEV *, 4> Operands;
    1251         635 :     bool hasTrunc = false;
    1252        1808 :     for (unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) {
    1253        2346 :       const SCEV *S = getTruncateExpr(SA->getOperand(i), Ty);
    1254        2346 :       if (!isa<SCEVCastExpr>(SA->getOperand(i)))
    1255        1724 :         hasTrunc = isa<SCEVTruncateExpr>(S);
    1256        1173 :       Operands.push_back(S);
    1257             :     }
    1258         635 :     if (!hasTrunc)
    1259         560 :       return getAddExpr(Operands);
    1260         710 :     UniqueSCEVs.FindNodeOrInsertPos(ID, IP);  // Mutates IP, returns NULL.
    1261             :   }
    1262             : 
    1263             :   // trunc(x1*x2*...*xN) --> trunc(x1)*trunc(x2)*...*trunc(xN) if we can
    1264             :   // eliminate all the truncates, or we replace other casts with truncates.
    1265          89 :   if (const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) {
    1266         145 :     SmallVector<const SCEV *, 4> Operands;
    1267          89 :     bool hasTrunc = false;
    1268         267 :     for (unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) {
    1269         356 :       const SCEV *S = getTruncateExpr(SM->getOperand(i), Ty);
    1270         356 :       if (!isa<SCEVCastExpr>(SM->getOperand(i)))
    1271         268 :         hasTrunc = isa<SCEVTruncateExpr>(S);
    1272         178 :       Operands.push_back(S);
    1273             :     }
    1274          89 :     if (!hasTrunc)
    1275          66 :       return getMulExpr(Operands);
    1276         112 :     UniqueSCEVs.FindNodeOrInsertPos(ID, IP);  // Mutates IP, returns NULL.
    1277             :   }
    1278             : 
    1279             :   // If the input value is a chrec scev, truncate the chrec's operands.
    1280        2592 :   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
    1281        5184 :     SmallVector<const SCEV *, 4> Operands;
    1282       10370 :     for (const SCEV *Op : AddRec->operands())
    1283        5186 :       Operands.push_back(getTruncateExpr(Op, Ty));
    1284        2592 :     return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
    1285             :   }
    1286             : 
    1287             :   // The cast wasn't folded; create an explicit cast node. We can reuse
    1288             :   // the existing insert position since if we get here, we won't have
    1289             :   // made any changes which would invalidate it.
    1290        4780 :   SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
    1291        4780 :                                                  Op, Ty);
    1292        4780 :   UniqueSCEVs.InsertNode(S, IP);
    1293        2390 :   return S;
    1294             : }
    1295             : 
    1296             : // Get the limit of a recurrence such that incrementing by Step cannot cause
    1297             : // signed overflow as long as the value of the recurrence within the
    1298             : // loop does not exceed this limit before incrementing.
    1299        4344 : static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
    1300             :                                                  ICmpInst::Predicate *Pred,
    1301             :                                                  ScalarEvolution *SE) {
    1302        4344 :   unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
    1303        4344 :   if (SE->isKnownPositive(Step)) {
    1304        2614 :     *Pred = ICmpInst::ICMP_SLT;
    1305        7842 :     return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
    1306        7842 :                            SE->getSignedRangeMax(Step));
    1307             :   }
    1308        1730 :   if (SE->isKnownNegative(Step)) {
    1309        1663 :     *Pred = ICmpInst::ICMP_SGT;
    1310        4989 :     return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
    1311        4989 :                            SE->getSignedRangeMin(Step));
    1312             :   }
    1313             :   return nullptr;
    1314             : }
    1315             : 
    1316             : // Get the limit of a recurrence such that incrementing by Step cannot cause
    1317             : // unsigned overflow as long as the value of the recurrence within the loop does
    1318             : // not exceed this limit before incrementing.
    1319         415 : static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
    1320             :                                                    ICmpInst::Predicate *Pred,
    1321             :                                                    ScalarEvolution *SE) {
    1322         415 :   unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
    1323         415 :   *Pred = ICmpInst::ICMP_ULT;
    1324             : 
    1325        1245 :   return SE->getConstant(APInt::getMinValue(BitWidth) -
    1326        1245 :                          SE->getUnsignedRangeMax(Step));
    1327             : }
    1328             : 
    1329             : namespace {
    1330             : 
    1331             : struct ExtendOpTraitsBase {
    1332             :   typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *,
    1333             :                                                           unsigned);
    1334             : };
    1335             : 
    1336             : // Used to make code generic over signed and unsigned overflow.
    1337             : template <typename ExtendOp> struct ExtendOpTraits {
    1338             :   // Members present:
    1339             :   //
    1340             :   // static const SCEV::NoWrapFlags WrapType;
    1341             :   //
    1342             :   // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
    1343             :   //
    1344             :   // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
    1345             :   //                                           ICmpInst::Predicate *Pred,
    1346             :   //                                           ScalarEvolution *SE);
    1347             : };
    1348             : 
    1349             : template <>
    1350             : struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
    1351             :   static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
    1352             : 
    1353             :   static const GetExtendExprTy GetExtendExpr;
    1354             : 
    1355             :   static const SCEV *getOverflowLimitForStep(const SCEV *Step,
    1356             :                                              ICmpInst::Predicate *Pred,
    1357             :                                              ScalarEvolution *SE) {
    1358         140 :     return getSignedOverflowLimitForStep(Step, Pred, SE);
    1359             :   }
    1360             : };
    1361             : 
    1362             : const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
    1363             :     SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
    1364             : 
    1365             : template <>
    1366             : struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
    1367             :   static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
    1368             : 
    1369             :   static const GetExtendExprTy GetExtendExpr;
    1370             : 
    1371             :   static const SCEV *getOverflowLimitForStep(const SCEV *Step,
    1372             :                                              ICmpInst::Predicate *Pred,
    1373             :                                              ScalarEvolution *SE) {
    1374         415 :     return getUnsignedOverflowLimitForStep(Step, Pred, SE);
    1375             :   }
    1376             : };
    1377             : 
    1378             : const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
    1379             :     SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
    1380             : 
    1381             : } // end anonymous namespace
    1382             : 
    1383             : // The recurrence AR has been shown to have no signed/unsigned wrap or something
    1384             : // close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
    1385             : // easily prove NSW/NUW for its preincrement or postincrement sibling. This
    1386             : // allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
    1387             : // Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
    1388             : // expression "Step + sext/zext(PreIncAR)" is congruent with
    1389             : // "sext/zext(PostIncAR)"
    1390             : template <typename ExtendOpTy>
    1391       27693 : static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty,
    1392             :                                         ScalarEvolution *SE, unsigned Depth) {
    1393       27693 :   auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
    1394       27693 :   auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
    1395             : 
    1396       27693 :   const Loop *L = AR->getLoop();
    1397       55386 :   const SCEV *Start = AR->getStart();
    1398       27693 :   const SCEV *Step = AR->getStepRecurrence(*SE);
    1399             : 
    1400             :   // Check for a simple looking step prior to loop entry.
    1401         644 :   const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
    1402             :   if (!SA)
    1403             :     return nullptr;
    1404             : 
    1405             :   // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
    1406             :   // subtraction is expensive. For this purpose, perform a quick and dirty
    1407             :   // difference, by checking for Step in the operand list.
    1408         644 :   SmallVector<const SCEV *, 4> DiffOps;
    1409        2580 :   for (const SCEV *Op : SA->operands())
    1410        1292 :     if (Op != Step)
    1411         816 :       DiffOps.push_back(Op);
    1412             : 
    1413         644 :   if (DiffOps.size() == SA->getNumOperands())
    1414             :     return nullptr;
    1415             : 
    1416             :   // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
    1417             :   // `Step`:
    1418             : 
    1419             :   // 1. NSW/NUW flags on the step increment.
    1420         476 :   auto PreStartFlags =
    1421         476 :     ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW);
    1422         476 :   const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags);
    1423         952 :   const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
    1424             :       SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
    1425             : 
    1426             :   // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
    1427             :   // "S+X does not sign/unsign-overflow".
    1428             :   //
    1429             : 
    1430         476 :   const SCEV *BECount = SE->getBackedgeTakenCount(L);
    1431        1112 :   if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
    1432         764 :       !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
    1433             :     return PreStart;
    1434             : 
    1435             :   // 2. Direct overflow check on the step operation's expression.
    1436         946 :   unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
    1437         946 :   Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
    1438         473 :   const SCEV *OperandExtendedStart =
    1439             :       SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth),
    1440             :                      (SE->*GetExtendExpr)(Step, WideTy, Depth));
    1441         473 :   if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) {
    1442         234 :     if (PreAR && AR->getNoWrapFlags(WrapType)) {
    1443             :       // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
    1444             :       // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
    1445             :       // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`.  Cache this fact.
    1446         116 :       const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(WrapType);
    1447             :     }
    1448             :     return PreStart;
    1449             :   }
    1450             : 
    1451             :   // 3. Loop precondition.
    1452             :   ICmpInst::Predicate Pred;
    1453         356 :   const SCEV *OverflowLimit =
    1454             :       ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
    1455             : 
    1456         712 :   if (OverflowLimit &&
    1457         356 :       SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit))
    1458             :     return PreStart;
    1459             : 
    1460             :   return nullptr;
    1461             : }
    1462             : 
    1463             : // Get the normalized zero or sign extended expression for this AddRec's Start.
    1464             : template <typename ExtendOpTy>
    1465       27693 : static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
    1466             :                                         ScalarEvolution *SE,
    1467             :                                         unsigned Depth) {
    1468       27693 :   auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
    1469             : 
    1470       27693 :   const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE, Depth);
    1471       27693 :   if (!PreStart)
    1472       54994 :     return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth);
    1473             : 
    1474             :   return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty,
    1475             :                                              Depth),
    1476         196 :                         (SE->*GetExtendExpr)(PreStart, Ty, Depth));
    1477             : }
    1478             : 
    1479             : // Try to prove away overflow by looking at "nearby" add recurrences.  A
    1480             : // motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
    1481             : // does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
    1482             : //
    1483             : // Formally:
    1484             : //
    1485             : //     {S,+,X} == {S-T,+,X} + T
    1486             : //  => Ext({S,+,X}) == Ext({S-T,+,X} + T)
    1487             : //
    1488             : // If ({S-T,+,X} + T) does not overflow  ... (1)
    1489             : //
    1490             : //  RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
    1491             : //
    1492             : // If {S-T,+,X} does not overflow  ... (2)
    1493             : //
    1494             : //  RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
    1495             : //      == {Ext(S-T)+Ext(T),+,Ext(X)}
    1496             : //
    1497             : // If (S-T)+T does not overflow  ... (3)
    1498             : //
    1499             : //  RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
    1500             : //      == {Ext(S),+,Ext(X)} == LHS
    1501             : //
    1502             : // Thus, if (1), (2) and (3) are true for some T, then
    1503             : //   Ext({S,+,X}) == {Ext(S),+,Ext(X)}
    1504             : //
    1505             : // (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
    1506             : // does not overflow" restricted to the 0th iteration.  Therefore we only need
    1507             : // to check for (1) and (2).
    1508             : //
    1509             : // In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
    1510             : // is `Delta` (defined below).
    1511             : template <typename ExtendOpTy>
    1512       12646 : bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
    1513             :                                                 const SCEV *Step,
    1514             :                                                 const Loop *L) {
    1515       12646 :   auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
    1516             : 
    1517             :   // We restrict `Start` to a constant to prevent SCEV from spending too much
    1518             :   // time here.  It is correct (but more expensive) to continue with a
    1519             :   // non-constant `Start` and do a general SCEV subtraction to compute
    1520             :   // `PreStart` below.
    1521        4647 :   const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
    1522             :   if (!StartC)
    1523             :     return false;
    1524             : 
    1525        4647 :   APInt StartAI = StartC->getAPInt();
    1526             : 
    1527       41757 :   for (unsigned Delta : {-2, -1, 1, 2}) {
    1528       92865 :     const SCEV *PreStart = getConstant(StartAI - Delta);
    1529             : 
    1530       37128 :     FoldingSetNodeID ID;
    1531       18573 :     ID.AddInteger(scAddRecExpr);
    1532       18573 :     ID.AddPointer(PreStart);
    1533       18573 :     ID.AddPointer(Step);
    1534       18573 :     ID.AddPointer(L);
    1535       18573 :     void *IP = nullptr;
    1536       18573 :     const auto *PreAR =
    1537       18573 :       static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
    1538             : 
    1539             :     // Give up if we don't already have the add recurrence we need because
    1540             :     // actually constructing an add recurrence is relatively expensive.
    1541       23913 :     if (PreAR && PreAR->getNoWrapFlags(WrapType)) {  // proves (2)
    1542         199 :       const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
    1543         199 :       ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
    1544         199 :       const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
    1545             :           DeltaS, &Pred, this);
    1546         199 :       if (Limit && isKnownPredicate(Pred, PreAR, Limit))  // proves (1)
    1547          18 :         return true;
    1548             :     }
    1549             :   }
    1550             : 
    1551             :   return false;
    1552             : }
    1553             : 
    1554             : const SCEV *
    1555      184481 : ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
    1556             :   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
    1557             :          "This is not an extending conversion!");
    1558             :   assert(isSCEVable(Ty) &&
    1559             :          "This is not a conversion to a SCEVable type!");
    1560      184481 :   Ty = getEffectiveSCEVType(Ty);
    1561             : 
    1562             :   // Fold if the operand is constant.
    1563       92441 :   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
    1564      184882 :     return getConstant(
    1565       92441 :       cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty)));
    1566             : 
    1567             :   // zext(zext(x)) --> zext(x)
    1568        4239 :   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
    1569        4239 :     return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
    1570             : 
    1571             :   // Before doing any expensive analysis, check to see if we've already
    1572             :   // computed a SCEV for this Op and Ty.
    1573       87801 :   FoldingSetNodeID ID;
    1574       87801 :   ID.AddInteger(scZeroExtend);
    1575       87801 :   ID.AddPointer(Op);
    1576       87801 :   ID.AddPointer(Ty);
    1577       87801 :   void *IP = nullptr;
    1578      175602 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    1579       44387 :   if (Depth > MaxExtDepth) {
    1580          24 :     SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
    1581          24 :                                                      Op, Ty);
    1582          24 :     UniqueSCEVs.InsertNode(S, IP);
    1583          12 :     return S;
    1584             :   }
    1585             : 
    1586             :   // zext(trunc(x)) --> zext(x) or x or trunc(x)
    1587        1159 :   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
    1588             :     // It's possible the bits taken off by the truncate were all zero bits. If
    1589             :     // so, we should be able to simplify this further.
    1590        1159 :     const SCEV *X = ST->getOperand();
    1591        2214 :     ConstantRange CR = getUnsignedRange(X);
    1592        1159 :     unsigned TruncBits = getTypeSizeInBits(ST->getType());
    1593        1159 :     unsigned NewBits = getTypeSizeInBits(Ty);
    1594        3477 :     if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
    1595        2318 :             CR.zextOrTrunc(NewBits)))
    1596         104 :       return getTruncateOrZeroExtend(X, Ty);
    1597             :   }
    1598             : 
    1599             :   // If the input value is a chrec scev, and we can prove that the value
    1600             :   // did not overflow the old, smaller, value, we can zero extend all of the
    1601             :   // operands (often constants).  This allows analysis of something like
    1602             :   // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
    1603       25275 :   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
    1604       25275 :     if (AR->isAffine()) {
    1605       25223 :       const SCEV *Start = AR->getStart();
    1606       25223 :       const SCEV *Step = AR->getStepRecurrence(*this);
    1607       50446 :       unsigned BitWidth = getTypeSizeInBits(AR->getType());
    1608       25223 :       const Loop *L = AR->getLoop();
    1609             : 
    1610       50446 :       if (!AR->hasNoUnsignedWrap()) {
    1611       19700 :         auto NewFlags = proveNoWrapViaConstantRanges(AR);
    1612       19700 :         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
    1613             :       }
    1614             : 
    1615             :       // If we have special knowledge that this addrec won't overflow,
    1616             :       // we don't need to do any further analysis.
    1617       50446 :       if (AR->hasNoUnsignedWrap())
    1618       14882 :         return getAddRecExpr(
    1619             :             getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
    1620        7441 :             getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
    1621             : 
    1622             :       // Check whether the backedge-taken count is SCEVCouldNotCompute.
    1623             :       // Note that this serves two purposes: It filters out loops that are
    1624             :       // simply not analyzable, and it covers the case where this code is
    1625             :       // being called from within backedge-taken count analysis, such that
    1626             :       // attempting to ask for the backedge-taken count would likely result
    1627             :       // in infinite recursion. In the later case, the analysis code will
    1628             :       // cope with a conservative value, and it will take care to purge
    1629             :       // that value once it has finished.
    1630       17782 :       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
    1631       35564 :       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
    1632             :         // Manually compute the final value for AR, checking for
    1633             :         // overflow.
    1634             : 
    1635             :         // Check whether the backedge-taken count can be losslessly casted to
    1636             :         // the addrec's type. The count is always unsigned.
    1637             :         const SCEV *CastedMaxBECount =
    1638       15302 :           getTruncateOrZeroExtend(MaxBECount, Start->getType());
    1639             :         const SCEV *RecastedMaxBECount =
    1640       15302 :           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
    1641       15302 :         if (MaxBECount == RecastedMaxBECount) {
    1642       28756 :           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
    1643             :           // Check whether Start+Step*MaxBECount has no unsigned overflow.
    1644       14378 :           const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step,
    1645       14378 :                                         SCEV::FlagAnyWrap, Depth + 1);
    1646       14378 :           const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul,
    1647             :                                                           SCEV::FlagAnyWrap,
    1648             :                                                           Depth + 1),
    1649       14378 :                                                WideTy, Depth + 1);
    1650       14378 :           const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1);
    1651             :           const SCEV *WideMaxBECount =
    1652       14378 :             getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
    1653             :           const SCEV *OperandExtendedAdd =
    1654       14378 :             getAddExpr(WideStart,
    1655             :                        getMulExpr(WideMaxBECount,
    1656             :                                   getZeroExtendExpr(Step, WideTy, Depth + 1),
    1657             :                                   SCEV::FlagAnyWrap, Depth + 1),
    1658       14378 :                        SCEV::FlagAnyWrap, Depth + 1);
    1659       14378 :           if (ZAdd == OperandExtendedAdd) {
    1660             :             // Cache knowledge of AR NUW, which is propagated to this AddRec.
    1661         786 :             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
    1662             :             // Return the expression with the addrec on the outside.
    1663         786 :             return getAddRecExpr(
    1664             :                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
    1665             :                                                          Depth + 1),
    1666             :                 getZeroExtendExpr(Step, Ty, Depth + 1), L,
    1667         393 :                 AR->getNoWrapFlags());
    1668             :           }
    1669             :           // Similar to above, only this time treat the step value as signed.
    1670             :           // This covers loops that count down.
    1671       13985 :           OperandExtendedAdd =
    1672       13985 :             getAddExpr(WideStart,
    1673             :                        getMulExpr(WideMaxBECount,
    1674             :                                   getSignExtendExpr(Step, WideTy, Depth + 1),
    1675             :                                   SCEV::FlagAnyWrap, Depth + 1),
    1676             :                        SCEV::FlagAnyWrap, Depth + 1);
    1677       13985 :           if (ZAdd == OperandExtendedAdd) {
    1678             :             // Cache knowledge of AR NW, which is propagated to this AddRec.
    1679             :             // Negative step causes unsigned wrap, but it still can't self-wrap.
    1680        1504 :             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
    1681             :             // Return the expression with the addrec on the outside.
    1682        1504 :             return getAddRecExpr(
    1683             :                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
    1684             :                                                          Depth + 1),
    1685             :                 getSignExtendExpr(Step, Ty, Depth + 1), L,
    1686         752 :                 AR->getNoWrapFlags());
    1687             :           }
    1688             :         }
    1689             :       }
    1690             : 
    1691             :       // Normally, in the cases we can prove no-overflow via a
    1692             :       // backedge guarding condition, we can also compute a backedge
    1693             :       // taken count for the loop.  The exceptions are assumptions and
    1694             :       // guards present in the loop -- SCEV is not great at exploiting
    1695             :       // these to compute max backedge taken counts, but can still use
    1696             :       // these to prove lack of overflow.  Use this fact to avoid
    1697             :       // doing extra work that may not pay off.
    1698       35745 :       if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
    1699        4942 :           !AC.assumptions().empty()) {
    1700             :         // If the backedge is guarded by a comparison with the pre-inc
    1701             :         // value the addrec is safe. Also, if the entry is guarded by
    1702             :         // a comparison with the start value and the backedge is
    1703             :         // guarded by a comparison with the post-inc value, the addrec
    1704             :         // is safe.
    1705       14199 :         if (isKnownPositive(Step)) {
    1706        7275 :           const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
    1707        7275 :                                       getUnsignedRangeMax(Step));
    1708        4813 :           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
    1709        3413 :               (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_ULT, Start, N) &&
    1710        1025 :                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT,
    1711        1025 :                                            AR->getPostIncExpr(*this), N))) {
    1712             :             // Cache knowledge of AR NUW, which is propagated to this
    1713             :             // AddRec.
    1714         122 :             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
    1715             :             // Return the expression with the addrec on the outside.
    1716         122 :             return getAddRecExpr(
    1717             :                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
    1718             :                                                          Depth + 1),
    1719             :                 getZeroExtendExpr(Step, Ty, Depth + 1), L,
    1720          61 :                 AR->getNoWrapFlags());
    1721             :           }
    1722       11774 :         } else if (isKnownNegative(Step)) {
    1723       35028 :           const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
    1724       35028 :                                       getSignedRangeMin(Step));
    1725       13152 :           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
    1726        1971 :               (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_UGT, Start, N) &&
    1727         495 :                isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT,
    1728         495 :                                            AR->getPostIncExpr(*this), N))) {
    1729             :             // Cache knowledge of AR NW, which is propagated to this
    1730             :             // AddRec.  Negative step causes unsigned wrap, but it
    1731             :             // still can't self-wrap.
    1732       20678 :             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
    1733             :             // Return the expression with the addrec on the outside.
    1734       20678 :             return getAddRecExpr(
    1735             :                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
    1736             :                                                          Depth + 1),
    1737             :                 getSignExtendExpr(Step, Ty, Depth + 1), L,
    1738       10339 :                 AR->getNoWrapFlags());
    1739             :           }
    1740             :         }
    1741             :       }
    1742             : 
    1743        6237 :       if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
    1744          20 :         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
    1745          20 :         return getAddRecExpr(
    1746             :             getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
    1747          10 :             getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
    1748             :       }
    1749             :     }
    1750             : 
    1751        5952 :   if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
    1752             :     // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
    1753       11904 :     if (SA->hasNoUnsignedWrap()) {
    1754             :       // If the addition does not unsign overflow then we can, by definition,
    1755             :       // commute the zero extension with the addition operation.
    1756         216 :       SmallVector<const SCEV *, 4> Ops;
    1757         432 :       for (const auto *Op : SA->operands())
    1758         216 :         Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
    1759         108 :       return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1);
    1760             :     }
    1761             :   }
    1762             : 
    1763             :   // The cast wasn't folded; create an explicit cast node.
    1764             :   // Recompute the insert position, as it may have been invalidated.
    1765       50334 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    1766       49228 :   SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
    1767       49228 :                                                    Op, Ty);
    1768       49228 :   UniqueSCEVs.InsertNode(S, IP);
    1769       24614 :   return S;
    1770             : }
    1771             : 
    1772             : const SCEV *
    1773      101795 : ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
    1774             :   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
    1775             :          "This is not an extending conversion!");
    1776             :   assert(isSCEVable(Ty) &&
    1777             :          "This is not a conversion to a SCEVable type!");
    1778      101795 :   Ty = getEffectiveSCEVType(Ty);
    1779             : 
    1780             :   // Fold if the operand is constant.
    1781       62475 :   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
    1782      124950 :     return getConstant(
    1783       62475 :       cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty)));
    1784             : 
    1785             :   // sext(sext(x)) --> sext(x)
    1786         252 :   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
    1787         252 :     return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1);
    1788             : 
    1789             :   // sext(zext(x)) --> zext(x)
    1790         157 :   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
    1791         157 :     return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
    1792             : 
    1793             :   // Before doing any expensive analysis, check to see if we've already
    1794             :   // computed a SCEV for this Op and Ty.
    1795       38911 :   FoldingSetNodeID ID;
    1796       38911 :   ID.AddInteger(scSignExtend);
    1797       38911 :   ID.AddPointer(Op);
    1798       38911 :   ID.AddPointer(Ty);
    1799       38911 :   void *IP = nullptr;
    1800       77822 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    1801             :   // Limit recursion depth.
    1802       32078 :   if (Depth > MaxExtDepth) {
    1803           8 :     SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
    1804           8 :                                                      Op, Ty);
    1805           8 :     UniqueSCEVs.InsertNode(S, IP);
    1806           4 :     return S;
    1807             :   }
    1808             : 
    1809             :   // sext(trunc(x)) --> sext(x) or x or trunc(x)
    1810         262 :   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
    1811             :     // It's possible the bits taken off by the truncate were all sign bits. If
    1812             :     // so, we should be able to simplify this further.
    1813         262 :     const SCEV *X = ST->getOperand();
    1814         521 :     ConstantRange CR = getSignedRange(X);
    1815         262 :     unsigned TruncBits = getTypeSizeInBits(ST->getType());
    1816         262 :     unsigned NewBits = getTypeSizeInBits(Ty);
    1817         786 :     if (CR.truncate(TruncBits).signExtend(NewBits).contains(
    1818         524 :             CR.sextOrTrunc(NewBits)))
    1819           3 :       return getTruncateOrSignExtend(X, Ty);
    1820             :   }
    1821             : 
    1822             :   // sext(C1 + (C2 * x)) --> C1 + sext(C2 * x) if C1 < C2
    1823        4887 :   if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
    1824        4887 :     if (SA->getNumOperands() == 2) {
    1825       14055 :       auto *SC1 = dyn_cast<SCEVConstant>(SA->getOperand(0));
    1826       14055 :       auto *SMul = dyn_cast<SCEVMulExpr>(SA->getOperand(1));
    1827        4685 :       if (SMul && SC1) {
    1828        3678 :         if (auto *SC2 = dyn_cast<SCEVConstant>(SMul->getOperand(0))) {
    1829        1224 :           const APInt &C1 = SC1->getAPInt();
    1830        1224 :           const APInt &C2 = SC2->getAPInt();
    1831        2219 :           if (C1.isStrictlyPositive() && C2.isStrictlyPositive() &&
    1832        1644 :               C2.ugt(C1) && C2.isPowerOf2())
    1833          16 :             return getAddExpr(getSignExtendExpr(SC1, Ty, Depth + 1),
    1834             :                               getSignExtendExpr(SMul, Ty, Depth + 1),
    1835          16 :                               SCEV::FlagAnyWrap, Depth + 1);
    1836             :         }
    1837             :       }
    1838             :     }
    1839             : 
    1840             :     // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
    1841        9742 :     if (SA->hasNoSignedWrap()) {
    1842             :       // If the addition does not sign overflow then we can, by definition,
    1843             :       // commute the sign extension with the addition operation.
    1844         612 :       SmallVector<const SCEV *, 4> Ops;
    1845        1224 :       for (const auto *Op : SA->operands())
    1846         612 :         Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1));
    1847         306 :       return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1);
    1848             :     }
    1849             :   }
    1850             :   // If the input value is a chrec scev, and we can prove that the value
    1851             :   // did not overflow the old, smaller, value, we can sign extend all of the
    1852             :   // operands (often constants).  This allows analysis of something like
    1853             :   // this:  for (signed char X = 0; X < 100; ++X) { int Y = X; }
    1854       15564 :   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
    1855       15564 :     if (AR->isAffine()) {
    1856       15526 :       const SCEV *Start = AR->getStart();
    1857       15526 :       const SCEV *Step = AR->getStepRecurrence(*this);
    1858       31052 :       unsigned BitWidth = getTypeSizeInBits(AR->getType());
    1859       15526 :       const Loop *L = AR->getLoop();
    1860             : 
    1861       31052 :       if (!AR->hasNoSignedWrap()) {
    1862        8824 :         auto NewFlags = proveNoWrapViaConstantRanges(AR);
    1863        8824 :         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
    1864             :       }
    1865             : 
    1866             :       // If we have special knowledge that this addrec won't overflow,
    1867             :       // we don't need to do any further analysis.
    1868       31052 :       if (AR->hasNoSignedWrap())
    1869        8402 :         return getAddRecExpr(
    1870             :             getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
    1871        8402 :             getSignExtendExpr(Step, Ty, Depth + 1), L, SCEV::FlagNSW);
    1872             : 
    1873             :       // Check whether the backedge-taken count is SCEVCouldNotCompute.
    1874             :       // Note that this serves two purposes: It filters out loops that are
    1875             :       // simply not analyzable, and it covers the case where this code is
    1876             :       // being called from within backedge-taken count analysis, such that
    1877             :       // attempting to ask for the backedge-taken count would likely result
    1878             :       // in infinite recursion. In the later case, the analysis code will
    1879             :       // cope with a conservative value, and it will take care to purge
    1880             :       // that value once it has finished.
    1881        7124 :       const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
    1882       14248 :       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
    1883             :         // Manually compute the final value for AR, checking for
    1884             :         // overflow.
    1885             : 
    1886             :         // Check whether the backedge-taken count can be losslessly casted to
    1887             :         // the addrec's type. The count is always unsigned.
    1888             :         const SCEV *CastedMaxBECount =
    1889        4315 :           getTruncateOrZeroExtend(MaxBECount, Start->getType());
    1890             :         const SCEV *RecastedMaxBECount =
    1891        4315 :           getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
    1892        4315 :         if (MaxBECount == RecastedMaxBECount) {
    1893        8142 :           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
    1894             :           // Check whether Start+Step*MaxBECount has no signed overflow.
    1895        4071 :           const SCEV *SMul = getMulExpr(CastedMaxBECount, Step,
    1896        4071 :                                         SCEV::FlagAnyWrap, Depth + 1);
    1897        4071 :           const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul,
    1898             :                                                           SCEV::FlagAnyWrap,
    1899             :                                                           Depth + 1),
    1900        4071 :                                                WideTy, Depth + 1);
    1901        4071 :           const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1);
    1902             :           const SCEV *WideMaxBECount =
    1903        4071 :             getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
    1904             :           const SCEV *OperandExtendedAdd =
    1905        4071 :             getAddExpr(WideStart,
    1906             :                        getMulExpr(WideMaxBECount,
    1907             :                                   getSignExtendExpr(Step, WideTy, Depth + 1),
    1908             :                                   SCEV::FlagAnyWrap, Depth + 1),
    1909        4071 :                        SCEV::FlagAnyWrap, Depth + 1);
    1910        4071 :           if (SAdd == OperandExtendedAdd) {
    1911             :             // Cache knowledge of AR NSW, which is propagated to this AddRec.
    1912         256 :             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
    1913             :             // Return the expression with the addrec on the outside.
    1914         256 :             return getAddRecExpr(
    1915             :                 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
    1916             :                                                          Depth + 1),
    1917             :                 getSignExtendExpr(Step, Ty, Depth + 1), L,
    1918         128 :                 AR->getNoWrapFlags());
    1919             :           }
    1920             :           // Similar to above, only this time treat the step value as unsigned.
    1921             :           // This covers loops that count up with an unsigned step.
    1922        3943 :           OperandExtendedAdd =
    1923        3943 :             getAddExpr(WideStart,
    1924             :                        getMulExpr(WideMaxBECount,
    1925             :                                   getZeroExtendExpr(Step, WideTy, Depth + 1),
    1926             :                                   SCEV::FlagAnyWrap, Depth + 1),
    1927             :                        SCEV::FlagAnyWrap, Depth + 1);
    1928        3943 :           if (SAdd == OperandExtendedAdd) {
    1929             :             // If AR wraps around then
    1930             :             //
    1931             :             //    abs(Step) * MaxBECount > unsigned-max(AR->getType())
    1932             :             // => SAdd != OperandExtendedAdd
    1933             :             //
    1934             :             // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
    1935             :             // (SAdd == OperandExtendedAdd => AR is NW)
    1936             : 
    1937           2 :             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
    1938             : 
    1939             :             // Return the expression with the addrec on the outside.
    1940           2 :             return getAddRecExpr(
    1941             :                 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
    1942             :                                                          Depth + 1),
    1943             :                 getZeroExtendExpr(Step, Ty, Depth + 1), L,
    1944           1 :                 AR->getNoWrapFlags());
    1945             :           }
    1946             :         }
    1947             :       }
    1948             : 
    1949             :       // Normally, in the cases we can prove no-overflow via a
    1950             :       // backedge guarding condition, we can also compute a backedge
    1951             :       // taken count for the loop.  The exceptions are assumptions and
    1952             :       // guards present in the loop -- SCEV is not great at exploiting
    1953             :       // these to compute max backedge taken counts, but can still use
    1954             :       // these to prove lack of overflow.  Use this fact to avoid
    1955             :       // doing extra work that may not pay off.
    1956             : 
    1957       16790 :       if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
    1958        5600 :           !AC.assumptions().empty()) {
    1959             :         // If the backedge is guarded by a comparison with the pre-inc
    1960             :         // value the addrec is safe. Also, if the entry is guarded by
    1961             :         // a comparison with the start value and the backedge is
    1962             :         // guarded by a comparison with the post-inc value, the addrec
    1963             :         // is safe.
    1964             :         ICmpInst::Predicate Pred;
    1965             :         const SCEV *OverflowLimit =
    1966        4204 :             getSignedOverflowLimitForStep(Step, &Pred, this);
    1967        8341 :         if (OverflowLimit &&
    1968        8167 :             (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
    1969        5580 :              (isLoopEntryGuardedByCond(L, Pred, Start, OverflowLimit) &&
    1970        1550 :               isLoopBackedgeGuardedByCond(L, Pred, AR->getPostIncExpr(*this),
    1971             :                                           OverflowLimit)))) {
    1972             :           // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec.
    1973         316 :           const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
    1974         316 :           return getAddRecExpr(
    1975             :               getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
    1976         158 :               getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
    1977             :         }
    1978             :       }
    1979             : 
    1980             :       // If Start and Step are constants, check if we can apply this
    1981             :       // transformation:
    1982             :       // sext{C1,+,C2} --> C1 + sext{0,+,C2} if C1 < C2
    1983        6837 :       auto *SC1 = dyn_cast<SCEVConstant>(Start);
    1984        6837 :       auto *SC2 = dyn_cast<SCEVConstant>(Step);
    1985        6837 :       if (SC1 && SC2) {
    1986        2454 :         const APInt &C1 = SC1->getAPInt();
    1987        2454 :         const APInt &C2 = SC2->getAPInt();
    1988        4247 :         if (C1.isStrictlyPositive() && C2.isStrictlyPositive() && C2.ugt(C1) &&
    1989         446 :             C2.isPowerOf2()) {
    1990         428 :           Start = getSignExtendExpr(Start, Ty, Depth + 1);
    1991        1712 :           const SCEV *NewAR = getAddRecExpr(getZero(AR->getType()), Step, L,
    1992         428 :                                             AR->getNoWrapFlags());
    1993         428 :           return getAddExpr(Start, getSignExtendExpr(NewAR, Ty, Depth + 1),
    1994         428 :                             SCEV::FlagAnyWrap, Depth + 1);
    1995             :         }
    1996             :       }
    1997             : 
    1998        6409 :       if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
    1999          16 :         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
    2000          16 :         return getAddRecExpr(
    2001             :             getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
    2002           8 :             getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
    2003             :       }
    2004             :     }
    2005             : 
    2006             :   // If the input value is provably positive and we could not simplify
    2007             :   // away the sext build a zext instead.
    2008       22624 :   if (isKnownNonNegative(Op))
    2009        6785 :     return getZeroExtendExpr(Op, Ty, Depth + 1);
    2010             : 
    2011             :   // The cast wasn't folded; create an explicit cast node.
    2012             :   // Recompute the insert position, as it may have been invalidated.
    2013       31678 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    2014       31376 :   SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
    2015       31376 :                                                    Op, Ty);
    2016       31376 :   UniqueSCEVs.InsertNode(S, IP);
    2017       15688 :   return S;
    2018             : }
    2019             : 
    2020             : /// getAnyExtendExpr - Return a SCEV for the given operand extended with
    2021             : /// unspecified bits out to the given type.
    2022        3973 : const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
    2023             :                                               Type *Ty) {
    2024             :   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
    2025             :          "This is not an extending conversion!");
    2026             :   assert(isSCEVable(Ty) &&
    2027             :          "This is not a conversion to a SCEVable type!");
    2028        3973 :   Ty = getEffectiveSCEVType(Ty);
    2029             : 
    2030             :   // Sign-extend negative constants.
    2031        5597 :   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
    2032        3248 :     if (SC->getAPInt().isNegative())
    2033         869 :       return getSignExtendExpr(Op, Ty);
    2034             : 
    2035             :   // Peel off a truncate cast.
    2036        3153 :   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
    2037          49 :     const SCEV *NewOp = T->getOperand();
    2038          49 :     if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
    2039           0 :       return getAnyExtendExpr(NewOp, Ty);
    2040          49 :     return getTruncateOrNoop(NewOp, Ty);
    2041             :   }
    2042             : 
    2043             :   // Next try a zext cast. If the cast is folded, use it.
    2044        3055 :   const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
    2045        6110 :   if (!isa<SCEVZeroExtendExpr>(ZExt))
    2046             :     return ZExt;
    2047             : 
    2048             :   // Next try a sext cast. If the cast is folded, use it.
    2049        1765 :   const SCEV *SExt = getSignExtendExpr(Op, Ty);
    2050        3530 :   if (!isa<SCEVSignExtendExpr>(SExt))
    2051             :     return SExt;
    2052             : 
    2053             :   // Force the cast to be folded into the operands of an addrec.
    2054        2287 :   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
    2055        1304 :     SmallVector<const SCEV *, 4> Ops;
    2056        2608 :     for (const SCEV *Op : AR->operands())
    2057        1304 :       Ops.push_back(getAnyExtendExpr(Op, Ty));
    2058         652 :     return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
    2059             :   }
    2060             : 
    2061             :   // If the expression is obviously signed, use the sext cast value.
    2062        1966 :   if (isa<SCEVSMaxExpr>(Op))
    2063             :     return SExt;
    2064             : 
    2065             :   // Absent any other information, use the zext cast value.
    2066         983 :   return ZExt;
    2067             : }
    2068             : 
    2069             : /// Process the given Ops list, which is a list of operands to be added under
    2070             : /// the given scale, update the given map. This is a helper function for
    2071             : /// getAddRecExpr. As an example of what it does, given a sequence of operands
    2072             : /// that would form an add expression like this:
    2073             : ///
    2074             : ///    m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
    2075             : ///
    2076             : /// where A and B are constants, update the map with these values:
    2077             : ///
    2078             : ///    (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
    2079             : ///
    2080             : /// and add 13 + A*B*29 to AccumulatedConstant.
    2081             : /// This will allow getAddRecExpr to produce this:
    2082             : ///
    2083             : ///    13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
    2084             : ///
    2085             : /// This form often exposes folding opportunities that are hidden in
    2086             : /// the original operand list.
    2087             : ///
    2088             : /// Return true iff it appears that any interesting folding opportunities
    2089             : /// may be exposed. This helps getAddRecExpr short-circuit extra work in
    2090             : /// the common case where no interesting opportunities are present, and
    2091             : /// is also used as a check to avoid infinite recursion.
    2092             : static bool
    2093      197809 : CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
    2094             :                              SmallVectorImpl<const SCEV *> &NewOps,
    2095             :                              APInt &AccumulatedConstant,
    2096             :                              const SCEV *const *Ops, size_t NumOperands,
    2097             :                              const APInt &Scale,
    2098             :                              ScalarEvolution &SE) {
    2099      197809 :   bool Interesting = false;
    2100             : 
    2101             :   // Iterate over the add operands. They are sorted, with constants first.
    2102      197809 :   unsigned i = 0;
    2103      394183 :   while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
    2104       98187 :     ++i;
    2105             :     // Pull a buried constant out to the outside.
    2106      293933 :     if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
    2107             :       Interesting = true;
    2108      196374 :     AccumulatedConstant += Scale * C->getAPInt();
    2109       98187 :   }
    2110             : 
    2111             :   // Next comes everything else. We're especially interested in multiplies
    2112             :   // here, but they're in the middle, so just visit the rest with one loop.
    2113      951033 :   for (; i != NumOperands; ++i) {
    2114      613512 :     const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
    2115      710700 :     if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
    2116             :       APInt NewScale =
    2117      833904 :           Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt();
    2118      589994 :       if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
    2119             :         // A multiplication of a constant with another add; recurse.
    2120        7098 :         const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
    2121        3549 :         Interesting |=
    2122        3549 :           CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
    2123             :                                        Add->op_begin(), Add->getNumOperands(),
    2124             :                                        NewScale, SE);
    2125             :       } else {
    2126             :         // A multiplication of a constant with some other value. Update
    2127             :         // the map.
    2128      819708 :         SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
    2129      204927 :         const SCEV *Key = SE.getMulExpr(MulOps);
    2130      614781 :         auto Pair = M.insert({Key, NewScale});
    2131      204927 :         if (Pair.second) {
    2132      202931 :           NewOps.push_back(Pair.first->first);
    2133             :         } else {
    2134        1996 :           Pair.first->second += NewScale;
    2135             :           // The map already had an entry for this value, which may indicate
    2136             :           // a folding opportunity.
    2137        1996 :           Interesting = true;
    2138             :         }
    2139             :       }
    2140             :     } else {
    2141             :       // An ordinary operand. Update the map.
    2142             :       std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
    2143      672544 :           M.insert({Ops[i], Scale});
    2144      168136 :       if (Pair.second) {
    2145      131976 :         NewOps.push_back(Pair.first->first);
    2146             :       } else {
    2147       36160 :         Pair.first->second += Scale;
    2148             :         // The map already had an entry for this value, which may indicate
    2149             :         // a folding opportunity.
    2150       36160 :         Interesting = true;
    2151             :       }
    2152             :     }
    2153             :   }
    2154             : 
    2155      197809 :   return Interesting;
    2156             : }
    2157             : 
    2158             : // We're trying to construct a SCEV of type `Type' with `Ops' as operands and
    2159             : // `OldFlags' as can't-wrap behavior.  Infer a more aggressive set of
    2160             : // can't-overflow flags for the operation if possible.
    2161             : static SCEV::NoWrapFlags
    2162     2885839 : StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
    2163             :                       const SmallVectorImpl<const SCEV *> &Ops,
    2164             :                       SCEV::NoWrapFlags Flags) {
    2165             :   using namespace std::placeholders;
    2166             : 
    2167             :   using OBO = OverflowingBinaryOperator;
    2168             : 
    2169     2885839 :   bool CanAnalyze =
    2170     2885839 :       Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr;
    2171             :   (void)CanAnalyze;
    2172             :   assert(CanAnalyze && "don't call from other places!");
    2173             : 
    2174     2885839 :   int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
    2175             :   SCEV::NoWrapFlags SignOrUnsignWrap =
    2176     2885839 :       ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
    2177             : 
    2178             :   // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
    2179             :   auto IsKnownNonNegative = [&](const SCEV *S) {
    2180      595343 :     return SE->isKnownNonNegative(S);
    2181     3481182 :   };
    2182             : 
    2183     3273287 :   if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative))
    2184       73534 :     Flags =
    2185       73534 :         ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
    2186             : 
    2187     2885839 :   SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
    2188             : 
    2189     4381318 :   if (SignOrUnsignWrap != SignOrUnsignMask && Type == scAddExpr &&
    2190     8548093 :       Ops.size() == 2 && isa<SCEVConstant>(Ops[0])) {
    2191             : 
    2192             :     // (A + C) --> (A + C)<nsw> if the addition does not sign overflow
    2193             :     // (A + C) --> (A + C)<nuw> if the addition does not unsign overflow
    2194             : 
    2195     4833596 :     const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt();
    2196     1208399 :     if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
    2197             :       auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
    2198     3084279 :           Instruction::Add, C, OBO::NoSignedWrap);
    2199     3084279 :       if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
    2200      867050 :         Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
    2201             :     }
    2202     1208399 :     if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
    2203             :       auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
    2204     3592539 :           Instruction::Add, C, OBO::NoUnsignedWrap);
    2205     3592539 :       if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
    2206      735844 :         Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
    2207             :     }
    2208             :   }
    2209             : 
    2210     2885839 :   return Flags;
    2211             : }
    2212             : 
    2213      751147 : bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
    2214      751147 :   if (!isLoopInvariant(S, L))
    2215             :     return false;
    2216             :   // If a value depends on a SCEVUnknown which is defined after the loop, we
    2217             :   // conservatively assume that we cannot calculate it at the loop's entry.
    2218             :   struct FindDominatedSCEVUnknown {
    2219             :     bool Found = false;
    2220             :     const Loop *L;
    2221             :     DominatorTree &DT;
    2222             :     LoopInfo &LI;
    2223             : 
    2224             :     FindDominatedSCEVUnknown(const Loop *L, DominatorTree &DT, LoopInfo &LI)
    2225      327841 :         : L(L), DT(DT), LI(LI) {}
    2226             : 
    2227       51222 :     bool checkSCEVUnknown(const SCEVUnknown *SU) {
    2228       64660 :       if (auto *I = dyn_cast<Instruction>(SU->getValue())) {
    2229       26876 :         if (DT.dominates(L->getHeader(), I->getParent()))
    2230         139 :           Found = true;
    2231             :         else
    2232             :           assert(DT.dominates(I->getParent(), L->getHeader()) &&
    2233             :                  "No dominance relationship between SCEV and loop?");
    2234             :       }
    2235       51222 :       return false;
    2236             :     }
    2237             : 
    2238      416497 :     bool follow(const SCEV *S) {
    2239      416497 :       switch (static_cast<SCEVTypes>(S->getSCEVType())) {
    2240             :       case scConstant:
    2241             :         return false;
    2242       52778 :       case scAddRecExpr:
    2243             :       case scTruncate:
    2244             :       case scZeroExtend:
    2245             :       case scSignExtend:
    2246             :       case scAddExpr:
    2247             :       case scMulExpr:
    2248             :       case scUMaxExpr:
    2249             :       case scSMaxExpr:
    2250             :       case scUDivExpr:
    2251       52778 :         return true;
    2252       51222 :       case scUnknown:
    2253       51222 :         return checkSCEVUnknown(cast<SCEVUnknown>(S));
    2254           0 :       case scCouldNotCompute:
    2255           0 :         llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
    2256             :       }
    2257             :       return false;
    2258             :     }
    2259             : 
    2260             :     bool isDone() { return Found; }
    2261             :   };
    2262             : 
    2263      655682 :   FindDominatedSCEVUnknown FSU(L, DT, LI);
    2264      655682 :   SCEVTraversal<FindDominatedSCEVUnknown> ST(FSU);
    2265      327841 :   ST.visitAll(S);
    2266      327841 :   return !FSU.Found;
    2267             : }
    2268             : 
    2269             : /// Get a canonical add expression, or something simpler if possible.
    2270     1610449 : const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
    2271             :                                         SCEV::NoWrapFlags Flags,
    2272             :                                         unsigned Depth) {
    2273             :   assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
    2274             :          "only nuw or nsw allowed");
    2275             :   assert(!Ops.empty() && "Cannot get empty add!");
    2276     3294629 :   if (Ops.size() == 1) return Ops[0];
    2277             : #ifndef NDEBUG
    2278             :   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
    2279             :   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
    2280             :     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
    2281             :            "SCEVAddExpr operand types don't match!");
    2282             : #endif
    2283             : 
    2284             :   // Sort by complexity, this groups all similar expression types together.
    2285     1536718 :   GroupByComplexity(Ops, &LI, DT);
    2286             : 
    2287     1536718 :   Flags = StrengthenNoWrapFlags(this, scAddExpr, Ops, Flags);
    2288             : 
    2289             :   // If there are any constants, fold them together.
    2290     1536718 :   unsigned Idx = 0;
    2291     3073436 :   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
    2292             :     ++Idx;
    2293             :     assert(Idx < Ops.size());
    2294     3480157 :     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
    2295             :       // We found two constants, fold them together!
    2296     4952409 :       Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt());
    2297     2076881 :       if (Ops.size() == 2) return Ops[0];
    2298       45580 :       Ops.erase(Ops.begin()+1);  // Erase the folded element
    2299      136740 :       LHSC = cast<SCEVConstant>(Ops[0]);
    2300       45580 :     }
    2301             : 
    2302             :     // If we are left with a constant zero being added, strip it off.
    2303     1357696 :     if (LHSC->getValue()->isZero()) {
    2304      168225 :       Ops.erase(Ops.begin());
    2305      168225 :       --Idx;
    2306             :     }
    2307             : 
    2308     1522720 :     if (Ops.size() == 1) return Ops[0];
    2309             :   }
    2310             : 
    2311             :   // Limit recursion calls depth.
    2312      709787 :   if (Depth > MaxArithDepth)
    2313       17066 :     return getOrCreateAddExpr(Ops, Flags);
    2314             : 
    2315             :   // Okay, check to see if the same value occurs in the operand list more than
    2316             :   // once.  If so, merge them together into an multiply expression.  Since we
    2317             :   // sorted the list, these values are required to be adjacent.
    2318     1385442 :   Type *Ty = Ops[0]->getType();
    2319      692721 :   bool FoundMatch = false;
    2320     2163651 :   for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
    2321     2338329 :     if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2
    2322             :       // Scan ahead to count how many equal operands there are.
    2323             :       unsigned Count = 2;
    2324        2241 :       while (i+Count != e && Ops[i+Count] == Ops[i])
    2325          36 :         ++Count;
    2326             :       // Merge the values into a multiply.
    2327        1651 :       const SCEV *Scale = getConstant(Ty, Count);
    2328        3302 :       const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1);
    2329        3302 :       if (Ops.size() == Count)
    2330             :         return Mul;
    2331         834 :       Ops[i] = Mul;
    2332        1251 :       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
    2333         417 :       --i; e -= Count - 1;
    2334         417 :       FoundMatch = true;
    2335             :     }
    2336      691487 :   if (FoundMatch)
    2337         399 :     return getAddExpr(Ops, Flags);
    2338             : 
    2339             :   // Check for truncates. If all the operands are truncated from the same
    2340             :   // type, see if factoring out the truncate would permit the result to be
    2341             :   // folded. eg., trunc(x) + m*trunc(n) --> trunc(x + trunc(m)*n)
    2342             :   // if the contents of the resulting outer trunc fold to something simple.
    2343     2767311 :   for (; Idx < Ops.size() && isa<SCEVTruncateExpr>(Ops[Idx]); ++Idx) {
    2344        2190 :     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Ops[Idx]);
    2345         730 :     Type *DstType = Trunc->getType();
    2346         730 :     Type *SrcType = Trunc->getOperand()->getType();
    2347        1427 :     SmallVector<const SCEV *, 8> LargeOps;
    2348         730 :     bool Ok = true;
    2349             :     // Check all the operands to see if they can be represented in the
    2350             :     // source type of the truncate.
    2351        2712 :     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
    2352        3704 :       if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
    2353         784 :         if (T->getOperand()->getType() != SrcType) {
    2354             :           Ok = false;
    2355             :           break;
    2356             :         }
    2357         676 :         LargeOps.push_back(T->getOperand());
    2358        1694 :       } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
    2359         342 :         LargeOps.push_back(getAnyExtendExpr(C, SrcType));
    2360         902 :       } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
    2361         468 :         SmallVector<const SCEV *, 8> LargeMulOps;
    2362         530 :         for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
    2363             :           if (const SCEVTruncateExpr *T =
    2364        1005 :                 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
    2365          69 :             if (T->getOperand()->getType() != SrcType) {
    2366             :               Ok = false;
    2367             :               break;
    2368             :             }
    2369          65 :             LargeMulOps.push_back(T->getOperand());
    2370         630 :           } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) {
    2371         231 :             LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
    2372             :           } else {
    2373             :             Ok = false;
    2374             :             break;
    2375             :           }
    2376             :         }
    2377         234 :         if (Ok)
    2378          62 :           LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1));
    2379             :       } else {
    2380             :         Ok = false;
    2381             :         break;
    2382             :       }
    2383             :     }
    2384         730 :     if (Ok) {
    2385             :       // Evaluate the expression in the larger type.
    2386         353 :       const SCEV *Fold = getAddExpr(LargeOps, Flags, Depth + 1);
    2387             :       // If it folds to something simple, use it. Otherwise, don't.
    2388        1051 :       if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
    2389          33 :         return getTruncateExpr(Fold, DstType);
    2390             :     }
    2391             :   }
    2392             : 
    2393             :   // Skip past any other cast SCEVs.
    2394     2227845 :   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
    2395       49438 :     ++Idx;
    2396             : 
    2397             :   // If there are add operands they would be next.
    2398     1382110 :   if (Idx < Ops.size()) {
    2399             :     bool DeletedAdd = false;
    2400     1586301 :     while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
    2401      387115 :       if (Ops.size() > AddOpsInlineThreshold ||
    2402      193556 :           Add->getNumOperands() > AddOpsInlineThreshold)
    2403             :         break;
    2404             :       // If we have an add, expand the add operands onto the end of the operands
    2405             :       // list.
    2406       96778 :       Ops.erase(Ops.begin()+Idx);
    2407      193556 :       Ops.append(Add->op_begin(), Add->op_end());
    2408       96778 :       DeletedAdd = true;
    2409       96778 :     }
    2410             : 
    2411             :     // If we deleted at least one add, we added operands to the end of the list,
    2412             :     // and they are not necessarily sorted.  Recurse to resort and resimplify
    2413             :     // any operands we just acquired.
    2414      647983 :     if (DeletedAdd)
    2415       92958 :       return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2416             :   }
    2417             : 
    2418             :   // Skip over the add expression until we get to a multiply.
    2419     1751223 :   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
    2420           1 :     ++Idx;
    2421             : 
    2422             :   // Check to see if there are any folding opportunities present with
    2423             :   // operands multiplied by constant values.
    2424     2861269 :   if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
    2425      194260 :     uint64_t BitWidth = getTypeSizeInBits(Ty);
    2426      351087 :     DenseMap<const SCEV *, APInt> M;
    2427      351087 :     SmallVector<const SCEV *, 8> NewOps;
    2428      545347 :     APInt AccumulatedConstant(BitWidth, 0);
    2429      777040 :     if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
    2430      194260 :                                      Ops.data(), Ops.size(),
    2431      582780 :                                      APInt(BitWidth, 1), *this)) {
    2432             :       struct APIntCompare {
    2433             :         bool operator()(const APInt &LHS, const APInt &RHS) const {
    2434       24397 :           return LHS.ult(RHS);
    2435             :         }
    2436             :       };
    2437             : 
    2438             :       // Some interesting folding opportunity is present, so its worthwhile to
    2439             :       // re-generate the operands list. Group the operands by constant scale,
    2440             :       // to avoid multiplying by the same constant scale multiple times.
    2441       74866 :       std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
    2442      158032 :       for (const SCEV *NewOp : NewOps)
    2443       45733 :         MulOpLists[M.find(NewOp)->second].push_back(NewOp);
    2444             :       // Re-generate the operands list.
    2445       37433 :       Ops.clear();
    2446       37433 :       if (AccumulatedConstant != 0)
    2447       20073 :         Ops.push_back(getConstant(AccumulatedConstant));
    2448      156565 :       for (auto &MulOp : MulOpLists)
    2449       88532 :         if (MulOp.first != 0)
    2450        8158 :           Ops.push_back(getMulExpr(
    2451             :               getConstant(MulOp.first),
    2452             :               getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1),
    2453             :               SCEV::FlagAnyWrap, Depth + 1));
    2454       37433 :       if (Ops.empty())
    2455       11764 :         return getZero(Ty);
    2456       51338 :       if (Ops.size() == 1)
    2457       47184 :         return Ops[0];
    2458        2077 :       return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2459             :     }
    2460             :   }
    2461             : 
    2462             :   // If we are adding something to a multiply expression, make sure the
    2463             :   // something is not already an operand of the multiply.  If so, merge it into
    2464             :   // the multiply.
    2465     2927776 :   for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
    2466      547629 :     const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
    2467      566202 :     for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
    2468      775816 :       const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
    2469      775816 :       if (isa<SCEVConstant>(MulOpSCEV))
    2470      159972 :         continue;
    2471     1566642 :       for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
    2472     2221724 :         if (MulOpSCEV == Ops[AddOp]) {
    2473             :           // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1))
    2474         184 :           const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
    2475          92 :           if (Mul->getNumOperands() != 2) {
    2476             :             // If the multiply has more than two operands, we must get the
    2477             :             // Y*Z term.
    2478             :             SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
    2479          78 :                                                 Mul->op_begin()+MulOp);
    2480          52 :             MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
    2481          26 :             InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
    2482             :           }
    2483         276 :           SmallVector<const SCEV *, 2> TwoOps = {getOne(Ty), InnerMul};
    2484          92 :           const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
    2485          92 :           const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV,
    2486          92 :                                             SCEV::FlagAnyWrap, Depth + 1);
    2487         184 :           if (Ops.size() == 2) return OuterMul;
    2488          23 :           if (AddOp < Idx) {
    2489           1 :             Ops.erase(Ops.begin()+AddOp);
    2490           2 :             Ops.erase(Ops.begin()+Idx-1);
    2491             :           } else {
    2492          22 :             Ops.erase(Ops.begin()+Idx);
    2493          44 :             Ops.erase(Ops.begin()+AddOp-1);
    2494             :           }
    2495          23 :           Ops.push_back(OuterMul);
    2496          23 :           return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2497             :         }
    2498             : 
    2499             :       // Check this multiply against other multiplies being added together.
    2500      560438 :       for (unsigned OtherMulIdx = Idx+1;
    2501     2013432 :            OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
    2502             :            ++OtherMulIdx) {
    2503     1010253 :         const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
    2504             :         // If MulOp occurs in OtherMul, we can fold the two multiplies
    2505             :         // together.
    2506     1065355 :         for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
    2507     1065355 :              OMulOp != e; ++OMulOp)
    2508     1465522 :           if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
    2509             :             // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
    2510        8314 :             const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
    2511        4157 :             if (Mul->getNumOperands() != 2) {
    2512             :               SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
    2513        7548 :                                                   Mul->op_begin()+MulOp);
    2514        5032 :               MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
    2515        2516 :               InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
    2516             :             }
    2517        8314 :             const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
    2518        4157 :             if (OtherMul->getNumOperands() != 2) {
    2519             :               SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
    2520       10668 :                                                   OtherMul->op_begin()+OMulOp);
    2521        7112 :               MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end());
    2522        3556 :               InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
    2523             :             }
    2524       12471 :             SmallVector<const SCEV *, 2> TwoOps = {InnerMul1, InnerMul2};
    2525             :             const SCEV *InnerMulSum =
    2526        4157 :                 getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
    2527        4157 :             const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum,
    2528        4157 :                                               SCEV::FlagAnyWrap, Depth + 1);
    2529        8314 :             if (Ops.size() == 2) return OuterMul;
    2530        1334 :             Ops.erase(Ops.begin()+Idx);
    2531        2668 :             Ops.erase(Ops.begin()+OtherMulIdx-1);
    2532        1334 :             Ops.push_back(OuterMul);
    2533        1334 :             return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2534             :           }
    2535             :       }
    2536             :     }
    2537             :   }
    2538             : 
    2539             :   // If there are any add recurrences in the operands list, see if any other
    2540             :   // added values are loop invariant.  If so, we can fold them into the
    2541             :   // recurrence.
    2542     1581950 :   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
    2543        5023 :     ++Idx;
    2544             : 
    2545             :   // Scan over all recurrences, trying to fold loop invariants into them.
    2546     2031758 :   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
    2547             :     // Scan all of the other operands to this add and add them to the vector if
    2548             :     // they are loop invariant w.r.t. the recurrence.
    2549      298191 :     SmallVector<const SCEV *, 8> LIOps;
    2550      878619 :     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
    2551      292873 :     const Loop *AddRecLoop = AddRec->getLoop();
    2552     1177182 :     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
    2553     1182872 :       if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
    2554      570798 :         LIOps.push_back(Ops[i]);
    2555      570798 :         Ops.erase(Ops.begin()+i);
    2556      285399 :         --i; --e;
    2557             :       }
    2558             : 
    2559             :     // If we found some loop invariants, fold them into the recurrence.
    2560      292873 :     if (!LIOps.empty()) {
    2561             :       //  NLI + LI + {Start,+,Step}  -->  NLI + {LI+Start,+,Step}
    2562      281627 :       LIOps.push_back(AddRec->getStart());
    2563             : 
    2564             :       SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
    2565     1126508 :                                              AddRec->op_end());
    2566             :       // This follows from the fact that the no-wrap flags on the outer add
    2567             :       // expression are applicable on the 0th iteration, when the add recurrence
    2568             :       // will be equal to its start value.
    2569      563254 :       AddRecOps[0] = getAddExpr(LIOps, Flags, Depth + 1);
    2570             : 
    2571             :       // Build the new addrec. Propagate the NUW and NSW flags if both the
    2572             :       // outer add and the inner addrec are guaranteed to have no overflow.
    2573             :       // Always propagate NW.
    2574      844881 :       Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
    2575      281627 :       const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
    2576             : 
    2577             :       // If all of the other operands were loop invariant, we are done.
    2578      563254 :       if (Ops.size() == 1) return NewRec;
    2579             : 
    2580             :       // Otherwise, add the folded AddRec by the non-invariant parts.
    2581         710 :       for (unsigned i = 0;; ++i)
    2582        4112 :         if (Ops[i] == AddRec) {
    2583        1982 :           Ops[i] = NewRec;
    2584             :           break;
    2585             :         }
    2586         991 :       return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2587             :     }
    2588             : 
    2589             :     // Okay, if there weren't any loop invariants to be folded, check to see if
    2590             :     // there are multiple AddRec's with the same loop induction variable being
    2591             :     // added together.  If so, we can fold them.
    2592       11246 :     for (unsigned OtherIdx = Idx+1;
    2593       39266 :          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
    2594             :          ++OtherIdx) {
    2595             :       // We expect the AddRecExpr's to be sorted in reverse dominance order,
    2596             :       // so that the 1st found AddRecExpr is dominated by all others.
    2597             :       assert(DT.dominates(
    2598             :            cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),
    2599             :            AddRec->getLoop()->getHeader()) &&
    2600             :         "AddRecExprs are not sorted in reverse dominance order?");
    2601       17784 :       if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
    2602             :         // Other + {A,+,B}<L> + {C,+,D}<L>  -->  Other + {A+C,+,B+D}<L>
    2603             :         SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
    2604       17784 :                                                AddRec->op_end());
    2605       41539 :         for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
    2606             :              ++OtherIdx) {
    2607       17799 :           const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
    2608        5933 :           if (OtherAddRec->getLoop() == AddRecLoop) {
    2609       19437 :             for (unsigned i = 0, e = OtherAddRec->getNumOperands();
    2610       19437 :                  i != e; ++i) {
    2611       28820 :               if (i >= AddRecOps.size()) {
    2612        1812 :                 AddRecOps.append(OtherAddRec->op_begin()+i,
    2613             :                                  OtherAddRec->op_end());
    2614         906 :                 break;
    2615             :               }
    2616             :               SmallVector<const SCEV *, 2> TwoOps = {
    2617       67520 :                   AddRecOps[i], OtherAddRec->getOperand(i)};
    2618       27008 :               AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
    2619             :             }
    2620       11866 :             Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
    2621             :           }
    2622             :         }
    2623             :         // Step size has changed, so we cannot guarantee no self-wraparound.
    2624       11856 :         Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
    2625        5928 :         return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2626             :       }
    2627             :     }
    2628             : 
    2629             :     // Otherwise couldn't fold anything into this recurrence.  Move onto the
    2630             :     // next one.
    2631             :   }
    2632             : 
    2633             :   // Okay, it looks like we really DO need an add expr.  Check to see if we
    2634             :   // already have one, otherwise create a new one.
    2635      268860 :   return getOrCreateAddExpr(Ops, Flags);
    2636             : }
    2637             : 
    2638             : const SCEV *
    2639      285926 : ScalarEvolution::getOrCreateAddExpr(SmallVectorImpl<const SCEV *> &Ops,
    2640             :                                     SCEV::NoWrapFlags Flags) {
    2641      571852 :   FoldingSetNodeID ID;
    2642      285926 :   ID.AddInteger(scAddExpr);
    2643     1194577 :   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
    2644     1245450 :     ID.AddPointer(Ops[i]);
    2645      285926 :   void *IP = nullptr;
    2646             :   SCEVAddExpr *S =
    2647      571852 :       static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
    2648      285926 :   if (!S) {
    2649      461928 :     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
    2650      615904 :     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
    2651      153976 :     S = new (SCEVAllocator)
    2652      769880 :         SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
    2653      153976 :     UniqueSCEVs.InsertNode(S, IP);
    2654             :   }
    2655      571852 :   S->setNoWrapFlags(Flags);
    2656      571852 :   return S;
    2657             : }
    2658             : 
    2659             : const SCEV *
    2660      257965 : ScalarEvolution::getOrCreateMulExpr(SmallVectorImpl<const SCEV *> &Ops,
    2661             :                                     SCEV::NoWrapFlags Flags) {
    2662      515930 :   FoldingSetNodeID ID;
    2663      257965 :   ID.AddInteger(scMulExpr);
    2664     1125424 :   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
    2665     1218988 :     ID.AddPointer(Ops[i]);
    2666      257965 :   void *IP = nullptr;
    2667             :   SCEVMulExpr *S =
    2668      515930 :     static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
    2669      257965 :   if (!S) {
    2670      177846 :     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
    2671      237128 :     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
    2672      118564 :     S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
    2673      237128 :                                         O, Ops.size());
    2674       59282 :     UniqueSCEVs.InsertNode(S, IP);
    2675             :   }
    2676      515930 :   S->setNoWrapFlags(Flags);
    2677      515930 :   return S;
    2678             : }
    2679             : 
    2680             : static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
    2681       25799 :   uint64_t k = i*j;
    2682       25799 :   if (j > 1 && k / j != i) Overflow = true;
    2683             :   return k;
    2684             : }
    2685             : 
    2686             : /// Compute the result of "n choose k", the binomial coefficient.  If an
    2687             : /// intermediate computation overflows, Overflow will be set and the return will
    2688             : /// be garbage. Overflow is not cleared on absence of overflow.
    2689       82932 : static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
    2690             :   // We use the multiplicative formula:
    2691             :   //     n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
    2692             :   // At each iteration, we take the n-th term of the numeral and divide by the
    2693             :   // (k-n)th term of the denominator.  This division will always produce an
    2694             :   // integral result, and helps reduce the chance of overflow in the
    2695             :   // intermediate computations. However, we can still overflow even when the
    2696             :   // final result would fit.
    2697             : 
    2698       82932 :   if (n == 0 || n == k) return 1;
    2699       48629 :   if (k > n) return 0;
    2700             : 
    2701       48629 :   if (k > n/2)
    2702        4374 :     k = n-k;
    2703             : 
    2704       48629 :   uint64_t r = 1;
    2705       74423 :   for (uint64_t i = 1; i <= k; ++i) {
    2706       51588 :     r = umul_ov(r, n-(i-1), Overflow);
    2707       25794 :     r /= i;
    2708             :   }
    2709             :   return r;
    2710             : }
    2711             : 
    2712             : /// Determine if any of the operands in this SCEV are a constant or if
    2713             : /// any of the add or multiply expressions in this SCEV contain a constant.
    2714       30060 : static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
    2715             :   struct FindConstantInAddMulChain {
    2716             :     bool FoundConstant = false;
    2717             : 
    2718             :     bool follow(const SCEV *S) {
    2719      232626 :       FoundConstant |= isa<SCEVConstant>(S);
    2720      313005 :       return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S);
    2721             :     }
    2722             : 
    2723             :     bool isDone() const {
    2724       52560 :       return FoundConstant;
    2725             :     }
    2726             :   };
    2727             : 
    2728       30060 :   FindConstantInAddMulChain F;
    2729       60120 :   SCEVTraversal<FindConstantInAddMulChain> ST(F);
    2730       30060 :   ST.visitAll(StartExpr);
    2731       60120 :   return F.FoundConstant;
    2732             : }
    2733             : 
    2734             : /// Get a canonical multiply expression, or something simpler if possible.
    2735     1002074 : const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
    2736             :                                         SCEV::NoWrapFlags Flags,
    2737             :                                         unsigned Depth) {
    2738             :   assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) &&
    2739             :          "only nuw or nsw allowed");
    2740             :   assert(!Ops.empty() && "Cannot get empty mul!");
    2741     2233104 :   if (Ops.size() == 1) return Ops[0];
    2742             : #ifndef NDEBUG
    2743             :   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
    2744             :   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
    2745             :     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
    2746             :            "SCEVMulExpr operand types don't match!");
    2747             : #endif
    2748             : 
    2749             :   // Sort by complexity, this groups all similar expression types together.
    2750      773118 :   GroupByComplexity(Ops, &LI, DT);
    2751             : 
    2752      773118 :   Flags = StrengthenNoWrapFlags(this, scMulExpr, Ops, Flags);
    2753             : 
    2754             :   // Limit recursion calls depth.
    2755      773118 :   if (Depth > MaxArithDepth)
    2756       68262 :     return getOrCreateMulExpr(Ops, Flags);
    2757             : 
    2758             :   // If there are any constants, fold them together.
    2759      704856 :   unsigned Idx = 0;
    2760     2069380 :   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
    2761             : 
    2762             :     // C1*(C2+V) -> C1*C2 + C1*V
    2763     1319336 :     if (Ops.size() == 2)
    2764     1218963 :         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
    2765             :           // If any of Add's ops are Adds or Muls with a constant,
    2766             :           // apply this transformation as well.
    2767       35465 :           if (Add->getNumOperands() == 2)
    2768             :             // TODO: There are some cases where this transformation is not
    2769             :             // profitable, for example:
    2770             :             // Add = (C0 + X) * Y + Z.
    2771             :             // Maybe the scope of this transformation should be narrowed down.
    2772       30060 :             if (containsConstantInAddMulChain(Add))
    2773       83976 :               return getAddExpr(getMulExpr(LHSC, Add->getOperand(0),
    2774             :                                            SCEV::FlagAnyWrap, Depth + 1),
    2775             :                                 getMulExpr(LHSC, Add->getOperand(1),
    2776             :                                            SCEV::FlagAnyWrap, Depth + 1),
    2777       27992 :                                 SCEV::FlagAnyWrap, Depth + 1);
    2778             : 
    2779             :     ++Idx;
    2780     1688403 :     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
    2781             :       // We found two constants, fold them together!
    2782             :       ConstantInt *Fold =
    2783     1368868 :           ConstantInt::get(getContext(), LHSC->getAPInt() * RHSC->getAPInt());
    2784      684434 :       Ops[0] = getConstant(Fold);
    2785      684434 :       Ops.erase(Ops.begin()+1);  // Erase the folded element
    2786      985234 :       if (Ops.size() == 1) return Ops[0];
    2787      124251 :       LHSC = cast<SCEVConstant>(Ops[0]);
    2788       41417 :     }
    2789             : 
    2790             :     // If we are left with a constant one being multiplied, strip it off.
    2791     1323504 :     if (cast<SCEVConstant>(Ops[0])->getValue()->isOne()) {
    2792       40824 :       Ops.erase(Ops.begin());
    2793       40824 :       --Idx;
    2794     1160208 :     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
    2795             :       // If we have a multiply of zero, it will always be zero.
    2796             :       return Ops[0];
    2797      558538 :     } else if (Ops[0]->isAllOnesValue()) {
    2798             :       // If we have a mul by -1 of an add, try distributing the -1 among the
    2799             :       // add operands.
    2800      303142 :       if (Ops.size() == 2) {
    2801      305647 :         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
    2802        4549 :           SmallVector<const SCEV *, 4> NewOps;
    2803        3885 :           bool AnyFolded = false;
    2804       19191 :           for (const SCEV *AddOp : Add->operands()) {
    2805       22842 :             const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap,
    2806       11421 :                                          Depth + 1);
    2807       22842 :             if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
    2808       11421 :             NewOps.push_back(Mul);
    2809             :           }
    2810        3885 :           if (AnyFolded)
    2811        6442 :             return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1);
    2812      341324 :         } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
    2813             :           // Negation preserves a recurrence's no self-wrap property.
    2814       94664 :           SmallVector<const SCEV *, 4> Operands;
    2815      190952 :           for (const SCEV *AddRecOp : AddRec->operands())
    2816      192576 :             Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap,
    2817             :                                           Depth + 1));
    2818             : 
    2819       94664 :           return getAddRecExpr(Operands, AddRec->getLoop(),
    2820       47332 :                                AddRec->getNoWrapFlags(SCEV::FlagNW));
    2821             :         }
    2822             :       }
    2823             :     }
    2824             : 
    2825      539080 :     if (Ops.size() == 1)
    2826       56108 :       return Ops[0];
    2827             :   }
    2828             : 
    2829             :   // Skip over the add expression until we get to a multiply.
    2830     1293382 :   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
    2831      120544 :     ++Idx;
    2832             : 
    2833             :   // If there are mul operands inline them all into this expression.
    2834      573348 :   if (Idx < Ops.size()) {
    2835             :     bool DeletedMul = false;
    2836      641083 :     while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
    2837      166095 :       if (Ops.size() > MulOpsInlineThreshold)
    2838             :         break;
    2839             :       // If we have an mul, expand the mul operands onto the end of the
    2840             :       // operands list.
    2841       55001 :       Ops.erase(Ops.begin()+Idx);
    2842      110002 :       Ops.append(Mul->op_begin(), Mul->op_end());
    2843       55001 :       DeletedMul = true;
    2844       55001 :     }
    2845             : 
    2846             :     // If we deleted at least one mul, we added operands to the end of the
    2847             :     // list, and they are not necessarily sorted.  Recurse to resort and
    2848             :     // resimplify any operands we just acquired.
    2849      237858 :     if (DeletedMul)
    2850       51120 :       return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2851             :   }
    2852             : 
    2853             :   // If there are any add recurrences in the operands list, see if any other
    2854             :   // added values are loop invariant.  If so, we can fold them into the
    2855             :   // recurrence.
    2856      720583 :   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
    2857       19240 :     ++Idx;
    2858             : 
    2859             :   // Scan over all recurrences, trying to fold loop invariants into them.
    2860      895652 :   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
    2861             :     // Scan all of the other operands to this mul and add them to the vector
    2862             :     // if they are loop invariant w.r.t. the recurrence.
    2863       84299 :     SmallVector<const SCEV *, 8> LIOps;
    2864      195225 :     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
    2865       65075 :     const Loop *AddRecLoop = AddRec->getLoop();
    2866      289861 :     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
    2867      319422 :       if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
    2868       84606 :         LIOps.push_back(Ops[i]);
    2869       84606 :         Ops.erase(Ops.begin()+i);
    2870       42303 :         --i; --e;
    2871             :       }
    2872             : 
    2873             :     // If we found some loop invariants, fold them into the recurrence.
    2874       65075 :     if (!LIOps.empty()) {
    2875             :       //  NLI * LI * {Start,+,Step}  -->  NLI * {LI*Start,+,LI*Step}
    2876       81996 :       SmallVector<const SCEV *, 4> NewOps;
    2877       40998 :       NewOps.reserve(AddRec->getNumOperands());
    2878       40998 :       const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1);
    2879      139557 :       for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
    2880      197118 :         NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i),
    2881             :                                     SCEV::FlagAnyWrap, Depth + 1));
    2882             : 
    2883             :       // Build the new addrec. Propagate the NUW and NSW flags if both the
    2884             :       // outer mul and the inner addrec are guaranteed to have no overflow.
    2885             :       //
    2886             :       // No self-wrap cannot be guaranteed after changing the step size, but
    2887             :       // will be inferred if either NUW or NSW is true.
    2888      122994 :       Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW));
    2889       40998 :       const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags);
    2890             : 
    2891             :       // If all of the other operands were loop invariant, we are done.
    2892       81996 :       if (Ops.size() == 1) return NewRec;
    2893             : 
    2894             :       // Otherwise, multiply the folded AddRec by the non-invariant parts.
    2895         292 :       for (unsigned i = 0;; ++i)
    2896       14356 :         if (Ops[i] == AddRec) {
    2897       13480 :           Ops[i] = NewRec;
    2898             :           break;
    2899             :         }
    2900        6740 :       return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2901             :     }
    2902             : 
    2903             :     // Okay, if there weren't any loop invariants to be folded, check to see
    2904             :     // if there are multiple AddRec's with the same loop induction variable
    2905             :     // being multiplied together.  If so, we can fold them.
    2906             : 
    2907             :     // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
    2908             :     // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
    2909             :     //       choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
    2910             :     //   ]]],+,...up to x=2n}.
    2911             :     // Note that the arguments to choose() are always integers with values
    2912             :     // known at compile time, never SCEV objects.
    2913             :     //
    2914             :     // The implementation avoids pointless extra computations when the two
    2915             :     // addrec's are of different length (mathematically, it's equivalent to
    2916             :     // an infinite stream of zeros on the right).
    2917       24077 :     bool OpsModified = false;
    2918       42095 :     for (unsigned OtherIdx = Idx+1;
    2919      128462 :          OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
    2920             :          ++OtherIdx) {
    2921             :       const SCEVAddRecExpr *OtherAddRec =
    2922       65598 :         dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
    2923       21866 :       if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop)
    2924       16777 :         continue;
    2925             : 
    2926             :       // Limit max number of arguments to avoid creation of unreasonably big
    2927             :       // SCEVAddRecs with very complex operands.
    2928       60509 :       if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
    2929       21866 :           MaxAddRecSize)
    2930       16777 :         continue;
    2931             : 
    2932        5089 :       bool Overflow = false;
    2933       10178 :       Type *Ty = AddRec->getType();
    2934        5089 :       bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
    2935        6330 :       SmallVector<const SCEV*, 7> AddRecOps;
    2936       29037 :       for (int x = 0, xe = AddRec->getNumOperands() +
    2937       10178 :              OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
    2938       18859 :         const SCEV *Term = getZero(Ty);
    2939       64887 :         for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
    2940       46028 :           uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
    2941      128960 :           for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
    2942       92056 :                  ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
    2943       82932 :                z < ze && !Overflow; ++z) {
    2944       36904 :             uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
    2945             :             uint64_t Coeff;
    2946       36904 :             if (LargerThan64Bits)
    2947             :               Coeff = umul_ov(Coeff1, Coeff2, Overflow);
    2948             :             else
    2949       36899 :               Coeff = Coeff1*Coeff2;
    2950       36904 :             const SCEV *CoeffTerm = getConstant(Ty, Coeff);
    2951       73808 :             const SCEV *Term1 = AddRec->getOperand(y-z);
    2952       73808 :             const SCEV *Term2 = OtherAddRec->getOperand(z);
    2953       36904 :             Term = getAddExpr(Term, getMulExpr(CoeffTerm, Term1, Term2,
    2954             :                                                SCEV::FlagAnyWrap, Depth + 1),
    2955             :                               SCEV::FlagAnyWrap, Depth + 1);
    2956             :           }
    2957             :         }
    2958       18859 :         AddRecOps.push_back(Term);
    2959             :       }
    2960        5089 :       if (!Overflow) {
    2961        5089 :         const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(),
    2962        5089 :                                               SCEV::FlagAnyWrap);
    2963       14021 :         if (Ops.size() == 2) return NewAddRec;
    2964        2492 :         Ops[Idx] = NewAddRec;
    2965        2492 :         Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
    2966        1246 :         OpsModified = true;
    2967             :         AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
    2968             :         if (!AddRec)
    2969             :           break;
    2970             :       }
    2971             :     }
    2972       20229 :     if (OpsModified)
    2973        1010 :       return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
    2974             : 
    2975             :     // Otherwise couldn't fold anything into this recurrence.  Move onto the
    2976             :     // next one.
    2977             :   }
    2978             : 
    2979             :   // Okay, it looks like we really DO need an mul expr.  Check to see if we
    2980             :   // already have one, otherwise create a new one.
    2981      189703 :   return getOrCreateMulExpr(Ops, Flags);
    2982             : }
    2983             : 
    2984             : /// Represents an unsigned remainder expression based on unsigned division.
    2985         231 : const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS,
    2986             :                                          const SCEV *RHS) {
    2987             :   assert(getEffectiveSCEVType(LHS->getType()) ==
    2988             :          getEffectiveSCEVType(RHS->getType()) &&
    2989             :          "SCEVURemExpr operand types don't match!");
    2990             : 
    2991             :   // Short-circuit easy cases
    2992         178 :   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
    2993             :     // If constant is one, the result is trivial
    2994         356 :     if (RHSC->getValue()->isOne())
    2995           2 :       return getZero(LHS->getType()); // X urem 1 --> 0
    2996             : 
    2997             :     // If constant is a power of two, fold into a zext(trunc(LHS)).
    2998         177 :     if (RHSC->getAPInt().isPowerOf2()) {
    2999          96 :       Type *FullTy = LHS->getType();
    3000             :       Type *TruncTy =
    3001         288 :           IntegerType::get(getContext(), RHSC->getAPInt().logBase2());
    3002          96 :       return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy);
    3003             :     }
    3004             :   }
    3005             : 
    3006             :   // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
    3007         134 :   const SCEV *UDiv = getUDivExpr(LHS, RHS);
    3008         134 :   const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW);
    3009         134 :   return getMinusSCEV(LHS, Mult, SCEV::FlagNUW);
    3010             : }
    3011             : 
    3012             : /// Get a canonical unsigned division expression, or something simpler if
    3013             : /// possible.
    3014       22223 : const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
    3015             :                                          const SCEV *RHS) {
    3016             :   assert(getEffectiveSCEVType(LHS->getType()) ==
    3017             :          getEffectiveSCEVType(RHS->getType()) &&
    3018             :          "SCEVUDivExpr operand types don't match!");
    3019             : 
    3020       21917 :   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
    3021       43834 :     if (RHSC->getValue()->isOne())
    3022             :       return LHS;                               // X udiv 1 --> x
    3023             :     // If the denominator is zero, the result of the udiv is undefined. Don't
    3024             :     // try to analyze it, because the resolution chosen here may differ from
    3025             :     // the resolution chosen in other parts of the compiler.
    3026       28558 :     if (!RHSC->getValue()->isZero()) {
    3027             :       // Determine if the division can be folded into the operands of
    3028             :       // its operands.
    3029             :       // TODO: Generalize this to non-constants by using known-bits information.
    3030       14278 :       Type *Ty = LHS->getType();
    3031       14278 :       unsigned LZ = RHSC->getAPInt().countLeadingZeros();
    3032       14278 :       unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
    3033             :       // For non-power-of-two values, effectively round the value up to the
    3034             :       // nearest power of two.
    3035       14278 :       if (!RHSC->getAPInt().isPowerOf2())
    3036         363 :         ++MaxShiftAmt;
    3037             :       IntegerType *ExtTy =
    3038       28556 :         IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
    3039         418 :       if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
    3040             :         if (const SCEVConstant *Step =
    3041         816 :             dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
    3042             :           // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
    3043         398 :           const APInt &StepInt = Step->getAPInt();
    3044         398 :           const APInt &DivInt = RHSC->getAPInt();
    3045        1213 :           if (!StepInt.urem(DivInt) &&
    3046          19 :               getZeroExtendExpr(AR, ExtTy) ==
    3047          38 :               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
    3048             :                             getZeroExtendExpr(Step, ExtTy),
    3049             :                             AR->getLoop(), SCEV::FlagAnyWrap)) {
    3050          24 :             SmallVector<const SCEV *, 4> Operands;
    3051          48 :             for (const SCEV *Op : AR->operands())
    3052          24 :               Operands.push_back(getUDivExpr(Op, RHS));
    3053          12 :             return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW);
    3054             :           }
    3055             :           /// Get a canonical UDivExpr for a recurrence.
    3056             :           /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
    3057             :           // We can currently only fold X%N if X is constant.
    3058         650 :           const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
    3059        1140 :           if (StartC && !DivInt.urem(StepInt) &&
    3060         226 :               getZeroExtendExpr(AR, ExtTy) ==
    3061         452 :               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
    3062             :                             getZeroExtendExpr(Step, ExtTy),
    3063             :                             AR->getLoop(), SCEV::FlagAnyWrap)) {
    3064         130 :             const APInt &StartInt = StartC->getAPInt();
    3065         130 :             const APInt &StartRem = StartInt.urem(StepInt);
    3066         130 :             if (StartRem != 0)
    3067          35 :               LHS = getAddRecExpr(getConstant(StartInt - StartRem), Step,
    3068             :                                   AR->getLoop(), SCEV::FlagNW);
    3069             :           }
    3070             :         }
    3071             :       // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
    3072        2115 :       if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
    3073        4230 :         SmallVector<const SCEV *, 4> Operands;
    3074        8771 :         for (const SCEV *Op : M->operands())
    3075        4541 :           Operands.push_back(getZeroExtendExpr(Op, ExtTy));
    3076        2115 :         if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
    3077             :           // Find an operand that's safely divisible.
    3078           0 :           for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
    3079           0 :             const SCEV *Op = M->getOperand(i);
    3080           0 :             const SCEV *Div = getUDivExpr(Op, RHSC);
    3081           0 :             if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
    3082           0 :               Operands = SmallVector<const SCEV *, 4>(M->op_begin(),
    3083             :                                                       M->op_end());
    3084           0 :               Operands[i] = Div;
    3085           0 :               return getMulExpr(Operands);
    3086             :             }
    3087             :           }
    3088             :       }
    3089             :       // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
    3090        1348 :       if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
    3091        2696 :         SmallVector<const SCEV *, 4> Operands;
    3092        5716 :         for (const SCEV *Op : A->operands())
    3093        3020 :           Operands.push_back(getZeroExtendExpr(Op, ExtTy));
    3094        1348 :         if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
    3095          12 :           Operands.clear();
    3096          12 :           for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
    3097          24 :             const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
    3098          36 :             if (isa<SCEVUDivExpr>(Op) ||
    3099          24 :                 getMulExpr(Op, RHS) != A->getOperand(i))
    3100             :               break;
    3101           0 :             Operands.push_back(Op);
    3102             :           }
    3103          12 :           if (Operands.size() == A->getNumOperands())
    3104           0 :             return getAddExpr(Operands);
    3105             :         }
    3106             :       }
    3107             : 
    3108             :       // Fold if both operands are constant.
    3109        9462 :       if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
    3110        9462 :         Constant *LHSCV = LHSC->getValue();
    3111        9462 :         Constant *RHSCV = RHSC->getValue();
    3112       18924 :         return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
    3113        9462 :                                                                    RHSCV)));
    3114             :       }
    3115             :     }
    3116             :   }
    3117             : 
    3118        5111 :   FoldingSetNodeID ID;
    3119        5111 :   ID.AddInteger(scUDivExpr);
    3120        5111 :   ID.AddPointer(LHS);
    3121        5111 :   ID.AddPointer(RHS);
    3122        5111 :   void *IP = nullptr;
    3123       10222 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    3124        5786 :   SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
    3125        8679 :                                              LHS, RHS);
    3126        5786 :   UniqueSCEVs.InsertNode(S, IP);
    3127        2893 :   return S;
    3128             : }
    3129             : 
    3130           0 : static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) {
    3131           0 :   APInt A = C1->getAPInt().abs();
    3132           0 :   APInt B = C2->getAPInt().abs();
    3133           0 :   uint32_t ABW = A.getBitWidth();
    3134           0 :   uint32_t BBW = B.getBitWidth();
    3135             : 
    3136           0 :   if (ABW > BBW)
    3137           0 :     B = B.zext(ABW);
    3138           0 :   else if (ABW < BBW)
    3139           0 :     A = A.zext(BBW);
    3140             : 
    3141           0 :   return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B));
    3142             : }
    3143             : 
    3144             : /// Get a canonical unsigned division expression, or something simpler if
    3145             : /// possible. There is no representation for an exact udiv in SCEV IR, but we
    3146             : /// can attempt to remove factors from the LHS and RHS.  We can't do this when
    3147             : /// it's not exact because the udiv may be clearing bits.
    3148         195 : const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS,
    3149             :                                               const SCEV *RHS) {
    3150             :   // TODO: we could try to find factors in all sorts of things, but for now we
    3151             :   // just deal with u/exact (multiply, constant). See SCEVDivision towards the
    3152             :   // end of this file for inspiration.
    3153             : 
    3154           1 :   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS);
    3155           2 :   if (!Mul || !Mul->hasNoUnsignedWrap())
    3156         195 :     return getUDivExpr(LHS, RHS);
    3157             : 
    3158           0 :   if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
    3159             :     // If the mulexpr multiplies by a constant, then that constant must be the
    3160             :     // first element of the mulexpr.
    3161           0 :     if (const auto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
    3162           0 :       if (LHSCst == RHSCst) {
    3163           0 :         SmallVector<const SCEV *, 2> Operands;
    3164           0 :         Operands.append(Mul->op_begin() + 1, Mul->op_end());
    3165           0 :         return getMulExpr(Operands);
    3166             :       }
    3167             : 
    3168             :       // We can't just assume that LHSCst divides RHSCst cleanly, it could be
    3169             :       // that there's a factor provided by one of the other terms. We need to
    3170             :       // check.
    3171           0 :       APInt Factor = gcd(LHSCst, RHSCst);
    3172           0 :       if (!Factor.isIntN(1)) {
    3173           0 :         LHSCst =
    3174           0 :             cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor)));
    3175           0 :         RHSCst =
    3176           0 :             cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor)));
    3177           0 :         SmallVector<const SCEV *, 2> Operands;
    3178           0 :         Operands.push_back(LHSCst);
    3179           0 :         Operands.append(Mul->op_begin() + 1, Mul->op_end());
    3180           0 :         LHS = getMulExpr(Operands);
    3181           0 :         RHS = RHSCst;
    3182           0 :         Mul = dyn_cast<SCEVMulExpr>(LHS);
    3183             :         if (!Mul)
    3184           0 :           return getUDivExactExpr(LHS, RHS);
    3185             :       }
    3186             :     }
    3187             :   }
    3188             : 
    3189           0 :   for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
    3190           0 :     if (Mul->getOperand(i) == RHS) {
    3191           0 :       SmallVector<const SCEV *, 2> Operands;
    3192           0 :       Operands.append(Mul->op_begin(), Mul->op_begin() + i);
    3193           0 :       Operands.append(Mul->op_begin() + i + 1, Mul->op_end());
    3194           0 :       return getMulExpr(Operands);
    3195             :     }
    3196             :   }
    3197             : 
    3198           0 :   return getUDivExpr(LHS, RHS);
    3199             : }
    3200             : 
    3201             : /// Get an add recurrence expression for the specified loop.  Simplify the
    3202             : /// expression as much as possible.
    3203      134327 : const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step,
    3204             :                                            const Loop *L,
    3205             :                                            SCEV::NoWrapFlags Flags) {
    3206      268654 :   SmallVector<const SCEV *, 4> Operands;
    3207      134327 :   Operands.push_back(Start);
    3208      134523 :   if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
    3209         196 :     if (StepChrec->getLoop() == L) {
    3210         112 :       Operands.append(StepChrec->op_begin(), StepChrec->op_end());
    3211          56 :       return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW));
    3212             :     }
    3213             : 
    3214      134271 :   Operands.push_back(Step);
    3215      134271 :   return getAddRecExpr(Operands, L, Flags);
    3216             : }
    3217             : 
    3218             : /// Get an add recurrence expression for the specified loop.  Simplify the
    3219             : /// expression as much as possible.
    3220             : const SCEV *
    3221      611774 : ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
    3222             :                                const Loop *L, SCEV::NoWrapFlags Flags) {
    3223     1239267 :   if (Operands.size() == 1) return Operands[0];
    3224             : #ifndef NDEBUG
    3225             :   Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
    3226             :   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
    3227             :     assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy &&
    3228             :            "SCEVAddRecExpr operand types don't match!");
    3229             :   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
    3230             :     assert(isLoopInvariant(Operands[i], L) &&
    3231             :            "SCEVAddRecExpr operand is not loop-invariant!");
    3232             : #endif
    3233             : 
    3234     1192110 :   if (Operands.back()->isZero()) {
    3235       40104 :     Operands.pop_back();
    3236       20052 :     return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0}  -->  X
    3237             :   }
    3238             : 
    3239             :   // It's tempting to want to call getMaxBackedgeTakenCount count here and
    3240             :   // use that information to infer NUW and NSW flags. However, computing a
    3241             :   // BE count requires calling getAddRecExpr, so we may not yet have a
    3242             :   // meaningful BE count at this point (and if we don't, we'd be stuck
    3243             :   // with a SCEVCouldNotCompute as the cached BE count).
    3244             : 
    3245      576003 :   Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
    3246             : 
    3247             :   // Canonicalize nested AddRecs in by nesting them in order of loop depth.
    3248     1285658 :   if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
    3249      133652 :     const Loop *NestedLoop = NestedAR->getLoop();
    3250      267304 :     if (L->contains(NestedLoop)
    3251      133652 :             ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
    3252      384605 :             : (!NestedLoop->contains(L) &&
    3253      351903 :                DT.dominates(L->getHeader(), NestedLoop->getHeader()))) {
    3254             :       SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
    3255           0 :                                                   NestedAR->op_end());
    3256           0 :       Operands[0] = NestedAR->getStart();
    3257             :       // AddRecs require their operands be loop-invariant with respect to their
    3258             :       // loops. Don't perform this transformation if it would break this
    3259             :       // requirement.
    3260           0 :       bool AllInvariant = all_of(
    3261           0 :           Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); });
    3262             : 
    3263           0 :       if (AllInvariant) {
    3264             :         // Create a recurrence for the outer loop with the same step size.
    3265             :         //
    3266             :         // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
    3267             :         // inner recurrence has the same property.
    3268             :         SCEV::NoWrapFlags OuterFlags =
    3269           0 :           maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
    3270             : 
    3271           0 :         NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
    3272           0 :         AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) {
    3273           0 :           return isLoopInvariant(Op, NestedLoop);
    3274           0 :         });
    3275             : 
    3276           0 :         if (AllInvariant) {
    3277             :           // Ok, both add recurrences are valid after the transformation.
    3278             :           //
    3279             :           // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
    3280             :           // the outer recurrence has the same property.
    3281             :           SCEV::NoWrapFlags InnerFlags =
    3282           0 :             maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
    3283           0 :           return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
    3284             :         }
    3285             :       }
    3286             :       // Reset Operands to its original state.
    3287           0 :       Operands[0] = NestedAR;
    3288             :     }
    3289             :   }
    3290             : 
    3291             :   // Okay, it looks like we really DO need an addrec expr.  Check to see if we
    3292             :   // already have one, otherwise create a new one.
    3293      576003 :   FoldingSetNodeID ID;
    3294      576003 :   ID.AddInteger(scAddRecExpr);
    3295     2335583 :   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
    3296     2367154 :     ID.AddPointer(Operands[i]);
    3297      576003 :   ID.AddPointer(L);
    3298      576003 :   void *IP = nullptr;
    3299             :   SCEVAddRecExpr *S =
    3300     1152006 :     static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
    3301      576003 :   if (!S) {
    3302      391770 :     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Operands.size());
    3303      522360 :     std::uninitialized_copy(Operands.begin(), Operands.end(), O);
    3304      261180 :     S = new (SCEVAllocator) SCEVAddRecExpr(ID.Intern(SCEVAllocator),
    3305      522360 :                                            O, Operands.size(), L);
    3306      130590 :     UniqueSCEVs.InsertNode(S, IP);
    3307             :   }
    3308     1152006 :   S->setNoWrapFlags(Flags);
    3309      576003 :   return S;
    3310             : }
    3311             : 
    3312             : const SCEV *
    3313       77163 : ScalarEvolution::getGEPExpr(GEPOperator *GEP,
    3314             :                             const SmallVectorImpl<const SCEV *> &IndexExprs) {
    3315       77163 :   const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
    3316             :   // getSCEV(Base)->getType() has the same address space as Base->getType()
    3317             :   // because SCEV::getType() preserves the address space.
    3318       77163 :   Type *IntPtrTy = getEffectiveSCEVType(BaseExpr->getType());
    3319             :   // FIXME(PR23527): Don't blindly transfer the inbounds flag from the GEP
    3320             :   // instruction to its SCEV, because the Instruction may be guarded by control
    3321             :   // flow and the no-overflow bits may not be valid for the expression in any
    3322             :   // context. This can be fixed similarly to how these flags are handled for
    3323             :   // adds.
    3324       77163 :   SCEV::NoWrapFlags Wrap = GEP->isInBounds() ? SCEV::FlagNSW
    3325       77163 :                                              : SCEV::FlagAnyWrap;
    3326             : 
    3327       77163 :   const SCEV *TotalOffset = getZero(IntPtrTy);
    3328             :   // The array size is unimportant. The first thing we do on CurTy is getting
    3329             :   // its element type.
    3330       77163 :   Type *CurTy = ArrayType::get(GEP->getSourceElementType(), 0);
    3331      356604 :   for (const SCEV *IndexExpr : IndexExprs) {
    3332             :     // Compute the (potentially symbolic) offset in bytes for this index.
    3333       21875 :     if (StructType *STy = dyn_cast<StructType>(CurTy)) {
    3334             :       // For a struct, add the member offset.
    3335       21875 :       ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
    3336       21875 :       unsigned FieldNo = Index->getZExtValue();
    3337       21875 :       const SCEV *FieldOffset = getOffsetOfExpr(IntPtrTy, STy, FieldNo);
    3338             : 
    3339             :       // Add the field offset to the running total offset.
    3340       21875 :       TotalOffset = getAddExpr(TotalOffset, FieldOffset);
    3341             : 
    3342             :       // Update CurTy to the type of the field at Index.
    3343       21875 :       CurTy = STy->getTypeAtIndex(Index);
    3344             :     } else {
    3345             :       // Update CurTy to its element type.
    3346      103240 :       CurTy = cast<SequentialType>(CurTy)->getElementType();
    3347             :       // For an array, add the element offset, explicitly scaled.
    3348      103240 :       const SCEV *ElementSize = getSizeOfExpr(IntPtrTy, CurTy);
    3349             :       // Getelementptr indices are signed.
    3350      103240 :       IndexExpr = getTruncateOrSignExtend(IndexExpr, IntPtrTy);
    3351             : 
    3352             :       // Multiply the index by the element size to compute the element offset.
    3353      103240 :       const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, Wrap);
    3354             : 
    3355             :       // Add the element offset to the running total offset.
    3356      103240 :       TotalOffset = getAddExpr(TotalOffset, LocalOffset);
    3357             :     }
    3358             :   }
    3359             : 
    3360             :   // Add the total offset from all the GEP indices to the base.
    3361       77163 :   return getAddExpr(BaseExpr, TotalOffset, Wrap);
    3362             : }
    3363             : 
    3364       19369 : const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
    3365             :                                          const SCEV *RHS) {
    3366       58107 :   SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
    3367       38738 :   return getSMaxExpr(Ops);
    3368             : }
    3369             : 
    3370             : const SCEV *
    3371       19433 : ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
    3372             :   assert(!Ops.empty() && "Cannot get empty smax!");
    3373       38866 :   if (Ops.size() == 1) return Ops[0];
    3374             : #ifndef NDEBUG
    3375             :   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
    3376             :   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
    3377             :     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
    3378             :            "SCEVSMaxExpr operand types don't match!");
    3379             : #endif
    3380             : 
    3381             :   // Sort by complexity, this groups all similar expression types together.
    3382       19433 :   GroupByComplexity(Ops, &LI, DT);
    3383             : 
    3384             :   // If there are any constants, fold them together.
    3385       19433 :   unsigned Idx = 0;
    3386       38866 :   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
    3387             :     ++Idx;
    3388             :     assert(Idx < Ops.size());
    3389       55736 :     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
    3390             :       // We found two constants, fold them together!
    3391       52494 :       ConstantInt *Fold = ConstantInt::get(
    3392       17498 :           getContext(), APIntOps::smax(LHSC->getAPInt(), RHSC->getAPInt()));
    3393       34996 :       Ops[0] = getConstant(Fold);
    3394       34996 :       Ops.erase(Ops.begin()+1);  // Erase the folded element
    3395       52492 :       if (Ops.size() == 1) return Ops[0];
    3396           6 :       LHSC = cast<SCEVConstant>(Ops[0]);
    3397           2 :     }
    3398             : 
    3399             :     // If we are left with a constant minimum-int, strip it off.
    3400        4863 :     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
    3401           3 :       Ops.erase(Ops.begin());
    3402           3 :       --Idx;
    3403        4854 :     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
    3404             :       // If we have an smax with a constant maximum-int, it will always be
    3405             :       // maximum-int.
    3406             :       return Ops[0];
    3407             :     }
    3408             : 
    3409        3243 :     if (Ops.size() == 1) return Ops[0];
    3410             :   }
    3411             : 
    3412             :   // Find the first SMax
    3413        9734 :   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
    3414        1230 :     ++Idx;
    3415             : 
    3416             :   // Check to see if one of the operands is an SMax. If so, expand its operands
    3417             :   // onto our operand list, and recurse to simplify.
    3418        3866 :   if (Idx < Ops.size()) {
    3419             :     bool DeletedSMax = false;
    3420        2067 :     while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
    3421          57 :       Ops.erase(Ops.begin()+Idx);
    3422         114 :       Ops.append(SMax->op_begin(), SMax->op_end());
    3423          57 :       DeletedSMax = true;
    3424          57 :     }
    3425             : 
    3426         948 :     if (DeletedSMax)
    3427          57 :       return getSMaxExpr(Ops);
    3428             :   }
    3429             : 
    3430             :   // Okay, check to see if the same value occurs in the operand list twice.  If
    3431             :   // so, delete one.  Since we sorted the list, these values are required to
    3432             :   // be adjacent.
    3433        6025 :   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
    3434             :     //  X smax Y smax Y  -->  X smax Y
    3435             :     //  X smax Y         -->  X, if X is always greater than Y
    3436        9091 :     if (Ops[i] == Ops[i+1] ||
    3437        6816 :         isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) {
    3438          21 :       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
    3439           7 :       --i; --e;
    3440        6798 :     } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) {
    3441          63 :       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
    3442          21 :       --i; --e;
    3443             :     }
    3444             : 
    3445        3780 :   if (Ops.size() == 1) return Ops[0];
    3446             : 
    3447             :   assert(!Ops.empty() && "Reduced smax down to nothing!");
    3448             : 
    3449             :   // Okay, it looks like we really DO need an smax expr.  Check to see if we
    3450             :   // already have one, otherwise create a new one.
    3451        1848 :   FoldingSetNodeID ID;
    3452        1848 :   ID.AddInteger(scSMaxExpr);
    3453        7789 :   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
    3454        8186 :     ID.AddPointer(Ops[i]);
    3455        1848 :   void *IP = nullptr;
    3456        3696 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    3457        5085 :   const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
    3458        6780 :   std::uninitialized_copy(Ops.begin(), Ops.end(), O);
    3459        3390 :   SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator),
    3460        6780 :                                              O, Ops.size());
    3461        3390 :   UniqueSCEVs.InsertNode(S, IP);
    3462        1695 :   return S;
    3463             : }
    3464             : 
    3465         894 : const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
    3466             :                                          const SCEV *RHS) {
    3467        2682 :   SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
    3468        1788 :   return getUMaxExpr(Ops);
    3469             : }
    3470             : 
    3471             : const SCEV *
    3472         916 : ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
    3473             :   assert(!Ops.empty() && "Cannot get empty umax!");
    3474        1832 :   if (Ops.size() == 1) return Ops[0];
    3475             : #ifndef NDEBUG
    3476             :   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
    3477             :   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
    3478             :     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
    3479             :            "SCEVUMaxExpr operand types don't match!");
    3480             : #endif
    3481             : 
    3482             :   // Sort by complexity, this groups all similar expression types together.
    3483         916 :   GroupByComplexity(Ops, &LI, DT);
    3484             : 
    3485             :   // If there are any constants, fold them together.
    3486         916 :   unsigned Idx = 0;
    3487        1832 :   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
    3488             :     ++Idx;
    3489             :     assert(Idx < Ops.size());
    3490        1567 :     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
    3491             :       // We found two constants, fold them together!
    3492         705 :       ConstantInt *Fold = ConstantInt::get(
    3493         235 :           getContext(), APIntOps::umax(LHSC->getAPInt(), RHSC->getAPInt()));
    3494         470 :       Ops[0] = getConstant(Fold);
    3495         470 :       Ops.erase(Ops.begin()+1);  // Erase the folded element
    3496         705 :       if (Ops.size() == 1) return Ops[0];
    3497           0 :       LHSC = cast<SCEVConstant>(Ops[0]);
    3498           0 :     }
    3499             : 
    3500             :     // If we are left with a constant minimum-int, strip it off.
    3501        1293 :     if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
    3502         123 :       Ops.erase(Ops.begin());
    3503         123 :       --Idx;
    3504         924 :     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
    3505             :       // If we have an umax with a constant maximum-int, it will always be
    3506             :       // maximum-int.
    3507             :       return Ops[0];
    3508             :     }
    3509             : 
    3510         981 :     if (Ops.size() == 1) return Ops[0];
    3511             :   }
    3512             : 
    3513             :   // Find the first UMax
    3514        3409 :   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
    3515         508 :     ++Idx;
    3516             : 
    3517             :   // Check to see if one of the operands is a UMax. If so, expand its operands
    3518             :   // onto our operand list, and recurse to simplify.
    3519        1112 :   if (Idx < Ops.size()) {
    3520             :     bool DeletedUMax = false;
    3521         572 :     while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
    3522          14 :       Ops.erase(Ops.begin()+Idx);
    3523          28 :       Ops.append(UMax->op_begin(), UMax->op_end());
    3524          14 :       DeletedUMax = true;
    3525          14 :     }
    3526             : 
    3527         265 :     if (DeletedUMax)
    3528          14 :       return getUMaxExpr(Ops);
    3529             :   }
    3530             : 
    3531             :   // Okay, check to see if the same value occurs in the operand list twice.  If
    3532             :   // so, delete one.  Since we sorted the list, these values are required to
    3533             :   // be adjacent.
    3534        1652 :   for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
    3535             :     //  X umax Y umax Y  -->  X umax Y
    3536             :     //  X umax Y         -->  X, if X is always greater than Y
    3537        2271 :     if (Ops[i] == Ops[i+1] ||
    3538        1701 :         isKnownPredicate(ICmpInst::ICMP_UGE, Ops[i], Ops[i+1])) {
    3539          21 :       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
    3540           7 :       --i; --e;
    3541        1683 :     } else if (isKnownPredicate(ICmpInst::ICMP_ULE, Ops[i], Ops[i+1])) {
    3542          45 :       Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
    3543          15 :       --i; --e;
    3544             :     }
    3545             : 
    3546        1106 :   if (Ops.size() == 1) return Ops[0];
    3547             : 
    3548             :   assert(!Ops.empty() && "Reduced umax down to nothing!");
    3549             : 
    3550             :   // Okay, it looks like we really DO need a umax expr.  Check to see if we
    3551             :   // already have one, otherwise create a new one.
    3552         520 :   FoldingSetNodeID ID;
    3553         520 :   ID.AddInteger(scUMaxExpr);
    3554        2106 :   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
    3555        2132 :     ID.AddPointer(Ops[i]);
    3556         520 :   void *IP = nullptr;
    3557        1040 :   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
    3558        1398 :   const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
    3559        1864 :   std::uninitialized_copy(Ops.begin(), Ops.end(), O);
    3560         932 :   SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator),
    3561        1864 :                                              O, Ops.size());
    3562         932 :   UniqueSCEVs.InsertNode(S, IP);
    3563         466 :   return S;
    3564             : }
    3565             : 
    3566       16010 : const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
    3567             :                                          const SCEV *RHS) {
    3568             :   // ~smax(~x, ~y) == smin(x, y).
    3569       16010 :   return getNotSCEV(getSMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
    3570             : }
    3571             : 
    3572         396 : const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
    3573             :                                          const SCEV *RHS) {
    3574             :   // ~umax(~x, ~y) == umin(x, y)
    3575         396 :   return getNotSCEV(getUMaxExpr(getNotSCEV(LHS), getNotSCEV(RHS)));
    3576             : }
    3577             : 
    3578      135933 : const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
    3579             :   // We can bypass creating a target-independent
    3580             :   // constant expression and then folding it back into a ConstantInt.
    3581             :   // This is just a compile-time optimization.
    3582      271866 :   return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy));
    3583             : }
    3584             : 
    3585       21875 : const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
    3586             :                                              StructType *STy,
    3587             :                                              unsigned FieldNo) {
    3588             :   // We can bypass creating a target-independent
    3589             :   // constant expression and then folding it back into a ConstantInt.
    3590             :   // This is just a compile-time optimization.
    3591       43750 :   return getConstant(
    3592       43750 :       IntTy, getDataLayout().getStructLayout(STy)->getElementOffset(FieldNo));
    3593             : }
    3594             : 
    3595      159981 : const SCEV *ScalarEvolution::getUnknown(Value *V) {
    3596             :   // Don't attempt to do anything other than create a SCEVUnknown object
    3597             :   // here.  createSCEV only calls getUnknown after checking for all other
    3598             :   // interesting possibilities, and any other code that calls getUnknown
    3599             :   // is doing so in order to hide a value from SCEV canonicalization.
    3600             : 
    3601      319962 :   FoldingSetNodeID ID;
    3602      159981 :   ID.AddInteger(scUnknown);
    3603      159981 :   ID.AddPointer(V);
    3604      159981 :   void *IP = nullptr;
    3605      319962 :   if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
    3606             :     assert(cast<SCEVUnknown>(S)->getValue() == V &&
    3607             :            "Stale SCEVUnknown in uniquing map!");
    3608             :     return S;
    3609             :   }
    3610      264346 :   SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
    3611      264346 :                                             FirstUnknown);
    3612      132173 :   FirstUnknown = cast<SCEVUnknown>(S);
    3613      264346 :   UniqueSCEVs.InsertNode(S, IP);
    3614      132173 :   return S;
    3615             : }
    3616             : 
    3617             : //===----------------------------------------------------------------------===//
    3618             : //            Basic SCEV Analysis and PHI Idiom Recognition Code
    3619             : //
    3620             : 
    3621             : /// Test if values of the given type are analyzable within the SCEV
    3622             : /// framework. This primarily includes integer types, and it can optionally
    3623             : /// include pointer types if the ScalarEvolution class has access to
    3624             : /// target-specific information.
    3625     1012124 : bool ScalarEvolution::isSCEVable(Type *Ty) const {
    3626             :   // Integers and pointers are always SCEVable.
    3627     1504098 :   return Ty->isIntegerTy() || Ty->isPointerTy();
    3628             : }
    3629             : 
    3630             : /// Return the size in bits of the specified type, for which isSCEVable must
    3631             : /// return true.
    3632     1973058 : uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
    3633             :   assert(isSCEVable(Ty) && "Type is not SCEVable!");
    3634     3946116 :   return getDataLayout().getTypeSizeInBits(Ty);
    3635             : }
    3636             : 
    3637             : /// Return a type with the same bitwidth as the given type and which represents
    3638             : /// how SCEV will treat the given type, for which isSCEVable must return
    3639             : /// true. For pointer types, this is the pointer-sized integer type.
    3640     1424731 : Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
    3641             :   assert(isSCEVable(Ty) && "Type is not SCEVable!");
    3642             : 
    3643     1424731 :   if (Ty->isIntegerTy())
    3644             :     return Ty;
    3645             : 
    3646             :   // The only other support type is pointer.
    3647             :   assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
    3648      464214 :   return getDataLayout().getIntPtrType(Ty);
    3649             : }
    3650             : 
    3651          29 : Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const {
    3652          29 :   return  getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2;
    3653             : }
    3654             : 
    3655      393737 : const SCEV *ScalarEvolution::getCouldNotCompute() {
    3656      787474 :   return CouldNotCompute.get();
    3657             : }
    3658             : 
    3659      921480 : bool ScalarEvolution::checkValidity(const SCEV *S) const {
    3660             :   bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) {
    3661      564139 :     auto *SU = dyn_cast<SCEVUnknown>(S);
    3662     1128278 :     return SU && SU->getValue() == nullptr;
    3663     1842960 :   });
    3664             : 
    3665      921480 :   return !ContainsNulls;
    3666             : }
    3667             : 
    3668       42175 : bool ScalarEvolution::containsAddRecurrence(const SCEV *S) {
    3669       42175 :   HasRecMapType::iterator I = HasRecMap.find(S);
    3670      126525 :   if (I != HasRecMap.end())
    3671        8560 :     return I->second;
    3672             : 
    3673       67230 :   bool FoundAddRec = SCEVExprContains(S, isa<SCEVAddRecExpr, const SCEV *>);
    3674      100845 :   HasRecMap.insert({S, FoundAddRec});
    3675       33615 :   return FoundAddRec;
    3676             : }
    3677             : 
    3678             : /// Try to split a SCEVAddExpr into a pair of {SCEV, ConstantInt}.
    3679             : /// If \p S is a SCEVAddExpr and is composed of a sub SCEV S' and an
    3680             : /// offset I, then return {S', I}, else return {\p S, nullptr}.
    3681      408414 : static std::pair<const SCEV *, ConstantInt *> splitAddExpr(const SCEV *S) {
    3682      475537 :   const auto *Add = dyn_cast<SCEVAddExpr>(S);
    3683             :   if (!Add)
    3684      682582 :     return {S, nullptr};
    3685             : 
    3686       67123 :   if (Add->getNumOperands() != 2)
    3687       11626 :     return {S, nullptr};
    3688             : 
    3689      154941 :   auto *ConstOp = dyn_cast<SCEVConstant>(Add->getOperand(0));
    3690             :   if (!ConstOp)
    3691       57978 :     return {S, nullptr};
    3692             : 
    3693       96963 :   return {Add->getOperand(1), ConstOp->getValue()};
    3694             : }
    3695             : 
    3696             : /// Return the ValueOffsetPair set for \p S. \p S can be represented
    3697             : /// by the value and offset from any ValueOffsetPair in the set.
    3698             : SetVector<ScalarEvolution::ValueOffsetPair> *
    3699      140566 : ScalarEvolution::getSCEVValues(const SCEV *S) {
    3700      140566 :   ExprValueMapType::iterator SI = ExprValueMap.find_as(S);
    3701      421698 :   if (SI == ExprValueMap.end())
    3702             :     return nullptr;
    3703             : #ifndef NDEBUG
    3704             :   if (VerifySCEVMap) {
    3705             :     // Check there is no dangling Value in the set returned.
    3706             :     for (const auto &VE : SI->second)
    3707             :       assert(ValueExprMap.count(VE.first));
    3708             :   }
    3709             : #endif
    3710       66439 :   return &SI->second;
    3711             : }
    3712             : 
    3713             : /// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
    3714             : /// cannot be used separately. eraseValueFromMap should be used to remove
    3715             : /// V from ValueExprMap and ExprValueMap at the same time.
    3716       81610 : void ScalarEvolution::eraseValueFromMap(Value *V) {
    3717       81610 :   ValueExprMapType::iterator I = ValueExprMap.find_as(V);
    3718      244830 :   if (I != ValueExprMap.end()) {
    3719       74817 :     const SCEV *S = I->second;
    3720             :     // Remove {V, 0} from the set of ExprValueMap[S]
    3721       74817 :     if (SetVector<ValueOffsetPair> *SV = getSCEVValues(S))
    3722       95926 :       SV->remove({V, nullptr});
    3723             : 
    3724             :     // Remove {V, Offset} from the set of ExprValueMap[Stripped]
    3725             :     const SCEV *Stripped;
    3726             :     ConstantInt *Offset;
    3727      224451 :     std::tie(Stripped, Offset) = splitAddExpr(S);
    3728       74817 :     if (Offset != nullptr) {
    3729        7629 :       if (SetVector<ValueOffsetPair> *SV = getSCEVValues(Stripped))
    3730         824 :         SV->remove({V, Offset});
    3731             :     }
    3732      149634 :     ValueExprMap.erase(V);
    3733             :   }
    3734       81610 : }
    3735             : 
    3736             : /// Return an existing SCEV if it exists, otherwise analyze the expression and
    3737             : /// create a new one.
    3738     1289149 : const SCEV *ScalarEvolution::getSCEV(Value *V) {
    3739             :   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
    3740             : 
    3741     1289149 :   const SCEV *S = getExistingSCEV(V);
    3742     1289149 :   if (S == nullptr) {
    3743      371422 :     S = createSCEV(V);
    3744             :     // During PHI resolution, it is possible to create two SCEVs for the same
    3745             :     // V, so it is needed to double check whether V->S is inserted into
    3746             :     // ValueExprMap before insert S->{V, 0} into ExprValueMap.
    3747             :     std::pair<ValueExprMapType::iterator, bool> Pair =
    3748     1857110 :         ValueExprMap.insert({SCEVCallbackVH(V, this), S});
    3749      371422 :     if (Pair.second) {
    3750     1000791 :       ExprValueMap[S].insert({V, nullptr});
    3751             : 
    3752             :       // If S == Stripped + Offset, add Stripped -> {V, Offset} into
    3753             :       // ExprValueMap.
    3754      333597 :       const SCEV *Stripped = S;
    3755             :       ConstantInt *Offset = nullptr;
    3756     1000791 :       std::tie(Stripped, Offset) = splitAddExpr(S);
    3757             :       // If stripped is SCEVUnknown, don't bother to save
    3758             :       // Stripped -> {V, offset}. It doesn't simplify and sometimes even
    3759             :       // increase the complexity of the expansion code.
    3760             :       // If V is GetElementPtrInst, don't save Stripped -> {V, offset}
    3761             :       // because it may generate add/sub instead of GEP in SCEV expansion.
    3762      358289 :       if (Offset != nullptr && !isa<SCEVUnknown>(Stripped) &&
    3763        2082 :           !isa<GetElementPtrInst>(V))
    3764        6246 :         ExprValueMap[Stripped].insert({V, Offset});
    3765             :     }
    3766             :   }
    3767     1289149 :   return S;
    3768             : }
    3769             : 
    3770     1349768 : const SCEV *ScalarEvolution::getExistingSCEV(Value *V) {
    3771             :   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
    3772             : 
    3773     1349768 :   ValueExprMapType::iterator I = ValueExprMap.find_as(V);
    3774     4049304 :   if (I != ValueExprMap.end()) {
    3775      921480 :     const SCEV *S = I->second;
    3776      921480 :     if (checkValidity(S))
    3777             :       return S;
    3778           0 :     eraseValueFromMap(V);
    3779           0 :     forgetMemoizedResults(S);
    3780             :   }
    3781             :   return nullptr;
    3782             : }
    3783             : 
    3784             : /// Return a SCEV corresponding to -V = -1*V
    3785      552329 : const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
    3786             :                                              SCEV::NoWrapFlags Flags) {
    3787      445822 :   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
    3788      891644 :     return getConstant(
    3789      445822 :                cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
    3790             : 
    3791      106507 :   Type *Ty = V->getType();
    3792      106507 :   Ty = getEffectiveSCEVType(Ty);
    3793      213014 :   return getMulExpr(
    3794      106507 :       V, getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))), Flags);
    3795             : }
    3796             : 
    3797             : /// Return a SCEV corresponding to ~V = -1-V
    3798      110476 : const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
    3799       68029 :   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
    3800      136058 :     return getConstant(
    3801       68029 :                 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
    3802             : 
    3803       42447 :   Type *Ty = V->getType();
    3804       42447 :   Ty = getEffectiveSCEVType(Ty);
    3805             :   const SCEV *AllOnes =
    3806       84894 :                    getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
    3807       42447 :   return getMinusSCEV(AllOnes, V);
    3808             : }
    3809             : 
    3810      601027 : const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
    3811             :                                           SCEV::NoWrapFlags Flags,
    3812             :                                           unsigned Depth) {
    3813             :   // Fast path: X - X --> 0.
    3814      601027 :   if (LHS == RHS)
    3815      115864 :     return getZero(LHS->getType());
    3816             : 
    3817             :   // We represent LHS - RHS as LHS + (-1)*RHS. This transformation
    3818             :   // makes it so that we cannot make much use of NUW.
    3819      543095 :   auto AddFlags = SCEV::FlagAnyWrap;
    3820             :   const bool RHSIsNotMinSigned =
    3821     1086190 :       !getSignedRangeMin(RHS).isMinSignedValue();
    3822      543095 :   if (maskFlags(Flags, SCEV::FlagNSW) == SCEV::FlagNSW) {
    3823             :     // Let M be the minimum representable signed value. Then (-1)*RHS
    3824             :     // signed-wraps if and only if RHS is M. That can happen even for
    3825             :     // a NSW subtraction because e.g. (-1)*M signed-wraps even though
    3826             :     // -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
    3827             :     // (-1)*RHS, we need to prove that RHS != M.
    3828             :     //
    3829             :     // If LHS is non-negative and we know that LHS - RHS does not
    3830             :     // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
    3831             :     // either by proving that RHS > M or that LHS >= 0.
    3832         127 :     if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) {
    3833             :       AddFlags = SCEV::FlagNSW;
    3834             :     }
    3835             :   }
    3836             : 
    3837             :   // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
    3838             :   // RHS is NSW and LHS >= 0.
    3839             :   //
    3840             :   // The difficulty here is that the NSW flag may have been proven
    3841             :   // relative to a loop that is to be found in a recurrence in LHS and
    3842             :   // not in RHS. Applying NSW to (-1)*M may then let the NSW have a
    3843             :   // larger scope than intended.
    3844      543095 :   auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
    3845             : 
    3846      543095 :   return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth);
    3847             : }
    3848             : 
    3849             : const SCEV *
    3850       76109 : ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty) {
    3851       76109 :   Type *SrcTy = V->getType();
    3852             :   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
    3853             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
    3854             :          "Cannot truncate or zero extend with non-integer arguments!");
    3855       76109 :   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
    3856             :     return V;  // No conversion
    3857       13962 :   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
    3858        6895 :     return getTruncateExpr(V, Ty);
    3859        7067 :   return getZeroExtendExpr(V, Ty);
    3860             : }
    3861             : 
    3862             : const SCEV *
    3863      103449 : ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
    3864             :                                          Type *Ty) {
    3865      103449 :   Type *SrcTy = V->getType();
    3866             :   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
    3867             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
    3868             :          "Cannot truncate or zero extend with non-integer arguments!");
    3869      103449 :   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
    3870             :     return V;  // No conversion
    3871        6703 :   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
    3872         441 :     return getTruncateExpr(V, Ty);
    3873        6262 :   return getSignExtendExpr(V, Ty);
    3874             : }
    3875             : 
    3876             : const SCEV *
    3877      127244 : ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
    3878      127244 :   Type *SrcTy = V->getType();
    3879             :   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
    3880             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
    3881             :          "Cannot noop or zero extend with non-integer arguments!");
    3882             :   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
    3883             :          "getNoopOrZeroExtend cannot truncate!");
    3884      127244 :   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
    3885             :     return V;  // No conversion
    3886       17882 :   return getZeroExtendExpr(V, Ty);
    3887             : }
    3888             : 
    3889             : const SCEV *
    3890        2208 : ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
    3891        2208 :   Type *SrcTy = V->getType();
    3892             :   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
    3893             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
    3894             :          "Cannot noop or sign extend with non-integer arguments!");
    3895             :   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
    3896             :          "getNoopOrSignExtend cannot truncate!");
    3897        2208 :   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
    3898             :     return V;  // No conversion
    3899         240 :   return getSignExtendExpr(V, Ty);
    3900             : }
    3901             : 
    3902             : const SCEV *
    3903          68 : ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
    3904          68 :   Type *SrcTy = V->getType();
    3905             :   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
    3906             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
    3907             :          "Cannot noop or any extend with non-integer arguments!");
    3908             :   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
    3909             :          "getNoopOrAnyExtend cannot truncate!");
    3910          68 :   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
    3911             :     return V;  // No conversion
    3912           0 :   return getAnyExtendExpr(V, Ty);
    3913             : }
    3914             : 
    3915             : const SCEV *
    3916         187 : ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
    3917         187 :   Type *SrcTy = V->getType();
    3918             :   assert((SrcTy->isIntegerTy() || SrcTy->isPointerTy()) &&
    3919             :          (Ty->isIntegerTy() || Ty->isPointerTy()) &&
    3920             :          "Cannot truncate or noop with non-integer arguments!");
    3921             :   assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
    3922             :          "getTruncateOrNoop cannot extend!");
    3923         187 :   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
    3924             :     return V;  // No conversion
    3925          15 :   return getTruncateExpr(V, Ty);
    3926             : }
    3927             : 
    3928           1 : const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
    3929             :                                                         const SCEV *RHS) {
    3930           1 :   const SCEV *PromotedLHS = LHS;
    3931           1 :   const SCEV *PromotedRHS = RHS;
    3932             : 
    3933           1 :   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
    3934           0 :     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
    3935             :   else
    3936           1 :     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
    3937             : 
    3938           1 :   return getUMaxExpr(PromotedLHS, PromotedRHS);
    3939             : }
    3940             : 
    3941         235 : const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
    3942             :                                                         const SCEV *RHS) {
    3943         235 :   const SCEV *PromotedLHS = LHS;
    3944         235 :   const SCEV *PromotedRHS = RHS;
    3945             : 
    3946         235 :   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
    3947          16 :     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
    3948             :   else
    3949         219 :     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
    3950             : 
    3951         235 :   return getUMinExpr(PromotedLHS, PromotedRHS);
    3952             : }
    3953             : 
    3954       53083 : const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
    3955             :   // A pointer operand may evaluate to a nonpointer expression, such as null.
    3956      206922 :   if (!V->getType()->isPointerTy())
    3957             :     return V;
    3958             : 
    3959           0 :   if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
    3960           0 :     return getPointerBase(Cast->getOperand());
    3961       50378 :   } else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
    3962       50378 :     const SCEV *PtrOp = nullptr;
    3963      152444 :     for (const SCEV *NAryOp : NAry->operands()) {
    3964      204132 :       if (NAryOp->getType()->isPointerTy()) {
    3965             :         // Cannot find the base of an expression with multiple pointer operands.
    3966       50378 :         if (PtrOp)
    3967             :           return V;
    3968             :         PtrOp = NAryOp;
    3969             :       }
    3970             :     }
    3971       50378 :     if (!PtrOp)
    3972             :       return V;
    3973             :     return getPointerBase(PtrOp);
    3974             :   }
    3975             :   return V;
    3976             : }
    3977             : 
    3978             : /// Push users of the given Instruction onto the given Worklist.
    3979             : static void
    3980      568715 : PushDefUseChildren(Instruction *I,
    3981             :                    SmallVectorImpl<Instruction *> &Worklist) {
    3982             :   // Push the def-use children onto the Worklist stack.
    3983     2997273 :   for (User *U : I->users())
    3984     1291128 :     Worklist.push_back(cast<Instruction>(U));
    3985      568715 : }
    3986             : 
    3987        5105 : void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) {
    3988       10210 :   SmallVector<Instruction *, 16> Worklist;
    3989        5105 :   PushDefUseChildren(PN, Worklist);
    3990             : 
    3991       10210 :   SmallPtrSet<Instruction *, 8> Visited;
    3992        5105 :   Visited.insert(PN);
    3993      125014 :   while (!Worklist.empty()) {
    3994      119909 :     Instruction *I = Worklist.pop_back_val();
    3995      119909 :     if (!Visited.insert(I).second)
    3996       33217 :       continue;
    3997             : 
    3998      104253 :     auto It = ValueExprMap.find_as(static_cast<Value *>(I));
    3999      312759 :     if (It != ValueExprMap.end()) {
    4000        8753 :       const SCEV *Old = It->second;
    4001             : 
    4002             :       // Short-circuit the def-use traversal if the symbolic name
    4003             :       // ceases to appear in expressions.
    4004        8753 :       if (Old != SymName && !hasOperand(Old, SymName))
    4005        1905 :         continue;
    4006             : 
    4007             :       // SCEVUnknown for a PHI either means that it has an unrecognized
    4008             :       // structure, it's a PHI that's in the progress of being computed
    4009             :       // by createNodeForPHI, or it's a single-value PHI. In the first case,
    4010             :       // additional loop trip count information isn't going to change anything.
    4011             :       // In the second case, createNodeForPHI will perform the necessary
    4012             :       // updates on its own when it gets to that point. In the third, we do
    4013             :       // want to forget the SCEVUnknown.
    4014        6862 :       if (!isa<PHINode>(I) ||
    4015        6866 :           !isa<SCEVUnknown>(Old) ||
    4016           4 :           (I != PN && Old == SymName)) {
    4017       13696 :         eraseValueFromMap(It->first);
    4018        6848 :         forgetMemoizedResults(Old);
    4019             :       }
    4020             :     }
    4021             : 
    4022      102348 :     PushDefUseChildren(I, Worklist);
    4023             :   }
    4024        5105 : }
    4025             : 
    4026             : namespace {
    4027             : 
    4028        9020 : class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> {
    4029             : public:
    4030             :   SCEVInitRewriter(const Loop *L, ScalarEvolution &SE)
    4031        9020 :       : SCEVRewriteVisitor(SE), L(L) {}
    4032             : 
    4033        4510 :   static const SCEV *rewrite(const SCEV *S, const Loop *L,
    4034             :                              ScalarEvolution &SE) {
    4035        9020 :     SCEVInitRewriter Rewriter(L, SE);
    4036        4510 :     const SCEV *Result = Rewriter.visit(S);
    4037        9020 :     return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
    4038             :   }
    4039             : 
    4040             :   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
    4041         104 :     if (!SE.isLoopInvariant(Expr, L))
    4042           0 :       Valid = false;
    4043             :     return Expr;
    4044             :   }
    4045             : 
    4046             :   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
    4047             :     // Only allow AddRecExprs for this loop.
    4048         423 :     if (Expr->getLoop() == L)
    4049         423 :       return Expr->getStart();
    4050           0 :     Valid = false;
    4051             :     return Expr;
    4052             :   }
    4053             : 
    4054             :   bool isValid() { return Valid; }
    4055             : 
    4056             : private:
    4057             :   const Loop *L;
    4058             :   bool Valid = true;
    4059             : };
    4060             : 
    4061        9020 : class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> {
    4062             : public:
    4063             :   SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE)
    4064        9020 :       : SCEVRewriteVisitor(SE), L(L) {}
    4065             : 
    4066        4510 :   static const SCEV *rewrite(const SCEV *S, const Loop *L,
    4067             :                              ScalarEvolution &SE) {
    4068        9020 :     SCEVShiftRewriter Rewriter(L, SE);
    4069        4510 :     const SCEV *Result = Rewriter.visit(S);
    4070        9020 :     return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
    4071             :   }
    4072             : 
    4073             :   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
    4074             :     // Only allow AddRecExprs for this loop.
    4075        4001 :     if (!SE.isLoopInvariant(Expr, L))
    4076        3875 :       Valid = false;
    4077             :     return Expr;
    4078             :   }
    4079             : 
    4080         524 :   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
    4081         968 :     if (Expr->getLoop() == L && Expr->isAffine())
    4082         444 :       return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE));
    4083          80 :     Valid = false;
    4084          80 :     return Expr;
    4085             :   }
    4086             : 
    4087             :   bool isValid() { return Valid; }
    4088             : 
    4089             : private:
    4090             :   const Loop *L;
    4091             :   bool Valid = true;
    4092             : };
    4093             : 
    4094             : } // end anonymous namespace
    4095             : 
    4096             : SCEV::NoWrapFlags
    4097       28524 : ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
    4098       28524 :   if (!AR->isAffine())
    4099             :     return SCEV::FlagAnyWrap;
    4100             : 
    4101             :   using OBO = OverflowingBinaryOperator;
    4102             : 
    4103       28524 :   SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap;
    4104             : 
    4105       57048 :   if (!AR->hasNoSignedWrap()) {
    4106       35292 :     ConstantRange AddRecRange = getSignedRange(AR);
    4107       52938 :     ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this));
    4108             : 
    4109             :     auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
    4110       35292 :         Instruction::Add, IncRange, OBO::NoSignedWrap);
    4111       17646 :     if (NSWRegion.contains(AddRecRange))
    4112        4103 :       Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW);
    4113             :   }
    4114             : 
    4115       57048 :   if (!AR->hasNoUnsignedWrap()) {
    4116       55600 :     ConstantRange AddRecRange = getUnsignedRange(AR);
    4117       83400 :     ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this));
    4118             : 
    4119             :     auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
    4120       55600 :         Instruction::Add, IncRange, OBO::NoUnsignedWrap);
    4121       27800 :     if (NUWRegion.contains(AddRecRange))
    4122        2262 :       Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW);
    4123             :   }
    4124             : 
    4125             :   return Result;
    4126             : }
    4127             : 
    4128             : namespace {
    4129             : 
    4130             : /// Represents an abstract binary operation.  This may exist as a
    4131             : /// normal instruction or constant expression, or may have been
    4132             : /// derived from an expression tree.
    4133             : struct BinaryOp {
    4134             :   unsigned Opcode;
    4135             :   Value *LHS;
    4136             :   Value *RHS;
    4137             :   bool IsNSW = false;
    4138             :   bool IsNUW = false;
    4139             : 
    4140             :   /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
    4141             :   /// constant expression.
    4142             :   Operator *Op = nullptr;
    4143             : 
    4144      109611 :   explicit BinaryOp(Operator *Op)
    4145      657666 :       : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)),
    4146      328833 :         Op(Op) {
    4147      104403 :     if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
    4148      104403 :       IsNSW = OBO->hasNoSignedWrap();
    4149      104403 :       IsNUW = OBO->hasNoUnsignedWrap();
    4150             :     }
    4151      109611 :   }
    4152             : 
    4153             :   explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false,
    4154             :                     bool IsNUW = false)
    4155             :       : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
    4156             : };
    4157             : 
    4158             : } // end anonymous namespace
    4159             : 
    4160             : /// Try to map \p V into a BinaryOp, and return \c None on failure.
    4161      357798 : static Optional<BinaryOp> MatchBinaryOp(Value *V, DominatorTree &DT) {
    4162      354677 :   auto *Op = dyn_cast<Operator>(V);
    4163             :   if (!Op)
    4164        3121 :     return None;
    4165             : 
    4166             :   // Implementation detail: all the cleverness here should happen without
    4167             :   // creating new SCEV expressions -- our caller knowns tricks to avoid creating
    4168             :   // SCEV expressions when possible, and we should not break that.
    4169             : 
    4170      354677 :   switch (Op->getOpcode()) {
    4171      109263 :   case Instruction::Add:
    4172             :   case Instruction::Sub:
    4173             :   case Instruction::Mul:
    4174             :   case Instruction::UDiv:
    4175             :   case Instruction::URem:
    4176             :   case Instruction::And:
    4177             :   case Instruction::Or:
    4178             :   case Instruction::AShr:
    4179             :   case Instruction::Shl:
    4180      218526 :     return BinaryOp(Op);
    4181             : 
    4182         343 :   case Instruction::Xor:
    4183         815 :     if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1)))
    4184             :       // If the RHS of the xor is a signmask, then this is just an add.
    4185             :       // Instcombine turns add of signmask into xor as a strength reduction step.
    4186         258 :       if (RHSC->getValue().isSignMask())
    4187          40 :         return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
    4188         666 :     return BinaryOp(Op);
    4189             : 
    4190        2462 :   case Instruction::LShr:
    4191             :     // Turn logical shift right of a constant into a unsigned divide.
    4192        7373 :     if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
    4193        7347 :       uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth();
    4194             : 
    4195             :       // If the shift count is not less than the bitwidth, the result of
    4196             :       // the shift is undefined. Don't try to analyze it, because the
    4197             :       // resolution chosen here may differ from the resolution chosen in
    4198             :       // other parts of the compiler.
    4199        2449 :       if (SA->getValue().ult(BitWidth)) {
    4200             :         Constant *X =
    4201        2447 :             ConstantInt::get(SA->getContext(),
    4202        7341 :                              APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
    4203        7341 :         return BinaryOp(Instruction::UDiv, Op->getOperand(0), X);
    4204             :       }
    4205             :     }
    4206          30 :     return BinaryOp(Op);
    4207             : 
    4208         804 :   case Instruction::ExtractValue: {
    4209         804 :     auto *EVI = cast<ExtractValueInst>(Op);
    4210        1608 :     if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
    4211             :       break;
    4212             : 
    4213         881 :     auto *CI = dyn_cast<CallInst>(EVI->getAggregateOperand());
    4214             :     if (!CI)
    4215             :       break;
    4216             : 
    4217         128 :     if (auto *F = CI->getCalledFunction())
    4218             :       switch (F->getIntrinsicID()) {
    4219          70 :       case Intrinsic::sadd_with_overflow:
    4220             :       case Intrinsic::uadd_with_overflow:
    4221          70 :         if (!isOverflowIntrinsicNoWrap(cast<IntrinsicInst>(CI), DT))
    4222          50 :           return BinaryOp(Instruction::Add, CI->getArgOperand(0),
    4223          25 :                           CI->getArgOperand(1));
    4224             : 
    4225             :         // Now that we know that all uses of the arithmetic-result component of
    4226             :         // CI are guarded by the overflow check, we can go ahead and pretend
    4227             :         // that the arithmetic is non-overflowing.
    4228          45 :         if (F->getIntrinsicID() == Intrinsic::sadd_with_overflow)
    4229          76 :           return BinaryOp(Instruction::Add, CI->getArgOperand(0),
    4230             :                           CI->getArgOperand(1), /* IsNSW = */ true,
    4231          38 :                           /* IsNUW = */ false);
    4232             :         else
    4233          14 :           return BinaryOp(Instruction::Add, CI->getArgOperand(0),
    4234             :                           CI->getArgOperand(1), /* IsNSW = */ false,
    4235           7 :                           /* IsNUW*/ true);
    4236          50 :       case Intrinsic::ssub_with_overflow:
    4237             :       case Intrinsic::usub_with_overflow:
    4238          50 :         if (!isOverflowIntrinsicNoWrap(cast<IntrinsicInst>(CI), DT))
    4239          34 :           return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
    4240          17 :                           CI->getArgOperand(1));
    4241             : 
    4242             :         // The same reasoning as sadd/uadd above.
    4243          33 :         if (F->getIntrinsicID() == Intrinsic::ssub_with_overflow)
    4244          34 :           return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
    4245             :                           CI->getArgOperand(1), /* IsNSW = */ true,
    4246          17 :                           /* IsNUW = */ false);
    4247             :         else
    4248          32 :           return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
    4249             :                           CI->getArgOperand(1), /* IsNSW = */ false,
    4250          16 :                           /* IsNUW = */ true);
    4251           6 :       case Intrinsic::smul_with_overflow:
    4252             :       case Intrinsic::umul_with_overflow:
    4253          12 :         return BinaryOp(Instruction::Mul, CI->getArgOperand(0),
    4254           6 :                         CI->getArgOperand(1));
    4255             :       default:
    4256             :         break;
    4257             :       }
    4258             :   }
    4259             : 
    4260             :   default:
    4261             :     break;
    4262             :   }
    4263             : 
    4264      242483 :   return None;
    4265             : }
    4266             : 
    4267             : /// Helper function to createAddRecFromPHIWithCasts. We have a phi 
    4268             : /// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
    4269             : /// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the 
    4270             : /// way. This function checks if \p Op, an operand of this SCEVAddExpr, 
    4271             : /// follows one of the following patterns:
    4272             : /// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
    4273             : /// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
    4274             : /// If the SCEV expression of \p Op conforms with one of the expected patterns
    4275             : /// we return the type of the truncation operation, and indicate whether the
    4276             : /// truncated type should be treated as signed/unsigned by setting 
    4277             : /// \p Signed to true/false, respectively.
    4278          65 : static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
    4279             :                                bool &Signed, ScalarEvolution &SE) {
    4280             :   // The case where Op == SymbolicPHI (that is, with no type conversions on 
    4281             :   // the way) is handled by the regular add recurrence creating logic and 
    4282             :   // would have already been triggered in createAddRecForPHI. Reaching it here
    4283             :   // means that createAddRecFromPHI had failed for this PHI before (e.g., 
    4284             :   // because one of the other operands of the SCEVAddExpr updating this PHI is
    4285             :   // not invariant). 
    4286             :   //
    4287             :   // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in 
    4288             :   // this case predicates that allow us to prove that Op == SymbolicPHI will
    4289             :   // be added.
    4290          65 :   if (Op == SymbolicPHI)
    4291             :     return nullptr;
    4292             : 
    4293          55 :   unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType());
    4294          55 :   unsigned NewBits = SE.getTypeSizeInBits(Op->getType());
    4295          55 :   if (SourceBits != NewBits)
    4296             :     return nullptr;
    4297             : 
    4298          55 :   const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op);
    4299          55 :   const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op);
    4300          55 :   if (!SExt && !ZExt)
    4301             :     return nullptr;
    4302             :   const SCEVTruncateExpr *Trunc =
    4303           7 :       SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand())
    4304           8 :            : dyn_cast<SCEVTruncateExpr>(ZExt->getOperand());
    4305           5 :   if (!Trunc)
    4306             :     return nullptr;
    4307           5 :   const SCEV *X = Trunc->getOperand();
    4308           5 :   if (X != SymbolicPHI)
    4309             :     return nullptr;
    4310           5 :   Signed = SExt != nullptr;
    4311           5 :   return Trunc->getType();
    4312             : }
    4313             : 
    4314         861 : static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
    4315        1722 :   if (!PN->getType()->isIntegerTy())
    4316             :     return nullptr;
    4317        1358 :   const Loop *L = LI.getLoopFor(PN->getParent());
    4318        1208 :   if (!L || L->getHeader() != PN->getParent())
    4319             :     return nullptr;
    4320             :   return L;
    4321             : }
    4322             : 
    4323             : // Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
    4324             : // computation that updates the phi follows the following pattern:
    4325             : //   (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
    4326             : // which correspond to a phi->trunc->sext/zext->add->phi update chain.
    4327             : // If so, try to see if it can be rewritten as an AddRecExpr under some
    4328             : // Predicates. If successful, return them as a pair. Also cache the results
    4329             : // of the analysis.
    4330             : //
    4331             : // Example usage scenario:
    4332             : //    Say the Rewriter is called for the following SCEV:
    4333             : //         8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
    4334             : //    where:
    4335             : //         %X = phi i64 (%Start, %BEValue)
    4336             : //    It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
    4337             : //    and call this function with %SymbolicPHI = %X.
    4338             : //
    4339             : //    The analysis will find that the value coming around the backedge has 
    4340             : //    the following SCEV:
    4341             : //         BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
    4342             : //    Upon concluding that this matches the desired pattern, the function
    4343             : //    will return the pair {NewAddRec, SmallPredsVec} where:
    4344             : //         NewAddRec = {%Start,+,%Step}
    4345             : //         SmallPredsVec = {P1, P2, P3} as follows:
    4346             : //           P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
    4347             : //           P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
    4348             : //           P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
    4349             : //    The returned pair means that SymbolicPHI can be rewritten into NewAddRec
    4350             : //    under the predicates {P1,P2,P3}.
    4351             : //    This predicated rewrite will be cached in PredicatedSCEVRewrites:
    4352             : //         PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)} 
    4353             : //
    4354             : // TODO's:
    4355             : //
    4356             : // 1) Extend the Induction descriptor to also support inductions that involve
    4357             : //    casts: When needed (namely, when we are called in the context of the 
    4358             : //    vectorizer induction analysis), a Set of cast instructions will be 
    4359             : //    populated by this method, and provided back to isInductionPHI. This is
    4360             : //    needed to allow the vectorizer to properly record them to be ignored by
    4361             : //    the cost model and to avoid vectorizing them (otherwise these casts,
    4362             : //    which are redundant under the runtime overflow checks, will be 
    4363             : //    vectorized, which can be costly).  
    4364             : //
    4365             : // 2) Support additional induction/PHISCEV patterns: We also want to support
    4366             : //    inductions where the sext-trunc / zext-trunc operations (partly) occur 
    4367             : //    after the induction update operation (the induction increment):
    4368             : //
    4369             : //      (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
    4370             : //    which correspond to a phi->add->trunc->sext/zext->phi update chain.
    4371             : //
    4372             : //      (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
    4373             : //    which correspond to a phi->trunc->add->sext/zext->phi update chain.
    4374             : //
    4375             : // 3) Outline common code with createAddRecFromPHI to avoid duplication.
    4376             : Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
    4377         206 : ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) {
    4378         412 :   SmallVector<const SCEVPredicate *, 3> Predicates;
    4379             : 
    4380             :   // *** Part1: Analyze if we have a phi-with-cast pattern for which we can 
    4381             :   // return an AddRec expression under some predicate.
    4382             :  
    4383         618 :   auto *PN = cast<PHINode>(SymbolicPHI->getValue());
    4384         206 :   const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
    4385             :   assert(L && "Expecting an integer loop header phi");
    4386             : 
    4387             :   // The loop may have multiple entrances or multiple exits; we can analyze
    4388             :   // this phi as an addrec if it has a unique entry value and a unique
    4389             :   // backedge value.
    4390         206 :   Value *BEValueV = nullptr, *StartValueV = nullptr;
    4391         824 :   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
    4392         414 :     Value *V = PN->getIncomingValue(i);
    4393        1242 :     if (L->contains(PN->getIncomingBlock(i))) {
    4394         207 :       if (!BEValueV) {
    4395             :         BEValueV = V;
    4396           1 :       } else if (BEValueV != V) {
    4397             :         BEValueV = nullptr;
    4398             :         break;
    4399             :       }
    4400         207 :     } else if (!StartValueV) {
    4401             :       StartValueV = V;
    4402           1 :     } else if (StartValueV != V) {
    4403             :       StartValueV = nullptr;
    4404             :       break;
    4405             :     }
    4406             :   }
    4407         206 :   if (!BEValueV || !StartValueV)
    4408             :     return None;
    4409             : 
    4410         204 :   const SCEV *BEValue = getSCEV(BEValueV);
    4411             : 
    4412             :   // If the value coming around the backedge is an add with the symbolic
    4413             :   // value we just inserted, possibly with casts that we can ignore under
    4414             :   // an appropriate runtime guard, then we found a simple induction variable!
    4415          31 :   const auto *Add = dyn_cast<SCEVAddExpr>(BEValue);
    4416             :   if (!Add)
    4417             :     return None;
    4418             : 
    4419             :   // If there is a single occurrence of the symbolic value, possibly
    4420             :   // casted, replace it with a recurrence. 
    4421          31 :   unsigned FoundIndex = Add->getNumOperands();
    4422          31 :   Type *TruncTy = nullptr;
    4423             :   bool Signed;
    4424          91 :   for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
    4425          65 :     if ((TruncTy = 
    4426         130 :              isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this)))
    4427             :       if (FoundIndex == e) {
    4428             :         FoundIndex = i;
    4429             :         break;
    4430             :       }
    4431             : 
    4432          31 :   if (FoundIndex == Add->getNumOperands())
    4433             :     return None;
    4434             : 
    4435             :   // Create an add with everything but the specified operand.
    4436           5 :   SmallVector<const SCEV *, 8> Ops;
    4437          15 :   for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
    4438          10 :     if (i != FoundIndex)
    4439          10 :       Ops.push_back(Add->getOperand(i));
    4440           5 :   const SCEV *Accum = getAddExpr(Ops);
    4441             : 
    4442             :   // The runtime checks will not be valid if the step amount is
    4443             :   // varying inside the loop.
    4444           5 :   if (!isLoopInvariant(Accum, L))
    4445             :     return None;
    4446             : 
    4447             :   // *** Part2: Create the predicates 
    4448             : 
    4449             :   // Analysis was successful: we have a phi-with-cast pattern for which we
    4450             :   // can return an AddRec expression under the following predicates:
    4451             :   //
    4452             :   // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
    4453             :   //     fits within the truncated type (does not overflow) for i = 0 to n-1.
    4454             :   // P2: An Equal predicate that guarantees that 
    4455             :   //     Start = (Ext ix (Trunc iy (Start) to ix) to iy)
    4456             :   // P3: An Equal predicate that guarantees that 
    4457             :   //     Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
    4458             :   //
    4459             :   // As we next prove, the above predicates guarantee that: 
    4460             :   //     Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
    4461             :   //
    4462             :   //
    4463             :   // More formally, we want to prove that:
    4464             :   //     Expr(i+1) = Start + (i+1) * Accum 
    4465             :   //               = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum 
    4466             :   //
    4467             :   // Given that:
    4468             :   // 1) Expr(0) = Start 
    4469             :   // 2) Expr(1) = Start + Accum 
    4470             :   //            = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
    4471             :   // 3) Induction hypothesis (step i):
    4472             :   //    Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum 
    4473             :   //
    4474             :   // Proof:
    4475             :   //  Expr(i+1) =
    4476             :   //   = Start + (i+1)*Accum
    4477             :   //   = (Start + i*Accum) + Accum
    4478             :   //   = Expr(i) + Accum  
    4479             :   //   = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum 
    4480             :   //                                                             :: from step i
    4481             :   //
    4482             :   //   = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum 
    4483             :   //
    4484             :   //   = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
    4485             :   //     + (Ext ix (Trunc iy (Accum) to ix) to iy)
    4486             :   //     + Accum                                                     :: from P3
    4487             :   //
    4488             :   //   = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy) 
    4489             :   //     + Accum                            :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
    4490             :   //
    4491             :   //   = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
    4492             :   //   = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum 
    4493             :   //
    4494             :   // By induction, the same applies to all iterations 1<=i<n:
    4495             :   //
    4496             : 
    4497             :   // Create a truncated addrec for which we will add a no overflow check (P1).
    4498           4 :   const SCEV *StartVal = getSCEV(StartValueV);
    4499             :   const SCEV *PHISCEV =
    4500           4 :       getAddRecExpr(getTruncateExpr(StartVal, TruncTy),
    4501           4 :                     getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap);
    4502             : 
    4503             :   // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
    4504             :   // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
    4505             :   // will be constant.
    4506             :   //
    4507             :   //  If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
    4508             :   // add P1.
    4509           3 :   if (const auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
    4510           3 :     SCEVWrapPredicate::IncrementWrapFlags AddedFlags =
    4511           3 :         Signed ? SCEVWrapPredicate::IncrementNSSW
    4512             :                : SCEVWrapPredicate::IncrementNUSW;
    4513           3 :     const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags);
    4514           3 :     Predicates.push_back(AddRecPred);
    4515             :   } else
    4516             :     assert(isa<SCEVConstant>(PHISCEV) && "Expected constant SCEV");
    4517             : 
    4518             :   // Create the Equal Predicates P2,P3:
    4519             : 
    4520             :   // It is possible that the predicates P2 and/or P3 are computable at
    4521             :   // compile time due to StartVal and/or Accum being constants.
    4522             :   // If either one is, then we can check that now and escape if either P2
    4523             :   // or P3 is false.
    4524             : 
    4525             :   // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
    4526             :   // for each of StartVal and Accum
    4527           7 :   auto GetExtendedExpr = [&](const SCEV *Expr) -> const SCEV * {
    4528             :     assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant");
    4529          14 :     const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy);
    4530             :     const SCEV *ExtendedExpr =
    4531          14 :         Signed ? getSignExtendExpr(TruncatedExpr, Expr->getType())
    4532           9 :                : getZeroExtendExpr(TruncatedExpr, Expr->getType());
    4533           7 :     return ExtendedExpr;
    4534           4 :   };
    4535             : 
    4536             :   // Given:
    4537             :   //  ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
    4538             :   //               = GetExtendedExpr(Expr)
    4539             :   // Determine whether the predicate P: Expr == ExtendedExpr
    4540             :   // is known to be false at compile time
    4541             :   auto PredIsKnownFalse = [&](const SCEV *Expr,
    4542             :                               const SCEV *ExtendedExpr) -> bool {
    4543          10 :     return Expr != ExtendedExpr &&
    4544           3 :            isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr);
    4545           4 :   };
    4546             : 
    4547           4 :   const SCEV *StartExtended = GetExtendedExpr(StartVal);
    4548           1 :   if (PredIsKnownFalse(StartVal, StartExtended)) {
    4549             :     DEBUG(dbgs() << "P2 is compile-time false\n";);
    4550             :     return None;
    4551             :   }
    4552             : 
    4553           3 :   const SCEV *AccumExtended = GetExtendedExpr(Accum);
    4554           0 :   if (PredIsKnownFalse(Accum, AccumExtended)) {
    4555             :     DEBUG(dbgs() << "P3 is compile-time false\n";);
    4556             :     return None;
    4557             :   }
    4558             : 
    4559             :   auto AppendPredicate = [&](const SCEV *Expr,
    4560           6 :                              const SCEV *ExtendedExpr) -> void {
    4561           8 :     if (Expr != ExtendedExpr &&
    4562           4 :         !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) {
    4563           2 :       const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr);
    4564             :       DEBUG (dbgs() << "Added Predicate: " << *Pred);
    4565           2 :       Predicates.push_back(Pred);
    4566             :     }
    4567           9 :   };
    4568             : 
    4569           3 :   AppendPredicate(StartVal, StartExtended);
    4570           3 :   AppendPredicate(Accum, AccumExtended);
    4571             : 
    4572             :   // *** Part3: Predicates are ready. Now go ahead and create the new addrec in
    4573             :   // which the casts had been folded away. The caller can rewrite SymbolicPHI
    4574             :   // into NewAR if it will also add the runtime overflow checks specified in
    4575             :   // Predicates.  
    4576           3 :   auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap);
    4577             : 
    4578             :   std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
    4579           6 :       std::make_pair(NewAR, Predicates);
    4580             :   // Remember the result of the analysis for this SCEV at this locayyytion.
    4581           9 :   PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite;
    4582           3 :   return PredRewrite;
    4583             : }
    4584             : 
    4585             : Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
    4586         655 : ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) {
    4587        1965 :   auto *PN = cast<PHINode>(SymbolicPHI->getValue());
    4588         655 :   const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
    4589         655 :   if (!L)
    4590             :     return None;
    4591             : 
    4592             :   // Check to see if we already analyzed this PHI.
    4593         752 :   auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L});
    4594        1128 :   if (I != PredicatedSCEVRewrites.end()) {
    4595             :     std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
    4596         510 :         I->second;
    4597             :     // Analysis was done before and failed to create an AddRec:
    4598         170 :     if (Rewrite.first == SymbolicPHI) 
    4599             :       return None;
    4600             :     // Analysis was done before and succeeded to create an AddRec under
    4601             :     // a predicate:
    4602             :     assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec");
    4603             :     assert(!(Rewrite.second).empty() && "Expected to find Predicates");
    4604             :     return Rewrite;
    4605             :   }
    4606             : 
    4607             :   Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
    4608         206 :     Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
    4609             : 
    4610             :   // Record in the cache that the analysis failed
    4611         206 :   if (!Rewrite) {
    4612         406 :     SmallVector<const SCEVPredicate *, 3> Predicates;
    4613        1015 :     PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
    4614         203 :     return None;
    4615             :   }
    4616             : 
    4617           3 :   return Rewrite;
    4618             : }
    4619             : 
    4620             : /// A helper function for createAddRecFromPHI to handle simple cases.
    4621             : ///
    4622             : /// This function tries to find an AddRec expression for the simplest (yet most
    4623             : /// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
    4624             : /// If it fails, createAddRecFromPHI will use a more general, but slow,
    4625             : /// technique for finding the AddRec expression.
    4626       40901 : const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
    4627             :                                                       Value *BEValueV,
    4628             :                                                       Value *StartValueV) {
    4629       81802 :   const Loop *L = LI.getLoopFor(PN->getParent());
    4630             :   assert(L && L->getHeader() == PN->getParent());
    4631             :   assert(BEValueV && StartValueV);
    4632             : 
    4633       81802 :   auto BO = MatchBinaryOp(BEValueV, DT);
    4634       40901 :   if (!BO)
    4635             :     return nullptr;
    4636             : 
    4637       33056 :   if (BO->Opcode != Instruction::Add)
    4638             :     return nullptr;
    4639             : 
    4640       32409 :   const SCEV *Accum = nullptr;
    4641       63164 :   if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
    4642       30649 :     Accum = getSCEV(BO->RHS);
    4643        2140 :   else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
    4644          33 :     Accum = getSCEV(BO->LHS);
    4645             : 
    4646       30682 :   if (!Accum)
    4647             :     return nullptr;
    4648             : 
    4649       30682 :   SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
    4650       30682 :   if (BO->IsNUW)
    4651       13821 :     Flags = setFlags(Flags, SCEV::FlagNUW);
    4652       30682 :   if (BO->IsNSW)
    4653       19161 :     Flags = setFlags(Flags, SCEV::FlagNSW);
    4654             : 
    4655       30682 :   const SCEV *StartVal = getSCEV(StartValueV);
    4656       30682 :   const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
    4657             : 
    4658       92046 :   ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
    4659             : 
    4660             :   // We can add Flags to the post-inc expression only if we
    4661             :   // know that it is *undefined behavior* for BEValueV to
    4662             :   // overflow.
    4663       30682 :   if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
    4664       30682 :     if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
    4665       12488 :       (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
    4666             : 
    4667             :   return PHISCEV;
    4668             : }
    4669             : 
    4670       46601 : const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
    4671       93202 :   const Loop *L = LI.getLoopFor(PN->getParent());
    4672       88218 :   if (!L || L->getHeader() != PN->getParent())
    4673             :     return nullptr;
    4674             : 
    4675             :   // The loop may have multiple entrances or multiple exits; we can analyze
    4676             :   // this phi as an addrec if it has a unique entry value and a unique
    4677             :   // backedge value.
    4678       40929 :   Value *BEValueV = nullptr, *StartValueV = nullptr;
    4679      163739 :   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
    4680       81909 :     Value *V = PN->getIncomingValue(i);
    4681      245727 :     if (L->contains(PN->getIncomingBlock(i))) {
    4682       40972 :       if (!BEValueV) {
    4683             :         BEValueV = V;
    4684          43 :       } else if (BEValueV != V) {
    4685             :         BEValueV = nullptr;
    4686             :         break;
    4687             :       }
    4688       40937 :     } else if (!StartValueV) {
    4689             :       StartValueV = V;
    4690          15 :     } else if (StartValueV != V) {
    4691             :       StartValueV = nullptr;
    4692             :       break;
    4693             :     }
    4694             :   }
    4695       40929 :   if (!BEValueV || !StartValueV)
    4696             :     return nullptr;
    4697             : 
    4698             :   assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
    4699             :          "PHI node already processed?");
    4700             : 
    4701             :   // First, try to find AddRec expression without creating a fictituos symbolic
    4702             :   // value for PN.
    4703       40901 :   if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
    4704             :     return S;
    4705             : 
    4706             :   // Handle PHI node value symbolically.
    4707       10219 :   const SCEV *SymbolicName = getUnknown(PN);
    4708       51095 :   ValueExprMap.insert({SCEVCallbackVH(PN, this), SymbolicName});
    4709             : 
    4710             :   // Using this symbolic name for the PHI, analyze the value coming around
    4711             :   // the back-edge.
    4712       10219 :   const SCEV *BEValue = getSCEV(BEValueV);
    4713             : 
    4714             :   // NOTE: If BEValue is loop invariant, we know that the PHI node just
    4715             :   // has a special value for the first iteration of the loop.
    4716             : 
    4717             :   // If the value coming around the backedge is an add with the symbolic
    4718             :   // value we just inserted, then we found a simple induction variable!
    4719        5709 :   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
    4720             :     // If there is a single occurrence of the symbolic value, replace it
    4721             :     // with a recurrence.
    4722        5709 :     unsigned FoundIndex = Add->getNumOperands();
    4723       12362 :     for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
    4724       23874 :       if (Add->getOperand(i) == SymbolicName)
    4725             :         if (FoundIndex == e) {
    4726             :           FoundIndex = i;
    4727             :           break;
    4728             :         }
    4729             : 
    4730        5709 :     if (FoundIndex != Add->getNumOperands()) {
    4731             :       // Create an add with everything but the specified operand.
    4732        5888 :       SmallVector<const SCEV *, 8> Ops;
    4733       16434 :       for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
    4734       11150 :         if (i != FoundIndex)
    4735       11732 :           Ops.push_back(Add->getOperand(i));
    4736        5284 :       const SCEV *Accum = getAddExpr(Ops);
    4737             : 
    4738             :       // This is not a valid addrec if the step amount is varying each
    4739             :       // loop iteration, but is not itself an addrec in this loop.
    4740        5944 :       if (isLoopInvariant(Accum, L) ||
    4741         716 :           (isa<SCEVAddRecExpr>(Accum) &&
    4742         112 :            cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
    4743        4680 :         SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
    4744             : 
    4745        9360 :         if (auto BO = MatchBinaryOp(BEValueV, DT)) {
    4746        1824 :           if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
    4747          30 :             if (BO->IsNUW)
    4748           0 :               Flags = setFlags(Flags, SCEV::FlagNUW);
    4749          30 :             if (BO->IsNSW)
    4750           0 :               Flags = setFlags(Flags, SCEV::FlagNSW);
    4751             :           }
    4752        3409 :         } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
    4753             :           // If the increment is an inbounds GEP, then we know the address
    4754             :           // space cannot be wrapped around. We cannot make any guarantee
    4755             :           // about signed or unsigned overflow because pointers are
    4756             :           // unsigned but we may have a negative index from the base
    4757             :           // pointer. We can guarantee that no unsigned wrap occurs if the
    4758             :           // indices form a positive value.
    4759        6194 :           if (GEP->isInBounds() && GEP->getOperand(0) == PN) {
    4760        2726 :             Flags = setFlags(Flags, SCEV::FlagNW);
    4761             : 
    4762        2726 :             const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
    4763        2726 :             if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
    4764        2433 :               Flags = setFlags(Flags, SCEV::FlagNUW);
    4765             :           }
    4766             : 
    4767             :           // We cannot transfer nuw and nsw flags from subtraction
    4768             :           // operations -- sub nuw X, Y is not the same as add nuw X, -Y
    4769             :           // for instance.
    4770             :         }
    4771             : 
    4772        4680 :         const SCEV *StartVal = getSCEV(StartValueV);
    4773        4680 :         const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
    4774             : 
    4775             :         // Okay, for the entire analysis of this edge we assumed the PHI
    4776             :         // to be symbolic.  We now need to go back and purge all of the
    4777             :         // entries for the scalars that use the symbolic expression.
    4778        4680 :         forgetSymbolicName(PN, SymbolicName);
    4779       14040 :         ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
    4780             : 
    4781             :         // We can add Flags to the post-inc expression only if we
    4782             :         // know that it is *undefined behavior* for BEValueV to
    4783             :         // overflow.
    4784        4680 :         if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
    4785        4680 :           if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
    4786        1263 :             (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
    4787             : 
    4788        9360 :         return PHISCEV;
    4789             :       }
    4790             :     }
    4791             :   } else {
    4792             :     // Otherwise, this could be a loop like this:
    4793             :     //     i = 0;  for (j = 1; ..; ++j) { ....  i = j; }
    4794             :     // In this case, j = {1,+,1}  and BEValue is j.
    4795             :     // Because the other in-value of i (0) fits the evolution of BEValue
    4796             :     // i really is an addrec evolution.
    4797             :     //
    4798             :     // We can generalize this saying that i is the shifted value of BEValue
    4799             :     // by one iteration:
    4800             :     //   PHI(f(0), f({1,+,1})) --> f({0,+,1})
    4801        4510 :     const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
    4802        4510 :     const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this);
    4803        5169 :     if (Shifted != getCouldNotCompute() &&
    4804         659 :         Start != getCouldNotCompute()) {
    4805         659 :       const SCEV *StartVal = getSCEV(StartValueV);
    4806         659 :       if (Start == StartVal) {
    4807             :         // Okay, for the entire analysis of this edge we assumed the PHI
    4808             :         // to be symbolic.  We now need to go back and purge all of the
    4809             :         // entries for the scalars that use the symbolic expression.
    4810         425 :         forgetSymbolicName(PN, SymbolicName);
    4811        1275 :         ValueExprMap[SCEVCallbackVH(PN, this)] = Shifted;
    4812         425 :         return Shifted;
    4813             :       }
    4814             :     }
    4815             :   }
    4816             : 
    4817             :   // Remove the temporary PHI node SCEV that has been inserted while intending
    4818             :   // to create an AddRecExpr for this PHI node. We can not keep this temporary
    4819             :   // as it will prevent later (possibly simpler) SCEV expressions to be added
    4820             :   // to the ValueExprMap.
    4821        5114 :   eraseValueFromMap(PN);
    4822             : 
    4823        5114 :   return nullptr;
    4824             : }
    4825             : 
    4826             : // Checks if the SCEV S is available at BB.  S is considered available at BB
    4827             : // if S can be materialized at BB without introducing a fault.
    4828        5198 : static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S,
    4829             :                                BasicBlock *BB) {
    4830             :   struct CheckAvailable {
    4831             :     bool TraversalDone = false;
    4832             :     bool Available = true;
    4833             : 
    4834             :     const Loop *L = nullptr;  // The loop BB is in (can be nullptr)
    4835             :     BasicBlock *BB = nullptr;
    4836             :     DominatorTree &DT;
    4837             : 
    4838             :     CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT)
    4839        5198 :       : L(L), BB(BB), DT(DT) {}
    4840             : 
    4841             :     bool setUnavailable() {
    4842        1683 :       TraversalDone = true;
    4843        1683 :       Available = false;
    4844             :       return false;
    4845             :     }
    4846             : 
    4847        9476 :     bool follow(const SCEV *S) {
    4848       18952 :       switch (S->getSCEVType()) {
    4849             :       case scConstant: case scTruncate: case scZeroExtend: case scSignExtend:
    4850             :       case scAddExpr: case scMulExpr: case scUMaxExpr: case scSMaxExpr:
    4851             :         // These expressions are available if their operand(s) is/are.
    4852             :         return true;
    4853             : 
    4854         238 :       case scAddRecExpr: {
    4855             :         // We allow add recurrences that are on the loop BB is in, or some
    4856             :         // outer loop.  This guarantees availability because the value of the
    4857             :         // add recurrence at BB is simply the "current" value of the induction
    4858             :         // variable.  We can relax this in the future; for instance an add
    4859             :         // recurrence on a sibling dominating loop is also available at BB.
    4860         238 :         const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop();
    4861         281 :         if (L && (ARLoop == L || ARLoop->contains(L)))
    4862             :           return true;
    4863             : 
    4864         256 :         return setUnavailable();
    4865             :       }
    4866             : 
    4867        4165 :       case scUnknown: {
    4868             :         // For SCEVUnknown, we check for simple dominance.
    4869        4165 :         const auto *SU = cast<SCEVUnknown>(S);
    4870        4165 :         Value *V = SU->getValue();
    4871             : 
    4872        8330 :         if (isa<Argument>(V))
    4873             :           return false;
    4874             : 
    4875       11480 :         if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB))
    4876             :           return false;
    4877             : 
    4878        2204 :         return setUnavailable();
    4879             :       }
    4880             : 
    4881         453 :       case scUDivExpr:
    4882             :       case scCouldNotCompute:
    4883             :         // We do not try to smart about these at all.
    4884         906 :         return setUnavailable();
    4885             :       }
    4886           0 :       llvm_unreachable("switch should be fully covered!");
    4887             :     }
    4888             : 
    4889             :     bool isDone() { return TraversalDone; }
    4890             :   };
    4891             : 
    4892        5198 :   CheckAvailable CA(L, BB, DT);
    4893       10396 :   SCEVTraversal<CheckAvailable> ST(CA);
    4894             : 
    4895        5198 :   ST.visitAll(S);
    4896       10396 :   return CA.Available;
    4897             : }
    4898             : 
    4899             : // Try to match a control flow sequence that branches out at BI and merges back
    4900             : // at Merge into a "C ? LHS : RHS" select pattern.  Return true on a successful
    4901             : // match.
    4902        3142 : static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge,
    4903             :                           Value *&C, Value *&LHS, Value *&RHS) {
    4904        3142 :   C = BI->getCondition();
    4905             : 
    4906        6284 :   BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0));
    4907        6284 :   BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1));
    4908             : 
    4909        3142 :   if (!LeftEdge.isSingleEdge())
    4910             :     return false;
    4911             : 
    4912             :   assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()");
    4913             : 
    4914        6284 :   Use &LeftUse = Merge->getOperandUse(0);
    4915        6284 :   Use &RightUse = Merge->getOperandUse(1);
    4916             : 
    4917        3142 :   if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) {
    4918        2098 :     LHS = LeftUse;
    4919        2098 :     RHS = RightUse;
    4920        2098 :     return true;
    4921             :   }
    4922             : 
    4923        1044 :   if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) {
    4924         903 :     LHS = RightUse;
    4925         903 :     RHS = LeftUse;
    4926         903 :     return true;
    4927             :   }
    4928             : 
    4929             :   return false;
    4930             : }
    4931             : 
    4932       10814 : const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
    4933             :   auto IsReachable =
    4934       27700 :       [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); };
    4935       27700 :   if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) {
    4936       16884 :     const Loop *L = LI.getLoopFor(PN->getParent());
    4937             : 
    4938             :     // We don't want to break LCSSA, even in a SCEV expression tree.
    4939       24352 :     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
    4940       38253 :       if (LI.getLoopFor(PN->getIncomingBlock(i)) != L)
    4941        6613 :         return nullptr;
    4942             : 
    4943             :     // Try to match
    4944             :     //
    4945             :     //  br %cond, label %left, label %right
    4946             :     // left:
    4947             :     //  br label %merge
    4948             :     // right:
    4949             :     //  br label %merge
    4950             :     // merge:
    4951             :     //  V = phi [ %x, %left ], [ %y, %right ]
    4952             :     //
    4953             :     // as "select %cond, %x, %y"
    4954             : 
    4955        6318 :     BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
    4956             :     assert(IDom && "At least the entry block should dominate PN");
    4957             : 
    4958        6318 :     auto *BI = dyn_cast<BranchInst>(IDom->getTerminator());
    4959        3159 :     Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
    4960             : 
    4961        6284 :     if (BI && BI->isConditional() &&
    4962        6143 :         BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) &&
    4963        8357 :         IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) &&
    4964        2197 :         IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent()))
    4965        1330 :       return createNodeForSelectOrPHI(PN, Cond, LHS, RHS);
    4966             :   }
    4967             : 
    4968             :   return nullptr;
    4969             : }
    4970             : 
    4971       46601 : const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
    4972       46601 :   if (const SCEV *S = createAddRecFromPHI(PN))
    4973             :     return S;
    4974             : 
    4975       10814 :   if (const SCEV *S = createNodeFromSelectLikePHI(PN))
    4976             :     return S;
    4977             : 
    4978             :   // If the PHI has a single incoming value, follow that value, unless the
    4979             :   // PHI's incoming blocks are in a different loop, in which case doing so
    4980             :   // risks breaking LCSSA form. Instcombine would normally zap these, but
    4981             :   // it doesn't have DominatorTree information, so it may miss cases.
    4982       28452 :   if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC}))
    4983        1801 :     if (LI.replacementPreservesLCSSAForm(PN, V))
    4984         141 :       return getSCEV(V);
    4985             : 
    4986             :   // If it's not a loop phi, we can't handle it yet.
    4987        9343 :   return getUnknown(PN);
    4988             : }
    4989             : 
    4990       13615 : const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Instruction *I,
    4991             :                                                       Value *Cond,
    4992             :                                                       Value *TrueVal,
    4993             :                                                       Value *FalseVal) {
    4994             :   // Handle "constant" branch or select. This can occur for instance when a
    4995             :   // loop pass transforms an inner loop and moves on to process the outer loop.
    4996          69 :   if (auto *CI = dyn_cast<ConstantInt>(Cond))
    4997          69 :     return getSCEV(CI->isOne() ? TrueVal : FalseVal);
    4998             : 
    4999             :   // Try to match some simple smax or umax patterns.
    5000       13031 :   auto *ICI = dyn_cast<ICmpInst>(Cond);
    5001             :   if (!ICI)
    5002         515 :     return getUnknown(I);
    5003             : 
    5004       26062 :   Value *LHS = ICI->getOperand(0);
    5005       26062 :   Value *RHS = ICI->getOperand(1);
    5006             : 
    5007       26062 :   switch (ICI->getPredicate()) {
    5008         272 :   case ICmpInst::ICMP_SLT:
    5009             :   case ICmpInst::ICMP_SLE:
    5010             :     std::swap(LHS, RHS);
    5011             :     LLVM_FALLTHROUGH;
    5012         632 :   case ICmpInst::ICMP_SGT:
    5013             :   case ICmpInst::ICMP_SGE:
    5014             :     // a >s b ? a+x : b+x  ->  smax(a, b)+x
    5015             :     // a >s b ? b+x : a+x  ->  smin(a, b)+x
    5016         632 :     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
    5017         616 :       const SCEV *LS = getNoopOrSignExtend(getSCEV(LHS), I->getType());
    5018         616 :       const SCEV *RS = getNoopOrSignExtend(getSCEV(RHS), I->getType());
    5019         616 :       const SCEV *LA = getSCEV(TrueVal);
    5020         616 :       const SCEV *RA = getSCEV(FalseVal);
    5021         616 :       const SCEV *LDiff = getMinusSCEV(LA, LS);
    5022         616 :       const SCEV *RDiff = getMinusSCEV(RA, RS);
    5023         616 :       if (LDiff == RDiff)
    5024         286 :         return getAddExpr(getSMaxExpr(LS, RS), LDiff);
    5025         330 :       LDiff = getMinusSCEV(LA, RS);
    5026         330 :       RDiff = getMinusSCEV(RA, LS);
    5027         330 :       if (LDiff == RDiff)
    5028          79 :         return getAddExpr(getSMinExpr(LS, RS), LDiff);
    5029             :     }
    5030             :     break;
    5031        2056 :   case ICmpInst::ICMP_ULT:
    5032             :   case ICmpInst::ICMP_ULE:
    5033             :     std::swap(LHS, RHS);
    5034             :     LLVM_FALLTHROUGH;
    5035        2835 :   case ICmpInst::ICMP_UGT:
    5036             :   case ICmpInst::ICMP_UGE:
    5037             :     // a >u b ? a+x : b+x  ->  umax(a, b)+x
    5038             :     // a >u b ? b+x : a+x  ->  umin(a, b)+x
    5039        2835 :     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
    5040        2824 :       const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
    5041        2824 :       const SCEV *RS = getNoopOrZeroExtend(getSCEV(RHS), I->getType());
    5042        2824 :       const SCEV *LA = getSCEV(TrueVal);
    5043        2824 :       const SCEV *RA = getSCEV(FalseVal);
    5044        2824 :       const SCEV *LDiff = getMinusSCEV(LA, LS);
    5045        2824 :       const SCEV *RDiff = getMinusSCEV(RA, RS);
    5046        2824 :       if (LDiff == RDiff)
    5047         141 :         return getAddExpr(getUMaxExpr(LS, RS), LDiff);
    5048        2683 :       LDiff = getMinusSCEV(LA, RS);
    5049        2683 :       RDiff = getMinusSCEV(RA, LS);
    5050        2683 :       if (LDiff == RDiff)
    5051         129 :         return getAddExpr(getUMinExpr(LS, RS), LDiff);
    5052             :     }
    5053             :     break;
    5054        6513 :   case ICmpInst::ICMP_NE:
    5055             :     // n != 0 ? n+x : 1+x  ->  umax(n, 1)+x
    5056       12894 :     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
    5057        9363 :         isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
    5058        1772 :       const SCEV *One = getOne(I->getType());
    5059         886 :       const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
    5060         886 :       const SCEV *LA = getSCEV(TrueVal);
    5061         886 :       const SCEV *RA = getSCEV(FalseVal);
    5062         886 :       const SCEV *LDiff = getMinusSCEV(LA, LS);
    5063         886 :       const SCEV *RDiff = getMinusSCEV(RA, One);
    5064         886 :       if (LDiff == RDiff)
    5065           1 :         return getAddExpr(getUMaxExpr(One, LS), LDiff);
    5066             :     }
    5067             :     break;
    5068        3051 :   case ICmpInst::ICMP_EQ:
    5069             :     // n == 0 ? 1+x : n+x  ->  umax(n, 1)+x
    5070        6062 :     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
    5071        8226 :         isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
    5072        1898 :       const SCEV *One = getOne(I->getType());
    5073         949 :       const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
    5074         949 :       const SCEV *LA = getSCEV(TrueVal);
    5075         949 :       const SCEV *RA = getSCEV(FalseVal);
    5076         949 :       const SCEV *LDiff = getMinusSCEV(LA, One);
    5077         949 :       const SCEV *RDiff = getMinusSCEV(RA, LS);
    5078         949 :       if (LDiff == RDiff)
    5079          14 :         return getAddExpr(getUMaxExpr(One, LS), LDiff);
    5080             :     }
    5081             :     break;
    5082             :   default:
    5083             :     break;
    5084             :   }
    5085             : 
    5086       12381 :   return getUnknown(I);
    5087             : }
    5088             : 
    5089             : /// Expand GEP instructions into add and multiply operations. This allows them
    5090             : /// to be analyzed by regular SCEV code.
    5091       63589 : const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
    5092             :   // Don't attempt to analyze GEPs over unsized objects.
    5093       63589 :   if (!GEP->getSourceElementType()->isSized())
    5094           0 :     return getUnknown(GEP);
    5095             : 
    5096       63589 :   SmallVector<const SCEV *, 4> IndexExprs;
    5097      343750 :   for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index)
    5098      108286 :     IndexExprs.push_back(getSCEV(*Index));
    5099       63589 :   return getGEPExpr(GEP, IndexExprs);
    5100             : }
    5101             : 
    5102      368522 : uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
    5103       75234 :   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
    5104       75234 :     return C->getAPInt().countTrailingZeros();
    5105             : 
    5106        1180 :   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
    5107        2360 :     return std::min(GetMinTrailingZeros(T->getOperand()),
    5108        3540 :                     (uint32_t)getTypeSizeInBits(T->getType()));
    5109             : 
    5110       12040 :   if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
    5111       12040 :     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
    5112       12040 :     return OpRes == getTypeSizeInBits(E->getOperand()->getType())
    5113           0 :                ? getTypeSizeInBits(E->getType())
    5114       12040 :                : OpRes;
    5115             :   }
    5116             : 
    5117        7239 :   if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
    5118        7239 :     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
    5119        7239 :     return OpRes == getTypeSizeInBits(E->getOperand()->getType())
    5120           0 :                ? getTypeSizeInBits(E->getType())
    5121        7239 :                : OpRes;
    5122             :   }
    5123             : 
    5124       54604 :   if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
    5125             :     // The result is the min of all operands results.
    5126      109208 :     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
    5127       92696 :     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
    5128      114276 :       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
    5129             :     return MinOpRes;
    5130             :   }
    5131             : 
    5132       41908 :   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
    5133             :     // The result is the sum of all operands results.
    5134       83816 :     uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
    5135       83816 :     uint32_t BitWidth = getTypeSizeInBits(M->getType());
    5136       89810 :     for (unsigned i = 1, e = M->getNumOperands();
    5137       89810 :          SumOpRes != BitWidth && i != e; ++i)
    5138       47902 :       SumOpRes =
    5139      191608 :           std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth);
    5140             :     return SumOpRes;
    5141             :   }
    5142             : 
    5143       92189 :   if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
    5144             :     // The result is the min of all operands results.
    5145      184378 :     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
    5146      149706 :     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
    5147      172551 :       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
    5148             :     return MinOpRes;
    5149             :   }
    5150             : 
    5151        1680 :   if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
    5152             :     // The result is the min of all operands results.
    5153        3360 :     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
    5154        2773 :     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
    5155        3279 :       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
    5156             :     return MinOpRes;
    5157             :   }
    5158             : 
    5159         476 :   if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
    5160             :     // The result is the min of all operands results.
    5161         952 :     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
    5162         527 :     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
    5163         153 :       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
    5164             :     return MinOpRes;
    5165             :   }
    5166             : 
    5167       79969 :   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
    5168             :     // For a SCEVUnknown, ask ValueTracking.
    5169      319876 :     KnownBits Known = computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT);
    5170       79969 :     return Known.countMinTrailingZeros();
    5171             :   }
    5172             : 
    5173             :   // SCEVUDivExpr
    5174             :   return 0;
    5175             : }
    5176             : 
    5177      895602 : uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
    5178      895602 :   auto I = MinTrailingZerosCache.find(S);
    5179     2686806 :   if (I != MinTrailingZerosCache.end())
    5180      527080 :     return I->second;
    5181             : 
    5182      368522 :   uint32_t Result = GetMinTrailingZerosImpl(S);
    5183     1105566 :   auto InsertPair = MinTrailingZerosCache.insert({S, Result});
    5184             :   assert(InsertPair.second && "Should insert a new key");
    5185      368522 :   return InsertPair.first->second;
    5186             : }
    5187             : 
    5188             : /// Helper method to assign a range to V from metadata present in the IR.
    5189      151367 : static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
    5190       80986 :   if (Instruction *I = dyn_cast<Instruction>(V))
    5191       55303 :     if (MDNode *MD = I->getMetadata(LLVMContext::MD_range))
    5192       20132 :       return getConstantRangeFromMetadata(*MD);
    5193             : 
    5194             :   return None;
    5195             : }
    5196             : 
    5197             : /// Determine the range for a particular SCEV.  If SignHint is
    5198             : /// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
    5199             : /// with a "cleaner" unsigned (resp. signed) representation.
    5200             : const ConstantRange &
    5201     5120555 : ScalarEvolution::getRangeRef(const SCEV *S,
    5202             :                              ScalarEvolution::RangeSignHint SignHint) {
    5203     5120555 :   DenseMap<const SCEV *, ConstantRange> &Cache =
    5204             :       SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
    5205             :                                                        : SignedRanges;
    5206             : 
    5207             :   // See if we've computed this range already.
    5208     5120555 :   DenseMap<const SCEV *, ConstantRange>::iterator I = Cache.find(S);
    5209    10241110 :   if (I != Cache.end())
    5210     4228748 :     return I->second;
    5211             : 
    5212      355256 :   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
    5213     1065768 :     return setRange(C, SignHint, ConstantRange(C->getAPInt()));
    5214             : 
    5215      536551 :   unsigned BitWidth = getTypeSizeInBits(S->getType());
    5216     1073102 :   ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
    5217             : 
    5218             :   // If the value has known zeros, the maximum value will have those known zeros
    5219             :   // as well.
    5220      536551 :   uint32_t TZ = GetMinTrailingZeros(S);
    5221      536551 :   if (TZ != 0) {
    5222      153077 :     if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
    5223       73828 :       ConservativeResult =
    5224      221484 :           ConstantRange(APInt::getMinValue(BitWidth),
    5225      442968 :                         APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
    5226             :     else
    5227       79249 :       ConservativeResult = ConstantRange(
    5228      158498 :           APInt::getSignedMinValue(BitWidth),
    5229      475494 :           APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
    5230             :   }
    5231             : 
    5232       92817 :   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
    5233      278451 :     ConstantRange X = getRangeRef(Add->getOperand(0), SignHint);
    5234      197232 :     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
    5235      208830 :       X = X.add(getRangeRef(Add->getOperand(i), SignHint));
    5236       92817 :     return setRange(Add, SignHint, ConservativeResult.intersectWith(X));
    5237             :   }
    5238             : 
    5239       78021 :   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
    5240      234063 :     ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint);
    5241      171138 :     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
    5242      186234 :       X = X.multiply(getRangeRef(Mul->getOperand(i), SignHint));
    5243       78021 :     return setRange(Mul, SignHint, ConservativeResult.intersectWith(X));
    5244             :   }
    5245             : 
    5246        3360 :   if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
    5247       10080 :     ConstantRange X = getRangeRef(SMax->getOperand(0), SignHint);
    5248        7514 :     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
    5249        8308 :       X = X.smax(getRangeRef(SMax->getOperand(i), SignHint));
    5250        3360 :     return setRange(SMax, SignHint, ConservativeResult.intersectWith(X));
    5251             :   }
    5252             : 
    5253         952 :   if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
    5254        2856 :     ConstantRange X = getRangeRef(UMax->getOperand(0), SignHint);
    5255        1956 :     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
    5256        2008 :       X = X.umax(getRangeRef(UMax->getOperand(i), SignHint));
    5257         952 :     return setRange(UMax, SignHint, ConservativeResult.intersectWith(X));
    5258             :   }
    5259             : 
    5260        3541 :   if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
    5261        7082 :     ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint);
    5262        7082 :     ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint);
    5263             :     return setRange(UDiv, SignHint,
    5264        3541 :                     ConservativeResult.intersectWith(X.udiv(Y)));
    5265             :   }
    5266             : 
    5267       23181 :   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
    5268       46362 :     ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint);
    5269             :     return setRange(ZExt, SignHint,
    5270       23181 :                     ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
    5271             :   }
    5272             : 
    5273       14245 :   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
    5274       28490 :     ConstantRange X = getRangeRef(SExt->getOperand(), SignHint);
    5275             :     return setRange(SExt, SignHint,
    5276       14245 :                     ConservativeResult.intersectWith(X.signExtend(BitWidth)));
    5277             :   }
    5278             : 
    5279        2030 :   if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
    5280        4060 :     ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint);
    5281             :     return setRange(Trunc, SignHint,
    5282        2030 :                     ConservativeResult.intersectWith(X.truncate(BitWidth)));
    5283             :   }
    5284             : 
    5285      167037 :   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
    5286             :     // If there's no unsigned wrap, the value will never be less than its
    5287             :     // initial value.
    5288      334074 :     if (AddRec->hasNoUnsignedWrap())
    5289      111731 :       if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart()))
    5290      105722 :         if (!C->getValue()->isZero())
    5291       15848 :           ConservativeResult = ConservativeResult.intersectWith(
    5292       95088 :               ConstantRange(C->getAPInt(), APInt(BitWidth, 0)));
    5293             : 
    5294             :     // If there's no signed wrap, and all the operands have the same sign or
    5295             :     // zero, the value won't ever change sign.
    5296      334074 :     if (AddRec->hasNoSignedWrap()) {
    5297       58797 :       bool AllNonNeg = true;
    5298       58797 :       bool AllNonPos = true;
    5299      176391 :       for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
    5300      235188 :         if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
    5301      235188 :         if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
    5302             :       }
    5303       58797 :       if (AllNonNeg)
    5304       47985 :         ConservativeResult = ConservativeResult.intersectWith(
    5305      143955 :           ConstantRange(APInt(BitWidth, 0),
    5306       95970 :                         APInt::getSignedMinValue(BitWidth)));
    5307       10812 :       else if (AllNonPos)
    5308         226 :         ConservativeResult = ConservativeResult.intersectWith(
    5309         678 :           ConstantRange(APInt::getSignedMinValue(BitWidth),
    5310         452 :                         APInt(BitWidth, 1)));
    5311             :     }
    5312             : 
    5313             :     // TODO: non-affine addrec
    5314      167037 :     if (AddRec->isAffine()) {
    5315      165311 :       const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
    5316      451256 :       if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
    5317      120634 :           getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
    5318             :         auto RangeFromAffine = getRangeForAffineAR(
    5319             :             AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
    5320      356208 :             BitWidth);
    5321      118736 :         if (!RangeFromAffine.isFullSet())
    5322       86927 :           ConservativeResult =
    5323      173854 :               ConservativeResult.intersectWith(RangeFromAffine);
    5324             : 
    5325             :         auto RangeFromFactoring = getRangeViaFactoring(
    5326             :             AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
    5327      356208 :             BitWidth);
    5328      118736 :         if (!RangeFromFactoring.isFullSet())
    5329          62 :           ConservativeResult =
    5330         124 :               ConservativeResult.intersectWith(RangeFromFactoring);
    5331             :       }
    5332             :     }
    5333             : 
    5334      334074 :     return setRange(AddRec, SignHint, std::move(ConservativeResult));
    5335             :   }
    5336             : 
    5337      151367 :   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
    5338             :     // Check if the IR explicitly contains !range metadata.
    5339      302734 :     Optional<ConstantRange> MDRange = GetRangeFromMetadata(U->getValue());
    5340      151367 :     if (MDRange.hasValue())
    5341       10066 :       ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue());
    5342             : 
    5343             :     // Split here to avoid paying the compile-time cost of calling both
    5344             :     // computeKnownBits and ComputeNumSignBits.  This restriction can be lifted
    5345             :     // if needed.
    5346      302734 :     const DataLayout &DL = getDataLayout();
    5347      151367 :     if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
    5348             :       // For a SCEVUnknown, ask ValueTracking.
    5349      216438 :       KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
    5350      505022 :       if (Known.One != ~Known.Zero + 1)
    5351       25135 :         ConservativeResult =
    5352       75405 :             ConservativeResult.intersectWith(ConstantRange(Known.One,
    5353      150810 :                                                            ~Known.Zero + 1));
    5354             :     } else {
    5355             :       assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
    5356             :              "generalize as needed!");
    5357      158442 :       unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
    5358       79221 :       if (NS > 1)
    5359       18468 :         ConservativeResult = ConservativeResult.intersectWith(
    5360       73872 :             ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
    5361       92340 :                           APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1));
    5362             :     }
    5363             : 
    5364      302734 :     return setRange(U, SignHint, std::move(ConservativeResult));
    5365             :   }
    5366             : 
    5367           0 :   return setRange(S, SignHint, std::move(ConservativeResult));
    5368             : }
    5369             : 
    5370             : // Given a StartRange, Step and MaxBECount for an expression compute a range of
    5371             : // values that the expression can take. Initially, the expression has a value
    5372             : // from StartRange and then is changed by Step up to MaxBECount times. Signed
    5373             : // argument defines if we treat Step as signed or unsigned.
    5374      356580 : static ConstantRange getRangeForAffineARHelper(APInt Step,
    5375             :                                                const ConstantRange &StartRange,
    5376             :                                                const APInt &MaxBECount,
    5377             :                                                unsigned BitWidth, bool Signed) {
    5378             :   // If either Step or MaxBECount is 0, then the expression won't change, and we
    5379             :   // just need to return the initial range.
    5380      356580 :   if (Step == 0 || MaxBECount == 0)
    5381        9404 :     return StartRange;
    5382             : 
    5383             :   // If we don't know anything about the initial value (i.e. StartRange is
    5384             :   // FullRange), then we don't know anything about the final range either.
    5385             :   // Return FullRange.
    5386      347176 :   if (StartRange.isFullSet())
    5387       52939 :     return ConstantRange(BitWidth, /* isFullSet = */ true);
    5388             : 
    5389             :   // If Step is signed and negative, then we use its absolute value, but we also
    5390             :   // note that we're moving in the opposite direction.
    5391      490705 :   bool Descending = Signed && Step.isNegative();
    5392             : 
    5393      294237 :   if (Signed)
    5394             :     // This is correct even for INT_SMIN. Let's look at i8 to illustrate this:
    5395             :     // abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128.
    5396             :     // This equations hold true due to the well-defined wrap-around behavior of
    5397             :     // APInt.
    5398      589404 :     Step = Step.abs();
    5399             : 
    5400             :   // Check if Offset is more than full span of BitWidth. If it is, the
    5401             :   // expression is guaranteed to overflow.
    5402     1471185 :   if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount))
    5403       47532 :     return ConstantRange(BitWidth, /* isFullSet = */ true);
    5404             : 
    5405             :   // Offset is by how much the expression can change. Checks above guarantee no
    5406             :   // overflow here.
    5407      246705 :   APInt Offset = Step * MaxBECount;
    5408             : 
    5409             :   // Minimum value of the final range will match the minimal value of StartRange
    5410             :   // if the expression is increasing and will be decreased by Offset otherwise.
    5411             :   // Maximum value of the final range will match the maximal value of StartRange
    5412             :   // if the expression is decreasing and will be increased by Offset otherwise.
    5413      740115 :   APInt StartLower = StartRange.getLower();
    5414     1233525 :   APInt StartUpper = StartRange.getUpper() - 1;
    5415       46500 :   APInt MovedBoundary = Descending ? (StartLower - std::move(Offset))
    5416      539910 :                                    : (StartUpper + std::move(Offset));
    5417             : 
    5418             :   // It's possible that the new minimum/maximum value will fall into the initial
    5419             :   // range (due to wrap around). This means that the expression can take any
    5420             :   // value in this bitwidth, and we have to return full range.
    5421      246705 :   if (StartRange.contains(MovedBoundary))
    5422        6995 :     return ConstantRange(BitWidth, /* isFullSet = */ true);
    5423             : 
    5424             :   APInt NewLower =
    5425      479420 :       Descending ? std::move(MovedBoundary) : std::move(StartLower);
    5426             :   APInt NewUpper =
    5427      719130 :       Descending ? std::move(StartUpper) : std::move(MovedBoundary);
    5428      239710 :   NewUpper += 1;
    5429             : 
    5430             :   // If we end up with full range, return a proper full range.
    5431      239710 :   if (NewLower == NewUpper)
    5432        8833 :     return ConstantRange(BitWidth, /* isFullSet = */ true);
    5433             : 
    5434             :   // No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
    5435     1154385 :   return ConstantRange(std::move(NewLower), std::move(NewUpper));
    5436             : }
    5437             : 
    5438      118860 : ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start,
    5439             :                                                    const SCEV *Step,
    5440             :                                                    const SCEV *MaxBECount,
    5441             :                                                    unsigned BitWidth) {
    5442             :   assert(!isa<SCEVCouldNotCompute>(MaxBECount) &&
    5443             :          getTypeSizeInBits(MaxBECount->getType()) <= BitWidth &&
    5444             :          "Precondition!");
    5445             : 
    5446      118860 :   MaxBECount = getNoopOrZeroExtend(MaxBECount, Start->getType());
    5447      237720 :   APInt MaxBECountValue = getUnsignedRangeMax(MaxBECount);
    5448             : 
    5449             :   // First, consider step signed.
    5450      237720 :   ConstantRange StartSRange = getSignedRange(Start);
    5451      237720 :   ConstantRange StepSRange = getSignedRange(Step);
    5452             : 
    5453             :   // If Step can be both positive and negative, we need to find ranges for the
    5454             :   // maximum absolute step values in both directions and union them.
    5455             :   ConstantRange SR =
    5456      237720 :       getRangeForAffineARHelper(StepSRange.getSignedMin(), StartSRange,
    5457      237720 :                                 MaxBECountValue, BitWidth, /* Signed = */ true);
    5458      237720 :   SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(),
    5459             :                                               StartSRange, MaxBECountValue,
    5460             :                                               BitWidth, /* Signed = */ true));
    5461             : 
    5462             :   // Next, consider step unsigned.
    5463             :   ConstantRange UR = getRangeForAffineARHelper(
    5464      237720 :       getUnsignedRangeMax(Step), getUnsignedRange(Start),
    5465      237720 :       MaxBECountValue, BitWidth, /* Signed = */ false);
    5466             : 
    5467             :   // Finally, intersect signed and unsigned ranges.
    5468      237720 :   return SR.intersectWith(UR);
    5469             : }
    5470             : 
    5471      118736 : ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
    5472             :                                                     const SCEV *Step,
    5473             :                                                     const SCEV *MaxBECount,
    5474             :                                                     unsigned BitWidth) {
    5475             :   //    RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
    5476             :   // == RangeOf({A,+,P}) union RangeOf({B,+,Q})
    5477             : 
    5478      356409 :   struct SelectPattern {
    5479             :     Value *Condition = nullptr;
    5480             :     APInt TrueValue;
    5481             :     APInt FalseValue;
    5482             : 
    5483      118803 :     explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth,
    5484      356409 :                            const SCEV *S) {
    5485      118932 :       Optional<unsigned> CastOp;
    5486      118932 :       APInt Offset(BitWidth, 0);
    5487             : 
    5488             :       assert(SE.getTypeSizeInBits(S->getType()) == BitWidth &&
    5489             :              "Should be!");
    5490             : 
    5491             :       // Peel off a constant offset:
    5492       11759 :       if (auto *SA = dyn_cast<SCEVAddExpr>(S)) {
    5493             :         // In the future we could consider being smarter here and handle
    5494             :         // {Start+Step,+,Step} too.
    5495       33879 :         if (SA->getNumOperands() != 2 || !isa<SCEVConstant>(SA->getOperand(0)))
    5496      118674 :           return;
    5497             : 
    5498       29709 :         Offset = cast<SCEVConstant>(SA->getOperand(0))->getAPInt();
    5499       19806 :         S = SA->getOperand(1);
    5500             :       }
    5501             : 
    5502             :       // Peel off a cast operation
    5503        1624 :       if (auto *SCast = dyn_cast<SCEVCastExpr>(S)) {
    5504        3248 :         CastOp = SCast->getSCEVType();
    5505        1624 :         S = SCast->getOperand();
    5506             :       }
    5507             : 
    5508             :       using namespace llvm::PatternMatch;
    5509             : 
    5510       16253 :       auto *SU = dyn_cast<SCEVUnknown>(S);
    5511             :       const APInt *TrueVal, *FalseVal;
    5512       32506 :       if (!SU ||
    5513      198212 :           !match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal),
    5514       16253 :                                           m_APInt(FalseVal)))) {
    5515      116818 :         Condition = nullptr;
    5516             :         return;
    5517             :       }
    5518             : 
    5519         129 :       TrueValue = *TrueVal;
    5520         129 :       FalseValue = *FalseVal;
    5521             : 
    5522             :       // Re-apply the cast we peeled off earlier
    5523         129 :       if (CastOp.hasValue())
    5524          32 :         switch (*CastOp) {
    5525           0 :         default:
    5526           0 :           llvm_unreachable("Unknown SCEV cast type!");
    5527             : 
    5528          16 :         case scTruncate:
    5529          48 :           TrueValue = TrueValue.trunc(BitWidth);
    5530          48 :           FalseValue = FalseValue.trunc(BitWidth);
    5531             :           break;
    5532           4 :         case scZeroExtend:
    5533          12 :           TrueValue = TrueValue.zext(BitWidth);
    5534          12 :           FalseValue = FalseValue.zext(BitWidth);
    5535             :           break;
    5536          12 :         case scSignExtend:
    5537          36 :           TrueValue = TrueValue.sext(BitWidth);
    5538          36 :           FalseValue = FalseValue.sext(BitWidth);
    5539             :           break;
    5540             :         }
    5541             : 
    5542             :       // Re-apply the constant offset we peeled off earlier
    5543         129 :       TrueValue += Offset;
    5544         129 :       FalseValue += Offset;
    5545             :     }
    5546             : 
    5547             :     bool isRecognized() { return Condition != nullptr; }
    5548             :   };
    5549             : 
    5550      237472 :   SelectPattern StartPattern(*this, BitWidth, Start);
    5551      118736 :   if (!StartPattern.isRecognized())
    5552      118669 :     return ConstantRange(BitWidth, /* isFullSet = */ true);
    5553             : 
    5554         134 :   SelectPattern StepPattern(*this, BitWidth, Step);
    5555          67 :   if (!StepPattern.isRecognized())
    5556           5 :     return ConstantRange(BitWidth, /* isFullSet = */ true);
    5557             : 
    5558          62 :   if (StartPattern.Condition != StepPattern.Condition) {
    5559             :     // We don't handle this case today; but we could, by considering four
    5560             :     // possibilities below instead of two. I'm not sure if there are cases where
    5561             :     // that will help over what getRange already does, though.
    5562           0 :     return ConstantRange(BitWidth, /* isFullSet = */ true);
    5563             :   }
    5564             : 
    5565             :   // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
    5566             :   // construct arbitrary general SCEV expressions here.  This function is called
    5567             :   // from deep in the call stack, and calling getSCEV (on a sext instruction,
    5568             :   // say) can end up caching a suboptimal value.
    5569             : 
    5570             :   // FIXME: without the explicit `this` receiver below, MSVC errors out with
    5571             :   // C2352 and C2512 (otherwise it isn't needed).
    5572             : 
    5573          62 :   const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue);
    5574          62 :   const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue);
    5575          62 :   const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue);
    5576          62 :   const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
    5577             : 
    5578             :   ConstantRange TrueRange =
    5579         124 :       this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth);
    5580             :   ConstantRange FalseRange =
    5581         124 :       this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth);
    5582             : 
    5583          62 :   return TrueRange.unionWith(FalseRange);
    5584             : }
    5585             : 
    5586       62978 : SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) {
    5587      125956 :   if (isa<ConstantExpr>(V)) return SCEV::FlagAnyWrap;
    5588      125944 :   const BinaryOperator *BinOp = cast<BinaryOperator>(V);
    5589             : 
    5590             :   // Return early if there are no flags to propagate to the SCEV.
    5591       62972 :   SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
    5592       62972 :   if (BinOp->hasNoUnsignedWrap())
    5593       19339 :     Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
    5594       62972 :   if (BinOp->hasNoSignedWrap())
    5595       31295 :     Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
    5596       62972 :   if (Flags == SCEV::FlagAnyWrap)
    5597             :     return SCEV::FlagAnyWrap;
    5598             : 
    5599       34025 :   return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap;
    5600             : }
    5601             : 
    5602       69331 : bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
    5603             :   // Here we check that I is in the header of the innermost loop containing I,
    5604             :   // since we only deal with instructions in the loop header. The actual loop we
    5605             :   // need to check later will come from an add recurrence, but getting that
    5606             :   // requires computing the SCEV of the operands, which can be expensive. This
    5607             :   // check we can do cheaply to rule out some cases early.
    5608      138662 :   Loop *InnermostContainingLoop = LI.getLoopFor(I->getParent());
    5609      135766 :   if (InnermostContainingLoop == nullptr ||
    5610      135766 :       InnermostContainingLoop->getHeader() != I->getParent())
    5611             :     return false;
    5612             : 
    5613             :   // Only proceed if we can prove that I does not yield poison.
    5614       35754 :   if (!programUndefinedIfFullPoison(I))
    5615             :     return false;
    5616             : 
    5617             :   // At this point we know that if I is executed, then it does not wrap
    5618             :   // according to at least one of NSW or NUW. If I is not executed, then we do
    5619             :   // not know if the calculation that I represents would wrap. Multiple
    5620             :   // instructions can map to the same SCEV. If we apply NSW or NUW from I to
    5621             :   // the SCEV, we must guarantee no wrapping for that SCEV also when it is
    5622             :   // derived from other instructions that map to the same SCEV. We cannot make
    5623             :   // that guarantee for cases where I is not executed. So we need to find the
    5624             :   // loop that I is considered in relation to and prove that I is executed for
    5625             :   // every iteration of that loop. That implies that the value that I
    5626             :   // calculates does not wrap anywhere in the loop, so then we can apply the
    5627             :   // flags to the SCEV.
    5628             :   //
    5629             :   // We check isLoopInvariant to disambiguate in case we are adding recurrences
    5630             :   // from different loops, so that we know which loop to prove that I is
    5631             :   // executed in.
    5632       24806 :   for (unsigned OpIndex = 0; OpIndex < I->getNumOperands(); ++OpIndex) {
    5633             :     // I could be an extractvalue from a call to an overflow intrinsic.
    5634             :     // TODO: We can do better here in some cases.
    5635       16612 :     if (!isSCEVable(I->getOperand(OpIndex)->getType()))
    5636             :       return false;
    5637       16610 :     const SCEV *Op = getSCEV(I->getOperand(OpIndex));
    5638             :     if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
    5639             :       bool AllOtherOpsLoopInvariant = true;
    5640       36478 :       for (unsigned OtherOpIndex = 0; OtherOpIndex < I->getNumOperands();
    5641             :            ++OtherOpIndex) {
    5642        9321 :         if (OtherOpIndex != OpIndex) {
    5643        9388 :           const SCEV *OtherOp = getSCEV(I->getOperand(OtherOpIndex));
    5644        4694 :           if (!isLoopInvariant(OtherOp, AddRec->getLoop())) {
    5645             :             AllOtherOpsLoopInvariant = false;
    5646             :             break;
    5647             :           }
    5648             :         }
    5649             :       }
    5650        9097 :       if (AllOtherOpsLoopInvariant &&
    5651        4403 :           isGuaranteedToExecuteForEveryIteration(I, AddRec->getLoop()))
    5652             :         return true;
    5653             :     }
    5654             :   }
    5655             :   return false;
    5656             : }
    5657             : 
    5658       35306 : bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) {
    5659             :   // If we know that \c I can never be poison period, then that's enough.
    5660       35306 :   if (isSCEVExprNeverPoison(I))
    5661             :     return true;
    5662             : 
    5663             :   // For an add recurrence specifically, we assume that infinite loops without
    5664             :   // side effects are undefined behavior, and then reason as follows:
    5665             :   //
    5666             :   // If the add recurrence is poison in any iteration, it is poison on all
    5667             :   // future iterations (since incrementing poison yields poison). If the result
    5668             :   // of the add recurrence is fed into the loop latch condition and the loop
    5669             :   // does not contain any throws or exiting blocks other than the latch, we now
    5670             :   // have the ability to "choose" whether the backedge is taken or not (by
    5671             :   // choosing a sufficiently evil value for the poison feeding into the branch)
    5672             :   // for every iteration including and after the one in which \p I first became
    5673             :   // poison.  There are two possibilities (let's call the iteration in which \p
    5674             :   // I first became poison as K):
    5675             :   //
    5676             :   //  1. In the set of iterations including and after K, the loop body executes
    5677             :   //     no side effects.  In this case executing the backege an infinte number
    5678             :   //     of times will yield undefined behavior.
    5679             :   //
    5680             :   //  2. In the set of iterations including and after K, the loop body executes
    5681             :   //     at least one side effect.  In this case, that specific instance of side
    5682             :   //     effect is control dependent on poison, which also yields undefined
    5683             :   //     behavior.
    5684             : 
    5685       34283 :   auto *ExitingBB = L->getExitingBlock();
    5686       34283 :   auto *LatchBB = L->getLoopLatch();
    5687       34283 :   if (!ExitingBB || !LatchBB || ExitingBB != LatchBB)
    5688             :     return false;
    5689             : 
    5690       21558 :   SmallPtrSet<const Instruction *, 16> Pushed;
    5691       43116 :   SmallVector<const Instruction *, 8> PoisonStack;
    5692             : 
    5693             :   // We start by assuming \c I, the post-inc add recurrence, is poison.  Only
    5694             :   // things that are known to be fully poison under that assumption go on the
    5695             :   // PoisonStack.
    5696       21558 :   Pushed.insert(I);
    5697       21558 :   PoisonStack.push_back(I);
    5698             : 
    5699       21558 :   bool LatchControlDependentOnPoison = false;
    5700       62285 :   while (!PoisonStack.empty() && !LatchControlDependentOnPoison) {
    5701       40727 :     const Instruction *Poison = PoisonStack.pop_back_val();
    5702             : 
    5703      228892 :     for (auto *PoisonUser : Poison->users()) {
    5704       61315 :       if (propagatesFullPoison(cast<Instruction>(PoisonUser))) {
    5705       19341 :         if (Pushed.insert(cast<Instruction>(PoisonUser)).second)
    5706       19339 :           PoisonStack.push_back(cast<Instruction>(PoisonUser));
    5707       15965 :       } else if (auto *BI = dyn_cast<BranchInst>(PoisonUser)) {
    5708             :         assert(BI->isConditional() && "Only possibility!");
    5709       15965 :         if (BI->getParent() == LatchBB) {
    5710             :           LatchControlDependentOnPoison = true;
    5711             :           break;
    5712             :         }
    5713             :       }
    5714             :     }
    5715             :   }
    5716             : 
    5717       37477 :   return LatchControlDependentOnPoison && loopHasNoAbnormalExits(L);
    5718             : }
    5719             : 
    5720             : ScalarEvolution::LoopProperties
    5721       16927 : ScalarEvolution::getLoopProperties(const Loop *L) {
    5722             :   using LoopProperties = ScalarEvolution::LoopProperties;
    5723             : 
    5724       16927 :   auto Itr = LoopPropertiesCache.find(L);
    5725       50781 :   if (Itr == LoopPropertiesCache.end()) {
    5726      177333 :     auto HasSideEffects = [](Instruction *I) {
    5727       18521 :       if (auto *SI = dyn_cast<StoreInst>(I))
    5728       18521 :         return !SI->isSimple();
    5729             : 
    5730      158812 :       return I->mayHaveSideEffects();
    5731             :     };
    5732             : 
    5733        8783 :     LoopProperties LP = {/* HasNoAbnormalExits */ true,
    5734             :                          /*HasNoSideEffects*/ true};
    5735             : 
    5736       54411 :     for (auto *BB : L->getBlocks())
    5737      235170 :       for (auto &I : *BB) {
    5738      177333 :         if (!isGuaranteedToTransferExecutionToSuccessor(&I))
    5739        2053 :           LP.HasNoAbnormalExits = false;
    5740      177333 :         if (HasSideEffects(&I))
    5741        3022 :           LP.HasNoSideEffects = false;
    5742      177333 :         if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects)
    5743             :           break; // We're already as pessimistic as we can get.
    5744             :       }
    5745             : 
    5746       26349 :     auto InsertPair = LoopPropertiesCache.insert({L, LP});
    5747             :     assert(InsertPair.second && "We just checked!");
    5748        8783 :     Itr = InsertPair.first;
    5749             :   }
    5750             : 
    5751       16927 :   return Itr->second;
    5752             : }
    5753             : 
    5754      371422 : const SCEV *ScalarEvolution::createSCEV(Value *V) {
    5755      371422 :   if (!isSCEVable(V->getType()))
    5756           0 :     return getUnknown(V);
    5757             : 
    5758      618491 :   if (Instruction *I = dyn_cast<Instruction>(V)) {
    5759             :     // Don't attempt to analyze instructions in blocks that aren't
    5760             :     // reachable. Such instructions don't matter, and they aren't required
    5761             :     // to obey basic rules for definitions dominating uses which this
    5762             :     // analysis depends on.
    5763      247069 :     if (!DT.isReachableFromEntry(I->getParent()))
    5764         155 :       return getUnknown(V);
    5765      202781 :   } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
    5766       78428 :     return getConstant(CI);
    5767       91850 :   else if (isa<ConstantPointerNull>(V))
    5768         956 :     return getZero(V->getType());
    5769       45447 :   else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
    5770           0 :     return GA->isInterposable() ? getUnknown(V) : getSCEV(GA->getAliasee());
    5771       90894 :   else if (!isa<ConstantExpr>(V))
    5772       43090 :     return getUnknown(V);
    5773             : 
    5774      498542 :   Operator *U = cast<Operator>(V);
    5775      440659 :   if (auto BO = MatchBinaryOp(U, DT)) {
    5776       59835 :     switch (BO->Opcode) {
    5777       41406 :     case Instruction::Add: {
    5778             :       // The simple thing to do would be to just call getSCEV on both operands
    5779             :       // and call getAddExpr with the result. However if we're looking at a
    5780             :       // bunch of things all added together, this can be quite inefficient,
    5781             :       // because it leads to N-1 getAddExpr calls for N ultimate operands.
    5782             :       // Instead, gather up all the operands and make a single getAddExpr call.
    5783             :       // LLVM IR canonical form means we need only traverse the left operands.
    5784       41406 :       SmallVector<const SCEV *, 4> AddOps;
    5785             :       do {
    5786       53894 :         if (BO->Op) {
    5787       53828 :           if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
    5788        3123 :             AddOps.push_back(OpSCEV);
    5789        3123 :             break;
    5790             :           }
    5791             : 
    5792             :           // If a NUW or NSW flag can be applied to the SCEV for this
    5793             :           // addition, then compute the SCEV for this addition by itself
    5794             :           // with a separate call to getAddExpr. We need to do that
    5795             :           // instead of pushing the operands of the addition onto AddOps,
    5796             :           // since the flags are only known to apply to this particular
    5797             :           // addition - they may not apply to other additions that can be
    5798             :           // formed with operands from AddOps.
    5799       50705 :           const SCEV *RHS = getSCEV(BO->RHS);
    5800       50705 :           SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
    5801       50705 :           if (Flags != SCEV::FlagAnyWrap) {
    5802        2744 :             const SCEV *LHS = getSCEV(BO->LHS);
    5803        2744 :             if (BO->Opcode == Instruction::Sub)
    5804           2 :               AddOps.push_back(getMinusSCEV(LHS, RHS, Flags));
    5805             :             else
    5806        2742 :               AddOps.push_back(getAddExpr(LHS, RHS, Flags));
    5807             :             break;
    5808             :           }
    5809             :         }
    5810             : 
    5811       48027 :         if (BO->Opcode == Instruction::Sub)
    5812         199 :           AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS)));
    5813             :         else
    5814       47828 :           AddOps.push_back(getSCEV(BO->RHS));
    5815             : 
    5816      108542 :         auto NewBO = MatchBinaryOp(BO->LHS, DT);
    5817       67451 :         if (!NewBO || (NewBO->Opcode != Instruction::Add &&
    5818        3595 :                        NewBO->Opcode != Instruction::Sub)) {
    5819       35539 :           AddOps.push_back(getSCEV(BO->LHS));
    5820             :           break;
    5821             :         }
    5822       12488 :         BO = NewBO;
    5823             :       } while (true);
    5824             : 
    5825       41406 :       return getAddExpr(AddOps);
    5826             :     }
    5827             : 
    5828        5808 :     case Instruction::Mul: {
    5829        5808 :       SmallVector<const SCEV *, 4> MulOps;
    5830             :       do {
    5831        6446 :         if (BO->Op) {
    5832        6442 :           if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
    5833         321 :             MulOps.push_back(OpSCEV);
    5834         321 :             break;
    5835             :           }
    5836             : 
    5837        6121 :           SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
    5838        6121 :           if (Flags != SCEV::FlagAnyWrap) {
    5839         133 :             MulOps.push_back(
    5840         399 :                 getMulExpr(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags));
    5841         133 :             break;
    5842             :           }
    5843             :         }
    5844             : 
    5845        5992 :         MulOps.push_back(getSCEV(BO->RHS));
    5846       12622 :         auto NewBO = MatchBinaryOp(BO->LHS, DT);
    5847        7859 :         if (!NewBO || NewBO->Opcode != Instruction::Mul) {
    5848        5354 :           MulOps.push_back(getSCEV(BO->LHS));
    5849             :           break;
    5850             :         }
    5851         638 :         BO = NewBO;
    5852             :       } while (true);
    5853             : 
    5854        5808 :       return getMulExpr(MulOps);
    5855             :     }
    5856        2189 :     case Instruction::UDiv:
    5857        4378 :       return getUDivExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
    5858         231 :     case Instruction::URem:
    5859         462 :       return getURemExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
    5860        4207 :     case Instruction::Sub: {
    5861        4207 :       SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
    5862        4207 :       if (BO->Op)
    5863        4183 :         Flags = getNoWrapFlagsFromUB(BO->Op);
    5864        8414 :       return getMinusSCEV(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags);
    5865             :     }
    5866        1594 :     case Instruction::And:
    5867             :       // For an expression like x&255 that merely masks off the high bits,
    5868             :       // use zext(trunc(x)) as the SCEV expression.
    5869        2921 :       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
    5870        1327 :         if (CI->isZero())
    5871        1277 :           return getSCEV(BO->RHS);
    5872        1325 :         if (CI->isMinusOne())
    5873           0 :           return getSCEV(BO->LHS);
    5874        1325 :         const APInt &A = CI->getValue();
    5875             : 
    5876             :         // Instcombine's ShrinkDemandedConstant may strip bits out of
    5877             :         // constants, obscuring what would otherwise be a low-bits mask.
    5878             :         // Use computeKnownBits to compute what ShrinkDemandedConstant
    5879             :         // knew about to reconstruct a low-bits mask value.
    5880        1325 :         unsigned LZ = A.countLeadingZeros();
    5881        1325 :         unsigned TZ = A.countTrailingZeros();
    5882        1325 :         unsigned BitWidth = A.getBitWidth();
    5883        1377 :         KnownBits Known(BitWidth);
    5884        3975 :         computeKnownBits(BO->LHS, Known, getDataLayout(),
    5885        1325 :                          0, &AC, nullptr, &DT);
    5886             : 
    5887             :         APInt EffectiveMask =
    5888        2702 :             APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ);
    5889       18550 :         if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) {
    5890        2546 :           const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ));
    5891        1273 :           const SCEV *LHS = getSCEV(BO->LHS);
    5892        1273 :           const SCEV *ShiftedLHS = nullptr;
    5893          25 :           if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) {
    5894          75 :             if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) {
    5895             :               // For an expression like (x * 8) & 8, simplify the multiply.
    5896          25 :               unsigned MulZeros = OpC->getAPInt().countTrailingZeros();
    5897          25 :               unsigned GCD = std::min(MulZeros, TZ);
    5898          50 :               APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD);
    5899          50 :               SmallVector<const SCEV*, 4> MulOps;
    5900          50 :               MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD)));
    5901          50 :               MulOps.append(LHSMul->op_begin() + 1, LHSMul->op_end());
    5902          50 :               auto *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags());
    5903          25 :               ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt));
    5904             :             }
    5905             :           }
    5906          25 :           if (!ShiftedLHS)
    5907        1248 :             ShiftedLHS = getUDivExpr(LHS, MulCount);
    5908        2546 :           return getMulExpr(
    5909             :               getZeroExtendExpr(
    5910             :                   getTruncateExpr(ShiftedLHS,
    5911        2546 :                       IntegerType::get(getContext(), BitWidth - LZ - TZ)),
    5912        1273 :                   BO->LHS->getType()),
    5913        2546 :               MulCount);
    5914             :         }
    5915             :       }
    5916             :       break;
    5917             : 
    5918         927 :     case Instruction::Or:
    5919             :       // If the RHS of the Or is a constant, we may have something like:
    5920             :       // X*4+1 which got turned into X*4|1.  Handle this as an Add so loop
    5921             :       // optimizations will transparently handle this case.
    5922             :       //
    5923             :       // In order for this transformation to be safe, the LHS must be of the
    5924             :       // form X*(2^n) and the Or constant must be less than 2^n.
    5925        1534 :       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
    5926         607 :         const SCEV *LHS = getSCEV(BO->LHS);
    5927         607 :         const APInt &CIVal = CI->getValue();
    5928        1214 :         if (GetMinTrailingZeros(LHS) >=
    5929         607 :             (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
    5930             :           // Build a plain add SCEV.
    5931         535 :           const SCEV *S = getAddExpr(LHS, getSCEV(CI));
    5932             :           // If the LHS of the add was an addrec and it has no-wrap flags,
    5933             :           // transfer the no-wrap flags, since an or won't introduce a wrap.
    5934         350 :           if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
    5935         350 :             const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
    5936         700 :             const_cast<SCEVAddRecExpr *>(NewAR)->setNoWrapFlags(
    5937             :                 OldAR->getNoWrapFlags());
    5938             :           }
    5939             :           return S;
    5940             :         }
    5941             :       }
    5942             :       break;
    5943             : 
    5944         256 :     case Instruction::Xor:
    5945         348 :       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
    5946             :         // If the RHS of xor is -1, then this is a not operation.
    5947          92 :         if (CI->isMinusOne())
    5948          44 :           return getNotSCEV(getSCEV(BO->LHS));
    5949             : 
    5950             :         // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
    5951             :         // This is a variant of the check for xor with -1, and it handles
    5952             :         // the case where instcombine has trimmed non-demanded bits out
    5953             :         // of an xor with -1.
    5954          58 :         if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS))
    5955          15 :           if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1)))
    5956           9 :             if (LBO->getOpcode() == Instruction::And &&
    5957          12 :                 LCI->getValue() == CI->getValue())
    5958             :               if (const SCEVZeroExtendExpr *Z =
    5959           6 :                       dyn_cast<SCEVZeroExtendExpr>(getSCEV(BO->LHS))) {
    5960           2 :                 Type *UTy = BO->LHS->getType();
    5961           2 :                 const SCEV *Z0 = Z->getOperand();
    5962           2 :                 Type *Z0Ty = Z0->getType();
    5963           2 :                 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
    5964             : 
    5965             :                 // If C is a low-bits mask, the zero extend is serving to
    5966             :                 // mask off the high bits. Complement the operand and
    5967             :                 // re-apply the zext.
    5968           2 :                 if (CI->getValue().isMask(Z0TySize))
    5969           4 :                   return getZeroExtendExpr(getNotSCEV(Z0), UTy);
    5970             : 
    5971             :                 // If C is a single bit, it may be in the sign-bit position
    5972             :                 // before the zero-extend. In this case, represent the xor
    5973             :                 // using an add, which is equivalent, and re-apply the zext.
    5974           0 :                 APInt Trunc = CI->getValue().trunc(Z0TySize);
    5975           0 :                 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
    5976           0 :                     Trunc.isSignMask())
    5977           0 :                   return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
    5978           0 :                                            UTy);
    5979             :               }
    5980             :       }
    5981             :       break;
    5982             : 
    5983        2095 :   case Instruction::Shl:
    5984             :     // Turn shift left of a constant amount into a multiply.
    5985        4069 :     if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) {
    5986        5922 :       uint32_t BitWidth = cast<IntegerType>(SA->getType())->getBitWidth();
    5987             : 
    5988             :       // If the shift count is not less than the bitwidth, the result of
    5989             :       // the shift is undefined. Don't try to analyze it, because the
    5990             :       // resolution chosen here may differ from the resolution chosen in
    5991             :       // other parts of the compiler.
    5992        3948 :       if (SA->getValue().uge(BitWidth))
    5993             :         break;
    5994             : 
    5995             :       // It is currently not resolved how to interpret NSW for left
    5996             :       // shift by BitWidth - 1, so we avoid applying flags in that
    5997             :       // case. Remove this check (or this comment) once the situation
    5998             :       // is resolved. See
    5999             :       // http://lists.llvm.org/pipermail/llvm-dev/2015-April/084195.html
    6000             :       // and http://reviews.llvm.org/D8890 .
    6001        1970 :       auto Flags = SCEV::FlagAnyWrap;
    6002        1970 :       if (BO->Op && SA->getValue().ult(BitWidth - 1))
    6003        1969 :         Flags = getNoWrapFlagsFromUB(BO->Op);
    6004             : 
    6005        1970 :       Constant *X = ConstantInt::get(getContext(),
    6006        5910 :         APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
    6007        3940 :       return getMulExpr(getSCEV(BO->LHS), getSCEV(X), Flags);
    6008             :     }
    6009             :     break;
    6010             : 
    6011        1108 :     case Instruction::AShr: {
    6012             :       // AShr X, C, where C is a constant.
    6013        2212 :       ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS);
    6014             :       if (!CI)
    6015             :         break;
    6016             : 
    6017        1104 :       Type *OuterTy = BO->LHS->getType();
    6018        1104 :       uint64_t BitWidth = getTypeSizeInBits(OuterTy);
    6019             :       // If the shift count is not less than the bitwidth, the result of
    6020             :       // the shift is undefined. Don't try to analyze it, because the
    6021             :       // resolution chosen here may differ from the resolution chosen in
    6022             :       // other parts of the compiler.
    6023        2208 :       if (CI->getValue().uge(BitWidth))
    6024             :         break;
    6025             : 
    6026        1100 :       if (CI->isZero())
    6027           1 :         return getSCEV(BO->LHS); // shift by zero --> noop
    6028             : 
    6029        1099 :       uint64_t AShrAmt = CI->getZExtValue();
    6030        2198 :       Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt);
    6031             : 
    6032        2139 :       Operator *L = dyn_cast<Operator>(BO->LHS);
    6033        1040 :       if (L && L->getOpcode() == Instruction::Shl) {
    6034             :         // X = Shl A, n
    6035             :         // Y = AShr X, m
    6036             :         // Both n and m are constant.
    6037             : 
    6038         490 :         const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0));
    6039         735 :         if (L->getOperand(1) == BO->RHS)
    6040             :           // For a two-shift sext-inreg, i.e. n = m,
    6041             :           // use sext(trunc(x)) as the SCEV expression.
    6042         213 :           return getSignExtendExpr(
    6043         213 :               getTruncateExpr(ShlOp0SCEV, TruncTy), OuterTy);
    6044             : 
    6045          96 :         ConstantInt *ShlAmtCI = dyn_cast<ConstantInt>(L->getOperand(1));
    6046          32 :         if (ShlAmtCI && ShlAmtCI->getValue().ult(BitWidth)) {
    6047          32 :           uint64_t ShlAmt = ShlAmtCI->getZExtValue();
    6048          32 :           if (ShlAmt > AShrAmt) {
    6049             :             // When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV
    6050             :             // expression. We already checked that ShlAmt < BitWidth, so
    6051             :             // the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as
    6052             :             // ShlAmt - AShrAmt < Amt.
    6053             :             APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt,
    6054           4 :                                             ShlAmt - AShrAmt);
    6055           2 :             return getSignExtendExpr(
    6056             :                 getMulExpr(getTruncateExpr(ShlOp0SCEV, TruncTy),
    6057           2 :                 getConstant(Mul)), OuterTy);
    6058             :           }
    6059             :         }
    6060             :       }
    6061             :       break;
    6062             :     }
    6063             :     }
    6064             :   }
    6065             : 
    6066      382776 :   switch (U->getOpcode()) {
    6067        2426 :   case Instruction::Trunc:
    6068        4852 :     return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
    6069             : 
    6070        1849 :   case Instruction::ZExt:
    6071        3698 :     return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
    6072             : 
    6073        8927 :   case Instruction::SExt:
    6074       26769 :     if (auto BO = MatchBinaryOp(U->getOperand(0), DT)) {
    6075             :       // The NSW flag of a subtract does not always survive the conversion to
    6076             :       // A + (-1)*B.  By pushing sign extension onto its operands we are much
    6077             :       // more likely to preserve NSW and allow later AddRec optimisations.
    6078             :       //
    6079             :       // NOTE: This is effectively duplicating this logic from getSignExtend:
    6080             :       //   sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
    6081             :       // but by that point the NSW information has potentially been lost.
    6082         640 :       if (BO->Opcode == Instruction::Sub && BO->IsNSW) {
    6083          12 :         Type *Ty = U->getType();
    6084          12 :         auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty);
    6085          12 :         auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty);
    6086          24 :         return getMinusSCEV(V1, V2, SCEV::FlagNSW);
    6087             :       }
    6088        8927 :     }
    6089       17830 :     return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
    6090             : 
    6091        6753 :   case Instruction::BitCast:
    6092             :     // BitCasts are no-op casts so we just eliminate the cast.
    6093       13506 :     if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
    6094       13278 :       return getSCEV(U->getOperand(0));
    6095             :     break;
    6096             : 
    6097             :   // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can
    6098             :   // lead to pointer expressions which cannot safely be expanded to GEPs,
    6099             :   // because ScalarEvolution doesn't respect the GEP aliasing rules when
    6100             :   // simplifying integer expressions.
    6101             : 
    6102       63589 :   case Instruction::GetElementPtr:
    6103      127178 :     return createNodeForGEP(cast<GEPOperator>(U));
    6104             : 
    6105       46601 :   case Instruction::PHI:
    6106       93202 :     return createNodeForPHI(cast<PHINode>(U));
    6107             : 
    6108       12287 :   case Instruction::Select:
    6109             :     // U can also be a select constant expr, which let fall through.  Since
    6110             :     // createNodeForSelect only works for a condition that is an `ICmpInst`, and
    6111             :     // constant expressions cannot have instructions as operands, we'd have
    6112             :     // returned getUnknown for a select constant expressions anyway.
    6113       24574 :     if (isa<Instruction>(U))
    6114       61425 :       return createNodeForSelectOrPHI(cast<Instruction>(U), U->getOperand(0),
    6115       12285 :                                       U->getOperand(1), U->getOperand(2));
    6116             :     break;
    6117             : 
    6118        9094 :   case Instruction::Call:
    6119             :   case Instruction::Invoke:
    6120       18188 :     if (Value *RV = CallSite(U).getReturnedArgOperand())
    6121           2 :       return getSCEV(RV);
    6122             :     break;
    6123             :   }
    6124             : 
    6125       49070 :   return getUnknown(V);
    6126             : }
    6127             : 
    6128             : //===----------------------------------------------------------------------===//
    6129             : //                   Iteration Count Computation Code
    6130             : //
    6131             : 
    6132       11233 : static unsigned getConstantTripCount(const SCEVConstant *ExitCount) {
    6133       11233 :   if (!ExitCount)
    6134             :     return 0;
    6135             : 
    6136        4710 :   ConstantInt *ExitConst = ExitCount->getValue();
    6137             : 
    6138             :   // Guard against huge trip counts.
    6139        9420 :   if (ExitConst->getValue().getActiveBits() > 32)
    6140             :     return 0;
    6141             : 
    6142             :   // In case of integer overflow, this returns 0, which is correct.
    6143        3933 :   return ((unsigned)ExitConst->getZExtValue()) + 1;
    6144             : }
    6145             : 
    6146        1469 : unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) {
    6147        1469 :   if (BasicBlock *ExitingBB = L->getExitingBlock())
    6148        1469 :     return getSmallConstantTripCount(L, ExitingBB);
    6149             : 
    6150             :   // No trip count information for multiple exits.
    6151             :   return 0;
    6152             : }
    6153             : 
    6154        6695 : unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L,
    6155             :                                                     BasicBlock *ExitingBlock) {
    6156             :   assert(ExitingBlock && "Must pass a non-null exiting block!");
    6157             :   assert(L->isLoopExiting(ExitingBlock) &&
    6158             :          "Exiting block must actually branch out of the loop!");
    6159             :   const SCEVConstant *ExitCount =
    6160       13390 :       dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock));
    6161        6695 :   return getConstantTripCount(ExitCount);
    6162             : }
    6163             : 
    6164        4538 : unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) {
    6165             :   const auto *MaxExitCount =
    6166        9076 :       dyn_cast<SCEVConstant>(getMaxBackedgeTakenCount(L));
    6167        4538 :   return getConstantTripCount(MaxExitCount);
    6168             : }
    6169             : 
    6170         229 : unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) {
    6171         229 :   if (BasicBlock *ExitingBB = L->getExitingBlock())
    6172         228 :     return getSmallConstantTripMultiple(L, ExitingBB);
    6173             : 
    6174             :   // No trip multiple information for multiple exits.
    6175             :   return 0;
    6176             : }
    6177             : 
    6178             : /// Returns the largest constant divisor of the trip count of this loop as a
    6179             : /// normal unsigned value, if possible. This means that the actual trip count is
    6180             : /// always a multiple of the returned value (don't forget the trip count could
    6181             : /// very well be zero as well!).
    6182             : ///
    6183             : /// Returns 1 if the trip count is unknown or not guaranteed to be the
    6184             : /// multiple of a constant (which is also the case if the trip count is simply
    6185             : /// constant, use getSmallConstantTripCount for that case), Will also return 1
    6186             : /// if the trip count is very large (>= 2^32).
    6187             : ///
    6188             : /// As explained in the comments for getSmallConstantTripCount, this assumes
    6189             : /// that control exits the loop via ExitingBlock.
    6190             : unsigned
    6191        5449 : ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
    6192             :                                               BasicBlock *ExitingBlock) {
    6193             :   assert(ExitingBlock && "Must pass a non-null exiting block!");
    6194             :   assert(L->isLoopExiting(ExitingBlock) &&
    6195             :          "Exiting block must actually branch out of the loop!");
    6196        5449 :   const SCEV *ExitCount = getExitCount(L, ExitingBlock);
    6197        5449 :   if (ExitCount == getCouldNotCompute())
    6198             :     return 1;
    6199             : 
    6200             :   // Get the trip count from the BE count by adding 1.
    6201        6740 :   const SCEV *TCExpr = getAddExpr(ExitCount, getOne(ExitCount->getType()));
    6202             : 
    6203        2032 :   const SCEVConstant *TC = dyn_cast<SCEVConstant>(TCExpr);
    6204             :   if (!TC)
    6205             :     // Attempt to factor more general cases. Returns the greatest power of
    6206             :     // two divisor. If overflow happens, the trip count expression is still
    6207             :     // divisible by the greatest power of 2 divisor returned.
    6208        2676 :     return 1U << std::min((uint32_t)31, GetMinTrailingZeros(TCExpr));
    6209             : 
    6210        2032 :   ConstantInt *Result = TC->getValue();
    6211             : 
    6212             :   // Guard against huge trip counts (this requires checking
    6213             :   // for zero to handle the case where the trip count == -1 and the
    6214             :   // addition wraps).
    6215        8128 :   if (!Result || Result->getValue().getActiveBits() > 32 ||
    6216        2032 :       Result->getValue().getActiveBits() == 0)
    6217             :     return 1;
    6218             : 
    6219        2030 :   return (unsigned)Result->getZExtValue();
    6220             : }
    6221             : 
    6222             : /// Get the expression for the number of loop iterations for which this loop is
    6223             : /// guaranteed not to exit via ExitingBlock. Otherwise return
    6224             : /// SCEVCouldNotCompute.
    6225       12724 : const SCEV *ScalarEvolution::getExitCount(const Loop *L,
    6226             :                                           BasicBlock *ExitingBlock) {
    6227       12724 :   return getBackedgeTakenInfo(L).getExact(ExitingBlock, this);
    6228             : }
    6229             : 
    6230             : const SCEV *
    6231        3152 : ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L,
    6232             :                                                  SCEVUnionPredicate &Preds) {
    6233        3152 :   return getPredicatedBackedgeTakenInfo(L).getExact(this, &Preds);
    6234             : }
    6235             : 
    6236       35912 : const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
    6237       35912 :   return getBackedgeTakenInfo(L).getExact(this);
    6238             : }
    6239             : 
    6240             : /// Similar to getBackedgeTakenCount, except return the least SCEV value that is
    6241             : /// known never to be less than the actual backedge taken count.
    6242      195500 : const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
    6243      195500 :   return getBackedgeTakenInfo(L).getMax(this);
    6244             : }
    6245             : 
    6246        4050 : bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) {
    6247        4050 :   return getBackedgeTakenInfo(L).isMaxOrZero(this);
    6248             : }
    6249             : 
    6250             : /// Push PHI nodes in the header of the given loop onto the given Worklist.
    6251             : static void
    6252       18964 : PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
    6253       37928 :   BasicBlock *Header = L->getHeader();
    6254             : 
    6255             :   // Push all Loop-header PHIs onto the Worklist stack.
    6256       18964 :   for (BasicBlock::iterator I = Header->begin();
    6257       25256 :        PHINode *PN = dyn_cast<PHINode>(I); ++I)
    6258       25256 :     Worklist.push_back(PN);
    6259       18964 : }
    6260             : 
    6261             : const ScalarEvolution::BackedgeTakenInfo &
    6262        3152 : ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) {
    6263        3152 :   auto &BTI = getBackedgeTakenInfo(L);
    6264        3152 :   if (BTI.hasFullInfo())
    6265             :     return BTI;
    6266             : 
    6267        2940 :   auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
    6268             : 
    6269         490 :   if (!Pair.second)
    6270          12 :     return Pair.first->second;
    6271             : 
    6272             :   BackedgeTakenInfo Result =
    6273         478 :       computeBackedgeTakenCount(L, /*AllowPredicates=*/true);
    6274             : 
    6275         956 :   return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result);
    6276             : }
    6277             : 
    6278             : const ScalarEvolution::BackedgeTakenInfo &
    6279      265597 : ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
    6280             :   // Initially insert an invalid entry for this loop. If the insertion
    6281             :   // succeeds, proceed to actually compute a backedge-taken count and
    6282             :   // update the value. The temporary CouldNotCompute value tells SCEV
    6283             :   // code elsewhere that it shouldn't attempt to request a new
    6284             :   // backedge-taken count, which could result in infinite recursion.
    6285             :   std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
    6286     1593582 :       BackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
    6287      265597 :   if (!Pair.second)
    6288      246767 :     return Pair.first->second;
    6289             : 
    6290             :   // computeBackedgeTakenCount may allocate memory for its result. Inserting it
    6291             :   // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
    6292             :   // must be cleared in this scope.
    6293       18830 :   BackedgeTakenInfo Result = computeBackedgeTakenCount(L);
    6294             : 
    6295       18830 :   if (Result.getExact(this) != getCouldNotCompute()) {
    6296             :     assert(isLoopInvariant(Result.getExact(this), L) &&
    6297             :            isLoopInvariant(Result.getMax(this), L) &&
    6298             :            "Computed backedge-taken count isn't loop invariant for loop!");
    6299             :     ++NumTripCountsComputed;
    6300             :   }
    6301        7739 :   else if (Result.getMax(this) == getCouldNotCompute() &&
    6302             :            isa<PHINode>(L->getHeader()->begin())) {
    6303             :     // Only count loops that have phi nodes as not being computable.
    6304             :     ++NumTripCountsNotComputed;
    6305             :   }
    6306             : 
    6307             :   // Now that we know more about the trip count for this loop, forget any
    6308             :   // existing SCEV values for PHI nodes in this loop since they are only
    6309             :   // conservative estimates made without the benefit of trip count
    6310             :   // information. This is similar to the code in forgetLoop, except that
    6311             :   // it handles SCEVUnknown PHI nodes specially.
    6312             :   if (Result.hasAnyInfo()) {
    6313       28194 :     SmallVector<Instruction *, 16> Worklist;
    6314       14097 :     PushLoopPHIs(L, Worklist);
    6315             : 
    6316       14097 :     SmallPtrSet<Instruction *, 8> Visited;
    6317      353854 :     while (!Worklist.empty()) {
    6318      339757 :       Instruction *I = Worklist.pop_back_val();
    6319      339757 :       if (!Visited.insert(I).second)
    6320       63692 :         continue;
    6321             : 
    6322             :       ValueExprMapType::iterator It =
    6323      276065 :         ValueExprMap.find_as(static_cast<Value *>(I));
    6324      828195 :       if (It != ValueExprMap.end()) {
    6325       33518 :         const SCEV *Old = It->second;
    6326             : 
    6327             :         // SCEVUnknown for a PHI either means that it has an unrecognized
    6328             :         // structure, or it's a PHI that's in the progress of being computed
    6329             :         // by createNodeForPHI.  In the former case, additional loop trip
    6330             :         // count information isn't going to change anything. In the later
    6331             :         // case, createNodeForPHI will perform the necessary updates on its
    6332             :         // own when it gets to that point.
    6333       83579 :         if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) {
    6334       63252 :           eraseValueFromMap(It->first);
    6335       31626 :           forgetMemoizedResults(Old, false);
    6336             :         }
    6337       67036 :         if (PHINode *PN = dyn_cast<PHINode>(I))
    6338       16543 :           ConstantEvolutionLoopExitValue.erase(PN);
    6339             :       }
    6340             : 
    6341      276065 :       PushDefUseChildren(I, Worklist);
    6342             :     }
    6343             :   }
    6344             : 
    6345             :   // Re-lookup the insert position, since the call to
    6346             :   // computeBackedgeTakenCount above could result in a
    6347             :   // recusive call to getBackedgeTakenInfo (on a different
    6348             :   // loop), which would invalidate the iterator computed
    6349             :   // earlier.
    6350       37660 :   return BackedgeTakenCounts.find(L)->second = std::move(Result);
    6351             : }
    6352             : 
    6353        4084 : void ScalarEvolution::forgetLoop(const Loop *L) {
    6354             :   // Drop any stored trip count value.
    6355             :   auto RemoveLoopFromBackedgeMap =
    6356        9734 :       [](DenseMap<const Loop *, BackedgeTakenInfo> &Map, const Loop *L) {
    6357        9734 :         auto BTCPos = Map.find(L);
    6358       19468 :         if (BTCPos != Map.end()) {
    6359        1526 :           BTCPos->second.clear();
    6360        1526 :           Map.erase(BTCPos);
    6361             :         }
    6362        9734 :       };
    6363             : 
    6364        8168 :   SmallVector<const Loop *, 16> LoopWorklist(1, L);
    6365        8168 :   SmallVector<Instruction *, 32> Worklist;
    6366        4084 :   SmallPtrSet<Instruction *, 16> Visited;
    6367             : 
    6368             :   // Iterate over all the loops and sub-loops to drop SCEV information.
    6369       13818 :   while (!LoopWorklist.empty()) {
    6370        4867 :     auto *CurrL = LoopWorklist.pop_back_val();
    6371             : 
    6372        4867 :     RemoveLoopFromBackedgeMap(BackedgeTakenCounts, CurrL);
    6373        4867 :     RemoveLoopFromBackedgeMap(PredicatedBackedgeTakenCounts, CurrL);
    6374             : 
    6375             :     // Drop information about predicated SCEV rewrites for this loop.
    6376        4921 :     for (auto I = PredicatedSCEVRewrites.begin();
    6377       14763 :          I != PredicatedSCEVRewrites.end();) {
    6378         108 :       std::pair<const SCEV *, const Loop *> Entry = I->first;
    6379          54 :       if (Entry.second == CurrL)
    6380         104 :         PredicatedSCEVRewrites.erase(I++);
    6381             :       else
    6382           2 :         ++I;
    6383             :     }
    6384             : 
    6385             :     // Drop information about expressions based on loop-header PHIs.
    6386        4867 :     PushLoopPHIs(CurrL, Worklist);
    6387             : 
    6388      115250 :     while (!Worklist.empty()) {
    6389      110383 :       Instruction *I = Worklist.pop_back_val();
    6390      110383 :       if (!Visited.insert(I).second)
    6391       19536 :         continue;
    6392             : 
    6393             :       ValueExprMapType::iterator It =
    6394       90847 :           ValueExprMap.find_as(static_cast<Value *>(I));
    6395      272541 :       if (It != ValueExprMap.end()) {
    6396        7458 :         eraseValueFromMap(It->first);
    6397        3729 :         forgetMemoizedResults(It->second);
    6398        3729 :         if (PHINode *PN = dyn_cast<PHINode>(I))
    6399        1308 :           ConstantEvolutionLoopExitValue.erase(PN);
    6400             :       }
    6401             : 
    6402       90847 :       PushDefUseChildren(I, Worklist);
    6403             :     }
    6404             : 
    6405       23425 :     for (auto I = ExitLimits.begin(); I != ExitLimits.end(); ++I) {
    6406        4412 :       auto &Query = I->first;
    6407        4412 :       if (Query.L == CurrL)
    6408        1820 :         ExitLimits.erase(I);
    6409             :     }
    6410             : 
    6411        4867 :     LoopPropertiesCache.erase(CurrL);
    6412             :     // Forget all contained loops too, to avoid dangling entries in the
    6413             :     // ValuesAtScopes map.
    6414       14601 :     LoopWorklist.append(CurrL->begin(), CurrL->end());
    6415             :   }
    6416        4084 : }
    6417             : 
    6418        8829 : void ScalarEvolution::forgetValue(Value *V) {
    6419        8829 :   Instruction *I = dyn_cast<Instruction>(V);
    6420        8829 :   if (!I) return;
    6421             : 
    6422             :   // Drop information about expressions based on loop-header PHIs.
    6423       17658 :   SmallVector<Instruction *, 16> Worklist;
    6424        8829 :   Worklist.push_back(I);
    6425             : 
    6426        8829 :   SmallPtrSet<Instruction *, 8> Visited;
    6427      118429 :   while (!Worklist.empty()) {
    6428      109600 :     I = Worklist.pop_back_val();
    6429      109600 :     if (!Visited.insert(I).second)
    6430       15250 :       continue;
    6431             : 
    6432             :     ValueExprMapType::iterator It =
    6433       94350 :       ValueExprMap.find_as(static_cast<Value *>(I));
    6434      283050 :     if (It != ValueExprMap.end()) {
    6435       27396 :       eraseValueFromMap(It->first);
    6436       13698 :       forgetMemoizedResults(It->second);
    6437       27396 :       if (PHINode *PN = dyn_cast<PHINode>(I))
    6438        3840 :         ConstantEvolutionLoopExitValue.erase(PN);
    6439             :     }
    6440             : 
    6441       94350 :     PushDefUseChildren(I, Worklist);
    6442             :   }
    6443             : }
    6444             : 
    6445             : /// Get the exact loop backedge taken count considering all loop exits. A
    6446             : /// computable result can only be returned for loops with a single exit.
    6447             : /// Returning the minimum taken count among all exits is incorrect because one
    6448             : /// of the loop's exit limit's may have been skipped. howFarToZero assumes that
    6449             : /// the limit of each loop test is never skipped. This is a valid assumption as
    6450             : /// long as the loop exits via that test. For precise results, it is the
    6451             : /// caller's responsibility to specify the relevant loop exit using
    6452             : /// getExact(ExitingBlock, SE).
    6453             : const SCEV *
    6454       57894 : ScalarEvolution::BackedgeTakenInfo::getExact(ScalarEvolution *SE,
    6455             :                                              SCEVUnionPredicate *Preds) const {
    6456             :   // If any exits were not computable, the loop is not computable.
    6457       57894 :   if (!isComplete() || ExitNotTaken.empty())
    6458       18064 :     return SE->getCouldNotCompute();
    6459             : 
    6460             :   const SCEV *BECount = nullptr;
    6461      119690 :   for (auto &ENT : ExitNotTaken) {
    6462             :     assert(ENT.ExactNotTaken != SE->getCouldNotCompute() && "bad exit SCEV");
    6463             : 
    6464       40195 :     if (!BECount)
    6465       39830 :       BECount = ENT.ExactNotTaken;
    6466         365 :     else if (BECount != ENT.ExactNotTaken)
    6467         265 :       return SE->getCouldNotCompute();
    6468       39949 :     if (Preds && !ENT.hasAlwaysTruePredicate())
    6469          38 :       Preds->add(ENT.Predicate.get());
    6470             : 
    6471             :     assert((Preds || ENT.hasAlwaysTruePredicate()) &&
    6472             :            "Predicate should be always true!");
    6473             :   }
    6474             : 
    6475             :   assert(BECount && "Invalid not taken count for loop exit");
    6476             :   return BECount;
    6477             : }
    6478             : 
    6479             : /// Get the exact not taken count for this loop exit.
    6480             : const SCEV *
    6481       26983 : ScalarEvolution::BackedgeTakenInfo::getExact(BasicBlock *ExitingBlock,
    6482             :                                              ScalarEvolution *SE) const {
    6483       82201 :   for (auto &ENT : ExitNotTaken)
    6484       45526 :     if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate())
    6485       21511 :       return ENT.ExactNotTaken;
    6486             : 
    6487        5472 :   return SE->getCouldNotCompute();
    6488             : }
    6489             : 
    6490             : /// getMax - Get the max backedge taken count for the loop.
    6491             : const SCEV *
    6492      203239 : ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE) const {
    6493             :   auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
    6494             :     return !ENT.hasAlwaysTruePredicate();
    6495             :   };
    6496             : 
    6497      609717 :   if (any_of(ExitNotTaken, PredicateNotAlwaysTrue) || !getMax())
    6498       26353 :     return SE->getCouldNotCompute();
    6499             : 
    6500             :   assert((isa<SCEVCouldNotCompute>(getMax()) || isa<SCEVConstant>(getMax())) &&
    6501             :          "No point in having a non-constant max backedge taken count!");
    6502             :   return getMax();
    6503             : }
    6504             : 
    6505        4050 : bool ScalarEvolution::BackedgeTakenInfo::isMaxOrZero(ScalarEvolution *SE) const {
    6506             :   auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
    6507             :     return !ENT.hasAlwaysTruePredicate();
    6508             :   };
    6509        4068 :   return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue);
    6510             : }
    6511             : 
    6512       98498 : bool ScalarEvolution::BackedgeTakenInfo::hasOperand(const SCEV *S,
    6513             :                                                     ScalarEvolution *SE) const {
    6514      212121 :   if (getMax() && getMax() != SE->getCouldNotCompute() &&
    6515       49968 :       SE->hasOperand(getMax(), S))
    6516             :     return true;
    6517             : 
    6518      345320 :   for (auto &ENT : ExitNotTaken)
    6519      100302 :     if (ENT.ExactNotTaken != SE->getCouldNotCompute() &&
    6520       50151 :         SE->hasOperand(ENT.ExactNotTaken, S))
    6521             :       return true;
    6522             : 
    6523             :   return false;
    6524             : }
    6525             : 
    6526       45518 : ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E)
    6527       91036 :     : ExactNotTaken(E), MaxNotTaken(E) {
    6528             :   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
    6529             :           isa<SCEVConstant>(MaxNotTaken)) &&
    6530             :          "No point in having a non-constant max backedge taken count!");
    6531       45518 : }
    6532             : 
    6533        7320 : ScalarEvolution::ExitLimit::ExitLimit(
    6534             :     const SCEV *E, const SCEV *M, bool MaxOrZero,
    6535        7320 :     ArrayRef<const SmallPtrSetImpl<const SCEVPredicate *> *> PredSetList)
    6536       14640 :     : ExactNotTaken(E), MaxNotTaken(M), MaxOrZero(MaxOrZero) {
    6537             :   assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
    6538             :           !isa<SCEVCouldNotCompute>(MaxNotTaken)) &&
    6539             :          "Exact is not allowed to be less precise than Max");
    6540             :   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
    6541             :           isa<SCEVConstant>(MaxNotTaken)) &&
    6542             :          "No point in having a non-constant max backedge taken count!");
    6543       22143 :   for (auto *PredSet : PredSetList)
    6544        7513 :     for (auto *P : *PredSet)
    6545          10 :       addPredicate(P);
    6546        7320 : }
    6547             : 
    6548        7097 : ScalarEvolution::ExitLimit::ExitLimit(
    6549             :     const SCEV *E, const SCEV *M, bool MaxOrZero,
    6550        7097 :     const SmallPtrSetImpl<const SCEVPredicate *> &PredSet)
    6551       14194 :     : ExitLimit(E, M, MaxOrZero, {&PredSet}) {
    6552             :   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
    6553             :           isa<SCEVConstant>(MaxNotTaken)) &&
    6554             :          "No point in having a non-constant max backedge taken count!");
    6555        7097 : }
    6556             : 
    6557          20 : ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E, const SCEV *M,
    6558          20 :                                       bool MaxOrZero)
    6559          20 :     : ExitLimit(E, M, MaxOrZero, None) {
    6560             :   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
    6561             :           isa<SCEVConstant>(MaxNotTaken)) &&
    6562             :          "No point in having a non-constant max backedge taken count!");
    6563          20 : }
    6564             : 
    6565             : /// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
    6566             : /// computable exit into a persistent ExitNotTakenInfo array.
    6567       19308 : ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
    6568             :     SmallVectorImpl<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo>
    6569             :         &&ExitCounts,
    6570       19308 :     bool Complete, const SCEV *MaxCount, bool MaxOrZero)
    6571       57924 :     : MaxAndComplete(MaxCount, Complete), MaxOrZero(MaxOrZero) {
    6572             :   using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
    6573             : 
    6574       57924 :   ExitNotTaken.reserve(ExitCounts.size());
    6575       19308 :   std::transform(
    6576             :       ExitCounts.begin(), ExitCounts.end(), std::back_inserter(ExitNotTaken),
    6577       14287 :       [&](const EdgeExitInfo &EEI) {
    6578       14287 :         BasicBlock *ExitBB = EEI.first;
    6579       14287 :         const ExitLimit &EL = EEI.second;
    6580       28574 :         if (EL.Predicates.empty())
    6581       42831 :           return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, nullptr);
    6582             : 
    6583          30 :         std::unique_ptr<SCEVUnionPredicate> Predicate(new SCEVUnionPredicate);
    6584          20 :         for (auto *Pred : EL.Predicates)
    6585          10 :           Predicate->add(Pred);
    6586             : 
    6587          40 :         return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, std::move(Predicate));
    6588       77232 :       });
    6589             :   assert((isa<SCEVCouldNotCompute>(MaxCount) || isa<SCEVConstant>(MaxCount)) &&
    6590             :          "No point in having a non-constant max backedge taken count!");
    6591       19308 : }
    6592             : 
    6593             : /// Invalidate this result and free the ExitNotTakenInfo array.
    6594       19212 : void ScalarEvolution::BackedgeTakenInfo::clear() {
    6595       38424 :   ExitNotTaken.clear();
    6596       19212 : }
    6597             : 
    6598             : /// Compute the number of times the backedge of the specified loop will execute.
    6599             : ScalarEvolution::BackedgeTakenInfo
    6600       19308 : ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
    6601             :                                            bool AllowPredicates) {
    6602       38616 :   SmallVector<BasicBlock *, 8> ExitingBlocks;
    6603       19308 :   L->getExitingBlocks(ExitingBlocks);
    6604             : 
    6605             :   using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
    6606             : 
    6607       38616 :   SmallVector<EdgeExitInfo, 4> ExitCounts;
    6608       19308 :   bool CouldComputeBECount = true;
    6609       19308 :   BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
    6610       19308 :   const SCEV *MustExitMaxBECount = nullptr;
    6611       19308 :   const SCEV *MayExitMaxBECount = nullptr;
    6612       19308 :   bool MustExitMaxOrZero = false;
    6613             : 
    6614             :   // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
    6615             :   // and compute maxBECount.
    6616             :   // Do a union of all the predicates here.
    6617       82918 :   for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
    6618       88604 :     BasicBlock *ExitBB = ExitingBlocks[i];
    6619       88604 :     ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates);
    6620             : 
    6621             :     assert((AllowPredicates || EL.Predicates.empty()) &&
    6622             :            "Predicated exit limit when predicates are not allowed!");
    6623             : 
    6624             :     // 1. For each exit that can be computed, add an entry to ExitCounts.
    6625             :     // CouldComputeBECount is true only if all exits can be computed.
    6626       44302 :     if (EL.ExactNotTaken == getCouldNotCompute())
    6627             :       // We couldn't compute an exact value for this exit, so
    6628             :       // we won't be able to compute an exact value for the loop.
    6629             :       CouldComputeBECount = false;
    6630             :     else
    6631       14287 :       ExitCounts.emplace_back(ExitBB, EL);
    6632             : 
    6633             :     // 2. Derive the loop's MaxBECount from each exit's max number of
    6634             :     // non-exiting iterations. Partition the loop exits into two kinds:
    6635             :     // LoopMustExits and LoopMayExits.
    6636             :     //
    6637             :     // If the exit dominates the loop latch, it is a LoopMustExit otherwise it
    6638             :     // is a LoopMayExit.  If any computable LoopMustExit is found, then
    6639             :     // MaxBECount is the minimum EL.MaxNotTaken of computable
    6640             :     // LoopMustExits. Otherwise, MaxBECount is conservatively the maximum
    6641             :     // EL.MaxNotTaken, where CouldNotCompute is considered greater than any
    6642             :     // computable EL.MaxNotTaken.
    6643       58660 :     if (EL.MaxNotTaken != getCouldNotCompute() && Latch &&
    6644       14358 :         DT.dominates(ExitBB, Latch)) {
    6645       14358 :       if (!MustExitMaxBECount) {
    6646       14135 :         MustExitMaxBECount = EL.MaxNotTaken;
    6647       14135 :         MustExitMaxOrZero = EL.MaxOrZero;
    6648             :       } else {
    6649         223 :         MustExitMaxBECount =
    6650         223 :             getUMinFromMismatchedTypes(MustExitMaxBECount, EL.MaxNotTaken);
    6651             :       }
    6652       29944 :     } else if (MayExitMaxBECount != getCouldNotCompute()) {
    6653        7872 :       if (!MayExitMaxBECount || EL.MaxNotTaken == getCouldNotCompute())
    6654        7871 :         MayExitMaxBECount = EL.MaxNotTaken;
    6655             :       else {
    6656           1 :         MayExitMaxBECount =
    6657           1 :             getUMaxFromMismatchedTypes(MayExitMaxBECount, EL.MaxNotTaken);
    6658             :       }
    6659             :     }
    6660             :   }
    6661       19308 :   const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
    6662        5173 :     (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute());
    6663             :   // The loop backedge will be taken the maximum or zero times if there's
    6664             :   // a single exit that must be taken the maximum or zero times.
    6665       19328 :   bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1);
    6666       19308 :   return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount,
    6667       57924 :                            MaxBECount, MaxOrZero);
    6668             : }
    6669             : 
    6670             : ScalarEvolution::ExitLimit
    6671       44302 : ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
    6672             :                                   bool AllowPredicates) {
    6673       88604 :   ExitLimitQuery Query(L, ExitingBlock, AllowPredicates);
    6674       44302 :   auto MaybeEL = ExitLimits.find(Query);
    6675      132906 :   if (MaybeEL != ExitLimits.end())
    6676         240 :     return MaybeEL->second;
    6677       44062 :   ExitLimit EL = computeExitLimitImpl(L, ExitingBlock, AllowPredicates);
    6678      176248 :   ExitLimits.insert({Query, EL});
    6679       44062 :   return EL;
    6680             : }
    6681             : 
    6682             : ScalarEvolution::ExitLimit
    6683       44062 : ScalarEvolution::computeExitLimitImpl(const Loop *L, BasicBlock *ExitingBlock,
    6684             :                                       bool AllowPredicates) {
    6685             :   // Okay, we've chosen an exiting block.  See what condition causes us to exit
    6686             :   // at this block and remember the exit block and whether all other targets
    6687             :   // lead to the loop header.
    6688       44062 :   bool MustExecuteLoopHeader = true;
    6689       44062 :   BasicBlock *Exit = nullptr;
    6690      264684 :   for (auto *SBB : successors(ExitingBlock))
    6691      176658 :     if (!L->contains(SBB)) {
    6692       44160 :       if (Exit) // Multiple exit successors.
    6693          98 :         return getCouldNotCompute();
    6694             :       Exit = SBB;
    6695       88338 :     } else if (SBB != L->getHeader()) {
    6696       28897 :       MustExecuteLoopHeader = false;
    6697             :     }
    6698             : 
    6699             :   // At this point, we know we have a conditional branch that determines whether
    6700             :   // the loop is exited.  However, we don't know if the branch is executed each
    6701             :   // time through the loop.  If not, then the execution count of the branch will
    6702             :   // not be equal to the trip count of the loop.
    6703             :   //
    6704             :   // Currently we check for this by checking to see if the Exit branch goes to
    6705             :   // the loop header.  If so, we know it will always execute the same number of
    6706             :   // times as the loop.  We also handle the case where the exit block *is* the
    6707             :   // loop header.  This is common for un-rotated loops.
    6708             :   //
    6709             :   // If both of those tests fail, walk up the unique predecessor chain to the
    6710             :   // header, stopping if there is an edge that doesn't exit the loop. If the
    6711             :   // header is reached, the execution count of the branch will be equal to the
    6712             :   // trip count of the loop.
    6713             :   //
    6714             :   //  More extensive analysis could be done to handle more cases here.
    6715             :   //
    6716       72668 :   if (!MustExecuteLoopHeader && ExitingBlock != L->getHeader()) {
    6717             :     // The simple checks failed, try climbing the unique predecessor chain
    6718             :     // up to the header.
    6719             :     bool Ok = false;
    6720       49326 :     for (BasicBlock *BB = ExitingBlock; BB; ) {
    6721       49326 :       BasicBlock *Pred = BB->getUniquePredecessor();
    6722       49326 :       if (!Pred)
    6723        9639 :         return getCouldNotCompute();
    6724       39687 :       TerminatorInst *PredTerm = Pred->getTerminator();
    6725      213381 :       for (const BasicBlock *PredSucc : PredTerm->successors()) {
    6726       72320 :         if (PredSucc == BB)
    6727       32778 :           continue;
    6728             :         // If the predecessor has a successor that isn't BB and isn't
    6729             :         // outside the loop, assume the worst.
    6730       79084 :         if (L->contains(PredSucc))
    6731       10633 :           return getCouldNotCompute();
    6732             :       }
    6733       58108 :       if (Pred == L->getHeader()) {
    6734             :         Ok = true;
    6735             :         break;
    6736             :       }
    6737             :       BB = Pred;
    6738             :     }
    6739        2667 :     if (!Ok)
    6740           0 :       return getCouldNotCompute();
    6741             :   }
    6742             : 
    6743       23692 :   bool IsOnlyExit = (L->getExitingBlock() != nullptr);
    6744       23692 :   TerminatorInst *Term = ExitingBlock->getTerminator();
    6745       21242 :   if (BranchInst *BI = dyn_cast<BranchInst>(Term)) {
    6746             :     assert(BI->isConditional() && "If unconditional, it can't be in loop!");
    6747             :     // Proceed to the next level to examine the exit condition expression.
    6748             :     return computeExitLimitFromCond(
    6749             :         L, BI->getCondition(), BI->getSuccessor(0), BI->getSuccessor(1),
    6750       84968 :         /*ControlsExit=*/IsOnlyExit, AllowPredicates);
    6751             :   }
    6752             : 
    6753          20 :   if (SwitchInst *SI = dyn_cast<SwitchInst>(Term))
    6754             :     return computeExitLimitFromSingleExitSwitch(L, SI, Exit,
    6755          20 :                                                 /*ControlsExit=*/IsOnlyExit);
    6756             : 
    6757        2430 :   return getCouldNotCompute();
    6758             : }
    6759             : 
    6760       21242 : ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond(
    6761             :     const Loop *L, Value *ExitCond, BasicBlock *TBB, BasicBlock *FBB,
    6762             :     bool ControlsExit, bool AllowPredicates) {
    6763       63726 :   ScalarEvolution::ExitLimitCacheTy Cache(L, TBB, FBB, AllowPredicates);
    6764             :   return computeExitLimitFromCondCached(Cache, L, ExitCond, TBB, FBB,
    6765       42484 :                                         ControlsExit, AllowPredicates);
    6766             : }
    6767             : 
    6768             : Optional<ScalarEvolution::ExitLimit>
    6769       21648 : ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond,
    6770             :                                       BasicBlock *TBB, BasicBlock *FBB,
    6771             :                                       bool ControlsExit, bool AllowPredicates) {
    6772             :   (void)this->L;
    6773             :   (void)this->TBB;
    6774             :   (void)this->FBB;
    6775             :   (void)this->AllowPredicates;
    6776             : 
    6777             :   assert(this->L == L && this->TBB == TBB && this->FBB == FBB &&
    6778             :          this->AllowPredicates == AllowPredicates &&
    6779             :          "Variance in assumed invariant key components!");
    6780       43296 :   auto Itr = TripCountMap.find({ExitCond, ControlsExit});
    6781       64944 :   if (Itr == TripCountMap.end())
    6782             :     return None;
    6783          60 :   return Itr->second;
    6784             : }
    6785             : 
    6786       21588 : void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond,
    6787             :                                              BasicBlock *TBB, BasicBlock *FBB,
    6788             :                                              bool ControlsExit,
    6789             :                                              bool AllowPredicates,
    6790             :                                              const ExitLimit &EL) {
    6791             :   assert(this->L == L && this->TBB == TBB && this->FBB == FBB &&
    6792             :          this->AllowPredicates == AllowPredicates &&
    6793             :          "Variance in assumed invariant key components!");
    6794             : 
    6795      107940 :   auto InsertResult = TripCountMap.insert({{ExitCond, ControlsExit}, EL});
    6796             :   assert(InsertResult.second && "Expected successful insertion!");
    6797             :   (void)InsertResult;
    6798       21588 : }
    6799             : 
    6800       21648 : ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
    6801             :     ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, BasicBlock *TBB,
    6802             :     BasicBlock *FBB, bool ControlsExit, bool AllowPredicates) {
    6803             : 
    6804       21648 :   if (auto MaybeEL =
    6805       43236 :           Cache.find(L, ExitCond, TBB, FBB, ControlsExit, AllowPredicates))
    6806         180 :     return *MaybeEL;
    6807             : 
    6808             :   ExitLimit EL = computeExitLimitFromCondImpl(Cache, L, ExitCond, TBB, FBB,
    6809       21588 :                                               ControlsExit, AllowPredicates);
    6810       21588 :   Cache.insert(L, ExitCond, TBB, FBB, ControlsExit, AllowPredicates, EL);
    6811       21588 :   return EL;
    6812             : }
    6813             : 
    6814       21588 : ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
    6815             :     ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, BasicBlock *TBB,
    6816             :     BasicBlock *FBB, bool ControlsExit, bool AllowPredicates) {
    6817             :   // Check if the controlling expression for this loop is an And or Or.
    6818         204 :   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
    6819         204 :     if (BO->getOpcode() == Instruction::And) {
    6820             :       // Recurse on the operands of the and.
    6821         330 :       bool EitherMayExit = L->contains(TBB);
    6822             :       ExitLimit EL0 = computeExitLimitFromCondCached(
    6823         165 :           Cache, L, BO->getOperand(0), TBB, FBB, ControlsExit && !EitherMayExit,
    6824         660 :           AllowPredicates);
    6825             :       ExitLimit EL1 = computeExitLimitFromCondCached(
    6826             :           Cache, L, BO->getOperand(1), TBB, FBB, ControlsExit && !EitherMayExit,
    6827         495 :           AllowPredicates);
    6828         165 :       const SCEV *BECount = getCouldNotCompute();
    6829         165 :       const SCEV *MaxBECount = getCouldNotCompute();
    6830         165 :       if (EitherMayExit) {
    6831             :         // Both conditions must be true for the loop to continue executing.
    6832             :         // Choose the less conservative count.
    6833         134 :         if (EL0.ExactNotTaken == getCouldNotCompute() ||
    6834          36 :             EL1.ExactNotTaken == getCouldNotCompute())
    6835          95 :           BECount = getCouldNotCompute();
    6836             :         else
    6837           3 :           BECount =
    6838           3 :               getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
    6839          98 :         if (EL0.MaxNotTaken == getCouldNotCompute())
    6840          62 :           MaxBECount = EL1.MaxNotTaken;
    6841          36 :         else if (EL1.MaxNotTaken == getCouldNotCompute())
    6842          33 :           MaxBECount = EL0.MaxNotTaken;
    6843             :         else
    6844           3 :           MaxBECount =
    6845           3 :               getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
    6846             :       } else {
    6847             :         // Both conditions must be true at the same time for the loop to exit.
    6848             :         // For now, be conservative.
    6849             :         assert(L->contains(FBB) && "Loop block has no successor in loop!");
    6850          67 :         if (EL0.MaxNotTaken == EL1.MaxNotTaken)
    6851          64 :           MaxBECount = EL0.MaxNotTaken;
    6852          67 :         if (EL0.ExactNotTaken == EL1.ExactNotTaken)
    6853          65 :           BECount = EL0.ExactNotTaken;
    6854             :       }
    6855             : 
    6856             :       // There are cases (e.g. PR26207) where computeExitLimitFromCond is able
    6857             :       // to be more aggressive when computing BECount than when computing
    6858             :       // MaxBECount.  In these cases it is possible for EL0.ExactNotTaken and
    6859             :       // EL1.ExactNotTaken to match, but for EL0.MaxNotTaken and EL1.MaxNotTaken
    6860             :       // to not.
    6861         377 :       if (isa<SCEVCouldNotCompute>(MaxBECount) &&
    6862          47 :           !isa<SCEVCouldNotCompute>(BECount))
    6863           2 :         MaxBECount = getConstant(getUnsignedRangeMax(BECount));
    6864             : 
    6865             :       return ExitLimit(BECount, MaxBECount, false,
    6866         330 :                        {&EL0.Predicates, &EL1.Predicates});
    6867             :     }
    6868          39 :     if (BO->getOpcode() == Instruction::Or) {
    6869             :       // Recurse on the operands of the or.
    6870          76 :       bool EitherMayExit = L->contains(FBB);
    6871             :       ExitLimit EL0 = computeExitLimitFromCondCached(
    6872          38 :           Cache, L, BO->getOperand(0), TBB, FBB, ControlsExit && !EitherMayExit,
    6873         152 :           AllowPredicates);
    6874             :       ExitLimit EL1 = computeExitLimitFromCondCached(
    6875             :           Cache, L, BO->getOperand(1), TBB, FBB, ControlsExit && !EitherMayExit,
    6876         114 :           AllowPredicates);
    6877          38 :       const SCEV *BECount = getCouldNotCompute();
    6878          38 :       const SCEV *MaxBECount = getCouldNotCompute();
    6879          38 :       if (EitherMayExit) {
    6880             :         // Both conditions must be false for the loop to continue executing.
    6881             :         // Choose the less conservative count.
    6882          23 :         if (EL0.ExactNotTaken == getCouldNotCompute() ||
    6883           7 :             EL1.ExactNotTaken == getCouldNotCompute())
    6884          13 :           BECount = getCouldNotCompute();
    6885             :         else
    6886           3 :           BECount =
    6887           3 :               getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
    6888          16 :         if (EL0.MaxNotTaken == getCouldNotCompute())
    6889           9 :           MaxBECount = EL1.MaxNotTaken;
    6890           7 :         else if (EL1.MaxNotTaken == getCouldNotCompute())
    6891           4 :           MaxBECount = EL0.MaxNotTaken;
    6892             :         else
    6893           3 :           MaxBECount =
    6894           3 :               getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
    6895             :       } else {
    6896             :         // Both conditions must be false at the same time for the loop to exit.
    6897             :         // For now, be conservative.
    6898             :         assert(L->contains(TBB) && "Loop block has no successor in loop!");
    6899          22 :         if (EL0.MaxNotTaken == EL1.MaxNotTaken)
    6900          22 :           MaxBECount = EL0.MaxNotTaken;
    6901          22 :         if (EL0.ExactNotTaken == EL1.ExactNotTaken)
    6902          22 :           BECount = EL0.ExactNotTaken;
    6903             :       }
    6904             : 
    6905             :       return ExitLimit(BECount, MaxBECount, false,
    6906          76 :                        {&EL0.Predicates, &EL1.Predicates});
    6907             :     }
    6908             :   }
    6909             : 
    6910             :   // With an icmp, it may be feasible to compute an exact backedge-taken count.
    6911             :   // Proceed to the next level to examine the icmp.
    6912       20044 :   if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) {
    6913             :     ExitLimit EL =
    6914       40088 :         computeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, ControlsExit);
    6915       20044 :     if (EL.hasFullInfo() || !AllowPredicates)
    6916             :       return EL;
    6917             : 
    6918             :     // Try again, but use SCEV predicates this time.
    6919             :     return computeExitLimitFromICmp(L, ExitCondICmp, TBB, FBB, ControlsExit,
    6920         424 :                                     /*AllowPredicates=*/true);
    6921             :   }
    6922             : 
    6923             :   // Check for a constant condition. These are normally stripped out by
    6924             :   // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
    6925             :   // preserve the CFG and is temporarily leaving constant conditions
    6926             :   // in place.
    6927         697 :   if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
    6928        2091 :     if (L->contains(FBB) == !CI->getZExtValue())
    6929             :       // The backedge is always taken.
    6930         228 :       return getCouldNotCompute();
    6931             :     else
    6932             :       // The backedge is never taken.
    6933         938 :       return getZero(CI->getType());
    6934             :   }
    6935             : 
    6936             :   // If it's not an integer or pointer comparison then compute it the hard way.
    6937        1288 :   return computeExitCountExhaustively(L, ExitCond, !L->contains(TBB));
    6938             : }
    6939             : 
    6940             : ScalarEvolution::ExitLimit
    6941       20468 : ScalarEvolution::computeExitLimitFromICmp(const Loop *L,
    6942             :                                           ICmpInst *ExitCond,
    6943             :                                           BasicBlock *TBB,
    6944             :                                           BasicBlock *FBB,
    6945             :                                           bool ControlsExit,
    6946             :                                           bool AllowPredicates) {
    6947             :   // If the condition was exit on true, convert the condition to exit on false
    6948             :   ICmpInst::Predicate Cond;
    6949       40936 :   if (!L->contains(FBB))
    6950       24914 :     Cond = ExitCond->getPredicate();
    6951             :   else
    6952       16022 :     Cond = ExitCond->getInversePredicate();
    6953             : 
    6954             :   // Handle common loops like: for (X = "string"; *X; ++X)
    6955       43135 :   if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
    6956        5889 :     if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
    6957             :       ExitLimit ItCnt =
    6958        2982 :         computeLoadConstantCompareExitLimit(LI, RHS, L, Cond);
    6959        1491 :       if (ItCnt.hasAnyInfo())
    6960           0 :         return ItCnt;
    6961             :     }
    6962             : 
    6963       40936 :   const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
    6964       40936 :   const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
    6965             : 
    6966             :   // Try to evaluate any dependencies out of the loop.
    6967       20468 :   LHS = getSCEVAtScope(LHS, L);
    6968       20468 :   RHS = getSCEVAtScope(RHS, L);
    6969             : 
    6970             :   // At this point, we would like to compute how many iterations of the
    6971             :   // loop the predicate will return true for these inputs.
    6972       20468 :   if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) {
    6973             :     // If there is a loop-invariant, force it into the RHS.
    6974         303 :     std::swap(LHS, RHS);
    6975         303 :     Cond = ICmpInst::getSwappedPredicate(Cond);
    6976             :   }
    6977             : 
    6978             :   // Simplify the operands before analyzing them.
    6979       20468 :   (void)SimplifyICmpOperands(Cond, LHS, RHS);
    6980             : 
    6981             :   // If we have a comparison of a chrec against a constant, try to use value
    6982             :   // ranges to answer this query.
    6983       32321 :   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
    6984       20903 :     if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
    6985        9050 :       if (AddRec->getLoop() == L) {
    6986             :         // Form the constant range.
    6987             :         ConstantRange CompRange =
    6988       10637 :             ConstantRange::makeExactICmpRegion(Cond, RHSC->getAPInt());
    6989             : 
    6990        9041 :         const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
    6991       18082 :         if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
    6992             :       }
    6993             : 
    6994       13023 :   switch (Cond) {
    6995        6530 :   case ICmpInst::ICMP_NE: {                     // while (X != Y)
    6996             :     // Convert to: while (X-Y != 0)
    6997             :     ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit,
    6998        6530 :                                 AllowPredicates);
    6999       13062 :     if (EL.hasAnyInfo()) return EL;
    7000        3264 :     break;
    7001             :   }
    7002         838 :   case ICmpInst::ICMP_EQ: {                     // while (X == Y)
    7003             :     // Convert to: while (X-Y == 0)
    7004         838 :     ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L);
    7005         838 :     if (EL.hasAnyInfo()) return EL;
    7006         838 :     break;
    7007             :   }
    7008        4213 :   case ICmpInst::ICMP_SLT:
    7009             :   case ICmpInst::ICMP_ULT: {                    // while (X < Y)
    7010        4213 :     bool IsSigned = Cond == ICmpInst::ICMP_SLT;
    7011             :     ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsExit,
    7012        4213 :                                     AllowPredicates);
    7013        9077 :     if (EL.hasAnyInfo()) return EL;
    7014        1781 :     break;
    7015             :   }
    7016        1095 :   case ICmpInst::ICMP_SGT:
    7017             :   case ICmpInst::ICMP_UGT: {                    // while (X > Y)
    7018        1095 :     bool IsSigned = Cond == ICmpInst::ICMP_SGT;
    7019             :     ExitLimit EL =
    7020             :         howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsExit,
    7021        1095 :                             AllowPredicates);
    7022        2029 :     if (EL.hasAnyInfo()) return EL;
    7023         628 :     break;
    7024             :   }
    7025             :   default:
    7026             :     break;
    7027             :   }
    7028             : 
    7029             :   auto *ExhaustiveCount =
    7030       13716 :       computeExitCountExhaustively(L, ExitCond, !L->contains(TBB));
    7031             : 
    7032       13716 :   if (!isa<SCEVCouldNotCompute>(ExhaustiveCount))
    7033          42 :     return ExhaustiveCount;
    7034             : 
    7035             :   return computeShiftCompareExitLimit(ExitCond->getOperand(0),
    7036       20448 :                                       ExitCond->getOperand(1), L, Cond);
    7037             : }
    7038             : 
    7039             : ScalarEvolution::ExitLimit
    7040          20 : ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L,
    7041             :                                                       SwitchInst *Switch,
    7042             :                                                       BasicBlock *ExitingBlock,
    7043             :                                                       bool ControlsExit) {
    7044             :   assert(!L->contains(ExitingBlock) && "Not an exiting block!");
    7045             : 
    7046             :   // Give up if the exit is the default dest of a switch.
    7047          20 :   if (Switch->getDefaultDest() == ExitingBlock)
    7048          15 :     return getCouldNotCompute();
    7049             : 
    7050             :   assert(L->contains(Switch->getDefaultDest()) &&
    7051             :          "Default case must not exit the loop!");
    7052           5 :   const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L);
    7053           5 :   const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock));
    7054             : 
    7055             :   // while (X != Y) --> while (X-Y != 0)
    7056           5 :   ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit);
    7057           5 :   if (EL.hasAnyInfo())
    7058             :     return EL;
    7059             : 
    7060           4 :   return getCouldNotCompute();
    7061             : }
    7062             : 
    7063             : static ConstantInt *
    7064        7599 : EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
    7065             :                                 ScalarEvolution &SE) {
    7066        7599 :   const SCEV *InVal = SE.getConstant(C);
    7067        7599 :   const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
    7068             :   assert(isa<SCEVConstant>(Val) &&
    7069             :          "Evaluation of SCEV at constant didn't fold correctly?");
    7070        7599 :   return cast<SCEVConstant>(Val)->getValue();
    7071             : }
    7072             : 
    7073             : /// Given an exit condition of 'icmp op load X, cst', try to see if we can
    7074             : /// compute the backedge execution count.
    7075             : ScalarEvolution::ExitLimit
    7076        1491 : ScalarEvolution::computeLoadConstantCompareExitLimit(
    7077             :   LoadInst *LI,
    7078             :   Constant *RHS,
    7079             :   const Loop *L,
    7080             :   ICmpInst::Predicate predicate) {
    7081        1491 :   if (LI->isVolatile()) return getCouldNotCompute();
    7082             : 
    7083             :   // Check to see if the loaded pointer is a getelementptr of a global.
    7084             :   // TODO: Use SCEV instead of manually grubbing with GEPs.
    7085        3824 :   GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
    7086         556 :   if (!GEP) return getCouldNotCompute();
    7087             : 
    7088             :   // Make sure that it is really a constant global we are gepping, with an
    7089             :   // initializer, and make sure the first IDX is really 0.
    7090         922 :   GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
    7091          18 :   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
    7092           0 :       GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
    7093           0 :       !cast<Constant>(GEP->getOperand(1))->isNullValue())
    7094         904 :     return getCouldNotCompute();
    7095             : 
    7096             :   // Okay, we allow one non-constant index into the GEP instruction.
    7097           0 :   Value *VarIdx = nullptr;
    7098           0 :   std::vector<Constant*> Indexes;
    7099           0 :   unsigned VarIdxNum = 0;
    7100           0 :   for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
    7101           0 :     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
    7102           0 :       Indexes.push_back(CI);
    7103           0 :     } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
    7104           0 :       if (VarIdx) return getCouldNotCompute();  // Multiple non-constant idx's.
    7105           0 :       VarIdx = GEP->getOperand(i);
    7106           0 :       VarIdxNum = i-2;
    7107           0 :       Indexes.push_back(nullptr);
    7108             :     }
    7109             : 
    7110             :   // Loop-invariant loads may be a byproduct of loop optimization. Skip them.
    7111           0 :   if (!VarIdx)
    7112           0 :     return getCouldNotCompute();
    7113             : 
    7114             :   // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
    7115             :   // Check to see if X is a loop variant variable value now.
    7116           0 :   const SCEV *Idx = getSCEV(VarIdx);
    7117           0 :   Idx = getSCEVAtScope(Idx, L);
    7118             : 
    7119             :   // We can only recognize very limited forms of loop index expressions, in
    7120             :   // particular, only affine AddRec's like {C1,+,C2}.
    7121           0 :   const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
    7122           0 :   if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) ||
    7123           0 :       !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
    7124           0 :       !isa<SCEVConstant>(IdxExpr->getOperand(1)))
    7125           0 :     return getCouldNotCompute();
    7126             : 
    7127           0 :   unsigned MaxSteps = MaxBruteForceIterations;
    7128           0 :   for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
    7129           0 :     ConstantInt *ItCst = ConstantInt::get(
    7130           0 :                            cast<IntegerType>(IdxExpr->getType()), IterationNum);
    7131           0 :     ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
    7132             : 
    7133             :     // Form the GEP offset.
    7134           0 :     Indexes[VarIdxNum] = Val;
    7135             : 
    7136           0 :     Constant *Result = ConstantFoldLoadThroughGEPIndices(GV->getInitializer(),
    7137           0 :                                                          Indexes);
    7138           0 :     if (!Result) break;  // Cannot compute!
    7139             : 
    7140             :     // Evaluate the condition for this iteration.
    7141           0 :     Result = ConstantExpr::getICmp(predicate, Result, RHS);
    7142           0 :     if (!isa<ConstantInt>(Result)) break;  // Couldn't decide for sure
    7143           0 :     if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
    7144           0 :       ++NumArrayLenItCounts;
    7145           0 :       return getConstant(ItCst);   // Found terminating iteration!
    7146             :     }
    7147             :   }
    7148           0 :   return getCouldNotCompute();
    7149             : }
    7150             : 
    7151        6816 : ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit(
    7152             :     Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) {
    7153        2476 :   ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV);
    7154             :   if (!RHS)
    7155        4340 :     return getCouldNotCompute();
    7156             : 
    7157        2476 :   const BasicBlock *Latch = L->getLoopLatch();
    7158        2476 :   if (!Latch)
    7159          15 :     return getCouldNotCompute();
    7160             : 
    7161        2461 :   const BasicBlock *Predecessor = L->getLoopPredecessor();
    7162        2461 :   if (!Predecessor)
    7163           3 :     return getCouldNotCompute();
    7164             : 
    7165             :   // Return true if V is of the form "LHS `shift_op` <positive constant>".
    7166             :   // Return LHS in OutLHS and shift_opt in OutOpCode.
    7167             :   auto MatchPositiveShift =
    7168        2637 :       [](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) {
    7169             : 
    7170             :     using namespace PatternMatch;
    7171             : 
    7172             :     ConstantInt *ShiftAmt;
    7173       10548 :     if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
    7174          25 :       OutOpCode = Instruction::LShr;
    7175       10448 :     else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
    7176          47 :       OutOpCode = Instruction::AShr;
    7177       10260 :     else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
    7178           3 :       OutOpCode = Instruction::Shl;
    7179             :     else
    7180             :       return false;
    7181             : 
    7182         150 :     return ShiftAmt->getValue().isStrictlyPositive();
    7183             :   };
    7184             : 
    7185             :   // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in
    7186             :   //
    7187             :   // loop:
    7188             :   //   %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ]
    7189             :   //   %iv.shifted = lshr i32 %iv, <positive constant>
    7190             :   //
    7191             :   // Return true on a successful match.  Return the corresponding PHI node (%iv
    7192             :   // above) in PNOut and the opcode of the shift operation in OpCodeOut.
    7193             :   auto MatchShiftRecurrence =
    7194        2458 :       [&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) {
    7195        4916 :     Optional<Instruction::BinaryOps> PostShiftOpCode;
    7196             : 
    7197             :     {
    7198             :       Instruction::BinaryOps OpC;
    7199             :       Value *V;
    7200             : 
    7201             :       // If we encounter a shift instruction, "peel off" the shift operation,
    7202             :       // and remember that we did so.  Later when we inspect %iv's backedge
    7203             :       // value, we will make sure that the backedge value uses the same
    7204             :       // operation.
    7205             :       //
    7206             :       // Note: the peeled shift operation does not have to be the same
    7207             :       // instruction as the one feeding into the PHI's backedge value.  We only
    7208             :       // really care about it being the same *kind* of shift instruction --
    7209             :       // that's all that is required for our later inferences to hold.
    7210        5125 :       if (MatchPositiveShift(LHS, V, OpC)) {
    7211          60 :         PostShiftOpCode = OpC;
    7212          30 :         LHS = V;
    7213             :       }
    7214             :     }
    7215             : 
    7216        4916 :     PNOut = dyn_cast<PHINode>(LHS);
    7217        2660 :     if (!PNOut || PNOut->getParent() != L->getHeader())
    7218             :       return false;
    7219             : 
    7220         179 :     Value *BEValue = PNOut->getIncomingValueForBlock(Latch);
    7221             :     Value *OpLHS;
    7222             : 
    7223             :     return
    7224             :         // The backedge value for the PHI node must be a shift by a positive
    7225             :         // amount
    7226         224 :         MatchPositiveShift(BEValue, OpLHS, OpCodeOut) &&
    7227             : 
    7228             :         // of the PHI node itself
    7229         224 :         OpLHS == PNOut &&
    7230             : 
    7231             :         // and the kind of shift should be match the kind of shift we peeled
    7232             :         // off, if any.
    7233          71 :         (!PostShiftOpCode.hasValue() || *PostShiftOpCode == OpCodeOut);
    7234        2458 :   };
    7235             : 
    7236             :   PHINode *PN;
    7237             :   Instruction::BinaryOps OpCode;
    7238        2458 :   if (!MatchShiftRecurrence(LHS, PN, OpCode))
    7239        2416 :     return getCouldNotCompute();
    7240             : 
    7241          84 :   const DataLayout &DL = getDataLayout();
    7242             : 
    7243             :   // The key rationale for this optimization is that for some kinds of shift
    7244             :   // recurrences, the value of the recurrence "stabilizes" to either 0 or -1
    7245             :   // within a finite number of iterations.  If the condition guarding the
    7246             :   // backedge (in the sense that the backedge is taken if the condition is true)
    7247             :   // is false for the value the shift recurrence stabilizes to, then we know
    7248             :   // that the backedge is taken only a finite number of times.
    7249             : 
    7250          42 :   ConstantInt *StableValue = nullptr;
    7251          42 :   switch (OpCode) {
    7252           0 :   default:
    7253           0 :     llvm_unreachable("Impossible case!");
    7254             : 
    7255          28 :   case Instruction::AShr: {
    7256             :     // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most
    7257             :     // bitwidth(K) iterations.
    7258          28 :     Value *FirstValue = PN->getIncomingValueForBlock(Predecessor);
    7259             :     KnownBits Known = computeKnownBits(FirstValue, DL, 0, nullptr,
    7260          28 :                                        Predecessor->getTerminator(), &DT);
    7261          56 :     auto *Ty = cast<IntegerType>(RHS->getType());
    7262          28 :     if (Known.isNonNegative())
    7263           3 :       StableValue = ConstantInt::get(Ty, 0);
    7264          25 :     else if (Known.isNegative())
    7265           6 :       StableValue = ConstantInt::get(Ty, -1, true);
    7266             :     else
    7267          19 :       return getCouldNotCompute();
    7268             : 
    7269           9 :     break;
    7270             :   }
    7271          14 :   case Instruction::LShr:
    7272             :   case Instruction::Shl:
    7273             :     // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>}
    7274             :     // stabilize to 0 in at most bitwidth(K) iterations.
    7275          28 :     StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0);
    7276          14 :     break;
    7277             :   }
    7278             : 
    7279             :   auto *Result =
    7280          23 :       ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI);
    7281             :   assert(Result->getType()->isIntegerTy(1) &&
    7282             :          "Otherwise cannot be an operand to a branch instruction");
    7283             : 
    7284          23 :   if (Result->isZeroValue()) {
    7285          20 :     unsigned BitWidth = getTypeSizeInBits(RHS->getType());
    7286             :     const SCEV *UpperBound =
    7287          40 :         getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth);
    7288          20 :     return ExitLimit(getCouldNotCompute(), UpperBound, false);
    7289             :   }
    7290             : 
    7291           3 :   return getCouldNotCompute();
    7292             : }
    7293             : 
    7294             : /// Return true if we can constant fold an instruction of the specified type,
    7295             : /// assuming that all operands were constants.
    7296       69943 : static bool CanConstantFold(const Instruction *I) {
    7297      147546 :   if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
    7298      211275 :       isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
    7299       23945 :       isa<LoadInst>(I))
    7300             :     return true;
    7301             : 
    7302       10082 :   if (const CallInst *CI = dyn_cast<CallInst>(I))
    7303        2115 :     if (const Function *F = CI->getCalledFunction())
    7304        2115 :       return canConstantFoldCallTo(CI, F);
    7305             :   return false;
    7306             : }
    7307             : 
    7308             : /// Determine whether this instruction can constant evolve within this loop
    7309             : /// assuming its operands can all constant evolve.
    7310       50348 : static bool canConstantEvolve(Instruction *I, const Loop *L) {
    7311             :   // An instruction outside of the loop can't be derived from a loop PHI.
    7312      100696 :   if (!L->contains(I)) return false;
    7313             : 
    7314       94546 :   if (isa<PHINode>(I)) {
    7315             :     // We don't currently keep track of the control flow needed to evaluate
    7316             :     // PHIs, so we cannot handle PHIs inside of loops.
    7317        8316 :     return L->getHeader() == I->getParent();
    7318             :   }
    7319             : 
    7320             :   // If we won't be able to constant fold this expression even if the operands
    7321             :   // are constants, bail early.
    7322       43115 :   return CanConstantFold(I);
    7323             : }
    7324             : 
    7325             : /// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
    7326             : /// recursing through each instruction operand until reaching a loop header phi.
    7327             : static PHINode *
    7328       18876 : getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L,
    7329             :                                DenseMap<Instruction *, PHINode *> &PHIMap,
    7330             :                                unsigned Depth) {
    7331       18876 :   if (Depth > MaxConstantEvolvingDepth)
    7332             :     return nullptr;
    7333             : 
    7334             :   // Otherwise, we can evaluate this instruction if all of its operands are
    7335             :   // constant or derived from a PHI node themselves.
    7336       18868 :   PHINode *PHI = nullptr;
    7337       51132 :   for (Value *Op : UseInst->operands()) {
    7338       31236 :     if (isa<Constant>(Op)) continue;
    7339             : 
    7340       43476 :     Instruction *OpInst = dyn_cast<Instruction>(Op);
    7341       34829 :     if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr;
    7342             : 
    7343       15895 :     PHINode *P = dyn_cast<PHINode>(OpInst);
    7344             :     if (!P)
    7345             :       // If this operand is already visited, reuse the prior result.
    7346             :       // We may have P != PHI if this is the deepest point at which the
    7347             :       // inconsistent paths meet.
    7348       24598 :       P = PHIMap.lookup(OpInst);
    7349        3926 :     if (!P) {
    7350             :       // Recurse and memoize the results, whether a phi is found or not.
    7351             :       // This recursive call invalidates pointers into PHIMap.
    7352       11969 :       P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1);
    7353       11969 :       PHIMap[OpInst] = P;
    7354             :     }
    7355       15895 :     if (!P)
    7356             :       return nullptr;  // Not evolving from PHI
    7357        8712 :     if (PHI && PHI != P)
    7358             :       return nullptr;  // Evolving from multiple different PHIs.
    7359        8647 :     PHI = P;
    7360             :   }
    7361             :   // This is a expression evolving from a constant PHI!
    7362             :   return PHI;
    7363             : }
    7364             : 
    7365             : /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
    7366             : /// in the loop that V is derived from.  We allow arbitrary operations along the
    7367             : /// way, but the operands of an operation must either be constants or a value
    7368             : /// derived from a constant PHI.  If this expression does not fit with these
    7369             : /// constraints, return null.
    7370        7502 : static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
    7371        7255 :   Instruction *I = dyn_cast<Instruction>(V);
    7372        7255 :   if (!I || !canConstantEvolve(I, L)) return nullptr;
    7373             : 
    7374        6907 :   if (PHINode *PN = dyn_cast<PHINode>(I))
    7375             :     return PN;
    7376             : 
    7377             :   // Record non-constant instructions contained by the loop.
    7378        6907 :   DenseMap<Instruction *, PHINode *> PHIMap;
    7379        6907 :   return getConstantEvolvingPHIOperands(I, L, PHIMap, 0);
    7380             : }
    7381             : 
    7382             : /// EvaluateExpression - Given an expression that passes the
    7383             : /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
    7384             : /// in the loop has the value PHIVal.  If we can't fold this expression for some
    7385             : /// reason, return null.
    7386       42541 : static Constant *EvaluateExpression(Value *V, const Loop *L,
    7387             :                                     DenseMap<Instruction *, Constant *> &Vals,
    7388             :                                     const DataLayout &DL,
    7389             :                                     const TargetLibraryInfo *TLI) {
    7390             :   // Convenient constant check, but redundant for recursive calls.
    7391             :   if (Constant *C = dyn_cast<Constant>(V)) return C;
    7392       42430 :   Instruction *I = dyn_cast<Instruction>(V);
    7393             :   if (!I) return nullptr;
    7394             : 
    7395       84860 :   if (Constant *C = Vals.lookup(I)) return C;
    7396             : 
    7397             :   // An instruction inside the loop depends on a value outside the loop that we
    7398             :   // weren't given a mapping for, or a value such as a call inside the loop.
    7399       22695 :   if (!canConstantEvolve(I, L)) return nullptr;
    7400             : 
    7401             :   // An unmapped PHI can be due to a branch or another loop inside this loop,
    7402             :   // or due to this not being the initial iteration through a loop where we
    7403             :   // couldn't compute the evolution of this particular PHI last time.
    7404       45338 :   if (isa<PHINode>(I)) return nullptr;
    7405             : 
    7406       67938 :   std::vector<Constant*> Operands(I->getNumOperands());
    7407             : 
    7408       89632 :   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
    7409      133428 :     Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i));
    7410       56295 :     if (!Operand) {
    7411       47292 :       Operands[i] = dyn_cast<Constant>(I->getOperand(i));
    7412       11959 :       if (!Operands[i]) return nullptr;
    7413       11819 :       continue;
    7414             :     }
    7415       32653 :     Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI);
    7416       32653 :     Vals[Operand] = C;
    7417       32653 :     if (!C) return nullptr;
    7418       65042 :     Operands[i] = C;
    7419             :   }
    7420             : 
    7421       45020 :   if (CmpInst *CI = dyn_cast<CmpInst>(I))
    7422        9006 :     return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
    7423        9006 :                                            Operands[1], DL, TLI);
    7424       18183 :   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
    7425         176 :     if (!LI->isVolatile())
    7426         176 :       return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
    7427             :   }
    7428       17831 :   return ConstantFoldInstOperands(I, Operands, DL, TLI);
    7429             : }
    7430             : 
    7431             : 
    7432             : // If every incoming value to PN except the one for BB is a specific Constant,
    7433             : // return that, else return nullptr.
    7434        1793 : static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) {
    7435        1793 :   Constant *IncomingVal = nullptr;
    7436             : 
    7437        4938 :   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
    7438        2594 :     if (PN->getIncomingBlock(i) == BB)
    7439         799 :       continue;
    7440             : 
    7441        2348 :     auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i));
    7442             :     if (!CurrentVal)
    7443             :       return nullptr;
    7444             : 
    7445         553 :     if (IncomingVal != CurrentVal) {
    7446         551 :       if (IncomingVal)
    7447             :         return nullptr;
    7448             :       IncomingVal = CurrentVal;
    7449             :     }
    7450             :   }
    7451             : 
    7452             :   return IncomingVal;
    7453             : }
    7454             : 
    7455             : /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
    7456             : /// in the header of its containing loop, we know the loop executes a
    7457             : /// constant number of times, and the PHI node is just a recurrence
    7458             : /// involving constants, fold it.
    7459             : Constant *
    7460         114 : ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
    7461             :                                                    const APInt &BEs,
    7462             :                                                    const Loop *L) {
    7463         114 :   auto I = ConstantEvolutionLoopExitValue.find(PN);
    7464         342 :   if (I != ConstantEvolutionLoopExitValue.end())
    7465           0 :     return I->second;
    7466             : 
    7467         114 :   if (BEs.ugt(MaxBruteForceIterations))
    7468          14 :     return ConstantEvolutionLoopExitValue[PN] = nullptr;  // Not going to evaluate it.
    7469             : 
    7470         214 :   Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
    7471             : 
    7472         107 :   DenseMap<Instruction *, Constant *> CurrentIterVals;
    7473         214 :   BasicBlock *Header = L->getHeader();
    7474             :   assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
    7475             : 
    7476         107 :   BasicBlock *Latch = L->getLoopLatch();
    7477         107 :   if (!Latch)
    7478             :     return nullptr;
    7479             : 
    7480         738 :   for (auto &I : *Header) {
    7481         310 :     PHINode *PHI = dyn_cast<PHINode>(&I);
    7482             :     if (!PHI) break;
    7483         310 :     auto *StartCST = getOtherIncomingValue(PHI, Latch);
    7484         310 :     if (!StartCST) continue;
    7485         418 :     CurrentIterVals[PHI] = StartCST;
    7486             :   }
    7487         214 :   if (!CurrentIterVals.count(PN))
    7488          17 :     return RetVal = nullptr;
    7489             : 
    7490          90 :   Value *BEValue = PN->getIncomingValueForBlock(Latch);
    7491             : 
    7492             :   // Execute the loop symbolically to determine the exit value.
    7493             :   assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) &&
    7494             :          "BEs is <= MaxBruteForceIterations which is an 'unsigned'!");
    7495             : 
    7496          90 :   unsigned NumIterations = BEs.getZExtValue(); // must be in range
    7497          90 :   unsigned IterationNum = 0;
    7498         180 :   const DataLayout &DL = getDataLayout();
    7499         437 :   for (; ; ++IterationNum) {
    7500         527 :     if (IterationNum == NumIterations)
    7501         128 :       return RetVal = CurrentIterVals[PN];  // Got exit value!
    7502             : 
    7503             :     // Compute the value of the PHIs for the next iteration.
    7504             :     // EvaluateExpression adds non-phi values to the CurrentIterVals map.
    7505         900 :     DenseMap<Instruction *, Constant *> NextIterVals;
    7506             :     Constant *NextPHI =
    7507         463 :         EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
    7508         463 :     if (!NextPHI)
    7509             :       return nullptr;        // Couldn't evaluate!
    7510         874 :     NextIterVals[PN] = NextPHI;
    7511             : 
    7512         874 :     bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
    7513             : 
    7514             :     // Also evaluate the other PHI nodes.  However, we don't get to stop if we
    7515             :     // cease to be able to evaluate one of them or if they stop evolving,
    7516             :     // because that doesn't necessarily prevent us from computing PN.
    7517         874 :     SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute;
    7518        2917 :     for (const auto &I : CurrentIterVals) {
    7519        3212 :       PHINode *PHI = dyn_cast<PHINode>(I.first);
    7520        2835 :       if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
    7521         377 :       PHIsToCompute.emplace_back(PHI, I.second);
    7522             :     }
    7523             :     // We use two distinct loops because EvaluateExpression may invalidate any
    7524             :     // iterators into CurrentIterVals.
    7525        1688 :     for (const auto &I : PHIsToCompute) {
    7526         377 :       PHINode *PHI = I.first;
    7527         754 :       Constant *&NextPHI = NextIterVals[PHI];
    7528         377 :       if (!NextPHI) {   // Not already computed.
    7529         377 :         Value *BEValue = PHI->getIncomingValueForBlock(Latch);
    7530         377 :         NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
    7531             :       }
    7532         377 :       if (NextPHI != I.second)
    7533         377 :         StoppedEvolving = false;
    7534             :     }
    7535             : 
    7536             :     // If all entries in CurrentIterVals == NextIterVals then we can stop
    7537             :     // iterating, the loop can't continue to change.
    7538         437 :     if (StoppedEvolving)
    7539           0 :       return RetVal = CurrentIterVals[PN];
    7540             : 
    7541         437 :     CurrentIterVals.swap(NextIterVals);
    7542         437 :   }
    7543             : }
    7544             : 
    7545        7502 : const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L,
    7546             :                                                           Value *Cond,
    7547             :                                                           bool ExitWhen) {
    7548        7502 :   PHINode *PN = getConstantEvolvingPHI(Cond, L);
    7549        7502 :   if (!PN) return getCouldNotCompute();
    7550             : 
    7551             :   // If the loop is canonicalized, the PHI will have exactly two entries.
    7552             :   // That's the only form we support here.
    7553         853 :   if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
    7554             : 
    7555         847 :   DenseMap<Instruction *, Constant *> CurrentIterVals;
    7556        1694 :   BasicBlock *Header = L->getHeader();
    7557             :   assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
    7558             : 
    7559         847 :   BasicBlock *Latch = L->getLoopLatch();
    7560             :   assert(Latch && "Should follow from NumIncomingValues == 2!");
    7561             : 
    7562        4871 :   for (auto &I : *Header) {
    7563        1483 :     PHINode *PHI = dyn_cast<PHINode>(&I);
    7564             :     if (!PHI)
    7565             :       break;
    7566        1483 :     auto *StartCST = getOtherIncomingValue(PHI, Latch);
    7567        1483 :     if (!StartCST) continue;
    7568         684 :     CurrentIterVals[PHI] = StartCST;
    7569             :   }
    7570         847 :   if (!CurrentIterVals.count(PN))
    7571         716 :     return getCouldNotCompute();
    7572             : 
    7573             :   // Okay, we find a PHI node that defines the trip count of this loop.  Execute
    7574             :   // the loop symbolically to determine when the condition gets a value of
    7575             :   // "ExitWhen".
    7576         131 :   unsigned MaxIterations = MaxBruteForceIterations;   // Limit analysis.
    7577         262 :   const DataLayout &DL = getDataLayout();
    7578        4402 :   for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
    7579        4363 :     auto *CondVal = dyn_cast_or_null<ConstantInt>(
    7580        8679 :         EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI));
    7581             : 
    7582             :     // Couldn't symbolically evaluate.
    7583         139 :     if (!CondVal) return getCouldNotCompute();
    7584             : 
    7585        8632 :     if (CondVal->getValue() == uint64_t(ExitWhen)) {
    7586          45 :       ++NumBruteForceTripCountsComputed;
    7587          90 :       return getConstant(Type::getInt32Ty(getContext()), IterationNum);
    7588             :     }
    7589             : 
    7590             :     // Update all the PHI nodes for the next iteration.
    7591        8542 :     DenseMap<Instruction *, Constant *> NextIterVals;
    7592             : 
    7593             :     // Create a list of which PHIs we need to compute. We want to do this before
    7594             :     // calling EvaluateExpression on them because that may invalidate iterators
    7595             :     // into CurrentIterVals.
    7596        8542 :     SmallVector<PHINode *, 8> PHIsToCompute;
    7597       30782 :     for (const auto &I : CurrentIterVals) {
    7598       35938 :       PHINode *PHI = dyn_cast<PHINode>(I.first);
    7599       31253 :       if (!PHI || PHI->getParent() != Header) continue;
    7600        4685 :       PHIsToCompute.push_back(PHI);
    7601             :     }
    7602       17498 :     for (PHINode *PHI : PHIsToCompute) {
    7603        9370 :       Constant *&NextPHI = NextIterVals[PHI];
    7604        4685 :       if (NextPHI) continue;    // Already computed!
    7605             : 
    7606        4685 :       Value *BEValue = PHI->getIncomingValueForBlock(Latch);
    7607        4685 :       NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
    7608             :     }
    7609        4271 :     CurrentIterVals.swap(NextIterVals);
    7610             :   }
    7611             : 
    7612             :   // Too many iterations were needed to evaluate.
    7613          39 :   return getCouldNotCompute();
    7614             : }
    7615             : 
    7616      508547 : const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
    7617             :   SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values =
    7618     1017094 :       ValuesAtScopes[V];
    7619             :   // Check to see if we've folded this expression at this loop before.
    7620     1973718 :   for (auto &LS : Values)
    7621      758537 :     if (LS.first == L)
    7622      310460 :       return LS.second ? LS.second : V;
    7623             : 
    7624      198087 :   Values.emplace_back(L, nullptr);
    7625             : 
    7626             :   // Otherwise compute it.
    7627      198087 :   const SCEV *C = computeSCEVAtScope(V, L);
    7628     1188221 :   for (auto &LS : reverse(ValuesAtScopes[V]))
    7629      197743 :     if (LS.first == L) {
    7630      197700 :       LS.second = C;
    7631      197700 :       break;
    7632             :     }
    7633             :   return C;
    7634             : }
    7635             : 
    7636             : /// This builds up a Constant using the ConstantExpr interface.  That way, we
    7637             : /// will return Constants for objects which aren't represented by a
    7638             : /// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
    7639             : /// Returns NULL if the SCEV isn't representable as a Constant.
    7640       35027 : static Constant *BuildConstantFromSCEV(const SCEV *V) {
    7641       35027 :   switch (static_cast<SCEVTypes>(V->getSCEVType())) {
    7642             :     case scCouldNotCompute:
    7643             :     case scAddRecExpr:
    7644             :       break;
    7645        6745 :     case scConstant:
    7646        6745 :       return cast<SCEVConstant>(V)->getValue();
    7647       12910 :     case scUnknown:
    7648       25820 :       return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
    7649         300 :     case scSignExtend: {
    7650         300 :       const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
    7651         300 :       if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
    7652           0 :         return ConstantExpr::getSExt(CastOp, SS->getType());
    7653             :       break;
    7654             :     }
    7655         240 :     case scZeroExtend: {
    7656         240 :       const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
    7657         240 :       if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
    7658           6 :         return ConstantExpr::getZExt(CastOp, SZ->getType());
    7659             :       break;
    7660             :     }
    7661         106 :     case scTruncate: {
    7662         106 :       const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
    7663         106 :       if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
    7664           3 :         return ConstantExpr::getTrunc(CastOp, ST->getType());
    7665             :       break;
    7666             :     }
    7667        6566 :     case scAddExpr: {
    7668        6566 :       const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
    7669       13132 :       if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
    7670        4854 :         if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
    7671           0 :           unsigned AS = PTy->getAddressSpace();
    7672           0 :           Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
    7673           0 :           C = ConstantExpr::getBitCast(C, DestPtrTy);
    7674             :         }
    7675        4871 :         for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
    7676        9710 :           Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
    7677        4855 :           if (!C2) return nullptr;
    7678             : 
    7679             :           // First pointer!
    7680          51 :           if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
    7681          26 :             unsigned AS = C2->getType()->getPointerAddressSpace();
    7682          13 :             std::swap(C, C2);
    7683          13 :             Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
    7684             :             // The offsets have been converted to bytes.  We can add bytes to an
    7685             :             // i8* by GEP with the byte count in the first index.
    7686          13 :             C = ConstantExpr::getBitCast(C, DestPtrTy);
    7687             :           }
    7688             : 
    7689             :           // Don't bother trying to sum two pointers. We probably can't
    7690             :           // statically compute a load that results from it anyway.
    7691          34 :           if (C2->getType()->isPointerTy())
    7692             :             return nullptr;
    7693             : 
    7694          30 :           if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
    7695          26 :             if (PTy->getElementType()->isStructTy())
    7696           0 :               C2 = ConstantExpr::getIntegerCast(
    7697           0 :                   C2, Type::getInt32Ty(C->getContext()), true);
    7698          26 :             C = ConstantExpr::getGetElementPtr(PTy->getElementType(), C, C2);
    7699             :           } else
    7700           4 :             C = ConstantExpr::getAdd(C, C2);
    7701             :         }
    7702             :         return C;
    7703             :       }
    7704             :       break;
    7705             :     }
    7706        1839 :     case scMulExpr: {
    7707        1839 :       const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
    7708        3678 :       if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
    7709             :         // Don't bother with pointers at all.
    7710        3622 :         if (C->getType()->isPointerTy()) return nullptr;
    7711        1814 :         for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
    7712        3622 :           Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
    7713        1814 :           if (!C2 || C2->getType()->isPointerTy()) return nullptr;
    7714           3 :           C = ConstantExpr::getMul(C, C2);
    7715             :         }
    7716             :         return C;
    7717             :       }
    7718             :       break;
    7719             :     }
    7720         205 :     case scUDivExpr: {
    7721         205 :       const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
    7722         205 :       if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
    7723           6 :         if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
    7724           2 :           if (LHS->getType() == RHS->getType())
    7725           2 :             return ConstantExpr::getUDiv(LHS, RHS);
    7726             :       break;
    7727             :     }
    7728             :     case scSMaxExpr:
    7729             :     case scUMaxExpr:
    7730             :       break; // TODO: smax, umax.
    7731             :   }
    7732             :   return nullptr;
    7733             : }
    7734             : 
    7735      198087 : const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
    7736      396174 :   if (isa<SCEVConstant>(V)) return V;
    7737             : 
    7738             :   // If this instruction is evolved from a constant-evolving PHI, compute the
    7739             :   // exit value from the loop without using SCEVs.
    7740      183278 :   if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
    7741       71429 :     if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
    7742       53828 :       const Loop *LI = this->LI[I->getParent()];
    7743       16879 :       if (LI && LI->getParentLoop() == L)  // Looking for loop exit value.
    7744         256 :         if (PHINode *PN = dyn_cast<PHINode>(I))
    7745         512 :           if (PN->getParent() == LI->getHeader()) {
    7746             :             // Okay, there is no closed form solution for the PHI node.  Check
    7747             :             // to see if the loop that contains it has a known backedge-taken
    7748             :             // count.  If so, we may be able to force computation of the exit
    7749             :             // value.
    7750         211 :             const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
    7751             :             if (const SCEVConstant *BTCC =
    7752         136 :                   dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
    7753             : 
    7754             :               // This trivial case can show up in some degenerate cases where
    7755             :               // the incoming IR has not yet been fully simplified.
    7756         272 :               if (BTCC->getValue()->isZero()) {
    7757             :                 Value *InitValue = nullptr;
    7758             :                 bool MultipleInitValues = false;
    7759          24 :                 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
    7760          69 :                   if (!LI->contains(PN->getIncomingBlock(i))) {
    7761          22 :                     if (!InitValue)
    7762             :                       InitValue = PN->getIncomingValue(i);
    7763           0 :                     else if (InitValue != PN->getIncomingValue(i)) {
    7764             :                       MultipleInitValues = true;
    7765             :                       break;
    7766             :                     }
    7767             :                   }
    7768          23 :                   if (!MultipleInitValues && InitValue)
    7769          22 :                     return getSCEV(InitValue);
    7770             :                 }
    7771             :               }
    7772             :               // Okay, we know how many times the containing loop executes.  If
    7773             :               // this is a constant evolving PHI node, get the final value at
    7774             :               // the specified iteration number.
    7775             :               Constant *RV =
    7776         114 :                   getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), LI);
    7777         114 :               if (RV) return getSCEV(RV);
    7778             :             }
    7779             :           }
    7780             : 
    7781             :       // Okay, this is an expression that we cannot symbolically evaluate
    7782             :       // into a SCEV.  Check to see if it's possible to symbolically evaluate
    7783             :       // the arguments into constants, and if so, try to constant propagate the
    7784             :       // result.  This is particularly useful for computing loop exit values.
    7785       26828 :       if (CanConstantFold(I)) {
    7786       21253 :         SmallVector<Constant *, 4> Operands;
    7787       20203 :         bool MadeImprovement = false;
    7788       41799 :         for (Value *Op : I->operands()) {
    7789       21795 :           if (Constant *C = dyn_cast<Constant>(Op)) {
    7790        1282 :             Operands.push_back(C);
    7791        1282 :             continue;
    7792             :           }
    7793             : 
    7794             :           // If any of the operands is non-constant and if they are
    7795             :           // non-integer and non-pointer, don't even try to analyze them
    7796             :           // with scev techniques.
    7797       19231 :           if (!isSCEVable(Op->getType()))
    7798       19120 :             return V;
    7799             : 
    7800       19099 :           const SCEV *OrigV = getSCEV(Op);
    7801       19099 :           const SCEV *OpV = getSCEVAtScope(OrigV, L);
    7802       19099 :           MadeImprovement |= OrigV != OpV;
    7803             : 
    7804       19099 :           Constant *C = BuildConstantFromSCEV(OpV);
    7805       19099 :           if (!C) return V;
    7806         111 :           if (C->getType() != Op->getType())
    7807          18 :             C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
    7808             :                                                               Op->getType(),
    7809             :                                                               false),
    7810             :                                       C, Op->getType());
    7811         111 :           Operands.push_back(C);
    7812             :         }
    7813             : 
    7814             :         // Check to see if getSCEVAtScope actually made an improvement.
    7815        1083 :         if (MadeImprovement) {
    7816          33 :           Constant *C = nullptr;
    7817          66 :           const DataLayout &DL = getDataLayout();
    7818          33 :           if (const CmpInst *CI = dyn_cast<CmpInst>(I))
    7819          27 :             C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
    7820          18 :                                                 Operands[1], DL, &TLI);
    7821          24 :           else if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
    7822          12 :             if (!LI->isVolatile())
    7823          24 :               C = ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
    7824             :           } else
    7825          24 :             C = ConstantFoldInstOperands(I, Operands, DL, &TLI);
    7826          33 :           if (!C) return V;
    7827          27 :           return getSCEV(C);
    7828             :         }
    7829             :       }
    7830             :     }
    7831             : 
    7832             :     // This is some other type of SCEVUnknown, just return it.
    7833             :     return V;
    7834             :   }
    7835             : 
    7836      140892 :   if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
    7837             :     // Avoid performing the look-up in the common case where the specified
    7838             :     // expression has no loop-variant portions.
    7839      127152 :     for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
    7840      192598 :       const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
    7841      192598 :       if (OpAtScope != Comm->getOperand(i)) {
    7842             :         // Okay, at least one of these operands is loop variant but might be
    7843             :         // foldable.  Build a new instance of the folded commutative expression.
    7844             :         SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
    7845       47373 :                                             Comm->op_begin()+i);
    7846       15791 :         NewOps.push_back(OpAtScope);
    7847             : 
    7848       27010 :         for (++i; i != e; ++i) {
    7849       22438 :           OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
    7850       11219 :           NewOps.push_back(OpAtScope);
    7851             :         }
    7852       31582 :         if (isa<SCEVAddExpr>(Comm))
    7853        3630 :           return getAddExpr(NewOps);
    7854       24322 :         if (isa<SCEVMulExpr>(Comm))
    7855       12156 :           return getMulExpr(NewOps);
    7856          10 :         if (isa<SCEVSMaxExpr>(Comm))
    7857           3 :           return getSMaxExpr(NewOps);
    7858           4 :         if (isa<SCEVUMaxExpr>(Comm))
    7859           2 :           return getUMaxExpr(NewOps);
    7860           0 :         llvm_unreachable("Unknown commutative SCEV type!");
    7861             :       }
    7862             :     }
    7863             :     // If we got here, all operands are loop invariant.
    7864             :     return Comm;
    7865             :   }
    7866             : 
    7867       48818 :   if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
    7868        1214 :     const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
    7869        1214 :     const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
    7870        1214 :     if (LHS == Div->getLHS() && RHS == Div->getRHS())
    7871             :       return Div;   // must be loop invariant
    7872           7 :     return getUDivExpr(LHS, RHS);
    7873             :   }
    7874             : 
    7875             :   // If this is a loop recurrence for a loop that does not contain L, then we
    7876             :   // are dealing with the final value computed by the loop.
    7877       88896 :   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
    7878             :     // First, attempt to evaluate each operand.
    7879             :     // Avoid performing the look-up in the common case where the specified
    7880             :     // expression has no loop-variant portions.
    7881      122672 :     for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
    7882      166120 :       const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L);
    7883      166120 :       if (OpAtScope == AddRec->getOperand(i))
    7884       80166 :         continue;
    7885             : 
    7886             :       // Okay, at least one of these operands is loop variant but might be
    7887             :       // foldable.  Build a new instance of the folded commutative expression.
    7888             :       SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(),
    7889        5788 :                                           AddRec->op_begin()+i);
    7890        2894 :       NewOps.push_back(OpAtScope);
    7891        8787 :       for (++i; i != e; ++i)
    7892       11786 :         NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L));
    7893             : 
    7894             :       const SCEV *FoldedRec =
    7895        5788 :         getAddRecExpr(NewOps, AddRec->getLoop(),
    7896        2894 :                       AddRec->getNoWrapFlags(SCEV::FlagNW));
    7897        2012 :       AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec);
    7898             :       // The addrec may be folded to a nonrecurrence, for example, if the
    7899             :       // induction variable is multiplied by zero after constant folding. Go
    7900             :       // ahead and return the folded value.
    7901             :       if (!AddRec)
    7902        1764 :         return FoldedRec;
    7903        2012 :       break;
    7904             :     }
    7905             : 
    7906             :     // If the scope is outside the addrec's loop, evaluate it by using the
    7907             :     // loop exit value of the addrec.
    7908       83248 :     if (!AddRec->getLoop()->contains(L)) {
    7909             :       // To evaluate this recurrence, we need to know how many times the AddRec
    7910             :       // loop iterates.  Compute this now.
    7911        3953 :       const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
    7912        3953 :       if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
    7913             : 
    7914             :       // Then, evaluate the AddRec.
    7915        3542 :       return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
    7916             :     }
    7917             : 
    7918             :     return AddRec;
    7919             :   }
    7920             : 
    7921        5072 :   if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
    7922        1188 :     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
    7923        1188 :     if (Op == Cast->getOperand())
    7924             :       return Cast;  // must be loop invariant
    7925          19 :     return getZeroExtendExpr(Op, Cast->getType());
    7926             :   }
    7927             : 
    7928        4480 :   if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
    7929        1784 :     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
    7930        1784 :     if (Op == Cast->getOperand())
    7931             :       return Cast;  // must be loop invariant
    7932          40 :     return getSignExtendExpr(Op, Cast->getType());
    7933             :   }
    7934             : 
    7935        1824 :   if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
    7936         912 :     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
    7937         912 :     if (Op == Cast->getOperand())
    7938             :       return Cast;  // must be loop invariant
    7939           7 :     return getTruncateExpr(Op, Cast->getType());
    7940             :   }
    7941             : 
    7942           0 :   llvm_unreachable("Unknown SCEV type!");
    7943             : }
    7944             : 
    7945      222711 : const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
    7946      222711 :   return getSCEVAtScope(getSCEV(V), L);
    7947             : }
    7948             : 
    7949             : /// Finds the minimum unsigned root of the following equation:
    7950             : ///
    7951             : ///     A * X = B (mod N)
    7952             : ///
    7953             : /// where N = 2^BW and BW is the common bit width of A and B. The signedness of
    7954             : /// A and B isn't important.
    7955             : ///
    7956             : /// If the equation does not have a solution, SCEVCouldNotCompute is returned.
    7957        1135 : static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B,
    7958             :                                                ScalarEvolution &SE) {
    7959        1135 :   uint32_t BW = A.getBitWidth();
    7960             :   assert(BW == SE.getTypeSizeInBits(B->getType()));
    7961             :   assert(A != 0 && "A must be non-zero.");
    7962             : 
    7963             :   // 1. D = gcd(A, N)
    7964             :   //
    7965             :   // The gcd of A and N may have only one prime factor: 2. The number of
    7966             :   // trailing zeros in A is its multiplicity
    7967        1135 :   uint32_t Mult2 = A.countTrailingZeros();
    7968             :   // D = 2^Mult2
    7969             : 
    7970             :   // 2. Check if B is divisible by D.
    7971             :   //
    7972             :   // B is divisible by D if and only if the multiplicity of prime factor 2 for B
    7973             :   // is not less than multiplicity of this prime factor for D.
    7974        1135 :   if (SE.GetMinTrailingZeros(B) < Mult2)
    7975         940 :     return SE.getCouldNotCompute();
    7976             : 
    7977             :   // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
    7978             :   // modulo (N / D).
    7979             :   //
    7980             :   // If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent
    7981             :   // (N / D) in general. The inverse itself always fits into BW bits, though,
    7982             :   // so we immediately truncate it.
    7983         390 :   APInt AD = A.lshr(Mult2).zext(BW + 1);  // AD = A / D
    7984         585 :   APInt Mod(BW + 1, 0);
    7985         390 :   Mod.setBit(BW - Mult2);  // Mod = N / D
    7986         585 :   APInt I = AD.multiplicativeInverse(Mod).trunc(BW);
    7987             : 
    7988             :   // 4. Compute the minimum unsigned root of the equation:
    7989             :   // I * (B / D) mod (N / D)
    7990             :   // To simplify the computation, we factor out the divide by D:
    7991             :   // (I * B mod N) / D
    7992         390 :   const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2));
    7993         195 :   return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D);
    7994             : }
    7995             : 
    7996             : /// Find the roots of the quadratic equation for the given quadratic chrec
    7997             : /// {L,+,M,+,N}.  This returns either the two roots (which might be the same) or
    7998             : /// two SCEVCouldNotCompute objects.
    7999             : static Optional<std::pair<const SCEVConstant *,const SCEVConstant *>>
    8000          24 : SolveQuadraticEquation(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
    8001             :   assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
    8002          72 :   const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
    8003          72 :   const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
    8004          72 :   const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
    8005             : 
    8006             :   // We currently can only solve this if the coefficients are constants.
    8007          24 :   if (!LC || !MC || !NC)
    8008             :     return None;
    8009             : 
    8010          24 :   uint32_t BitWidth = LC->getAPInt().getBitWidth();
    8011          24 :   const APInt &L = LC->getAPInt();
    8012          24 :   const APInt &M = MC->getAPInt();
    8013          24 :   const APInt &N = NC->getAPInt();
    8014          24 :   APInt Two(BitWidth, 2);
    8015             : 
    8016             :   // Convert from chrec coefficients to polynomial coefficients AX^2+BX+C
    8017             : 
    8018             :   // The A coefficient is N/2
    8019          48 :   APInt A = N.sdiv(Two);
    8020             : 
    8021             :   // The B coefficient is M-N/2
    8022          48 :   APInt B = M;
    8023          24 :   B -= A; // A is the same as N/2.
    8024             : 
    8025             :   // The C coefficient is L.
    8026          24 :   const APInt& C = L;
    8027             : 
    8028             :   // Compute the B^2-4ac term.
    8029          48 :   APInt SqrtTerm = B;
    8030          24 :   SqrtTerm *= B;
    8031          96 :   SqrtTerm -= 4 * (A * C);
    8032             : 
    8033          48 :   if (SqrtTerm.isNegative()) {
    8034             :     // The loop is provably infinite.
    8035             :     return None;
    8036             :   }
    8037             : 
    8038             :   // Compute sqrt(B^2-4ac). This is guaranteed to be the nearest
    8039             :   // integer value or else APInt::sqrt() will assert.
    8040          21 :   APInt SqrtVal = SqrtTerm.sqrt();
    8041             : 
    8042             :   // Compute the two solutions for the quadratic formula.
    8043             :   // The divisions must be performed as signed divisions.
    8044         105 :   APInt NegB = -std::move(B);
    8045          63 :   APInt TwoA = std::move(A);
    8046          21 :   TwoA <<= 1;
    8047          21 :   if (TwoA.isNullValue())
    8048             :     return None;
    8049             : 
    8050          24 :   LLVMContext &Context = SE.getContext();
    8051             : 
    8052             :   ConstantInt *Solution1 =
    8053          60 :     ConstantInt::get(Context, (NegB + SqrtVal).sdiv(TwoA));
    8054             :   ConstantInt *Solution2 =
    8055          60 :     ConstantInt::get(Context, (NegB - SqrtVal).sdiv(TwoA));
    8056             : 
    8057          36 :   return std::make_pair(cast<SCEVConstant>(SE.getConstant(Solution1)),
    8058          36 :                         cast<SCEVConstant>(SE.getConstant(Solution2)));
    8059             : }
    8060             : 
    8061             : ScalarEvolution::ExitLimit
    8062        6535 : ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
    8063             :                               bool AllowPredicates) {
    8064             : 
    8065             :   // This is only used for loops with a "x != y" exit test. The exit condition
    8066             :   // is now expressed as a single expression, V = x-y. So the exit test is
    8067             :   // effectively V != 0.  We know and take advantage of the fact that this
    8068             :   // expression only being used in a comparison by zero context.
    8069             : 
    8070       13070 :   SmallPtrSet<const SCEVPredicate *, 4> Predicates;
    8071             :   // If the value is a constant
    8072           9 :   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
    8073             :     // If the value is already zero, the branch will execute zero times.
    8074          18 :     if (C->getValue()->isZero()) return C;
    8075           0 :     return getCouldNotCompute();  // Otherwise it will loop infinitely.
    8076             :   }
    8077             : 
    8078        6526 :   const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V);
    8079        6526 :   if (!AddRec && AllowPredicates)
    8080             :     // Try to make this an AddRec using runtime tests, in the first X
    8081             :     // iterations of this loop, where X is the SCEV expression found by the
    8082             :     // algorithm below.
    8083         142 :     AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates);
    8084             : 
    8085        6526 :   if (!AddRec || AddRec->getLoop() != L)
    8086        2293 :     return getCouldNotCompute();
    8087             : 
    8088             :   // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
    8089             :   // the quadratic equation to solve it.
    8090        4260 :   if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
    8091          18 :     if (auto Roots = SolveQuadraticEquation(AddRec, *this)) {
    8092           3 :       const SCEVConstant *R1 = Roots->first;
    8093           3 :       const SCEVConstant *R2 = Roots->second;
    8094             :       // Pick the smallest positive root value.
    8095           3 :       if (ConstantInt *CB = dyn_cast<ConstantInt>(ConstantExpr::getICmp(
    8096           6 :               CmpInst::ICMP_ULT, R1->getValue(), R2->getValue()))) {
    8097           3 :         if (!CB->getZExtValue())
    8098             :           std::swap(R1, R2); // R1 is the minimum root now.
    8099             : 
    8100             :         // We can only use this value if the chrec ends up with an exact zero
    8101             :         // value at this index.  When solving for "X*X != 5", for example, we
    8102             :         // should not accept a root of 2.
    8103           3 :         const SCEV *Val = AddRec->evaluateAtIteration(R1, *this);
    8104           3 :         if (Val->isZero())
    8105             :           // We found a quadratic root!
    8106           0 :           return ExitLimit(R1, R1, false, Predicates);
    8107             :       }
    8108             :     }
    8109           9 :     return getCouldNotCompute();
    8110             :   }
    8111             : 
    8112             :   // Otherwise we can only handle this if it is affine.
    8113        4224 :   if (!AddRec->isAffine())
    8114           0 :     return getCouldNotCompute();
    8115             : 
    8116             :   // If this is an affine expression, the execution count of this branch is
    8117             :   // the minimum unsigned root of the following equation:
    8118             :   //
    8119             :   //     Start + Step*N = 0 (mod 2^BW)
    8120             :   //
    8121             :   // equivalent to:
    8122             :   //
    8123             :   //             Step*N = -Start (mod 2^BW)
    8124             :   //
    8125             :   // where BW is the common bit width of Start and Step.
    8126             : 
    8127             :   // Get the initial value for the loop.
    8128        8448 :   const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
    8129        8448 :   const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
    8130             : 
    8131             :   // For now we handle only constant steps.
    8132             :   //
    8133             :   // TODO: Handle a nonconstant Step given AddRec<NUW>. If the
    8134             :   // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap
    8135             :   // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step.
    8136             :   // We have not yet seen any such cases.
    8137        4201 :   const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
    8138        8402 :   if (!StepC || StepC->getValue()->isZero())
    8139          26 :     return getCouldNotCompute();
    8140             : 
    8141             :   // For positive steps (counting up until unsigned overflow):
    8142             :   //   N = -Start/Step (as unsigned)
    8143             :   // For negative steps (counting down to zero):
    8144             :   //   N = Start/-Step
    8145             :   // First compute the unsigned distance from zero in the direction of Step.
    8146        8396 :   bool CountDown = StepC->getAPInt().isNegative();
    8147        4198 :   const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start);
    8148             : 
    8149             :   // Handle unitary steps, which cannot wraparound.
    8150             :   // 1*N = -Start; -1*N = Start (mod 2^BW), so:
    8151             :   //   N = Distance (as unsigned)
    8152       10357 :   if (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne()) {
    8153        5514 :     APInt MaxBECount = getUnsignedRangeMax(Distance);
    8154             : 
    8155             :     // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
    8156             :     // we end up with a loop whose backedge-taken count is n - 1.  Detect this
    8157             :     // case, and see if we can improve the bound.
    8158             :     //
    8159             :     // Explicitly handling this here is necessary because getUnsignedRange
    8160             :     // isn't context-sensitive; it doesn't know that we only care about the
    8161             :     // range inside the loop.
    8162        5514 :     const SCEV *Zero = getZero(Distance->getType());
    8163        5514 :     const SCEV *One = getOne(Distance->getType());
    8164        2757 :     const SCEV *DistancePlusOne = getAddExpr(Distance, One);
    8165        2757 :     if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
    8166             :       // If Distance + 1 doesn't overflow, we can compute the maximum distance
    8167             :       // as "unsigned_max(Distance + 1) - 1".
    8168        3310 :       ConstantRange CR = getUnsignedRange(DistancePlusOne);
    8169        8275 :       MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1);
    8170             :     }
    8171        2757 :     return ExitLimit(Distance, getConstant(MaxBECount), false, Predicates);
    8172             :   }
    8173             : 
    8174             :   // If the condition controls loop exit (the loop exits only if the expression
    8175             :   // is true) and the addition is no-wrap we can use unsigned divide to
    8176             :   // compute the backedge count.  In this case, the step may not divide the
    8177             :   // distance, but we don't care because if the condition is "missed" the loop
    8178             :   // will have undefined behavior due to wrapping.
    8179        5031 :   if (ControlsExit && AddRec->hasNoSelfWrap() &&
    8180        2008 :       loopHasNoAbnormalExits(AddRec->getLoop())) {
    8181             :     const SCEV *Exact =
    8182         306 :         getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
    8183             :     const SCEV *Max =
    8184         306 :         Exact == getCouldNotCompute()
    8185         612 :             ? Exact
    8186         918 :             : getConstant(getUnsignedRangeMax(Exact));
    8187         306 :     return ExitLimit(Exact, Max, false, Predicates);
    8188             :   }
    8189             : 
    8190             :   // Solve the general equation.
    8191        2270 :   const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
    8192        1135 :                                                getNegativeSCEV(Start), *this);
    8193        1135 :   const SCEV *M = E == getCouldNotCompute()
    8194        1330 :                       ? E
    8195        1525 :                       : getConstant(getUnsignedRangeMax(E));
    8196        1135 :   return ExitLimit(E, M, false, Predicates);
    8197             : }
    8198             : 
    8199             : ScalarEvolution::ExitLimit
    8200         838 : ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) {
    8201             :   // Loops that look like: while (X == 0) are very strange indeed.  We don't
    8202             :   // handle them yet except for the trivial case.  This could be expanded in the
    8203             :   // future as needed.
    8204             : 
    8205             :   // If the value is a constant, check to see if it is known to be non-zero
    8206             :   // already.  If so, the backedge will execute zero times.
    8207          24 :   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
    8208          48 :     if (!C->getValue()->isZero())
    8209           0 :       return getZero(C->getType());
    8210          24 :     return getCouldNotCompute();  // Otherwise it will loop infinitely.
    8211             :   }
    8212             : 
    8213             :   // We could implement others, but I really doubt anyone writes loops like
    8214             :   // this, and if they did, they would already be constant folded.
    8215         814 :   return getCouldNotCompute();
    8216             : }
    8217             : 
    8218             : std::pair<BasicBlock *, BasicBlock *>
    8219       44889 : ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
    8220             :   // If the block has a unique predecessor, then there is no path from the
    8221             :   // predecessor to the block that does not go through the direct edge
    8222             :   // from the predecessor to the block.
    8223       89778 :   if (BasicBlock *Pred = BB->getSinglePredecessor())
    8224       22769 :     return {Pred, BB};
    8225             : 
    8226             :   // A loop's header is defined to be a block that dominates the loop.
    8227             :   // If the header has a unique predecessor outside the loop, it must be
    8228             :   // a block that has exactly one successor that can reach the loop.
    8229       44240 :   if (Loop *L = LI.getLoopFor(BB))
    8230       17985 :     return {L->getLoopPredecessor(), L->getHeader()};
    8231             : 
    8232       32250 :   return {nullptr, nullptr};
    8233             : }
    8234             : 
    8235             : /// SCEV structural equivalence is usually sufficient for testing whether two
    8236             : /// expressions are equal, however for the purposes of looking for a condition
    8237             : /// guarding a loop, it can be useful to be a little more general, since a
    8238             : /// front-end may have replicated the controlling expression.
    8239      347100 : static bool HasSameValue(const SCEV *A, const SCEV *B) {
    8240             :   // Quick check to see if they are the same SCEV.
    8241      347100 :   if (A == B) return true;
    8242             : 
    8243        4795 :   auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) {
    8244             :     // Not all instructions that are "identical" compute the same value.  For
    8245             :     // instance, two distinct alloca instructions allocating the same type are
    8246             :     // identical and do not read memory; but compute distinct values.
    8247        5037 :     return A->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A));
    8248        4795 :   };
    8249             : 
    8250             :   // Otherwise, if they're both SCEVUnknown, it's possible that they hold
    8251             :   // two different instructions with the same value. Check for this case.
    8252       43967 :   if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
    8253        7216 :     if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
    8254       12972 :       if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
    8255       10551 :         if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
    8256        4795 :           if (ComputesEqualValues(AI, BI))
    8257             :             return true;
    8258             : 
    8259             :   // Otherwise assume they may have a different value.
    8260             :   return false;
    8261             : }
    8262             : 
    8263      194339 : bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
    8264             :                                            const SCEV *&LHS, const SCEV *&RHS,
    8265             :                                            unsigned Depth) {
    8266      194339 :   bool Changed = false;
    8267             : 
    8268             :   // If we hit the max recursion limit bail out.
    8269      194339 :   if (Depth >= 3)
    8270             :     return false;
    8271             : 
    8272             :   // Canonicalize a constant to the right side.
    8273      214649 :   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
    8274             :     // Check for both operands constant.
    8275       30463 :     if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
    8276       20306 :       if (ConstantExpr::getICmp(Pred,
    8277             :                                 LHSC->getValue(),
    8278       10153 :                                 RHSC->getValue())->isNullValue())
    8279             :         goto trivially_false;
    8280             :       else
    8281             :         goto trivially_true;
    8282             :     }
    8283             :     // Otherwise swap the operands to put the constant on the right.
    8284       10157 :     std::swap(LHS, RHS);
    8285       10157 :     Pred = ICmpInst::getSwappedPredicate(Pred);
    8286       10157 :     Changed = true;
    8287             :   }
    8288             : 
    8289             :   // If we're comparing an addrec with a value which is loop-invariant in the
    8290             :   // addrec's loop, put the addrec on the left. Also make a dominance check,
    8291             :   // as both operands could be addrecs loop-invariant in each other's loop.
    8292      185238 :   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) {
    8293        1052 :     const Loop *L = AR->getLoop();
    8294        1244 :     if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) {
    8295         157 :       std::swap(LHS, RHS);
    8296         157 :       Pred = ICmpInst::getSwappedPredicate(Pred);
    8297         157 :       Changed = true;
    8298             :     }
    8299             :   }
    8300             : 
    8301             :   // If there's a constant operand, canonicalize comparisons with boundary
    8302             :   // cases, and canonicalize *-or-equal comparisons to regular comparisons.
    8303      332311 :   if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
    8304      148125 :     const APInt &RA = RC->getAPInt();
    8305             : 
    8306      148125 :     bool SimplifiedByConstantRange = false;
    8307             : 
    8308      296250 :     if (!ICmpInst::isEquality(Pred)) {
    8309      189986 :       ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA);
    8310       95053 :       if (ExactCR.isFullSet())
    8311             :         goto trivially_true;
    8312       95046 :       else if (ExactCR.isEmptySet())
    8313             :         goto trivially_false;
    8314             : 
    8315      189866 :       APInt NewRHS;
    8316             :       CmpInst::Predicate NewPred;
    8317      189866 :       if (ExactCR.getEquivalentICmp(NewPred, NewRHS) &&
    8318      189866 :           ICmpInst::isEquality(NewPred)) {
    8319             :         // We were able to convert an inequality to an equality.
    8320       19183 :         Pred = NewPred;
    8321       19183 :         RHS = getConstant(NewRHS);
    8322       19183 :         Changed = SimplifiedByConstantRange = true;
    8323             :       }
    8324             :     }
    8325             : 
    8326      148005 :     if (!SimplifiedByConstantRange) {
    8327      128822 :       switch (Pred) {
    8328             :       default:
    8329             :         break;
    8330       53072 :       case ICmpInst::ICMP_EQ:
    8331             :       case ICmpInst::ICMP_NE:
    8332             :         // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
    8333       53072 :         if (!RA)
    8334       25058 :           if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
    8335             :             if (const SCEVMulExpr *ME =
    8336        7147 :                     dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
    8337         597 :               if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
    8338         572 :                   ME->getOperand(0)->isAllOnesValue()) {
    8339         522 :                 RHS = AE->getOperand(1);
    8340         522 :                 LHS = ME->getOperand(1);
    8341         261 :                 Changed = true;
    8342             :               }
    8343             :         break;
    8344             : 
    8345             : 
    8346             :         // The "Should have been caught earlier!" messages refer to the fact
    8347             :         // that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
    8348             :         // should have fired on the corresponding cases, and canonicalized the
    8349             :         // check to trivially_true or trivially_false.
    8350             : 
    8351        4161 :       case ICmpInst::ICMP_UGE:
    8352             :         assert(!RA.isMinValue() && "Should have been caught earlier!");
    8353        4161 :         Pred = ICmpInst::ICMP_UGT;
    8354       16644 :         RHS = getConstant(RA - 1);
    8355        4161 :         Changed = true;
    8356        4161 :         break;
    8357        1381 :       case ICmpInst::ICMP_ULE:
    8358             :         assert(!RA.isMaxValue() && "Should have been caught earlier!");
    8359        1381 :         Pred = ICmpInst::ICMP_ULT;
    8360        5524 :         RHS = getConstant(RA + 1);
    8361        1381 :         Changed = true;
    8362        1381 :         break;
    8363        5072 :       case ICmpInst::ICMP_SGE:
    8364             :         assert(!RA.isMinSignedValue() && "Should have been caught earlier!");
    8365        5072 :         Pred = ICmpInst::ICMP_SGT;
    8366       20288 :         RHS = getConstant(RA - 1);
    8367        5072 :         Changed = true;
    8368        5072 :         break;
    8369        2743 :       case ICmpInst::ICMP_SLE:
    8370             :         assert(!RA.isMaxSignedValue() && "Should have been caught earlier!");
    8371        2743 :         Pred = ICmpInst::ICMP_SLT;
    8372       10972 :         RHS = getConstant(RA + 1);
    8373        2743 :         Changed = true;
    8374        2743 :         break;
    8375             :       }
    8376             :     }
    8377             :   }
    8378             : 
    8379             :   // Check for obvious equality.
    8380      184066 :   if (HasSameValue(LHS, RHS)) {
    8381         796 :     if (ICmpInst::isTrueWhenEqual(Pred))
    8382             :       goto trivially_true;
    8383          30 :     if (ICmpInst::isFalseWhenEqual(Pred))
    8384             :       goto trivially_false;
    8385             :   }
    8386             : 
    8387             :   // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
    8388             :   // adding or subtracting 1 from one of the operands.
    8389      183270 :   switch (Pred) {
    8390        1153 :   case ICmpInst::ICMP_SLE:
    8391        3459 :     if (!getSignedRangeMax(RHS).isMaxSignedValue()) {
    8392         276 :       RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
    8393             :                        SCEV::FlagNSW);
    8394         276 :       Pred = ICmpInst::ICMP_SLT;
    8395         276 :       Changed = true;
    8396        2631 :     } else if (!getSignedRangeMin(LHS).isMinSignedValue()) {
    8397          71 :       LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS,
    8398             :                        SCEV::FlagNSW);
    8399          71 :       Pred = ICmpInst::ICMP_SLT;
    8400          71 :       Changed = true;
    8401             :     }
    8402             :     break;
    8403        1560 :   case ICmpInst::ICMP_SGE:
    8404        4680 :     if (!getSignedRangeMin(RHS).isMinSignedValue()) {
    8405         228 :       RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS,
    8406             :                        SCEV::FlagNSW);
    8407         228 :       Pred = ICmpInst::ICMP_SGT;
    8408         228 :       Changed = true;
    8409        3996 :     } else if (!getSignedRangeMax(LHS).isMaxSignedValue()) {
    8410         106 :       LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
    8411             :                        SCEV::FlagNSW);
    8412         106 :       Pred = ICmpInst::ICMP_SGT;
    8413         106 :       Changed = true;
    8414             :     }
    8415             :     break;
    8416         958 :   case ICmpInst::ICMP_ULE:
    8417        3832 :     if (!getUnsignedRangeMax(RHS).isMaxValue()) {
    8418         275 :       RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
    8419             :                        SCEV::FlagNUW);
    8420         275 :       Pred = ICmpInst::ICMP_ULT;
    8421         275 :       Changed = true;
    8422        2732 :     } else if (!getUnsignedRangeMin(LHS).isMinValue()) {
    8423          68 :       LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS);
    8424          68 :       Pred = ICmpInst::ICMP_ULT;
    8425          68 :       Changed = true;
    8426             :     }
    8427             :     break;
    8428        1475 :   case ICmpInst::ICMP_UGE:
    8429        5900 :     if (!getUnsignedRangeMin(RHS).isMinValue()) {
    8430          48 :       RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
    8431          48 :       Pred = ICmpInst::ICMP_UGT;
    8432          48 :       Changed = true;
    8433        5708 :     } else if (!getUnsignedRangeMax(LHS).isMaxValue()) {
    8434         143 :       LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
    8435             :                        SCEV::FlagNUW);
    8436         143 :       Pred = ICmpInst::ICMP_UGT;
    8437         143 :       Changed = true;
    8438             :     }
    8439             :     break;
    8440             :   default:
    8441             :     break;
    8442             :   }
    8443             : 
    8444             :   // TODO: More simplifications are possible here.
    8445             : 
    8446             :   // Recursively simplify until we either hit a recursion limit or nothing
    8447             :   // changes.
    8448      182055 :   if (Changed)
    8449       39148 :     return SimplifyICmpOperands(Pred, LHS, RHS, Depth+1);
    8450             : 
    8451             :   return Changed;
    8452             : 
    8453        2419 : trivially_true:
    8454             :   // Return 0 == 0.
    8455        4852 :   LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
    8456        2426 :   Pred = ICmpInst::ICMP_EQ;
    8457        2426 :   return true;
    8458             : 
    8459        8530 : trivially_false:
    8460             :   // Return 0 != 0.
    8461       17286 :   LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
    8462        8643 :   Pred = ICmpInst::ICMP_NE;
    8463        8643 :   return true;
    8464             : }
    8465             : 
    8466       22868 : bool ScalarEvolution::isKnownNegative(const SCEV *S) {
    8467       68604 :   return getSignedRangeMax(S).isNegative();
    8468             : }
    8469             : 
    8470       36457 : bool ScalarEvolution::isKnownPositive(const SCEV *S) {
    8471       72914 :   return getSignedRangeMin(S).isStrictlyPositive();
    8472             : }
    8473             : 
    8474      748481 : bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
    8475     2245443 :   return !getSignedRangeMin(S).isNegative();
    8476             : }
    8477             : 
    8478      118035 : bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
    8479      236070 :   return !getSignedRangeMax(S).isStrictlyPositive();
    8480             : }
    8481             : 
    8482        8088 : bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
    8483        8088 :   return isKnownNegative(S) || isKnownPositive(S);
    8484             : }
    8485             : 
    8486       31008 : bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
    8487             :                                        const SCEV *LHS, const SCEV *RHS) {
    8488             :   // Canonicalize the inputs first.
    8489       31008 :   (void)SimplifyICmpOperands(Pred, LHS, RHS);
    8490             : 
    8491             :   // If LHS or RHS is an addrec, check to see if the condition is true in
    8492             :   // every iteration of the loop.
    8493             :   // If LHS and RHS are both addrec, both conditions must be true in
    8494             :   // every iteration of the loop.
    8495       62016 :   const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS);
    8496       62016 :   const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
    8497       31008 :   bool LeftGuarded = false;
    8498       31008 :   bool RightGuarded = false;
    8499       31008 :   if (LAR) {
    8500       13669 :     const Loop *L = LAR->getLoop();
    8501       19172 :     if (isLoopEntryGuardedByCond(L, Pred, LAR->getStart(), RHS) &&
    8502        5503 :         isLoopBackedgeGuardedByCond(L, Pred, LAR->getPostIncExpr(*this), RHS)) {
    8503        1615 :       if (!RAR) return true;
    8504             :       LeftGuarded = true;
    8505             :     }
    8506             :   }
    8507       29393 :   if (RAR) {
    8508         129 :     const Loop *L = RAR->getLoop();
    8509         147 :     if (isLoopEntryGuardedByCond(L, Pred, LHS, RAR->getStart()) &&
    8510          18 :         isLoopBackedgeGuardedByCond(L, Pred, LHS, RAR->getPostIncExpr(*this))) {
    8511           1 :       if (!LAR) return true;
    8512             :       RightGuarded = true;
    8513             :     }
    8514             :   }
    8515       29393 :   if (LeftGuarded && RightGuarded)
    8516             :     return true;
    8517             : 
    8518       29393 :   if (isKnownPredicateViaSplitting(Pred, LHS, RHS))
    8519             :     return true;
    8520             : 
    8521             :   // Otherwise see what can be done with known constant ranges.
    8522       29299 :   return isKnownPredicateViaConstantRanges(Pred, LHS, RHS);
    8523             : }
    8524             : 
    8525         901 : bool ScalarEvolution::isMonotonicPredicate(const SCEVAddRecExpr *LHS,
    8526             :                                            ICmpInst::Predicate Pred,
    8527             :                                            bool &Increasing) {
    8528         901 :   bool Result = isMonotonicPredicateImpl(LHS, Pred, Increasing);
    8529             : 
    8530             : #ifndef NDEBUG
    8531             :   // Verify an invariant: inverting the predicate should turn a monotonically
    8532             :   // increasing change to a monotonically decreasing one, and vice versa.
    8533             :   bool IncreasingSwapped;
    8534             :   bool ResultSwapped = isMonotonicPredicateImpl(
    8535             :       LHS, ICmpInst::getSwappedPredicate(Pred), IncreasingSwapped);
    8536             : 
    8537             :   assert(Result == ResultSwapped && "should be able to analyze both!");
    8538             :   if (ResultSwapped)
    8539             :     assert(Increasing == !IncreasingSwapped &&
    8540             :            "monotonicity should flip as we flip the predicate");
    8541             : #endif
    8542             : 
    8543         901 :   return Result;
    8544             : }
    8545             : 
    8546         901 : bool ScalarEvolution::isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS,
    8547             :                                                ICmpInst::Predicate Pred,
    8548             :                                                bool &Increasing) {
    8549             : 
    8550             :   // A zero step value for LHS means the induction variable is essentially a
    8551             :   // loop invariant value. We don't really depend on the predicate actually
    8552             :   // flipping from false to true (for increasing predicates, and the other way
    8553             :   // around for decreasing predicates), all we care about is that *if* the
    8554             :   // predicate changes then it only changes from false to true.
    8555             :   //
    8556             :   // A zero step value in itself is not very useful, but there may be places
    8557             :   // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be
    8558             :   // as general as possible.
    8559             : 
    8560         901 :   switch (Pred) {
    8561             :   default:
    8562             :     return false; // Conservative answer
    8563             : 
    8564         333 :   case ICmpInst::ICMP_UGT:
    8565             :   case ICmpInst::ICMP_UGE:
    8566             :   case ICmpInst::ICMP_ULT:
    8567             :   case ICmpInst::ICMP_ULE:
    8568         666 :     if (!LHS->hasNoUnsignedWrap())
    8569             :       return false;
    8570             : 
    8571         230 :     Increasing = Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE;
    8572         230 :     return true;
    8573             : 
    8574         393 :   case ICmpInst::ICMP_SGT:
    8575             :   case ICmpInst::ICMP_SGE:
    8576             :   case ICmpInst::ICMP_SLT:
    8577             :   case ICmpInst::ICMP_SLE: {
    8578         786 :     if (!LHS->hasNoSignedWrap())
    8579             :       return false;
    8580             : 
    8581         380 :     const SCEV *Step = LHS->getStepRecurrence(*this);
    8582             : 
    8583         380 :     if (isKnownNonNegative(Step)) {
    8584         279 :       Increasing = Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE;
    8585         279 :       return true;
    8586             :     }
    8587             : 
    8588         101 :     if (isKnownNonPositive(Step)) {
    8589          99 :       Increasing = Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE;
    8590          99 :       return true;
    8591             :     }
    8592             : 
    8593             :     return false;
    8594             :   }
    8595             : 
    8596             :   }
    8597             : 
    8598             :   llvm_unreachable("switch has default clause!");
    8599             : }
    8600             : 
    8601        1341 : bool ScalarEvolution::isLoopInvariantPredicate(
    8602             :     ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
    8603             :     ICmpInst::Predicate &InvariantPred, const SCEV *&InvariantLHS,
    8604             :     const SCEV *&InvariantRHS) {
    8605             : 
    8606             :   // If there is a loop-invariant, force it into the RHS, otherwise bail out.
    8607        1341 :   if (!isLoopInvariant(RHS, L)) {
    8608         335 :     if (!isLoopInvariant(LHS, L))
    8609             :       return false;
    8610             : 
    8611           0 :     std::swap(LHS, RHS);
    8612           0 :     Pred = ICmpInst::getSwappedPredicate(Pred);
    8613             :   }
    8614             : 
    8615        1857 :   const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS);
    8616         851 :   if (!ArLHS || ArLHS->getLoop() != L)
    8617             :     return false;
    8618             : 
    8619             :   bool Increasing;
    8620         851 :   if (!isMonotonicPredicate(ArLHS, Pred, Increasing))
    8621             :     return false;
    8622             : 
    8623             :   // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to
    8624             :   // true as the loop iterates, and the backedge is control dependent on
    8625             :   // "ArLHS `Pred` RHS" == true then we can reason as follows:
    8626             :   //
    8627             :   //   * if the predicate was false in the first iteration then the predicate
    8628             :   //     is never evaluated again, since the loop exits without taking the
    8629             :   //     backedge.
    8630             :   //   * if the predicate was true in the first iteration then it will
    8631             :   //     continue to be true for all future iterations since it is
    8632             :   //     monotonically increasing.
    8633             :   //
    8634             :   // For both the above possibilities, we can replace the loop varying
    8635             :   // predicate with its value on the first iteration of the loop (which is
    8636             :   // loop invariant).
    8637             :   //
    8638             :   // A similar reasoning applies for a monotonically decreasing predicate, by
    8639             :   // replacing true with false and false with true in the above two bullets.
    8640             : 
    8641         568 :   auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred);
    8642             : 
    8643         568 :   if (!isLoopBackedgeGuardedByCond(L, P, LHS, RHS))
    8644             :     return false;
    8645             : 
    8646           9 :   InvariantPred = Pred;
    8647           9 :   InvariantLHS = ArLHS->getStart();
    8648           9 :   InvariantRHS = RHS;
    8649           9 :   return true;
    8650             : }
    8651             : 
    8652      142887 : bool ScalarEvolution::isKnownPredicateViaConstantRanges(
    8653             :     ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) {
    8654      142887 :   if (HasSameValue(LHS, RHS))
    8655       15991 :     return ICmpInst::isTrueWhenEqual(Pred);
    8656             : 
    8657             :   // This code is split out from isKnownPredicate because it is called from
    8658             :   // within isLoopEntryGuardedByCond.
    8659             : 
    8660             :   auto CheckRanges =
    8661      129266 :       [&](const ConstantRange &RangeLHS, const ConstantRange &RangeRHS) {
    8662      258532 :     return ConstantRange::makeSatisfyingICmpRegion(Pred, RangeRHS)
    8663             :         .contains(RangeLHS);
    8664      385428 :   };
    8665             : 
    8666             :   // The check at the top of the function catches the case where the values are
    8667             :   // known to be equal.
    8668      126896 :   if (Pred == CmpInst::ICMP_EQ)
    8669             :     return false;
    8670             : 
    8671      121303 :   if (Pred == CmpInst::ICMP_NE)
    8672       45419 :     return CheckRanges(getSignedRange(LHS), getSignedRange(RHS)) ||
    8673       53336 :            CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS)) ||
    8674        7917 :            isKnownNonZero(getMinusSCEV(LHS, RHS));
    8675             : 
    8676      111472 :   if (CmpInst::isSigned(Pred))
    8677       95412 :     return CheckRanges(getSignedRange(LHS), getSignedRange(RHS));
    8678             : 
    8679      127532 :   return CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS));
    8680             : }
    8681             : 
    8682       46197 : bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,
    8683             :                                                     const SCEV *LHS,
    8684             :                                                     const SCEV *RHS) {
    8685             :   // Match Result to (X + Y)<ExpectedFlags> where Y is a constant integer.
    8686             :   // Return Y via OutY.
    8687             :   auto MatchBinaryAddToConst =
    8688             :       [this](const SCEV *Result, const SCEV *X, APInt &OutY,
    8689       32366 :              SCEV::NoWrapFlags ExpectedFlags) {
    8690             :     const SCEV *NonConstOp, *ConstOp;
    8691             :     SCEV::NoWrapFlags FlagsPresent;
    8692             : 
    8693       36360 :     if (!splitBinaryAdd(Result, ConstOp, NonConstOp, FlagsPresent) ||
    8694       35856 :         !isa<SCEVConstant>(ConstOp) || NonConstOp != X)
    8695             :       return false;
    8696             : 
    8697        1095 :     OutY = cast<SCEVConstant>(ConstOp)->getAPInt();
    8698         365 :     return (FlagsPresent & ExpectedFlags) == ExpectedFlags;
    8699       46197 :   };
    8700             : 
    8701       92394 :   APInt C;
    8702             : 
    8703       46197 :   switch (Pred) {
    8704             :   default:
    8705             :     break;
    8706             : 
    8707        5276 :   case ICmpInst::ICMP_SGE:
    8708             :     std::swap(LHS, RHS);
    8709             :     LLVM_FALLTHROUGH;
    8710       14419 :   case ICmpInst::ICMP_SLE:
    8711             :     // X s<= (X + C)<nsw> if C >= 0
    8712       14514 :     if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) && C.isNonNegative())
    8713             :       return true;
    8714             : 
    8715             :     // (X + C)<nsw> s<= X if C <= 0
    8716       14376 :     if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) &&
    8717          37 :         !C.isStrictlyPositive())
    8718             :       return true;
    8719             :     break;
    8720             : 
    8721        1804 :   case ICmpInst::ICMP_SGT:
    8722             :     std::swap(LHS, RHS);
    8723             :     LLVM_FALLTHROUGH;
    8724        1804 :   case ICmpInst::ICMP_SLT:
    8725             :     // X s< (X + C)<nsw> if C > 0
    8726        1804 :     if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) &&
    8727           0 :         C.isStrictlyPositive())
    8728             :       return true;
    8729             : 
    8730             :     // (X + C)<nsw> s< X if C < 0
    8731        1804 :     if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) && C.isNegative())
    8732             :       return true;
    8733             :     break;
    8734             :   }
    8735             : 
    8736             :   return false;
    8737             : }
    8738             : 
    8739       29393 : bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred,
    8740             :                                                    const SCEV *LHS,
    8741             :                                                    const SCEV *RHS) {
    8742       29393 :   if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate)
    8743             :     return false;
    8744             : 
    8745             :   // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on
    8746             :   // the stack can result in exponential time complexity.
    8747        4226 :   SaveAndRestore<bool> Restore(ProvingSplitPredicate, true);
    8748             : 
    8749             :   // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L
    8750             :   //
    8751             :   // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use
    8752             :   // isKnownPredicate.  isKnownPredicate is more powerful, but also more
    8753             :   // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the
    8754             :   // interesting cases seen in practice.  We can consider "upgrading" L >= 0 to
    8755             :   // use isKnownPredicate later if needed.
    8756        3586 :   return isKnownNonNegative(RHS) &&
    8757        4743 :          isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) &&
    8758        3270 :          isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS);
    8759             : }
    8760             : 
    8761       80379 : bool ScalarEvolution::isImpliedViaGuard(BasicBlock *BB,
    8762             :                                         ICmpInst::Predicate Pred,
    8763             :                                         const SCEV *LHS, const SCEV *RHS) {
    8764             :   // No need to even try if we know the module has no guards.
    8765       80379 :   if (!HasGuards)
    8766             :     return false;
    8767             : 
    8768        1372 :   return any_of(*BB, [&](Instruction &I) {
    8769             :     using namespace llvm::PatternMatch;
    8770             : 
    8771             :     Value *Condition;
    8772        3057 :     return match(&I, m_Intrinsic<Intrinsic::experimental_guard>(
    8773        2141 :                          m_Value(Condition))) &&
    8774        1122 :            isImpliedCond(Pred, LHS, RHS, Condition, false);
    8775         353 :   });
    8776             : }
    8777             : 
    8778             : /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
    8779             : /// protected by a conditional between LHS and RHS.  This is used to
    8780             : /// to eliminate casts.
    8781             : bool
    8782       27397 : ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
    8783             :                                              ICmpInst::Predicate Pred,
    8784             :                                              const SCEV *LHS, const SCEV *RHS) {
    8785             :   // Interpret a null as meaning no loop, where there is obviously no guard
    8786             :   // (interprocedural conditions notwithstanding).
    8787       27397 :   if (!L) return true;
    8788             : 
    8789       27397 :   if (isKnownPredicateViaConstantRanges(Pred, LHS, RHS))
    8790             :     return true;
    8791             : 
    8792       16086 :   BasicBlock *Latch = L->getLoopLatch();
    8793       16086 :   if (!Latch)
    8794             :     return false;
    8795             : 
    8796             :   BranchInst *LoopContinuePredicate =
    8797       32169 :     dyn_cast<BranchInst>(Latch->getTerminator());
    8798       31375 :   if (LoopContinuePredicate && LoopContinuePredicate->isConditional() &&
    8799       30584 :       isImpliedCond(Pred, LHS, RHS,
    8800             :                     LoopContinuePredicate->getCondition(),
    8801       30584 :                     LoopContinuePredicate->getSuccessor(0) != L->getHeader()))
    8802             :     return true;
    8803             : 
    8804             :   // We don't want more than one activation of the following loops on the stack
    8805             :   // -- that can lead to O(n!) time complexity.
    8806       15311 :   if (WalkingBEDominatingConds)
    8807             :     return false;
    8808             : 
    8809       28518 :   SaveAndRestore<bool> ClearOnExit(WalkingBEDominatingConds, true);
    8810             : 
    8811             :   // See if we can exploit a trip count to prove the predicate.
    8812       14259 :   const auto &BETakenInfo = getBackedgeTakenInfo(L);
    8813       14259 :   const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this);
    8814       14259 :   if (LatchBECount != getCouldNotCompute()) {
    8815             :     // We know that Latch branches back to the loop header exactly
    8816             :     // LatchBECount times.  This means the backdege condition at Latch is
    8817             :     // equivalent to  "{0,+,1} u< LatchBECount".
    8818       13158 :     Type *Ty = LatchBECount->getType();
    8819       13158 :     auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
    8820             :     const SCEV *LoopCounter =
    8821       26316 :       getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags);
    8822       13158 :     if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter,
    8823             :                       LatchBECount))
    8824             :       return true;
    8825             :   }
    8826             : 
    8827             :   // Check conditions due to any @llvm.assume intrinsics.
    8828       60405 :   for (auto &AssumeVH : AC.assumptions()) {
    8829        3479 :     if (!AssumeVH)
    8830           0 :       continue;
    8831        3479 :     auto *CI = cast<CallInst>(AssumeVH);
    8832        6958 :     if (!DT.dominates(CI, Latch->getTerminator()))
    8833           0 :       continue;
    8834             : 
    8835        3479 :     if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false))
    8836             :       return true;
    8837             :   }
    8838             : 
    8839             :   // If the loop is not reachable from the entry block, we risk running into an
    8840             :   // infinite loop as we walk up into the dom tree.  These loops do not matter
    8841             :   // anyway, so we just return a conservative answer when we see them.
    8842       28460 :   if (!DT.isReachableFromEntry(L->getHeader()))
    8843             :     return false;
    8844             : 
    8845       14230 :   if (isImpliedViaGuard(Latch, Pred, LHS, RHS))
    8846             :     return true;
    8847             : 
    8848       74716 :   for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()];
    8849       32065 :        DTN != HeaderDTN; DTN = DTN->getIDom()) {
    8850             :     assert(DTN && "should reach the loop header before reaching the root!");
    8851             : 
    8852       17903 :     BasicBlock *BB = DTN->getBlock();
    8853       17903 :     if (isImpliedViaGuard(BB, Pred, LHS, RHS))
    8854          55 :       return true;
    8855             : 
    8856       17903 :     BasicBlock *PBB = BB->getSinglePredecessor();
    8857       17903 :     if (!PBB)
    8858       22396 :       continue;
    8859             : 
    8860       14346 :     BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator());
    8861       10279 :     if (!ContinuePredicate || !ContinuePredicate->isConditional())
    8862        5276 :       continue;
    8863             : 
    8864        4067 :     Value *Condition = ContinuePredicate->getCondition();
    8865             : 
    8866             :     // If we have an edge `E` within the loop body that dominates the only
    8867             :     // latch, the condition guarding `E` also guards the backedge.  This
    8868             :     // reasoning works only for loops with a single latch.
    8869             : 
    8870        4067 :     BasicBlockEdge DominatingEdge(PBB, BB);
    8871        4067 :     if (DominatingEdge.isSingleEdge()) {
    8872             :       // We're constructively (and conservatively) enumerating edges within the
    8873             :       // loop body that dominate the latch.  The dominator tree better agree
    8874             :       // with us on this:
    8875             :       assert(DT.dominates(DominatingEdge, Latch) && "should be!");
    8876             : 
    8877        4067 :       if (isImpliedCond(Pred, LHS, RHS, Condition,
    8878        4067 :                         BB != ContinuePredicate->getSuccessor(0)))
    8879             :         return true;
    8880             :     }
    8881             :   }
    8882             : 
    8883             :   return false;
    8884             : }
    8885             : 
    8886             : bool
    8887       27794 : ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
    8888             :                                           ICmpInst::Predicate Pred,
    8889             :                                           const SCEV *LHS, const SCEV *RHS) {
    8890             :   // Interpret a null as meaning no loop, where there is obviously no guard
    8891             :   // (interprocedural conditions notwithstanding).
    8892       27794 :   if (!L) return false;
    8893             : 
    8894       27794 :   if (isKnownPredicateViaConstantRanges(Pred, LHS, RHS))
    8895             :     return true;
    8896             : 
    8897             :   // Starting at the loop predecessor, climb up the predecessor chain, as long
    8898             :   // as there are predecessors that can be found that have unique successors
    8899             :   // leading to the original header.
    8900       89778 :   for (std::pair<BasicBlock *, BasicBlock *>
    8901       58458 :          Pair(L->getLoopPredecessor(), L->getHeader());
    8902       64375 :        Pair.first;
    8903       89778 :        Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
    8904             : 
    8905       48246 :     if (isImpliedViaGuard(Pair.first, Pred, LHS, RHS))
    8906             :       return true;
    8907             : 
    8908             :     BranchInst *LoopEntryPredicate =
    8909      141616 :       dyn_cast<BranchInst>(Pair.first->getTerminator());
    8910       74080 :     if (!LoopEntryPredicate ||
    8911       45156 :         LoopEntryPredicate->isUnconditional())
    8912       28924 :       continue;
    8913             : 
    8914       38612 :     if (isImpliedCond(Pred, LHS, RHS,
    8915             :                       LoopEntryPredicate->getCondition(),
    8916       19306 :                       LoopEntryPredicate->getSuccessor(0) != Pair.second))
    8917             :       return true;
    8918             :   }
    8919             : 
    8920             :   // Check conditions due to any @llvm.assume intrinsics.
    8921       74541 :   for (auto &AssumeVH : AC.assumptions()) {
    8922       10034 :     if (!AssumeVH)
    8923           0 :       continue;
    8924       10034 :     auto *CI = cast<CallInst>(AssumeVH);
    8925       20068 :     if (!DT.dominates(CI, L->getHeader()))
    8926        9993 :       continue;
    8927             : 
    8928          41 :     if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false))
    8929             :       return true;
    8930             :   }
    8931             : 
    8932             :   return false;
    8933             : }
    8934             : 
    8935       42771 : bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred,
    8936             :                                     const SCEV *LHS, const SCEV *RHS,
    8937             :                                     Value *FoundCondValue,
    8938             :                                     bool Inverse) {
    8939       42771 :   if (!PendingLoopPredicates.insert(FoundCondValue).second)
    8940             :     return false;
    8941             : 
    8942             :   auto ClearOnExit =
    8943      164612 :       make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); });
    8944             : 
    8945             :   // Recursively handle And and Or conditions.
    8946       41591 :   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) {
    8947         438 :     if (BO->getOpcode() == Instruction::And) {
    8948         267 :       if (!Inverse)
    8949         561 :         return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
    8950         330 :                isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
    8951         171 :     } else if (BO->getOpcode() == Instruction::Or) {
    8952         151 :       if (Inverse)
    8953         181 :         return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
    8954         118 :                isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
    8955             :     }
    8956             :   }
    8957             : 
    8958       80340 :   ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue);
    8959             :   if (!ICI) return false;
    8960             : 
    8961             :   // Now that we found a conditional branch that dominates the loop or controls
    8962             :   // the loop latch. Check to see if it is the comparison we are looking for.
    8963             :   ICmpInst::Predicate FoundPred;
    8964       39446 :   if (Inverse)
    8965       37096 :     FoundPred = ICI->getInversePredicate();
    8966             :   else
    8967       41796 :     FoundPred = ICI->getPredicate();
    8968             : 
    8969       78892 :   const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
    8970       78892 :   const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
    8971             : 
    8972       39446 :   return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS);
    8973             : }
    8974             : 
    8975       52604 : bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS,
    8976             :                                     const SCEV *RHS,
    8977             :                                     ICmpInst::Predicate FoundPred,
    8978             :                                     const SCEV *FoundLHS,
    8979             :                                     const SCEV *FoundRHS) {
    8980             :   // Balance the types.
    8981      105208 :   if (getTypeSizeInBits(LHS->getType()) <
    8982       52604 :       getTypeSizeInBits(FoundLHS->getType())) {
    8983        5891 :     if (CmpInst::isSigned(Pred)) {
    8984         802 :       LHS = getSignExtendExpr(LHS, FoundLHS->getType());
    8985         802 :       RHS = getSignExtendExpr(RHS, FoundLHS->getType());
    8986             :     } else {
    8987        5089 :       LHS = getZeroExtendExpr(LHS, FoundLHS->getType());
    8988        5089 :       RHS = getZeroExtendExpr(RHS, FoundLHS->getType());
    8989             :     }
    8990       93426 :   } else if (getTypeSizeInBits(LHS->getType()) >
    8991       46713 :       getTypeSizeInBits(FoundLHS->getType())) {
    8992        3899 :     if (CmpInst::isSigned(FoundPred)) {
    8993         701 :       FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
    8994         701 :       FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
    8995             :     } else {
    8996        3198 :       FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
    8997        3198 :       FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
    8998             :     }
    8999             :   }
    9000             : 
    9001             :   // Canonicalize the query to match the way instcombine will have
    9002             :   // canonicalized the comparison.
    9003       52604 :   if (SimplifyICmpOperands(Pred, LHS, RHS))
    9004        1493 :     if (LHS == RHS)
    9005        1493 :       return CmpInst::isTrueWhenEqual(Pred);
    9006       51111 :   if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS))
    9007          36 :     if (FoundLHS == FoundRHS)
    9008          36 :       return CmpInst::isFalseWhenEqual(FoundPred);
    9009             : 
    9010             :   // Check to see if we can make the LHS or RHS match.
    9011       51075 :   if (LHS == FoundRHS || RHS == FoundLHS) {
    9012         804 :     if (isa<SCEVConstant>(RHS)) {
    9013         171 :       std::swap(FoundLHS, FoundRHS);
    9014         171 :       FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
    9015             :     } else {
    9016         231 :       std::swap(LHS, RHS);
    9017         231 :       Pred = ICmpInst::getSwappedPredicate(Pred);
    9018             :     }
    9019             :   }
    9020             : 
    9021             :   // Check whether the found predicate is the same as the desired predicate.
    9022       51075 :   if (FoundPred == Pred)
    9023       15866 :     return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
    9024             : 
    9025             :   // Check whether swapping the found predicate makes it the same as the
    9026             :   // desired predicate.
    9027       35209 :   if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
    9028        4684 :     if (isa<SCEVConstant>(RHS))
    9029        2226 :       return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
    9030             :     else
    9031         116 :       return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
    9032         116 :                                    RHS, LHS, FoundLHS, FoundRHS);
    9033             :   }
    9034             : 
    9035             :   // Unsigned comparison is the same as signed comparison when both the operands
    9036             :   // are non-negative.
    9037       49697 :   if (CmpInst::isUnsigned(FoundPred) &&
    9038       20466 :       CmpInst::getSignedPredicate(FoundPred) == Pred &&
    9039       39390 :       isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS))
    9040        2556 :     return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
    9041             : 
    9042             :   // Check if we can make progress by sharpening ranges.
    9043       39719 :   if (FoundPred == ICmpInst::ICMP_NE &&
    9044       28224 :       (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) {
    9045             : 
    9046        7339 :     const SCEVConstant *C = nullptr;
    9047        7339 :     const SCEV *V = nullptr;
    9048             : 
    9049       14678 :     if (isa<SCEVConstant>(FoundLHS)) {
    9050           0 :       C = cast<SCEVConstant>(FoundLHS);
    9051           0 :       V = FoundRHS;
    9052             :     } else {
    9053       14678 :       C = cast<SCEVConstant>(FoundRHS);
    9054        7339 :       V = FoundLHS;
    9055             :     }
    9056             : 
    9057             :     // The guarding predicate tells us that C != V. If the known range
    9058             :     // of V is [C, t), we can sharpen the range to [C + 1, t).  The
    9059             :     // range we consider has to correspond to same signedness as the
    9060             :     // predicate we're interested in folding.
    9061             : 
    9062        7339 :     APInt Min = ICmpInst::isSigned(Pred) ?
    9063       14655 :         getSignedRangeMin(V) : getUnsignedRangeMin(V);
    9064             : 
    9065       14678 :     if (Min == C->getAPInt()) {
    9066             :       // Given (V >= Min && V != Min) we conclude V >= (Min + 1).
    9067             :       // This is true even if (Min + 1) wraps around -- in case of
    9068             :       // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)).
    9069             : 
    9070        8509 :       APInt SharperMin = Min + 1;
    9071             : 
    9072        2133 :       switch (Pred) {
    9073          38 :         case ICmpInst::ICMP_SGE:
    9074             :         case ICmpInst::ICMP_UGE:
    9075             :           // We know V `Pred` SharperMin.  If this implies LHS `Pred`
    9076             :           // RHS, we're done.
    9077          38 :           if (isImpliedCondOperands(Pred, LHS, RHS, V,
    9078             :                                     getConstant(SharperMin)))
    9079          23 :             return true;
    9080             :           LLVM_FALLTHROUGH;
    9081             : 
    9082             :         case ICmpInst::ICMP_SGT:
    9083             :         case ICmpInst::ICMP_UGT:
    9084             :           // We know from the range information that (V `Pred` Min ||
    9085             :           // V == Min).  We know from the guarding condition that !(V
    9086             :           // == Min).  This gives us
    9087             :           //
    9088             :           //       V `Pred` Min || V == Min && !(V == Min)
    9089             :           //   =>  V `Pred` Min
    9090             :           //
    9091             :           // If V `Pred` Min implies LHS `Pred` RHS, we're done.
    9092             : 
    9093         673 :           if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min)))
    9094             :             return true;
    9095             :           LLVM_FALLTHROUGH;
    9096             : 
    9097             :         default:
    9098             :           // No change
    9099             :           break;
    9100             :       }
    9101             :     }
    9102             :   }
    9103             : 
    9104             :   // Check whether the actual condition is beyond sufficient.
    9105       30288 :   if (FoundPred == ICmpInst::ICMP_EQ)
    9106        2281 :     if (ICmpInst::isTrueWhenEqual(Pred))
    9107           3 :       if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
    9108             :         return true;
    9109       30288 :   if (Pred == ICmpInst::ICMP_NE)
    9110       14717 :     if (!ICmpInst::isTrueWhenEqual(FoundPred))
    9111       13057 :       if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
    9112             :         return true;
    9113             : 
    9114             :   // Otherwise assume the worst.
    9115             :   return false;
    9116             : }
    9117             : 
    9118       74956 : bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr,
    9119             :                                      const SCEV *&L, const SCEV *&R,
    9120             :                                      SCEV::NoWrapFlags &Flags) {
    9121       18367 :   const auto *AE = dyn_cast<SCEVAddExpr>(Expr);
    9122       18367 :   if (!AE || AE->getNumOperands() != 2)
    9123             :     return false;
    9124             : 
    9125       33130 :   L = AE->getOperand(0);
    9126       33130 :   R = AE->getOperand(1);
    9127       33130 :   Flags = AE->getNoWrapFlags();
    9128       16565 :   return true;
    9129             : }
    9130             : 
    9131       48767 : Optional<APInt> ScalarEvolution::computeConstantDifference(const SCEV *More,
    9132             :                                                            const SCEV *Less) {
    9133             :   // We avoid subtracting expressions here because this function is usually
    9134             :   // fairly deep in the call stack (i.e. is called many times).
    9135             : 
    9136       75911 :   if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
    9137       51432 :     const auto *LAR = cast<SCEVAddRecExpr>(Less);
    9138       51432 :     const auto *MAR = cast<SCEVAddRecExpr>(More);
    9139             : 
    9140       25716 :     if (LAR->getLoop() != MAR->getLoop())
    9141             :       return None;
    9142             : 
    9143             :     // We look at affine expressions only; not for correctness but to keep
    9144             :     // getStepRecurrence cheap.
    9145       51228 :     if (!LAR->isAffine() || !MAR->isAffine())
    9146             :       return None;
    9147             : 
    9148       25600 :     if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
    9149             :       return None;
    9150             : 
    9151       19526 :     Less = LAR->getStart();
    9152       19526 :     More = MAR->getStart();
    9153             : 
    9154             :     // fall through
    9155             :   }
    9156             : 
    9157      111000 :   if (isa<SCEVConstant>(Less) && isa<SCEVConstant>(More)) {
    9158       62778 :     const auto &M = cast<SCEVConstant>(More)->getAPInt();
    9159       62778 :     const auto &L = cast<SCEVConstant>(Less)->getAPInt();
    9160      104630 :     return M - L;
    9161             :   }
    9162             : 
    9163             :   const SCEV *L, *R;
    9164             :   SCEV::NoWrapFlags Flags;
    9165       21651 :   if (splitBinaryAdd(Less, L, R, Flags))
    9166       12297 :     if (const auto *LC = dyn_cast<SCEVConstant>(L))
    9167        5873 :       if (R == More)
    9168        4272 :         return -(LC->getAPInt());
    9169             : 
    9170       20939 :   if (splitBinaryAdd(More, L, R, Flags))
    9171       12126 :     if (const auto *LC = dyn_cast<SCEVConstant>(L))
    9172        5979 :       if (R == Less)
    9173        1109 :         return LC->getAPInt();
    9174             : 
    9175             :   return None;
    9176             : }
    9177             : 
    9178       34101 : bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(
    9179             :     ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
    9180             :     const SCEV *FoundLHS, const SCEV *FoundRHS) {
    9181       34101 :   if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT)
    9182             :     return false;
    9183             : 
    9184       14117 :   const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS);
    9185             :   if (!AddRecLHS)
    9186             :     return false;
    9187             : 
    9188       13662 :   const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS);
    9189             :   if (!AddRecFoundLHS)
    9190             :     return false;
    9191             : 
    9192             :   // We'd like to let SCEV reason about control dependencies, so we constrain
    9193             :   // both the inequalities to be about add recurrences on the same loop.  This
    9194             :   // way we can use isLoopEntryGuardedByCond later.
    9195             : 
    9196       13662 :   const Loop *L = AddRecFoundLHS->getLoop();
    9197       13662 :   if (L != AddRecLHS->getLoop())
    9198             :     return false;
    9199             : 
    9200             :   //  FoundLHS u< FoundRHS u< -C =>  (FoundLHS + C) u< (FoundRHS + C) ... (1)
    9201             :   //
    9202             :   //  FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
    9203             :   //                                                                  ... (2)
    9204             :   //
    9205             :   // Informal proof for (2), assuming (1) [*]:
    9206             :   //
    9207             :   // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
    9208             :   //
    9209             :   // Then
    9210             :   //
    9211             :   //       FoundLHS s< FoundRHS s< INT_MIN - C
    9212             :   // <=>  (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C   [ using (3) ]
    9213             :   // <=>  (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
    9214             :   // <=>  (FoundLHS + INT_MIN + C + INT_MIN) s<
    9215             :   //                        (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
    9216             :   // <=>  FoundLHS + C s< FoundRHS + C
    9217             :   //
    9218             :   // [*]: (1) can be proved by ruling out overflow.
    9219             :   //
    9220             :   // [**]: This can be proved by analyzing all the four possibilities:
    9221             :   //    (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
    9222             :   //    (A s>= 0, B s>= 0).
    9223             :   //
    9224             :   // Note:
    9225             :   // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
    9226             :   // will not sign underflow.  For instance, say FoundLHS = (i8 -128), FoundRHS
    9227             :   // = (i8 -127) and C = (i8 -100).  Then INT_MIN - C = (i8 -28), and FoundRHS
    9228             :   // s< (INT_MIN - C).  Lack of sign overflow / underflow in "FoundRHS + C" is
    9229             :   // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
    9230             :   // C)".
    9231             : 
    9232       13644 :   Optional<APInt> LDiff = computeConstantDifference(LHS, FoundLHS);
    9233       27288 :   Optional<APInt> RDiff = computeConstantDifference(RHS, FoundRHS);
    9234       27834 :   if (!LDiff || !RDiff || *LDiff != *RDiff)
    9235             :     return false;
    9236             : 
    9237          38 :   if (LDiff->isMinValue())
    9238             :     return true;
    9239             : 
    9240           9 :   APInt FoundRHSLimit;
    9241             : 
    9242           9 :   if (Pred == CmpInst::ICMP_ULT) {
    9243          24 :     FoundRHSLimit = -(*RDiff);
    9244             :   } else {
    9245             :     assert(Pred == CmpInst::ICMP_SLT && "Checked above!");
    9246          25 :     FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff;
    9247             :   }
    9248             : 
    9249             :   // Try to prove (1) or (2), as needed.
    9250           9 :   return isLoopEntryGuardedByCond(L, Pred, FoundRHS,
    9251           9 :                                   getConstant(FoundRHSLimit));
    9252             : }
    9253             : 
    9254       34535 : bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
    9255             :                                             const SCEV *LHS, const SCEV *RHS,
    9256             :                                             const SCEV *FoundLHS,
    9257             :                                             const SCEV *FoundRHS) {
    9258       34535 :   if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS))
    9259             :     return true;
    9260             : 
    9261       34101 :   if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS))
    9262             :     return true;
    9263             : 
    9264       34085 :   return isImpliedCondOperandsHelper(Pred, LHS, RHS,
    9265       64412 :                                      FoundLHS, FoundRHS) ||
    9266             :          // ~x < ~y --> x > y
    9267       30327 :          isImpliedCondOperandsHelper(Pred, LHS, RHS,
    9268             :                                      getNotSCEV(FoundRHS),
    9269             :                                      getNotSCEV(FoundLHS));
    9270             : }
    9271             : 
    9272             : /// If Expr computes ~A, return A else return nullptr
    9273       44602 : static const SCEV *MatchNotExpr(const SCEV *Expr) {
    9274        3675 :   const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
    9275        6833 :   if (!Add || Add->getNumOperands() != 2 ||
    9276        6316 :       !Add->getOperand(0)->isAllOnesValue())
    9277             :     return nullptr;
    9278             : 
    9279        4160 :   const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
    9280        1212 :   if (!AddRHS || AddRHS->getNumOperands() != 2 ||
    9281        1212 :       !AddRHS->getOperand(0)->isAllOnesValue())
    9282             :     return nullptr;
    9283             : 
    9284        1116 :   return AddRHS->getOperand(1);
    9285             : }
    9286             : 
    9287             : /// Is MaybeMaxExpr an SMax or UMax of Candidate and some other values?
    9288             : template<typename MaxExprType>
    9289       45144 : static bool IsMaxConsistingOf(const SCEV *MaybeMaxExpr,
    9290             :                               const SCEV *Candidate) {
    9291         190 :   const MaxExprType *MaxExpr = dyn_cast<MaxExprType>(MaybeMaxExpr);
    9292             :   if (!MaxExpr) return false;
    9293             : 
    9294         570 :   return find(MaxExpr->operands(), Candidate) != MaxExpr->op_end();
    9295             : }
    9296             : 
    9297             : /// Is MaybeMinExpr an SMin or UMin of Candidate and some other values?
    9298             : template<typename MaxExprType>
    9299       44602 : static bool IsMinConsistingOf(ScalarEvolution &SE,
    9300             :                               const SCEV *MaybeMinExpr,
    9301             :                               const SCEV *Candidate) {
    9302       44602 :   const SCEV *MaybeMaxExpr = MatchNotExpr(MaybeMinExpr);
    9303       44602 :   if (!MaybeMaxExpr)
    9304             :     return false;
    9305             : 
    9306         558 :   return IsMaxConsistingOf<MaxExprType>(MaybeMaxExpr, SE.getNotSCEV(Candidate));
    9307             : }
    9308             : 
    9309       46374 : static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE,
    9310             :                                            ICmpInst::Predicate Pred,
    9311             :                                            const SCEV *LHS, const SCEV *RHS) {
    9312             :   // If both sides are affine addrecs for the same loop, with equal
    9313             :   // steps, and we know the recurrences don't wrap, then we only
    9314             :   // need to check the predicate on the starting values.
    9315             : 
    9316       46374 :   if (!ICmpInst::isRelational(Pred))
    9317             :     return false;
    9318             : 
    9319       27101 :   const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS);
    9320             :   if (!LAR)
    9321             :     return false;
    9322       13960 :   const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
    9323             :   if (!RAR)
    9324             :     return false;
    9325       13960 :   if (LAR->getLoop() != RAR->getLoop())
    9326             :     return false;
    9327       27527 :   if (!LAR->isAffine() || !RAR->isAffine())
    9328             :     return false;
    9329             : 
    9330       13759 :   if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE))
    9331             :     return false;
    9332             : 
    9333        8753 :   SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ?
    9334        8753 :                          SCEV::FlagNSW : SCEV::FlagNUW;
    9335       22002 :   if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW))
    9336             :     return false;
    9337             : 
    9338        4260 :   return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart());
    9339             : }
    9340             : 
    9341             : /// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max
    9342             : /// expression?
    9343       46406 : static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE,
    9344             :                                         ICmpInst::Predicate Pred,
    9345             :                                         const SCEV *LHS, const SCEV *RHS) {
    9346       46406 :   switch (Pred) {
    9347             :   default:
    9348             :     return false;
    9349             : 
    9350        5285 :   case ICmpInst::ICMP_SGE:
    9351             :     std::swap(LHS, RHS);
    9352             :     LLVM_FALLTHROUGH;
    9353       14470 :   case ICmpInst::ICMP_SLE:
    9354             :     return
    9355             :       // min(A, ...) <= A
    9356       28932 :       IsMinConsistingOf<SCEVSMaxExpr>(SE, LHS, RHS) ||
    9357             :       // A <= max(A, ...)
    9358       14462 :       IsMaxConsistingOf<SCEVSMaxExpr>(RHS, LHS);
    9359             : 
    9360        9675 :   case ICmpInst::ICMP_UGE:
    9361             :     std::swap(LHS, RHS);
    9362             :     LLVM_FALLTHROUGH;
    9363       30132 :   case ICmpInst::ICMP_ULE:
    9364             :     return
    9365             :       // min(A, ...) <= A
    9366       60256 :       IsMinConsistingOf<SCEVUMaxExpr>(SE, LHS, RHS) ||
    9367             :       // A <= max(A, ...)
    9368       30124 :       IsMaxConsistingOf<SCEVUMaxExpr>(RHS, LHS);
    9369             :   }
    9370             : 
    9371             :   llvm_unreachable("covered switch fell through?!");
    9372             : }
    9373             : 
    9374       62469 : bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred,
    9375             :                                              const SCEV *LHS, const SCEV *RHS,
    9376             :                                              const SCEV *FoundLHS,
    9377             :                                              const SCEV *FoundRHS,
    9378             :                                              unsigned Depth) {
    9379             :   assert(getTypeSizeInBits(LHS->getType()) ==
    9380             :              getTypeSizeInBits(RHS->getType()) &&
    9381             :          "LHS and RHS have different sizes?");
    9382             :   assert(getTypeSizeInBits(FoundLHS->getType()) ==
    9383             :              getTypeSizeInBits(FoundRHS->getType()) &&
    9384             :          "FoundLHS and FoundRHS have different sizes?");
    9385             :   // We want to avoid hurting the compile time with analysis of too big trees.
    9386       62469 :   if (Depth > MaxSCEVOperationsImplicationDepth)
    9387             :     return false;
    9388             :   // We only want to work with ICMP_SGT comparison so far.
    9389             :   // TODO: Extend to ICMP_UGT?
    9390       62469 :   if (Pred == ICmpInst::ICMP_SLT) {
    9391        8214 :     Pred = ICmpInst::ICMP_SGT;
    9392        8214 :     std::swap(LHS, RHS);
    9393             :     std::swap(FoundLHS, FoundRHS);
    9394             :   }
    9395       62469 :   if (Pred != ICmpInst::ICMP_SGT)
    9396             :     return false;
    9397             : 
    9398             :   auto GetOpFromSExt = [&](const SCEV *S) {
    9399        1026 :     if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(S))
    9400        1026 :       return Ext->getOperand();
    9401             :     // TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off
    9402             :     // the constant in some cases.
    9403             :     return S;
    9404             :   };
    9405             : 
    9406             :   // Acquire values from extensions.
    9407       16063 :   auto *OrigFoundLHS = FoundLHS;
    9408       32126 :   LHS = GetOpFromSExt(LHS);
    9409       32126 :   FoundLHS = GetOpFromSExt(FoundLHS);
    9410             : 
    9411             :   // Is the SGT predicate can be proved trivially or using the found context.
    9412        2985 :   auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) {
    9413        4789 :     return isKnownViaSimpleReasoning(ICmpInst::ICMP_SGT, S1, S2) ||
    9414        3608 :            isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS,
    9415        3608 :                                   FoundRHS, Depth + 1);
    9416       19048 :   };
    9417             : 
    9418       18205 :   if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(LHS)) {
    9419             :     // We want to avoid creation of any new non-constant SCEV. Since we are
    9420             :     // going to compare the operands to RHS, we should be certain that we don't
    9421             :     // need any size extensions for this. So let's decline all cases when the
    9422             :     // sizes of types of LHS and RHS do not match.
    9423             :     // TODO: Maybe try to get RHS from sext to catch more cases?
    9424        2142 :     if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType()))
    9425        1256 :       return false;
    9426             : 
    9427             :     // Should not overflow.
    9428        4244 :     if (!LHSAddExpr->hasNoSignedWrap())
    9429             :       return false;
    9430             : 
    9431        1780 :     auto *LL = LHSAddExpr->getOperand(0);
    9432        1780 :     auto *LR = LHSAddExpr->getOperand(1);
    9433        1780 :     auto *MinusOne = getNegativeSCEV(getOne(RHS->getType()));
    9434             : 
    9435             :     // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context.
    9436        1776 :     auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) {
    9437        1776 :       return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS);
    9438        2666 :     };
    9439             :     // Try to prove the following rule:
    9440             :     // (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS).
    9441             :     // (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS).
    9442         890 :     if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL))
    9443             :       return true;
    9444       16130 :   } else if (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(LHS)) {
    9445             :     Value *LL, *LR;
    9446             :     // FIXME: Once we have SDiv implemented, we can get rid of this matching.
    9447             : 
    9448             :     using namespace llvm::PatternMatch;
    9449             : 
    9450       11045 :     if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) {
    9451             :       // Rules for division.
    9452             :       // We are going to perform some comparisons with Denominator and its
    9453             :       // derivative expressions. In general case, creating a SCEV for it may
    9454             :       // lead to a complex analysis of the entire graph, and in particular it
    9455             :       // can request trip count recalculation for the same loop. This would
    9456             :       // cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid
    9457             :       // this, we only want to create SCEVs that are constants in this section.
    9458             :       // So we bail if Denominator is not a constant.
    9459         698 :       if (!isa<ConstantInt>(LR))
    9460         333 :         return false;
    9461             : 
    9462         698 :       auto *Denominator = cast<SCEVConstant>(getSCEV(LR));
    9463             : 
    9464             :       // We want to make sure that LHS = FoundLHS / Denominator. If it is so,
    9465             :       // then a SCEV for the numerator already exists and matches with FoundLHS.
    9466         349 :       auto *Numerator = getExistingSCEV(LL);
    9467         349 :       if (!Numerator || Numerator->getType() != FoundLHS->getType())
    9468             :         return false;
    9469             : 
    9470             :       // Make sure that the numerator matches with FoundLHS and the denominator
    9471             :       // is positive.
    9472         303 :       if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator))
    9473             :         return false;
    9474             : 
    9475          29 :       auto *DTy = Denominator->getType();
    9476          29 :       auto *FRHSTy = FoundRHS->getType();
    9477          58 :       if (DTy->isPointerTy() != FRHSTy->isPointerTy())
    9478             :         // One of types is a pointer and another one is not. We cannot extend
    9479             :         // them properly to a wider type, so let us just reject this case.
    9480             :         // TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help
    9481             :         // to avoid this check.
    9482             :         return false;
    9483             : 
    9484             :       // Given that:
    9485             :       // FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0.
    9486          29 :       auto *WTy = getWiderType(DTy, FRHSTy);
    9487          29 :       auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy);
    9488          29 :       auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy);
    9489             : 
    9490             :       // Try to prove the following rule:
    9491             :       // (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS).
    9492             :       // For example, given that FoundLHS > 2. It means that FoundLHS is at
    9493             :       // least 3. If we divide it by Denominator < 4, we will have at least 1.
    9494          29 :       auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2));
    9495          54 :       if (isKnownNonPositive(RHS) &&
    9496          25 :           IsSGTViaContext(FoundRHSExt, DenomMinusTwo))
    9497             :         return true;
    9498             : 
    9499             :       // Try to prove the following rule:
    9500             :       // (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS).
    9501             :       // For example, given that FoundLHS > -3. Then FoundLHS is at least -2.
    9502             :       // If we divide it by Denominator > 2, then:
    9503             :       // 1. If FoundLHS is negative, then the result is 0.
    9504             :       // 2. If FoundLHS is non-negative, then the result is non-negative.
    9505             :       // Anyways, the result is non-negative.
    9506          22 :       auto *MinusOne = getNegativeSCEV(getOne(WTy));
    9507          22 :       auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt);
    9508          36 :       if (isKnownNegative(RHS) &&
    9509          14 :           IsSGTViaContext(FoundRHSExt, NegDenomMinusOne))
    9510             :         return true;
    9511             :     }
    9512             :   }
    9513             : 
    9514             :   return false;
    9515             : }
    9516             : 
    9517             : bool
    9518       58397 : ScalarEvolution::isKnownViaSimpleReasoning(ICmpInst::Predicate Pred,
    9519             :                                            const SCEV *LHS, const SCEV *RHS) {
    9520      104803 :   return isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
    9521       92780 :          IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) ||
    9522      150968 :          IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) ||
    9523      104594 :          isKnownPredicateViaNoOverflow(Pred, LHS, RHS);
    9524             : }
    9525             : 
    9526             : bool
    9527       64412 : ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
    9528             :                                              const SCEV *LHS, const SCEV *RHS,
    9529             :                                              const SCEV *FoundLHS,
    9530             :                                              const SCEV *FoundRHS) {
    9531       64412 :   switch (Pred) {
    9532           0 :   default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
    9533       17811 :   case ICmpInst::ICMP_EQ:
    9534             :   case ICmpInst::ICMP_NE:
    9535       17811 :     if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
    9536             :       return true;
    9537             :     break;
    9538        8449 :   case ICmpInst::ICMP_SLT:
    9539             :   case ICmpInst::ICMP_SLE:
    9540        8946 :     if (isKnownViaSimpleReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
    9541         497 :         isKnownViaSimpleReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS))
    9542             :       return true;
    9543             :     break;
    9544        7776 :   case ICmpInst::ICMP_SGT:
    9545             :   case ICmpInst::ICMP_SGE:
    9546       10641 :     if (isKnownViaSimpleReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
    9547        2865 :         isKnownViaSimpleReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS))
    9548             :       return true;
    9549             :     break;
    9550       25188 :   case ICmpInst::ICMP_ULT:
    9551             :   case ICmpInst::ICMP_ULE:
    9552       30312 :     if (isKnownViaSimpleReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
    9553        5124 :         isKnownViaSimpleReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS))
    9554             :       return true;
    9555             :     break;
    9556        5188 :   case ICmpInst::ICMP_UGT:
    9557             :   case ICmpInst::ICMP_UGE:
    9558        5513 :     if (isKnownViaSimpleReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
    9559         325 :         isKnownViaSimpleReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS))
    9560             :       return true;
    9561             :     break;
    9562             :   }
    9563             : 
    9564             :   // Maybe it can be proved via operations?
    9565       60665 :   if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS))
    9566             :     return true;
    9567             : 
    9568       60654 :   return false;
    9569             : }
    9570             : 
    9571       34535 : bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,
    9572             :                                                      const SCEV *LHS,
    9573             :                                                      const SCEV *RHS,
    9574             :                                                      const SCEV *FoundLHS,
    9575             :                                                      const SCEV *FoundRHS) {
    9576      102032 :   if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS))
    9577             :     // The restriction on `FoundRHS` be lifted easily -- it exists only to
    9578             :     // reduce the compile time impact of this optimization.
    9579             :     return false;
    9580             : 
    9581       21479 :   Optional<APInt> Addend = computeConstantDifference(LHS, FoundLHS);
    9582       21479 :   if (!Addend)
    9583             :     return false;
    9584             : 
    9585       22962 :   const APInt &ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt();
    9586             : 
    9587             :   // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the
    9588             :   // antecedent "`FoundLHS` `Pred` `FoundRHS`".
    9589             :   ConstantRange FoundLHSRange =
    9590       22962 :       ConstantRange::makeAllowedICmpRegion(Pred, ConstFoundRHS);
    9591             : 
    9592             :   // Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`:
    9593       30616 :   ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend));
    9594             : 
    9595             :   // We can also compute the range of values for `LHS` that satisfy the
    9596             :   // consequent, "`LHS` `Pred` `RHS`":
    9597       22962 :   const APInt &ConstRHS = cast<SCEVConstant>(RHS)->getAPInt();
    9598             :   ConstantRange SatisfyingLHSRange =
    9599       22962 :       ConstantRange::makeSatisfyingICmpRegion(Pred, ConstRHS);
    9600             : 
    9601             :   // The antecedent implies the consequent if every value of `LHS` that
    9602             :   // satisfies the antecedent also satisfies the consequent.
    9603        7654 :   return SatisfyingLHSRange.contains(LHSRange);
    9604             : }
    9605             : 
    9606         306 : bool ScalarEvolution::doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
    9607             :                                          bool IsSigned, bool NoWrap) {
    9608             :   assert(isKnownPositive(Stride) && "Positive stride expected!");
    9609             : 
    9610         306 :   if (NoWrap) return false;
    9611             : 
    9612         190 :   unsigned BitWidth = getTypeSizeInBits(RHS->getType());
    9613         380 :   const SCEV *One = getOne(Stride->getType());
    9614             : 
    9615         190 :   if (IsSigned) {
    9616         172 :     APInt MaxRHS = getSignedRangeMax(RHS);
    9617         172 :     APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
    9618         258 :     APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
    9619             : 
    9620             :     // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow!
    9621         516 :     return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS);
    9622             :   }
    9623             : 
    9624         104 :   APInt MaxRHS = getUnsignedRangeMax(RHS);
    9625         208 :   APInt MaxValue = APInt::getMaxValue(BitWidth);
    9626         312 :   APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
    9627             : 
    9628             :   // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow!
    9629         624 :   return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS);
    9630             : }
    9631             : 
    9632          29 : bool ScalarEvolution::doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
    9633             :                                          bool IsSigned, bool NoWrap) {
    9634          29 :   if (NoWrap) return false;
    9635             : 
    9636          22 :   unsigned BitWidth = getTypeSizeInBits(RHS->getType());
    9637          44 :   const SCEV *One = getOne(Stride->getType());
    9638             : 
    9639          22 :   if (IsSigned) {
    9640          26 :     APInt MinRHS = getSignedRangeMin(RHS);
    9641          26 :     APInt MinValue = APInt::getSignedMinValue(BitWidth);
    9642          39 :     APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
    9643             : 
    9644             :     // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow!
    9645          78 :     return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS);
    9646             :   }
    9647             : 
    9648           9 :   APInt MinRHS = getUnsignedRangeMin(RHS);
    9649          18 :   APInt MinValue = APInt::getMinValue(BitWidth);
    9650          27 :   APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
    9651             : 
    9652             :   // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow!
    9653          54 :   return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS);
    9654             : }
    9655             : 
    9656        7066 : const SCEV *ScalarEvolution::computeBECount(const SCEV *Delta, const SCEV *Step,
    9657             :                                             bool Equality) {
    9658       14132 :   const SCEV *One = getOne(Step->getType());
    9659       14132 :   Delta = Equality ? getAddExpr(Delta, Step)
    9660        7066 :                    : getAddExpr(Delta, getMinusSCEV(Step, One));
    9661        7066 :   return getUDivExpr(Delta, Step);
    9662             : }
    9663             : 
    9664             : ScalarEvolution::ExitLimit
    9665        4213 : ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
    9666             :                                   const Loop *L, bool IsSigned,
    9667             :                                   bool ControlsExit, bool AllowPredicates) {
    9668        8426 :   SmallPtrSet<const SCEVPredicate *, 4> Predicates;
    9669             :   // We handle only IV < Invariant
    9670        4213 :   if (!isLoopInvariant(RHS, L))
    9671         871 :     return getCouldNotCompute();
    9672             : 
    9673        3342 :   const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
    9674        3342 :   bool PredicatedIV = false;
    9675             : 
    9676        3342 :   if (!IV && AllowPredicates) {
    9677             :     // Try to make this an AddRec using runtime tests, in the first X
    9678             :     // iterations of this loop, where X is the SCEV expression found by the
    9679             :     // algorithm below.
    9680          40 :     IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
    9681          40 :     PredicatedIV = true;
    9682             :   }
    9683             : 
    9684             :   // Avoid weird loops
    9685        6013 :   if (!IV || IV->getLoop() != L || !IV->isAffine())
    9686         674 :     return getCouldNotCompute();
    9687             : 
    9688        4615 :   bool NoWrap = ControlsExit &&
    9689        6562 :                 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
    9690             : 
    9691        2668 :   const SCEV *Stride = IV->getStepRecurrence(*this);
    9692             : 
    9693        2668 :   bool PositiveStride = isKnownPositive(Stride);
    9694             : 
    9695             :   // Avoid negative or zero stride values.
    9696        2668 :   if (!PositiveStride) {
    9697             :     // We can compute the correct backedge taken count for loops with unknown
    9698             :     // strides if we can prove that the loop is not an infinite loop with side
    9699             :     // effects. Here's the loop structure we are trying to handle -
    9700             :     //
    9701             :     // i = start
    9702             :     // do {
    9703             :     //   A[i] = i;
    9704             :     //   i += s;
    9705             :     // } while (i < end);
    9706             :     //
    9707             :     // The backedge taken count for such loops is evaluated as -
    9708             :     // (max(end, start + stride) - start - 1) /u stride
    9709             :     //
    9710             :     // The additional preconditions that we need to check to prove correctness
    9711             :     // of the above formula is as follows -
    9712             :     //
    9713             :     // a) IV is either nuw or nsw depending upon signedness (indicated by the
    9714             :     //    NoWrap flag).
    9715             :     // b) loop is single exit with no side effects.
    9716             :     //
    9717             :     //
    9718             :     // Precondition a) implies that if the stride is negative, this is a single
    9719             :     // trip loop. The backedge taken count formula reduces to zero in this case.
    9720             :     //
    9721             :     // Precondition b) implies that the unknown stride cannot be zero otherwise
    9722             :     // we have UB.
    9723             :     //
    9724             :     // The positive stride case is the same as isKnownPositive(Stride) returning
    9725             :     // true (original behavior of the function).
    9726             :     //
    9727             :     // We want to make sure that the stride is truly unknown as there are edge
    9728             :     // cases where ScalarEvolution propagates no wrap flags to the
    9729             :     // post-increment/decrement IV even though the increment/decrement operation
    9730             :     // itself is wrapping. The computed backedge taken count may be wrong in
    9731             :     // such cases. This is prevented by checking that the stride is not known to
    9732             :     // be either positive or non-positive. For example, no wrap flags are
    9733             :     // propagated to the post-increment IV of this loop with a trip count of 2 -
    9734             :     //
    9735             :     // unsigned char i;
    9736             :     // for(i=127; i<128; i+=129)
    9737             :     //   A[i] = i;
    9738             :     //
    9739         119 :     if (PredicatedIV || !NoWrap || isKnownNonPositive(Stride) ||
    9740           4 :         !loopHasNoSideEffects(L))
    9741         111 :       return getCouldNotCompute();
    9742        2859 :   } else if (!Stride->isOne() &&
    9743         306 :              doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap))
    9744             :     // Avoid proven overflow cases: this will ensure that the backedge taken
    9745             :     // count will not generate any unsigned overflow. Relaxed no-overflow
    9746             :     // conditions exploit NoWrapFlags, allowing to optimize in presence of
    9747             :     // undefined behaviors like the case of C language.
    9748         125 :     return getCouldNotCompute();
    9749             : 
    9750        2432 :   ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT
    9751             :                                       : ICmpInst::ICMP_ULT;
    9752        4864 :   const SCEV *Start = IV->getStart();
    9753        2432 :   const SCEV *End = RHS;
    9754             :   // If the backedge is taken at least once, then it will be taken
    9755             :   // (End-Start)/Stride times (rounded up to a multiple of Stride), where Start
    9756             :   // is the LHS value of the less-than comparison the first time it is evaluated
    9757             :   // and End is the RHS.
    9758             :   const SCEV *BECountIfBackedgeTaken =
    9759        2432 :     computeBECount(getMinusSCEV(End, Start), Stride, false);
    9760             :   // If the loop entry is guarded by the result of the backedge test of the
    9761             :   // first loop iteration, then we know the backedge will be taken at least
    9762             :   // once and so the backedge taken count is as above. If not then we use the
    9763             :   // expression (max(End,Start)-Start)/Stride to describe the backedge count,
    9764             :   // as if the backedge is taken at least once max(End,Start) is End and so the
    9765             :   // result is as above, and if not max(End,Start) is Start so we get a backedge
    9766             :   // count of zero.
    9767             :   const SCEV *BECount;
    9768        2432 :   if (isLoopEntryGuardedByCond(L, Cond, getMinusSCEV(Start, Stride), RHS))
    9769             :     BECount = BECountIfBackedgeTaken;
    9770             :   else {
    9771        1375 :     End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start);
    9772        1375 :     BECount = computeBECount(getMinusSCEV(End, Start), Stride, false);
    9773             :   }
    9774             : 
    9775             :   const SCEV *MaxBECount;
    9776        2432 :   bool MaxOrZero = false;
    9777        4864 :   if (isa<SCEVConstant>(BECount))
    9778             :     MaxBECount = BECount;
    9779        4840 :   else if (isa<SCEVConstant>(BECountIfBackedgeTaken)) {
    9780             :     // If we know exactly how many times the backedge will be taken if it's
    9781             :     // taken at least once, then the backedge count will either be that or
    9782             :     // zero.
    9783             :     MaxBECount = BECountIfBackedgeTaken;
    9784             :     MaxOrZero = true;
    9785             :   } else {
    9786             :     // Calculate the maximum backedge count based on the range of values
    9787             :     // permitted by Start, End, and Stride.
    9788             :     APInt MinStart = IsSigned ? getSignedRangeMin(Start)
    9789        4788 :                               : getUnsignedRangeMin(Start);
    9790             : 
    9791        2394 :     unsigned BitWidth = getTypeSizeInBits(LHS->getType());
    9792             : 
    9793        4788 :     APInt StrideForMaxBECount;
    9794             : 
    9795        2394 :     if (PositiveStride)
    9796        2390 :       StrideForMaxBECount =
    9797        4780 :         IsSigned ? getSignedRangeMin(Stride)
    9798             :                  : getUnsignedRangeMin(Stride);
    9799             :     else
    9800             :       // Using a stride of 1 is safe when computing max backedge taken count for
    9801             :       // a loop with unknown stride.
    9802          16 :       StrideForMaxBECount = APInt(BitWidth, 1, IsSigned);
    9803             : 
    9804             :     APInt Limit =
    9805       12862 :       IsSigned ? APInt::getSignedMaxValue(BitWidth) - (StrideForMaxBECount - 1)
    9806       15866 :                : APInt::getMaxValue(BitWidth) - (StrideForMaxBECount - 1);
    9807             : 
    9808             :     // Although End can be a MAX expression we estimate MaxEnd considering only
    9809             :     // the case End = RHS. This is safe because in the other case (End - Start)
    9810             :     // is zero, leading to a zero maximum backedge taken count.
    9811             :     APInt MaxEnd =
    9812        6431 :       IsSigned ? APIntOps::smin(getSignedRangeMax(RHS), Limit)
    9813       11970 :                : APIntOps::umin(getUnsignedRangeMax(RHS), Limit);
    9814             : 
    9815       11970 :     MaxBECount = computeBECount(getConstant(MaxEnd - MinStart),
    9816             :                                 getConstant(StrideForMaxBECount), false);
    9817             :   }
    9818             : 
    9819        4864 :   if (isa<SCEVCouldNotCompute>(MaxBECount) &&
    9820           0 :       !isa<SCEVCouldNotCompute>(BECount))
    9821           0 :     MaxBECount = getConstant(getUnsignedRangeMax(BECount));
    9822             : 
    9823        2432 :   return ExitLimit(BECount, MaxBECount, MaxOrZero, Predicates);
    9824             : }
    9825             : 
    9826             : ScalarEvolution::ExitLimit
    9827        1095 : ScalarEvolution::howManyGreaterThans(const SCEV *LHS, const SCEV *RHS,
    9828             :                                      const Loop *L, bool IsSigned,
    9829             :                                      bool ControlsExit, bool AllowPredicates) {
    9830        2190 :   SmallPtrSet<const SCEVPredicate *, 4> Predicates;
    9831             :   // We handle only IV > Invariant
    9832        1095 :   if (!isLoopInvariant(RHS, L))
    9833         187 :     return getCouldNotCompute();
    9834             : 
    9835         908 :   const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
    9836         908 :   if (!IV && AllowPredicates)
    9837             :     // Try to make this an AddRec using runtime tests, in the first X
    9838             :     // iterations of this loop, where X is the SCEV expression found by the
    9839             :     // algorithm below.
    9840          32 :     IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
    9841             : 
    9842             :   // Avoid weird loops
    9843        1425 :   if (!IV || IV->getLoop() != L || !IV->isAffine())
    9844         391 :     return getCouldNotCompute();
    9845             : 
    9846         830 :   bool NoWrap = ControlsExit &&
    9847        1143 :                 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
    9848             : 
    9849         517 :   const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this));
    9850             : 
    9851             :   // Avoid negative or zero stride values
    9852         517 :   if (!isKnownPositive(Stride))
    9853          48 :     return getCouldNotCompute();
    9854             : 
    9855             :   // Avoid proven overflow cases: this will ensure that the backedge taken count
    9856             :   // will not generate any unsigned overflow. Relaxed no-overflow conditions
    9857             :   // exploit NoWrapFlags, allowing to optimize in presence of undefined
    9858             :   // behaviors like the case of C language.
    9859         469 :   if (!Stride->isOne() && doesIVOverflowOnGT(RHS, Stride, IsSigned, NoWrap))
    9860           2 :     return getCouldNotCompute();
    9861             : 
    9862         934 :   ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT
    9863             :                                       : ICmpInst::ICMP_UGT;
    9864             : 
    9865         934 :   const SCEV *Start = IV->getStart();
    9866         467 :   const SCEV *End = RHS;
    9867         467 :   if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS))
    9868         256 :     End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start);
    9869             : 
    9870         467 :   const SCEV *BECount = computeBECount(getMinusSCEV(Start, End), Stride, false);
    9871             : 
    9872             :   APInt MaxStart = IsSigned ? getSignedRangeMax(Start)
    9873         467 :                             : getUnsignedRangeMax(Start);
    9874             : 
    9875             :   APInt MinStride = IsSigned ? getSignedRangeMin(Stride)
    9876