1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the implementation of the scalar evolution analysis
10 // engine, which is used primarily to analyze expressions involving induction
11 // variables in loops.
12 //
13 // There are several aspects to this library.  First is the representation of
14 // scalar expressions, which are represented as subclasses of the SCEV class.
15 // These classes are used to represent certain types of subexpressions that we
16 // can handle. We only create one SCEV of a particular shape, so
17 // pointer-comparisons for equality are legal.
18 //
19 // One important aspect of the SCEV objects is that they are never cyclic, even
20 // if there is a cycle in the dataflow for an expression (ie, a PHI node).  If
21 // the PHI node is one of the idioms that we can represent (e.g., a polynomial
22 // recurrence) then we represent it directly as a recurrence node, otherwise we
23 // represent it as a SCEVUnknown node.
24 //
25 // In addition to being able to represent expressions of various types, we also
26 // have folders that are used to build the *canonical* representation for a
27 // particular expression.  These folders are capable of using a variety of
28 // rewrite rules to simplify the expressions.
29 //
30 // Once the folders are defined, we can implement the more interesting
31 // higher-level code, such as the code that recognizes PHI nodes of various
32 // types, computes the execution count of a loop, etc.
33 //
34 // TODO: We should use these routines and value representations to implement
35 // dependence analysis!
36 //
37 //===----------------------------------------------------------------------===//
38 //
39 // There are several good references for the techniques used in this analysis.
40 //
41 //  Chains of recurrences -- a method to expedite the evaluation
42 //  of closed-form functions
43 //  Olaf Bachmann, Paul S. Wang, Eugene V. Zima
44 //
45 //  On computational properties of chains of recurrences
46 //  Eugene V. Zima
47 //
48 //  Symbolic Evaluation of Chains of Recurrences for Loop Optimization
49 //  Robert A. van Engelen
50 //
51 //  Efficient Symbolic Analysis for Optimizing Compilers
52 //  Robert A. van Engelen
53 //
54 //  Using the chains of recurrences algebra for data dependence testing and
55 //  induction variable substitution
56 //  MS Thesis, Johnie Birch
57 //
58 //===----------------------------------------------------------------------===//
59 
60 #include "llvm/Analysis/ScalarEvolution.h"
61 #include "llvm/ADT/APInt.h"
62 #include "llvm/ADT/ArrayRef.h"
63 #include "llvm/ADT/DenseMap.h"
64 #include "llvm/ADT/DepthFirstIterator.h"
65 #include "llvm/ADT/EquivalenceClasses.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/ScalarEvolutionDivision.h"
83 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
84 #include "llvm/Analysis/TargetLibraryInfo.h"
85 #include "llvm/Analysis/ValueTracking.h"
86 #include "llvm/Config/llvm-config.h"
87 #include "llvm/IR/Argument.h"
88 #include "llvm/IR/BasicBlock.h"
89 #include "llvm/IR/CFG.h"
90 #include "llvm/IR/Constant.h"
91 #include "llvm/IR/ConstantRange.h"
92 #include "llvm/IR/Constants.h"
93 #include "llvm/IR/DataLayout.h"
94 #include "llvm/IR/DerivedTypes.h"
95 #include "llvm/IR/Dominators.h"
96 #include "llvm/IR/Function.h"
97 #include "llvm/IR/GlobalAlias.h"
98 #include "llvm/IR/GlobalValue.h"
99 #include "llvm/IR/GlobalVariable.h"
100 #include "llvm/IR/InstIterator.h"
101 #include "llvm/IR/InstrTypes.h"
102 #include "llvm/IR/Instruction.h"
103 #include "llvm/IR/Instructions.h"
104 #include "llvm/IR/IntrinsicInst.h"
105 #include "llvm/IR/Intrinsics.h"
106 #include "llvm/IR/LLVMContext.h"
107 #include "llvm/IR/Metadata.h"
108 #include "llvm/IR/Operator.h"
109 #include "llvm/IR/PatternMatch.h"
110 #include "llvm/IR/Type.h"
111 #include "llvm/IR/Use.h"
112 #include "llvm/IR/User.h"
113 #include "llvm/IR/Value.h"
114 #include "llvm/IR/Verifier.h"
115 #include "llvm/InitializePasses.h"
116 #include "llvm/Pass.h"
117 #include "llvm/Support/Casting.h"
118 #include "llvm/Support/CommandLine.h"
119 #include "llvm/Support/Compiler.h"
120 #include "llvm/Support/Debug.h"
121 #include "llvm/Support/ErrorHandling.h"
122 #include "llvm/Support/KnownBits.h"
123 #include "llvm/Support/SaveAndRestore.h"
124 #include "llvm/Support/raw_ostream.h"
125 #include <algorithm>
126 #include <cassert>
127 #include <climits>
128 #include <cstddef>
129 #include <cstdint>
130 #include <cstdlib>
131 #include <map>
132 #include <memory>
133 #include <tuple>
134 #include <utility>
135 #include <vector>
136 
137 using namespace llvm;
138 
139 #define DEBUG_TYPE "scalar-evolution"
140 
141 STATISTIC(NumArrayLenItCounts,
142           "Number of trip counts computed with array length");
143 STATISTIC(NumTripCountsComputed,
144           "Number of loops with predictable loop counts");
145 STATISTIC(NumTripCountsNotComputed,
146           "Number of loops without predictable loop counts");
147 STATISTIC(NumBruteForceTripCountsComputed,
148           "Number of loops with trip counts computed by force");
149 
150 static cl::opt<unsigned>
151 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
152                         cl::ZeroOrMore,
153                         cl::desc("Maximum number of iterations SCEV will "
154                                  "symbolically execute a constant "
155                                  "derived loop"),
156                         cl::init(100));
157 
158 // FIXME: Enable this with EXPENSIVE_CHECKS when the test suite is clean.
159 static cl::opt<bool> VerifySCEV(
160     "verify-scev", cl::Hidden,
161     cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
162 static cl::opt<bool> VerifySCEVStrict(
163     "verify-scev-strict", cl::Hidden,
164     cl::desc("Enable stricter verification with -verify-scev is passed"));
165 static cl::opt<bool>
166     VerifySCEVMap("verify-scev-maps", cl::Hidden,
167                   cl::desc("Verify no dangling value in ScalarEvolution's "
168                            "ExprValueMap (slow)"));
169 
170 static cl::opt<bool> VerifyIR(
171     "scev-verify-ir", cl::Hidden,
172     cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"),
173     cl::init(false));
174 
175 static cl::opt<unsigned> MulOpsInlineThreshold(
176     "scev-mulops-inline-threshold", cl::Hidden,
177     cl::desc("Threshold for inlining multiplication operands into a SCEV"),
178     cl::init(32));
179 
180 static cl::opt<unsigned> AddOpsInlineThreshold(
181     "scev-addops-inline-threshold", cl::Hidden,
182     cl::desc("Threshold for inlining addition operands into a SCEV"),
183     cl::init(500));
184 
185 static cl::opt<unsigned> MaxSCEVCompareDepth(
186     "scalar-evolution-max-scev-compare-depth", cl::Hidden,
187     cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
188     cl::init(32));
189 
190 static cl::opt<unsigned> MaxSCEVOperationsImplicationDepth(
191     "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden,
192     cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
193     cl::init(2));
194 
195 static cl::opt<unsigned> MaxValueCompareDepth(
196     "scalar-evolution-max-value-compare-depth", cl::Hidden,
197     cl::desc("Maximum depth of recursive value complexity comparisons"),
198     cl::init(2));
199 
200 static cl::opt<unsigned>
201     MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden,
202                   cl::desc("Maximum depth of recursive arithmetics"),
203                   cl::init(32));
204 
205 static cl::opt<unsigned> MaxConstantEvolvingDepth(
206     "scalar-evolution-max-constant-evolving-depth", cl::Hidden,
207     cl::desc("Maximum depth of recursive constant evolving"), cl::init(32));
208 
209 static cl::opt<unsigned>
210     MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden,
211                  cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"),
212                  cl::init(8));
213 
214 static cl::opt<unsigned>
215     MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
216                   cl::desc("Max coefficients in AddRec during evolving"),
217                   cl::init(8));
218 
219 static cl::opt<unsigned>
220     HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden,
221                   cl::desc("Size of the expression which is considered huge"),
222                   cl::init(4096));
223 
224 static cl::opt<bool>
225 ClassifyExpressions("scalar-evolution-classify-expressions",
226     cl::Hidden, cl::init(true),
227     cl::desc("When printing analysis, include information on every instruction"));
228 
229 
230 //===----------------------------------------------------------------------===//
231 //                           SCEV class definitions
232 //===----------------------------------------------------------------------===//
233 
234 //===----------------------------------------------------------------------===//
235 // Implementation of the SCEV class.
236 //
237 
238 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const239 LLVM_DUMP_METHOD void SCEV::dump() const {
240   print(dbgs());
241   dbgs() << '\n';
242 }
243 #endif
244 
print(raw_ostream & OS) const245 void SCEV::print(raw_ostream &OS) const {
246   switch (static_cast<SCEVTypes>(getSCEVType())) {
247   case scConstant:
248     cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
249     return;
250   case scTruncate: {
251     const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
252     const SCEV *Op = Trunc->getOperand();
253     OS << "(trunc " << *Op->getType() << " " << *Op << " to "
254        << *Trunc->getType() << ")";
255     return;
256   }
257   case scZeroExtend: {
258     const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
259     const SCEV *Op = ZExt->getOperand();
260     OS << "(zext " << *Op->getType() << " " << *Op << " to "
261        << *ZExt->getType() << ")";
262     return;
263   }
264   case scSignExtend: {
265     const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
266     const SCEV *Op = SExt->getOperand();
267     OS << "(sext " << *Op->getType() << " " << *Op << " to "
268        << *SExt->getType() << ")";
269     return;
270   }
271   case scAddRecExpr: {
272     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
273     OS << "{" << *AR->getOperand(0);
274     for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
275       OS << ",+," << *AR->getOperand(i);
276     OS << "}<";
277     if (AR->hasNoUnsignedWrap())
278       OS << "nuw><";
279     if (AR->hasNoSignedWrap())
280       OS << "nsw><";
281     if (AR->hasNoSelfWrap() &&
282         !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW)))
283       OS << "nw><";
284     AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false);
285     OS << ">";
286     return;
287   }
288   case scAddExpr:
289   case scMulExpr:
290   case scUMaxExpr:
291   case scSMaxExpr:
292   case scUMinExpr:
293   case scSMinExpr: {
294     const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
295     const char *OpStr = nullptr;
296     switch (NAry->getSCEVType()) {
297     case scAddExpr: OpStr = " + "; break;
298     case scMulExpr: OpStr = " * "; break;
299     case scUMaxExpr: OpStr = " umax "; break;
300     case scSMaxExpr: OpStr = " smax "; break;
301     case scUMinExpr:
302       OpStr = " umin ";
303       break;
304     case scSMinExpr:
305       OpStr = " smin ";
306       break;
307     }
308     OS << "(";
309     for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
310          I != E; ++I) {
311       OS << **I;
312       if (std::next(I) != E)
313         OS << OpStr;
314     }
315     OS << ")";
316     switch (NAry->getSCEVType()) {
317     case scAddExpr:
318     case scMulExpr:
319       if (NAry->hasNoUnsignedWrap())
320         OS << "<nuw>";
321       if (NAry->hasNoSignedWrap())
322         OS << "<nsw>";
323     }
324     return;
325   }
326   case scUDivExpr: {
327     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
328     OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
329     return;
330   }
331   case scUnknown: {
332     const SCEVUnknown *U = cast<SCEVUnknown>(this);
333     Type *AllocTy;
334     if (U->isSizeOf(AllocTy)) {
335       OS << "sizeof(" << *AllocTy << ")";
336       return;
337     }
338     if (U->isAlignOf(AllocTy)) {
339       OS << "alignof(" << *AllocTy << ")";
340       return;
341     }
342 
343     Type *CTy;
344     Constant *FieldNo;
345     if (U->isOffsetOf(CTy, FieldNo)) {
346       OS << "offsetof(" << *CTy << ", ";
347       FieldNo->printAsOperand(OS, false);
348       OS << ")";
349       return;
350     }
351 
352     // Otherwise just print it normally.
353     U->getValue()->printAsOperand(OS, false);
354     return;
355   }
356   case scCouldNotCompute:
357     OS << "***COULDNOTCOMPUTE***";
358     return;
359   }
360   llvm_unreachable("Unknown SCEV kind!");
361 }
362 
getType() const363 Type *SCEV::getType() const {
364   switch (static_cast<SCEVTypes>(getSCEVType())) {
365   case scConstant:
366     return cast<SCEVConstant>(this)->getType();
367   case scTruncate:
368   case scZeroExtend:
369   case scSignExtend:
370     return cast<SCEVCastExpr>(this)->getType();
371   case scAddRecExpr:
372   case scMulExpr:
373   case scUMaxExpr:
374   case scSMaxExpr:
375   case scUMinExpr:
376   case scSMinExpr:
377     return cast<SCEVNAryExpr>(this)->getType();
378   case scAddExpr:
379     return cast<SCEVAddExpr>(this)->getType();
380   case scUDivExpr:
381     return cast<SCEVUDivExpr>(this)->getType();
382   case scUnknown:
383     return cast<SCEVUnknown>(this)->getType();
384   case scCouldNotCompute:
385     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
386   }
387   llvm_unreachable("Unknown SCEV kind!");
388 }
389 
isZero() const390 bool SCEV::isZero() const {
391   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
392     return SC->getValue()->isZero();
393   return false;
394 }
395 
isOne() const396 bool SCEV::isOne() const {
397   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
398     return SC->getValue()->isOne();
399   return false;
400 }
401 
isAllOnesValue() const402 bool SCEV::isAllOnesValue() const {
403   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
404     return SC->getValue()->isMinusOne();
405   return false;
406 }
407 
isNonConstantNegative() const408 bool SCEV::isNonConstantNegative() const {
409   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
410   if (!Mul) return false;
411 
412   // If there is a constant factor, it will be first.
413   const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
414   if (!SC) return false;
415 
416   // Return true if the value is negative, this matches things like (-42 * V).
417   return SC->getAPInt().isNegative();
418 }
419 
SCEVCouldNotCompute()420 SCEVCouldNotCompute::SCEVCouldNotCompute() :
421   SCEV(FoldingSetNodeIDRef(), scCouldNotCompute, 0) {}
422 
classof(const SCEV * S)423 bool SCEVCouldNotCompute::classof(const SCEV *S) {
424   return S->getSCEVType() == scCouldNotCompute;
425 }
426 
getConstant(ConstantInt * V)427 const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
428   FoldingSetNodeID ID;
429   ID.AddInteger(scConstant);
430   ID.AddPointer(V);
431   void *IP = nullptr;
432   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
433   SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
434   UniqueSCEVs.InsertNode(S, IP);
435   return S;
436 }
437 
getConstant(const APInt & Val)438 const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
439   return getConstant(ConstantInt::get(getContext(), Val));
440 }
441 
442 const SCEV *
getConstant(Type * Ty,uint64_t V,bool isSigned)443 ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
444   IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
445   return getConstant(ConstantInt::get(ITy, V, isSigned));
446 }
447 
SCEVCastExpr(const FoldingSetNodeIDRef ID,unsigned SCEVTy,const SCEV * op,Type * ty)448 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID,
449                            unsigned SCEVTy, const SCEV *op, Type *ty)
450   : SCEV(ID, SCEVTy, computeExpressionSize(op)), Op(op), Ty(ty) {}
451 
SCEVTruncateExpr(const FoldingSetNodeIDRef ID,const SCEV * op,Type * ty)452 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
453                                    const SCEV *op, Type *ty)
454   : SCEVCastExpr(ID, scTruncate, op, ty) {
455   assert(Op->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
456          "Cannot truncate non-integer value!");
457 }
458 
SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,const SCEV * op,Type * ty)459 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
460                                        const SCEV *op, Type *ty)
461   : SCEVCastExpr(ID, scZeroExtend, op, ty) {
462   assert(Op->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
463          "Cannot zero extend non-integer value!");
464 }
465 
SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,const SCEV * op,Type * ty)466 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
467                                        const SCEV *op, Type *ty)
468   : SCEVCastExpr(ID, scSignExtend, op, ty) {
469   assert(Op->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
470          "Cannot sign extend non-integer value!");
471 }
472 
deleted()473 void SCEVUnknown::deleted() {
474   // Clear this SCEVUnknown from various maps.
475   SE->forgetMemoizedResults(this);
476 
477   // Remove this SCEVUnknown from the uniquing map.
478   SE->UniqueSCEVs.RemoveNode(this);
479 
480   // Release the value.
481   setValPtr(nullptr);
482 }
483 
allUsesReplacedWith(Value * New)484 void SCEVUnknown::allUsesReplacedWith(Value *New) {
485   // Remove this SCEVUnknown from the uniquing map.
486   SE->UniqueSCEVs.RemoveNode(this);
487 
488   // Update this SCEVUnknown to point to the new value. This is needed
489   // because there may still be outstanding SCEVs which still point to
490   // this SCEVUnknown.
491   setValPtr(New);
492 }
493 
isSizeOf(Type * & AllocTy) const494 bool SCEVUnknown::isSizeOf(Type *&AllocTy) const {
495   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
496     if (VCE->getOpcode() == Instruction::PtrToInt)
497       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
498         if (CE->getOpcode() == Instruction::GetElementPtr &&
499             CE->getOperand(0)->isNullValue() &&
500             CE->getNumOperands() == 2)
501           if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
502             if (CI->isOne()) {
503               AllocTy = cast<PointerType>(CE->getOperand(0)->getType())
504                                  ->getElementType();
505               return true;
506             }
507 
508   return false;
509 }
510 
isAlignOf(Type * & AllocTy) const511 bool SCEVUnknown::isAlignOf(Type *&AllocTy) const {
512   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
513     if (VCE->getOpcode() == Instruction::PtrToInt)
514       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
515         if (CE->getOpcode() == Instruction::GetElementPtr &&
516             CE->getOperand(0)->isNullValue()) {
517           Type *Ty =
518             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
519           if (StructType *STy = dyn_cast<StructType>(Ty))
520             if (!STy->isPacked() &&
521                 CE->getNumOperands() == 3 &&
522                 CE->getOperand(1)->isNullValue()) {
523               if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
524                 if (CI->isOne() &&
525                     STy->getNumElements() == 2 &&
526                     STy->getElementType(0)->isIntegerTy(1)) {
527                   AllocTy = STy->getElementType(1);
528                   return true;
529                 }
530             }
531         }
532 
533   return false;
534 }
535 
isOffsetOf(Type * & CTy,Constant * & FieldNo) const536 bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) const {
537   if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
538     if (VCE->getOpcode() == Instruction::PtrToInt)
539       if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
540         if (CE->getOpcode() == Instruction::GetElementPtr &&
541             CE->getNumOperands() == 3 &&
542             CE->getOperand(0)->isNullValue() &&
543             CE->getOperand(1)->isNullValue()) {
544           Type *Ty =
545             cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
546           // Ignore vector types here so that ScalarEvolutionExpander doesn't
547           // emit getelementptrs that index into vectors.
548           if (Ty->isStructTy() || Ty->isArrayTy()) {
549             CTy = Ty;
550             FieldNo = CE->getOperand(2);
551             return true;
552           }
553         }
554 
555   return false;
556 }
557 
558 //===----------------------------------------------------------------------===//
559 //                               SCEV Utilities
560 //===----------------------------------------------------------------------===//
561 
562 /// Compare the two values \p LV and \p RV in terms of their "complexity" where
563 /// "complexity" is a partial (and somewhat ad-hoc) relation used to order
564 /// operands in SCEV expressions.  \p EqCache is a set of pairs of values that
565 /// have been previously deemed to be "equally complex" by this routine.  It is
566 /// intended to avoid exponential time complexity in cases like:
567 ///
568 ///   %a = f(%x, %y)
569 ///   %b = f(%a, %a)
570 ///   %c = f(%b, %b)
571 ///
572 ///   %d = f(%x, %y)
573 ///   %e = f(%d, %d)
574 ///   %f = f(%e, %e)
575 ///
576 ///   CompareValueComplexity(%f, %c)
577 ///
578 /// Since we do not continue running this routine on expression trees once we
579 /// have seen unequal values, there is no need to track them in the cache.
580 static int
CompareValueComplexity(EquivalenceClasses<const Value * > & EqCacheValue,const LoopInfo * const LI,Value * LV,Value * RV,unsigned Depth)581 CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue,
582                        const LoopInfo *const LI, Value *LV, Value *RV,
583                        unsigned Depth) {
584   if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV))
585     return 0;
586 
587   // Order pointer values after integer values. This helps SCEVExpander form
588   // GEPs.
589   bool LIsPointer = LV->getType()->isPointerTy(),
590        RIsPointer = RV->getType()->isPointerTy();
591   if (LIsPointer != RIsPointer)
592     return (int)LIsPointer - (int)RIsPointer;
593 
594   // Compare getValueID values.
595   unsigned LID = LV->getValueID(), RID = RV->getValueID();
596   if (LID != RID)
597     return (int)LID - (int)RID;
598 
599   // Sort arguments by their position.
600   if (const auto *LA = dyn_cast<Argument>(LV)) {
601     const auto *RA = cast<Argument>(RV);
602     unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
603     return (int)LArgNo - (int)RArgNo;
604   }
605 
606   if (const auto *LGV = dyn_cast<GlobalValue>(LV)) {
607     const auto *RGV = cast<GlobalValue>(RV);
608 
609     const auto IsGVNameSemantic = [&](const GlobalValue *GV) {
610       auto LT = GV->getLinkage();
611       return !(GlobalValue::isPrivateLinkage(LT) ||
612                GlobalValue::isInternalLinkage(LT));
613     };
614 
615     // Use the names to distinguish the two values, but only if the
616     // names are semantically important.
617     if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
618       return LGV->getName().compare(RGV->getName());
619   }
620 
621   // For instructions, compare their loop depth, and their operand count.  This
622   // is pretty loose.
623   if (const auto *LInst = dyn_cast<Instruction>(LV)) {
624     const auto *RInst = cast<Instruction>(RV);
625 
626     // Compare loop depths.
627     const BasicBlock *LParent = LInst->getParent(),
628                      *RParent = RInst->getParent();
629     if (LParent != RParent) {
630       unsigned LDepth = LI->getLoopDepth(LParent),
631                RDepth = LI->getLoopDepth(RParent);
632       if (LDepth != RDepth)
633         return (int)LDepth - (int)RDepth;
634     }
635 
636     // Compare the number of operands.
637     unsigned LNumOps = LInst->getNumOperands(),
638              RNumOps = RInst->getNumOperands();
639     if (LNumOps != RNumOps)
640       return (int)LNumOps - (int)RNumOps;
641 
642     for (unsigned Idx : seq(0u, LNumOps)) {
643       int Result =
644           CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx),
645                                  RInst->getOperand(Idx), Depth + 1);
646       if (Result != 0)
647         return Result;
648     }
649   }
650 
651   EqCacheValue.unionSets(LV, RV);
652   return 0;
653 }
654 
655 // Return negative, zero, or positive, if LHS is less than, equal to, or greater
656 // than RHS, respectively. A three-way result allows recursive comparisons to be
657 // more efficient.
CompareSCEVComplexity(EquivalenceClasses<const SCEV * > & EqCacheSCEV,EquivalenceClasses<const Value * > & EqCacheValue,const LoopInfo * const LI,const SCEV * LHS,const SCEV * RHS,DominatorTree & DT,unsigned Depth=0)658 static int CompareSCEVComplexity(
659     EquivalenceClasses<const SCEV *> &EqCacheSCEV,
660     EquivalenceClasses<const Value *> &EqCacheValue,
661     const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS,
662     DominatorTree &DT, unsigned Depth = 0) {
663   // Fast-path: SCEVs are uniqued so we can do a quick equality check.
664   if (LHS == RHS)
665     return 0;
666 
667   // Primarily, sort the SCEVs by their getSCEVType().
668   unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
669   if (LType != RType)
670     return (int)LType - (int)RType;
671 
672   if (Depth > MaxSCEVCompareDepth || EqCacheSCEV.isEquivalent(LHS, RHS))
673     return 0;
674   // Aside from the getSCEVType() ordering, the particular ordering
675   // isn't very important except that it's beneficial to be consistent,
676   // so that (a + b) and (b + a) don't end up as different expressions.
677   switch (static_cast<SCEVTypes>(LType)) {
678   case scUnknown: {
679     const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
680     const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
681 
682     int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(),
683                                    RU->getValue(), Depth + 1);
684     if (X == 0)
685       EqCacheSCEV.unionSets(LHS, RHS);
686     return X;
687   }
688 
689   case scConstant: {
690     const SCEVConstant *LC = cast<SCEVConstant>(LHS);
691     const SCEVConstant *RC = cast<SCEVConstant>(RHS);
692 
693     // Compare constant values.
694     const APInt &LA = LC->getAPInt();
695     const APInt &RA = RC->getAPInt();
696     unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
697     if (LBitWidth != RBitWidth)
698       return (int)LBitWidth - (int)RBitWidth;
699     return LA.ult(RA) ? -1 : 1;
700   }
701 
702   case scAddRecExpr: {
703     const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
704     const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
705 
706     // There is always a dominance between two recs that are used by one SCEV,
707     // so we can safely sort recs by loop header dominance. We require such
708     // order in getAddExpr.
709     const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
710     if (LLoop != RLoop) {
711       const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
712       assert(LHead != RHead && "Two loops share the same header?");
713       if (DT.dominates(LHead, RHead))
714         return 1;
715       else
716         assert(DT.dominates(RHead, LHead) &&
717                "No dominance between recurrences used by one SCEV?");
718       return -1;
719     }
720 
721     // Addrec complexity grows with operand count.
722     unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands();
723     if (LNumOps != RNumOps)
724       return (int)LNumOps - (int)RNumOps;
725 
726     // Lexicographically compare.
727     for (unsigned i = 0; i != LNumOps; ++i) {
728       int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
729                                     LA->getOperand(i), RA->getOperand(i), DT,
730                                     Depth + 1);
731       if (X != 0)
732         return X;
733     }
734     EqCacheSCEV.unionSets(LHS, RHS);
735     return 0;
736   }
737 
738   case scAddExpr:
739   case scMulExpr:
740   case scSMaxExpr:
741   case scUMaxExpr:
742   case scSMinExpr:
743   case scUMinExpr: {
744     const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS);
745     const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
746 
747     // Lexicographically compare n-ary expressions.
748     unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands();
749     if (LNumOps != RNumOps)
750       return (int)LNumOps - (int)RNumOps;
751 
752     for (unsigned i = 0; i != LNumOps; ++i) {
753       int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
754                                     LC->getOperand(i), RC->getOperand(i), DT,
755                                     Depth + 1);
756       if (X != 0)
757         return X;
758     }
759     EqCacheSCEV.unionSets(LHS, RHS);
760     return 0;
761   }
762 
763   case scUDivExpr: {
764     const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS);
765     const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
766 
767     // Lexicographically compare udiv expressions.
768     int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(),
769                                   RC->getLHS(), DT, Depth + 1);
770     if (X != 0)
771       return X;
772     X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getRHS(),
773                               RC->getRHS(), DT, Depth + 1);
774     if (X == 0)
775       EqCacheSCEV.unionSets(LHS, RHS);
776     return X;
777   }
778 
779   case scTruncate:
780   case scZeroExtend:
781   case scSignExtend: {
782     const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS);
783     const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
784 
785     // Compare cast expressions by operand.
786     int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
787                                   LC->getOperand(), RC->getOperand(), DT,
788                                   Depth + 1);
789     if (X == 0)
790       EqCacheSCEV.unionSets(LHS, RHS);
791     return X;
792   }
793 
794   case scCouldNotCompute:
795     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
796   }
797   llvm_unreachable("Unknown SCEV kind!");
798 }
799 
800 /// Given a list of SCEV objects, order them by their complexity, and group
801 /// objects of the same complexity together by value.  When this routine is
802 /// finished, we know that any duplicates in the vector are consecutive and that
803 /// complexity is monotonically increasing.
804 ///
805 /// Note that we go take special precautions to ensure that we get deterministic
806 /// results from this routine.  In other words, we don't want the results of
807 /// this to depend on where the addresses of various SCEV objects happened to
808 /// land in memory.
GroupByComplexity(SmallVectorImpl<const SCEV * > & Ops,LoopInfo * LI,DominatorTree & DT)809 static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
810                               LoopInfo *LI, DominatorTree &DT) {
811   if (Ops.size() < 2) return;  // Noop
812 
813   EquivalenceClasses<const SCEV *> EqCacheSCEV;
814   EquivalenceClasses<const Value *> EqCacheValue;
815   if (Ops.size() == 2) {
816     // This is the common case, which also happens to be trivially simple.
817     // Special case it.
818     const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
819     if (CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, RHS, LHS, DT) < 0)
820       std::swap(LHS, RHS);
821     return;
822   }
823 
824   // Do the rough sort by complexity.
825   llvm::stable_sort(Ops, [&](const SCEV *LHS, const SCEV *RHS) {
826     return CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LHS, RHS, DT) <
827            0;
828   });
829 
830   // Now that we are sorted by complexity, group elements of the same
831   // complexity.  Note that this is, at worst, N^2, but the vector is likely to
832   // be extremely short in practice.  Note that we take this approach because we
833   // do not want to depend on the addresses of the objects we are grouping.
834   for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
835     const SCEV *S = Ops[i];
836     unsigned Complexity = S->getSCEVType();
837 
838     // If there are any objects of the same complexity and same value as this
839     // one, group them.
840     for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
841       if (Ops[j] == S) { // Found a duplicate.
842         // Move it to immediately after i'th element.
843         std::swap(Ops[i+1], Ops[j]);
844         ++i;   // no need to rescan it.
845         if (i == e-2) return;  // Done!
846       }
847     }
848   }
849 }
850 
851 /// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at
852 /// least HugeExprThreshold nodes).
hasHugeExpression(ArrayRef<const SCEV * > Ops)853 static bool hasHugeExpression(ArrayRef<const SCEV *> Ops) {
854   return any_of(Ops, [](const SCEV *S) {
855     return S->getExpressionSize() >= HugeExprThreshold;
856   });
857 }
858 
859 //===----------------------------------------------------------------------===//
860 //                      Simple SCEV method implementations
861 //===----------------------------------------------------------------------===//
862 
863 /// Compute BC(It, K).  The result has width W.  Assume, K > 0.
BinomialCoefficient(const SCEV * It,unsigned K,ScalarEvolution & SE,Type * ResultTy)864 static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
865                                        ScalarEvolution &SE,
866                                        Type *ResultTy) {
867   // Handle the simplest case efficiently.
868   if (K == 1)
869     return SE.getTruncateOrZeroExtend(It, ResultTy);
870 
871   // We are using the following formula for BC(It, K):
872   //
873   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
874   //
875   // Suppose, W is the bitwidth of the return value.  We must be prepared for
876   // overflow.  Hence, we must assure that the result of our computation is
877   // equal to the accurate one modulo 2^W.  Unfortunately, division isn't
878   // safe in modular arithmetic.
879   //
880   // However, this code doesn't use exactly that formula; the formula it uses
881   // is something like the following, where T is the number of factors of 2 in
882   // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
883   // exponentiation:
884   //
885   //   BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
886   //
887   // This formula is trivially equivalent to the previous formula.  However,
888   // this formula can be implemented much more efficiently.  The trick is that
889   // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
890   // arithmetic.  To do exact division in modular arithmetic, all we have
891   // to do is multiply by the inverse.  Therefore, this step can be done at
892   // width W.
893   //
894   // The next issue is how to safely do the division by 2^T.  The way this
895   // is done is by doing the multiplication step at a width of at least W + T
896   // bits.  This way, the bottom W+T bits of the product are accurate. Then,
897   // when we perform the division by 2^T (which is equivalent to a right shift
898   // by T), the bottom W bits are accurate.  Extra bits are okay; they'll get
899   // truncated out after the division by 2^T.
900   //
901   // In comparison to just directly using the first formula, this technique
902   // is much more efficient; using the first formula requires W * K bits,
903   // but this formula less than W + K bits. Also, the first formula requires
904   // a division step, whereas this formula only requires multiplies and shifts.
905   //
906   // It doesn't matter whether the subtraction step is done in the calculation
907   // width or the input iteration count's width; if the subtraction overflows,
908   // the result must be zero anyway.  We prefer here to do it in the width of
909   // the induction variable because it helps a lot for certain cases; CodeGen
910   // isn't smart enough to ignore the overflow, which leads to much less
911   // efficient code if the width of the subtraction is wider than the native
912   // register width.
913   //
914   // (It's possible to not widen at all by pulling out factors of 2 before
915   // the multiplication; for example, K=2 can be calculated as
916   // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
917   // extra arithmetic, so it's not an obvious win, and it gets
918   // much more complicated for K > 3.)
919 
920   // Protection from insane SCEVs; this bound is conservative,
921   // but it probably doesn't matter.
922   if (K > 1000)
923     return SE.getCouldNotCompute();
924 
925   unsigned W = SE.getTypeSizeInBits(ResultTy);
926 
927   // Calculate K! / 2^T and T; we divide out the factors of two before
928   // multiplying for calculating K! / 2^T to avoid overflow.
929   // Other overflow doesn't matter because we only care about the bottom
930   // W bits of the result.
931   APInt OddFactorial(W, 1);
932   unsigned T = 1;
933   for (unsigned i = 3; i <= K; ++i) {
934     APInt Mult(W, i);
935     unsigned TwoFactors = Mult.countTrailingZeros();
936     T += TwoFactors;
937     Mult.lshrInPlace(TwoFactors);
938     OddFactorial *= Mult;
939   }
940 
941   // We need at least W + T bits for the multiplication step
942   unsigned CalculationBits = W + T;
943 
944   // Calculate 2^T, at width T+W.
945   APInt DivFactor = APInt::getOneBitSet(CalculationBits, T);
946 
947   // Calculate the multiplicative inverse of K! / 2^T;
948   // this multiplication factor will perform the exact division by
949   // K! / 2^T.
950   APInt Mod = APInt::getSignedMinValue(W+1);
951   APInt MultiplyFactor = OddFactorial.zext(W+1);
952   MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
953   MultiplyFactor = MultiplyFactor.trunc(W);
954 
955   // Calculate the product, at width T+W
956   IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
957                                                       CalculationBits);
958   const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
959   for (unsigned i = 1; i != K; ++i) {
960     const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
961     Dividend = SE.getMulExpr(Dividend,
962                              SE.getTruncateOrZeroExtend(S, CalculationTy));
963   }
964 
965   // Divide by 2^T
966   const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
967 
968   // Truncate the result, and divide by K! / 2^T.
969 
970   return SE.getMulExpr(SE.getConstant(MultiplyFactor),
971                        SE.getTruncateOrZeroExtend(DivResult, ResultTy));
972 }
973 
974 /// Return the value of this chain of recurrences at the specified iteration
975 /// number.  We can evaluate this recurrence by multiplying each element in the
976 /// chain by the binomial coefficient corresponding to it.  In other words, we
977 /// can evaluate {A,+,B,+,C,+,D} as:
978 ///
979 ///   A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
980 ///
981 /// where BC(It, k) stands for binomial coefficient.
evaluateAtIteration(const SCEV * It,ScalarEvolution & SE) const982 const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
983                                                 ScalarEvolution &SE) const {
984   const SCEV *Result = getStart();
985   for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
986     // The computation is correct in the face of overflow provided that the
987     // multiplication is performed _after_ the evaluation of the binomial
988     // coefficient.
989     const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
990     if (isa<SCEVCouldNotCompute>(Coeff))
991       return Coeff;
992 
993     Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
994   }
995   return Result;
996 }
997 
998 //===----------------------------------------------------------------------===//
999 //                    SCEV Expression folder implementations
1000 //===----------------------------------------------------------------------===//
1001 
getTruncateExpr(const SCEV * Op,Type * Ty,unsigned Depth)1002 const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty,
1003                                              unsigned Depth) {
1004   assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
1005          "This is not a truncating conversion!");
1006   assert(isSCEVable(Ty) &&
1007          "This is not a conversion to a SCEVable type!");
1008   Ty = getEffectiveSCEVType(Ty);
1009 
1010   FoldingSetNodeID ID;
1011   ID.AddInteger(scTruncate);
1012   ID.AddPointer(Op);
1013   ID.AddPointer(Ty);
1014   void *IP = nullptr;
1015   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1016 
1017   // Fold if the operand is constant.
1018   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1019     return getConstant(
1020       cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
1021 
1022   // trunc(trunc(x)) --> trunc(x)
1023   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
1024     return getTruncateExpr(ST->getOperand(), Ty, Depth + 1);
1025 
1026   // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
1027   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1028     return getTruncateOrSignExtend(SS->getOperand(), Ty, Depth + 1);
1029 
1030   // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
1031   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1032     return getTruncateOrZeroExtend(SZ->getOperand(), Ty, Depth + 1);
1033 
1034   if (Depth > MaxCastDepth) {
1035     SCEV *S =
1036         new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), Op, Ty);
1037     UniqueSCEVs.InsertNode(S, IP);
1038     addToLoopUseLists(S);
1039     return S;
1040   }
1041 
1042   // trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and
1043   // trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN),
1044   // if after transforming we have at most one truncate, not counting truncates
1045   // that replace other casts.
1046   if (isa<SCEVAddExpr>(Op) || isa<SCEVMulExpr>(Op)) {
1047     auto *CommOp = cast<SCEVCommutativeExpr>(Op);
1048     SmallVector<const SCEV *, 4> Operands;
1049     unsigned numTruncs = 0;
1050     for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2;
1051          ++i) {
1052       const SCEV *S = getTruncateExpr(CommOp->getOperand(i), Ty, Depth + 1);
1053       if (!isa<SCEVCastExpr>(CommOp->getOperand(i)) && isa<SCEVTruncateExpr>(S))
1054         numTruncs++;
1055       Operands.push_back(S);
1056     }
1057     if (numTruncs < 2) {
1058       if (isa<SCEVAddExpr>(Op))
1059         return getAddExpr(Operands);
1060       else if (isa<SCEVMulExpr>(Op))
1061         return getMulExpr(Operands);
1062       else
1063         llvm_unreachable("Unexpected SCEV type for Op.");
1064     }
1065     // Although we checked in the beginning that ID is not in the cache, it is
1066     // possible that during recursion and different modification ID was inserted
1067     // into the cache. So if we find it, just return it.
1068     if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1069       return S;
1070   }
1071 
1072   // If the input value is a chrec scev, truncate the chrec's operands.
1073   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
1074     SmallVector<const SCEV *, 4> Operands;
1075     for (const SCEV *Op : AddRec->operands())
1076       Operands.push_back(getTruncateExpr(Op, Ty, Depth + 1));
1077     return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
1078   }
1079 
1080   // The cast wasn't folded; create an explicit cast node. We can reuse
1081   // the existing insert position since if we get here, we won't have
1082   // made any changes which would invalidate it.
1083   SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
1084                                                  Op, Ty);
1085   UniqueSCEVs.InsertNode(S, IP);
1086   addToLoopUseLists(S);
1087   return S;
1088 }
1089 
1090 // Get the limit of a recurrence such that incrementing by Step cannot cause
1091 // signed overflow as long as the value of the recurrence within the
1092 // loop does not exceed this limit before incrementing.
getSignedOverflowLimitForStep(const SCEV * Step,ICmpInst::Predicate * Pred,ScalarEvolution * SE)1093 static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
1094                                                  ICmpInst::Predicate *Pred,
1095                                                  ScalarEvolution *SE) {
1096   unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1097   if (SE->isKnownPositive(Step)) {
1098     *Pred = ICmpInst::ICMP_SLT;
1099     return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
1100                            SE->getSignedRangeMax(Step));
1101   }
1102   if (SE->isKnownNegative(Step)) {
1103     *Pred = ICmpInst::ICMP_SGT;
1104     return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
1105                            SE->getSignedRangeMin(Step));
1106   }
1107   return nullptr;
1108 }
1109 
1110 // Get the limit of a recurrence such that incrementing by Step cannot cause
1111 // unsigned overflow as long as the value of the recurrence within the loop does
1112 // not exceed this limit before incrementing.
getUnsignedOverflowLimitForStep(const SCEV * Step,ICmpInst::Predicate * Pred,ScalarEvolution * SE)1113 static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
1114                                                    ICmpInst::Predicate *Pred,
1115                                                    ScalarEvolution *SE) {
1116   unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1117   *Pred = ICmpInst::ICMP_ULT;
1118 
1119   return SE->getConstant(APInt::getMinValue(BitWidth) -
1120                          SE->getUnsignedRangeMax(Step));
1121 }
1122 
1123 namespace {
1124 
1125 struct ExtendOpTraitsBase {
1126   typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *,
1127                                                           unsigned);
1128 };
1129 
1130 // Used to make code generic over signed and unsigned overflow.
1131 template <typename ExtendOp> struct ExtendOpTraits {
1132   // Members present:
1133   //
1134   // static const SCEV::NoWrapFlags WrapType;
1135   //
1136   // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1137   //
1138   // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1139   //                                           ICmpInst::Predicate *Pred,
1140   //                                           ScalarEvolution *SE);
1141 };
1142 
1143 template <>
1144 struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
1145   static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
1146 
1147   static const GetExtendExprTy GetExtendExpr;
1148 
getOverflowLimitForStep__anond51b32ac0411::ExtendOpTraits1149   static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1150                                              ICmpInst::Predicate *Pred,
1151                                              ScalarEvolution *SE) {
1152     return getSignedOverflowLimitForStep(Step, Pred, SE);
1153   }
1154 };
1155 
1156 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1157     SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
1158 
1159 template <>
1160 struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
1161   static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
1162 
1163   static const GetExtendExprTy GetExtendExpr;
1164 
getOverflowLimitForStep__anond51b32ac0411::ExtendOpTraits1165   static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1166                                              ICmpInst::Predicate *Pred,
1167                                              ScalarEvolution *SE) {
1168     return getUnsignedOverflowLimitForStep(Step, Pred, SE);
1169   }
1170 };
1171 
1172 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1173     SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
1174 
1175 } // end anonymous namespace
1176 
1177 // The recurrence AR has been shown to have no signed/unsigned wrap or something
1178 // close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1179 // easily prove NSW/NUW for its preincrement or postincrement sibling. This
1180 // allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1181 // Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1182 // expression "Step + sext/zext(PreIncAR)" is congruent with
1183 // "sext/zext(PostIncAR)"
1184 template <typename ExtendOpTy>
getPreStartForExtend(const SCEVAddRecExpr * AR,Type * Ty,ScalarEvolution * SE,unsigned Depth)1185 static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty,
1186                                         ScalarEvolution *SE, unsigned Depth) {
1187   auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1188   auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1189 
1190   const Loop *L = AR->getLoop();
1191   const SCEV *Start = AR->getStart();
1192   const SCEV *Step = AR->getStepRecurrence(*SE);
1193 
1194   // Check for a simple looking step prior to loop entry.
1195   const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1196   if (!SA)
1197     return nullptr;
1198 
1199   // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1200   // subtraction is expensive. For this purpose, perform a quick and dirty
1201   // difference, by checking for Step in the operand list.
1202   SmallVector<const SCEV *, 4> DiffOps;
1203   for (const SCEV *Op : SA->operands())
1204     if (Op != Step)
1205       DiffOps.push_back(Op);
1206 
1207   if (DiffOps.size() == SA->getNumOperands())
1208     return nullptr;
1209 
1210   // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1211   // `Step`:
1212 
1213   // 1. NSW/NUW flags on the step increment.
1214   auto PreStartFlags =
1215     ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW);
1216   const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags);
1217   const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1218       SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
1219 
1220   // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1221   // "S+X does not sign/unsign-overflow".
1222   //
1223 
1224   const SCEV *BECount = SE->getBackedgeTakenCount(L);
1225   if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
1226       !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
1227     return PreStart;
1228 
1229   // 2. Direct overflow check on the step operation's expression.
1230   unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
1231   Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
1232   const SCEV *OperandExtendedStart =
1233       SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth),
1234                      (SE->*GetExtendExpr)(Step, WideTy, Depth));
1235   if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) {
1236     if (PreAR && AR->getNoWrapFlags(WrapType)) {
1237       // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1238       // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1239       // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`.  Cache this fact.
1240       const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(WrapType);
1241     }
1242     return PreStart;
1243   }
1244 
1245   // 3. Loop precondition.
1246   ICmpInst::Predicate Pred;
1247   const SCEV *OverflowLimit =
1248       ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1249 
1250   if (OverflowLimit &&
1251       SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit))
1252     return PreStart;
1253 
1254   return nullptr;
1255 }
1256 
1257 // Get the normalized zero or sign extended expression for this AddRec's Start.
1258 template <typename ExtendOpTy>
getExtendAddRecStart(const SCEVAddRecExpr * AR,Type * Ty,ScalarEvolution * SE,unsigned Depth)1259 static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
1260                                         ScalarEvolution *SE,
1261                                         unsigned Depth) {
1262   auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1263 
1264   const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE, Depth);
1265   if (!PreStart)
1266     return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth);
1267 
1268   return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty,
1269                                              Depth),
1270                         (SE->*GetExtendExpr)(PreStart, Ty, Depth));
1271 }
1272 
1273 // Try to prove away overflow by looking at "nearby" add recurrences.  A
1274 // motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1275 // does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1276 //
1277 // Formally:
1278 //
1279 //     {S,+,X} == {S-T,+,X} + T
1280 //  => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1281 //
1282 // If ({S-T,+,X} + T) does not overflow  ... (1)
1283 //
1284 //  RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1285 //
1286 // If {S-T,+,X} does not overflow  ... (2)
1287 //
1288 //  RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1289 //      == {Ext(S-T)+Ext(T),+,Ext(X)}
1290 //
1291 // If (S-T)+T does not overflow  ... (3)
1292 //
1293 //  RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1294 //      == {Ext(S),+,Ext(X)} == LHS
1295 //
1296 // Thus, if (1), (2) and (3) are true for some T, then
1297 //   Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1298 //
1299 // (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1300 // does not overflow" restricted to the 0th iteration.  Therefore we only need
1301 // to check for (1) and (2).
1302 //
1303 // In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1304 // is `Delta` (defined below).
1305 template <typename ExtendOpTy>
proveNoWrapByVaryingStart(const SCEV * Start,const SCEV * Step,const Loop * L)1306 bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
1307                                                 const SCEV *Step,
1308                                                 const Loop *L) {
1309   auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1310 
1311   // We restrict `Start` to a constant to prevent SCEV from spending too much
1312   // time here.  It is correct (but more expensive) to continue with a
1313   // non-constant `Start` and do a general SCEV subtraction to compute
1314   // `PreStart` below.
1315   const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
1316   if (!StartC)
1317     return false;
1318 
1319   APInt StartAI = StartC->getAPInt();
1320 
1321   for (unsigned Delta : {-2, -1, 1, 2}) {
1322     const SCEV *PreStart = getConstant(StartAI - Delta);
1323 
1324     FoldingSetNodeID ID;
1325     ID.AddInteger(scAddRecExpr);
1326     ID.AddPointer(PreStart);
1327     ID.AddPointer(Step);
1328     ID.AddPointer(L);
1329     void *IP = nullptr;
1330     const auto *PreAR =
1331       static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1332 
1333     // Give up if we don't already have the add recurrence we need because
1334     // actually constructing an add recurrence is relatively expensive.
1335     if (PreAR && PreAR->getNoWrapFlags(WrapType)) {  // proves (2)
1336       const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
1337       ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1338       const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1339           DeltaS, &Pred, this);
1340       if (Limit && isKnownPredicate(Pred, PreAR, Limit))  // proves (1)
1341         return true;
1342     }
1343   }
1344 
1345   return false;
1346 }
1347 
1348 // Finds an integer D for an expression (C + x + y + ...) such that the top
1349 // level addition in (D + (C - D + x + y + ...)) would not wrap (signed or
1350 // unsigned) and the number of trailing zeros of (C - D + x + y + ...) is
1351 // maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and
1352 // the (C + x + y + ...) expression is \p WholeAddExpr.
extractConstantWithoutWrapping(ScalarEvolution & SE,const SCEVConstant * ConstantTerm,const SCEVAddExpr * WholeAddExpr)1353 static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1354                                             const SCEVConstant *ConstantTerm,
1355                                             const SCEVAddExpr *WholeAddExpr) {
1356   const APInt &C = ConstantTerm->getAPInt();
1357   const unsigned BitWidth = C.getBitWidth();
1358   // Find number of trailing zeros of (x + y + ...) w/o the C first:
1359   uint32_t TZ = BitWidth;
1360   for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I)
1361     TZ = std::min(TZ, SE.GetMinTrailingZeros(WholeAddExpr->getOperand(I)));
1362   if (TZ) {
1363     // Set D to be as many least significant bits of C as possible while still
1364     // guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap:
1365     return TZ < BitWidth ? C.trunc(TZ).zext(BitWidth) : C;
1366   }
1367   return APInt(BitWidth, 0);
1368 }
1369 
1370 // Finds an integer D for an affine AddRec expression {C,+,x} such that the top
1371 // level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the
1372 // number of trailing zeros of (C - D + x * n) is maximized, where C is the \p
1373 // ConstantStart, x is an arbitrary \p Step, and n is the loop trip count.
extractConstantWithoutWrapping(ScalarEvolution & SE,const APInt & ConstantStart,const SCEV * Step)1374 static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1375                                             const APInt &ConstantStart,
1376                                             const SCEV *Step) {
1377   const unsigned BitWidth = ConstantStart.getBitWidth();
1378   const uint32_t TZ = SE.GetMinTrailingZeros(Step);
1379   if (TZ)
1380     return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth)
1381                          : ConstantStart;
1382   return APInt(BitWidth, 0);
1383 }
1384 
1385 const SCEV *
getZeroExtendExpr(const SCEV * Op,Type * Ty,unsigned Depth)1386 ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1387   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1388          "This is not an extending conversion!");
1389   assert(isSCEVable(Ty) &&
1390          "This is not a conversion to a SCEVable type!");
1391   Ty = getEffectiveSCEVType(Ty);
1392 
1393   // Fold if the operand is constant.
1394   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1395     return getConstant(
1396       cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty)));
1397 
1398   // zext(zext(x)) --> zext(x)
1399   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1400     return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1401 
1402   // Before doing any expensive analysis, check to see if we've already
1403   // computed a SCEV for this Op and Ty.
1404   FoldingSetNodeID ID;
1405   ID.AddInteger(scZeroExtend);
1406   ID.AddPointer(Op);
1407   ID.AddPointer(Ty);
1408   void *IP = nullptr;
1409   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1410   if (Depth > MaxCastDepth) {
1411     SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1412                                                      Op, Ty);
1413     UniqueSCEVs.InsertNode(S, IP);
1414     addToLoopUseLists(S);
1415     return S;
1416   }
1417 
1418   // zext(trunc(x)) --> zext(x) or x or trunc(x)
1419   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1420     // It's possible the bits taken off by the truncate were all zero bits. If
1421     // so, we should be able to simplify this further.
1422     const SCEV *X = ST->getOperand();
1423     ConstantRange CR = getUnsignedRange(X);
1424     unsigned TruncBits = getTypeSizeInBits(ST->getType());
1425     unsigned NewBits = getTypeSizeInBits(Ty);
1426     if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
1427             CR.zextOrTrunc(NewBits)))
1428       return getTruncateOrZeroExtend(X, Ty, Depth);
1429   }
1430 
1431   // If the input value is a chrec scev, and we can prove that the value
1432   // did not overflow the old, smaller, value, we can zero extend all of the
1433   // operands (often constants).  This allows analysis of something like
1434   // this:  for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
1435   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1436     if (AR->isAffine()) {
1437       const SCEV *Start = AR->getStart();
1438       const SCEV *Step = AR->getStepRecurrence(*this);
1439       unsigned BitWidth = getTypeSizeInBits(AR->getType());
1440       const Loop *L = AR->getLoop();
1441 
1442       if (!AR->hasNoUnsignedWrap()) {
1443         auto NewFlags = proveNoWrapViaConstantRanges(AR);
1444         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
1445       }
1446 
1447       // If we have special knowledge that this addrec won't overflow,
1448       // we don't need to do any further analysis.
1449       if (AR->hasNoUnsignedWrap())
1450         return getAddRecExpr(
1451             getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1452             getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1453 
1454       // Check whether the backedge-taken count is SCEVCouldNotCompute.
1455       // Note that this serves two purposes: It filters out loops that are
1456       // simply not analyzable, and it covers the case where this code is
1457       // being called from within backedge-taken count analysis, such that
1458       // attempting to ask for the backedge-taken count would likely result
1459       // in infinite recursion. In the later case, the analysis code will
1460       // cope with a conservative value, and it will take care to purge
1461       // that value once it has finished.
1462       const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
1463       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1464         // Manually compute the final value for AR, checking for
1465         // overflow.
1466 
1467         // Check whether the backedge-taken count can be losslessly casted to
1468         // the addrec's type. The count is always unsigned.
1469         const SCEV *CastedMaxBECount =
1470             getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth);
1471         const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
1472             CastedMaxBECount, MaxBECount->getType(), Depth);
1473         if (MaxBECount == RecastedMaxBECount) {
1474           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1475           // Check whether Start+Step*MaxBECount has no unsigned overflow.
1476           const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step,
1477                                         SCEV::FlagAnyWrap, Depth + 1);
1478           const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul,
1479                                                           SCEV::FlagAnyWrap,
1480                                                           Depth + 1),
1481                                                WideTy, Depth + 1);
1482           const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1);
1483           const SCEV *WideMaxBECount =
1484             getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
1485           const SCEV *OperandExtendedAdd =
1486             getAddExpr(WideStart,
1487                        getMulExpr(WideMaxBECount,
1488                                   getZeroExtendExpr(Step, WideTy, Depth + 1),
1489                                   SCEV::FlagAnyWrap, Depth + 1),
1490                        SCEV::FlagAnyWrap, Depth + 1);
1491           if (ZAdd == OperandExtendedAdd) {
1492             // Cache knowledge of AR NUW, which is propagated to this AddRec.
1493             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1494             // Return the expression with the addrec on the outside.
1495             return getAddRecExpr(
1496                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1497                                                          Depth + 1),
1498                 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1499                 AR->getNoWrapFlags());
1500           }
1501           // Similar to above, only this time treat the step value as signed.
1502           // This covers loops that count down.
1503           OperandExtendedAdd =
1504             getAddExpr(WideStart,
1505                        getMulExpr(WideMaxBECount,
1506                                   getSignExtendExpr(Step, WideTy, Depth + 1),
1507                                   SCEV::FlagAnyWrap, Depth + 1),
1508                        SCEV::FlagAnyWrap, Depth + 1);
1509           if (ZAdd == OperandExtendedAdd) {
1510             // Cache knowledge of AR NW, which is propagated to this AddRec.
1511             // Negative step causes unsigned wrap, but it still can't self-wrap.
1512             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1513             // Return the expression with the addrec on the outside.
1514             return getAddRecExpr(
1515                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1516                                                          Depth + 1),
1517                 getSignExtendExpr(Step, Ty, Depth + 1), L,
1518                 AR->getNoWrapFlags());
1519           }
1520         }
1521       }
1522 
1523       // Normally, in the cases we can prove no-overflow via a
1524       // backedge guarding condition, we can also compute a backedge
1525       // taken count for the loop.  The exceptions are assumptions and
1526       // guards present in the loop -- SCEV is not great at exploiting
1527       // these to compute max backedge taken counts, but can still use
1528       // these to prove lack of overflow.  Use this fact to avoid
1529       // doing extra work that may not pay off.
1530       if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
1531           !AC.assumptions().empty()) {
1532         // If the backedge is guarded by a comparison with the pre-inc
1533         // value the addrec is safe. Also, if the entry is guarded by
1534         // a comparison with the start value and the backedge is
1535         // guarded by a comparison with the post-inc value, the addrec
1536         // is safe.
1537         if (isKnownPositive(Step)) {
1538           const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
1539                                       getUnsignedRangeMax(Step));
1540           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
1541               isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) {
1542             // Cache knowledge of AR NUW, which is propagated to this
1543             // AddRec.
1544             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1545             // Return the expression with the addrec on the outside.
1546             return getAddRecExpr(
1547                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1548                                                          Depth + 1),
1549                 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1550                 AR->getNoWrapFlags());
1551           }
1552         } else if (isKnownNegative(Step)) {
1553           const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
1554                                       getSignedRangeMin(Step));
1555           if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
1556               isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR, N)) {
1557             // Cache knowledge of AR NW, which is propagated to this
1558             // AddRec.  Negative step causes unsigned wrap, but it
1559             // still can't self-wrap.
1560             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1561             // Return the expression with the addrec on the outside.
1562             return getAddRecExpr(
1563                 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1564                                                          Depth + 1),
1565                 getSignExtendExpr(Step, Ty, Depth + 1), L,
1566                 AR->getNoWrapFlags());
1567           }
1568         }
1569       }
1570 
1571       // zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step}))<nuw><nsw>
1572       // if D + (C - D + Step * n) could be proven to not unsigned wrap
1573       // where D maximizes the number of trailing zeros of (C - D + Step * n)
1574       if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
1575         const APInt &C = SC->getAPInt();
1576         const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
1577         if (D != 0) {
1578           const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1579           const SCEV *SResidual =
1580               getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
1581           const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1582           return getAddExpr(SZExtD, SZExtR,
1583                             (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1584                             Depth + 1);
1585         }
1586       }
1587 
1588       if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1589         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1590         return getAddRecExpr(
1591             getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1592             getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1593       }
1594     }
1595 
1596   // zext(A % B) --> zext(A) % zext(B)
1597   {
1598     const SCEV *LHS;
1599     const SCEV *RHS;
1600     if (matchURem(Op, LHS, RHS))
1601       return getURemExpr(getZeroExtendExpr(LHS, Ty, Depth + 1),
1602                          getZeroExtendExpr(RHS, Ty, Depth + 1));
1603   }
1604 
1605   // zext(A / B) --> zext(A) / zext(B).
1606   if (auto *Div = dyn_cast<SCEVUDivExpr>(Op))
1607     return getUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty, Depth + 1),
1608                        getZeroExtendExpr(Div->getRHS(), Ty, Depth + 1));
1609 
1610   if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1611     // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
1612     if (SA->hasNoUnsignedWrap()) {
1613       // If the addition does not unsign overflow then we can, by definition,
1614       // commute the zero extension with the addition operation.
1615       SmallVector<const SCEV *, 4> Ops;
1616       for (const auto *Op : SA->operands())
1617         Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1618       return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1);
1619     }
1620 
1621     // zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...))
1622     // if D + (C - D + x + y + ...) could be proven to not unsigned wrap
1623     // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1624     //
1625     // Often address arithmetics contain expressions like
1626     // (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))).
1627     // This transformation is useful while proving that such expressions are
1628     // equal or differ by a small constant amount, see LoadStoreVectorizer pass.
1629     if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1630       const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1631       if (D != 0) {
1632         const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1633         const SCEV *SResidual =
1634             getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth);
1635         const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1636         return getAddExpr(SZExtD, SZExtR,
1637                           (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1638                           Depth + 1);
1639       }
1640     }
1641   }
1642 
1643   if (auto *SM = dyn_cast<SCEVMulExpr>(Op)) {
1644     // zext((A * B * ...)<nuw>) --> (zext(A) * zext(B) * ...)<nuw>
1645     if (SM->hasNoUnsignedWrap()) {
1646       // If the multiply does not unsign overflow then we can, by definition,
1647       // commute the zero extension with the multiply operation.
1648       SmallVector<const SCEV *, 4> Ops;
1649       for (const auto *Op : SM->operands())
1650         Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1651       return getMulExpr(Ops, SCEV::FlagNUW, Depth + 1);
1652     }
1653 
1654     // zext(2^K * (trunc X to iN)) to iM ->
1655     // 2^K * (zext(trunc X to i{N-K}) to iM)<nuw>
1656     //
1657     // Proof:
1658     //
1659     //     zext(2^K * (trunc X to iN)) to iM
1660     //   = zext((trunc X to iN) << K) to iM
1661     //   = zext((trunc X to i{N-K}) << K)<nuw> to iM
1662     //     (because shl removes the top K bits)
1663     //   = zext((2^K * (trunc X to i{N-K}))<nuw>) to iM
1664     //   = (2^K * (zext(trunc X to i{N-K}) to iM))<nuw>.
1665     //
1666     if (SM->getNumOperands() == 2)
1667       if (auto *MulLHS = dyn_cast<SCEVConstant>(SM->getOperand(0)))
1668         if (MulLHS->getAPInt().isPowerOf2())
1669           if (auto *TruncRHS = dyn_cast<SCEVTruncateExpr>(SM->getOperand(1))) {
1670             int NewTruncBits = getTypeSizeInBits(TruncRHS->getType()) -
1671                                MulLHS->getAPInt().logBase2();
1672             Type *NewTruncTy = IntegerType::get(getContext(), NewTruncBits);
1673             return getMulExpr(
1674                 getZeroExtendExpr(MulLHS, Ty),
1675                 getZeroExtendExpr(
1676                     getTruncateExpr(TruncRHS->getOperand(), NewTruncTy), Ty),
1677                 SCEV::FlagNUW, Depth + 1);
1678           }
1679   }
1680 
1681   // The cast wasn't folded; create an explicit cast node.
1682   // Recompute the insert position, as it may have been invalidated.
1683   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1684   SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1685                                                    Op, Ty);
1686   UniqueSCEVs.InsertNode(S, IP);
1687   addToLoopUseLists(S);
1688   return S;
1689 }
1690 
1691 const SCEV *
getSignExtendExpr(const SCEV * Op,Type * Ty,unsigned Depth)1692 ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1693   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1694          "This is not an extending conversion!");
1695   assert(isSCEVable(Ty) &&
1696          "This is not a conversion to a SCEVable type!");
1697   Ty = getEffectiveSCEVType(Ty);
1698 
1699   // Fold if the operand is constant.
1700   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1701     return getConstant(
1702       cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty)));
1703 
1704   // sext(sext(x)) --> sext(x)
1705   if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1706     return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1);
1707 
1708   // sext(zext(x)) --> zext(x)
1709   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1710     return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1711 
1712   // Before doing any expensive analysis, check to see if we've already
1713   // computed a SCEV for this Op and Ty.
1714   FoldingSetNodeID ID;
1715   ID.AddInteger(scSignExtend);
1716   ID.AddPointer(Op);
1717   ID.AddPointer(Ty);
1718   void *IP = nullptr;
1719   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1720   // Limit recursion depth.
1721   if (Depth > MaxCastDepth) {
1722     SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1723                                                      Op, Ty);
1724     UniqueSCEVs.InsertNode(S, IP);
1725     addToLoopUseLists(S);
1726     return S;
1727   }
1728 
1729   // sext(trunc(x)) --> sext(x) or x or trunc(x)
1730   if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1731     // It's possible the bits taken off by the truncate were all sign bits. If
1732     // so, we should be able to simplify this further.
1733     const SCEV *X = ST->getOperand();
1734     ConstantRange CR = getSignedRange(X);
1735     unsigned TruncBits = getTypeSizeInBits(ST->getType());
1736     unsigned NewBits = getTypeSizeInBits(Ty);
1737     if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1738             CR.sextOrTrunc(NewBits)))
1739       return getTruncateOrSignExtend(X, Ty, Depth);
1740   }
1741 
1742   if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1743     // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
1744     if (SA->hasNoSignedWrap()) {
1745       // If the addition does not sign overflow then we can, by definition,
1746       // commute the sign extension with the addition operation.
1747       SmallVector<const SCEV *, 4> Ops;
1748       for (const auto *Op : SA->operands())
1749         Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1));
1750       return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1);
1751     }
1752 
1753     // sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...))
1754     // if D + (C - D + x + y + ...) could be proven to not signed wrap
1755     // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1756     //
1757     // For instance, this will bring two seemingly different expressions:
1758     //     1 + sext(5 + 20 * %x + 24 * %y)  and
1759     //         sext(6 + 20 * %x + 24 * %y)
1760     // to the same form:
1761     //     2 + sext(4 + 20 * %x + 24 * %y)
1762     if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1763       const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1764       if (D != 0) {
1765         const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
1766         const SCEV *SResidual =
1767             getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth);
1768         const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
1769         return getAddExpr(SSExtD, SSExtR,
1770                           (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1771                           Depth + 1);
1772       }
1773     }
1774   }
1775   // If the input value is a chrec scev, and we can prove that the value
1776   // did not overflow the old, smaller, value, we can sign extend all of the
1777   // operands (often constants).  This allows analysis of something like
1778   // this:  for (signed char X = 0; X < 100; ++X) { int Y = X; }
1779   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1780     if (AR->isAffine()) {
1781       const SCEV *Start = AR->getStart();
1782       const SCEV *Step = AR->getStepRecurrence(*this);
1783       unsigned BitWidth = getTypeSizeInBits(AR->getType());
1784       const Loop *L = AR->getLoop();
1785 
1786       if (!AR->hasNoSignedWrap()) {
1787         auto NewFlags = proveNoWrapViaConstantRanges(AR);
1788         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
1789       }
1790 
1791       // If we have special knowledge that this addrec won't overflow,
1792       // we don't need to do any further analysis.
1793       if (AR->hasNoSignedWrap())
1794         return getAddRecExpr(
1795             getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
1796             getSignExtendExpr(Step, Ty, Depth + 1), L, SCEV::FlagNSW);
1797 
1798       // Check whether the backedge-taken count is SCEVCouldNotCompute.
1799       // Note that this serves two purposes: It filters out loops that are
1800       // simply not analyzable, and it covers the case where this code is
1801       // being called from within backedge-taken count analysis, such that
1802       // attempting to ask for the backedge-taken count would likely result
1803       // in infinite recursion. In the later case, the analysis code will
1804       // cope with a conservative value, and it will take care to purge
1805       // that value once it has finished.
1806       const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
1807       if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1808         // Manually compute the final value for AR, checking for
1809         // overflow.
1810 
1811         // Check whether the backedge-taken count can be losslessly casted to
1812         // the addrec's type. The count is always unsigned.
1813         const SCEV *CastedMaxBECount =
1814             getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth);
1815         const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
1816             CastedMaxBECount, MaxBECount->getType(), Depth);
1817         if (MaxBECount == RecastedMaxBECount) {
1818           Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1819           // Check whether Start+Step*MaxBECount has no signed overflow.
1820           const SCEV *SMul = getMulExpr(CastedMaxBECount, Step,
1821                                         SCEV::FlagAnyWrap, Depth + 1);
1822           const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul,
1823                                                           SCEV::FlagAnyWrap,
1824                                                           Depth + 1),
1825                                                WideTy, Depth + 1);
1826           const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1);
1827           const SCEV *WideMaxBECount =
1828             getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
1829           const SCEV *OperandExtendedAdd =
1830             getAddExpr(WideStart,
1831                        getMulExpr(WideMaxBECount,
1832                                   getSignExtendExpr(Step, WideTy, Depth + 1),
1833                                   SCEV::FlagAnyWrap, Depth + 1),
1834                        SCEV::FlagAnyWrap, Depth + 1);
1835           if (SAdd == OperandExtendedAdd) {
1836             // Cache knowledge of AR NSW, which is propagated to this AddRec.
1837             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1838             // Return the expression with the addrec on the outside.
1839             return getAddRecExpr(
1840                 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
1841                                                          Depth + 1),
1842                 getSignExtendExpr(Step, Ty, Depth + 1), L,
1843                 AR->getNoWrapFlags());
1844           }
1845           // Similar to above, only this time treat the step value as unsigned.
1846           // This covers loops that count up with an unsigned step.
1847           OperandExtendedAdd =
1848             getAddExpr(WideStart,
1849                        getMulExpr(WideMaxBECount,
1850                                   getZeroExtendExpr(Step, WideTy, Depth + 1),
1851                                   SCEV::FlagAnyWrap, Depth + 1),
1852                        SCEV::FlagAnyWrap, Depth + 1);
1853           if (SAdd == OperandExtendedAdd) {
1854             // If AR wraps around then
1855             //
1856             //    abs(Step) * MaxBECount > unsigned-max(AR->getType())
1857             // => SAdd != OperandExtendedAdd
1858             //
1859             // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
1860             // (SAdd == OperandExtendedAdd => AR is NW)
1861 
1862             const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1863 
1864             // Return the expression with the addrec on the outside.
1865             return getAddRecExpr(
1866                 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
1867                                                          Depth + 1),
1868                 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1869                 AR->getNoWrapFlags());
1870           }
1871         }
1872       }
1873 
1874       // Normally, in the cases we can prove no-overflow via a
1875       // backedge guarding condition, we can also compute a backedge
1876       // taken count for the loop.  The exceptions are assumptions and
1877       // guards present in the loop -- SCEV is not great at exploiting
1878       // these to compute max backedge taken counts, but can still use
1879       // these to prove lack of overflow.  Use this fact to avoid
1880       // doing extra work that may not pay off.
1881 
1882       if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
1883           !AC.assumptions().empty()) {
1884         // If the backedge is guarded by a comparison with the pre-inc
1885         // value the addrec is safe. Also, if the entry is guarded by
1886         // a comparison with the start value and the backedge is
1887         // guarded by a comparison with the post-inc value, the addrec
1888         // is safe.
1889         ICmpInst::Predicate Pred;
1890         const SCEV *OverflowLimit =
1891             getSignedOverflowLimitForStep(Step, &Pred, this);
1892         if (OverflowLimit &&
1893             (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
1894              isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
1895           // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec.
1896           const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1897           return getAddRecExpr(
1898               getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
1899               getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1900         }
1901       }
1902 
1903       // sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step}))<nuw><nsw>
1904       // if D + (C - D + Step * n) could be proven to not signed wrap
1905       // where D maximizes the number of trailing zeros of (C - D + Step * n)
1906       if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
1907         const APInt &C = SC->getAPInt();
1908         const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
1909         if (D != 0) {
1910           const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
1911           const SCEV *SResidual =
1912               getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
1913           const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
1914           return getAddExpr(SSExtD, SSExtR,
1915                             (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1916                             Depth + 1);
1917         }
1918       }
1919 
1920       if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
1921         const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
1922         return getAddRecExpr(
1923             getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
1924             getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1925       }
1926     }
1927 
1928   // If the input value is provably positive and we could not simplify
1929   // away the sext build a zext instead.
1930   if (isKnownNonNegative(Op))
1931     return getZeroExtendExpr(Op, Ty, Depth + 1);
1932 
1933   // The cast wasn't folded; create an explicit cast node.
1934   // Recompute the insert position, as it may have been invalidated.
1935   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1936   SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1937                                                    Op, Ty);
1938   UniqueSCEVs.InsertNode(S, IP);
1939   addToLoopUseLists(S);
1940   return S;
1941 }
1942 
1943 /// getAnyExtendExpr - Return a SCEV for the given operand extended with
1944 /// unspecified bits out to the given type.
getAnyExtendExpr(const SCEV * Op,Type * Ty)1945 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
1946                                               Type *Ty) {
1947   assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1948          "This is not an extending conversion!");
1949   assert(isSCEVable(Ty) &&
1950          "This is not a conversion to a SCEVable type!");
1951   Ty = getEffectiveSCEVType(Ty);
1952 
1953   // Sign-extend negative constants.
1954   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1955     if (SC->getAPInt().isNegative())
1956       return getSignExtendExpr(Op, Ty);
1957 
1958   // Peel off a truncate cast.
1959   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
1960     const SCEV *NewOp = T->getOperand();
1961     if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
1962       return getAnyExtendExpr(NewOp, Ty);
1963     return getTruncateOrNoop(NewOp, Ty);
1964   }
1965 
1966   // Next try a zext cast. If the cast is folded, use it.
1967   const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
1968   if (!isa<SCEVZeroExtendExpr>(ZExt))
1969     return ZExt;
1970 
1971   // Next try a sext cast. If the cast is folded, use it.
1972   const SCEV *SExt = getSignExtendExpr(Op, Ty);
1973   if (!isa<SCEVSignExtendExpr>(SExt))
1974     return SExt;
1975 
1976   // Force the cast to be folded into the operands of an addrec.
1977   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
1978     SmallVector<const SCEV *, 4> Ops;
1979     for (const SCEV *Op : AR->operands())
1980       Ops.push_back(getAnyExtendExpr(Op, Ty));
1981     return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
1982   }
1983 
1984   // If the expression is obviously signed, use the sext cast value.
1985   if (isa<SCEVSMaxExpr>(Op))
1986     return SExt;
1987 
1988   // Absent any other information, use the zext cast value.
1989   return ZExt;
1990 }
1991 
1992 /// Process the given Ops list, which is a list of operands to be added under
1993 /// the given scale, update the given map. This is a helper function for
1994 /// getAddRecExpr. As an example of what it does, given a sequence of operands
1995 /// that would form an add expression like this:
1996 ///
1997 ///    m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
1998 ///
1999 /// where A and B are constants, update the map with these values:
2000 ///
2001 ///    (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
2002 ///
2003 /// and add 13 + A*B*29 to AccumulatedConstant.
2004 /// This will allow getAddRecExpr to produce this:
2005 ///
2006 ///    13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
2007 ///
2008 /// This form often exposes folding opportunities that are hidden in
2009 /// the original operand list.
2010 ///
2011 /// Return true iff it appears that any interesting folding opportunities
2012 /// may be exposed. This helps getAddRecExpr short-circuit extra work in
2013 /// the common case where no interesting opportunities are present, and
2014 /// is also used as a check to avoid infinite recursion.
2015 static bool
CollectAddOperandsWithScales(DenseMap<const SCEV *,APInt> & M,SmallVectorImpl<const SCEV * > & NewOps,APInt & AccumulatedConstant,const SCEV * const * Ops,size_t NumOperands,const APInt & Scale,ScalarEvolution & SE)2016 CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
2017                              SmallVectorImpl<const SCEV *> &NewOps,
2018                              APInt &AccumulatedConstant,
2019                              const SCEV *const *Ops, size_t NumOperands,
2020                              const APInt &Scale,
2021                              ScalarEvolution &SE) {
2022   bool Interesting = false;
2023 
2024   // Iterate over the add operands. They are sorted, with constants first.
2025   unsigned i = 0;
2026   while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2027     ++i;
2028     // Pull a buried constant out to the outside.
2029     if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
2030       Interesting = true;
2031     AccumulatedConstant += Scale * C->getAPInt();
2032   }
2033 
2034   // Next comes everything else. We're especially interested in multiplies
2035   // here, but they're in the middle, so just visit the rest with one loop.
2036   for (; i != NumOperands; ++i) {
2037     const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
2038     if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
2039       APInt NewScale =
2040           Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt();
2041       if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
2042         // A multiplication of a constant with another add; recurse.
2043         const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
2044         Interesting |=
2045           CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2046                                        Add->op_begin(), Add->getNumOperands(),
2047                                        NewScale, SE);
2048       } else {
2049         // A multiplication of a constant with some other value. Update
2050         // the map.
2051         SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
2052         const SCEV *Key = SE.getMulExpr(MulOps);
2053         auto Pair = M.insert({Key, NewScale});
2054         if (Pair.second) {
2055           NewOps.push_back(Pair.first->first);
2056         } else {
2057           Pair.first->second += NewScale;
2058           // The map already had an entry for this value, which may indicate
2059           // a folding opportunity.
2060           Interesting = true;
2061         }
2062       }
2063     } else {
2064       // An ordinary operand. Update the map.
2065       std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
2066           M.insert({Ops[i], Scale});
2067       if (Pair.second) {
2068         NewOps.push_back(Pair.first->first);
2069       } else {
2070         Pair.first->second += Scale;
2071         // The map already had an entry for this value, which may indicate
2072         // a folding opportunity.
2073         Interesting = true;
2074       }
2075     }
2076   }
2077 
2078   return Interesting;
2079 }
2080 
2081 // We're trying to construct a SCEV of type `Type' with `Ops' as operands and
2082 // `OldFlags' as can't-wrap behavior.  Infer a more aggressive set of
2083 // can't-overflow flags for the operation if possible.
2084 static SCEV::NoWrapFlags
StrengthenNoWrapFlags(ScalarEvolution * SE,SCEVTypes Type,const ArrayRef<const SCEV * > Ops,SCEV::NoWrapFlags Flags)2085 StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
2086                       const ArrayRef<const SCEV *> Ops,
2087                       SCEV::NoWrapFlags Flags) {
2088   using namespace std::placeholders;
2089 
2090   using OBO = OverflowingBinaryOperator;
2091 
2092   bool CanAnalyze =
2093       Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr;
2094   (void)CanAnalyze;
2095   assert(CanAnalyze && "don't call from other places!");
2096 
2097   int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
2098   SCEV::NoWrapFlags SignOrUnsignWrap =
2099       ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2100 
2101   // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
2102   auto IsKnownNonNegative = [&](const SCEV *S) {
2103     return SE->isKnownNonNegative(S);
2104   };
2105 
2106   if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative))
2107     Flags =
2108         ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
2109 
2110   SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2111 
2112   if (SignOrUnsignWrap != SignOrUnsignMask &&
2113       (Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 &&
2114       isa<SCEVConstant>(Ops[0])) {
2115 
2116     auto Opcode = [&] {
2117       switch (Type) {
2118       case scAddExpr:
2119         return Instruction::Add;
2120       case scMulExpr:
2121         return Instruction::Mul;
2122       default:
2123         llvm_unreachable("Unexpected SCEV op.");
2124       }
2125     }();
2126 
2127     const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt();
2128 
2129     // (A <opcode> C) --> (A <opcode> C)<nsw> if the op doesn't sign overflow.
2130     if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
2131       auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2132           Opcode, C, OBO::NoSignedWrap);
2133       if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
2134         Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
2135     }
2136 
2137     // (A <opcode> C) --> (A <opcode> C)<nuw> if the op doesn't unsign overflow.
2138     if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
2139       auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2140           Opcode, C, OBO::NoUnsignedWrap);
2141       if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
2142         Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2143     }
2144   }
2145 
2146   return Flags;
2147 }
2148 
isAvailableAtLoopEntry(const SCEV * S,const Loop * L)2149 bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
2150   return isLoopInvariant(S, L) && properlyDominates(S, L->getHeader());
2151 }
2152 
2153 /// Get a canonical add expression, or something simpler if possible.
getAddExpr(SmallVectorImpl<const SCEV * > & Ops,SCEV::NoWrapFlags Flags,unsigned Depth)2154 const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
2155                                         SCEV::NoWrapFlags Flags,
2156                                         unsigned Depth) {
2157   assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
2158          "only nuw or nsw allowed");
2159   assert(!Ops.empty() && "Cannot get empty add!");
2160   if (Ops.size() == 1) return Ops[0];
2161 #ifndef NDEBUG
2162   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2163   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2164     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2165            "SCEVAddExpr operand types don't match!");
2166 #endif
2167 
2168   // Sort by complexity, this groups all similar expression types together.
2169   GroupByComplexity(Ops, &LI, DT);
2170 
2171   Flags = StrengthenNoWrapFlags(this, scAddExpr, Ops, Flags);
2172 
2173   // If there are any constants, fold them together.
2174   unsigned Idx = 0;
2175   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2176     ++Idx;
2177     assert(Idx < Ops.size());
2178     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2179       // We found two constants, fold them together!
2180       Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt());
2181       if (Ops.size() == 2) return Ops[0];
2182       Ops.erase(Ops.begin()+1);  // Erase the folded element
2183       LHSC = cast<SCEVConstant>(Ops[0]);
2184     }
2185 
2186     // If we are left with a constant zero being added, strip it off.
2187     if (LHSC->getValue()->isZero()) {
2188       Ops.erase(Ops.begin());
2189       --Idx;
2190     }
2191 
2192     if (Ops.size() == 1) return Ops[0];
2193   }
2194 
2195   // Limit recursion calls depth.
2196   if (Depth > MaxArithDepth || hasHugeExpression(Ops))
2197     return getOrCreateAddExpr(Ops, Flags);
2198 
2199   if (SCEV *S = std::get<0>(findExistingSCEVInCache(scAddExpr, Ops))) {
2200     static_cast<SCEVAddExpr *>(S)->setNoWrapFlags(Flags);
2201     return S;
2202   }
2203 
2204   // Okay, check to see if the same value occurs in the operand list more than
2205   // once.  If so, merge them together into an multiply expression.  Since we
2206   // sorted the list, these values are required to be adjacent.
2207   Type *Ty = Ops[0]->getType();
2208   bool FoundMatch = false;
2209   for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
2210     if (Ops[i] == Ops[i+1]) {      //  X + Y + Y  -->  X + Y*2
2211       // Scan ahead to count how many equal operands there are.
2212       unsigned Count = 2;
2213       while (i+Count != e && Ops[i+Count] == Ops[i])
2214         ++Count;
2215       // Merge the values into a multiply.
2216       const SCEV *Scale = getConstant(Ty, Count);
2217       const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1);
2218       if (Ops.size() == Count)
2219         return Mul;
2220       Ops[i] = Mul;
2221       Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
2222       --i; e -= Count - 1;
2223       FoundMatch = true;
2224     }
2225   if (FoundMatch)
2226     return getAddExpr(Ops, Flags, Depth + 1);
2227 
2228   // Check for truncates. If all the operands are truncated from the same
2229   // type, see if factoring out the truncate would permit the result to be
2230   // folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y)
2231   // if the contents of the resulting outer trunc fold to something simple.
2232   auto FindTruncSrcType = [&]() -> Type * {
2233     // We're ultimately looking to fold an addrec of truncs and muls of only
2234     // constants and truncs, so if we find any other types of SCEV
2235     // as operands of the addrec then we bail and return nullptr here.
2236     // Otherwise, we return the type of the operand of a trunc that we find.
2237     if (auto *T = dyn_cast<SCEVTruncateExpr>(Ops[Idx]))
2238       return T->getOperand()->getType();
2239     if (const auto *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2240       const auto *LastOp = Mul->getOperand(Mul->getNumOperands() - 1);
2241       if (const auto *T = dyn_cast<SCEVTruncateExpr>(LastOp))
2242         return T->getOperand()->getType();
2243     }
2244     return nullptr;
2245   };
2246   if (auto *SrcType = FindTruncSrcType()) {
2247     SmallVector<const SCEV *, 8> LargeOps;
2248     bool Ok = true;
2249     // Check all the operands to see if they can be represented in the
2250     // source type of the truncate.
2251     for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
2252       if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
2253         if (T->getOperand()->getType() != SrcType) {
2254           Ok = false;
2255           break;
2256         }
2257         LargeOps.push_back(T->getOperand());
2258       } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2259         LargeOps.push_back(getAnyExtendExpr(C, SrcType));
2260       } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
2261         SmallVector<const SCEV *, 8> LargeMulOps;
2262         for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2263           if (const SCEVTruncateExpr *T =
2264                 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2265             if (T->getOperand()->getType() != SrcType) {
2266               Ok = false;
2267               break;
2268             }
2269             LargeMulOps.push_back(T->getOperand());
2270           } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) {
2271             LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
2272           } else {
2273             Ok = false;
2274             break;
2275           }
2276         }
2277         if (Ok)
2278           LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1));
2279       } else {
2280         Ok = false;
2281         break;
2282       }
2283     }
2284     if (Ok) {
2285       // Evaluate the expression in the larger type.
2286       const SCEV *Fold = getAddExpr(LargeOps, SCEV::FlagAnyWrap, Depth + 1);
2287       // If it folds to something simple, use it. Otherwise, don't.
2288       if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
2289         return getTruncateExpr(Fold, Ty);
2290     }
2291   }
2292 
2293   // Skip past any other cast SCEVs.
2294   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
2295     ++Idx;
2296 
2297   // If there are add operands they would be next.
2298   if (Idx < Ops.size()) {
2299     bool DeletedAdd = false;
2300     while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
2301       if (Ops.size() > AddOpsInlineThreshold ||
2302           Add->getNumOperands() > AddOpsInlineThreshold)
2303         break;
2304       // If we have an add, expand the add operands onto the end of the operands
2305       // list.
2306       Ops.erase(Ops.begin()+Idx);
2307       Ops.append(Add->op_begin(), Add->op_end());
2308       DeletedAdd = true;
2309     }
2310 
2311     // If we deleted at least one add, we added operands to the end of the list,
2312     // and they are not necessarily sorted.  Recurse to resort and resimplify
2313     // any operands we just acquired.
2314     if (DeletedAdd)
2315       return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2316   }
2317 
2318   // Skip over the add expression until we get to a multiply.
2319   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2320     ++Idx;
2321 
2322   // Check to see if there are any folding opportunities present with
2323   // operands multiplied by constant values.
2324   if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
2325     uint64_t BitWidth = getTypeSizeInBits(Ty);
2326     DenseMap<const SCEV *, APInt> M;
2327     SmallVector<const SCEV *, 8> NewOps;
2328     APInt AccumulatedConstant(BitWidth, 0);
2329     if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2330                                      Ops.data(), Ops.size(),
2331                                      APInt(BitWidth, 1), *this)) {
2332       struct APIntCompare {
2333         bool operator()(const APInt &LHS, const APInt &RHS) const {
2334           return LHS.ult(RHS);
2335         }
2336       };
2337 
2338       // Some interesting folding opportunity is present, so its worthwhile to
2339       // re-generate the operands list. Group the operands by constant scale,
2340       // to avoid multiplying by the same constant scale multiple times.
2341       std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
2342       for (const SCEV *NewOp : NewOps)
2343         MulOpLists[M.find(NewOp)->second].push_back(NewOp);
2344       // Re-generate the operands list.
2345       Ops.clear();
2346       if (AccumulatedConstant != 0)
2347         Ops.push_back(getConstant(AccumulatedConstant));
2348       for (auto &MulOp : MulOpLists)
2349         if (MulOp.first != 0)
2350           Ops.push_back(getMulExpr(
2351               getConstant(MulOp.first),
2352               getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1),
2353               SCEV::FlagAnyWrap, Depth + 1));
2354       if (Ops.empty())
2355         return getZero(Ty);
2356       if (Ops.size() == 1)
2357         return Ops[0];
2358       return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2359     }
2360   }
2361 
2362   // If we are adding something to a multiply expression, make sure the
2363   // something is not already an operand of the multiply.  If so, merge it into
2364   // the multiply.
2365   for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
2366     const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
2367     for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
2368       const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
2369       if (isa<SCEVConstant>(MulOpSCEV))
2370         continue;
2371       for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
2372         if (MulOpSCEV == Ops[AddOp]) {
2373           // Fold W + X + (X * Y * Z)  -->  W + (X * ((Y*Z)+1))
2374           const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
2375           if (Mul->getNumOperands() != 2) {
2376             // If the multiply has more than two operands, we must get the
2377             // Y*Z term.
2378             SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2379                                                 Mul->op_begin()+MulOp);
2380             MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2381             InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2382           }
2383           SmallVector<const SCEV *, 2> TwoOps = {getOne(Ty), InnerMul};
2384           const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2385           const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV,
2386                                             SCEV::FlagAnyWrap, Depth + 1);
2387           if (Ops.size() == 2) return OuterMul;
2388           if (AddOp < Idx) {
2389             Ops.erase(Ops.begin()+AddOp);
2390             Ops.erase(Ops.begin()+Idx-1);
2391           } else {
2392             Ops.erase(Ops.begin()+Idx);
2393             Ops.erase(Ops.begin()+AddOp-1);
2394           }
2395           Ops.push_back(OuterMul);
2396           return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2397         }
2398 
2399       // Check this multiply against other multiplies being added together.
2400       for (unsigned OtherMulIdx = Idx+1;
2401            OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
2402            ++OtherMulIdx) {
2403         const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
2404         // If MulOp occurs in OtherMul, we can fold the two multiplies
2405         // together.
2406         for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
2407              OMulOp != e; ++OMulOp)
2408           if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
2409             // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
2410             const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
2411             if (Mul->getNumOperands() != 2) {
2412               SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2413                                                   Mul->op_begin()+MulOp);
2414               MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2415               InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2416             }
2417             const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
2418             if (OtherMul->getNumOperands() != 2) {
2419               SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
2420                                                   OtherMul->op_begin()+OMulOp);
2421               MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end());
2422               InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2423             }
2424             SmallVector<const SCEV *, 2> TwoOps = {InnerMul1, InnerMul2};
2425             const SCEV *InnerMulSum =
2426                 getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2427             const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum,
2428                                               SCEV::FlagAnyWrap, Depth + 1);
2429             if (Ops.size() == 2) return OuterMul;
2430             Ops.erase(Ops.begin()+Idx);
2431             Ops.erase(Ops.begin()+OtherMulIdx-1);
2432             Ops.push_back(OuterMul);
2433             return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2434           }
2435       }
2436     }
2437   }
2438 
2439   // If there are any add recurrences in the operands list, see if any other
2440   // added values are loop invariant.  If so, we can fold them into the
2441   // recurrence.
2442   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2443     ++Idx;
2444 
2445   // Scan over all recurrences, trying to fold loop invariants into them.
2446   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2447     // Scan all of the other operands to this add and add them to the vector if
2448     // they are loop invariant w.r.t. the recurrence.
2449     SmallVector<const SCEV *, 8> LIOps;
2450     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2451     const Loop *AddRecLoop = AddRec->getLoop();
2452     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2453       if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
2454         LIOps.push_back(Ops[i]);
2455         Ops.erase(Ops.begin()+i);
2456         --i; --e;
2457       }
2458 
2459     // If we found some loop invariants, fold them into the recurrence.
2460     if (!LIOps.empty()) {
2461       //  NLI + LI + {Start,+,Step}  -->  NLI + {LI+Start,+,Step}
2462       LIOps.push_back(AddRec->getStart());
2463 
2464       SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
2465                                              AddRec->op_end());
2466       // This follows from the fact that the no-wrap flags on the outer add
2467       // expression are applicable on the 0th iteration, when the add recurrence
2468       // will be equal to its start value.
2469       AddRecOps[0] = getAddExpr(LIOps, Flags, Depth + 1);
2470 
2471       // Build the new addrec. Propagate the NUW and NSW flags if both the
2472       // outer add and the inner addrec are guaranteed to have no overflow.
2473       // Always propagate NW.
2474       Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
2475       const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
2476 
2477       // If all of the other operands were loop invariant, we are done.
2478       if (Ops.size() == 1) return NewRec;
2479 
2480       // Otherwise, add the folded AddRec by the non-invariant parts.
2481       for (unsigned i = 0;; ++i)
2482         if (Ops[i] == AddRec) {
2483           Ops[i] = NewRec;
2484           break;
2485         }
2486       return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2487     }
2488 
2489     // Okay, if there weren't any loop invariants to be folded, check to see if
2490     // there are multiple AddRec's with the same loop induction variable being
2491     // added together.  If so, we can fold them.
2492     for (unsigned OtherIdx = Idx+1;
2493          OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2494          ++OtherIdx) {
2495       // We expect the AddRecExpr's to be sorted in reverse dominance order,
2496       // so that the 1st found AddRecExpr is dominated by all others.
2497       assert(DT.dominates(
2498            cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),
2499            AddRec->getLoop()->getHeader()) &&
2500         "AddRecExprs are not sorted in reverse dominance order?");
2501       if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2502         // Other + {A,+,B}<L> + {C,+,D}<L>  -->  Other + {A+C,+,B+D}<L>
2503         SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
2504                                                AddRec->op_end());
2505         for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2506              ++OtherIdx) {
2507           const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2508           if (OtherAddRec->getLoop() == AddRecLoop) {
2509             for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2510                  i != e; ++i) {
2511               if (i >= AddRecOps.size()) {
2512                 AddRecOps.append(OtherAddRec->op_begin()+i,
2513                                  OtherAddRec->op_end());
2514                 break;
2515               }
2516               SmallVector<const SCEV *, 2> TwoOps = {
2517                   AddRecOps[i], OtherAddRec->getOperand(i)};
2518               AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2519             }
2520             Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2521           }
2522         }
2523         // Step size has changed, so we cannot guarantee no self-wraparound.
2524         Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
2525         return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2526       }
2527     }
2528 
2529     // Otherwise couldn't fold anything into this recurrence.  Move onto the
2530     // next one.
2531   }
2532 
2533   // Okay, it looks like we really DO need an add expr.  Check to see if we
2534   // already have one, otherwise create a new one.
2535   return getOrCreateAddExpr(Ops, Flags);
2536 }
2537 
2538 const SCEV *
getOrCreateAddExpr(ArrayRef<const SCEV * > Ops,SCEV::NoWrapFlags Flags)2539 ScalarEvolution::getOrCreateAddExpr(ArrayRef<const SCEV *> Ops,
2540                                     SCEV::NoWrapFlags Flags) {
2541   FoldingSetNodeID ID;
2542   ID.AddInteger(scAddExpr);
2543   for (const SCEV *Op : Ops)
2544     ID.AddPointer(Op);
2545   void *IP = nullptr;
2546   SCEVAddExpr *S =
2547       static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2548   if (!S) {
2549     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2550     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2551     S = new (SCEVAllocator)
2552         SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
2553     UniqueSCEVs.InsertNode(S, IP);
2554     addToLoopUseLists(S);
2555   }
2556   S->setNoWrapFlags(Flags);
2557   return S;
2558 }
2559 
2560 const SCEV *
getOrCreateAddRecExpr(ArrayRef<const SCEV * > Ops,const Loop * L,SCEV::NoWrapFlags Flags)2561 ScalarEvolution::getOrCreateAddRecExpr(ArrayRef<const SCEV *> Ops,
2562                                        const Loop *L, SCEV::NoWrapFlags Flags) {
2563   FoldingSetNodeID ID;
2564   ID.AddInteger(scAddRecExpr);
2565   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2566     ID.AddPointer(Ops[i]);
2567   ID.AddPointer(L);
2568   void *IP = nullptr;
2569   SCEVAddRecExpr *S =
2570       static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2571   if (!S) {
2572     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2573     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2574     S = new (SCEVAllocator)
2575         SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L);
2576     UniqueSCEVs.InsertNode(S, IP);
2577     addToLoopUseLists(S);
2578   }
2579   S->setNoWrapFlags(Flags);
2580   return S;
2581 }
2582 
2583 const SCEV *
getOrCreateMulExpr(ArrayRef<const SCEV * > Ops,SCEV::NoWrapFlags Flags)2584 ScalarEvolution::getOrCreateMulExpr(ArrayRef<const SCEV *> Ops,
2585                                     SCEV::NoWrapFlags Flags) {
2586   FoldingSetNodeID ID;
2587   ID.AddInteger(scMulExpr);
2588   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2589     ID.AddPointer(Ops[i]);
2590   void *IP = nullptr;
2591   SCEVMulExpr *S =
2592     static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2593   if (!S) {
2594     const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2595     std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2596     S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
2597                                         O, Ops.size());
2598     UniqueSCEVs.InsertNode(S, IP);
2599     addToLoopUseLists(S);
2600   }
2601   S->setNoWrapFlags(Flags);
2602   return S;
2603 }
2604 
umul_ov(uint64_t i,uint64_t j,bool & Overflow)2605 static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
2606   uint64_t k = i*j;
2607   if (j > 1 && k / j != i) Overflow = true;
2608   return k;
2609 }
2610 
2611 /// Compute the result of "n choose k", the binomial coefficient.  If an
2612 /// intermediate computation overflows, Overflow will be set and the return will
2613 /// be garbage. Overflow is not cleared on absence of overflow.
Choose(uint64_t n,uint64_t k,bool & Overflow)2614 static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
2615   // We use the multiplicative formula:
2616   //     n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
2617   // At each iteration, we take the n-th term of the numeral and divide by the
2618   // (k-n)th term of the denominator.  This division will always produce an
2619   // integral result, and helps reduce the chance of overflow in the
2620   // intermediate computations. However, we can still overflow even when the
2621   // final result would fit.
2622 
2623   if (n == 0 || n == k) return 1;
2624   if (k > n) return 0;
2625 
2626   if (k > n/2)
2627     k = n-k;
2628 
2629   uint64_t r = 1;
2630   for (uint64_t i = 1; i <= k; ++i) {
2631     r = umul_ov(r, n-(i-1), Overflow);
2632     r /= i;
2633   }
2634   return r;
2635 }
2636 
2637 /// Determine if any of the operands in this SCEV are a constant or if
2638 /// any of the add or multiply expressions in this SCEV contain a constant.
containsConstantInAddMulChain(const SCEV * StartExpr)2639 static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
2640   struct FindConstantInAddMulChain {
2641     bool FoundConstant = false;
2642 
2643     bool follow(const SCEV *S) {
2644       FoundConstant |= isa<SCEVConstant>(S);
2645       return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S);
2646     }
2647 
2648     bool isDone() const {
2649       return FoundConstant;
2650     }
2651   };
2652 
2653   FindConstantInAddMulChain F;
2654   SCEVTraversal<FindConstantInAddMulChain> ST(F);
2655   ST.visitAll(StartExpr);
2656   return F.FoundConstant;
2657 }
2658 
2659 /// Get a canonical multiply expression, or something simpler if possible.
getMulExpr(SmallVectorImpl<const SCEV * > & Ops,SCEV::NoWrapFlags Flags,unsigned Depth)2660 const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
2661                                         SCEV::NoWrapFlags Flags,
2662                                         unsigned Depth) {
2663   assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) &&
2664          "only nuw or nsw allowed");
2665   assert(!Ops.empty() && "Cannot get empty mul!");
2666   if (Ops.size() == 1) return Ops[0];
2667 #ifndef NDEBUG
2668   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2669   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2670     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2671            "SCEVMulExpr operand types don't match!");
2672 #endif
2673 
2674   // Sort by complexity, this groups all similar expression types together.
2675   GroupByComplexity(Ops, &LI, DT);
2676 
2677   Flags = StrengthenNoWrapFlags(this, scMulExpr, Ops, Flags);
2678 
2679   // Limit recursion calls depth, but fold all-constant expressions.
2680   // `Ops` is sorted, so it's enough to check just last one.
2681   if ((Depth > MaxArithDepth || hasHugeExpression(Ops)) &&
2682       !isa<SCEVConstant>(Ops.back()))
2683     return getOrCreateMulExpr(Ops, Flags);
2684 
2685   if (SCEV *S = std::get<0>(findExistingSCEVInCache(scMulExpr, Ops))) {
2686     static_cast<SCEVMulExpr *>(S)->setNoWrapFlags(Flags);
2687     return S;
2688   }
2689 
2690   // If there are any constants, fold them together.
2691   unsigned Idx = 0;
2692   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2693 
2694     if (Ops.size() == 2)
2695       // C1*(C2+V) -> C1*C2 + C1*V
2696       if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
2697         // If any of Add's ops are Adds or Muls with a constant, apply this
2698         // transformation as well.
2699         //
2700         // TODO: There are some cases where this transformation is not
2701         // profitable; for example, Add = (C0 + X) * Y + Z.  Maybe the scope of
2702         // this transformation should be narrowed down.
2703         if (Add->getNumOperands() == 2 && containsConstantInAddMulChain(Add))
2704           return getAddExpr(getMulExpr(LHSC, Add->getOperand(0),
2705                                        SCEV::FlagAnyWrap, Depth + 1),
2706                             getMulExpr(LHSC, Add->getOperand(1),
2707                                        SCEV::FlagAnyWrap, Depth + 1),
2708                             SCEV::FlagAnyWrap, Depth + 1);
2709 
2710     ++Idx;
2711     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2712       // We found two constants, fold them together!
2713       ConstantInt *Fold =
2714           ConstantInt::get(getContext(), LHSC->getAPInt() * RHSC->getAPInt());
2715       Ops[0] = getConstant(Fold);
2716       Ops.erase(Ops.begin()+1);  // Erase the folded element
2717       if (Ops.size() == 1) return Ops[0];
2718       LHSC = cast<SCEVConstant>(Ops[0]);
2719     }
2720 
2721     // If we are left with a constant one being multiplied, strip it off.
2722     if (cast<SCEVConstant>(Ops[0])->getValue()->isOne()) {
2723       Ops.erase(Ops.begin());
2724       --Idx;
2725     } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
2726       // If we have a multiply of zero, it will always be zero.
2727       return Ops[0];
2728     } else if (Ops[0]->isAllOnesValue()) {
2729       // If we have a mul by -1 of an add, try distributing the -1 among the
2730       // add operands.
2731       if (Ops.size() == 2) {
2732         if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
2733           SmallVector<const SCEV *, 4> NewOps;
2734           bool AnyFolded = false;
2735           for (const SCEV *AddOp : Add->operands()) {
2736             const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap,
2737                                          Depth + 1);
2738             if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
2739             NewOps.push_back(Mul);
2740           }
2741           if (AnyFolded)
2742             return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1);
2743         } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
2744           // Negation preserves a recurrence's no self-wrap property.
2745           SmallVector<const SCEV *, 4> Operands;
2746           for (const SCEV *AddRecOp : AddRec->operands())
2747             Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap,
2748                                           Depth + 1));
2749 
2750           return getAddRecExpr(Operands, AddRec->getLoop(),
2751                                AddRec->getNoWrapFlags(SCEV::FlagNW));
2752         }
2753       }
2754     }
2755 
2756     if (Ops.size() == 1)
2757       return Ops[0];
2758   }
2759 
2760   // Skip over the add expression until we get to a multiply.
2761   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2762     ++Idx;
2763 
2764   // If there are mul operands inline them all into this expression.
2765   if (Idx < Ops.size()) {
2766     bool DeletedMul = false;
2767     while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2768       if (Ops.size() > MulOpsInlineThreshold)
2769         break;
2770       // If we have an mul, expand the mul operands onto the end of the
2771       // operands list.
2772       Ops.erase(Ops.begin()+Idx);
2773       Ops.append(Mul->op_begin(), Mul->op_end());
2774       DeletedMul = true;
2775     }
2776 
2777     // If we deleted at least one mul, we added operands to the end of the
2778     // list, and they are not necessarily sorted.  Recurse to resort and
2779     // resimplify any operands we just acquired.
2780     if (DeletedMul)
2781       return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2782   }
2783 
2784   // If there are any add recurrences in the operands list, see if any other
2785   // added values are loop invariant.  If so, we can fold them into the
2786   // recurrence.
2787   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2788     ++Idx;
2789 
2790   // Scan over all recurrences, trying to fold loop invariants into them.
2791   for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2792     // Scan all of the other operands to this mul and add them to the vector
2793     // if they are loop invariant w.r.t. the recurrence.
2794     SmallVector<const SCEV *, 8> LIOps;
2795     const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2796     const Loop *AddRecLoop = AddRec->getLoop();
2797     for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2798       if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
2799         LIOps.push_back(Ops[i]);
2800         Ops.erase(Ops.begin()+i);
2801         --i; --e;
2802       }
2803 
2804     // If we found some loop invariants, fold them into the recurrence.
2805     if (!LIOps.empty()) {
2806       //  NLI * LI * {Start,+,Step}  -->  NLI * {LI*Start,+,LI*Step}
2807       SmallVector<const SCEV *, 4> NewOps;
2808       NewOps.reserve(AddRec->getNumOperands());
2809       const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1);
2810       for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
2811         NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i),
2812                                     SCEV::FlagAnyWrap, Depth + 1));
2813 
2814       // Build the new addrec. Propagate the NUW and NSW flags if both the
2815       // outer mul and the inner addrec are guaranteed to have no overflow.
2816       //
2817       // No self-wrap cannot be guaranteed after changing the step size, but
2818       // will be inferred if either NUW or NSW is true.
2819       Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW));
2820       const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags);
2821 
2822       // If all of the other operands were loop invariant, we are done.
2823       if (Ops.size() == 1) return NewRec;
2824 
2825       // Otherwise, multiply the folded AddRec by the non-invariant parts.
2826       for (unsigned i = 0;; ++i)
2827         if (Ops[i] == AddRec) {
2828           Ops[i] = NewRec;
2829           break;
2830         }
2831       return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2832     }
2833 
2834     // Okay, if there weren't any loop invariants to be folded, check to see
2835     // if there are multiple AddRec's with the same loop induction variable
2836     // being multiplied together.  If so, we can fold them.
2837 
2838     // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
2839     // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
2840     //       choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
2841     //   ]]],+,...up to x=2n}.
2842     // Note that the arguments to choose() are always integers with values
2843     // known at compile time, never SCEV objects.
2844     //
2845     // The implementation avoids pointless extra computations when the two
2846     // addrec's are of different length (mathematically, it's equivalent to
2847     // an infinite stream of zeros on the right).
2848     bool OpsModified = false;
2849     for (unsigned OtherIdx = Idx+1;
2850          OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2851          ++OtherIdx) {
2852       const SCEVAddRecExpr *OtherAddRec =
2853         dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2854       if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop)
2855         continue;
2856 
2857       // Limit max number of arguments to avoid creation of unreasonably big
2858       // SCEVAddRecs with very complex operands.
2859       if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
2860           MaxAddRecSize || hasHugeExpression({AddRec, OtherAddRec}))
2861         continue;
2862 
2863       bool Overflow = false;
2864       Type *Ty = AddRec->getType();
2865       bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
2866       SmallVector<const SCEV*, 7> AddRecOps;
2867       for (int x = 0, xe = AddRec->getNumOperands() +
2868              OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
2869         SmallVector <const SCEV *, 7> SumOps;
2870         for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
2871           uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
2872           for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
2873                  ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
2874                z < ze && !Overflow; ++z) {
2875             uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
2876             uint64_t Coeff;
2877             if (LargerThan64Bits)
2878               Coeff = umul_ov(Coeff1, Coeff2, Overflow);
2879             else
2880               Coeff = Coeff1*Coeff2;
2881             const SCEV *CoeffTerm = getConstant(Ty, Coeff);
2882             const SCEV *Term1 = AddRec->getOperand(y-z);
2883             const SCEV *Term2 = OtherAddRec->getOperand(z);
2884             SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2,
2885                                         SCEV::FlagAnyWrap, Depth + 1));
2886           }
2887         }
2888         if (SumOps.empty())
2889           SumOps.push_back(getZero(Ty));
2890         AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1));
2891       }
2892       if (!Overflow) {
2893         const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRecLoop,
2894                                               SCEV::FlagAnyWrap);
2895         if (Ops.size() == 2) return NewAddRec;
2896         Ops[Idx] = NewAddRec;
2897         Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2898         OpsModified = true;
2899         AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
2900         if (!AddRec)
2901           break;
2902       }
2903     }
2904     if (OpsModified)
2905       return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2906 
2907     // Otherwise couldn't fold anything into this recurrence.  Move onto the
2908     // next one.
2909   }
2910 
2911   // Okay, it looks like we really DO need an mul expr.  Check to see if we
2912   // already have one, otherwise create a new one.
2913   return getOrCreateMulExpr(Ops, Flags);
2914 }
2915 
2916 /// Represents an unsigned remainder expression based on unsigned division.
getURemExpr(const SCEV * LHS,const SCEV * RHS)2917 const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS,
2918                                          const SCEV *RHS) {
2919   assert(getEffectiveSCEVType(LHS->getType()) ==
2920          getEffectiveSCEVType(RHS->getType()) &&
2921          "SCEVURemExpr operand types don't match!");
2922 
2923   // Short-circuit easy cases
2924   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
2925     // If constant is one, the result is trivial
2926     if (RHSC->getValue()->isOne())
2927       return getZero(LHS->getType()); // X urem 1 --> 0
2928 
2929     // If constant is a power of two, fold into a zext(trunc(LHS)).
2930     if (RHSC->getAPInt().isPowerOf2()) {
2931       Type *FullTy = LHS->getType();
2932       Type *TruncTy =
2933           IntegerType::get(getContext(), RHSC->getAPInt().logBase2());
2934       return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy);
2935     }
2936   }
2937 
2938   // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
2939   const SCEV *UDiv = getUDivExpr(LHS, RHS);
2940   const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW);
2941   return getMinusSCEV(LHS, Mult, SCEV::FlagNUW);
2942 }
2943 
2944 /// Get a canonical unsigned division expression, or something simpler if
2945 /// possible.
getUDivExpr(const SCEV * LHS,const SCEV * RHS)2946 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
2947                                          const SCEV *RHS) {
2948   assert(getEffectiveSCEVType(LHS->getType()) ==
2949          getEffectiveSCEVType(RHS->getType()) &&
2950          "SCEVUDivExpr operand types don't match!");
2951 
2952   FoldingSetNodeID ID;
2953   ID.AddInteger(scUDivExpr);
2954   ID.AddPointer(LHS);
2955   ID.AddPointer(RHS);
2956   void *IP = nullptr;
2957   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
2958     return S;
2959 
2960   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
2961     if (RHSC->getValue()->isOne())
2962       return LHS;                               // X udiv 1 --> x
2963     // If the denominator is zero, the result of the udiv is undefined. Don't
2964     // try to analyze it, because the resolution chosen here may differ from
2965     // the resolution chosen in other parts of the compiler.
2966     if (!RHSC->getValue()->isZero()) {
2967       // Determine if the division can be folded into the operands of
2968       // its operands.
2969       // TODO: Generalize this to non-constants by using known-bits information.
2970       Type *Ty = LHS->getType();
2971       unsigned LZ = RHSC->getAPInt().countLeadingZeros();
2972       unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
2973       // For non-power-of-two values, effectively round the value up to the
2974       // nearest power of two.
2975       if (!RHSC->getAPInt().isPowerOf2())
2976         ++MaxShiftAmt;
2977       IntegerType *ExtTy =
2978         IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
2979       if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
2980         if (const SCEVConstant *Step =
2981             dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
2982           // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
2983           const APInt &StepInt = Step->getAPInt();
2984           const APInt &DivInt = RHSC->getAPInt();
2985           if (!StepInt.urem(DivInt) &&
2986               getZeroExtendExpr(AR, ExtTy) ==
2987               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
2988                             getZeroExtendExpr(Step, ExtTy),
2989                             AR->getLoop(), SCEV::FlagAnyWrap)) {
2990             SmallVector<const SCEV *, 4> Operands;
2991             for (const SCEV *Op : AR->operands())
2992               Operands.push_back(getUDivExpr(Op, RHS));
2993             return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW);
2994           }
2995           /// Get a canonical UDivExpr for a recurrence.
2996           /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
2997           // We can currently only fold X%N if X is constant.
2998           const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
2999           if (StartC && !DivInt.urem(StepInt) &&
3000               getZeroExtendExpr(AR, ExtTy) ==
3001               getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3002                             getZeroExtendExpr(Step, ExtTy),
3003                             AR->getLoop(), SCEV::FlagAnyWrap)) {
3004             const APInt &StartInt = StartC->getAPInt();
3005             const APInt &StartRem = StartInt.urem(StepInt);
3006             if (StartRem != 0) {
3007               const SCEV *NewLHS =
3008                   getAddRecExpr(getConstant(StartInt - StartRem), Step,
3009                                 AR->getLoop(), SCEV::FlagNW);
3010               if (LHS != NewLHS) {
3011                 LHS = NewLHS;
3012 
3013                 // Reset the ID to include the new LHS, and check if it is
3014                 // already cached.
3015                 ID.clear();
3016                 ID.AddInteger(scUDivExpr);
3017                 ID.AddPointer(LHS);
3018                 ID.AddPointer(RHS);
3019                 IP = nullptr;
3020                 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
3021                   return S;
3022               }
3023             }
3024           }
3025         }
3026       // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
3027       if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
3028         SmallVector<const SCEV *, 4> Operands;
3029         for (const SCEV *Op : M->operands())
3030           Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3031         if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
3032           // Find an operand that's safely divisible.
3033           for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
3034             const SCEV *Op = M->getOperand(i);
3035             const SCEV *Div = getUDivExpr(Op, RHSC);
3036             if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
3037               Operands = SmallVector<const SCEV *, 4>(M->op_begin(),
3038                                                       M->op_end());
3039               Operands[i] = Div;
3040               return getMulExpr(Operands);
3041             }
3042           }
3043       }
3044 
3045       // (A/B)/C --> A/(B*C) if safe and B*C can be folded.
3046       if (const SCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(LHS)) {
3047         if (auto *DivisorConstant =
3048                 dyn_cast<SCEVConstant>(OtherDiv->getRHS())) {
3049           bool Overflow = false;
3050           APInt NewRHS =
3051               DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
3052           if (Overflow) {
3053             return getConstant(RHSC->getType(), 0, false);
3054           }
3055           return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS));
3056         }
3057       }
3058 
3059       // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
3060       if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
3061         SmallVector<const SCEV *, 4> Operands;
3062         for (const SCEV *Op : A->operands())
3063           Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3064         if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
3065           Operands.clear();
3066           for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
3067             const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
3068             if (isa<SCEVUDivExpr>(Op) ||
3069                 getMulExpr(Op, RHS) != A->getOperand(i))
3070               break;
3071             Operands.push_back(Op);
3072           }
3073           if (Operands.size() == A->getNumOperands())
3074             return getAddExpr(Operands);
3075         }
3076       }
3077 
3078       // Fold if both operands are constant.
3079       if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
3080         Constant *LHSCV = LHSC->getValue();
3081         Constant *RHSCV = RHSC->getValue();
3082         return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
3083                                                                    RHSCV)));
3084       }
3085     }
3086   }
3087 
3088   // The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs
3089   // changes). Make sure we get a new one.
3090   IP = nullptr;
3091   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
3092   SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
3093                                              LHS, RHS);
3094   UniqueSCEVs.InsertNode(S, IP);
3095   addToLoopUseLists(S);
3096   return S;
3097 }
3098 
gcd(const SCEVConstant * C1,const SCEVConstant * C2)3099 static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) {
3100   APInt A = C1->getAPInt().abs();
3101   APInt B = C2->getAPInt().abs();
3102   uint32_t ABW = A.getBitWidth();
3103   uint32_t BBW = B.getBitWidth();
3104 
3105   if (ABW > BBW)
3106     B = B.zext(ABW);
3107   else if (ABW < BBW)
3108     A = A.zext(BBW);
3109 
3110   return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B));
3111 }
3112 
3113 /// Get a canonical unsigned division expression, or something simpler if
3114 /// possible. There is no representation for an exact udiv in SCEV IR, but we
3115 /// can attempt to remove factors from the LHS and RHS.  We can't do this when
3116 /// it's not exact because the udiv may be clearing bits.
getUDivExactExpr(const SCEV * LHS,const SCEV * RHS)3117 const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS,
3118                                               const SCEV *RHS) {
3119   // TODO: we could try to find factors in all sorts of things, but for now we
3120   // just deal with u/exact (multiply, constant). See SCEVDivision towards the
3121   // end of this file for inspiration.
3122 
3123   const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS);
3124   if (!Mul || !Mul->hasNoUnsignedWrap())
3125     return getUDivExpr(LHS, RHS);
3126 
3127   if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
3128     // If the mulexpr multiplies by a constant, then that constant must be the
3129     // first element of the mulexpr.
3130     if (const auto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
3131       if (LHSCst == RHSCst) {
3132         SmallVector<const SCEV *, 2> Operands;
3133         Operands.append(Mul->op_begin() + 1, Mul->op_end());
3134         return getMulExpr(Operands);
3135       }
3136 
3137       // We can't just assume that LHSCst divides RHSCst cleanly, it could be
3138       // that there's a factor provided by one of the other terms. We need to
3139       // check.
3140       APInt Factor = gcd(LHSCst, RHSCst);
3141       if (!Factor.isIntN(1)) {
3142         LHSCst =
3143             cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor)));
3144         RHSCst =
3145             cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor)));
3146         SmallVector<const SCEV *, 2> Operands;
3147         Operands.push_back(LHSCst);
3148         Operands.append(Mul->op_begin() + 1, Mul->op_end());
3149         LHS = getMulExpr(Operands);
3150         RHS = RHSCst;
3151         Mul = dyn_cast<SCEVMulExpr>(LHS);
3152         if (!Mul)
3153           return getUDivExactExpr(LHS, RHS);
3154       }
3155     }
3156   }
3157 
3158   for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
3159     if (Mul->getOperand(i) == RHS) {
3160       SmallVector<const SCEV *, 2> Operands;
3161       Operands.append(Mul->op_begin(), Mul->op_begin() + i);
3162       Operands.append(Mul->op_begin() + i + 1, Mul->op_end());
3163       return getMulExpr(Operands);
3164     }
3165   }
3166 
3167   return getUDivExpr(LHS, RHS);
3168 }
3169 
3170 /// Get an add recurrence expression for the specified loop.  Simplify the
3171 /// expression as much as possible.
getAddRecExpr(const SCEV * Start,const SCEV * Step,const Loop * L,SCEV::NoWrapFlags Flags)3172 const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step,
3173                                            const Loop *L,
3174                                            SCEV::NoWrapFlags Flags) {
3175   SmallVector<const SCEV *, 4> Operands;
3176   Operands.push_back(Start);
3177   if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
3178     if (StepChrec->getLoop() == L) {
3179       Operands.append(StepChrec->op_begin(), StepChrec->op_end());
3180       return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW));
3181     }
3182 
3183   Operands.push_back(Step);
3184   return getAddRecExpr(Operands, L, Flags);
3185 }
3186 
3187 /// Get an add recurrence expression for the specified loop.  Simplify the
3188 /// expression as much as possible.
3189 const SCEV *
getAddRecExpr(SmallVectorImpl<const SCEV * > & Operands,const Loop * L,SCEV::NoWrapFlags Flags)3190 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
3191                                const Loop *L, SCEV::NoWrapFlags Flags) {
3192   if (Operands.size() == 1) return Operands[0];
3193 #ifndef NDEBUG
3194   Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
3195   for (unsigned i = 1, e = Operands.size(); i != e; ++i)
3196     assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy &&
3197            "SCEVAddRecExpr operand types don't match!");
3198   for (unsigned i = 0, e = Operands.size(); i != e; ++i)
3199     assert(isLoopInvariant(Operands[i], L) &&
3200            "SCEVAddRecExpr operand is not loop-invariant!");
3201 #endif
3202 
3203   if (Operands.back()->isZero()) {
3204     Operands.pop_back();
3205     return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0}  -->  X
3206   }
3207 
3208   // It's tempting to want to call getConstantMaxBackedgeTakenCount count here and
3209   // use that information to infer NUW and NSW flags. However, computing a
3210   // BE count requires calling getAddRecExpr, so we may not yet have a
3211   // meaningful BE count at this point (and if we don't, we'd be stuck
3212   // with a SCEVCouldNotCompute as the cached BE count).
3213 
3214   Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
3215 
3216   // Canonicalize nested AddRecs in by nesting them in order of loop depth.
3217   if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
3218     const Loop *NestedLoop = NestedAR->getLoop();
3219     if (L->contains(NestedLoop)
3220             ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
3221             : (!NestedLoop->contains(L) &&
3222                DT.dominates(L->getHeader(), NestedLoop->getHeader()))) {
3223       SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
3224                                                   NestedAR->op_end());
3225       Operands[0] = NestedAR->getStart();
3226       // AddRecs require their operands be loop-invariant with respect to their
3227       // loops. Don't perform this transformation if it would break this
3228       // requirement.
3229       bool AllInvariant = all_of(
3230           Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); });
3231 
3232       if (AllInvariant) {
3233         // Create a recurrence for the outer loop with the same step size.
3234         //
3235         // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
3236         // inner recurrence has the same property.
3237         SCEV::NoWrapFlags OuterFlags =
3238           maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
3239 
3240         NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
3241         AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) {
3242           return isLoopInvariant(Op, NestedLoop);
3243         });
3244 
3245         if (AllInvariant) {
3246           // Ok, both add recurrences are valid after the transformation.
3247           //
3248           // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
3249           // the outer recurrence has the same property.
3250           SCEV::NoWrapFlags InnerFlags =
3251             maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
3252           return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
3253         }
3254       }
3255       // Reset Operands to its original state.
3256       Operands[0] = NestedAR;
3257     }
3258   }
3259 
3260   // Okay, it looks like we really DO need an addrec expr.  Check to see if we
3261   // already have one, otherwise create a new one.
3262   return getOrCreateAddRecExpr(Operands, L, Flags);
3263 }
3264 
3265 const SCEV *
getGEPExpr(GEPOperator * GEP,const SmallVectorImpl<const SCEV * > & IndexExprs)3266 ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3267                             const SmallVectorImpl<const SCEV *> &IndexExprs) {
3268   const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
3269   // getSCEV(Base)->getType() has the same address space as Base->getType()
3270   // because SCEV::getType() preserves the address space.
3271   Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType());
3272   // FIXME(PR23527): Don't blindly transfer the inbounds flag from the GEP
3273   // instruction to its SCEV, because the Instruction may be guarded by control
3274   // flow and the no-overflow bits may not be valid for the expression in any
3275   // context. This can be fixed similarly to how these flags are handled for
3276   // adds.
3277   SCEV::NoWrapFlags Wrap = GEP->isInBounds() ? SCEV::FlagNSW
3278                                              : SCEV::FlagAnyWrap;
3279 
3280   const SCEV *TotalOffset = getZero(IntIdxTy);
3281   Type *CurTy = GEP->getType();
3282   bool FirstIter = true;
3283   for (const SCEV *IndexExpr : IndexExprs) {
3284     // Compute the (potentially symbolic) offset in bytes for this index.
3285     if (StructType *STy = dyn_cast<StructType>(CurTy)) {
3286       // For a struct, add the member offset.
3287       ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
3288       unsigned FieldNo = Index->getZExtValue();
3289       const SCEV *FieldOffset = getOffsetOfExpr(IntIdxTy, STy, FieldNo);
3290 
3291       // Add the field offset to the running total offset.
3292       TotalOffset = getAddExpr(TotalOffset, FieldOffset);
3293 
3294       // Update CurTy to the type of the field at Index.
3295       CurTy = STy->getTypeAtIndex(Index);
3296     } else {
3297       // Update CurTy to its element type.
3298       if (FirstIter) {
3299         assert(isa<PointerType>(CurTy) &&
3300                "The first index of a GEP indexes a pointer");
3301         CurTy = GEP->getSourceElementType();
3302         FirstIter = false;
3303       } else {
3304         CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0);
3305       }
3306       // For an array, add the element offset, explicitly scaled.
3307       const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy);
3308       // Getelementptr indices are signed.
3309       IndexExpr = getTruncateOrSignExtend(IndexExpr, IntIdxTy);
3310 
3311       // Multiply the index by the element size to compute the element offset.
3312       const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, Wrap);
3313 
3314       // Add the element offset to the running total offset.
3315       TotalOffset = getAddExpr(TotalOffset, LocalOffset);
3316     }
3317   }
3318 
3319   // Add the total offset from all the GEP indices to the base.
3320   auto *GEPExpr = getAddExpr(BaseExpr, TotalOffset, Wrap);
3321   assert(BaseExpr->getType() == GEPExpr->getType() &&
3322          "GEP should not change type mid-flight.");
3323   return GEPExpr;
3324 }
3325 
3326 std::tuple<SCEV *, FoldingSetNodeID, void *>
findExistingSCEVInCache(int SCEVType,ArrayRef<const SCEV * > Ops)3327 ScalarEvolution::findExistingSCEVInCache(int SCEVType,
3328                                          ArrayRef<const SCEV *> Ops) {
3329   FoldingSetNodeID ID;
3330   void *IP = nullptr;
3331   ID.AddInteger(SCEVType);
3332   for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3333     ID.AddPointer(Ops[i]);
3334   return std::tuple<SCEV *, FoldingSetNodeID, void *>(
3335       UniqueSCEVs.FindNodeOrInsertPos(ID, IP), std::move(ID), IP);
3336 }
3337 
getMinMaxExpr(unsigned Kind,SmallVectorImpl<const SCEV * > & Ops)3338 const SCEV *ScalarEvolution::getMinMaxExpr(unsigned Kind,
3339                                            SmallVectorImpl<const SCEV *> &Ops) {
3340   assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!");
3341   if (Ops.size() == 1) return Ops[0];
3342 #ifndef NDEBUG
3343   Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
3344   for (unsigned i = 1, e = Ops.size(); i != e; ++i)
3345     assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
3346            "Operand types don't match!");
3347 #endif
3348 
3349   bool IsSigned = Kind == scSMaxExpr || Kind == scSMinExpr;
3350   bool IsMax = Kind == scSMaxExpr || Kind == scUMaxExpr;
3351 
3352   // Sort by complexity, this groups all similar expression types together.
3353   GroupByComplexity(Ops, &LI, DT);
3354 
3355   // Check if we have created the same expression before.
3356   if (const SCEV *S = std::get<0>(findExistingSCEVInCache(Kind, Ops))) {
3357     return S;
3358   }
3359 
3360   // If there are any constants, fold them together.
3361   unsigned Idx = 0;
3362   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3363     ++Idx;
3364     assert(Idx < Ops.size());
3365     auto FoldOp = [&](const APInt &LHS, const APInt &RHS) {
3366       if (Kind == scSMaxExpr)
3367         return APIntOps::smax(LHS, RHS);
3368       else if (Kind == scSMinExpr)
3369         return APIntOps::smin(LHS, RHS);
3370       else if (Kind == scUMaxExpr)
3371         return APIntOps::umax(LHS, RHS);
3372       else if (Kind == scUMinExpr)
3373         return APIntOps::umin(LHS, RHS);
3374       llvm_unreachable("Unknown SCEV min/max opcode");
3375     };
3376 
3377     while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3378       // We found two constants, fold them together!
3379       ConstantInt *Fold = ConstantInt::get(
3380           getContext(), FoldOp(LHSC->getAPInt(), RHSC->getAPInt()));
3381       Ops[0] = getConstant(Fold);
3382       Ops.erase(Ops.begin()+1);  // Erase the folded element
3383       if (Ops.size() == 1) return Ops[0];
3384       LHSC = cast<SCEVConstant>(Ops[0]);
3385     }
3386 
3387     bool IsMinV = LHSC->getValue()->isMinValue(IsSigned);
3388     bool IsMaxV = LHSC->getValue()->isMaxValue(IsSigned);
3389 
3390     if (IsMax ? IsMinV : IsMaxV) {
3391       // If we are left with a constant minimum(/maximum)-int, strip it off.
3392       Ops.erase(Ops.begin());
3393       --Idx;
3394     } else if (IsMax ? IsMaxV : IsMinV) {
3395       // If we have a max(/min) with a constant maximum(/minimum)-int,
3396       // it will always be the extremum.
3397       return LHSC;
3398     }
3399 
3400     if (Ops.size() == 1) return Ops[0];
3401   }
3402 
3403   // Find the first operation of the same kind
3404   while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind)
3405     ++Idx;
3406 
3407   // Check to see if one of the operands is of the same kind. If so, expand its
3408   // operands onto our operand list, and recurse to simplify.
3409   if (Idx < Ops.size()) {
3410     bool DeletedAny = false;
3411     while (Ops[Idx]->getSCEVType() == Kind) {
3412       const SCEVMinMaxExpr *SMME = cast<SCEVMinMaxExpr>(Ops[Idx]);
3413       Ops.erase(Ops.begin()+Idx);
3414       Ops.append(SMME->op_begin(), SMME->op_end());
3415       DeletedAny = true;
3416     }
3417 
3418     if (DeletedAny)
3419       return getMinMaxExpr(Kind, Ops);
3420   }
3421 
3422   // Okay, check to see if the same value occurs in the operand list twice.  If
3423   // so, delete one.  Since we sorted the list, these values are required to
3424   // be adjacent.
3425   llvm::CmpInst::Predicate GEPred =
3426       IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
3427   llvm::CmpInst::Predicate LEPred =
3428       IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
3429   llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred;
3430   llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred;
3431   for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) {
3432     if (Ops[i] == Ops[i + 1] ||
3433         isKnownViaNonRecursiveReasoning(FirstPred, Ops[i], Ops[i + 1])) {
3434       //  X op Y op Y  -->  X op Y
3435       //  X op Y       -->  X, if we know X, Y are ordered appropriately
3436       Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2);
3437       --i;
3438       --e;
3439     } else if (isKnownViaNonRecursiveReasoning(SecondPred, Ops[i],
3440                                                Ops[i + 1])) {
3441       //  X op Y       -->  Y, if we know X, Y are ordered appropriately
3442       Ops.erase(Ops.begin() + i, Ops.begin() + i + 1);
3443       --i;
3444       --e;
3445     }
3446   }
3447 
3448   if (Ops.size() == 1) return Ops[0];
3449 
3450   assert(!Ops.empty() && "Reduced smax down to nothing!");
3451 
3452   // Okay, it looks like we really DO need an expr.  Check to see if we
3453   // already have one, otherwise create a new one.
3454   const SCEV *ExistingSCEV;
3455   FoldingSetNodeID ID;
3456   void *IP;
3457   std::tie(ExistingSCEV, ID, IP) = findExistingSCEVInCache(Kind, Ops);
3458   if (ExistingSCEV)
3459     return ExistingSCEV;
3460   const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3461   std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3462   SCEV *S = new (SCEVAllocator) SCEVMinMaxExpr(
3463       ID.Intern(SCEVAllocator), static_cast<SCEVTypes>(Kind), O, Ops.size());
3464 
3465   UniqueSCEVs.InsertNode(S, IP);
3466   addToLoopUseLists(S);
3467   return S;
3468 }
3469 
getSMaxExpr(const SCEV * LHS,const SCEV * RHS)3470 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS, const SCEV *RHS) {
3471   SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
3472   return getSMaxExpr(Ops);
3473 }
3474 
getSMaxExpr(SmallVectorImpl<const SCEV * > & Ops)3475 const SCEV *ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
3476   return getMinMaxExpr(scSMaxExpr, Ops);
3477 }
3478 
getUMaxExpr(const SCEV * LHS,const SCEV * RHS)3479 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS, const SCEV *RHS) {
3480   SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
3481   return getUMaxExpr(Ops);
3482 }
3483 
getUMaxExpr(SmallVectorImpl<const SCEV * > & Ops)3484 const SCEV *ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
3485   return getMinMaxExpr(scUMaxExpr, Ops);
3486 }
3487 
getSMinExpr(const SCEV * LHS,const SCEV * RHS)3488 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
3489                                          const SCEV *RHS) {
3490   SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
3491   return getSMinExpr(Ops);
3492 }
3493 
getSMinExpr(SmallVectorImpl<const SCEV * > & Ops)3494 const SCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl<const SCEV *> &Ops) {
3495   return getMinMaxExpr(scSMinExpr, Ops);
3496 }
3497 
getUMinExpr(const SCEV * LHS,const SCEV * RHS)3498 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
3499                                          const SCEV *RHS) {
3500   SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
3501   return getUMinExpr(Ops);
3502 }
3503 
getUMinExpr(SmallVectorImpl<const SCEV * > & Ops)3504 const SCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl<const SCEV *> &Ops) {
3505   return getMinMaxExpr(scUMinExpr, Ops);
3506 }
3507 
getSizeOfExpr(Type * IntTy,Type * AllocTy)3508 const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
3509   // We can bypass creating a target-independent
3510   // constant expression and then folding it back into a ConstantInt.
3511   // This is just a compile-time optimization.
3512   if (isa<ScalableVectorType>(AllocTy)) {
3513     Constant *NullPtr = Constant::getNullValue(AllocTy->getPointerTo());
3514     Constant *One = ConstantInt::get(IntTy, 1);
3515     Constant *GEP = ConstantExpr::getGetElementPtr(AllocTy, NullPtr, One);
3516     return getSCEV(ConstantExpr::getPtrToInt(GEP, IntTy));
3517   }
3518   return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy));
3519 }
3520 
getOffsetOfExpr(Type * IntTy,StructType * STy,unsigned FieldNo)3521 const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
3522                                              StructType *STy,
3523                                              unsigned FieldNo) {
3524   // We can bypass creating a target-independent
3525   // constant expression and then folding it back into a ConstantInt.
3526   // This is just a compile-time optimization.
3527   return getConstant(
3528       IntTy, getDataLayout().getStructLayout(STy)->getElementOffset(FieldNo));
3529 }
3530 
getUnknown(Value * V)3531 const SCEV *ScalarEvolution::getUnknown(Value *V) {
3532   // Don't attempt to do anything other than create a SCEVUnknown object
3533   // here.  createSCEV only calls getUnknown after checking for all other
3534   // interesting possibilities, and any other code that calls getUnknown
3535   // is doing so in order to hide a value from SCEV canonicalization.
3536 
3537   FoldingSetNodeID ID;
3538   ID.AddInteger(scUnknown);
3539   ID.AddPointer(V);
3540   void *IP = nullptr;
3541   if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
3542     assert(cast<SCEVUnknown>(S)->getValue() == V &&
3543            "Stale SCEVUnknown in uniquing map!");
3544     return S;
3545   }
3546   SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
3547                                             FirstUnknown);
3548   FirstUnknown = cast<SCEVUnknown>(S);
3549   UniqueSCEVs.InsertNode(S, IP);
3550   return S;
3551 }
3552 
3553 //===----------------------------------------------------------------------===//
3554 //            Basic SCEV Analysis and PHI Idiom Recognition Code
3555 //
3556 
3557 /// Test if values of the given type are analyzable within the SCEV
3558 /// framework. This primarily includes integer types, and it can optionally
3559 /// include pointer types if the ScalarEvolution class has access to
3560 /// target-specific information.
isSCEVable(Type * Ty) const3561 bool ScalarEvolution::isSCEVable(Type *Ty) const {
3562   // Integers and pointers are always SCEVable.
3563   return Ty->isIntOrPtrTy();
3564 }
3565 
3566 /// Return the size in bits of the specified type, for which isSCEVable must
3567 /// return true.
getTypeSizeInBits(Type * Ty) const3568 uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
3569   assert(isSCEVable(Ty) && "Type is not SCEVable!");
3570   if (Ty->isPointerTy())
3571     return getDataLayout().getIndexTypeSizeInBits(Ty);
3572   return getDataLayout().getTypeSizeInBits(Ty);
3573 }
3574 
3575 /// Return a type with the same bitwidth as the given type and which represents
3576 /// how SCEV will treat the given type, for which isSCEVable must return
3577 /// true. For pointer types, this is the pointer index sized integer type.
getEffectiveSCEVType(Type * Ty) const3578 Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
3579   assert(isSCEVable(Ty) && "Type is not SCEVable!");
3580 
3581   if (Ty->isIntegerTy())
3582     return Ty;
3583 
3584   // The only other support type is pointer.
3585   assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
3586   return getDataLayout().getIndexType(Ty);
3587 }
3588 
getWiderType(Type * T1,Type * T2) const3589 Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const {
3590   return  getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2;
3591 }
3592 
getCouldNotCompute()3593 const SCEV *ScalarEvolution::getCouldNotCompute() {
3594   return CouldNotCompute.get();
3595 }
3596 
checkValidity(const SCEV * S) const3597 bool ScalarEvolution::checkValidity(const SCEV *S) const {
3598   bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) {
3599     auto *SU = dyn_cast<SCEVUnknown>(S);
3600     return SU && SU->getValue() == nullptr;
3601   });
3602 
3603   return !ContainsNulls;
3604 }
3605 
containsAddRecurrence(const SCEV * S)3606 bool ScalarEvolution::containsAddRecurrence(const SCEV *S) {
3607   HasRecMapType::iterator I = HasRecMap.find(S);
3608   if (I != HasRecMap.end())
3609     return I->second;
3610 
3611   bool FoundAddRec =
3612       SCEVExprContains(S, [](const SCEV *S) { return isa<SCEVAddRecExpr>(S); });
3613   HasRecMap.insert({S, FoundAddRec});
3614   return FoundAddRec;
3615 }
3616 
3617 /// Try to split a SCEVAddExpr into a pair of {SCEV, ConstantInt}.
3618 /// If \p S is a SCEVAddExpr and is composed of a sub SCEV S' and an
3619 /// offset I, then return {S', I}, else return {\p S, nullptr}.
splitAddExpr(const SCEV * S)3620 static std::pair<const SCEV *, ConstantInt *> splitAddExpr(const SCEV *S) {
3621   const auto *Add = dyn_cast<SCEVAddExpr>(S);
3622   if (!Add)
3623     return {S, nullptr};
3624 
3625   if (Add->getNumOperands() != 2)
3626     return {S, nullptr};
3627 
3628   auto *ConstOp = dyn_cast<SCEVConstant>(Add->getOperand(0));
3629   if (!ConstOp)
3630     return {S, nullptr};
3631 
3632   return {Add->getOperand(1), ConstOp->getValue()};
3633 }
3634 
3635 /// Return the ValueOffsetPair set for \p S. \p S can be represented
3636 /// by the value and offset from any ValueOffsetPair in the set.
3637 SetVector<ScalarEvolution::ValueOffsetPair> *
getSCEVValues(const SCEV * S)3638 ScalarEvolution::getSCEVValues(const SCEV *S) {
3639   ExprValueMapType::iterator SI = ExprValueMap.find_as(S);
3640   if (SI == ExprValueMap.end())
3641     return nullptr;
3642 #ifndef NDEBUG
3643   if (VerifySCEVMap) {
3644     // Check there is no dangling Value in the set returned.
3645     for (const auto &VE : SI->second)
3646       assert(ValueExprMap.count(VE.first));
3647   }
3648 #endif
3649   return &SI->second;
3650 }
3651 
3652 /// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
3653 /// cannot be used separately. eraseValueFromMap should be used to remove
3654 /// V from ValueExprMap and ExprValueMap at the same time.
eraseValueFromMap(Value * V)3655 void ScalarEvolution::eraseValueFromMap(Value *V) {
3656   ValueExprMapType::iterator I = ValueExprMap.find_as(V);
3657   if (I != ValueExprMap.end()) {
3658     const SCEV *S = I->second;
3659     // Remove {V, 0} from the set of ExprValueMap[S]
3660     if (SetVector<ValueOffsetPair> *SV = getSCEVValues(S))
3661       SV->remove({V, nullptr});
3662 
3663     // Remove {V, Offset} from the set of ExprValueMap[Stripped]
3664     const SCEV *Stripped;
3665     ConstantInt *Offset;
3666     std::tie(Stripped, Offset) = splitAddExpr(S);
3667     if (Offset != nullptr) {
3668       if (SetVector<ValueOffsetPair> *SV = getSCEVValues(Stripped))
3669         SV->remove({V, Offset});
3670     }
3671     ValueExprMap.erase(V);
3672   }
3673 }
3674 
3675 /// Check whether value has nuw/nsw/exact set but SCEV does not.
3676 /// TODO: In reality it is better to check the poison recursively
3677 /// but this is better than nothing.
SCEVLostPoisonFlags(const SCEV * S,const Value * V)3678 static bool SCEVLostPoisonFlags(const SCEV *S, const Value *V) {
3679   if (auto *I = dyn_cast<Instruction>(V)) {
3680     if (isa<OverflowingBinaryOperator>(I)) {
3681       if (auto *NS = dyn_cast<SCEVNAryExpr>(S)) {
3682         if (I->hasNoSignedWrap() && !NS->hasNoSignedWrap())
3683           return true;
3684         if (I->hasNoUnsignedWrap() && !NS->hasNoUnsignedWrap())
3685           return true;
3686       }
3687     } else if (isa<PossiblyExactOperator>(I) && I->isExact())
3688       return true;
3689   }
3690   return false;
3691 }
3692 
3693 /// Return an existing SCEV if it exists, otherwise analyze the expression and
3694 /// create a new one.
getSCEV(Value * V)3695 const SCEV *ScalarEvolution::getSCEV(Value *V) {
3696   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
3697 
3698   const SCEV *S = getExistingSCEV(V);
3699   if (S == nullptr) {
3700     S = createSCEV(V);
3701     // During PHI resolution, it is possible to create two SCEVs for the same
3702     // V, so it is needed to double check whether V->S is inserted into
3703     // ValueExprMap before insert S->{V, 0} into ExprValueMap.
3704     std::pair<ValueExprMapType::iterator, bool> Pair =
3705         ValueExprMap.insert({SCEVCallbackVH(V, this), S});
3706     if (Pair.second && !SCEVLostPoisonFlags(S, V)) {
3707       ExprValueMap[S].insert({V, nullptr});
3708 
3709       // If S == Stripped + Offset, add Stripped -> {V, Offset} into
3710       // ExprValueMap.
3711       const SCEV *Stripped = S;
3712       ConstantInt *Offset = nullptr;
3713       std::tie(Stripped, Offset) = splitAddExpr(S);
3714       // If stripped is SCEVUnknown, don't bother to save
3715       // Stripped -> {V, offset}. It doesn't simplify and sometimes even
3716       // increase the complexity of the expansion code.
3717       // If V is GetElementPtrInst, don't save Stripped -> {V, offset}
3718       // because it may generate add/sub instead of GEP in SCEV expansion.
3719       if (Offset != nullptr && !isa<SCEVUnknown>(Stripped) &&
3720           !isa<GetElementPtrInst>(V))
3721         ExprValueMap[Stripped].insert({V, Offset});
3722     }
3723   }
3724   return S;
3725 }
3726 
getExistingSCEV(Value * V)3727 const SCEV *ScalarEvolution::getExistingSCEV(Value *V) {
3728   assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
3729 
3730   ValueExprMapType::iterator I = ValueExprMap.find_as(V);
3731   if (I != ValueExprMap.end()) {
3732     const SCEV *S = I->second;
3733     if (checkValidity(S))
3734       return S;
3735     eraseValueFromMap(V);
3736     forgetMemoizedResults(S);
3737   }
3738   return nullptr;
3739 }
3740 
3741 /// Return a SCEV corresponding to -V = -1*V
getNegativeSCEV(const SCEV * V,SCEV::NoWrapFlags Flags)3742 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
3743                                              SCEV::NoWrapFlags Flags) {
3744   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
3745     return getConstant(
3746                cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
3747 
3748   Type *Ty = V->getType();
3749   Ty = getEffectiveSCEVType(Ty);
3750   return getMulExpr(
3751       V, getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))), Flags);
3752 }
3753 
3754 /// If Expr computes ~A, return A else return nullptr
MatchNotExpr(const SCEV * Expr)3755 static const SCEV *MatchNotExpr(const SCEV *Expr) {
3756   const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
3757   if (!Add || Add->getNumOperands() != 2 ||
3758       !Add->getOperand(0)->isAllOnesValue())
3759     return nullptr;
3760 
3761   const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
3762   if (!AddRHS || AddRHS->getNumOperands() != 2 ||
3763       !AddRHS->getOperand(0)->isAllOnesValue())
3764     return nullptr;
3765 
3766   return AddRHS->getOperand(1);
3767 }
3768 
3769 /// Return a SCEV corresponding to ~V = -1-V
getNotSCEV(const SCEV * V)3770 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
3771   if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
3772     return getConstant(
3773                 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
3774 
3775   // Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y)
3776   if (const SCEVMinMaxExpr *MME = dyn_cast<SCEVMinMaxExpr>(V)) {
3777     auto MatchMinMaxNegation = [&](const SCEVMinMaxExpr *MME) {
3778       SmallVector<const SCEV *, 2> MatchedOperands;
3779       for (const SCEV *Operand : MME->operands()) {
3780         const SCEV *Matched = MatchNotExpr(Operand);
3781         if (!Matched)
3782           return (const SCEV *)nullptr;
3783         MatchedOperands.push_back(Matched);
3784       }
3785       return getMinMaxExpr(
3786           SCEVMinMaxExpr::negate(static_cast<SCEVTypes>(MME->getSCEVType())),
3787           MatchedOperands);
3788     };
3789     if (const SCEV *Replaced = MatchMinMaxNegation(MME))
3790       return Replaced;
3791   }
3792 
3793   Type *Ty = V->getType();
3794   Ty = getEffectiveSCEVType(Ty);
3795   const SCEV *AllOnes =
3796                    getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
3797   return getMinusSCEV(AllOnes, V);
3798 }
3799 
getMinusSCEV(const SCEV * LHS,const SCEV * RHS,SCEV::NoWrapFlags Flags,unsigned Depth)3800 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
3801                                           SCEV::NoWrapFlags Flags,
3802                                           unsigned Depth) {
3803   // Fast path: X - X --> 0.
3804   if (LHS == RHS)
3805     return getZero(LHS->getType());
3806 
3807   // We represent LHS - RHS as LHS + (-1)*RHS. This transformation
3808   // makes it so that we cannot make much use of NUW.
3809   auto AddFlags = SCEV::FlagAnyWrap;
3810   const bool RHSIsNotMinSigned =
3811       !getSignedRangeMin(RHS).isMinSignedValue();
3812   if (maskFlags(Flags, SCEV::FlagNSW) == SCEV::FlagNSW) {
3813     // Let M be the minimum representable signed value. Then (-1)*RHS
3814     // signed-wraps if and only if RHS is M. That can happen even for
3815     // a NSW subtraction because e.g. (-1)*M signed-wraps even though
3816     // -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
3817     // (-1)*RHS, we need to prove that RHS != M.
3818     //
3819     // If LHS is non-negative and we know that LHS - RHS does not
3820     // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
3821     // either by proving that RHS > M or that LHS >= 0.
3822     if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) {
3823       AddFlags = SCEV::FlagNSW;
3824     }
3825   }
3826 
3827   // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
3828   // RHS is NSW and LHS >= 0.
3829   //
3830   // The difficulty here is that the NSW flag may have been proven
3831   // relative to a loop that is to be found in a recurrence in LHS and
3832   // not in RHS. Applying NSW to (-1)*M may then let the NSW have a
3833   // larger scope than intended.
3834   auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
3835 
3836   return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth);
3837 }
3838 
getTruncateOrZeroExtend(const SCEV * V,Type * Ty,unsigned Depth)3839 const SCEV *ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty,
3840                                                      unsigned Depth) {
3841   Type *SrcTy = V->getType();
3842   assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
3843          "Cannot truncate or zero extend with non-integer arguments!");
3844   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3845     return V;  // No conversion
3846   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
3847     return getTruncateExpr(V, Ty, Depth);
3848   return getZeroExtendExpr(V, Ty, Depth);
3849 }
3850 
getTruncateOrSignExtend(const SCEV * V,Type * Ty,unsigned Depth)3851 const SCEV *ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, Type *Ty,
3852                                                      unsigned Depth) {
3853   Type *SrcTy = V->getType();
3854   assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
3855          "Cannot truncate or zero extend with non-integer arguments!");
3856   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3857     return V;  // No conversion
3858   if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
3859     return getTruncateExpr(V, Ty, Depth);
3860   return getSignExtendExpr(V, Ty, Depth);
3861 }
3862 
3863 const SCEV *
getNoopOrZeroExtend(const SCEV * V,Type * Ty)3864 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
3865   Type *SrcTy = V->getType();
3866   assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
3867          "Cannot noop or zero extend with non-integer arguments!");
3868   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3869          "getNoopOrZeroExtend cannot truncate!");
3870   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3871     return V;  // No conversion
3872   return getZeroExtendExpr(V, Ty);
3873 }
3874 
3875 const SCEV *
getNoopOrSignExtend(const SCEV * V,Type * Ty)3876 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
3877   Type *SrcTy = V->getType();
3878   assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
3879          "Cannot noop or sign extend with non-integer arguments!");
3880   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3881          "getNoopOrSignExtend cannot truncate!");
3882   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3883     return V;  // No conversion
3884   return getSignExtendExpr(V, Ty);
3885 }
3886 
3887 const SCEV *
getNoopOrAnyExtend(const SCEV * V,Type * Ty)3888 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
3889   Type *SrcTy = V->getType();
3890   assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
3891          "Cannot noop or any extend with non-integer arguments!");
3892   assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
3893          "getNoopOrAnyExtend cannot truncate!");
3894   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3895     return V;  // No conversion
3896   return getAnyExtendExpr(V, Ty);
3897 }
3898 
3899 const SCEV *
getTruncateOrNoop(const SCEV * V,Type * Ty)3900 ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
3901   Type *SrcTy = V->getType();
3902   assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
3903          "Cannot truncate or noop with non-integer arguments!");
3904   assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
3905          "getTruncateOrNoop cannot extend!");
3906   if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
3907     return V;  // No conversion
3908   return getTruncateExpr(V, Ty);
3909 }
3910 
getUMaxFromMismatchedTypes(const SCEV * LHS,const SCEV * RHS)3911 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
3912                                                         const SCEV *RHS) {
3913   const SCEV *PromotedLHS = LHS;
3914   const SCEV *PromotedRHS = RHS;
3915 
3916   if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
3917     PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
3918   else
3919     PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
3920 
3921   return getUMaxExpr(PromotedLHS, PromotedRHS);
3922 }
3923 
getUMinFromMismatchedTypes(const SCEV * LHS,const SCEV * RHS)3924 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
3925                                                         const SCEV *RHS) {
3926   SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
3927   return getUMinFromMismatchedTypes(Ops);
3928 }
3929 
getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV * > & Ops)3930 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(
3931     SmallVectorImpl<const SCEV *> &Ops) {
3932   assert(!Ops.empty() && "At least one operand must be!");
3933   // Trivial case.
3934   if (Ops.size() == 1)
3935     return Ops[0];
3936 
3937   // Find the max type first.
3938   Type *MaxType = nullptr;
3939   for (auto *S : Ops)
3940     if (MaxType)
3941       MaxType = getWiderType(MaxType, S->getType());
3942     else
3943       MaxType = S->getType();
3944 
3945   // Extend all ops to max type.
3946   SmallVector<const SCEV *, 2> PromotedOps;
3947   for (auto *S : Ops)
3948     PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType));
3949 
3950   // Generate umin.
3951   return getUMinExpr(PromotedOps);
3952 }
3953 
getPointerBase(const SCEV * V)3954 const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
3955   // A pointer operand may evaluate to a nonpointer expression, such as null.
3956   if (!V->getType()->isPointerTy())
3957     return V;
3958 
3959   while (true) {
3960     if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
3961       V = Cast->getOperand();
3962     } else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
3963       const SCEV *PtrOp = nullptr;
3964       for (const SCEV *NAryOp : NAry->operands()) {
3965         if (NAryOp->getType()->isPointerTy()) {
3966           // Cannot find the base of an expression with multiple pointer ops.
3967           if (PtrOp)
3968             return V;
3969           PtrOp = NAryOp;
3970         }
3971       }
3972       if (!PtrOp) // All operands were non-pointer.
3973         return V;
3974       V = PtrOp;
3975     } else // Not something we can look further into.
3976       return V;
3977   }
3978 }
3979 
3980 /// Push users of the given Instruction onto the given Worklist.
3981 static void
PushDefUseChildren(Instruction * I,SmallVectorImpl<Instruction * > & Worklist)3982 PushDefUseChildren(Instruction *I,
3983                    SmallVectorImpl<Instruction *> &Worklist) {
3984   // Push the def-use children onto the Worklist stack.
3985   for (User *U : I->users())
3986     Worklist.push_back(cast<Instruction>(U));
3987 }
3988 
forgetSymbolicName(Instruction * PN,const SCEV * SymName)3989 void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) {
3990   SmallVector<Instruction *, 16> Worklist;
3991   PushDefUseChildren(PN, Worklist);
3992 
3993   SmallPtrSet<Instruction *, 8> Visited;
3994   Visited.insert(PN);
3995   while (!Worklist.empty()) {
3996     Instruction *I = Worklist.pop_back_val();
3997     if (!Visited.insert(I).second)
3998       continue;
3999 
4000     auto It = ValueExprMap.find_as(static_cast<Value *>(I));
4001     if (It != ValueExprMap.end()) {
4002       const SCEV *Old = It->second;
4003 
4004       // Short-circuit the def-use traversal if the symbolic name
4005       // ceases to appear in expressions.
4006       if (Old != SymName && !hasOperand(Old, SymName))
4007         continue;
4008 
4009       // SCEVUnknown for a PHI either means that it has an unrecognized
4010       // structure, it's a PHI that's in the progress of being computed
4011       // by createNodeForPHI, or it's a single-value PHI. In the first case,
4012       // additional loop trip count information isn't going to change anything.
4013       // In the second case, createNodeForPHI will perform the necessary
4014       // updates on its own when it gets to that point. In the third, we do
4015       // want to forget the SCEVUnknown.
4016       if (!isa<PHINode>(I) ||
4017           !isa<SCEVUnknown>(Old) ||
4018           (I != PN && Old == SymName)) {
4019         eraseValueFromMap(It->first);
4020         forgetMemoizedResults(Old);
4021       }
4022     }
4023 
4024     PushDefUseChildren(I, Worklist);
4025   }
4026 }
4027 
4028 namespace {
4029 
4030 /// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start
4031 /// expression in case its Loop is L. If it is not L then
4032 /// if IgnoreOtherLoops is true then use AddRec itself
4033 /// otherwise rewrite cannot be done.
4034 /// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4035 class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> {
4036 public:
rewrite(const SCEV * S,const Loop * L,ScalarEvolution & SE,bool IgnoreOtherLoops=true)4037   static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
4038                              bool IgnoreOtherLoops = true) {
4039     SCEVInitRewriter Rewriter(L, SE);
4040     const SCEV *Result = Rewriter.visit(S);
4041     if (Rewriter.hasSeenLoopVariantSCEVUnknown())
4042       return SE.getCouldNotCompute();
4043     return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops
4044                ? SE.getCouldNotCompute()
4045                : Result;
4046   }
4047 
visitUnknown(const SCEVUnknown * Expr)4048   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4049     if (!SE.isLoopInvariant(Expr, L))
4050       SeenLoopVariantSCEVUnknown = true;
4051     return Expr;
4052   }
4053 
visitAddRecExpr(const SCEVAddRecExpr * Expr)4054   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4055     // Only re-write AddRecExprs for this loop.
4056     if (Expr->getLoop() == L)
4057       return Expr->getStart();
4058     SeenOtherLoops = true;
4059     return Expr;
4060   }
4061 
hasSeenLoopVariantSCEVUnknown()4062   bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4063 
hasSeenOtherLoops()4064   bool hasSeenOtherLoops() { return SeenOtherLoops; }
4065 
4066 private:
SCEVInitRewriter(const Loop * L,ScalarEvolution & SE)4067   explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE)
4068       : SCEVRewriteVisitor(SE), L(L) {}
4069 
4070   const Loop *L;
4071   bool SeenLoopVariantSCEVUnknown = false;
4072   bool SeenOtherLoops = false;
4073 };
4074 
4075 /// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post
4076 /// increment expression in case its Loop is L. If it is not L then
4077 /// use AddRec itself.
4078 /// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4079 class SCEVPostIncRewriter : public SCEVRewriteVisitor<SCEVPostIncRewriter> {
4080 public:
rewrite(const SCEV * S,const Loop * L,ScalarEvolution & SE)4081   static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) {
4082     SCEVPostIncRewriter Rewriter(L, SE);
4083     const SCEV *Result = Rewriter.visit(S);
4084     return Rewriter.hasSeenLoopVariantSCEVUnknown()
4085         ? SE.getCouldNotCompute()
4086         : Result;
4087   }
4088 
visitUnknown(const SCEVUnknown * Expr)4089   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4090     if (!SE.isLoopInvariant(Expr, L))
4091       SeenLoopVariantSCEVUnknown = true;
4092     return Expr;
4093   }
4094 
visitAddRecExpr(const SCEVAddRecExpr * Expr)4095   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4096     // Only re-write AddRecExprs for this loop.
4097     if (Expr->getLoop() == L)
4098       return Expr->getPostIncExpr(SE);
4099     SeenOtherLoops = true;
4100     return Expr;
4101   }
4102 
hasSeenLoopVariantSCEVUnknown()4103   bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4104 
hasSeenOtherLoops()4105   bool hasSeenOtherLoops() { return SeenOtherLoops; }
4106 
4107 private:
SCEVPostIncRewriter(const Loop * L,ScalarEvolution & SE)4108   explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE)
4109       : SCEVRewriteVisitor(SE), L(L) {}
4110 
4111   const Loop *L;
4112   bool SeenLoopVariantSCEVUnknown = false;
4113   bool SeenOtherLoops = false;
4114 };
4115 
4116 /// This class evaluates the compare condition by matching it against the
4117 /// condition of loop latch. If there is a match we assume a true value
4118 /// for the condition while building SCEV nodes.
4119 class SCEVBackedgeConditionFolder
4120     : public SCEVRewriteVisitor<SCEVBackedgeConditionFolder> {
4121 public:
rewrite(const SCEV * S,const Loop * L,ScalarEvolution & SE)4122   static const SCEV *rewrite(const SCEV *S, const Loop *L,
4123                              ScalarEvolution &SE) {
4124     bool IsPosBECond = false;
4125     Value *BECond = nullptr;
4126     if (BasicBlock *Latch = L->getLoopLatch()) {
4127       BranchInst *BI = dyn_cast<BranchInst>(Latch->getTerminator());
4128       if (BI && BI->isConditional()) {
4129         assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
4130                "Both outgoing branches should not target same header!");
4131         BECond = BI->getCondition();
4132         IsPosBECond = BI->getSuccessor(0) == L->getHeader();
4133       } else {
4134         return S;
4135       }
4136     }
4137     SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE);
4138     return Rewriter.visit(S);
4139   }
4140 
visitUnknown(const SCEVUnknown * Expr)4141   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4142     const SCEV *Result = Expr;
4143     bool InvariantF = SE.isLoopInvariant(Expr, L);
4144 
4145     if (!InvariantF) {
4146       Instruction *I = cast<Instruction>(Expr->getValue());
4147       switch (I->getOpcode()) {
4148       case Instruction::Select: {
4149         SelectInst *SI = cast<SelectInst>(I);
4150         Optional<const SCEV *> Res =
4151             compareWithBackedgeCondition(SI->getCondition());
4152         if (Res.hasValue()) {
4153           bool IsOne = cast<SCEVConstant>(Res.getValue())->getValue()->isOne();
4154           Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue());
4155         }
4156         break;
4157       }
4158       default: {
4159         Optional<const SCEV *> Res = compareWithBackedgeCondition(I);
4160         if (Res.hasValue())
4161           Result = Res.getValue();
4162         break;
4163       }
4164       }
4165     }
4166     return Result;
4167   }
4168 
4169 private:
SCEVBackedgeConditionFolder(const Loop * L,Value * BECond,bool IsPosBECond,ScalarEvolution & SE)4170   explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond,
4171                                        bool IsPosBECond, ScalarEvolution &SE)
4172       : SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond),
4173         IsPositiveBECond(IsPosBECond) {}
4174 
4175   Optional<const SCEV *> compareWithBackedgeCondition(Value *IC);
4176 
4177   const Loop *L;
4178   /// Loop back condition.
4179   Value *BackedgeCond = nullptr;
4180   /// Set to true if loop back is on positive branch condition.
4181   bool IsPositiveBECond;
4182 };
4183 
4184 Optional<const SCEV *>
compareWithBackedgeCondition(Value * IC)4185 SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) {
4186 
4187   // If value matches the backedge condition for loop latch,
4188   // then return a constant evolution node based on loopback
4189   // branch taken.
4190   if (BackedgeCond == IC)
4191     return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext()))
4192                             : SE.getZero(Type::getInt1Ty(SE.getContext()));
4193   return None;
4194 }
4195 
4196 class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> {
4197 public:
rewrite(const SCEV * S,const Loop * L,ScalarEvolution & SE)4198   static const SCEV *rewrite(const SCEV *S, const Loop *L,
4199                              ScalarEvolution &SE) {
4200     SCEVShiftRewriter Rewriter(L, SE);
4201     const SCEV *Result = Rewriter.visit(S);
4202     return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
4203   }
4204 
visitUnknown(const SCEVUnknown * Expr)4205   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4206     // Only allow AddRecExprs for this loop.
4207     if (!SE.isLoopInvariant(Expr, L))
4208       Valid = false;
4209     return Expr;
4210   }
4211 
visitAddRecExpr(const SCEVAddRecExpr * Expr)4212   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4213     if (Expr->getLoop() == L && Expr->isAffine())
4214       return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE));
4215     Valid = false;
4216     return Expr;
4217   }
4218 
isValid()4219   bool isValid() { return Valid; }
4220 
4221 private:
SCEVShiftRewriter(const Loop * L,ScalarEvolution & SE)4222   explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE)
4223       : SCEVRewriteVisitor(SE), L(L) {}
4224 
4225   const Loop *L;
4226   bool Valid = true;
4227 };
4228 
4229 } // end anonymous namespace
4230 
4231 SCEV::NoWrapFlags
proveNoWrapViaConstantRanges(const SCEVAddRecExpr * AR)4232 ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
4233   if (!AR->isAffine())
4234     return SCEV::FlagAnyWrap;
4235 
4236   using OBO = OverflowingBinaryOperator;
4237 
4238   SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap;
4239 
4240   if (!AR->hasNoSignedWrap()) {
4241     ConstantRange AddRecRange = getSignedRange(AR);
4242     ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this));
4243 
4244     auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4245         Instruction::Add, IncRange, OBO::NoSignedWrap);
4246     if (NSWRegion.contains(AddRecRange))
4247       Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW);
4248   }
4249 
4250   if (!AR->hasNoUnsignedWrap()) {
4251     ConstantRange AddRecRange = getUnsignedRange(AR);
4252     ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this));
4253 
4254     auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4255         Instruction::Add, IncRange, OBO::NoUnsignedWrap);
4256     if (NUWRegion.contains(AddRecRange))
4257       Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW);
4258   }
4259 
4260   return Result;
4261 }
4262 
4263 namespace {
4264 
4265 /// Represents an abstract binary operation.  This may exist as a
4266 /// normal instruction or constant expression, or may have been
4267 /// derived from an expression tree.
4268 struct BinaryOp {
4269   unsigned Opcode;
4270   Value *LHS;
4271   Value *RHS;
4272   bool IsNSW = false;
4273   bool IsNUW = false;
4274 
4275   /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
4276   /// constant expression.
4277   Operator *Op = nullptr;
4278 
BinaryOp__anond51b32ac0f11::BinaryOp4279   explicit BinaryOp(Operator *Op)
4280       : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)),
4281         Op(Op) {
4282     if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
4283       IsNSW = OBO->hasNoSignedWrap();
4284       IsNUW = OBO->hasNoUnsignedWrap();
4285     }
4286   }
4287 
BinaryOp__anond51b32ac0f11::BinaryOp4288   explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false,
4289                     bool IsNUW = false)
4290       : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
4291 };
4292 
4293 } // end anonymous namespace
4294 
4295 /// Try to map \p V into a BinaryOp, and return \c None on failure.
MatchBinaryOp(Value * V,DominatorTree & DT)4296 static Optional<BinaryOp> MatchBinaryOp(Value *V, DominatorTree &DT) {
4297   auto *Op = dyn_cast<Operator>(V);
4298   if (!Op)
4299     return None;
4300 
4301   // Implementation detail: all the cleverness here should happen without
4302   // creating new SCEV expressions -- our caller knowns tricks to avoid creating
4303   // SCEV expressions when possible, and we should not break that.
4304 
4305   switch (Op->getOpcode()) {
4306   case Instruction::Add:
4307   case Instruction::Sub:
4308   case Instruction::Mul:
4309   case Instruction::UDiv:
4310   case Instruction::URem:
4311   case Instruction::And:
4312   case Instruction::Or:
4313   case Instruction::AShr:
4314   case Instruction::Shl:
4315     return BinaryOp(Op);
4316 
4317   case Instruction::Xor:
4318     if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1)))
4319       // If the RHS of the xor is a signmask, then this is just an add.
4320       // Instcombine turns add of signmask into xor as a strength reduction step.
4321       if (RHSC->getValue().isSignMask())
4322         return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
4323     return BinaryOp(Op);
4324 
4325   case Instruction::LShr:
4326     // Turn logical shift right of a constant into a unsigned divide.
4327     if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
4328       uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth();
4329 
4330       // If the shift count is not less than the bitwidth, the result of
4331       // the shift is undefined. Don't try to analyze it, because the
4332       // resolution chosen here may differ from the resolution chosen in
4333       // other parts of the compiler.
4334       if (SA->getValue().ult(BitWidth)) {
4335         Constant *X =
4336             ConstantInt::get(SA->getContext(),
4337                              APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
4338         return BinaryOp(Instruction::UDiv, Op->getOperand(0), X);
4339       }
4340     }
4341     return BinaryOp(Op);
4342 
4343   case Instruction::ExtractValue: {
4344     auto *EVI = cast<ExtractValueInst>(Op);
4345     if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
4346       break;
4347 
4348     auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand());
4349     if (!WO)
4350       break;
4351 
4352     Instruction::BinaryOps BinOp = WO->getBinaryOp();
4353     bool Signed = WO->isSigned();
4354     // TODO: Should add nuw/nsw flags for mul as well.
4355     if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT))
4356       return BinaryOp(BinOp, WO->getLHS(), WO->getRHS());
4357 
4358     // Now that we know that all uses of the arithmetic-result component of
4359     // CI are guarded by the overflow check, we can go ahead and pretend
4360     // that the arithmetic is non-overflowing.
4361     return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(),
4362                     /* IsNSW = */ Signed, /* IsNUW = */ !Signed);
4363   }
4364 
4365   default:
4366     break;
4367   }
4368 
4369   // Recognise intrinsic loop.decrement.reg, and as this has exactly the same
4370   // semantics as a Sub, return a binary sub expression.
4371   if (auto *II = dyn_cast<IntrinsicInst>(V))
4372     if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg)
4373       return BinaryOp(Instruction::Sub, II->getOperand(0), II->getOperand(1));
4374 
4375   return None;
4376 }
4377 
4378 /// Helper function to createAddRecFromPHIWithCasts. We have a phi
4379 /// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
4380 /// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the
4381 /// way. This function checks if \p Op, an operand of this SCEVAddExpr,
4382 /// follows one of the following patterns:
4383 /// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4384 /// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4385 /// If the SCEV expression of \p Op conforms with one of the expected patterns
4386 /// we return the type of the truncation operation, and indicate whether the
4387 /// truncated type should be treated as signed/unsigned by setting
4388 /// \p Signed to true/false, respectively.
isSimpleCastedPHI(const SCEV * Op,const SCEVUnknown * SymbolicPHI,bool & Signed,ScalarEvolution & SE)4389 static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
4390                                bool &Signed, ScalarEvolution &SE) {
4391   // The case where Op == SymbolicPHI (that is, with no type conversions on
4392   // the way) is handled by the regular add recurrence creating logic and
4393   // would have already been triggered in createAddRecForPHI. Reaching it here
4394   // means that createAddRecFromPHI had failed for this PHI before (e.g.,
4395   // because one of the other operands of the SCEVAddExpr updating this PHI is
4396   // not invariant).
4397   //
4398   // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in
4399   // this case predicates that allow us to prove that Op == SymbolicPHI will
4400   // be added.
4401   if (Op == SymbolicPHI)
4402     return nullptr;
4403 
4404   unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType());
4405   unsigned NewBits = SE.getTypeSizeInBits(Op->getType());
4406   if (SourceBits != NewBits)
4407     return nullptr;
4408 
4409   const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op);
4410   const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op);
4411   if (!SExt && !ZExt)
4412     return nullptr;
4413   const SCEVTruncateExpr *Trunc =
4414       SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand())
4415            : dyn_cast<SCEVTruncateExpr>(ZExt->getOperand());
4416   if (!Trunc)
4417     return nullptr;
4418   const SCEV *X = Trunc->getOperand();
4419   if (X != SymbolicPHI)
4420     return nullptr;
4421   Signed = SExt != nullptr;
4422   return Trunc->getType();
4423 }
4424 
isIntegerLoopHeaderPHI(const PHINode * PN,LoopInfo & LI)4425 static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
4426   if (!PN->getType()->isIntegerTy())
4427     return nullptr;
4428   const Loop *L = LI.getLoopFor(PN->getParent());
4429   if (!L || L->getHeader() != PN->getParent())
4430     return nullptr;
4431   return L;
4432 }
4433 
4434 // Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
4435 // computation that updates the phi follows the following pattern:
4436 //   (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
4437 // which correspond to a phi->trunc->sext/zext->add->phi update chain.
4438 // If so, try to see if it can be rewritten as an AddRecExpr under some
4439 // Predicates. If successful, return them as a pair. Also cache the results
4440 // of the analysis.
4441 //
4442 // Example usage scenario:
4443 //    Say the Rewriter is called for the following SCEV:
4444 //         8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4445 //    where:
4446 //         %X = phi i64 (%Start, %BEValue)
4447 //    It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
4448 //    and call this function with %SymbolicPHI = %X.
4449 //
4450 //    The analysis will find that the value coming around the backedge has
4451 //    the following SCEV:
4452 //         BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4453 //    Upon concluding that this matches the desired pattern, the function
4454 //    will return the pair {NewAddRec, SmallPredsVec} where:
4455 //         NewAddRec = {%Start,+,%Step}
4456 //         SmallPredsVec = {P1, P2, P3} as follows:
4457 //           P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
4458 //           P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
4459 //           P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
4460 //    The returned pair means that SymbolicPHI can be rewritten into NewAddRec
4461 //    under the predicates {P1,P2,P3}.
4462 //    This predicated rewrite will be cached in PredicatedSCEVRewrites:
4463 //         PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)}
4464 //
4465 // TODO's:
4466 //
4467 // 1) Extend the Induction descriptor to also support inductions that involve
4468 //    casts: When needed (namely, when we are called in the context of the
4469 //    vectorizer induction analysis), a Set of cast instructions will be
4470 //    populated by this method, and provided back to isInductionPHI. This is
4471 //    needed to allow the vectorizer to properly record them to be ignored by
4472 //    the cost model and to avoid vectorizing them (otherwise these casts,
4473 //    which are redundant under the runtime overflow checks, will be
4474 //    vectorized, which can be costly).
4475 //
4476 // 2) Support additional induction/PHISCEV patterns: We also want to support
4477 //    inductions where the sext-trunc / zext-trunc operations (partly) occur
4478 //    after the induction update operation (the induction increment):
4479 //
4480 //      (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
4481 //    which correspond to a phi->add->trunc->sext/zext->phi update chain.
4482 //
4483 //      (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
4484 //    which correspond to a phi->trunc->add->sext/zext->phi update chain.
4485 //
4486 // 3) Outline common code with createAddRecFromPHI to avoid duplication.
4487 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
createAddRecFromPHIWithCastsImpl(const SCEVUnknown * SymbolicPHI)4488 ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) {
4489   SmallVector<const SCEVPredicate *, 3> Predicates;
4490 
4491   // *** Part1: Analyze if we have a phi-with-cast pattern for which we can
4492   // return an AddRec expression under some predicate.
4493 
4494   auto *PN = cast<PHINode>(SymbolicPHI->getValue());
4495   const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
4496   assert(L && "Expecting an integer loop header phi");
4497 
4498   // The loop may have multiple entrances or multiple exits; we can analyze
4499   // this phi as an addrec if it has a unique entry value and a unique
4500   // backedge value.
4501   Value *BEValueV = nullptr, *StartValueV = nullptr;
4502   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
4503     Value *V = PN->getIncomingValue(i);
4504     if (L->contains(PN->getIncomingBlock(i))) {
4505       if (!BEValueV) {
4506         BEValueV = V;
4507       } else if (BEValueV != V) {
4508         BEValueV = nullptr;
4509         break;
4510       }
4511     } else if (!StartValueV) {
4512       StartValueV = V;
4513     } else if (StartValueV != V) {
4514       StartValueV = nullptr;
4515       break;
4516     }
4517   }
4518   if (!BEValueV || !StartValueV)
4519     return None;
4520 
4521   const SCEV *BEValue = getSCEV(BEValueV);
4522 
4523   // If the value coming around the backedge is an add with the symbolic
4524   // value we just inserted, possibly with casts that we can ignore under
4525   // an appropriate runtime guard, then we found a simple induction variable!
4526   const auto *Add = dyn_cast<SCEVAddExpr>(BEValue);
4527   if (!Add)
4528     return None;
4529 
4530   // If there is a single occurrence of the symbolic value, possibly
4531   // casted, replace it with a recurrence.
4532   unsigned FoundIndex = Add->getNumOperands();
4533   Type *TruncTy = nullptr;
4534   bool Signed;
4535   for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4536     if ((TruncTy =
4537              isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this)))
4538       if (FoundIndex == e) {
4539         FoundIndex = i;
4540         break;
4541       }
4542 
4543   if (FoundIndex == Add->getNumOperands())
4544     return None;
4545 
4546   // Create an add with everything but the specified operand.
4547   SmallVector<const SCEV *, 8> Ops;
4548   for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4549     if (i != FoundIndex)
4550       Ops.push_back(Add->getOperand(i));
4551   const SCEV *Accum = getAddExpr(Ops);
4552 
4553   // The runtime checks will not be valid if the step amount is
4554   // varying inside the loop.
4555   if (!isLoopInvariant(Accum, L))
4556     return None;
4557 
4558   // *** Part2: Create the predicates
4559 
4560   // Analysis was successful: we have a phi-with-cast pattern for which we
4561   // can return an AddRec expression under the following predicates:
4562   //
4563   // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
4564   //     fits within the truncated type (does not overflow) for i = 0 to n-1.
4565   // P2: An Equal predicate that guarantees that
4566   //     Start = (Ext ix (Trunc iy (Start) to ix) to iy)
4567   // P3: An Equal predicate that guarantees that
4568   //     Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
4569   //
4570   // As we next prove, the above predicates guarantee that:
4571   //     Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
4572   //
4573   //
4574   // More formally, we want to prove that:
4575   //     Expr(i+1) = Start + (i+1) * Accum
4576   //               = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
4577   //
4578   // Given that:
4579   // 1) Expr(0) = Start
4580   // 2) Expr(1) = Start + Accum
4581   //            = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
4582   // 3) Induction hypothesis (step i):
4583   //    Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum
4584   //
4585   // Proof:
4586   //  Expr(i+1) =
4587   //   = Start + (i+1)*Accum
4588   //   = (Start + i*Accum) + Accum
4589   //   = Expr(i) + Accum
4590   //   = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum
4591   //                                                             :: from step i
4592   //
4593   //   = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum
4594   //
4595   //   = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
4596   //     + (Ext ix (Trunc iy (Accum) to ix) to iy)
4597   //     + Accum                                                     :: from P3
4598   //
4599   //   = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy)
4600   //     + Accum                            :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
4601   //
4602   //   = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
4603   //   = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
4604   //
4605   // By induction, the same applies to all iterations 1<=i<n:
4606   //
4607 
4608   // Create a truncated addrec for which we will add a no overflow check (P1).
4609   const SCEV *StartVal = getSCEV(StartValueV);
4610   const SCEV *PHISCEV =
4611       getAddRecExpr(getTruncateExpr(StartVal, TruncTy),
4612                     getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap);
4613 
4614   // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
4615   // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
4616   // will be constant.
4617   //
4618   //  If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
4619   // add P1.
4620   if (const auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
4621     SCEVWrapPredicate::IncrementWrapFlags AddedFlags =
4622         Signed ? SCEVWrapPredicate::IncrementNSSW
4623                : SCEVWrapPredicate::IncrementNUSW;
4624     const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags);
4625     Predicates.push_back(AddRecPred);
4626   }
4627 
4628   // Create the Equal Predicates P2,P3:
4629 
4630   // It is possible that the predicates P2 and/or P3 are computable at
4631   // compile time due to StartVal and/or Accum being constants.
4632   // If either one is, then we can check that now and escape if either P2
4633   // or P3 is false.
4634 
4635   // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
4636   // for each of StartVal and Accum
4637   auto getExtendedExpr = [&](const SCEV *Expr,
4638                              bool CreateSignExtend) -> const SCEV * {
4639     assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant");
4640     const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy);
4641     const SCEV *ExtendedExpr =
4642         CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType())
4643                          : getZeroExtendExpr(TruncatedExpr, Expr->getType());
4644     return ExtendedExpr;
4645   };
4646 
4647   // Given:
4648   //  ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
4649   //               = getExtendedExpr(Expr)
4650   // Determine whether the predicate P: Expr == ExtendedExpr
4651   // is known to be false at compile time
4652   auto PredIsKnownFalse = [&](const SCEV *Expr,
4653                               const SCEV *ExtendedExpr) -> bool {
4654     return Expr != ExtendedExpr &&
4655            isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr);
4656   };
4657 
4658   const SCEV *StartExtended = getExtendedExpr(StartVal, Signed);
4659   if (PredIsKnownFalse(StartVal, StartExtended)) {
4660     LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";);
4661     return None;
4662   }
4663 
4664   // The Step is always Signed (because the overflow checks are either
4665   // NSSW or NUSW)
4666   const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true);
4667   if (PredIsKnownFalse(Accum, AccumExtended)) {
4668     LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";);
4669     return None;
4670   }
4671 
4672   auto AppendPredicate = [&](const SCEV *Expr,
4673                              const SCEV *ExtendedExpr) -> void {
4674     if (Expr != ExtendedExpr &&
4675         !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) {
4676       const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr);
4677       LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred);
4678       Predicates.push_back(Pred);
4679     }
4680   };
4681 
4682   AppendPredicate(StartVal, StartExtended);
4683   AppendPredicate(Accum, AccumExtended);
4684 
4685   // *** Part3: Predicates are ready. Now go ahead and create the new addrec in
4686   // which the casts had been folded away. The caller can rewrite SymbolicPHI
4687   // into NewAR if it will also add the runtime overflow checks specified in
4688   // Predicates.
4689   auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap);
4690 
4691   std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
4692       std::make_pair(NewAR, Predicates);
4693   // Remember the result of the analysis for this SCEV at this locayyytion.
4694   PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite;
4695   return PredRewrite;
4696 }
4697 
4698 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
createAddRecFromPHIWithCasts(const SCEVUnknown * SymbolicPHI)4699 ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) {
4700   auto *PN = cast<PHINode>(SymbolicPHI->getValue());
4701   const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
4702   if (!L)
4703     return None;
4704 
4705   // Check to see if we already analyzed this PHI.
4706   auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L});
4707   if (I != PredicatedSCEVRewrites.end()) {
4708     std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
4709         I->second;
4710     // Analysis was done before and failed to create an AddRec:
4711     if (Rewrite.first == SymbolicPHI)
4712       return None;
4713     // Analysis was done before and succeeded to create an AddRec under
4714     // a predicate:
4715     assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec");
4716     assert(!(Rewrite.second).empty() && "Expected to find Predicates");
4717     return Rewrite;
4718   }
4719 
4720   Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4721     Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
4722 
4723   // Record in the cache that the analysis failed
4724   if (!Rewrite) {
4725     SmallVector<const SCEVPredicate *, 3> Predicates;
4726     PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
4727     return None;
4728   }
4729 
4730   return Rewrite;
4731 }
4732 
4733 // FIXME: This utility is currently required because the Rewriter currently
4734 // does not rewrite this expression:
4735 // {0, +, (sext ix (trunc iy to ix) to iy)}
4736 // into {0, +, %step},
4737 // even when the following Equal predicate exists:
4738 // "%step == (sext ix (trunc iy to ix) to iy)".
areAddRecsEqualWithPreds(const SCEVAddRecExpr * AR1,const SCEVAddRecExpr * AR2) const4739 bool PredicatedScalarEvolution::areAddRecsEqualWithPreds(
4740     const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const {
4741   if (AR1 == AR2)
4742     return true;
4743 
4744   auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool {
4745     if (Expr1 != Expr2 && !Preds.implies(SE.getEqualPredicate(Expr1, Expr2)) &&
4746         !Preds.implies(SE.getEqualPredicate(Expr2, Expr1)))
4747       return false;
4748     return true;
4749   };
4750 
4751   if (!areExprsEqual(AR1->getStart(), AR2->getStart()) ||
4752       !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE)))
4753     return false;
4754   return true;
4755 }
4756 
4757 /// A helper function for createAddRecFromPHI to handle simple cases.
4758 ///
4759 /// This function tries to find an AddRec expression for the simplest (yet most
4760 /// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
4761 /// If it fails, createAddRecFromPHI will use a more general, but slow,
4762 /// technique for finding the AddRec expression.
createSimpleAffineAddRec(PHINode * PN,Value * BEValueV,Value * StartValueV)4763 const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
4764                                                       Value *BEValueV,
4765                                                       Value *StartValueV) {
4766   const Loop *L = LI.getLoopFor(PN->getParent());
4767   assert(L && L->getHeader() == PN->getParent());
4768   assert(BEValueV && StartValueV);
4769 
4770   auto BO = MatchBinaryOp(BEValueV, DT);
4771   if (!BO)
4772     return nullptr;
4773 
4774   if (BO->Opcode != Instruction::Add)
4775     return nullptr;
4776 
4777   const SCEV *Accum = nullptr;
4778   if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
4779     Accum = getSCEV(BO->RHS);
4780   else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
4781     Accum = getSCEV(BO->LHS);
4782 
4783   if (!Accum)
4784     return nullptr;
4785 
4786   SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
4787   if (BO->IsNUW)
4788     Flags = setFlags(Flags, SCEV::FlagNUW);
4789   if (BO->IsNSW)
4790     Flags = setFlags(Flags, SCEV::FlagNSW);
4791 
4792   const SCEV *StartVal = getSCEV(StartValueV);
4793   const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
4794 
4795   ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
4796 
4797   // We can add Flags to the post-inc expression only if we
4798   // know that it is *undefined behavior* for BEValueV to
4799   // overflow.
4800   if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
4801     if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
4802       (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
4803 
4804   return PHISCEV;
4805 }
4806 
createAddRecFromPHI(PHINode * PN)4807 const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
4808   const Loop *L = LI.getLoopFor(PN->getParent());
4809   if (!L || L->getHeader() != PN->getParent())
4810     return nullptr;
4811 
4812   // The loop may have multiple entrances or multiple exits; we can analyze
4813   // this phi as an addrec if it has a unique entry value and a unique
4814   // backedge value.
4815   Value *BEValueV = nullptr, *StartValueV = nullptr;
4816   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
4817     Value *V = PN->getIncomingValue(i);
4818     if (L->contains(PN->getIncomingBlock(i))) {
4819       if (!BEValueV) {
4820         BEValueV = V;
4821       } else if (BEValueV != V) {
4822         BEValueV = nullptr;
4823         break;
4824       }
4825     } else if (!StartValueV) {
4826       StartValueV = V;
4827     } else if (StartValueV != V) {
4828       StartValueV = nullptr;
4829       break;
4830     }
4831   }
4832   if (!BEValueV || !StartValueV)
4833     return nullptr;
4834 
4835   assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
4836          "PHI node already processed?");
4837 
4838   // First, try to find AddRec expression without creating a fictituos symbolic
4839   // value for PN.
4840   if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
4841     return S;
4842 
4843   // Handle PHI node value symbolically.
4844   const SCEV *SymbolicName = getUnknown(PN);
4845   ValueExprMap.insert({SCEVCallbackVH(PN, this), SymbolicName});
4846 
4847   // Using this symbolic name for the PHI, analyze the value coming around
4848   // the back-edge.
4849   const SCEV *BEValue = getSCEV(BEValueV);
4850 
4851   // NOTE: If BEValue is loop invariant, we know that the PHI node just
4852   // has a special value for the first iteration of the loop.
4853 
4854   // If the value coming around the backedge is an add with the symbolic
4855   // value we just inserted, then we found a simple induction variable!
4856   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
4857     // If there is a single occurrence of the symbolic value, replace it
4858     // with a recurrence.
4859     unsigned FoundIndex = Add->getNumOperands();
4860     for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4861       if (Add->getOperand(i) == SymbolicName)
4862         if (FoundIndex == e) {
4863           FoundIndex = i;
4864           break;
4865         }
4866 
4867     if (FoundIndex != Add->getNumOperands()) {
4868       // Create an add with everything but the specified operand.
4869       SmallVector<const SCEV *, 8> Ops;
4870       for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4871         if (i != FoundIndex)
4872           Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i),
4873                                                              L, *this));
4874       const SCEV *Accum = getAddExpr(Ops);
4875 
4876       // This is not a valid addrec if the step amount is varying each
4877       // loop iteration, but is not itself an addrec in this loop.
4878       if (isLoopInvariant(Accum, L) ||
4879           (isa<SCEVAddRecExpr>(Accum) &&
4880            cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
4881         SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
4882 
4883         if (auto BO = MatchBinaryOp(BEValueV, DT)) {
4884           if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
4885             if (BO->IsNUW)
4886               Flags = setFlags(Flags, SCEV::FlagNUW);
4887             if (BO->IsNSW)
4888               Flags = setFlags(Flags, SCEV::FlagNSW);
4889           }
4890         } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
4891           // If the increment is an inbounds GEP, then we know the address
4892           // space cannot be wrapped around. We cannot make any guarantee
4893           // about signed or unsigned overflow because pointers are
4894           // unsigned but we may have a negative index from the base
4895           // pointer. We can guarantee that no unsigned wrap occurs if the
4896           // indices form a positive value.
4897           if (GEP->isInBounds() && GEP->getOperand(0) == PN) {
4898             Flags = setFlags(Flags, SCEV::FlagNW);
4899 
4900             const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
4901             if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
4902               Flags = setFlags(Flags, SCEV::FlagNUW);
4903           }
4904 
4905           // We cannot transfer nuw and nsw flags from subtraction
4906           // operations -- sub nuw X, Y is not the same as add nuw X, -Y
4907           // for instance.
4908         }
4909 
4910         const SCEV *StartVal = getSCEV(StartValueV);
4911         const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
4912 
4913         // Okay, for the entire analysis of this edge we assumed the PHI
4914         // to be symbolic.  We now need to go back and purge all of the
4915         // entries for the scalars that use the symbolic expression.
4916         forgetSymbolicName(PN, SymbolicName);
4917         ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
4918 
4919         // We can add Flags to the post-inc expression only if we
4920         // know that it is *undefined behavior* for BEValueV to
4921         // overflow.
4922         if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
4923           if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
4924             (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
4925 
4926         return PHISCEV;
4927       }
4928     }
4929   } else {
4930     // Otherwise, this could be a loop like this:
4931     //     i = 0;  for (j = 1; ..; ++j) { ....  i = j; }
4932     // In this case, j = {1,+,1}  and BEValue is j.
4933     // Because the other in-value of i (0) fits the evolution of BEValue
4934     // i really is an addrec evolution.
4935     //
4936     // We can generalize this saying that i is the shifted value of BEValue
4937     // by one iteration:
4938     //   PHI(f(0), f({1,+,1})) --> f({0,+,1})
4939     const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
4940     const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false);
4941     if (Shifted != getCouldNotCompute() &&
4942         Start != getCouldNotCompute()) {
4943       const SCEV *StartVal = getSCEV(StartValueV);
4944       if (Start == StartVal) {
4945         // Okay, for the entire analysis of this edge we assumed the PHI
4946         // to be symbolic.  We now need to go back and purge all of the
4947         // entries for the scalars that use the symbolic expression.
4948         forgetSymbolicName(PN, SymbolicName);
4949         ValueExprMap[SCEVCallbackVH(PN, this)] = Shifted;
4950         return Shifted;
4951       }
4952     }
4953   }
4954 
4955   // Remove the temporary PHI node SCEV that has been inserted while intending
4956   // to create an AddRecExpr for this PHI node. We can not keep this temporary
4957   // as it will prevent later (possibly simpler) SCEV expressions to be added
4958   // to the ValueExprMap.
4959   eraseValueFromMap(PN);
4960 
4961   return nullptr;
4962 }
4963 
4964 // Checks if the SCEV S is available at BB.  S is considered available at BB
4965 // if S can be materialized at BB without introducing a fault.
IsAvailableOnEntry(const Loop * L,DominatorTree & DT,const SCEV * S,BasicBlock * BB)4966 static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S,
4967                                BasicBlock *BB) {
4968   struct CheckAvailable {
4969     bool TraversalDone = false;
4970     bool Available = true;
4971 
4972     const Loop *L = nullptr;  // The loop BB is in (can be nullptr)
4973     BasicBlock *BB = nullptr;
4974     DominatorTree &DT;
4975 
4976     CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT)
4977       : L(L), BB(BB), DT(DT) {}
4978 
4979     bool setUnavailable() {
4980       TraversalDone = true;
4981       Available = false;
4982       return false;
4983     }
4984 
4985     bool follow(const SCEV *S) {
4986       switch (S->getSCEVType()) {
4987       case scConstant: case scTruncate: case scZeroExtend: case scSignExtend:
4988       case scAddExpr: case scMulExpr: case scUMaxExpr: case scSMaxExpr:
4989       case scUMinExpr:
4990       case scSMinExpr:
4991         // These expressions are available if their operand(s) is/are.
4992         return true;
4993 
4994       case scAddRecExpr: {
4995         // We allow add recurrences that are on the loop BB is in, or some
4996         // outer loop.  This guarantees availability because the value of the
4997         // add recurrence at BB is simply the "current" value of the induction
4998         // variable.  We can relax this in the future; for instance an add
4999         // recurrence on a sibling dominating loop is also available at BB.
5000         const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop();
5001         if (L && (ARLoop == L || ARLoop->contains(L)))
5002           return true;
5003 
5004         return setUnavailable();
5005       }
5006 
5007       case scUnknown: {
5008         // For SCEVUnknown, we check for simple dominance.
5009         const auto *SU = cast<SCEVUnknown>(S);
5010         Value *V = SU->getValue();
5011 
5012         if (isa<Argument>(V))
5013           return false;
5014 
5015         if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB))
5016           return false;
5017 
5018         return setUnavailable();
5019       }
5020 
5021       case scUDivExpr:
5022       case scCouldNotCompute:
5023         // We do not try to smart about these at all.
5024         return setUnavailable();
5025       }
5026       llvm_unreachable("switch should be fully covered!");
5027     }
5028 
5029     bool isDone() { return TraversalDone; }
5030   };
5031 
5032   CheckAvailable CA(L, BB, DT);
5033   SCEVTraversal<CheckAvailable> ST(CA);
5034 
5035   ST.visitAll(S);
5036   return CA.Available;
5037 }
5038 
5039 // Try to match a control flow sequence that branches out at BI and merges back
5040 // at Merge into a "C ? LHS : RHS" select pattern.  Return true on a successful
5041 // match.
BrPHIToSelect(DominatorTree & DT,BranchInst * BI,PHINode * Merge,Value * & C,Value * & LHS,Value * & RHS)5042 static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge,
5043                           Value *&C, Value *&LHS, Value *&RHS) {
5044   C = BI->getCondition();
5045 
5046   BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0));
5047   BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1));
5048 
5049   if (!LeftEdge.isSingleEdge())
5050     return false;
5051 
5052   assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()");
5053 
5054   Use &LeftUse = Merge->getOperandUse(0);
5055   Use &RightUse = Merge->getOperandUse(1);
5056 
5057   if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) {
5058     LHS = LeftUse;
5059     RHS = RightUse;
5060     return true;
5061   }
5062 
5063   if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) {
5064     LHS = RightUse;
5065     RHS = LeftUse;
5066     return true;
5067   }
5068 
5069   return false;
5070 }
5071 
createNodeFromSelectLikePHI(PHINode * PN)5072 const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
5073   auto IsReachable =
5074       [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); };
5075   if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) {
5076     const Loop *L = LI.getLoopFor(PN->getParent());
5077 
5078     // We don't want to break LCSSA, even in a SCEV expression tree.
5079     for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
5080       if (LI.getLoopFor(PN->getIncomingBlock(i)) != L)
5081         return nullptr;
5082 
5083     // Try to match
5084     //
5085     //  br %cond, label %left, label %right
5086     // left:
5087     //  br label %merge
5088     // right:
5089     //  br label %merge
5090     // merge:
5091     //  V = phi [ %x, %left ], [ %y, %right ]
5092     //
5093     // as "select %cond, %x, %y"
5094 
5095     BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
5096     assert(IDom && "At least the entry block should dominate PN");
5097 
5098     auto *BI = dyn_cast<BranchInst>(IDom->getTerminator());
5099     Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
5100 
5101     if (BI && BI->isConditional() &&
5102         BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) &&
5103         IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) &&
5104         IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent()))
5105       return createNodeForSelectOrPHI(PN, Cond, LHS, RHS);
5106   }
5107 
5108   return nullptr;
5109 }
5110 
createNodeForPHI(PHINode * PN)5111 const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
5112   if (const SCEV *S = createAddRecFromPHI(PN))
5113     return S;
5114 
5115   if (const SCEV *S = createNodeFromSelectLikePHI(PN))
5116     return S;
5117 
5118   // If the PHI has a single incoming value, follow that value, unless the
5119   // PHI's incoming blocks are in a different loop, in which case doing so
5120   // risks breaking LCSSA form. Instcombine would normally zap these, but
5121   // it doesn't have DominatorTree information, so it may miss cases.
5122   if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC}))
5123     if (LI.replacementPreservesLCSSAForm(PN, V))
5124       return getSCEV(V);
5125 
5126   // If it's not a loop phi, we can't handle it yet.
5127   return getUnknown(PN);
5128 }
5129 
createNodeForSelectOrPHI(Instruction * I,Value * Cond,Value * TrueVal,Value * FalseVal)5130 const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Instruction *I,
5131                                                       Value *Cond,
5132                                                       Value *TrueVal,
5133                                                       Value *FalseVal) {
5134   // Handle "constant" branch or select. This can occur for instance when a
5135   // loop pass transforms an inner loop and moves on to process the outer loop.
5136   if (auto *CI = dyn_cast<ConstantInt>(Cond))
5137     return getSCEV(CI->isOne() ? TrueVal : FalseVal);
5138 
5139   // Try to match some simple smax or umax patterns.
5140   auto *ICI = dyn_cast<ICmpInst>(Cond);
5141   if (!ICI)
5142     return getUnknown(I);
5143 
5144   Value *LHS = ICI->getOperand(0);
5145   Value *RHS = ICI->getOperand(1);
5146 
5147   switch (ICI->getPredicate()) {
5148   case ICmpInst::ICMP_SLT:
5149   case ICmpInst::ICMP_SLE:
5150     std::swap(LHS, RHS);
5151     LLVM_FALLTHROUGH;
5152   case ICmpInst::ICMP_SGT:
5153   case ICmpInst::ICMP_SGE:
5154     // a >s b ? a+x : b+x  ->  smax(a, b)+x
5155     // a >s b ? b+x : a+x  ->  smin(a, b)+x
5156     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
5157       const SCEV *LS = getNoopOrSignExtend(getSCEV(LHS), I->getType());
5158       const SCEV *RS = getNoopOrSignExtend(getSCEV(RHS), I->getType());
5159       const SCEV *LA = getSCEV(TrueVal);
5160       const SCEV *RA = getSCEV(FalseVal);
5161       const SCEV *LDiff = getMinusSCEV(LA, LS);
5162       const SCEV *RDiff = getMinusSCEV(RA, RS);
5163       if (LDiff == RDiff)
5164         return getAddExpr(getSMaxExpr(LS, RS), LDiff);
5165       LDiff = getMinusSCEV(LA, RS);
5166       RDiff = getMinusSCEV(RA, LS);
5167       if (LDiff == RDiff)
5168         return getAddExpr(getSMinExpr(LS, RS), LDiff);
5169     }
5170     break;
5171   case ICmpInst::ICMP_ULT:
5172   case ICmpInst::ICMP_ULE:
5173     std::swap(LHS, RHS);
5174     LLVM_FALLTHROUGH;
5175   case ICmpInst::ICMP_UGT:
5176   case ICmpInst::ICMP_UGE:
5177     // a >u b ? a+x : b+x  ->  umax(a, b)+x
5178     // a >u b ? b+x : a+x  ->  umin(a, b)+x
5179     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
5180       const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5181       const SCEV *RS = getNoopOrZeroExtend(getSCEV(RHS), I->getType());
5182       const SCEV *LA = getSCEV(TrueVal);
5183       const SCEV *RA = getSCEV(FalseVal);
5184       const SCEV *LDiff = getMinusSCEV(LA, LS);
5185       const SCEV *RDiff = getMinusSCEV(RA, RS);
5186       if (LDiff == RDiff)
5187         return getAddExpr(getUMaxExpr(LS, RS), LDiff);
5188       LDiff = getMinusSCEV(LA, RS);
5189       RDiff = getMinusSCEV(RA, LS);
5190       if (LDiff == RDiff)
5191         return getAddExpr(getUMinExpr(LS, RS), LDiff);
5192     }
5193     break;
5194   case ICmpInst::ICMP_NE:
5195     // n != 0 ? n+x : 1+x  ->  umax(n, 1)+x
5196     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5197         isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5198       const SCEV *One = getOne(I->getType());
5199       const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5200       const SCEV *LA = getSCEV(TrueVal);
5201       const SCEV *RA = getSCEV(FalseVal);
5202       const SCEV *LDiff = getMinusSCEV(LA, LS);
5203       const SCEV *RDiff = getMinusSCEV(RA, One);
5204       if (LDiff == RDiff)
5205         return getAddExpr(getUMaxExpr(One, LS), LDiff);
5206     }
5207     break;
5208   case ICmpInst::ICMP_EQ:
5209     // n == 0 ? 1+x : n+x  ->  umax(n, 1)+x
5210     if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5211         isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5212       const SCEV *One = getOne(I->getType());
5213       const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5214       const SCEV *LA = getSCEV(TrueVal);
5215       const SCEV *RA = getSCEV(FalseVal);
5216       const SCEV *LDiff = getMinusSCEV(LA, One);
5217       const SCEV *RDiff = getMinusSCEV(RA, LS);
5218       if (LDiff == RDiff)
5219         return getAddExpr(getUMaxExpr(One, LS), LDiff);
5220     }
5221     break;
5222   default:
5223     break;
5224   }
5225 
5226   return getUnknown(I);
5227 }
5228 
5229 /// Expand GEP instructions into add and multiply operations. This allows them
5230 /// to be analyzed by regular SCEV code.
createNodeForGEP(GEPOperator * GEP)5231 const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
5232   // Don't attempt to analyze GEPs over unsized objects.
5233   if (!GEP->getSourceElementType()->isSized())
5234     return getUnknown(GEP);
5235   const DataLayout &DL = F.getParent()->getDataLayout();
5236   // FIXME: Ideally, we should teach Scalar Evolution to
5237   // understand fat pointers.
5238   if (DL.isFatPointer(GEP->getPointerOperandType()->getPointerAddressSpace()))
5239     return getUnknown(GEP);
5240 
5241   SmallVector<const SCEV *, 4> IndexExprs;
5242   for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index)
5243     IndexExprs.push_back(getSCEV(*Index));
5244   return getGEPExpr(GEP, IndexExprs);
5245 }
5246 
GetMinTrailingZerosImpl(const SCEV * S)5247 uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
5248   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
5249     return C->getAPInt().countTrailingZeros();
5250 
5251   if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
5252     return std::min(GetMinTrailingZeros(T->getOperand()),
5253                     (uint32_t)getTypeSizeInBits(T->getType()));
5254 
5255   if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
5256     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
5257     return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5258                ? getTypeSizeInBits(E->getType())
5259                : OpRes;
5260   }
5261 
5262   if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
5263     uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
5264     return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5265                ? getTypeSizeInBits(E->getType())
5266                : OpRes;
5267   }
5268 
5269   if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
5270     // The result is the min of all operands results.
5271     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
5272     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
5273       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
5274     return MinOpRes;
5275   }
5276 
5277   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
5278     // The result is the sum of all operands results.
5279     uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
5280     unsigned BitWidth = getTypeSizeInBits(M->getType());
5281     for (unsigned i = 1, e = M->getNumOperands();
5282          SumOpRes != BitWidth && i != e; ++i)
5283       SumOpRes =
5284           std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth);
5285     return SumOpRes;
5286   }
5287 
5288   if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
5289     // The result is the min of all operands results.
5290     uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
5291     for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
5292       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
5293     return MinOpRes;
5294   }
5295 
5296   if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
5297     // The result is the min of all operands results.
5298     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
5299     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
5300       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
5301     return MinOpRes;
5302   }
5303 
5304   if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
5305     // The result is the min of all operands results.
5306     uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
5307     for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
5308       MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
5309     return MinOpRes;
5310   }
5311 
5312   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
5313     // For a SCEVUnknown, ask ValueTracking.
5314     KnownBits Known = computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT);
5315     return Known.countMinTrailingZeros();
5316   }
5317 
5318   // SCEVUDivExpr
5319   return 0;
5320 }
5321 
GetMinTrailingZeros(const SCEV * S)5322 uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
5323   auto I = MinTrailingZerosCache.find(S);
5324   if (I != MinTrailingZerosCache.end())
5325     return I->second;
5326 
5327   uint32_t Result = GetMinTrailingZerosImpl(S);
5328   auto InsertPair = MinTrailingZerosCache.insert({S, Result});
5329   assert(InsertPair.second && "Should insert a new key");
5330   return InsertPair.first->second;
5331 }
5332 
5333 /// Helper method to assign a range to V from metadata present in the IR.
GetRangeFromMetadata(Value * V)5334 static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
5335   if (Instruction *I = dyn_cast<Instruction>(V))
5336     if (MDNode *MD = I->getMetadata(LLVMContext::MD_range))
5337       return getConstantRangeFromMetadata(*MD);
5338 
5339   return None;
5340 }
5341 
5342 /// Determine the range for a particular SCEV.  If SignHint is
5343 /// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
5344 /// with a "cleaner" unsigned (resp. signed) representation.
5345 const ConstantRange &
getRangeRef(const SCEV * S,ScalarEvolution::RangeSignHint SignHint)5346 ScalarEvolution::getRangeRef(const SCEV *S,
5347                              ScalarEvolution::RangeSignHint SignHint) {
5348   DenseMap<const SCEV *, ConstantRange> &Cache =
5349       SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
5350                                                        : SignedRanges;
5351   ConstantRange::PreferredRangeType RangeType =
5352       SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED
5353           ? ConstantRange::Unsigned : ConstantRange::Signed;
5354 
5355   // See if we've computed this range already.
5356   DenseMap<const SCEV *, ConstantRange>::iterator I = Cache.find(S);
5357   if (I != Cache.end())
5358     return I->second;
5359 
5360   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
5361     return setRange(C, SignHint, ConstantRange(C->getAPInt()));
5362 
5363   unsigned BitWidth = getTypeSizeInBits(S->getType());
5364   ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
5365   using OBO = OverflowingBinaryOperator;
5366 
5367   // If the value has known zeros, the maximum value will have those known zeros
5368   // as well.
5369   uint32_t TZ = GetMinTrailingZeros(S);
5370   if (TZ != 0) {
5371     if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
5372       ConservativeResult =
5373           ConstantRange(APInt::getMinValue(BitWidth),
5374                         APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
5375     else
5376       ConservativeResult = ConstantRange(
5377           APInt::getSignedMinValue(BitWidth),
5378           APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
5379   }
5380 
5381   if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
5382     ConstantRange X = getRangeRef(Add->getOperand(0), SignHint);
5383     unsigned WrapType = OBO::AnyWrap;
5384     if (Add->hasNoSignedWrap())
5385       WrapType |= OBO::NoSignedWrap;
5386     if (Add->hasNoUnsignedWrap())
5387       WrapType |= OBO::NoUnsignedWrap;
5388     for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
5389       X = X.addWithNoWrap(getRangeRef(Add->getOperand(i), SignHint),
5390                           WrapType, RangeType);
5391     return setRange(Add, SignHint,
5392                     ConservativeResult.intersectWith(X, RangeType));
5393   }
5394 
5395   if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
5396     ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint);
5397     for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
5398       X = X.multiply(getRangeRef(Mul->getOperand(i), SignHint));
5399     return setRange(Mul, SignHint,
5400                     ConservativeResult.intersectWith(X, RangeType));
5401   }
5402 
5403   if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
5404     ConstantRange X = getRangeRef(SMax->getOperand(0), SignHint);
5405     for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
5406       X = X.smax(getRangeRef(SMax->getOperand(i), SignHint));
5407     return setRange(SMax, SignHint,
5408                     ConservativeResult.intersectWith(X, RangeType));
5409   }
5410 
5411   if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
5412     ConstantRange X = getRangeRef(UMax->getOperand(0), SignHint);
5413     for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
5414       X = X.umax(getRangeRef(UMax->getOperand(i), SignHint));
5415     return setRange(UMax, SignHint,
5416                     ConservativeResult.intersectWith(X, RangeType));
5417   }
5418 
5419   if (const SCEVSMinExpr *SMin = dyn_cast<SCEVSMinExpr>(S)) {
5420     ConstantRange X = getRangeRef(SMin->getOperand(0), SignHint);
5421     for (unsigned i = 1, e = SMin->getNumOperands(); i != e; ++i)
5422       X = X.smin(getRangeRef(SMin->getOperand(i), SignHint));
5423     return setRange(SMin, SignHint,
5424                     ConservativeResult.intersectWith(X, RangeType));
5425   }
5426 
5427   if (const SCEVUMinExpr *UMin = dyn_cast<SCEVUMinExpr>(S)) {
5428     ConstantRange X = getRangeRef(UMin->getOperand(0), SignHint);
5429     for (unsigned i = 1, e = UMin->getNumOperands(); i != e; ++i)
5430       X = X.umin(getRangeRef(UMin->getOperand(i), SignHint));
5431     return setRange(UMin, SignHint,
5432                     ConservativeResult.intersectWith(X, RangeType));
5433   }
5434 
5435   if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
5436     ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint);
5437     ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint);
5438     return setRange(UDiv, SignHint,
5439                     ConservativeResult.intersectWith(X.udiv(Y), RangeType));
5440   }
5441 
5442   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
5443     ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint);
5444     return setRange(ZExt, SignHint,
5445                     ConservativeResult.intersectWith(X.zeroExtend(BitWidth),
5446                                                      RangeType));
5447   }
5448 
5449   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
5450     ConstantRange X = getRangeRef(SExt->getOperand(), SignHint);
5451     return setRange(SExt, SignHint,
5452                     ConservativeResult.intersectWith(X.signExtend(BitWidth),
5453                                                      RangeType));
5454   }
5455 
5456   if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
5457     ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint);
5458     return setRange(Trunc, SignHint,
5459                     ConservativeResult.intersectWith(X.truncate(BitWidth),
5460                                                      RangeType));
5461   }
5462 
5463   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
5464     // If there's no unsigned wrap, the value will never be less than its
5465     // initial value.
5466     if (AddRec->hasNoUnsignedWrap()) {
5467       APInt UnsignedMinValue = getUnsignedRangeMin(AddRec->getStart());
5468       if (!UnsignedMinValue.isNullValue())
5469         ConservativeResult = ConservativeResult.intersectWith(
5470             ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), RangeType);
5471     }
5472 
5473     // If there's no signed wrap, and all the operands except initial value have
5474     // the same sign or zero, the value won't ever be:
5475     // 1: smaller than initial value if operands are non negative,
5476     // 2: bigger than initial value if operands are non positive.
5477     // For both cases, value can not cross signed min/max boundary.
5478     if (AddRec->hasNoSignedWrap()) {
5479       bool AllNonNeg = true;
5480       bool AllNonPos = true;
5481       for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) {
5482         if (!isKnownNonNegative(AddRec->getOperand(i)))
5483           AllNonNeg = false;
5484         if (!isKnownNonPositive(AddRec->getOperand(i)))
5485           AllNonPos = false;
5486       }
5487       if (AllNonNeg)
5488         ConservativeResult = ConservativeResult.intersectWith(
5489             ConstantRange::getNonEmpty(getSignedRangeMin(AddRec->getStart()),
5490                                        APInt::getSignedMinValue(BitWidth)),
5491             RangeType);
5492       else if (AllNonPos)
5493         ConservativeResult = ConservativeResult.intersectWith(
5494             ConstantRange::getNonEmpty(
5495                 APInt::getSignedMinValue(BitWidth),
5496                 getSignedRangeMax(AddRec->getStart()) + 1),
5497             RangeType);
5498     }
5499 
5500     // TODO: non-affine addrec
5501     if (AddRec->isAffine()) {
5502       const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(AddRec->getLoop());
5503       if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
5504           getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
5505         auto RangeFromAffine = getRangeForAffineAR(
5506             AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
5507             BitWidth);
5508         if (!RangeFromAffine.isFullSet())
5509           ConservativeResult =
5510               ConservativeResult.intersectWith(RangeFromAffine, RangeType);
5511 
5512         auto RangeFromFactoring = getRangeViaFactoring(
5513             AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
5514             BitWidth);
5515         if (!RangeFromFactoring.isFullSet())
5516           ConservativeResult =
5517               ConservativeResult.intersectWith(RangeFromFactoring, RangeType);
5518       }
5519     }
5520 
5521     return setRange(AddRec, SignHint, std::move(ConservativeResult));
5522   }
5523 
5524   if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
5525     // Check if the IR explicitly contains !range metadata.
5526     Optional<ConstantRange> MDRange = GetRangeFromMetadata(U->getValue());
5527     if (MDRange.hasValue())
5528       ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue(),
5529                                                             RangeType);
5530 
5531     // Split here to avoid paying the compile-time cost of calling both
5532     // computeKnownBits and ComputeNumSignBits.  This restriction can be lifted
5533     // if needed.
5534     const DataLayout &DL = getDataLayout();
5535     if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
5536       // For a SCEVUnknown, ask ValueTracking.
5537       KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
5538       if (Known.getBitWidth() != BitWidth)
5539         Known = Known.zextOrTrunc(BitWidth);
5540       // If Known does not result in full-set, intersect with it.
5541       if (Known.getMinValue() != Known.getMaxValue() + 1)
5542         ConservativeResult = ConservativeResult.intersectWith(
5543             ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1),
5544             RangeType);
5545     } else {
5546       assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
5547              "generalize as needed!");
5548       unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
5549       // If the pointer size is larger than the index size type, this can cause
5550       // NS to be larger than BitWidth. So compensate for this.
5551       if (U->getType()->isPointerTy()) {
5552         unsigned ptrSize = DL.getPointerTypeSizeInBits(U->getType());
5553         int ptrIdxDiff = ptrSize - BitWidth;
5554         if (ptrIdxDiff > 0 && ptrSize > BitWidth && NS > (unsigned)ptrIdxDiff)
5555           NS -= ptrIdxDiff;
5556       }
5557 
5558       if (NS > 1)
5559         ConservativeResult = ConservativeResult.intersectWith(
5560             ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
5561                           APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1),
5562             RangeType);
5563     }
5564 
5565     // A range of Phi is a subset of union of all ranges of its input.
5566     if (const PHINode *Phi = dyn_cast<PHINode>(U->getValue())) {
5567       // Make sure that we do not run over cycled Phis.
5568       if (PendingPhiRanges.insert(Phi).second) {
5569         ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false);
5570         for (auto &Op : Phi->operands()) {
5571           auto OpRange = getRangeRef(getSCEV(Op), SignHint);
5572           RangeFromOps = RangeFromOps.unionWith(OpRange);
5573           // No point to continue if we already have a full set.
5574           if (RangeFromOps.isFullSet())
5575             break;
5576         }
5577         ConservativeResult =
5578             ConservativeResult.intersectWith(RangeFromOps, RangeType);
5579         bool Erased = PendingPhiRanges.erase(Phi);
5580         assert(Erased && "Failed to erase Phi properly?");
5581         (void) Erased;
5582       }
5583     }
5584 
5585     return setRange(U, SignHint, std::move(ConservativeResult));
5586   }
5587 
5588   return setRange(S, SignHint, std::move(ConservativeResult));
5589 }
5590 
5591 // Given a StartRange, Step and MaxBECount for an expression compute a range of
5592 // values that the expression can take. Initially, the expression has a value
5593 // from StartRange and then is changed by Step up to MaxBECount times. Signed
5594 // argument defines if we treat Step as signed or unsigned.
getRangeForAffineARHelper(APInt Step,const ConstantRange & StartRange,const APInt & MaxBECount,unsigned BitWidth,bool Signed)5595 static ConstantRange getRangeForAffineARHelper(APInt Step,
5596                                                const ConstantRange &StartRange,
5597                                                const APInt &MaxBECount,
5598                                                unsigned BitWidth, bool Signed) {
5599   // If either Step or MaxBECount is 0, then the expression won't change, and we
5600   // just need to return the initial range.
5601   if (Step == 0 || MaxBECount == 0)
5602     return StartRange;
5603 
5604   // If we don't know anything about the initial value (i.e. StartRange is
5605   // FullRange), then we don't know anything about the final range either.
5606   // Return FullRange.
5607   if (StartRange.isFullSet())
5608     return ConstantRange::getFull(BitWidth);
5609 
5610   // If Step is signed and negative, then we use its absolute value, but we also
5611   // note that we're moving in the opposite direction.
5612   bool Descending = Signed && Step.isNegative();
5613 
5614   if (Signed)
5615     // This is correct even for INT_SMIN. Let's look at i8 to illustrate this:
5616     // abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128.
5617     // This equations hold true due to the well-defined wrap-around behavior of
5618     // APInt.
5619     Step = Step.abs();
5620 
5621   // Check if Offset is more than full span of BitWidth. If it is, the
5622   // expression is guaranteed to overflow.
5623   if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount))
5624     return ConstantRange::getFull(BitWidth);
5625 
5626   // Offset is by how much the expression can change. Checks above guarantee no
5627   // overflow here.
5628   APInt Offset = Step * MaxBECount;
5629 
5630   // Minimum value of the final range will match the minimal value of StartRange
5631   // if the expression is increasing and will be decreased by Offset otherwise.
5632   // Maximum value of the final range will match the maximal value of StartRange
5633   // if the expression is decreasing and will be increased by Offset otherwise.
5634   APInt StartLower = StartRange.getLower();
5635   APInt StartUpper = StartRange.getUpper() - 1;
5636   APInt MovedBoundary = Descending ? (StartLower - std::move(Offset))
5637                                    : (StartUpper + std::move(Offset));
5638 
5639   // It's possible that the new minimum/maximum value will fall into the initial
5640   // range (due to wrap around). This means that the expression can take any
5641   // value in this bitwidth, and we have to return full range.
5642   if (StartRange.contains(MovedBoundary))
5643     return ConstantRange::getFull(BitWidth);
5644 
5645   APInt NewLower =
5646       Descending ? std::move(MovedBoundary) : std::move(StartLower);
5647   APInt NewUpper =
5648       Descending ? std::move(StartUpper) : std::move(MovedBoundary);
5649   NewUpper += 1;
5650 
5651   // No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
5652   return ConstantRange::getNonEmpty(std::move(NewLower), std::move(NewUpper));
5653 }
5654 
getRangeForAffineAR(const SCEV * Start,const SCEV * Step,const SCEV * MaxBECount,unsigned BitWidth)5655 ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start,
5656                                                    const SCEV *Step,
5657                                                    const SCEV *MaxBECount,
5658                                                    unsigned BitWidth) {
5659   assert(!isa<SCEVCouldNotCompute>(MaxBECount) &&
5660          getTypeSizeInBits(MaxBECount->getType()) <= BitWidth &&
5661          "Precondition!");
5662 
5663   MaxBECount = getNoopOrZeroExtend(MaxBECount, Start->getType());
5664   APInt MaxBECountValue = getUnsignedRangeMax(MaxBECount);
5665 
5666   // First, consider step signed.
5667   ConstantRange StartSRange = getSignedRange(Start);
5668   ConstantRange StepSRange = getSignedRange(Step);
5669 
5670   // If Step can be both positive and negative, we need to find ranges for the
5671   // maximum absolute step values in both directions and union them.
5672   ConstantRange SR =
5673       getRangeForAffineARHelper(StepSRange.getSignedMin(), StartSRange,
5674                                 MaxBECountValue, BitWidth, /* Signed = */ true);
5675   SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(),
5676                                               StartSRange, MaxBECountValue,
5677                                               BitWidth, /* Signed = */ true));
5678 
5679   // Next, consider step unsigned.
5680   ConstantRange UR = getRangeForAffineARHelper(
5681       getUnsignedRangeMax(Step), getUnsignedRange(Start),
5682       MaxBECountValue, BitWidth, /* Signed = */ false);
5683 
5684   // Finally, intersect signed and unsigned ranges.
5685   return SR.intersectWith(UR, ConstantRange::Smallest);
5686 }
5687 
getRangeViaFactoring(const SCEV * Start,const SCEV * Step,const SCEV * MaxBECount,unsigned BitWidth)5688 ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
5689                                                     const SCEV *Step,
5690                                                     const SCEV *MaxBECount,
5691                                                     unsigned BitWidth) {
5692   //    RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
5693   // == RangeOf({A,+,P}) union RangeOf({B,+,Q})
5694 
5695   struct SelectPattern {
5696     Value *Condition = nullptr;
5697     APInt TrueValue;
5698     APInt FalseValue;
5699 
5700     explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth,
5701                            const SCEV *S) {
5702       Optional<unsigned> CastOp;
5703       APInt Offset(BitWidth, 0);
5704 
5705       assert(SE.getTypeSizeInBits(S->getType()) == BitWidth &&
5706              "Should be!");
5707 
5708       // Peel off a constant offset:
5709       if (auto *SA = dyn_cast<SCEVAddExpr>(S)) {
5710         // In the future we could consider being smarter here and handle
5711         // {Start+Step,+,Step} too.
5712         if (SA->getNumOperands() != 2 || !isa<SCEVConstant>(SA->getOperand(0)))
5713           return;
5714 
5715         Offset = cast<SCEVConstant>(SA->getOperand(0))->getAPInt();
5716         S = SA->getOperand(1);
5717       }
5718 
5719       // Peel off a cast operation
5720       if (auto *SCast = dyn_cast<SCEVCastExpr>(S)) {
5721         CastOp = SCast->getSCEVType();
5722         S = SCast->getOperand();
5723       }
5724 
5725       using namespace llvm::PatternMatch;
5726 
5727       auto *SU = dyn_cast<SCEVUnknown>(S);
5728       const APInt *TrueVal, *FalseVal;
5729       if (!SU ||
5730           !match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal),
5731                                           m_APInt(FalseVal)))) {
5732         Condition = nullptr;
5733         return;
5734       }
5735 
5736       TrueValue = *TrueVal;
5737       FalseValue = *FalseVal;
5738 
5739       // Re-apply the cast we peeled off earlier
5740       if (CastOp.hasValue())
5741         switch (*CastOp) {
5742         default:
5743           llvm_unreachable("Unknown SCEV cast type!");
5744 
5745         case scTruncate:
5746           TrueValue = TrueValue.trunc(BitWidth);
5747           FalseValue = FalseValue.trunc(BitWidth);
5748           break;
5749         case scZeroExtend:
5750           TrueValue = TrueValue.zext(BitWidth);
5751           FalseValue = FalseValue.zext(BitWidth);
5752           break;
5753         case scSignExtend:
5754           TrueValue = TrueValue.sext(BitWidth);
5755           FalseValue = FalseValue.sext(BitWidth);
5756           break;
5757         }
5758 
5759       // Re-apply the constant offset we peeled off earlier
5760       TrueValue += Offset;
5761       FalseValue += Offset;
5762     }
5763 
5764     bool isRecognized() { return Condition != nullptr; }
5765   };
5766 
5767   SelectPattern StartPattern(*this, BitWidth, Start);
5768   if (!StartPattern.isRecognized())
5769     return ConstantRange::getFull(BitWidth);
5770 
5771   SelectPattern StepPattern(*this, BitWidth, Step);
5772   if (!StepPattern.isRecognized())
5773     return ConstantRange::getFull(BitWidth);
5774 
5775   if (StartPattern.Condition != StepPattern.Condition) {
5776     // We don't handle this case today; but we could, by considering four
5777     // possibilities below instead of two. I'm not sure if there are cases where
5778     // that will help over what getRange already does, though.
5779     return ConstantRange::getFull(BitWidth);
5780   }
5781 
5782   // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
5783   // construct arbitrary general SCEV expressions here.  This function is called
5784   // from deep in the call stack, and calling getSCEV (on a sext instruction,
5785   // say) can end up caching a suboptimal value.
5786 
5787   // FIXME: without the explicit `this` receiver below, MSVC errors out with
5788   // C2352 and C2512 (otherwise it isn't needed).
5789 
5790   const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue);
5791   const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue);
5792   const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue);
5793   const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
5794 
5795   ConstantRange TrueRange =
5796       this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth);
5797   ConstantRange FalseRange =
5798       this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth);
5799 
5800   return TrueRange.unionWith(FalseRange);
5801 }
5802 
getNoWrapFlagsFromUB(const Value * V)5803 SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) {
5804   if (isa<ConstantExpr>(V)) return SCEV::FlagAnyWrap;
5805   const BinaryOperator *BinOp = cast<BinaryOperator>(V);
5806 
5807   // Return early if there are no flags to propagate to the SCEV.
5808   SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5809   if (BinOp->hasNoUnsignedWrap())
5810     Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
5811   if (BinOp->hasNoSignedWrap())
5812     Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
5813   if (Flags == SCEV::FlagAnyWrap)
5814     return SCEV::FlagAnyWrap;
5815 
5816   return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap;
5817 }
5818 
isSCEVExprNeverPoison(const Instruction * I)5819 bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
5820   // Here we check that I is in the header of the innermost loop containing I,
5821   // since we only deal with instructions in the loop header. The actual loop we
5822   // need to check later will come from an add recurrence, but getting that
5823   // requires computing the SCEV of the operands, which can be expensive. This
5824   // check we can do cheaply to rule out some cases early.
5825   Loop *InnermostContainingLoop = LI.getLoopFor(I->getParent());
5826   if (InnermostContainingLoop == nullptr ||
5827       InnermostContainingLoop->getHeader() != I->getParent())
5828     return false;
5829 
5830   // Only proceed if we can prove that I does not yield poison.
5831   if (!programUndefinedIfPoison(I))
5832     return false;
5833 
5834   // At this point we know that if I is executed, then it does not wrap
5835   // according to at least one of NSW or NUW. If I is not executed, then we do
5836   // not know if the calculation that I represents would wrap. Multiple
5837   // instructions can map to the same SCEV. If we apply NSW or NUW from I to
5838   // the SCEV, we must guarantee no wrapping for that SCEV also when it is
5839   // derived from other instructions that map to the same SCEV. We cannot make
5840   // that guarantee for cases where I is not executed. So we need to find the
5841   // loop that I is considered in relation to and prove that I is executed for
5842   // every iteration of that loop. That implies that the value that I
5843   // calculates does not wrap anywhere in the loop, so then we can apply the
5844   // flags to the SCEV.
5845   //
5846   // We check isLoopInvariant to disambiguate in case we are adding recurrences
5847   // from different loops, so that we know which loop to prove that I is
5848   // executed in.
5849   for (unsigned OpIndex = 0; OpIndex < I->getNumOperands(); ++OpIndex) {
5850     // I could be an extractvalue from a call to an overflow intrinsic.
5851     // TODO: We can do better here in some cases.
5852     if (!isSCEVable(I->getOperand(OpIndex)->getType()))
5853       return false;
5854     const SCEV *Op = getSCEV(I->getOperand(OpIndex));
5855     if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
5856       bool AllOtherOpsLoopInvariant = true;
5857       for (unsigned OtherOpIndex = 0; OtherOpIndex < I->getNumOperands();
5858            ++OtherOpIndex) {
5859         if (OtherOpIndex != OpIndex) {
5860           const SCEV *OtherOp = getSCEV(I->getOperand(OtherOpIndex));
5861           if (!isLoopInvariant(OtherOp, AddRec->getLoop())) {
5862             AllOtherOpsLoopInvariant = false;
5863             break;
5864           }
5865         }
5866       }
5867       if (AllOtherOpsLoopInvariant &&
5868           isGuaranteedToExecuteForEveryIteration(I, AddRec->getLoop()))
5869         return true;
5870     }
5871   }
5872   return false;
5873 }
5874 
isAddRecNeverPoison(const Instruction * I,const Loop * L)5875 bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) {
5876   // If we know that \c I can never be poison period, then that's enough.
5877   if (isSCEVExprNeverPoison(I))
5878     return true;
5879 
5880   // For an add recurrence specifically, we assume that infinite loops without
5881   // side effects are undefined behavior, and then reason as follows:
5882   //
5883   // If the add recurrence is poison in any iteration, it is poison on all
5884   // future iterations (since incrementing poison yields poison). If the result
5885   // of the add recurrence is fed into the loop latch condition and the loop
5886   // does not contain any throws or exiting blocks other than the latch, we now
5887   // have the ability to "choose" whether the backedge is taken or not (by
5888   // choosing a sufficiently evil value for the poison feeding into the branch)
5889   // for every iteration including and after the one in which \p I first became
5890   // poison.  There are two possibilities (let's call the iteration in which \p
5891   // I first became poison as K):
5892   //
5893   //  1. In the set of iterations including and after K, the loop body executes
5894   //     no side effects.  In this case executing the backege an infinte number
5895   //     of times will yield undefined behavior.
5896   //
5897   //  2. In the set of iterations including and after K, the loop body executes
5898   //     at least one side effect.  In this case, that specific instance of side
5899   //     effect is control dependent on poison, which also yields undefined
5900   //     behavior.
5901 
5902   auto *ExitingBB = L->getExitingBlock();
5903   auto *LatchBB = L->getLoopLatch();
5904   if (!ExitingBB || !LatchBB || ExitingBB != LatchBB)
5905     return false;
5906 
5907   SmallPtrSet<const Instruction *, 16> Pushed;
5908   SmallVector<const Instruction *, 8> PoisonStack;
5909 
5910   // We start by assuming \c I, the post-inc add recurrence, is poison.  Only
5911   // things that are known to be poison under that assumption go on the
5912   // PoisonStack.
5913   Pushed.insert(I);
5914   PoisonStack.push_back(I);
5915 
5916   bool LatchControlDependentOnPoison = false;
5917   while (!PoisonStack.empty() && !LatchControlDependentOnPoison) {
5918     const Instruction *Poison = PoisonStack.pop_back_val();
5919 
5920     for (auto *PoisonUser : Poison->users()) {
5921       if (propagatesPoison(cast<Instruction>(PoisonUser))) {
5922         if (Pushed.insert(cast<Instruction>(PoisonUser)).second)
5923           PoisonStack.push_back(cast<Instruction>(PoisonUser));
5924       } else if (auto *BI = dyn_cast<BranchInst>(PoisonUser)) {
5925         assert(BI->isConditional() && "Only possibility!");
5926         if (BI->getParent() == LatchBB) {
5927           LatchControlDependentOnPoison = true;
5928           break;
5929         }
5930       }
5931     }
5932   }
5933 
5934   return LatchControlDependentOnPoison && loopHasNoAbnormalExits(L);
5935 }
5936 
5937 ScalarEvolution::LoopProperties
getLoopProperties(const Loop * L)5938 ScalarEvolution::getLoopProperties(const Loop *L) {
5939   using LoopProperties = ScalarEvolution::LoopProperties;
5940 
5941   auto Itr = LoopPropertiesCache.find(L);
5942   if (Itr == LoopPropertiesCache.end()) {
5943     auto HasSideEffects = [](Instruction *I) {
5944       if (auto *SI = dyn_cast<StoreInst>(I))
5945         return !SI->isSimple();
5946 
5947       return I->mayHaveSideEffects();
5948     };
5949 
5950     LoopProperties LP = {/* HasNoAbnormalExits */ true,
5951                          /*HasNoSideEffects*/ true};
5952 
5953     for (auto *BB : L->getBlocks())
5954       for (auto &I : *BB) {
5955         if (!isGuaranteedToTransferExecutionToSuccessor(&I))
5956           LP.HasNoAbnormalExits = false;
5957         if (HasSideEffects(&I))
5958           LP.HasNoSideEffects = false;
5959         if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects)
5960           break; // We're already as pessimistic as we can get.
5961       }
5962 
5963     auto InsertPair = LoopPropertiesCache.insert({L, LP});
5964     assert(InsertPair.second && "We just checked!");
5965     Itr = InsertPair.first;
5966   }
5967 
5968   return Itr->second;
5969 }
5970 
createSCEV(Value * V)5971 const SCEV *ScalarEvolution::createSCEV(Value *V) {
5972   if (!isSCEVable(V->getType()))
5973     return getUnknown(V);
5974 
5975   if (Instruction *I = dyn_cast<Instruction>(V)) {
5976     // Don't attempt to analyze instructions in blocks that aren't
5977     // reachable. Such instructions don't matter, and they aren't required
5978     // to obey basic rules for definitions dominating uses which this
5979     // analysis depends on.
5980     if (!DT.isReachableFromEntry(I->getParent()))
5981       return getUnknown(UndefValue::get(V->getType()));
5982   } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
5983     return getConstant(CI);
5984   else if (isa<ConstantPointerNull>(V))
5985     return getZero(V->getType());
5986   else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
5987     return GA->isInterposable() ? getUnknown(V) : getSCEV(GA->getAliasee());
5988   else if (!isa<ConstantExpr>(V))
5989     return getUnknown(V);
5990 
5991   Operator *U = cast<Operator>(V);
5992   if (auto BO = MatchBinaryOp(U, DT)) {
5993     switch (BO->Opcode) {
5994     case Instruction::Add: {
5995       // The simple thing to do would be to just call getSCEV on both operands
5996       // and call getAddExpr with the result. However if we're looking at a
5997       // bunch of things all added together, this can be quite inefficient,
5998       // because it leads to N-1 getAddExpr calls for N ultimate operands.
5999       // Instead, gather up all the operands and make a single getAddExpr call.
6000       // LLVM IR canonical form means we need only traverse the left operands.
6001       SmallVector<const SCEV *, 4> AddOps;
6002       do {
6003         if (BO->Op) {
6004           if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
6005             AddOps.push_back(OpSCEV);
6006             break;
6007           }
6008 
6009           // If a NUW or NSW flag can be applied to the SCEV for this
6010           // addition, then compute the SCEV for this addition by itself
6011           // with a separate call to getAddExpr. We need to do that
6012           // instead of pushing the operands of the addition onto AddOps,
6013           // since the flags are only known to apply to this particular
6014           // addition - they may not apply to other additions that can be
6015           // formed with operands from AddOps.
6016           const SCEV *RHS = getSCEV(BO->RHS);
6017           SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
6018           if (Flags != SCEV::FlagAnyWrap) {
6019             const SCEV *LHS = getSCEV(BO->LHS);
6020             if (BO->Opcode == Instruction::Sub)
6021               AddOps.push_back(getMinusSCEV(LHS, RHS, Flags));
6022             else
6023               AddOps.push_back(getAddExpr(LHS, RHS, Flags));
6024             break;
6025           }
6026         }
6027 
6028         if (BO->Opcode == Instruction::Sub)
6029           AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS)));
6030         else
6031           AddOps.push_back(getSCEV(BO->RHS));
6032 
6033         auto NewBO = MatchBinaryOp(BO->LHS, DT);
6034         if (!NewBO || (NewBO->Opcode != Instruction::Add &&
6035                        NewBO->Opcode != Instruction::Sub)) {
6036           AddOps.push_back(getSCEV(BO->LHS));
6037           break;
6038         }
6039         BO = NewBO;
6040       } while (true);
6041 
6042       return getAddExpr(AddOps);
6043     }
6044 
6045     case Instruction::Mul: {
6046       SmallVector<const SCEV *, 4> MulOps;
6047       do {
6048         if (BO->Op) {
6049           if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
6050             MulOps.push_back(OpSCEV);
6051             break;
6052           }
6053 
6054           SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
6055           if (Flags != SCEV::FlagAnyWrap) {
6056             MulOps.push_back(
6057                 getMulExpr(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags));
6058             break;
6059           }
6060         }
6061 
6062         MulOps.push_back(getSCEV(BO->RHS));
6063         auto NewBO = MatchBinaryOp(BO->LHS, DT);
6064         if (!NewBO || NewBO->Opcode != Instruction::Mul) {
6065           MulOps.push_back(getSCEV(BO->LHS));
6066           break;
6067         }
6068         BO = NewBO;
6069       } while (true);
6070 
6071       return getMulExpr(MulOps);
6072     }
6073     case Instruction::UDiv:
6074       return getUDivExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
6075     case Instruction::URem:
6076       return getURemExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
6077     case Instruction::Sub: {
6078       SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
6079       if (BO->Op)
6080         Flags = getNoWrapFlagsFromUB(BO->Op);
6081       return getMinusSCEV(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags);
6082     }
6083     case Instruction::And:
6084       // For an expression like x&255 that merely masks off the high bits,
6085       // use zext(trunc(x)) as the SCEV expression.
6086       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6087         if (CI->isZero())
6088           return getSCEV(BO->RHS);
6089         if (CI->isMinusOne())
6090           return getSCEV(BO->LHS);
6091         const APInt &A = CI->getValue();
6092 
6093         // Instcombine's ShrinkDemandedConstant may strip bits out of
6094         // constants, obscuring what would otherwise be a low-bits mask.
6095         // Use computeKnownBits to compute what ShrinkDemandedConstant
6096         // knew about to reconstruct a low-bits mask value.
6097         unsigned LZ = A.countLeadingZeros();
6098         unsigned TZ = A.countTrailingZeros();
6099         unsigned BitWidth = A.getBitWidth();
6100         KnownBits Known(BitWidth);
6101         computeKnownBits(BO->LHS, Known, getDataLayout(),
6102                          0, &AC, nullptr, &DT);
6103 
6104         APInt EffectiveMask =
6105             APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ);
6106         if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) {
6107           const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ));
6108           const SCEV *LHS = getSCEV(BO->LHS);
6109           const SCEV *ShiftedLHS = nullptr;
6110           if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) {
6111             if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) {
6112               // For an expression like (x * 8) & 8, simplify the multiply.
6113               unsigned MulZeros = OpC->getAPInt().countTrailingZeros();
6114               unsigned GCD = std::min(MulZeros, TZ);
6115               APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD);
6116               SmallVector<const SCEV*, 4> MulOps;
6117               MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD)));
6118               MulOps.append(LHSMul->op_begin() + 1, LHSMul->op_end());
6119               auto *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags());
6120               ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt));
6121             }
6122           }
6123           if (!ShiftedLHS)
6124             ShiftedLHS = getUDivExpr(LHS, MulCount);
6125           return getMulExpr(
6126               getZeroExtendExpr(
6127                   getTruncateExpr(ShiftedLHS,
6128                       IntegerType::get(getContext(), BitWidth - LZ - TZ)),
6129                   BO->LHS->getType()),
6130               MulCount);
6131         }
6132       }
6133       break;
6134 
6135     case Instruction::Or:
6136       // If the RHS of the Or is a constant, we may have something like:
6137       // X*4+1 which got turned into X*4|1.  Handle this as an Add so loop
6138       // optimizations will transparently handle this case.
6139       //
6140       // In order for this transformation to be safe, the LHS must be of the
6141       // form X*(2^n) and the Or constant must be less than 2^n.
6142       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6143         const SCEV *LHS = getSCEV(BO->LHS);
6144         const APInt &CIVal = CI->getValue();
6145         if (GetMinTrailingZeros(LHS) >=
6146             (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
6147           // Build a plain add SCEV.
6148           return getAddExpr(LHS, getSCEV(CI),
6149                             (SCEV::NoWrapFlags)(SCEV::FlagNUW | SCEV::FlagNSW));
6150         }
6151       }
6152       break;
6153 
6154     case Instruction::Xor:
6155       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6156         // If the RHS of xor is -1, then this is a not operation.
6157         if (CI->isMinusOne())
6158           return getNotSCEV(getSCEV(BO->LHS));
6159 
6160         // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
6161         // This is a variant of the check for xor with -1, and it handles
6162         // the case where instcombine has trimmed non-demanded bits out
6163         // of an xor with -1.
6164         if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS))
6165           if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1)))
6166             if (LBO->getOpcode() == Instruction::And &&
6167                 LCI->getValue() == CI->getValue())
6168               if (const SCEVZeroExtendExpr *Z =
6169                       dyn_cast<SCEVZeroExtendExpr>(getSCEV(BO->LHS))) {
6170                 Type *UTy = BO->LHS->getType();
6171                 const SCEV *Z0 = Z->getOperand();
6172                 Type *Z0Ty = Z0->getType();
6173                 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
6174 
6175                 // If C is a low-bits mask, the zero extend is serving to
6176                 // mask off the high bits. Complement the operand and
6177                 // re-apply the zext.
6178                 if (CI->getValue().isMask(Z0TySize))
6179                   return getZeroExtendExpr(getNotSCEV(Z0), UTy);
6180 
6181                 // If C is a single bit, it may be in the sign-bit position
6182                 // before the zero-extend. In this case, represent the xor
6183                 // using an add, which is equivalent, and re-apply the zext.
6184                 APInt Trunc = CI->getValue().trunc(Z0TySize);
6185                 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
6186                     Trunc.isSignMask())
6187                   return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
6188                                            UTy);
6189               }
6190       }
6191       break;
6192 
6193     case Instruction::Shl:
6194       // Turn shift left of a constant amount into a multiply.
6195       if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) {
6196         uint32_t BitWidth = cast<IntegerType>(SA->getType())->getBitWidth();
6197 
6198         // If the shift count is not less than the bitwidth, the result of
6199         // the shift is undefined. Don't try to analyze it, because the
6200         // resolution chosen here may differ from the resolution chosen in
6201         // other parts of the compiler.
6202         if (SA->getValue().uge(BitWidth))
6203           break;
6204 
6205         // We can safely preserve the nuw flag in all cases. It's also safe to
6206         // turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation
6207         // requires special handling. It can be preserved as long as we're not
6208         // left shifting by bitwidth - 1.
6209         auto Flags = SCEV::FlagAnyWrap;
6210         if (BO->Op) {
6211           auto MulFlags = getNoWrapFlagsFromUB(BO->Op);
6212           if ((MulFlags & SCEV::FlagNSW) &&
6213               ((MulFlags & SCEV::FlagNUW) || SA->getValue().ult(BitWidth - 1)))
6214             Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNSW);
6215           if (MulFlags & SCEV::FlagNUW)
6216             Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNUW);
6217         }
6218 
6219         Constant *X = ConstantInt::get(
6220             getContext(), APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
6221         return getMulExpr(getSCEV(BO->LHS), getSCEV(X), Flags);
6222       }
6223       break;
6224 
6225     case Instruction::AShr: {
6226       // AShr X, C, where C is a constant.
6227       ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS);
6228       if (!CI)
6229         break;
6230 
6231       Type *OuterTy = BO->LHS->getType();
6232       uint64_t BitWidth = getTypeSizeInBits(OuterTy);
6233       // If the shift count is not less than the bitwidth, the result of
6234       // the shift is undefined. Don't try to analyze it, because the
6235       // resolution chosen here may differ from the resolution chosen in
6236       // other parts of the compiler.
6237       if (CI->getValue().uge(BitWidth))
6238         break;
6239 
6240       if (CI->isZero())
6241         return getSCEV(BO->LHS); // shift by zero --> noop
6242 
6243       uint64_t AShrAmt = CI->getZExtValue();
6244       Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt);
6245 
6246       Operator *L = dyn_cast<Operator>(BO->LHS);
6247       if (L && L->getOpcode() == Instruction::Shl) {
6248         // X = Shl A, n
6249         // Y = AShr X, m
6250         // Both n and m are constant.
6251 
6252         const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0));
6253         if (L->getOperand(1) == BO->RHS)
6254           // For a two-shift sext-inreg, i.e. n = m,
6255           // use sext(trunc(x)) as the SCEV expression.
6256           return getSignExtendExpr(
6257               getTruncateExpr(ShlOp0SCEV, TruncTy), OuterTy);
6258 
6259         ConstantInt *ShlAmtCI = dyn_cast<ConstantInt>(L->getOperand(1));
6260         if (ShlAmtCI && ShlAmtCI->getValue().ult(BitWidth)) {
6261           uint64_t ShlAmt = ShlAmtCI->getZExtValue();
6262           if (ShlAmt > AShrAmt) {
6263             // When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV
6264             // expression. We already checked that ShlAmt < BitWidth, so
6265             // the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as
6266             // ShlAmt - AShrAmt < Amt.
6267             APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt,
6268                                             ShlAmt - AShrAmt);
6269             return getSignExtendExpr(
6270                 getMulExpr(getTruncateExpr(ShlOp0SCEV, TruncTy),
6271                 getConstant(Mul)), OuterTy);
6272           }
6273         }
6274       }
6275       break;
6276     }
6277     }
6278   }
6279 
6280   switch (U->getOpcode()) {
6281   case Instruction::Trunc:
6282     return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
6283 
6284   case Instruction::ZExt:
6285     return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
6286 
6287   case Instruction::SExt:
6288     if (auto BO = MatchBinaryOp(U->getOperand(0), DT)) {
6289       // The NSW flag of a subtract does not always survive the conversion to
6290       // A + (-1)*B.  By pushing sign extension onto its operands we are much
6291       // more likely to preserve NSW and allow later AddRec optimisations.
6292       //
6293       // NOTE: This is effectively duplicating this logic from getSignExtend:
6294       //   sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
6295       // but by that point the NSW information has potentially been lost.
6296       if (BO->Opcode == Instruction::Sub && BO->IsNSW) {
6297         Type *Ty = U->getType();
6298         auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty);
6299         auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty);
6300         return getMinusSCEV(V1, V2, SCEV::FlagNSW);
6301       }
6302     }
6303     return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
6304 
6305   case Instruction::BitCast:
6306     // BitCasts are no-op casts so we just eliminate the cast.
6307     if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
6308       return getSCEV(U->getOperand(0));
6309     break;
6310 
6311   case Instruction::SDiv:
6312     // If both operands are non-negative, this is just an udiv.
6313     if (isKnownNonNegative(getSCEV(U->getOperand(0))) &&
6314         isKnownNonNegative(getSCEV(U->getOperand(1))))
6315       return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1)));
6316     break;
6317 
6318   case Instruction::SRem:
6319     // If both operands are non-negative, this is just an urem.
6320     if (isKnownNonNegative(getSCEV(U->getOperand(0))) &&
6321         isKnownNonNegative(getSCEV(U->getOperand(1))))
6322       return getURemExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1)));
6323     break;
6324 
6325   // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can
6326   // lead to pointer expressions which cannot safely be expanded to GEPs,
6327   // because ScalarEvolution doesn't respect the GEP aliasing rules when
6328   // simplifying integer expressions.
6329 
6330   case Instruction::GetElementPtr:
6331     return createNodeForGEP(cast<GEPOperator>(U));
6332 
6333   case Instruction::PHI:
6334     return createNodeForPHI(cast<PHINode>(U));
6335 
6336   case Instruction::Select:
6337     // U can also be a select constant expr, which let fall through.  Since
6338     // createNodeForSelect only works for a condition that is an `ICmpInst`, and
6339     // constant expressions cannot have instructions as operands, we'd have
6340     // returned getUnknown for a select constant expressions anyway.
6341     if (isa<Instruction>(U))
6342       return createNodeForSelectOrPHI(cast<Instruction>(U), U->getOperand(0),
6343                                       U->getOperand(1), U->getOperand(2));
6344     break;
6345 
6346   case Instruction::Call:
6347   case Instruction::Invoke:
6348     if (Value *RV = cast<CallBase>(U)->getReturnedArgOperand())
6349       return getSCEV(RV);
6350     break;
6351   }
6352 
6353   return getUnknown(V);
6354 }
6355 
6356 //===----------------------------------------------------------------------===//
6357 //                   Iteration Count Computation Code
6358 //
6359 
getConstantTripCount(const SCEVConstant * ExitCount)6360 static unsigned getConstantTripCount(const SCEVConstant *ExitCount) {
6361   if (!ExitCount)
6362     return 0;
6363 
6364   ConstantInt *ExitConst = ExitCount->getValue();
6365 
6366   // Guard against huge trip counts.
6367   if (ExitConst->getValue().getActiveBits() > 32)
6368     return 0;
6369 
6370   // In case of integer overflow, this returns 0, which is correct.
6371   return ((unsigned)ExitConst->getZExtValue()) + 1;
6372 }
6373 
getSmallConstantTripCount(const Loop * L)6374 unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) {
6375   if (BasicBlock *ExitingBB = L->getExitingBlock())
6376     return getSmallConstantTripCount(L, ExitingBB);
6377 
6378   // No trip count information for multiple exits.
6379   return 0;
6380 }
6381 
getSmallConstantTripCount(const Loop * L,BasicBlock * ExitingBlock)6382 unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L,
6383                                                     BasicBlock *ExitingBlock) {
6384   assert(ExitingBlock && "Must pass a non-null exiting block!");
6385   assert(L->isLoopExiting(ExitingBlock) &&
6386          "Exiting block must actually branch out of the loop!");
6387   const SCEVConstant *ExitCount =
6388       dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock));
6389   return getConstantTripCount(ExitCount);
6390 }
6391 
getSmallConstantMaxTripCount(const Loop * L)6392 unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) {
6393   const auto *MaxExitCount =
6394       dyn_cast<SCEVConstant>(getConstantMaxBackedgeTakenCount(L));
6395   return getConstantTripCount(MaxExitCount);
6396 }
6397 
getSmallConstantTripMultiple(const Loop * L)6398 unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) {
6399   if (BasicBlock *ExitingBB = L->getExitingBlock())
6400     return getSmallConstantTripMultiple(L, ExitingBB);
6401 
6402   // No trip multiple information for multiple exits.
6403   return 0;
6404 }
6405 
6406 /// Returns the largest constant divisor of the trip count of this loop as a
6407 /// normal unsigned value, if possible. This means that the actual trip count is
6408 /// always a multiple of the returned value (don't forget the trip count could
6409 /// very well be zero as well!).
6410 ///
6411 /// Returns 1 if the trip count is unknown or not guaranteed to be the
6412 /// multiple of a constant (which is also the case if the trip count is simply
6413 /// constant, use getSmallConstantTripCount for that case), Will also return 1
6414 /// if the trip count is very large (>= 2^32).
6415 ///
6416 /// As explained in the comments for getSmallConstantTripCount, this assumes
6417 /// that control exits the loop via ExitingBlock.
6418 unsigned
getSmallConstantTripMultiple(const Loop * L,BasicBlock * ExitingBlock)6419 ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
6420                                               BasicBlock *ExitingBlock) {
6421   assert(ExitingBlock && "Must pass a non-null exiting block!");
6422   assert(L->isLoopExiting(ExitingBlock) &&
6423          "Exiting block must actually branch out of the loop!");
6424   const SCEV *ExitCount = getExitCount(L, ExitingBlock);
6425   if (ExitCount == getCouldNotCompute())
6426     return 1;
6427 
6428   // Get the trip count from the BE count by adding 1.
6429   const SCEV *TCExpr = getAddExpr(ExitCount, getOne(ExitCount->getType()));
6430 
6431   const SCEVConstant *TC = dyn_cast<SCEVConstant>(TCExpr);
6432   if (!TC)
6433     // Attempt to factor more general cases. Returns the greatest power of
6434     // two divisor. If overflow happens, the trip count expression is still
6435     // divisible by the greatest power of 2 divisor returned.
6436     return 1U << std::min((uint32_t)31, GetMinTrailingZeros(TCExpr));
6437 
6438   ConstantInt *Result = TC->getValue();
6439 
6440   // Guard against huge trip counts (this requires checking
6441   // for zero to handle the case where the trip count == -1 and the
6442   // addition wraps).
6443   if (!Result || Result->getValue().getActiveBits() > 32 ||
6444       Result->getValue().getActiveBits() == 0)
6445     return 1;
6446 
6447   return (unsigned)Result->getZExtValue();
6448 }
6449 
getExitCount(const Loop * L,BasicBlock * ExitingBlock,ExitCountKind Kind)6450 const SCEV *ScalarEvolution::getExitCount(const Loop *L,
6451                                           BasicBlock *ExitingBlock,
6452                                           ExitCountKind Kind) {
6453   switch (Kind) {
6454   case Exact:
6455     return getBackedgeTakenInfo(L).getExact(ExitingBlock, this);
6456   case ConstantMaximum:
6457     return getBackedgeTakenInfo(L).getMax(ExitingBlock, this);
6458   };
6459   llvm_unreachable("Invalid ExitCountKind!");
6460 }
6461 
6462 const SCEV *
getPredicatedBackedgeTakenCount(const Loop * L,SCEVUnionPredicate & Preds)6463 ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L,
6464                                                  SCEVUnionPredicate &Preds) {
6465   return getPredicatedBackedgeTakenInfo(L).getExact(L, this, &Preds);
6466 }
6467 
getBackedgeTakenCount(const Loop * L,ExitCountKind Kind)6468 const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L,
6469                                                    ExitCountKind Kind) {
6470   switch (Kind) {
6471   case Exact:
6472     return getBackedgeTakenInfo(L).getExact(L, this);
6473   case ConstantMaximum:
6474     return getBackedgeTakenInfo(L).getMax(this);
6475   };
6476   llvm_unreachable("Invalid ExitCountKind!");
6477 }
6478 
isBackedgeTakenCountMaxOrZero(const Loop * L)6479 bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) {
6480   return getBackedgeTakenInfo(L).isMaxOrZero(this);
6481 }
6482 
6483 /// Push PHI nodes in the header of the given loop onto the given Worklist.
6484 static void
PushLoopPHIs(const Loop * L,SmallVectorImpl<Instruction * > & Worklist)6485 PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
6486   BasicBlock *Header = L->getHeader();
6487 
6488   // Push all Loop-header PHIs onto the Worklist stack.
6489   for (PHINode &PN : Header->phis())
6490     Worklist.push_back(&PN);
6491 }
6492 
6493 const ScalarEvolution::BackedgeTakenInfo &
getPredicatedBackedgeTakenInfo(const Loop * L)6494 ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) {
6495   auto &BTI = getBackedgeTakenInfo(L);
6496   if (BTI.hasFullInfo())
6497     return BTI;
6498 
6499   auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
6500 
6501   if (!Pair.second)
6502     return Pair.first->second;
6503 
6504   BackedgeTakenInfo Result =
6505       computeBackedgeTakenCount(L, /*AllowPredicates=*/true);
6506 
6507   return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result);
6508 }
6509 
6510 const ScalarEvolution::BackedgeTakenInfo &
getBackedgeTakenInfo(const Loop * L)6511 ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
6512   // Initially insert an invalid entry for this loop. If the insertion
6513   // succeeds, proceed to actually compute a backedge-taken count and
6514   // update the value. The temporary CouldNotCompute value tells SCEV
6515   // code elsewhere that it shouldn't attempt to request a new
6516   // backedge-taken count, which could result in infinite recursion.
6517   std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
6518       BackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
6519   if (!Pair.second)
6520     return Pair.first->second;
6521 
6522   // computeBackedgeTakenCount may allocate memory for its result. Inserting it
6523   // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
6524   // must be cleared in this scope.
6525   BackedgeTakenInfo Result = computeBackedgeTakenCount(L);
6526 
6527   // In product build, there are no usage of statistic.
6528   (void)NumTripCountsComputed;
6529   (void)NumTripCountsNotComputed;
6530 #if LLVM_ENABLE_STATS || !defined(NDEBUG)
6531   const SCEV *BEExact = Result.getExact(L, this);
6532   if (BEExact != getCouldNotCompute()) {
6533     assert(isLoopInvariant(BEExact, L) &&
6534            isLoopInvariant(Result.getMax(this), L) &&
6535            "Computed backedge-taken count isn't loop invariant for loop!");
6536     ++NumTripCountsComputed;
6537   }
6538   else if (Result.getMax(this) == getCouldNotCompute() &&
6539            isa<PHINode>(L->getHeader()->begin())) {
6540     // Only count loops that have phi nodes as not being computable.
6541     ++NumTripCountsNotComputed;
6542   }
6543 #endif // LLVM_ENABLE_STATS || !defined(NDEBUG)
6544 
6545   // Now that we know more about the trip count for this loop, forget any
6546   // existing SCEV values for PHI nodes in this loop since they are only
6547   // conservative estimates made without the benefit of trip count
6548   // information. This is similar to the code in forgetLoop, except that
6549   // it handles SCEVUnknown PHI nodes specially.
6550   if (Result.hasAnyInfo()) {
6551     SmallVector<Instruction *, 16> Worklist;
6552     PushLoopPHIs(L, Worklist);
6553 
6554     SmallPtrSet<Instruction *, 8> Discovered;
6555     while (!Worklist.empty()) {
6556       Instruction *I = Worklist.pop_back_val();
6557 
6558       ValueExprMapType::iterator It =
6559         ValueExprMap.find_as(static_cast<Value *>(I));
6560       if (It != ValueExprMap.end()) {
6561         const SCEV *Old = It->second;
6562 
6563         // SCEVUnknown for a PHI either means that it has an unrecognized
6564         // structure, or it's a PHI that's in the progress of being computed
6565         // by createNodeForPHI.  In the former case, additional loop trip
6566         // count information isn't going to change anything. In the later
6567         // case, createNodeForPHI will perform the necessary updates on its
6568         // own when it gets to that point.
6569         if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) {
6570           eraseValueFromMap(It->first);
6571           forgetMemoizedResults(Old);
6572         }
6573         if (PHINode *PN = dyn_cast<PHINode>(I))
6574           ConstantEvolutionLoopExitValue.erase(PN);
6575       }
6576 
6577       // Since we don't need to invalidate anything for correctness and we're
6578       // only invalidating to make SCEV's results more precise, we get to stop
6579       // early to avoid invalidating too much.  This is especially important in
6580       // cases like:
6581       //
6582       //   %v = f(pn0, pn1) // pn0 and pn1 used through some other phi node
6583       // loop0:
6584       //   %pn0 = phi
6585       //   ...
6586       // loop1:
6587       //   %pn1 = phi
6588       //   ...
6589       //
6590       // where both loop0 and loop1's backedge taken count uses the SCEV
6591       // expression for %v.  If we don't have the early stop below then in cases
6592       // like the above, getBackedgeTakenInfo(loop1) will clear out the trip
6593       // count for loop0 and getBackedgeTakenInfo(loop0) will clear out the trip
6594       // count for loop1, effectively nullifying SCEV's trip count cache.
6595       for (auto *U : I->users())
6596         if (auto *I = dyn_cast<Instruction>(U)) {
6597           auto *LoopForUser = LI.getLoopFor(I->getParent());
6598           if (LoopForUser && L->contains(LoopForUser) &&
6599               Discovered.insert(I).second)
6600             Worklist.push_back(I);
6601         }
6602     }
6603   }
6604 
6605   // Re-lookup the insert position, since the call to
6606   // computeBackedgeTakenCount above could result in a
6607   // recusive call to getBackedgeTakenInfo (on a different
6608   // loop), which would invalidate the iterator computed
6609   // earlier.
6610   return BackedgeTakenCounts.find(L)->second = std::move(Result);
6611 }
6612 
forgetAllLoops()6613 void ScalarEvolution::forgetAllLoops() {
6614   // This method is intended to forget all info about loops. It should
6615   // invalidate caches as if the following happened:
6616   // - The trip counts of all loops have changed arbitrarily
6617   // - Every llvm::Value has been updated in place to produce a different
6618   // result.
6619   BackedgeTakenCounts.clear();
6620   PredicatedBackedgeTakenCounts.clear();
6621   LoopPropertiesCache.clear();
6622   ConstantEvolutionLoopExitValue.clear();
6623   ValueExprMap.clear();
6624   ValuesAtScopes.clear();
6625   LoopDispositions.clear();
6626   BlockDispositions.clear();
6627   UnsignedRanges.clear();
6628   SignedRanges.clear();
6629   ExprValueMap.clear();
6630   HasRecMap.clear();
6631   MinTrailingZerosCache.clear();
6632   PredicatedSCEVRewrites.clear();
6633 }
6634 
forgetLoop(const Loop * L)6635 void ScalarEvolution::forgetLoop(const Loop *L) {
6636   // Drop any stored trip count value.
6637   auto RemoveLoopFromBackedgeMap =
6638       [](DenseMap<const Loop *, BackedgeTakenInfo> &Map, const Loop *L) {
6639         auto BTCPos = Map.find(L);
6640         if (BTCPos != Map.end()) {
6641           BTCPos->second.clear();
6642           Map.erase(BTCPos);
6643         }
6644       };
6645 
6646   SmallVector<const Loop *, 16> LoopWorklist(1, L);
6647   SmallVector<Instruction *, 32> Worklist;
6648   SmallPtrSet<Instruction *, 16> Visited;
6649 
6650   // Iterate over all the loops and sub-loops to drop SCEV information.
6651   while (!LoopWorklist.empty()) {
6652     auto *CurrL = LoopWorklist.pop_back_val();
6653 
6654     RemoveLoopFromBackedgeMap(BackedgeTakenCounts, CurrL);
6655     RemoveLoopFromBackedgeMap(PredicatedBackedgeTakenCounts, CurrL);
6656 
6657     // Drop information about predicated SCEV rewrites for this loop.
6658     for (auto I = PredicatedSCEVRewrites.begin();
6659          I != PredicatedSCEVRewrites.end();) {
6660       std::pair<const SCEV *, const Loop *> Entry = I->first;
6661       if (Entry.second == CurrL)
6662         PredicatedSCEVRewrites.erase(I++);
6663       else
6664         ++I;
6665     }
6666 
6667     auto LoopUsersItr = LoopUsers.find(CurrL);
6668     if (LoopUsersItr != LoopUsers.end()) {
6669       for (auto *S : LoopUsersItr->second)
6670         forgetMemoizedResults(S);
6671       LoopUsers.erase(LoopUsersItr);
6672     }
6673 
6674     // Drop information about expressions based on loop-header PHIs.
6675     PushLoopPHIs(CurrL, Worklist);
6676 
6677     while (!Worklist.empty()) {
6678       Instruction *I = Worklist.pop_back_val();
6679       if (!Visited.insert(I).second)
6680         continue;
6681 
6682       ValueExprMapType::iterator It =
6683           ValueExprMap.find_as(static_cast<Value *>(I));
6684       if (It != ValueExprMap.end()) {
6685         eraseValueFromMap(It->first);
6686         forgetMemoizedResults(It->second);
6687         if (PHINode *PN = dyn_cast<PHINode>(I))
6688           ConstantEvolutionLoopExitValue.erase(PN);
6689       }
6690 
6691       PushDefUseChildren(I, Worklist);
6692     }
6693 
6694     LoopPropertiesCache.erase(CurrL);
6695     // Forget all contained loops too, to avoid dangling entries in the
6696     // ValuesAtScopes map.
6697     LoopWorklist.append(CurrL->begin(), CurrL->end());
6698   }
6699 }
6700 
forgetTopmostLoop(const Loop * L)6701 void ScalarEvolution::forgetTopmostLoop(const Loop *L) {
6702   while (Loop *Parent = L->getParentLoop())
6703     L = Parent;
6704   forgetLoop(L);
6705 }
6706 
forgetValue(Value * V)6707 void ScalarEvolution::forgetValue(Value *V) {
6708   Instruction *I = dyn_cast<Instruction>(V);
6709   if (!I) return;
6710 
6711   // Drop information about expressions based on loop-header PHIs.
6712   SmallVector<Instruction *, 16> Worklist;
6713   Worklist.push_back(I);
6714 
6715   SmallPtrSet<Instruction *, 8> Visited;
6716   while (!Worklist.empty()) {
6717     I = Worklist.pop_back_val();
6718     if (!Visited.insert(I).second)
6719       continue;
6720 
6721     ValueExprMapType::iterator It =
6722       ValueExprMap.find_as(static_cast<Value *>(I));
6723     if (It != ValueExprMap.end()) {
6724       eraseValueFromMap(It->first);
6725       forgetMemoizedResults(It->second);
6726       if (PHINode *PN = dyn_cast<PHINode>(I))
6727         ConstantEvolutionLoopExitValue.erase(PN);
6728     }
6729 
6730     PushDefUseChildren(I, Worklist);
6731   }
6732 }
6733 
forgetLoopDispositions(const Loop * L)6734 void ScalarEvolution::forgetLoopDispositions(const Loop *L) {
6735   LoopDispositions.clear();
6736 }
6737 
6738 /// Get the exact loop backedge taken count considering all loop exits. A
6739 /// computable result can only be returned for loops with all exiting blocks
6740 /// dominating the latch. howFarToZero assumes that the limit of each loop test
6741 /// is never skipped. This is a valid assumption as long as the loop exits via
6742 /// that test. For precise results, it is the caller's responsibility to specify
6743 /// the relevant loop exiting block using getExact(ExitingBlock, SE).
6744 const SCEV *
getExact(const Loop * L,ScalarEvolution * SE,SCEVUnionPredicate * Preds) const6745 ScalarEvolution::BackedgeTakenInfo::getExact(const Loop *L, ScalarEvolution *SE,
6746                                              SCEVUnionPredicate *Preds) const {
6747   // If any exits were not computable, the loop is not computable.
6748   if (!isComplete() || ExitNotTaken.empty())
6749     return SE->getCouldNotCompute();
6750 
6751   const BasicBlock *Latch = L->getLoopLatch();
6752   // All exiting blocks we have collected must dominate the only backedge.
6753   if (!Latch)
6754     return SE->getCouldNotCompute();
6755 
6756   // All exiting blocks we have gathered dominate loop's latch, so exact trip
6757   // count is simply a minimum out of all these calculated exit counts.
6758   SmallVector<const SCEV *, 2> Ops;
6759   for (auto &ENT : ExitNotTaken) {
6760     const SCEV *BECount = ENT.ExactNotTaken;
6761     assert(BECount != SE->getCouldNotCompute() && "Bad exit SCEV!");
6762     assert(SE->DT.dominates(ENT.ExitingBlock, Latch) &&
6763            "We should only have known counts for exiting blocks that dominate "
6764            "latch!");
6765 
6766     Ops.push_back(BECount);
6767 
6768     if (Preds && !ENT.hasAlwaysTruePredicate())
6769       Preds->add(ENT.Predicate.get());
6770 
6771     assert((Preds || ENT.hasAlwaysTruePredicate()) &&
6772            "Predicate should be always true!");
6773   }
6774 
6775   return SE->getUMinFromMismatchedTypes(Ops);
6776 }
6777 
6778 /// Get the exact not taken count for this loop exit.
6779 const SCEV *
getExact(BasicBlock * ExitingBlock,ScalarEvolution * SE) const6780 ScalarEvolution::BackedgeTakenInfo::getExact(BasicBlock *ExitingBlock,
6781                                              ScalarEvolution *SE) const {
6782   for (auto &ENT : ExitNotTaken)
6783     if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate())
6784       return ENT.ExactNotTaken;
6785 
6786   return SE->getCouldNotCompute();
6787 }
6788 
6789 const SCEV *
getMax(BasicBlock * ExitingBlock,ScalarEvolution * SE) const6790 ScalarEvolution::BackedgeTakenInfo::getMax(BasicBlock *ExitingBlock,
6791                                            ScalarEvolution *SE) const {
6792   for (auto &ENT : ExitNotTaken)
6793     if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate())
6794       return ENT.MaxNotTaken;
6795 
6796   return SE->getCouldNotCompute();
6797 }
6798 
6799 /// getMax - Get the max backedge taken count for the loop.
6800 const SCEV *
getMax(ScalarEvolution * SE) const6801 ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE) const {
6802   auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
6803     return !ENT.hasAlwaysTruePredicate();
6804   };
6805 
6806   if (any_of(ExitNotTaken, PredicateNotAlwaysTrue) || !getMax())
6807     return SE->getCouldNotCompute();
6808 
6809   assert((isa<SCEVCouldNotCompute>(getMax()) || isa<SCEVConstant>(getMax())) &&
6810          "No point in having a non-constant max backedge taken count!");
6811   return getMax();
6812 }
6813 
isMaxOrZero(ScalarEvolution * SE) const6814 bool ScalarEvolution::BackedgeTakenInfo::isMaxOrZero(ScalarEvolution *SE) const {
6815   auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
6816     return !ENT.hasAlwaysTruePredicate();
6817   };
6818   return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue);
6819 }
6820 
hasOperand(const SCEV * S,ScalarEvolution * SE) const6821 bool ScalarEvolution::BackedgeTakenInfo::hasOperand(const SCEV *S,
6822                                                     ScalarEvolution *SE) const {
6823   if (getMax() && getMax() != SE->getCouldNotCompute() &&
6824       SE->hasOperand(getMax(), S))
6825     return true;
6826 
6827   for (auto &ENT : ExitNotTaken)
6828     if (ENT.ExactNotTaken != SE->getCouldNotCompute() &&
6829         SE->hasOperand(ENT.ExactNotTaken, S))
6830       return true;
6831 
6832   return false;
6833 }
6834 
ExitLimit(const SCEV * E)6835 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E)
6836     : ExactNotTaken(E), MaxNotTaken(E) {
6837   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6838           isa<SCEVConstant>(MaxNotTaken)) &&
6839          "No point in having a non-constant max backedge taken count!");
6840 }
6841 
ExitLimit(const SCEV * E,const SCEV * M,bool MaxOrZero,ArrayRef<const SmallPtrSetImpl<const SCEVPredicate * > * > PredSetList)6842 ScalarEvolution::ExitLimit::ExitLimit(
6843     const SCEV *E, const SCEV *M, bool MaxOrZero,
6844     ArrayRef<const SmallPtrSetImpl<const SCEVPredicate *> *> PredSetList)
6845     : ExactNotTaken(E), MaxNotTaken(M), MaxOrZero(MaxOrZero) {
6846   assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
6847           !isa<SCEVCouldNotCompute>(MaxNotTaken)) &&
6848          "Exact is not allowed to be less precise than Max");
6849   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6850           isa<SCEVConstant>(MaxNotTaken)) &&
6851          "No point in having a non-constant max backedge taken count!");
6852   for (auto *PredSet : PredSetList)
6853     for (auto *P : *PredSet)
6854       addPredicate(P);
6855 }
6856 
ExitLimit(const SCEV * E,const SCEV * M,bool MaxOrZero,const SmallPtrSetImpl<const SCEVPredicate * > & PredSet)6857 ScalarEvolution::ExitLimit::ExitLimit(
6858     const SCEV *E, const SCEV *M, bool MaxOrZero,
6859     const SmallPtrSetImpl<const SCEVPredicate *> &PredSet)
6860     : ExitLimit(E, M, MaxOrZero, {&PredSet}) {
6861   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6862           isa<SCEVConstant>(MaxNotTaken)) &&
6863          "No point in having a non-constant max backedge taken count!");
6864 }
6865 
ExitLimit(const SCEV * E,const SCEV * M,bool MaxOrZero)6866 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E, const SCEV *M,
6867                                       bool MaxOrZero)
6868     : ExitLimit(E, M, MaxOrZero, None) {
6869   assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6870           isa<SCEVConstant>(MaxNotTaken)) &&
6871          "No point in having a non-constant max backedge taken count!");
6872 }
6873 
6874 /// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
6875 /// computable exit into a persistent ExitNotTakenInfo array.
BackedgeTakenInfo(ArrayRef<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo> ExitCounts,bool Complete,const SCEV * MaxCount,bool MaxOrZero)6876 ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
6877     ArrayRef<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo>
6878         ExitCounts,
6879     bool Complete, const SCEV *MaxCount, bool MaxOrZero)
6880     : MaxAndComplete(MaxCount, Complete), MaxOrZero(MaxOrZero) {
6881   using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
6882 
6883   ExitNotTaken.reserve(ExitCounts.size());
6884   std::transform(
6885       ExitCounts.begin(), ExitCounts.end(), std::back_inserter(ExitNotTaken),
6886       [&](const EdgeExitInfo &EEI) {
6887         BasicBlock *ExitBB = EEI.first;
6888         const ExitLimit &EL = EEI.second;
6889         if (EL.Predicates.empty())
6890           return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, EL.MaxNotTaken,
6891                                   nullptr);
6892 
6893         std::unique_ptr<SCEVUnionPredicate> Predicate(new SCEVUnionPredicate);
6894         for (auto *Pred : EL.Predicates)
6895           Predicate->add(Pred);
6896 
6897         return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, EL.MaxNotTaken,
6898                                 std::move(Predicate));
6899       });
6900   assert((isa<SCEVCouldNotCompute>(MaxCount) || isa<SCEVConstant>(MaxCount)) &&
6901          "No point in having a non-constant max backedge taken count!");
6902 }
6903 
6904 /// Invalidate this result and free the ExitNotTakenInfo array.
clear()6905 void ScalarEvolution::BackedgeTakenInfo::clear() {
6906   ExitNotTaken.clear();
6907 }
6908 
6909 /// Compute the number of times the backedge of the specified loop will execute.
6910 ScalarEvolution::BackedgeTakenInfo
computeBackedgeTakenCount(const Loop * L,bool AllowPredicates)6911 ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
6912                                            bool AllowPredicates) {
6913   SmallVector<BasicBlock *, 8> ExitingBlocks;
6914   L->getExitingBlocks(ExitingBlocks);
6915 
6916   using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
6917 
6918   SmallVector<EdgeExitInfo, 4> ExitCounts;
6919   bool CouldComputeBECount = true;
6920   BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
6921   const SCEV *MustExitMaxBECount = nullptr;
6922   const SCEV *MayExitMaxBECount = nullptr;
6923   bool MustExitMaxOrZero = false;
6924 
6925   // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
6926   // and compute maxBECount.
6927   // Do a union of all the predicates here.
6928   for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
6929     BasicBlock *ExitBB = ExitingBlocks[i];
6930 
6931     // We canonicalize untaken exits to br (constant), ignore them so that
6932     // proving an exit untaken doesn't negatively impact our ability to reason
6933     // about the loop as whole.
6934     if (auto *BI = dyn_cast<BranchInst>(ExitBB->getTerminator()))
6935       if (auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
6936         bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
6937         if ((ExitIfTrue && CI->isZero()) || (!ExitIfTrue && CI->isOne()))
6938           continue;
6939       }
6940 
6941     ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates);
6942 
6943     assert((AllowPredicates || EL.Predicates.empty()) &&
6944            "Predicated exit limit when predicates are not allowed!");
6945 
6946     // 1. For each exit that can be computed, add an entry to ExitCounts.
6947     // CouldComputeBECount is true only if all exits can be computed.
6948     if (EL.ExactNotTaken == getCouldNotCompute())
6949       // We couldn't compute an exact value for this exit, so
6950       // we won't be able to compute an exact value for the loop.
6951       CouldComputeBECount = false;
6952     else
6953       ExitCounts.emplace_back(ExitBB, EL);
6954 
6955     // 2. Derive the loop's MaxBECount from each exit's max number of
6956     // non-exiting iterations. Partition the loop exits into two kinds:
6957     // LoopMustExits and LoopMayExits.
6958     //
6959     // If the exit dominates the loop latch, it is a LoopMustExit otherwise it
6960     // is a LoopMayExit.  If any computable LoopMustExit is found, then
6961     // MaxBECount is the minimum EL.MaxNotTaken of computable
6962     // LoopMustExits. Otherwise, MaxBECount is conservatively the maximum
6963     // EL.MaxNotTaken, where CouldNotCompute is considered greater than any
6964     // computable EL.MaxNotTaken.
6965     if (EL.MaxNotTaken != getCouldNotCompute() && Latch &&
6966         DT.dominates(ExitBB, Latch)) {
6967       if (!MustExitMaxBECount) {
6968         MustExitMaxBECount = EL.MaxNotTaken;
6969         MustExitMaxOrZero = EL.MaxOrZero;
6970       } else {
6971         MustExitMaxBECount =
6972             getUMinFromMismatchedTypes(MustExitMaxBECount, EL.MaxNotTaken);
6973       }
6974     } else if (MayExitMaxBECount != getCouldNotCompute()) {
6975       if (!MayExitMaxBECount || EL.MaxNotTaken == getCouldNotCompute())
6976         MayExitMaxBECount = EL.MaxNotTaken;
6977       else {
6978         MayExitMaxBECount =
6979             getUMaxFromMismatchedTypes(MayExitMaxBECount, EL.MaxNotTaken);
6980       }
6981     }
6982   }
6983   const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
6984     (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute());
6985   // The loop backedge will be taken the maximum or zero times if there's
6986   // a single exit that must be taken the maximum or zero times.
6987   bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1);
6988   return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount,
6989                            MaxBECount, MaxOrZero);
6990 }
6991 
6992 ScalarEvolution::ExitLimit
computeExitLimit(const Loop * L,BasicBlock * ExitingBlock,bool AllowPredicates)6993 ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
6994                                       bool AllowPredicates) {
6995   assert(L->contains(ExitingBlock) && "Exit count for non-loop block?");
6996   // If our exiting block does not dominate the latch, then its connection with
6997   // loop's exit limit may be far from trivial.
6998   const BasicBlock *Latch = L->getLoopLatch();
6999   if (!Latch || !DT.dominates(ExitingBlock, Latch))
7000     return getCouldNotCompute();
7001 
7002   bool IsOnlyExit = (L->getExitingBlock() != nullptr);
7003   Instruction *Term = ExitingBlock->getTerminator();
7004   if (BranchInst *BI = dyn_cast<BranchInst>(Term)) {
7005     assert(BI->isConditional() && "If unconditional, it can't be in loop!");
7006     bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
7007     assert(ExitIfTrue == L->contains(BI->getSuccessor(1)) &&
7008            "It should have one successor in loop and one exit block!");
7009     // Proceed to the next level to examine the exit condition expression.
7010     return computeExitLimitFromCond(
7011         L, BI->getCondition(), ExitIfTrue,
7012         /*ControlsExit=*/IsOnlyExit, AllowPredicates);
7013   }
7014 
7015   if (SwitchInst *SI = dyn_cast<SwitchInst>(Term)) {
7016     // For switch, make sure that there is a single exit from the loop.
7017     BasicBlock *Exit = nullptr;
7018     for (auto *SBB : successors(ExitingBlock))
7019       if (!L->contains(SBB)) {
7020         if (Exit) // Multiple exit successors.
7021           return getCouldNotCompute();
7022         Exit = SBB;
7023       }
7024     assert(Exit && "Exiting block must have at least one exit");
7025     return computeExitLimitFromSingleExitSwitch(L, SI, Exit,
7026                                                 /*ControlsExit=*/IsOnlyExit);
7027   }
7028 
7029   return getCouldNotCompute();
7030 }
7031 
computeExitLimitFromCond(const Loop * L,Value * ExitCond,bool ExitIfTrue,bool ControlsExit,bool AllowPredicates)7032 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond(
7033     const Loop *L, Value *ExitCond, bool ExitIfTrue,
7034     bool ControlsExit, bool AllowPredicates) {
7035   ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates);
7036   return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue,
7037                                         ControlsExit, AllowPredicates);
7038 }
7039 
7040 Optional<ScalarEvolution::ExitLimit>
find(const Loop * L,Value * ExitCond,bool ExitIfTrue,bool ControlsExit,bool AllowPredicates)7041 ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond,
7042                                       bool ExitIfTrue, bool ControlsExit,
7043                                       bool AllowPredicates) {
7044   (void)this->L;
7045   (void)this->ExitIfTrue;
7046   (void)this->AllowPredicates;
7047 
7048   assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
7049          this->AllowPredicates == AllowPredicates &&
7050          "Variance in assumed invariant key components!");
7051   auto Itr = TripCountMap.find({ExitCond, ControlsExit});
7052   if (Itr == TripCountMap.end())
7053     return None;
7054   return Itr->second;
7055 }
7056 
insert(const Loop * L,Value * ExitCond,bool ExitIfTrue,bool ControlsExit,bool AllowPredicates,const ExitLimit & EL)7057 void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond,
7058                                              bool ExitIfTrue,
7059                                              bool ControlsExit,
7060                                              bool AllowPredicates,
7061                                              const ExitLimit &EL) {
7062   assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
7063          this->AllowPredicates == AllowPredicates &&
7064          "Variance in assumed invariant key components!");
7065 
7066   auto InsertResult = TripCountMap.insert({{ExitCond, ControlsExit}, EL});
7067   assert(InsertResult.second && "Expected successful insertion!");
7068   (void)InsertResult;
7069   (void)ExitIfTrue;
7070 }
7071 
computeExitLimitFromCondCached(ExitLimitCacheTy & Cache,const Loop * L,Value * ExitCond,bool ExitIfTrue,bool ControlsExit,bool AllowPredicates)7072 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
7073     ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
7074     bool ControlsExit, bool AllowPredicates) {
7075 
7076   if (auto MaybeEL =
7077           Cache.find(L, ExitCond, ExitIfTrue, ControlsExit, AllowPredicates))
7078     return *MaybeEL;
7079 
7080   ExitLimit EL = computeExitLimitFromCondImpl(Cache, L, ExitCond, ExitIfTrue,
7081                                               ControlsExit, AllowPredicates);
7082   Cache.insert(L, ExitCond, ExitIfTrue, ControlsExit, AllowPredicates, EL);
7083   return EL;
7084 }
7085 
computeExitLimitFromCondImpl(ExitLimitCacheTy & Cache,const Loop * L,Value * ExitCond,bool ExitIfTrue,bool ControlsExit,bool AllowPredicates)7086 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
7087     ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
7088     bool ControlsExit, bool AllowPredicates) {
7089   // Check if the controlling expression for this loop is an And or Or.
7090   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
7091     if (BO->getOpcode() == Instruction::And) {
7092       // Recurse on the operands of the and.
7093       bool EitherMayExit = !ExitIfTrue;
7094       ExitLimit EL0 = computeExitLimitFromCondCached(
7095           Cache, L, BO->getOperand(0), ExitIfTrue,
7096           ControlsExit && !EitherMayExit, AllowPredicates);
7097       ExitLimit EL1 = computeExitLimitFromCondCached(
7098           Cache, L, BO->getOperand(1), ExitIfTrue,
7099           ControlsExit && !EitherMayExit, AllowPredicates);
7100       // Be robust against unsimplified IR for the form "and i1 X, true"
7101       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1)))
7102         return CI->isOne() ? EL0 : EL1;
7103       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(0)))
7104         return CI->isOne() ? EL1 : EL0;
7105       const SCEV *BECount = getCouldNotCompute();
7106       const SCEV *MaxBECount = getCouldNotCompute();
7107       if (EitherMayExit) {
7108         // Both conditions must be true for the loop to continue executing.
7109         // Choose the less conservative count.
7110         if (EL0.ExactNotTaken == getCouldNotCompute() ||
7111             EL1.ExactNotTaken == getCouldNotCompute())
7112           BECount = getCouldNotCompute();
7113         else
7114           BECount =
7115               getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
7116         if (EL0.MaxNotTaken == getCouldNotCompute())
7117           MaxBECount = EL1.MaxNotTaken;
7118         else if (EL1.MaxNotTaken == getCouldNotCompute())
7119           MaxBECount = EL0.MaxNotTaken;
7120         else
7121           MaxBECount =
7122               getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
7123       } else {
7124         // Both conditions must be true at the same time for the loop to exit.
7125         // For now, be conservative.
7126         if (EL0.MaxNotTaken == EL1.MaxNotTaken)
7127           MaxBECount = EL0.MaxNotTaken;
7128         if (EL0.ExactNotTaken == EL1.ExactNotTaken)
7129           BECount = EL0.ExactNotTaken;
7130       }
7131 
7132       // There are cases (e.g. PR26207) where computeExitLimitFromCond is able
7133       // to be more aggressive when computing BECount than when computing
7134       // MaxBECount.  In these cases it is possible for EL0.ExactNotTaken and
7135       // EL1.ExactNotTaken to match, but for EL0.MaxNotTaken and EL1.MaxNotTaken
7136       // to not.
7137       if (isa<SCEVCouldNotCompute>(MaxBECount) &&
7138           !isa<SCEVCouldNotCompute>(BECount))
7139         MaxBECount = getConstant(getUnsignedRangeMax(BECount));
7140 
7141       return ExitLimit(BECount, MaxBECount, false,
7142                        {&EL0.Predicates, &EL1.Predicates});
7143     }
7144     if (BO->getOpcode() == Instruction::Or) {
7145       // Recurse on the operands of the or.
7146       bool EitherMayExit = ExitIfTrue;
7147       ExitLimit EL0 = computeExitLimitFromCondCached(
7148           Cache, L, BO->getOperand(0), ExitIfTrue,
7149           ControlsExit && !EitherMayExit, AllowPredicates);
7150       ExitLimit EL1 = computeExitLimitFromCondCached(
7151           Cache, L, BO->getOperand(1), ExitIfTrue,
7152           ControlsExit && !EitherMayExit, AllowPredicates);
7153       // Be robust against unsimplified IR for the form "or i1 X, true"
7154       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(1)))
7155         return CI->isZero() ? EL0 : EL1;
7156       if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->getOperand(0)))
7157         return CI->isZero() ? EL1 : EL0;
7158       const SCEV *BECount = getCouldNotCompute();
7159       const SCEV *MaxBECount = getCouldNotCompute();
7160       if (EitherMayExit) {
7161         // Both conditions must be false for the loop to continue executing.
7162         // Choose the less conservative count.
7163         if (EL0.ExactNotTaken == getCouldNotCompute() ||
7164             EL1.ExactNotTaken == getCouldNotCompute())
7165           BECount = getCouldNotCompute();
7166         else
7167           BECount =
7168               getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
7169         if (EL0.MaxNotTaken == getCouldNotCompute())
7170           MaxBECount = EL1.MaxNotTaken;
7171         else if (EL1.MaxNotTaken == getCouldNotCompute())
7172           MaxBECount = EL0.MaxNotTaken;
7173         else
7174           MaxBECount =
7175               getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
7176       } else {
7177         // Both conditions must be false at the same time for the loop to exit.
7178         // For now, be conservative.
7179         if (EL0.MaxNotTaken == EL1.MaxNotTaken)
7180           MaxBECount = EL0.MaxNotTaken;
7181         if (EL0.ExactNotTaken == EL1.ExactNotTaken)
7182           BECount = EL0.ExactNotTaken;
7183       }
7184       // There are cases (e.g. PR26207) where computeExitLimitFromCond is able
7185       // to be more aggressive when computing BECount than when computing
7186       // MaxBECount.  In these cases it is possible for EL0.ExactNotTaken and
7187       // EL1.ExactNotTaken to match, but for EL0.MaxNotTaken and EL1.MaxNotTaken
7188       // to not.
7189       if (isa<SCEVCouldNotCompute>(MaxBECount) &&
7190           !isa<SCEVCouldNotCompute>(BECount))
7191         MaxBECount = getConstant(getUnsignedRangeMax(BECount));
7192 
7193       return ExitLimit(BECount, MaxBECount, false,
7194                        {&EL0.Predicates, &EL1.Predicates});
7195     }
7196   }
7197 
7198   // With an icmp, it may be feasible to compute an exact backedge-taken count.
7199   // Proceed to the next level to examine the icmp.
7200   if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) {
7201     ExitLimit EL =
7202         computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsExit);
7203     if (EL.hasFullInfo() || !AllowPredicates)
7204       return EL;
7205 
7206     // Try again, but use SCEV predicates this time.
7207     return computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsExit,
7208                                     /*AllowPredicates=*/true);
7209   }
7210 
7211   // Check for a constant condition. These are normally stripped out by
7212   // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
7213   // preserve the CFG and is temporarily leaving constant conditions
7214   // in place.
7215   if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
7216     if (ExitIfTrue == !CI->getZExtValue())
7217       // The backedge is always taken.
7218       return getCouldNotCompute();
7219     else
7220       // The backedge is never taken.
7221       return getZero(CI->getType());
7222   }
7223 
7224   // If it's not an integer or pointer comparison then compute it the hard way.
7225   return computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
7226 }
7227 
7228 ScalarEvolution::ExitLimit
computeExitLimitFromICmp(const Loop * L,ICmpInst * ExitCond,bool ExitIfTrue,bool ControlsExit,bool AllowPredicates)7229 ScalarEvolution::computeExitLimitFromICmp(const Loop *L,
7230                                           ICmpInst *ExitCond,
7231                                           bool ExitIfTrue,
7232                                           bool ControlsExit,
7233                                           bool AllowPredicates) {
7234   // If the condition was exit on true, convert the condition to exit on false
7235   ICmpInst::Predicate Pred;
7236   if (!ExitIfTrue)
7237     Pred = ExitCond->getPredicate();
7238   else
7239     Pred = ExitCond->getInversePredicate();
7240   const ICmpInst::Predicate OriginalPred = Pred;
7241 
7242   // Handle common loops like: for (X = "string"; *X; ++X)
7243   if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
7244     if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
7245       ExitLimit ItCnt =
7246         computeLoadConstantCompareExitLimit(LI, RHS, L, Pred);
7247       if (ItCnt.hasAnyInfo())
7248         return ItCnt;
7249     }
7250 
7251   const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
7252   const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
7253 
7254   // Try to evaluate any dependencies out of the loop.
7255   LHS = getSCEVAtScope(LHS, L);
7256   RHS = getSCEVAtScope(RHS, L);
7257 
7258   // At this point, we would like to compute how many iterations of the
7259   // loop the predicate will return true for these inputs.
7260   if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) {
7261     // If there is a loop-invariant, force it into the RHS.
7262     std::swap(LHS, RHS);
7263     Pred = ICmpInst::getSwappedPredicate(Pred);
7264   }
7265 
7266   // Simplify the operands before analyzing them.
7267   (void)SimplifyICmpOperands(Pred, LHS, RHS);
7268 
7269   // If we have a comparison of a chrec against a constant, try to use value
7270   // ranges to answer this query.
7271   if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
7272     if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
7273       if (AddRec->getLoop() == L) {
7274         // Form the constant range.
7275         ConstantRange CompRange =
7276             ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt());
7277 
7278         const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
7279         if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
7280       }
7281 
7282   switch (Pred) {
7283   case ICmpInst::ICMP_NE: {                     // while (X != Y)
7284     // Convert to: while (X-Y != 0)
7285     ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit,
7286                                 AllowPredicates);
7287     if (EL.hasAnyInfo()) return EL;
7288     break;
7289   }
7290   case ICmpInst::ICMP_EQ: {                     // while (X == Y)
7291     // Convert to: while (X-Y == 0)
7292     ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L);
7293     if (EL.hasAnyInfo()) return EL;
7294     break;
7295   }
7296   case ICmpInst::ICMP_SLT:
7297   case ICmpInst::ICMP_ULT: {                    // while (X < Y)
7298     bool IsSigned = Pred == ICmpInst::ICMP_SLT;
7299     ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsExit,
7300                                     AllowPredicates);
7301     if (EL.hasAnyInfo()) return EL;
7302     break;
7303   }
7304   case ICmpInst::ICMP_SGT:
7305   case ICmpInst::ICMP_UGT: {                    // while (X > Y)
7306     bool IsSigned = Pred == ICmpInst::ICMP_SGT;
7307     ExitLimit EL =
7308         howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsExit,
7309                             AllowPredicates);
7310     if (EL.hasAnyInfo()) return EL;
7311     break;
7312   }
7313   default:
7314     break;
7315   }
7316 
7317   auto *ExhaustiveCount =
7318       computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
7319 
7320   if (!isa<SCEVCouldNotCompute>(ExhaustiveCount))
7321     return ExhaustiveCount;
7322 
7323   return computeShiftCompareExitLimit(ExitCond->getOperand(0),
7324                                       ExitCond->getOperand(1), L, OriginalPred);
7325 }
7326 
7327 ScalarEvolution::ExitLimit
computeExitLimitFromSingleExitSwitch(const Loop * L,SwitchInst * Switch,BasicBlock * ExitingBlock,bool ControlsExit)7328 ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L,
7329                                                       SwitchInst *Switch,
7330                                                       BasicBlock *ExitingBlock,
7331                                                       bool ControlsExit) {
7332   assert(!L->contains(ExitingBlock) && "Not an exiting block!");
7333 
7334   // Give up if the exit is the default dest of a switch.
7335   if (Switch->getDefaultDest() == ExitingBlock)
7336     return getCouldNotCompute();
7337 
7338   assert(L->contains(Switch->getDefaultDest()) &&
7339          "Default case must not exit the loop!");
7340   const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L);
7341   const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock));
7342 
7343   // while (X != Y) --> while (X-Y != 0)
7344   ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit);
7345   if (EL.hasAnyInfo())
7346     return EL;
7347 
7348   return getCouldNotCompute();
7349 }
7350 
7351 static ConstantInt *
EvaluateConstantChrecAtConstant(const SCEVAddRecExpr * AddRec,ConstantInt * C,ScalarEvolution & SE)7352 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
7353                                 ScalarEvolution &SE) {
7354   const SCEV *InVal = SE.getConstant(C);
7355   const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
7356   assert(isa<SCEVConstant>(Val) &&
7357          "Evaluation of SCEV at constant didn't fold correctly?");
7358   return cast<SCEVConstant>(Val)->getValue();
7359 }
7360 
7361 /// Given an exit condition of 'icmp op load X, cst', try to see if we can
7362 /// compute the backedge execution count.
7363 ScalarEvolution::ExitLimit
computeLoadConstantCompareExitLimit(LoadInst * LI,Constant * RHS,const Loop * L,ICmpInst::Predicate predicate)7364 ScalarEvolution::computeLoadConstantCompareExitLimit(
7365   LoadInst *LI,
7366   Constant *RHS,
7367   const Loop *L,
7368   ICmpInst::Predicate predicate) {
7369   if (LI->isVolatile()) return getCouldNotCompute();
7370 
7371   // Check to see if the loaded pointer is a getelementptr of a global.
7372   // TODO: Use SCEV instead of manually grubbing with GEPs.
7373   GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
7374   if (!GEP) return getCouldNotCompute();
7375 
7376   // Make sure that it is really a constant global we are gepping, with an
7377   // initializer, and make sure the first IDX is really 0.
7378   GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
7379   if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
7380       GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
7381       !cast<Constant>(GEP->getOperand(1))->isNullValue())
7382     return getCouldNotCompute();
7383 
7384   // Okay, we allow one non-constant index into the GEP instruction.
7385   Value *VarIdx = nullptr;
7386   std::vector<Constant*> Indexes;
7387   unsigned VarIdxNum = 0;
7388   for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
7389     if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
7390       Indexes.push_back(CI);
7391     } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
7392       if (VarIdx) return getCouldNotCompute();  // Multiple non-constant idx's.
7393       VarIdx = GEP->getOperand(i);
7394       VarIdxNum = i-2;
7395       Indexes.push_back(nullptr);
7396     }
7397 
7398   // Loop-invariant loads may be a byproduct of loop optimization. Skip them.
7399   if (!VarIdx)
7400     return getCouldNotCompute();
7401 
7402   // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
7403   // Check to see if X is a loop variant variable value now.
7404   const SCEV *Idx = getSCEV(VarIdx);
7405   Idx = getSCEVAtScope(Idx, L);
7406 
7407   // We can only recognize very limited forms of loop index expressions, in
7408   // particular, only affine AddRec's like {C1,+,C2}.
7409   const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
7410   if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) ||
7411       !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
7412       !isa<SCEVConstant>(IdxExpr->getOperand(1)))
7413     return getCouldNotCompute();
7414 
7415   unsigned MaxSteps = MaxBruteForceIterations;
7416   for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
7417     ConstantInt *ItCst = ConstantInt::get(
7418                            cast<IntegerType>(IdxExpr->getType()), IterationNum);
7419     ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
7420 
7421     // Form the GEP offset.
7422     Indexes[VarIdxNum] = Val;
7423 
7424     Constant *Result = ConstantFoldLoadThroughGEPIndices(GV->getInitializer(),
7425                                                          Indexes);
7426     if (!Result) break;  // Cannot compute!
7427 
7428     // Evaluate the condition for this iteration.
7429     Result = ConstantExpr::getICmp(predicate, Result, RHS);
7430     if (!isa<ConstantInt>(Result)) break;  // Couldn't decide for sure
7431     if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
7432       ++NumArrayLenItCounts;
7433       return getConstant(ItCst);   // Found terminating iteration!
7434     }
7435   }
7436   return getCouldNotCompute();
7437 }
7438 
computeShiftCompareExitLimit(Value * LHS,Value * RHSV,const Loop * L,ICmpInst::Predicate Pred)7439 ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit(
7440     Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) {
7441   ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV);
7442   if (!RHS)
7443     return getCouldNotCompute();
7444 
7445   const BasicBlock *Latch = L->getLoopLatch();
7446   if (!Latch)
7447     return getCouldNotCompute();
7448 
7449   const BasicBlock *Predecessor = L->getLoopPredecessor();
7450   if (!Predecessor)
7451     return getCouldNotCompute();
7452 
7453   // Return true if V is of the form "LHS `shift_op` <positive constant>".
7454   // Return LHS in OutLHS and shift_opt in OutOpCode.
7455   auto MatchPositiveShift =
7456       [](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) {
7457 
7458     using namespace PatternMatch;
7459 
7460     ConstantInt *ShiftAmt;
7461     if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7462       OutOpCode = Instruction::LShr;
7463     else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7464       OutOpCode = Instruction::AShr;
7465     else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7466       OutOpCode = Instruction::Shl;
7467     else
7468       return false;
7469 
7470     return ShiftAmt->getValue().isStrictlyPositive();
7471   };
7472 
7473   // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in
7474   //
7475   // loop:
7476   //   %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ]
7477   //   %iv.shifted = lshr i32 %iv, <positive constant>
7478   //
7479   // Return true on a successful match.  Return the corresponding PHI node (%iv
7480   // above) in PNOut and the opcode of the shift operation in OpCodeOut.
7481   auto MatchShiftRecurrence =
7482       [&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) {
7483     Optional<Instruction::BinaryOps> PostShiftOpCode;
7484 
7485     {
7486       Instruction::BinaryOps OpC;
7487       Value *V;
7488 
7489       // If we encounter a shift instruction, "peel off" the shift operation,
7490       // and remember that we did so.  Later when we inspect %iv's backedge
7491       // value, we will make sure that the backedge value uses the same
7492       // operation.
7493       //
7494       // Note: the peeled shift operation does not have to be the same
7495       // instruction as the one feeding into the PHI's backedge value.  We only
7496       // really care about it being the same *kind* of shift instruction --
7497       // that's all that is required for our later inferences to hold.
7498       if (MatchPositiveShift(LHS, V, OpC)) {
7499         PostShiftOpCode = OpC;
7500         LHS = V;
7501       }
7502     }
7503 
7504     PNOut = dyn_cast<PHINode>(LHS);
7505     if (!PNOut || PNOut->getParent() != L->getHeader())
7506       return false;
7507 
7508     Value *BEValue = PNOut->getIncomingValueForBlock(Latch);
7509     Value *OpLHS;
7510 
7511     return
7512         // The backedge value for the PHI node must be a shift by a positive
7513         // amount
7514         MatchPositiveShift(BEValue, OpLHS, OpCodeOut) &&
7515 
7516         // of the PHI node itself
7517         OpLHS == PNOut &&
7518 
7519         // and the kind of shift should be match the kind of shift we peeled
7520         // off, if any.
7521         (!PostShiftOpCode.hasValue() || *PostShiftOpCode == OpCodeOut);
7522   };
7523 
7524   PHINode *PN;
7525   Instruction::BinaryOps OpCode;
7526   if (!MatchShiftRecurrence(LHS, PN, OpCode))
7527     return getCouldNotCompute();
7528 
7529   const DataLayout &DL = getDataLayout();
7530 
7531   // The key rationale for this optimization is that for some kinds of shift
7532   // recurrences, the value of the recurrence "stabilizes" to either 0 or -1
7533   // within a finite number of iterations.  If the condition guarding the
7534   // backedge (in the sense that the backedge is taken if the condition is true)
7535   // is false for the value the shift recurrence stabilizes to, then we know
7536   // that the backedge is taken only a finite number of times.
7537 
7538   ConstantInt *StableValue = nullptr;
7539   switch (OpCode) {
7540   default:
7541     llvm_unreachable("Impossible case!");
7542 
7543   case Instruction::AShr: {
7544     // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most
7545     // bitwidth(K) iterations.
7546     Value *FirstValue = PN->getIncomingValueForBlock(Predecessor);
7547     KnownBits Known = computeKnownBits(FirstValue, DL, 0, nullptr,
7548                                        Predecessor->getTerminator(), &DT);
7549     auto *Ty = cast<IntegerType>(RHS->getType());
7550     if (Known.isNonNegative())
7551       StableValue = ConstantInt::get(Ty, 0);
7552     else if (Known.isNegative())
7553       StableValue = ConstantInt::get(Ty, -1, true);
7554     else
7555       return getCouldNotCompute();
7556 
7557     break;
7558   }
7559   case Instruction::LShr:
7560   case Instruction::Shl:
7561     // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>}
7562     // stabilize to 0 in at most bitwidth(K) iterations.
7563     StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0);
7564     break;
7565   }
7566 
7567   auto *Result =
7568       ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI);
7569   assert(Result->getType()->isIntegerTy(1) &&
7570          "Otherwise cannot be an operand to a branch instruction");
7571 
7572   if (Result->isZeroValue()) {
7573     unsigned BitWidth = getTypeSizeInBits(RHS->getType());
7574     const SCEV *UpperBound =
7575         getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth);
7576     return ExitLimit(getCouldNotCompute(), UpperBound, false);
7577   }
7578 
7579   return getCouldNotCompute();
7580 }
7581 
7582 /// Return true if we can constant fold an instruction of the specified type,
7583 /// assuming that all operands were constants.
CanConstantFold(const Instruction * I)7584 static bool CanConstantFold(const Instruction *I) {
7585   if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
7586       isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
7587       isa<LoadInst>(I) || isa<ExtractValueInst>(I))
7588     return true;
7589 
7590   if (const CallInst *CI = dyn_cast<CallInst>(I))
7591     if (const Function *F = CI->getCalledFunction())
7592       return canConstantFoldCallTo(CI, F);
7593   return false;
7594 }
7595 
7596 /// Determine whether this instruction can constant evolve within this loop
7597 /// assuming its operands can all constant evolve.
canConstantEvolve(Instruction * I,const Loop * L)7598 static bool canConstantEvolve(Instruction *I, const Loop *L) {
7599   // An instruction outside of the loop can't be derived from a loop PHI.
7600   if (!L->contains(I)) return false;
7601 
7602   if (isa<PHINode>(I)) {
7603     // We don't currently keep track of the control flow needed to evaluate
7604     // PHIs, so we cannot handle PHIs inside of loops.
7605     return L->getHeader() == I->getParent();
7606   }
7607 
7608   // If we won't be able to constant fold this expression even if the operands
7609   // are constants, bail early.
7610   return CanConstantFold(I);
7611 }
7612 
7613 /// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
7614 /// recursing through each instruction operand until reaching a loop header phi.
7615 static PHINode *
getConstantEvolvingPHIOperands(Instruction * UseInst,const Loop * L,DenseMap<Instruction *,PHINode * > & PHIMap,unsigned Depth)7616 getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L,
7617                                DenseMap<Instruction *, PHINode *> &PHIMap,
7618                                unsigned Depth) {
7619   if (Depth > MaxConstantEvolvingDepth)
7620     return nullptr;
7621 
7622   // Otherwise, we can evaluate this instruction if all of its operands are
7623   // constant or derived from a PHI node themselves.
7624   PHINode *PHI = nullptr;
7625   for (Value *Op : UseInst->operands()) {
7626     if (isa<Constant>(Op)) continue;
7627 
7628     Instruction *OpInst = dyn_cast<Instruction>(Op);
7629     if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr;
7630 
7631     PHINode *P = dyn_cast<PHINode>(OpInst);
7632     if (!P)
7633       // If this operand is already visited, reuse the prior result.
7634       // We may have P != PHI if this is the deepest point at which the
7635       // inconsistent paths meet.
7636       P = PHIMap.lookup(OpInst);
7637     if (!P) {
7638       // Recurse and memoize the results, whether a phi is found or not.
7639       // This recursive call invalidates pointers into PHIMap.
7640       P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1);
7641       PHIMap[OpInst] = P;
7642     }
7643     if (!P)
7644       return nullptr;  // Not evolving from PHI
7645     if (PHI && PHI != P)
7646       return nullptr;  // Evolving from multiple different PHIs.
7647     PHI = P;
7648   }
7649   // This is a expression evolving from a constant PHI!
7650   return PHI;
7651 }
7652 
7653 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
7654 /// in the loop that V is derived from.  We allow arbitrary operations along the
7655 /// way, but the operands of an operation must either be constants or a value
7656 /// derived from a constant PHI.  If this expression does not fit with these
7657 /// constraints, return null.
getConstantEvolvingPHI(Value * V,const Loop * L)7658 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
7659   Instruction *I = dyn_cast<Instruction>(V);
7660   if (!I || !canConstantEvolve(I, L)) return nullptr;
7661 
7662   if (PHINode *PN = dyn_cast<PHINode>(I))
7663     return PN;
7664 
7665   // Record non-constant instructions contained by the loop.
7666   DenseMap<Instruction *, PHINode *> PHIMap;
7667   return getConstantEvolvingPHIOperands(I, L, PHIMap, 0);
7668 }
7669 
7670 /// EvaluateExpression - Given an expression that passes the
7671 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
7672 /// in the loop has the value PHIVal.  If we can't fold this expression for some
7673 /// reason, return null.
EvaluateExpression(Value * V,const Loop * L,DenseMap<Instruction *,Constant * > & Vals,const DataLayout & DL,const TargetLibraryInfo * TLI)7674 static Constant *EvaluateExpression(Value *V, const Loop *L,
7675                                     DenseMap<Instruction *, Constant *> &Vals,
7676                                     const DataLayout &DL,
7677                                     const TargetLibraryInfo *TLI) {
7678   // Convenient constant check, but redundant for recursive calls.
7679   if (Constant *C = dyn_cast<Constant>(V)) return C;
7680   Instruction *I = dyn_cast<Instruction>(V);
7681   if (!I) return nullptr;
7682 
7683   if (Constant *C = Vals.lookup(I)) return C;
7684 
7685   // An instruction inside the loop depends on a value outside the loop that we
7686   // weren't given a mapping for, or a value such as a call inside the loop.
7687   if (!canConstantEvolve(I, L)) return nullptr;
7688 
7689   // An unmapped PHI can be due to a branch or another loop inside this loop,
7690   // or due to this not being the initial iteration through a loop where we
7691   // couldn't compute the evolution of this particular PHI last time.
7692   if (isa<PHINode>(I)) return nullptr;
7693 
7694   std::vector<Constant*> Operands(I->getNumOperands());
7695 
7696   for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
7697     Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i));
7698     if (!Operand) {
7699       Operands[i] = dyn_cast<Constant>(I->getOperand(i));
7700       if (!Operands[i]) return nullptr;
7701       continue;
7702     }
7703     Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI);
7704     Vals[Operand] = C;
7705     if (!C) return nullptr;
7706     Operands[i] = C;
7707   }
7708 
7709   if (CmpInst *CI = dyn_cast<CmpInst>(I))
7710     return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
7711                                            Operands[1], DL, TLI);
7712   if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
7713     if (!LI->isVolatile())
7714       return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
7715   }
7716   return ConstantFoldInstOperands(I, Operands, DL, TLI);
7717 }
7718 
7719 
7720 // If every incoming value to PN except the one for BB is a specific Constant,
7721 // return that, else return nullptr.
getOtherIncomingValue(PHINode * PN,BasicBlock * BB)7722 static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) {
7723   Constant *IncomingVal = nullptr;
7724 
7725   for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
7726     if (PN->getIncomingBlock(i) == BB)
7727       continue;
7728 
7729     auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i));
7730     if (!CurrentVal)
7731       return nullptr;
7732 
7733     if (IncomingVal != CurrentVal) {
7734       if (IncomingVal)
7735         return nullptr;
7736       IncomingVal = CurrentVal;
7737     }
7738   }
7739 
7740   return IncomingVal;
7741 }
7742 
7743 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
7744 /// in the header of its containing loop, we know the loop executes a
7745 /// constant number of times, and the PHI node is just a recurrence
7746 /// involving constants, fold it.
7747 Constant *
getConstantEvolutionLoopExitValue(PHINode * PN,const APInt & BEs,const Loop * L)7748 ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
7749                                                    const APInt &BEs,
7750                                                    const Loop *L) {
7751   auto I = ConstantEvolutionLoopExitValue.find(PN);
7752   if (I != ConstantEvolutionLoopExitValue.end())
7753     return I->second;
7754 
7755   if (BEs.ugt(MaxBruteForceIterations))
7756     return ConstantEvolutionLoopExitValue[PN] = nullptr;  // Not going to evaluate it.
7757 
7758   Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
7759 
7760   DenseMap<Instruction *, Constant *> CurrentIterVals;
7761   BasicBlock *Header = L->getHeader();
7762   assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
7763 
7764   BasicBlock *Latch = L->getLoopLatch();
7765   if (!Latch)
7766     return nullptr;
7767 
7768   for (PHINode &PHI : Header->phis()) {
7769     if (auto *StartCST = getOtherIncomingValue(&PHI, Latch))
7770       CurrentIterVals[&PHI] = StartCST;
7771   }
7772   if (!CurrentIterVals.count(PN))
7773     return RetVal = nullptr;
7774 
7775   Value *BEValue = PN->getIncomingValueForBlock(Latch);
7776 
7777   // Execute the loop symbolically to determine the exit value.
7778   assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) &&
7779          "BEs is <= MaxBruteForceIterations which is an 'unsigned'!");
7780 
7781   unsigned NumIterations = BEs.getZExtValue(); // must be in range
7782   unsigned IterationNum = 0;
7783   const DataLayout &DL = getDataLayout();
7784   for (; ; ++IterationNum) {
7785     if (IterationNum == NumIterations)
7786       return RetVal = CurrentIterVals[PN];  // Got exit value!
7787 
7788     // Compute the value of the PHIs for the next iteration.
7789     // EvaluateExpression adds non-phi values to the CurrentIterVals map.
7790     DenseMap<Instruction *, Constant *> NextIterVals;
7791     Constant *NextPHI =
7792         EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
7793     if (!NextPHI)
7794       return nullptr;        // Couldn't evaluate!
7795     NextIterVals[PN] = NextPHI;
7796 
7797     bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
7798 
7799     // Also evaluate the other PHI nodes.  However, we don't get to stop if we
7800     // cease to be able to evaluate one of them or if they stop evolving,
7801     // because that doesn't necessarily prevent us from computing PN.
7802     SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute;
7803     for (const auto &I : CurrentIterVals) {
7804       PHINode *PHI = dyn_cast<PHINode>(I.first);
7805       if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
7806       PHIsToCompute.emplace_back(PHI, I.second);
7807     }
7808     // We use two distinct loops because EvaluateExpression may invalidate any
7809     // iterators into CurrentIterVals.
7810     for (const auto &I : PHIsToCompute) {
7811       PHINode *PHI = I.first;
7812       Constant *&NextPHI = NextIterVals[PHI];
7813       if (!NextPHI) {   // Not already computed.
7814         Value *BEValue = PHI->getIncomingValueForBlock(Latch);
7815         NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
7816       }
7817       if (NextPHI != I.second)
7818         StoppedEvolving = false;
7819     }
7820 
7821     // If all entries in CurrentIterVals == NextIterVals then we can stop
7822     // iterating, the loop can't continue to change.
7823     if (StoppedEvolving)
7824       return RetVal = CurrentIterVals[PN];
7825 
7826     CurrentIterVals.swap(NextIterVals);
7827   }
7828 }
7829 
computeExitCountExhaustively(const Loop * L,Value * Cond,bool ExitWhen)7830 const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L,
7831                                                           Value *Cond,
7832                                                           bool ExitWhen) {
7833   PHINode *PN = getConstantEvolvingPHI(Cond, L);
7834   if (!PN) return getCouldNotCompute();
7835 
7836   // If the loop is canonicalized, the PHI will have exactly two entries.
7837   // That's the only form we support here.
7838   if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
7839 
7840   DenseMap<Instruction *, Constant *> CurrentIterVals;
7841   BasicBlock *Header = L->getHeader();
7842   assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
7843 
7844   BasicBlock *Latch = L->getLoopLatch();
7845   assert(Latch && "Should follow from NumIncomingValues == 2!");
7846 
7847   for (PHINode &PHI : Header->phis()) {
7848     if (auto *StartCST = getOtherIncomingValue(&PHI, Latch))
7849       CurrentIterVals[&PHI] = StartCST;
7850   }
7851   if (!CurrentIterVals.count(PN))
7852     return getCouldNotCompute();
7853 
7854   // Okay, we find a PHI node that defines the trip count of this loop.  Execute
7855   // the loop symbolically to determine when the condition gets a value of
7856   // "ExitWhen".
7857   unsigned MaxIterations = MaxBruteForceIterations;   // Limit analysis.
7858   const DataLayout &DL = getDataLayout();
7859   for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
7860     auto *CondVal = dyn_cast_or_null<ConstantInt>(
7861         EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI));
7862 
7863     // Couldn't symbolically evaluate.
7864     if (!CondVal) return getCouldNotCompute();
7865 
7866     if (CondVal->getValue() == uint64_t(ExitWhen)) {
7867       ++NumBruteForceTripCountsComputed;
7868       return getConstant(Type::getInt32Ty(getContext()), IterationNum);
7869     }
7870 
7871     // Update all the PHI nodes for the next iteration.
7872     DenseMap<Instruction *, Constant *> NextIterVals;
7873 
7874     // Create a list of which PHIs we need to compute. We want to do this before
7875     // calling EvaluateExpression on them because that may invalidate iterators
7876     // into CurrentIterVals.
7877     SmallVector<PHINode *, 8> PHIsToCompute;
7878     for (const auto &I : CurrentIterVals) {
7879       PHINode *PHI = dyn_cast<PHINode>(I.first);
7880       if (!PHI || PHI->getParent() != Header) continue;
7881       PHIsToCompute.push_back(PHI);
7882     }
7883     for (PHINode *PHI : PHIsToCompute) {
7884       Constant *&NextPHI = NextIterVals[PHI];
7885       if (NextPHI) continue;    // Already computed!
7886 
7887       Value *BEValue = PHI->getIncomingValueForBlock(Latch);
7888       NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
7889     }
7890     CurrentIterVals.swap(NextIterVals);
7891   }
7892 
7893   // Too many iterations were needed to evaluate.
7894   return getCouldNotCompute();
7895 }
7896 
getSCEVAtScope(const SCEV * V,const Loop * L)7897 const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
7898   SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values =
7899       ValuesAtScopes[V];
7900   // Check to see if we've folded this expression at this loop before.
7901   for (auto &LS : Values)
7902     if (LS.first == L)
7903       return LS.second ? LS.second : V;
7904 
7905   Values.emplace_back(L, nullptr);
7906 
7907   // Otherwise compute it.
7908   const SCEV *C = computeSCEVAtScope(V, L);
7909   for (auto &LS : reverse(ValuesAtScopes[V]))
7910     if (LS.first == L) {
7911       LS.second = C;
7912       break;
7913     }
7914   return C;
7915 }
7916 
7917 /// This builds up a Constant using the ConstantExpr interface.  That way, we
7918 /// will return Constants for objects which aren't represented by a
7919 /// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
7920 /// Returns NULL if the SCEV isn't representable as a Constant.
BuildConstantFromSCEV(const SCEV * V)7921 static Constant *BuildConstantFromSCEV(const SCEV *V) {
7922   switch (static_cast<SCEVTypes>(V->getSCEVType())) {
7923     case scCouldNotCompute:
7924     case scAddRecExpr:
7925       break;
7926     case scConstant:
7927       return cast<SCEVConstant>(V)->getValue();
7928     case scUnknown:
7929       return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
7930     case scSignExtend: {
7931       const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
7932       if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
7933         return ConstantExpr::getSExt(CastOp, SS->getType());
7934       break;
7935     }
7936     case scZeroExtend: {
7937       const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
7938       if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
7939         return ConstantExpr::getZExt(CastOp, SZ->getType());
7940       break;
7941     }
7942     case scTruncate: {
7943       const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
7944       if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
7945         return ConstantExpr::getTrunc(CastOp, ST->getType());
7946       break;
7947     }
7948     case scAddExpr: {
7949       const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
7950       if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
7951         if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
7952           unsigned AS = PTy->getAddressSpace();
7953           Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
7954           C = ConstantExpr::getBitCast(C, DestPtrTy);
7955         }
7956         for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
7957           Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
7958           if (!C2) return nullptr;
7959 
7960           // First pointer!
7961           if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
7962             unsigned AS = C2->getType()->getPointerAddressSpace();
7963             std::swap(C, C2);
7964             Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
7965             // The offsets have been converted to bytes.  We can add bytes to an
7966             // i8* by GEP with the byte count in the first index.
7967             C = ConstantExpr::getBitCast(C, DestPtrTy);
7968           }
7969 
7970           // Don't bother trying to sum two pointers. We probably can't
7971           // statically compute a load that results from it anyway.
7972           if (C2->getType()->isPointerTy())
7973             return nullptr;
7974 
7975           if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
7976             if (PTy->getElementType()->isStructTy())
7977               C2 = ConstantExpr::getIntegerCast(
7978                   C2, Type::getInt32Ty(C->getContext()), true);
7979             C = ConstantExpr::getGetElementPtr(PTy->getElementType(), C, C2);
7980           } else
7981             C = ConstantExpr::getAdd(C, C2);
7982         }
7983         return C;
7984       }
7985       break;
7986     }
7987     case scMulExpr: {
7988       const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
7989       if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
7990         // Don't bother with pointers at all.
7991         if (C->getType()->isPointerTy()) return nullptr;
7992         for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
7993           Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
7994           if (!C2 || C2->getType()->isPointerTy()) return nullptr;
7995           C = ConstantExpr::getMul(C, C2);
7996         }
7997         return C;
7998       }
7999       break;
8000     }
8001     case scUDivExpr: {
8002       const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
8003       if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
8004         if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
8005           if (LHS->getType() == RHS->getType())
8006             return ConstantExpr::getUDiv(LHS, RHS);
8007       break;
8008     }
8009     case scSMaxExpr:
8010     case scUMaxExpr:
8011     case scSMinExpr:
8012     case scUMinExpr:
8013       break; // TODO: smax, umax, smin, umax.
8014   }
8015   return nullptr;
8016 }
8017 
computeSCEVAtScope(const SCEV * V,const Loop * L)8018 const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
8019   if (isa<SCEVConstant>(V)) return V;
8020 
8021   // If this instruction is evolved from a constant-evolving PHI, compute the
8022   // exit value from the loop without using SCEVs.
8023   if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
8024     if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
8025       if (PHINode *PN = dyn_cast<PHINode>(I)) {
8026         const Loop *LI = this->LI[I->getParent()];
8027         // Looking for loop exit value.
8028         if (LI && LI->getParentLoop() == L &&
8029             PN->getParent() == LI->getHeader()) {
8030           // Okay, there is no closed form solution for the PHI node.  Check
8031           // to see if the loop that contains it has a known backedge-taken
8032           // count.  If so, we may be able to force computation of the exit
8033           // value.
8034           const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
8035           // This trivial case can show up in some degenerate cases where
8036           // the incoming IR has not yet been fully simplified.
8037           if (BackedgeTakenCount->isZero()) {
8038             Value *InitValue = nullptr;
8039             bool MultipleInitValues = false;
8040             for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
8041               if (!LI->contains(PN->getIncomingBlock(i))) {
8042                 if (!InitValue)
8043                   InitValue = PN->getIncomingValue(i);
8044                 else if (InitValue != PN->getIncomingValue(i)) {
8045                   MultipleInitValues = true;
8046                   break;
8047                 }
8048               }
8049             }
8050             if (!MultipleInitValues && InitValue)
8051               return getSCEV(InitValue);
8052           }
8053           // Do we have a loop invariant value flowing around the backedge
8054           // for a loop which must execute the backedge?
8055           if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) &&
8056               isKnownPositive(BackedgeTakenCount) &&
8057               PN->getNumIncomingValues() == 2) {
8058 
8059             unsigned InLoopPred = LI->contains(PN->getIncomingBlock(0)) ? 0 : 1;
8060             Value *BackedgeVal = PN->getIncomingValue(InLoopPred);
8061             if (LI->isLoopInvariant(BackedgeVal))
8062               return getSCEV(BackedgeVal);
8063           }
8064           if (auto *BTCC = dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
8065             // Okay, we know how many times the containing loop executes.  If
8066             // this is a constant evolving PHI node, get the final value at
8067             // the specified iteration number.
8068             Constant *RV =
8069                 getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), LI);
8070             if (RV) return getSCEV(RV);
8071           }
8072         }
8073 
8074         // If there is a single-input Phi, evaluate it at our scope. If we can
8075         // prove that this replacement does not break LCSSA form, use new value.
8076         if (PN->getNumOperands() == 1) {
8077           const SCEV *Input = getSCEV(PN->getOperand(0));
8078           const SCEV *InputAtScope = getSCEVAtScope(Input, L);
8079           // TODO: We can generalize it using LI.replacementPreservesLCSSAForm,
8080           // for the simplest case just support constants.
8081           if (isa<SCEVConstant>(InputAtScope)) return InputAtScope;
8082         }
8083       }
8084 
8085       // Okay, this is an expression that we cannot symbolically evaluate
8086       // into a SCEV.  Check to see if it's possible to symbolically evaluate
8087       // the arguments into constants, and if so, try to constant propagate the
8088       // result.  This is particularly useful for computing loop exit values.
8089       if (CanConstantFold(I)) {
8090         SmallVector<Constant *, 4> Operands;
8091         bool MadeImprovement = false;
8092         for (Value *Op : I->operands()) {
8093           if (Constant *C = dyn_cast<Constant>(Op)) {
8094             Operands.push_back(C);
8095             continue;
8096           }
8097 
8098           // If any of the operands is non-constant and if they are
8099           // non-integer and non-pointer, don't even try to analyze them
8100           // with scev techniques.
8101           if (!isSCEVable(Op->getType()))
8102             return V;
8103 
8104           const SCEV *OrigV = getSCEV(Op);
8105           const SCEV *OpV = getSCEVAtScope(OrigV, L);
8106           MadeImprovement |= OrigV != OpV;
8107 
8108           Constant *C = BuildConstantFromSCEV(OpV);
8109           if (!C) return V;
8110           if (C->getType() != Op->getType())
8111             C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
8112                                                               Op->getType(),
8113                                                               false),
8114                                       C, Op->getType());
8115           Operands.push_back(C);
8116         }
8117 
8118         // Check to see if getSCEVAtScope actually made an improvement.
8119         if (MadeImprovement) {
8120           Constant *C = nullptr;
8121           const DataLayout &DL = getDataLayout();
8122           if (const CmpInst *CI = dyn_cast<CmpInst>(I))
8123             C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
8124                                                 Operands[1], DL, &TLI);
8125           else if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
8126             if (!LI->isVolatile())
8127               C = ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
8128           } else
8129             C = ConstantFoldInstOperands(I, Operands, DL, &TLI);
8130           if (!C) return V;
8131           return getSCEV(C);
8132         }
8133       }
8134     }
8135 
8136     // This is some other type of SCEVUnknown, just return it.
8137     return V;
8138   }
8139 
8140   if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
8141     // Avoid performing the look-up in the common case where the specified
8142     // expression has no loop-variant portions.
8143     for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
8144       const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
8145       if (OpAtScope != Comm->getOperand(i)) {
8146         // Okay, at least one of these operands is loop variant but might be
8147         // foldable.  Build a new instance of the folded commutative expression.
8148         SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
8149                                             Comm->op_begin()+i);
8150         NewOps.push_back(OpAtScope);
8151 
8152         for (++i; i != e; ++i) {
8153           OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
8154           NewOps.push_back(OpAtScope);
8155         }
8156         if (isa<SCEVAddExpr>(Comm))
8157           return getAddExpr(NewOps, Comm->getNoWrapFlags());
8158         if (isa<SCEVMulExpr>(Comm))
8159           return getMulExpr(NewOps, Comm->getNoWrapFlags());
8160         if (isa<SCEVMinMaxExpr>(Comm))
8161           return getMinMaxExpr(Comm->getSCEVType(), NewOps);
8162         llvm_unreachable("Unknown commutative SCEV type!");
8163       }
8164     }
8165     // If we got here, all operands are loop invariant.
8166     return Comm;
8167   }
8168 
8169   if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
8170     const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
8171     const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
8172     if (LHS == Div->getLHS() && RHS == Div->getRHS())
8173       return Div;   // must be loop invariant
8174     return getUDivExpr(LHS, RHS);
8175   }
8176 
8177   // If this is a loop recurrence for a loop that does not contain L, then we
8178   // are dealing with the final value computed by the loop.
8179   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
8180     // First, attempt to evaluate each operand.
8181     // Avoid performing the look-up in the common case where the specified
8182     // expression has no loop-variant portions.
8183     for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
8184       const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L);
8185       if (OpAtScope == AddRec->getOperand(i))
8186         continue;
8187 
8188       // Okay, at least one of these operands is loop variant but might be
8189       // foldable.  Build a new instance of the folded commutative expression.
8190       SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(),
8191                                           AddRec->op_begin()+i);
8192       NewOps.push_back(OpAtScope);
8193       for (++i; i != e; ++i)
8194         NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L));
8195 
8196       const SCEV *FoldedRec =
8197         getAddRecExpr(NewOps, AddRec->getLoop(),
8198                       AddRec->getNoWrapFlags(SCEV::FlagNW));
8199       AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec);
8200       // The addrec may be folded to a nonrecurrence, for example, if the
8201       // induction variable is multiplied by zero after constant folding. Go
8202       // ahead and return the folded value.
8203       if (!AddRec)
8204         return FoldedRec;
8205       break;
8206     }
8207 
8208     // If the scope is outside the addrec's loop, evaluate it by using the
8209     // loop exit value of the addrec.
8210     if (!AddRec->getLoop()->contains(L)) {
8211       // To evaluate this recurrence, we need to know how many times the AddRec
8212       // loop iterates.  Compute this now.
8213       const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
8214       if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
8215 
8216       // Then, evaluate the AddRec.
8217       return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
8218     }
8219 
8220     return AddRec;
8221   }
8222 
8223   if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
8224     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
8225     if (Op == Cast->getOperand())
8226       return Cast;  // must be loop invariant
8227     return getZeroExtendExpr(Op, Cast->getType());
8228   }
8229 
8230   if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
8231     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
8232     if (Op == Cast->getOperand())
8233       return Cast;  // must be loop invariant
8234     return getSignExtendExpr(Op, Cast->getType());
8235   }
8236 
8237   if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
8238     const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
8239     if (Op == Cast->getOperand())
8240       return Cast;  // must be loop invariant
8241     return getTruncateExpr(Op, Cast->getType());
8242   }
8243 
8244   llvm_unreachable("Unknown SCEV type!");
8245 }
8246 
getSCEVAtScope(Value * V,const Loop * L)8247 const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
8248   return getSCEVAtScope(getSCEV(V), L);
8249 }
8250 
stripInjectiveFunctions(const SCEV * S) const8251 const SCEV *ScalarEvolution::stripInjectiveFunctions(const SCEV *S) const {
8252   if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S))
8253     return stripInjectiveFunctions(ZExt->getOperand());
8254   if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S))
8255     return stripInjectiveFunctions(SExt->getOperand());
8256   return S;
8257 }
8258 
8259 /// Finds the minimum unsigned root of the following equation:
8260 ///
8261 ///     A * X = B (mod N)
8262 ///
8263 /// where N = 2^BW and BW is the common bit width of A and B. The signedness of
8264 /// A and B isn't important.
8265 ///
8266 /// If the equation does not have a solution, SCEVCouldNotCompute is returned.
SolveLinEquationWithOverflow(const APInt & A,const SCEV * B,ScalarEvolution & SE)8267 static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B,
8268                                                ScalarEvolution &SE) {
8269   uint32_t BW = A.getBitWidth();
8270   assert(BW == SE.getTypeSizeInBits(B->getType()));
8271   assert(A != 0 && "A must be non-zero.");
8272 
8273   // 1. D = gcd(A, N)
8274   //
8275   // The gcd of A and N may have only one prime factor: 2. The number of
8276   // trailing zeros in A is its multiplicity
8277   uint32_t Mult2 = A.countTrailingZeros();
8278   // D = 2^Mult2
8279 
8280   // 2. Check if B is divisible by D.
8281   //
8282   // B is divisible by D if and only if the multiplicity of prime factor 2 for B
8283   // is not less than multiplicity of this prime factor for D.
8284   if (SE.GetMinTrailingZeros(B) < Mult2)
8285     return SE.getCouldNotCompute();
8286 
8287   // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
8288   // modulo (N / D).
8289   //
8290   // If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent
8291   // (N / D) in general. The inverse itself always fits into BW bits, though,
8292   // so we immediately truncate it.
8293   APInt AD = A.lshr(Mult2).zext(BW + 1);  // AD = A / D
8294   APInt Mod(BW + 1, 0);
8295   Mod.setBit(BW - Mult2);  // Mod = N / D
8296   APInt I = AD.multiplicativeInverse(Mod).trunc(BW);
8297 
8298   // 4. Compute the minimum unsigned root of the equation:
8299   // I * (B / D) mod (N / D)
8300   // To simplify the computation, we factor out the divide by D:
8301   // (I * B mod N) / D
8302   const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2));
8303   return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D);
8304 }
8305 
8306 /// For a given quadratic addrec, generate coefficients of the corresponding
8307 /// quadratic equation, multiplied by a common value to ensure that they are
8308 /// integers.
8309 /// The returned value is a tuple { A, B, C, M, BitWidth }, where
8310 /// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C
8311 /// were multiplied by, and BitWidth is the bit width of the original addrec
8312 /// coefficients.
8313 /// This function returns None if the addrec coefficients are not compile-
8314 /// time constants.
8315 static Optional<std::tuple<APInt, APInt, APInt, APInt, unsigned>>
GetQuadraticEquation(const SCEVAddRecExpr * AddRec)8316 GetQuadraticEquation(const SCEVAddRecExpr *AddRec) {
8317   assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
8318   const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
8319   const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
8320   const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
8321   LLVM_DEBUG(dbgs() << __func__ << ": analyzing quadratic addrec: "
8322                     << *AddRec << '\n');
8323 
8324   // We currently can only solve this if the coefficients are constants.
8325   if (!LC || !MC || !NC) {
8326     LLVM_DEBUG(dbgs() << __func__ << ": coefficients are not constant\n");
8327     return None;
8328   }
8329 
8330   APInt L = LC->getAPInt();
8331   APInt M = MC->getAPInt();
8332   APInt N = NC->getAPInt();
8333   assert(!N.isNullValue() && "This is not a quadratic addrec");
8334 
8335   unsigned BitWidth = LC->getAPInt().getBitWidth();
8336   unsigned NewWidth = BitWidth + 1;
8337   LLVM_DEBUG(dbgs() << __func__ << ": addrec coeff bw: "
8338                     << BitWidth << '\n');
8339   // The sign-extension (as opposed to a zero-extension) here matches the
8340   // extension used in SolveQuadraticEquationWrap (with the same motivation).
8341   N = N.sext(NewWidth);
8342   M = M.sext(NewWidth);
8343   L = L.sext(NewWidth);
8344 
8345   // The increments are M, M+N, M+2N, ..., so the accumulated values are
8346   //   L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is,
8347   //   L+M, L+2M+N, L+3M+3N, ...
8348   // After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N.
8349   //
8350   // The equation Acc = 0 is then
8351   //   L + nM + n(n-1)/2 N = 0,  or  2L + 2M n + n(n-1) N = 0.
8352   // In a quadratic form it becomes:
8353   //   N n^2 + (2M-N) n + 2L = 0.
8354 
8355   APInt A = N;
8356   APInt B = 2 * M - A;
8357   APInt C = 2 * L;
8358   APInt T = APInt(NewWidth, 2);
8359   LLVM_DEBUG(dbgs() << __func__ << ": equation " << A << "x^2 + " << B
8360                     << "x + " << C << ", coeff bw: " << NewWidth
8361                     << ", multiplied by " << T << '\n');
8362   return std::make_tuple(A, B, C, T, BitWidth);
8363 }
8364 
8365 /// Helper function to compare optional APInts:
8366 /// (a) if X and Y both exist, return min(X, Y),
8367 /// (b) if neither X nor Y exist, return None,
8368 /// (c) if exactly one of X and Y exists, return that value.
MinOptional(Optional<APInt> X,Optional<APInt> Y)8369 static Optional<APInt> MinOptional(Optional<APInt> X, Optional<APInt> Y) {
8370   if (X.hasValue() && Y.hasValue()) {
8371     unsigned W = std::max(X->getBitWidth(), Y->getBitWidth());
8372     APInt XW = X->sextOrSelf(W);
8373     APInt YW = Y->sextOrSelf(W);
8374     return XW.slt(YW) ? *X : *Y;
8375   }
8376   if (!X.hasValue() && !Y.hasValue())
8377     return None;
8378   return X.hasValue() ? *X : *Y;
8379 }
8380 
8381 /// Helper function to truncate an optional APInt to a given BitWidth.
8382 /// When solving addrec-related equations, it is preferable to return a value
8383 /// that has the same bit width as the original addrec's coefficients. If the
8384 /// solution fits in the original bit width, truncate it (except for i1).
8385 /// Returning a value of a different bit width may inhibit some optimizations.
8386 ///
8387 /// In general, a solution to a quadratic equation generated from an addrec
8388 /// may require BW+1 bits, where BW is the bit width of the addrec's
8389 /// coefficients. The reason is that the coefficients of the quadratic
8390 /// equation are BW+1 bits wide (to avoid truncation when converting from
8391 /// the addrec to the equation).
TruncIfPossible(Optional<APInt> X,unsigned BitWidth)8392 static Optional<APInt> TruncIfPossible(Optional<APInt> X, unsigned BitWidth) {
8393   if (!X.hasValue())
8394     return None;
8395   unsigned W = X->getBitWidth();
8396   if (BitWidth > 1 && BitWidth < W && X->isIntN(BitWidth))
8397     return X->trunc(BitWidth);
8398   return X;
8399 }
8400 
8401 /// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n
8402 /// iterations. The values L, M, N are assumed to be signed, and they
8403 /// should all have the same bit widths.
8404 /// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW,
8405 /// where BW is the bit width of the addrec's coefficients.
8406 /// If the calculated value is a BW-bit integer (for BW > 1), it will be
8407 /// returned as such, otherwise the bit width of the returned value may
8408 /// be greater than BW.
8409 ///
8410 /// This function returns None if
8411 /// (a) the addrec coefficients are not constant, or
8412 /// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases
8413 ///     like x^2 = 5, no integer solutions exist, in other cases an integer
8414 ///     solution may exist, but SolveQuadraticEquationWrap may fail to find it.
8415 static Optional<APInt>
SolveQuadraticAddRecExact(const SCEVAddRecExpr * AddRec,ScalarEvolution & SE)8416 SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
8417   APInt A, B, C, M;
8418   unsigned BitWidth;
8419   auto T = GetQuadraticEquation(AddRec);
8420   if (!T.hasValue())
8421     return None;
8422 
8423   std::tie(A, B, C, M, BitWidth) = *T;
8424   LLVM_DEBUG(dbgs() << __func__ << ": solving for unsigned overflow\n");
8425   Optional<APInt> X = APIntOps::SolveQuadraticEquationWrap(A, B, C, BitWidth+1);
8426   if (!X.hasValue())
8427     return None;
8428 
8429   ConstantInt *CX = ConstantInt::get(SE.getContext(), *X);
8430   ConstantInt *V = EvaluateConstantChrecAtConstant(AddRec, CX, SE);
8431   if (!V->isZero())
8432     return None;
8433 
8434   return TruncIfPossible(X, BitWidth);
8435 }
8436 
8437 /// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n
8438 /// iterations. The values M, N are assumed to be signed, and they
8439 /// should all have the same bit widths.
8440 /// Find the least n such that c(n) does not belong to the given range,
8441 /// while c(n-1) does.
8442 ///
8443 /// This function returns None if
8444 /// (a) the addrec coefficients are not constant, or
8445 /// (b) SolveQuadraticEquationWrap was unable to find a solution for the
8446 ///     bounds of the range.
8447 static Optional<APInt>
SolveQuadraticAddRecRange(const SCEVAddRecExpr * AddRec,const ConstantRange & Range,ScalarEvolution & SE)8448 SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec,
8449                           const ConstantRange &Range, ScalarEvolution &SE) {
8450   assert(AddRec->getOperand(0)->isZero() &&
8451          "Starting value of addrec should be 0");
8452   LLVM_DEBUG(dbgs() << __func__ << ": solving boundary crossing for range "
8453                     << Range << ", addrec " << *AddRec << '\n');
8454   // This case is handled in getNumIterationsInRange. Here we can assume that
8455   // we start in the range.
8456   assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) &&
8457          "Addrec's initial value should be in range");
8458 
8459   APInt A, B, C, M;
8460   unsigned BitWidth;
8461   auto T = GetQuadraticEquation(AddRec);
8462   if (!T.hasValue())
8463     return None;
8464 
8465   // Be careful about the return value: there can be two reasons for not
8466   // returning an actual number. First, if no solutions to the equations
8467   // were found, and second, if the solutions don't leave the given range.
8468   // The first case means that the actual solution is "unknown", the second
8469   // means that it's known, but not valid. If the solution is unknown, we
8470   // cannot make any conclusions.
8471   // Return a pair: the optional solution and a flag indicating if the
8472   // solution was found.
8473   auto SolveForBoundary = [&](APInt Bound) -> std::pair<Optional<APInt>,bool> {
8474     // Solve for signed overflow and unsigned overflow, pick the lower
8475     // solution.
8476     LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: checking boundary "
8477                       << Bound << " (before multiplying by " << M << ")\n");
8478     Bound *= M; // The quadratic equation multiplier.
8479 
8480     Optional<APInt> SO = None;
8481     if (BitWidth > 1) {
8482       LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
8483                            "signed overflow\n");
8484       SO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, BitWidth);
8485     }
8486     LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
8487                          "unsigned overflow\n");
8488     Optional<APInt> UO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound,
8489                                                               BitWidth+1);
8490 
8491     auto LeavesRange = [&] (const APInt &X) {
8492       ConstantInt *C0 = ConstantInt::get(SE.getContext(), X);
8493       ConstantInt *V0 = EvaluateConstantChrecAtConstant(AddRec, C0, SE);
8494       if (Range.contains(V0->getValue()))
8495         return false;
8496       // X should be at least 1, so X-1 is non-negative.
8497       ConstantInt *C1 = ConstantInt::get(SE.getContext(), X-1);
8498       ConstantInt *V1 = EvaluateConstantChrecAtConstant(AddRec, C1, SE);
8499       if (Range.contains(V1->getValue()))
8500         return true;
8501       return false;
8502     };
8503 
8504     // If SolveQuadraticEquationWrap returns None, it means that there can
8505     // be a solution, but the function failed to find it. We cannot treat it
8506     // as "no solution".
8507     if (!SO.hasValue() || !UO.hasValue())
8508       return { None, false };
8509 
8510     // Check the smaller value first to see if it leaves the range.
8511     // At this point, both SO and UO must have values.
8512     Optional<APInt> Min = MinOptional(SO, UO);
8513     if (LeavesRange(*Min))
8514       return { Min, true };
8515     Optional<APInt> Max = Min == SO ? UO : SO;
8516     if (LeavesRange(*Max))
8517       return { Max, true };
8518 
8519     // Solutions were found, but were eliminated, hence the "true".
8520     return { None, true };
8521   };
8522 
8523   std::tie(A, B, C, M, BitWidth) = *T;
8524   // Lower bound is inclusive, subtract 1 to represent the exiting value.
8525   APInt Lower = Range.getLower().sextOrSelf(A.getBitWidth()) - 1;
8526   APInt Upper = Range.getUpper().sextOrSelf(A.getBitWidth());
8527   auto SL = SolveForBoundary(Lower);
8528   auto SU = SolveForBoundary(Upper);
8529   // If any of the solutions was unknown, no meaninigful conclusions can
8530   // be made.
8531   if (!SL.second || !SU.second)
8532     return None;
8533 
8534   // Claim: The correct solution is not some value between Min and Max.
8535   //
8536   // Justification: Assuming that Min and Max are different values, one of
8537   // them is when the first signed overflow happens, the other is when the
8538   // first unsigned overflow happens. Crossing the range boundary is only
8539   // possible via an overflow (treating 0 as a special case of it, modeling
8540   // an overflow as crossing k*2^W for some k).
8541   //
8542   // The interesting case here is when Min was eliminated as an invalid
8543   // solution, but Max was not. The argument is that if there was another
8544   // overflow between Min and Max, it would also have been eliminated if
8545   // it was considered.
8546   //
8547   // For a given boundary, it is possible to have two overflows of the same
8548   // type (signed/unsigned) without having the other type in between: this
8549   // can happen when the vertex of the parabola is between the iterations
8550   // corresponding to the overflows. This is only possible when the two
8551   // overflows cross k*2^W for the same k. In such case, if the second one
8552   // left the range (and was the first one to do so), the first overflow
8553   // would have to enter the range, which would mean that either we had left
8554   // the range before or that we started outside of it. Both of these cases
8555   // are contradictions.
8556   //
8557   // Claim: In the case where SolveForBoundary returns None, the correct
8558   // solution is not some value between the Max for this boundary and the
8559   // Min of the other boundary.
8560   //
8561   // Justification: Assume that we had such Max_A and Min_B corresponding
8562   // to range boundaries A and B and such that Max_A < Min_B. If there was
8563   // a solution between Max_A and Min_B, it would have to be caused by an
8564   // overflow corresponding to either A or B. It cannot correspond to B,
8565   // since Min_B is the first occurrence of such an overflow. If it
8566   // corresponded to A, it would have to be either a signed or an unsigned
8567   // overflow that is larger than both eliminated overflows for A. But
8568   // between the eliminated overflows and this overflow, the values would
8569   // cover the entire value space, thus crossing the other boundary, which
8570   // is a contradiction.
8571 
8572   return TruncIfPossible(MinOptional(SL.first, SU.first), BitWidth);
8573 }
8574 
8575 ScalarEvolution::ExitLimit
howFarToZero(const SCEV * V,const Loop * L,bool ControlsExit,bool AllowPredicates)8576 ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
8577                               bool AllowPredicates) {
8578 
8579   // This is only used for loops with a "x != y" exit test. The exit condition
8580   // is now expressed as a single expression, V = x-y. So the exit test is
8581   // effectively V != 0.  We know and take advantage of the fact that this
8582   // expression only being used in a comparison by zero context.
8583 
8584   SmallPtrSet<const SCEVPredicate *, 4> Predicates;
8585   // If the value is a constant
8586   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
8587     // If the value is already zero, the branch will execute zero times.
8588     if (C->getValue()->isZero()) return C;
8589     return getCouldNotCompute();  // Otherwise it will loop infinitely.
8590   }
8591 
8592   const SCEVAddRecExpr *AddRec =
8593       dyn_cast<SCEVAddRecExpr>(stripInjectiveFunctions(V));
8594 
8595   if (!AddRec && AllowPredicates)
8596     // Try to make this an AddRec using runtime tests, in the first X
8597     // iterations of this loop, where X is the SCEV expression found by the
8598     // algorithm below.
8599     AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates);
8600 
8601   if (!AddRec || AddRec->getLoop() != L)
8602     return getCouldNotCompute();
8603 
8604   // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
8605   // the quadratic equation to solve it.
8606   if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
8607     // We can only use this value if the chrec ends up with an exact zero
8608     // value at this index.  When solving for "X*X != 5", for example, we
8609     // should not accept a root of 2.
8610     if (auto S = SolveQuadraticAddRecExact(AddRec, *this)) {
8611       const auto *R = cast<SCEVConstant>(getConstant(S.getValue()));
8612       return ExitLimit(R, R, false, Predicates);
8613     }
8614     return getCouldNotCompute();
8615   }
8616 
8617   // Otherwise we can only handle this if it is affine.
8618   if (!AddRec->isAffine())
8619     return getCouldNotCompute();
8620 
8621   // If this is an affine expression, the execution count of this branch is
8622   // the minimum unsigned root of the following equation:
8623   //
8624   //     Start + Step*N = 0 (mod 2^BW)
8625   //
8626   // equivalent to:
8627   //
8628   //             Step*N = -Start (mod 2^BW)
8629   //
8630   // where BW is the common bit width of Start and Step.
8631 
8632   // Get the initial value for the loop.
8633   const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
8634   const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
8635 
8636   // For now we handle only constant steps.
8637   //
8638   // TODO: Handle a nonconstant Step given AddRec<NUW>. If the
8639   // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap
8640   // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step.
8641   // We have not yet seen any such cases.
8642   const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
8643   if (!StepC || StepC->getValue()->isZero())
8644     return getCouldNotCompute();
8645 
8646   // For positive steps (counting up until unsigned overflow):
8647   //   N = -Start/Step (as unsigned)
8648   // For negative steps (counting down to zero):
8649   //   N = Start/-Step
8650   // First compute the unsigned distance from zero in the direction of Step.
8651   bool CountDown = StepC->getAPInt().isNegative();
8652   const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start);
8653 
8654   // Handle unitary steps, which cannot wraparound.
8655   // 1*N = -Start; -1*N = Start (mod 2^BW), so:
8656   //   N = Distance (as unsigned)
8657   if (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne()) {
8658     APInt MaxBECount = getUnsignedRangeMax(Distance);
8659 
8660     // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
8661     // we end up with a loop whose backedge-taken count is n - 1.  Detect this
8662     // case, and see if we can improve the bound.
8663     //
8664     // Explicitly handling this here is necessary because getUnsignedRange
8665     // isn't context-sensitive; it doesn't know that we only care about the
8666     // range inside the loop.
8667     const SCEV *Zero = getZero(Distance->getType());
8668     const SCEV *One = getOne(Distance->getType());
8669     const SCEV *DistancePlusOne = getAddExpr(Distance, One);
8670     if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
8671       // If Distance + 1 doesn't overflow, we can compute the maximum distance
8672       // as "unsigned_max(Distance + 1) - 1".
8673       ConstantRange CR = getUnsignedRange(DistancePlusOne);
8674       MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1);
8675     }
8676     return ExitLimit(Distance, getConstant(MaxBECount), false, Predicates);
8677   }
8678 
8679   // If the condition controls loop exit (the loop exits only if the expression
8680   // is true) and the addition is no-wrap we can use unsigned divide to
8681   // compute the backedge count.  In this case, the step may not divide the
8682   // distance, but we don't care because if the condition is "missed" the loop
8683   // will have undefined behavior due to wrapping.
8684   if (ControlsExit && AddRec->hasNoSelfWrap() &&
8685       loopHasNoAbnormalExits(AddRec->getLoop())) {
8686     const SCEV *Exact =
8687         getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
8688     const SCEV *Max =
8689         Exact == getCouldNotCompute()
8690             ? Exact
8691             : getConstant(getUnsignedRangeMax(Exact));
8692     return ExitLimit(Exact, Max, false, Predicates);
8693   }
8694 
8695   // Solve the general equation.
8696   const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
8697                                                getNegativeSCEV(Start), *this);
8698   const SCEV *M = E == getCouldNotCompute()
8699                       ? E
8700                       : getConstant(getUnsignedRangeMax(E));
8701   return ExitLimit(E, M, false, Predicates);
8702 }
8703 
8704 ScalarEvolution::ExitLimit
howFarToNonZero(const SCEV * V,const Loop * L)8705 ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) {
8706   // Loops that look like: while (X == 0) are very strange indeed.  We don't
8707   // handle them yet except for the trivial case.  This could be expanded in the
8708   // future as needed.
8709 
8710   // If the value is a constant, check to see if it is known to be non-zero
8711   // already.  If so, the backedge will execute zero times.
8712   if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
8713     if (!C->getValue()->isZero())
8714       return getZero(C->getType());
8715     return getCouldNotCompute();  // Otherwise it will loop infinitely.
8716   }
8717 
8718   // We could implement others, but I really doubt anyone writes loops like
8719   // this, and if they did, they would already be constant folded.
8720   return getCouldNotCompute();
8721 }
8722 
8723 std::pair<BasicBlock *, BasicBlock *>
getPredecessorWithUniqueSuccessorForBB(BasicBlock * BB)8724 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
8725   // If the block has a unique predecessor, then there is no path from the
8726   // predecessor to the block that does not go through the direct edge
8727   // from the predecessor to the block.
8728   if (BasicBlock *Pred = BB->getSinglePredecessor())
8729     return {Pred, BB};
8730 
8731   // A loop's header is defined to be a block that dominates the loop.
8732   // If the header has a unique predecessor outside the loop, it must be
8733   // a block that has exactly one successor that can reach the loop.
8734   if (Loop *L = LI.getLoopFor(BB))
8735     return {L->getLoopPredecessor(), L->getHeader()};
8736 
8737   return {nullptr, nullptr};
8738 }
8739 
8740 /// SCEV structural equivalence is usually sufficient for testing whether two
8741 /// expressions are equal, however for the purposes of looking for a condition
8742 /// guarding a loop, it can be useful to be a little more general, since a
8743 /// front-end may have replicated the controlling expression.
HasSameValue(const SCEV * A,const SCEV * B)8744 static bool HasSameValue(const SCEV *A, const SCEV *B) {
8745   // Quick check to see if they are the same SCEV.
8746   if (A == B) return true;
8747 
8748   auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) {
8749     // Not all instructions that are "identical" compute the same value.  For
8750     // instance, two distinct alloca instructions allocating the same type are
8751     // identical and do not read memory; but compute distinct values.
8752     return A->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A));
8753   };
8754 
8755   // Otherwise, if they're both SCEVUnknown, it's possible that they hold
8756   // two different instructions with the same value. Check for this case.
8757   if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
8758     if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
8759       if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
8760         if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
8761           if (ComputesEqualValues(AI, BI))
8762             return true;
8763 
8764   // Otherwise assume they may have a different value.
8765   return false;
8766 }
8767 
SimplifyICmpOperands(ICmpInst::Predicate & Pred,const SCEV * & LHS,const SCEV * & RHS,unsigned Depth)8768 bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
8769                                            const SCEV *&LHS, const SCEV *&RHS,
8770                                            unsigned Depth) {
8771   bool Changed = false;
8772   // Simplifies ICMP to trivial true or false by turning it into '0 == 0' or
8773   // '0 != 0'.
8774   auto TrivialCase = [&](bool TriviallyTrue) {
8775     LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
8776     Pred = TriviallyTrue ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
8777     return true;
8778   };
8779   // If we hit the max recursion limit bail out.
8780   if (Depth >= 3)
8781     return false;
8782 
8783   // Canonicalize a constant to the right side.
8784   if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
8785     // Check for both operands constant.
8786     if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
8787       if (ConstantExpr::getICmp(Pred,
8788                                 LHSC->getValue(),
8789                                 RHSC->getValue())->isNullValue())
8790         return TrivialCase(false);
8791       else
8792         return TrivialCase(true);
8793     }
8794     // Otherwise swap the operands to put the constant on the right.
8795     std::swap(LHS, RHS);
8796     Pred = ICmpInst::getSwappedPredicate(Pred);
8797     Changed = true;
8798   }
8799 
8800   // If we're comparing an addrec with a value which is loop-invariant in the
8801   // addrec's loop, put the addrec on the left. Also make a dominance check,
8802   // as both operands could be addrecs loop-invariant in each other's loop.
8803   if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) {
8804     const Loop *L = AR->getLoop();
8805     if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) {
8806       std::swap(LHS, RHS);
8807       Pred = ICmpInst::getSwappedPredicate(Pred);
8808       Changed = true;
8809     }
8810   }
8811 
8812   // If there's a constant operand, canonicalize comparisons with boundary
8813   // cases, and canonicalize *-or-equal comparisons to regular comparisons.
8814   if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
8815     const APInt &RA = RC->getAPInt();
8816 
8817     bool SimplifiedByConstantRange = false;
8818 
8819     if (!ICmpInst::isEquality(Pred)) {
8820       ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA);
8821       if (ExactCR.isFullSet())
8822         return TrivialCase(true);
8823       else if (ExactCR.isEmptySet())
8824         return TrivialCase(false);
8825 
8826       APInt NewRHS;
8827       CmpInst::Predicate NewPred;
8828       if (ExactCR.getEquivalentICmp(NewPred, NewRHS) &&
8829           ICmpInst::isEquality(NewPred)) {
8830         // We were able to convert an inequality to an equality.
8831         Pred = NewPred;
8832         RHS = getConstant(NewRHS);
8833         Changed = SimplifiedByConstantRange = true;
8834       }
8835     }
8836 
8837     if (!SimplifiedByConstantRange) {
8838       switch (Pred) {
8839       default:
8840         break;
8841       case ICmpInst::ICMP_EQ:
8842       case ICmpInst::ICMP_NE:
8843         // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
8844         if (!RA)
8845           if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
8846             if (const SCEVMulExpr *ME =
8847                     dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
8848               if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
8849                   ME->getOperand(0)->isAllOnesValue()) {
8850                 RHS = AE->getOperand(1);
8851                 LHS = ME->getOperand(1);
8852                 Changed = true;
8853               }
8854         break;
8855 
8856 
8857         // The "Should have been caught earlier!" messages refer to the fact
8858         // that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
8859         // should have fired on the corresponding cases, and canonicalized the
8860         // check to trivial case.
8861 
8862       case ICmpInst::ICMP_UGE:
8863         assert(!RA.isMinValue() && "Should have been caught earlier!");
8864         Pred = ICmpInst::ICMP_UGT;
8865         RHS = getConstant(RA - 1);
8866         Changed = true;
8867         break;
8868       case ICmpInst::ICMP_ULE:
8869         assert(!RA.isMaxValue() && "Should have been caught earlier!");
8870         Pred = ICmpInst::ICMP_ULT;
8871         RHS = getConstant(RA + 1);
8872         Changed = true;
8873         break;
8874       case ICmpInst::ICMP_SGE:
8875         assert(!RA.isMinSignedValue() && "Should have been caught earlier!");
8876         Pred = ICmpInst::ICMP_SGT;
8877         RHS = getConstant(RA - 1);
8878         Changed = true;
8879         break;
8880       case ICmpInst::ICMP_SLE:
8881         assert(!RA.isMaxSignedValue() && "Should have been caught earlier!");
8882         Pred = ICmpInst::ICMP_SLT;
8883         RHS = getConstant(RA + 1);
8884         Changed = true;
8885         break;
8886       }
8887     }
8888   }
8889 
8890   // Check for obvious equality.
8891   if (HasSameValue(LHS, RHS)) {
8892     if (ICmpInst::isTrueWhenEqual(Pred))
8893       return TrivialCase(true);
8894     if (ICmpInst::isFalseWhenEqual(Pred))
8895       return TrivialCase(false);
8896   }
8897 
8898   // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
8899   // adding or subtracting 1 from one of the operands.
8900   switch (Pred) {
8901   case ICmpInst::ICMP_SLE:
8902     if (!getSignedRangeMax(RHS).isMaxSignedValue()) {
8903       RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
8904                        SCEV::FlagNSW);
8905       Pred = ICmpInst::ICMP_SLT;
8906       Changed = true;
8907     } else if (!getSignedRangeMin(LHS).isMinSignedValue()) {
8908       LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS,
8909                        SCEV::FlagNSW);
8910       Pred = ICmpInst::ICMP_SLT;
8911       Changed = true;
8912     }
8913     break;
8914   case ICmpInst::ICMP_SGE:
8915     if (!getSignedRangeMin(RHS).isMinSignedValue()) {
8916       RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS,
8917                        SCEV::FlagNSW);
8918       Pred = ICmpInst::ICMP_SGT;
8919       Changed = true;
8920     } else if (!getSignedRangeMax(LHS).isMaxSignedValue()) {
8921       LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
8922                        SCEV::FlagNSW);
8923       Pred = ICmpInst::ICMP_SGT;
8924       Changed = true;
8925     }
8926     break;
8927   case ICmpInst::ICMP_ULE:
8928     if (!getUnsignedRangeMax(RHS).isMaxValue()) {
8929       RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
8930                        SCEV::FlagNUW);
8931       Pred = ICmpInst::ICMP_ULT;
8932       Changed = true;
8933     } else if (!getUnsignedRangeMin(LHS).isMinValue()) {
8934       LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS);
8935       Pred = ICmpInst::ICMP_ULT;
8936       Changed = true;
8937     }
8938     break;
8939   case ICmpInst::ICMP_UGE:
8940     if (!getUnsignedRangeMin(RHS).isMinValue()) {
8941       RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
8942       Pred = ICmpInst::ICMP_UGT;
8943       Changed = true;
8944     } else if (!getUnsignedRangeMax(LHS).isMaxValue()) {
8945       LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
8946                        SCEV::FlagNUW);
8947       Pred = ICmpInst::ICMP_UGT;
8948       Changed = true;
8949     }
8950     break;
8951   default:
8952     break;
8953   }
8954 
8955   // TODO: More simplifications are possible here.
8956 
8957   // Recursively simplify until we either hit a recursion limit or nothing
8958   // changes.
8959   if (Changed)
8960     return SimplifyICmpOperands(Pred, LHS, RHS, Depth+1);
8961 
8962   return Changed;
8963 }
8964 
isKnownNegative(const SCEV * S)8965 bool ScalarEvolution::isKnownNegative(const SCEV *S) {
8966   return getSignedRangeMax(S).isNegative();
8967 }
8968 
isKnownPositive(const SCEV * S)8969 bool ScalarEvolution::isKnownPositive(const SCEV *S) {
8970   return getSignedRangeMin(S).isStrictlyPositive();
8971 }
8972 
isKnownNonNegative(const SCEV * S)8973 bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
8974   return !getSignedRangeMin(S).isNegative();
8975 }
8976 
isKnownNonPositive(const SCEV * S)8977 bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
8978   return !getSignedRangeMax(S).isStrictlyPositive();
8979 }
8980 
isKnownNonZero(const SCEV * S)8981 bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
8982   return isKnownNegative(S) || isKnownPositive(S);
8983 }
8984 
8985 std::pair<const SCEV *, const SCEV *>
SplitIntoInitAndPostInc(const Loop * L,const SCEV * S)8986 ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) {
8987   // Compute SCEV on entry of loop L.
8988   const SCEV *Start = SCEVInitRewriter::rewrite(S, L, *this);
8989   if (Start == getCouldNotCompute())
8990     return { Start, Start };
8991   // Compute post increment SCEV for loop L.
8992   const SCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, *this);
8993   assert(PostInc != getCouldNotCompute() && "Unexpected could not compute");
8994   return { Start, PostInc };
8995 }
8996 
isKnownViaInduction(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)8997 bool ScalarEvolution::isKnownViaInduction(ICmpInst::Predicate Pred,
8998                                           const SCEV *LHS, const SCEV *RHS) {
8999   // First collect all loops.
9000   SmallPtrSet<const Loop *, 8> LoopsUsed;
9001   getUsedLoops(LHS, LoopsUsed);
9002   getUsedLoops(RHS, LoopsUsed);
9003 
9004   if (LoopsUsed.empty())
9005     return false;
9006 
9007   // Domination relationship must be a linear order on collected loops.
9008 #ifndef NDEBUG
9009   for (auto *L1 : LoopsUsed)
9010     for (auto *L2 : LoopsUsed)
9011       assert((DT.dominates(L1->getHeader(), L2->getHeader()) ||
9012               DT.dominates(L2->getHeader(), L1->getHeader())) &&
9013              "Domination relationship is not a linear order");
9014 #endif
9015 
9016   const Loop *MDL =
9017       *std::max_element(LoopsUsed.begin(), LoopsUsed.end(),
9018                         [&](const Loop *L1, const Loop *L2) {
9019          return DT.properlyDominates(L1->getHeader(), L2->getHeader());
9020        });
9021 
9022   // Get init and post increment value for LHS.
9023   auto SplitLHS = SplitIntoInitAndPostInc(MDL, LHS);
9024   // if LHS contains unknown non-invariant SCEV then bail out.
9025   if (SplitLHS.first == getCouldNotCompute())
9026     return false;
9027   assert (SplitLHS.second != getCouldNotCompute() && "Unexpected CNC");
9028   // Get init and post increment value for RHS.
9029   auto SplitRHS = SplitIntoInitAndPostInc(MDL, RHS);
9030   // if RHS contains unknown non-invariant SCEV then bail out.
9031   if (SplitRHS.first == getCouldNotCompute())
9032     return false;
9033   assert (SplitRHS.second != getCouldNotCompute() && "Unexpected CNC");
9034   // It is possible that init SCEV contains an invariant load but it does
9035   // not dominate MDL and is not available at MDL loop entry, so we should
9036   // check it here.
9037   if (!isAvailableAtLoopEntry(SplitLHS.first, MDL) ||
9038       !isAvailableAtLoopEntry(SplitRHS.first, MDL))
9039     return false;
9040 
9041   // It seems backedge guard check is faster than entry one so in some cases
9042   // it can speed up whole estimation by short circuit
9043   return isLoopBackedgeGuardedByCond(MDL, Pred, SplitLHS.second,
9044                                      SplitRHS.second) &&
9045          isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first);
9046 }
9047 
isKnownPredicate(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)9048 bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
9049                                        const SCEV *LHS, const SCEV *RHS) {
9050   // Canonicalize the inputs first.
9051   (void)SimplifyICmpOperands(Pred, LHS, RHS);
9052 
9053   if (isKnownViaInduction(Pred, LHS, RHS))
9054     return true;
9055 
9056   if (isKnownPredicateViaSplitting(Pred, LHS, RHS))
9057     return true;
9058 
9059   // Otherwise see what can be done with some simple reasoning.
9060   return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS);
9061 }
9062 
isKnownOnEveryIteration(ICmpInst::Predicate Pred,const SCEVAddRecExpr * LHS,const SCEV * RHS)9063 bool ScalarEvolution::isKnownOnEveryIteration(ICmpInst::Predicate Pred,
9064                                               const SCEVAddRecExpr *LHS,
9065                                               const SCEV *RHS) {
9066   const Loop *L = LHS->getLoop();
9067   return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) &&
9068          isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS);
9069 }
9070 
isMonotonicPredicate(const SCEVAddRecExpr * LHS,ICmpInst::Predicate Pred,bool & Increasing)9071 bool ScalarEvolution::isMonotonicPredicate(const SCEVAddRecExpr *LHS,
9072                                            ICmpInst::Predicate Pred,
9073                                            bool &Increasing) {
9074   bool Result = isMonotonicPredicateImpl(LHS, Pred, Increasing);
9075 
9076 #ifndef NDEBUG
9077   // Verify an invariant: inverting the predicate should turn a monotonically
9078   // increasing change to a monotonically decreasing one, and vice versa.
9079   bool IncreasingSwapped;
9080   bool ResultSwapped = isMonotonicPredicateImpl(
9081       LHS, ICmpInst::getSwappedPredicate(Pred), IncreasingSwapped);
9082 
9083   assert(Result == ResultSwapped && "should be able to analyze both!");
9084   if (ResultSwapped)
9085     assert(Increasing == !IncreasingSwapped &&
9086            "monotonicity should flip as we flip the predicate");
9087 #endif
9088 
9089   return Result;
9090 }
9091 
isMonotonicPredicateImpl(const SCEVAddRecExpr * LHS,ICmpInst::Predicate Pred,bool & Increasing)9092 bool ScalarEvolution::isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS,
9093                                                ICmpInst::Predicate Pred,
9094                                                bool &Increasing) {
9095 
9096   // A zero step value for LHS means the induction variable is essentially a
9097   // loop invariant value. We don't really depend on the predicate actually
9098   // flipping from false to true (for increasing predicates, and the other way
9099   // around for decreasing predicates), all we care about is that *if* the
9100   // predicate changes then it only changes from false to true.
9101   //
9102   // A zero step value in itself is not very useful, but there may be places
9103   // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be
9104   // as general as possible.
9105 
9106   switch (Pred) {
9107   default:
9108     return false; // Conservative answer
9109 
9110   case ICmpInst::ICMP_UGT:
9111   case ICmpInst::ICMP_UGE:
9112   case ICmpInst::ICMP_ULT:
9113   case ICmpInst::ICMP_ULE:
9114     if (!LHS->hasNoUnsignedWrap())
9115       return false;
9116 
9117     Increasing = Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE;
9118     return true;
9119 
9120   case ICmpInst::ICMP_SGT:
9121   case ICmpInst::ICMP_SGE:
9122   case ICmpInst::ICMP_SLT:
9123   case ICmpInst::ICMP_SLE: {
9124     if (!LHS->hasNoSignedWrap())
9125       return false;
9126 
9127     const SCEV *Step = LHS->getStepRecurrence(*this);
9128 
9129     if (isKnownNonNegative(Step)) {
9130       Increasing = Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE;
9131       return true;
9132     }
9133 
9134     if (isKnownNonPositive(Step)) {
9135       Increasing = Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE;
9136       return true;
9137     }
9138 
9139     return false;
9140   }
9141 
9142   }
9143 
9144   llvm_unreachable("switch has default clause!");
9145 }
9146 
isLoopInvariantPredicate(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,const Loop * L,ICmpInst::Predicate & InvariantPred,const SCEV * & InvariantLHS,const SCEV * & InvariantRHS)9147 bool ScalarEvolution::isLoopInvariantPredicate(
9148     ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
9149     ICmpInst::Predicate &InvariantPred, const SCEV *&InvariantLHS,
9150     const SCEV *&InvariantRHS) {
9151 
9152   // If there is a loop-invariant, force it into the RHS, otherwise bail out.
9153   if (!isLoopInvariant(RHS, L)) {
9154     if (!isLoopInvariant(LHS, L))
9155       return false;
9156 
9157     std::swap(LHS, RHS);
9158     Pred = ICmpInst::getSwappedPredicate(Pred);
9159   }
9160 
9161   const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS);
9162   if (!ArLHS || ArLHS->getLoop() != L)
9163     return false;
9164 
9165   bool Increasing;
9166   if (!isMonotonicPredicate(ArLHS, Pred, Increasing))
9167     return false;
9168 
9169   // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to
9170   // true as the loop iterates, and the backedge is control dependent on
9171   // "ArLHS `Pred` RHS" == true then we can reason as follows:
9172   //
9173   //   * if the predicate was false in the first iteration then the predicate
9174   //     is never evaluated again, since the loop exits without taking the
9175   //     backedge.
9176   //   * if the predicate was true in the first iteration then it will
9177   //     continue to be true for all future iterations since it is
9178   //     monotonically increasing.
9179   //
9180   // For both the above possibilities, we can replace the loop varying
9181   // predicate with its value on the first iteration of the loop (which is
9182   // loop invariant).
9183   //
9184   // A similar reasoning applies for a monotonically decreasing predicate, by
9185   // replacing true with false and false with true in the above two bullets.
9186 
9187   auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred);
9188 
9189   if (!isLoopBackedgeGuardedByCond(L, P, LHS, RHS))
9190     return false;
9191 
9192   InvariantPred = Pred;
9193   InvariantLHS = ArLHS->getStart();
9194   InvariantRHS = RHS;
9195   return true;
9196 }
9197 
isKnownPredicateViaConstantRanges(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)9198 bool ScalarEvolution::isKnownPredicateViaConstantRanges(
9199     ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) {
9200   if (HasSameValue(LHS, RHS))
9201     return ICmpInst::isTrueWhenEqual(Pred);
9202 
9203   // This code is split out from isKnownPredicate because it is called from
9204   // within isLoopEntryGuardedByCond.
9205 
9206   auto CheckRanges =
9207       [&](const ConstantRange &RangeLHS, const ConstantRange &RangeRHS) {
9208     return ConstantRange::makeSatisfyingICmpRegion(Pred, RangeRHS)
9209         .contains(RangeLHS);
9210   };
9211 
9212   // The check at the top of the function catches the case where the values are
9213   // known to be equal.
9214   if (Pred == CmpInst::ICMP_EQ)
9215     return false;
9216 
9217   if (Pred == CmpInst::ICMP_NE)
9218     return CheckRanges(getSignedRange(LHS), getSignedRange(RHS)) ||
9219            CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS)) ||
9220            isKnownNonZero(getMinusSCEV(LHS, RHS));
9221 
9222   if (CmpInst::isSigned(Pred))
9223     return CheckRanges(getSignedRange(LHS), getSignedRange(RHS));
9224 
9225   return CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS));
9226 }
9227 
isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)9228 bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,
9229                                                     const SCEV *LHS,
9230                                                     const SCEV *RHS) {
9231   // Match Result to (X + Y)<ExpectedFlags> where Y is a constant integer.
9232   // Return Y via OutY.
9233   auto MatchBinaryAddToConst =
9234       [this](const SCEV *Result, const SCEV *X, APInt &OutY,
9235              SCEV::NoWrapFlags ExpectedFlags) {
9236     const SCEV *NonConstOp, *ConstOp;
9237     SCEV::NoWrapFlags FlagsPresent;
9238 
9239     if (!splitBinaryAdd(Result, ConstOp, NonConstOp, FlagsPresent) ||
9240         !isa<SCEVConstant>(ConstOp) || NonConstOp != X)
9241       return false;
9242 
9243     OutY = cast<SCEVConstant>(ConstOp)->getAPInt();
9244     return (FlagsPresent & ExpectedFlags) == ExpectedFlags;
9245   };
9246 
9247   APInt C;
9248 
9249   switch (Pred) {
9250   default:
9251     break;
9252 
9253   case ICmpInst::ICMP_SGE:
9254     std::swap(LHS, RHS);
9255     LLVM_FALLTHROUGH;
9256   case ICmpInst::ICMP_SLE:
9257     // X s<= (X + C)<nsw> if C >= 0
9258     if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) && C.isNonNegative())
9259       return true;
9260 
9261     // (X + C)<nsw> s<= X if C <= 0
9262     if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) &&
9263         !C.isStrictlyPositive())
9264       return true;
9265     break;
9266 
9267   case ICmpInst::ICMP_SGT:
9268     std::swap(LHS, RHS);
9269     LLVM_FALLTHROUGH;
9270   case ICmpInst::ICMP_SLT:
9271     // X s< (X + C)<nsw> if C > 0
9272     if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) &&
9273         C.isStrictlyPositive())
9274       return true;
9275 
9276     // (X + C)<nsw> s< X if C < 0
9277     if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) && C.isNegative())
9278       return true;
9279     break;
9280   }
9281 
9282   return false;
9283 }
9284 
isKnownPredicateViaSplitting(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)9285 bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred,
9286                                                    const SCEV *LHS,
9287                                                    const SCEV *RHS) {
9288   if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate)
9289     return false;
9290 
9291   // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on
9292   // the stack can result in exponential time complexity.
9293   SaveAndRestore<bool> Restore(ProvingSplitPredicate, true);
9294 
9295   // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L
9296   //
9297   // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use
9298   // isKnownPredicate.  isKnownPredicate is more powerful, but also more
9299   // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the
9300   // interesting cases seen in practice.  We can consider "upgrading" L >= 0 to
9301   // use isKnownPredicate later if needed.
9302   return isKnownNonNegative(RHS) &&
9303          isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) &&
9304          isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS);
9305 }
9306 
isImpliedViaGuard(BasicBlock * BB,ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)9307 bool ScalarEvolution::isImpliedViaGuard(BasicBlock *BB,
9308                                         ICmpInst::Predicate Pred,
9309                                         const SCEV *LHS, const SCEV *RHS) {
9310   // No need to even try if we know the module has no guards.
9311   if (!HasGuards)
9312     return false;
9313 
9314   return any_of(*BB, [&](Instruction &I) {
9315     using namespace llvm::PatternMatch;
9316 
9317     Value *Condition;
9318     return match(&I, m_Intrinsic<Intrinsic::experimental_guard>(
9319                          m_Value(Condition))) &&
9320            isImpliedCond(Pred, LHS, RHS, Condition, false);
9321   });
9322 }
9323 
9324 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
9325 /// protected by a conditional between LHS and RHS.  This is used to
9326 /// to eliminate casts.
9327 bool
isLoopBackedgeGuardedByCond(const Loop * L,ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)9328 ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
9329                                              ICmpInst::Predicate Pred,
9330                                              const SCEV *LHS, const SCEV *RHS) {
9331   // Interpret a null as meaning no loop, where there is obviously no guard
9332   // (interprocedural conditions notwithstanding).
9333   if (!L) return true;
9334 
9335   if (VerifyIR)
9336     assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
9337            "This cannot be done on broken IR!");
9338 
9339 
9340   if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
9341     return true;
9342 
9343   BasicBlock *Latch = L->getLoopLatch();
9344   if (!Latch)
9345     return false;
9346 
9347   BranchInst *LoopContinuePredicate =
9348     dyn_cast<BranchInst>(Latch->getTerminator());
9349   if (LoopContinuePredicate && LoopContinuePredicate->isConditional() &&
9350       isImpliedCond(Pred, LHS, RHS,
9351                     LoopContinuePredicate->getCondition(),
9352                     LoopContinuePredicate->getSuccessor(0) != L->getHeader()))
9353     return true;
9354 
9355   // We don't want more than one activation of the following loops on the stack
9356   // -- that can lead to O(n!) time complexity.
9357   if (WalkingBEDominatingConds)
9358     return false;
9359 
9360   SaveAndRestore<bool> ClearOnExit(WalkingBEDominatingConds, true);
9361 
9362   // See if we can exploit a trip count to prove the predicate.
9363   const auto &BETakenInfo = getBackedgeTakenInfo(L);
9364   const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this);
9365   if (LatchBECount != getCouldNotCompute()) {
9366     // We know that Latch branches back to the loop header exactly
9367     // LatchBECount times.  This means the backdege condition at Latch is
9368     // equivalent to  "{0,+,1} u< LatchBECount".
9369     Type *Ty = LatchBECount->getType();
9370     auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
9371     const SCEV *LoopCounter =
9372       getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags);
9373     if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter,
9374                       LatchBECount))
9375       return true;
9376   }
9377 
9378   // Check conditions due to any @llvm.assume intrinsics.
9379   for (auto &AssumeVH : AC.assumptions()) {
9380     if (!AssumeVH)
9381       continue;
9382     auto *CI = cast<CallInst>(AssumeVH);
9383     if (!DT.dominates(CI, Latch->getTerminator()))
9384       continue;
9385 
9386     if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false))
9387       return true;
9388   }
9389 
9390   // If the loop is not reachable from the entry block, we risk running into an
9391   // infinite loop as we walk up into the dom tree.  These loops do not matter
9392   // anyway, so we just return a conservative answer when we see them.
9393   if (!DT.isReachableFromEntry(L->getHeader()))
9394     return false;
9395 
9396   if (isImpliedViaGuard(Latch, Pred, LHS, RHS))
9397     return true;
9398 
9399   for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()];
9400        DTN != HeaderDTN; DTN = DTN->getIDom()) {
9401     assert(DTN && "should reach the loop header before reaching the root!");
9402 
9403     BasicBlock *BB = DTN->getBlock();
9404     if (isImpliedViaGuard(BB, Pred, LHS, RHS))
9405       return true;
9406 
9407     BasicBlock *PBB = BB->getSinglePredecessor();
9408     if (!PBB)
9409       continue;
9410 
9411     BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator());
9412     if (!ContinuePredicate || !ContinuePredicate->isConditional())
9413       continue;
9414 
9415     Value *Condition = ContinuePredicate->getCondition();
9416 
9417     // If we have an edge `E` within the loop body that dominates the only
9418     // latch, the condition guarding `E` also guards the backedge.  This
9419     // reasoning works only for loops with a single latch.
9420 
9421     BasicBlockEdge DominatingEdge(PBB, BB);
9422     if (DominatingEdge.isSingleEdge()) {
9423       // We're constructively (and conservatively) enumerating edges within the
9424       // loop body that dominate the latch.  The dominator tree better agree
9425       // with us on this:
9426       assert(DT.dominates(DominatingEdge, Latch) && "should be!");
9427 
9428       if (isImpliedCond(Pred, LHS, RHS, Condition,
9429                         BB != ContinuePredicate->getSuccessor(0)))
9430         return true;
9431     }
9432   }
9433 
9434   return false;
9435 }
9436 
9437 bool
isLoopEntryGuardedByCond(const Loop * L,ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)9438 ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
9439                                           ICmpInst::Predicate Pred,
9440                                           const SCEV *LHS, const SCEV *RHS) {
9441   // Interpret a null as meaning no loop, where there is obviously no guard
9442   // (interprocedural conditions notwithstanding).
9443   if (!L) return false;
9444 
9445   if (VerifyIR)
9446     assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
9447            "This cannot be done on broken IR!");
9448 
9449   // Both LHS and RHS must be available at loop entry.
9450   assert(isAvailableAtLoopEntry(LHS, L) &&
9451          "LHS is not available at Loop Entry");
9452   assert(isAvailableAtLoopEntry(RHS, L) &&
9453          "RHS is not available at Loop Entry");
9454 
9455   if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
9456     return true;
9457 
9458   // If we cannot prove strict comparison (e.g. a > b), maybe we can prove
9459   // the facts (a >= b && a != b) separately. A typical situation is when the
9460   // non-strict comparison is known from ranges and non-equality is known from
9461   // dominating predicates. If we are proving strict comparison, we always try
9462   // to prove non-equality and non-strict comparison separately.
9463   auto NonStrictPredicate = ICmpInst::getNonStrictPredicate(Pred);
9464   const bool ProvingStrictComparison = (Pred != NonStrictPredicate);
9465   bool ProvedNonStrictComparison = false;
9466   bool ProvedNonEquality = false;
9467 
9468   if (ProvingStrictComparison) {
9469     ProvedNonStrictComparison =
9470         isKnownViaNonRecursiveReasoning(NonStrictPredicate, LHS, RHS);
9471     ProvedNonEquality =
9472         isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_NE, LHS, RHS);
9473     if (ProvedNonStrictComparison && ProvedNonEquality)
9474       return true;
9475   }
9476 
9477   // Try to prove (Pred, LHS, RHS) using isImpliedViaGuard.
9478   auto ProveViaGuard = [&](BasicBlock *Block) {
9479     if (isImpliedViaGuard(Block, Pred, LHS, RHS))
9480       return true;
9481     if (ProvingStrictComparison) {
9482       if (!ProvedNonStrictComparison)
9483         ProvedNonStrictComparison =
9484             isImpliedViaGuard(Block, NonStrictPredicate, LHS, RHS);
9485       if (!ProvedNonEquality)
9486         ProvedNonEquality =
9487             isImpliedViaGuard(Block, ICmpInst::ICMP_NE, LHS, RHS);
9488       if (ProvedNonStrictComparison && ProvedNonEquality)
9489         return true;
9490     }
9491     return false;
9492   };
9493 
9494   // Try to prove (Pred, LHS, RHS) using isImpliedCond.
9495   auto ProveViaCond = [&](Value *Condition, bool Inverse) {
9496     if (isImpliedCond(Pred, LHS, RHS, Condition, Inverse))
9497       return true;
9498     if (ProvingStrictComparison) {
9499       if (!ProvedNonStrictComparison)
9500         ProvedNonStrictComparison =
9501             isImpliedCond(NonStrictPredicate, LHS, RHS, Condition, Inverse);
9502       if (!ProvedNonEquality)
9503         ProvedNonEquality =
9504             isImpliedCond(ICmpInst::ICMP_NE, LHS, RHS, Condition, Inverse);
9505       if (ProvedNonStrictComparison && ProvedNonEquality)
9506         return true;
9507     }
9508     return false;
9509   };
9510 
9511   // Starting at the loop predecessor, climb up the predecessor chain, as long
9512   // as there are predecessors that can be found that have unique successors
9513   // leading to the original header.
9514   for (std::pair<BasicBlock *, BasicBlock *>
9515          Pair(L->getLoopPredecessor(), L->getHeader());
9516        Pair.first;
9517        Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
9518 
9519     if (ProveViaGuard(Pair.first))
9520       return true;
9521 
9522     BranchInst *LoopEntryPredicate =
9523       dyn_cast<BranchInst>(Pair.first->getTerminator());
9524     if (!LoopEntryPredicate ||
9525         LoopEntryPredicate->isUnconditional())
9526       continue;
9527 
9528     if (ProveViaCond(LoopEntryPredicate->getCondition(),
9529                      LoopEntryPredicate->getSuccessor(0) != Pair.second))
9530       return true;
9531   }
9532 
9533   // Check conditions due to any @llvm.assume intrinsics.
9534   for (auto &AssumeVH : AC.assumptions()) {
9535     if (!AssumeVH)
9536       continue;
9537     auto *CI = cast<CallInst>(AssumeVH);
9538     if (!DT.dominates(CI, L->getHeader()))
9539       continue;
9540 
9541     if (ProveViaCond(CI->getArgOperand(0), false))
9542       return true;
9543   }
9544 
9545   return false;
9546 }
9547 
isImpliedCond(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,Value * FoundCondValue,bool Inverse)9548 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred,
9549                                     const SCEV *LHS, const SCEV *RHS,
9550                                     Value *FoundCondValue,
9551                                     bool Inverse) {
9552   if (!PendingLoopPredicates.insert(FoundCondValue).second)
9553     return false;
9554 
9555   auto ClearOnExit =
9556       make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); });
9557 
9558   // Recursively handle And and Or conditions.
9559   if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) {
9560     if (BO->getOpcode() == Instruction::And) {
9561       if (!Inverse)
9562         return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
9563                isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
9564     } else if (BO->getOpcode() == Instruction::Or) {
9565       if (Inverse)
9566         return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
9567                isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
9568     }
9569   }
9570 
9571   ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue);
9572   if (!ICI) return false;
9573 
9574   // Now that we found a conditional branch that dominates the loop or controls
9575   // the loop latch. Check to see if it is the comparison we are looking for.
9576   ICmpInst::Predicate FoundPred;
9577   if (Inverse)
9578     FoundPred = ICI->getInversePredicate();
9579   else
9580     FoundPred = ICI->getPredicate();
9581 
9582   const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
9583   const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
9584 
9585   return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS);
9586 }
9587 
isImpliedCond(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,ICmpInst::Predicate FoundPred,const SCEV * FoundLHS,const SCEV * FoundRHS)9588 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS,
9589                                     const SCEV *RHS,
9590                                     ICmpInst::Predicate FoundPred,
9591                                     const SCEV *FoundLHS,
9592                                     const SCEV *FoundRHS) {
9593   // Balance the types.
9594   if (getTypeSizeInBits(LHS->getType()) <
9595       getTypeSizeInBits(FoundLHS->getType())) {
9596     if (CmpInst::isSigned(Pred)) {
9597       LHS = getSignExtendExpr(LHS, FoundLHS->getType());
9598       RHS = getSignExtendExpr(RHS, FoundLHS->getType());
9599     } else {
9600       LHS = getZeroExtendExpr(LHS, FoundLHS->getType());
9601       RHS = getZeroExtendExpr(RHS, FoundLHS->getType());
9602     }
9603   } else if (getTypeSizeInBits(LHS->getType()) >
9604       getTypeSizeInBits(FoundLHS->getType())) {
9605     if (CmpInst::isSigned(FoundPred)) {
9606       FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
9607       FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
9608     } else {
9609       FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
9610       FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
9611     }
9612   }
9613 
9614   // Canonicalize the query to match the way instcombine will have
9615   // canonicalized the comparison.
9616   if (SimplifyICmpOperands(Pred, LHS, RHS))
9617     if (LHS == RHS)
9618       return CmpInst::isTrueWhenEqual(Pred);
9619   if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS))
9620     if (FoundLHS == FoundRHS)
9621       return CmpInst::isFalseWhenEqual(FoundPred);
9622 
9623   // Check to see if we can make the LHS or RHS match.
9624   if (LHS == FoundRHS || RHS == FoundLHS) {
9625     if (isa<SCEVConstant>(RHS)) {
9626       std::swap(FoundLHS, FoundRHS);
9627       FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
9628     } else {
9629       std::swap(LHS, RHS);
9630       Pred = ICmpInst::getSwappedPredicate(Pred);
9631     }
9632   }
9633 
9634   // Check whether the found predicate is the same as the desired predicate.
9635   if (FoundPred == Pred)
9636     return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
9637 
9638   // Check whether swapping the found predicate makes it the same as the
9639   // desired predicate.
9640   if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
9641     if (isa<SCEVConstant>(RHS))
9642       return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
9643     else
9644       return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
9645                                    RHS, LHS, FoundLHS, FoundRHS);
9646   }
9647 
9648   // Unsigned comparison is the same as signed comparison when both the operands
9649   // are non-negative.
9650   if (CmpInst::isUnsigned(FoundPred) &&
9651       CmpInst::getSignedPredicate(FoundPred) == Pred &&
9652       isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS))
9653     return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
9654 
9655   // Check if we can make progress by sharpening ranges.
9656   if (FoundPred == ICmpInst::ICMP_NE &&
9657       (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) {
9658 
9659     const SCEVConstant *C = nullptr;
9660     const SCEV *V = nullptr;
9661 
9662     if (isa<SCEVConstant>(FoundLHS)) {
9663       C = cast<SCEVConstant>(FoundLHS);
9664       V = FoundRHS;
9665     } else {
9666       C = cast<SCEVConstant>(FoundRHS);
9667       V = FoundLHS;
9668     }
9669 
9670     // The guarding predicate tells us that C != V. If the known range
9671     // of V is [C, t), we can sharpen the range to [C + 1, t).  The
9672     // range we consider has to correspond to same signedness as the
9673     // predicate we're interested in folding.
9674 
9675     APInt Min = ICmpInst::isSigned(Pred) ?
9676         getSignedRangeMin(V) : getUnsignedRangeMin(V);
9677 
9678     if (Min == C->getAPInt()) {
9679       // Given (V >= Min && V != Min) we conclude V >= (Min + 1).
9680       // This is true even if (Min + 1) wraps around -- in case of
9681       // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)).
9682 
9683       APInt SharperMin = Min + 1;
9684 
9685       switch (Pred) {
9686         case ICmpInst::ICMP_SGE:
9687         case ICmpInst::ICMP_UGE:
9688           // We know V `Pred` SharperMin.  If this implies LHS `Pred`
9689           // RHS, we're done.
9690           if (isImpliedCondOperands(Pred, LHS, RHS, V,
9691                                     getConstant(SharperMin)))
9692             return true;
9693           LLVM_FALLTHROUGH;
9694 
9695         case ICmpInst::ICMP_SGT:
9696         case ICmpInst::ICMP_UGT:
9697           // We know from the range information that (V `Pred` Min ||
9698           // V == Min).  We know from the guarding condition that !(V
9699           // == Min).  This gives us
9700           //
9701           //       V `Pred` Min || V == Min && !(V == Min)
9702           //   =>  V `Pred` Min
9703           //
9704           // If V `Pred` Min implies LHS `Pred` RHS, we're done.
9705 
9706           if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min)))
9707             return true;
9708           LLVM_FALLTHROUGH;
9709 
9710         default:
9711           // No change
9712           break;
9713       }
9714     }
9715   }
9716 
9717   // Check whether the actual condition is beyond sufficient.
9718   if (FoundPred == ICmpInst::ICMP_EQ)
9719     if (ICmpInst::isTrueWhenEqual(Pred))
9720       if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
9721         return true;
9722   if (Pred == ICmpInst::ICMP_NE)
9723     if (!ICmpInst::isTrueWhenEqual(FoundPred))
9724       if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
9725         return true;
9726 
9727   // Otherwise assume the worst.
9728   return false;
9729 }
9730 
splitBinaryAdd(const SCEV * Expr,const SCEV * & L,const SCEV * & R,SCEV::NoWrapFlags & Flags)9731 bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr,
9732                                      const SCEV *&L, const SCEV *&R,
9733                                      SCEV::NoWrapFlags &Flags) {
9734   const auto *AE = dyn_cast<SCEVAddExpr>(Expr);
9735   if (!AE || AE->getNumOperands() != 2)
9736     return false;
9737 
9738   L = AE->getOperand(0);
9739   R = AE->getOperand(1);
9740   Flags = AE->getNoWrapFlags();
9741   return true;
9742 }
9743 
computeConstantDifference(const SCEV * More,const SCEV * Less)9744 Optional<APInt> ScalarEvolution::computeConstantDifference(const SCEV *More,
9745                                                            const SCEV *Less) {
9746   // We avoid subtracting expressions here because this function is usually
9747   // fairly deep in the call stack (i.e. is called many times).
9748 
9749   // X - X = 0.
9750   if (More == Less)
9751     return APInt(getTypeSizeInBits(More->getType()), 0);
9752 
9753   if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
9754     const auto *LAR = cast<SCEVAddRecExpr>(Less);
9755     const auto *MAR = cast<SCEVAddRecExpr>(More);
9756 
9757     if (LAR->getLoop() != MAR->getLoop())
9758       return None;
9759 
9760     // We look at affine expressions only; not for correctness but to keep
9761     // getStepRecurrence cheap.
9762     if (!LAR->isAffine() || !MAR->isAffine())
9763       return None;
9764 
9765     if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
9766       return None;
9767 
9768     Less = LAR->getStart();
9769     More = MAR->getStart();
9770 
9771     // fall through
9772   }
9773 
9774   if (isa<SCEVConstant>(Less) && isa<SCEVConstant>(More)) {
9775     const auto &M = cast<SCEVConstant>(More)->getAPInt();
9776     const auto &L = cast<SCEVConstant>(Less)->getAPInt();
9777     return M - L;
9778   }
9779 
9780   SCEV::NoWrapFlags Flags;
9781   const SCEV *LLess = nullptr, *RLess = nullptr;
9782   const SCEV *LMore = nullptr, *RMore = nullptr;
9783   const SCEVConstant *C1 = nullptr, *C2 = nullptr;
9784   // Compare (X + C1) vs X.
9785   if (splitBinaryAdd(Less, LLess, RLess, Flags))
9786     if ((C1 = dyn_cast<SCEVConstant>(LLess)))
9787       if (RLess == More)
9788         return -(C1->getAPInt());
9789 
9790   // Compare X vs (X + C2).
9791   if (splitBinaryAdd(More, LMore, RMore, Flags))
9792     if ((C2 = dyn_cast<SCEVConstant>(LMore)))
9793       if (RMore == Less)
9794         return C2->getAPInt();
9795 
9796   // Compare (X + C1) vs (X + C2).
9797   if (C1 && C2 && RLess == RMore)
9798     return C2->getAPInt() - C1->getAPInt();
9799 
9800   return None;
9801 }
9802 
isImpliedCondOperandsViaNoOverflow(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,const SCEV * FoundLHS,const SCEV * FoundRHS)9803 bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(
9804     ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
9805     const SCEV *FoundLHS, const SCEV *FoundRHS) {
9806   if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT)
9807     return false;
9808 
9809   const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS);
9810   if (!AddRecLHS)
9811     return false;
9812 
9813   const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS);
9814   if (!AddRecFoundLHS)
9815     return false;
9816 
9817   // We'd like to let SCEV reason about control dependencies, so we constrain
9818   // both the inequalities to be about add recurrences on the same loop.  This
9819   // way we can use isLoopEntryGuardedByCond later.
9820 
9821   const Loop *L = AddRecFoundLHS->getLoop();
9822   if (L != AddRecLHS->getLoop())
9823     return false;
9824 
9825   //  FoundLHS u< FoundRHS u< -C =>  (FoundLHS + C) u< (FoundRHS + C) ... (1)
9826   //
9827   //  FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
9828   //                                                                  ... (2)
9829   //
9830   // Informal proof for (2), assuming (1) [*]:
9831   //
9832   // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
9833   //
9834   // Then
9835   //
9836   //       FoundLHS s< FoundRHS s< INT_MIN - C
9837   // <=>  (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C   [ using (3) ]
9838   // <=>  (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
9839   // <=>  (FoundLHS + INT_MIN + C + INT_MIN) s<
9840   //                        (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
9841   // <=>  FoundLHS + C s< FoundRHS + C
9842   //
9843   // [*]: (1) can be proved by ruling out overflow.
9844   //
9845   // [**]: This can be proved by analyzing all the four possibilities:
9846   //    (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
9847   //    (A s>= 0, B s>= 0).
9848   //
9849   // Note:
9850   // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
9851   // will not sign underflow.  For instance, say FoundLHS = (i8 -128), FoundRHS
9852   // = (i8 -127) and C = (i8 -100).  Then INT_MIN - C = (i8 -28), and FoundRHS
9853   // s< (INT_MIN - C).  Lack of sign overflow / underflow in "FoundRHS + C" is
9854   // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
9855   // C)".
9856 
9857   Optional<APInt> LDiff = computeConstantDifference(LHS, FoundLHS);
9858   Optional<APInt> RDiff = computeConstantDifference(RHS, FoundRHS);
9859   if (!LDiff || !RDiff || *LDiff != *RDiff)
9860     return false;
9861 
9862   if (LDiff->isMinValue())
9863     return true;
9864 
9865   APInt FoundRHSLimit;
9866 
9867   if (Pred == CmpInst::ICMP_ULT) {
9868     FoundRHSLimit = -(*RDiff);
9869   } else {
9870     assert(Pred == CmpInst::ICMP_SLT && "Checked above!");
9871     FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff;
9872   }
9873 
9874   // Try to prove (1) or (2), as needed.
9875   return isAvailableAtLoopEntry(FoundRHS, L) &&
9876          isLoopEntryGuardedByCond(L, Pred, FoundRHS,
9877                                   getConstant(FoundRHSLimit));
9878 }
9879 
isImpliedViaMerge(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,const SCEV * FoundLHS,const SCEV * FoundRHS,unsigned Depth)9880 bool ScalarEvolution::isImpliedViaMerge(ICmpInst::Predicate Pred,
9881                                         const SCEV *LHS, const SCEV *RHS,
9882                                         const SCEV *FoundLHS,
9883                                         const SCEV *FoundRHS, unsigned Depth) {
9884   const PHINode *LPhi = nullptr, *RPhi = nullptr;
9885 
9886   auto ClearOnExit = make_scope_exit([&]() {
9887     if (LPhi) {
9888       bool Erased = PendingMerges.erase(LPhi);
9889       assert(Erased && "Failed to erase LPhi!");
9890       (void)Erased;
9891     }
9892     if (RPhi) {
9893       bool Erased = PendingMerges.erase(RPhi);
9894       assert(Erased && "Failed to erase RPhi!");
9895       (void)Erased;
9896     }
9897   });
9898 
9899   // Find respective Phis and check that they are not being pending.
9900   if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS))
9901     if (auto *Phi = dyn_cast<PHINode>(LU->getValue())) {
9902       if (!PendingMerges.insert(Phi).second)
9903         return false;
9904       LPhi = Phi;
9905     }
9906   if (const SCEVUnknown *RU = dyn_cast<SCEVUnknown>(RHS))
9907     if (auto *Phi = dyn_cast<PHINode>(RU->getValue())) {
9908       // If we detect a loop of Phi nodes being processed by this method, for
9909       // example:
9910       //
9911       //   %a = phi i32 [ %some1, %preheader ], [ %b, %latch ]
9912       //   %b = phi i32 [ %some2, %preheader ], [ %a, %latch ]
9913       //
9914       // we don't want to deal with a case that complex, so return conservative
9915       // answer false.
9916       if (!PendingMerges.insert(Phi).second)
9917         return false;
9918       RPhi = Phi;
9919     }
9920 
9921   // If none of LHS, RHS is a Phi, nothing to do here.
9922   if (!LPhi && !RPhi)
9923     return false;
9924 
9925   // If there is a SCEVUnknown Phi we are interested in, make it left.
9926   if (!LPhi) {
9927     std::swap(LHS, RHS);
9928     std::swap(FoundLHS, FoundRHS);
9929     std::swap(LPhi, RPhi);
9930     Pred = ICmpInst::getSwappedPredicate(Pred);
9931   }
9932 
9933   assert(LPhi && "LPhi should definitely be a SCEVUnknown Phi!");
9934   const BasicBlock *LBB = LPhi->getParent();
9935   const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
9936 
9937   auto ProvedEasily = [&](const SCEV *S1, const SCEV *S2) {
9938     return isKnownViaNonRecursiveReasoning(Pred, S1, S2) ||
9939            isImpliedCondOperandsViaRanges(Pred, S1, S2, FoundLHS, FoundRHS) ||
9940            isImpliedViaOperations(Pred, S1, S2, FoundLHS, FoundRHS, Depth);
9941   };
9942 
9943   if (RPhi && RPhi->getParent() == LBB) {
9944     // Case one: RHS is also a SCEVUnknown Phi from the same basic block.
9945     // If we compare two Phis from the same block, and for each entry block
9946     // the predicate is true for incoming values from this block, then the
9947     // predicate is also true for the Phis.
9948     for (const BasicBlock *IncBB : predecessors(LBB)) {
9949       const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
9950       const SCEV *R = getSCEV(RPhi->getIncomingValueForBlock(IncBB));
9951       if (!ProvedEasily(L, R))
9952         return false;
9953     }
9954   } else if (RAR && RAR->getLoop()->getHeader() == LBB) {
9955     // Case two: RHS is also a Phi from the same basic block, and it is an
9956     // AddRec. It means that there is a loop which has both AddRec and Unknown
9957     // PHIs, for it we can compare incoming values of AddRec from above the loop
9958     // and latch with their respective incoming values of LPhi.
9959     // TODO: Generalize to handle loops with many inputs in a header.
9960     if (LPhi->getNumIncomingValues() != 2) return false;
9961 
9962     auto *RLoop = RAR->getLoop();
9963     auto *Predecessor = RLoop->getLoopPredecessor();
9964     assert(Predecessor && "Loop with AddRec with no predecessor?");
9965     const SCEV *L1 = getSCEV(LPhi->getIncomingValueForBlock(Predecessor));
9966     if (!ProvedEasily(L1, RAR->getStart()))
9967       return false;
9968     auto *Latch = RLoop->getLoopLatch();
9969     assert(Latch && "Loop with AddRec with no latch?");
9970     const SCEV *L2 = getSCEV(LPhi->getIncomingValueForBlock(Latch));
9971     if (!ProvedEasily(L2, RAR->getPostIncExpr(*this)))
9972       return false;
9973   } else {
9974     // In all other cases go over inputs of LHS and compare each of them to RHS,
9975     // the predicate is true for (LHS, RHS) if it is true for all such pairs.
9976     // At this point RHS is either a non-Phi, or it is a Phi from some block
9977     // different from LBB.
9978     for (const BasicBlock *IncBB : predecessors(LBB)) {
9979       // Check that RHS is available in this block.
9980       if (!dominates(RHS, IncBB))
9981         return false;
9982       const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
9983       if (!ProvedEasily(L, RHS))
9984         return false;
9985     }
9986   }
9987   return true;
9988 }
9989 
isImpliedCondOperands(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,const SCEV * FoundLHS,const SCEV * FoundRHS)9990 bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
9991                                             const SCEV *LHS, const SCEV *RHS,
9992                                             const SCEV *FoundLHS,
9993                                             const SCEV *FoundRHS) {
9994   if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS))
9995     return true;
9996 
9997   if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS))
9998     return true;
9999 
10000   return isImpliedCondOperandsHelper(Pred, LHS, RHS,
10001                                      FoundLHS, FoundRHS) ||
10002          // ~x < ~y --> x > y
10003          isImpliedCondOperandsHelper(Pred, LHS, RHS,
10004                                      getNotSCEV(FoundRHS),
10005                                      getNotSCEV(FoundLHS));
10006 }
10007 
10008 /// Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values?
10009 template <typename MinMaxExprType>
IsMinMaxConsistingOf(const SCEV * MaybeMinMaxExpr,const SCEV * Candidate)10010 static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr,
10011                                  const SCEV *Candidate) {
10012   const MinMaxExprType *MinMaxExpr = dyn_cast<MinMaxExprType>(MaybeMinMaxExpr);
10013   if (!MinMaxExpr)
10014     return false;
10015 
10016   return find(MinMaxExpr->operands(), Candidate) != MinMaxExpr->op_end();
10017 }
10018 
IsKnownPredicateViaAddRecStart(ScalarEvolution & SE,ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)10019 static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE,
10020                                            ICmpInst::Predicate Pred,
10021                                            const SCEV *LHS, const SCEV *RHS) {
10022   // If both sides are affine addrecs for the same loop, with equal
10023   // steps, and we know the recurrences don't wrap, then we only
10024   // need to check the predicate on the starting values.
10025 
10026   if (!ICmpInst::isRelational(Pred))
10027     return false;
10028 
10029   const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS);
10030   if (!LAR)
10031     return false;
10032   const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
10033   if (!RAR)
10034     return false;
10035   if (LAR->getLoop() != RAR->getLoop())
10036     return false;
10037   if (!LAR->isAffine() || !RAR->isAffine())
10038     return false;
10039 
10040   if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE))
10041     return false;
10042 
10043   SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ?
10044                          SCEV::FlagNSW : SCEV::FlagNUW;
10045   if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW))
10046     return false;
10047 
10048   return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart());
10049 }
10050 
10051 /// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max
10052 /// expression?
IsKnownPredicateViaMinOrMax(ScalarEvolution & SE,ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)10053 static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE,
10054                                         ICmpInst::Predicate Pred,
10055                                         const SCEV *LHS, const SCEV *RHS) {
10056   switch (Pred) {
10057   default:
10058     return false;
10059 
10060   case ICmpInst::ICMP_SGE:
10061     std::swap(LHS, RHS);
10062     LLVM_FALLTHROUGH;
10063   case ICmpInst::ICMP_SLE:
10064     return
10065         // min(A, ...) <= A
10066         IsMinMaxConsistingOf<SCEVSMinExpr>(LHS, RHS) ||
10067         // A <= max(A, ...)
10068         IsMinMaxConsistingOf<SCEVSMaxExpr>(RHS, LHS);
10069 
10070   case ICmpInst::ICMP_UGE:
10071     std::swap(LHS, RHS);
10072     LLVM_FALLTHROUGH;
10073   case ICmpInst::ICMP_ULE:
10074     return
10075         // min(A, ...) <= A
10076         IsMinMaxConsistingOf<SCEVUMinExpr>(LHS, RHS) ||
10077         // A <= max(A, ...)
10078         IsMinMaxConsistingOf<SCEVUMaxExpr>(RHS, LHS);
10079   }
10080 
10081   llvm_unreachable("covered switch fell through?!");
10082 }
10083 
isImpliedViaOperations(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,const SCEV * FoundLHS,const SCEV * FoundRHS,unsigned Depth)10084 bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred,
10085                                              const SCEV *LHS, const SCEV *RHS,
10086                                              const SCEV *FoundLHS,
10087                                              const SCEV *FoundRHS,
10088                                              unsigned Depth) {
10089   assert(getTypeSizeInBits(LHS->getType()) ==
10090              getTypeSizeInBits(RHS->getType()) &&
10091          "LHS and RHS have different sizes?");
10092   assert(getTypeSizeInBits(FoundLHS->getType()) ==
10093              getTypeSizeInBits(FoundRHS->getType()) &&
10094          "FoundLHS and FoundRHS have different sizes?");
10095   // We want to avoid hurting the compile time with analysis of too big trees.
10096   if (Depth > MaxSCEVOperationsImplicationDepth)
10097     return false;
10098   // We only want to work with ICMP_SGT comparison so far.
10099   // TODO: Extend to ICMP_UGT?
10100   if (Pred == ICmpInst::ICMP_SLT) {
10101     Pred = ICmpInst::ICMP_SGT;
10102     std::swap(LHS, RHS);
10103     std::swap(FoundLHS, FoundRHS);
10104   }
10105   if (Pred != ICmpInst::ICMP_SGT)
10106     return false;
10107 
10108   auto GetOpFromSExt = [&](const SCEV *S) {
10109     if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(S))
10110       return Ext->getOperand();
10111     // TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off
10112     // the constant in some cases.
10113     return S;
10114   };
10115 
10116   // Acquire values from extensions.
10117   auto *OrigLHS = LHS;
10118   auto *OrigFoundLHS = FoundLHS;
10119   LHS = GetOpFromSExt(LHS);
10120   FoundLHS = GetOpFromSExt(FoundLHS);
10121 
10122   // Is the SGT predicate can be proved trivially or using the found context.
10123   auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) {
10124     return isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGT, S1, S2) ||
10125            isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS,
10126                                   FoundRHS, Depth + 1);
10127   };
10128 
10129   if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(LHS)) {
10130     // We want to avoid creation of any new non-constant SCEV. Since we are
10131     // going to compare the operands to RHS, we should be certain that we don't
10132     // need any size extensions for this. So let's decline all cases when the
10133     // sizes of types of LHS and RHS do not match.
10134     // TODO: Maybe try to get RHS from sext to catch more cases?
10135     if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType()))
10136       return false;
10137 
10138     // Should not overflow.
10139     if (!LHSAddExpr->hasNoSignedWrap())
10140       return false;
10141 
10142     auto *LL = LHSAddExpr->getOperand(0);
10143     auto *LR = LHSAddExpr->getOperand(1);
10144     auto *MinusOne = getNegativeSCEV(getOne(RHS->getType()));
10145 
10146     // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context.
10147     auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) {
10148       return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS);
10149     };
10150     // Try to prove the following rule:
10151     // (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS).
10152     // (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS).
10153     if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL))
10154       return true;
10155   } else if (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(LHS)) {
10156     Value *LL, *LR;
10157     // FIXME: Once we have SDiv implemented, we can get rid of this matching.
10158 
10159     using namespace llvm::PatternMatch;
10160 
10161     if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) {
10162       // Rules for division.
10163       // We are going to perform some comparisons with Denominator and its
10164       // derivative expressions. In general case, creating a SCEV for it may
10165       // lead to a complex analysis of the entire graph, and in particular it
10166       // can request trip count recalculation for the same loop. This would
10167       // cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid
10168       // this, we only want to create SCEVs that are constants in this section.
10169       // So we bail if Denominator is not a constant.
10170       if (!isa<ConstantInt>(LR))
10171         return false;
10172 
10173       auto *Denominator = cast<SCEVConstant>(getSCEV(LR));
10174 
10175       // We want to make sure that LHS = FoundLHS / Denominator. If it is so,
10176       // then a SCEV for the numerator already exists and matches with FoundLHS.
10177       auto *Numerator = getExistingSCEV(LL);
10178       if (!Numerator || Numerator->getType() != FoundLHS->getType())
10179         return false;
10180 
10181       // Make sure that the numerator matches with FoundLHS and the denominator
10182       // is positive.
10183       if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator))
10184         return false;
10185 
10186       auto *DTy = Denominator->getType();
10187       auto *FRHSTy = FoundRHS->getType();
10188       if (DTy->isPointerTy() != FRHSTy->isPointerTy())
10189         // One of types is a pointer and another one is not. We cannot extend
10190         // them properly to a wider type, so let us just reject this case.
10191         // TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help
10192         // to avoid this check.
10193         return false;
10194 
10195       // Given that:
10196       // FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0.
10197       auto *WTy = getWiderType(DTy, FRHSTy);
10198       auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy);
10199       auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy);
10200 
10201       // Try to prove the following rule:
10202       // (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS).
10203       // For example, given that FoundLHS > 2. It means that FoundLHS is at
10204       // least 3. If we divide it by Denominator < 4, we will have at least 1.
10205       auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2));
10206       if (isKnownNonPositive(RHS) &&
10207           IsSGTViaContext(FoundRHSExt, DenomMinusTwo))
10208         return true;
10209 
10210       // Try to prove the following rule:
10211       // (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS).
10212       // For example, given that FoundLHS > -3. Then FoundLHS is at least -2.
10213       // If we divide it by Denominator > 2, then:
10214       // 1. If FoundLHS is negative, then the result is 0.
10215       // 2. If FoundLHS is non-negative, then the result is non-negative.
10216       // Anyways, the result is non-negative.
10217       auto *MinusOne = getNegativeSCEV(getOne(WTy));
10218       auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt);
10219       if (isKnownNegative(RHS) &&
10220           IsSGTViaContext(FoundRHSExt, NegDenomMinusOne))
10221         return true;
10222     }
10223   }
10224 
10225   // If our expression contained SCEVUnknown Phis, and we split it down and now
10226   // need to prove something for them, try to prove the predicate for every
10227   // possible incoming values of those Phis.
10228   if (isImpliedViaMerge(Pred, OrigLHS, RHS, OrigFoundLHS, FoundRHS, Depth + 1))
10229     return true;
10230 
10231   return false;
10232 }
10233 
isKnownPredicateExtendIdiom(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)10234 static bool isKnownPredicateExtendIdiom(ICmpInst::Predicate Pred,
10235                                         const SCEV *LHS, const SCEV *RHS) {
10236   // zext x u<= sext x, sext x s<= zext x
10237   switch (Pred) {
10238   case ICmpInst::ICMP_SGE:
10239     std::swap(LHS, RHS);
10240     LLVM_FALLTHROUGH;
10241   case ICmpInst::ICMP_SLE: {
10242     // If operand >=s 0 then ZExt == SExt.  If operand <s 0 then SExt <s ZExt.
10243     const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(LHS);
10244     const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(RHS);
10245     if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand())
10246       return true;
10247     break;
10248   }
10249   case ICmpInst::ICMP_UGE:
10250     std::swap(LHS, RHS);
10251     LLVM_FALLTHROUGH;
10252   case ICmpInst::ICMP_ULE: {
10253     // If operand >=s 0 then ZExt == SExt.  If operand <s 0 then ZExt <u SExt.
10254     const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS);
10255     const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(RHS);
10256     if (SExt && ZExt && SExt->getOperand() == ZExt->getOperand())
10257       return true;
10258     break;
10259   }
10260   default:
10261     break;
10262   };
10263   return false;
10264 }
10265 
10266 bool
isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS)10267 ScalarEvolution::isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred,
10268                                            const SCEV *LHS, const SCEV *RHS) {
10269   return isKnownPredicateExtendIdiom(Pred, LHS, RHS) ||
10270          isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
10271          IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) ||
10272          IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) ||
10273          isKnownPredicateViaNoOverflow(Pred, LHS, RHS);
10274 }
10275 
10276 bool
isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,const SCEV * FoundLHS,const SCEV * FoundRHS)10277 ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
10278                                              const SCEV *LHS, const SCEV *RHS,
10279                                              const SCEV *FoundLHS,
10280                                              const SCEV *FoundRHS) {
10281   switch (Pred) {
10282   default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
10283   case ICmpInst::ICMP_EQ:
10284   case ICmpInst::ICMP_NE:
10285     if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
10286       return true;
10287     break;
10288   case ICmpInst::ICMP_SLT:
10289   case ICmpInst::ICMP_SLE:
10290     if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
10291         isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS))
10292       return true;
10293     break;
10294   case ICmpInst::ICMP_SGT:
10295   case ICmpInst::ICMP_SGE:
10296     if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
10297         isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS))
10298       return true;
10299     break;
10300   case ICmpInst::ICMP_ULT:
10301   case ICmpInst::ICMP_ULE:
10302     if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
10303         isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS))
10304       return true;
10305     break;
10306   case ICmpInst::ICMP_UGT:
10307   case ICmpInst::ICMP_UGE:
10308     if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
10309         isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS))
10310       return true;
10311     break;
10312   }
10313 
10314   // Maybe it can be proved via operations?
10315   if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS))
10316     return true;
10317 
10318   return false;
10319 }
10320 
isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,const SCEV * LHS,const SCEV * RHS,const SCEV * FoundLHS,const SCEV * FoundRHS)10321 bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,
10322                                                      const SCEV *LHS,
10323                                                      const SCEV *RHS,
10324                                                      const SCEV *FoundLHS,
10325                                                      const SCEV *FoundRHS) {
10326   if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS))
10327     // The restriction on `FoundRHS` be lifted easily -- it exists only to
10328     // reduce the compile time impact of this optimization.
10329     return false;
10330 
10331   Optional<APInt> Addend = computeConstantDifference(LHS, FoundLHS);
10332   if (!Addend)
10333     return false;
10334 
10335   const APInt &ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt();
10336 
10337   // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the
10338   // antecedent "`FoundLHS` `Pred` `FoundRHS`".
10339   ConstantRange FoundLHSRange =
10340       ConstantRange::makeAllowedICmpRegion(Pred, ConstFoundRHS);
10341 
10342   // Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`:
10343   ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend));
10344 
10345   // We can also compute the range of values for `LHS` that satisfy the
10346   // consequent, "`LHS` `Pred` `RHS`":
10347   const APInt &ConstRHS = cast<SCEVConstant>(RHS)->getAPInt();
10348   ConstantRange SatisfyingLHSRange =
10349       ConstantRange::makeSatisfyingICmpRegion(Pred, ConstRHS);
10350 
10351   // The antecedent implies the consequent if every value of `LHS` that
10352   // satisfies the antecedent also satisfies the consequent.
10353   return SatisfyingLHSRange.contains(LHSRange);
10354 }
10355 
doesIVOverflowOnLT(const SCEV * RHS,const SCEV * Stride,bool IsSigned,bool NoWrap)10356 bool ScalarEvolution::doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
10357                                          bool IsSigned, bool NoWrap) {
10358   assert(isKnownPositive(Stride) && "Positive stride expected!");
10359 
10360   if (NoWrap) return false;
10361 
10362   unsigned BitWidth = getTypeSizeInBits(RHS->getType());
10363   const SCEV *One = getOne(Stride->getType());
10364 
10365   if (IsSigned) {
10366     APInt MaxRHS = getSignedRangeMax(RHS);
10367     APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
10368     APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
10369 
10370     // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow!
10371     return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS);
10372   }
10373 
10374   APInt MaxRHS = getUnsignedRangeMax(RHS);
10375   APInt MaxValue = APInt::getMaxValue(BitWidth);
10376   APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
10377 
10378   // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow!
10379   return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS);
10380 }
10381 
doesIVOverflowOnGT(const SCEV * RHS,const SCEV * Stride,bool IsSigned,bool NoWrap)10382 bool ScalarEvolution::doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
10383                                          bool IsSigned, bool NoWrap) {
10384   if (NoWrap) return false;
10385 
10386   unsigned BitWidth = getTypeSizeInBits(RHS->getType());
10387   const SCEV *One = getOne(Stride->getType());
10388 
10389   if (IsSigned) {
10390     APInt MinRHS = getSignedRangeMin(RHS);
10391     APInt MinValue = APInt::getSignedMinValue(BitWidth);
10392     APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
10393 
10394     // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow!
10395     return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS);
10396   }
10397 
10398   APInt MinRHS = getUnsignedRangeMin(RHS);
10399   APInt MinValue = APInt::getMinValue(BitWidth);
10400   APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
10401 
10402   // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow!
10403   return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS);
10404 }
10405 
computeBECount(const SCEV * Delta,const SCEV * Step,bool Equality)10406 const SCEV *ScalarEvolution::computeBECount(const SCEV *Delta, const SCEV *Step,
10407                                             bool Equality) {
10408   const SCEV *One = getOne(Step->getType());
10409   Delta = Equality ? getAddExpr(Delta, Step)
10410                    : getAddExpr(Delta, getMinusSCEV(Step, One));
10411   return getUDivExpr(Delta, Step);
10412 }
10413 
computeMaxBECountForLT(const SCEV * Start,const SCEV * Stride,const SCEV * End,unsigned BitWidth,bool IsSigned)10414 const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
10415                                                     const SCEV *Stride,
10416                                                     const SCEV *End,
10417                                                     unsigned BitWidth,
10418                                                     bool IsSigned) {
10419 
10420   assert(!isKnownNonPositive(Stride) &&
10421          "Stride is expected strictly positive!");
10422   // Calculate the maximum backedge count based on the range of values
10423   // permitted by Start, End, and Stride.
10424   const SCEV *MaxBECount;
10425   APInt MinStart =
10426       IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start);
10427 
10428   APInt StrideForMaxBECount =
10429       IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride);
10430 
10431   // We already know that the stride is positive, so we paper over conservatism
10432   // in our range computation by forcing StrideForMaxBECount to be at least one.
10433   // In theory this is unnecessary, but we expect MaxBECount to be a
10434   // SCEVConstant, and (udiv <constant> 0) is not constant folded by SCEV (there
10435   // is nothing to constant fold it to).
10436   APInt One(BitWidth, 1, IsSigned);
10437   StrideForMaxBECount = APIntOps::smax(One, StrideForMaxBECount);
10438 
10439   APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth)
10440                             : APInt::getMaxValue(BitWidth);
10441   APInt Limit = MaxValue - (StrideForMaxBECount - 1);
10442 
10443   // Although End can be a MAX expression we estimate MaxEnd considering only
10444   // the case End = RHS of the loop termination condition. This is safe because
10445   // in the other case (End - Start) is zero, leading to a zero maximum backedge
10446   // taken count.
10447   APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit)
10448                           : APIntOps::umin(getUnsignedRangeMax(End), Limit);
10449 
10450   MaxBECount = computeBECount(getConstant(MaxEnd - MinStart) /* Delta */,
10451                               getConstant(StrideForMaxBECount) /* Step */,
10452                               false /* Equality */);
10453 
10454   return MaxBECount;
10455 }
10456 
10457 ScalarEvolution::ExitLimit
howManyLessThans(const SCEV * LHS,const SCEV * RHS,const Loop * L,bool IsSigned,bool ControlsExit,bool AllowPredicates)10458 ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
10459                                   const Loop *L, bool IsSigned,
10460                                   bool ControlsExit, bool AllowPredicates) {
10461   SmallPtrSet<const SCEVPredicate *, 4> Predicates;
10462 
10463   const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
10464   bool PredicatedIV = false;
10465 
10466   if (!IV && AllowPredicates) {
10467     // Try to make this an AddRec using runtime tests, in the first X
10468     // iterations of this loop, where X is the SCEV expression found by the
10469     // algorithm below.
10470     IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
10471     PredicatedIV = true;
10472   }
10473 
10474   // Avoid weird loops
10475   if (!IV || IV->getLoop() != L || !IV->isAffine())
10476     return getCouldNotCompute();
10477 
10478   bool NoWrap = ControlsExit &&
10479                 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
10480 
10481   const SCEV *Stride = IV->getStepRecurrence(*this);
10482 
10483   bool PositiveStride = isKnownPositive(Stride);
10484 
10485   // Avoid negative or zero stride values.
10486   if (!PositiveStride) {
10487     // We can compute the correct backedge taken count for loops with unknown
10488     // strides if we can prove that the loop is not an infinite loop with side
10489     // effects. Here's the loop structure we are trying to handle -
10490     //
10491     // i = start
10492     // do {
10493     //   A[i] = i;
10494     //   i += s;
10495     // } while (i < end);
10496     //
10497     // The backedge taken count for such loops is evaluated as -
10498     // (max(end, start + stride) - start - 1) /u stride
10499     //
10500     // The additional preconditions that we need to check to prove correctness
10501     // of the above formula is as follows -
10502     //
10503     // a) IV is either nuw or nsw depending upon signedness (indicated by the
10504     //    NoWrap flag).
10505     // b) loop is single exit with no side effects.
10506     //
10507     //
10508     // Precondition a) implies that if the stride is negative, this is a single
10509     // trip loop. The backedge taken count formula reduces to zero in this case.
10510     //
10511     // Precondition b) implies that the unknown stride cannot be zero otherwise
10512     // we have UB.
10513     //
10514     // The positive stride case is the same as isKnownPositive(Stride) returning
10515     // true (original behavior of the function).
10516     //
10517     // We want to make sure that the stride is truly unknown as there are edge
10518     // cases where ScalarEvolution propagates no wrap flags to the
10519     // post-increment/decrement IV even though the increment/decrement operation
10520     // itself is wrapping. The computed backedge taken count may be wrong in
10521     // such cases. This is prevented by checking that the stride is not known to
10522     // be either positive or non-positive. For example, no wrap flags are
10523     // propagated to the post-increment IV of this loop with a trip count of 2 -
10524     //
10525     // unsigned char i;
10526     // for(i=127; i<128; i+=129)
10527     //   A[i] = i;
10528     //
10529     if (PredicatedIV || !NoWrap || isKnownNonPositive(Stride) ||
10530         !loopHasNoSideEffects(L))
10531       return getCouldNotCompute();
10532   } else if (!Stride->isOne() &&
10533              doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap))
10534     // Avoid proven overflow cases: this will ensure that the backedge taken
10535     // count will not generate any unsigned overflow. Relaxed no-overflow
10536     // conditions exploit NoWrapFlags, allowing to optimize in presence of
10537     // undefined behaviors like the case of C language.
10538     return getCouldNotCompute();
10539 
10540   ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT
10541                                       : ICmpInst::ICMP_ULT;
10542   const SCEV *Start = IV->getStart();
10543   const SCEV *End = RHS;
10544   // When the RHS is not invariant, we do not know the end bound of the loop and
10545   // cannot calculate the ExactBECount needed by ExitLimit. However, we can
10546   // calculate the MaxBECount, given the start, stride and max value for the end
10547   // bound of the loop (RHS), and the fact that IV does not overflow (which is
10548   // checked above).
10549   if (!isLoopInvariant(RHS, L)) {
10550     const SCEV *MaxBECount = computeMaxBECountForLT(
10551         Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
10552     return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
10553                      false /*MaxOrZero*/, Predicates);
10554   }
10555   // If the backedge is taken at least once, then it will be taken
10556   // (End-Start)/Stride times (rounded up to a multiple of Stride), where Start
10557   // is the LHS value of the less-than comparison the first time it is evaluated
10558   // and End is the RHS.
10559   const SCEV *BECountIfBackedgeTaken =
10560     computeBECount(getMinusSCEV(End, Start), Stride, false);
10561   // If the loop entry is guarded by the result of the backedge test of the
10562   // first loop iteration, then we know the backedge will be taken at least
10563   // once and so the backedge taken count is as above. If not then we use the
10564   // expression (max(End,Start)-Start)/Stride to describe the backedge count,
10565   // as if the backedge is taken at least once max(End,Start) is End and so the
10566   // result is as above, and if not max(End,Start) is Start so we get a backedge
10567   // count of zero.
10568   const SCEV *BECount;
10569   if (isLoopEntryGuardedByCond(L, Cond, getMinusSCEV(Start, Stride), RHS))
10570     BECount = BECountIfBackedgeTaken;
10571   else {
10572     End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start);
10573     BECount = computeBECount(getMinusSCEV(End, Start), Stride, false);
10574   }
10575 
10576   const SCEV *MaxBECount;
10577   bool MaxOrZero = false;
10578   if (isa<SCEVConstant>(BECount))
10579     MaxBECount = BECount;
10580   else if (isa<SCEVConstant>(BECountIfBackedgeTaken)) {
10581     // If we know exactly how many times the backedge will be taken if it's
10582     // taken at least once, then the backedge count will either be that or
10583     // zero.
10584     MaxBECount = BECountIfBackedgeTaken;
10585     MaxOrZero = true;
10586   } else {
10587     MaxBECount = computeMaxBECountForLT(
10588         Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
10589   }
10590 
10591   if (isa<SCEVCouldNotCompute>(MaxBECount) &&
10592       !isa<SCEVCouldNotCompute>(BECount))
10593     MaxBECount = getConstant(getUnsignedRangeMax(BECount));
10594 
10595   return ExitLimit(BECount, MaxBECount, MaxOrZero, Predicates);
10596 }
10597 
10598 ScalarEvolution::ExitLimit
howManyGreaterThans(const SCEV * LHS,const SCEV * RHS,const Loop * L,bool IsSigned,bool ControlsExit,bool AllowPredicates)10599 ScalarEvolution::howManyGreaterThans(const SCEV *LHS, const SCEV *RHS,
10600                                      const Loop *L, bool IsSigned,
10601                                      bool ControlsExit, bool AllowPredicates) {
10602   SmallPtrSet<const SCEVPredicate *, 4> Predicates;
10603   // We handle only IV > Invariant
10604   if (!isLoopInvariant(RHS, L))
10605     return getCouldNotCompute();
10606 
10607   const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
10608   if (!IV && AllowPredicates)
10609     // Try to make this an AddRec using runtime tests, in the first X
10610     // iterations of this loop, where X is the SCEV expression found by the
10611     // algorithm below.
10612     IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
10613 
10614   // Avoid weird loops
10615   if (!IV || IV->getLoop() != L || !IV->isAffine())
10616     return getCouldNotCompute();
10617 
10618   bool NoWrap = ControlsExit &&
10619                 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
10620 
10621   const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this));
10622 
10623   // Avoid negative or zero stride values
10624   if (!isKnownPositive(Stride))
10625     return getCouldNotCompute();
10626 
10627   // Avoid proven overflow cases: this will ensure that the backedge taken count
10628   // will not generate any unsigned overflow. Relaxed no-overflow conditions
10629   // exploit NoWrapFlags, allowing to optimize in presence of undefined
10630   // behaviors like the case of C language.
10631   if (!Stride->isOne() && doesIVOverflowOnGT(RHS, Stride, IsSigned, NoWrap))
10632     return getCouldNotCompute();
10633 
10634   ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT
10635                                       : ICmpInst::ICMP_UGT;
10636 
10637   const SCEV *Start = IV->getStart();
10638   const SCEV *End = RHS;
10639   if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS))
10640     End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start);
10641 
10642   const SCEV *BECount = computeBECount(getMinusSCEV(Start, End), Stride, false);
10643 
10644   APInt MaxStart = IsSigned ? getSignedRangeMax(Start)
10645                             : getUnsignedRangeMax(Start);
10646 
10647   APInt MinStride = IsSigned ? getSignedRangeMin(Stride)
10648                              : getUnsignedRangeMin(Stride);
10649 
10650   unsigned BitWidth = getTypeSizeInBits(LHS->getType());
10651   APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1)
10652                          : APInt::getMinValue(BitWidth) + (MinStride - 1);
10653 
10654   // Although End can be a MIN expression we estimate MinEnd considering only
10655   // the case End = RHS. This is safe because in the other case (Start - End)
10656   // is zero, leading to a zero maximum backedge taken count.
10657   APInt MinEnd =
10658     IsSigned ? APIntOps::smax(getSignedRangeMin(RHS), Limit)
10659              : APIntOps::umax(getUnsignedRangeMin(RHS), Limit);
10660 
10661   const SCEV *MaxBECount = isa<SCEVConstant>(BECount)
10662                                ? BECount
10663                                : computeBECount(getConstant(MaxStart - MinEnd),
10664                                                 getConstant(MinStride), false);
10665 
10666   if (isa<SCEVCouldNotCompute>(MaxBECount))
10667     MaxBECount = BECount;
10668 
10669   return ExitLimit(BECount, MaxBECount, false, Predicates);
10670 }
10671 
getNumIterationsInRange(const ConstantRange & Range,ScalarEvolution & SE) const10672 const SCEV *SCEVAddRecExpr::getNumIterationsInRange(const ConstantRange &Range,
10673                                                     ScalarEvolution &SE) const {
10674   if (Range.isFullSet())  // Infinite loop.
10675     return SE.getCouldNotCompute();
10676 
10677   // If the start is a non-zero constant, shift the range to simplify things.
10678   if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
10679     if (!SC->getValue()->isZero()) {
10680       SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
10681       Operands[0] = SE.getZero(SC->getType());
10682       const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(),
10683                                              getNoWrapFlags(FlagNW));
10684       if (const auto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
10685         return ShiftedAddRec->getNumIterationsInRange(
10686             Range.subtract(SC->getAPInt()), SE);
10687       // This is strange and shouldn't happen.
10688       return SE.getCouldNotCompute();
10689     }
10690 
10691   // The only time we can solve this is when we have all constant indices.
10692   // Otherwise, we cannot determine the overflow conditions.
10693   if (any_of(operands(), [](const SCEV *Op) { return !isa<SCEVConstant>(Op); }))
10694     return SE.getCouldNotCompute();
10695 
10696   // Okay at this point we know that all elements of the chrec are constants and
10697   // that the start element is zero.
10698 
10699   // First check to see if the range contains zero.  If not, the first
10700   // iteration exits.
10701   unsigned BitWidth = SE.getTypeSizeInBits(getType());
10702   if (!Range.contains(APInt(BitWidth, 0)))
10703     return SE.getZero(getType());
10704 
10705   if (isAffine()) {
10706     // If this is an affine expression then we have this situation:
10707     //   Solve {0,+,A} in Range  ===  Ax in Range
10708 
10709     // We know that zero is in the range.  If A is positive then we know that
10710     // the upper value of the range must be the first possible exit value.
10711     // If A is negative then the lower of the range is the last possible loop
10712     // value.  Also note that we already checked for a full range.
10713     APInt A = cast<SCEVConstant>(getOperand(1))->getAPInt();
10714     APInt End = A.sge(1) ? (Range.getUpper() - 1) : Range.getLower();
10715 
10716     // The exit value should be (End+A)/A.
10717     APInt ExitVal = (End + A).udiv(A);
10718     ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
10719 
10720     // Evaluate at the exit value.  If we really did fall out of the valid
10721     // range, then we computed our trip count, otherwise wrap around or other
10722     // things must have happened.
10723     ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
10724     if (Range.contains(Val->getValue()))
10725       return SE.getCouldNotCompute();  // Something strange happened
10726 
10727     // Ensure that the previous value is in the range.  This is a sanity check.
10728     assert(Range.contains(
10729            EvaluateConstantChrecAtConstant(this,
10730            ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) &&
10731            "Linear scev computation is off in a bad way!");
10732     return SE.getConstant(ExitValue);
10733   }
10734 
10735   if (isQuadratic()) {
10736     if (auto S = SolveQuadraticAddRecRange(this, Range, SE))
10737       return SE.getConstant(S.getValue());
10738   }
10739 
10740   return SE.getCouldNotCompute();
10741 }
10742 
10743 const SCEVAddRecExpr *
getPostIncExpr(ScalarEvolution & SE) const10744 SCEVAddRecExpr::getPostIncExpr(ScalarEvolution &SE) const {
10745   assert(getNumOperands() > 1 && "AddRec with zero step?");
10746   // There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)),
10747   // but in this case we cannot guarantee that the value returned will be an
10748   // AddRec because SCEV does not have a fixed point where it stops
10749   // simplification: it is legal to return ({rec1} + {rec2}). For example, it
10750   // may happen if we reach arithmetic depth limit while simplifying. So we
10751   // construct the returned value explicitly.
10752   SmallVector<const SCEV *, 3> Ops;
10753   // If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and
10754   // (this + Step) is {A+B,+,B+C,+...,+,N}.
10755   for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i)
10756     Ops.push_back(SE.getAddExpr(getOperand(i), getOperand(i + 1)));
10757   // We know that the last operand is not a constant zero (otherwise it would
10758   // have been popped out earlier). This guarantees us that if the result has
10759   // the same last operand, then it will also not be popped out, meaning that
10760   // the returned value will be an AddRec.
10761   const SCEV *Last = getOperand(getNumOperands() - 1);
10762   assert(!Last->isZero() && "Recurrency with zero step?");
10763   Ops.push_back(Last);
10764   return cast<SCEVAddRecExpr>(SE.getAddRecExpr(Ops, getLoop(),
10765                                                SCEV::FlagAnyWrap));
10766 }
10767 
10768 // Return true when S contains at least an undef value.
containsUndefs(const SCEV * S)10769 static inline bool containsUndefs(const SCEV *S) {
10770   return SCEVExprContains(S, [](const SCEV *S) {
10771     if (const auto *SU = dyn_cast<SCEVUnknown>(S))
10772       return isa<UndefValue>(SU->getValue());
10773     return false;
10774   });
10775 }
10776 
10777 namespace {
10778 
10779 // Collect all steps of SCEV expressions.
10780 struct SCEVCollectStrides {
10781   ScalarEvolution &SE;
10782   SmallVectorImpl<const SCEV *> &Strides;
10783 
SCEVCollectStrides__anond51b32ac2e11::SCEVCollectStrides10784   SCEVCollectStrides(ScalarEvolution &SE, SmallVectorImpl<const SCEV *> &S)
10785       : SE(SE), Strides(S) {}
10786 
follow__anond51b32ac2e11::SCEVCollectStrides10787   bool follow(const SCEV *S) {
10788     if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
10789       Strides.push_back(AR->getStepRecurrence(SE));
10790     return true;
10791   }
10792 
isDone__anond51b32ac2e11::SCEVCollectStrides10793   bool isDone() const { return false; }
10794 };
10795 
10796 // Collect all SCEVUnknown and SCEVMulExpr expressions.
10797 struct SCEVCollectTerms {
10798   SmallVectorImpl<const SCEV *> &Terms;
10799 
SCEVCollectTerms__anond51b32ac2e11::SCEVCollectTerms10800   SCEVCollectTerms(SmallVectorImpl<const SCEV *> &T) : Terms(T) {}
10801 
follow__anond51b32ac2e11::SCEVCollectTerms10802   bool follow(const SCEV *S) {
10803     if (isa<SCEVUnknown>(S) || isa<SCEVMulExpr>(S) ||
10804         isa<SCEVSignExtendExpr>(S)) {
10805       if (!containsUndefs(S))
10806         Terms.push_back(S);
10807 
10808       // Stop recursion: once we collected a term, do not walk its operands.
10809       return false;
10810     }
10811 
10812     // Keep looking.
10813     return true;
10814   }
10815 
isDone__anond51b32ac2e11::SCEVCollectTerms10816   bool isDone() const { return false; }
10817 };
10818 
10819 // Check if a SCEV contains an AddRecExpr.
10820 struct SCEVHasAddRec {
10821   bool &ContainsAddRec;
10822 
SCEVHasAddRec__anond51b32ac2e11::SCEVHasAddRec10823   SCEVHasAddRec(bool &ContainsAddRec) : ContainsAddRec(ContainsAddRec) {
10824     ContainsAddRec = false;
10825   }
10826 
follow__anond51b32ac2e11::SCEVHasAddRec10827   bool follow(const SCEV *S) {
10828     if (isa<SCEVAddRecExpr>(S)) {
10829       ContainsAddRec = true;
10830 
10831       // Stop recursion: once we collected a term, do not walk its operands.
10832       return false;
10833     }
10834 
10835     // Keep looking.
10836     return true;
10837   }
10838 
isDone__anond51b32ac2e11::SCEVHasAddRec10839   bool isDone() const { return false; }
10840 };
10841 
10842 // Find factors that are multiplied with an expression that (possibly as a
10843 // subexpression) contains an AddRecExpr. In the expression:
10844 //
10845 //  8 * (100 +  %p * %q * (%a + {0, +, 1}_loop))
10846 //
10847 // "%p * %q" are factors multiplied by the expression "(%a + {0, +, 1}_loop)"
10848 // that contains the AddRec {0, +, 1}_loop. %p * %q are likely to be array size
10849 // parameters as they form a product with an induction variable.
10850 //
10851 // This collector expects all array size parameters to be in the same MulExpr.
10852 // It might be necessary to later add support for collecting parameters that are
10853 // spread over different nested MulExpr.
10854 struct SCEVCollectAddRecMultiplies {
10855   SmallVectorImpl<const SCEV *> &Terms;
10856   ScalarEvolution &SE;
10857 
SCEVCollectAddRecMultiplies__anond51b32ac2e11::SCEVCollectAddRecMultiplies10858   SCEVCollectAddRecMultiplies(SmallVectorImpl<const SCEV *> &T, ScalarEvolution &SE)
10859       : Terms(T), SE(SE) {}
10860 
follow__anond51b32ac2e11::SCEVCollectAddRecMultiplies10861   bool follow(const SCEV *S) {
10862     if (auto *Mul = dyn_cast<SCEVMulExpr>(S)) {
10863       bool HasAddRec = false;
10864       SmallVector<const SCEV *, 0> Operands;
10865       for (auto Op : Mul->operands()) {
10866         const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Op);
10867         if (Unknown && !isa<CallInst>(Unknown->getValue())) {
10868           Operands.push_back(Op);
10869         } else if (Unknown) {
10870           HasAddRec = true;
10871         } else {
10872           bool ContainsAddRec = false;
10873           SCEVHasAddRec ContiansAddRec(ContainsAddRec);
10874           visitAll(Op, ContiansAddRec);
10875           HasAddRec |= ContainsAddRec;
10876         }
10877       }
10878       if (Operands.size() == 0)
10879         return true;
10880 
10881       if (!HasAddRec)
10882         return false;
10883 
10884       Terms.push_back(SE.getMulExpr(Operands));
10885       // Stop recursion: once we collected a term, do not walk its operands.
10886       return false;
10887     }
10888 
10889     // Keep looking.
10890     return true;
10891   }
10892 
isDone__anond51b32ac2e11::SCEVCollectAddRecMultiplies10893   bool isDone() const { return false; }
10894 };
10895 
10896 } // end anonymous namespace
10897 
10898 /// Find parametric terms in this SCEVAddRecExpr. We first for parameters in
10899 /// two places:
10900 ///   1) The strides of AddRec expressions.
10901 ///   2) Unknowns that are multiplied with AddRec expressions.
collectParametricTerms(const SCEV * Expr,SmallVectorImpl<const SCEV * > & Terms)10902 void ScalarEvolution::collectParametricTerms(const SCEV *Expr,
10903     SmallVectorImpl<const SCEV *> &Terms) {
10904   SmallVector<const SCEV *, 4> Strides;
10905   SCEVCollectStrides StrideCollector(*this, Strides);
10906   visitAll(Expr, StrideCollector);
10907 
10908   LLVM_DEBUG({
10909     dbgs() << "Strides:\n";
10910     for (const SCEV *S : Strides)
10911       dbgs() << *S << "\n";
10912   });
10913 
10914   for (const SCEV *S : Strides) {
10915     SCEVCollectTerms TermCollector(Terms);
10916     visitAll(S, TermCollector);
10917   }
10918 
10919   LLVM_DEBUG({
10920     dbgs() << "Terms:\n";
10921     for (const SCEV *T : Terms)
10922       dbgs() << *T << "\n";
10923   });
10924 
10925   SCEVCollectAddRecMultiplies MulCollector(Terms, *this);
10926   visitAll(Expr, MulCollector);
10927 }
10928 
findArrayDimensionsRec(ScalarEvolution & SE,SmallVectorImpl<const SCEV * > & Terms,SmallVectorImpl<const SCEV * > & Sizes)10929 static bool findArrayDimensionsRec(ScalarEvolution &SE,
10930                                    SmallVectorImpl<const SCEV *> &Terms,
10931                                    SmallVectorImpl<const SCEV *> &Sizes) {
10932   int Last = Terms.size() - 1;
10933   const SCEV *Step = Terms[Last];
10934 
10935   // End of recursion.
10936   if (Last == 0) {
10937     if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Step)) {
10938       SmallVector<const SCEV *, 2> Qs;
10939       for (const SCEV *Op : M->operands())
10940         if (!isa<SCEVConstant>(Op))
10941           Qs.push_back(Op);
10942 
10943       Step = SE.getMulExpr(Qs);
10944     }
10945 
10946     Sizes.push_back(Step);
10947     return true;
10948   }
10949 
10950   for (const SCEV *&Term : Terms) {
10951     // Normalize the terms before the next call to findArrayDimensionsRec.
10952     const SCEV *Q, *R;
10953     SCEVDivision::divide(SE, Term, Step, &Q, &R);
10954 
10955     // Bail out when GCD does not evenly divide one of the terms.
10956     if (!R->isZero())
10957       return false;
10958 
10959     Term = Q;
10960   }
10961 
10962   // Remove all SCEVConstants.
10963   Terms.erase(
10964       remove_if(Terms, [](const SCEV *E) { return isa<SCEVConstant>(E); }),
10965       Terms.end());
10966 
10967   if (Terms.size() > 0)
10968     if (!findArrayDimensionsRec(SE, Terms, Sizes))
10969       return false;
10970 
10971   Sizes.push_back(Step);
10972   return true;
10973 }
10974 
10975 // Returns true when one of the SCEVs of Terms contains a SCEVUnknown parameter.
containsParameters(SmallVectorImpl<const SCEV * > & Terms)10976 static inline bool containsParameters(SmallVectorImpl<const SCEV *> &Terms) {
10977   for (const SCEV *T : Terms)
10978     if (SCEVExprContains(T, [](const SCEV *S) { return isa<SCEVUnknown>(S); }))
10979       return true;
10980 
10981   return false;
10982 }
10983 
10984 // Return the number of product terms in S.
numberOfTerms(const SCEV * S)10985 static inline int numberOfTerms(const SCEV *S) {
10986   if (const SCEVMulExpr *Expr = dyn_cast<SCEVMulExpr>(S))
10987     return Expr->getNumOperands();
10988   return 1;
10989 }
10990 
removeConstantFactors(ScalarEvolution & SE,const SCEV * T)10991 static const SCEV *removeConstantFactors(ScalarEvolution &SE, const SCEV *T) {
10992   if (isa<SCEVConstant>(T))
10993     return nullptr;
10994 
10995   if (isa<SCEVUnknown>(T))
10996     return T;
10997 
10998   if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(T)) {
10999     SmallVector<const SCEV *, 2> Factors;
11000     for (const SCEV *Op : M->operands())
11001       if (!isa<SCEVConstant>(Op))
11002         Factors.push_back(Op);
11003 
11004     return SE.getMulExpr(Factors);
11005   }
11006 
11007   return T;
11008 }
11009 
11010 /// Return the size of an element read or written by Inst.
getElementSize(Instruction * Inst)11011 const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) {
11012   Type *Ty;
11013   if (StoreInst *Store = dyn_cast<StoreInst>(Inst))
11014     Ty = Store->getValueOperand()->getType();
11015   else if (LoadInst *Load = dyn_cast<LoadInst>(Inst))
11016     Ty = Load->getType();
11017   else
11018     return nullptr;
11019 
11020   Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty));
11021   return getSizeOfExpr(ETy, Ty);
11022 }
11023 
findArrayDimensions(SmallVectorImpl<const SCEV * > & Terms,SmallVectorImpl<const SCEV * > & Sizes,const SCEV * ElementSize)11024 void ScalarEvolution::findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
11025                                           SmallVectorImpl<const SCEV *> &Sizes,
11026                                           const SCEV *ElementSize) {
11027   if (Terms.size() < 1 || !ElementSize)
11028     return;
11029 
11030   // Early return when Terms do not contain parameters: we do not delinearize
11031   // non parametric SCEVs.
11032   if (!containsParameters(Terms))
11033     return;
11034 
11035   LLVM_DEBUG({
11036     dbgs() << "Terms:\n";
11037     for (const SCEV *T : Terms)
11038       dbgs() << *T << "\n";
11039   });
11040 
11041   // Remove duplicates.
11042   array_pod_sort(Terms.begin(), Terms.end());
11043   Terms.erase(std::unique(Terms.begin(), Terms.end()), Terms.end());
11044 
11045   // Put larger terms first.
11046   llvm::sort(Terms, [](const SCEV *LHS, const SCEV *RHS) {
11047     return numberOfTerms(LHS) > numberOfTerms(RHS);
11048   });
11049 
11050   // Try to divide all terms by the element size. If term is not divisible by
11051   // element size, proceed with the original term.
11052   for (const SCEV *&Term : Terms) {
11053     const SCEV *Q, *R;
11054     SCEVDivision::divide(*this, Term, ElementSize, &Q, &R);
11055     if (!Q->isZero())
11056       Term = Q;
11057   }
11058 
11059   SmallVector<const SCEV *, 4> NewTerms;
11060 
11061   // Remove constant factors.
11062   for (const SCEV *T : Terms)
11063     if (const SCEV *NewT = removeConstantFactors(*this, T))
11064       NewTerms.push_back(NewT);
11065 
11066   LLVM_DEBUG({
11067     dbgs() << "Terms after sorting:\n";
11068     for (const SCEV *T : NewTerms)
11069       dbgs() << *T << "\n";
11070   });
11071 
11072   if (NewTerms.empty() || !findArrayDimensionsRec(*this, NewTerms, Sizes)) {
11073     Sizes.clear();
11074     return;
11075   }
11076 
11077   // The last element to be pushed into Sizes is the size of an element.
11078   Sizes.push_back(ElementSize);
11079 
11080   LLVM_DEBUG({
11081     dbgs() << "Sizes:\n";
11082     for (const SCEV *S : Sizes)
11083       dbgs() << *S << "\n";
11084   });
11085 }
11086 
computeAccessFunctions(const SCEV * Expr,SmallVectorImpl<const SCEV * > & Subscripts,SmallVectorImpl<const SCEV * > & Sizes)11087 void ScalarEvolution::computeAccessFunctions(
11088     const SCEV *Expr, SmallVectorImpl<const SCEV *> &Subscripts,
11089     SmallVectorImpl<const SCEV *> &Sizes) {
11090   // Early exit in case this SCEV is not an affine multivariate function.
11091   if (Sizes.empty())
11092     return;
11093 
11094   if (auto *AR = dyn_cast<SCEVAddRecExpr>(Expr))
11095     if (!AR->isAffine())
11096       return;
11097 
11098   const SCEV *Res = Expr;
11099   int Last = Sizes.size() - 1;
11100   for (int i = Last; i >= 0; i--) {
11101     const SCEV *Q, *R;
11102     SCEVDivision::divide(*this, Res, Sizes[i], &Q, &R);
11103 
11104     LLVM_DEBUG({
11105       dbgs() << "Res: " << *Res << "\n";
11106       dbgs() << "Sizes[i]: " << *Sizes[i] << "\n";
11107       dbgs() << "Res divided by Sizes[i]:\n";
11108       dbgs() << "Quotient: " << *Q << "\n";
11109       dbgs() << "Remainder: " << *R << "\n";
11110     });
11111 
11112     Res = Q;
11113 
11114     // Do not record the last subscript corresponding to the size of elements in
11115     // the array.
11116     if (i == Last) {
11117 
11118       // Bail out if the remainder is too complex.
11119       if (isa<SCEVAddRecExpr>(R)) {
11120         Subscripts.clear();
11121         Sizes.clear();
11122         return;
11123       }
11124 
11125       continue;
11126     }
11127 
11128     // Record the access function for the current subscript.
11129     Subscripts.push_back(R);
11130   }
11131 
11132   // Also push in last position the remainder of the last division: it will be
11133   // the access function of the innermost dimension.
11134   Subscripts.push_back(Res);
11135 
11136   std::reverse(Subscripts.begin(), Subscripts.end());
11137 
11138   LLVM_DEBUG({
11139     dbgs() << "Subscripts:\n";
11140     for (const SCEV *S : Subscripts)
11141       dbgs() << *S << "\n";
11142   });
11143 }
11144 
11145 /// Splits the SCEV into two vectors of SCEVs representing the subscripts and
11146 /// sizes of an array access. Returns the remainder of the delinearization that
11147 /// is the offset start of the array.  The SCEV->delinearize algorithm computes
11148 /// the multiples of SCEV coefficients: that is a pattern matching of sub
11149 /// expressions in the stride and base of a SCEV corresponding to the
11150 /// computation of a GCD (greatest common divisor) of base and stride.  When
11151 /// SCEV->delinearize fails, it returns the SCEV unchanged.
11152 ///
11153 /// For example: when analyzing the memory access A[i][j][k] in this loop nest
11154 ///
11155 ///  void foo(long n, long m, long o, double A[n][m][o]) {
11156 ///
11157 ///    for (long i = 0; i < n; i++)
11158 ///      for (long j = 0; j < m; j++)
11159 ///        for (long k = 0; k < o; k++)
11160 ///          A[i][j][k] = 1.0;
11161 ///  }
11162 ///
11163 /// the delinearization input is the following AddRec SCEV:
11164 ///
11165 ///  AddRec: {{{%A,+,(8 * %m * %o)}<%for.i>,+,(8 * %o)}<%for.j>,+,8}<%for.k>
11166 ///
11167 /// From this SCEV, we are able to say that the base offset of the access is %A
11168 /// because it appears as an offset that does not divide any of the strides in
11169 /// the loops:
11170 ///
11171 ///  CHECK: Base offset: %A
11172 ///
11173 /// and then SCEV->delinearize determines the size of some of the dimensions of
11174 /// the array as these are the multiples by which the strides are happening:
11175 ///
11176 ///  CHECK: ArrayDecl[UnknownSize][%m][%o] with elements of sizeof(double) bytes.
11177 ///
11178 /// Note that the outermost dimension remains of UnknownSize because there are
11179 /// no strides that would help identifying the size of the last dimension: when
11180 /// the array has been statically allocated, one could compute the size of that
11181 /// dimension by dividing the overall size of the array by the size of the known
11182 /// dimensions: %m * %o * 8.
11183 ///
11184 /// Finally delinearize provides the access functions for the array reference
11185 /// that does correspond to A[i][j][k] of the above C testcase:
11186 ///
11187 ///  CHECK: ArrayRef[{0,+,1}<%for.i>][{0,+,1}<%for.j>][{0,+,1}<%for.k>]
11188 ///
11189 /// The testcases are checking the output of a function pass:
11190 /// DelinearizationPass that walks through all loads and stores of a function
11191 /// asking for the SCEV of the memory access with respect to all enclosing
11192 /// loops, calling SCEV->delinearize on that and printing the results.
delinearize(const SCEV * Expr,SmallVectorImpl<const SCEV * > & Subscripts,SmallVectorImpl<const SCEV * > & Sizes,const SCEV * ElementSize)11193 void ScalarEvolution::delinearize(const SCEV *Expr,
11194                                  SmallVectorImpl<const SCEV *> &Subscripts,
11195                                  SmallVectorImpl<const SCEV *> &Sizes,
11196                                  const SCEV *ElementSize) {
11197   // First step: collect parametric terms.
11198   SmallVector<const SCEV *, 4> Terms;
11199   collectParametricTerms(Expr, Terms);
11200 
11201   if (Terms.empty())
11202     return;
11203 
11204   // Second step: find subscript sizes.
11205   findArrayDimensions(Terms, Sizes, ElementSize);
11206 
11207   if (Sizes.empty())
11208     return;
11209 
11210   // Third step: compute the access functions for each subscript.
11211   computeAccessFunctions(Expr, Subscripts, Sizes);
11212 
11213   if (Subscripts.empty())
11214     return;
11215 
11216   LLVM_DEBUG({
11217     dbgs() << "succeeded to delinearize " << *Expr << "\n";
11218     dbgs() << "ArrayDecl[UnknownSize]";
11219     for (const SCEV *S : Sizes)
11220       dbgs() << "[" << *S << "]";
11221 
11222     dbgs() << "\nArrayRef";
11223     for (const SCEV *S : Subscripts)
11224       dbgs() << "[" << *S << "]";
11225     dbgs() << "\n";
11226   });
11227 }
11228 
getIndexExpressionsFromGEP(const GetElementPtrInst * GEP,SmallVectorImpl<const SCEV * > & Subscripts,SmallVectorImpl<int> & Sizes)11229 bool ScalarEvolution::getIndexExpressionsFromGEP(
11230     const GetElementPtrInst *GEP, SmallVectorImpl<const SCEV *> &Subscripts,
11231     SmallVectorImpl<int> &Sizes) {
11232   assert(Subscripts.empty() && Sizes.empty() &&
11233          "Expected output lists to be empty on entry to this function.");
11234   assert(GEP && "getIndexExpressionsFromGEP called with a null GEP");
11235   Type *Ty = GEP->getPointerOperandType();
11236   bool DroppedFirstDim = false;
11237   for (unsigned i = 1; i < GEP->getNumOperands(); i++) {
11238     const SCEV *Expr = getSCEV(GEP->getOperand(i));
11239     if (i == 1) {
11240       if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
11241         Ty = PtrTy->getElementType();
11242       } else if (auto *ArrayTy = dyn_cast<ArrayType>(Ty)) {
11243         Ty = ArrayTy->getElementType();
11244       } else {
11245         Subscripts.clear();
11246         Sizes.clear();
11247         return false;
11248       }
11249       if (auto *Const = dyn_cast<SCEVConstant>(Expr))
11250         if (Const->getValue()->isZero()) {
11251           DroppedFirstDim = true;
11252           continue;
11253         }
11254       Subscripts.push_back(Expr);
11255       continue;
11256     }
11257 
11258     auto *ArrayTy = dyn_cast<ArrayType>(Ty);
11259     if (!ArrayTy) {
11260       Subscripts.clear();
11261       Sizes.clear();
11262       return false;
11263     }
11264 
11265     Subscripts.push_back(Expr);
11266     if (!(DroppedFirstDim && i == 2))
11267       Sizes.push_back(ArrayTy->getNumElements());
11268 
11269     Ty = ArrayTy->getElementType();
11270   }
11271   return !Subscripts.empty();
11272 }
11273 
11274 //===----------------------------------------------------------------------===//
11275 //                   SCEVCallbackVH Class Implementation
11276 //===----------------------------------------------------------------------===//
11277 
deleted()11278 void ScalarEvolution::SCEVCallbackVH::deleted() {
11279   assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
11280   if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
11281     SE->ConstantEvolutionLoopExitValue.erase(PN);
11282   SE->eraseValueFromMap(getValPtr());
11283   // this now dangles!
11284 }
11285 
allUsesReplacedWith(Value * V)11286 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) {
11287   assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
11288 
11289   // Forget all the expressions associated with users of the old value,
11290   // so that future queries will recompute the expressions using the new
11291   // value.
11292   Value *Old = getValPtr();
11293   SmallVector<User *, 16> Worklist(Old->user_begin(), Old->user_end());
11294   SmallPtrSet<User *, 8> Visited;
11295   while (!Worklist.empty()) {
11296     User *U = Worklist.pop_back_val();
11297     // Deleting the Old value will cause this to dangle. Postpone
11298     // that until everything else is done.
11299     if (U == Old)
11300       continue;
11301     if (!Visited.insert(U).second)
11302       continue;
11303     if (PHINode *PN = dyn_cast<PHINode>(U))
11304       SE->ConstantEvolutionLoopExitValue.erase(PN);
11305     SE->eraseValueFromMap(U);
11306     Worklist.insert(Worklist.end(), U->user_begin(), U->user_end());
11307   }
11308   // Delete the Old value.
11309   if (PHINode *PN = dyn_cast<PHINode>(Old))
11310     SE->ConstantEvolutionLoopExitValue.erase(PN);
11311   SE->eraseValueFromMap(Old);
11312   // this now dangles!
11313 }
11314 
SCEVCallbackVH(Value * V,ScalarEvolution * se)11315 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
11316   : CallbackVH(V), SE(se) {}
11317 
11318 //===----------------------------------------------------------------------===//
11319 //                   ScalarEvolution Class Implementation
11320 //===----------------------------------------------------------------------===//
11321 
ScalarEvolution(Function & F,TargetLibraryInfo & TLI,AssumptionCache & AC,DominatorTree & DT,LoopInfo & LI)11322 ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI,
11323                                  AssumptionCache &AC, DominatorTree &DT,
11324                                  LoopInfo &LI)
11325     : F(F), TLI(TLI), AC(AC), DT(DT), LI(LI),
11326       CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64),
11327       LoopDispositions(64), BlockDispositions(64) {
11328   // To use guards for proving predicates, we need to scan every instruction in
11329   // relevant basic blocks, and not just terminators.  Doing this is a waste of
11330   // time if the IR does not actually contain any calls to
11331   // @llvm.experimental.guard, so do a quick check and remember this beforehand.
11332   //
11333   // This pessimizes the case where a pass that preserves ScalarEvolution wants
11334   // to _add_ guards to the module when there weren't any before, and wants
11335   // ScalarEvolution to optimize based on those guards.  For now we prefer to be
11336   // efficient in lieu of being smart in that rather obscure case.
11337 
11338   auto *GuardDecl = F.getParent()->getFunction(
11339       Intrinsic::getName(Intrinsic::experimental_guard));
11340   HasGuards = GuardDecl && !GuardDecl->use_empty();
11341 }
11342 
ScalarEvolution(ScalarEvolution && Arg)11343 ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
11344     : F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT),
11345       LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)),
11346       ValueExprMap(std::move(Arg.ValueExprMap)),
11347       PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
11348       PendingPhiRanges(std::move(Arg.PendingPhiRanges)),
11349       PendingMerges(std::move(Arg.PendingMerges)),
11350       MinTrailingZerosCache(std::move(Arg.MinTrailingZerosCache)),
11351       BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)),
11352       PredicatedBackedgeTakenCounts(
11353           std::move(Arg.PredicatedBackedgeTakenCounts)),
11354       ConstantEvolutionLoopExitValue(
11355           std::move(Arg.ConstantEvolutionLoopExitValue)),
11356       ValuesAtScopes(std::move(Arg.ValuesAtScopes)),
11357       LoopDispositions(std::move(Arg.LoopDispositions)),
11358       LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)),
11359       BlockDispositions(std::move(Arg.BlockDispositions)),
11360       UnsignedRanges(std::move(Arg.UnsignedRanges)),
11361       SignedRanges(std::move(Arg.SignedRanges)),
11362       UniqueSCEVs(std::move(Arg.UniqueSCEVs)),
11363       UniquePreds(std::move(Arg.UniquePreds)),
11364       SCEVAllocator(std::move(Arg.SCEVAllocator)),
11365       LoopUsers(std::move(Arg.LoopUsers)),
11366       PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)),
11367       FirstUnknown(Arg.FirstUnknown) {
11368   Arg.FirstUnknown = nullptr;
11369 }
11370 
~ScalarEvolution()11371 ScalarEvolution::~ScalarEvolution() {
11372   // Iterate through all the SCEVUnknown instances and call their
11373   // destructors, so that they release their references to their values.
11374   for (SCEVUnknown *U = FirstUnknown; U;) {
11375     SCEVUnknown *Tmp = U;
11376     U = U->Next;
11377     Tmp->~SCEVUnknown();
11378   }
11379   FirstUnknown = nullptr;
11380 
11381   ExprValueMap.clear();
11382   ValueExprMap.clear();
11383   HasRecMap.clear();
11384 
11385   // Free any extra memory created for ExitNotTakenInfo in the unlikely event
11386   // that a loop had multiple computable exits.
11387   for (auto &BTCI : BackedgeTakenCounts)
11388     BTCI.second.clear();
11389   for (auto &BTCI : PredicatedBackedgeTakenCounts)
11390     BTCI.second.clear();
11391 
11392   assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
11393   assert(PendingPhiRanges.empty() && "getRangeRef garbage");
11394   assert(PendingMerges.empty() && "isImpliedViaMerge garbage");
11395   assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!");
11396   assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!");
11397 }
11398 
hasLoopInvariantBackedgeTakenCount(const Loop * L)11399 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
11400   return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
11401 }
11402 
PrintLoopInfo(raw_ostream & OS,ScalarEvolution * SE,const Loop * L)11403 static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
11404                           const Loop *L) {
11405   // Print all inner loops first
11406   for (Loop *I : *L)
11407     PrintLoopInfo(OS, SE, I);
11408 
11409   OS << "Loop ";
11410   L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11411   OS << ": ";
11412 
11413   SmallVector<BasicBlock *, 8> ExitingBlocks;
11414   L->getExitingBlocks(ExitingBlocks);
11415   if (ExitingBlocks.size() != 1)
11416     OS << "<multiple exits> ";
11417 
11418   if (SE->hasLoopInvariantBackedgeTakenCount(L))
11419     OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L) << "\n";
11420   else
11421     OS << "Unpredictable backedge-taken count.\n";
11422 
11423   if (ExitingBlocks.size() > 1)
11424     for (BasicBlock *ExitingBlock : ExitingBlocks) {
11425       OS << "  exit count for " << ExitingBlock->getName() << ": "
11426          << *SE->getExitCount(L, ExitingBlock) << "\n";
11427     }
11428 
11429   OS << "Loop ";
11430   L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11431   OS << ": ";
11432 
11433   if (!isa<SCEVCouldNotCompute>(SE->getConstantMaxBackedgeTakenCount(L))) {
11434     OS << "max backedge-taken count is " << *SE->getConstantMaxBackedgeTakenCount(L);
11435     if (SE->isBackedgeTakenCountMaxOrZero(L))
11436       OS << ", actual taken count either this or zero.";
11437   } else {
11438     OS << "Unpredictable max backedge-taken count. ";
11439   }
11440 
11441   OS << "\n"
11442         "Loop ";
11443   L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11444   OS << ": ";
11445 
11446   SCEVUnionPredicate Pred;
11447   auto PBT = SE->getPredicatedBackedgeTakenCount(L, Pred);
11448   if (!isa<SCEVCouldNotCompute>(PBT)) {
11449     OS << "Predicated backedge-taken count is " << *PBT << "\n";
11450     OS << " Predicates:\n";
11451     Pred.print(OS, 4);
11452   } else {
11453     OS << "Unpredictable predicated backedge-taken count. ";
11454   }
11455   OS << "\n";
11456 
11457   if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
11458     OS << "Loop ";
11459     L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11460     OS << ": ";
11461     OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n";
11462   }
11463 }
11464 
loopDispositionToStr(ScalarEvolution::LoopDisposition LD)11465 static StringRef loopDispositionToStr(ScalarEvolution::LoopDisposition LD) {
11466   switch (LD) {
11467   case ScalarEvolution::LoopVariant:
11468     return "Variant";
11469   case ScalarEvolution::LoopInvariant:
11470     return "Invariant";
11471   case ScalarEvolution::LoopComputable:
11472     return "Computable";
11473   }
11474   llvm_unreachable("Unknown ScalarEvolution::LoopDisposition kind!");
11475 }
11476 
print(raw_ostream & OS) const11477 void ScalarEvolution::print(raw_ostream &OS) const {
11478   // ScalarEvolution's implementation of the print method is to print
11479   // out SCEV values of all instructions that are interesting. Doing
11480   // this potentially causes it to create new SCEV objects though,
11481   // which technically conflicts with the const qualifier. This isn't
11482   // observable from outside the class though, so casting away the
11483   // const isn't dangerous.
11484   ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
11485 
11486   if (ClassifyExpressions) {
11487     OS << "Classifying expressions for: ";
11488     F.printAsOperand(OS, /*PrintType=*/false);
11489     OS << "\n";
11490     for (Instruction &I : instructions(F))
11491       if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
11492         OS << I << '\n';
11493         OS << "  -->  ";
11494         const SCEV *SV = SE.getSCEV(&I);
11495         SV->print(OS);
11496         if (!isa<SCEVCouldNotCompute>(SV)) {
11497           OS << " U: ";
11498           SE.getUnsignedRange(SV).print(OS);
11499           OS << " S: ";
11500           SE.getSignedRange(SV).print(OS);
11501         }
11502 
11503         const Loop *L = LI.getLoopFor(I.getParent());
11504 
11505         const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
11506         if (AtUse != SV) {
11507           OS << "  -->  ";
11508           AtUse->print(OS);
11509           if (!isa<SCEVCouldNotCompute>(AtUse)) {
11510             OS << " U: ";
11511             SE.getUnsignedRange(AtUse).print(OS);
11512             OS << " S: ";
11513             SE.getSignedRange(AtUse).print(OS);
11514           }
11515         }
11516 
11517         if (L) {
11518           OS << "\t\t" "Exits: ";
11519           const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
11520           if (!SE.isLoopInvariant(ExitValue, L)) {
11521             OS << "<<Unknown>>";
11522           } else {
11523             OS << *ExitValue;
11524           }
11525 
11526           bool First = true;
11527           for (auto *Iter = L; Iter; Iter = Iter->getParentLoop()) {
11528             if (First) {
11529               OS << "\t\t" "LoopDispositions: { ";
11530               First = false;
11531             } else {
11532               OS << ", ";
11533             }
11534 
11535             Iter->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11536             OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, Iter));
11537           }
11538 
11539           for (auto *InnerL : depth_first(L)) {
11540             if (InnerL == L)
11541               continue;
11542             if (First) {
11543               OS << "\t\t" "LoopDispositions: { ";
11544               First = false;
11545             } else {
11546               OS << ", ";
11547             }
11548 
11549             InnerL->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11550             OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, InnerL));
11551           }
11552 
11553           OS << " }";
11554         }
11555 
11556         OS << "\n";
11557       }
11558   }
11559 
11560   OS << "Determining loop execution counts for: ";
11561   F.printAsOperand(OS, /*PrintType=*/false);
11562   OS << "\n";
11563   for (Loop *I : LI)
11564     PrintLoopInfo(OS, &SE, I);
11565 }
11566 
11567 ScalarEvolution::LoopDisposition
getLoopDisposition(const SCEV * S,const Loop * L)11568 ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) {
11569   auto &Values = LoopDispositions[S];
11570   for (auto &V : Values) {
11571     if (V.getPointer() == L)
11572       return V.getInt();
11573   }
11574   Values.emplace_back(L, LoopVariant);
11575   LoopDisposition D = computeLoopDisposition(S, L);
11576   auto &Values2 = LoopDispositions[S];
11577   for (auto &V : make_range(Values2.rbegin(), Values2.rend())) {
11578     if (V.getPointer() == L) {
11579       V.setInt(D);
11580       break;
11581     }
11582   }
11583   return D;
11584 }
11585 
11586 ScalarEvolution::LoopDisposition
computeLoopDisposition(const SCEV * S,const Loop * L)11587 ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) {
11588   switch (static_cast<SCEVTypes>(S->getSCEVType())) {
11589   case scConstant:
11590     return LoopInvariant;
11591   case scTruncate:
11592   case scZeroExtend:
11593   case scSignExtend:
11594     return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L);
11595   case scAddRecExpr: {
11596     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
11597 
11598     // If L is the addrec's loop, it's computable.
11599     if (AR->getLoop() == L)
11600       return LoopComputable;
11601 
11602     // Add recurrences are never invariant in the function-body (null loop).
11603     if (!L)
11604       return LoopVariant;
11605 
11606     // Everything that is not defined at loop entry is variant.
11607     if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))
11608       return LoopVariant;
11609     assert(!L->contains(AR->getLoop()) && "Containing loop's header does not"
11610            " dominate the contained loop's header?");
11611 
11612     // This recurrence is invariant w.r.t. L if AR's loop contains L.
11613     if (AR->getLoop()->contains(L))
11614       return LoopInvariant;
11615 
11616     // This recurrence is variant w.r.t. L if any of its operands
11617     // are variant.
11618     for (auto *Op : AR->operands())
11619       if (!isLoopInvariant(Op, L))
11620         return LoopVariant;
11621 
11622     // Otherwise it's loop-invariant.
11623     return LoopInvariant;
11624   }
11625   case scAddExpr:
11626   case scMulExpr:
11627   case scUMaxExpr:
11628   case scSMaxExpr:
11629   case scUMinExpr:
11630   case scSMinExpr: {
11631     bool HasVarying = false;
11632     for (auto *Op : cast<SCEVNAryExpr>(S)->operands()) {
11633       LoopDisposition D = getLoopDisposition(Op, L);
11634       if (D == LoopVariant)
11635         return LoopVariant;
11636       if (D == LoopComputable)
11637         HasVarying = true;
11638     }
11639     return HasVarying ? LoopComputable : LoopInvariant;
11640   }
11641   case scUDivExpr: {
11642     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
11643     LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L);
11644     if (LD == LoopVariant)
11645       return LoopVariant;
11646     LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L);
11647     if (RD == LoopVariant)
11648       return LoopVariant;
11649     return (LD == LoopInvariant && RD == LoopInvariant) ?
11650            LoopInvariant : LoopComputable;
11651   }
11652   case scUnknown:
11653     // All non-instruction values are loop invariant.  All instructions are loop
11654     // invariant if they are not contained in the specified loop.
11655     // Instructions are never considered invariant in the function body
11656     // (null loop) because they are defined within the "loop".
11657     if (auto *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue()))
11658       return (L && !L->contains(I)) ? LoopInvariant : LoopVariant;
11659     return LoopInvariant;
11660   case scCouldNotCompute:
11661     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
11662   }
11663   llvm_unreachable("Unknown SCEV kind!");
11664 }
11665 
isLoopInvariant(const SCEV * S,const Loop * L)11666 bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) {
11667   return getLoopDisposition(S, L) == LoopInvariant;
11668 }
11669 
hasComputableLoopEvolution(const SCEV * S,const Loop * L)11670 bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) {
11671   return getLoopDisposition(S, L) == LoopComputable;
11672 }
11673 
11674 ScalarEvolution::BlockDisposition
getBlockDisposition(const SCEV * S,const BasicBlock * BB)11675 ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) {
11676   auto &Values = BlockDispositions[S];
11677   for (auto &V : Values) {
11678     if (V.getPointer() == BB)
11679       return V.getInt();
11680   }
11681   Values.emplace_back(BB, DoesNotDominateBlock);
11682   BlockDisposition D = computeBlockDisposition(S, BB);
11683   auto &Values2 = BlockDispositions[S];
11684   for (auto &V : make_range(Values2.rbegin(), Values2.rend())) {
11685     if (V.getPointer() == BB) {
11686       V.setInt(D);
11687       break;
11688     }
11689   }
11690   return D;
11691 }
11692 
11693 ScalarEvolution::BlockDisposition
computeBlockDisposition(const SCEV * S,const BasicBlock * BB)11694 ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
11695   switch (static_cast<SCEVTypes>(S->getSCEVType())) {
11696   case scConstant:
11697     return ProperlyDominatesBlock;
11698   case scTruncate:
11699   case scZeroExtend:
11700   case scSignExtend:
11701     return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB);
11702   case scAddRecExpr: {
11703     // This uses a "dominates" query instead of "properly dominates" query
11704     // to test for proper dominance too, because the instruction which
11705     // produces the addrec's value is a PHI, and a PHI effectively properly
11706     // dominates its entire containing block.
11707     const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
11708     if (!DT.dominates(AR->getLoop()->getHeader(), BB))
11709       return DoesNotDominateBlock;
11710 
11711     // Fall through into SCEVNAryExpr handling.
11712     LLVM_FALLTHROUGH;
11713   }
11714   case scAddExpr:
11715   case scMulExpr:
11716   case scUMaxExpr:
11717   case scSMaxExpr:
11718   case scUMinExpr:
11719   case scSMinExpr: {
11720     const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
11721     bool Proper = true;
11722     for (const SCEV *NAryOp : NAry->operands()) {
11723       BlockDisposition D = getBlockDisposition(NAryOp, BB);
11724       if (D == DoesNotDominateBlock)
11725         return DoesNotDominateBlock;
11726       if (D == DominatesBlock)
11727         Proper = false;
11728     }
11729     return Proper ? ProperlyDominatesBlock : DominatesBlock;
11730   }
11731   case scUDivExpr: {
11732     const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
11733     const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS();
11734     BlockDisposition LD = getBlockDisposition(LHS, BB);
11735     if (LD == DoesNotDominateBlock)
11736       return DoesNotDominateBlock;
11737     BlockDisposition RD = getBlockDisposition(RHS, BB);
11738     if (RD == DoesNotDominateBlock)
11739       return DoesNotDominateBlock;
11740     return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ?
11741       ProperlyDominatesBlock : DominatesBlock;
11742   }
11743   case scUnknown:
11744     if (Instruction *I =
11745           dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) {
11746       if (I->getParent() == BB)
11747         return DominatesBlock;
11748       if (DT.properlyDominates(I->getParent(), BB))
11749         return ProperlyDominatesBlock;
11750       return DoesNotDominateBlock;
11751     }
11752     return ProperlyDominatesBlock;
11753   case scCouldNotCompute:
11754     llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
11755   }
11756   llvm_unreachable("Unknown SCEV kind!");
11757 }
11758 
dominates(const SCEV * S,const BasicBlock * BB)11759 bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) {
11760   return getBlockDisposition(S, BB) >= DominatesBlock;
11761 }
11762 
properlyDominates(const SCEV * S,const BasicBlock * BB)11763 bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) {
11764   return getBlockDisposition(S, BB) == ProperlyDominatesBlock;
11765 }
11766 
hasOperand(const SCEV * S,const SCEV * Op) const11767 bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const {
11768   return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; });
11769 }
11770 
hasOperand(const SCEV * S) const11771 bool ScalarEvolution::ExitLimit::hasOperand(const SCEV *S) const {
11772   auto IsS = [&](const SCEV *X) { return S == X; };
11773   auto ContainsS = [&](const SCEV *X) {
11774     return !isa<SCEVCouldNotCompute>(X) && SCEVExprContains(X, IsS);
11775   };
11776   return ContainsS(ExactNotTaken) || ContainsS(MaxNotTaken);
11777 }
11778 
11779 void
forgetMemoizedResults(const SCEV * S)11780 ScalarEvolution::forgetMemoizedResults(const SCEV *S) {
11781   ValuesAtScopes.erase(S);
11782   LoopDispositions.erase(S);
11783   BlockDispositions.erase(S);
11784   UnsignedRanges.erase(S);
11785   SignedRanges.erase(S);
11786   ExprValueMap.erase(S);
11787   HasRecMap.erase(S);
11788   MinTrailingZerosCache.erase(S);
11789 
11790   for (auto I = PredicatedSCEVRewrites.begin();
11791        I != PredicatedSCEVRewrites.end();) {
11792     std::pair<const SCEV *, const Loop *> Entry = I->first;
11793     if (Entry.first == S)
11794       PredicatedSCEVRewrites.erase(I++);
11795     else
11796       ++I;
11797   }
11798 
11799   auto RemoveSCEVFromBackedgeMap =
11800       [S, this](DenseMap<const Loop *, BackedgeTakenInfo> &Map) {
11801         for (auto I = Map.begin(), E = Map.end(); I != E;) {
11802           BackedgeTakenInfo &BEInfo = I->second;
11803           if (BEInfo.hasOperand(S, this)) {
11804             BEInfo.clear();
11805             Map.erase(I++);
11806           } else
11807             ++I;
11808         }
11809       };
11810 
11811   RemoveSCEVFromBackedgeMap(BackedgeTakenCounts);
11812   RemoveSCEVFromBackedgeMap(PredicatedBackedgeTakenCounts);
11813 }
11814 
11815 void
getUsedLoops(const SCEV * S,SmallPtrSetImpl<const Loop * > & LoopsUsed)11816 ScalarEvolution::getUsedLoops(const SCEV *S,
11817                               SmallPtrSetImpl<const Loop *> &LoopsUsed) {
11818   struct FindUsedLoops {
11819     FindUsedLoops(SmallPtrSetImpl<const Loop *> &LoopsUsed)
11820         : LoopsUsed(LoopsUsed) {}
11821     SmallPtrSetImpl<const Loop *> &LoopsUsed;
11822     bool follow(const SCEV *S) {
11823       if (auto *AR = dyn_cast<SCEVAddRecExpr>(S))
11824         LoopsUsed.insert(AR->getLoop());
11825       return true;
11826     }
11827 
11828     bool isDone() const { return false; }
11829   };
11830 
11831   FindUsedLoops F(LoopsUsed);
11832   SCEVTraversal<FindUsedLoops>(F).visitAll(S);
11833 }
11834 
addToLoopUseLists(const SCEV * S)11835 void ScalarEvolution::addToLoopUseLists(const SCEV *S) {
11836   SmallPtrSet<const Loop *, 8> LoopsUsed;
11837   getUsedLoops(S, LoopsUsed);
11838   for (auto *L : LoopsUsed)
11839     LoopUsers[L].push_back(S);
11840 }
11841 
verify() const11842 void ScalarEvolution::verify() const {
11843   ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
11844   ScalarEvolution SE2(F, TLI, AC, DT, LI);
11845 
11846   SmallVector<Loop *, 8> LoopStack(LI.begin(), LI.end());
11847 
11848   // Map's SCEV expressions from one ScalarEvolution "universe" to another.
11849   struct SCEVMapper : public SCEVRewriteVisitor<SCEVMapper> {
11850     SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor<SCEVMapper>(SE) {}
11851 
11852     const SCEV *visitConstant(const SCEVConstant *Constant) {
11853       return SE.getConstant(Constant->getAPInt());
11854     }
11855 
11856     const SCEV *visitUnknown(const SCEVUnknown *Expr) {
11857       return SE.getUnknown(Expr->getValue());
11858     }
11859 
11860     const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
11861       return SE.getCouldNotCompute();
11862     }
11863   };
11864 
11865   SCEVMapper SCM(SE2);
11866 
11867   while (!LoopStack.empty()) {
11868     auto *L = LoopStack.pop_back_val();
11869     LoopStack.insert(LoopStack.end(), L->begin(), L->end());
11870 
11871     auto *CurBECount = SCM.visit(
11872         const_cast<ScalarEvolution *>(this)->getBackedgeTakenCount(L));
11873     auto *NewBECount = SE2.getBackedgeTakenCount(L);
11874 
11875     if (CurBECount == SE2.getCouldNotCompute() ||
11876         NewBECount == SE2.getCouldNotCompute()) {
11877       // NB! This situation is legal, but is very suspicious -- whatever pass
11878       // change the loop to make a trip count go from could not compute to
11879       // computable or vice-versa *should have* invalidated SCEV.  However, we
11880       // choose not to assert here (for now) since we don't want false
11881       // positives.
11882       continue;
11883     }
11884 
11885     if (containsUndefs(CurBECount) || containsUndefs(NewBECount)) {
11886       // SCEV treats "undef" as an unknown but consistent value (i.e. it does
11887       // not propagate undef aggressively).  This means we can (and do) fail
11888       // verification in cases where a transform makes the trip count of a loop
11889       // go from "undef" to "undef+1" (say).  The transform is fine, since in
11890       // both cases the loop iterates "undef" times, but SCEV thinks we
11891       // increased the trip count of the loop by 1 incorrectly.
11892       continue;
11893     }
11894 
11895     if (SE.getTypeSizeInBits(CurBECount->getType()) >
11896         SE.getTypeSizeInBits(NewBECount->getType()))
11897       NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType());
11898     else if (SE.getTypeSizeInBits(CurBECount->getType()) <
11899              SE.getTypeSizeInBits(NewBECount->getType()))
11900       CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType());
11901 
11902     const SCEV *Delta = SE2.getMinusSCEV(CurBECount, NewBECount);
11903 
11904     // Unless VerifySCEVStrict is set, we only compare constant deltas.
11905     if ((VerifySCEVStrict || isa<SCEVConstant>(Delta)) && !Delta->isZero()) {
11906       dbgs() << "Trip Count for " << *L << " Changed!\n";
11907       dbgs() << "Old: " << *CurBECount << "\n";
11908       dbgs() << "New: " << *NewBECount << "\n";
11909       dbgs() << "Delta: " << *Delta << "\n";
11910       std::abort();
11911     }
11912   }
11913 }
11914 
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator & Inv)11915 bool ScalarEvolution::invalidate(
11916     Function &F, const PreservedAnalyses &PA,
11917     FunctionAnalysisManager::Invalidator &Inv) {
11918   // Invalidate the ScalarEvolution object whenever it isn't preserved or one
11919   // of its dependencies is invalidated.
11920   auto PAC = PA.getChecker<ScalarEvolutionAnalysis>();
11921   return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
11922          Inv.invalidate<AssumptionAnalysis>(F, PA) ||
11923          Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
11924          Inv.invalidate<LoopAnalysis>(F, PA);
11925 }
11926 
11927 AnalysisKey ScalarEvolutionAnalysis::Key;
11928 
run(Function & F,FunctionAnalysisManager & AM)11929 ScalarEvolution ScalarEvolutionAnalysis::run(Function &F,
11930                                              FunctionAnalysisManager &AM) {
11931   return ScalarEvolution(F, AM.getResult<TargetLibraryAnalysis>(F),
11932                          AM.getResult<AssumptionAnalysis>(F),
11933                          AM.getResult<DominatorTreeAnalysis>(F),
11934                          AM.getResult<LoopAnalysis>(F));
11935 }
11936 
11937 PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)11938 ScalarEvolutionVerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
11939   AM.getResult<ScalarEvolutionAnalysis>(F).verify();
11940   return PreservedAnalyses::all();
11941 }
11942 
11943 PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)11944 ScalarEvolutionPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
11945   AM.getResult<ScalarEvolutionAnalysis>(F).print(OS);
11946   return PreservedAnalyses::all();
11947 }
11948 
11949 INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution",
11950                       "Scalar Evolution Analysis", false, true)
11951 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
11952 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
11953 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
11954 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
11955 INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution",
11956                     "Scalar Evolution Analysis", false, true)
11957 
11958 char ScalarEvolutionWrapperPass::ID = 0;
11959 
ScalarEvolutionWrapperPass()11960 ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) {
11961   initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry());
11962 }
11963 
runOnFunction(Function & F)11964 bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) {
11965   SE.reset(new ScalarEvolution(
11966       F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
11967       getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
11968       getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
11969       getAnalysis<LoopInfoWrapperPass>().getLoopInfo()));
11970   return false;
11971 }
11972 
releaseMemory()11973 void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); }
11974 
print(raw_ostream & OS,const Module *) const11975 void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const {
11976   SE->print(OS);
11977 }
11978 
verifyAnalysis() const11979 void ScalarEvolutionWrapperPass::verifyAnalysis() const {
11980   if (!VerifySCEV)
11981     return;
11982 
11983   SE->verify();
11984 }
11985 
getAnalysisUsage(AnalysisUsage & AU) const11986 void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
11987   AU.setPreservesAll();
11988   AU.addRequiredTransitive<AssumptionCacheTracker>();
11989   AU.addRequiredTransitive<LoopInfoWrapperPass>();
11990   AU.addRequiredTransitive<DominatorTreeWrapperPass>();
11991   AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
11992 }
11993 
getEqualPredicate(const SCEV * LHS,const SCEV * RHS)11994 const SCEVPredicate *ScalarEvolution::getEqualPredicate(const SCEV *LHS,
11995                                                         const SCEV *RHS) {
11996   FoldingSetNodeID ID;
11997   assert(LHS->getType() == RHS->getType() &&
11998          "Type mismatch between LHS and RHS");
11999   // Unique this node based on the arguments
12000   ID.AddInteger(SCEVPredicate::P_Equal);
12001   ID.AddPointer(LHS);
12002   ID.AddPointer(RHS);
12003   void *IP = nullptr;
12004   if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
12005     return S;
12006   SCEVEqualPredicate *Eq = new (SCEVAllocator)
12007       SCEVEqualPredicate(ID.Intern(SCEVAllocator), LHS, RHS);
12008   UniquePreds.InsertNode(Eq, IP);
12009   return Eq;
12010 }
12011 
getWrapPredicate(const SCEVAddRecExpr * AR,SCEVWrapPredicate::IncrementWrapFlags AddedFlags)12012 const SCEVPredicate *ScalarEvolution::getWrapPredicate(
12013     const SCEVAddRecExpr *AR,
12014     SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
12015   FoldingSetNodeID ID;
12016   // Unique this node based on the arguments
12017   ID.AddInteger(SCEVPredicate::P_Wrap);
12018   ID.AddPointer(AR);
12019   ID.AddInteger(AddedFlags);
12020   void *IP = nullptr;
12021   if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
12022     return S;
12023   auto *OF = new (SCEVAllocator)
12024       SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags);
12025   UniquePreds.InsertNode(OF, IP);
12026   return OF;
12027 }
12028 
12029 namespace {
12030 
12031 class SCEVPredicateRewriter : public SCEVRewriteVisitor<SCEVPredicateRewriter> {
12032 public:
12033 
12034   /// Rewrites \p S in the context of a loop L and the SCEV predication
12035   /// infrastructure.
12036   ///
12037   /// If \p Pred is non-null, the SCEV expression is rewritten to respect the
12038   /// equivalences present in \p Pred.
12039   ///
12040   /// If \p NewPreds is non-null, rewrite is free to add further predicates to
12041   /// \p NewPreds such that the result will be an AddRecExpr.
rewrite(const SCEV * S,const Loop * L,ScalarEvolution & SE,SmallPtrSetImpl<const SCEVPredicate * > * NewPreds,SCEVUnionPredicate * Pred)12042   static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
12043                              SmallPtrSetImpl<const SCEVPredicate *> *NewPreds,
12044                              SCEVUnionPredicate *Pred) {
12045     SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred);
12046     return Rewriter.visit(S);
12047   }
12048 
visitUnknown(const SCEVUnknown * Expr)12049   const SCEV *visitUnknown(const SCEVUnknown *Expr) {
12050     if (Pred) {
12051       auto ExprPreds = Pred->getPredicatesForExpr(Expr);
12052       for (auto *Pred : ExprPreds)
12053         if (const auto *IPred = dyn_cast<SCEVEqualPredicate>(Pred))
12054           if (IPred->getLHS() == Expr)
12055             return IPred->getRHS();
12056     }
12057     return convertToAddRecWithPreds(Expr);
12058   }
12059 
visitZeroExtendExpr(const SCEVZeroExtendExpr * Expr)12060   const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
12061     const SCEV *Operand = visit(Expr->getOperand());
12062     const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
12063     if (AR && AR->getLoop() == L && AR->isAffine()) {
12064       // This couldn't be folded because the operand didn't have the nuw
12065       // flag. Add the nusw flag as an assumption that we could make.
12066       const SCEV *Step = AR->getStepRecurrence(SE);
12067       Type *Ty = Expr->getType();
12068       if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW))
12069         return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty),
12070                                 SE.getSignExtendExpr(Step, Ty), L,
12071                                 AR->getNoWrapFlags());
12072     }
12073     return SE.getZeroExtendExpr(Operand, Expr->getType());
12074   }
12075 
visitSignExtendExpr(const SCEVSignExtendExpr * Expr)12076   const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
12077     const SCEV *Operand = visit(Expr->getOperand());
12078     const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
12079     if (AR && AR->getLoop() == L && AR->isAffine()) {
12080       // This couldn't be folded because the operand didn't have the nsw
12081       // flag. Add the nssw flag as an assumption that we could make.
12082       const SCEV *Step = AR->getStepRecurrence(SE);
12083       Type *Ty = Expr->getType();
12084       if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW))
12085         return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty),
12086                                 SE.getSignExtendExpr(Step, Ty), L,
12087                                 AR->getNoWrapFlags());
12088     }
12089     return SE.getSignExtendExpr(Operand, Expr->getType());
12090   }
12091 
12092 private:
SCEVPredicateRewriter(const Loop * L,ScalarEvolution & SE,SmallPtrSetImpl<const SCEVPredicate * > * NewPreds,SCEVUnionPredicate * Pred)12093   explicit SCEVPredicateRewriter(const Loop *L, ScalarEvolution &SE,
12094                         SmallPtrSetImpl<const SCEVPredicate *> *NewPreds,
12095                         SCEVUnionPredicate *Pred)
12096       : SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {}
12097 
addOverflowAssumption(const SCEVPredicate * P)12098   bool addOverflowAssumption(const SCEVPredicate *P) {
12099     if (!NewPreds) {
12100       // Check if we've already made this assumption.
12101       return Pred && Pred->implies(P);
12102     }
12103     NewPreds->insert(P);
12104     return true;
12105   }
12106 
addOverflowAssumption(const SCEVAddRecExpr * AR,SCEVWrapPredicate::IncrementWrapFlags AddedFlags)12107   bool addOverflowAssumption(const SCEVAddRecExpr *AR,
12108                              SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
12109     auto *A = SE.getWrapPredicate(AR, AddedFlags);
12110     return addOverflowAssumption(A);
12111   }
12112 
12113   // If \p Expr represents a PHINode, we try to see if it can be represented
12114   // as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible
12115   // to add this predicate as a runtime overflow check, we return the AddRec.
12116   // If \p Expr does not meet these conditions (is not a PHI node, or we
12117   // couldn't create an AddRec for it, or couldn't add the predicate), we just
12118   // return \p Expr.
convertToAddRecWithPreds(const SCEVUnknown * Expr)12119   const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) {
12120     if (!isa<PHINode>(Expr->getValue()))
12121       return Expr;
12122     Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
12123     PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr);
12124     if (!PredicatedRewrite)
12125       return Expr;
12126     for (auto *P : PredicatedRewrite->second){
12127       // Wrap predicates from outer loops are not supported.
12128       if (auto *WP = dyn_cast<const SCEVWrapPredicate>(P)) {
12129         auto *AR = cast<const SCEVAddRecExpr>(WP->getExpr());
12130         if (L != AR->getLoop())
12131           return Expr;
12132       }
12133       if (!addOverflowAssumption(P))
12134         return Expr;
12135     }
12136     return PredicatedRewrite->first;
12137   }
12138 
12139   SmallPtrSetImpl<const SCEVPredicate *> *NewPreds;
12140   SCEVUnionPredicate *Pred;
12141   const Loop *L;
12142 };
12143 
12144 } // end anonymous namespace
12145 
rewriteUsingPredicate(const SCEV * S,const Loop * L,SCEVUnionPredicate & Preds)12146 const SCEV *ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L,
12147                                                    SCEVUnionPredicate &Preds) {
12148   return SCEVPredicateRewriter::rewrite(S, L, *this, nullptr, &Preds);
12149 }
12150 
convertSCEVToAddRecWithPredicates(const SCEV * S,const Loop * L,SmallPtrSetImpl<const SCEVPredicate * > & Preds)12151 const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates(
12152     const SCEV *S, const Loop *L,
12153     SmallPtrSetImpl<const SCEVPredicate *> &Preds) {
12154   SmallPtrSet<const SCEVPredicate *, 4> TransformPreds;
12155   S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds, nullptr);
12156   auto *AddRec = dyn_cast<SCEVAddRecExpr>(S);
12157 
12158   if (!AddRec)
12159     return nullptr;
12160 
12161   // Since the transformation was successful, we can now transfer the SCEV
12162   // predicates.
12163   for (auto *P : TransformPreds)
12164     Preds.insert(P);
12165 
12166   return AddRec;
12167 }
12168 
12169 /// SCEV predicates
SCEVPredicate(const FoldingSetNodeIDRef ID,SCEVPredicateKind Kind)12170 SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID,
12171                              SCEVPredicateKind Kind)
12172     : FastID(ID), Kind(Kind) {}
12173 
SCEVEqualPredicate(const FoldingSetNodeIDRef ID,const SCEV * LHS,const SCEV * RHS)12174 SCEVEqualPredicate::SCEVEqualPredicate(const FoldingSetNodeIDRef ID,
12175                                        const SCEV *LHS, const SCEV *RHS)
12176     : SCEVPredicate(ID, P_Equal), LHS(LHS), RHS(RHS) {
12177   assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match");
12178   assert(LHS != RHS && "LHS and RHS are the same SCEV");
12179 }
12180 
implies(const SCEVPredicate * N) const12181 bool SCEVEqualPredicate::implies(const SCEVPredicate *N) const {
12182   const auto *Op = dyn_cast<SCEVEqualPredicate>(N);
12183 
12184   if (!Op)
12185     return false;
12186 
12187   return Op->LHS == LHS && Op->RHS == RHS;
12188 }
12189 
isAlwaysTrue() const12190 bool SCEVEqualPredicate::isAlwaysTrue() const { return false; }
12191 
getExpr() const12192 const SCEV *SCEVEqualPredicate::getExpr() const { return LHS; }
12193 
print(raw_ostream & OS,unsigned Depth) const12194 void SCEVEqualPredicate::print(raw_ostream &OS, unsigned Depth) const {
12195   OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n";
12196 }
12197 
SCEVWrapPredicate(const FoldingSetNodeIDRef ID,const SCEVAddRecExpr * AR,IncrementWrapFlags Flags)12198 SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID,
12199                                      const SCEVAddRecExpr *AR,
12200                                      IncrementWrapFlags Flags)
12201     : SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {}
12202 
getExpr() const12203 const SCEV *SCEVWrapPredicate::getExpr() const { return AR; }
12204 
implies(const SCEVPredicate * N) const12205 bool SCEVWrapPredicate::implies(const SCEVPredicate *N) const {
12206   const auto *Op = dyn_cast<SCEVWrapPredicate>(N);
12207 
12208   return Op && Op->AR == AR && setFlags(Flags, Op->Flags) == Flags;
12209 }
12210 
isAlwaysTrue() const12211 bool SCEVWrapPredicate::isAlwaysTrue() const {
12212   SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags();
12213   IncrementWrapFlags IFlags = Flags;
12214 
12215   if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags)
12216     IFlags = clearFlags(IFlags, IncrementNSSW);
12217 
12218   return IFlags == IncrementAnyWrap;
12219 }
12220 
print(raw_ostream & OS,unsigned Depth) const12221 void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const {
12222   OS.indent(Depth) << *getExpr() << " Added Flags: ";
12223   if (SCEVWrapPredicate::IncrementNUSW & getFlags())
12224     OS << "<nusw>";
12225   if (SCEVWrapPredicate::IncrementNSSW & getFlags())
12226     OS << "<nssw>";
12227   OS << "\n";
12228 }
12229 
12230 SCEVWrapPredicate::IncrementWrapFlags
getImpliedFlags(const SCEVAddRecExpr * AR,ScalarEvolution & SE)12231 SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR,
12232                                    ScalarEvolution &SE) {
12233   IncrementWrapFlags ImpliedFlags = IncrementAnyWrap;
12234   SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags();
12235 
12236   // We can safely transfer the NSW flag as NSSW.
12237   if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags)
12238     ImpliedFlags = IncrementNSSW;
12239 
12240   if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) {
12241     // If the increment is positive, the SCEV NUW flag will also imply the
12242     // WrapPredicate NUSW flag.
12243     if (const auto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE)))
12244       if (Step->getValue()->getValue().isNonNegative())
12245         ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW);
12246   }
12247 
12248   return ImpliedFlags;
12249 }
12250 
12251 /// Union predicates don't get cached so create a dummy set ID for it.
SCEVUnionPredicate()12252 SCEVUnionPredicate::SCEVUnionPredicate()
12253     : SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) {}
12254 
isAlwaysTrue() const12255 bool SCEVUnionPredicate::isAlwaysTrue() const {
12256   return all_of(Preds,
12257                 [](const SCEVPredicate *I) { return I->isAlwaysTrue(); });
12258 }
12259 
12260 ArrayRef<const SCEVPredicate *>
getPredicatesForExpr(const SCEV * Expr)12261 SCEVUnionPredicate::getPredicatesForExpr(const SCEV *Expr) {
12262   auto I = SCEVToPreds.find(Expr);
12263   if (I == SCEVToPreds.end())
12264     return ArrayRef<const SCEVPredicate *>();
12265   return I->second;
12266 }
12267 
implies(const SCEVPredicate * N) const12268 bool SCEVUnionPredicate::implies(const SCEVPredicate *N) const {
12269   if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N))
12270     return all_of(Set->Preds,
12271                   [this](const SCEVPredicate *I) { return this->implies(I); });
12272 
12273   auto ScevPredsIt = SCEVToPreds.find(N->getExpr());
12274   if (ScevPredsIt == SCEVToPreds.end())
12275     return false;
12276   auto &SCEVPreds = ScevPredsIt->second;
12277 
12278   return any_of(SCEVPreds,
12279                 [N](const SCEVPredicate *I) { return I->implies(N); });
12280 }
12281 
getExpr() const12282 const SCEV *SCEVUnionPredicate::getExpr() const { return nullptr; }
12283 
print(raw_ostream & OS,unsigned Depth) const12284 void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const {
12285   for (auto Pred : Preds)
12286     Pred->print(OS, Depth);
12287 }
12288 
add(const SCEVPredicate * N)12289 void SCEVUnionPredicate::add(const SCEVPredicate *N) {
12290   if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N)) {
12291     for (auto Pred : Set->Preds)
12292       add(Pred);
12293     return;
12294   }
12295 
12296   if (implies(N))
12297     return;
12298 
12299   const SCEV *Key = N->getExpr();
12300   assert(Key && "Only SCEVUnionPredicate doesn't have an "
12301                 " associated expression!");
12302 
12303   SCEVToPreds[Key].push_back(N);
12304   Preds.push_back(N);
12305 }
12306 
PredicatedScalarEvolution(ScalarEvolution & SE,Loop & L)12307 PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE,
12308                                                      Loop &L)
12309     : SE(SE), L(L) {}
12310 
getSCEV(Value * V)12311 const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) {
12312   const SCEV *Expr = SE.getSCEV(V);
12313   RewriteEntry &Entry = RewriteMap[Expr];
12314 
12315   // If we already have an entry and the version matches, return it.
12316   if (Entry.second && Generation == Entry.first)
12317     return Entry.second;
12318 
12319   // We found an entry but it's stale. Rewrite the stale entry
12320   // according to the current predicate.
12321   if (Entry.second)
12322     Expr = Entry.second;
12323 
12324   const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, Preds);
12325   Entry = {Generation, NewSCEV};
12326 
12327   return NewSCEV;
12328 }
12329 
getBackedgeTakenCount()12330 const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() {
12331   if (!BackedgeCount) {
12332     SCEVUnionPredicate BackedgePred;
12333     BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, BackedgePred);
12334     addPredicate(BackedgePred);
12335   }
12336   return BackedgeCount;
12337 }
12338 
addPredicate(const SCEVPredicate & Pred)12339 void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) {
12340   if (Preds.implies(&Pred))
12341     return;
12342   Preds.add(&Pred);
12343   updateGeneration();
12344 }
12345 
getUnionPredicate() const12346 const SCEVUnionPredicate &PredicatedScalarEvolution::getUnionPredicate() const {
12347   return Preds;
12348 }
12349 
updateGeneration()12350 void PredicatedScalarEvolution::updateGeneration() {
12351   // If the generation number wrapped recompute everything.
12352   if (++Generation == 0) {
12353     for (auto &II : RewriteMap) {
12354       const SCEV *Rewritten = II.second.second;
12355       II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, Preds)};
12356     }
12357   }
12358 }
12359 
setNoOverflow(Value * V,SCEVWrapPredicate::IncrementWrapFlags Flags)12360 void PredicatedScalarEvolution::setNoOverflow(
12361     Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
12362   const SCEV *Expr = getSCEV(V);
12363   const auto *AR = cast<SCEVAddRecExpr>(Expr);
12364 
12365   auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE);
12366 
12367   // Clear the statically implied flags.
12368   Flags = SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags);
12369   addPredicate(*SE.getWrapPredicate(AR, Flags));
12370 
12371   auto II = FlagsMap.insert({V, Flags});
12372   if (!II.second)
12373     II.first->second = SCEVWrapPredicate::setFlags(Flags, II.first->second);
12374 }
12375 
hasNoOverflow(Value * V,SCEVWrapPredicate::IncrementWrapFlags Flags)12376 bool PredicatedScalarEvolution::hasNoOverflow(
12377     Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
12378   const SCEV *Expr = getSCEV(V);
12379   const auto *AR = cast<SCEVAddRecExpr>(Expr);
12380 
12381   Flags = SCEVWrapPredicate::clearFlags(
12382       Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE));
12383 
12384   auto II = FlagsMap.find(V);
12385 
12386   if (II != FlagsMap.end())
12387     Flags = SCEVWrapPredicate::clearFlags(Flags, II->second);
12388 
12389   return Flags == SCEVWrapPredicate::IncrementAnyWrap;
12390 }
12391 
getAsAddRec(Value * V)12392 const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) {
12393   const SCEV *Expr = this->getSCEV(V);
12394   SmallPtrSet<const SCEVPredicate *, 4> NewPreds;
12395   auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds);
12396 
12397   if (!New)
12398     return nullptr;
12399 
12400   for (auto *P : NewPreds)
12401     Preds.add(P);
12402 
12403   updateGeneration();
12404   RewriteMap[SE.getSCEV(V)] = {Generation, New};
12405   return New;
12406 }
12407 
PredicatedScalarEvolution(const PredicatedScalarEvolution & Init)12408 PredicatedScalarEvolution::PredicatedScalarEvolution(
12409     const PredicatedScalarEvolution &Init)
12410     : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L), Preds(Init.Preds),
12411       Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) {
12412   for (auto I : Init.FlagsMap)
12413     FlagsMap.insert(I);
12414 }
12415 
print(raw_ostream & OS,unsigned Depth) const12416 void PredicatedScalarEvolution::print(raw_ostream &OS, unsigned Depth) const {
12417   // For each block.
12418   for (auto *BB : L.getBlocks())
12419     for (auto &I : *BB) {
12420       if (!SE.isSCEVable(I.getType()))
12421         continue;
12422 
12423       auto *Expr = SE.getSCEV(&I);
12424       auto II = RewriteMap.find(Expr);
12425 
12426       if (II == RewriteMap.end())
12427         continue;
12428 
12429       // Don't print things that are not interesting.
12430       if (II->second.second == Expr)
12431         continue;
12432 
12433       OS.indent(Depth) << "[PSE]" << I << ":\n";
12434       OS.indent(Depth + 2) << *Expr << "\n";
12435       OS.indent(Depth + 2) << "--> " << *II->second.second << "\n";
12436     }
12437 }
12438 
12439 // Match the mathematical pattern A - (A / B) * B, where A and B can be
12440 // arbitrary expressions.
12441 // It's not always easy, as A and B can be folded (imagine A is X / 2, and B is
12442 // 4, A / B becomes X / 8).
matchURem(const SCEV * Expr,const SCEV * & LHS,const SCEV * & RHS)12443 bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS,
12444                                 const SCEV *&RHS) {
12445   const auto *Add = dyn_cast<SCEVAddExpr>(Expr);
12446   if (Add == nullptr || Add->getNumOperands() != 2)
12447     return false;
12448 
12449   const SCEV *A = Add->getOperand(1);
12450   const auto *Mul = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
12451 
12452   if (Mul == nullptr)
12453     return false;
12454 
12455   const auto MatchURemWithDivisor = [&](const SCEV *B) {
12456     // (SomeExpr + (-(SomeExpr / B) * B)).
12457     if (Expr == getURemExpr(A, B)) {
12458       LHS = A;
12459       RHS = B;
12460       return true;
12461     }
12462     return false;
12463   };
12464 
12465   // (SomeExpr + (-1 * (SomeExpr / B) * B)).
12466   if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0)))
12467     return MatchURemWithDivisor(Mul->getOperand(1)) ||
12468            MatchURemWithDivisor(Mul->getOperand(2));
12469 
12470   // (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)).
12471   if (Mul->getNumOperands() == 2)
12472     return MatchURemWithDivisor(Mul->getOperand(1)) ||
12473            MatchURemWithDivisor(Mul->getOperand(0)) ||
12474            MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(1))) ||
12475            MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(0)));
12476   return false;
12477 }
12478