1 //===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the implementation of the scalar evolution analysis
11 // engine, which is used primarily to analyze expressions involving induction
12 // variables in loops.
14 // There are several aspects to this library. First is the representation of
15 // scalar expressions, which are represented as subclasses of the SCEV class.
16 // These classes are used to represent certain types of subexpressions that we
17 // can handle. We only create one SCEV of a particular shape, so
18 // pointer-comparisons for equality are legal.
20 // One important aspect of the SCEV objects is that they are never cyclic, even
21 // if there is a cycle in the dataflow for an expression (ie, a PHI node). If
22 // the PHI node is one of the idioms that we can represent (e.g., a polynomial
23 // recurrence) then we represent it directly as a recurrence node, otherwise we
24 // represent it as a SCEVUnknown node.
26 // In addition to being able to represent expressions of various types, we also
27 // have folders that are used to build the *canonical* representation for a
28 // particular expression. These folders are capable of using a variety of
29 // rewrite rules to simplify the expressions.
31 // Once the folders are defined, we can implement the more interesting
32 // higher-level code, such as the code that recognizes PHI nodes of various
33 // types, computes the execution count of a loop, etc.
35 // TODO: We should use these routines and value representations to implement
36 // dependence analysis!
38 //===----------------------------------------------------------------------===//
40 // There are several good references for the techniques used in this analysis.
42 // Chains of recurrences -- a method to expedite the evaluation
43 // of closed-form functions
44 // Olaf Bachmann, Paul S. Wang, Eugene V. Zima
46 // On computational properties of chains of recurrences
49 // Symbolic Evaluation of Chains of Recurrences for Loop Optimization
50 // Robert A. van Engelen
52 // Efficient Symbolic Analysis for Optimizing Compilers
53 // Robert A. van Engelen
55 // Using the chains of recurrences algebra for data dependence testing and
56 // induction variable substitution
57 // MS Thesis, Johnie Birch
59 //===----------------------------------------------------------------------===//
61 #include "llvm/Analysis/ScalarEvolution.h"
62 #include "llvm/ADT/APInt.h"
63 #include "llvm/ADT/ArrayRef.h"
64 #include "llvm/ADT/DenseMap.h"
65 #include "llvm/ADT/DepthFirstIterator.h"
66 #include "llvm/ADT/EquivalenceClasses.h"
67 #include "llvm/ADT/FoldingSet.h"
68 #include "llvm/ADT/None.h"
69 #include "llvm/ADT/Optional.h"
70 #include "llvm/ADT/STLExtras.h"
71 #include "llvm/ADT/ScopeExit.h"
72 #include "llvm/ADT/Sequence.h"
73 #include "llvm/ADT/SetVector.h"
74 #include "llvm/ADT/SmallPtrSet.h"
75 #include "llvm/ADT/SmallSet.h"
76 #include "llvm/ADT/SmallVector.h"
77 #include "llvm/ADT/Statistic.h"
78 #include "llvm/ADT/StringRef.h"
79 #include "llvm/Analysis/AssumptionCache.h"
80 #include "llvm/Analysis/ConstantFolding.h"
81 #include "llvm/Analysis/InstructionSimplify.h"
82 #include "llvm/Analysis/LoopInfo.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/CallSite.h"
91 #include "llvm/IR/Constant.h"
92 #include "llvm/IR/ConstantRange.h"
93 #include "llvm/IR/Constants.h"
94 #include "llvm/IR/DataLayout.h"
95 #include "llvm/IR/DerivedTypes.h"
96 #include "llvm/IR/Dominators.h"
97 #include "llvm/IR/Function.h"
98 #include "llvm/IR/GlobalAlias.h"
99 #include "llvm/IR/GlobalValue.h"
100 #include "llvm/IR/GlobalVariable.h"
101 #include "llvm/IR/InstIterator.h"
102 #include "llvm/IR/InstrTypes.h"
103 #include "llvm/IR/Instruction.h"
104 #include "llvm/IR/Instructions.h"
105 #include "llvm/IR/IntrinsicInst.h"
106 #include "llvm/IR/Intrinsics.h"
107 #include "llvm/IR/LLVMContext.h"
108 #include "llvm/IR/Metadata.h"
109 #include "llvm/IR/Operator.h"
110 #include "llvm/IR/PatternMatch.h"
111 #include "llvm/IR/Type.h"
112 #include "llvm/IR/Use.h"
113 #include "llvm/IR/User.h"
114 #include "llvm/IR/Value.h"
115 #include "llvm/IR/Verifier.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"
137 using namespace llvm;
139 #define DEBUG_TYPE "scalar-evolution"
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");
150 static cl::opt<unsigned>
151 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
152 cl::desc("Maximum number of iterations SCEV will "
153 "symbolically execute a constant "
157 // FIXME: Enable this with EXPENSIVE_CHECKS when the test suite is clean.
158 static cl::opt<bool> VerifySCEV(
159 "verify-scev", cl::Hidden,
160 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
162 VerifySCEVMap("verify-scev-maps", cl::Hidden,
163 cl::desc("Verify no dangling value in ScalarEvolution's "
164 "ExprValueMap (slow)"));
166 static cl::opt<bool> VerifyIR(
167 "scev-verify-ir", cl::Hidden,
168 cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"),
171 static cl::opt<unsigned> MulOpsInlineThreshold(
172 "scev-mulops-inline-threshold", cl::Hidden,
173 cl::desc("Threshold for inlining multiplication operands into a SCEV"),
176 static cl::opt<unsigned> AddOpsInlineThreshold(
177 "scev-addops-inline-threshold", cl::Hidden,
178 cl::desc("Threshold for inlining addition operands into a SCEV"),
181 static cl::opt<unsigned> MaxSCEVCompareDepth(
182 "scalar-evolution-max-scev-compare-depth", cl::Hidden,
183 cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
186 static cl::opt<unsigned> MaxSCEVOperationsImplicationDepth(
187 "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden,
188 cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
191 static cl::opt<unsigned> MaxValueCompareDepth(
192 "scalar-evolution-max-value-compare-depth", cl::Hidden,
193 cl::desc("Maximum depth of recursive value complexity comparisons"),
196 static cl::opt<unsigned>
197 MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden,
198 cl::desc("Maximum depth of recursive arithmetics"),
201 static cl::opt<unsigned> MaxConstantEvolvingDepth(
202 "scalar-evolution-max-constant-evolving-depth", cl::Hidden,
203 cl::desc("Maximum depth of recursive constant evolving"), cl::init(32));
205 static cl::opt<unsigned>
206 MaxExtDepth("scalar-evolution-max-ext-depth", cl::Hidden,
207 cl::desc("Maximum depth of recursive SExt/ZExt"),
210 static cl::opt<unsigned>
211 MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
212 cl::desc("Max coefficients in AddRec during evolving"),
215 //===----------------------------------------------------------------------===//
216 // SCEV class definitions
217 //===----------------------------------------------------------------------===//
219 //===----------------------------------------------------------------------===//
220 // Implementation of the SCEV class.
223 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
224 LLVM_DUMP_METHOD void SCEV::dump() const {
230 void SCEV::print(raw_ostream &OS) const {
231 switch (static_cast<SCEVTypes>(getSCEVType())) {
233 cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
236 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
237 const SCEV *Op = Trunc->getOperand();
238 OS << "(trunc " << *Op->getType() << " " << *Op << " to "
239 << *Trunc->getType() << ")";
243 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
244 const SCEV *Op = ZExt->getOperand();
245 OS << "(zext " << *Op->getType() << " " << *Op << " to "
246 << *ZExt->getType() << ")";
250 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
251 const SCEV *Op = SExt->getOperand();
252 OS << "(sext " << *Op->getType() << " " << *Op << " to "
253 << *SExt->getType() << ")";
257 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
258 OS << "{" << *AR->getOperand(0);
259 for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
260 OS << ",+," << *AR->getOperand(i);
262 if (AR->hasNoUnsignedWrap())
264 if (AR->hasNoSignedWrap())
266 if (AR->hasNoSelfWrap() &&
267 !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW | FlagNSW)))
269 AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false);
277 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
278 const char *OpStr = nullptr;
279 switch (NAry->getSCEVType()) {
280 case scAddExpr: OpStr = " + "; break;
281 case scMulExpr: OpStr = " * "; break;
282 case scUMaxExpr: OpStr = " umax "; break;
283 case scSMaxExpr: OpStr = " smax "; break;
286 for (SCEVNAryExpr::op_iterator I = NAry->op_begin(), E = NAry->op_end();
289 if (std::next(I) != E)
293 switch (NAry->getSCEVType()) {
296 if (NAry->hasNoUnsignedWrap())
298 if (NAry->hasNoSignedWrap())
304 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
305 OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
309 const SCEVUnknown *U = cast<SCEVUnknown>(this);
311 if (U->isSizeOf(AllocTy)) {
312 OS << "sizeof(" << *AllocTy << ")";
315 if (U->isAlignOf(AllocTy)) {
316 OS << "alignof(" << *AllocTy << ")";
322 if (U->isOffsetOf(CTy, FieldNo)) {
323 OS << "offsetof(" << *CTy << ", ";
324 FieldNo->printAsOperand(OS, false);
329 // Otherwise just print it normally.
330 U->getValue()->printAsOperand(OS, false);
333 case scCouldNotCompute:
334 OS << "***COULDNOTCOMPUTE***";
337 llvm_unreachable("Unknown SCEV kind!");
340 Type *SCEV::getType() const {
341 switch (static_cast<SCEVTypes>(getSCEVType())) {
343 return cast<SCEVConstant>(this)->getType();
347 return cast<SCEVCastExpr>(this)->getType();
352 return cast<SCEVNAryExpr>(this)->getType();
354 return cast<SCEVAddExpr>(this)->getType();
356 return cast<SCEVUDivExpr>(this)->getType();
358 return cast<SCEVUnknown>(this)->getType();
359 case scCouldNotCompute:
360 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
362 llvm_unreachable("Unknown SCEV kind!");
365 bool SCEV::isZero() const {
366 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
367 return SC->getValue()->isZero();
371 bool SCEV::isOne() const {
372 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
373 return SC->getValue()->isOne();
377 bool SCEV::isAllOnesValue() const {
378 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
379 return SC->getValue()->isMinusOne();
383 bool SCEV::isNonConstantNegative() const {
384 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
385 if (!Mul) return false;
387 // If there is a constant factor, it will be first.
388 const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
389 if (!SC) return false;
391 // Return true if the value is negative, this matches things like (-42 * V).
392 return SC->getAPInt().isNegative();
395 SCEVCouldNotCompute::SCEVCouldNotCompute() :
396 SCEV(FoldingSetNodeIDRef(), scCouldNotCompute) {}
398 bool SCEVCouldNotCompute::classof(const SCEV *S) {
399 return S->getSCEVType() == scCouldNotCompute;
402 const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
404 ID.AddInteger(scConstant);
407 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
408 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
409 UniqueSCEVs.InsertNode(S, IP);
413 const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
414 return getConstant(ConstantInt::get(getContext(), Val));
418 ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
419 IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
420 return getConstant(ConstantInt::get(ITy, V, isSigned));
423 SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID,
424 unsigned SCEVTy, const SCEV *op, Type *ty)
425 : SCEV(ID, SCEVTy), Op(op), Ty(ty) {}
427 SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID,
428 const SCEV *op, Type *ty)
429 : SCEVCastExpr(ID, scTruncate, op, ty) {
430 assert(Op->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
431 "Cannot truncate non-integer value!");
434 SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID,
435 const SCEV *op, Type *ty)
436 : SCEVCastExpr(ID, scZeroExtend, op, ty) {
437 assert(Op->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
438 "Cannot zero extend non-integer value!");
441 SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID,
442 const SCEV *op, Type *ty)
443 : SCEVCastExpr(ID, scSignExtend, op, ty) {
444 assert(Op->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
445 "Cannot sign extend non-integer value!");
448 void SCEVUnknown::deleted() {
449 // Clear this SCEVUnknown from various maps.
450 SE->forgetMemoizedResults(this);
452 // Remove this SCEVUnknown from the uniquing map.
453 SE->UniqueSCEVs.RemoveNode(this);
455 // Release the value.
459 void SCEVUnknown::allUsesReplacedWith(Value *New) {
460 // Remove this SCEVUnknown from the uniquing map.
461 SE->UniqueSCEVs.RemoveNode(this);
463 // Update this SCEVUnknown to point to the new value. This is needed
464 // because there may still be outstanding SCEVs which still point to
469 bool SCEVUnknown::isSizeOf(Type *&AllocTy) const {
470 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
471 if (VCE->getOpcode() == Instruction::PtrToInt)
472 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
473 if (CE->getOpcode() == Instruction::GetElementPtr &&
474 CE->getOperand(0)->isNullValue() &&
475 CE->getNumOperands() == 2)
476 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(1)))
478 AllocTy = cast<PointerType>(CE->getOperand(0)->getType())
486 bool SCEVUnknown::isAlignOf(Type *&AllocTy) const {
487 if (ConstantExpr *VCE = dyn_cast<ConstantExpr>(getValue()))
488 if (VCE->getOpcode() == Instruction::PtrToInt)
489 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(VCE->getOperand(0)))
490 if (CE->getOpcode() == Instruction::GetElementPtr &&
491 CE->getOperand(0)->isNullValue()) {
493 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
494 if (StructType *STy = dyn_cast<StructType>(Ty))
495 if (!STy->isPacked() &&
496 CE->getNumOperands() == 3 &&
497 CE->getOperand(1)->isNullValue()) {
498 if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand(2)))
500 STy->getNumElements() == 2 &&
501 STy->getElementType(0)->isIntegerTy(1)) {
502 AllocTy = STy->getElementType(1);
511 bool SCEVUnknown::isOffsetOf(Type *&CTy, Constant *&FieldNo) 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->getNumOperands() == 3 &&
517 CE->getOperand(0)->isNullValue() &&
518 CE->getOperand(1)->isNullValue()) {
520 cast<PointerType>(CE->getOperand(0)->getType())->getElementType();
521 // Ignore vector types here so that ScalarEvolutionExpander doesn't
522 // emit getelementptrs that index into vectors.
523 if (Ty->isStructTy() || Ty->isArrayTy()) {
525 FieldNo = CE->getOperand(2);
533 //===----------------------------------------------------------------------===//
535 //===----------------------------------------------------------------------===//
537 /// Compare the two values \p LV and \p RV in terms of their "complexity" where
538 /// "complexity" is a partial (and somewhat ad-hoc) relation used to order
539 /// operands in SCEV expressions. \p EqCache is a set of pairs of values that
540 /// have been previously deemed to be "equally complex" by this routine. It is
541 /// intended to avoid exponential time complexity in cases like:
551 /// CompareValueComplexity(%f, %c)
553 /// Since we do not continue running this routine on expression trees once we
554 /// have seen unequal values, there is no need to track them in the cache.
556 CompareValueComplexity(EquivalenceClasses<const Value *> &EqCacheValue,
557 const LoopInfo *const LI, Value *LV, Value *RV,
559 if (Depth > MaxValueCompareDepth || EqCacheValue.isEquivalent(LV, RV))
562 // Order pointer values after integer values. This helps SCEVExpander form
564 bool LIsPointer = LV->getType()->isPointerTy(),
565 RIsPointer = RV->getType()->isPointerTy();
566 if (LIsPointer != RIsPointer)
567 return (int)LIsPointer - (int)RIsPointer;
569 // Compare getValueID values.
570 unsigned LID = LV->getValueID(), RID = RV->getValueID();
572 return (int)LID - (int)RID;
574 // Sort arguments by their position.
575 if (const auto *LA = dyn_cast<Argument>(LV)) {
576 const auto *RA = cast<Argument>(RV);
577 unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
578 return (int)LArgNo - (int)RArgNo;
581 if (const auto *LGV = dyn_cast<GlobalValue>(LV)) {
582 const auto *RGV = cast<GlobalValue>(RV);
584 const auto IsGVNameSemantic = [&](const GlobalValue *GV) {
585 auto LT = GV->getLinkage();
586 return !(GlobalValue::isPrivateLinkage(LT) ||
587 GlobalValue::isInternalLinkage(LT));
590 // Use the names to distinguish the two values, but only if the
591 // names are semantically important.
592 if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
593 return LGV->getName().compare(RGV->getName());
596 // For instructions, compare their loop depth, and their operand count. This
598 if (const auto *LInst = dyn_cast<Instruction>(LV)) {
599 const auto *RInst = cast<Instruction>(RV);
601 // Compare loop depths.
602 const BasicBlock *LParent = LInst->getParent(),
603 *RParent = RInst->getParent();
604 if (LParent != RParent) {
605 unsigned LDepth = LI->getLoopDepth(LParent),
606 RDepth = LI->getLoopDepth(RParent);
607 if (LDepth != RDepth)
608 return (int)LDepth - (int)RDepth;
611 // Compare the number of operands.
612 unsigned LNumOps = LInst->getNumOperands(),
613 RNumOps = RInst->getNumOperands();
614 if (LNumOps != RNumOps)
615 return (int)LNumOps - (int)RNumOps;
617 for (unsigned Idx : seq(0u, LNumOps)) {
619 CompareValueComplexity(EqCacheValue, LI, LInst->getOperand(Idx),
620 RInst->getOperand(Idx), Depth + 1);
626 EqCacheValue.unionSets(LV, RV);
630 // Return negative, zero, or positive, if LHS is less than, equal to, or greater
631 // than RHS, respectively. A three-way result allows recursive comparisons to be
633 static int CompareSCEVComplexity(
634 EquivalenceClasses<const SCEV *> &EqCacheSCEV,
635 EquivalenceClasses<const Value *> &EqCacheValue,
636 const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS,
637 DominatorTree &DT, unsigned Depth = 0) {
638 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
642 // Primarily, sort the SCEVs by their getSCEVType().
643 unsigned LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
645 return (int)LType - (int)RType;
647 if (Depth > MaxSCEVCompareDepth || EqCacheSCEV.isEquivalent(LHS, RHS))
649 // Aside from the getSCEVType() ordering, the particular ordering
650 // isn't very important except that it's beneficial to be consistent,
651 // so that (a + b) and (b + a) don't end up as different expressions.
652 switch (static_cast<SCEVTypes>(LType)) {
654 const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
655 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
657 int X = CompareValueComplexity(EqCacheValue, LI, LU->getValue(),
658 RU->getValue(), Depth + 1);
660 EqCacheSCEV.unionSets(LHS, RHS);
665 const SCEVConstant *LC = cast<SCEVConstant>(LHS);
666 const SCEVConstant *RC = cast<SCEVConstant>(RHS);
668 // Compare constant values.
669 const APInt &LA = LC->getAPInt();
670 const APInt &RA = RC->getAPInt();
671 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
672 if (LBitWidth != RBitWidth)
673 return (int)LBitWidth - (int)RBitWidth;
674 return LA.ult(RA) ? -1 : 1;
678 const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
679 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
681 // There is always a dominance between two recs that are used by one SCEV,
682 // so we can safely sort recs by loop header dominance. We require such
683 // order in getAddExpr.
684 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
685 if (LLoop != RLoop) {
686 const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
687 assert(LHead != RHead && "Two loops share the same header?");
688 if (DT.dominates(LHead, RHead))
691 assert(DT.dominates(RHead, LHead) &&
692 "No dominance between recurrences used by one SCEV?");
696 // Addrec complexity grows with operand count.
697 unsigned LNumOps = LA->getNumOperands(), RNumOps = RA->getNumOperands();
698 if (LNumOps != RNumOps)
699 return (int)LNumOps - (int)RNumOps;
701 // Lexicographically compare.
702 for (unsigned i = 0; i != LNumOps; ++i) {
703 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
704 LA->getOperand(i), RA->getOperand(i), DT,
709 EqCacheSCEV.unionSets(LHS, RHS);
717 const SCEVNAryExpr *LC = cast<SCEVNAryExpr>(LHS);
718 const SCEVNAryExpr *RC = cast<SCEVNAryExpr>(RHS);
720 // Lexicographically compare n-ary expressions.
721 unsigned LNumOps = LC->getNumOperands(), RNumOps = RC->getNumOperands();
722 if (LNumOps != RNumOps)
723 return (int)LNumOps - (int)RNumOps;
725 for (unsigned i = 0; i != LNumOps; ++i) {
726 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
727 LC->getOperand(i), RC->getOperand(i), DT,
732 EqCacheSCEV.unionSets(LHS, RHS);
737 const SCEVUDivExpr *LC = cast<SCEVUDivExpr>(LHS);
738 const SCEVUDivExpr *RC = cast<SCEVUDivExpr>(RHS);
740 // Lexicographically compare udiv expressions.
741 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getLHS(),
742 RC->getLHS(), DT, Depth + 1);
745 X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, LC->getRHS(),
746 RC->getRHS(), DT, Depth + 1);
748 EqCacheSCEV.unionSets(LHS, RHS);
755 const SCEVCastExpr *LC = cast<SCEVCastExpr>(LHS);
756 const SCEVCastExpr *RC = cast<SCEVCastExpr>(RHS);
758 // Compare cast expressions by operand.
759 int X = CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
760 LC->getOperand(), RC->getOperand(), DT,
763 EqCacheSCEV.unionSets(LHS, RHS);
767 case scCouldNotCompute:
768 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
770 llvm_unreachable("Unknown SCEV kind!");
773 /// Given a list of SCEV objects, order them by their complexity, and group
774 /// objects of the same complexity together by value. When this routine is
775 /// finished, we know that any duplicates in the vector are consecutive and that
776 /// complexity is monotonically increasing.
778 /// Note that we go take special precautions to ensure that we get deterministic
779 /// results from this routine. In other words, we don't want the results of
780 /// this to depend on where the addresses of various SCEV objects happened to
782 static void GroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
783 LoopInfo *LI, DominatorTree &DT) {
784 if (Ops.size() < 2) return; // Noop
786 EquivalenceClasses<const SCEV *> EqCacheSCEV;
787 EquivalenceClasses<const Value *> EqCacheValue;
788 if (Ops.size() == 2) {
789 // This is the common case, which also happens to be trivially simple.
791 const SCEV *&LHS = Ops[0], *&RHS = Ops[1];
792 if (CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI, RHS, LHS, DT) < 0)
797 // Do the rough sort by complexity.
798 std::stable_sort(Ops.begin(), Ops.end(),
799 [&](const SCEV *LHS, const SCEV *RHS) {
800 return CompareSCEVComplexity(EqCacheSCEV, EqCacheValue, LI,
804 // Now that we are sorted by complexity, group elements of the same
805 // complexity. Note that this is, at worst, N^2, but the vector is likely to
806 // be extremely short in practice. Note that we take this approach because we
807 // do not want to depend on the addresses of the objects we are grouping.
808 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
809 const SCEV *S = Ops[i];
810 unsigned Complexity = S->getSCEVType();
812 // If there are any objects of the same complexity and same value as this
814 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
815 if (Ops[j] == S) { // Found a duplicate.
816 // Move it to immediately after i'th element.
817 std::swap(Ops[i+1], Ops[j]);
818 ++i; // no need to rescan it.
819 if (i == e-2) return; // Done!
825 // Returns the size of the SCEV S.
826 static inline int sizeOfSCEV(const SCEV *S) {
827 struct FindSCEVSize {
830 FindSCEVSize() = default;
832 bool follow(const SCEV *S) {
834 // Keep looking at all operands of S.
838 bool isDone() const {
844 SCEVTraversal<FindSCEVSize> ST(F);
851 struct SCEVDivision : public SCEVVisitor<SCEVDivision, void> {
853 // Computes the Quotient and Remainder of the division of Numerator by
855 static void divide(ScalarEvolution &SE, const SCEV *Numerator,
856 const SCEV *Denominator, const SCEV **Quotient,
857 const SCEV **Remainder) {
858 assert(Numerator && Denominator && "Uninitialized SCEV");
860 SCEVDivision D(SE, Numerator, Denominator);
862 // Check for the trivial case here to avoid having to check for it in the
864 if (Numerator == Denominator) {
870 if (Numerator->isZero()) {
876 // A simple case when N/1. The quotient is N.
877 if (Denominator->isOne()) {
878 *Quotient = Numerator;
883 // Split the Denominator when it is a product.
884 if (const SCEVMulExpr *T = dyn_cast<SCEVMulExpr>(Denominator)) {
886 *Quotient = Numerator;
887 for (const SCEV *Op : T->operands()) {
888 divide(SE, *Quotient, Op, &Q, &R);
891 // Bail out when the Numerator is not divisible by one of the terms of
895 *Remainder = Numerator;
904 *Quotient = D.Quotient;
905 *Remainder = D.Remainder;
908 // Except in the trivial case described above, we do not know how to divide
909 // Expr by Denominator for the following functions with empty implementation.
910 void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {}
911 void visitZeroExtendExpr(const SCEVZeroExtendExpr *Numerator) {}
912 void visitSignExtendExpr(const SCEVSignExtendExpr *Numerator) {}
913 void visitUDivExpr(const SCEVUDivExpr *Numerator) {}
914 void visitSMaxExpr(const SCEVSMaxExpr *Numerator) {}
915 void visitUMaxExpr(const SCEVUMaxExpr *Numerator) {}
916 void visitUnknown(const SCEVUnknown *Numerator) {}
917 void visitCouldNotCompute(const SCEVCouldNotCompute *Numerator) {}
919 void visitConstant(const SCEVConstant *Numerator) {
920 if (const SCEVConstant *D = dyn_cast<SCEVConstant>(Denominator)) {
921 APInt NumeratorVal = Numerator->getAPInt();
922 APInt DenominatorVal = D->getAPInt();
923 uint32_t NumeratorBW = NumeratorVal.getBitWidth();
924 uint32_t DenominatorBW = DenominatorVal.getBitWidth();
926 if (NumeratorBW > DenominatorBW)
927 DenominatorVal = DenominatorVal.sext(NumeratorBW);
928 else if (NumeratorBW < DenominatorBW)
929 NumeratorVal = NumeratorVal.sext(DenominatorBW);
931 APInt QuotientVal(NumeratorVal.getBitWidth(), 0);
932 APInt RemainderVal(NumeratorVal.getBitWidth(), 0);
933 APInt::sdivrem(NumeratorVal, DenominatorVal, QuotientVal, RemainderVal);
934 Quotient = SE.getConstant(QuotientVal);
935 Remainder = SE.getConstant(RemainderVal);
940 void visitAddRecExpr(const SCEVAddRecExpr *Numerator) {
941 const SCEV *StartQ, *StartR, *StepQ, *StepR;
942 if (!Numerator->isAffine())
943 return cannotDivide(Numerator);
944 divide(SE, Numerator->getStart(), Denominator, &StartQ, &StartR);
945 divide(SE, Numerator->getStepRecurrence(SE), Denominator, &StepQ, &StepR);
946 // Bail out if the types do not match.
947 Type *Ty = Denominator->getType();
948 if (Ty != StartQ->getType() || Ty != StartR->getType() ||
949 Ty != StepQ->getType() || Ty != StepR->getType())
950 return cannotDivide(Numerator);
951 Quotient = SE.getAddRecExpr(StartQ, StepQ, Numerator->getLoop(),
952 Numerator->getNoWrapFlags());
953 Remainder = SE.getAddRecExpr(StartR, StepR, Numerator->getLoop(),
954 Numerator->getNoWrapFlags());
957 void visitAddExpr(const SCEVAddExpr *Numerator) {
958 SmallVector<const SCEV *, 2> Qs, Rs;
959 Type *Ty = Denominator->getType();
961 for (const SCEV *Op : Numerator->operands()) {
963 divide(SE, Op, Denominator, &Q, &R);
965 // Bail out if types do not match.
966 if (Ty != Q->getType() || Ty != R->getType())
967 return cannotDivide(Numerator);
973 if (Qs.size() == 1) {
979 Quotient = SE.getAddExpr(Qs);
980 Remainder = SE.getAddExpr(Rs);
983 void visitMulExpr(const SCEVMulExpr *Numerator) {
984 SmallVector<const SCEV *, 2> Qs;
985 Type *Ty = Denominator->getType();
987 bool FoundDenominatorTerm = false;
988 for (const SCEV *Op : Numerator->operands()) {
989 // Bail out if types do not match.
990 if (Ty != Op->getType())
991 return cannotDivide(Numerator);
993 if (FoundDenominatorTerm) {
998 // Check whether Denominator divides one of the product operands.
1000 divide(SE, Op, Denominator, &Q, &R);
1006 // Bail out if types do not match.
1007 if (Ty != Q->getType())
1008 return cannotDivide(Numerator);
1010 FoundDenominatorTerm = true;
1014 if (FoundDenominatorTerm) {
1019 Quotient = SE.getMulExpr(Qs);
1023 if (!isa<SCEVUnknown>(Denominator))
1024 return cannotDivide(Numerator);
1026 // The Remainder is obtained by replacing Denominator by 0 in Numerator.
1027 ValueToValueMap RewriteMap;
1028 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
1029 cast<SCEVConstant>(Zero)->getValue();
1030 Remainder = SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
1032 if (Remainder->isZero()) {
1033 // The Quotient is obtained by replacing Denominator by 1 in Numerator.
1034 RewriteMap[cast<SCEVUnknown>(Denominator)->getValue()] =
1035 cast<SCEVConstant>(One)->getValue();
1037 SCEVParameterRewriter::rewrite(Numerator, SE, RewriteMap, true);
1041 // Quotient is (Numerator - Remainder) divided by Denominator.
1043 const SCEV *Diff = SE.getMinusSCEV(Numerator, Remainder);
1044 // This SCEV does not seem to simplify: fail the division here.
1045 if (sizeOfSCEV(Diff) > sizeOfSCEV(Numerator))
1046 return cannotDivide(Numerator);
1047 divide(SE, Diff, Denominator, &Q, &R);
1049 return cannotDivide(Numerator);
1054 SCEVDivision(ScalarEvolution &S, const SCEV *Numerator,
1055 const SCEV *Denominator)
1056 : SE(S), Denominator(Denominator) {
1057 Zero = SE.getZero(Denominator->getType());
1058 One = SE.getOne(Denominator->getType());
1060 // We generally do not know how to divide Expr by Denominator. We
1061 // initialize the division to a "cannot divide" state to simplify the rest
1063 cannotDivide(Numerator);
1066 // Convenience function for giving up on the division. We set the quotient to
1067 // be equal to zero and the remainder to be equal to the numerator.
1068 void cannotDivide(const SCEV *Numerator) {
1070 Remainder = Numerator;
1073 ScalarEvolution &SE;
1074 const SCEV *Denominator, *Quotient, *Remainder, *Zero, *One;
1077 } // end anonymous namespace
1079 //===----------------------------------------------------------------------===//
1080 // Simple SCEV method implementations
1081 //===----------------------------------------------------------------------===//
1083 /// Compute BC(It, K). The result has width W. Assume, K > 0.
1084 static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
1085 ScalarEvolution &SE,
1087 // Handle the simplest case efficiently.
1089 return SE.getTruncateOrZeroExtend(It, ResultTy);
1091 // We are using the following formula for BC(It, K):
1093 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
1095 // Suppose, W is the bitwidth of the return value. We must be prepared for
1096 // overflow. Hence, we must assure that the result of our computation is
1097 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
1098 // safe in modular arithmetic.
1100 // However, this code doesn't use exactly that formula; the formula it uses
1101 // is something like the following, where T is the number of factors of 2 in
1102 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
1105 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
1107 // This formula is trivially equivalent to the previous formula. However,
1108 // this formula can be implemented much more efficiently. The trick is that
1109 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
1110 // arithmetic. To do exact division in modular arithmetic, all we have
1111 // to do is multiply by the inverse. Therefore, this step can be done at
1114 // The next issue is how to safely do the division by 2^T. The way this
1115 // is done is by doing the multiplication step at a width of at least W + T
1116 // bits. This way, the bottom W+T bits of the product are accurate. Then,
1117 // when we perform the division by 2^T (which is equivalent to a right shift
1118 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
1119 // truncated out after the division by 2^T.
1121 // In comparison to just directly using the first formula, this technique
1122 // is much more efficient; using the first formula requires W * K bits,
1123 // but this formula less than W + K bits. Also, the first formula requires
1124 // a division step, whereas this formula only requires multiplies and shifts.
1126 // It doesn't matter whether the subtraction step is done in the calculation
1127 // width or the input iteration count's width; if the subtraction overflows,
1128 // the result must be zero anyway. We prefer here to do it in the width of
1129 // the induction variable because it helps a lot for certain cases; CodeGen
1130 // isn't smart enough to ignore the overflow, which leads to much less
1131 // efficient code if the width of the subtraction is wider than the native
1134 // (It's possible to not widen at all by pulling out factors of 2 before
1135 // the multiplication; for example, K=2 can be calculated as
1136 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
1137 // extra arithmetic, so it's not an obvious win, and it gets
1138 // much more complicated for K > 3.)
1140 // Protection from insane SCEVs; this bound is conservative,
1141 // but it probably doesn't matter.
1143 return SE.getCouldNotCompute();
1145 unsigned W = SE.getTypeSizeInBits(ResultTy);
1147 // Calculate K! / 2^T and T; we divide out the factors of two before
1148 // multiplying for calculating K! / 2^T to avoid overflow.
1149 // Other overflow doesn't matter because we only care about the bottom
1150 // W bits of the result.
1151 APInt OddFactorial(W, 1);
1153 for (unsigned i = 3; i <= K; ++i) {
1155 unsigned TwoFactors = Mult.countTrailingZeros();
1157 Mult.lshrInPlace(TwoFactors);
1158 OddFactorial *= Mult;
1161 // We need at least W + T bits for the multiplication step
1162 unsigned CalculationBits = W + T;
1164 // Calculate 2^T, at width T+W.
1165 APInt DivFactor = APInt::getOneBitSet(CalculationBits, T);
1167 // Calculate the multiplicative inverse of K! / 2^T;
1168 // this multiplication factor will perform the exact division by
1170 APInt Mod = APInt::getSignedMinValue(W+1);
1171 APInt MultiplyFactor = OddFactorial.zext(W+1);
1172 MultiplyFactor = MultiplyFactor.multiplicativeInverse(Mod);
1173 MultiplyFactor = MultiplyFactor.trunc(W);
1175 // Calculate the product, at width T+W
1176 IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
1178 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
1179 for (unsigned i = 1; i != K; ++i) {
1180 const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
1181 Dividend = SE.getMulExpr(Dividend,
1182 SE.getTruncateOrZeroExtend(S, CalculationTy));
1186 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
1188 // Truncate the result, and divide by K! / 2^T.
1190 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
1191 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
1194 /// Return the value of this chain of recurrences at the specified iteration
1195 /// number. We can evaluate this recurrence by multiplying each element in the
1196 /// chain by the binomial coefficient corresponding to it. In other words, we
1197 /// can evaluate {A,+,B,+,C,+,D} as:
1199 /// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
1201 /// where BC(It, k) stands for binomial coefficient.
1202 const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
1203 ScalarEvolution &SE) const {
1204 const SCEV *Result = getStart();
1205 for (unsigned i = 1, e = getNumOperands(); i != e; ++i) {
1206 // The computation is correct in the face of overflow provided that the
1207 // multiplication is performed _after_ the evaluation of the binomial
1209 const SCEV *Coeff = BinomialCoefficient(It, i, SE, getType());
1210 if (isa<SCEVCouldNotCompute>(Coeff))
1213 Result = SE.getAddExpr(Result, SE.getMulExpr(getOperand(i), Coeff));
1218 //===----------------------------------------------------------------------===//
1219 // SCEV Expression folder implementations
1220 //===----------------------------------------------------------------------===//
1222 const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
1224 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
1225 "This is not a truncating conversion!");
1226 assert(isSCEVable(Ty) &&
1227 "This is not a conversion to a SCEVable type!");
1228 Ty = getEffectiveSCEVType(Ty);
1230 FoldingSetNodeID ID;
1231 ID.AddInteger(scTruncate);
1235 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1237 // Fold if the operand is constant.
1238 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1240 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
1242 // trunc(trunc(x)) --> trunc(x)
1243 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
1244 return getTruncateExpr(ST->getOperand(), Ty);
1246 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
1247 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1248 return getTruncateOrSignExtend(SS->getOperand(), Ty);
1250 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
1251 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1252 return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
1254 // trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and
1255 // trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN),
1256 // if after transforming we have at most one truncate, not counting truncates
1257 // that replace other casts.
1258 if (isa<SCEVAddExpr>(Op) || isa<SCEVMulExpr>(Op)) {
1259 auto *CommOp = cast<SCEVCommutativeExpr>(Op);
1260 SmallVector<const SCEV *, 4> Operands;
1261 unsigned numTruncs = 0;
1262 for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2;
1264 const SCEV *S = getTruncateExpr(CommOp->getOperand(i), Ty);
1265 if (!isa<SCEVCastExpr>(CommOp->getOperand(i)) && isa<SCEVTruncateExpr>(S))
1267 Operands.push_back(S);
1269 if (numTruncs < 2) {
1270 if (isa<SCEVAddExpr>(Op))
1271 return getAddExpr(Operands);
1272 else if (isa<SCEVMulExpr>(Op))
1273 return getMulExpr(Operands);
1275 llvm_unreachable("Unexpected SCEV type for Op.");
1277 // Although we checked in the beginning that ID is not in the cache, it is
1278 // possible that during recursion and different modification ID was inserted
1279 // into the cache. So if we find it, just return it.
1280 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1284 // If the input value is a chrec scev, truncate the chrec's operands.
1285 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
1286 SmallVector<const SCEV *, 4> Operands;
1287 for (const SCEV *Op : AddRec->operands())
1288 Operands.push_back(getTruncateExpr(Op, Ty));
1289 return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
1292 // The cast wasn't folded; create an explicit cast node. We can reuse
1293 // the existing insert position since if we get here, we won't have
1294 // made any changes which would invalidate it.
1295 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
1297 UniqueSCEVs.InsertNode(S, IP);
1298 addToLoopUseLists(S);
1302 // Get the limit of a recurrence such that incrementing by Step cannot cause
1303 // signed overflow as long as the value of the recurrence within the
1304 // loop does not exceed this limit before incrementing.
1305 static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
1306 ICmpInst::Predicate *Pred,
1307 ScalarEvolution *SE) {
1308 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1309 if (SE->isKnownPositive(Step)) {
1310 *Pred = ICmpInst::ICMP_SLT;
1311 return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
1312 SE->getSignedRangeMax(Step));
1314 if (SE->isKnownNegative(Step)) {
1315 *Pred = ICmpInst::ICMP_SGT;
1316 return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
1317 SE->getSignedRangeMin(Step));
1322 // Get the limit of a recurrence such that incrementing by Step cannot cause
1323 // unsigned overflow as long as the value of the recurrence within the loop does
1324 // not exceed this limit before incrementing.
1325 static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
1326 ICmpInst::Predicate *Pred,
1327 ScalarEvolution *SE) {
1328 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1329 *Pred = ICmpInst::ICMP_ULT;
1331 return SE->getConstant(APInt::getMinValue(BitWidth) -
1332 SE->getUnsignedRangeMax(Step));
1337 struct ExtendOpTraitsBase {
1338 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *,
1342 // Used to make code generic over signed and unsigned overflow.
1343 template <typename ExtendOp> struct ExtendOpTraits {
1346 // static const SCEV::NoWrapFlags WrapType;
1348 // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1350 // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1351 // ICmpInst::Predicate *Pred,
1352 // ScalarEvolution *SE);
1356 struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
1357 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
1359 static const GetExtendExprTy GetExtendExpr;
1361 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1362 ICmpInst::Predicate *Pred,
1363 ScalarEvolution *SE) {
1364 return getSignedOverflowLimitForStep(Step, Pred, SE);
1368 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1369 SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
1372 struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
1373 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
1375 static const GetExtendExprTy GetExtendExpr;
1377 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1378 ICmpInst::Predicate *Pred,
1379 ScalarEvolution *SE) {
1380 return getUnsignedOverflowLimitForStep(Step, Pred, SE);
1384 const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1385 SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
1387 } // end anonymous namespace
1389 // The recurrence AR has been shown to have no signed/unsigned wrap or something
1390 // close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1391 // easily prove NSW/NUW for its preincrement or postincrement sibling. This
1392 // allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1393 // Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1394 // expression "Step + sext/zext(PreIncAR)" is congruent with
1395 // "sext/zext(PostIncAR)"
1396 template <typename ExtendOpTy>
1397 static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty,
1398 ScalarEvolution *SE, unsigned Depth) {
1399 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1400 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1402 const Loop *L = AR->getLoop();
1403 const SCEV *Start = AR->getStart();
1404 const SCEV *Step = AR->getStepRecurrence(*SE);
1406 // Check for a simple looking step prior to loop entry.
1407 const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1411 // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1412 // subtraction is expensive. For this purpose, perform a quick and dirty
1413 // difference, by checking for Step in the operand list.
1414 SmallVector<const SCEV *, 4> DiffOps;
1415 for (const SCEV *Op : SA->operands())
1417 DiffOps.push_back(Op);
1419 if (DiffOps.size() == SA->getNumOperands())
1422 // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1425 // 1. NSW/NUW flags on the step increment.
1426 auto PreStartFlags =
1427 ScalarEvolution::maskFlags(SA->getNoWrapFlags(), SCEV::FlagNUW);
1428 const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags);
1429 const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1430 SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
1432 // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1433 // "S+X does not sign/unsign-overflow".
1436 const SCEV *BECount = SE->getBackedgeTakenCount(L);
1437 if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
1438 !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
1441 // 2. Direct overflow check on the step operation's expression.
1442 unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
1443 Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
1444 const SCEV *OperandExtendedStart =
1445 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth),
1446 (SE->*GetExtendExpr)(Step, WideTy, Depth));
1447 if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) {
1448 if (PreAR && AR->getNoWrapFlags(WrapType)) {
1449 // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1450 // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1451 // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact.
1452 const_cast<SCEVAddRecExpr *>(PreAR)->setNoWrapFlags(WrapType);
1457 // 3. Loop precondition.
1458 ICmpInst::Predicate Pred;
1459 const SCEV *OverflowLimit =
1460 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1462 if (OverflowLimit &&
1463 SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit))
1469 // Get the normalized zero or sign extended expression for this AddRec's Start.
1470 template <typename ExtendOpTy>
1471 static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
1472 ScalarEvolution *SE,
1474 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1476 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE, Depth);
1478 return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth);
1480 return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty,
1482 (SE->*GetExtendExpr)(PreStart, Ty, Depth));
1485 // Try to prove away overflow by looking at "nearby" add recurrences. A
1486 // motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1487 // does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1491 // {S,+,X} == {S-T,+,X} + T
1492 // => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1494 // If ({S-T,+,X} + T) does not overflow ... (1)
1496 // RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1498 // If {S-T,+,X} does not overflow ... (2)
1500 // RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1501 // == {Ext(S-T)+Ext(T),+,Ext(X)}
1503 // If (S-T)+T does not overflow ... (3)
1505 // RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1506 // == {Ext(S),+,Ext(X)} == LHS
1508 // Thus, if (1), (2) and (3) are true for some T, then
1509 // Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1511 // (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1512 // does not overflow" restricted to the 0th iteration. Therefore we only need
1513 // to check for (1) and (2).
1515 // In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1516 // is `Delta` (defined below).
1517 template <typename ExtendOpTy>
1518 bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
1521 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1523 // We restrict `Start` to a constant to prevent SCEV from spending too much
1524 // time here. It is correct (but more expensive) to continue with a
1525 // non-constant `Start` and do a general SCEV subtraction to compute
1526 // `PreStart` below.
1527 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
1531 APInt StartAI = StartC->getAPInt();
1533 for (unsigned Delta : {-2, -1, 1, 2}) {
1534 const SCEV *PreStart = getConstant(StartAI - Delta);
1536 FoldingSetNodeID ID;
1537 ID.AddInteger(scAddRecExpr);
1538 ID.AddPointer(PreStart);
1539 ID.AddPointer(Step);
1543 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1545 // Give up if we don't already have the add recurrence we need because
1546 // actually constructing an add recurrence is relatively expensive.
1547 if (PreAR && PreAR->getNoWrapFlags(WrapType)) { // proves (2)
1548 const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
1549 ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1550 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1551 DeltaS, &Pred, this);
1552 if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1)
1560 // Finds an integer D for an expression (C + x + y + ...) such that the top
1561 // level addition in (D + (C - D + x + y + ...)) would not wrap (signed or
1562 // unsigned) and the number of trailing zeros of (C - D + x + y + ...) is
1563 // maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and
1564 // the (C + x + y + ...) expression is \p WholeAddExpr.
1565 static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1566 const SCEVConstant *ConstantTerm,
1567 const SCEVAddExpr *WholeAddExpr) {
1568 const APInt C = ConstantTerm->getAPInt();
1569 const unsigned BitWidth = C.getBitWidth();
1570 // Find number of trailing zeros of (x + y + ...) w/o the C first:
1571 uint32_t TZ = BitWidth;
1572 for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I)
1573 TZ = std::min(TZ, SE.GetMinTrailingZeros(WholeAddExpr->getOperand(I)));
1575 // Set D to be as many least significant bits of C as possible while still
1576 // guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap:
1577 return TZ < BitWidth ? C.trunc(TZ).zext(BitWidth) : C;
1579 return APInt(BitWidth, 0);
1582 // Finds an integer D for an affine AddRec expression {C,+,x} such that the top
1583 // level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the
1584 // number of trailing zeros of (C - D + x * n) is maximized, where C is the \p
1585 // ConstantStart, x is an arbitrary \p Step, and n is the loop trip count.
1586 static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1587 const APInt &ConstantStart,
1589 const unsigned BitWidth = ConstantStart.getBitWidth();
1590 const uint32_t TZ = SE.GetMinTrailingZeros(Step);
1592 return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth)
1594 return APInt(BitWidth, 0);
1598 ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1599 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1600 "This is not an extending conversion!");
1601 assert(isSCEVable(Ty) &&
1602 "This is not a conversion to a SCEVable type!");
1603 Ty = getEffectiveSCEVType(Ty);
1605 // Fold if the operand is constant.
1606 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1608 cast<ConstantInt>(ConstantExpr::getZExt(SC->getValue(), Ty)));
1610 // zext(zext(x)) --> zext(x)
1611 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1612 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1614 // Before doing any expensive analysis, check to see if we've already
1615 // computed a SCEV for this Op and Ty.
1616 FoldingSetNodeID ID;
1617 ID.AddInteger(scZeroExtend);
1621 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1622 if (Depth > MaxExtDepth) {
1623 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1625 UniqueSCEVs.InsertNode(S, IP);
1626 addToLoopUseLists(S);
1630 // zext(trunc(x)) --> zext(x) or x or trunc(x)
1631 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1632 // It's possible the bits taken off by the truncate were all zero bits. If
1633 // so, we should be able to simplify this further.
1634 const SCEV *X = ST->getOperand();
1635 ConstantRange CR = getUnsignedRange(X);
1636 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1637 unsigned NewBits = getTypeSizeInBits(Ty);
1638 if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
1639 CR.zextOrTrunc(NewBits)))
1640 return getTruncateOrZeroExtend(X, Ty);
1643 // If the input value is a chrec scev, and we can prove that the value
1644 // did not overflow the old, smaller, value, we can zero extend all of the
1645 // operands (often constants). This allows analysis of something like
1646 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
1647 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1648 if (AR->isAffine()) {
1649 const SCEV *Start = AR->getStart();
1650 const SCEV *Step = AR->getStepRecurrence(*this);
1651 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1652 const Loop *L = AR->getLoop();
1654 if (!AR->hasNoUnsignedWrap()) {
1655 auto NewFlags = proveNoWrapViaConstantRanges(AR);
1656 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
1659 // If we have special knowledge that this addrec won't overflow,
1660 // we don't need to do any further analysis.
1661 if (AR->hasNoUnsignedWrap())
1662 return getAddRecExpr(
1663 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1664 getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1666 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1667 // Note that this serves two purposes: It filters out loops that are
1668 // simply not analyzable, and it covers the case where this code is
1669 // being called from within backedge-taken count analysis, such that
1670 // attempting to ask for the backedge-taken count would likely result
1671 // in infinite recursion. In the later case, the analysis code will
1672 // cope with a conservative value, and it will take care to purge
1673 // that value once it has finished.
1674 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
1675 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1676 // Manually compute the final value for AR, checking for
1679 // Check whether the backedge-taken count can be losslessly casted to
1680 // the addrec's type. The count is always unsigned.
1681 const SCEV *CastedMaxBECount =
1682 getTruncateOrZeroExtend(MaxBECount, Start->getType());
1683 const SCEV *RecastedMaxBECount =
1684 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
1685 if (MaxBECount == RecastedMaxBECount) {
1686 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1687 // Check whether Start+Step*MaxBECount has no unsigned overflow.
1688 const SCEV *ZMul = getMulExpr(CastedMaxBECount, Step,
1689 SCEV::FlagAnyWrap, Depth + 1);
1690 const SCEV *ZAdd = getZeroExtendExpr(getAddExpr(Start, ZMul,
1694 const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1);
1695 const SCEV *WideMaxBECount =
1696 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
1697 const SCEV *OperandExtendedAdd =
1698 getAddExpr(WideStart,
1699 getMulExpr(WideMaxBECount,
1700 getZeroExtendExpr(Step, WideTy, Depth + 1),
1701 SCEV::FlagAnyWrap, Depth + 1),
1702 SCEV::FlagAnyWrap, Depth + 1);
1703 if (ZAdd == OperandExtendedAdd) {
1704 // Cache knowledge of AR NUW, which is propagated to this AddRec.
1705 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1706 // Return the expression with the addrec on the outside.
1707 return getAddRecExpr(
1708 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1710 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1711 AR->getNoWrapFlags());
1713 // Similar to above, only this time treat the step value as signed.
1714 // This covers loops that count down.
1715 OperandExtendedAdd =
1716 getAddExpr(WideStart,
1717 getMulExpr(WideMaxBECount,
1718 getSignExtendExpr(Step, WideTy, Depth + 1),
1719 SCEV::FlagAnyWrap, Depth + 1),
1720 SCEV::FlagAnyWrap, Depth + 1);
1721 if (ZAdd == OperandExtendedAdd) {
1722 // Cache knowledge of AR NW, which is propagated to this AddRec.
1723 // Negative step causes unsigned wrap, but it still can't self-wrap.
1724 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1725 // Return the expression with the addrec on the outside.
1726 return getAddRecExpr(
1727 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1729 getSignExtendExpr(Step, Ty, Depth + 1), L,
1730 AR->getNoWrapFlags());
1735 // Normally, in the cases we can prove no-overflow via a
1736 // backedge guarding condition, we can also compute a backedge
1737 // taken count for the loop. The exceptions are assumptions and
1738 // guards present in the loop -- SCEV is not great at exploiting
1739 // these to compute max backedge taken counts, but can still use
1740 // these to prove lack of overflow. Use this fact to avoid
1741 // doing extra work that may not pay off.
1742 if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
1743 !AC.assumptions().empty()) {
1744 // If the backedge is guarded by a comparison with the pre-inc
1745 // value the addrec is safe. Also, if the entry is guarded by
1746 // a comparison with the start value and the backedge is
1747 // guarded by a comparison with the post-inc value, the addrec
1749 if (isKnownPositive(Step)) {
1750 const SCEV *N = getConstant(APInt::getMinValue(BitWidth) -
1751 getUnsignedRangeMax(Step));
1752 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_ULT, AR, N) ||
1753 isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR, N)) {
1754 // Cache knowledge of AR NUW, which is propagated to this
1756 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1757 // Return the expression with the addrec on the outside.
1758 return getAddRecExpr(
1759 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1761 getZeroExtendExpr(Step, Ty, Depth + 1), L,
1762 AR->getNoWrapFlags());
1764 } else if (isKnownNegative(Step)) {
1765 const SCEV *N = getConstant(APInt::getMaxValue(BitWidth) -
1766 getSignedRangeMin(Step));
1767 if (isLoopBackedgeGuardedByCond(L, ICmpInst::ICMP_UGT, AR, N) ||
1768 isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR, N)) {
1769 // Cache knowledge of AR NW, which is propagated to this
1770 // AddRec. Negative step causes unsigned wrap, but it
1771 // still can't self-wrap.
1772 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
1773 // Return the expression with the addrec on the outside.
1774 return getAddRecExpr(
1775 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this,
1777 getSignExtendExpr(Step, Ty, Depth + 1), L,
1778 AR->getNoWrapFlags());
1783 // zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step}))<nuw><nsw>
1784 // if D + (C - D + Step * n) could be proven to not unsigned wrap
1785 // where D maximizes the number of trailing zeros of (C - D + Step * n)
1786 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
1787 const APInt &C = SC->getAPInt();
1788 const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
1790 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1791 const SCEV *SResidual =
1792 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
1793 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1794 return getAddExpr(SZExtD, SZExtR,
1795 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1800 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1801 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNUW);
1802 return getAddRecExpr(
1803 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1),
1804 getZeroExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
1808 // zext(A % B) --> zext(A) % zext(B)
1812 if (matchURem(Op, LHS, RHS))
1813 return getURemExpr(getZeroExtendExpr(LHS, Ty, Depth + 1),
1814 getZeroExtendExpr(RHS, Ty, Depth + 1));
1817 // zext(A / B) --> zext(A) / zext(B).
1818 if (auto *Div = dyn_cast<SCEVUDivExpr>(Op))
1819 return getUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty, Depth + 1),
1820 getZeroExtendExpr(Div->getRHS(), Ty, Depth + 1));
1822 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1823 // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
1824 if (SA->hasNoUnsignedWrap()) {
1825 // If the addition does not unsign overflow then we can, by definition,
1826 // commute the zero extension with the addition operation.
1827 SmallVector<const SCEV *, 4> Ops;
1828 for (const auto *Op : SA->operands())
1829 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1830 return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1);
1833 // zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...))
1834 // if D + (C - D + x + y + ...) could be proven to not unsigned wrap
1835 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1837 // Often address arithmetics contain expressions like
1838 // (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))).
1839 // This transformation is useful while proving that such expressions are
1840 // equal or differ by a small constant amount, see LoadStoreVectorizer pass.
1841 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1842 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1844 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1845 const SCEV *SResidual =
1846 getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth);
1847 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1848 return getAddExpr(SZExtD, SZExtR,
1849 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1855 if (auto *SM = dyn_cast<SCEVMulExpr>(Op)) {
1856 // zext((A * B * ...)<nuw>) --> (zext(A) * zext(B) * ...)<nuw>
1857 if (SM->hasNoUnsignedWrap()) {
1858 // If the multiply does not unsign overflow then we can, by definition,
1859 // commute the zero extension with the multiply operation.
1860 SmallVector<const SCEV *, 4> Ops;
1861 for (const auto *Op : SM->operands())
1862 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1863 return getMulExpr(Ops, SCEV::FlagNUW, Depth + 1);
1866 // zext(2^K * (trunc X to iN)) to iM ->
1867 // 2^K * (zext(trunc X to i{N-K}) to iM)<nuw>
1871 // zext(2^K * (trunc X to iN)) to iM
1872 // = zext((trunc X to iN) << K) to iM
1873 // = zext((trunc X to i{N-K}) << K)<nuw> to iM
1874 // (because shl removes the top K bits)
1875 // = zext((2^K * (trunc X to i{N-K}))<nuw>) to iM
1876 // = (2^K * (zext(trunc X to i{N-K}) to iM))<nuw>.
1878 if (SM->getNumOperands() == 2)
1879 if (auto *MulLHS = dyn_cast<SCEVConstant>(SM->getOperand(0)))
1880 if (MulLHS->getAPInt().isPowerOf2())
1881 if (auto *TruncRHS = dyn_cast<SCEVTruncateExpr>(SM->getOperand(1))) {
1882 int NewTruncBits = getTypeSizeInBits(TruncRHS->getType()) -
1883 MulLHS->getAPInt().logBase2();
1884 Type *NewTruncTy = IntegerType::get(getContext(), NewTruncBits);
1886 getZeroExtendExpr(MulLHS, Ty),
1888 getTruncateExpr(TruncRHS->getOperand(), NewTruncTy), Ty),
1889 SCEV::FlagNUW, Depth + 1);
1893 // The cast wasn't folded; create an explicit cast node.
1894 // Recompute the insert position, as it may have been invalidated.
1895 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1896 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1898 UniqueSCEVs.InsertNode(S, IP);
1899 addToLoopUseLists(S);
1904 ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1905 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1906 "This is not an extending conversion!");
1907 assert(isSCEVable(Ty) &&
1908 "This is not a conversion to a SCEVable type!");
1909 Ty = getEffectiveSCEVType(Ty);
1911 // Fold if the operand is constant.
1912 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1914 cast<ConstantInt>(ConstantExpr::getSExt(SC->getValue(), Ty)));
1916 // sext(sext(x)) --> sext(x)
1917 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1918 return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1);
1920 // sext(zext(x)) --> zext(x)
1921 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1922 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1924 // Before doing any expensive analysis, check to see if we've already
1925 // computed a SCEV for this Op and Ty.
1926 FoldingSetNodeID ID;
1927 ID.AddInteger(scSignExtend);
1931 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
1932 // Limit recursion depth.
1933 if (Depth > MaxExtDepth) {
1934 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1936 UniqueSCEVs.InsertNode(S, IP);
1937 addToLoopUseLists(S);
1941 // sext(trunc(x)) --> sext(x) or x or trunc(x)
1942 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1943 // It's possible the bits taken off by the truncate were all sign bits. If
1944 // so, we should be able to simplify this further.
1945 const SCEV *X = ST->getOperand();
1946 ConstantRange CR = getSignedRange(X);
1947 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1948 unsigned NewBits = getTypeSizeInBits(Ty);
1949 if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1950 CR.sextOrTrunc(NewBits)))
1951 return getTruncateOrSignExtend(X, Ty);
1954 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1955 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
1956 if (SA->hasNoSignedWrap()) {
1957 // If the addition does not sign overflow then we can, by definition,
1958 // commute the sign extension with the addition operation.
1959 SmallVector<const SCEV *, 4> Ops;
1960 for (const auto *Op : SA->operands())
1961 Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1));
1962 return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1);
1965 // sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...))
1966 // if D + (C - D + x + y + ...) could be proven to not signed wrap
1967 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1969 // For instance, this will bring two seemingly different expressions:
1970 // 1 + sext(5 + 20 * %x + 24 * %y) and
1971 // sext(6 + 20 * %x + 24 * %y)
1972 // to the same form:
1973 // 2 + sext(4 + 20 * %x + 24 * %y)
1974 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1975 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1977 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
1978 const SCEV *SResidual =
1979 getAddExpr(getConstant(-D), SA, SCEV::FlagAnyWrap, Depth);
1980 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
1981 return getAddExpr(SSExtD, SSExtR,
1982 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1987 // If the input value is a chrec scev, and we can prove that the value
1988 // did not overflow the old, smaller, value, we can sign extend all of the
1989 // operands (often constants). This allows analysis of something like
1990 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
1991 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1992 if (AR->isAffine()) {
1993 const SCEV *Start = AR->getStart();
1994 const SCEV *Step = AR->getStepRecurrence(*this);
1995 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1996 const Loop *L = AR->getLoop();
1998 if (!AR->hasNoSignedWrap()) {
1999 auto NewFlags = proveNoWrapViaConstantRanges(AR);
2000 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(NewFlags);
2003 // If we have special knowledge that this addrec won't overflow,
2004 // we don't need to do any further analysis.
2005 if (AR->hasNoSignedWrap())
2006 return getAddRecExpr(
2007 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
2008 getSignExtendExpr(Step, Ty, Depth + 1), L, SCEV::FlagNSW);
2010 // Check whether the backedge-taken count is SCEVCouldNotCompute.
2011 // Note that this serves two purposes: It filters out loops that are
2012 // simply not analyzable, and it covers the case where this code is
2013 // being called from within backedge-taken count analysis, such that
2014 // attempting to ask for the backedge-taken count would likely result
2015 // in infinite recursion. In the later case, the analysis code will
2016 // cope with a conservative value, and it will take care to purge
2017 // that value once it has finished.
2018 const SCEV *MaxBECount = getMaxBackedgeTakenCount(L);
2019 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
2020 // Manually compute the final value for AR, checking for
2023 // Check whether the backedge-taken count can be losslessly casted to
2024 // the addrec's type. The count is always unsigned.
2025 const SCEV *CastedMaxBECount =
2026 getTruncateOrZeroExtend(MaxBECount, Start->getType());
2027 const SCEV *RecastedMaxBECount =
2028 getTruncateOrZeroExtend(CastedMaxBECount, MaxBECount->getType());
2029 if (MaxBECount == RecastedMaxBECount) {
2030 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
2031 // Check whether Start+Step*MaxBECount has no signed overflow.
2032 const SCEV *SMul = getMulExpr(CastedMaxBECount, Step,
2033 SCEV::FlagAnyWrap, Depth + 1);
2034 const SCEV *SAdd = getSignExtendExpr(getAddExpr(Start, SMul,
2038 const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1);
2039 const SCEV *WideMaxBECount =
2040 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
2041 const SCEV *OperandExtendedAdd =
2042 getAddExpr(WideStart,
2043 getMulExpr(WideMaxBECount,
2044 getSignExtendExpr(Step, WideTy, Depth + 1),
2045 SCEV::FlagAnyWrap, Depth + 1),
2046 SCEV::FlagAnyWrap, Depth + 1);
2047 if (SAdd == OperandExtendedAdd) {
2048 // Cache knowledge of AR NSW, which is propagated to this AddRec.
2049 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
2050 // Return the expression with the addrec on the outside.
2051 return getAddRecExpr(
2052 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
2054 getSignExtendExpr(Step, Ty, Depth + 1), L,
2055 AR->getNoWrapFlags());
2057 // Similar to above, only this time treat the step value as unsigned.
2058 // This covers loops that count up with an unsigned step.
2059 OperandExtendedAdd =
2060 getAddExpr(WideStart,
2061 getMulExpr(WideMaxBECount,
2062 getZeroExtendExpr(Step, WideTy, Depth + 1),
2063 SCEV::FlagAnyWrap, Depth + 1),
2064 SCEV::FlagAnyWrap, Depth + 1);
2065 if (SAdd == OperandExtendedAdd) {
2066 // If AR wraps around then
2068 // abs(Step) * MaxBECount > unsigned-max(AR->getType())
2069 // => SAdd != OperandExtendedAdd
2071 // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
2072 // (SAdd == OperandExtendedAdd => AR is NW)
2074 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
2076 // Return the expression with the addrec on the outside.
2077 return getAddRecExpr(
2078 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this,
2080 getZeroExtendExpr(Step, Ty, Depth + 1), L,
2081 AR->getNoWrapFlags());
2086 // Normally, in the cases we can prove no-overflow via a
2087 // backedge guarding condition, we can also compute a backedge
2088 // taken count for the loop. The exceptions are assumptions and
2089 // guards present in the loop -- SCEV is not great at exploiting
2090 // these to compute max backedge taken counts, but can still use
2091 // these to prove lack of overflow. Use this fact to avoid
2092 // doing extra work that may not pay off.
2094 if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
2095 !AC.assumptions().empty()) {
2096 // If the backedge is guarded by a comparison with the pre-inc
2097 // value the addrec is safe. Also, if the entry is guarded by
2098 // a comparison with the start value and the backedge is
2099 // guarded by a comparison with the post-inc value, the addrec
2101 ICmpInst::Predicate Pred;
2102 const SCEV *OverflowLimit =
2103 getSignedOverflowLimitForStep(Step, &Pred, this);
2104 if (OverflowLimit &&
2105 (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
2106 isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
2107 // Cache knowledge of AR NSW, then propagate NSW to the wide AddRec.
2108 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
2109 return getAddRecExpr(
2110 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
2111 getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
2115 // sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step}))<nuw><nsw>
2116 // if D + (C - D + Step * n) could be proven to not signed wrap
2117 // where D maximizes the number of trailing zeros of (C - D + Step * n)
2118 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
2119 const APInt &C = SC->getAPInt();
2120 const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
2122 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
2123 const SCEV *SResidual =
2124 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
2125 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
2126 return getAddExpr(SSExtD, SSExtR,
2127 (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
2132 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
2133 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNSW);
2134 return getAddRecExpr(
2135 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1),
2136 getSignExtendExpr(Step, Ty, Depth + 1), L, AR->getNoWrapFlags());
2140 // If the input value is provably positive and we could not simplify
2141 // away the sext build a zext instead.
2142 if (isKnownNonNegative(Op))
2143 return getZeroExtendExpr(Op, Ty, Depth + 1);
2145 // The cast wasn't folded; create an explicit cast node.
2146 // Recompute the insert position, as it may have been invalidated.
2147 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
2148 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
2150 UniqueSCEVs.InsertNode(S, IP);
2151 addToLoopUseLists(S);
2155 /// getAnyExtendExpr - Return a SCEV for the given operand extended with
2156 /// unspecified bits out to the given type.
2157 const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
2159 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
2160 "This is not an extending conversion!");
2161 assert(isSCEVable(Ty) &&
2162 "This is not a conversion to a SCEVable type!");
2163 Ty = getEffectiveSCEVType(Ty);
2165 // Sign-extend negative constants.
2166 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
2167 if (SC->getAPInt().isNegative())
2168 return getSignExtendExpr(Op, Ty);
2170 // Peel off a truncate cast.
2171 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
2172 const SCEV *NewOp = T->getOperand();
2173 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
2174 return getAnyExtendExpr(NewOp, Ty);
2175 return getTruncateOrNoop(NewOp, Ty);
2178 // Next try a zext cast. If the cast is folded, use it.
2179 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
2180 if (!isa<SCEVZeroExtendExpr>(ZExt))
2183 // Next try a sext cast. If the cast is folded, use it.
2184 const SCEV *SExt = getSignExtendExpr(Op, Ty);
2185 if (!isa<SCEVSignExtendExpr>(SExt))
2188 // Force the cast to be folded into the operands of an addrec.
2189 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
2190 SmallVector<const SCEV *, 4> Ops;
2191 for (const SCEV *Op : AR->operands())
2192 Ops.push_back(getAnyExtendExpr(Op, Ty));
2193 return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
2196 // If the expression is obviously signed, use the sext cast value.
2197 if (isa<SCEVSMaxExpr>(Op))
2200 // Absent any other information, use the zext cast value.
2204 /// Process the given Ops list, which is a list of operands to be added under
2205 /// the given scale, update the given map. This is a helper function for
2206 /// getAddRecExpr. As an example of what it does, given a sequence of operands
2207 /// that would form an add expression like this:
2209 /// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
2211 /// where A and B are constants, update the map with these values:
2213 /// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
2215 /// and add 13 + A*B*29 to AccumulatedConstant.
2216 /// This will allow getAddRecExpr to produce this:
2218 /// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
2220 /// This form often exposes folding opportunities that are hidden in
2221 /// the original operand list.
2223 /// Return true iff it appears that any interesting folding opportunities
2224 /// may be exposed. This helps getAddRecExpr short-circuit extra work in
2225 /// the common case where no interesting opportunities are present, and
2226 /// is also used as a check to avoid infinite recursion.
2228 CollectAddOperandsWithScales(DenseMap<const SCEV *, APInt> &M,
2229 SmallVectorImpl<const SCEV *> &NewOps,
2230 APInt &AccumulatedConstant,
2231 const SCEV *const *Ops, size_t NumOperands,
2233 ScalarEvolution &SE) {
2234 bool Interesting = false;
2236 // Iterate over the add operands. They are sorted, with constants first.
2238 while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2240 // Pull a buried constant out to the outside.
2241 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
2243 AccumulatedConstant += Scale * C->getAPInt();
2246 // Next comes everything else. We're especially interested in multiplies
2247 // here, but they're in the middle, so just visit the rest with one loop.
2248 for (; i != NumOperands; ++i) {
2249 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
2250 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
2252 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt();
2253 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
2254 // A multiplication of a constant with another add; recurse.
2255 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
2257 CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2258 Add->op_begin(), Add->getNumOperands(),
2261 // A multiplication of a constant with some other value. Update
2263 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin()+1, Mul->op_end());
2264 const SCEV *Key = SE.getMulExpr(MulOps);
2265 auto Pair = M.insert({Key, NewScale});
2267 NewOps.push_back(Pair.first->first);
2269 Pair.first->second += NewScale;
2270 // The map already had an entry for this value, which may indicate
2271 // a folding opportunity.
2276 // An ordinary operand. Update the map.
2277 std::pair<DenseMap<const SCEV *, APInt>::iterator, bool> Pair =
2278 M.insert({Ops[i], Scale});
2280 NewOps.push_back(Pair.first->first);
2282 Pair.first->second += Scale;
2283 // The map already had an entry for this value, which may indicate
2284 // a folding opportunity.
2293 // We're trying to construct a SCEV of type `Type' with `Ops' as operands and
2294 // `OldFlags' as can't-wrap behavior. Infer a more aggressive set of
2295 // can't-overflow flags for the operation if possible.
2296 static SCEV::NoWrapFlags
2297 StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type,
2298 const SmallVectorImpl<const SCEV *> &Ops,
2299 SCEV::NoWrapFlags Flags) {
2300 using namespace std::placeholders;
2302 using OBO = OverflowingBinaryOperator;
2305 Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr;
2307 assert(CanAnalyze && "don't call from other places!");
2309 int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
2310 SCEV::NoWrapFlags SignOrUnsignWrap =
2311 ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2313 // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
2314 auto IsKnownNonNegative = [&](const SCEV *S) {
2315 return SE->isKnownNonNegative(S);
2318 if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative))
2320 ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
2322 SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2324 if (SignOrUnsignWrap != SignOrUnsignMask &&
2325 (Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 &&
2326 isa<SCEVConstant>(Ops[0])) {
2331 return Instruction::Add;
2333 return Instruction::Mul;
2335 llvm_unreachable("Unexpected SCEV op.");
2339 const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt();
2341 // (A <opcode> C) --> (A <opcode> C)<nsw> if the op doesn't sign overflow.
2342 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
2343 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2344 Opcode, C, OBO::NoSignedWrap);
2345 if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
2346 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
2349 // (A <opcode> C) --> (A <opcode> C)<nuw> if the op doesn't unsign overflow.
2350 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
2351 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2352 Opcode, C, OBO::NoUnsignedWrap);
2353 if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
2354 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
2361 bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
2362 return isLoopInvariant(S, L) && properlyDominates(S, L->getHeader());
2365 /// Get a canonical add expression, or something simpler if possible.
2366 const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
2367 SCEV::NoWrapFlags Flags,
2369 assert(!(Flags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
2370 "only nuw or nsw allowed");
2371 assert(!Ops.empty() && "Cannot get empty add!");
2372 if (Ops.size() == 1) return Ops[0];
2374 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2375 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2376 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2377 "SCEVAddExpr operand types don't match!");
2380 // Sort by complexity, this groups all similar expression types together.
2381 GroupByComplexity(Ops, &LI, DT);
2383 Flags = StrengthenNoWrapFlags(this, scAddExpr, Ops, Flags);
2385 // If there are any constants, fold them together.
2387 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2389 assert(Idx < Ops.size());
2390 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2391 // We found two constants, fold them together!
2392 Ops[0] = getConstant(LHSC->getAPInt() + RHSC->getAPInt());
2393 if (Ops.size() == 2) return Ops[0];
2394 Ops.erase(Ops.begin()+1); // Erase the folded element
2395 LHSC = cast<SCEVConstant>(Ops[0]);
2398 // If we are left with a constant zero being added, strip it off.
2399 if (LHSC->getValue()->isZero()) {
2400 Ops.erase(Ops.begin());
2404 if (Ops.size() == 1) return Ops[0];
2407 // Limit recursion calls depth.
2408 if (Depth > MaxArithDepth)
2409 return getOrCreateAddExpr(Ops, Flags);
2411 // Okay, check to see if the same value occurs in the operand list more than
2412 // once. If so, merge them together into an multiply expression. Since we
2413 // sorted the list, these values are required to be adjacent.
2414 Type *Ty = Ops[0]->getType();
2415 bool FoundMatch = false;
2416 for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
2417 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
2418 // Scan ahead to count how many equal operands there are.
2420 while (i+Count != e && Ops[i+Count] == Ops[i])
2422 // Merge the values into a multiply.
2423 const SCEV *Scale = getConstant(Ty, Count);
2424 const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1);
2425 if (Ops.size() == Count)
2428 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
2429 --i; e -= Count - 1;
2433 return getAddExpr(Ops, Flags, Depth + 1);
2435 // Check for truncates. If all the operands are truncated from the same
2436 // type, see if factoring out the truncate would permit the result to be
2437 // folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y)
2438 // if the contents of the resulting outer trunc fold to something simple.
2439 auto FindTruncSrcType = [&]() -> Type * {
2440 // We're ultimately looking to fold an addrec of truncs and muls of only
2441 // constants and truncs, so if we find any other types of SCEV
2442 // as operands of the addrec then we bail and return nullptr here.
2443 // Otherwise, we return the type of the operand of a trunc that we find.
2444 if (auto *T = dyn_cast<SCEVTruncateExpr>(Ops[Idx]))
2445 return T->getOperand()->getType();
2446 if (const auto *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2447 const auto *LastOp = Mul->getOperand(Mul->getNumOperands() - 1);
2448 if (const auto *T = dyn_cast<SCEVTruncateExpr>(LastOp))
2449 return T->getOperand()->getType();
2453 if (auto *SrcType = FindTruncSrcType()) {
2454 SmallVector<const SCEV *, 8> LargeOps;
2456 // Check all the operands to see if they can be represented in the
2457 // source type of the truncate.
2458 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
2459 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Ops[i])) {
2460 if (T->getOperand()->getType() != SrcType) {
2464 LargeOps.push_back(T->getOperand());
2465 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2466 LargeOps.push_back(getAnyExtendExpr(C, SrcType));
2467 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Ops[i])) {
2468 SmallVector<const SCEV *, 8> LargeMulOps;
2469 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2470 if (const SCEVTruncateExpr *T =
2471 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2472 if (T->getOperand()->getType() != SrcType) {
2476 LargeMulOps.push_back(T->getOperand());
2477 } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) {
2478 LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
2485 LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1));
2492 // Evaluate the expression in the larger type.
2493 const SCEV *Fold = getAddExpr(LargeOps, SCEV::FlagAnyWrap, Depth + 1);
2494 // If it folds to something simple, use it. Otherwise, don't.
2495 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
2496 return getTruncateExpr(Fold, Ty);
2500 // Skip past any other cast SCEVs.
2501 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
2504 // If there are add operands they would be next.
2505 if (Idx < Ops.size()) {
2506 bool DeletedAdd = false;
2507 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
2508 if (Ops.size() > AddOpsInlineThreshold ||
2509 Add->getNumOperands() > AddOpsInlineThreshold)
2511 // If we have an add, expand the add operands onto the end of the operands
2513 Ops.erase(Ops.begin()+Idx);
2514 Ops.append(Add->op_begin(), Add->op_end());
2518 // If we deleted at least one add, we added operands to the end of the list,
2519 // and they are not necessarily sorted. Recurse to resort and resimplify
2520 // any operands we just acquired.
2522 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2525 // Skip over the add expression until we get to a multiply.
2526 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2529 // Check to see if there are any folding opportunities present with
2530 // operands multiplied by constant values.
2531 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
2532 uint64_t BitWidth = getTypeSizeInBits(Ty);
2533 DenseMap<const SCEV *, APInt> M;
2534 SmallVector<const SCEV *, 8> NewOps;
2535 APInt AccumulatedConstant(BitWidth, 0);
2536 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2537 Ops.data(), Ops.size(),
2538 APInt(BitWidth, 1), *this)) {
2539 struct APIntCompare {
2540 bool operator()(const APInt &LHS, const APInt &RHS) const {
2541 return LHS.ult(RHS);
2545 // Some interesting folding opportunity is present, so its worthwhile to
2546 // re-generate the operands list. Group the operands by constant scale,
2547 // to avoid multiplying by the same constant scale multiple times.
2548 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
2549 for (const SCEV *NewOp : NewOps)
2550 MulOpLists[M.find(NewOp)->second].push_back(NewOp);
2551 // Re-generate the operands list.
2553 if (AccumulatedConstant != 0)
2554 Ops.push_back(getConstant(AccumulatedConstant));
2555 for (auto &MulOp : MulOpLists)
2556 if (MulOp.first != 0)
2557 Ops.push_back(getMulExpr(
2558 getConstant(MulOp.first),
2559 getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1),
2560 SCEV::FlagAnyWrap, Depth + 1));
2563 if (Ops.size() == 1)
2565 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2569 // If we are adding something to a multiply expression, make sure the
2570 // something is not already an operand of the multiply. If so, merge it into
2572 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
2573 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
2574 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
2575 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
2576 if (isa<SCEVConstant>(MulOpSCEV))
2578 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
2579 if (MulOpSCEV == Ops[AddOp]) {
2580 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
2581 const SCEV *InnerMul = Mul->getOperand(MulOp == 0);
2582 if (Mul->getNumOperands() != 2) {
2583 // If the multiply has more than two operands, we must get the
2585 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2586 Mul->op_begin()+MulOp);
2587 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2588 InnerMul = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2590 SmallVector<const SCEV *, 2> TwoOps = {getOne(Ty), InnerMul};
2591 const SCEV *AddOne = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2592 const SCEV *OuterMul = getMulExpr(AddOne, MulOpSCEV,
2593 SCEV::FlagAnyWrap, Depth + 1);
2594 if (Ops.size() == 2) return OuterMul;
2596 Ops.erase(Ops.begin()+AddOp);
2597 Ops.erase(Ops.begin()+Idx-1);
2599 Ops.erase(Ops.begin()+Idx);
2600 Ops.erase(Ops.begin()+AddOp-1);
2602 Ops.push_back(OuterMul);
2603 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2606 // Check this multiply against other multiplies being added together.
2607 for (unsigned OtherMulIdx = Idx+1;
2608 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
2610 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
2611 // If MulOp occurs in OtherMul, we can fold the two multiplies
2613 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
2614 OMulOp != e; ++OMulOp)
2615 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
2616 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
2617 const SCEV *InnerMul1 = Mul->getOperand(MulOp == 0);
2618 if (Mul->getNumOperands() != 2) {
2619 SmallVector<const SCEV *, 4> MulOps(Mul->op_begin(),
2620 Mul->op_begin()+MulOp);
2621 MulOps.append(Mul->op_begin()+MulOp+1, Mul->op_end());
2622 InnerMul1 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2624 const SCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
2625 if (OtherMul->getNumOperands() != 2) {
2626 SmallVector<const SCEV *, 4> MulOps(OtherMul->op_begin(),
2627 OtherMul->op_begin()+OMulOp);
2628 MulOps.append(OtherMul->op_begin()+OMulOp+1, OtherMul->op_end());
2629 InnerMul2 = getMulExpr(MulOps, SCEV::FlagAnyWrap, Depth + 1);
2631 SmallVector<const SCEV *, 2> TwoOps = {InnerMul1, InnerMul2};
2632 const SCEV *InnerMulSum =
2633 getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2634 const SCEV *OuterMul = getMulExpr(MulOpSCEV, InnerMulSum,
2635 SCEV::FlagAnyWrap, Depth + 1);
2636 if (Ops.size() == 2) return OuterMul;
2637 Ops.erase(Ops.begin()+Idx);
2638 Ops.erase(Ops.begin()+OtherMulIdx-1);
2639 Ops.push_back(OuterMul);
2640 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2646 // If there are any add recurrences in the operands list, see if any other
2647 // added values are loop invariant. If so, we can fold them into the
2649 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2652 // Scan over all recurrences, trying to fold loop invariants into them.
2653 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2654 // Scan all of the other operands to this add and add them to the vector if
2655 // they are loop invariant w.r.t. the recurrence.
2656 SmallVector<const SCEV *, 8> LIOps;
2657 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2658 const Loop *AddRecLoop = AddRec->getLoop();
2659 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2660 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
2661 LIOps.push_back(Ops[i]);
2662 Ops.erase(Ops.begin()+i);
2666 // If we found some loop invariants, fold them into the recurrence.
2667 if (!LIOps.empty()) {
2668 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
2669 LIOps.push_back(AddRec->getStart());
2671 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
2673 // This follows from the fact that the no-wrap flags on the outer add
2674 // expression are applicable on the 0th iteration, when the add recurrence
2675 // will be equal to its start value.
2676 AddRecOps[0] = getAddExpr(LIOps, Flags, Depth + 1);
2678 // Build the new addrec. Propagate the NUW and NSW flags if both the
2679 // outer add and the inner addrec are guaranteed to have no overflow.
2680 // Always propagate NW.
2681 Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
2682 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
2684 // If all of the other operands were loop invariant, we are done.
2685 if (Ops.size() == 1) return NewRec;
2687 // Otherwise, add the folded AddRec by the non-invariant parts.
2688 for (unsigned i = 0;; ++i)
2689 if (Ops[i] == AddRec) {
2693 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2696 // Okay, if there weren't any loop invariants to be folded, check to see if
2697 // there are multiple AddRec's with the same loop induction variable being
2698 // added together. If so, we can fold them.
2699 for (unsigned OtherIdx = Idx+1;
2700 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2702 // We expect the AddRecExpr's to be sorted in reverse dominance order,
2703 // so that the 1st found AddRecExpr is dominated by all others.
2704 assert(DT.dominates(
2705 cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),
2706 AddRec->getLoop()->getHeader()) &&
2707 "AddRecExprs are not sorted in reverse dominance order?");
2708 if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2709 // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L>
2710 SmallVector<const SCEV *, 4> AddRecOps(AddRec->op_begin(),
2712 for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2714 const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2715 if (OtherAddRec->getLoop() == AddRecLoop) {
2716 for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2718 if (i >= AddRecOps.size()) {
2719 AddRecOps.append(OtherAddRec->op_begin()+i,
2720 OtherAddRec->op_end());
2723 SmallVector<const SCEV *, 2> TwoOps = {
2724 AddRecOps[i], OtherAddRec->getOperand(i)};
2725 AddRecOps[i] = getAddExpr(TwoOps, SCEV::FlagAnyWrap, Depth + 1);
2727 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2730 // Step size has changed, so we cannot guarantee no self-wraparound.
2731 Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
2732 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2736 // Otherwise couldn't fold anything into this recurrence. Move onto the
2740 // Okay, it looks like we really DO need an add expr. Check to see if we
2741 // already have one, otherwise create a new one.
2742 return getOrCreateAddExpr(Ops, Flags);
2746 ScalarEvolution::getOrCreateAddExpr(SmallVectorImpl<const SCEV *> &Ops,
2747 SCEV::NoWrapFlags Flags) {
2748 FoldingSetNodeID ID;
2749 ID.AddInteger(scAddExpr);
2750 for (const SCEV *Op : Ops)
2754 static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2756 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2757 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2758 S = new (SCEVAllocator)
2759 SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
2760 UniqueSCEVs.InsertNode(S, IP);
2761 addToLoopUseLists(S);
2763 S->setNoWrapFlags(Flags);
2768 ScalarEvolution::getOrCreateAddRecExpr(SmallVectorImpl<const SCEV *> &Ops,
2769 const Loop *L, SCEV::NoWrapFlags Flags) {
2770 FoldingSetNodeID ID;
2771 ID.AddInteger(scAddRecExpr);
2772 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2773 ID.AddPointer(Ops[i]);
2777 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2779 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2780 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2781 S = new (SCEVAllocator)
2782 SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L);
2783 UniqueSCEVs.InsertNode(S, IP);
2784 addToLoopUseLists(S);
2786 S->setNoWrapFlags(Flags);
2791 ScalarEvolution::getOrCreateMulExpr(SmallVectorImpl<const SCEV *> &Ops,
2792 SCEV::NoWrapFlags Flags) {
2793 FoldingSetNodeID ID;
2794 ID.AddInteger(scMulExpr);
2795 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2796 ID.AddPointer(Ops[i]);
2799 static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2801 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
2802 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2803 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
2805 UniqueSCEVs.InsertNode(S, IP);
2806 addToLoopUseLists(S);
2808 S->setNoWrapFlags(Flags);
2812 static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
2814 if (j > 1 && k / j != i) Overflow = true;
2818 /// Compute the result of "n choose k", the binomial coefficient. If an
2819 /// intermediate computation overflows, Overflow will be set and the return will
2820 /// be garbage. Overflow is not cleared on absence of overflow.
2821 static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
2822 // We use the multiplicative formula:
2823 // n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
2824 // At each iteration, we take the n-th term of the numeral and divide by the
2825 // (k-n)th term of the denominator. This division will always produce an
2826 // integral result, and helps reduce the chance of overflow in the
2827 // intermediate computations. However, we can still overflow even when the
2828 // final result would fit.
2830 if (n == 0 || n == k) return 1;
2831 if (k > n) return 0;
2837 for (uint64_t i = 1; i <= k; ++i) {
2838 r = umul_ov(r, n-(i-1), Overflow);
2844 /// Determine if any of the operands in this SCEV are a constant or if
2845 /// any of the add or multiply expressions in this SCEV contain a constant.
2846 static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
2847 struct FindConstantInAddMulChain {
2848 bool FoundConstant = false;
2850 bool follow(const SCEV *S) {
2851 FoundConstant |= isa<SCEVConstant>(S);
2852 return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S);
2855 bool isDone() const {
2856 return FoundConstant;
2860 FindConstantInAddMulChain F;
2861 SCEVTraversal<FindConstantInAddMulChain> ST(F);
2862 ST.visitAll(StartExpr);
2863 return F.FoundConstant;
2866 /// Get a canonical multiply expression, or something simpler if possible.
2867 const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
2868 SCEV::NoWrapFlags Flags,
2870 assert(Flags == maskFlags(Flags, SCEV::FlagNUW | SCEV::FlagNSW) &&
2871 "only nuw or nsw allowed");
2872 assert(!Ops.empty() && "Cannot get empty mul!");
2873 if (Ops.size() == 1) return Ops[0];
2875 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2876 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2877 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2878 "SCEVMulExpr operand types don't match!");
2881 // Sort by complexity, this groups all similar expression types together.
2882 GroupByComplexity(Ops, &LI, DT);
2884 Flags = StrengthenNoWrapFlags(this, scMulExpr, Ops, Flags);
2886 // Limit recursion calls depth.
2887 if (Depth > MaxArithDepth)
2888 return getOrCreateMulExpr(Ops, Flags);
2890 // If there are any constants, fold them together.
2892 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
2894 if (Ops.size() == 2)
2895 // C1*(C2+V) -> C1*C2 + C1*V
2896 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
2897 // If any of Add's ops are Adds or Muls with a constant, apply this
2898 // transformation as well.
2900 // TODO: There are some cases where this transformation is not
2901 // profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of
2902 // this transformation should be narrowed down.
2903 if (Add->getNumOperands() == 2 && containsConstantInAddMulChain(Add))
2904 return getAddExpr(getMulExpr(LHSC, Add->getOperand(0),
2905 SCEV::FlagAnyWrap, Depth + 1),
2906 getMulExpr(LHSC, Add->getOperand(1),
2907 SCEV::FlagAnyWrap, Depth + 1),
2908 SCEV::FlagAnyWrap, Depth + 1);
2911 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
2912 // We found two constants, fold them together!
2914 ConstantInt::get(getContext(), LHSC->getAPInt() * RHSC->getAPInt());
2915 Ops[0] = getConstant(Fold);
2916 Ops.erase(Ops.begin()+1); // Erase the folded element
2917 if (Ops.size() == 1) return Ops[0];
2918 LHSC = cast<SCEVConstant>(Ops[0]);
2921 // If we are left with a constant one being multiplied, strip it off.
2922 if (cast<SCEVConstant>(Ops[0])->getValue()->isOne()) {
2923 Ops.erase(Ops.begin());
2925 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isZero()) {
2926 // If we have a multiply of zero, it will always be zero.
2928 } else if (Ops[0]->isAllOnesValue()) {
2929 // If we have a mul by -1 of an add, try distributing the -1 among the
2931 if (Ops.size() == 2) {
2932 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
2933 SmallVector<const SCEV *, 4> NewOps;
2934 bool AnyFolded = false;
2935 for (const SCEV *AddOp : Add->operands()) {
2936 const SCEV *Mul = getMulExpr(Ops[0], AddOp, SCEV::FlagAnyWrap,
2938 if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
2939 NewOps.push_back(Mul);
2942 return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1);
2943 } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
2944 // Negation preserves a recurrence's no self-wrap property.
2945 SmallVector<const SCEV *, 4> Operands;
2946 for (const SCEV *AddRecOp : AddRec->operands())
2947 Operands.push_back(getMulExpr(Ops[0], AddRecOp, SCEV::FlagAnyWrap,
2950 return getAddRecExpr(Operands, AddRec->getLoop(),
2951 AddRec->getNoWrapFlags(SCEV::FlagNW));
2956 if (Ops.size() == 1)
2960 // Skip over the add expression until we get to a multiply.
2961 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2964 // If there are mul operands inline them all into this expression.
2965 if (Idx < Ops.size()) {
2966 bool DeletedMul = false;
2967 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2968 if (Ops.size() > MulOpsInlineThreshold)
2970 // If we have an mul, expand the mul operands onto the end of the
2972 Ops.erase(Ops.begin()+Idx);
2973 Ops.append(Mul->op_begin(), Mul->op_end());
2977 // If we deleted at least one mul, we added operands to the end of the
2978 // list, and they are not necessarily sorted. Recurse to resort and
2979 // resimplify any operands we just acquired.
2981 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2984 // If there are any add recurrences in the operands list, see if any other
2985 // added values are loop invariant. If so, we can fold them into the
2987 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2990 // Scan over all recurrences, trying to fold loop invariants into them.
2991 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2992 // Scan all of the other operands to this mul and add them to the vector
2993 // if they are loop invariant w.r.t. the recurrence.
2994 SmallVector<const SCEV *, 8> LIOps;
2995 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2996 const Loop *AddRecLoop = AddRec->getLoop();
2997 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2998 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
2999 LIOps.push_back(Ops[i]);
3000 Ops.erase(Ops.begin()+i);
3004 // If we found some loop invariants, fold them into the recurrence.
3005 if (!LIOps.empty()) {
3006 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
3007 SmallVector<const SCEV *, 4> NewOps;
3008 NewOps.reserve(AddRec->getNumOperands());
3009 const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1);
3010 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i)
3011 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i),
3012 SCEV::FlagAnyWrap, Depth + 1));
3014 // Build the new addrec. Propagate the NUW and NSW flags if both the
3015 // outer mul and the inner addrec are guaranteed to have no overflow.
3017 // No self-wrap cannot be guaranteed after changing the step size, but
3018 // will be inferred if either NUW or NSW is true.
3019 Flags = AddRec->getNoWrapFlags(clearFlags(Flags, SCEV::FlagNW));
3020 const SCEV *NewRec = getAddRecExpr(NewOps, AddRecLoop, Flags);
3022 // If all of the other operands were loop invariant, we are done.
3023 if (Ops.size() == 1) return NewRec;
3025 // Otherwise, multiply the folded AddRec by the non-invariant parts.
3026 for (unsigned i = 0;; ++i)
3027 if (Ops[i] == AddRec) {
3031 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3034 // Okay, if there weren't any loop invariants to be folded, check to see
3035 // if there are multiple AddRec's with the same loop induction variable
3036 // being multiplied together. If so, we can fold them.
3038 // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
3039 // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
3040 // choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
3041 // ]]],+,...up to x=2n}.
3042 // Note that the arguments to choose() are always integers with values
3043 // known at compile time, never SCEV objects.
3045 // The implementation avoids pointless extra computations when the two
3046 // addrec's are of different length (mathematically, it's equivalent to
3047 // an infinite stream of zeros on the right).
3048 bool OpsModified = false;
3049 for (unsigned OtherIdx = Idx+1;
3050 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
3052 const SCEVAddRecExpr *OtherAddRec =
3053 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
3054 if (!OtherAddRec || OtherAddRec->getLoop() != AddRecLoop)
3057 // Limit max number of arguments to avoid creation of unreasonably big
3058 // SCEVAddRecs with very complex operands.
3059 if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
3063 bool Overflow = false;
3064 Type *Ty = AddRec->getType();
3065 bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
3066 SmallVector<const SCEV*, 7> AddRecOps;
3067 for (int x = 0, xe = AddRec->getNumOperands() +
3068 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
3069 SmallVector <const SCEV *, 7> SumOps;
3070 for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
3071 uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
3072 for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
3073 ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
3074 z < ze && !Overflow; ++z) {
3075 uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
3077 if (LargerThan64Bits)
3078 Coeff = umul_ov(Coeff1, Coeff2, Overflow);
3080 Coeff = Coeff1*Coeff2;
3081 const SCEV *CoeffTerm = getConstant(Ty, Coeff);
3082 const SCEV *Term1 = AddRec->getOperand(y-z);
3083 const SCEV *Term2 = OtherAddRec->getOperand(z);
3084 SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2,
3085 SCEV::FlagAnyWrap, Depth + 1));
3089 SumOps.push_back(getZero(Ty));
3090 AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1));
3093 const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(),
3095 if (Ops.size() == 2) return NewAddRec;
3096 Ops[Idx] = NewAddRec;
3097 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
3099 AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
3105 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3107 // Otherwise couldn't fold anything into this recurrence. Move onto the
3111 // Okay, it looks like we really DO need an mul expr. Check to see if we
3112 // already have one, otherwise create a new one.
3113 return getOrCreateMulExpr(Ops, Flags);
3116 /// Represents an unsigned remainder expression based on unsigned division.
3117 const SCEV *ScalarEvolution::getURemExpr(const SCEV *LHS,
3119 assert(getEffectiveSCEVType(LHS->getType()) ==
3120 getEffectiveSCEVType(RHS->getType()) &&
3121 "SCEVURemExpr operand types don't match!");
3123 // Short-circuit easy cases
3124 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3125 // If constant is one, the result is trivial
3126 if (RHSC->getValue()->isOne())
3127 return getZero(LHS->getType()); // X urem 1 --> 0
3129 // If constant is a power of two, fold into a zext(trunc(LHS)).
3130 if (RHSC->getAPInt().isPowerOf2()) {
3131 Type *FullTy = LHS->getType();
3133 IntegerType::get(getContext(), RHSC->getAPInt().logBase2());
3134 return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy);
3138 // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
3139 const SCEV *UDiv = getUDivExpr(LHS, RHS);
3140 const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW);
3141 return getMinusSCEV(LHS, Mult, SCEV::FlagNUW);
3144 /// Get a canonical unsigned division expression, or something simpler if
3146 const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
3148 assert(getEffectiveSCEVType(LHS->getType()) ==
3149 getEffectiveSCEVType(RHS->getType()) &&
3150 "SCEVUDivExpr operand types don't match!");
3152 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3153 if (RHSC->getValue()->isOne())
3154 return LHS; // X udiv 1 --> x
3155 // If the denominator is zero, the result of the udiv is undefined. Don't
3156 // try to analyze it, because the resolution chosen here may differ from
3157 // the resolution chosen in other parts of the compiler.
3158 if (!RHSC->getValue()->isZero()) {
3159 // Determine if the division can be folded into the operands of
3161 // TODO: Generalize this to non-constants by using known-bits information.
3162 Type *Ty = LHS->getType();
3163 unsigned LZ = RHSC->getAPInt().countLeadingZeros();
3164 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
3165 // For non-power-of-two values, effectively round the value up to the
3166 // nearest power of two.
3167 if (!RHSC->getAPInt().isPowerOf2())
3169 IntegerType *ExtTy =
3170 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
3171 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
3172 if (const SCEVConstant *Step =
3173 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
3174 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
3175 const APInt &StepInt = Step->getAPInt();
3176 const APInt &DivInt = RHSC->getAPInt();
3177 if (!StepInt.urem(DivInt) &&
3178 getZeroExtendExpr(AR, ExtTy) ==
3179 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3180 getZeroExtendExpr(Step, ExtTy),
3181 AR->getLoop(), SCEV::FlagAnyWrap)) {
3182 SmallVector<const SCEV *, 4> Operands;
3183 for (const SCEV *Op : AR->operands())
3184 Operands.push_back(getUDivExpr(Op, RHS));
3185 return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW);
3187 /// Get a canonical UDivExpr for a recurrence.
3188 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
3189 // We can currently only fold X%N if X is constant.
3190 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
3191 if (StartC && !DivInt.urem(StepInt) &&
3192 getZeroExtendExpr(AR, ExtTy) ==
3193 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3194 getZeroExtendExpr(Step, ExtTy),
3195 AR->getLoop(), SCEV::FlagAnyWrap)) {
3196 const APInt &StartInt = StartC->getAPInt();
3197 const APInt &StartRem = StartInt.urem(StepInt);
3199 LHS = getAddRecExpr(getConstant(StartInt - StartRem), Step,
3200 AR->getLoop(), SCEV::FlagNW);
3203 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
3204 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
3205 SmallVector<const SCEV *, 4> Operands;
3206 for (const SCEV *Op : M->operands())
3207 Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3208 if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
3209 // Find an operand that's safely divisible.
3210 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
3211 const SCEV *Op = M->getOperand(i);
3212 const SCEV *Div = getUDivExpr(Op, RHSC);
3213 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
3214 Operands = SmallVector<const SCEV *, 4>(M->op_begin(),
3217 return getMulExpr(Operands);
3222 // (A/B)/C --> A/(B*C) if safe and B*C can be folded.
3223 if (const SCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(LHS)) {
3224 if (auto *DivisorConstant =
3225 dyn_cast<SCEVConstant>(OtherDiv->getRHS())) {
3226 bool Overflow = false;
3228 DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
3230 return getConstant(RHSC->getType(), 0, false);
3232 return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS));
3236 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
3237 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
3238 SmallVector<const SCEV *, 4> Operands;
3239 for (const SCEV *Op : A->operands())
3240 Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3241 if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
3243 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
3244 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
3245 if (isa<SCEVUDivExpr>(Op) ||
3246 getMulExpr(Op, RHS) != A->getOperand(i))
3248 Operands.push_back(Op);
3250 if (Operands.size() == A->getNumOperands())
3251 return getAddExpr(Operands);
3255 // Fold if both operands are constant.
3256 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
3257 Constant *LHSCV = LHSC->getValue();
3258 Constant *RHSCV = RHSC->getValue();
3259 return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
3265 FoldingSetNodeID ID;
3266 ID.AddInteger(scUDivExpr);
3270 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
3271 SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator),
3273 UniqueSCEVs.InsertNode(S, IP);
3274 addToLoopUseLists(S);
3278 static const APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) {
3279 APInt A = C1->getAPInt().abs();
3280 APInt B = C2->getAPInt().abs();
3281 uint32_t ABW = A.getBitWidth();
3282 uint32_t BBW = B.getBitWidth();
3289 return APIntOps::GreatestCommonDivisor(std::move(A), std::move(B));
3292 /// Get a canonical unsigned division expression, or something simpler if
3293 /// possible. There is no representation for an exact udiv in SCEV IR, but we
3294 /// can attempt to remove factors from the LHS and RHS. We can't do this when
3295 /// it's not exact because the udiv may be clearing bits.
3296 const SCEV *ScalarEvolution::getUDivExactExpr(const SCEV *LHS,
3298 // TODO: we could try to find factors in all sorts of things, but for now we
3299 // just deal with u/exact (multiply, constant). See SCEVDivision towards the
3300 // end of this file for inspiration.
3302 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS);
3303 if (!Mul || !Mul->hasNoUnsignedWrap())
3304 return getUDivExpr(LHS, RHS);
3306 if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
3307 // If the mulexpr multiplies by a constant, then that constant must be the
3308 // first element of the mulexpr.
3309 if (const auto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
3310 if (LHSCst == RHSCst) {
3311 SmallVector<const SCEV *, 2> Operands;
3312 Operands.append(Mul->op_begin() + 1, Mul->op_end());
3313 return getMulExpr(Operands);
3316 // We can't just assume that LHSCst divides RHSCst cleanly, it could be
3317 // that there's a factor provided by one of the other terms. We need to
3319 APInt Factor = gcd(LHSCst, RHSCst);
3320 if (!Factor.isIntN(1)) {
3322 cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor)));
3324 cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor)));
3325 SmallVector<const SCEV *, 2> Operands;
3326 Operands.push_back(LHSCst);
3327 Operands.append(Mul->op_begin() + 1, Mul->op_end());
3328 LHS = getMulExpr(Operands);
3330 Mul = dyn_cast<SCEVMulExpr>(LHS);
3332 return getUDivExactExpr(LHS, RHS);
3337 for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
3338 if (Mul->getOperand(i) == RHS) {
3339 SmallVector<const SCEV *, 2> Operands;
3340 Operands.append(Mul->op_begin(), Mul->op_begin() + i);
3341 Operands.append(Mul->op_begin() + i + 1, Mul->op_end());
3342 return getMulExpr(Operands);
3346 return getUDivExpr(LHS, RHS);
3349 /// Get an add recurrence expression for the specified loop. Simplify the
3350 /// expression as much as possible.
3351 const SCEV *ScalarEvolution::getAddRecExpr(const SCEV *Start, const SCEV *Step,
3353 SCEV::NoWrapFlags Flags) {
3354 SmallVector<const SCEV *, 4> Operands;
3355 Operands.push_back(Start);
3356 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
3357 if (StepChrec->getLoop() == L) {
3358 Operands.append(StepChrec->op_begin(), StepChrec->op_end());
3359 return getAddRecExpr(Operands, L, maskFlags(Flags, SCEV::FlagNW));
3362 Operands.push_back(Step);
3363 return getAddRecExpr(Operands, L, Flags);
3366 /// Get an add recurrence expression for the specified loop. Simplify the
3367 /// expression as much as possible.
3369 ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
3370 const Loop *L, SCEV::NoWrapFlags Flags) {
3371 if (Operands.size() == 1) return Operands[0];
3373 Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
3374 for (unsigned i = 1, e = Operands.size(); i != e; ++i)
3375 assert(getEffectiveSCEVType(Operands[i]->getType()) == ETy &&
3376 "SCEVAddRecExpr operand types don't match!");
3377 for (unsigned i = 0, e = Operands.size(); i != e; ++i)
3378 assert(isLoopInvariant(Operands[i], L) &&
3379 "SCEVAddRecExpr operand is not loop-invariant!");
3382 if (Operands.back()->isZero()) {
3383 Operands.pop_back();
3384 return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X
3387 // It's tempting to want to call getMaxBackedgeTakenCount count here and
3388 // use that information to infer NUW and NSW flags. However, computing a
3389 // BE count requires calling getAddRecExpr, so we may not yet have a
3390 // meaningful BE count at this point (and if we don't, we'd be stuck
3391 // with a SCEVCouldNotCompute as the cached BE count).
3393 Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
3395 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
3396 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
3397 const Loop *NestedLoop = NestedAR->getLoop();
3398 if (L->contains(NestedLoop)
3399 ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
3400 : (!NestedLoop->contains(L) &&
3401 DT.dominates(L->getHeader(), NestedLoop->getHeader()))) {
3402 SmallVector<const SCEV *, 4> NestedOperands(NestedAR->op_begin(),
3403 NestedAR->op_end());
3404 Operands[0] = NestedAR->getStart();
3405 // AddRecs require their operands be loop-invariant with respect to their
3406 // loops. Don't perform this transformation if it would break this
3408 bool AllInvariant = all_of(
3409 Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); });
3412 // Create a recurrence for the outer loop with the same step size.
3414 // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
3415 // inner recurrence has the same property.
3416 SCEV::NoWrapFlags OuterFlags =
3417 maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
3419 NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
3420 AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) {
3421 return isLoopInvariant(Op, NestedLoop);
3425 // Ok, both add recurrences are valid after the transformation.
3427 // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
3428 // the outer recurrence has the same property.
3429 SCEV::NoWrapFlags InnerFlags =
3430 maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
3431 return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
3434 // Reset Operands to its original state.
3435 Operands[0] = NestedAR;
3439 // Okay, it looks like we really DO need an addrec expr. Check to see if we
3440 // already have one, otherwise create a new one.
3441 return getOrCreateAddRecExpr(Operands, L, Flags);
3445 ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3446 const SmallVectorImpl<const SCEV *> &IndexExprs) {
3447 const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
3448 // getSCEV(Base)->getType() has the same address space as Base->getType()
3449 // because SCEV::getType() preserves the address space.
3450 Type *IntPtrTy = getEffectiveSCEVType(BaseExpr->getType());
3451 // FIXME(PR23527): Don't blindly transfer the inbounds flag from the GEP
3452 // instruction to its SCEV, because the Instruction may be guarded by control
3453 // flow and the no-overflow bits may not be valid for the expression in any
3454 // context. This can be fixed similarly to how these flags are handled for
3456 SCEV::NoWrapFlags Wrap = GEP->isInBounds() ? SCEV::FlagNSW
3457 : SCEV::FlagAnyWrap;
3459 const SCEV *TotalOffset = getZero(IntPtrTy);
3460 // The array size is unimportant. The first thing we do on CurTy is getting
3461 // its element type.
3462 Type *CurTy = ArrayType::get(GEP->getSourceElementType(), 0);
3463 for (const SCEV *IndexExpr : IndexExprs) {
3464 // Compute the (potentially symbolic) offset in bytes for this index.
3465 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
3466 // For a struct, add the member offset.
3467 ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
3468 unsigned FieldNo = Index->getZExtValue();
3469 const SCEV *FieldOffset = getOffsetOfExpr(IntPtrTy, STy, FieldNo);
3471 // Add the field offset to the running total offset.
3472 TotalOffset = getAddExpr(TotalOffset, FieldOffset);
3474 // Update CurTy to the type of the field at Index.
3475 CurTy = STy->getTypeAtIndex(Index);
3477 // Update CurTy to its element type.
3478 CurTy = cast<SequentialType>(CurTy)->getElementType();
3479 // For an array, add the element offset, explicitly scaled.
3480 const SCEV *ElementSize = getSizeOfExpr(IntPtrTy, CurTy);
3481 // Getelementptr indices are signed.
3482 IndexExpr = getTruncateOrSignExtend(IndexExpr, IntPtrTy);
3484 // Multiply the index by the element size to compute the element offset.
3485 const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, Wrap);
3487 // Add the element offset to the running total offset.
3488 TotalOffset = getAddExpr(TotalOffset, LocalOffset);
3492 // Add the total offset from all the GEP indices to the base.
3493 return getAddExpr(BaseExpr, TotalOffset, Wrap);
3496 const SCEV *ScalarEvolution::getSMaxExpr(const SCEV *LHS,
3498 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
3499 return getSMaxExpr(Ops);
3503 ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
3504 assert(!Ops.empty() && "Cannot get empty smax!");
3505 if (Ops.size() == 1) return Ops[0];
3507 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
3508 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
3509 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
3510 "SCEVSMaxExpr operand types don't match!");
3513 // Sort by complexity, this groups all similar expression types together.
3514 GroupByComplexity(Ops, &LI, DT);
3516 // If there are any constants, fold them together.
3518 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3520 assert(Idx < Ops.size());
3521 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3522 // We found two constants, fold them together!
3523 ConstantInt *Fold = ConstantInt::get(
3524 getContext(), APIntOps::smax(LHSC->getAPInt(), RHSC->getAPInt()));
3525 Ops[0] = getConstant(Fold);
3526 Ops.erase(Ops.begin()+1); // Erase the folded element
3527 if (Ops.size() == 1) return Ops[0];
3528 LHSC = cast<SCEVConstant>(Ops[0]);
3531 // If we are left with a constant minimum-int, strip it off.
3532 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(true)) {
3533 Ops.erase(Ops.begin());
3535 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(true)) {
3536 // If we have an smax with a constant maximum-int, it will always be
3541 if (Ops.size() == 1) return Ops[0];
3544 // Find the first SMax
3545 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scSMaxExpr)
3548 // Check to see if one of the operands is an SMax. If so, expand its operands
3549 // onto our operand list, and recurse to simplify.
3550 if (Idx < Ops.size()) {
3551 bool DeletedSMax = false;
3552 while (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(Ops[Idx])) {
3553 Ops.erase(Ops.begin()+Idx);
3554 Ops.append(SMax->op_begin(), SMax->op_end());
3559 return getSMaxExpr(Ops);
3562 // Okay, check to see if the same value occurs in the operand list twice. If
3563 // so, delete one. Since we sorted the list, these values are required to
3565 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
3566 // X smax Y smax Y --> X smax Y
3567 // X smax Y --> X, if X is always greater than Y
3568 if (Ops[i] == Ops[i+1] ||
3569 isKnownPredicate(ICmpInst::ICMP_SGE, Ops[i], Ops[i+1])) {
3570 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+2);
3572 } else if (isKnownPredicate(ICmpInst::ICMP_SLE, Ops[i], Ops[i+1])) {
3573 Ops.erase(Ops.begin()+i, Ops.begin()+i+1);
3577 if (Ops.size() == 1) return Ops[0];
3579 assert(!Ops.empty() && "Reduced smax down to nothing!");
3581 // Okay, it looks like we really DO need an smax expr. Check to see if we
3582 // already have one, otherwise create a new one.
3583 FoldingSetNodeID ID;
3584 ID.AddInteger(scSMaxExpr);
3585 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3586 ID.AddPointer(Ops[i]);
3588 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
3589 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3590 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3591 SCEV *S = new (SCEVAllocator) SCEVSMaxExpr(ID.Intern(SCEVAllocator),
3593 UniqueSCEVs.InsertNode(S, IP);
3594 addToLoopUseLists(S);
3598 const SCEV *ScalarEvolution::getUMaxExpr(const SCEV *LHS,
3600 SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
3601 return getUMaxExpr(Ops);
3605 ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
3606 assert(!Ops.empty() && "Cannot get empty umax!");
3607 if (Ops.size() == 1) return Ops[0];
3609 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
3610 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
3611 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
3612 "SCEVUMaxExpr operand types don't match!");
3615 // Sort by complexity, this groups all similar expression types together.
3616 GroupByComplexity(Ops, &LI, DT);
3618 // If there are any constants, fold them together.
3620 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3622 assert(Idx < Ops.size());
3623 while (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Ops[Idx])) {
3624 // We found two constants, fold them together!
3625 ConstantInt *Fold = ConstantInt::get(
3626 getContext(), APIntOps::umax(LHSC->getAPInt(), RHSC->getAPInt()));
3627 Ops[0] = getConstant(Fold);
3628 Ops.erase(Ops.begin()+1); // Erase the folded element
3629 if (Ops.size() == 1) return Ops[0];
3630 LHSC = cast<SCEVConstant>(Ops[0]);
3633 // If we are left with a constant minimum-int, strip it off.
3634 if (cast<SCEVConstant>(Ops[0])->getValue()->isMinValue(false)) {
3635 Ops.erase(Ops.begin());
3637 } else if (cast<SCEVConstant>(Ops[0])->getValue()->isMaxValue(false)) {
3638 // If we have an umax with a constant maximum-int, it will always be
3643 if (Ops.size() == 1) return Ops[0];
3646 // Find the first UMax
3647 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scUMaxExpr)
3650 // Check to see if one of the operands is a UMax. If so, expand its operands
3651 // onto our operand list, and recurse to simplify.
3652 if (Idx < Ops.size()) {
3653 bool DeletedUMax = false;
3654 while (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(Ops[Idx])) {
3655 Ops.erase(Ops.begin()+Idx);
3656 Ops.append(UMax->op_begin(), UMax->op_end());
3661 return getUMaxExpr(Ops);
3664 // Okay, check to see if the same value occurs in the operand list twice. If
3665 // so, delete one. Since we sorted the list, these values are required to
3667 for (unsigned i = 0, e = Ops.size()-1; i != e; ++i)
3668 // X umax Y umax Y --> X umax Y
3669 // X umax Y --> X, if X is always greater than Y
3670 if (Ops[i] == Ops[i + 1] || isKnownViaNonRecursiveReasoning(
3671 ICmpInst::ICMP_UGE, Ops[i], Ops[i + 1])) {
3672 Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2);
3674 } else if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, Ops[i],
3676 Ops.erase(Ops.begin() + i, Ops.begin() + i + 1);
3680 if (Ops.size() == 1) return Ops[0];
3682 assert(!Ops.empty() && "Reduced umax down to nothing!");
3684 // Okay, it looks like we really DO need a umax expr. Check to see if we
3685 // already have one, otherwise create a new one.
3686 FoldingSetNodeID ID;
3687 ID.AddInteger(scUMaxExpr);
3688 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3689 ID.AddPointer(Ops[i]);
3691 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
3692 const SCEV **O = SCEVAllocator.Allocate<const SCEV *>(Ops.size());
3693 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3694 SCEV *S = new (SCEVAllocator) SCEVUMaxExpr(ID.Intern(SCEVAllocator),
3696 UniqueSCEVs.InsertNode(S, IP);
3697 addToLoopUseLists(S);
3701 const SCEV *ScalarEvolution::getSMinExpr(const SCEV *LHS,
3703 SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
3704 return getSMinExpr(Ops);
3707 const SCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl<const SCEV *> &Ops) {
3708 // ~smax(~x, ~y, ~z) == smin(x, y, z).
3709 SmallVector<const SCEV *, 2> NotOps;
3711 NotOps.push_back(getNotSCEV(S));
3712 return getNotSCEV(getSMaxExpr(NotOps));
3715 const SCEV *ScalarEvolution::getUMinExpr(const SCEV *LHS,
3717 SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
3718 return getUMinExpr(Ops);
3721 const SCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl<const SCEV *> &Ops) {
3722 assert(!Ops.empty() && "At least one operand must be!");
3724 if (Ops.size() == 1)
3727 // ~umax(~x, ~y, ~z) == umin(x, y, z).
3728 SmallVector<const SCEV *, 2> NotOps;
3730 NotOps.push_back(getNotSCEV(S));
3731 return getNotSCEV(getUMaxExpr(NotOps));
3734 const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
3735 // We can bypass creating a target-independent
3736 // constant expression and then folding it back into a ConstantInt.
3737 // This is just a compile-time optimization.
3738 return getConstant(IntTy, getDataLayout().getTypeAllocSize(AllocTy));
3741 const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
3744 // We can bypass creating a target-independent
3745 // constant expression and then folding it back into a ConstantInt.
3746 // This is just a compile-time optimization.
3748 IntTy, getDataLayout().getStructLayout(STy)->getElementOffset(FieldNo));
3751 const SCEV *ScalarEvolution::getUnknown(Value *V) {
3752 // Don't attempt to do anything other than create a SCEVUnknown object
3753 // here. createSCEV only calls getUnknown after checking for all other
3754 // interesting possibilities, and any other code that calls getUnknown
3755 // is doing so in order to hide a value from SCEV canonicalization.
3757 FoldingSetNodeID ID;
3758 ID.AddInteger(scUnknown);
3761 if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
3762 assert(cast<SCEVUnknown>(S)->getValue() == V &&
3763 "Stale SCEVUnknown in uniquing map!");
3766 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
3768 FirstUnknown = cast<SCEVUnknown>(S);
3769 UniqueSCEVs.InsertNode(S, IP);
3773 //===----------------------------------------------------------------------===//
3774 // Basic SCEV Analysis and PHI Idiom Recognition Code
3777 /// Test if values of the given type are analyzable within the SCEV
3778 /// framework. This primarily includes integer types, and it can optionally
3779 /// include pointer types if the ScalarEvolution class has access to
3780 /// target-specific information.
3781 bool ScalarEvolution::isSCEVable(Type *Ty) const {
3782 // Integers and pointers are always SCEVable.
3783 return Ty->isIntOrPtrTy();
3786 /// Return the size in bits of the specified type, for which isSCEVable must
3788 uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
3789 assert(isSCEVable(Ty) && "Type is not SCEVable!");
3790 if (Ty->isPointerTy())
3791 return getDataLayout().getIndexTypeSizeInBits(Ty);
3792 return getDataLayout().getTypeSizeInBits(Ty);
3795 /// Return a type with the same bitwidth as the given type and which represents
3796 /// how SCEV will treat the given type, for which isSCEVable must return
3797 /// true. For pointer types, this is the pointer-sized integer type.
3798 Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
3799 assert(isSCEVable(Ty) && "Type is not SCEVable!");
3801 if (Ty->isIntegerTy())
3804 // The only other support type is pointer.
3805 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
3806 return getDataLayout().getIntPtrType(Ty);
3809 Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const {
3810 return getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2;
3813 const SCEV *ScalarEvolution::getCouldNotCompute() {
3814 return CouldNotCompute.get();
3817 bool ScalarEvolution::checkValidity(const SCEV *S) const {
3818 bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) {
3819 auto *SU = dyn_cast<SCEVUnknown>(S);
3820 return SU && SU->getValue() == nullptr;
3823 return !ContainsNulls;
3826 bool ScalarEvolution::containsAddRecurrence(const SCEV *S) {
3827 HasRecMapType::iterator I = HasRecMap.find(S);
3828 if (I != HasRecMap.end())
3831 bool FoundAddRec = SCEVExprContains(S, isa<SCEVAddRecExpr, const SCEV *>);
3832 HasRecMap.insert({S, FoundAddRec});
3836 /// Try to split a SCEVAddExpr into a pair of {SCEV, ConstantInt}.
3837 /// If \p S is a SCEVAddExpr and is composed of a sub SCEV S' and an
3838 /// offset I, then return {S', I}, else return {\p S, nullptr}.
3839 static std::pair<const SCEV *, ConstantInt *> splitAddExpr(const SCEV *S) {
3840 const auto *Add = dyn_cast<SCEVAddExpr>(S);
3842 return {S, nullptr};
3844 if (Add->getNumOperands() != 2)
3845 return {S, nullptr};
3847 auto *ConstOp = dyn_cast<SCEVConstant>(Add->getOperand(0));
3849 return {S, nullptr};
3851 return {Add->getOperand(1), ConstOp->getValue()};
3854 /// Return the ValueOffsetPair set for \p S. \p S can be represented
3855 /// by the value and offset from any ValueOffsetPair in the set.
3856 SetVector<ScalarEvolution::ValueOffsetPair> *
3857 ScalarEvolution::getSCEVValues(const SCEV *S) {
3858 ExprValueMapType::iterator SI = ExprValueMap.find_as(S);
3859 if (SI == ExprValueMap.end())
3862 if (VerifySCEVMap) {
3863 // Check there is no dangling Value in the set returned.
3864 for (const auto &VE : SI->second)
3865 assert(ValueExprMap.count(VE.first));
3871 /// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
3872 /// cannot be used separately. eraseValueFromMap should be used to remove
3873 /// V from ValueExprMap and ExprValueMap at the same time.
3874 void ScalarEvolution::eraseValueFromMap(Value *V) {
3875 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
3876 if (I != ValueExprMap.end()) {
3877 const SCEV *S = I->second;
3878 // Remove {V, 0} from the set of ExprValueMap[S]
3879 if (SetVector<ValueOffsetPair> *SV = getSCEVValues(S))
3880 SV->remove({V, nullptr});
3882 // Remove {V, Offset} from the set of ExprValueMap[Stripped]
3883 const SCEV *Stripped;
3884 ConstantInt *Offset;
3885 std::tie(Stripped, Offset) = splitAddExpr(S);
3886 if (Offset != nullptr) {
3887 if (SetVector<ValueOffsetPair> *SV = getSCEVValues(Stripped))
3888 SV->remove({V, Offset});
3890 ValueExprMap.erase(V);
3894 /// Check whether value has nuw/nsw/exact set but SCEV does not.
3895 /// TODO: In reality it is better to check the poison recursevely
3896 /// but this is better than nothing.
3897 static bool SCEVLostPoisonFlags(const SCEV *S, const Value *V) {
3898 if (auto *I = dyn_cast<Instruction>(V)) {
3899 if (isa<OverflowingBinaryOperator>(I)) {
3900 if (auto *NS = dyn_cast<SCEVNAryExpr>(S)) {
3901 if (I->hasNoSignedWrap() && !NS->hasNoSignedWrap())
3903 if (I->hasNoUnsignedWrap() && !NS->hasNoUnsignedWrap())
3906 } else if (isa<PossiblyExactOperator>(I) && I->isExact())
3912 /// Return an existing SCEV if it exists, otherwise analyze the expression and
3913 /// create a new one.
3914 const SCEV *ScalarEvolution::getSCEV(Value *V) {
3915 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
3917 const SCEV *S = getExistingSCEV(V);
3920 // During PHI resolution, it is possible to create two SCEVs for the same
3921 // V, so it is needed to double check whether V->S is inserted into
3922 // ValueExprMap before insert S->{V, 0} into ExprValueMap.
3923 std::pair<ValueExprMapType::iterator, bool> Pair =
3924 ValueExprMap.insert({SCEVCallbackVH(V, this), S});
3925 if (Pair.second && !SCEVLostPoisonFlags(S, V)) {
3926 ExprValueMap[S].insert({V, nullptr});
3928 // If S == Stripped + Offset, add Stripped -> {V, Offset} into
3930 const SCEV *Stripped = S;
3931 ConstantInt *Offset = nullptr;
3932 std::tie(Stripped, Offset) = splitAddExpr(S);
3933 // If stripped is SCEVUnknown, don't bother to save
3934 // Stripped -> {V, offset}. It doesn't simplify and sometimes even
3935 // increase the complexity of the expansion code.
3936 // If V is GetElementPtrInst, don't save Stripped -> {V, offset}
3937 // because it may generate add/sub instead of GEP in SCEV expansion.
3938 if (Offset != nullptr && !isa<SCEVUnknown>(Stripped) &&
3939 !isa<GetElementPtrInst>(V))
3940 ExprValueMap[Stripped].insert({V, Offset});
3946 const SCEV *ScalarEvolution::getExistingSCEV(Value *V) {
3947 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
3949 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
3950 if (I != ValueExprMap.end()) {
3951 const SCEV *S = I->second;
3952 if (checkValidity(S))
3954 eraseValueFromMap(V);
3955 forgetMemoizedResults(S);
3960 /// Return a SCEV corresponding to -V = -1*V
3961 const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
3962 SCEV::NoWrapFlags Flags) {
3963 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
3965 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
3967 Type *Ty = V->getType();
3968 Ty = getEffectiveSCEVType(Ty);
3970 V, getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty))), Flags);
3973 /// Return a SCEV corresponding to ~V = -1-V
3974 const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
3975 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
3977 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
3979 Type *Ty = V->getType();
3980 Ty = getEffectiveSCEVType(Ty);
3981 const SCEV *AllOnes =
3982 getConstant(cast<ConstantInt>(Constant::getAllOnesValue(Ty)));
3983 return getMinusSCEV(AllOnes, V);
3986 const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
3987 SCEV::NoWrapFlags Flags,
3989 // Fast path: X - X --> 0.
3991 return getZero(LHS->getType());
3993 // We represent LHS - RHS as LHS + (-1)*RHS. This transformation
3994 // makes it so that we cannot make much use of NUW.
3995 auto AddFlags = SCEV::FlagAnyWrap;
3996 const bool RHSIsNotMinSigned =
3997 !getSignedRangeMin(RHS).isMinSignedValue();
3998 if (maskFlags(Flags, SCEV::FlagNSW) == SCEV::FlagNSW) {
3999 // Let M be the minimum representable signed value. Then (-1)*RHS
4000 // signed-wraps if and only if RHS is M. That can happen even for
4001 // a NSW subtraction because e.g. (-1)*M signed-wraps even though
4002 // -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
4003 // (-1)*RHS, we need to prove that RHS != M.
4005 // If LHS is non-negative and we know that LHS - RHS does not
4006 // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
4007 // either by proving that RHS > M or that LHS >= 0.
4008 if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) {
4009 AddFlags = SCEV::FlagNSW;
4013 // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
4014 // RHS is NSW and LHS >= 0.
4016 // The difficulty here is that the NSW flag may have been proven
4017 // relative to a loop that is to be found in a recurrence in LHS and
4018 // not in RHS. Applying NSW to (-1)*M may then let the NSW have a
4019 // larger scope than intended.
4020 auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
4022 return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth);
4026 ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty) {
4027 Type *SrcTy = V->getType();
4028 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4029 "Cannot truncate or zero extend with non-integer arguments!");
4030 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4031 return V; // No conversion
4032 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
4033 return getTruncateExpr(V, Ty);
4034 return getZeroExtendExpr(V, Ty);
4038 ScalarEvolution::getTruncateOrSignExtend(const SCEV *V,
4040 Type *SrcTy = V->getType();
4041 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4042 "Cannot truncate or zero extend with non-integer arguments!");
4043 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4044 return V; // No conversion
4045 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
4046 return getTruncateExpr(V, Ty);
4047 return getSignExtendExpr(V, Ty);
4051 ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
4052 Type *SrcTy = V->getType();
4053 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4054 "Cannot noop or zero extend with non-integer arguments!");
4055 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
4056 "getNoopOrZeroExtend cannot truncate!");
4057 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4058 return V; // No conversion
4059 return getZeroExtendExpr(V, Ty);
4063 ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
4064 Type *SrcTy = V->getType();
4065 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4066 "Cannot noop or sign extend with non-integer arguments!");
4067 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
4068 "getNoopOrSignExtend cannot truncate!");
4069 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4070 return V; // No conversion
4071 return getSignExtendExpr(V, Ty);
4075 ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
4076 Type *SrcTy = V->getType();
4077 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4078 "Cannot noop or any extend with non-integer arguments!");
4079 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
4080 "getNoopOrAnyExtend cannot truncate!");
4081 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4082 return V; // No conversion
4083 return getAnyExtendExpr(V, Ty);
4087 ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
4088 Type *SrcTy = V->getType();
4089 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4090 "Cannot truncate or noop with non-integer arguments!");
4091 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
4092 "getTruncateOrNoop cannot extend!");
4093 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4094 return V; // No conversion
4095 return getTruncateExpr(V, Ty);
4098 const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
4100 const SCEV *PromotedLHS = LHS;
4101 const SCEV *PromotedRHS = RHS;
4103 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
4104 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
4106 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
4108 return getUMaxExpr(PromotedLHS, PromotedRHS);
4111 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
4113 SmallVector<const SCEV *, 2> Ops = { LHS, RHS };
4114 return getUMinFromMismatchedTypes(Ops);
4117 const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(
4118 SmallVectorImpl<const SCEV *> &Ops) {
4119 assert(!Ops.empty() && "At least one operand must be!");
4121 if (Ops.size() == 1)
4124 // Find the max type first.
4125 Type *MaxType = nullptr;
4128 MaxType = getWiderType(MaxType, S->getType());
4130 MaxType = S->getType();
4132 // Extend all ops to max type.
4133 SmallVector<const SCEV *, 2> PromotedOps;
4135 PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType));
4138 return getUMinExpr(PromotedOps);
4141 const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
4142 // A pointer operand may evaluate to a nonpointer expression, such as null.
4143 if (!V->getType()->isPointerTy())
4146 if (const SCEVCastExpr *Cast = dyn_cast<SCEVCastExpr>(V)) {
4147 return getPointerBase(Cast->getOperand());
4148 } else if (const SCEVNAryExpr *NAry = dyn_cast<SCEVNAryExpr>(V)) {
4149 const SCEV *PtrOp = nullptr;
4150 for (const SCEV *NAryOp : NAry->operands()) {
4151 if (NAryOp->getType()->isPointerTy()) {
4152 // Cannot find the base of an expression with multiple pointer operands.
4160 return getPointerBase(PtrOp);
4165 /// Push users of the given Instruction onto the given Worklist.
4167 PushDefUseChildren(Instruction *I,
4168 SmallVectorImpl<Instruction *> &Worklist) {
4169 // Push the def-use children onto the Worklist stack.
4170 for (User *U : I->users())
4171 Worklist.push_back(cast<Instruction>(U));
4174 void ScalarEvolution::forgetSymbolicName(Instruction *PN, const SCEV *SymName) {
4175 SmallVector<Instruction *, 16> Worklist;
4176 PushDefUseChildren(PN, Worklist);
4178 SmallPtrSet<Instruction *, 8> Visited;
4180 while (!Worklist.empty()) {
4181 Instruction *I = Worklist.pop_back_val();
4182 if (!Visited.insert(I).second)
4185 auto It = ValueExprMap.find_as(static_cast<Value *>(I));
4186 if (It != ValueExprMap.end()) {
4187 const SCEV *Old = It->second;
4189 // Short-circuit the def-use traversal if the symbolic name
4190 // ceases to appear in expressions.
4191 if (Old != SymName && !hasOperand(Old, SymName))
4194 // SCEVUnknown for a PHI either means that it has an unrecognized
4195 // structure, it's a PHI that's in the progress of being computed
4196 // by createNodeForPHI, or it's a single-value PHI. In the first case,
4197 // additional loop trip count information isn't going to change anything.
4198 // In the second case, createNodeForPHI will perform the necessary
4199 // updates on its own when it gets to that point. In the third, we do
4200 // want to forget the SCEVUnknown.
4201 if (!isa<PHINode>(I) ||
4202 !isa<SCEVUnknown>(Old) ||
4203 (I != PN && Old == SymName)) {
4204 eraseValueFromMap(It->first);
4205 forgetMemoizedResults(Old);
4209 PushDefUseChildren(I, Worklist);
4215 /// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start
4216 /// expression in case its Loop is L. If it is not L then
4217 /// if IgnoreOtherLoops is true then use AddRec itself
4218 /// otherwise rewrite cannot be done.
4219 /// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4220 class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> {
4222 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
4223 bool IgnoreOtherLoops = true) {
4224 SCEVInitRewriter Rewriter(L, SE);
4225 const SCEV *Result = Rewriter.visit(S);
4226 if (Rewriter.hasSeenLoopVariantSCEVUnknown())
4227 return SE.getCouldNotCompute();
4228 return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops
4229 ? SE.getCouldNotCompute()
4233 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4234 if (!SE.isLoopInvariant(Expr, L))
4235 SeenLoopVariantSCEVUnknown = true;
4239 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4240 // Only re-write AddRecExprs for this loop.
4241 if (Expr->getLoop() == L)
4242 return Expr->getStart();
4243 SeenOtherLoops = true;
4247 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4249 bool hasSeenOtherLoops() { return SeenOtherLoops; }
4252 explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE)
4253 : SCEVRewriteVisitor(SE), L(L) {}
4256 bool SeenLoopVariantSCEVUnknown = false;
4257 bool SeenOtherLoops = false;
4260 /// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post
4261 /// increment expression in case its Loop is L. If it is not L then
4262 /// use AddRec itself.
4263 /// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4264 class SCEVPostIncRewriter : public SCEVRewriteVisitor<SCEVPostIncRewriter> {
4266 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) {
4267 SCEVPostIncRewriter Rewriter(L, SE);
4268 const SCEV *Result = Rewriter.visit(S);
4269 return Rewriter.hasSeenLoopVariantSCEVUnknown()
4270 ? SE.getCouldNotCompute()
4274 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4275 if (!SE.isLoopInvariant(Expr, L))
4276 SeenLoopVariantSCEVUnknown = true;
4280 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4281 // Only re-write AddRecExprs for this loop.
4282 if (Expr->getLoop() == L)
4283 return Expr->getPostIncExpr(SE);
4284 SeenOtherLoops = true;
4288 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4290 bool hasSeenOtherLoops() { return SeenOtherLoops; }
4293 explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE)
4294 : SCEVRewriteVisitor(SE), L(L) {}
4297 bool SeenLoopVariantSCEVUnknown = false;
4298 bool SeenOtherLoops = false;
4301 /// This class evaluates the compare condition by matching it against the
4302 /// condition of loop latch. If there is a match we assume a true value
4303 /// for the condition while building SCEV nodes.
4304 class SCEVBackedgeConditionFolder
4305 : public SCEVRewriteVisitor<SCEVBackedgeConditionFolder> {
4307 static const SCEV *rewrite(const SCEV *S, const Loop *L,
4308 ScalarEvolution &SE) {
4309 bool IsPosBECond = false;
4310 Value *BECond = nullptr;
4311 if (BasicBlock *Latch = L->getLoopLatch()) {
4312 BranchInst *BI = dyn_cast<BranchInst>(Latch->getTerminator());
4313 if (BI && BI->isConditional()) {
4314 assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
4315 "Both outgoing branches should not target same header!");
4316 BECond = BI->getCondition();
4317 IsPosBECond = BI->getSuccessor(0) == L->getHeader();
4322 SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE);
4323 return Rewriter.visit(S);
4326 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4327 const SCEV *Result = Expr;
4328 bool InvariantF = SE.isLoopInvariant(Expr, L);
4331 Instruction *I = cast<Instruction>(Expr->getValue());
4332 switch (I->getOpcode()) {
4333 case Instruction::Select: {
4334 SelectInst *SI = cast<SelectInst>(I);
4335 Optional<const SCEV *> Res =
4336 compareWithBackedgeCondition(SI->getCondition());
4337 if (Res.hasValue()) {
4338 bool IsOne = cast<SCEVConstant>(Res.getValue())->getValue()->isOne();
4339 Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue());
4344 Optional<const SCEV *> Res = compareWithBackedgeCondition(I);
4346 Result = Res.getValue();
4355 explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond,
4356 bool IsPosBECond, ScalarEvolution &SE)
4357 : SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond),
4358 IsPositiveBECond(IsPosBECond) {}
4360 Optional<const SCEV *> compareWithBackedgeCondition(Value *IC);
4363 /// Loop back condition.
4364 Value *BackedgeCond = nullptr;
4365 /// Set to true if loop back is on positive branch condition.
4366 bool IsPositiveBECond;
4369 Optional<const SCEV *>
4370 SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) {
4372 // If value matches the backedge condition for loop latch,
4373 // then return a constant evolution node based on loopback
4375 if (BackedgeCond == IC)
4376 return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext()))
4377 : SE.getZero(Type::getInt1Ty(SE.getContext()));
4381 class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> {
4383 static const SCEV *rewrite(const SCEV *S, const Loop *L,
4384 ScalarEvolution &SE) {
4385 SCEVShiftRewriter Rewriter(L, SE);
4386 const SCEV *Result = Rewriter.visit(S);
4387 return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
4390 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4391 // Only allow AddRecExprs for this loop.
4392 if (!SE.isLoopInvariant(Expr, L))
4397 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4398 if (Expr->getLoop() == L && Expr->isAffine())
4399 return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE));
4404 bool isValid() { return Valid; }
4407 explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE)
4408 : SCEVRewriteVisitor(SE), L(L) {}
4414 } // end anonymous namespace
4417 ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
4418 if (!AR->isAffine())
4419 return SCEV::FlagAnyWrap;
4421 using OBO = OverflowingBinaryOperator;
4423 SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap;
4425 if (!AR->hasNoSignedWrap()) {
4426 ConstantRange AddRecRange = getSignedRange(AR);
4427 ConstantRange IncRange = getSignedRange(AR->getStepRecurrence(*this));
4429 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4430 Instruction::Add, IncRange, OBO::NoSignedWrap);
4431 if (NSWRegion.contains(AddRecRange))
4432 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNSW);
4435 if (!AR->hasNoUnsignedWrap()) {
4436 ConstantRange AddRecRange = getUnsignedRange(AR);
4437 ConstantRange IncRange = getUnsignedRange(AR->getStepRecurrence(*this));
4439 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
4440 Instruction::Add, IncRange, OBO::NoUnsignedWrap);
4441 if (NUWRegion.contains(AddRecRange))
4442 Result = ScalarEvolution::setFlags(Result, SCEV::FlagNUW);
4450 /// Represents an abstract binary operation. This may exist as a
4451 /// normal instruction or constant expression, or may have been
4452 /// derived from an expression tree.
4460 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
4461 /// constant expression.
4462 Operator *Op = nullptr;
4464 explicit BinaryOp(Operator *Op)
4465 : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)),
4467 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
4468 IsNSW = OBO->hasNoSignedWrap();
4469 IsNUW = OBO->hasNoUnsignedWrap();
4473 explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false,
4475 : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
4478 } // end anonymous namespace
4480 /// Try to map \p V into a BinaryOp, and return \c None on failure.
4481 static Optional<BinaryOp> MatchBinaryOp(Value *V, DominatorTree &DT) {
4482 auto *Op = dyn_cast<Operator>(V);
4486 // Implementation detail: all the cleverness here should happen without
4487 // creating new SCEV expressions -- our caller knowns tricks to avoid creating
4488 // SCEV expressions when possible, and we should not break that.
4490 switch (Op->getOpcode()) {
4491 case Instruction::Add:
4492 case Instruction::Sub:
4493 case Instruction::Mul:
4494 case Instruction::UDiv:
4495 case Instruction::URem:
4496 case Instruction::And:
4497 case Instruction::Or:
4498 case Instruction::AShr:
4499 case Instruction::Shl:
4500 return BinaryOp(Op);
4502 case Instruction::Xor:
4503 if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1)))
4504 // If the RHS of the xor is a signmask, then this is just an add.
4505 // Instcombine turns add of signmask into xor as a strength reduction step.
4506 if (RHSC->getValue().isSignMask())
4507 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
4508 return BinaryOp(Op);
4510 case Instruction::LShr:
4511 // Turn logical shift right of a constant into a unsigned divide.
4512 if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
4513 uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth();
4515 // If the shift count is not less than the bitwidth, the result of
4516 // the shift is undefined. Don't try to analyze it, because the
4517 // resolution chosen here may differ from the resolution chosen in
4518 // other parts of the compiler.
4519 if (SA->getValue().ult(BitWidth)) {
4521 ConstantInt::get(SA->getContext(),
4522 APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
4523 return BinaryOp(Instruction::UDiv, Op->getOperand(0), X);
4526 return BinaryOp(Op);
4528 case Instruction::ExtractValue: {
4529 auto *EVI = cast<ExtractValueInst>(Op);
4530 if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
4533 auto *CI = dyn_cast<CallInst>(EVI->getAggregateOperand());
4537 if (auto *F = CI->getCalledFunction())
4538 switch (F->getIntrinsicID()) {
4539 case Intrinsic::sadd_with_overflow:
4540 case Intrinsic::uadd_with_overflow:
4541 if (!isOverflowIntrinsicNoWrap(cast<IntrinsicInst>(CI), DT))
4542 return BinaryOp(Instruction::Add, CI->getArgOperand(0),
4543 CI->getArgOperand(1));
4545 // Now that we know that all uses of the arithmetic-result component of
4546 // CI are guarded by the overflow check, we can go ahead and pretend
4547 // that the arithmetic is non-overflowing.
4548 if (F->getIntrinsicID() == Intrinsic::sadd_with_overflow)
4549 return BinaryOp(Instruction::Add, CI->getArgOperand(0),
4550 CI->getArgOperand(1), /* IsNSW = */ true,
4551 /* IsNUW = */ false);
4553 return BinaryOp(Instruction::Add, CI->getArgOperand(0),
4554 CI->getArgOperand(1), /* IsNSW = */ false,
4556 case Intrinsic::ssub_with_overflow:
4557 case Intrinsic::usub_with_overflow:
4558 if (!isOverflowIntrinsicNoWrap(cast<IntrinsicInst>(CI), DT))
4559 return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
4560 CI->getArgOperand(1));
4562 // The same reasoning as sadd/uadd above.
4563 if (F->getIntrinsicID() == Intrinsic::ssub_with_overflow)
4564 return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
4565 CI->getArgOperand(1), /* IsNSW = */ true,
4566 /* IsNUW = */ false);
4568 return BinaryOp(Instruction::Sub, CI->getArgOperand(0),
4569 CI->getArgOperand(1), /* IsNSW = */ false,
4570 /* IsNUW = */ true);
4571 case Intrinsic::smul_with_overflow:
4572 case Intrinsic::umul_with_overflow:
4573 return BinaryOp(Instruction::Mul, CI->getArgOperand(0),
4574 CI->getArgOperand(1));
4588 /// Helper function to createAddRecFromPHIWithCasts. We have a phi
4589 /// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
4590 /// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the
4591 /// way. This function checks if \p Op, an operand of this SCEVAddExpr,
4592 /// follows one of the following patterns:
4593 /// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4594 /// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
4595 /// If the SCEV expression of \p Op conforms with one of the expected patterns
4596 /// we return the type of the truncation operation, and indicate whether the
4597 /// truncated type should be treated as signed/unsigned by setting
4598 /// \p Signed to true/false, respectively.
4599 static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
4600 bool &Signed, ScalarEvolution &SE) {
4601 // The case where Op == SymbolicPHI (that is, with no type conversions on
4602 // the way) is handled by the regular add recurrence creating logic and
4603 // would have already been triggered in createAddRecForPHI. Reaching it here
4604 // means that createAddRecFromPHI had failed for this PHI before (e.g.,
4605 // because one of the other operands of the SCEVAddExpr updating this PHI is
4608 // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in
4609 // this case predicates that allow us to prove that Op == SymbolicPHI will
4611 if (Op == SymbolicPHI)
4614 unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType());
4615 unsigned NewBits = SE.getTypeSizeInBits(Op->getType());
4616 if (SourceBits != NewBits)
4619 const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op);
4620 const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op);
4623 const SCEVTruncateExpr *Trunc =
4624 SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand())
4625 : dyn_cast<SCEVTruncateExpr>(ZExt->getOperand());
4628 const SCEV *X = Trunc->getOperand();
4629 if (X != SymbolicPHI)
4631 Signed = SExt != nullptr;
4632 return Trunc->getType();
4635 static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
4636 if (!PN->getType()->isIntegerTy())
4638 const Loop *L = LI.getLoopFor(PN->getParent());
4639 if (!L || L->getHeader() != PN->getParent())
4644 // Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
4645 // computation that updates the phi follows the following pattern:
4646 // (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
4647 // which correspond to a phi->trunc->sext/zext->add->phi update chain.
4648 // If so, try to see if it can be rewritten as an AddRecExpr under some
4649 // Predicates. If successful, return them as a pair. Also cache the results
4652 // Example usage scenario:
4653 // Say the Rewriter is called for the following SCEV:
4654 // 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4656 // %X = phi i64 (%Start, %BEValue)
4657 // It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
4658 // and call this function with %SymbolicPHI = %X.
4660 // The analysis will find that the value coming around the backedge has
4661 // the following SCEV:
4662 // BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
4663 // Upon concluding that this matches the desired pattern, the function
4664 // will return the pair {NewAddRec, SmallPredsVec} where:
4665 // NewAddRec = {%Start,+,%Step}
4666 // SmallPredsVec = {P1, P2, P3} as follows:
4667 // P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
4668 // P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
4669 // P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
4670 // The returned pair means that SymbolicPHI can be rewritten into NewAddRec
4671 // under the predicates {P1,P2,P3}.
4672 // This predicated rewrite will be cached in PredicatedSCEVRewrites:
4673 // PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)}
4677 // 1) Extend the Induction descriptor to also support inductions that involve
4678 // casts: When needed (namely, when we are called in the context of the
4679 // vectorizer induction analysis), a Set of cast instructions will be
4680 // populated by this method, and provided back to isInductionPHI. This is
4681 // needed to allow the vectorizer to properly record them to be ignored by
4682 // the cost model and to avoid vectorizing them (otherwise these casts,
4683 // which are redundant under the runtime overflow checks, will be
4684 // vectorized, which can be costly).
4686 // 2) Support additional induction/PHISCEV patterns: We also want to support
4687 // inductions where the sext-trunc / zext-trunc operations (partly) occur
4688 // after the induction update operation (the induction increment):
4690 // (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
4691 // which correspond to a phi->add->trunc->sext/zext->phi update chain.
4693 // (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
4694 // which correspond to a phi->trunc->add->sext/zext->phi update chain.
4696 // 3) Outline common code with createAddRecFromPHI to avoid duplication.
4697 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4698 ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) {
4699 SmallVector<const SCEVPredicate *, 3> Predicates;
4701 // *** Part1: Analyze if we have a phi-with-cast pattern for which we can
4702 // return an AddRec expression under some predicate.
4704 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
4705 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
4706 assert(L && "Expecting an integer loop header phi");
4708 // The loop may have multiple entrances or multiple exits; we can analyze
4709 // this phi as an addrec if it has a unique entry value and a unique
4711 Value *BEValueV = nullptr, *StartValueV = nullptr;
4712 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
4713 Value *V = PN->getIncomingValue(i);
4714 if (L->contains(PN->getIncomingBlock(i))) {
4717 } else if (BEValueV != V) {
4721 } else if (!StartValueV) {
4723 } else if (StartValueV != V) {
4724 StartValueV = nullptr;
4728 if (!BEValueV || !StartValueV)
4731 const SCEV *BEValue = getSCEV(BEValueV);
4733 // If the value coming around the backedge is an add with the symbolic
4734 // value we just inserted, possibly with casts that we can ignore under
4735 // an appropriate runtime guard, then we found a simple induction variable!
4736 const auto *Add = dyn_cast<SCEVAddExpr>(BEValue);
4740 // If there is a single occurrence of the symbolic value, possibly
4741 // casted, replace it with a recurrence.
4742 unsigned FoundIndex = Add->getNumOperands();
4743 Type *TruncTy = nullptr;
4745 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4747 isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this)))
4748 if (FoundIndex == e) {
4753 if (FoundIndex == Add->getNumOperands())
4756 // Create an add with everything but the specified operand.
4757 SmallVector<const SCEV *, 8> Ops;
4758 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
4759 if (i != FoundIndex)
4760 Ops.push_back(Add->getOperand(i));
4761 const SCEV *Accum = getAddExpr(Ops);
4763 // The runtime checks will not be valid if the step amount is
4764 // varying inside the loop.
4765 if (!isLoopInvariant(Accum, L))
4768 // *** Part2: Create the predicates
4770 // Analysis was successful: we have a phi-with-cast pattern for which we
4771 // can return an AddRec expression under the following predicates:
4773 // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
4774 // fits within the truncated type (does not overflow) for i = 0 to n-1.
4775 // P2: An Equal predicate that guarantees that
4776 // Start = (Ext ix (Trunc iy (Start) to ix) to iy)
4777 // P3: An Equal predicate that guarantees that
4778 // Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
4780 // As we next prove, the above predicates guarantee that:
4781 // Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
4784 // More formally, we want to prove that:
4785 // Expr(i+1) = Start + (i+1) * Accum
4786 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
4789 // 1) Expr(0) = Start
4790 // 2) Expr(1) = Start + Accum
4791 // = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
4792 // 3) Induction hypothesis (step i):
4793 // Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum
4797 // = Start + (i+1)*Accum
4798 // = (Start + i*Accum) + Accum
4799 // = Expr(i) + Accum
4800 // = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum
4803 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum
4805 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
4806 // + (Ext ix (Trunc iy (Accum) to ix) to iy)
4807 // + Accum :: from P3
4809 // = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy)
4810 // + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
4812 // = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
4813 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
4815 // By induction, the same applies to all iterations 1<=i<n:
4818 // Create a truncated addrec for which we will add a no overflow check (P1).
4819 const SCEV *StartVal = getSCEV(StartValueV);
4820 const SCEV *PHISCEV =
4821 getAddRecExpr(getTruncateExpr(StartVal, TruncTy),
4822 getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap);
4824 // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
4825 // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
4826 // will be constant.
4828 // If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
4830 if (const auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
4831 SCEVWrapPredicate::IncrementWrapFlags AddedFlags =
4832 Signed ? SCEVWrapPredicate::IncrementNSSW
4833 : SCEVWrapPredicate::IncrementNUSW;
4834 const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags);
4835 Predicates.push_back(AddRecPred);
4838 // Create the Equal Predicates P2,P3:
4840 // It is possible that the predicates P2 and/or P3 are computable at
4841 // compile time due to StartVal and/or Accum being constants.
4842 // If either one is, then we can check that now and escape if either P2
4845 // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
4846 // for each of StartVal and Accum
4847 auto getExtendedExpr = [&](const SCEV *Expr,
4848 bool CreateSignExtend) -> const SCEV * {
4849 assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant");
4850 const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy);
4851 const SCEV *ExtendedExpr =
4852 CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType())
4853 : getZeroExtendExpr(TruncatedExpr, Expr->getType());
4854 return ExtendedExpr;
4858 // ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
4859 // = getExtendedExpr(Expr)
4860 // Determine whether the predicate P: Expr == ExtendedExpr
4861 // is known to be false at compile time
4862 auto PredIsKnownFalse = [&](const SCEV *Expr,
4863 const SCEV *ExtendedExpr) -> bool {
4864 return Expr != ExtendedExpr &&
4865 isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr);
4868 const SCEV *StartExtended = getExtendedExpr(StartVal, Signed);
4869 if (PredIsKnownFalse(StartVal, StartExtended)) {
4870 LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";);
4874 // The Step is always Signed (because the overflow checks are either
4876 const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true);
4877 if (PredIsKnownFalse(Accum, AccumExtended)) {
4878 LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";);
4882 auto AppendPredicate = [&](const SCEV *Expr,
4883 const SCEV *ExtendedExpr) -> void {
4884 if (Expr != ExtendedExpr &&
4885 !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) {
4886 const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr);
4887 LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred);
4888 Predicates.push_back(Pred);
4892 AppendPredicate(StartVal, StartExtended);
4893 AppendPredicate(Accum, AccumExtended);
4895 // *** Part3: Predicates are ready. Now go ahead and create the new addrec in
4896 // which the casts had been folded away. The caller can rewrite SymbolicPHI
4897 // into NewAR if it will also add the runtime overflow checks specified in
4899 auto *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap);
4901 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
4902 std::make_pair(NewAR, Predicates);
4903 // Remember the result of the analysis for this SCEV at this locayyytion.
4904 PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite;
4908 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4909 ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) {
4910 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
4911 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
4915 // Check to see if we already analyzed this PHI.
4916 auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L});
4917 if (I != PredicatedSCEVRewrites.end()) {
4918 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
4920 // Analysis was done before and failed to create an AddRec:
4921 if (Rewrite.first == SymbolicPHI)
4923 // Analysis was done before and succeeded to create an AddRec under
4925 assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec");
4926 assert(!(Rewrite.second).empty() && "Expected to find Predicates");
4930 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
4931 Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
4933 // Record in the cache that the analysis failed
4935 SmallVector<const SCEVPredicate *, 3> Predicates;
4936 PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
4943 // FIXME: This utility is currently required because the Rewriter currently
4944 // does not rewrite this expression:
4945 // {0, +, (sext ix (trunc iy to ix) to iy)}
4946 // into {0, +, %step},
4947 // even when the following Equal predicate exists:
4948 // "%step == (sext ix (trunc iy to ix) to iy)".
4949 bool PredicatedScalarEvolution::areAddRecsEqualWithPreds(
4950 const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const {
4954 auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool {
4955 if (Expr1 != Expr2 && !Preds.implies(SE.getEqualPredicate(Expr1, Expr2)) &&
4956 !Preds.implies(SE.getEqualPredicate(Expr2, Expr1)))
4961 if (!areExprsEqual(AR1->getStart(), AR2->getStart()) ||
4962 !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE)))
4967 /// A helper function for createAddRecFromPHI to handle simple cases.
4969 /// This function tries to find an AddRec expression for the simplest (yet most
4970 /// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
4971 /// If it fails, createAddRecFromPHI will use a more general, but slow,
4972 /// technique for finding the AddRec expression.
4973 const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
4975 Value *StartValueV) {
4976 const Loop *L = LI.getLoopFor(PN->getParent());
4977 assert(L && L->getHeader() == PN->getParent());
4978 assert(BEValueV && StartValueV);
4980 auto BO = MatchBinaryOp(BEValueV, DT);
4984 if (BO->Opcode != Instruction::Add)
4987 const SCEV *Accum = nullptr;
4988 if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
4989 Accum = getSCEV(BO->RHS);
4990 else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
4991 Accum = getSCEV(BO->LHS);
4996 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
4998 Flags = setFlags(Flags, SCEV::FlagNUW);
5000 Flags = setFlags(Flags, SCEV::FlagNSW);
5002 const SCEV *StartVal = getSCEV(StartValueV);
5003 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
5005 ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
5007 // We can add Flags to the post-inc expression only if we
5008 // know that it is *undefined behavior* for BEValueV to
5010 if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
5011 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
5012 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
5017 const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
5018 const Loop *L = LI.getLoopFor(PN->getParent());
5019 if (!L || L->getHeader() != PN->getParent())
5022 // The loop may have multiple entrances or multiple exits; we can analyze
5023 // this phi as an addrec if it has a unique entry value and a unique
5025 Value *BEValueV = nullptr, *StartValueV = nullptr;
5026 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5027 Value *V = PN->getIncomingValue(i);
5028 if (L->contains(PN->getIncomingBlock(i))) {
5031 } else if (BEValueV != V) {
5035 } else if (!StartValueV) {
5037 } else if (StartValueV != V) {
5038 StartValueV = nullptr;
5042 if (!BEValueV || !StartValueV)
5045 assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
5046 "PHI node already processed?");
5048 // First, try to find AddRec expression without creating a fictituos symbolic
5050 if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
5053 // Handle PHI node value symbolically.
5054 const SCEV *SymbolicName = getUnknown(PN);
5055 ValueExprMap.insert({SCEVCallbackVH(PN, this), SymbolicName});
5057 // Using this symbolic name for the PHI, analyze the value coming around
5059 const SCEV *BEValue = getSCEV(BEValueV);
5061 // NOTE: If BEValue is loop invariant, we know that the PHI node just
5062 // has a special value for the first iteration of the loop.
5064 // If the value coming around the backedge is an add with the symbolic
5065 // value we just inserted, then we found a simple induction variable!
5066 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
5067 // If there is a single occurrence of the symbolic value, replace it
5068 // with a recurrence.
5069 unsigned FoundIndex = Add->getNumOperands();
5070 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5071 if (Add->getOperand(i) == SymbolicName)
5072 if (FoundIndex == e) {
5077 if (FoundIndex != Add->getNumOperands()) {
5078 // Create an add with everything but the specified operand.
5079 SmallVector<const SCEV *, 8> Ops;
5080 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5081 if (i != FoundIndex)
5082 Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i),
5084 const SCEV *Accum = getAddExpr(Ops);
5086 // This is not a valid addrec if the step amount is varying each
5087 // loop iteration, but is not itself an addrec in this loop.
5088 if (isLoopInvariant(Accum, L) ||
5089 (isa<SCEVAddRecExpr>(Accum) &&
5090 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
5091 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5093 if (auto BO = MatchBinaryOp(BEValueV, DT)) {
5094 if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
5096 Flags = setFlags(Flags, SCEV::FlagNUW);
5098 Flags = setFlags(Flags, SCEV::FlagNSW);
5100 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
5101 // If the increment is an inbounds GEP, then we know the address
5102 // space cannot be wrapped around. We cannot make any guarantee
5103 // about signed or unsigned overflow because pointers are
5104 // unsigned but we may have a negative index from the base
5105 // pointer. We can guarantee that no unsigned wrap occurs if the
5106 // indices form a positive value.
5107 if (GEP->isInBounds() && GEP->getOperand(0) == PN) {
5108 Flags = setFlags(Flags, SCEV::FlagNW);
5110 const SCEV *Ptr = getSCEV(GEP->getPointerOperand());
5111 if (isKnownPositive(getMinusSCEV(getSCEV(GEP), Ptr)))
5112 Flags = setFlags(Flags, SCEV::FlagNUW);
5115 // We cannot transfer nuw and nsw flags from subtraction
5116 // operations -- sub nuw X, Y is not the same as add nuw X, -Y
5120 const SCEV *StartVal = getSCEV(StartValueV);
5121 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
5123 // Okay, for the entire analysis of this edge we assumed the PHI
5124 // to be symbolic. We now need to go back and purge all of the
5125 // entries for the scalars that use the symbolic expression.
5126 forgetSymbolicName(PN, SymbolicName);
5127 ValueExprMap[SCEVCallbackVH(PN, this)] = PHISCEV;
5129 // We can add Flags to the post-inc expression only if we
5130 // know that it is *undefined behavior* for BEValueV to
5132 if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
5133 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
5134 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
5140 // Otherwise, this could be a loop like this:
5141 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
5142 // In this case, j = {1,+,1} and BEValue is j.
5143 // Because the other in-value of i (0) fits the evolution of BEValue
5144 // i really is an addrec evolution.
5146 // We can generalize this saying that i is the shifted value of BEValue
5147 // by one iteration:
5148 // PHI(f(0), f({1,+,1})) --> f({0,+,1})
5149 const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
5150 const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false);
5151 if (Shifted != getCouldNotCompute() &&
5152 Start != getCouldNotCompute()) {
5153 const SCEV *StartVal = getSCEV(StartValueV);
5154 if (Start == StartVal) {
5155 // Okay, for the entire analysis of this edge we assumed the PHI
5156 // to be symbolic. We now need to go back and purge all of the
5157 // entries for the scalars that use the symbolic expression.
5158 forgetSymbolicName(PN, SymbolicName);
5159 ValueExprMap[SCEVCallbackVH(PN, this)] = Shifted;
5165 // Remove the temporary PHI node SCEV that has been inserted while intending
5166 // to create an AddRecExpr for this PHI node. We can not keep this temporary
5167 // as it will prevent later (possibly simpler) SCEV expressions to be added
5168 // to the ValueExprMap.
5169 eraseValueFromMap(PN);
5174 // Checks if the SCEV S is available at BB. S is considered available at BB
5175 // if S can be materialized at BB without introducing a fault.
5176 static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S,
5178 struct CheckAvailable {
5179 bool TraversalDone = false;
5180 bool Available = true;
5182 const Loop *L = nullptr; // The loop BB is in (can be nullptr)
5183 BasicBlock *BB = nullptr;
5186 CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT)
5187 : L(L), BB(BB), DT(DT) {}
5189 bool setUnavailable() {
5190 TraversalDone = true;
5195 bool follow(const SCEV *S) {
5196 switch (S->getSCEVType()) {
5197 case scConstant: case scTruncate: case scZeroExtend: case scSignExtend:
5198 case scAddExpr: case scMulExpr: case scUMaxExpr: case scSMaxExpr:
5199 // These expressions are available if their operand(s) is/are.
5202 case scAddRecExpr: {
5203 // We allow add recurrences that are on the loop BB is in, or some
5204 // outer loop. This guarantees availability because the value of the
5205 // add recurrence at BB is simply the "current" value of the induction
5206 // variable. We can relax this in the future; for instance an add
5207 // recurrence on a sibling dominating loop is also available at BB.
5208 const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop();
5209 if (L && (ARLoop == L || ARLoop->contains(L)))
5212 return setUnavailable();
5216 // For SCEVUnknown, we check for simple dominance.
5217 const auto *SU = cast<SCEVUnknown>(S);
5218 Value *V = SU->getValue();
5220 if (isa<Argument>(V))
5223 if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB))
5226 return setUnavailable();
5230 case scCouldNotCompute:
5231 // We do not try to smart about these at all.
5232 return setUnavailable();
5234 llvm_unreachable("switch should be fully covered!");
5237 bool isDone() { return TraversalDone; }
5240 CheckAvailable CA(L, BB, DT);
5241 SCEVTraversal<CheckAvailable> ST(CA);
5244 return CA.Available;
5247 // Try to match a control flow sequence that branches out at BI and merges back
5248 // at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful
5250 static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge,
5251 Value *&C, Value *&LHS, Value *&RHS) {
5252 C = BI->getCondition();
5254 BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0));
5255 BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1));
5257 if (!LeftEdge.isSingleEdge())
5260 assert(RightEdge.isSingleEdge() && "Follows from LeftEdge.isSingleEdge()");
5262 Use &LeftUse = Merge->getOperandUse(0);
5263 Use &RightUse = Merge->getOperandUse(1);
5265 if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) {
5271 if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) {
5280 const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
5282 [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); };
5283 if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) {
5284 const Loop *L = LI.getLoopFor(PN->getParent());
5286 // We don't want to break LCSSA, even in a SCEV expression tree.
5287 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
5288 if (LI.getLoopFor(PN->getIncomingBlock(i)) != L)
5293 // br %cond, label %left, label %right
5299 // V = phi [ %x, %left ], [ %y, %right ]
5301 // as "select %cond, %x, %y"
5303 BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
5304 assert(IDom && "At least the entry block should dominate PN");
5306 auto *BI = dyn_cast<BranchInst>(IDom->getTerminator());
5307 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
5309 if (BI && BI->isConditional() &&
5310 BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) &&
5311 IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) &&
5312 IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent()))
5313 return createNodeForSelectOrPHI(PN, Cond, LHS, RHS);
5319 const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
5320 if (const SCEV *S = createAddRecFromPHI(PN))
5323 if (const SCEV *S = createNodeFromSelectLikePHI(PN))
5326 // If the PHI has a single incoming value, follow that value, unless the
5327 // PHI's incoming blocks are in a different loop, in which case doing so
5328 // risks breaking LCSSA form. Instcombine would normally zap these, but
5329 // it doesn't have DominatorTree information, so it may miss cases.
5330 if (Value *V = SimplifyInstruction(PN, {getDataLayout(), &TLI, &DT, &AC}))
5331 if (LI.replacementPreservesLCSSAForm(PN, V))
5334 // If it's not a loop phi, we can't handle it yet.
5335 return getUnknown(PN);
5338 const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Instruction *I,
5342 // Handle "constant" branch or select. This can occur for instance when a
5343 // loop pass transforms an inner loop and moves on to process the outer loop.
5344 if (auto *CI = dyn_cast<ConstantInt>(Cond))
5345 return getSCEV(CI->isOne() ? TrueVal : FalseVal);
5347 // Try to match some simple smax or umax patterns.
5348 auto *ICI = dyn_cast<ICmpInst>(Cond);
5350 return getUnknown(I);
5352 Value *LHS = ICI->getOperand(0);
5353 Value *RHS = ICI->getOperand(1);
5355 switch (ICI->getPredicate()) {
5356 case ICmpInst::ICMP_SLT:
5357 case ICmpInst::ICMP_SLE:
5358 std::swap(LHS, RHS);
5360 case ICmpInst::ICMP_SGT:
5361 case ICmpInst::ICMP_SGE:
5362 // a >s b ? a+x : b+x -> smax(a, b)+x
5363 // a >s b ? b+x : a+x -> smin(a, b)+x
5364 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
5365 const SCEV *LS = getNoopOrSignExtend(getSCEV(LHS), I->getType());
5366 const SCEV *RS = getNoopOrSignExtend(getSCEV(RHS), I->getType());
5367 const SCEV *LA = getSCEV(TrueVal);
5368 const SCEV *RA = getSCEV(FalseVal);
5369 const SCEV *LDiff = getMinusSCEV(LA, LS);
5370 const SCEV *RDiff = getMinusSCEV(RA, RS);
5372 return getAddExpr(getSMaxExpr(LS, RS), LDiff);
5373 LDiff = getMinusSCEV(LA, RS);
5374 RDiff = getMinusSCEV(RA, LS);
5376 return getAddExpr(getSMinExpr(LS, RS), LDiff);
5379 case ICmpInst::ICMP_ULT:
5380 case ICmpInst::ICMP_ULE:
5381 std::swap(LHS, RHS);
5383 case ICmpInst::ICMP_UGT:
5384 case ICmpInst::ICMP_UGE:
5385 // a >u b ? a+x : b+x -> umax(a, b)+x
5386 // a >u b ? b+x : a+x -> umin(a, b)+x
5387 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType())) {
5388 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5389 const SCEV *RS = getNoopOrZeroExtend(getSCEV(RHS), I->getType());
5390 const SCEV *LA = getSCEV(TrueVal);
5391 const SCEV *RA = getSCEV(FalseVal);
5392 const SCEV *LDiff = getMinusSCEV(LA, LS);
5393 const SCEV *RDiff = getMinusSCEV(RA, RS);
5395 return getAddExpr(getUMaxExpr(LS, RS), LDiff);
5396 LDiff = getMinusSCEV(LA, RS);
5397 RDiff = getMinusSCEV(RA, LS);
5399 return getAddExpr(getUMinExpr(LS, RS), LDiff);
5402 case ICmpInst::ICMP_NE:
5403 // n != 0 ? n+x : 1+x -> umax(n, 1)+x
5404 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5405 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5406 const SCEV *One = getOne(I->getType());
5407 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5408 const SCEV *LA = getSCEV(TrueVal);
5409 const SCEV *RA = getSCEV(FalseVal);
5410 const SCEV *LDiff = getMinusSCEV(LA, LS);
5411 const SCEV *RDiff = getMinusSCEV(RA, One);
5413 return getAddExpr(getUMaxExpr(One, LS), LDiff);
5416 case ICmpInst::ICMP_EQ:
5417 // n == 0 ? 1+x : n+x -> umax(n, 1)+x
5418 if (getTypeSizeInBits(LHS->getType()) <= getTypeSizeInBits(I->getType()) &&
5419 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
5420 const SCEV *One = getOne(I->getType());
5421 const SCEV *LS = getNoopOrZeroExtend(getSCEV(LHS), I->getType());
5422 const SCEV *LA = getSCEV(TrueVal);
5423 const SCEV *RA = getSCEV(FalseVal);
5424 const SCEV *LDiff = getMinusSCEV(LA, One);
5425 const SCEV *RDiff = getMinusSCEV(RA, LS);
5427 return getAddExpr(getUMaxExpr(One, LS), LDiff);
5434 return getUnknown(I);
5437 /// Expand GEP instructions into add and multiply operations. This allows them
5438 /// to be analyzed by regular SCEV code.
5439 const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
5440 // Don't attempt to analyze GEPs over unsized objects.
5441 if (!GEP->getSourceElementType()->isSized())
5442 return getUnknown(GEP);
5444 SmallVector<const SCEV *, 4> IndexExprs;
5445 for (auto Index = GEP->idx_begin(); Index != GEP->idx_end(); ++Index)
5446 IndexExprs.push_back(getSCEV(*Index));
5447 return getGEPExpr(GEP, IndexExprs);
5450 uint32_t ScalarEvolution::GetMinTrailingZerosImpl(const SCEV *S) {
5451 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
5452 return C->getAPInt().countTrailingZeros();
5454 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(S))
5455 return std::min(GetMinTrailingZeros(T->getOperand()),
5456 (uint32_t)getTypeSizeInBits(T->getType()));
5458 if (const SCEVZeroExtendExpr *E = dyn_cast<SCEVZeroExtendExpr>(S)) {
5459 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
5460 return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5461 ? getTypeSizeInBits(E->getType())
5465 if (const SCEVSignExtendExpr *E = dyn_cast<SCEVSignExtendExpr>(S)) {
5466 uint32_t OpRes = GetMinTrailingZeros(E->getOperand());
5467 return OpRes == getTypeSizeInBits(E->getOperand()->getType())
5468 ? getTypeSizeInBits(E->getType())
5472 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
5473 // The result is the min of all operands results.
5474 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
5475 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
5476 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
5480 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(S)) {
5481 // The result is the sum of all operands results.
5482 uint32_t SumOpRes = GetMinTrailingZeros(M->getOperand(0));
5483 uint32_t BitWidth = getTypeSizeInBits(M->getType());
5484 for (unsigned i = 1, e = M->getNumOperands();
5485 SumOpRes != BitWidth && i != e; ++i)
5487 std::min(SumOpRes + GetMinTrailingZeros(M->getOperand(i)), BitWidth);
5491 if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(S)) {
5492 // The result is the min of all operands results.
5493 uint32_t MinOpRes = GetMinTrailingZeros(A->getOperand(0));
5494 for (unsigned i = 1, e = A->getNumOperands(); MinOpRes && i != e; ++i)
5495 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(A->getOperand(i)));
5499 if (const SCEVSMaxExpr *M = dyn_cast<SCEVSMaxExpr>(S)) {
5500 // The result is the min of all operands results.
5501 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
5502 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
5503 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
5507 if (const SCEVUMaxExpr *M = dyn_cast<SCEVUMaxExpr>(S)) {
5508 // The result is the min of all operands results.
5509 uint32_t MinOpRes = GetMinTrailingZeros(M->getOperand(0));
5510 for (unsigned i = 1, e = M->getNumOperands(); MinOpRes && i != e; ++i)
5511 MinOpRes = std::min(MinOpRes, GetMinTrailingZeros(M->getOperand(i)));
5515 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
5516 // For a SCEVUnknown, ask ValueTracking.
5517 KnownBits Known = computeKnownBits(U->getValue(), getDataLayout(), 0, &AC, nullptr, &DT);
5518 return Known.countMinTrailingZeros();
5525 uint32_t ScalarEvolution::GetMinTrailingZeros(const SCEV *S) {
5526 auto I = MinTrailingZerosCache.find(S);
5527 if (I != MinTrailingZerosCache.end())
5530 uint32_t Result = GetMinTrailingZerosImpl(S);
5531 auto InsertPair = MinTrailingZerosCache.insert({S, Result});
5532 assert(InsertPair.second && "Should insert a new key");
5533 return InsertPair.first->second;
5536 /// Helper method to assign a range to V from metadata present in the IR.
5537 static Optional<ConstantRange> GetRangeFromMetadata(Value *V) {
5538 if (Instruction *I = dyn_cast<Instruction>(V))
5539 if (MDNode *MD = I->getMetadata(LLVMContext::MD_range))
5540 return getConstantRangeFromMetadata(*MD);
5545 /// Determine the range for a particular SCEV. If SignHint is
5546 /// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
5547 /// with a "cleaner" unsigned (resp. signed) representation.
5548 const ConstantRange &
5549 ScalarEvolution::getRangeRef(const SCEV *S,
5550 ScalarEvolution::RangeSignHint SignHint) {
5551 DenseMap<const SCEV *, ConstantRange> &Cache =
5552 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
5555 // See if we've computed this range already.
5556 DenseMap<const SCEV *, ConstantRange>::iterator I = Cache.find(S);
5557 if (I != Cache.end())
5560 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
5561 return setRange(C, SignHint, ConstantRange(C->getAPInt()));
5563 unsigned BitWidth = getTypeSizeInBits(S->getType());
5564 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
5566 // If the value has known zeros, the maximum value will have those known zeros
5568 uint32_t TZ = GetMinTrailingZeros(S);
5570 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED)
5571 ConservativeResult =
5572 ConstantRange(APInt::getMinValue(BitWidth),
5573 APInt::getMaxValue(BitWidth).lshr(TZ).shl(TZ) + 1);
5575 ConservativeResult = ConstantRange(
5576 APInt::getSignedMinValue(BitWidth),
5577 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
5580 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S)) {
5581 ConstantRange X = getRangeRef(Add->getOperand(0), SignHint);
5582 for (unsigned i = 1, e = Add->getNumOperands(); i != e; ++i)
5583 X = X.add(getRangeRef(Add->getOperand(i), SignHint));
5584 return setRange(Add, SignHint, ConservativeResult.intersectWith(X));
5587 if (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(S)) {
5588 ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint);
5589 for (unsigned i = 1, e = Mul->getNumOperands(); i != e; ++i)
5590 X = X.multiply(getRangeRef(Mul->getOperand(i), SignHint));
5591 return setRange(Mul, SignHint, ConservativeResult.intersectWith(X));
5594 if (const SCEVSMaxExpr *SMax = dyn_cast<SCEVSMaxExpr>(S)) {
5595 ConstantRange X = getRangeRef(SMax->getOperand(0), SignHint);
5596 for (unsigned i = 1, e = SMax->getNumOperands(); i != e; ++i)
5597 X = X.smax(getRangeRef(SMax->getOperand(i), SignHint));
5598 return setRange(SMax, SignHint, ConservativeResult.intersectWith(X));
5601 if (const SCEVUMaxExpr *UMax = dyn_cast<SCEVUMaxExpr>(S)) {
5602 ConstantRange X = getRangeRef(UMax->getOperand(0), SignHint);
5603 for (unsigned i = 1, e = UMax->getNumOperands(); i != e; ++i)
5604 X = X.umax(getRangeRef(UMax->getOperand(i), SignHint));
5605 return setRange(UMax, SignHint, ConservativeResult.intersectWith(X));
5608 if (const SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(S)) {
5609 ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint);
5610 ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint);
5611 return setRange(UDiv, SignHint,
5612 ConservativeResult.intersectWith(X.udiv(Y)));
5615 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S)) {
5616 ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint);
5617 return setRange(ZExt, SignHint,
5618 ConservativeResult.intersectWith(X.zeroExtend(BitWidth)));
5621 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S)) {
5622 ConstantRange X = getRangeRef(SExt->getOperand(), SignHint);
5623 return setRange(SExt, SignHint,
5624 ConservativeResult.intersectWith(X.signExtend(BitWidth)));
5627 if (const SCEVTruncateExpr *Trunc = dyn_cast<SCEVTruncateExpr>(S)) {
5628 ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint);
5629 return setRange(Trunc, SignHint,
5630 ConservativeResult.intersectWith(X.truncate(BitWidth)));
5633 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
5634 // If there's no unsigned wrap, the value will never be less than its
5636 if (AddRec->hasNoUnsignedWrap())
5637 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(AddRec->getStart()))
5638 if (!C->getValue()->isZero())
5639 ConservativeResult = ConservativeResult.intersectWith(
5640 ConstantRange(C->getAPInt(), APInt(BitWidth, 0)));
5642 // If there's no signed wrap, and all the operands have the same sign or
5643 // zero, the value won't ever change sign.
5644 if (AddRec->hasNoSignedWrap()) {
5645 bool AllNonNeg = true;
5646 bool AllNonPos = true;
5647 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
5648 if (!isKnownNonNegative(AddRec->getOperand(i))) AllNonNeg = false;
5649 if (!isKnownNonPositive(AddRec->getOperand(i))) AllNonPos = false;
5652 ConservativeResult = ConservativeResult.intersectWith(
5653 ConstantRange(APInt(BitWidth, 0),
5654 APInt::getSignedMinValue(BitWidth)));
5656 ConservativeResult = ConservativeResult.intersectWith(
5657 ConstantRange(APInt::getSignedMinValue(BitWidth),
5658 APInt(BitWidth, 1)));
5661 // TODO: non-affine addrec
5662 if (AddRec->isAffine()) {
5663 const SCEV *MaxBECount = getMaxBackedgeTakenCount(AddRec->getLoop());
5664 if (!isa<SCEVCouldNotCompute>(MaxBECount) &&
5665 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth) {
5666 auto RangeFromAffine = getRangeForAffineAR(
5667 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
5669 if (!RangeFromAffine.isFullSet())
5670 ConservativeResult =
5671 ConservativeResult.intersectWith(RangeFromAffine);
5673 auto RangeFromFactoring = getRangeViaFactoring(
5674 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount,
5676 if (!RangeFromFactoring.isFullSet())
5677 ConservativeResult =
5678 ConservativeResult.intersectWith(RangeFromFactoring);
5682 return setRange(AddRec, SignHint, std::move(ConservativeResult));
5685 if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
5686 // Check if the IR explicitly contains !range metadata.
5687 Optional<ConstantRange> MDRange = GetRangeFromMetadata(U->getValue());
5688 if (MDRange.hasValue())
5689 ConservativeResult = ConservativeResult.intersectWith(MDRange.getValue());
5691 // Split here to avoid paying the compile-time cost of calling both
5692 // computeKnownBits and ComputeNumSignBits. This restriction can be lifted
5694 const DataLayout &DL = getDataLayout();
5695 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
5696 // For a SCEVUnknown, ask ValueTracking.
5697 KnownBits Known = computeKnownBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
5698 if (Known.One != ~Known.Zero + 1)
5699 ConservativeResult =
5700 ConservativeResult.intersectWith(ConstantRange(Known.One,
5703 assert(SignHint == ScalarEvolution::HINT_RANGE_SIGNED &&
5704 "generalize as needed!");
5705 unsigned NS = ComputeNumSignBits(U->getValue(), DL, 0, &AC, nullptr, &DT);
5707 ConservativeResult = ConservativeResult.intersectWith(
5708 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
5709 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1));
5712 // A range of Phi is a subset of union of all ranges of its input.
5713 if (const PHINode *Phi = dyn_cast<PHINode>(U->getValue())) {
5714 // Make sure that we do not run over cycled Phis.
5715 if (PendingPhiRanges.insert(Phi).second) {
5716 ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false);
5717 for (auto &Op : Phi->operands()) {
5718 auto OpRange = getRangeRef(getSCEV(Op), SignHint);
5719 RangeFromOps = RangeFromOps.unionWith(OpRange);
5720 // No point to continue if we already have a full set.
5721 if (RangeFromOps.isFullSet())
5724 ConservativeResult = ConservativeResult.intersectWith(RangeFromOps);
5725 bool Erased = PendingPhiRanges.erase(Phi);
5726 assert(Erased && "Failed to erase Phi properly?");
5731 return setRange(U, SignHint, std::move(ConservativeResult));
5734 return setRange(S, SignHint, std::move(ConservativeResult));
5737 // Given a StartRange, Step and MaxBECount for an expression compute a range of
5738 // values that the expression can take. Initially, the expression has a value
5739 // from StartRange and then is changed by Step up to MaxBECount times. Signed
5740 // argument defines if we treat Step as signed or unsigned.
5741 static ConstantRange getRangeForAffineARHelper(APInt Step,
5742 const ConstantRange &StartRange,
5743 const APInt &MaxBECount,
5744 unsigned BitWidth, bool Signed) {
5745 // If either Step or MaxBECount is 0, then the expression won't change, and we
5746 // just need to return the initial range.
5747 if (Step == 0 || MaxBECount == 0)
5750 // If we don't know anything about the initial value (i.e. StartRange is
5751 // FullRange), then we don't know anything about the final range either.
5752 // Return FullRange.
5753 if (StartRange.isFullSet())
5754 return ConstantRange(BitWidth, /* isFullSet = */ true);
5756 // If Step is signed and negative, then we use its absolute value, but we also
5757 // note that we're moving in the opposite direction.
5758 bool Descending = Signed && Step.isNegative();
5761 // This is correct even for INT_SMIN. Let's look at i8 to illustrate this:
5762 // abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128.
5763 // This equations hold true due to the well-defined wrap-around behavior of
5767 // Check if Offset is more than full span of BitWidth. If it is, the
5768 // expression is guaranteed to overflow.
5769 if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount))
5770 return ConstantRange(BitWidth, /* isFullSet = */ true);
5772 // Offset is by how much the expression can change. Checks above guarantee no
5774 APInt Offset = Step * MaxBECount;
5776 // Minimum value of the final range will match the minimal value of StartRange
5777 // if the expression is increasing and will be decreased by Offset otherwise.
5778 // Maximum value of the final range will match the maximal value of StartRange
5779 // if the expression is decreasing and will be increased by Offset otherwise.
5780 APInt StartLower = StartRange.getLower();
5781 APInt StartUpper = StartRange.getUpper() - 1;
5782 APInt MovedBoundary = Descending ? (StartLower - std::move(Offset))
5783 : (StartUpper + std::move(Offset));
5785 // It's possible that the new minimum/maximum value will fall into the initial
5786 // range (due to wrap around). This means that the expression can take any
5787 // value in this bitwidth, and we have to return full range.
5788 if (StartRange.contains(MovedBoundary))
5789 return ConstantRange(BitWidth, /* isFullSet = */ true);
5792 Descending ? std::move(MovedBoundary) : std::move(StartLower);
5794 Descending ? std::move(StartUpper) : std::move(MovedBoundary);
5797 // If we end up with full range, return a proper full range.
5798 if (NewLower == NewUpper)
5799 return ConstantRange(BitWidth, /* isFullSet = */ true);
5801 // No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
5802 return ConstantRange(std::move(NewLower), std::move(NewUpper));
5805 ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start,
5807 const SCEV *MaxBECount,
5808 unsigned BitWidth) {
5809 assert(!isa<SCEVCouldNotCompute>(MaxBECount) &&
5810 getTypeSizeInBits(MaxBECount->getType()) <= BitWidth &&
5813 MaxBECount = getNoopOrZeroExtend(MaxBECount, Start->getType());
5814 APInt MaxBECountValue = getUnsignedRangeMax(MaxBECount);
5816 // First, consider step signed.
5817 ConstantRange StartSRange = getSignedRange(Start);
5818 ConstantRange StepSRange = getSignedRange(Step);
5820 // If Step can be both positive and negative, we need to find ranges for the
5821 // maximum absolute step values in both directions and union them.
5823 getRangeForAffineARHelper(StepSRange.getSignedMin(), StartSRange,
5824 MaxBECountValue, BitWidth, /* Signed = */ true);
5825 SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(),
5826 StartSRange, MaxBECountValue,
5827 BitWidth, /* Signed = */ true));
5829 // Next, consider step unsigned.
5830 ConstantRange UR = getRangeForAffineARHelper(
5831 getUnsignedRangeMax(Step), getUnsignedRange(Start),
5832 MaxBECountValue, BitWidth, /* Signed = */ false);
5834 // Finally, intersect signed and unsigned ranges.
5835 return SR.intersectWith(UR);
5838 ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
5840 const SCEV *MaxBECount,
5841 unsigned BitWidth) {
5842 // RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
5843 // == RangeOf({A,+,P}) union RangeOf({B,+,Q})
5845 struct SelectPattern {
5846 Value *Condition = nullptr;
5850 explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth,
5852 Optional<unsigned> CastOp;
5853 APInt Offset(BitWidth, 0);
5855 assert(SE.getTypeSizeInBits(S->getType()) == BitWidth &&
5858 // Peel off a constant offset:
5859 if (auto *SA = dyn_cast<SCEVAddExpr>(S)) {
5860 // In the future we could consider being smarter here and handle
5861 // {Start+Step,+,Step} too.
5862 if (SA->getNumOperands() != 2 || !isa<SCEVConstant>(SA->getOperand(0)))
5865 Offset = cast<SCEVConstant>(SA->getOperand(0))->getAPInt();
5866 S = SA->getOperand(1);
5869 // Peel off a cast operation
5870 if (auto *SCast = dyn_cast<SCEVCastExpr>(S)) {
5871 CastOp = SCast->getSCEVType();
5872 S = SCast->getOperand();
5875 using namespace llvm::PatternMatch;
5877 auto *SU = dyn_cast<SCEVUnknown>(S);
5878 const APInt *TrueVal, *FalseVal;
5880 !match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal),
5881 m_APInt(FalseVal)))) {
5882 Condition = nullptr;
5886 TrueValue = *TrueVal;
5887 FalseValue = *FalseVal;
5889 // Re-apply the cast we peeled off earlier
5890 if (CastOp.hasValue())
5893 llvm_unreachable("Unknown SCEV cast type!");
5896 TrueValue = TrueValue.trunc(BitWidth);
5897 FalseValue = FalseValue.trunc(BitWidth);
5900 TrueValue = TrueValue.zext(BitWidth);
5901 FalseValue = FalseValue.zext(BitWidth);
5904 TrueValue = TrueValue.sext(BitWidth);
5905 FalseValue = FalseValue.sext(BitWidth);
5909 // Re-apply the constant offset we peeled off earlier
5910 TrueValue += Offset;
5911 FalseValue += Offset;
5914 bool isRecognized() { return Condition != nullptr; }
5917 SelectPattern StartPattern(*this, BitWidth, Start);
5918 if (!StartPattern.isRecognized())
5919 return ConstantRange(BitWidth, /* isFullSet = */ true);
5921 SelectPattern StepPattern(*this, BitWidth, Step);
5922 if (!StepPattern.isRecognized())
5923 return ConstantRange(BitWidth, /* isFullSet = */ true);
5925 if (StartPattern.Condition != StepPattern.Condition) {
5926 // We don't handle this case today; but we could, by considering four
5927 // possibilities below instead of two. I'm not sure if there are cases where
5928 // that will help over what getRange already does, though.
5929 return ConstantRange(BitWidth, /* isFullSet = */ true);
5932 // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
5933 // construct arbitrary general SCEV expressions here. This function is called
5934 // from deep in the call stack, and calling getSCEV (on a sext instruction,
5935 // say) can end up caching a suboptimal value.
5937 // FIXME: without the explicit `this` receiver below, MSVC errors out with
5938 // C2352 and C2512 (otherwise it isn't needed).
5940 const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue);
5941 const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue);
5942 const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue);
5943 const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
5945 ConstantRange TrueRange =
5946 this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount, BitWidth);
5947 ConstantRange FalseRange =
5948 this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount, BitWidth);
5950 return TrueRange.unionWith(FalseRange);
5953 SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) {
5954 if (isa<ConstantExpr>(V)) return SCEV::FlagAnyWrap;
5955 const BinaryOperator *BinOp = cast<BinaryOperator>(V);
5957 // Return early if there are no flags to propagate to the SCEV.
5958 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5959 if (BinOp->hasNoUnsignedWrap())
5960 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNUW);
5961 if (BinOp->hasNoSignedWrap())
5962 Flags = ScalarEvolution::setFlags(Flags, SCEV::FlagNSW);
5963 if (Flags == SCEV::FlagAnyWrap)
5964 return SCEV::FlagAnyWrap;
5966 return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap;
5969 bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
5970 // Here we check that I is in the header of the innermost loop containing I,
5971 // since we only deal with instructions in the loop header. The actual loop we
5972 // need to check later will come from an add recurrence, but getting that
5973 // requires computing the SCEV of the operands, which can be expensive. This
5974 // check we can do cheaply to rule out some cases early.
5975 Loop *InnermostContainingLoop = LI.getLoopFor(I->getParent());
5976 if (InnermostContainingLoop == nullptr ||
5977 InnermostContainingLoop->getHeader() != I->getParent())
5980 // Only proceed if we can prove that I does not yield poison.
5981 if (!programUndefinedIfFullPoison(I))
5984 // At this point we know that if I is executed, then it does not wrap
5985 // according to at least one of NSW or NUW. If I is not executed, then we do
5986 // not know if the calculation that I represents would wrap. Multiple
5987 // instructions can map to the same SCEV. If we apply NSW or NUW from I to
5988 // the SCEV, we must guarantee no wrapping for that SCEV also when it is
5989 // derived from other instructions that map to the same SCEV. We cannot make
5990 // that guarantee for cases where I is not executed. So we need to find the
5991 // loop that I is considered in relation to and prove that I is executed for
5992 // every iteration of that loop. That implies that the value that I
5993 // calculates does not wrap anywhere in the loop, so then we can apply the
5994 // flags to the SCEV.
5996 // We check isLoopInvariant to disambiguate in case we are adding recurrences
5997 // from different loops, so that we know which loop to prove that I is
5999 for (unsigned OpIndex = 0; OpIndex < I->getNumOperands(); ++OpIndex) {
6000 // I could be an extractvalue from a call to an overflow intrinsic.
6001 // TODO: We can do better here in some cases.
6002 if (!isSCEVable(I->getOperand(OpIndex)->getType()))
6004 const SCEV *Op = getSCEV(I->getOperand(OpIndex));
6005 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
6006 bool AllOtherOpsLoopInvariant = true;
6007 for (unsigned OtherOpIndex = 0; OtherOpIndex < I->getNumOperands();
6009 if (OtherOpIndex != OpIndex) {
6010 const SCEV *OtherOp = getSCEV(I->getOperand(OtherOpIndex));
6011 if (!isLoopInvariant(OtherOp, AddRec->getLoop())) {
6012 AllOtherOpsLoopInvariant = false;
6017 if (AllOtherOpsLoopInvariant &&
6018 isGuaranteedToExecuteForEveryIteration(I, AddRec->getLoop()))
6025 bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) {
6026 // If we know that \c I can never be poison period, then that's enough.
6027 if (isSCEVExprNeverPoison(I))
6030 // For an add recurrence specifically, we assume that infinite loops without
6031 // side effects are undefined behavior, and then reason as follows:
6033 // If the add recurrence is poison in any iteration, it is poison on all
6034 // future iterations (since incrementing poison yields poison). If the result
6035 // of the add recurrence is fed into the loop latch condition and the loop
6036 // does not contain any throws or exiting blocks other than the latch, we now
6037 // have the ability to "choose" whether the backedge is taken or not (by
6038 // choosing a sufficiently evil value for the poison feeding into the branch)
6039 // for every iteration including and after the one in which \p I first became
6040 // poison. There are two possibilities (let's call the iteration in which \p
6041 // I first became poison as K):
6043 // 1. In the set of iterations including and after K, the loop body executes
6044 // no side effects. In this case executing the backege an infinte number
6045 // of times will yield undefined behavior.
6047 // 2. In the set of iterations including and after K, the loop body executes
6048 // at least one side effect. In this case, that specific instance of side
6049 // effect is control dependent on poison, which also yields undefined
6052 auto *ExitingBB = L->getExitingBlock();
6053 auto *LatchBB = L->getLoopLatch();
6054 if (!ExitingBB || !LatchBB || ExitingBB != LatchBB)
6057 SmallPtrSet<const Instruction *, 16> Pushed;
6058 SmallVector<const Instruction *, 8> PoisonStack;
6060 // We start by assuming \c I, the post-inc add recurrence, is poison. Only
6061 // things that are known to be fully poison under that assumption go on the
6064 PoisonStack.push_back(I);
6066 bool LatchControlDependentOnPoison = false;
6067 while (!PoisonStack.empty() && !LatchControlDependentOnPoison) {
6068 const Instruction *Poison = PoisonStack.pop_back_val();
6070 for (auto *PoisonUser : Poison->users()) {
6071 if (propagatesFullPoison(cast<Instruction>(PoisonUser))) {
6072 if (Pushed.insert(cast<Instruction>(PoisonUser)).second)
6073 PoisonStack.push_back(cast<Instruction>(PoisonUser));
6074 } else if (auto *BI = dyn_cast<BranchInst>(PoisonUser)) {
6075 assert(BI->isConditional() && "Only possibility!");
6076 if (BI->getParent() == LatchBB) {
6077 LatchControlDependentOnPoison = true;
6084 return LatchControlDependentOnPoison && loopHasNoAbnormalExits(L);
6087 ScalarEvolution::LoopProperties
6088 ScalarEvolution::getLoopProperties(const Loop *L) {
6089 using LoopProperties = ScalarEvolution::LoopProperties;
6091 auto Itr = LoopPropertiesCache.find(L);
6092 if (Itr == LoopPropertiesCache.end()) {
6093 auto HasSideEffects = [](Instruction *I) {
6094 if (auto *SI = dyn_cast<StoreInst>(I))
6095 return !SI->isSimple();
6097 return I->mayHaveSideEffects();
6100 LoopProperties LP = {/* HasNoAbnormalExits */ true,
6101 /*HasNoSideEffects*/ true};
6103 for (auto *BB : L->getBlocks())
6104 for (auto &I : *BB) {
6105 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
6106 LP.HasNoAbnormalExits = false;
6107 if (HasSideEffects(&I))
6108 LP.HasNoSideEffects = false;
6109 if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects)
6110 break; // We're already as pessimistic as we can get.
6113 auto InsertPair = LoopPropertiesCache.insert({L, LP});
6114 assert(InsertPair.second && "We just checked!");
6115 Itr = InsertPair.first;
6121 const SCEV *ScalarEvolution::createSCEV(Value *V) {
6122 if (!isSCEVable(V->getType()))
6123 return getUnknown(V);
6125 if (Instruction *I = dyn_cast<Instruction>(V)) {
6126 // Don't attempt to analyze instructions in blocks that aren't
6127 // reachable. Such instructions don't matter, and they aren't required
6128 // to obey basic rules for definitions dominating uses which this
6129 // analysis depends on.
6130 if (!DT.isReachableFromEntry(I->getParent()))
6131 return getUnknown(V);
6132 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
6133 return getConstant(CI);
6134 else if (isa<ConstantPointerNull>(V))
6135 return getZero(V->getType());
6136 else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
6137 return GA->isInterposable() ? getUnknown(V) : getSCEV(GA->getAliasee());
6138 else if (!isa<ConstantExpr>(V))
6139 return getUnknown(V);
6141 Operator *U = cast<Operator>(V);
6142 if (auto BO = MatchBinaryOp(U, DT)) {
6143 switch (BO->Opcode) {
6144 case Instruction::Add: {
6145 // The simple thing to do would be to just call getSCEV on both operands
6146 // and call getAddExpr with the result. However if we're looking at a
6147 // bunch of things all added together, this can be quite inefficient,
6148 // because it leads to N-1 getAddExpr calls for N ultimate operands.
6149 // Instead, gather up all the operands and make a single getAddExpr call.
6150 // LLVM IR canonical form means we need only traverse the left operands.
6151 SmallVector<const SCEV *, 4> AddOps;
6154 if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
6155 AddOps.push_back(OpSCEV);
6159 // If a NUW or NSW flag can be applied to the SCEV for this
6160 // addition, then compute the SCEV for this addition by itself
6161 // with a separate call to getAddExpr. We need to do that
6162 // instead of pushing the operands of the addition onto AddOps,
6163 // since the flags are only known to apply to this particular
6164 // addition - they may not apply to other additions that can be
6165 // formed with operands from AddOps.
6166 const SCEV *RHS = getSCEV(BO->RHS);
6167 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
6168 if (Flags != SCEV::FlagAnyWrap) {
6169 const SCEV *LHS = getSCEV(BO->LHS);
6170 if (BO->Opcode == Instruction::Sub)
6171 AddOps.push_back(getMinusSCEV(LHS, RHS, Flags));
6173 AddOps.push_back(getAddExpr(LHS, RHS, Flags));
6178 if (BO->Opcode == Instruction::Sub)
6179 AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS)));
6181 AddOps.push_back(getSCEV(BO->RHS));
6183 auto NewBO = MatchBinaryOp(BO->LHS, DT);
6184 if (!NewBO || (NewBO->Opcode != Instruction::Add &&
6185 NewBO->Opcode != Instruction::Sub)) {
6186 AddOps.push_back(getSCEV(BO->LHS));
6192 return getAddExpr(AddOps);
6195 case Instruction::Mul: {
6196 SmallVector<const SCEV *, 4> MulOps;
6199 if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
6200 MulOps.push_back(OpSCEV);
6204 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
6205 if (Flags != SCEV::FlagAnyWrap) {
6207 getMulExpr(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags));
6212 MulOps.push_back(getSCEV(BO->RHS));
6213 auto NewBO = MatchBinaryOp(BO->LHS, DT);
6214 if (!NewBO || NewBO->Opcode != Instruction::Mul) {
6215 MulOps.push_back(getSCEV(BO->LHS));
6221 return getMulExpr(MulOps);
6223 case Instruction::UDiv:
6224 return getUDivExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
6225 case Instruction::URem:
6226 return getURemExpr(getSCEV(BO->LHS), getSCEV(BO->RHS));
6227 case Instruction::Sub: {
6228 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
6230 Flags = getNoWrapFlagsFromUB(BO->Op);
6231 return getMinusSCEV(getSCEV(BO->LHS), getSCEV(BO->RHS), Flags);
6233 case Instruction::And:
6234 // For an expression like x&255 that merely masks off the high bits,
6235 // use zext(trunc(x)) as the SCEV expression.
6236 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6238 return getSCEV(BO->RHS);
6239 if (CI->isMinusOne())
6240 return getSCEV(BO->LHS);
6241 const APInt &A = CI->getValue();
6243 // Instcombine's ShrinkDemandedConstant may strip bits out of
6244 // constants, obscuring what would otherwise be a low-bits mask.
6245 // Use computeKnownBits to compute what ShrinkDemandedConstant
6246 // knew about to reconstruct a low-bits mask value.
6247 unsigned LZ = A.countLeadingZeros();
6248 unsigned TZ = A.countTrailingZeros();
6249 unsigned BitWidth = A.getBitWidth();
6250 KnownBits Known(BitWidth);
6251 computeKnownBits(BO->LHS, Known, getDataLayout(),
6252 0, &AC, nullptr, &DT);
6254 APInt EffectiveMask =
6255 APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ);
6256 if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) {
6257 const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ));
6258 const SCEV *LHS = getSCEV(BO->LHS);
6259 const SCEV *ShiftedLHS = nullptr;
6260 if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) {
6261 if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) {
6262 // For an expression like (x * 8) & 8, simplify the multiply.
6263 unsigned MulZeros = OpC->getAPInt().countTrailingZeros();
6264 unsigned GCD = std::min(MulZeros, TZ);
6265 APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD);
6266 SmallVector<const SCEV*, 4> MulOps;
6267 MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD)));
6268 MulOps.append(LHSMul->op_begin() + 1, LHSMul->op_end());
6269 auto *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags());
6270 ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt));
6274 ShiftedLHS = getUDivExpr(LHS, MulCount);
6277 getTruncateExpr(ShiftedLHS,
6278 IntegerType::get(getContext(), BitWidth - LZ - TZ)),
6279 BO->LHS->getType()),
6285 case Instruction::Or:
6286 // If the RHS of the Or is a constant, we may have something like:
6287 // X*4+1 which got turned into X*4|1. Handle this as an Add so loop
6288 // optimizations will transparently handle this case.
6290 // In order for this transformation to be safe, the LHS must be of the
6291 // form X*(2^n) and the Or constant must be less than 2^n.
6292 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6293 const SCEV *LHS = getSCEV(BO->LHS);
6294 const APInt &CIVal = CI->getValue();
6295 if (GetMinTrailingZeros(LHS) >=
6296 (CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
6297 // Build a plain add SCEV.
6298 const SCEV *S = getAddExpr(LHS, getSCEV(CI));
6299 // If the LHS of the add was an addrec and it has no-wrap flags,
6300 // transfer the no-wrap flags, since an or won't introduce a wrap.
6301 if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
6302 const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
6303 const_cast<SCEVAddRecExpr *>(NewAR)->setNoWrapFlags(
6304 OldAR->getNoWrapFlags());
6311 case Instruction::Xor:
6312 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
6313 // If the RHS of xor is -1, then this is a not operation.
6314 if (CI->isMinusOne())
6315 return getNotSCEV(getSCEV(BO->LHS));
6317 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
6318 // This is a variant of the check for xor with -1, and it handles
6319 // the case where instcombine has trimmed non-demanded bits out
6320 // of an xor with -1.
6321 if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS))
6322 if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1)))
6323 if (LBO->getOpcode() == Instruction::And &&
6324 LCI->getValue() == CI->getValue())
6325 if (const SCEVZeroExtendExpr *Z =
6326 dyn_cast<SCEVZeroExtendExpr>(getSCEV(BO->LHS))) {
6327 Type *UTy = BO->LHS->getType();
6328 const SCEV *Z0 = Z->getOperand();
6329 Type *Z0Ty = Z0->getType();
6330 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
6332 // If C is a low-bits mask, the zero extend is serving to
6333 // mask off the high bits. Complement the operand and
6334 // re-apply the zext.
6335 if (CI->getValue().isMask(Z0TySize))
6336 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
6338 // If C is a single bit, it may be in the sign-bit position
6339 // before the zero-extend. In this case, represent the xor
6340 // using an add, which is equivalent, and re-apply the zext.
6341 APInt Trunc = CI->getValue().trunc(Z0TySize);
6342 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
6344 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
6350 case Instruction::Shl:
6351 // Turn shift left of a constant amount into a multiply.
6352 if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) {
6353 uint32_t BitWidth = cast<IntegerType>(SA->getType())->getBitWidth();
6355 // If the shift count is not less than the bitwidth, the result of
6356 // the shift is undefined. Don't try to analyze it, because the
6357 // resolution chosen here may differ from the resolution chosen in
6358 // other parts of the compiler.
6359 if (SA->getValue().uge(BitWidth))
6362 // It is currently not resolved how to interpret NSW for left
6363 // shift by BitWidth - 1, so we avoid applying flags in that
6364 // case. Remove this check (or this comment) once the situation
6366 // http://lists.llvm.org/pipermail/llvm-dev/2015-April/084195.html
6367 // and http://reviews.llvm.org/D8890 .
6368 auto Flags = SCEV::FlagAnyWrap;
6369 if (BO->Op && SA->getValue().ult(BitWidth - 1))
6370 Flags = getNoWrapFlagsFromUB(BO->Op);
6372 Constant *X = ConstantInt::get(
6373 getContext(), APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
6374 return getMulExpr(getSCEV(BO->LHS), getSCEV(X), Flags);
6378 case Instruction::AShr: {
6379 // AShr X, C, where C is a constant.
6380 ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS);
6384 Type *OuterTy = BO->LHS->getType();
6385 uint64_t BitWidth = getTypeSizeInBits(OuterTy);
6386 // If the shift count is not less than the bitwidth, the result of
6387 // the shift is undefined. Don't try to analyze it, because the
6388 // resolution chosen here may differ from the resolution chosen in
6389 // other parts of the compiler.
6390 if (CI->getValue().uge(BitWidth))
6394 return getSCEV(BO->LHS); // shift by zero --> noop
6396 uint64_t AShrAmt = CI->getZExtValue();
6397 Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt);
6399 Operator *L = dyn_cast<Operator>(BO->LHS);
6400 if (L && L->getOpcode() == Instruction::Shl) {
6403 // Both n and m are constant.
6405 const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0));
6406 if (L->getOperand(1) == BO->RHS)
6407 // For a two-shift sext-inreg, i.e. n = m,
6408 // use sext(trunc(x)) as the SCEV expression.
6409 return getSignExtendExpr(
6410 getTruncateExpr(ShlOp0SCEV, TruncTy), OuterTy);
6412 ConstantInt *ShlAmtCI = dyn_cast<ConstantInt>(L->getOperand(1));
6413 if (ShlAmtCI && ShlAmtCI->getValue().ult(BitWidth)) {
6414 uint64_t ShlAmt = ShlAmtCI->getZExtValue();
6415 if (ShlAmt > AShrAmt) {
6416 // When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV
6417 // expression. We already checked that ShlAmt < BitWidth, so
6418 // the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as
6419 // ShlAmt - AShrAmt < Amt.
6420 APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt,
6422 return getSignExtendExpr(
6423 getMulExpr(getTruncateExpr(ShlOp0SCEV, TruncTy),
6424 getConstant(Mul)), OuterTy);
6433 switch (U->getOpcode()) {
6434 case Instruction::Trunc:
6435 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
6437 case Instruction::ZExt:
6438 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
6440 case Instruction::SExt:
6441 if (auto BO = MatchBinaryOp(U->getOperand(0), DT)) {
6442 // The NSW flag of a subtract does not always survive the conversion to
6443 // A + (-1)*B. By pushing sign extension onto its operands we are much
6444 // more likely to preserve NSW and allow later AddRec optimisations.
6446 // NOTE: This is effectively duplicating this logic from getSignExtend:
6447 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
6448 // but by that point the NSW information has potentially been lost.
6449 if (BO->Opcode == Instruction::Sub && BO->IsNSW) {
6450 Type *Ty = U->getType();
6451 auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty);
6452 auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty);
6453 return getMinusSCEV(V1, V2, SCEV::FlagNSW);
6456 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
6458 case Instruction::BitCast:
6459 // BitCasts are no-op casts so we just eliminate the cast.
6460 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
6461 return getSCEV(U->getOperand(0));
6464 // It's tempting to handle inttoptr and ptrtoint as no-ops, however this can
6465 // lead to pointer expressions which cannot safely be expanded to GEPs,
6466 // because ScalarEvolution doesn't respect the GEP aliasing rules when
6467 // simplifying integer expressions.
6469 case Instruction::GetElementPtr:
6470 return createNodeForGEP(cast<GEPOperator>(U));
6472 case Instruction::PHI:
6473 return createNodeForPHI(cast<PHINode>(U));
6475 case Instruction::Select:
6476 // U can also be a select constant expr, which let fall through. Since
6477 // createNodeForSelect only works for a condition that is an `ICmpInst`, and
6478 // constant expressions cannot have instructions as operands, we'd have
6479 // returned getUnknown for a select constant expressions anyway.
6480 if (isa<Instruction>(U))
6481 return createNodeForSelectOrPHI(cast<Instruction>(U), U->getOperand(0),
6482 U->getOperand(1), U->getOperand(2));
6485 case Instruction::Call:
6486 case Instruction::Invoke:
6487 if (Value *RV = CallSite(U).getReturnedArgOperand())
6492 return getUnknown(V);
6495 //===----------------------------------------------------------------------===//
6496 // Iteration Count Computation Code
6499 static unsigned getConstantTripCount(const SCEVConstant *ExitCount) {
6503 ConstantInt *ExitConst = ExitCount->getValue();
6505 // Guard against huge trip counts.
6506 if (ExitConst->getValue().getActiveBits() > 32)
6509 // In case of integer overflow, this returns 0, which is correct.
6510 return ((unsigned)ExitConst->getZExtValue()) + 1;
6513 unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) {
6514 if (BasicBlock *ExitingBB = L->getExitingBlock())
6515 return getSmallConstantTripCount(L, ExitingBB);
6517 // No trip count information for multiple exits.
6521 unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L,
6522 BasicBlock *ExitingBlock) {
6523 assert(ExitingBlock && "Must pass a non-null exiting block!");
6524 assert(L->isLoopExiting(ExitingBlock) &&
6525 "Exiting block must actually branch out of the loop!");
6526 const SCEVConstant *ExitCount =
6527 dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock));
6528 return getConstantTripCount(ExitCount);
6531 unsigned ScalarEvolution::getSmallConstantMaxTripCount(const Loop *L) {
6532 const auto *MaxExitCount =
6533 dyn_cast<SCEVConstant>(getMaxBackedgeTakenCount(L));
6534 return getConstantTripCount(MaxExitCount);
6537 unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) {
6538 if (BasicBlock *ExitingBB = L->getExitingBlock())
6539 return getSmallConstantTripMultiple(L, ExitingBB);
6541 // No trip multiple information for multiple exits.
6545 /// Returns the largest constant divisor of the trip count of this loop as a
6546 /// normal unsigned value, if possible. This means that the actual trip count is
6547 /// always a multiple of the returned value (don't forget the trip count could
6548 /// very well be zero as well!).
6550 /// Returns 1 if the trip count is unknown or not guaranteed to be the
6551 /// multiple of a constant (which is also the case if the trip count is simply
6552 /// constant, use getSmallConstantTripCount for that case), Will also return 1
6553 /// if the trip count is very large (>= 2^32).
6555 /// As explained in the comments for getSmallConstantTripCount, this assumes
6556 /// that control exits the loop via ExitingBlock.
6558 ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
6559 BasicBlock *ExitingBlock) {
6560 assert(ExitingBlock && "Must pass a non-null exiting block!");
6561 assert(L->isLoopExiting(ExitingBlock) &&
6562 "Exiting block must actually branch out of the loop!");
6563 const SCEV *ExitCount = getExitCount(L, ExitingBlock);
6564 if (ExitCount == getCouldNotCompute())
6567 // Get the trip count from the BE count by adding 1.
6568 const SCEV *TCExpr = getAddExpr(ExitCount, getOne(ExitCount->getType()));
6570 const SCEVConstant *TC = dyn_cast<SCEVConstant>(TCExpr);
6572 // Attempt to factor more general cases. Returns the greatest power of
6573 // two divisor. If overflow happens, the trip count expression is still
6574 // divisible by the greatest power of 2 divisor returned.
6575 return 1U << std::min((uint32_t)31, GetMinTrailingZeros(TCExpr));
6577 ConstantInt *Result = TC->getValue();
6579 // Guard against huge trip counts (this requires checking
6580 // for zero to handle the case where the trip count == -1 and the
6582 if (!Result || Result->getValue().getActiveBits() > 32 ||
6583 Result->getValue().getActiveBits() == 0)
6586 return (unsigned)Result->getZExtValue();
6589 /// Get the expression for the number of loop iterations for which this loop is
6590 /// guaranteed not to exit via ExitingBlock. Otherwise return
6591 /// SCEVCouldNotCompute.
6592 const SCEV *ScalarEvolution::getExitCount(const Loop *L,
6593 BasicBlock *ExitingBlock) {
6594 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this);
6598 ScalarEvolution::getPredicatedBackedgeTakenCount(const Loop *L,
6599 SCEVUnionPredicate &Preds) {
6600 return getPredicatedBackedgeTakenInfo(L).getExact(L, this, &Preds);
6603 const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L) {
6604 return getBackedgeTakenInfo(L).getExact(L, this);
6607 /// Similar to getBackedgeTakenCount, except return the least SCEV value that is
6608 /// known never to be less than the actual backedge taken count.
6609 const SCEV *ScalarEvolution::getMaxBackedgeTakenCount(const Loop *L) {
6610 return getBackedgeTakenInfo(L).getMax(this);
6613 bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) {
6614 return getBackedgeTakenInfo(L).isMaxOrZero(this);
6617 /// Push PHI nodes in the header of the given loop onto the given Worklist.
6619 PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
6620 BasicBlock *Header = L->getHeader();
6622 // Push all Loop-header PHIs onto the Worklist stack.
6623 for (PHINode &PN : Header->phis())
6624 Worklist.push_back(&PN);
6627 const ScalarEvolution::BackedgeTakenInfo &
6628 ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) {
6629 auto &BTI = getBackedgeTakenInfo(L);
6630 if (BTI.hasFullInfo())
6633 auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
6636 return Pair.first->second;
6638 BackedgeTakenInfo Result =
6639 computeBackedgeTakenCount(L, /*AllowPredicates=*/true);
6641 return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result);
6644 const ScalarEvolution::BackedgeTakenInfo &
6645 ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
6646 // Initially insert an invalid entry for this loop. If the insertion
6647 // succeeds, proceed to actually compute a backedge-taken count and
6648 // update the value. The temporary CouldNotCompute value tells SCEV
6649 // code elsewhere that it shouldn't attempt to request a new
6650 // backedge-taken count, which could result in infinite recursion.
6651 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
6652 BackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
6654 return Pair.first->second;
6656 // computeBackedgeTakenCount may allocate memory for its result. Inserting it
6657 // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
6658 // must be cleared in this scope.
6659 BackedgeTakenInfo Result = computeBackedgeTakenCount(L);
6661 // In product build, there are no usage of statistic.
6662 (void)NumTripCountsComputed;
6663 (void)NumTripCountsNotComputed;
6664 #if LLVM_ENABLE_STATS || !defined(NDEBUG)
6665 const SCEV *BEExact = Result.getExact(L, this);
6666 if (BEExact != getCouldNotCompute()) {
6667 assert(isLoopInvariant(BEExact, L) &&
6668 isLoopInvariant(Result.getMax(this), L) &&
6669 "Computed backedge-taken count isn't loop invariant for loop!");
6670 ++NumTripCountsComputed;
6672 else if (Result.getMax(this) == getCouldNotCompute() &&
6673 isa<PHINode>(L->getHeader()->begin())) {
6674 // Only count loops that have phi nodes as not being computable.
6675 ++NumTripCountsNotComputed;
6677 #endif // LLVM_ENABLE_STATS || !defined(NDEBUG)
6679 // Now that we know more about the trip count for this loop, forget any
6680 // existing SCEV values for PHI nodes in this loop since they are only
6681 // conservative estimates made without the benefit of trip count
6682 // information. This is similar to the code in forgetLoop, except that
6683 // it handles SCEVUnknown PHI nodes specially.
6684 if (Result.hasAnyInfo()) {
6685 SmallVector<Instruction *, 16> Worklist;
6686 PushLoopPHIs(L, Worklist);
6688 SmallPtrSet<Instruction *, 8> Discovered;
6689 while (!Worklist.empty()) {
6690 Instruction *I = Worklist.pop_back_val();
6692 ValueExprMapType::iterator It =
6693 ValueExprMap.find_as(static_cast<Value *>(I));
6694 if (It != ValueExprMap.end()) {
6695 const SCEV *Old = It->second;
6697 // SCEVUnknown for a PHI either means that it has an unrecognized
6698 // structure, or it's a PHI that's in the progress of being computed
6699 // by createNodeForPHI. In the former case, additional loop trip
6700 // count information isn't going to change anything. In the later
6701 // case, createNodeForPHI will perform the necessary updates on its
6702 // own when it gets to that point.
6703 if (!isa<PHINode>(I) || !isa<SCEVUnknown>(Old)) {
6704 eraseValueFromMap(It->first);
6705 forgetMemoizedResults(Old);
6707 if (PHINode *PN = dyn_cast<PHINode>(I))
6708 ConstantEvolutionLoopExitValue.erase(PN);
6711 // Since we don't need to invalidate anything for correctness and we're
6712 // only invalidating to make SCEV's results more precise, we get to stop
6713 // early to avoid invalidating too much. This is especially important in
6716 // %v = f(pn0, pn1) // pn0 and pn1 used through some other phi node
6724 // where both loop0 and loop1's backedge taken count uses the SCEV
6725 // expression for %v. If we don't have the early stop below then in cases
6726 // like the above, getBackedgeTakenInfo(loop1) will clear out the trip
6727 // count for loop0 and getBackedgeTakenInfo(loop0) will clear out the trip
6728 // count for loop1, effectively nullifying SCEV's trip count cache.
6729 for (auto *U : I->users())
6730 if (auto *I = dyn_cast<Instruction>(U)) {
6731 auto *LoopForUser = LI.getLoopFor(I->getParent());
6732 if (LoopForUser && L->contains(LoopForUser) &&
6733 Discovered.insert(I).second)
6734 Worklist.push_back(I);
6739 // Re-lookup the insert position, since the call to
6740 // computeBackedgeTakenCount above could result in a
6741 // recusive call to getBackedgeTakenInfo (on a different
6742 // loop), which would invalidate the iterator computed
6744 return BackedgeTakenCounts.find(L)->second = std::move(Result);
6747 void ScalarEvolution::forgetLoop(const Loop *L) {
6748 // Drop any stored trip count value.
6749 auto RemoveLoopFromBackedgeMap =
6750 [](DenseMap<const Loop *, BackedgeTakenInfo> &Map, const Loop *L) {
6751 auto BTCPos = Map.find(L);
6752 if (BTCPos != Map.end()) {
6753 BTCPos->second.clear();
6758 SmallVector<const Loop *, 16> LoopWorklist(1, L);
6759 SmallVector<Instruction *, 32> Worklist;
6760 SmallPtrSet<Instruction *, 16> Visited;
6762 // Iterate over all the loops and sub-loops to drop SCEV information.
6763 while (!LoopWorklist.empty()) {
6764 auto *CurrL = LoopWorklist.pop_back_val();
6766 RemoveLoopFromBackedgeMap(BackedgeTakenCounts, CurrL);
6767 RemoveLoopFromBackedgeMap(PredicatedBackedgeTakenCounts, CurrL);
6769 // Drop information about predicated SCEV rewrites for this loop.
6770 for (auto I = PredicatedSCEVRewrites.begin();
6771 I != PredicatedSCEVRewrites.end();) {
6772 std::pair<const SCEV *, const Loop *> Entry = I->first;
6773 if (Entry.second == CurrL)
6774 PredicatedSCEVRewrites.erase(I++);
6779 auto LoopUsersItr = LoopUsers.find(CurrL);
6780 if (LoopUsersItr != LoopUsers.end()) {
6781 for (auto *S : LoopUsersItr->second)
6782 forgetMemoizedResults(S);
6783 LoopUsers.erase(LoopUsersItr);
6786 // Drop information about expressions based on loop-header PHIs.
6787 PushLoopPHIs(CurrL, Worklist);
6789 while (!Worklist.empty()) {
6790 Instruction *I = Worklist.pop_back_val();
6791 if (!Visited.insert(I).second)
6794 ValueExprMapType::iterator It =
6795 ValueExprMap.find_as(static_cast<Value *>(I));
6796 if (It != ValueExprMap.end()) {
6797 eraseValueFromMap(It->first);
6798 forgetMemoizedResults(It->second);
6799 if (PHINode *PN = dyn_cast<PHINode>(I))
6800 ConstantEvolutionLoopExitValue.erase(PN);
6803 PushDefUseChildren(I, Worklist);
6806 LoopPropertiesCache.erase(CurrL);
6807 // Forget all contained loops too, to avoid dangling entries in the
6808 // ValuesAtScopes map.
6809 LoopWorklist.append(CurrL->begin(), CurrL->end());
6813 void ScalarEvolution::forgetTopmostLoop(const Loop *L) {
6814 while (Loop *Parent = L->getParentLoop())
6819 void ScalarEvolution::forgetValue(Value *V) {
6820 Instruction *I = dyn_cast<Instruction>(V);
6823 // Drop information about expressions based on loop-header PHIs.
6824 SmallVector<Instruction *, 16> Worklist;
6825 Worklist.push_back(I);
6827 SmallPtrSet<Instruction *, 8> Visited;
6828 while (!Worklist.empty()) {
6829 I = Worklist.pop_back_val();
6830 if (!Visited.insert(I).second)
6833 ValueExprMapType::iterator It =
6834 ValueExprMap.find_as(static_cast<Value *>(I));
6835 if (It != ValueExprMap.end()) {
6836 eraseValueFromMap(It->first);
6837 forgetMemoizedResults(It->second);
6838 if (PHINode *PN = dyn_cast<PHINode>(I))
6839 ConstantEvolutionLoopExitValue.erase(PN);
6842 PushDefUseChildren(I, Worklist);
6846 /// Get the exact loop backedge taken count considering all loop exits. A
6847 /// computable result can only be returned for loops with all exiting blocks
6848 /// dominating the latch. howFarToZero assumes that the limit of each loop test
6849 /// is never skipped. This is a valid assumption as long as the loop exits via
6850 /// that test. For precise results, it is the caller's responsibility to specify
6851 /// the relevant loop exiting block using getExact(ExitingBlock, SE).
6853 ScalarEvolution::BackedgeTakenInfo::getExact(const Loop *L, ScalarEvolution *SE,
6854 SCEVUnionPredicate *Preds) const {
6855 // If any exits were not computable, the loop is not computable.
6856 if (!isComplete() || ExitNotTaken.empty())
6857 return SE->getCouldNotCompute();
6859 const BasicBlock *Latch = L->getLoopLatch();
6860 // All exiting blocks we have collected must dominate the only backedge.
6862 return SE->getCouldNotCompute();
6864 // All exiting blocks we have gathered dominate loop's latch, so exact trip
6865 // count is simply a minimum out of all these calculated exit counts.
6866 SmallVector<const SCEV *, 2> Ops;
6867 for (auto &ENT : ExitNotTaken) {
6868 const SCEV *BECount = ENT.ExactNotTaken;
6869 assert(BECount != SE->getCouldNotCompute() && "Bad exit SCEV!");
6870 assert(SE->DT.dominates(ENT.ExitingBlock, Latch) &&
6871 "We should only have known counts for exiting blocks that dominate "
6874 Ops.push_back(BECount);
6876 if (Preds && !ENT.hasAlwaysTruePredicate())
6877 Preds->add(ENT.Predicate.get());
6879 assert((Preds || ENT.hasAlwaysTruePredicate()) &&
6880 "Predicate should be always true!");
6883 return SE->getUMinFromMismatchedTypes(Ops);
6886 /// Get the exact not taken count for this loop exit.
6888 ScalarEvolution::BackedgeTakenInfo::getExact(BasicBlock *ExitingBlock,
6889 ScalarEvolution *SE) const {
6890 for (auto &ENT : ExitNotTaken)
6891 if (ENT.ExitingBlock == ExitingBlock && ENT.hasAlwaysTruePredicate())
6892 return ENT.ExactNotTaken;
6894 return SE->getCouldNotCompute();
6897 /// getMax - Get the max backedge taken count for the loop.
6899 ScalarEvolution::BackedgeTakenInfo::getMax(ScalarEvolution *SE) const {
6900 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
6901 return !ENT.hasAlwaysTruePredicate();
6904 if (any_of(ExitNotTaken, PredicateNotAlwaysTrue) || !getMax())
6905 return SE->getCouldNotCompute();
6907 assert((isa<SCEVCouldNotCompute>(getMax()) || isa<SCEVConstant>(getMax())) &&
6908 "No point in having a non-constant max backedge taken count!");
6912 bool ScalarEvolution::BackedgeTakenInfo::isMaxOrZero(ScalarEvolution *SE) const {
6913 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
6914 return !ENT.hasAlwaysTruePredicate();
6916 return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue);
6919 bool ScalarEvolution::BackedgeTakenInfo::hasOperand(const SCEV *S,
6920 ScalarEvolution *SE) const {
6921 if (getMax() && getMax() != SE->getCouldNotCompute() &&
6922 SE->hasOperand(getMax(), S))
6925 for (auto &ENT : ExitNotTaken)
6926 if (ENT.ExactNotTaken != SE->getCouldNotCompute() &&
6927 SE->hasOperand(ENT.ExactNotTaken, S))
6933 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E)
6934 : ExactNotTaken(E), MaxNotTaken(E) {
6935 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6936 isa<SCEVConstant>(MaxNotTaken)) &&
6937 "No point in having a non-constant max backedge taken count!");
6940 ScalarEvolution::ExitLimit::ExitLimit(
6941 const SCEV *E, const SCEV *M, bool MaxOrZero,
6942 ArrayRef<const SmallPtrSetImpl<const SCEVPredicate *> *> PredSetList)
6943 : ExactNotTaken(E), MaxNotTaken(M), MaxOrZero(MaxOrZero) {
6944 assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
6945 !isa<SCEVCouldNotCompute>(MaxNotTaken)) &&
6946 "Exact is not allowed to be less precise than Max");
6947 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6948 isa<SCEVConstant>(MaxNotTaken)) &&
6949 "No point in having a non-constant max backedge taken count!");
6950 for (auto *PredSet : PredSetList)
6951 for (auto *P : *PredSet)
6955 ScalarEvolution::ExitLimit::ExitLimit(
6956 const SCEV *E, const SCEV *M, bool MaxOrZero,
6957 const SmallPtrSetImpl<const SCEVPredicate *> &PredSet)
6958 : ExitLimit(E, M, MaxOrZero, {&PredSet}) {
6959 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6960 isa<SCEVConstant>(MaxNotTaken)) &&
6961 "No point in having a non-constant max backedge taken count!");
6964 ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E, const SCEV *M,
6966 : ExitLimit(E, M, MaxOrZero, None) {
6967 assert((isa<SCEVCouldNotCompute>(MaxNotTaken) ||
6968 isa<SCEVConstant>(MaxNotTaken)) &&
6969 "No point in having a non-constant max backedge taken count!");
6972 /// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
6973 /// computable exit into a persistent ExitNotTakenInfo array.
6974 ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
6975 SmallVectorImpl<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo>
6977 bool Complete, const SCEV *MaxCount, bool MaxOrZero)
6978 : MaxAndComplete(MaxCount, Complete), MaxOrZero(MaxOrZero) {
6979 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
6981 ExitNotTaken.reserve(ExitCounts.size());
6983 ExitCounts.begin(), ExitCounts.end(), std::back_inserter(ExitNotTaken),
6984 [&](const EdgeExitInfo &EEI) {
6985 BasicBlock *ExitBB = EEI.first;
6986 const ExitLimit &EL = EEI.second;
6987 if (EL.Predicates.empty())
6988 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, nullptr);
6990 std::unique_ptr<SCEVUnionPredicate> Predicate(new SCEVUnionPredicate);
6991 for (auto *Pred : EL.Predicates)
6992 Predicate->add(Pred);
6994 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken, std::move(Predicate));
6996 assert((isa<SCEVCouldNotCompute>(MaxCount) || isa<SCEVConstant>(MaxCount)) &&
6997 "No point in having a non-constant max backedge taken count!");
7000 /// Invalidate this result and free the ExitNotTakenInfo array.
7001 void ScalarEvolution::BackedgeTakenInfo::clear() {
7002 ExitNotTaken.clear();
7005 /// Compute the number of times the backedge of the specified loop will execute.
7006 ScalarEvolution::BackedgeTakenInfo
7007 ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
7008 bool AllowPredicates) {
7009 SmallVector<BasicBlock *, 8> ExitingBlocks;
7010 L->getExitingBlocks(ExitingBlocks);
7012 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
7014 SmallVector<EdgeExitInfo, 4> ExitCounts;
7015 bool CouldComputeBECount = true;
7016 BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
7017 const SCEV *MustExitMaxBECount = nullptr;
7018 const SCEV *MayExitMaxBECount = nullptr;
7019 bool MustExitMaxOrZero = false;
7021 // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
7022 // and compute maxBECount.
7023 // Do a union of all the predicates here.
7024 for (unsigned i = 0, e = ExitingBlocks.size(); i != e; ++i) {
7025 BasicBlock *ExitBB = ExitingBlocks[i];
7026 ExitLimit EL = computeExitLimit(L, ExitBB, AllowPredicates);
7028 assert((AllowPredicates || EL.Predicates.empty()) &&
7029 "Predicated exit limit when predicates are not allowed!");
7031 // 1. For each exit that can be computed, add an entry to ExitCounts.
7032 // CouldComputeBECount is true only if all exits can be computed.
7033 if (EL.ExactNotTaken == getCouldNotCompute())
7034 // We couldn't compute an exact value for this exit, so
7035 // we won't be able to compute an exact value for the loop.
7036 CouldComputeBECount = false;
7038 ExitCounts.emplace_back(ExitBB, EL);
7040 // 2. Derive the loop's MaxBECount from each exit's max number of
7041 // non-exiting iterations. Partition the loop exits into two kinds:
7042 // LoopMustExits and LoopMayExits.
7044 // If the exit dominates the loop latch, it is a LoopMustExit otherwise it
7045 // is a LoopMayExit. If any computable LoopMustExit is found, then
7046 // MaxBECount is the minimum EL.MaxNotTaken of computable
7047 // LoopMustExits. Otherwise, MaxBECount is conservatively the maximum
7048 // EL.MaxNotTaken, where CouldNotCompute is considered greater than any
7049 // computable EL.MaxNotTaken.
7050 if (EL.MaxNotTaken != getCouldNotCompute() && Latch &&
7051 DT.dominates(ExitBB, Latch)) {
7052 if (!MustExitMaxBECount) {
7053 MustExitMaxBECount = EL.MaxNotTaken;
7054 MustExitMaxOrZero = EL.MaxOrZero;
7056 MustExitMaxBECount =
7057 getUMinFromMismatchedTypes(MustExitMaxBECount, EL.MaxNotTaken);
7059 } else if (MayExitMaxBECount != getCouldNotCompute()) {
7060 if (!MayExitMaxBECount || EL.MaxNotTaken == getCouldNotCompute())
7061 MayExitMaxBECount = EL.MaxNotTaken;
7064 getUMaxFromMismatchedTypes(MayExitMaxBECount, EL.MaxNotTaken);
7068 const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
7069 (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute());
7070 // The loop backedge will be taken the maximum or zero times if there's
7071 // a single exit that must be taken the maximum or zero times.
7072 bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1);
7073 return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount,
7074 MaxBECount, MaxOrZero);
7077 ScalarEvolution::ExitLimit
7078 ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
7079 bool AllowPredicates) {
7080 assert(L->contains(ExitingBlock) && "Exit count for non-loop block?");
7081 // If our exiting block does not dominate the latch, then its connection with
7082 // loop's exit limit may be far from trivial.
7083 const BasicBlock *Latch = L->getLoopLatch();
7084 if (!Latch || !DT.dominates(ExitingBlock, Latch))
7085 return getCouldNotCompute();
7087 bool IsOnlyExit = (L->getExitingBlock() != nullptr);
7088 Instruction *Term = ExitingBlock->getTerminator();
7089 if (BranchInst *BI = dyn_cast<BranchInst>(Term)) {
7090 assert(BI->isConditional() && "If unconditional, it can't be in loop!");
7091 bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
7092 assert(ExitIfTrue == L->contains(BI->getSuccessor(1)) &&
7093 "It should have one successor in loop and one exit block!");
7094 // Proceed to the next level to examine the exit condition expression.
7095 return computeExitLimitFromCond(
7096 L, BI->getCondition(), ExitIfTrue,
7097 /*ControlsExit=*/IsOnlyExit, AllowPredicates);
7100 if (SwitchInst *SI = dyn_cast<SwitchInst>(Term)) {
7101 // For switch, make sure that there is a single exit from the loop.
7102 BasicBlock *Exit = nullptr;
7103 for (auto *SBB : successors(ExitingBlock))
7104 if (!L->contains(SBB)) {
7105 if (Exit) // Multiple exit successors.
7106 return getCouldNotCompute();
7109 assert(Exit && "Exiting block must have at least one exit");
7110 return computeExitLimitFromSingleExitSwitch(L, SI, Exit,
7111 /*ControlsExit=*/IsOnlyExit);
7114 return getCouldNotCompute();
7117 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond(
7118 const Loop *L, Value *ExitCond, bool ExitIfTrue,
7119 bool ControlsExit, bool AllowPredicates) {
7120 ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates);
7121 return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue,
7122 ControlsExit, AllowPredicates);
7125 Optional<ScalarEvolution::ExitLimit>
7126 ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond,
7127 bool ExitIfTrue, bool ControlsExit,
7128 bool AllowPredicates) {
7130 (void)this->ExitIfTrue;
7131 (void)this->AllowPredicates;
7133 assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
7134 this->AllowPredicates == AllowPredicates &&
7135 "Variance in assumed invariant key components!");
7136 auto Itr = TripCountMap.find({ExitCond, ControlsExit});
7137 if (Itr == TripCountMap.end())
7142 void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond,
7145 bool AllowPredicates,
7146 const ExitLimit &EL) {
7147 assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
7148 this->AllowPredicates == AllowPredicates &&
7149 "Variance in assumed invariant key components!");
7151 auto InsertResult = TripCountMap.insert({{ExitCond, ControlsExit}, EL});
7152 assert(InsertResult.second && "Expected successful insertion!");
7157 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
7158 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
7159 bool ControlsExit, bool AllowPredicates) {
7162 Cache.find(L, ExitCond, ExitIfTrue, ControlsExit, AllowPredicates))
7165 ExitLimit EL = computeExitLimitFromCondImpl(Cache, L, ExitCond, ExitIfTrue,
7166 ControlsExit, AllowPredicates);
7167 Cache.insert(L, ExitCond, ExitIfTrue, ControlsExit, AllowPredicates, EL);
7171 ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
7172 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
7173 bool ControlsExit, bool AllowPredicates) {
7174 // Check if the controlling expression for this loop is an And or Or.
7175 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(ExitCond)) {
7176 if (BO->getOpcode() == Instruction::And) {
7177 // Recurse on the operands of the and.
7178 bool EitherMayExit = !ExitIfTrue;
7179 ExitLimit EL0 = computeExitLimitFromCondCached(
7180 Cache, L, BO->getOperand(0), ExitIfTrue,
7181 ControlsExit && !EitherMayExit, AllowPredicates);
7182 ExitLimit EL1 = computeExitLimitFromCondCached(
7183 Cache, L, BO->getOperand(1), ExitIfTrue,
7184 ControlsExit && !EitherMayExit, AllowPredicates);
7185 const SCEV *BECount = getCouldNotCompute();
7186 const SCEV *MaxBECount = getCouldNotCompute();
7187 if (EitherMayExit) {
7188 // Both conditions must be true for the loop to continue executing.
7189 // Choose the less conservative count.
7190 if (EL0.ExactNotTaken == getCouldNotCompute() ||
7191 EL1.ExactNotTaken == getCouldNotCompute())
7192 BECount = getCouldNotCompute();
7195 getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
7196 if (EL0.MaxNotTaken == getCouldNotCompute())
7197 MaxBECount = EL1.MaxNotTaken;
7198 else if (EL1.MaxNotTaken == getCouldNotCompute())
7199 MaxBECount = EL0.MaxNotTaken;
7202 getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
7204 // Both conditions must be true at the same time for the loop to exit.
7205 // For now, be conservative.
7206 if (EL0.MaxNotTaken == EL1.MaxNotTaken)
7207 MaxBECount = EL0.MaxNotTaken;
7208 if (EL0.ExactNotTaken == EL1.ExactNotTaken)
7209 BECount = EL0.ExactNotTaken;
7212 // There are cases (e.g. PR26207) where computeExitLimitFromCond is able
7213 // to be more aggressive when computing BECount than when computing
7214 // MaxBECount. In these cases it is possible for EL0.ExactNotTaken and
7215 // EL1.ExactNotTaken to match, but for EL0.MaxNotTaken and EL1.MaxNotTaken
7217 if (isa<SCEVCouldNotCompute>(MaxBECount) &&
7218 !isa<SCEVCouldNotCompute>(BECount))
7219 MaxBECount = getConstant(getUnsignedRangeMax(BECount));
7221 return ExitLimit(BECount, MaxBECount, false,
7222 {&EL0.Predicates, &EL1.Predicates});
7224 if (BO->getOpcode() == Instruction::Or) {
7225 // Recurse on the operands of the or.
7226 bool EitherMayExit = ExitIfTrue;
7227 ExitLimit EL0 = computeExitLimitFromCondCached(
7228 Cache, L, BO->getOperand(0), ExitIfTrue,
7229 ControlsExit && !EitherMayExit, AllowPredicates);
7230 ExitLimit EL1 = computeExitLimitFromCondCached(
7231 Cache, L, BO->getOperand(1), ExitIfTrue,
7232 ControlsExit && !EitherMayExit, AllowPredicates);
7233 const SCEV *BECount = getCouldNotCompute();
7234 const SCEV *MaxBECount = getCouldNotCompute();
7235 if (EitherMayExit) {
7236 // Both conditions must be false for the loop to continue executing.
7237 // Choose the less conservative count.
7238 if (EL0.ExactNotTaken == getCouldNotCompute() ||
7239 EL1.ExactNotTaken == getCouldNotCompute())
7240 BECount = getCouldNotCompute();
7243 getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken);
7244 if (EL0.MaxNotTaken == getCouldNotCompute())
7245 MaxBECount = EL1.MaxNotTaken;
7246 else if (EL1.MaxNotTaken == getCouldNotCompute())
7247 MaxBECount = EL0.MaxNotTaken;
7250 getUMinFromMismatchedTypes(EL0.MaxNotTaken, EL1.MaxNotTaken);
7252 // Both conditions must be false at the same time for the loop to exit.
7253 // For now, be conservative.
7254 if (EL0.MaxNotTaken == EL1.MaxNotTaken)
7255 MaxBECount = EL0.MaxNotTaken;
7256 if (EL0.ExactNotTaken == EL1.ExactNotTaken)
7257 BECount = EL0.ExactNotTaken;
7260 return ExitLimit(BECount, MaxBECount, false,
7261 {&EL0.Predicates, &EL1.Predicates});
7265 // With an icmp, it may be feasible to compute an exact backedge-taken count.
7266 // Proceed to the next level to examine the icmp.
7267 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) {
7269 computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsExit);
7270 if (EL.hasFullInfo() || !AllowPredicates)
7273 // Try again, but use SCEV predicates this time.
7274 return computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsExit,
7275 /*AllowPredicates=*/true);
7278 // Check for a constant condition. These are normally stripped out by
7279 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
7280 // preserve the CFG and is temporarily leaving constant conditions
7282 if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
7283 if (ExitIfTrue == !CI->getZExtValue())
7284 // The backedge is always taken.
7285 return getCouldNotCompute();
7287 // The backedge is never taken.
7288 return getZero(CI->getType());
7291 // If it's not an integer or pointer comparison then compute it the hard way.
7292 return computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
7295 ScalarEvolution::ExitLimit
7296 ScalarEvolution::computeExitLimitFromICmp(const Loop *L,
7300 bool AllowPredicates) {
7301 // If the condition was exit on true, convert the condition to exit on false
7302 ICmpInst::Predicate Pred;
7304 Pred = ExitCond->getPredicate();
7306 Pred = ExitCond->getInversePredicate();
7307 const ICmpInst::Predicate OriginalPred = Pred;
7309 // Handle common loops like: for (X = "string"; *X; ++X)
7310 if (LoadInst *LI = dyn_cast<LoadInst>(ExitCond->getOperand(0)))
7311 if (Constant *RHS = dyn_cast<Constant>(ExitCond->getOperand(1))) {
7313 computeLoadConstantCompareExitLimit(LI, RHS, L, Pred);
7314 if (ItCnt.hasAnyInfo())
7318 const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
7319 const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
7321 // Try to evaluate any dependencies out of the loop.
7322 LHS = getSCEVAtScope(LHS, L);
7323 RHS = getSCEVAtScope(RHS, L);
7325 // At this point, we would like to compute how many iterations of the
7326 // loop the predicate will return true for these inputs.
7327 if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) {
7328 // If there is a loop-invariant, force it into the RHS.
7329 std::swap(LHS, RHS);
7330 Pred = ICmpInst::getSwappedPredicate(Pred);
7333 // Simplify the operands before analyzing them.
7334 (void)SimplifyICmpOperands(Pred, LHS, RHS);
7336 // If we have a comparison of a chrec against a constant, try to use value
7337 // ranges to answer this query.
7338 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
7339 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
7340 if (AddRec->getLoop() == L) {
7341 // Form the constant range.
7342 ConstantRange CompRange =
7343 ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt());
7345 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
7346 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
7350 case ICmpInst::ICMP_NE: { // while (X != Y)
7351 // Convert to: while (X-Y != 0)
7352 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit,
7354 if (EL.hasAnyInfo()) return EL;
7357 case ICmpInst::ICMP_EQ: { // while (X == Y)
7358 // Convert to: while (X-Y == 0)
7359 ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L);
7360 if (EL.hasAnyInfo()) return EL;
7363 case ICmpInst::ICMP_SLT:
7364 case ICmpInst::ICMP_ULT: { // while (X < Y)
7365 bool IsSigned = Pred == ICmpInst::ICMP_SLT;
7366 ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsExit,
7368 if (EL.hasAnyInfo()) return EL;
7371 case ICmpInst::ICMP_SGT:
7372 case ICmpInst::ICMP_UGT: { // while (X > Y)
7373 bool IsSigned = Pred == ICmpInst::ICMP_SGT;
7375 howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsExit,
7377 if (EL.hasAnyInfo()) return EL;
7384 auto *ExhaustiveCount =
7385 computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
7387 if (!isa<SCEVCouldNotCompute>(ExhaustiveCount))
7388 return ExhaustiveCount;
7390 return computeShiftCompareExitLimit(ExitCond->getOperand(0),
7391 ExitCond->getOperand(1), L, OriginalPred);
7394 ScalarEvolution::ExitLimit
7395 ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L,
7397 BasicBlock *ExitingBlock,
7398 bool ControlsExit) {
7399 assert(!L->contains(ExitingBlock) && "Not an exiting block!");
7401 // Give up if the exit is the default dest of a switch.
7402 if (Switch->getDefaultDest() == ExitingBlock)
7403 return getCouldNotCompute();
7405 assert(L->contains(Switch->getDefaultDest()) &&
7406 "Default case must not exit the loop!");
7407 const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L);
7408 const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock));
7410 // while (X != Y) --> while (X-Y != 0)
7411 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsExit);
7412 if (EL.hasAnyInfo())
7415 return getCouldNotCompute();
7418 static ConstantInt *
7419 EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
7420 ScalarEvolution &SE) {
7421 const SCEV *InVal = SE.getConstant(C);
7422 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
7423 assert(isa<SCEVConstant>(Val) &&
7424 "Evaluation of SCEV at constant didn't fold correctly?");
7425 return cast<SCEVConstant>(Val)->getValue();
7428 /// Given an exit condition of 'icmp op load X, cst', try to see if we can
7429 /// compute the backedge execution count.
7430 ScalarEvolution::ExitLimit
7431 ScalarEvolution::computeLoadConstantCompareExitLimit(
7435 ICmpInst::Predicate predicate) {
7436 if (LI->isVolatile()) return getCouldNotCompute();
7438 // Check to see if the loaded pointer is a getelementptr of a global.
7439 // TODO: Use SCEV instead of manually grubbing with GEPs.
7440 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0));
7441 if (!GEP) return getCouldNotCompute();
7443 // Make sure that it is really a constant global we are gepping, with an
7444 // initializer, and make sure the first IDX is really 0.
7445 GlobalVariable *GV = dyn_cast<GlobalVariable>(GEP->getOperand(0));
7446 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer() ||
7447 GEP->getNumOperands() < 3 || !isa<Constant>(GEP->getOperand(1)) ||
7448 !cast<Constant>(GEP->getOperand(1))->isNullValue())
7449 return getCouldNotCompute();
7451 // Okay, we allow one non-constant index into the GEP instruction.
7452 Value *VarIdx = nullptr;
7453 std::vector<Constant*> Indexes;
7454 unsigned VarIdxNum = 0;
7455 for (unsigned i = 2, e = GEP->getNumOperands(); i != e; ++i)
7456 if (ConstantInt *CI = dyn_cast<ConstantInt>(GEP->getOperand(i))) {
7457 Indexes.push_back(CI);
7458 } else if (!isa<ConstantInt>(GEP->getOperand(i))) {
7459 if (VarIdx) return getCouldNotCompute(); // Multiple non-constant idx's.
7460 VarIdx = GEP->getOperand(i);
7462 Indexes.push_back(nullptr);
7465 // Loop-invariant loads may be a byproduct of loop optimization. Skip them.
7467 return getCouldNotCompute();
7469 // Okay, we know we have a (load (gep GV, 0, X)) comparison with a constant.
7470 // Check to see if X is a loop variant variable value now.
7471 const SCEV *Idx = getSCEV(VarIdx);
7472 Idx = getSCEVAtScope(Idx, L);
7474 // We can only recognize very limited forms of loop index expressions, in
7475 // particular, only affine AddRec's like {C1,+,C2}.
7476 const SCEVAddRecExpr *IdxExpr = dyn_cast<SCEVAddRecExpr>(Idx);
7477 if (!IdxExpr || !IdxExpr->isAffine() || isLoopInvariant(IdxExpr, L) ||
7478 !isa<SCEVConstant>(IdxExpr->getOperand(0)) ||
7479 !isa<SCEVConstant>(IdxExpr->getOperand(1)))
7480 return getCouldNotCompute();
7482 unsigned MaxSteps = MaxBruteForceIterations;
7483 for (unsigned IterationNum = 0; IterationNum != MaxSteps; ++IterationNum) {
7484 ConstantInt *ItCst = ConstantInt::get(
7485 cast<IntegerType>(IdxExpr->getType()), IterationNum);
7486 ConstantInt *Val = EvaluateConstantChrecAtConstant(IdxExpr, ItCst, *this);
7488 // Form the GEP offset.
7489 Indexes[VarIdxNum] = Val;
7491 Constant *Result = ConstantFoldLoadThroughGEPIndices(GV->getInitializer(),
7493 if (!Result) break; // Cannot compute!
7495 // Evaluate the condition for this iteration.
7496 Result = ConstantExpr::getICmp(predicate, Result, RHS);
7497 if (!isa<ConstantInt>(Result)) break; // Couldn't decide for sure
7498 if (cast<ConstantInt>(Result)->getValue().isMinValue()) {
7499 ++NumArrayLenItCounts;
7500 return getConstant(ItCst); // Found terminating iteration!
7503 return getCouldNotCompute();
7506 ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit(
7507 Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) {
7508 ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV);
7510 return getCouldNotCompute();
7512 const BasicBlock *Latch = L->getLoopLatch();
7514 return getCouldNotCompute();
7516 const BasicBlock *Predecessor = L->getLoopPredecessor();
7518 return getCouldNotCompute();
7520 // Return true if V is of the form "LHS `shift_op` <positive constant>".
7521 // Return LHS in OutLHS and shift_opt in OutOpCode.
7522 auto MatchPositiveShift =
7523 [](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) {
7525 using namespace PatternMatch;
7527 ConstantInt *ShiftAmt;
7528 if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7529 OutOpCode = Instruction::LShr;
7530 else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7531 OutOpCode = Instruction::AShr;
7532 else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
7533 OutOpCode = Instruction::Shl;
7537 return ShiftAmt->getValue().isStrictlyPositive();
7540 // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in
7543 // %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ]
7544 // %iv.shifted = lshr i32 %iv, <positive constant>
7546 // Return true on a successful match. Return the corresponding PHI node (%iv
7547 // above) in PNOut and the opcode of the shift operation in OpCodeOut.
7548 auto MatchShiftRecurrence =
7549 [&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) {
7550 Optional<Instruction::BinaryOps> PostShiftOpCode;
7553 Instruction::BinaryOps OpC;
7556 // If we encounter a shift instruction, "peel off" the shift operation,
7557 // and remember that we did so. Later when we inspect %iv's backedge
7558 // value, we will make sure that the backedge value uses the same
7561 // Note: the peeled shift operation does not have to be the same
7562 // instruction as the one feeding into the PHI's backedge value. We only
7563 // really care about it being the same *kind* of shift instruction --
7564 // that's all that is required for our later inferences to hold.
7565 if (MatchPositiveShift(LHS, V, OpC)) {
7566 PostShiftOpCode = OpC;
7571 PNOut = dyn_cast<PHINode>(LHS);
7572 if (!PNOut || PNOut->getParent() != L->getHeader())
7575 Value *BEValue = PNOut->getIncomingValueForBlock(Latch);
7579 // The backedge value for the PHI node must be a shift by a positive
7581 MatchPositiveShift(BEValue, OpLHS, OpCodeOut) &&
7583 // of the PHI node itself
7586 // and the kind of shift should be match the kind of shift we peeled
7588 (!PostShiftOpCode.hasValue() || *PostShiftOpCode == OpCodeOut);
7592 Instruction::BinaryOps OpCode;
7593 if (!MatchShiftRecurrence(LHS, PN, OpCode))
7594 return getCouldNotCompute();
7596 const DataLayout &DL = getDataLayout();
7598 // The key rationale for this optimization is that for some kinds of shift
7599 // recurrences, the value of the recurrence "stabilizes" to either 0 or -1
7600 // within a finite number of iterations. If the condition guarding the
7601 // backedge (in the sense that the backedge is taken if the condition is true)
7602 // is false for the value the shift recurrence stabilizes to, then we know
7603 // that the backedge is taken only a finite number of times.
7605 ConstantInt *StableValue = nullptr;
7608 llvm_unreachable("Impossible case!");
7610 case Instruction::AShr: {
7611 // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most
7612 // bitwidth(K) iterations.
7613 Value *FirstValue = PN->getIncomingValueForBlock(Predecessor);
7614 KnownBits Known = computeKnownBits(FirstValue, DL, 0, nullptr,
7615 Predecessor->getTerminator(), &DT);
7616 auto *Ty = cast<IntegerType>(RHS->getType());
7617 if (Known.isNonNegative())
7618 StableValue = ConstantInt::get(Ty, 0);
7619 else if (Known.isNegative())
7620 StableValue = ConstantInt::get(Ty, -1, true);
7622 return getCouldNotCompute();
7626 case Instruction::LShr:
7627 case Instruction::Shl:
7628 // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>}
7629 // stabilize to 0 in at most bitwidth(K) iterations.
7630 StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0);
7635 ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI);
7636 assert(Result->getType()->isIntegerTy(1) &&
7637 "Otherwise cannot be an operand to a branch instruction");
7639 if (Result->isZeroValue()) {
7640 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
7641 const SCEV *UpperBound =
7642 getConstant(getEffectiveSCEVType(RHS->getType()), BitWidth);
7643 return ExitLimit(getCouldNotCompute(), UpperBound, false);
7646 return getCouldNotCompute();
7649 /// Return true if we can constant fold an instruction of the specified type,
7650 /// assuming that all operands were constants.
7651 static bool CanConstantFold(const Instruction *I) {
7652 if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
7653 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
7657 if (const CallInst *CI = dyn_cast<CallInst>(I))
7658 if (const Function *F = CI->getCalledFunction())
7659 return canConstantFoldCallTo(CI, F);
7663 /// Determine whether this instruction can constant evolve within this loop
7664 /// assuming its operands can all constant evolve.
7665 static bool canConstantEvolve(Instruction *I, const Loop *L) {
7666 // An instruction outside of the loop can't be derived from a loop PHI.
7667 if (!L->contains(I)) return false;
7669 if (isa<PHINode>(I)) {
7670 // We don't currently keep track of the control flow needed to evaluate
7671 // PHIs, so we cannot handle PHIs inside of loops.
7672 return L->getHeader() == I->getParent();
7675 // If we won't be able to constant fold this expression even if the operands
7676 // are constants, bail early.
7677 return CanConstantFold(I);
7680 /// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
7681 /// recursing through each instruction operand until reaching a loop header phi.
7683 getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L,
7684 DenseMap<Instruction *, PHINode *> &PHIMap,
7686 if (Depth > MaxConstantEvolvingDepth)
7689 // Otherwise, we can evaluate this instruction if all of its operands are
7690 // constant or derived from a PHI node themselves.
7691 PHINode *PHI = nullptr;
7692 for (Value *Op : UseInst->operands()) {
7693 if (isa<Constant>(Op)) continue;
7695 Instruction *OpInst = dyn_cast<Instruction>(Op);
7696 if (!OpInst || !canConstantEvolve(OpInst, L)) return nullptr;
7698 PHINode *P = dyn_cast<PHINode>(OpInst);
7700 // If this operand is already visited, reuse the prior result.
7701 // We may have P != PHI if this is the deepest point at which the
7702 // inconsistent paths meet.
7703 P = PHIMap.lookup(OpInst);
7705 // Recurse and memoize the results, whether a phi is found or not.
7706 // This recursive call invalidates pointers into PHIMap.
7707 P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, Depth + 1);
7711 return nullptr; // Not evolving from PHI
7712 if (PHI && PHI != P)
7713 return nullptr; // Evolving from multiple different PHIs.
7716 // This is a expression evolving from a constant PHI!
7720 /// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
7721 /// in the loop that V is derived from. We allow arbitrary operations along the
7722 /// way, but the operands of an operation must either be constants or a value
7723 /// derived from a constant PHI. If this expression does not fit with these
7724 /// constraints, return null.
7725 static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
7726 Instruction *I = dyn_cast<Instruction>(V);
7727 if (!I || !canConstantEvolve(I, L)) return nullptr;
7729 if (PHINode *PN = dyn_cast<PHINode>(I))
7732 // Record non-constant instructions contained by the loop.
7733 DenseMap<Instruction *, PHINode *> PHIMap;
7734 return getConstantEvolvingPHIOperands(I, L, PHIMap, 0);
7737 /// EvaluateExpression - Given an expression that passes the
7738 /// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
7739 /// in the loop has the value PHIVal. If we can't fold this expression for some
7740 /// reason, return null.
7741 static Constant *EvaluateExpression(Value *V, const Loop *L,
7742 DenseMap<Instruction *, Constant *> &Vals,
7743 const DataLayout &DL,
7744 const TargetLibraryInfo *TLI) {
7745 // Convenient constant check, but redundant for recursive calls.
7746 if (Constant *C = dyn_cast<Constant>(V)) return C;
7747 Instruction *I = dyn_cast<Instruction>(V);
7748 if (!I) return nullptr;
7750 if (Constant *C = Vals.lookup(I)) return C;
7752 // An instruction inside the loop depends on a value outside the loop that we
7753 // weren't given a mapping for, or a value such as a call inside the loop.
7754 if (!canConstantEvolve(I, L)) return nullptr;
7756 // An unmapped PHI can be due to a branch or another loop inside this loop,
7757 // or due to this not being the initial iteration through a loop where we
7758 // couldn't compute the evolution of this particular PHI last time.
7759 if (isa<PHINode>(I)) return nullptr;
7761 std::vector<Constant*> Operands(I->getNumOperands());
7763 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
7764 Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i));
7766 Operands[i] = dyn_cast<Constant>(I->getOperand(i));
7767 if (!Operands[i]) return nullptr;
7770 Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI);
7772 if (!C) return nullptr;
7776 if (CmpInst *CI = dyn_cast<CmpInst>(I))
7777 return ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
7778 Operands[1], DL, TLI);
7779 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
7780 if (!LI->isVolatile())
7781 return ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
7783 return ConstantFoldInstOperands(I, Operands, DL, TLI);
7787 // If every incoming value to PN except the one for BB is a specific Constant,
7788 // return that, else return nullptr.
7789 static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) {
7790 Constant *IncomingVal = nullptr;
7792 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
7793 if (PN->getIncomingBlock(i) == BB)
7796 auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i));
7800 if (IncomingVal != CurrentVal) {
7803 IncomingVal = CurrentVal;
7810 /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
7811 /// in the header of its containing loop, we know the loop executes a
7812 /// constant number of times, and the PHI node is just a recurrence
7813 /// involving constants, fold it.
7815 ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
7818 auto I = ConstantEvolutionLoopExitValue.find(PN);
7819 if (I != ConstantEvolutionLoopExitValue.end())
7822 if (BEs.ugt(MaxBruteForceIterations))
7823 return ConstantEvolutionLoopExitValue[PN] = nullptr; // Not going to evaluate it.
7825 Constant *&RetVal = ConstantEvolutionLoopExitValue[PN];
7827 DenseMap<Instruction *, Constant *> CurrentIterVals;
7828 BasicBlock *Header = L->getHeader();
7829 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
7831 BasicBlock *Latch = L->getLoopLatch();
7835 for (PHINode &PHI : Header->phis()) {
7836 if (auto *StartCST = getOtherIncomingValue(&PHI, Latch))
7837 CurrentIterVals[&PHI] = StartCST;
7839 if (!CurrentIterVals.count(PN))
7840 return RetVal = nullptr;
7842 Value *BEValue = PN->getIncomingValueForBlock(Latch);
7844 // Execute the loop symbolically to determine the exit value.
7845 assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) &&
7846 "BEs is <= MaxBruteForceIterations which is an 'unsigned'!");
7848 unsigned NumIterations = BEs.getZExtValue(); // must be in range
7849 unsigned IterationNum = 0;
7850 const DataLayout &DL = getDataLayout();
7851 for (; ; ++IterationNum) {
7852 if (IterationNum == NumIterations)
7853 return RetVal = CurrentIterVals[PN]; // Got exit value!
7855 // Compute the value of the PHIs for the next iteration.
7856 // EvaluateExpression adds non-phi values to the CurrentIterVals map.
7857 DenseMap<Instruction *, Constant *> NextIterVals;
7859 EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
7861 return nullptr; // Couldn't evaluate!
7862 NextIterVals[PN] = NextPHI;
7864 bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
7866 // Also evaluate the other PHI nodes. However, we don't get to stop if we
7867 // cease to be able to evaluate one of them or if they stop evolving,
7868 // because that doesn't necessarily prevent us from computing PN.
7869 SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute;
7870 for (const auto &I : CurrentIterVals) {
7871 PHINode *PHI = dyn_cast<PHINode>(I.first);
7872 if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
7873 PHIsToCompute.emplace_back(PHI, I.second);
7875 // We use two distinct loops because EvaluateExpression may invalidate any
7876 // iterators into CurrentIterVals.
7877 for (const auto &I : PHIsToCompute) {
7878 PHINode *PHI = I.first;
7879 Constant *&NextPHI = NextIterVals[PHI];
7880 if (!NextPHI) { // Not already computed.
7881 Value *BEValue = PHI->getIncomingValueForBlock(Latch);
7882 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
7884 if (NextPHI != I.second)
7885 StoppedEvolving = false;
7888 // If all entries in CurrentIterVals == NextIterVals then we can stop
7889 // iterating, the loop can't continue to change.
7890 if (StoppedEvolving)
7891 return RetVal = CurrentIterVals[PN];
7893 CurrentIterVals.swap(NextIterVals);
7897 const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L,
7900 PHINode *PN = getConstantEvolvingPHI(Cond, L);
7901 if (!PN) return getCouldNotCompute();
7903 // If the loop is canonicalized, the PHI will have exactly two entries.
7904 // That's the only form we support here.
7905 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
7907 DenseMap<Instruction *, Constant *> CurrentIterVals;
7908 BasicBlock *Header = L->getHeader();
7909 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
7911 BasicBlock *Latch = L->getLoopLatch();
7912 assert(Latch && "Should follow from NumIncomingValues == 2!");
7914 for (PHINode &PHI : Header->phis()) {
7915 if (auto *StartCST = getOtherIncomingValue(&PHI, Latch))
7916 CurrentIterVals[&PHI] = StartCST;
7918 if (!CurrentIterVals.count(PN))
7919 return getCouldNotCompute();
7921 // Okay, we find a PHI node that defines the trip count of this loop. Execute
7922 // the loop symbolically to determine when the condition gets a value of
7924 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
7925 const DataLayout &DL = getDataLayout();
7926 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
7927 auto *CondVal = dyn_cast_or_null<ConstantInt>(
7928 EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI));
7930 // Couldn't symbolically evaluate.
7931 if (!CondVal) return getCouldNotCompute();
7933 if (CondVal->getValue() == uint64_t(ExitWhen)) {
7934 ++NumBruteForceTripCountsComputed;
7935 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
7938 // Update all the PHI nodes for the next iteration.
7939 DenseMap<Instruction *, Constant *> NextIterVals;
7941 // Create a list of which PHIs we need to compute. We want to do this before
7942 // calling EvaluateExpression on them because that may invalidate iterators
7943 // into CurrentIterVals.
7944 SmallVector<PHINode *, 8> PHIsToCompute;
7945 for (const auto &I : CurrentIterVals) {
7946 PHINode *PHI = dyn_cast<PHINode>(I.first);
7947 if (!PHI || PHI->getParent() != Header) continue;
7948 PHIsToCompute.push_back(PHI);
7950 for (PHINode *PHI : PHIsToCompute) {
7951 Constant *&NextPHI = NextIterVals[PHI];
7952 if (NextPHI) continue; // Already computed!
7954 Value *BEValue = PHI->getIncomingValueForBlock(Latch);
7955 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
7957 CurrentIterVals.swap(NextIterVals);
7960 // Too many iterations were needed to evaluate.
7961 return getCouldNotCompute();
7964 const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
7965 SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values =
7967 // Check to see if we've folded this expression at this loop before.
7968 for (auto &LS : Values)
7970 return LS.second ? LS.second : V;
7972 Values.emplace_back(L, nullptr);
7974 // Otherwise compute it.
7975 const SCEV *C = computeSCEVAtScope(V, L);
7976 for (auto &LS : reverse(ValuesAtScopes[V]))
7977 if (LS.first == L) {
7984 /// This builds up a Constant using the ConstantExpr interface. That way, we
7985 /// will return Constants for objects which aren't represented by a
7986 /// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
7987 /// Returns NULL if the SCEV isn't representable as a Constant.
7988 static Constant *BuildConstantFromSCEV(const SCEV *V) {
7989 switch (static_cast<SCEVTypes>(V->getSCEVType())) {
7990 case scCouldNotCompute:
7994 return cast<SCEVConstant>(V)->getValue();
7996 return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
7997 case scSignExtend: {
7998 const SCEVSignExtendExpr *SS = cast<SCEVSignExtendExpr>(V);
7999 if (Constant *CastOp = BuildConstantFromSCEV(SS->getOperand()))
8000 return ConstantExpr::getSExt(CastOp, SS->getType());
8003 case scZeroExtend: {
8004 const SCEVZeroExtendExpr *SZ = cast<SCEVZeroExtendExpr>(V);
8005 if (Constant *CastOp = BuildConstantFromSCEV(SZ->getOperand()))
8006 return ConstantExpr::getZExt(CastOp, SZ->getType());
8010 const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
8011 if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
8012 return ConstantExpr::getTrunc(CastOp, ST->getType());
8016 const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
8017 if (Constant *C = BuildConstantFromSCEV(SA->getOperand(0))) {
8018 if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
8019 unsigned AS = PTy->getAddressSpace();
8020 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
8021 C = ConstantExpr::getBitCast(C, DestPtrTy);
8023 for (unsigned i = 1, e = SA->getNumOperands(); i != e; ++i) {
8024 Constant *C2 = BuildConstantFromSCEV(SA->getOperand(i));
8025 if (!C2) return nullptr;
8028 if (!C->getType()->isPointerTy() && C2->getType()->isPointerTy()) {
8029 unsigned AS = C2->getType()->getPointerAddressSpace();
8031 Type *DestPtrTy = Type::getInt8PtrTy(C->getContext(), AS);
8032 // The offsets have been converted to bytes. We can add bytes to an
8033 // i8* by GEP with the byte count in the first index.
8034 C = ConstantExpr::getBitCast(C, DestPtrTy);
8037 // Don't bother trying to sum two pointers. We probably can't
8038 // statically compute a load that results from it anyway.
8039 if (C2->getType()->isPointerTy())
8042 if (PointerType *PTy = dyn_cast<PointerType>(C->getType())) {
8043 if (PTy->getElementType()->isStructTy())
8044 C2 = ConstantExpr::getIntegerCast(
8045 C2, Type::getInt32Ty(C->getContext()), true);
8046 C = ConstantExpr::getGetElementPtr(PTy->getElementType(), C, C2);
8048 C = ConstantExpr::getAdd(C, C2);
8055 const SCEVMulExpr *SM = cast<SCEVMulExpr>(V);
8056 if (Constant *C = BuildConstantFromSCEV(SM->getOperand(0))) {
8057 // Don't bother with pointers at all.
8058 if (C->getType()->isPointerTy()) return nullptr;
8059 for (unsigned i = 1, e = SM->getNumOperands(); i != e; ++i) {
8060 Constant *C2 = BuildConstantFromSCEV(SM->getOperand(i));
8061 if (!C2 || C2->getType()->isPointerTy()) return nullptr;
8062 C = ConstantExpr::getMul(C, C2);
8069 const SCEVUDivExpr *SU = cast<SCEVUDivExpr>(V);
8070 if (Constant *LHS = BuildConstantFromSCEV(SU->getLHS()))
8071 if (Constant *RHS = BuildConstantFromSCEV(SU->getRHS()))
8072 if (LHS->getType() == RHS->getType())
8073 return ConstantExpr::getUDiv(LHS, RHS);
8078 break; // TODO: smax, umax.
8083 const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
8084 if (isa<SCEVConstant>(V)) return V;
8086 // If this instruction is evolved from a constant-evolving PHI, compute the
8087 // exit value from the loop without using SCEVs.
8088 if (const SCEVUnknown *SU = dyn_cast<SCEVUnknown>(V)) {
8089 if (Instruction *I = dyn_cast<Instruction>(SU->getValue())) {
8090 const Loop *LI = this->LI[I->getParent()];
8091 if (LI && LI->getParentLoop() == L) // Looking for loop exit value.
8092 if (PHINode *PN = dyn_cast<PHINode>(I))
8093 if (PN->getParent() == LI->getHeader()) {
8094 // Okay, there is no closed form solution for the PHI node. Check
8095 // to see if the loop that contains it has a known backedge-taken
8096 // count. If so, we may be able to force computation of the exit
8098 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(LI);
8099 if (const SCEVConstant *BTCC =
8100 dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
8102 // This trivial case can show up in some degenerate cases where
8103 // the incoming IR has not yet been fully simplified.
8104 if (BTCC->getValue()->isZero()) {
8105 Value *InitValue = nullptr;
8106 bool MultipleInitValues = false;
8107 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
8108 if (!LI->contains(PN->getIncomingBlock(i))) {
8110 InitValue = PN->getIncomingValue(i);
8111 else if (InitValue != PN->getIncomingValue(i)) {
8112 MultipleInitValues = true;
8116 if (!MultipleInitValues && InitValue)
8117 return getSCEV(InitValue);
8120 // Okay, we know how many times the containing loop executes. If
8121 // this is a constant evolving PHI node, get the final value at
8122 // the specified iteration number.
8124 getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), LI);
8125 if (RV) return getSCEV(RV);
8129 // Okay, this is an expression that we cannot symbolically evaluate
8130 // into a SCEV. Check to see if it's possible to symbolically evaluate
8131 // the arguments into constants, and if so, try to constant propagate the
8132 // result. This is particularly useful for computing loop exit values.
8133 if (CanConstantFold(I)) {
8134 SmallVector<Constant *, 4> Operands;
8135 bool MadeImprovement = false;
8136 for (Value *Op : I->operands()) {
8137 if (Constant *C = dyn_cast<Constant>(Op)) {
8138 Operands.push_back(C);
8142 // If any of the operands is non-constant and if they are
8143 // non-integer and non-pointer, don't even try to analyze them
8144 // with scev techniques.
8145 if (!isSCEVable(Op->getType()))
8148 const SCEV *OrigV = getSCEV(Op);
8149 const SCEV *OpV = getSCEVAtScope(OrigV, L);
8150 MadeImprovement |= OrigV != OpV;
8152 Constant *C = BuildConstantFromSCEV(OpV);
8154 if (C->getType() != Op->getType())
8155 C = ConstantExpr::getCast(CastInst::getCastOpcode(C, false,
8159 Operands.push_back(C);
8162 // Check to see if getSCEVAtScope actually made an improvement.
8163 if (MadeImprovement) {
8164 Constant *C = nullptr;
8165 const DataLayout &DL = getDataLayout();
8166 if (const CmpInst *CI = dyn_cast<CmpInst>(I))
8167 C = ConstantFoldCompareInstOperands(CI->getPredicate(), Operands[0],
8168 Operands[1], DL, &TLI);
8169 else if (const LoadInst *LI = dyn_cast<LoadInst>(I)) {
8170 if (!LI->isVolatile())
8171 C = ConstantFoldLoadFromConstPtr(Operands[0], LI->getType(), DL);
8173 C = ConstantFoldInstOperands(I, Operands, DL, &TLI);
8180 // This is some other type of SCEVUnknown, just return it.
8184 if (const SCEVCommutativeExpr *Comm = dyn_cast<SCEVCommutativeExpr>(V)) {
8185 // Avoid performing the look-up in the common case where the specified
8186 // expression has no loop-variant portions.
8187 for (unsigned i = 0, e = Comm->getNumOperands(); i != e; ++i) {
8188 const SCEV *OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
8189 if (OpAtScope != Comm->getOperand(i)) {
8190 // Okay, at least one of these operands is loop variant but might be
8191 // foldable. Build a new instance of the folded commutative expression.
8192 SmallVector<const SCEV *, 8> NewOps(Comm->op_begin(),
8193 Comm->op_begin()+i);
8194 NewOps.push_back(OpAtScope);
8196 for (++i; i != e; ++i) {
8197 OpAtScope = getSCEVAtScope(Comm->getOperand(i), L);
8198 NewOps.push_back(OpAtScope);
8200 if (isa<SCEVAddExpr>(Comm))
8201 return getAddExpr(NewOps);
8202 if (isa<SCEVMulExpr>(Comm))
8203 return getMulExpr(NewOps);
8204 if (isa<SCEVSMaxExpr>(Comm))
8205 return getSMaxExpr(NewOps);
8206 if (isa<SCEVUMaxExpr>(Comm))
8207 return getUMaxExpr(NewOps);
8208 llvm_unreachable("Unknown commutative SCEV type!");
8211 // If we got here, all operands are loop invariant.
8215 if (const SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
8216 const SCEV *LHS = getSCEVAtScope(Div->getLHS(), L);
8217 const SCEV *RHS = getSCEVAtScope(Div->getRHS(), L);
8218 if (LHS == Div->getLHS() && RHS == Div->getRHS())
8219 return Div; // must be loop invariant
8220 return getUDivExpr(LHS, RHS);
8223 // If this is a loop recurrence for a loop that does not contain L, then we
8224 // are dealing with the final value computed by the loop.
8225 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
8226 // First, attempt to evaluate each operand.
8227 // Avoid performing the look-up in the common case where the specified
8228 // expression has no loop-variant portions.
8229 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
8230 const SCEV *OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L);
8231 if (OpAtScope == AddRec->getOperand(i))
8234 // Okay, at least one of these operands is loop variant but might be
8235 // foldable. Build a new instance of the folded commutative expression.
8236 SmallVector<const SCEV *, 8> NewOps(AddRec->op_begin(),
8237 AddRec->op_begin()+i);
8238 NewOps.push_back(OpAtScope);
8239 for (++i; i != e; ++i)
8240 NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L));
8242 const SCEV *FoldedRec =
8243 getAddRecExpr(NewOps, AddRec->getLoop(),
8244 AddRec->getNoWrapFlags(SCEV::FlagNW));
8245 AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec);
8246 // The addrec may be folded to a nonrecurrence, for example, if the
8247 // induction variable is multiplied by zero after constant folding. Go
8248 // ahead and return the folded value.
8254 // If the scope is outside the addrec's loop, evaluate it by using the
8255 // loop exit value of the addrec.
8256 if (!AddRec->getLoop()->contains(L)) {
8257 // To evaluate this recurrence, we need to know how many times the AddRec
8258 // loop iterates. Compute this now.
8259 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(AddRec->getLoop());
8260 if (BackedgeTakenCount == getCouldNotCompute()) return AddRec;
8262 // Then, evaluate the AddRec.
8263 return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
8269 if (const SCEVZeroExtendExpr *Cast = dyn_cast<SCEVZeroExtendExpr>(V)) {
8270 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
8271 if (Op == Cast->getOperand())
8272 return Cast; // must be loop invariant
8273 return getZeroExtendExpr(Op, Cast->getType());
8276 if (const SCEVSignExtendExpr *Cast = dyn_cast<SCEVSignExtendExpr>(V)) {
8277 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
8278 if (Op == Cast->getOperand())
8279 return Cast; // must be loop invariant
8280 return getSignExtendExpr(Op, Cast->getType());
8283 if (const SCEVTruncateExpr *Cast = dyn_cast<SCEVTruncateExpr>(V)) {
8284 const SCEV *Op = getSCEVAtScope(Cast->getOperand(), L);
8285 if (Op == Cast->getOperand())
8286 return Cast; // must be loop invariant
8287 return getTruncateExpr(Op, Cast->getType());
8290 llvm_unreachable("Unknown SCEV type!");
8293 const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
8294 return getSCEVAtScope(getSCEV(V), L);
8297 const SCEV *ScalarEvolution::stripInjectiveFunctions(const SCEV *S) const {
8298 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S))
8299 return stripInjectiveFunctions(ZExt->getOperand());
8300 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S))
8301 return stripInjectiveFunctions(SExt->getOperand());
8305 /// Finds the minimum unsigned root of the following equation:
8307 /// A * X = B (mod N)
8309 /// where N = 2^BW and BW is the common bit width of A and B. The signedness of
8310 /// A and B isn't important.
8312 /// If the equation does not have a solution, SCEVCouldNotCompute is returned.
8313 static const SCEV *SolveLinEquationWithOverflow(const APInt &A, const SCEV *B,
8314 ScalarEvolution &SE) {
8315 uint32_t BW = A.getBitWidth();
8316 assert(BW == SE.getTypeSizeInBits(B->getType()));
8317 assert(A != 0 && "A must be non-zero.");
8321 // The gcd of A and N may have only one prime factor: 2. The number of
8322 // trailing zeros in A is its multiplicity
8323 uint32_t Mult2 = A.countTrailingZeros();
8326 // 2. Check if B is divisible by D.
8328 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
8329 // is not less than multiplicity of this prime factor for D.
8330 if (SE.GetMinTrailingZeros(B) < Mult2)
8331 return SE.getCouldNotCompute();
8333 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
8336 // If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent
8337 // (N / D) in general. The inverse itself always fits into BW bits, though,
8338 // so we immediately truncate it.
8339 APInt AD = A.lshr(Mult2).zext(BW + 1); // AD = A / D
8340 APInt Mod(BW + 1, 0);
8341 Mod.setBit(BW - Mult2); // Mod = N / D
8342 APInt I = AD.multiplicativeInverse(Mod).trunc(BW);
8344 // 4. Compute the minimum unsigned root of the equation:
8345 // I * (B / D) mod (N / D)
8346 // To simplify the computation, we factor out the divide by D:
8347 // (I * B mod N) / D
8348 const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2));
8349 return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D);
8352 /// For a given quadratic addrec, generate coefficients of the corresponding
8353 /// quadratic equation, multiplied by a common value to ensure that they are
8355 /// The returned value is a tuple { A, B, C, M, BitWidth }, where
8356 /// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C
8357 /// were multiplied by, and BitWidth is the bit width of the original addrec
8359 /// This function returns None if the addrec coefficients are not compile-
8361 static Optional<std::tuple<APInt, APInt, APInt, APInt, unsigned>>
8362 GetQuadraticEquation(const SCEVAddRecExpr *AddRec) {
8363 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
8364 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
8365 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
8366 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
8367 LLVM_DEBUG(dbgs() << __func__ << ": analyzing quadratic addrec: "
8368 << *AddRec << '\n');
8370 // We currently can only solve this if the coefficients are constants.
8371 if (!LC || !MC || !NC) {
8372 LLVM_DEBUG(dbgs() << __func__ << ": coefficients are not constant\n");
8376 APInt L = LC->getAPInt();
8377 APInt M = MC->getAPInt();
8378 APInt N = NC->getAPInt();
8379 assert(!N.isNullValue() && "This is not a quadratic addrec");
8381 unsigned BitWidth = LC->getAPInt().getBitWidth();
8382 unsigned NewWidth = BitWidth + 1;
8383 LLVM_DEBUG(dbgs() << __func__ << ": addrec coeff bw: "
8384 << BitWidth << '\n');
8385 // The sign-extension (as opposed to a zero-extension) here matches the
8386 // extension used in SolveQuadraticEquationWrap (with the same motivation).
8387 N = N.sext(NewWidth);
8388 M = M.sext(NewWidth);
8389 L = L.sext(NewWidth);
8391 // The increments are M, M+N, M+2N, ..., so the accumulated values are
8392 // L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is,
8393 // L+M, L+2M+N, L+3M+3N, ...
8394 // After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N.
8396 // The equation Acc = 0 is then
8397 // L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0.
8398 // In a quadratic form it becomes:
8399 // N n^2 + (2M-N) n + 2L = 0.
8402 APInt B = 2 * M - A;
8404 APInt T = APInt(NewWidth, 2);
8405 LLVM_DEBUG(dbgs() << __func__ << ": equation " << A << "x^2 + " << B
8406 << "x + " << C << ", coeff bw: " << NewWidth
8407 << ", multiplied by " << T << '\n');
8408 return std::make_tuple(A, B, C, T, BitWidth);
8411 /// Helper function to compare optional APInts:
8412 /// (a) if X and Y both exist, return min(X, Y),
8413 /// (b) if neither X nor Y exist, return None,
8414 /// (c) if exactly one of X and Y exists, return that value.
8415 static Optional<APInt> MinOptional(Optional<APInt> X, Optional<APInt> Y) {
8416 if (X.hasValue() && Y.hasValue()) {
8417 unsigned W = std::max(X->getBitWidth(), Y->getBitWidth());
8418 APInt XW = X->sextOrSelf(W);
8419 APInt YW = Y->sextOrSelf(W);
8420 return XW.slt(YW) ? *X : *Y;
8422 if (!X.hasValue() && !Y.hasValue())
8424 return X.hasValue() ? *X : *Y;
8427 /// Helper function to truncate an optional APInt to a given BitWidth.
8428 /// When solving addrec-related equations, it is preferable to return a value
8429 /// that has the same bit width as the original addrec's coefficients. If the
8430 /// solution fits in the original bit width, truncate it (except for i1).
8431 /// Returning a value of a different bit width may inhibit some optimizations.
8433 /// In general, a solution to a quadratic equation generated from an addrec
8434 /// may require BW+1 bits, where BW is the bit width of the addrec's
8435 /// coefficients. The reason is that the coefficients of the quadratic
8436 /// equation are BW+1 bits wide (to avoid truncation when converting from
8437 /// the addrec to the equation).
8438 static Optional<APInt> TruncIfPossible(Optional<APInt> X, unsigned BitWidth) {
8441 unsigned W = X->getBitWidth();
8442 if (BitWidth > 1 && BitWidth < W && X->isIntN(BitWidth))
8443 return X->trunc(BitWidth);
8447 /// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n
8448 /// iterations. The values L, M, N are assumed to be signed, and they
8449 /// should all have the same bit widths.
8450 /// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW,
8451 /// where BW is the bit width of the addrec's coefficients.
8452 /// If the calculated value is a BW-bit integer (for BW > 1), it will be
8453 /// returned as such, otherwise the bit width of the returned value may
8454 /// be greater than BW.
8456 /// This function returns None if
8457 /// (a) the addrec coefficients are not constant, or
8458 /// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases
8459 /// like x^2 = 5, no integer solutions exist, in other cases an integer
8460 /// solution may exist, but SolveQuadraticEquationWrap may fail to find it.
8461 static Optional<APInt>
8462 SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
8465 auto T = GetQuadraticEquation(AddRec);
8469 std::tie(A, B, C, M, BitWidth) = *T;
8470 LLVM_DEBUG(dbgs() << __func__ << ": solving for unsigned overflow\n");
8471 Optional<APInt> X = APIntOps::SolveQuadraticEquationWrap(A, B, C, BitWidth+1);
8475 ConstantInt *CX = ConstantInt::get(SE.getContext(), *X);
8476 ConstantInt *V = EvaluateConstantChrecAtConstant(AddRec, CX, SE);
8480 return TruncIfPossible(X, BitWidth);
8483 /// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n
8484 /// iterations. The values M, N are assumed to be signed, and they
8485 /// should all have the same bit widths.
8486 /// Find the least n such that c(n) does not belong to the given range,
8487 /// while c(n-1) does.
8489 /// This function returns None if
8490 /// (a) the addrec coefficients are not constant, or
8491 /// (b) SolveQuadraticEquationWrap was unable to find a solution for the
8492 /// bounds of the range.
8493 static Optional<APInt>
8494 SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec,
8495 const ConstantRange &Range, ScalarEvolution &SE) {
8496 assert(AddRec->getOperand(0)->isZero() &&
8497 "Starting value of addrec should be 0");
8498 LLVM_DEBUG(dbgs() << __func__ << ": solving boundary crossing for range "
8499 << Range << ", addrec " << *AddRec << '\n');
8500 // This case is handled in getNumIterationsInRange. Here we can assume that
8501 // we start in the range.
8502 assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) &&
8503 "Addrec's initial value should be in range");
8507 auto T = GetQuadraticEquation(AddRec);
8511 // Be careful about the return value: there can be two reasons for not
8512 // returning an actual number. First, if no solutions to the equations
8513 // were found, and second, if the solutions don't leave the given range.
8514 // The first case means that the actual solution is "unknown", the second
8515 // means that it's known, but not valid. If the solution is unknown, we
8516 // cannot make any conclusions.
8517 // Return a pair: the optional solution and a flag indicating if the
8518 // solution was found.
8519 auto SolveForBoundary = [&](APInt Bound) -> std::pair<Optional<APInt>,bool> {
8520 // Solve for signed overflow and unsigned overflow, pick the lower
8522 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: checking boundary "
8523 << Bound << " (before multiplying by " << M << ")\n");
8524 Bound *= M; // The quadratic equation multiplier.
8526 Optional<APInt> SO = None;
8528 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
8529 "signed overflow\n");
8530 SO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound, BitWidth);
8532 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
8533 "unsigned overflow\n");
8534 Optional<APInt> UO = APIntOps::SolveQuadraticEquationWrap(A, B, -Bound,
8537 auto LeavesRange = [&] (const APInt &X) {
8538 ConstantInt *C0 = ConstantInt::get(SE.getContext(), X);
8539 ConstantInt *V0 = EvaluateConstantChrecAtConstant(AddRec, C0, SE);
8540 if (Range.contains(V0->getValue()))
8542 // X should be at least 1, so X-1 is non-negative.
8543 ConstantInt *C1 = ConstantInt::get(SE.getContext(), X-1);
8544 ConstantInt *V1 = EvaluateConstantChrecAtConstant(AddRec, C1, SE);
8545 if (Range.contains(V1->getValue()))
8550 // If SolveQuadraticEquationWrap returns None, it means that there can
8551 // be a solution, but the function failed to find it. We cannot treat it
8552 // as "no solution".
8553 if (!SO.hasValue() || !UO.hasValue())
8554 return { None, false };
8556 // Check the smaller value first to see if it leaves the range.
8557 // At this point, both SO and UO must have values.
8558 Optional<APInt> Min = MinOptional(SO, UO);
8559 if (LeavesRange(*Min))
8560 return { Min, true };
8561 Optional<APInt> Max = Min == SO ? UO : SO;
8562 if (LeavesRange(*Max))
8563 return { Max, true };
8565 // Solutions were found, but were eliminated, hence the "true".
8566 return { None, true };
8569 std::tie(A, B, C, M, BitWidth) = *T;
8570 // Lower bound is inclusive, subtract 1 to represent the exiting value.
8571 APInt Lower = Range.getLower().sextOrSelf(A.getBitWidth()) - 1;
8572 APInt Upper = Range.getUpper().sextOrSelf(A.getBitWidth());
8573 auto SL = SolveForBoundary(Lower);
8574 auto SU = SolveForBoundary(Upper);
8575 // If any of the solutions was unknown, no meaninigful conclusions can
8577 if (!SL.second || !SU.second)
8580 // Claim: The correct solution is not some value between Min and Max.
8582 // Justification: Assuming that Min and Max are different values, one of
8583 // them is when the first signed overflow happens, the other is when the
8584 // first unsigned overflow happens. Crossing the range boundary is only
8585 // possible via an overflow (treating 0 as a special case of it, modeling
8586 // an overflow as crossing k*2^W for some k).
8588 // The interesting case here is when Min was eliminated as an invalid
8589 // solution, but Max was not. The argument is that if there was another
8590 // overflow between Min and Max, it would also have been eliminated if
8591 // it was considered.
8593 // For a given boundary, it is possible to have two overflows of the same
8594 // type (signed/unsigned) without having the other type in between: this
8595 // can happen when the vertex of the parabola is between the iterations
8596 // corresponding to the overflows. This is only possible when the two
8597 // overflows cross k*2^W for the same k. In such case, if the second one
8598 // left the range (and was the first one to do so), the first overflow
8599 // would have to enter the range, which would mean that either we had left
8600 // the range before or that we started outside of it. Both of these cases
8601 // are contradictions.
8603 // Claim: In the case where SolveForBoundary returns None, the correct
8604 // solution is not some value between the Max for this boundary and the
8605 // Min of the other boundary.
8607 // Justification: Assume that we had such Max_A and Min_B corresponding
8608 // to range boundaries A and B and such that Max_A < Min_B. If there was
8609 // a solution between Max_A and Min_B, it would have to be caused by an
8610 // overflow corresponding to either A or B. It cannot correspond to B,
8611 // since Min_B is the first occurrence of such an overflow. If it
8612 // corresponded to A, it would have to be either a signed or an unsigned
8613 // overflow that is larger than both eliminated overflows for A. But
8614 // between the eliminated overflows and this overflow, the values would
8615 // cover the entire value space, thus crossing the other boundary, which
8616 // is a contradiction.
8618 return TruncIfPossible(MinOptional(SL.first, SU.first), BitWidth);
8621 ScalarEvolution::ExitLimit
8622 ScalarEvolution::howFarToZero(const SCEV *V, const Loop *L, bool ControlsExit,
8623 bool AllowPredicates) {
8625 // This is only used for loops with a "x != y" exit test. The exit condition
8626 // is now expressed as a single expression, V = x-y. So the exit test is
8627 // effectively V != 0. We know and take advantage of the fact that this
8628 // expression only being used in a comparison by zero context.
8630 SmallPtrSet<const SCEVPredicate *, 4> Predicates;
8631 // If the value is a constant
8632 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
8633 // If the value is already zero, the branch will execute zero times.
8634 if (C->getValue()->isZero()) return C;
8635 return getCouldNotCompute(); // Otherwise it will loop infinitely.
8638 const SCEVAddRecExpr *AddRec =
8639 dyn_cast<SCEVAddRecExpr>(stripInjectiveFunctions(V));
8641 if (!AddRec && AllowPredicates)
8642 // Try to make this an AddRec using runtime tests, in the first X
8643 // iterations of this loop, where X is the SCEV expression found by the
8645 AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates);
8647 if (!AddRec || AddRec->getLoop() != L)
8648 return getCouldNotCompute();
8650 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
8651 // the quadratic equation to solve it.
8652 if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
8653 // We can only use this value if the chrec ends up with an exact zero
8654 // value at this index. When solving for "X*X != 5", for example, we
8655 // should not accept a root of 2.
8656 if (auto S = SolveQuadraticAddRecExact(AddRec, *this)) {
8657 const auto *R = cast<SCEVConstant>(getConstant(S.getValue()));
8658 return ExitLimit(R, R, false, Predicates);
8660 return getCouldNotCompute();
8663 // Otherwise we can only handle this if it is affine.
8664 if (!AddRec->isAffine())
8665 return getCouldNotCompute();
8667 // If this is an affine expression, the execution count of this branch is
8668 // the minimum unsigned root of the following equation:
8670 // Start + Step*N = 0 (mod 2^BW)
8674 // Step*N = -Start (mod 2^BW)
8676 // where BW is the common bit width of Start and Step.
8678 // Get the initial value for the loop.
8679 const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
8680 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
8682 // For now we handle only constant steps.
8684 // TODO: Handle a nonconstant Step given AddRec<NUW>. If the
8685 // AddRec is NUW, then (in an unsigned sense) it cannot be counting up to wrap
8686 // to 0, it must be counting down to equal 0. Consequently, N = Start / -Step.
8687 // We have not yet seen any such cases.
8688 const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
8689 if (!StepC || StepC->getValue()->isZero())
8690 return getCouldNotCompute();
8692 // For positive steps (counting up until unsigned overflow):
8693 // N = -Start/Step (as unsigned)
8694 // For negative steps (counting down to zero):
8696 // First compute the unsigned distance from zero in the direction of Step.
8697 bool CountDown = StepC->getAPInt().isNegative();
8698 const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start);
8700 // Handle unitary steps, which cannot wraparound.
8701 // 1*N = -Start; -1*N = Start (mod 2^BW), so:
8702 // N = Distance (as unsigned)
8703 if (StepC->getValue()->isOne() || StepC->getValue()->isMinusOne()) {
8704 APInt MaxBECount = getUnsignedRangeMax(Distance);
8706 // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
8707 // we end up with a loop whose backedge-taken count is n - 1. Detect this
8708 // case, and see if we can improve the bound.
8710 // Explicitly handling this here is necessary because getUnsignedRange
8711 // isn't context-sensitive; it doesn't know that we only care about the
8712 // range inside the loop.
8713 const SCEV *Zero = getZero(Distance->getType());
8714 const SCEV *One = getOne(Distance->getType());
8715 const SCEV *DistancePlusOne = getAddExpr(Distance, One);
8716 if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
8717 // If Distance + 1 doesn't overflow, we can compute the maximum distance
8718 // as "unsigned_max(Distance + 1) - 1".
8719 ConstantRange CR = getUnsignedRange(DistancePlusOne);
8720 MaxBECount = APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1);
8722 return ExitLimit(Distance, getConstant(MaxBECount), false, Predicates);
8725 // If the condition controls loop exit (the loop exits only if the expression
8726 // is true) and the addition is no-wrap we can use unsigned divide to
8727 // compute the backedge count. In this case, the step may not divide the
8728 // distance, but we don't care because if the condition is "missed" the loop
8729 // will have undefined behavior due to wrapping.
8730 if (ControlsExit && AddRec->hasNoSelfWrap() &&
8731 loopHasNoAbnormalExits(AddRec->getLoop())) {
8733 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
8735 Exact == getCouldNotCompute()
8737 : getConstant(getUnsignedRangeMax(Exact));
8738 return ExitLimit(Exact, Max, false, Predicates);
8741 // Solve the general equation.
8742 const SCEV *E = SolveLinEquationWithOverflow(StepC->getAPInt(),
8743 getNegativeSCEV(Start), *this);
8744 const SCEV *M = E == getCouldNotCompute()
8746 : getConstant(getUnsignedRangeMax(E));
8747 return ExitLimit(E, M, false, Predicates);
8750 ScalarEvolution::ExitLimit
8751 ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) {
8752 // Loops that look like: while (X == 0) are very strange indeed. We don't
8753 // handle them yet except for the trivial case. This could be expanded in the
8754 // future as needed.
8756 // If the value is a constant, check to see if it is known to be non-zero
8757 // already. If so, the backedge will execute zero times.
8758 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
8759 if (!C->getValue()->isZero())
8760 return getZero(C->getType());
8761 return getCouldNotCompute(); // Otherwise it will loop infinitely.
8764 // We could implement others, but I really doubt anyone writes loops like
8765 // this, and if they did, they would already be constant folded.
8766 return getCouldNotCompute();
8769 std::pair<BasicBlock *, BasicBlock *>
8770 ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
8771 // If the block has a unique predecessor, then there is no path from the
8772 // predecessor to the block that does not go through the direct edge
8773 // from the predecessor to the block.
8774 if (BasicBlock *Pred = BB->getSinglePredecessor())
8777 // A loop's header is defined to be a block that dominates the loop.
8778 // If the header has a unique predecessor outside the loop, it must be
8779 // a block that has exactly one successor that can reach the loop.
8780 if (Loop *L = LI.getLoopFor(BB))
8781 return {L->getLoopPredecessor(), L->getHeader()};
8783 return {nullptr, nullptr};
8786 /// SCEV structural equivalence is usually sufficient for testing whether two
8787 /// expressions are equal, however for the purposes of looking for a condition
8788 /// guarding a loop, it can be useful to be a little more general, since a
8789 /// front-end may have replicated the controlling expression.
8790 static bool HasSameValue(const SCEV *A, const SCEV *B) {
8791 // Quick check to see if they are the same SCEV.
8792 if (A == B) return true;
8794 auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) {
8795 // Not all instructions that are "identical" compute the same value. For
8796 // instance, two distinct alloca instructions allocating the same type are
8797 // identical and do not read memory; but compute distinct values.
8798 return A->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A));
8801 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
8802 // two different instructions with the same value. Check for this case.
8803 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
8804 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
8805 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
8806 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
8807 if (ComputesEqualValues(AI, BI))
8810 // Otherwise assume they may have a different value.
8814 bool ScalarEvolution::SimplifyICmpOperands(ICmpInst::Predicate &Pred,
8815 const SCEV *&LHS, const SCEV *&RHS,
8817 bool Changed = false;
8818 // Simplifies ICMP to trivial true or false by turning it into '0 == 0' or
8820 auto TrivialCase = [&](bool TriviallyTrue) {
8821 LHS = RHS = getConstant(ConstantInt::getFalse(getContext()));
8822 Pred = TriviallyTrue ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
8825 // If we hit the max recursion limit bail out.
8829 // Canonicalize a constant to the right side.
8830 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
8831 // Check for both operands constant.
8832 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
8833 if (ConstantExpr::getICmp(Pred,
8835 RHSC->getValue())->isNullValue())
8836 return TrivialCase(false);
8838 return TrivialCase(true);
8840 // Otherwise swap the operands to put the constant on the right.
8841 std::swap(LHS, RHS);
8842 Pred = ICmpInst::getSwappedPredicate(Pred);
8846 // If we're comparing an addrec with a value which is loop-invariant in the
8847 // addrec's loop, put the addrec on the left. Also make a dominance check,
8848 // as both operands could be addrecs loop-invariant in each other's loop.
8849 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) {
8850 const Loop *L = AR->getLoop();
8851 if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) {
8852 std::swap(LHS, RHS);
8853 Pred = ICmpInst::getSwappedPredicate(Pred);
8858 // If there's a constant operand, canonicalize comparisons with boundary
8859 // cases, and canonicalize *-or-equal comparisons to regular comparisons.
8860 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
8861 const APInt &RA = RC->getAPInt();
8863 bool SimplifiedByConstantRange = false;
8865 if (!ICmpInst::isEquality(Pred)) {
8866 ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, RA);
8867 if (ExactCR.isFullSet())
8868 return TrivialCase(true);
8869 else if (ExactCR.isEmptySet())
8870 return TrivialCase(false);
8873 CmpInst::Predicate NewPred;
8874 if (ExactCR.getEquivalentICmp(NewPred, NewRHS) &&
8875 ICmpInst::isEquality(NewPred)) {
8876 // We were able to convert an inequality to an equality.
8878 RHS = getConstant(NewRHS);
8879 Changed = SimplifiedByConstantRange = true;
8883 if (!SimplifiedByConstantRange) {
8887 case ICmpInst::ICMP_EQ:
8888 case ICmpInst::ICMP_NE:
8889 // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
8891 if (const SCEVAddExpr *AE = dyn_cast<SCEVAddExpr>(LHS))
8892 if (const SCEVMulExpr *ME =
8893 dyn_cast<SCEVMulExpr>(AE->getOperand(0)))
8894 if (AE->getNumOperands() == 2 && ME->getNumOperands() == 2 &&
8895 ME->getOperand(0)->isAllOnesValue()) {
8896 RHS = AE->getOperand(1);
8897 LHS = ME->getOperand(1);
8903 // The "Should have been caught earlier!" messages refer to the fact
8904 // that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
8905 // should have fired on the corresponding cases, and canonicalized the
8906 // check to trivial case.
8908 case ICmpInst::ICMP_UGE:
8909 assert(!RA.isMinValue() && "Should have been caught earlier!");
8910 Pred = ICmpInst::ICMP_UGT;
8911 RHS = getConstant(RA - 1);
8914 case ICmpInst::ICMP_ULE:
8915 assert(!RA.isMaxValue() && "Should have been caught earlier!");
8916 Pred = ICmpInst::ICMP_ULT;
8917 RHS = getConstant(RA + 1);
8920 case ICmpInst::ICMP_SGE:
8921 assert(!RA.isMinSignedValue() && "Should have been caught earlier!");
8922 Pred = ICmpInst::ICMP_SGT;
8923 RHS = getConstant(RA - 1);
8926 case ICmpInst::ICMP_SLE:
8927 assert(!RA.isMaxSignedValue() && "Should have been caught earlier!");
8928 Pred = ICmpInst::ICMP_SLT;
8929 RHS = getConstant(RA + 1);
8936 // Check for obvious equality.
8937 if (HasSameValue(LHS, RHS)) {
8938 if (ICmpInst::isTrueWhenEqual(Pred))
8939 return TrivialCase(true);
8940 if (ICmpInst::isFalseWhenEqual(Pred))
8941 return TrivialCase(false);
8944 // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
8945 // adding or subtracting 1 from one of the operands.
8947 case ICmpInst::ICMP_SLE:
8948 if (!getSignedRangeMax(RHS).isMaxSignedValue()) {
8949 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
8951 Pred = ICmpInst::ICMP_SLT;
8953 } else if (!getSignedRangeMin(LHS).isMinSignedValue()) {
8954 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS,
8956 Pred = ICmpInst::ICMP_SLT;
8960 case ICmpInst::ICMP_SGE:
8961 if (!getSignedRangeMin(RHS).isMinSignedValue()) {
8962 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS,
8964 Pred = ICmpInst::ICMP_SGT;
8966 } else if (!getSignedRangeMax(LHS).isMaxSignedValue()) {
8967 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
8969 Pred = ICmpInst::ICMP_SGT;
8973 case ICmpInst::ICMP_ULE:
8974 if (!getUnsignedRangeMax(RHS).isMaxValue()) {
8975 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
8977 Pred = ICmpInst::ICMP_ULT;
8979 } else if (!getUnsignedRangeMin(LHS).isMinValue()) {
8980 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS);
8981 Pred = ICmpInst::ICMP_ULT;
8985 case ICmpInst::ICMP_UGE:
8986 if (!getUnsignedRangeMin(RHS).isMinValue()) {
8987 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
8988 Pred = ICmpInst::ICMP_UGT;
8990 } else if (!getUnsignedRangeMax(LHS).isMaxValue()) {
8991 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
8993 Pred = ICmpInst::ICMP_UGT;
9001 // TODO: More simplifications are possible here.
9003 // Recursively simplify until we either hit a recursion limit or nothing
9006 return SimplifyICmpOperands(Pred, LHS, RHS, Depth+1);
9011 bool ScalarEvolution::isKnownNegative(const SCEV *S) {
9012 return getSignedRangeMax(S).isNegative();
9015 bool ScalarEvolution::isKnownPositive(const SCEV *S) {
9016 return getSignedRangeMin(S).isStrictlyPositive();
9019 bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
9020 return !getSignedRangeMin(S).isNegative();
9023 bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
9024 return !getSignedRangeMax(S).isStrictlyPositive();
9027 bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
9028 return isKnownNegative(S) || isKnownPositive(S);
9031 std::pair<const SCEV *, const SCEV *>
9032 ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) {
9033 // Compute SCEV on entry of loop L.
9034 const SCEV *Start = SCEVInitRewriter::rewrite(S, L, *this);
9035 if (Start == getCouldNotCompute())
9036 return { Start, Start };
9037 // Compute post increment SCEV for loop L.
9038 const SCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, *this);
9039 assert(PostInc != getCouldNotCompute() && "Unexpected could not compute");
9040 return { Start, PostInc };
9043 bool ScalarEvolution::isKnownViaInduction(ICmpInst::Predicate Pred,
9044 const SCEV *LHS, const SCEV *RHS) {
9045 // First collect all loops.
9046 SmallPtrSet<const Loop *, 8> LoopsUsed;
9047 getUsedLoops(LHS, LoopsUsed);
9048 getUsedLoops(RHS, LoopsUsed);
9050 if (LoopsUsed.empty())
9053 // Domination relationship must be a linear order on collected loops.
9055 for (auto *L1 : LoopsUsed)
9056 for (auto *L2 : LoopsUsed)
9057 assert((DT.dominates(L1->getHeader(), L2->getHeader()) ||
9058 DT.dominates(L2->getHeader(), L1->getHeader())) &&
9059 "Domination relationship is not a linear order");
9063 *std::max_element(LoopsUsed.begin(), LoopsUsed.end(),
9064 [&](const Loop *L1, const Loop *L2) {
9065 return DT.properlyDominates(L1->getHeader(), L2->getHeader());
9068 // Get init and post increment value for LHS.
9069 auto SplitLHS = SplitIntoInitAndPostInc(MDL, LHS);
9070 // if LHS contains unknown non-invariant SCEV then bail out.
9071 if (SplitLHS.first == getCouldNotCompute())
9073 assert (SplitLHS.second != getCouldNotCompute() && "Unexpected CNC");
9074 // Get init and post increment value for RHS.
9075 auto SplitRHS = SplitIntoInitAndPostInc(MDL, RHS);
9076 // if RHS contains unknown non-invariant SCEV then bail out.
9077 if (SplitRHS.first == getCouldNotCompute())
9079 assert (SplitRHS.second != getCouldNotCompute() && "Unexpected CNC");
9080 // It is possible that init SCEV contains an invariant load but it does
9081 // not dominate MDL and is not available at MDL loop entry, so we should
9083 if (!isAvailableAtLoopEntry(SplitLHS.first, MDL) ||
9084 !isAvailableAtLoopEntry(SplitRHS.first, MDL))
9087 return isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first) &&
9088 isLoopBackedgeGuardedByCond(MDL, Pred, SplitLHS.second,
9092 bool ScalarEvolution::isKnownPredicate(ICmpInst::Predicate Pred,
9093 const SCEV *LHS, const SCEV *RHS) {
9094 // Canonicalize the inputs first.
9095 (void)SimplifyICmpOperands(Pred, LHS, RHS);
9097 if (isKnownViaInduction(Pred, LHS, RHS))
9100 if (isKnownPredicateViaSplitting(Pred, LHS, RHS))
9103 // Otherwise see what can be done with some simple reasoning.
9104 return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS);
9107 bool ScalarEvolution::isKnownOnEveryIteration(ICmpInst::Predicate Pred,
9108 const SCEVAddRecExpr *LHS,
9110 const Loop *L = LHS->getLoop();
9111 return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) &&
9112 isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS);
9115 bool ScalarEvolution::isMonotonicPredicate(const SCEVAddRecExpr *LHS,
9116 ICmpInst::Predicate Pred,
9118 bool Result = isMonotonicPredicateImpl(LHS, Pred, Increasing);
9121 // Verify an invariant: inverting the predicate should turn a monotonically
9122 // increasing change to a monotonically decreasing one, and vice versa.
9123 bool IncreasingSwapped;
9124 bool ResultSwapped = isMonotonicPredicateImpl(
9125 LHS, ICmpInst::getSwappedPredicate(Pred), IncreasingSwapped);
9127 assert(Result == ResultSwapped && "should be able to analyze both!");
9129 assert(Increasing == !IncreasingSwapped &&
9130 "monotonicity should flip as we flip the predicate");
9136 bool ScalarEvolution::isMonotonicPredicateImpl(const SCEVAddRecExpr *LHS,
9137 ICmpInst::Predicate Pred,
9140 // A zero step value for LHS means the induction variable is essentially a
9141 // loop invariant value. We don't really depend on the predicate actually
9142 // flipping from false to true (for increasing predicates, and the other way
9143 // around for decreasing predicates), all we care about is that *if* the
9144 // predicate changes then it only changes from false to true.
9146 // A zero step value in itself is not very useful, but there may be places
9147 // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be
9148 // as general as possible.
9152 return false; // Conservative answer
9154 case ICmpInst::ICMP_UGT:
9155 case ICmpInst::ICMP_UGE:
9156 case ICmpInst::ICMP_ULT:
9157 case ICmpInst::ICMP_ULE:
9158 if (!LHS->hasNoUnsignedWrap())
9161 Increasing = Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE;
9164 case ICmpInst::ICMP_SGT:
9165 case ICmpInst::ICMP_SGE:
9166 case ICmpInst::ICMP_SLT:
9167 case ICmpInst::ICMP_SLE: {
9168 if (!LHS->hasNoSignedWrap())
9171 const SCEV *Step = LHS->getStepRecurrence(*this);
9173 if (isKnownNonNegative(Step)) {
9174 Increasing = Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE;
9178 if (isKnownNonPositive(Step)) {
9179 Increasing = Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE;
9188 llvm_unreachable("switch has default clause!");
9191 bool ScalarEvolution::isLoopInvariantPredicate(
9192 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
9193 ICmpInst::Predicate &InvariantPred, const SCEV *&InvariantLHS,
9194 const SCEV *&InvariantRHS) {
9196 // If there is a loop-invariant, force it into the RHS, otherwise bail out.
9197 if (!isLoopInvariant(RHS, L)) {
9198 if (!isLoopInvariant(LHS, L))
9201 std::swap(LHS, RHS);
9202 Pred = ICmpInst::getSwappedPredicate(Pred);
9205 const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS);
9206 if (!ArLHS || ArLHS->getLoop() != L)
9210 if (!isMonotonicPredicate(ArLHS, Pred, Increasing))
9213 // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to
9214 // true as the loop iterates, and the backedge is control dependent on
9215 // "ArLHS `Pred` RHS" == true then we can reason as follows:
9217 // * if the predicate was false in the first iteration then the predicate
9218 // is never evaluated again, since the loop exits without taking the
9220 // * if the predicate was true in the first iteration then it will
9221 // continue to be true for all future iterations since it is
9222 // monotonically increasing.
9224 // For both the above possibilities, we can replace the loop varying
9225 // predicate with its value on the first iteration of the loop (which is
9228 // A similar reasoning applies for a monotonically decreasing predicate, by
9229 // replacing true with false and false with true in the above two bullets.
9231 auto P = Increasing ? Pred : ICmpInst::getInversePredicate(Pred);
9233 if (!isLoopBackedgeGuardedByCond(L, P, LHS, RHS))
9236 InvariantPred = Pred;
9237 InvariantLHS = ArLHS->getStart();
9242 bool ScalarEvolution::isKnownPredicateViaConstantRanges(
9243 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS) {
9244 if (HasSameValue(LHS, RHS))
9245 return ICmpInst::isTrueWhenEqual(Pred);
9247 // This code is split out from isKnownPredicate because it is called from
9248 // within isLoopEntryGuardedByCond.
9251 [&](const ConstantRange &RangeLHS, const ConstantRange &RangeRHS) {
9252 return ConstantRange::makeSatisfyingICmpRegion(Pred, RangeRHS)
9253 .contains(RangeLHS);
9256 // The check at the top of the function catches the case where the values are
9257 // known to be equal.
9258 if (Pred == CmpInst::ICMP_EQ)
9261 if (Pred == CmpInst::ICMP_NE)
9262 return CheckRanges(getSignedRange(LHS), getSignedRange(RHS)) ||
9263 CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS)) ||
9264 isKnownNonZero(getMinusSCEV(LHS, RHS));
9266 if (CmpInst::isSigned(Pred))
9267 return CheckRanges(getSignedRange(LHS), getSignedRange(RHS));
9269 return CheckRanges(getUnsignedRange(LHS), getUnsignedRange(RHS));
9272 bool ScalarEvolution::isKnownPredicateViaNoOverflow(ICmpInst::Predicate Pred,
9275 // Match Result to (X + Y)<ExpectedFlags> where Y is a constant integer.
9276 // Return Y via OutY.
9277 auto MatchBinaryAddToConst =
9278 [this](const SCEV *Result, const SCEV *X, APInt &OutY,
9279 SCEV::NoWrapFlags ExpectedFlags) {
9280 const SCEV *NonConstOp, *ConstOp;
9281 SCEV::NoWrapFlags FlagsPresent;
9283 if (!splitBinaryAdd(Result, ConstOp, NonConstOp, FlagsPresent) ||
9284 !isa<SCEVConstant>(ConstOp) || NonConstOp != X)
9287 OutY = cast<SCEVConstant>(ConstOp)->getAPInt();
9288 return (FlagsPresent & ExpectedFlags) == ExpectedFlags;
9297 case ICmpInst::ICMP_SGE:
9298 std::swap(LHS, RHS);
9300 case ICmpInst::ICMP_SLE:
9301 // X s<= (X + C)<nsw> if C >= 0
9302 if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) && C.isNonNegative())
9305 // (X + C)<nsw> s<= X if C <= 0
9306 if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) &&
9307 !C.isStrictlyPositive())
9311 case ICmpInst::ICMP_SGT:
9312 std::swap(LHS, RHS);
9314 case ICmpInst::ICMP_SLT:
9315 // X s< (X + C)<nsw> if C > 0
9316 if (MatchBinaryAddToConst(RHS, LHS, C, SCEV::FlagNSW) &&
9317 C.isStrictlyPositive())
9320 // (X + C)<nsw> s< X if C < 0
9321 if (MatchBinaryAddToConst(LHS, RHS, C, SCEV::FlagNSW) && C.isNegative())
9329 bool ScalarEvolution::isKnownPredicateViaSplitting(ICmpInst::Predicate Pred,
9332 if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate)
9335 // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on
9336 // the stack can result in exponential time complexity.
9337 SaveAndRestore<bool> Restore(ProvingSplitPredicate, true);
9339 // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L
9341 // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use
9342 // isKnownPredicate. isKnownPredicate is more powerful, but also more
9343 // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the
9344 // interesting cases seen in practice. We can consider "upgrading" L >= 0 to
9345 // use isKnownPredicate later if needed.
9346 return isKnownNonNegative(RHS) &&
9347 isKnownPredicate(CmpInst::ICMP_SGE, LHS, getZero(LHS->getType())) &&
9348 isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS);
9351 bool ScalarEvolution::isImpliedViaGuard(BasicBlock *BB,
9352 ICmpInst::Predicate Pred,
9353 const SCEV *LHS, const SCEV *RHS) {
9354 // No need to even try if we know the module has no guards.
9358 return any_of(*BB, [&](Instruction &I) {
9359 using namespace llvm::PatternMatch;
9362 return match(&I, m_Intrinsic<Intrinsic::experimental_guard>(
9363 m_Value(Condition))) &&
9364 isImpliedCond(Pred, LHS, RHS, Condition, false);
9368 /// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
9369 /// protected by a conditional between LHS and RHS. This is used to
9370 /// to eliminate casts.
9372 ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
9373 ICmpInst::Predicate Pred,
9374 const SCEV *LHS, const SCEV *RHS) {
9375 // Interpret a null as meaning no loop, where there is obviously no guard
9376 // (interprocedural conditions notwithstanding).
9377 if (!L) return true;
9380 assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
9381 "This cannot be done on broken IR!");
9384 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
9387 BasicBlock *Latch = L->getLoopLatch();
9391 BranchInst *LoopContinuePredicate =
9392 dyn_cast<BranchInst>(Latch->getTerminator());
9393 if (LoopContinuePredicate && LoopContinuePredicate->isConditional() &&
9394 isImpliedCond(Pred, LHS, RHS,
9395 LoopContinuePredicate->getCondition(),
9396 LoopContinuePredicate->getSuccessor(0) != L->getHeader()))
9399 // We don't want more than one activation of the following loops on the stack
9400 // -- that can lead to O(n!) time complexity.
9401 if (WalkingBEDominatingConds)
9404 SaveAndRestore<bool> ClearOnExit(WalkingBEDominatingConds, true);
9406 // See if we can exploit a trip count to prove the predicate.
9407 const auto &BETakenInfo = getBackedgeTakenInfo(L);
9408 const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this);
9409 if (LatchBECount != getCouldNotCompute()) {
9410 // We know that Latch branches back to the loop header exactly
9411 // LatchBECount times. This means the backdege condition at Latch is
9412 // equivalent to "{0,+,1} u< LatchBECount".
9413 Type *Ty = LatchBECount->getType();
9414 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
9415 const SCEV *LoopCounter =
9416 getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags);
9417 if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter,
9422 // Check conditions due to any @llvm.assume intrinsics.
9423 for (auto &AssumeVH : AC.assumptions()) {
9426 auto *CI = cast<CallInst>(AssumeVH);
9427 if (!DT.dominates(CI, Latch->getTerminator()))
9430 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false))
9434 // If the loop is not reachable from the entry block, we risk running into an
9435 // infinite loop as we walk up into the dom tree. These loops do not matter
9436 // anyway, so we just return a conservative answer when we see them.
9437 if (!DT.isReachableFromEntry(L->getHeader()))
9440 if (isImpliedViaGuard(Latch, Pred, LHS, RHS))
9443 for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()];
9444 DTN != HeaderDTN; DTN = DTN->getIDom()) {
9445 assert(DTN && "should reach the loop header before reaching the root!");
9447 BasicBlock *BB = DTN->getBlock();
9448 if (isImpliedViaGuard(BB, Pred, LHS, RHS))
9451 BasicBlock *PBB = BB->getSinglePredecessor();
9455 BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator());
9456 if (!ContinuePredicate || !ContinuePredicate->isConditional())
9459 Value *Condition = ContinuePredicate->getCondition();
9461 // If we have an edge `E` within the loop body that dominates the only
9462 // latch, the condition guarding `E` also guards the backedge. This
9463 // reasoning works only for loops with a single latch.
9465 BasicBlockEdge DominatingEdge(PBB, BB);
9466 if (DominatingEdge.isSingleEdge()) {
9467 // We're constructively (and conservatively) enumerating edges within the
9468 // loop body that dominate the latch. The dominator tree better agree
9470 assert(DT.dominates(DominatingEdge, Latch) && "should be!");
9472 if (isImpliedCond(Pred, LHS, RHS, Condition,
9473 BB != ContinuePredicate->getSuccessor(0)))
9482 ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L,
9483 ICmpInst::Predicate Pred,
9484 const SCEV *LHS, const SCEV *RHS) {
9485 // Interpret a null as meaning no loop, where there is obviously no guard
9486 // (interprocedural conditions notwithstanding).
9487 if (!L) return false;
9490 assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
9491 "This cannot be done on broken IR!");
9493 // Both LHS and RHS must be available at loop entry.
9494 assert(isAvailableAtLoopEntry(LHS, L) &&
9495 "LHS is not available at Loop Entry");
9496 assert(isAvailableAtLoopEntry(RHS, L) &&
9497 "RHS is not available at Loop Entry");
9499 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
9502 // If we cannot prove strict comparison (e.g. a > b), maybe we can prove
9503 // the facts (a >= b && a != b) separately. A typical situation is when the
9504 // non-strict comparison is known from ranges and non-equality is known from
9505 // dominating predicates. If we are proving strict comparison, we always try
9506 // to prove non-equality and non-strict comparison separately.
9507 auto NonStrictPredicate = ICmpInst::getNonStrictPredicate(Pred);
9508 const bool ProvingStrictComparison = (Pred != NonStrictPredicate);
9509 bool ProvedNonStrictComparison = false;
9510 bool ProvedNonEquality = false;
9512 if (ProvingStrictComparison) {
9513 ProvedNonStrictComparison =
9514 isKnownViaNonRecursiveReasoning(NonStrictPredicate, LHS, RHS);
9516 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_NE, LHS, RHS);
9517 if (ProvedNonStrictComparison && ProvedNonEquality)
9521 // Try to prove (Pred, LHS, RHS) using isImpliedViaGuard.
9522 auto ProveViaGuard = [&](BasicBlock *Block) {
9523 if (isImpliedViaGuard(Block, Pred, LHS, RHS))
9525 if (ProvingStrictComparison) {
9526 if (!ProvedNonStrictComparison)
9527 ProvedNonStrictComparison =
9528 isImpliedViaGuard(Block, NonStrictPredicate, LHS, RHS);
9529 if (!ProvedNonEquality)
9531 isImpliedViaGuard(Block, ICmpInst::ICMP_NE, LHS, RHS);
9532 if (ProvedNonStrictComparison && ProvedNonEquality)
9538 // Try to prove (Pred, LHS, RHS) using isImpliedCond.
9539 auto ProveViaCond = [&](Value *Condition, bool Inverse) {
9540 if (isImpliedCond(Pred, LHS, RHS, Condition, Inverse))
9542 if (ProvingStrictComparison) {
9543 if (!ProvedNonStrictComparison)
9544 ProvedNonStrictComparison =
9545 isImpliedCond(NonStrictPredicate, LHS, RHS, Condition, Inverse);
9546 if (!ProvedNonEquality)
9548 isImpliedCond(ICmpInst::ICMP_NE, LHS, RHS, Condition, Inverse);
9549 if (ProvedNonStrictComparison && ProvedNonEquality)
9555 // Starting at the loop predecessor, climb up the predecessor chain, as long
9556 // as there are predecessors that can be found that have unique successors
9557 // leading to the original header.
9558 for (std::pair<BasicBlock *, BasicBlock *>
9559 Pair(L->getLoopPredecessor(), L->getHeader());
9561 Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
9563 if (ProveViaGuard(Pair.first))
9566 BranchInst *LoopEntryPredicate =
9567 dyn_cast<BranchInst>(Pair.first->getTerminator());
9568 if (!LoopEntryPredicate ||
9569 LoopEntryPredicate->isUnconditional())
9572 if (ProveViaCond(LoopEntryPredicate->getCondition(),
9573 LoopEntryPredicate->getSuccessor(0) != Pair.second))
9577 // Check conditions due to any @llvm.assume intrinsics.
9578 for (auto &AssumeVH : AC.assumptions()) {
9581 auto *CI = cast<CallInst>(AssumeVH);
9582 if (!DT.dominates(CI, L->getHeader()))
9585 if (ProveViaCond(CI->getArgOperand(0), false))
9592 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred,
9593 const SCEV *LHS, const SCEV *RHS,
9594 Value *FoundCondValue,
9596 if (!PendingLoopPredicates.insert(FoundCondValue).second)
9600 make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); });
9602 // Recursively handle And and Or conditions.
9603 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(FoundCondValue)) {
9604 if (BO->getOpcode() == Instruction::And) {
9606 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
9607 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
9608 } else if (BO->getOpcode() == Instruction::Or) {
9610 return isImpliedCond(Pred, LHS, RHS, BO->getOperand(0), Inverse) ||
9611 isImpliedCond(Pred, LHS, RHS, BO->getOperand(1), Inverse);
9615 ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue);
9616 if (!ICI) return false;
9618 // Now that we found a conditional branch that dominates the loop or controls
9619 // the loop latch. Check to see if it is the comparison we are looking for.
9620 ICmpInst::Predicate FoundPred;
9622 FoundPred = ICI->getInversePredicate();
9624 FoundPred = ICI->getPredicate();
9626 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
9627 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
9629 return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS);
9632 bool ScalarEvolution::isImpliedCond(ICmpInst::Predicate Pred, const SCEV *LHS,
9634 ICmpInst::Predicate FoundPred,
9635 const SCEV *FoundLHS,
9636 const SCEV *FoundRHS) {
9637 // Balance the types.
9638 if (getTypeSizeInBits(LHS->getType()) <
9639 getTypeSizeInBits(FoundLHS->getType())) {
9640 if (CmpInst::isSigned(Pred)) {
9641 LHS = getSignExtendExpr(LHS, FoundLHS->getType());
9642 RHS = getSignExtendExpr(RHS, FoundLHS->getType());
9644 LHS = getZeroExtendExpr(LHS, FoundLHS->getType());
9645 RHS = getZeroExtendExpr(RHS, FoundLHS->getType());
9647 } else if (getTypeSizeInBits(LHS->getType()) >
9648 getTypeSizeInBits(FoundLHS->getType())) {
9649 if (CmpInst::isSigned(FoundPred)) {
9650 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
9651 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
9653 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
9654 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
9658 // Canonicalize the query to match the way instcombine will have
9659 // canonicalized the comparison.
9660 if (SimplifyICmpOperands(Pred, LHS, RHS))
9662 return CmpInst::isTrueWhenEqual(Pred);
9663 if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS))
9664 if (FoundLHS == FoundRHS)
9665 return CmpInst::isFalseWhenEqual(FoundPred);
9667 // Check to see if we can make the LHS or RHS match.
9668 if (LHS == FoundRHS || RHS == FoundLHS) {
9669 if (isa<SCEVConstant>(RHS)) {
9670 std::swap(FoundLHS, FoundRHS);
9671 FoundPred = ICmpInst::getSwappedPredicate(FoundPred);
9673 std::swap(LHS, RHS);
9674 Pred = ICmpInst::getSwappedPredicate(Pred);
9678 // Check whether the found predicate is the same as the desired predicate.
9679 if (FoundPred == Pred)
9680 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
9682 // Check whether swapping the found predicate makes it the same as the
9683 // desired predicate.
9684 if (ICmpInst::getSwappedPredicate(FoundPred) == Pred) {
9685 if (isa<SCEVConstant>(RHS))
9686 return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS);
9688 return isImpliedCondOperands(ICmpInst::getSwappedPredicate(Pred),
9689 RHS, LHS, FoundLHS, FoundRHS);
9692 // Unsigned comparison is the same as signed comparison when both the operands
9693 // are non-negative.
9694 if (CmpInst::isUnsigned(FoundPred) &&
9695 CmpInst::getSignedPredicate(FoundPred) == Pred &&
9696 isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS))
9697 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS);
9699 // Check if we can make progress by sharpening ranges.
9700 if (FoundPred == ICmpInst::ICMP_NE &&
9701 (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) {
9703 const SCEVConstant *C = nullptr;
9704 const SCEV *V = nullptr;
9706 if (isa<SCEVConstant>(FoundLHS)) {
9707 C = cast<SCEVConstant>(FoundLHS);
9710 C = cast<SCEVConstant>(FoundRHS);
9714 // The guarding predicate tells us that C != V. If the known range
9715 // of V is [C, t), we can sharpen the range to [C + 1, t). The
9716 // range we consider has to correspond to same signedness as the
9717 // predicate we're interested in folding.
9719 APInt Min = ICmpInst::isSigned(Pred) ?
9720 getSignedRangeMin(V) : getUnsignedRangeMin(V);
9722 if (Min == C->getAPInt()) {
9723 // Given (V >= Min && V != Min) we conclude V >= (Min + 1).
9724 // This is true even if (Min + 1) wraps around -- in case of
9725 // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)).
9727 APInt SharperMin = Min + 1;
9730 case ICmpInst::ICMP_SGE:
9731 case ICmpInst::ICMP_UGE:
9732 // We know V `Pred` SharperMin. If this implies LHS `Pred`
9734 if (isImpliedCondOperands(Pred, LHS, RHS, V,
9735 getConstant(SharperMin)))
9739 case ICmpInst::ICMP_SGT:
9740 case ICmpInst::ICMP_UGT:
9741 // We know from the range information that (V `Pred` Min ||
9742 // V == Min). We know from the guarding condition that !(V
9743 // == Min). This gives us
9745 // V `Pred` Min || V == Min && !(V == Min)
9748 // If V `Pred` Min implies LHS `Pred` RHS, we're done.
9750 if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min)))
9761 // Check whether the actual condition is beyond sufficient.
9762 if (FoundPred == ICmpInst::ICMP_EQ)
9763 if (ICmpInst::isTrueWhenEqual(Pred))
9764 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS))
9766 if (Pred == ICmpInst::ICMP_NE)
9767 if (!ICmpInst::isTrueWhenEqual(FoundPred))
9768 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS))
9771 // Otherwise assume the worst.
9775 bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr,
9776 const SCEV *&L, const SCEV *&R,
9777 SCEV::NoWrapFlags &Flags) {
9778 const auto *AE = dyn_cast<SCEVAddExpr>(Expr);
9779 if (!AE || AE->getNumOperands() != 2)
9782 L = AE->getOperand(0);
9783 R = AE->getOperand(1);
9784 Flags = AE->getNoWrapFlags();
9788 Optional<APInt> ScalarEvolution::computeConstantDifference(const SCEV *More,
9790 // We avoid subtracting expressions here because this function is usually
9791 // fairly deep in the call stack (i.e. is called many times).
9793 if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
9794 const auto *LAR = cast<SCEVAddRecExpr>(Less);
9795 const auto *MAR = cast<SCEVAddRecExpr>(More);
9797 if (LAR->getLoop() != MAR->getLoop())
9800 // We look at affine expressions only; not for correctness but to keep
9801 // getStepRecurrence cheap.
9802 if (!LAR->isAffine() || !MAR->isAffine())
9805 if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
9808 Less = LAR->getStart();
9809 More = MAR->getStart();
9814 if (isa<SCEVConstant>(Less) && isa<SCEVConstant>(More)) {
9815 const auto &M = cast<SCEVConstant>(More)->getAPInt();
9816 const auto &L = cast<SCEVConstant>(Less)->getAPInt();
9820 SCEV::NoWrapFlags Flags;
9821 const SCEV *LLess = nullptr, *RLess = nullptr;
9822 const SCEV *LMore = nullptr, *RMore = nullptr;
9823 const SCEVConstant *C1 = nullptr, *C2 = nullptr;
9824 // Compare (X + C1) vs X.
9825 if (splitBinaryAdd(Less, LLess, RLess, Flags))
9826 if ((C1 = dyn_cast<SCEVConstant>(LLess)))
9828 return -(C1->getAPInt());
9830 // Compare X vs (X + C2).
9831 if (splitBinaryAdd(More, LMore, RMore, Flags))
9832 if ((C2 = dyn_cast<SCEVConstant>(LMore)))
9834 return C2->getAPInt();
9836 // Compare (X + C1) vs (X + C2).
9837 if (C1 && C2 && RLess == RMore)
9838 return C2->getAPInt() - C1->getAPInt();
9843 bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(
9844 ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS,
9845 const SCEV *FoundLHS, const SCEV *FoundRHS) {
9846 if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT)
9849 const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS);
9853 const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS);
9854 if (!AddRecFoundLHS)
9857 // We'd like to let SCEV reason about control dependencies, so we constrain
9858 // both the inequalities to be about add recurrences on the same loop. This
9859 // way we can use isLoopEntryGuardedByCond later.
9861 const Loop *L = AddRecFoundLHS->getLoop();
9862 if (L != AddRecLHS->getLoop())
9865 // FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1)
9867 // FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
9870 // Informal proof for (2), assuming (1) [*]:
9872 // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
9876 // FoundLHS s< FoundRHS s< INT_MIN - C
9877 // <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ]
9878 // <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
9879 // <=> (FoundLHS + INT_MIN + C + INT_MIN) s<
9880 // (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
9881 // <=> FoundLHS + C s< FoundRHS + C
9883 // [*]: (1) can be proved by ruling out overflow.
9885 // [**]: This can be proved by analyzing all the four possibilities:
9886 // (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
9887 // (A s>= 0, B s>= 0).
9890 // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
9891 // will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS
9892 // = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS
9893 // s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is
9894 // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
9897 Optional<APInt> LDiff = computeConstantDifference(LHS, FoundLHS);
9898 Optional<APInt> RDiff = computeConstantDifference(RHS, FoundRHS);
9899 if (!LDiff || !RDiff || *LDiff != *RDiff)
9902 if (LDiff->isMinValue())
9905 APInt FoundRHSLimit;
9907 if (Pred == CmpInst::ICMP_ULT) {
9908 FoundRHSLimit = -(*RDiff);
9910 assert(Pred == CmpInst::ICMP_SLT && "Checked above!");
9911 FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff;
9914 // Try to prove (1) or (2), as needed.
9915 return isAvailableAtLoopEntry(FoundRHS, L) &&
9916 isLoopEntryGuardedByCond(L, Pred, FoundRHS,
9917 getConstant(FoundRHSLimit));
9920 bool ScalarEvolution::isImpliedViaMerge(ICmpInst::Predicate Pred,
9921 const SCEV *LHS, const SCEV *RHS,
9922 const SCEV *FoundLHS,
9923 const SCEV *FoundRHS, unsigned Depth) {
9924 const PHINode *LPhi = nullptr, *RPhi = nullptr;
9926 auto ClearOnExit = make_scope_exit([&]() {
9928 bool Erased = PendingMerges.erase(LPhi);
9929 assert(Erased && "Failed to erase LPhi!");
9933 bool Erased = PendingMerges.erase(RPhi);
9934 assert(Erased && "Failed to erase RPhi!");
9939 // Find respective Phis and check that they are not being pending.
9940 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS))
9941 if (auto *Phi = dyn_cast<PHINode>(LU->getValue())) {
9942 if (!PendingMerges.insert(Phi).second)
9946 if (const SCEVUnknown *RU = dyn_cast<SCEVUnknown>(RHS))
9947 if (auto *Phi = dyn_cast<PHINode>(RU->getValue())) {
9948 // If we detect a loop of Phi nodes being processed by this method, for
9951 // %a = phi i32 [ %some1, %preheader ], [ %b, %latch ]
9952 // %b = phi i32 [ %some2, %preheader ], [ %a, %latch ]
9954 // we don't want to deal with a case that complex, so return conservative
9956 if (!PendingMerges.insert(Phi).second)
9961 // If none of LHS, RHS is a Phi, nothing to do here.
9965 // If there is a SCEVUnknown Phi we are interested in, make it left.
9967 std::swap(LHS, RHS);
9968 std::swap(FoundLHS, FoundRHS);
9969 std::swap(LPhi, RPhi);
9970 Pred = ICmpInst::getSwappedPredicate(Pred);
9973 assert(LPhi && "LPhi should definitely be a SCEVUnknown Phi!");
9974 const BasicBlock *LBB = LPhi->getParent();
9975 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
9977 auto ProvedEasily = [&](const SCEV *S1, const SCEV *S2) {
9978 return isKnownViaNonRecursiveReasoning(Pred, S1, S2) ||
9979 isImpliedCondOperandsViaRanges(Pred, S1, S2, FoundLHS, FoundRHS) ||
9980 isImpliedViaOperations(Pred, S1, S2, FoundLHS, FoundRHS, Depth);
9983 if (RPhi && RPhi->getParent() == LBB) {
9984 // Case one: RHS is also a SCEVUnknown Phi from the same basic block.
9985 // If we compare two Phis from the same block, and for each entry block
9986 // the predicate is true for incoming values from this block, then the
9987 // predicate is also true for the Phis.
9988 for (const BasicBlock *IncBB : predecessors(LBB)) {
9989 const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
9990 const SCEV *R = getSCEV(RPhi->getIncomingValueForBlock(IncBB));
9991 if (!ProvedEasily(L, R))
9994 } else if (RAR && RAR->getLoop()->getHeader() == LBB) {
9995 // Case two: RHS is also a Phi from the same basic block, and it is an
9996 // AddRec. It means that there is a loop which has both AddRec and Unknown
9997 // PHIs, for it we can compare incoming values of AddRec from above the loop
9998 // and latch with their respective incoming values of LPhi.
9999 // TODO: Generalize to handle loops with many inputs in a header.
10000 if (LPhi->getNumIncomingValues() != 2) return false;
10002 auto *RLoop = RAR->getLoop();
10003 auto *Predecessor = RLoop->getLoopPredecessor();
10004 assert(Predecessor && "Loop with AddRec with no predecessor?");
10005 const SCEV *L1 = getSCEV(LPhi->getIncomingValueForBlock(Predecessor));
10006 if (!ProvedEasily(L1, RAR->getStart()))
10008 auto *Latch = RLoop->getLoopLatch();
10009 assert(Latch && "Loop with AddRec with no latch?");
10010 const SCEV *L2 = getSCEV(LPhi->getIncomingValueForBlock(Latch));
10011 if (!ProvedEasily(L2, RAR->getPostIncExpr(*this)))
10014 // In all other cases go over inputs of LHS and compare each of them to RHS,
10015 // the predicate is true for (LHS, RHS) if it is true for all such pairs.
10016 // At this point RHS is either a non-Phi, or it is a Phi from some block
10017 // different from LBB.
10018 for (const BasicBlock *IncBB : predecessors(LBB)) {
10019 // Check that RHS is available in this block.
10020 if (!dominates(RHS, IncBB))
10022 const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
10023 if (!ProvedEasily(L, RHS))
10030 bool ScalarEvolution::isImpliedCondOperands(ICmpInst::Predicate Pred,
10031 const SCEV *LHS, const SCEV *RHS,
10032 const SCEV *FoundLHS,
10033 const SCEV *FoundRHS) {
10034 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundLHS, FoundRHS))
10037 if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS))
10040 return isImpliedCondOperandsHelper(Pred, LHS, RHS,
10041 FoundLHS, FoundRHS) ||
10042 // ~x < ~y --> x > y
10043 isImpliedCondOperandsHelper(Pred, LHS, RHS,
10044 getNotSCEV(FoundRHS),
10045 getNotSCEV(FoundLHS));
10048 /// If Expr computes ~A, return A else return nullptr
10049 static const SCEV *MatchNotExpr(const SCEV *Expr) {
10050 const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
10051 if (!Add || Add->getNumOperands() != 2 ||
10052 !Add->getOperand(0)->isAllOnesValue())
10055 const SCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
10056 if (!AddRHS || AddRHS->getNumOperands() != 2 ||
10057 !AddRHS->getOperand(0)->isAllOnesValue())
10060 return AddRHS->getOperand(1);
10063 /// Is MaybeMaxExpr an SMax or UMax of Candidate and some other values?
10064 template<typename MaxExprType>
10065 static bool IsMaxConsistingOf(const SCEV *MaybeMaxExpr,
10066 const SCEV *Candidate) {
10067 const MaxExprType *MaxExpr = dyn_cast<MaxExprType>(MaybeMaxExpr);
10068 if (!MaxExpr) return false;
10070 return find(MaxExpr->operands(), Candidate) != MaxExpr->op_end();
10073 /// Is MaybeMinExpr an SMin or UMin of Candidate and some other values?
10074 template<typename MaxExprType>
10075 static bool IsMinConsistingOf(ScalarEvolution &SE,
10076 const SCEV *MaybeMinExpr,
10077 const SCEV *Candidate) {
10078 const SCEV *MaybeMaxExpr = MatchNotExpr(MaybeMinExpr);
10082 return IsMaxConsistingOf<MaxExprType>(MaybeMaxExpr, SE.getNotSCEV(Candidate));
10085 static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE,
10086 ICmpInst::Predicate Pred,
10087 const SCEV *LHS, const SCEV *RHS) {
10088 // If both sides are affine addrecs for the same loop, with equal
10089 // steps, and we know the recurrences don't wrap, then we only
10090 // need to check the predicate on the starting values.
10092 if (!ICmpInst::isRelational(Pred))
10095 const SCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS);
10098 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
10101 if (LAR->getLoop() != RAR->getLoop())
10103 if (!LAR->isAffine() || !RAR->isAffine())
10106 if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE))
10109 SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ?
10110 SCEV::FlagNSW : SCEV::FlagNUW;
10111 if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW))
10114 return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart());
10117 /// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max
10119 static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE,
10120 ICmpInst::Predicate Pred,
10121 const SCEV *LHS, const SCEV *RHS) {
10126 case ICmpInst::ICMP_SGE:
10127 std::swap(LHS, RHS);
10129 case ICmpInst::ICMP_SLE:
10131 // min(A, ...) <= A
10132 IsMinConsistingOf<SCEVSMaxExpr>(SE, LHS, RHS) ||
10133 // A <= max(A, ...)
10134 IsMaxConsistingOf<SCEVSMaxExpr>(RHS, LHS);
10136 case ICmpInst::ICMP_UGE:
10137 std::swap(LHS, RHS);
10139 case ICmpInst::ICMP_ULE:
10141 // min(A, ...) <= A
10142 IsMinConsistingOf<SCEVUMaxExpr>(SE, LHS, RHS) ||
10143 // A <= max(A, ...)
10144 IsMaxConsistingOf<SCEVUMaxExpr>(RHS, LHS);
10147 llvm_unreachable("covered switch fell through?!");
10150 bool ScalarEvolution::isImpliedViaOperations(ICmpInst::Predicate Pred,
10151 const SCEV *LHS, const SCEV *RHS,
10152 const SCEV *FoundLHS,
10153 const SCEV *FoundRHS,
10155 assert(getTypeSizeInBits(LHS->getType()) ==
10156 getTypeSizeInBits(RHS->getType()) &&
10157 "LHS and RHS have different sizes?");
10158 assert(getTypeSizeInBits(FoundLHS->getType()) ==
10159 getTypeSizeInBits(FoundRHS->getType()) &&
10160 "FoundLHS and FoundRHS have different sizes?");
10161 // We want to avoid hurting the compile time with analysis of too big trees.
10162 if (Depth > MaxSCEVOperationsImplicationDepth)
10164 // We only want to work with ICMP_SGT comparison so far.
10165 // TODO: Extend to ICMP_UGT?
10166 if (Pred == ICmpInst::ICMP_SLT) {
10167 Pred = ICmpInst::ICMP_SGT;
10168 std::swap(LHS, RHS);
10169 std::swap(FoundLHS, FoundRHS);
10171 if (Pred != ICmpInst::ICMP_SGT)
10174 auto GetOpFromSExt = [&](const SCEV *S) {
10175 if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(S))
10176 return Ext->getOperand();
10177 // TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off
10178 // the constant in some cases.
10182 // Acquire values from extensions.
10183 auto *OrigLHS = LHS;
10184 auto *OrigFoundLHS = FoundLHS;
10185 LHS = GetOpFromSExt(LHS);
10186 FoundLHS = GetOpFromSExt(FoundLHS);
10188 // Is the SGT predicate can be proved trivially or using the found context.
10189 auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) {
10190 return isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGT, S1, S2) ||
10191 isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS,
10192 FoundRHS, Depth + 1);
10195 if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(LHS)) {
10196 // We want to avoid creation of any new non-constant SCEV. Since we are
10197 // going to compare the operands to RHS, we should be certain that we don't
10198 // need any size extensions for this. So let's decline all cases when the
10199 // sizes of types of LHS and RHS do not match.
10200 // TODO: Maybe try to get RHS from sext to catch more cases?
10201 if (getTypeSizeInBits(LHS->getType()) != getTypeSizeInBits(RHS->getType()))
10204 // Should not overflow.
10205 if (!LHSAddExpr->hasNoSignedWrap())
10208 auto *LL = LHSAddExpr->getOperand(0);
10209 auto *LR = LHSAddExpr->getOperand(1);
10210 auto *MinusOne = getNegativeSCEV(getOne(RHS->getType()));
10212 // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context.
10213 auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) {
10214 return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS);
10216 // Try to prove the following rule:
10217 // (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS).
10218 // (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS).
10219 if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL))
10221 } else if (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(LHS)) {
10223 // FIXME: Once we have SDiv implemented, we can get rid of this matching.
10225 using namespace llvm::PatternMatch;
10227 if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) {
10228 // Rules for division.
10229 // We are going to perform some comparisons with Denominator and its
10230 // derivative expressions. In general case, creating a SCEV for it may
10231 // lead to a complex analysis of the entire graph, and in particular it
10232 // can request trip count recalculation for the same loop. This would
10233 // cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid
10234 // this, we only want to create SCEVs that are constants in this section.
10235 // So we bail if Denominator is not a constant.
10236 if (!isa<ConstantInt>(LR))
10239 auto *Denominator = cast<SCEVConstant>(getSCEV(LR));
10241 // We want to make sure that LHS = FoundLHS / Denominator. If it is so,
10242 // then a SCEV for the numerator already exists and matches with FoundLHS.
10243 auto *Numerator = getExistingSCEV(LL);
10244 if (!Numerator || Numerator->getType() != FoundLHS->getType())
10247 // Make sure that the numerator matches with FoundLHS and the denominator
10249 if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator))
10252 auto *DTy = Denominator->getType();
10253 auto *FRHSTy = FoundRHS->getType();
10254 if (DTy->isPointerTy() != FRHSTy->isPointerTy())
10255 // One of types is a pointer and another one is not. We cannot extend
10256 // them properly to a wider type, so let us just reject this case.
10257 // TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help
10258 // to avoid this check.
10262 // FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0.
10263 auto *WTy = getWiderType(DTy, FRHSTy);
10264 auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy);
10265 auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy);
10267 // Try to prove the following rule:
10268 // (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS).
10269 // For example, given that FoundLHS > 2. It means that FoundLHS is at
10270 // least 3. If we divide it by Denominator < 4, we will have at least 1.
10271 auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2));
10272 if (isKnownNonPositive(RHS) &&
10273 IsSGTViaContext(FoundRHSExt, DenomMinusTwo))
10276 // Try to prove the following rule:
10277 // (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS).
10278 // For example, given that FoundLHS > -3. Then FoundLHS is at least -2.
10279 // If we divide it by Denominator > 2, then:
10280 // 1. If FoundLHS is negative, then the result is 0.
10281 // 2. If FoundLHS is non-negative, then the result is non-negative.
10282 // Anyways, the result is non-negative.
10283 auto *MinusOne = getNegativeSCEV(getOne(WTy));
10284 auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt);
10285 if (isKnownNegative(RHS) &&
10286 IsSGTViaContext(FoundRHSExt, NegDenomMinusOne))
10291 // If our expression contained SCEVUnknown Phis, and we split it down and now
10292 // need to prove something for them, try to prove the predicate for every
10293 // possible incoming values of those Phis.
10294 if (isImpliedViaMerge(Pred, OrigLHS, RHS, OrigFoundLHS, FoundRHS, Depth + 1))
10301 ScalarEvolution::isKnownViaNonRecursiveReasoning(ICmpInst::Predicate Pred,
10302 const SCEV *LHS, const SCEV *RHS) {
10303 return isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
10304 IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) ||
10305 IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) ||
10306 isKnownPredicateViaNoOverflow(Pred, LHS, RHS);
10310 ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
10311 const SCEV *LHS, const SCEV *RHS,
10312 const SCEV *FoundLHS,
10313 const SCEV *FoundRHS) {
10315 default: llvm_unreachable("Unexpected ICmpInst::Predicate value!");
10316 case ICmpInst::ICMP_EQ:
10317 case ICmpInst::ICMP_NE:
10318 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
10321 case ICmpInst::ICMP_SLT:
10322 case ICmpInst::ICMP_SLE:
10323 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
10324 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS))
10327 case ICmpInst::ICMP_SGT:
10328 case ICmpInst::ICMP_SGE:
10329 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
10330 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS))
10333 case ICmpInst::ICMP_ULT:
10334 case ICmpInst::ICMP_ULE:
10335 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
10336 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS))
10339 case ICmpInst::ICMP_UGT:
10340 case ICmpInst::ICMP_UGE:
10341 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
10342 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS))
10347 // Maybe it can be proved via operations?
10348 if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS))
10354 bool ScalarEvolution::isImpliedCondOperandsViaRanges(ICmpInst::Predicate Pred,
10357 const SCEV *FoundLHS,
10358 const SCEV *FoundRHS) {
10359 if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS))
10360 // The restriction on `FoundRHS` be lifted easily -- it exists only to
10361 // reduce the compile time impact of this optimization.
10364 Optional<APInt> Addend = computeConstantDifference(LHS, FoundLHS);
10368 const APInt &ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt();
10370 // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the
10371 // antecedent "`FoundLHS` `Pred` `FoundRHS`".
10372 ConstantRange FoundLHSRange =
10373 ConstantRange::makeAllowedICmpRegion(Pred, ConstFoundRHS);
10375 // Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`:
10376 ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend));
10378 // We can also compute the range of values for `LHS` that satisfy the
10379 // consequent, "`LHS` `Pred` `RHS`":
10380 const APInt &ConstRHS = cast<SCEVConstant>(RHS)->getAPInt();
10381 ConstantRange SatisfyingLHSRange =
10382 ConstantRange::makeSatisfyingICmpRegion(Pred, ConstRHS);
10384 // The antecedent implies the consequent if every value of `LHS` that
10385 // satisfies the antecedent also satisfies the consequent.
10386 return SatisfyingLHSRange.contains(LHSRange);
10389 bool ScalarEvolution::doesIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
10390 bool IsSigned, bool NoWrap) {
10391 assert(isKnownPositive(Stride) && "Positive stride expected!");
10393 if (NoWrap) return false;
10395 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
10396 const SCEV *One = getOne(Stride->getType());
10399 APInt MaxRHS = getSignedRangeMax(RHS);
10400 APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
10401 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
10403 // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow!
10404 return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS);
10407 APInt MaxRHS = getUnsignedRangeMax(RHS);
10408 APInt MaxValue = APInt::getMaxValue(BitWidth);
10409 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
10411 // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow!
10412 return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS);
10415 bool ScalarEvolution::doesIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
10416 bool IsSigned, bool NoWrap) {
10417 if (NoWrap) return false;
10419 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
10420 const SCEV *One = getOne(Stride->getType());
10423 APInt MinRHS = getSignedRangeMin(RHS);
10424 APInt MinValue = APInt::getSignedMinValue(BitWidth);
10425 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
10427 // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow!
10428 return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS);
10431 APInt MinRHS = getUnsignedRangeMin(RHS);
10432 APInt MinValue = APInt::getMinValue(BitWidth);
10433 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
10435 // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow!
10436 return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS);
10439 const SCEV *ScalarEvolution::computeBECount(const SCEV *Delta, const SCEV *Step,
10441 const SCEV *One = getOne(Step->getType());
10442 Delta = Equality ? getAddExpr(Delta, Step)
10443 : getAddExpr(Delta, getMinusSCEV(Step, One));
10444 return getUDivExpr(Delta, Step);
10447 const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
10448 const SCEV *Stride,
10453 assert(!isKnownNonPositive(Stride) &&
10454 "Stride is expected strictly positive!");
10455 // Calculate the maximum backedge count based on the range of values
10456 // permitted by Start, End, and Stride.
10457 const SCEV *MaxBECount;
10459 IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start);
10461 APInt StrideForMaxBECount =
10462 IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride);
10464 // We already know that the stride is positive, so we paper over conservatism
10465 // in our range computation by forcing StrideForMaxBECount to be at least one.
10466 // In theory this is unnecessary, but we expect MaxBECount to be a
10467 // SCEVConstant, and (udiv <constant> 0) is not constant folded by SCEV (there
10468 // is nothing to constant fold it to).
10469 APInt One(BitWidth, 1, IsSigned);
10470 StrideForMaxBECount = APIntOps::smax(One, StrideForMaxBECount);
10472 APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth)
10473 : APInt::getMaxValue(BitWidth);
10474 APInt Limit = MaxValue - (StrideForMaxBECount - 1);
10476 // Although End can be a MAX expression we estimate MaxEnd considering only
10477 // the case End = RHS of the loop termination condition. This is safe because
10478 // in the other case (End - Start) is zero, leading to a zero maximum backedge
10480 APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit)
10481 : APIntOps::umin(getUnsignedRangeMax(End), Limit);
10483 MaxBECount = computeBECount(getConstant(MaxEnd - MinStart) /* Delta */,
10484 getConstant(StrideForMaxBECount) /* Step */,
10485 false /* Equality */);
10490 ScalarEvolution::ExitLimit
10491 ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
10492 const Loop *L, bool IsSigned,
10493 bool ControlsExit, bool AllowPredicates) {
10494 SmallPtrSet<const SCEVPredicate *, 4> Predicates;
10496 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
10497 bool PredicatedIV = false;
10499 if (!IV && AllowPredicates) {
10500 // Try to make this an AddRec using runtime tests, in the first X
10501 // iterations of this loop, where X is the SCEV expression found by the
10502 // algorithm below.
10503 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
10504 PredicatedIV = true;
10507 // Avoid weird loops
10508 if (!IV || IV->getLoop() != L || !IV->isAffine())
10509 return getCouldNotCompute();
10511 bool NoWrap = ControlsExit &&
10512 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
10514 const SCEV *Stride = IV->getStepRecurrence(*this);
10516 bool PositiveStride = isKnownPositive(Stride);
10518 // Avoid negative or zero stride values.
10519 if (!PositiveStride) {
10520 // We can compute the correct backedge taken count for loops with unknown
10521 // strides if we can prove that the loop is not an infinite loop with side
10522 // effects. Here's the loop structure we are trying to handle -
10528 // } while (i < end);
10530 // The backedge taken count for such loops is evaluated as -
10531 // (max(end, start + stride) - start - 1) /u stride
10533 // The additional preconditions that we need to check to prove correctness
10534 // of the above formula is as follows -
10536 // a) IV is either nuw or nsw depending upon signedness (indicated by the
10538 // b) loop is single exit with no side effects.
10541 // Precondition a) implies that if the stride is negative, this is a single
10542 // trip loop. The backedge taken count formula reduces to zero in this case.
10544 // Precondition b) implies that the unknown stride cannot be zero otherwise
10547 // The positive stride case is the same as isKnownPositive(Stride) returning
10548 // true (original behavior of the function).
10550 // We want to make sure that the stride is truly unknown as there are edge
10551 // cases where ScalarEvolution propagates no wrap flags to the
10552 // post-increment/decrement IV even though the increment/decrement operation
10553 // itself is wrapping. The computed backedge taken count may be wrong in
10554 // such cases. This is prevented by checking that the stride is not known to
10555 // be either positive or non-positive. For example, no wrap flags are
10556 // propagated to the post-increment IV of this loop with a trip count of 2 -
10558 // unsigned char i;
10559 // for(i=127; i<128; i+=129)
10562 if (PredicatedIV || !NoWrap || isKnownNonPositive(Stride) ||
10563 !loopHasNoSideEffects(L))
10564 return getCouldNotCompute();
10565 } else if (!Stride->isOne() &&
10566 doesIVOverflowOnLT(RHS, Stride, IsSigned, NoWrap))
10567 // Avoid proven overflow cases: this will ensure that the backedge taken
10568 // count will not generate any unsigned overflow. Relaxed no-overflow
10569 // conditions exploit NoWrapFlags, allowing to optimize in presence of
10570 // undefined behaviors like the case of C language.
10571 return getCouldNotCompute();
10573 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT
10574 : ICmpInst::ICMP_ULT;
10575 const SCEV *Start = IV->getStart();
10576 const SCEV *End = RHS;
10577 // When the RHS is not invariant, we do not know the end bound of the loop and
10578 // cannot calculate the ExactBECount needed by ExitLimit. However, we can
10579 // calculate the MaxBECount, given the start, stride and max value for the end
10580 // bound of the loop (RHS), and the fact that IV does not overflow (which is
10582 if (!isLoopInvariant(RHS, L)) {
10583 const SCEV *MaxBECount = computeMaxBECountForLT(
10584 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
10585 return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
10586 false /*MaxOrZero*/, Predicates);
10588 // If the backedge is taken at least once, then it will be taken
10589 // (End-Start)/Stride times (rounded up to a multiple of Stride), where Start
10590 // is the LHS value of the less-than comparison the first time it is evaluated
10591 // and End is the RHS.
10592 const SCEV *BECountIfBackedgeTaken =
10593 computeBECount(getMinusSCEV(End, Start), Stride, false);
10594 // If the loop entry is guarded by the result of the backedge test of the
10595 // first loop iteration, then we know the backedge will be taken at least
10596 // once and so the backedge taken count is as above. If not then we use the
10597 // expression (max(End,Start)-Start)/Stride to describe the backedge count,
10598 // as if the backedge is taken at least once max(End,Start) is End and so the
10599 // result is as above, and if not max(End,Start) is Start so we get a backedge
10601 const SCEV *BECount;
10602 if (isLoopEntryGuardedByCond(L, Cond, getMinusSCEV(Start, Stride), RHS))
10603 BECount = BECountIfBackedgeTaken;
10605 End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start);
10606 BECount = computeBECount(getMinusSCEV(End, Start), Stride, false);
10609 const SCEV *MaxBECount;
10610 bool MaxOrZero = false;
10611 if (isa<SCEVConstant>(BECount))
10612 MaxBECount = BECount;
10613 else if (isa<SCEVConstant>(BECountIfBackedgeTaken)) {
10614 // If we know exactly how many times the backedge will be taken if it's
10615 // taken at least once, then the backedge count will either be that or
10617 MaxBECount = BECountIfBackedgeTaken;
10620 MaxBECount = computeMaxBECountForLT(
10621 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
10624 if (isa<SCEVCouldNotCompute>(MaxBECount) &&
10625 !isa<SCEVCouldNotCompute>(BECount))
10626 MaxBECount = getConstant(getUnsignedRangeMax(BECount));
10628 return ExitLimit(BECount, MaxBECount, MaxOrZero, Predicates);
10631 ScalarEvolution::ExitLimit
10632 ScalarEvolution::howManyGreaterThans(const SCEV *LHS, const SCEV *RHS,
10633 const Loop *L, bool IsSigned,
10634 bool ControlsExit, bool AllowPredicates) {
10635 SmallPtrSet<const SCEVPredicate *, 4> Predicates;
10636 // We handle only IV > Invariant
10637 if (!isLoopInvariant(RHS, L))
10638 return getCouldNotCompute();
10640 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
10641 if (!IV && AllowPredicates)
10642 // Try to make this an AddRec using runtime tests, in the first X
10643 // iterations of this loop, where X is the SCEV expression found by the
10644 // algorithm below.
10645 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
10647 // Avoid weird loops
10648 if (!IV || IV->getLoop() != L || !IV->isAffine())
10649 return getCouldNotCompute();
10651 bool NoWrap = ControlsExit &&
10652 IV->getNoWrapFlags(IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW);
10654 const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this));
10656 // Avoid negative or zero stride values
10657 if (!isKnownPositive(Stride))
10658 return getCouldNotCompute();
10660 // Avoid proven overflow cases: this will ensure that the backedge taken count
10661 // will not generate any unsigned overflow. Relaxed no-overflow conditions
10662 // exploit NoWrapFlags, allowing to optimize in presence of undefined
10663 // behaviors like the case of C language.
10664 if (!Stride->isOne() && doesIVOverflowOnGT(RHS, Stride, IsSigned, NoWrap))
10665 return getCouldNotCompute();
10667 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT
10668 : ICmpInst::ICMP_UGT;
10670 const SCEV *Start = IV->getStart();
10671 const SCEV *End = RHS;
10672 if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS))
10673 End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start);
10675 const SCEV *BECount = computeBECount(getMinusSCEV(Start, End), Stride, false);
10677 APInt MaxStart = IsSigned ? getSignedRangeMax(Start)
10678 : getUnsignedRangeMax(Start);
10680 APInt MinStride = IsSigned ? getSignedRangeMin(Stride)
10681 : getUnsignedRangeMin(Stride);
10683 unsigned BitWidth = getTypeSizeInBits(LHS->getType());
10684 APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1)
10685 : APInt::getMinValue(BitWidth) + (MinStride - 1);
10687 // Although End can be a MIN expression we estimate MinEnd considering only
10688 // the case End = RHS. This is safe because in the other case (Start - End)
10689 // is zero, leading to a zero maximum backedge taken count.
10691 IsSigned ? APIntOps::smax(getSignedRangeMin(RHS), Limit)
10692 : APIntOps::umax(getUnsignedRangeMin(RHS), Limit);
10695 const SCEV *MaxBECount = getCouldNotCompute();
10696 if (isa<SCEVConstant>(BECount))
10697 MaxBECount = BECount;
10699 MaxBECount = computeBECount(getConstant(MaxStart - MinEnd),
10700 getConstant(MinStride), false);
10702 if (isa<SCEVCouldNotCompute>(MaxBECount))
10703 MaxBECount = BECount;
10705 return ExitLimit(BECount, MaxBECount, false, Predicates);
10708 const SCEV *SCEVAddRecExpr::getNumIterationsInRange(const ConstantRange &Range,
10709 ScalarEvolution &SE) const {
10710 if (Range.isFullSet()) // Infinite loop.
10711 return SE.getCouldNotCompute();
10713 // If the start is a non-zero constant, shift the range to simplify things.
10714 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
10715 if (!SC->getValue()->isZero()) {
10716 SmallVector<const SCEV *, 4> Operands(op_begin(), op_end());
10717 Operands[0] = SE.getZero(SC->getType());
10718 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(),
10719 getNoWrapFlags(FlagNW));
10720 if (const auto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
10721 return ShiftedAddRec->getNumIterationsInRange(
10722 Range.subtract(SC->getAPInt()), SE);
10723 // This is strange and shouldn't happen.
10724 return SE.getCouldNotCompute();
10727 // The only time we can solve this is when we have all constant indices.
10728 // Otherwise, we cannot determine the overflow conditions.
10729 if (any_of(operands(), [](const SCEV *Op) { return !isa<SCEVConstant>(Op); }))
10730 return SE.getCouldNotCompute();
10732 // Okay at this point we know that all elements of the chrec are constants and
10733 // that the start element is zero.
10735 // First check to see if the range contains zero. If not, the first
10736 // iteration exits.
10737 unsigned BitWidth = SE.getTypeSizeInBits(getType());
10738 if (!Range.contains(APInt(BitWidth, 0)))
10739 return SE.getZero(getType());
10742 // If this is an affine expression then we have this situation:
10743 // Solve {0,+,A} in Range === Ax in Range
10745 // We know that zero is in the range. If A is positive then we know that
10746 // the upper value of the range must be the first possible exit value.
10747 // If A is negative then the lower of the range is the last possible loop
10748 // value. Also note that we already checked for a full range.
10749 APInt A = cast<SCEVConstant>(getOperand(1))->getAPInt();
10750 APInt End = A.sge(1) ? (Range.getUpper() - 1) : Range.getLower();
10752 // The exit value should be (End+A)/A.
10753 APInt ExitVal = (End + A).udiv(A);
10754 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
10756 // Evaluate at the exit value. If we really did fall out of the valid
10757 // range, then we computed our trip count, otherwise wrap around or other
10758 // things must have happened.
10759 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
10760 if (Range.contains(Val->getValue()))
10761 return SE.getCouldNotCompute(); // Something strange happened
10763 // Ensure that the previous value is in the range. This is a sanity check.
10764 assert(Range.contains(
10765 EvaluateConstantChrecAtConstant(this,
10766 ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) &&
10767 "Linear scev computation is off in a bad way!");
10768 return SE.getConstant(ExitValue);
10771 if (isQuadratic()) {
10772 if (auto S = SolveQuadraticAddRecRange(this, Range, SE))
10773 return SE.getConstant(S.getValue());
10776 return SE.getCouldNotCompute();
10779 const SCEVAddRecExpr *
10780 SCEVAddRecExpr::getPostIncExpr(ScalarEvolution &SE) const {
10781 assert(getNumOperands() > 1 && "AddRec with zero step?");
10782 // There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)),
10783 // but in this case we cannot guarantee that the value returned will be an
10784 // AddRec because SCEV does not have a fixed point where it stops
10785 // simplification: it is legal to return ({rec1} + {rec2}). For example, it
10786 // may happen if we reach arithmetic depth limit while simplifying. So we
10787 // construct the returned value explicitly.
10788 SmallVector<const SCEV *, 3> Ops;
10789 // If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and
10790 // (this + Step) is {A+B,+,B+C,+...,+,N}.
10791 for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i)
10792 Ops.push_back(SE.getAddExpr(getOperand(i), getOperand(i + 1)));
10793 // We know that the last operand is not a constant zero (otherwise it would
10794 // have been popped out earlier). This guarantees us that if the result has
10795 // the same last operand, then it will also not be popped out, meaning that
10796 // the returned value will be an AddRec.
10797 const SCEV *Last = getOperand(getNumOperands() - 1);
10798 assert(!Last->isZero() && "Recurrency with zero step?");
10799 Ops.push_back(Last);
10800 return cast<SCEVAddRecExpr>(SE.getAddRecExpr(Ops, getLoop(),
10801 SCEV::FlagAnyWrap));
10804 // Return true when S contains at least an undef value.
10805 static inline bool containsUndefs(const SCEV *S) {
10806 return SCEVExprContains(S, [](const SCEV *S) {
10807 if (const auto *SU = dyn_cast<SCEVUnknown>(S))
10808 return isa<UndefValue>(SU->getValue());
10809 else if (const auto *SC = dyn_cast<SCEVConstant>(S))
10810 return isa<UndefValue>(SC->getValue());
10817 // Collect all steps of SCEV expressions.
10818 struct SCEVCollectStrides {
10819 ScalarEvolution &SE;
10820 SmallVectorImpl<const SCEV *> &Strides;
10822 SCEVCollectStrides(ScalarEvolution &SE, SmallVectorImpl<const SCEV *> &S)
10823 : SE(SE), Strides(S) {}
10825 bool follow(const SCEV *S) {
10826 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S))
10827 Strides.push_back(AR->getStepRecurrence(SE));
10831 bool isDone() const { return false; }
10834 // Collect all SCEVUnknown and SCEVMulExpr expressions.
10835 struct SCEVCollectTerms {
10836 SmallVectorImpl<const SCEV *> &Terms;
10838 SCEVCollectTerms(SmallVectorImpl<const SCEV *> &T) : Terms(T) {}
10840 bool follow(const SCEV *S) {
10841 if (isa<SCEVUnknown>(S) || isa<SCEVMulExpr>(S) ||
10842 isa<SCEVSignExtendExpr>(S)) {
10843 if (!containsUndefs(S))
10844 Terms.push_back(S);
10846 // Stop recursion: once we collected a term, do not walk its operands.
10854 bool isDone() const { return false; }
10857 // Check if a SCEV contains an AddRecExpr.
10858 struct SCEVHasAddRec {
10859 bool &ContainsAddRec;
10861 SCEVHasAddRec(bool &ContainsAddRec) : ContainsAddRec(ContainsAddRec) {
10862 ContainsAddRec = false;
10865 bool follow(const SCEV *S) {
10866 if (isa<SCEVAddRecExpr>(S)) {
10867 ContainsAddRec = true;
10869 // Stop recursion: once we collected a term, do not walk its operands.
10877 bool isDone() const { return false; }
10880 // Find factors that are multiplied with an expression that (possibly as a
10881 // subexpression) contains an AddRecExpr. In the expression:
10883 // 8 * (100 + %p * %q * (%a + {0, +, 1}_loop))
10885 // "%p * %q" are factors multiplied by the expression "(%a + {0, +, 1}_loop)"
10886 // that contains the AddRec {0, +, 1}_loop. %p * %q are likely to be array size
10887 // parameters as they form a product with an induction variable.
10889 // This collector expects all array size parameters to be in the same MulExpr.
10890 // It might be necessary to later add support for collecting parameters that are
10891 // spread over different nested MulExpr.
10892 struct SCEVCollectAddRecMultiplies {
10893 SmallVectorImpl<const SCEV *> &Terms;
10894 ScalarEvolution &SE;
10896 SCEVCollectAddRecMultiplies(SmallVectorImpl<const SCEV *> &T, ScalarEvolution &SE)
10897 : Terms(T), SE(SE) {}
10899 bool follow(const SCEV *S) {
10900 if (auto *Mul = dyn_cast<SCEVMulExpr>(S)) {
10901 bool HasAddRec = false;
10902 SmallVector<const SCEV *, 0> Operands;
10903 for (auto Op : Mul->operands()) {
10904 const SCEVUnknown *Unknown = dyn_cast<SCEVUnknown>(Op);
10905 if (Unknown && !isa<CallInst>(Unknown->getValue())) {
10906 Operands.push_back(Op);
10907 } else if (Unknown) {
10910 bool ContainsAddRec;
10911 SCEVHasAddRec ContiansAddRec(ContainsAddRec);
10912 visitAll(Op, ContiansAddRec);
10913 HasAddRec |= ContainsAddRec;
10916 if (Operands.size() == 0)
10922 Terms.push_back(SE.getMulExpr(Operands));
10923 // Stop recursion: once we collected a term, do not walk its operands.
10931 bool isDone() const { return false; }
10934 } // end anonymous namespace
10936 /// Find parametric terms in this SCEVAddRecExpr. We first for parameters in
10938 /// 1) The strides of AddRec expressions.
10939 /// 2) Unknowns that are multiplied with AddRec expressions.
10940 void ScalarEvolution::collectParametricTerms(const SCEV *Expr,
10941 SmallVectorImpl<const SCEV *> &Terms) {
10942 SmallVector<const SCEV *, 4> Strides;
10943 SCEVCollectStrides StrideCollector(*this, Strides);
10944 visitAll(Expr, StrideCollector);
10947 dbgs() << "Strides:\n";
10948 for (const SCEV *S : Strides)
10949 dbgs() << *S << "\n";
10952 for (const SCEV *S : Strides) {
10953 SCEVCollectTerms TermCollector(Terms);
10954 visitAll(S, TermCollector);
10958 dbgs() << "Terms:\n";
10959 for (const SCEV *T : Terms)
10960 dbgs() << *T << "\n";
10963 SCEVCollectAddRecMultiplies MulCollector(Terms, *this);
10964 visitAll(Expr, MulCollector);
10967 static bool findArrayDimensionsRec(ScalarEvolution &SE,
10968 SmallVectorImpl<const SCEV *> &Terms,
10969 SmallVectorImpl<const SCEV *> &Sizes) {
10970 int Last = Terms.size() - 1;
10971 const SCEV *Step = Terms[Last];
10973 // End of recursion.
10975 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Step)) {
10976 SmallVector<const SCEV *, 2> Qs;
10977 for (const SCEV *Op : M->operands())
10978 if (!isa<SCEVConstant>(Op))
10981 Step = SE.getMulExpr(Qs);
10984 Sizes.push_back(Step);
10988 for (const SCEV *&Term : Terms) {
10989 // Normalize the terms before the next call to findArrayDimensionsRec.
10991 SCEVDivision::divide(SE, Term, Step, &Q, &R);
10993 // Bail out when GCD does not evenly divide one of the terms.
11000 // Remove all SCEVConstants.
11002 remove_if(Terms, [](const SCEV *E) { return isa<SCEVConstant>(E); }),
11005 if (Terms.size() > 0)
11006 if (!findArrayDimensionsRec(SE, Terms, Sizes))
11009 Sizes.push_back(Step);
11013 // Returns true when one of the SCEVs of Terms contains a SCEVUnknown parameter.
11014 static inline bool containsParameters(SmallVectorImpl<const SCEV *> &Terms) {
11015 for (const SCEV *T : Terms)
11016 if (SCEVExprContains(T, isa<SCEVUnknown, const SCEV *>))
11021 // Return the number of product terms in S.
11022 static inline int numberOfTerms(const SCEV *S) {
11023 if (const SCEVMulExpr *Expr = dyn_cast<SCEVMulExpr>(S))
11024 return Expr->getNumOperands();
11028 static const SCEV *removeConstantFactors(ScalarEvolution &SE, const SCEV *T) {
11029 if (isa<SCEVConstant>(T))
11032 if (isa<SCEVUnknown>(T))
11035 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(T)) {
11036 SmallVector<const SCEV *, 2> Factors;
11037 for (const SCEV *Op : M->operands())
11038 if (!isa<SCEVConstant>(Op))
11039 Factors.push_back(Op);
11041 return SE.getMulExpr(Factors);
11047 /// Return the size of an element read or written by Inst.
11048 const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) {
11050 if (StoreInst *Store = dyn_cast<StoreInst>(Inst))
11051 Ty = Store->getValueOperand()->getType();
11052 else if (LoadInst *Load = dyn_cast<LoadInst>(Inst))
11053 Ty = Load->getType();
11057 Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty));
11058 return getSizeOfExpr(ETy, Ty);
11061 void ScalarEvolution::findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
11062 SmallVectorImpl<const SCEV *> &Sizes,
11063 const SCEV *ElementSize) {
11064 if (Terms.size() < 1 || !ElementSize)
11067 // Early return when Terms do not contain parameters: we do not delinearize
11068 // non parametric SCEVs.
11069 if (!containsParameters(Terms))
11073 dbgs() << "Terms:\n";
11074 for (const SCEV *T : Terms)
11075 dbgs() << *T << "\n";
11078 // Remove duplicates.
11079 array_pod_sort(Terms.begin(), Terms.end());
11080 Terms.erase(std::unique(Terms.begin(), Terms.end()), Terms.end());
11082 // Put larger terms first.
11083 llvm::sort(Terms, [](const SCEV *LHS, const SCEV *RHS) {
11084 return numberOfTerms(LHS) > numberOfTerms(RHS);
11087 // Try to divide all terms by the element size. If term is not divisible by
11088 // element size, proceed with the original term.
11089 for (const SCEV *&Term : Terms) {
11091 SCEVDivision::divide(*this, Term, ElementSize, &Q, &R);
11096 SmallVector<const SCEV *, 4> NewTerms;
11098 // Remove constant factors.
11099 for (const SCEV *T : Terms)
11100 if (const SCEV *NewT = removeConstantFactors(*this, T))
11101 NewTerms.push_back(NewT);
11104 dbgs() << "Terms after sorting:\n";
11105 for (const SCEV *T : NewTerms)
11106 dbgs() << *T << "\n";
11109 if (NewTerms.empty() || !findArrayDimensionsRec(*this, NewTerms, Sizes)) {
11114 // The last element to be pushed into Sizes is the size of an element.
11115 Sizes.push_back(ElementSize);
11118 dbgs() << "Sizes:\n";
11119 for (const SCEV *S : Sizes)
11120 dbgs() << *S << "\n";
11124 void ScalarEvolution::computeAccessFunctions(
11125 const SCEV *Expr, SmallVectorImpl<const SCEV *> &Subscripts,
11126 SmallVectorImpl<const SCEV *> &Sizes) {
11127 // Early exit in case this SCEV is not an affine multivariate function.
11131 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Expr))
11132 if (!AR->isAffine())
11135 const SCEV *Res = Expr;
11136 int Last = Sizes.size() - 1;
11137 for (int i = Last; i >= 0; i--) {
11139 SCEVDivision::divide(*this, Res, Sizes[i], &Q, &R);
11142 dbgs() << "Res: " << *Res << "\n";
11143 dbgs() << "Sizes[i]: " << *Sizes[i] << "\n";
11144 dbgs() << "Res divided by Sizes[i]:\n";
11145 dbgs() << "Quotient: " << *Q << "\n";
11146 dbgs() << "Remainder: " << *R << "\n";
11151 // Do not record the last subscript corresponding to the size of elements in
11155 // Bail out if the remainder is too complex.
11156 if (isa<SCEVAddRecExpr>(R)) {
11157 Subscripts.clear();
11165 // Record the access function for the current subscript.
11166 Subscripts.push_back(R);
11169 // Also push in last position the remainder of the last division: it will be
11170 // the access function of the innermost dimension.
11171 Subscripts.push_back(Res);
11173 std::reverse(Subscripts.begin(), Subscripts.end());
11176 dbgs() << "Subscripts:\n";
11177 for (const SCEV *S : Subscripts)
11178 dbgs() << *S << "\n";
11182 /// Splits the SCEV into two vectors of SCEVs representing the subscripts and
11183 /// sizes of an array access. Returns the remainder of the delinearization that
11184 /// is the offset start of the array. The SCEV->delinearize algorithm computes
11185 /// the multiples of SCEV coefficients: that is a pattern matching of sub
11186 /// expressions in the stride and base of a SCEV corresponding to the
11187 /// computation of a GCD (greatest common divisor) of base and stride. When
11188 /// SCEV->delinearize fails, it returns the SCEV unchanged.
11190 /// For example: when analyzing the memory access A[i][j][k] in this loop nest
11192 /// void foo(long n, long m, long o, double A[n][m][o]) {
11194 /// for (long i = 0; i < n; i++)
11195 /// for (long j = 0; j < m; j++)
11196 /// for (long k = 0; k < o; k++)
11197 /// A[i][j][k] = 1.0;
11200 /// the delinearization input is the following AddRec SCEV:
11202 /// AddRec: {{{%A,+,(8 * %m * %o)}<%for.i>,+,(8 * %o)}<%for.j>,+,8}<%for.k>
11204 /// From this SCEV, we are able to say that the base offset of the access is %A
11205 /// because it appears as an offset that does not divide any of the strides in
11208 /// CHECK: Base offset: %A
11210 /// and then SCEV->delinearize determines the size of some of the dimensions of
11211 /// the array as these are the multiples by which the strides are happening:
11213 /// CHECK: ArrayDecl[UnknownSize][%m][%o] with elements of sizeof(double) bytes.
11215 /// Note that the outermost dimension remains of UnknownSize because there are
11216 /// no strides that would help identifying the size of the last dimension: when
11217 /// the array has been statically allocated, one could compute the size of that
11218 /// dimension by dividing the overall size of the array by the size of the known
11219 /// dimensions: %m * %o * 8.
11221 /// Finally delinearize provides the access functions for the array reference
11222 /// that does correspond to A[i][j][k] of the above C testcase:
11224 /// CHECK: ArrayRef[{0,+,1}<%for.i>][{0,+,1}<%for.j>][{0,+,1}<%for.k>]
11226 /// The testcases are checking the output of a function pass:
11227 /// DelinearizationPass that walks through all loads and stores of a function
11228 /// asking for the SCEV of the memory access with respect to all enclosing
11229 /// loops, calling SCEV->delinearize on that and printing the results.
11230 void ScalarEvolution::delinearize(const SCEV *Expr,
11231 SmallVectorImpl<const SCEV *> &Subscripts,
11232 SmallVectorImpl<const SCEV *> &Sizes,
11233 const SCEV *ElementSize) {
11234 // First step: collect parametric terms.
11235 SmallVector<const SCEV *, 4> Terms;
11236 collectParametricTerms(Expr, Terms);
11241 // Second step: find subscript sizes.
11242 findArrayDimensions(Terms, Sizes, ElementSize);
11247 // Third step: compute the access functions for each subscript.
11248 computeAccessFunctions(Expr, Subscripts, Sizes);
11250 if (Subscripts.empty())
11254 dbgs() << "succeeded to delinearize " << *Expr << "\n";
11255 dbgs() << "ArrayDecl[UnknownSize]";
11256 for (const SCEV *S : Sizes)
11257 dbgs() << "[" << *S << "]";
11259 dbgs() << "\nArrayRef";
11260 for (const SCEV *S : Subscripts)
11261 dbgs() << "[" << *S << "]";
11266 //===----------------------------------------------------------------------===//
11267 // SCEVCallbackVH Class Implementation
11268 //===----------------------------------------------------------------------===//
11270 void ScalarEvolution::SCEVCallbackVH::deleted() {
11271 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
11272 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
11273 SE->ConstantEvolutionLoopExitValue.erase(PN);
11274 SE->eraseValueFromMap(getValPtr());
11275 // this now dangles!
11278 void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) {
11279 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
11281 // Forget all the expressions associated with users of the old value,
11282 // so that future queries will recompute the expressions using the new
11284 Value *Old = getValPtr();
11285 SmallVector<User *, 16> Worklist(Old->user_begin(), Old->user_end());
11286 SmallPtrSet<User *, 8> Visited;
11287 while (!Worklist.empty()) {
11288 User *U = Worklist.pop_back_val();
11289 // Deleting the Old value will cause this to dangle. Postpone
11290 // that until everything else is done.
11293 if (!Visited.insert(U).second)
11295 if (PHINode *PN = dyn_cast<PHINode>(U))
11296 SE->ConstantEvolutionLoopExitValue.erase(PN);
11297 SE->eraseValueFromMap(U);
11298 Worklist.insert(Worklist.end(), U->user_begin(), U->user_end());
11300 // Delete the Old value.
11301 if (PHINode *PN = dyn_cast<PHINode>(Old))
11302 SE->ConstantEvolutionLoopExitValue.erase(PN);
11303 SE->eraseValueFromMap(Old);
11304 // this now dangles!
11307 ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
11308 : CallbackVH(V), SE(se) {}
11310 //===----------------------------------------------------------------------===//
11311 // ScalarEvolution Class Implementation
11312 //===----------------------------------------------------------------------===//
11314 ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI,
11315 AssumptionCache &AC, DominatorTree &DT,
11317 : F(F), TLI(TLI), AC(AC), DT(DT), LI(LI),
11318 CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64),
11319 LoopDispositions(64), BlockDispositions(64) {
11320 // To use guards for proving predicates, we need to scan every instruction in
11321 // relevant basic blocks, and not just terminators. Doing this is a waste of
11322 // time if the IR does not actually contain any calls to
11323 // @llvm.experimental.guard, so do a quick check and remember this beforehand.
11325 // This pessimizes the case where a pass that preserves ScalarEvolution wants
11326 // to _add_ guards to the module when there weren't any before, and wants
11327 // ScalarEvolution to optimize based on those guards. For now we prefer to be
11328 // efficient in lieu of being smart in that rather obscure case.
11330 auto *GuardDecl = F.getParent()->getFunction(
11331 Intrinsic::getName(Intrinsic::experimental_guard));
11332 HasGuards = GuardDecl && !GuardDecl->use_empty();
11335 ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
11336 : F(Arg.F), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC), DT(Arg.DT),
11337 LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)),
11338 ValueExprMap(std::move(Arg.ValueExprMap)),
11339 PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
11340 PendingPhiRanges(std::move(Arg.PendingPhiRanges)),
11341 PendingMerges(std::move(Arg.PendingMerges)),
11342 MinTrailingZerosCache(std::move(Arg.MinTrailingZerosCache)),
11343 BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)),
11344 PredicatedBackedgeTakenCounts(
11345 std::move(Arg.PredicatedBackedgeTakenCounts)),
11346 ConstantEvolutionLoopExitValue(
11347 std::move(Arg.ConstantEvolutionLoopExitValue)),
11348 ValuesAtScopes(std::move(Arg.ValuesAtScopes)),
11349 LoopDispositions(std::move(Arg.LoopDispositions)),
11350 LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)),
11351 BlockDispositions(std::move(Arg.BlockDispositions)),
11352 UnsignedRanges(std::move(Arg.UnsignedRanges)),
11353 SignedRanges(std::move(Arg.SignedRanges)),
11354 UniqueSCEVs(std::move(Arg.UniqueSCEVs)),
11355 UniquePreds(std::move(Arg.UniquePreds)),
11356 SCEVAllocator(std::move(Arg.SCEVAllocator)),
11357 LoopUsers(std::move(Arg.LoopUsers)),
11358 PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)),
11359 FirstUnknown(Arg.FirstUnknown) {
11360 Arg.FirstUnknown = nullptr;
11363 ScalarEvolution::~ScalarEvolution() {
11364 // Iterate through all the SCEVUnknown instances and call their
11365 // destructors, so that they release their references to their values.
11366 for (SCEVUnknown *U = FirstUnknown; U;) {
11367 SCEVUnknown *Tmp = U;
11369 Tmp->~SCEVUnknown();
11371 FirstUnknown = nullptr;
11373 ExprValueMap.clear();
11374 ValueExprMap.clear();
11377 // Free any extra memory created for ExitNotTakenInfo in the unlikely event
11378 // that a loop had multiple computable exits.
11379 for (auto &BTCI : BackedgeTakenCounts)
11380 BTCI.second.clear();
11381 for (auto &BTCI : PredicatedBackedgeTakenCounts)
11382 BTCI.second.clear();
11384 assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
11385 assert(PendingPhiRanges.empty() && "getRangeRef garbage");
11386 assert(PendingMerges.empty() && "isImpliedViaMerge garbage");
11387 assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!");
11388 assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!");
11391 bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
11392 return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
11395 static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
11397 // Print all inner loops first
11399 PrintLoopInfo(OS, SE, I);
11402 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11405 SmallVector<BasicBlock *, 8> ExitBlocks;
11406 L->getExitBlocks(ExitBlocks);
11407 if (ExitBlocks.size() != 1)
11408 OS << "<multiple exits> ";
11410 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
11411 OS << "backedge-taken count is " << *SE->getBackedgeTakenCount(L);
11413 OS << "Unpredictable backedge-taken count. ";
11418 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11421 if (!isa<SCEVCouldNotCompute>(SE->getMaxBackedgeTakenCount(L))) {
11422 OS << "max backedge-taken count is " << *SE->getMaxBackedgeTakenCount(L);
11423 if (SE->isBackedgeTakenCountMaxOrZero(L))
11424 OS << ", actual taken count either this or zero.";
11426 OS << "Unpredictable max backedge-taken count. ";
11431 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11434 SCEVUnionPredicate Pred;
11435 auto PBT = SE->getPredicatedBackedgeTakenCount(L, Pred);
11436 if (!isa<SCEVCouldNotCompute>(PBT)) {
11437 OS << "Predicated backedge-taken count is " << *PBT << "\n";
11438 OS << " Predicates:\n";
11441 OS << "Unpredictable predicated backedge-taken count. ";
11445 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
11447 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11449 OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n";
11453 static StringRef loopDispositionToStr(ScalarEvolution::LoopDisposition LD) {
11455 case ScalarEvolution::LoopVariant:
11457 case ScalarEvolution::LoopInvariant:
11458 return "Invariant";
11459 case ScalarEvolution::LoopComputable:
11460 return "Computable";
11462 llvm_unreachable("Unknown ScalarEvolution::LoopDisposition kind!");
11465 void ScalarEvolution::print(raw_ostream &OS) const {
11466 // ScalarEvolution's implementation of the print method is to print
11467 // out SCEV values of all instructions that are interesting. Doing
11468 // this potentially causes it to create new SCEV objects though,
11469 // which technically conflicts with the const qualifier. This isn't
11470 // observable from outside the class though, so casting away the
11471 // const isn't dangerous.
11472 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
11474 OS << "Classifying expressions for: ";
11475 F.printAsOperand(OS, /*PrintType=*/false);
11477 for (Instruction &I : instructions(F))
11478 if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
11481 const SCEV *SV = SE.getSCEV(&I);
11483 if (!isa<SCEVCouldNotCompute>(SV)) {
11485 SE.getUnsignedRange(SV).print(OS);
11487 SE.getSignedRange(SV).print(OS);
11490 const Loop *L = LI.getLoopFor(I.getParent());
11492 const SCEV *AtUse = SE.getSCEVAtScope(SV, L);
11496 if (!isa<SCEVCouldNotCompute>(AtUse)) {
11498 SE.getUnsignedRange(AtUse).print(OS);
11500 SE.getSignedRange(AtUse).print(OS);
11505 OS << "\t\t" "Exits: ";
11506 const SCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
11507 if (!SE.isLoopInvariant(ExitValue, L)) {
11508 OS << "<<Unknown>>";
11514 for (auto *Iter = L; Iter; Iter = Iter->getParentLoop()) {
11516 OS << "\t\t" "LoopDispositions: { ";
11522 Iter->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11523 OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, Iter));
11526 for (auto *InnerL : depth_first(L)) {
11530 OS << "\t\t" "LoopDispositions: { ";
11536 InnerL->getHeader()->printAsOperand(OS, /*PrintType=*/false);
11537 OS << ": " << loopDispositionToStr(SE.getLoopDisposition(SV, InnerL));
11546 OS << "Determining loop execution counts for: ";
11547 F.printAsOperand(OS, /*PrintType=*/false);
11550 PrintLoopInfo(OS, &SE, I);
11553 ScalarEvolution::LoopDisposition
11554 ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) {
11555 auto &Values = LoopDispositions[S];
11556 for (auto &V : Values) {
11557 if (V.getPointer() == L)
11560 Values.emplace_back(L, LoopVariant);
11561 LoopDisposition D = computeLoopDisposition(S, L);
11562 auto &Values2 = LoopDispositions[S];
11563 for (auto &V : make_range(Values2.rbegin(), Values2.rend())) {
11564 if (V.getPointer() == L) {
11572 ScalarEvolution::LoopDisposition
11573 ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) {
11574 switch (static_cast<SCEVTypes>(S->getSCEVType())) {
11576 return LoopInvariant;
11580 return getLoopDisposition(cast<SCEVCastExpr>(S)->getOperand(), L);
11581 case scAddRecExpr: {
11582 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
11584 // If L is the addrec's loop, it's computable.
11585 if (AR->getLoop() == L)
11586 return LoopComputable;
11588 // Add recurrences are never invariant in the function-body (null loop).
11590 return LoopVariant;
11592 // Everything that is not defined at loop entry is variant.
11593 if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))
11594 return LoopVariant;
11595 assert(!L->contains(AR->getLoop()) && "Containing loop's header does not"
11596 " dominate the contained loop's header?");
11598 // This recurrence is invariant w.r.t. L if AR's loop contains L.
11599 if (AR->getLoop()->contains(L))
11600 return LoopInvariant;
11602 // This recurrence is variant w.r.t. L if any of its operands
11604 for (auto *Op : AR->operands())
11605 if (!isLoopInvariant(Op, L))
11606 return LoopVariant;
11608 // Otherwise it's loop-invariant.
11609 return LoopInvariant;
11615 bool HasVarying = false;
11616 for (auto *Op : cast<SCEVNAryExpr>(S)->operands()) {
11617 LoopDisposition D = getLoopDisposition(Op, L);
11618 if (D == LoopVariant)
11619 return LoopVariant;
11620 if (D == LoopComputable)
11623 return HasVarying ? LoopComputable : LoopInvariant;
11626 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
11627 LoopDisposition LD = getLoopDisposition(UDiv->getLHS(), L);
11628 if (LD == LoopVariant)
11629 return LoopVariant;
11630 LoopDisposition RD = getLoopDisposition(UDiv->getRHS(), L);
11631 if (RD == LoopVariant)
11632 return LoopVariant;
11633 return (LD == LoopInvariant && RD == LoopInvariant) ?
11634 LoopInvariant : LoopComputable;
11637 // All non-instruction values are loop invariant. All instructions are loop
11638 // invariant if they are not contained in the specified loop.
11639 // Instructions are never considered invariant in the function body
11640 // (null loop) because they are defined within the "loop".
11641 if (auto *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue()))
11642 return (L && !L->contains(I)) ? LoopInvariant : LoopVariant;
11643 return LoopInvariant;
11644 case scCouldNotCompute:
11645 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
11647 llvm_unreachable("Unknown SCEV kind!");
11650 bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) {
11651 return getLoopDisposition(S, L) == LoopInvariant;
11654 bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) {
11655 return getLoopDisposition(S, L) == LoopComputable;
11658 ScalarEvolution::BlockDisposition
11659 ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) {
11660 auto &Values = BlockDispositions[S];
11661 for (auto &V : Values) {
11662 if (V.getPointer() == BB)
11665 Values.emplace_back(BB, DoesNotDominateBlock);
11666 BlockDisposition D = computeBlockDisposition(S, BB);
11667 auto &Values2 = BlockDispositions[S];
11668 for (auto &V : make_range(Values2.rbegin(), Values2.rend())) {
11669 if (V.getPointer() == BB) {
11677 ScalarEvolution::BlockDisposition
11678 ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
11679 switch (static_cast<SCEVTypes>(S->getSCEVType())) {
11681 return ProperlyDominatesBlock;
11685 return getBlockDisposition(cast<SCEVCastExpr>(S)->getOperand(), BB);
11686 case scAddRecExpr: {
11687 // This uses a "dominates" query instead of "properly dominates" query
11688 // to test for proper dominance too, because the instruction which
11689 // produces the addrec's value is a PHI, and a PHI effectively properly
11690 // dominates its entire containing block.
11691 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
11692 if (!DT.dominates(AR->getLoop()->getHeader(), BB))
11693 return DoesNotDominateBlock;
11695 // Fall through into SCEVNAryExpr handling.
11702 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(S);
11703 bool Proper = true;
11704 for (const SCEV *NAryOp : NAry->operands()) {
11705 BlockDisposition D = getBlockDisposition(NAryOp, BB);
11706 if (D == DoesNotDominateBlock)
11707 return DoesNotDominateBlock;
11708 if (D == DominatesBlock)
11711 return Proper ? ProperlyDominatesBlock : DominatesBlock;
11714 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
11715 const SCEV *LHS = UDiv->getLHS(), *RHS = UDiv->getRHS();
11716 BlockDisposition LD = getBlockDisposition(LHS, BB);
11717 if (LD == DoesNotDominateBlock)
11718 return DoesNotDominateBlock;
11719 BlockDisposition RD = getBlockDisposition(RHS, BB);
11720 if (RD == DoesNotDominateBlock)
11721 return DoesNotDominateBlock;
11722 return (LD == ProperlyDominatesBlock && RD == ProperlyDominatesBlock) ?
11723 ProperlyDominatesBlock : DominatesBlock;
11726 if (Instruction *I =
11727 dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) {
11728 if (I->getParent() == BB)
11729 return DominatesBlock;
11730 if (DT.properlyDominates(I->getParent(), BB))
11731 return ProperlyDominatesBlock;
11732 return DoesNotDominateBlock;
11734 return ProperlyDominatesBlock;
11735 case scCouldNotCompute:
11736 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
11738 llvm_unreachable("Unknown SCEV kind!");
11741 bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) {
11742 return getBlockDisposition(S, BB) >= DominatesBlock;
11745 bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) {
11746 return getBlockDisposition(S, BB) == ProperlyDominatesBlock;
11749 bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const {
11750 return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; });
11753 bool ScalarEvolution::ExitLimit::hasOperand(const SCEV *S) const {
11754 auto IsS = [&](const SCEV *X) { return S == X; };
11755 auto ContainsS = [&](const SCEV *X) {
11756 return !isa<SCEVCouldNotCompute>(X) && SCEVExprContains(X, IsS);
11758 return ContainsS(ExactNotTaken) || ContainsS(MaxNotTaken);
11762 ScalarEvolution::forgetMemoizedResults(const SCEV *S) {
11763 ValuesAtScopes.erase(S);
11764 LoopDispositions.erase(S);
11765 BlockDispositions.erase(S);
11766 UnsignedRanges.erase(S);
11767 SignedRanges.erase(S);
11768 ExprValueMap.erase(S);
11769 HasRecMap.erase(S);
11770 MinTrailingZerosCache.erase(S);
11772 for (auto I = PredicatedSCEVRewrites.begin();
11773 I != PredicatedSCEVRewrites.end();) {
11774 std::pair<const SCEV *, const Loop *> Entry = I->first;
11775 if (Entry.first == S)
11776 PredicatedSCEVRewrites.erase(I++);
11781 auto RemoveSCEVFromBackedgeMap =
11782 [S, this](DenseMap<const Loop *, BackedgeTakenInfo> &Map) {
11783 for (auto I = Map.begin(), E = Map.end(); I != E;) {
11784 BackedgeTakenInfo &BEInfo = I->second;
11785 if (BEInfo.hasOperand(S, this)) {
11793 RemoveSCEVFromBackedgeMap(BackedgeTakenCounts);
11794 RemoveSCEVFromBackedgeMap(PredicatedBackedgeTakenCounts);
11798 ScalarEvolution::getUsedLoops(const SCEV *S,
11799 SmallPtrSetImpl<const Loop *> &LoopsUsed) {
11800 struct FindUsedLoops {
11801 FindUsedLoops(SmallPtrSetImpl<const Loop *> &LoopsUsed)
11802 : LoopsUsed(LoopsUsed) {}
11803 SmallPtrSetImpl<const Loop *> &LoopsUsed;
11804 bool follow(const SCEV *S) {
11805 if (auto *AR = dyn_cast<SCEVAddRecExpr>(S))
11806 LoopsUsed.insert(AR->getLoop());
11810 bool isDone() const { return false; }
11813 FindUsedLoops F(LoopsUsed);
11814 SCEVTraversal<FindUsedLoops>(F).visitAll(S);
11817 void ScalarEvolution::addToLoopUseLists(const SCEV *S) {
11818 SmallPtrSet<const Loop *, 8> LoopsUsed;
11819 getUsedLoops(S, LoopsUsed);
11820 for (auto *L : LoopsUsed)
11821 LoopUsers[L].push_back(S);
11824 void ScalarEvolution::verify() const {
11825 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
11826 ScalarEvolution SE2(F, TLI, AC, DT, LI);
11828 SmallVector<Loop *, 8> LoopStack(LI.begin(), LI.end());
11830 // Map's SCEV expressions from one ScalarEvolution "universe" to another.
11831 struct SCEVMapper : public SCEVRewriteVisitor<SCEVMapper> {
11832 SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor<SCEVMapper>(SE) {}
11834 const SCEV *visitConstant(const SCEVConstant *Constant) {
11835 return SE.getConstant(Constant->getAPInt());
11838 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
11839 return SE.getUnknown(Expr->getValue());
11842 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
11843 return SE.getCouldNotCompute();
11847 SCEVMapper SCM(SE2);
11849 while (!LoopStack.empty()) {
11850 auto *L = LoopStack.pop_back_val();
11851 LoopStack.insert(LoopStack.end(), L->begin(), L->end());
11853 auto *CurBECount = SCM.visit(
11854 const_cast<ScalarEvolution *>(this)->getBackedgeTakenCount(L));
11855 auto *NewBECount = SE2.getBackedgeTakenCount(L);
11857 if (CurBECount == SE2.getCouldNotCompute() ||
11858 NewBECount == SE2.getCouldNotCompute()) {
11859 // NB! This situation is legal, but is very suspicious -- whatever pass
11860 // change the loop to make a trip count go from could not compute to
11861 // computable or vice-versa *should have* invalidated SCEV. However, we
11862 // choose not to assert here (for now) since we don't want false
11867 if (containsUndefs(CurBECount) || containsUndefs(NewBECount)) {
11868 // SCEV treats "undef" as an unknown but consistent value (i.e. it does
11869 // not propagate undef aggressively). This means we can (and do) fail
11870 // verification in cases where a transform makes the trip count of a loop
11871 // go from "undef" to "undef+1" (say). The transform is fine, since in
11872 // both cases the loop iterates "undef" times, but SCEV thinks we
11873 // increased the trip count of the loop by 1 incorrectly.
11877 if (SE.getTypeSizeInBits(CurBECount->getType()) >
11878 SE.getTypeSizeInBits(NewBECount->getType()))
11879 NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType());
11880 else if (SE.getTypeSizeInBits(CurBECount->getType()) <
11881 SE.getTypeSizeInBits(NewBECount->getType()))
11882 CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType());
11884 auto *ConstantDelta =
11885 dyn_cast<SCEVConstant>(SE2.getMinusSCEV(CurBECount, NewBECount));
11887 if (ConstantDelta && ConstantDelta->getAPInt() != 0) {
11888 dbgs() << "Trip Count Changed!\n";
11889 dbgs() << "Old: " << *CurBECount << "\n";
11890 dbgs() << "New: " << *NewBECount << "\n";
11891 dbgs() << "Delta: " << *ConstantDelta << "\n";
11897 bool ScalarEvolution::invalidate(
11898 Function &F, const PreservedAnalyses &PA,
11899 FunctionAnalysisManager::Invalidator &Inv) {
11900 // Invalidate the ScalarEvolution object whenever it isn't preserved or one
11901 // of its dependencies is invalidated.
11902 auto PAC = PA.getChecker<ScalarEvolutionAnalysis>();
11903 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
11904 Inv.invalidate<AssumptionAnalysis>(F, PA) ||
11905 Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
11906 Inv.invalidate<LoopAnalysis>(F, PA);
11909 AnalysisKey ScalarEvolutionAnalysis::Key;
11911 ScalarEvolution ScalarEvolutionAnalysis::run(Function &F,
11912 FunctionAnalysisManager &AM) {
11913 return ScalarEvolution(F, AM.getResult<TargetLibraryAnalysis>(F),
11914 AM.getResult<AssumptionAnalysis>(F),
11915 AM.getResult<DominatorTreeAnalysis>(F),
11916 AM.getResult<LoopAnalysis>(F));
11920 ScalarEvolutionPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
11921 AM.getResult<ScalarEvolutionAnalysis>(F).print(OS);
11922 return PreservedAnalyses::all();
11925 INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution",
11926 "Scalar Evolution Analysis", false, true)
11927 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
11928 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
11929 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
11930 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
11931 INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution",
11932 "Scalar Evolution Analysis", false, true)
11934 char ScalarEvolutionWrapperPass::ID = 0;
11936 ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) {
11937 initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry());
11940 bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) {
11941 SE.reset(new ScalarEvolution(
11942 F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(),
11943 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
11944 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
11945 getAnalysis<LoopInfoWrapperPass>().getLoopInfo()));
11949 void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); }
11951 void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const {
11955 void ScalarEvolutionWrapperPass::verifyAnalysis() const {
11962 void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
11963 AU.setPreservesAll();
11964 AU.addRequiredTransitive<AssumptionCacheTracker>();
11965 AU.addRequiredTransitive<LoopInfoWrapperPass>();
11966 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
11967 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
11970 const SCEVPredicate *ScalarEvolution::getEqualPredicate(const SCEV *LHS,
11972 FoldingSetNodeID ID;
11973 assert(LHS->getType() == RHS->getType() &&
11974 "Type mismatch between LHS and RHS");
11975 // Unique this node based on the arguments
11976 ID.AddInteger(SCEVPredicate::P_Equal);
11977 ID.AddPointer(LHS);
11978 ID.AddPointer(RHS);
11979 void *IP = nullptr;
11980 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
11982 SCEVEqualPredicate *Eq = new (SCEVAllocator)
11983 SCEVEqualPredicate(ID.Intern(SCEVAllocator), LHS, RHS);
11984 UniquePreds.InsertNode(Eq, IP);
11988 const SCEVPredicate *ScalarEvolution::getWrapPredicate(
11989 const SCEVAddRecExpr *AR,
11990 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
11991 FoldingSetNodeID ID;
11992 // Unique this node based on the arguments
11993 ID.AddInteger(SCEVPredicate::P_Wrap);
11995 ID.AddInteger(AddedFlags);
11996 void *IP = nullptr;
11997 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
11999 auto *OF = new (SCEVAllocator)
12000 SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags);
12001 UniquePreds.InsertNode(OF, IP);
12007 class SCEVPredicateRewriter : public SCEVRewriteVisitor<SCEVPredicateRewriter> {
12010 /// Rewrites \p S in the context of a loop L and the SCEV predication
12011 /// infrastructure.
12013 /// If \p Pred is non-null, the SCEV expression is rewritten to respect the
12014 /// equivalences present in \p Pred.
12016 /// If \p NewPreds is non-null, rewrite is free to add further predicates to
12017 /// \p NewPreds such that the result will be an AddRecExpr.
12018 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
12019 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds,
12020 SCEVUnionPredicate *Pred) {
12021 SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred);
12022 return Rewriter.visit(S);
12025 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
12027 auto ExprPreds = Pred->getPredicatesForExpr(Expr);
12028 for (auto *Pred : ExprPreds)
12029 if (const auto *IPred = dyn_cast<SCEVEqualPredicate>(Pred))
12030 if (IPred->getLHS() == Expr)
12031 return IPred->getRHS();
12033 return convertToAddRecWithPreds(Expr);
12036 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
12037 const SCEV *Operand = visit(Expr->getOperand());
12038 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
12039 if (AR && AR->getLoop() == L && AR->isAffine()) {
12040 // This couldn't be folded because the operand didn't have the nuw
12041 // flag. Add the nusw flag as an assumption that we could make.
12042 const SCEV *Step = AR->getStepRecurrence(SE);
12043 Type *Ty = Expr->getType();
12044 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW))
12045 return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty),
12046 SE.getSignExtendExpr(Step, Ty), L,
12047 AR->getNoWrapFlags());
12049 return SE.getZeroExtendExpr(Operand, Expr->getType());
12052 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
12053 const SCEV *Operand = visit(Expr->getOperand());
12054 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
12055 if (AR && AR->getLoop() == L && AR->isAffine()) {
12056 // This couldn't be folded because the operand didn't have the nsw
12057 // flag. Add the nssw flag as an assumption that we could make.
12058 const SCEV *Step = AR->getStepRecurrence(SE);
12059 Type *Ty = Expr->getType();
12060 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW))
12061 return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty),
12062 SE.getSignExtendExpr(Step, Ty), L,
12063 AR->getNoWrapFlags());
12065 return SE.getSignExtendExpr(Operand, Expr->getType());
12069 explicit SCEVPredicateRewriter(const Loop *L, ScalarEvolution &SE,
12070 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds,
12071 SCEVUnionPredicate *Pred)
12072 : SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {}
12074 bool addOverflowAssumption(const SCEVPredicate *P) {
12076 // Check if we've already made this assumption.
12077 return Pred && Pred->implies(P);
12079 NewPreds->insert(P);
12083 bool addOverflowAssumption(const SCEVAddRecExpr *AR,
12084 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
12085 auto *A = SE.getWrapPredicate(AR, AddedFlags);
12086 return addOverflowAssumption(A);
12089 // If \p Expr represents a PHINode, we try to see if it can be represented
12090 // as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible
12091 // to add this predicate as a runtime overflow check, we return the AddRec.
12092 // If \p Expr does not meet these conditions (is not a PHI node, or we
12093 // couldn't create an AddRec for it, or couldn't add the predicate), we just
12095 const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) {
12096 if (!isa<PHINode>(Expr->getValue()))
12098 Optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
12099 PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr);
12100 if (!PredicatedRewrite)
12102 for (auto *P : PredicatedRewrite->second){
12103 // Wrap predicates from outer loops are not supported.
12104 if (auto *WP = dyn_cast<const SCEVWrapPredicate>(P)) {
12105 auto *AR = cast<const SCEVAddRecExpr>(WP->getExpr());
12106 if (L != AR->getLoop())
12109 if (!addOverflowAssumption(P))
12112 return PredicatedRewrite->first;
12115 SmallPtrSetImpl<const SCEVPredicate *> *NewPreds;
12116 SCEVUnionPredicate *Pred;
12120 } // end anonymous namespace
12122 const SCEV *ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L,
12123 SCEVUnionPredicate &Preds) {
12124 return SCEVPredicateRewriter::rewrite(S, L, *this, nullptr, &Preds);
12127 const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates(
12128 const SCEV *S, const Loop *L,
12129 SmallPtrSetImpl<const SCEVPredicate *> &Preds) {
12130 SmallPtrSet<const SCEVPredicate *, 4> TransformPreds;
12131 S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds, nullptr);
12132 auto *AddRec = dyn_cast<SCEVAddRecExpr>(S);
12137 // Since the transformation was successful, we can now transfer the SCEV
12139 for (auto *P : TransformPreds)
12145 /// SCEV predicates
12146 SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID,
12147 SCEVPredicateKind Kind)
12148 : FastID(ID), Kind(Kind) {}
12150 SCEVEqualPredicate::SCEVEqualPredicate(const FoldingSetNodeIDRef ID,
12151 const SCEV *LHS, const SCEV *RHS)
12152 : SCEVPredicate(ID, P_Equal), LHS(LHS), RHS(RHS) {
12153 assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match");
12154 assert(LHS != RHS && "LHS and RHS are the same SCEV");
12157 bool SCEVEqualPredicate::implies(const SCEVPredicate *N) const {
12158 const auto *Op = dyn_cast<SCEVEqualPredicate>(N);
12163 return Op->LHS == LHS && Op->RHS == RHS;
12166 bool SCEVEqualPredicate::isAlwaysTrue() const { return false; }
12168 const SCEV *SCEVEqualPredicate::getExpr() const { return LHS; }
12170 void SCEVEqualPredicate::print(raw_ostream &OS, unsigned Depth) const {
12171 OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n";
12174 SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID,
12175 const SCEVAddRecExpr *AR,
12176 IncrementWrapFlags Flags)
12177 : SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {}
12179 const SCEV *SCEVWrapPredicate::getExpr() const { return AR; }
12181 bool SCEVWrapPredicate::implies(const SCEVPredicate *N) const {
12182 const auto *Op = dyn_cast<SCEVWrapPredicate>(N);
12184 return Op && Op->AR == AR && setFlags(Flags, Op->Flags) == Flags;
12187 bool SCEVWrapPredicate::isAlwaysTrue() const {
12188 SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags();
12189 IncrementWrapFlags IFlags = Flags;
12191 if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags)
12192 IFlags = clearFlags(IFlags, IncrementNSSW);
12194 return IFlags == IncrementAnyWrap;
12197 void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const {
12198 OS.indent(Depth) << *getExpr() << " Added Flags: ";
12199 if (SCEVWrapPredicate::IncrementNUSW & getFlags())
12201 if (SCEVWrapPredicate::IncrementNSSW & getFlags())
12206 SCEVWrapPredicate::IncrementWrapFlags
12207 SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR,
12208 ScalarEvolution &SE) {
12209 IncrementWrapFlags ImpliedFlags = IncrementAnyWrap;
12210 SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags();
12212 // We can safely transfer the NSW flag as NSSW.
12213 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags)
12214 ImpliedFlags = IncrementNSSW;
12216 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) {
12217 // If the increment is positive, the SCEV NUW flag will also imply the
12218 // WrapPredicate NUSW flag.
12219 if (const auto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE)))
12220 if (Step->getValue()->getValue().isNonNegative())
12221 ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW);
12224 return ImpliedFlags;
12227 /// Union predicates don't get cached so create a dummy set ID for it.
12228 SCEVUnionPredicate::SCEVUnionPredicate()
12229 : SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) {}
12231 bool SCEVUnionPredicate::isAlwaysTrue() const {
12232 return all_of(Preds,
12233 [](const SCEVPredicate *I) { return I->isAlwaysTrue(); });
12236 ArrayRef<const SCEVPredicate *>
12237 SCEVUnionPredicate::getPredicatesForExpr(const SCEV *Expr) {
12238 auto I = SCEVToPreds.find(Expr);
12239 if (I == SCEVToPreds.end())
12240 return ArrayRef<const SCEVPredicate *>();
12244 bool SCEVUnionPredicate::implies(const SCEVPredicate *N) const {
12245 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N))
12246 return all_of(Set->Preds,
12247 [this](const SCEVPredicate *I) { return this->implies(I); });
12249 auto ScevPredsIt = SCEVToPreds.find(N->getExpr());
12250 if (ScevPredsIt == SCEVToPreds.end())
12252 auto &SCEVPreds = ScevPredsIt->second;
12254 return any_of(SCEVPreds,
12255 [N](const SCEVPredicate *I) { return I->implies(N); });
12258 const SCEV *SCEVUnionPredicate::getExpr() const { return nullptr; }
12260 void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const {
12261 for (auto Pred : Preds)
12262 Pred->print(OS, Depth);
12265 void SCEVUnionPredicate::add(const SCEVPredicate *N) {
12266 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N)) {
12267 for (auto Pred : Set->Preds)
12275 const SCEV *Key = N->getExpr();
12276 assert(Key && "Only SCEVUnionPredicate doesn't have an "
12277 " associated expression!");
12279 SCEVToPreds[Key].push_back(N);
12280 Preds.push_back(N);
12283 PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE,
12287 const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) {
12288 const SCEV *Expr = SE.getSCEV(V);
12289 RewriteEntry &Entry = RewriteMap[Expr];
12291 // If we already have an entry and the version matches, return it.
12292 if (Entry.second && Generation == Entry.first)
12293 return Entry.second;
12295 // We found an entry but it's stale. Rewrite the stale entry
12296 // according to the current predicate.
12298 Expr = Entry.second;
12300 const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, Preds);
12301 Entry = {Generation, NewSCEV};
12306 const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() {
12307 if (!BackedgeCount) {
12308 SCEVUnionPredicate BackedgePred;
12309 BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, BackedgePred);
12310 addPredicate(BackedgePred);
12312 return BackedgeCount;
12315 void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) {
12316 if (Preds.implies(&Pred))
12319 updateGeneration();
12322 const SCEVUnionPredicate &PredicatedScalarEvolution::getUnionPredicate() const {
12326 void PredicatedScalarEvolution::updateGeneration() {
12327 // If the generation number wrapped recompute everything.
12328 if (++Generation == 0) {
12329 for (auto &II : RewriteMap) {
12330 const SCEV *Rewritten = II.second.second;
12331 II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, Preds)};
12336 void PredicatedScalarEvolution::setNoOverflow(
12337 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
12338 const SCEV *Expr = getSCEV(V);
12339 const auto *AR = cast<SCEVAddRecExpr>(Expr);
12341 auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE);
12343 // Clear the statically implied flags.
12344 Flags = SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags);
12345 addPredicate(*SE.getWrapPredicate(AR, Flags));
12347 auto II = FlagsMap.insert({V, Flags});
12349 II.first->second = SCEVWrapPredicate::setFlags(Flags, II.first->second);
12352 bool PredicatedScalarEvolution::hasNoOverflow(
12353 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
12354 const SCEV *Expr = getSCEV(V);
12355 const auto *AR = cast<SCEVAddRecExpr>(Expr);
12357 Flags = SCEVWrapPredicate::clearFlags(
12358 Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE));
12360 auto II = FlagsMap.find(V);
12362 if (II != FlagsMap.end())
12363 Flags = SCEVWrapPredicate::clearFlags(Flags, II->second);
12365 return Flags == SCEVWrapPredicate::IncrementAnyWrap;
12368 const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) {
12369 const SCEV *Expr = this->getSCEV(V);
12370 SmallPtrSet<const SCEVPredicate *, 4> NewPreds;
12371 auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds);
12376 for (auto *P : NewPreds)
12379 updateGeneration();
12380 RewriteMap[SE.getSCEV(V)] = {Generation, New};
12384 PredicatedScalarEvolution::PredicatedScalarEvolution(
12385 const PredicatedScalarEvolution &Init)
12386 : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L), Preds(Init.Preds),
12387 Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) {
12388 for (const auto &I : Init.FlagsMap)
12389 FlagsMap.insert(I);
12392 void PredicatedScalarEvolution::print(raw_ostream &OS, unsigned Depth) const {
12394 for (auto *BB : L.getBlocks())
12395 for (auto &I : *BB) {
12396 if (!SE.isSCEVable(I.getType()))
12399 auto *Expr = SE.getSCEV(&I);
12400 auto II = RewriteMap.find(Expr);
12402 if (II == RewriteMap.end())
12405 // Don't print things that are not interesting.
12406 if (II->second.second == Expr)
12409 OS.indent(Depth) << "[PSE]" << I << ":\n";
12410 OS.indent(Depth + 2) << *Expr << "\n";
12411 OS.indent(Depth + 2) << "--> " << *II->second.second << "\n";
12415 // Match the mathematical pattern A - (A / B) * B, where A and B can be
12416 // arbitrary expressions.
12417 // It's not always easy, as A and B can be folded (imagine A is X / 2, and B is
12418 // 4, A / B becomes X / 8).
12419 bool ScalarEvolution::matchURem(const SCEV *Expr, const SCEV *&LHS,
12420 const SCEV *&RHS) {
12421 const auto *Add = dyn_cast<SCEVAddExpr>(Expr);
12422 if (Add == nullptr || Add->getNumOperands() != 2)
12425 const SCEV *A = Add->getOperand(1);
12426 const auto *Mul = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
12428 if (Mul == nullptr)
12431 const auto MatchURemWithDivisor = [&](const SCEV *B) {
12432 // (SomeExpr + (-(SomeExpr / B) * B)).
12433 if (Expr == getURemExpr(A, B)) {
12441 // (SomeExpr + (-1 * (SomeExpr / B) * B)).
12442 if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0)))
12443 return MatchURemWithDivisor(Mul->getOperand(1)) ||
12444 MatchURemWithDivisor(Mul->getOperand(2));
12446 // (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)).
12447 if (Mul->getNumOperands() == 2)
12448 return MatchURemWithDivisor(Mul->getOperand(1)) ||
12449 MatchURemWithDivisor(Mul->getOperand(0)) ||
12450 MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(1))) ||
12451 MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(0)));