LLVM 24.0.0git
ScalarEvolution.cpp
Go to the documentation of this file.
1//===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of the scalar evolution analysis
10// engine, which is used primarily to analyze expressions involving induction
11// variables in loops.
12//
13// There are several aspects to this library. First is the representation of
14// scalar expressions, which are represented as subclasses of the SCEV class.
15// These classes are used to represent certain types of subexpressions that we
16// can handle. We only create one SCEV of a particular shape, so
17// pointer-comparisons for equality are legal.
18//
19// One important aspect of the SCEV objects is that they are never cyclic, even
20// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
21// the PHI node is one of the idioms that we can represent (e.g., a polynomial
22// recurrence) then we represent it directly as a recurrence node, otherwise we
23// represent it as a SCEVUnknown node.
24//
25// In addition to being able to represent expressions of various types, we also
26// have folders that are used to build the *canonical* representation for a
27// particular expression. These folders are capable of using a variety of
28// rewrite rules to simplify the expressions.
29//
30// Once the folders are defined, we can implement the more interesting
31// higher-level code, such as the code that recognizes PHI nodes of various
32// types, computes the execution count of a loop, etc.
33//
34// TODO: We should use these routines and value representations to implement
35// dependence analysis!
36//
37//===----------------------------------------------------------------------===//
38//
39// There are several good references for the techniques used in this analysis.
40//
41// Chains of recurrences -- a method to expedite the evaluation
42// of closed-form functions
43// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
44//
45// On computational properties of chains of recurrences
46// Eugene V. Zima
47//
48// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
49// Robert A. van Engelen
50//
51// Efficient Symbolic Analysis for Optimizing Compilers
52// Robert A. van Engelen
53//
54// Using the chains of recurrences algebra for data dependence testing and
55// induction variable substitution
56// MS Thesis, Johnie Birch
57//
58//===----------------------------------------------------------------------===//
59
61#include "llvm/ADT/APInt.h"
62#include "llvm/ADT/ArrayRef.h"
63#include "llvm/ADT/DenseMap.h"
65#include "llvm/ADT/FoldingSet.h"
66#include "llvm/ADT/STLExtras.h"
67#include "llvm/ADT/ScopeExit.h"
68#include "llvm/ADT/Sequence.h"
71#include "llvm/ADT/Statistic.h"
73#include "llvm/ADT/StringRef.h"
83#include "llvm/Config/llvm-config.h"
84#include "llvm/IR/Argument.h"
85#include "llvm/IR/BasicBlock.h"
86#include "llvm/IR/CFG.h"
87#include "llvm/IR/Constant.h"
89#include "llvm/IR/Constants.h"
90#include "llvm/IR/DataLayout.h"
92#include "llvm/IR/Dominators.h"
93#include "llvm/IR/Function.h"
94#include "llvm/IR/GlobalAlias.h"
95#include "llvm/IR/GlobalValue.h"
97#include "llvm/IR/InstrTypes.h"
98#include "llvm/IR/Instruction.h"
101#include "llvm/IR/Intrinsics.h"
102#include "llvm/IR/LLVMContext.h"
103#include "llvm/IR/Operator.h"
104#include "llvm/IR/PatternMatch.h"
105#include "llvm/IR/Type.h"
106#include "llvm/IR/Use.h"
107#include "llvm/IR/User.h"
108#include "llvm/IR/Value.h"
109#include "llvm/IR/Verifier.h"
111#include "llvm/Pass.h"
112#include "llvm/Support/Casting.h"
115#include "llvm/Support/Debug.h"
121#include <algorithm>
122#include <cassert>
123#include <climits>
124#include <cstdint>
125#include <cstdlib>
126#include <map>
127#include <memory>
128#include <numeric>
129#include <optional>
130#include <tuple>
131#include <utility>
132#include <vector>
133
134using namespace llvm;
135using namespace PatternMatch;
136using namespace SCEVPatternMatch;
137
138#define DEBUG_TYPE "scalar-evolution"
139
140STATISTIC(NumExitCountsComputed,
141 "Number of loop exits with predictable exit counts");
142STATISTIC(NumExitCountsNotComputed,
143 "Number of loop exits without predictable exit counts");
144STATISTIC(NumBruteForceTripCountsComputed,
145 "Number of loops with trip counts computed by force");
146
147#ifdef EXPENSIVE_CHECKS
148bool llvm::VerifySCEV = true;
149#else
150bool llvm::VerifySCEV = false;
151#endif
152
154 MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
155 cl::desc("Maximum number of iterations SCEV will "
156 "symbolically execute a constant "
157 "derived loop"),
158 cl::init(100));
159
161 "verify-scev", cl::Hidden, cl::location(VerifySCEV),
162 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
164 "verify-scev-strict", cl::Hidden,
165 cl::desc("Enable stricter verification with -verify-scev is passed"));
166
168 "scev-verify-ir", cl::Hidden,
169 cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"),
170 cl::init(false));
171
173 "scev-mulops-inline-threshold", cl::Hidden,
174 cl::desc("Threshold for inlining multiplication operands into a SCEV"),
175 cl::init(32));
176
178 "scev-addops-inline-threshold", cl::Hidden,
179 cl::desc("Threshold for inlining addition operands into a SCEV"),
180 cl::init(500));
181
183 "scalar-evolution-max-scev-compare-depth", cl::Hidden,
184 cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
185 cl::init(32));
186
188 "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden,
189 cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
190 cl::init(2));
191
193 "scalar-evolution-max-value-compare-depth", cl::Hidden,
194 cl::desc("Maximum depth of recursive value complexity comparisons"),
195 cl::init(2));
196
198 MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden,
199 cl::desc("Maximum depth of recursive arithmetics"),
200 cl::init(32));
201
203 "scalar-evolution-max-constant-evolving-depth", cl::Hidden,
204 cl::desc("Maximum depth of recursive constant evolving"), cl::init(32));
205
207 MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden,
208 cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"),
209 cl::init(8));
210
212 MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
213 cl::desc("Max coefficients in AddRec during evolving"),
214 cl::init(8));
215
217 HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden,
218 cl::desc("Size of the expression which is considered huge"),
219 cl::init(4096));
220
222 "scev-range-iter-threshold", cl::Hidden,
223 cl::desc("Threshold for switching to iteratively computing SCEV ranges"),
224 cl::init(32));
225
227 "scalar-evolution-max-loop-guard-collection-depth", cl::Hidden,
228 cl::desc("Maximum depth for recursive loop guard collection"), cl::init(1));
229
230static cl::opt<bool>
231ClassifyExpressions("scalar-evolution-classify-expressions",
232 cl::Hidden, cl::init(true),
233 cl::desc("When printing analysis, include information on every instruction"));
234
236 "scalar-evolution-use-expensive-range-sharpening", cl::Hidden,
237 cl::init(false),
238 cl::desc("Use more powerful methods of sharpening expression ranges. May "
239 "be costly in terms of compile time"));
240
241static cl::opt<bool>
242 EnableFiniteLoopControl("scalar-evolution-finite-loop", cl::Hidden,
243 cl::desc("Handle <= and >= in finite loops"),
244 cl::init(true));
245
247 "scalar-evolution-use-context-for-no-wrap-flag-strenghening", cl::Hidden,
248 cl::desc("Infer nuw/nsw flags using context where suitable"),
249 cl::init(true));
250
251//===----------------------------------------------------------------------===//
252// SCEV class definitions
253//===----------------------------------------------------------------------===//
254
256 // Leaf nodes are always their own canonical.
257 switch (getSCEVType()) {
258 case scConstant:
259 case scVScale:
260 case scUnknown:
261 CanonicalSCEV = this;
262 return;
263 default:
264 break;
265 }
266
267 // For all other expressions, check whether any immediate operand has a
268 // different canonical. Since operands are always created before their parent,
269 // their canonical pointers are already set — no recursion needed.
270 bool Changed = false;
272 for (SCEVUse Op : operands()) {
273 CanonOps.push_back(Op->getCanonical());
274 Changed |= CanonOps.back() != Op;
275 }
276
277 if (!Changed) {
278 CanonicalSCEV = this;
279 return;
280 }
281
282 // Rebuild the expression from the canonical operands, stripping use flags.
283 CanonicalSCEV = SE.getWithOperands(this, CanonOps);
284}
285
286//===----------------------------------------------------------------------===//
287// Implementation of the SCEV class.
288//
289
290#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
292 print(dbgs());
293 dbgs() << '\n';
294}
295#endif
296
297void SCEV::print(raw_ostream &OS) const {
298 switch (getSCEVType()) {
299 case scConstant:
300 cast<SCEVConstant>(this)->getValue()->printAsOperand(OS, false);
301 return;
302 case scVScale:
303 OS << "vscale";
304 return;
305 case scPtrToAddr: {
306 const SCEVCastExpr *PtrCast = cast<SCEVCastExpr>(this);
307 SCEVUse Op = PtrCast->getOperand();
308 OS << "(ptrtoaddr " << *Op->getType() << " " << Op << " to "
309 << *PtrCast->getType() << ")";
310 return;
311 }
312 case scTruncate: {
313 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
314 SCEVUse Op = Trunc->getOperand();
315 OS << "(trunc " << *Op->getType() << " " << Op << " to "
316 << *Trunc->getType() << ")";
317 return;
318 }
319 case scZeroExtend: {
321 SCEVUse Op = ZExt->getOperand();
322 OS << "(zext " << *Op->getType() << " " << Op << " to " << *ZExt->getType()
323 << ")";
324 return;
325 }
326 case scSignExtend: {
328 SCEVUse Op = SExt->getOperand();
329 OS << "(sext " << *Op->getType() << " " << Op << " to " << *SExt->getType()
330 << ")";
331 return;
332 }
333 case scAddRecExpr: {
334 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
335 OS << "{" << AR->getOperand(0);
336 for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
337 OS << ",+," << AR->getOperand(i);
338 OS << "}<";
339 if (AR->hasNoUnsignedWrap())
340 OS << "nuw><";
341 if (AR->hasNoSignedWrap())
342 OS << "nsw><";
343 if (AR->hasNoSelfWrap() && !AR->hasNoUnsignedWrap() &&
344 !AR->hasNoSignedWrap())
345 OS << "nw><";
346 AR->getLoop()->getHeader()->printAsOperand(OS, /*PrintType=*/false);
347 OS << ">";
348 return;
349 }
350 case scAddExpr:
351 case scMulExpr:
352 case scUMaxExpr:
353 case scSMaxExpr:
354 case scUMinExpr:
355 case scSMinExpr:
357 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
358 const char *OpStr = nullptr;
359 switch (NAry->getSCEVType()) {
360 case scAddExpr: OpStr = " + "; break;
361 case scMulExpr: OpStr = " * "; break;
362 case scUMaxExpr: OpStr = " umax "; break;
363 case scSMaxExpr: OpStr = " smax "; break;
364 case scUMinExpr:
365 OpStr = " umin ";
366 break;
367 case scSMinExpr:
368 OpStr = " smin ";
369 break;
371 OpStr = " umin_seq ";
372 break;
373 default:
374 llvm_unreachable("There are no other nary expression types.");
375 }
376 OS << "(" << llvm::interleaved(NAry->operands(), OpStr) << ")";
377 switch (NAry->getSCEVType()) {
378 case scAddExpr:
379 case scMulExpr:
380 if (NAry->hasNoUnsignedWrap())
381 OS << "<nuw>";
382 if (NAry->hasNoSignedWrap())
383 OS << "<nsw>";
384 break;
385 default:
386 // Nothing to print for other nary expressions.
387 break;
388 }
389 return;
390 }
391 case scUDivExpr: {
392 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
393 OS << "(" << UDiv->getLHS() << " /u " << UDiv->getRHS() << ")";
394 return;
395 }
396 case scUnknown:
397 cast<SCEVUnknown>(this)->getValue()->printAsOperand(OS, false);
398 return;
400 OS << "***COULDNOTCOMPUTE***";
401 return;
402 }
403 llvm_unreachable("Unknown SCEV kind!");
404}
405
407 switch (getSCEVType()) {
408 case scConstant:
409 case scVScale:
410 case scUnknown:
411 return {};
412 case scPtrToAddr:
413 case scTruncate:
414 case scZeroExtend:
415 case scSignExtend:
416 return cast<SCEVCastExpr>(this)->operands();
417 case scAddRecExpr:
418 case scAddExpr:
419 case scMulExpr:
420 case scUMaxExpr:
421 case scSMaxExpr:
422 case scUMinExpr:
423 case scSMinExpr:
425 return cast<SCEVNAryExpr>(this)->operands();
426 case scUDivExpr:
427 return cast<SCEVUDivExpr>(this)->operands();
429 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
430 }
431 llvm_unreachable("Unknown SCEV kind!");
432}
433
434bool SCEV::isZero() const { return match(this, m_scev_Zero()); }
435
436bool SCEV::isOne() const { return match(this, m_scev_One()); }
437
438bool SCEV::isAllOnesValue() const { return match(this, m_scev_AllOnes()); }
439
442 if (!Mul) return false;
443
444 // If there is a constant factor, it will be first.
445 const SCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
446 if (!SC) return false;
447
448 // Return true if the value is negative, this matches things like (-42 * V).
449 return SC->getAPInt().isNegative();
450}
451
454
456 return S->getSCEVType() == scCouldNotCompute;
457}
458
460 auto &Entry = ConstantSCEVs[V];
461 if (Entry)
462 return Entry;
463
466 ID.AddPointer(V);
468 if (SCEVConstant *S =
469 static_cast<SCEVConstant *>(UniqueSCEVs.lookup(ID, Token)))
470 return Entry = S;
471 SCEVConstant *S =
472 new (SCEVAllocator) SCEVConstant(ID.Intern(SCEVAllocator), V);
473 UniqueSCEVs.insert(S, Token);
474 S->computeAndSetCanonical(*this);
475 return Entry = S;
476}
477
479 return getConstant(ConstantInt::get(getContext(), Val));
480}
481
482const SCEV *
485 // TODO: Avoid implicit trunc?
486 // See https://github.com/llvm/llvm-project/issues/112510.
487 return getConstant(
488 ConstantInt::get(ITy, V, isSigned, /*ImplicitTrunc=*/true));
489}
490
494 ID.AddPointer(Ty);
496 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
497 return S;
498 SCEV *S = new (SCEVAllocator) SCEVVScale(ID.Intern(SCEVAllocator), Ty);
499 UniqueSCEVs.insert(S, Token);
500 S->computeAndSetCanonical(*this);
501 return S;
502}
503
505 SCEV::NoWrapFlags Flags) {
506 const SCEV *Res = getConstant(Ty, EC.getKnownMinValue());
507 if (EC.isScalable())
508 Res = getMulExpr(Res, getVScale(Ty), Flags);
509 return Res;
510}
511
513 SCEVUse op, Type *ty)
514 : SCEV(ID, SCEVTy, computeExpressionSize(op), ty), Op(op) {}
515
516SCEVPtrToAddrExpr::SCEVPtrToAddrExpr(const FoldingSetNodeIDRef ID,
517 const SCEV *Op, Type *ITy)
518 : SCEVCastExpr(ID, scPtrToAddr, Op, ITy) {
519 assert(getOperand()->getType()->isPointerTy() && getType()->isIntegerTy() &&
520 "Must be a non-bit-width-changing pointer-to-integer cast!");
521}
522
527
528SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, SCEVUse op,
529 Type *ty)
531 assert(getOperand()->getType()->isIntOrPtrTy() && getType()->isIntOrPtrTy() &&
532 "Cannot truncate non-integer value!");
533}
534
535SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, SCEVUse op,
536 Type *ty)
538 assert(getOperand()->getType()->isIntOrPtrTy() && getType()->isIntOrPtrTy() &&
539 "Cannot zero extend non-integer value!");
540}
541
542SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, SCEVUse op,
543 Type *ty)
545 assert(getOperand()->getType()->isIntOrPtrTy() && getType()->isIntOrPtrTy() &&
546 "Cannot sign extend non-integer value!");
547}
548
550 // Clear this SCEVUnknown from various maps.
551 SE->forgetMemoizedResults({this});
552
553 // Remove this SCEVUnknown from the uniquing map.
554 SE->UniqueSCEVs.erase(this);
555
556 // Release the value.
557 setValPtr(nullptr);
558}
559
560void SCEVUnknown::allUsesReplacedWith(Value *New) {
561 // Clear this SCEVUnknown from various maps.
562 SE->forgetMemoizedResults({this});
563
564 // Remove this SCEVUnknown from the uniquing map.
565 SE->UniqueSCEVs.erase(this);
566
567 // Replace the value pointer in case someone is still using this SCEVUnknown.
568 setValPtr(New);
569}
570
571//===----------------------------------------------------------------------===//
572// SCEV Utilities
573//===----------------------------------------------------------------------===//
574
575/// Compare the two values \p LV and \p RV in terms of their "complexity" where
576/// "complexity" is a partial (and somewhat ad-hoc) relation used to order
577/// operands in SCEV expressions.
578static int CompareValueComplexity(const LoopInfo *const LI, Value *LV,
579 Value *RV, unsigned Depth) {
581 return 0;
582
583 // Order pointer values after integer values. This helps SCEVExpander form
584 // GEPs.
585 bool LIsPointer = LV->getType()->isPointerTy(),
586 RIsPointer = RV->getType()->isPointerTy();
587 if (LIsPointer != RIsPointer)
588 return (int)LIsPointer - (int)RIsPointer;
589
590 // Compare getValueID values.
591 unsigned LID = LV->getValueID(), RID = RV->getValueID();
592 if (LID != RID)
593 return (int)LID - (int)RID;
594
595 // Sort arguments by their position.
596 if (const auto *LA = dyn_cast<Argument>(LV)) {
597 const auto *RA = cast<Argument>(RV);
598 unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
599 return (int)LArgNo - (int)RArgNo;
600 }
601
602 if (const auto *LGV = dyn_cast<GlobalValue>(LV)) {
603 const auto *RGV = cast<GlobalValue>(RV);
604
605 if (auto L = LGV->getLinkage() - RGV->getLinkage())
606 return L;
607
608 const auto IsGVNameSemantic = [&](const GlobalValue *GV) {
609 auto LT = GV->getLinkage();
610 return !(GlobalValue::isPrivateLinkage(LT) ||
612 };
613
614 // Use the names to distinguish the two values, but only if the
615 // names are semantically important.
616 if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
617 return LGV->getName().compare(RGV->getName());
618 }
619
620 // For instructions, compare their loop depth, and their operand count. This
621 // is pretty loose.
622 if (const auto *LInst = dyn_cast<Instruction>(LV)) {
623 const auto *RInst = cast<Instruction>(RV);
624
625 // Compare loop depths.
626 const BasicBlock *LParent = LInst->getParent(),
627 *RParent = RInst->getParent();
628 if (LParent != RParent) {
629 unsigned LDepth = LI->getLoopDepth(LParent),
630 RDepth = LI->getLoopDepth(RParent);
631 if (LDepth != RDepth)
632 return (int)LDepth - (int)RDepth;
633 }
634
635 // Compare the number of operands.
636 unsigned LNumOps = LInst->getNumOperands(),
637 RNumOps = RInst->getNumOperands();
638 if (LNumOps != RNumOps)
639 return (int)LNumOps - (int)RNumOps;
640
641 for (unsigned Idx : seq(LNumOps)) {
642 int Result = CompareValueComplexity(LI, LInst->getOperand(Idx),
643 RInst->getOperand(Idx), Depth + 1);
644 if (Result != 0)
645 return Result;
646 }
647 }
648
649 return 0;
650}
651
652// Return negative, zero, or positive, if LHS is less than, equal to, or greater
653// than RHS, respectively. A three-way result allows recursive comparisons to be
654// more efficient.
655// If the max analysis depth was reached, return std::nullopt, assuming we do
656// not know if they are equivalent for sure.
657static std::optional<int>
658CompareSCEVComplexity(const LoopInfo *const LI, const SCEV *LHS,
659 const SCEV *RHS, DominatorTree &DT, unsigned Depth = 0) {
660 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
661 if (LHS == RHS)
662 return 0;
663
664 // Primarily, sort the SCEVs by their getSCEVType().
665 SCEVTypes LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
666 if (LType != RType)
667 return (int)LType - (int)RType;
668
670 return std::nullopt;
671
672 // Aside from the getSCEVType() ordering, the particular ordering
673 // isn't very important except that it's beneficial to be consistent,
674 // so that (a + b) and (b + a) don't end up as different expressions.
675 switch (LType) {
676 case scUnknown: {
677 const SCEVUnknown *LU = cast<SCEVUnknown>(LHS);
678 const SCEVUnknown *RU = cast<SCEVUnknown>(RHS);
679
680 int X =
681 CompareValueComplexity(LI, LU->getValue(), RU->getValue(), Depth + 1);
682 return X;
683 }
684
685 case scConstant: {
688
689 // Compare constant values.
690 const APInt &LA = LC->getAPInt();
691 const APInt &RA = RC->getAPInt();
692 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
693 if (LBitWidth != RBitWidth)
694 return (int)LBitWidth - (int)RBitWidth;
695 return LA.ult(RA) ? -1 : 1;
696 }
697
698 case scVScale: {
699 const auto *LTy = cast<IntegerType>(cast<SCEVVScale>(LHS)->getType());
700 const auto *RTy = cast<IntegerType>(cast<SCEVVScale>(RHS)->getType());
701 return LTy->getBitWidth() - RTy->getBitWidth();
702 }
703
704 case scAddRecExpr: {
707
708 // There is always a dominance between two recs that are used by one SCEV,
709 // so we can safely sort recs by loop header dominance. We require such
710 // order in getAddExpr.
711 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
712 if (LLoop != RLoop) {
713 const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
714 assert(LHead != RHead && "Two loops share the same header?");
715 if (DT.dominates(LHead, RHead))
716 return 1;
717 assert(DT.dominates(RHead, LHead) &&
718 "No dominance between recurrences used by one SCEV?");
719 return -1;
720 }
721
722 [[fallthrough]];
723 }
724
725 case scTruncate:
726 case scZeroExtend:
727 case scSignExtend:
728 case scPtrToAddr:
729 case scAddExpr:
730 case scMulExpr:
731 case scUDivExpr:
732 case scSMaxExpr:
733 case scUMaxExpr:
734 case scSMinExpr:
735 case scUMinExpr:
737 ArrayRef<SCEVUse> LOps = LHS->operands();
738 ArrayRef<SCEVUse> ROps = RHS->operands();
739
740 // Lexicographically compare n-ary-like expressions.
741 unsigned LNumOps = LOps.size(), RNumOps = ROps.size();
742 if (LNumOps != RNumOps)
743 return (int)LNumOps - (int)RNumOps;
744
745 for (unsigned i = 0; i != LNumOps; ++i) {
746 auto X = CompareSCEVComplexity(LI, LOps[i].getPointer(),
747 ROps[i].getPointer(), DT, Depth + 1);
748 if (X != 0)
749 return X;
750 }
751 return 0;
752 }
753
755 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
756 }
757 llvm_unreachable("Unknown SCEV kind!");
758}
759
760/// Given a list of SCEV objects, order them by their complexity, and group
761/// objects of the same complexity together by value. When this routine is
762/// finished, we know that any duplicates in the vector are consecutive and that
763/// complexity is monotonically increasing.
764///
765/// Note that we go take special precautions to ensure that we get deterministic
766/// results from this routine. In other words, we don't want the results of
767/// this to depend on where the addresses of various SCEV objects happened to
768/// land in memory.
770 DominatorTree &DT) {
771 if (Ops.size() < 2) return; // Noop
772
773 // Whether LHS has provably less complexity than RHS.
774 auto IsLessComplex = [&](SCEVUse LHS, SCEVUse RHS) {
775 auto Complexity = CompareSCEVComplexity(LI, LHS, RHS, DT);
776 return Complexity && *Complexity < 0;
777 };
778 if (Ops.size() == 2) {
779 // This is the common case, which also happens to be trivially simple.
780 // Special case it.
781 SCEVUse &LHS = Ops[0], &RHS = Ops[1];
782 if (IsLessComplex(RHS, LHS))
783 std::swap(LHS, RHS);
784 return;
785 }
786
787 // Do the rough sort by complexity.
789 Ops, [&](SCEVUse LHS, SCEVUse RHS) { return IsLessComplex(LHS, RHS); });
790
791 // Now that we are sorted by complexity, group elements of the same
792 // complexity. Note that this is, at worst, N^2, but the vector is likely to
793 // be extremely short in practice. Note that we take this approach because we
794 // do not want to depend on the addresses of the objects we are grouping.
795 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
796 const SCEV *S = Ops[i];
797 unsigned Complexity = S->getSCEVType();
798
799 // If there are any objects of the same complexity and same value as this
800 // one, group them.
801 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
802 if (Ops[j] == S) { // Found a duplicate.
803 // Move it to immediately after i'th element.
804 std::swap(Ops[i+1], Ops[j]);
805 ++i; // no need to rescan it.
806 if (i == e-2) return; // Done!
807 }
808 }
809 }
810}
811
812/// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at
813/// least HugeExprThreshold nodes).
815 return any_of(Ops, [](const SCEV *S) {
817 });
818}
819
820/// Performs a number of common optimizations on the passed \p Ops. If the
821/// whole expression reduces down to a single operand, it will be returned.
822///
823/// The following optimizations are performed:
824/// * Fold constants using the \p Fold function.
825/// * Remove identity constants satisfying \p IsIdentity.
826/// * If a constant satisfies \p IsAbsorber, return it.
827/// * Sort operands by complexity.
828template <typename FoldT, typename IsIdentityT, typename IsAbsorberT>
829static const SCEV *
831 SmallVectorImpl<SCEVUse> &Ops, FoldT Fold,
832 IsIdentityT IsIdentity, IsAbsorberT IsAbsorber) {
833 const SCEVConstant *Folded = nullptr;
834 for (unsigned Idx = 0; Idx < Ops.size();) {
835 const SCEV *Op = Ops[Idx];
836 if (const auto *C = dyn_cast<SCEVConstant>(Op)) {
837 if (!Folded)
838 Folded = C;
839 else
840 Folded = cast<SCEVConstant>(
841 SE.getConstant(Fold(Folded->getAPInt(), C->getAPInt())));
842 Ops.erase(Ops.begin() + Idx);
843 continue;
844 }
845 ++Idx;
846 }
847
848 if (Ops.empty()) {
849 assert(Folded && "Must have folded value");
850 return Folded;
851 }
852
853 if (Folded && IsAbsorber(Folded->getAPInt()))
854 return Folded;
855
856 GroupByComplexity(Ops, &LI, DT);
857 if (Folded && !IsIdentity(Folded->getAPInt()))
858 Ops.insert(Ops.begin(), Folded);
859
860 return Ops.size() == 1 ? Ops[0] : nullptr;
861}
862
863//===----------------------------------------------------------------------===//
864// Simple SCEV method implementations
865//===----------------------------------------------------------------------===//
866
867/// Compute BC(It, K). The result has width W. Assume, K > 0.
868static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
869 ScalarEvolution &SE,
870 Type *ResultTy) {
871 // Handle the simplest case efficiently.
872 if (K == 1)
873 return SE.getTruncateOrZeroExtend(It, ResultTy);
874
875 // We are using the following formula for BC(It, K):
876 //
877 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
878 //
879 // Suppose, W is the bitwidth of the return value. We must be prepared for
880 // overflow. Hence, we must assure that the result of our computation is
881 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
882 // safe in modular arithmetic.
883 //
884 // However, this code doesn't use exactly that formula; the formula it uses
885 // is something like the following, where T is the number of factors of 2 in
886 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
887 // exponentiation:
888 //
889 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
890 //
891 // This formula is trivially equivalent to the previous formula. However,
892 // this formula can be implemented much more efficiently. The trick is that
893 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
894 // arithmetic. To do exact division in modular arithmetic, all we have
895 // to do is multiply by the inverse. Therefore, this step can be done at
896 // width W.
897 //
898 // The next issue is how to safely do the division by 2^T. The way this
899 // is done is by doing the multiplication step at a width of at least W + T
900 // bits. This way, the bottom W+T bits of the product are accurate. Then,
901 // when we perform the division by 2^T (which is equivalent to a right shift
902 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
903 // truncated out after the division by 2^T.
904 //
905 // In comparison to just directly using the first formula, this technique
906 // is much more efficient; using the first formula requires W * K bits,
907 // but this formula less than W + K bits. Also, the first formula requires
908 // a division step, whereas this formula only requires multiplies and shifts.
909 //
910 // It doesn't matter whether the subtraction step is done in the calculation
911 // width or the input iteration count's width; if the subtraction overflows,
912 // the result must be zero anyway. We prefer here to do it in the width of
913 // the induction variable because it helps a lot for certain cases; CodeGen
914 // isn't smart enough to ignore the overflow, which leads to much less
915 // efficient code if the width of the subtraction is wider than the native
916 // register width.
917 //
918 // (It's possible to not widen at all by pulling out factors of 2 before
919 // the multiplication; for example, K=2 can be calculated as
920 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
921 // extra arithmetic, so it's not an obvious win, and it gets
922 // much more complicated for K > 3.)
923
924 // Protection from insane SCEVs; this bound is conservative,
925 // but it probably doesn't matter.
926 if (K > 1000)
927 return SE.getCouldNotCompute();
928
929 unsigned W = SE.getTypeSizeInBits(ResultTy);
930
931 // Calculate K! / 2^T and T; we divide out the factors of two before
932 // multiplying for calculating K! / 2^T to avoid overflow.
933 // Other overflow doesn't matter because we only care about the bottom
934 // W bits of the result.
935 APInt OddFactorial(W, 1);
936 unsigned T = 1;
937 for (unsigned i = 3; i <= K; ++i) {
938 unsigned TwoFactors = countr_zero(i);
939 T += TwoFactors;
940 OddFactorial *= (i >> TwoFactors);
941 }
942
943 // We need at least W + T bits for the multiplication step
944 unsigned CalculationBits = W + T;
945
946 // Calculate 2^T, at width T+W.
947 APInt DivFactor = APInt::getOneBitSet(CalculationBits, T);
948
949 // Calculate the multiplicative inverse of K! / 2^T;
950 // this multiplication factor will perform the exact division by
951 // K! / 2^T.
952 APInt MultiplyFactor = OddFactorial.multiplicativeInverse();
953
954 // Calculate the product, at width T+W
955 IntegerType *CalculationTy = IntegerType::get(SE.getContext(),
956 CalculationBits);
957 const SCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
958 for (unsigned i = 1; i != K; ++i) {
959 const SCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
960 Dividend = SE.getMulExpr(Dividend,
961 SE.getTruncateOrZeroExtend(S, CalculationTy));
962 }
963
964 // Divide by 2^T
965 const SCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
966
967 // Truncate the result, and divide by K! / 2^T.
968
969 return SE.getMulExpr(SE.getConstant(MultiplyFactor),
970 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
971}
972
973/// Return the value of this chain of recurrences at the specified iteration
974/// number. We can evaluate this recurrence by multiplying each element in the
975/// chain by the binomial coefficient corresponding to it. In other words, we
976/// can evaluate {A,+,B,+,C,+,D} as:
977///
978/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
979///
980/// where BC(It, k) stands for binomial coefficient.
982 ScalarEvolution &SE) const {
983 return evaluateAtIteration(operands(), It, SE);
984}
985
987 const SCEV *It, ScalarEvolution &SE,
988 SCEV::NoWrapFlags UseFlags) {
989 assert(Operands.size() > 0);
990 assert((Operands.size() == 2 || UseFlags == SCEV::FlagAnyWrap) &&
991 "use-specific flags only supported for affine AddRecs");
992 SCEVUse Result = Operands[0].getPointer();
993 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
994 // The computation is correct in the face of overflow provided that the
995 // multiplication is performed _after_ the evaluation of the binomial
996 // coefficient.
997 const SCEV *Coeff = BinomialCoefficient(It, i, SE, Result->getType());
998 if (isa<SCEVCouldNotCompute>(Coeff))
999 return Coeff;
1000
1001 const SCEV *Mul = SE.getMulExpr(Operands[i].getPointer(), Coeff);
1002 Result = SE.getAddExpr(Result, Mul, {SCEV::FlagAnyWrap, UseFlags});
1003 }
1004 return Result;
1005}
1006
1008 const SCEV *BTC = SE.getBackedgeTakenCount(getLoop());
1009 if (isa<SCEVCouldNotCompute>(BTC))
1010 return BTC;
1011 // The loop reaches iteration BTC, so the value this recurrence computes there
1012 // is the value it had, and that did not wrap.
1013 return evaluateAtIteration(operands(), BTC, SE,
1016}
1017
1018//===----------------------------------------------------------------------===//
1019// SCEV Expression folder implementations
1020//===----------------------------------------------------------------------===//
1021
1022/// The SCEVCastSinkingRewriter takes a scalar evolution expression,
1023/// which computes a pointer-typed value, and rewrites the whole expression
1024/// tree so that *all* the computations are done on integers, and the only
1025/// pointer-typed operands in the expression are SCEVUnknown.
1026/// The CreatePtrCast callback is invoked to create the actual conversion
1027/// (ptrtoint or ptrtoaddr) at the SCEVUnknown leaves.
1029 : public SCEVRewriteVisitor<SCEVCastSinkingRewriter> {
1031 using ConversionFn = function_ref<const SCEV *(const SCEVUnknown *)>;
1032 Type *TargetTy;
1033 ConversionFn CreatePtrCast;
1034
1035public:
1037 ConversionFn CreatePtrCast)
1038 : Base(SE), TargetTy(TargetTy), CreatePtrCast(std::move(CreatePtrCast)) {}
1039
1040 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
1041 Type *TargetTy, ConversionFn CreatePtrCast) {
1042 SCEVCastSinkingRewriter Rewriter(SE, TargetTy, std::move(CreatePtrCast));
1043 return Rewriter.visit(Scev);
1044 }
1045
1046 const SCEV *visit(const SCEV *S) {
1047 Type *STy = S->getType();
1048 // If the expression is not pointer-typed, just keep it as-is.
1049 if (!STy->isPointerTy())
1050 return S;
1051 // Else, recursively sink the cast down into it.
1052 return Base::visit(S);
1053 }
1054
1055 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
1056 // Preserve wrap flags on rewritten SCEVAddExpr, which the default
1057 // implementation drops.
1059 bool Changed = false;
1060 for (SCEVUse Op : Expr->operands()) {
1061 Operands.push_back(visit(Op.getPointer()));
1062 Changed |= Op.getPointer() != Operands.back();
1063 }
1064 return !Changed ? Expr : SE.getAddExpr(Operands, Expr->getNoWrapFlags());
1065 }
1066
1067 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
1068 assert(Expr->getType()->isPointerTy() &&
1069 "Should only reach pointer-typed SCEVUnknown's.");
1070 // Perform some basic constant folding. If the operand of the cast is a
1071 // null pointer, don't create a cast SCEV expression (that will be left
1072 // as-is), but produce a zero constant.
1074 return SE.getZero(TargetTy);
1075 return CreatePtrCast(Expr);
1076 }
1077};
1078
1080 assert(Op->getType()->isPointerTy() && "Op must be a pointer");
1081
1082 // Treat pointers with unstable representation conservatively, since the
1083 // address bits may change.
1084 if (DL.hasUnstableRepresentation(Op->getType()))
1085 return getCouldNotCompute();
1086
1087 Type *Ty = DL.getAddressType(Op->getType());
1088
1089 // Use the rewriter to sink the cast down to SCEVUnknown leaves.
1090 // The rewriter handles null pointer constant folding.
1092 Op, *this, Ty, [this, Ty](const SCEVUnknown *U) {
1095 ID.AddPointer(U);
1096 ID.AddPointer(Ty);
1098 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
1099 return S;
1100 SCEV *S = new (SCEVAllocator)
1101 SCEVPtrToAddrExpr(ID.Intern(SCEVAllocator), U, Ty);
1102 UniqueSCEVs.insert(S, Token);
1103 S->computeAndSetCanonical(*this);
1104 registerUser(S, {U});
1105 return static_cast<const SCEV *>(S);
1106 });
1107 assert(IntOp->getType()->isIntegerTy() &&
1108 "We must have succeeded in sinking the cast, "
1109 "and ending up with an integer-typed expression!");
1110 return IntOp;
1111}
1112
1114 unsigned Depth) {
1115 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
1116 "This is not a truncating conversion!");
1117 assert(isSCEVable(Ty) &&
1118 "This is not a conversion to a SCEVable type!");
1119 assert(!Op->getType()->isPointerTy() && "Can't truncate pointer!");
1120 Ty = getEffectiveSCEVType(Ty);
1121
1124 ID.AddPointer(Op.getOpaqueValue());
1125 ID.AddPointer(Ty);
1127 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
1128 return S;
1129
1130 // Fold if the operand is constant.
1131 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1132 return getConstant(
1133 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
1134
1135 // trunc(trunc(x)) --> trunc(x)
1137 return getTruncateExpr(ST->getOperand(), Ty, Depth + 1);
1138
1139 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
1141 return getTruncateOrSignExtend(SS->getOperand(), Ty, Depth + 1);
1142
1143 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
1145 return getTruncateOrZeroExtend(SZ->getOperand(), Ty, Depth + 1);
1146
1147 if (Depth > MaxCastDepth) {
1148 SCEV *S =
1149 new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator), Op, Ty);
1150 UniqueSCEVs.insert(S, Token);
1151 S->computeAndSetCanonical(*this);
1152 registerUser(S, Op);
1153 return S;
1154 }
1155
1156 // trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and
1157 // trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN),
1158 // if after transforming we have at most one truncate, not counting truncates
1159 // that replace other casts.
1161 auto *CommOp = cast<SCEVCommutativeExpr>(Op);
1163 unsigned numTruncs = 0;
1164 for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2;
1165 ++i) {
1166 const SCEV *S = getTruncateExpr(CommOp->getOperand(i), Ty, Depth + 1);
1167 if (!isa<SCEVIntegralCastExpr>(CommOp->getOperand(i)) &&
1169 numTruncs++;
1170 Operands.push_back(S);
1171 }
1172 if (numTruncs < 2) {
1173 if (isa<SCEVAddExpr>(Op))
1174 return getAddExpr(Operands);
1175 if (isa<SCEVMulExpr>(Op))
1176 return getMulExpr(Operands);
1177 llvm_unreachable("Unexpected SCEV type for Op.");
1178 }
1179 // Although we checked in the beginning that ID is not in the cache, it is
1180 // possible that during recursion and different modification ID was inserted
1181 // into the cache. So if we find it, just return it.
1182 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
1183 return S;
1184 }
1185
1186 // If the input value is a chrec scev, truncate the chrec's operands.
1187 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
1189 for (const SCEV *Op : AddRec->operands())
1190 Operands.push_back(getTruncateExpr(Op, Ty, Depth + 1));
1191 return getAddRecExpr(Operands, AddRec->getLoop(), SCEV::FlagAnyWrap);
1192 }
1193
1194 // Return zero if truncating to known zeros.
1195 uint32_t MinTrailingZeros = getMinTrailingZeros(Op);
1196 if (MinTrailingZeros >= getTypeSizeInBits(Ty))
1197 return getZero(Ty);
1198
1199 // The cast wasn't folded; create an explicit cast node. We can reuse
1200 // the existing insert position since if we get here, we won't have
1201 // made any changes which would invalidate it.
1202 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(SCEVAllocator),
1203 Op, Ty);
1204 UniqueSCEVs.insert(S, Token);
1205 S->computeAndSetCanonical(*this);
1206 registerUser(S, Op);
1207 return S;
1208}
1209
1210// Get the limit of a recurrence such that incrementing by Step cannot cause
1211// signed overflow as long as the value of the recurrence within the
1212// loop does not exceed this limit before incrementing.
1213static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
1214 ICmpInst::Predicate *Pred,
1215 ScalarEvolution *SE) {
1216 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1217 if (SE->isKnownPositive(Step)) {
1218 *Pred = ICmpInst::ICMP_SLT;
1220 SE->getSignedRangeMax(Step));
1221 }
1222 if (SE->isKnownNegative(Step)) {
1223 *Pred = ICmpInst::ICMP_SGT;
1225 SE->getSignedRangeMin(Step));
1226 }
1227 return nullptr;
1228}
1229
1230// Get the limit of a recurrence such that incrementing by Step cannot cause
1231// unsigned overflow as long as the value of the recurrence within the loop does
1232// not exceed this limit before incrementing.
1234 ICmpInst::Predicate *Pred,
1235 ScalarEvolution *SE) {
1236 unsigned BitWidth = SE->getTypeSizeInBits(Step->getType());
1237 *Pred = ICmpInst::ICMP_ULT;
1238
1240 SE->getUnsignedRangeMax(Step));
1241}
1242
1243namespace {
1244
1245struct ExtendOpTraitsBase {
1246 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(SCEVUse, Type *,
1247 unsigned);
1248};
1249
1250// Used to make code generic over signed and unsigned overflow.
1251template <typename ExtendOp> struct ExtendOpTraits {
1252 // Members present:
1253 //
1254 // static const SCEV::NoWrapFlags WrapType;
1255 //
1256 // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1257 //
1258 // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1259 // ICmpInst::Predicate *Pred,
1260 // ScalarEvolution *SE);
1261};
1262
1263template <>
1264struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
1265 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
1266
1267 static const GetExtendExprTy GetExtendExpr;
1268
1269 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1270 ICmpInst::Predicate *Pred,
1271 ScalarEvolution *SE) {
1272 return getSignedOverflowLimitForStep(Step, Pred, SE);
1273 }
1274};
1275
1276const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1278
1279template <>
1280struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
1281 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
1282
1283 static const GetExtendExprTy GetExtendExpr;
1284
1285 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1286 ICmpInst::Predicate *Pred,
1287 ScalarEvolution *SE) {
1288 return getUnsignedOverflowLimitForStep(Step, Pred, SE);
1289 }
1290};
1291
1292const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1294
1295} // end anonymous namespace
1296
1297// The recurrence AR has been shown to have no signed/unsigned wrap or something
1298// close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1299// easily prove NSW/NUW for its preincrement or postincrement sibling. This
1300// allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1301// Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1302// expression "Step + sext/zext(PreIncAR)" is congruent with
1303// "sext/zext(PostIncAR)"
1304template <typename ExtendOpTy>
1306 ScalarEvolution *SE, unsigned Depth) {
1307 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1308 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1309
1310 const Loop *L = AR->getLoop();
1311 const SCEV *Start = AR->getStart();
1312 const SCEV *Step = AR->getStepRecurrence(*SE);
1313
1314 // Check for a simple looking step prior to loop entry.
1315 const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1316 if (!SA)
1317 return nullptr;
1318
1319 // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1320 // subtraction is expensive. For this purpose, perform a quick and dirty
1321 // difference, by checking for Step in the operand list. Note, that
1322 // SA might have repeated ops, like %a + %a + ..., so only remove one.
1323 SmallVector<SCEVUse, 4> DiffOps(SA->operands());
1324 for (auto It = DiffOps.begin(); It != DiffOps.end(); ++It)
1325 if (*It == Step) {
1326 DiffOps.erase(It);
1327 break;
1328 }
1329
1330 if (DiffOps.size() == SA->getNumOperands())
1331 return nullptr;
1332
1333 // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1334 // `Step`:
1335
1336 // 1. NSW/NUW flags on the step increment.
1337 auto PreStartFlags =
1339 const SCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags);
1341 SE->getAddRecExpr(PreStart, Step, L, SCEV::FlagAnyWrap));
1342
1343 // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1344 // "S+X does not sign/unsign-overflow".
1345 //
1346
1347 const SCEV *BECount = SE->getBackedgeTakenCount(L);
1348 if (PreAR && any(PreAR->getNoWrapFlags(WrapType)) &&
1349 !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
1350 return PreStart;
1351
1352 // 2. Direct overflow check on the step operation's expression.
1353 unsigned BitWidth = SE->getTypeSizeInBits(AR->getType());
1354 Type *WideTy = IntegerType::get(SE->getContext(), BitWidth * 2);
1355 const SCEV *OperandExtendedStart =
1356 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth),
1357 (SE->*GetExtendExpr)(Step, WideTy, Depth));
1358 if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) {
1359 if (PreAR && any(AR->getNoWrapFlags(WrapType))) {
1360 // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1361 // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1362 // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact.
1363 SE->setNoWrapFlags(const_cast<SCEVAddRecExpr *>(PreAR), WrapType);
1364 }
1365 return PreStart;
1366 }
1367
1368 // 3. Loop precondition.
1370 const SCEV *OverflowLimit =
1371 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1372
1373 if (OverflowLimit &&
1374 SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit))
1375 return PreStart;
1376
1377 return nullptr;
1378}
1379
1380// Get the normalized zero or sign extended expression for this AddRec's Start.
1381template <typename ExtendOpTy>
1382static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
1383 ScalarEvolution *SE,
1384 unsigned Depth) {
1385 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1386
1387 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, SE, Depth);
1388 if (!PreStart)
1389 return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth);
1390
1391 return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty,
1392 Depth),
1393 (SE->*GetExtendExpr)(PreStart, Ty, Depth));
1394}
1395
1396// Try to prove away overflow by looking at "nearby" add recurrences. A
1397// motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1398// does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1399//
1400// Formally:
1401//
1402// {S,+,X} == {S-T,+,X} + T
1403// => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1404//
1405// If ({S-T,+,X} + T) does not overflow ... (1)
1406//
1407// RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1408//
1409// If {S-T,+,X} does not overflow ... (2)
1410//
1411// RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1412// == {Ext(S-T)+Ext(T),+,Ext(X)}
1413//
1414// If (S-T)+T does not overflow ... (3)
1415//
1416// RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1417// == {Ext(S),+,Ext(X)} == LHS
1418//
1419// Thus, if (1), (2) and (3) are true for some T, then
1420// Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1421//
1422// (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1423// does not overflow" restricted to the 0th iteration. Therefore we only need
1424// to check for (1) and (2).
1425//
1426// In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1427// is `Delta` (defined below).
1428template <typename ExtendOpTy>
1429bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
1430 const SCEV *Step,
1431 const Loop *L) {
1432 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1433
1434 // We restrict `Start` to a constant to prevent SCEV from spending too much
1435 // time here. It is correct (but more expensive) to continue with a
1436 // non-constant `Start` and do a general SCEV subtraction to compute
1437 // `PreStart` below.
1438 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
1439 if (!StartC)
1440 return false;
1441
1442 APInt StartAI = StartC->getAPInt();
1443
1444 for (unsigned Delta : {-2, -1, 1, 2}) {
1445 const SCEV *PreStart = getConstant(StartAI - Delta);
1446
1447 FoldingSetNodeID ID;
1448 ID.AddInteger(scAddRecExpr);
1449 ID.AddPointer(PreStart);
1450 ID.AddPointer(Step);
1451 ID.AddPointer(L);
1452 FoldingSetInsertToken Token;
1453 const auto *PreAR =
1454 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.lookup(ID, Token));
1455
1456 // Give up if we don't already have the add recurrence we need because
1457 // actually constructing an add recurrence is relatively expensive.
1458 if (PreAR && any(PreAR->getNoWrapFlags(WrapType))) { // proves (2)
1459 const SCEV *DeltaS = getConstant(StartC->getType(), Delta);
1461 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1462 DeltaS, &Pred, this);
1463 if (Limit && isKnownPredicate(Pred, PreAR, Limit)) // proves (1)
1464 return true;
1465 }
1466 }
1467
1468 return false;
1469}
1470
1471// Finds an integer D for an expression (C + x + y + ...) such that the top
1472// level addition in (D + (C - D + x + y + ...)) would not wrap (signed or
1473// unsigned) and the number of trailing zeros of (C - D + x + y + ...) is
1474// maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and
1475// the (C + x + y + ...) expression is \p WholeAddExpr.
1477 const SCEVConstant *ConstantTerm,
1478 const SCEVAddExpr *WholeAddExpr) {
1479 const APInt &C = ConstantTerm->getAPInt();
1480 const unsigned BitWidth = C.getBitWidth();
1481 // Find number of trailing zeros of (x + y + ...) w/o the C first:
1482 uint32_t TZ = BitWidth;
1483 for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I)
1484 TZ = std::min(TZ, SE.getMinTrailingZeros(WholeAddExpr->getOperand(I)));
1485 if (TZ) {
1486 // Set D to be as many least significant bits of C as possible while still
1487 // guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap:
1488 return TZ < BitWidth ? C.trunc(TZ).zext(BitWidth) : C;
1489 }
1490 return APInt(BitWidth, 0);
1491}
1492
1493// Finds an integer D for an affine AddRec expression {C,+,x} such that the top
1494// level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the
1495// number of trailing zeros of (C - D + x * n) is maximized, where C is the \p
1496// ConstantStart, x is an arbitrary \p Step, and n is the loop trip count.
1498 const APInt &ConstantStart,
1499 const SCEV *Step) {
1500 const unsigned BitWidth = ConstantStart.getBitWidth();
1501 const uint32_t TZ = SE.getMinTrailingZeros(Step);
1502 if (TZ)
1503 return TZ < BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth)
1504 : ConstantStart;
1505 return APInt(BitWidth, 0);
1506}
1507
1509 const ScalarEvolution::FoldID &ID, const SCEV *S,
1512 &FoldCacheUser) {
1513 auto I = FoldCache.insert({ID, S});
1514 if (!I.second) {
1515 // Remove FoldCacheUser entry for ID when replacing an existing FoldCache
1516 // entry.
1517 auto &UserIDs = FoldCacheUser[I.first->second];
1518 assert(count(UserIDs, ID) == 1 && "unexpected duplicates in UserIDs");
1519 for (unsigned I = 0; I != UserIDs.size(); ++I)
1520 if (UserIDs[I] == ID) {
1521 std::swap(UserIDs[I], UserIDs.back());
1522 break;
1523 }
1524 UserIDs.pop_back();
1525 I.first->second = S;
1526 }
1527 FoldCacheUser[S].push_back(ID);
1528}
1529
1531 unsigned Depth) {
1532 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1533 "This is not an extending conversion!");
1534 assert(isSCEVable(Ty) &&
1535 "This is not a conversion to a SCEVable type!");
1536 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1537 Ty = getEffectiveSCEVType(Ty);
1538
1539 FoldID ID(scZeroExtend, Op, Ty);
1540 if (const SCEV *S = FoldCache.lookup(ID))
1541 return S;
1542
1543 const SCEV *S = getZeroExtendExprImpl(Op, Ty, Depth);
1545 insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser);
1546 return S;
1547}
1548
1550 unsigned Depth) {
1551 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1552 "This is not an extending conversion!");
1553 assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!");
1554 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1555
1556 // Fold if the operand is constant.
1557 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1558 return getConstant(SC->getAPInt().zext(getTypeSizeInBits(Ty)));
1559
1560 // zext(zext(x)) --> zext(x)
1562 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1563
1564 // If the operand is an affine AddRec with the no-unsigned-wrap flag, the
1565 // zero-extension distributes over the recurrence.
1566 const SCEV *Start, *Step;
1567 const Loop *L;
1568 if (Depth <= MaxCastDepth &&
1569 match(Op, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step), m_Loop(L)))) {
1570 const auto *AR = cast<SCEVAddRecExpr>(Op);
1571 if (AR->hasNoUnsignedWrap()) {
1572 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1);
1573 Step = getZeroExtendExpr(Step, Ty, Depth + 1);
1574 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1575 }
1576 }
1577
1578 // Before doing any expensive analysis, check to see if we've already
1579 // computed a SCEV for this Op and Ty.
1582 ID.AddPointer(Op.getOpaqueValue());
1583 ID.AddPointer(Ty);
1585 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
1586 return S;
1587 if (Depth > MaxCastDepth) {
1588 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1589 Op, Ty);
1590 UniqueSCEVs.insert(S, Token);
1591 S->computeAndSetCanonical(*this);
1592 registerUser(S, Op);
1593 return S;
1594 }
1595
1596 // zext(trunc(x)) --> zext(x) or x or trunc(x)
1598 // It's possible the bits taken off by the truncate were all zero bits. If
1599 // so, we should be able to simplify this further.
1600 const SCEV *X = ST->getOperand();
1602 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1603 unsigned NewBits = getTypeSizeInBits(Ty);
1604 if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
1605 CR.zextOrTrunc(NewBits)))
1606 return getTruncateOrZeroExtend(X, Ty, Depth);
1607 }
1608
1609 // If the input value is a chrec scev, and we can prove that the value
1610 // did not overflow the old, smaller, value, we can zero extend all of the
1611 // operands (often constants). This allows analysis of something like
1612 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
1613 if (match(Op, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step), m_Loop(L)))) {
1614 const auto *AR = cast<SCEVAddRecExpr>(Op);
1615 unsigned BitWidth = getTypeSizeInBits(AR->getType());
1616
1617 // The no-unsigned-wrap case is handled before the uniquing lookup above.
1618
1619 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1620 // Note that this serves two purposes: It filters out loops that are
1621 // simply not analyzable, and it covers the case where this code is
1622 // being called from within backedge-taken count analysis, such that
1623 // attempting to ask for the backedge-taken count would likely result
1624 // in infinite recursion. In the later case, the analysis code will
1625 // cope with a conservative value, and it will take care to purge
1626 // that value once it has finished.
1627 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
1628 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1629 // Manually compute the final value for AR, checking for overflow.
1630
1631 // Check whether the backedge-taken count can be losslessly casted to
1632 // the addrec's type. The count is always unsigned.
1633 const SCEV *CastedMaxBECount =
1634 getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth);
1635 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
1636 CastedMaxBECount, MaxBECount->getType(), Depth);
1637 if (MaxBECount == RecastedMaxBECount) {
1638 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
1639 // Check whether Start+Step*MaxBECount has no unsigned overflow.
1640 const SCEV *ZMul =
1641 getMulExpr(CastedMaxBECount, Step, SCEV::FlagAnyWrap, Depth + 1);
1642 const SCEV *ZAdd = getZeroExtendExpr(
1643 getAddExpr(Start, ZMul, SCEV::FlagAnyWrap, Depth + 1), WideTy,
1644 Depth + 1);
1645 const SCEV *WideStart = getZeroExtendExpr(Start, WideTy, Depth + 1);
1646 const SCEV *WideMaxBECount =
1647 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
1648 const SCEV *OperandExtendedAdd =
1649 getAddExpr(WideStart,
1650 getMulExpr(WideMaxBECount,
1651 getZeroExtendExpr(Step, WideTy, Depth + 1),
1654 if (ZAdd == OperandExtendedAdd) {
1655 // Cache knowledge of AR NUW, which is propagated to this AddRec.
1656 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNUW);
1657 // Return the expression with the addrec on the outside.
1658 Start =
1660 Step = getZeroExtendExpr(Step, Ty, Depth + 1);
1661 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1662 }
1663 // Similar to above, only this time treat the step value as signed.
1664 // This covers loops that count down.
1665 OperandExtendedAdd =
1666 getAddExpr(WideStart,
1667 getMulExpr(WideMaxBECount,
1668 getSignExtendExpr(Step, WideTy, Depth + 1),
1671 if (ZAdd == OperandExtendedAdd) {
1672 // Cache knowledge of AR NW, which is propagated to this AddRec.
1673 // Negative step causes unsigned wrap, but it still can't self-wrap.
1674 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW);
1675 // Return the expression with the addrec on the outside.
1676 Start =
1678 Step = getSignExtendExpr(Step, Ty, Depth + 1);
1679 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1680 }
1681 }
1682 }
1683
1684 // Normally, in the cases we can prove no-overflow via a
1685 // backedge guarding condition, we can also compute a backedge
1686 // taken count for the loop. The exceptions are assumptions and
1687 // guards present in the loop -- SCEV is not great at exploiting
1688 // these to compute max backedge taken counts, but can still use
1689 // these to prove lack of overflow. Use this fact to avoid
1690 // doing extra work that may not pay off.
1691 if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
1692 !AC.assumptions().empty()) {
1693
1694 auto NewFlags = proveNoUnsignedWrapViaInduction(AR);
1695 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
1696 if (AR->hasNoUnsignedWrap()) {
1697 // Same as nuw case above - duplicated here to avoid a compile time
1698 // issue. It's not clear that the order of checks does matter, but
1699 // it's one of two issue possible causes for a change which was
1700 // reverted. Be conservative for the moment.
1701 Start =
1703 Step = getZeroExtendExpr(Step, Ty, Depth + 1);
1704 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1705 }
1706
1707 // For a negative step, we can extend the operands iff doing so only
1708 // traverses values in the range zext([0,UINT_MAX]).
1709 if (isKnownNegative(Step)) {
1710 const SCEV *N =
1714 // Cache knowledge of AR NW, which is propagated to this
1715 // AddRec. Negative step causes unsigned wrap, but it
1716 // still can't self-wrap.
1717 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW);
1718 // Return the expression with the addrec on the outside.
1719 Start =
1721 Step = getSignExtendExpr(Step, Ty, Depth + 1);
1722 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1723 }
1724 }
1725 }
1726
1727 // zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step}))<nuw><nsw>
1728 // if D + (C - D + Step * n) could be proven to not unsigned wrap
1729 // where D maximizes the number of trailing zeros of (C - D + Step * n)
1730 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
1731 const APInt &C = SC->getAPInt();
1732 const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
1733 if (D != 0) {
1734 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1735 const SCEV *SResidual =
1736 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
1737 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1738 return getAddExpr(SZExtD, SZExtR, SCEV::FlagNSW | SCEV::FlagNUW,
1739 Depth + 1);
1740 }
1741 }
1742
1743 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1744 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNUW);
1745 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, this, Depth + 1);
1746 Step = getZeroExtendExpr(Step, Ty, Depth + 1);
1747 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1748 }
1749 }
1750
1751 // zext(A % B) --> zext(A) % zext(B)
1752 {
1753 const SCEV *LHS;
1754 const SCEV *RHS;
1755 if (match(Op, m_scev_URem(m_SCEV(LHS), m_SCEV(RHS), *this)))
1756 return getURemExpr(getZeroExtendExpr(LHS, Ty, Depth + 1),
1757 getZeroExtendExpr(RHS, Ty, Depth + 1));
1758 }
1759
1760 // zext(A / B) --> zext(A) / zext(B).
1761 if (auto *Div = dyn_cast<SCEVUDivExpr>(Op))
1762 return getUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty, Depth + 1),
1763 getZeroExtendExpr(Div->getRHS(), Ty, Depth + 1));
1764
1765 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1766 // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
1767 if (SA->hasNoUnsignedWrap()) {
1768 // If the addition does not unsign overflow then we can, by definition,
1769 // commute the zero extension with the addition operation.
1771 for (SCEVUse Op : SA->operands())
1772 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1773 return getAddExpr(Ops, SCEV::FlagNUW, Depth + 1);
1774 }
1775
1776 const APInt *C, *C2;
1777 // zext (C + A)<nsw> -> (sext(C) + sext(A))<nsw> if zext (C + A)<nsw> >=s 0.
1778 // Currently the non-negative check is done manually, as isKnownNonNegative
1779 // is too expensive.
1780 if (SA->hasNoSignedWrap() &&
1782 m_scev_SMax(m_scev_APInt(C2), m_SCEV()))) &&
1783 C->isNegative() && !C->isMinSignedValue() && C2->sge(C->abs())) {
1784 assert(isKnownNonNegative(SA) && "incorrectly determined non-negative");
1785 return getAddExpr(getSignExtendExpr(SA->getOperand(0), Ty, Depth + 1),
1786 getSignExtendExpr(SA->getOperand(1), Ty, Depth + 1),
1787 SCEV::FlagNSW, Depth + 1);
1788 }
1789
1790 // zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...))
1791 // if D + (C - D + x + y + ...) could be proven to not unsigned wrap
1792 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1793 //
1794 // Often address arithmetics contain expressions like
1795 // (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))).
1796 // This transformation is useful while proving that such expressions are
1797 // equal or differ by a small constant amount, see LoadStoreVectorizer pass.
1798 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1799 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1800 if (D != 0) {
1801 const SCEV *SZExtD = getZeroExtendExpr(getConstant(D), Ty, Depth);
1802 const SCEV *SResidual =
1804 const SCEV *SZExtR = getZeroExtendExpr(SResidual, Ty, Depth + 1);
1805 return getAddExpr(SZExtD, SZExtR, (SCEV::FlagNSW | SCEV::FlagNUW),
1806 Depth + 1);
1807 }
1808 }
1809 }
1810
1811 if (auto *SM = dyn_cast<SCEVMulExpr>(Op)) {
1812 // zext((A * B * ...)<nuw>) --> (zext(A) * zext(B) * ...)<nuw>
1813 if (SM->hasNoUnsignedWrap()) {
1814 // If the multiply does not unsign overflow then we can, by definition,
1815 // commute the zero extension with the multiply operation.
1817 for (SCEVUse Op : SM->operands())
1818 Ops.push_back(getZeroExtendExpr(Op, Ty, Depth + 1));
1819 return getMulExpr(Ops, SCEV::FlagNUW, Depth + 1);
1820 }
1821
1822 // zext(2^K * (trunc X to iN)) to iM ->
1823 // 2^K * (zext(trunc X to i{N-K}) to iM)<nuw>
1824 //
1825 // Proof:
1826 //
1827 // zext(2^K * (trunc X to iN)) to iM
1828 // = zext((trunc X to iN) << K) to iM
1829 // = zext((trunc X to i{N-K}) << K)<nuw> to iM
1830 // (because shl removes the top K bits)
1831 // = zext((2^K * (trunc X to i{N-K}))<nuw>) to iM
1832 // = (2^K * (zext(trunc X to i{N-K}) to iM))<nuw>.
1833 //
1834 const APInt *C;
1835 const SCEV *TruncRHS;
1836 if (match(SM,
1837 m_scev_Mul(m_scev_APInt(C), m_scev_Trunc(m_SCEV(TruncRHS)))) &&
1838 C->isPowerOf2()) {
1839 int NewTruncBits =
1840 getTypeSizeInBits(SM->getOperand(1)->getType()) - C->logBase2();
1841 Type *NewTruncTy = IntegerType::get(getContext(), NewTruncBits);
1842 return getMulExpr(
1843 getZeroExtendExpr(SM->getOperand(0), Ty),
1844 getZeroExtendExpr(getTruncateExpr(TruncRHS, NewTruncTy), Ty),
1845 SCEV::FlagNUW, Depth + 1);
1846 }
1847 }
1848
1849 // zext(umin(x, y)) -> umin(zext(x), zext(y))
1850 // zext(umax(x, y)) -> umax(zext(x), zext(y))
1854 for (SCEVUse Operand : MinMax->operands())
1855 Operands.push_back(getZeroExtendExpr(Operand, Ty));
1857 return getUMinExpr(Operands);
1858 return getUMaxExpr(Operands);
1859 }
1860
1861 // zext(umin_seq(x, y)) -> umin_seq(zext(x), zext(y))
1863 assert(isa<SCEVSequentialUMinExpr>(MinMax) && "Not supported!");
1865 for (SCEVUse Operand : MinMax->operands())
1866 Operands.push_back(getZeroExtendExpr(Operand, Ty));
1867 return getUMinExpr(Operands, /*Sequential*/ true);
1868 }
1869
1870 // The cast wasn't folded; create an explicit cast node.
1871 // Recompute the insert position, as it may have been invalidated.
1872 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
1873 return S;
1874 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1875 Op, Ty);
1876 UniqueSCEVs.insert(S, Token);
1877 S->computeAndSetCanonical(*this);
1878 registerUser(S, Op);
1879 return S;
1880}
1881
1883 unsigned Depth) {
1884 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1885 "This is not an extending conversion!");
1886 assert(isSCEVable(Ty) &&
1887 "This is not a conversion to a SCEVable type!");
1888 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1889 Ty = getEffectiveSCEVType(Ty);
1890
1891 FoldID ID(scSignExtend, Op, Ty);
1892 if (const SCEV *S = FoldCache.lookup(ID))
1893 return S;
1894
1895 const SCEV *S = getSignExtendExprImpl(Op, Ty, Depth);
1897 insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser);
1898 return S;
1899}
1900
1902 unsigned Depth) {
1903 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1904 "This is not an extending conversion!");
1905 assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!");
1906 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1907 Ty = getEffectiveSCEVType(Ty);
1908
1909 // Fold if the operand is constant.
1910 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1911 return getConstant(SC->getAPInt().sext(getTypeSizeInBits(Ty)));
1912
1913 // sext(sext(x)) --> sext(x)
1915 return getSignExtendExpr(SS->getOperand(), Ty, Depth + 1);
1916
1917 // sext(zext(x)) --> zext(x)
1919 return getZeroExtendExpr(SZ->getOperand(), Ty, Depth + 1);
1920
1921 // If the operand is an affine AddRec with the no-signed-wrap flag, the
1922 // sign-extension distributes over the recurrence.
1923 const SCEV *Start, *Step;
1924 const Loop *L;
1925 if (Depth <= MaxCastDepth &&
1926 match(Op, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step), m_Loop(L)))) {
1927 const auto *AR = cast<SCEVAddRecExpr>(Op);
1928 if (AR->hasNoSignedWrap()) {
1929 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1);
1930 Step = getSignExtendExpr(Step, Ty, Depth + 1);
1931 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1932 }
1933 }
1934
1935 // Before doing any expensive analysis, check to see if we've already
1936 // computed a SCEV for this Op and Ty.
1939 ID.AddPointer(Op.getOpaqueValue());
1940 ID.AddPointer(Ty);
1942 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
1943 return S;
1944 // Limit recursion depth.
1945 if (Depth > MaxCastDepth) {
1946 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1947 Op, Ty);
1948 UniqueSCEVs.insert(S, Token);
1949 S->computeAndSetCanonical(*this);
1950 registerUser(S, Op);
1951 return S;
1952 }
1953
1954 // sext(trunc(x)) --> sext(x) or x or trunc(x)
1956 // It's possible the bits taken off by the truncate were all sign bits. If
1957 // so, we should be able to simplify this further.
1958 const SCEV *X = ST->getOperand();
1960 unsigned TruncBits = getTypeSizeInBits(ST->getType());
1961 unsigned NewBits = getTypeSizeInBits(Ty);
1962 if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1963 CR.sextOrTrunc(NewBits)))
1964 return getTruncateOrSignExtend(X, Ty, Depth);
1965 }
1966
1967 if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1968 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
1969 if (SA->hasNoSignedWrap()) {
1970 // If the addition does not sign overflow then we can, by definition,
1971 // commute the sign extension with the addition operation.
1973 for (SCEVUse Op : SA->operands())
1974 Ops.push_back(getSignExtendExpr(Op, Ty, Depth + 1));
1975 return getAddExpr(Ops, SCEV::FlagNSW, Depth + 1);
1976 }
1977
1978 // sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...))
1979 // if D + (C - D + x + y + ...) could be proven to not signed wrap
1980 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1981 //
1982 // For instance, this will bring two seemingly different expressions:
1983 // 1 + sext(5 + 20 * %x + 24 * %y) and
1984 // sext(6 + 20 * %x + 24 * %y)
1985 // to the same form:
1986 // 2 + sext(4 + 20 * %x + 24 * %y)
1987 if (const auto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1988 const APInt &D = extractConstantWithoutWrapping(*this, SC, SA);
1989 if (D != 0) {
1990 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
1991 const SCEV *SResidual =
1993 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
1994 return getAddExpr(SSExtD, SSExtR, (SCEV::FlagNSW | SCEV::FlagNUW),
1995 Depth + 1);
1996 }
1997 }
1998 }
1999 // If the input value is a chrec scev, and we can prove that the value
2000 // did not overflow the old, smaller, value, we can sign extend all of the
2001 // operands (often constants). This allows analysis of something like
2002 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
2003 if (match(Op, m_scev_AffineAddRec(m_SCEV(Start), m_SCEV(Step), m_Loop(L)))) {
2004 const auto *AR = cast<SCEVAddRecExpr>(Op);
2005 unsigned BitWidth = getTypeSizeInBits(AR->getType());
2006
2007 // The no-signed-wrap case is handled before the uniquing lookup above.
2008
2009 // Check whether the backedge-taken count is SCEVCouldNotCompute.
2010 // Note that this serves two purposes: It filters out loops that are
2011 // simply not analyzable, and it covers the case where this code is
2012 // being called from within backedge-taken count analysis, such that
2013 // attempting to ask for the backedge-taken count would likely result
2014 // in infinite recursion. In the later case, the analysis code will
2015 // cope with a conservative value, and it will take care to purge
2016 // that value once it has finished.
2017 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
2018 if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
2019 // Manually compute the final value for AR, checking for
2020 // overflow.
2021
2022 // Check whether the backedge-taken count can be losslessly casted to
2023 // the addrec's type. The count is always unsigned.
2024 const SCEV *CastedMaxBECount =
2025 getTruncateOrZeroExtend(MaxBECount, Start->getType(), Depth);
2026 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
2027 CastedMaxBECount, MaxBECount->getType(), Depth);
2028 if (MaxBECount == RecastedMaxBECount) {
2029 Type *WideTy = IntegerType::get(getContext(), BitWidth * 2);
2030 // Check whether Start+Step*MaxBECount has no signed overflow.
2031 const SCEV *SMul =
2032 getMulExpr(CastedMaxBECount, Step, SCEV::FlagAnyWrap, Depth + 1);
2033 const SCEV *SAdd = getSignExtendExpr(
2034 getAddExpr(Start, SMul, SCEV::FlagAnyWrap, Depth + 1), WideTy,
2035 Depth + 1);
2036 const SCEV *WideStart = getSignExtendExpr(Start, WideTy, Depth + 1);
2037 const SCEV *WideMaxBECount =
2038 getZeroExtendExpr(CastedMaxBECount, WideTy, Depth + 1);
2039 const SCEV *OperandExtendedAdd =
2040 getAddExpr(WideStart,
2041 getMulExpr(WideMaxBECount,
2042 getSignExtendExpr(Step, WideTy, Depth + 1),
2045 if (SAdd == OperandExtendedAdd) {
2046 // Cache knowledge of AR NSW, which is propagated to this AddRec.
2047 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNSW);
2048 // Return the expression with the addrec on the outside.
2049 Start =
2051 Step = getSignExtendExpr(Step, Ty, Depth + 1);
2052 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2053 }
2054 // Similar to above, only this time treat the step value as unsigned.
2055 // This covers loops that count up with an unsigned step.
2056 OperandExtendedAdd =
2057 getAddExpr(WideStart,
2058 getMulExpr(WideMaxBECount,
2059 getZeroExtendExpr(Step, WideTy, Depth + 1),
2062 if (SAdd == OperandExtendedAdd) {
2063 // If AR wraps around then
2064 //
2065 // abs(Step) * MaxBECount > unsigned-max(AR->getType())
2066 // => SAdd != OperandExtendedAdd
2067 //
2068 // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
2069 // (SAdd == OperandExtendedAdd => AR is NW)
2070
2071 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNW);
2072
2073 // Return the expression with the addrec on the outside.
2074 Start =
2076 Step = getZeroExtendExpr(Step, Ty, Depth + 1);
2077 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2078 }
2079 }
2080 }
2081
2082 auto NewFlags = proveNoSignedWrapViaInduction(AR);
2083 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
2084 if (AR->hasNoSignedWrap()) {
2085 // Same as nsw case above - duplicated here to avoid a compile time
2086 // issue. It's not clear that the order of checks does matter, but
2087 // it's one of two issue possible causes for a change which was
2088 // reverted. Be conservative for the moment.
2089 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1);
2090 Step = getSignExtendExpr(Step, Ty, Depth + 1);
2091 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2092 }
2093
2094 // sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step}))<nuw><nsw>
2095 // if D + (C - D + Step * n) could be proven to not signed wrap
2096 // where D maximizes the number of trailing zeros of (C - D + Step * n)
2097 if (const auto *SC = dyn_cast<SCEVConstant>(Start)) {
2098 const APInt &C = SC->getAPInt();
2099 const APInt &D = extractConstantWithoutWrapping(*this, C, Step);
2100 if (D != 0) {
2101 const SCEV *SSExtD = getSignExtendExpr(getConstant(D), Ty, Depth);
2102 const SCEV *SResidual =
2103 getAddRecExpr(getConstant(C - D), Step, L, AR->getNoWrapFlags());
2104 const SCEV *SSExtR = getSignExtendExpr(SResidual, Ty, Depth + 1);
2105 return getAddExpr(SSExtD, SSExtR, (SCEV::FlagNSW | SCEV::FlagNUW),
2106 Depth + 1);
2107 }
2108 }
2109
2110 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
2111 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), SCEV::FlagNSW);
2112 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, this, Depth + 1);
2113 Step = getSignExtendExpr(Step, Ty, Depth + 1);
2114 return getAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2115 }
2116 }
2117
2118 // If the input value is provably positive and we could not simplify
2119 // away the sext build a zext instead.
2121 return getZeroExtendExpr(Op, Ty, Depth + 1);
2122
2123 // sext(smin(x, y)) -> smin(sext(x), sext(y))
2124 // sext(smax(x, y)) -> smax(sext(x), sext(y))
2128 for (SCEVUse Operand : MinMax->operands())
2129 Operands.push_back(getSignExtendExpr(Operand, Ty));
2131 return getSMinExpr(Operands);
2132 return getSMaxExpr(Operands);
2133 }
2134
2135 // The cast wasn't folded; create an explicit cast node.
2136 // Recompute the insert position, as it may have been invalidated.
2137 if (const SCEV *S = UniqueSCEVs.lookup(ID, Token))
2138 return S;
2139 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
2140 Op, Ty);
2141 UniqueSCEVs.insert(S, Token);
2142 S->computeAndSetCanonical(*this);
2143 registerUser(S, Op);
2144 return S;
2145}
2146
2148 switch (Kind) {
2149 case scTruncate:
2150 return getTruncateExpr(Op, Ty);
2151 case scZeroExtend:
2152 return getZeroExtendExpr(Op, Ty);
2153 case scSignExtend:
2154 return getSignExtendExpr(Op, Ty);
2155 case scPtrToAddr: {
2156 const SCEV *Expr = getPtrToAddrExpr(Op);
2157 assert(Expr->getType() == Ty && "requested type must match");
2158 return Expr;
2159 }
2160 default:
2161 llvm_unreachable("Not a SCEV cast expression!");
2162 }
2163}
2164
2165/// getAnyExtendExpr - Return a SCEV for the given operand extended with
2166/// unspecified bits out to the given type.
2168 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
2169 "This is not an extending conversion!");
2170 assert(isSCEVable(Ty) &&
2171 "This is not a conversion to a SCEVable type!");
2172 Ty = getEffectiveSCEVType(Ty);
2173
2174 // Sign-extend negative constants.
2175 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
2176 if (SC->getAPInt().isNegative())
2177 return getSignExtendExpr(Op, Ty);
2178
2179 // Peel off a truncate cast.
2181 const SCEV *NewOp = T->getOperand();
2182 if (getTypeSizeInBits(NewOp->getType()) < getTypeSizeInBits(Ty))
2183 return getAnyExtendExpr(NewOp, Ty);
2184 return getTruncateOrNoop(NewOp, Ty);
2185 }
2186
2187 // Next try a zext cast. If the cast is folded, use it.
2188 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
2189 if (!isa<SCEVZeroExtendExpr>(ZExt))
2190 return ZExt;
2191
2192 // Next try a sext cast. If the cast is folded, use it.
2193 const SCEV *SExt = getSignExtendExpr(Op, Ty);
2194 if (!isa<SCEVSignExtendExpr>(SExt))
2195 return SExt;
2196
2197 // Force the cast to be folded into the operands of an addrec.
2198 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
2200 for (const SCEV *Op : AR->operands())
2201 Ops.push_back(getAnyExtendExpr(Op, Ty));
2202 return getAddRecExpr(Ops, AR->getLoop(), SCEV::FlagNW);
2203 }
2204
2205 // If the expression is obviously signed, use the sext cast value.
2206 if (isa<SCEVSMaxExpr>(Op))
2207 return SExt;
2208
2209 // Absent any other information, use the zext cast value.
2210 return ZExt;
2211}
2212
2213/// Process the given Ops list, which is a list of operands to be added under
2214/// the given scale, update the given map. This is a helper function for
2215/// getAddRecExpr. As an example of what it does, given a sequence of operands
2216/// that would form an add expression like this:
2217///
2218/// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
2219///
2220/// where A and B are constants, update the map with these values:
2221///
2222/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
2223///
2224/// and add 13 + A*B*29 to AccumulatedConstant.
2225/// This will allow getAddRecExpr to produce this:
2226///
2227/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
2228///
2229/// This form often exposes folding opportunities that are hidden in
2230/// the original operand list.
2231///
2232/// Return true iff it appears that any interesting folding opportunities
2233/// may be exposed. This helps getAddRecExpr short-circuit extra work in
2234/// the common case where no interesting opportunities are present, and
2235/// is also used as a check to avoid infinite recursion.
2238 APInt &AccumulatedConstant,
2240 const APInt &Scale,
2241 ScalarEvolution &SE) {
2242 bool Interesting = false;
2243
2244 // Iterate over the add operands. They are sorted, with constants first.
2245 unsigned i = 0;
2246 while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2247 ++i;
2248 // Pull a buried constant out to the outside.
2249 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
2250 Interesting = true;
2251 AccumulatedConstant += Scale * C->getAPInt();
2252 }
2253
2254 // Next comes everything else. We're especially interested in multiplies
2255 // here, but they're in the middle, so just visit the rest with one loop.
2256 for (; i != Ops.size(); ++i) {
2258 if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
2259 APInt NewScale =
2260 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt();
2261 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
2262 // A multiplication of a constant with another add; recurse.
2263 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
2264 Interesting |= CollectAddOperandsWithScales(
2265 M, NewOps, AccumulatedConstant, Add->operands(), NewScale, SE);
2266 } else {
2267 // A multiplication of a constant with some other value. Update
2268 // the map.
2269 SmallVector<SCEVUse, 4> MulOps(drop_begin(Mul->operands()));
2270 const SCEV *Key = SE.getMulExpr(MulOps);
2271 auto Pair = M.insert({Key, NewScale});
2272 if (Pair.second) {
2273 NewOps.push_back(Pair.first->first);
2274 } else {
2275 Pair.first->second += NewScale;
2276 // The map already had an entry for this value, which may indicate
2277 // a folding opportunity.
2278 Interesting = true;
2279 }
2280 }
2281 } else {
2282 // An ordinary operand. Update the map.
2283 auto Pair = M.insert({Ops[i], Scale});
2284 if (Pair.second) {
2285 NewOps.push_back(Pair.first->first);
2286 } else {
2287 Pair.first->second += Scale;
2288 // The map already had an entry for this value, which may indicate
2289 // a folding opportunity.
2290 Interesting = true;
2291 }
2292 }
2293 }
2294
2295 return Interesting;
2296}
2297
2299 const SCEV *LHS, const SCEV *RHS,
2300 const Instruction *CtxI) {
2301 auto Operation = [this, BinOp](SCEVUse L, SCEVUse R) -> const SCEV * {
2302 switch (BinOp) {
2303 default:
2304 llvm_unreachable("Unsupported binary op");
2305 case Instruction::Add:
2306 return getAddExpr(L, R);
2307 case Instruction::Sub:
2308 return getMinusSCEV(L, R);
2309 case Instruction::Mul:
2310 return getMulExpr(L, R);
2311 }
2312 };
2313
2314 const SCEV *(ScalarEvolution::*Extension)(SCEVUse, Type *, unsigned) =
2317
2318 // Check ext(LHS op RHS) == ext(LHS) op ext(RHS)
2319 auto *NarrowTy = cast<IntegerType>(LHS->getType());
2320 auto *WideTy =
2321 IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
2322
2323 const SCEV *A = (this->*Extension)(Operation(LHS, RHS), WideTy, 0);
2324 const SCEV *LHSB = (this->*Extension)(LHS, WideTy, 0);
2325 const SCEV *RHSB = (this->*Extension)(RHS, WideTy, 0);
2326 const SCEV *B = Operation(LHSB, RHSB);
2327 if (A == B)
2328 return true;
2329 // Can we use context to prove the fact we need?
2330 if (!CtxI)
2331 return false;
2332 // TODO: Support mul.
2333 if (BinOp == Instruction::Mul)
2334 return false;
2335 auto *RHSC = dyn_cast<SCEVConstant>(RHS);
2336 // TODO: Lift this limitation.
2337 if (!RHSC)
2338 return false;
2339 APInt C = RHSC->getAPInt();
2340 unsigned NumBits = C.getBitWidth();
2341 bool IsSub = (BinOp == Instruction::Sub);
2342 bool IsNegativeConst = (Signed && C.isNegative());
2343 // Compute the direction and magnitude by which we need to check overflow.
2344 bool OverflowDown = IsSub ^ IsNegativeConst;
2345 APInt Magnitude = C;
2346 if (IsNegativeConst) {
2347 if (C == APInt::getSignedMinValue(NumBits))
2348 // TODO: SINT_MIN on inversion gives the same negative value, we don't
2349 // want to deal with that.
2350 return false;
2351 Magnitude = -C;
2352 }
2353
2355 if (OverflowDown) {
2356 // To avoid overflow down, we need to make sure that MIN + Magnitude <= LHS.
2357 APInt Min = Signed ? APInt::getSignedMinValue(NumBits)
2358 : APInt::getMinValue(NumBits);
2359 APInt Limit = Min + Magnitude;
2360 return isKnownPredicateAt(Pred, getConstant(Limit), LHS, CtxI);
2361 } else {
2362 // To avoid overflow up, we need to make sure that LHS <= MAX - Magnitude.
2363 APInt Max = Signed ? APInt::getSignedMaxValue(NumBits)
2364 : APInt::getMaxValue(NumBits);
2365 APInt Limit = Max - Magnitude;
2366 return isKnownPredicateAt(Pred, LHS, getConstant(Limit), CtxI);
2367 }
2368}
2369
2370std::optional<SCEV::NoWrapFlags>
2372 const OverflowingBinaryOperator *OBO) {
2373 // It cannot be done any better.
2374 if (OBO->hasNoUnsignedWrap() && OBO->hasNoSignedWrap())
2375 return std::nullopt;
2376
2377 SCEV::NoWrapFlags Flags = SCEV::NoWrapFlags::FlagAnyWrap;
2378
2379 if (OBO->hasNoUnsignedWrap())
2381 if (OBO->hasNoSignedWrap())
2383
2384 bool Deduced = false;
2385
2387 const SCEV *LHS = getSCEV(OBO->getOperand(0));
2388 const SCEV *RHS = getSCEV(OBO->getOperand(1));
2389
2390 bool CanUseNSW = true;
2391 const APInt *ShiftAmt;
2392 // Treat `shl %a, C` as `mul %a, 1 << C`.
2393 if (match(OBO, m_Shl(m_Value(), m_APInt(ShiftAmt)))) {
2394 unsigned BitWidth = ShiftAmt->getBitWidth();
2395 if (ShiftAmt->uge(BitWidth))
2396 return std::nullopt;
2397 // NSW only transfers if the shift amount is < BitWidth - 1, as INT_MIN * -1
2398 // overflows.
2399 CanUseNSW = ShiftAmt->ult(BitWidth - 1);
2400 Opcode = Instruction::Mul;
2402 } else if (Opcode != Instruction::Add && Opcode != Instruction::Sub &&
2403 Opcode != Instruction::Mul) {
2404 return std::nullopt;
2405 }
2406
2407 const Instruction *CtxI =
2409 if (!OBO->hasNoUnsignedWrap() &&
2410 willNotOverflow(Opcode, /* Signed */ false, LHS, RHS, CtxI)) {
2412 Deduced = true;
2413 }
2414
2415 if (CanUseNSW && !OBO->hasNoSignedWrap() &&
2416 willNotOverflow(Opcode, /* Signed */ true, LHS, RHS, CtxI)) {
2418 Deduced = true;
2419 }
2420
2421 if (Deduced)
2422 return Flags;
2423 return std::nullopt;
2424}
2425
2426// We're trying to construct a SCEV of type `Type' with `Ops' as operands and
2427// `OldFlags' as can't-wrap behavior. Infer a more aggressive set of
2428// can't-overflow flags for the operation if possible.
2432 SCEV::NoWrapFlags Flags) {
2433 using namespace std::placeholders;
2434
2435 using OBO = OverflowingBinaryOperator;
2436
2437 bool CanAnalyze =
2439 (void)CanAnalyze;
2440 assert(CanAnalyze && "don't call from other places!");
2441
2442 SCEV::NoWrapFlags SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
2443 SCEV::NoWrapFlags SignOrUnsignWrap =
2444 ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2445
2446 // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
2447 auto IsKnownNonNegative = [&](SCEVUse U) {
2448 return SE->isKnownNonNegative(U);
2449 };
2450
2451 if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Ops, IsKnownNonNegative))
2452 Flags = ScalarEvolution::setFlags(Flags, SignOrUnsignMask);
2453
2454 SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2455
2456 if (SignOrUnsignWrap != SignOrUnsignMask &&
2457 (Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 &&
2458 isa<SCEVConstant>(Ops[0])) {
2459
2460 auto Opcode = [&] {
2461 switch (Type) {
2462 case scAddExpr:
2463 return Instruction::Add;
2464 case scMulExpr:
2465 return Instruction::Mul;
2466 default:
2467 llvm_unreachable("Unexpected SCEV op.");
2468 }
2469 }();
2470
2471 const APInt &C = cast<SCEVConstant>(Ops[0])->getAPInt();
2472
2473 // (A <opcode> C) --> (A <opcode> C)<nsw> if the op doesn't sign overflow.
2474 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
2476 Opcode, C, OBO::NoSignedWrap);
2477 if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
2479 }
2480
2481 // (A <opcode> C) --> (A <opcode> C)<nuw> if the op doesn't unsign overflow.
2482 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
2484 Opcode, C, OBO::NoUnsignedWrap);
2485 if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
2487 }
2488 }
2489
2490 // <0,+,nonnegative><nw> is also nuw
2491 // TODO: Add corresponding nsw case
2493 !ScalarEvolution::hasFlags(Flags, SCEV::FlagNUW) && Ops.size() == 2 &&
2494 Ops[0]->isZero() && IsKnownNonNegative(Ops[1]))
2496
2497 // both (udiv X, Y) * Y and Y * (udiv X, Y) are always NUW
2499 Ops.size() == 2) {
2500 if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[0]))
2501 if (UDiv->getOperand(1) == Ops[1])
2503 if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[1]))
2504 if (UDiv->getOperand(1) == Ops[0])
2506 }
2507
2508 return Flags;
2509}
2510
2512 return isLoopInvariant(S, L) && properlyDominates(S, L->getHeader());
2513}
2514
2515/// Get a canonical add expression, or something simpler if possible.
2517 SCEVFlags Flags, unsigned Depth) {
2518 SCEV::NoWrapFlags OrigFlags = Flags.ExprFlags;
2519 SCEV::NoWrapFlags UseFlags = Flags.UseFlags;
2520 assert(!(OrigFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
2521 "only nuw or nsw allowed");
2522 assert(!(UseFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
2523 "only nuw or nsw allowed");
2524 assert(!Ops.empty() && "Cannot get empty add!");
2525 if (Ops.size() == 1) return Ops[0];
2526#ifndef NDEBUG
2527 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2528 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2529 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2530 "SCEVAddExpr operand types don't match!");
2531 unsigned NumPtrs = count_if(
2532 Ops, [](const SCEV *Op) { return Op->getType()->isPointerTy(); });
2533 assert(NumPtrs <= 1 && "add has at most one pointer operand");
2534#endif
2535
2536 const SCEV *Folded = constantFoldAndGroupOps(
2537 *this, LI, DT, Ops,
2538 [](const APInt &C1, const APInt &C2) { return C1 + C2; },
2539 [](const APInt &C) { return C.isZero(); }, // identity
2540 [](const APInt &C) { return false; }); // absorber
2541 if (Folded)
2542 return Folded;
2543
2544#ifndef NDEBUG
2545 // Keep track of operands after constant folding, for verification when adding
2546 // use-specific flags.
2547 const SmallVector<SCEVUse, 8> OrigOps(Ops.begin(), Ops.end());
2548#endif
2549
2550 unsigned Idx = isa<SCEVConstant>(Ops[0]) ? 1 : 0;
2551
2552 // Delay expensive flag strengthening until necessary.
2553 auto ComputeFlags = [this, OrigFlags](ArrayRef<SCEVUse> Ops) {
2554 return StrengthenNoWrapFlags(this, scAddExpr, Ops, OrigFlags);
2555 };
2556
2557 // Limit recursion calls depth.
2559 return {getOrCreateAddExpr(Ops, ComputeFlags(Ops)), UseFlags};
2560
2561 if (SCEV *S = findExistingSCEVInCache(scAddExpr, Ops)) {
2562 // Don't strengthen flags if we have no new information.
2563 SCEVAddExpr *Add = static_cast<SCEVAddExpr *>(S);
2564 if (Add->getNoWrapFlags(OrigFlags) != OrigFlags)
2565 Add->setNoWrapFlags(ComputeFlags(Ops));
2566 return {S, UseFlags};
2567 }
2568
2569 // Okay, check to see if the same value occurs in the operand list more than
2570 // once. If so, merge them together into an multiply expression. Since we
2571 // sorted the list, these values are required to be adjacent.
2572 Type *Ty = Ops[0]->getType();
2573 bool FoundMatch = false;
2574 for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
2575 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
2576 // Scan ahead to count how many equal operands there are.
2577 unsigned Count = 2;
2578 while (i+Count != e && Ops[i+Count] == Ops[i])
2579 ++Count;
2580 // Merge the values into a multiply.
2581 SCEVUse Scale = getConstant(Ty, Count);
2582 const SCEV *Mul = getMulExpr(Scale, Ops[i], SCEV::FlagAnyWrap, Depth + 1);
2583 if (Ops.size() == Count)
2584 return Mul;
2585 Ops[i] = Mul;
2586 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
2587 --i; e -= Count - 1;
2588 FoundMatch = true;
2589 }
2590 if (FoundMatch)
2591 return getAddExpr(Ops, OrigFlags, Depth + 1);
2592
2593 // Check for truncates. If all the operands are truncated from the same
2594 // type, see if factoring out the truncate would permit the result to be
2595 // folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y)
2596 // if the contents of the resulting outer trunc fold to something simple.
2597 auto FindTruncSrcType = [&]() -> Type * {
2598 // We're ultimately looking to fold an addrec of truncs and muls of only
2599 // constants and truncs, so if we find any other types of SCEV
2600 // as operands of the addrec then we bail and return nullptr here.
2601 // Otherwise, we return the type of the operand of a trunc that we find.
2602 if (auto *T = dyn_cast<SCEVTruncateExpr>(Ops[Idx]))
2603 return T->getOperand()->getType();
2604 if (const auto *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2605 SCEVUse LastOp = Mul->getOperand(Mul->getNumOperands() - 1);
2606 if (const auto *T = dyn_cast<SCEVTruncateExpr>(LastOp))
2607 return T->getOperand()->getType();
2608 }
2609 return nullptr;
2610 };
2611 if (auto *SrcType = FindTruncSrcType()) {
2612 SmallVector<SCEVUse, 8> LargeOps;
2613 bool Ok = true;
2614 // Check all the operands to see if they can be represented in the
2615 // source type of the truncate.
2616 for (const SCEV *Op : Ops) {
2618 if (T->getOperand()->getType() != SrcType) {
2619 Ok = false;
2620 break;
2621 }
2622 LargeOps.push_back(T->getOperand());
2623 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Op)) {
2624 LargeOps.push_back(getAnyExtendExpr(C, SrcType));
2625 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Op)) {
2626 SmallVector<SCEVUse, 8> LargeMulOps;
2627 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2628 if (const SCEVTruncateExpr *T =
2629 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2630 if (T->getOperand()->getType() != SrcType) {
2631 Ok = false;
2632 break;
2633 }
2634 LargeMulOps.push_back(T->getOperand());
2635 } else if (const auto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) {
2636 LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
2637 } else {
2638 Ok = false;
2639 break;
2640 }
2641 }
2642 if (Ok)
2643 LargeOps.push_back(getMulExpr(LargeMulOps, SCEV::FlagAnyWrap, Depth + 1));
2644 } else {
2645 Ok = false;
2646 break;
2647 }
2648 }
2649 if (Ok) {
2650 // Evaluate the expression in the larger type.
2651 const SCEV *Fold = getAddExpr(LargeOps, SCEV::FlagAnyWrap, Depth + 1);
2652 // If it folds to something simple, use it. Otherwise, don't.
2653 if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
2654 return getTruncateExpr(Fold, Ty);
2655 }
2656 }
2657
2658 if (Ops.size() == 2) {
2659 // Check if we have an expression of the form ((X + C1) - C2), where C1 and
2660 // C2 can be folded in a way that allows retaining wrapping flags of (X +
2661 // C1).
2662 const SCEV *A = Ops[0];
2663 const SCEV *B = Ops[1];
2664 auto *AddExpr = dyn_cast<SCEVAddExpr>(B);
2665 auto *C = dyn_cast<SCEVConstant>(A);
2666 if (AddExpr && C && isa<SCEVConstant>(AddExpr->getOperand(0))) {
2667 auto C1 = cast<SCEVConstant>(AddExpr->getOperand(0))->getAPInt();
2668 auto C2 = C->getAPInt();
2669 SCEV::NoWrapFlags PreservedFlags = SCEV::FlagAnyWrap;
2670
2671 APInt ConstAdd = C1 + C2;
2672 auto AddFlags = AddExpr->getNoWrapFlags();
2673 // Adding a smaller constant is NUW if the original AddExpr was NUW.
2675 ConstAdd.ule(C1)) {
2676 PreservedFlags =
2678 }
2679
2680 // Adding a constant with the same sign and small magnitude is NSW, if the
2681 // original AddExpr was NSW.
2683 C1.isSignBitSet() == ConstAdd.isSignBitSet() &&
2684 ConstAdd.abs().ule(C1.abs())) {
2685 PreservedFlags =
2687 }
2688
2689 if (PreservedFlags != SCEV::FlagAnyWrap) {
2690 SmallVector<SCEVUse, 4> NewOps(AddExpr->operands());
2691 NewOps[0] = getConstant(ConstAdd);
2692 return getAddExpr(NewOps, PreservedFlags);
2693 }
2694 }
2695
2696 // Try to push the constant operand into a ZExt: A + zext (-A + B) -> zext
2697 // (B), if trunc (A) + -A + B does not unsigned-wrap.
2698 const SCEVAddExpr *InnerAdd;
2699 if (match(B, m_scev_ZExt(m_scev_Add(InnerAdd)))) {
2700 const SCEV *NarrowA = getTruncateExpr(A, InnerAdd->getType());
2701 if (NarrowA == getNegativeSCEV(InnerAdd->getOperand(0)) &&
2702 getZeroExtendExpr(NarrowA, B->getType()) == A &&
2703 hasFlags(StrengthenNoWrapFlags(this, scAddExpr, {NarrowA, InnerAdd},
2705 SCEV::FlagNUW)) {
2706 return getZeroExtendExpr(getAddExpr(NarrowA, InnerAdd), B->getType());
2707 }
2708 }
2709 }
2710
2711 // Canonicalize (-1 * urem X, Y) + X --> (Y * X/Y)
2712 const SCEV *Y;
2713 if (Ops.size() == 2 &&
2714 match(Ops[0],
2716 m_scev_URem(m_scev_Specific(Ops[1]), m_SCEV(Y), *this))))
2717 return getMulExpr(Y, getUDivExpr(Ops[1], Y));
2718
2719 // Skip past any other cast SCEVs.
2720 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
2721 ++Idx;
2722
2723 // If there are add operands they would be next.
2724 if (Idx < Ops.size()) {
2725 bool DeletedAdd = false;
2726 // If the original flags and all inlined SCEVAddExprs are NUW, use the
2727 // common NUW flag for expression after inlining. Other flags cannot be
2728 // preserved, because they may depend on the original order of operations.
2729 SCEV::NoWrapFlags CommonFlags = maskFlags(OrigFlags, SCEV::FlagNUW);
2730 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
2731 if (Ops.size() > AddOpsInlineThreshold ||
2732 Add->getNumOperands() > AddOpsInlineThreshold)
2733 break;
2734 // If we have an add, expand the add operands onto the end of the operands
2735 // list.
2736 Ops.erase(Ops.begin()+Idx);
2737 append_range(Ops, Add->operands());
2738 DeletedAdd = true;
2739 CommonFlags = maskFlags(CommonFlags, Add->getNoWrapFlags());
2740 }
2741
2742 // If we deleted at least one add, we added operands to the end of the list,
2743 // and they are not necessarily sorted. Recurse to resort and resimplify
2744 // any operands we just acquired.
2745 if (DeletedAdd)
2746 return getAddExpr(Ops, CommonFlags, Depth + 1);
2747 }
2748
2749 // Skip over the add expression until we get to a multiply.
2750 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2751 ++Idx;
2752
2753 // Check to see if there are any folding opportunities present with
2754 // operands multiplied by constant values.
2755 if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
2756 uint64_t BitWidth = getTypeSizeInBits(Ty);
2759 APInt AccumulatedConstant(BitWidth, 0);
2760 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2761 Ops, APInt(BitWidth, 1), *this)) {
2762 struct APIntCompare {
2763 bool operator()(const APInt &LHS, const APInt &RHS) const {
2764 return LHS.ult(RHS);
2765 }
2766 };
2767
2768 // Some interesting folding opportunity is present, so its worthwhile to
2769 // re-generate the operands list. Group the operands by constant scale,
2770 // to avoid multiplying by the same constant scale multiple times.
2771 std::map<APInt, SmallVector<SCEVUse, 4>, APIntCompare> MulOpLists;
2772 for (const SCEV *NewOp : NewOps)
2773 MulOpLists[M.find(NewOp)->second].push_back(NewOp);
2774 // Re-generate the operands list.
2775 Ops.clear();
2776 if (AccumulatedConstant != 0)
2777 Ops.push_back(getConstant(AccumulatedConstant));
2778 for (auto &MulOp : MulOpLists) {
2779 if (MulOp.first == 1) {
2780 Ops.push_back(getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1));
2781 } else if (MulOp.first != 0) {
2782 Ops.push_back(getMulExpr(
2783 getConstant(MulOp.first),
2784 getAddExpr(MulOp.second, SCEV::FlagAnyWrap, Depth + 1),
2785 SCEV::FlagAnyWrap, Depth + 1));
2786 }
2787 }
2788 if (Ops.empty())
2789 return getZero(Ty);
2790 if (Ops.size() == 1)
2791 return Ops[0];
2792 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2793 }
2794 }
2795
2796 // Given a SCEVMulExpr and an operand index, return the product of all
2797 // operands except the one at OpIdx.
2798 auto StripFactor = [&](const SCEVMulExpr *M, unsigned OpIdx) -> SCEVUse {
2799 if (M->getNumOperands() == 2)
2800 return M->getOperand(OpIdx == 0);
2801 SmallVector<SCEVUse, 4> Remaining(M->operands().take_front(OpIdx));
2802 append_range(Remaining, M->operands().drop_front(OpIdx + 1));
2803 return getMulExpr(Remaining, SCEV::FlagAnyWrap, Depth + 1);
2804 };
2805
2806 // If we are adding something to a multiply expression, make sure the
2807 // something is not already an operand of the multiply. If so, merge it into
2808 // the multiply.
2809 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
2810 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
2811 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
2812 // Scan all terms to find every occurrence of common factor MulOpSCEV
2813 // and fold them in one shot:
2814 // A1*X + A2*X + ... + An*X --> X * (A1 + A2 + ... + An)
2815 const SCEV *MulOpSCEV = Mul->getOperand(MulOp);
2816 if (isa<SCEVConstant>(MulOpSCEV))
2817 continue;
2818
2819 // Cofactors: 1 for bare addends matching MulOpSCEV, or the
2820 // remaining product for multiply terms containing MulOpSCEV.
2821 SmallVector<SCEVUse, 4> Cofactors;
2822 SmallVector<unsigned, 4> DeadIndices;
2823 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp) {
2824 if (MulOpSCEV == Ops[AddOp]) {
2825 // W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
2826 Cofactors.push_back(getOne(Ty));
2827 DeadIndices.push_back(AddOp);
2828 continue;
2829 }
2830
2831 if (AddOp <= Idx || !isa<SCEVMulExpr>(Ops[AddOp]))
2832 continue;
2833
2834 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[AddOp]);
2835 for (unsigned OMulOp = 0, OE = OtherMul->getNumOperands(); OMulOp != OE;
2836 ++OMulOp) {
2837 if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
2838 // (A*B*C) + (A*D*E) --> A * (B*C + D*E)
2839 Cofactors.push_back(StripFactor(OtherMul, OMulOp));
2840 DeadIndices.push_back(AddOp);
2841 break;
2842 }
2843 }
2844 }
2845
2846 // Fold all collected cofactors with the anchor multiply's cofactor:
2847 // MulOpSCEV * (Cofactor_1 + ... + Cofactor_n + AnchorCofactor)
2848 if (!Cofactors.empty()) {
2849 Cofactors.push_back(StripFactor(Mul, MulOp));
2850
2851 SCEVUse InnerSum = getAddExpr(Cofactors, SCEV::FlagAnyWrap, Depth + 1);
2852 SCEVUse OuterMul =
2853 getMulExpr(MulOpSCEV, InnerSum, SCEV::FlagAnyWrap, Depth + 1);
2854
2855 // DeadIndices does not include Idx (the anchor), hence +1.
2856 if (Ops.size() == DeadIndices.size() + 1)
2857 return OuterMul;
2858
2859 // Erase Ops[Idx] first, then erase DeadIndices in reverse order.
2860 // The -1 adjustment accounts for the shift from removing Idx;
2861 // reverse order means each erasure only shifts later positions,
2862 // which have already been processed.
2863 Ops.erase(Ops.begin() + Idx);
2864 for (unsigned Dead : reverse(DeadIndices))
2865 Ops.erase(Ops.begin() + (Dead > Idx ? Dead - 1 : Dead));
2866
2867 Ops.push_back(OuterMul);
2868 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2869 }
2870 }
2871 }
2872
2873 // If there are any add recurrences in the operands list, see if any other
2874 // added values are loop invariant. If so, we can fold them into the
2875 // recurrence.
2876 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2877 ++Idx;
2878
2879 // Scan over all recurrences, trying to fold loop invariants into them.
2880 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2881 // Scan all of the other operands to this add and add them to the vector if
2882 // they are loop invariant w.r.t. the recurrence.
2884 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2885 const Loop *AddRecLoop = AddRec->getLoop();
2886 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2887 if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
2888 LIOps.push_back(Ops[i]);
2889 Ops.erase(Ops.begin()+i);
2890 --i; --e;
2891 }
2892
2893 // If we found some loop invariants, fold them into the recurrence.
2894 if (!LIOps.empty()) {
2895 // Compute nowrap flags for the addition of the loop-invariant ops and
2896 // the addrec. Temporarily push it as an operand for that purpose. These
2897 // flags are valid in the scope of the addrec only.
2898 LIOps.push_back(AddRec);
2899 SCEV::NoWrapFlags Flags = ComputeFlags(LIOps);
2900 LIOps.pop_back();
2901
2902 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
2903 LIOps.push_back(AddRec->getStart());
2904
2905 SmallVector<SCEVUse, 4> AddRecOps(AddRec->operands());
2906
2907 // It is not in general safe to propagate flags valid on an add within
2908 // the addrec scope to one outside it. We must prove that the inner
2909 // scope is guaranteed to execute if the outer one does to be able to
2910 // safely propagate. We know the program is undefined if poison is
2911 // produced on the inner scoped addrec. We also know that *for this use*
2912 // the outer scoped add can't overflow (because of the flags we just
2913 // computed for the inner scoped add) without the program being undefined.
2914 // Proving that entry to the outer scope neccesitates entry to the inner
2915 // scope, thus proves the program undefined if the flags would be violated
2916 // in the outer scope.
2917 SCEV::NoWrapFlags AddFlags = Flags;
2918 if (AddFlags != SCEV::FlagAnyWrap) {
2919 auto *DefI = getDefiningScopeBound(LIOps);
2920 auto *ReachI = &*AddRecLoop->getHeader()->begin();
2921 if (!isGuaranteedToTransferExecutionTo(DefI, ReachI))
2922 AddFlags = SCEV::FlagAnyWrap;
2923 }
2924 AddRecOps[0] = getAddExpr(LIOps, AddFlags, Depth + 1);
2925
2926 // Build the new addrec. Propagate the NUW and NSW flags if both the
2927 // outer add and the inner addrec are guaranteed to have no overflow.
2928 // Always propagate NW.
2929 Flags = AddRec->getNoWrapFlags(setFlags(Flags, SCEV::FlagNW));
2930 const SCEV *NewRec = getAddRecExpr(AddRecOps, AddRecLoop, Flags);
2931
2932 // If all of the other operands were loop invariant, we are done.
2933 if (Ops.size() == 1) return NewRec;
2934
2935 // Otherwise, add the folded AddRec by the non-invariant parts.
2936 for (unsigned i = 0;; ++i)
2937 if (Ops[i] == AddRec) {
2938 Ops[i] = NewRec;
2939 break;
2940 }
2941 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2942 }
2943
2944 // Okay, if there weren't any loop invariants to be folded, check to see if
2945 // there are multiple AddRec's with the same loop induction variable being
2946 // added together. If so, we can fold them.
2947 for (unsigned OtherIdx = Idx+1;
2948 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2949 ++OtherIdx) {
2950 // We expect the AddRecExpr's to be sorted in reverse dominance order,
2951 // so that the 1st found AddRecExpr is dominated by all others.
2952 assert(DT.dominates(
2953 cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),
2954 AddRec->getLoop()->getHeader()) &&
2955 "AddRecExprs are not sorted in reverse dominance order?");
2956 if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2957 // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L>
2958 SmallVector<SCEVUse, 4> AddRecOps(AddRec->operands());
2959 for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2960 ++OtherIdx) {
2961 const auto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2962 if (OtherAddRec->getLoop() == AddRecLoop) {
2963 for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2964 i != e; ++i) {
2965 if (i >= AddRecOps.size()) {
2966 append_range(AddRecOps, OtherAddRec->operands().drop_front(i));
2967 break;
2968 }
2969 AddRecOps[i] =
2970 getAddExpr(AddRecOps[i], OtherAddRec->getOperand(i),
2972 }
2973 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2974 }
2975 }
2976 // Step size has changed, so we cannot guarantee no self-wraparound.
2977 Ops[Idx] = getAddRecExpr(AddRecOps, AddRecLoop, SCEV::FlagAnyWrap);
2978 return getAddExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
2979 }
2980 }
2981
2982 // Otherwise couldn't fold anything into this recurrence. Move onto the
2983 // next one.
2984 }
2985
2986 // Okay, it looks like we really DO need an add expr. Check to see if we
2987 // already have one, otherwise create a new one.
2988 assert((UseFlags == SCEV::FlagAnyWrap || equal(OrigOps, Ops)) &&
2989 "Tried to add SCEVUse flags after operands changed");
2990 return {getOrCreateAddExpr(Ops, ComputeFlags(Ops)), UseFlags};
2991}
2992
2993const SCEV *ScalarEvolution::getOrCreateAddExpr(ArrayRef<SCEVUse> Ops,
2994 SCEV::NoWrapFlags Flags) {
2997 for (SCEVUse Op : Ops)
2998 ID.AddPointer(Op.getOpaqueValue());
3000 SCEVAddExpr *S = static_cast<SCEVAddExpr *>(UniqueSCEVs.lookup(ID, Token));
3001 if (!S) {
3002 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Ops.size());
3004 S = new (SCEVAllocator)
3005 SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
3006 UniqueSCEVs.insert(S, Token);
3007 S->computeAndSetCanonical(*this);
3008 registerUser(S, Ops);
3009 }
3010 S->setNoWrapFlags(Flags);
3011 return S;
3012}
3013
3014const SCEV *ScalarEvolution::getOrCreateAddRecExpr(ArrayRef<SCEVUse> Ops,
3015 const Loop *L,
3016 SCEV::NoWrapFlags Flags) {
3017 FoldingSetNodeID ID;
3018 ID.AddInteger(scAddRecExpr);
3019 for (SCEVUse Op : Ops)
3020 ID.AddPointer(Op.getOpaqueValue());
3021 ID.AddPointer(L);
3022 FoldingSetInsertToken Token;
3023 SCEVAddRecExpr *S =
3024 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.lookup(ID, Token));
3025 if (!S) {
3026 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Ops.size());
3028 S = new (SCEVAllocator)
3029 SCEVAddRecExpr(ID.Intern(SCEVAllocator), O, Ops.size(), L);
3030 UniqueSCEVs.insert(S, Token);
3031 S->computeAndSetCanonical(*this);
3032 LoopUsers[L].push_back(S);
3033 registerUser(S, Ops);
3034 }
3035 setNoWrapFlags(S, Flags);
3036 return S;
3037}
3038
3039const SCEV *ScalarEvolution::getOrCreateMulExpr(ArrayRef<SCEVUse> Ops,
3040 SCEV::NoWrapFlags Flags) {
3041 FoldingSetNodeID ID;
3042 ID.AddInteger(scMulExpr);
3043 for (SCEVUse Op : Ops)
3044 ID.AddPointer(Op.getOpaqueValue());
3045 FoldingSetInsertToken Token;
3046 SCEVMulExpr *S = static_cast<SCEVMulExpr *>(UniqueSCEVs.lookup(ID, Token));
3047 if (!S) {
3048 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Ops.size());
3050 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(SCEVAllocator),
3051 O, Ops.size());
3052 UniqueSCEVs.insert(S, Token);
3053 S->computeAndSetCanonical(*this);
3054 registerUser(S, Ops);
3055 }
3056 S->setNoWrapFlags(Flags);
3057 return S;
3058}
3059
3060const SCEV *ScalarEvolution::getOrCreateUDivExpr(SCEVUse LHS, SCEVUse RHS) {
3061 FoldingSetNodeID ID;
3062 ID.AddInteger(scUDivExpr);
3063 ID.AddPointer(LHS.getOpaqueValue());
3064 ID.AddPointer(RHS.getOpaqueValue());
3065 FoldingSetInsertToken Token;
3066 SCEV *S = UniqueSCEVs.lookup(ID, Token);
3067 if (!S) {
3068 S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(SCEVAllocator), LHS, RHS);
3069 UniqueSCEVs.insert(S, Token);
3070 S->computeAndSetCanonical(*this);
3071 registerUser(S, {LHS, RHS});
3072 }
3073 return S;
3074}
3075
3076static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
3077 uint64_t k = i*j;
3078 if (j > 1 && k / j != i) Overflow = true;
3079 return k;
3080}
3081
3082/// Compute the result of "n choose k", the binomial coefficient. If an
3083/// intermediate computation overflows, Overflow will be set and the return will
3084/// be garbage. Overflow is not cleared on absence of overflow.
3085static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
3086 // We use the multiplicative formula:
3087 // n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
3088 // At each iteration, we take the n-th term of the numeral and divide by the
3089 // (k-n)th term of the denominator. This division will always produce an
3090 // integral result, and helps reduce the chance of overflow in the
3091 // intermediate computations. However, we can still overflow even when the
3092 // final result would fit.
3093
3094 if (n == 0 || n == k) return 1;
3095 if (k > n) return 0;
3096
3097 if (k > n/2)
3098 k = n-k;
3099
3100 uint64_t r = 1;
3101 for (uint64_t i = 1; i <= k; ++i) {
3102 r = umul_ov(r, n-(i-1), Overflow);
3103 r /= i;
3104 }
3105 return r;
3106}
3107
3108/// Determine if any of the operands in this SCEV are a constant or if
3109/// any of the add or multiply expressions in this SCEV contain a constant.
3110static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
3111 struct FindConstantInAddMulChain {
3112 bool FoundConstant = false;
3113
3114 bool follow(const SCEV *S) {
3115 FoundConstant |= isa<SCEVConstant>(S);
3116 return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S);
3117 }
3118
3119 bool isDone() const {
3120 return FoundConstant;
3121 }
3122 };
3123
3124 FindConstantInAddMulChain F;
3126 ST.visitAll(StartExpr);
3127 return F.FoundConstant;
3128}
3129
3130/// Get a canonical multiply expression, or something simpler if possible.
3132 SCEVFlags Flags, unsigned Depth) {
3133 SCEV::NoWrapFlags OrigFlags = Flags.ExprFlags;
3134 SCEV::NoWrapFlags UseFlags = Flags.UseFlags;
3135 assert(OrigFlags == maskFlags(OrigFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
3136 "only nuw or nsw allowed");
3137 assert(UseFlags == maskFlags(UseFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
3138 "only nuw or nsw allowed");
3139 assert(!Ops.empty() && "Cannot get empty mul!");
3140 if (Ops.size() == 1) return Ops[0];
3141#ifndef NDEBUG
3142 Type *ETy = Ops[0]->getType();
3143 assert(!ETy->isPointerTy());
3144 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
3145 assert(Ops[i]->getType() == ETy &&
3146 "SCEVMulExpr operand types don't match!");
3147#endif
3148
3149 const SCEV *Folded = constantFoldAndGroupOps(
3150 *this, LI, DT, Ops,
3151 [](const APInt &C1, const APInt &C2) { return C1 * C2; },
3152 [](const APInt &C) { return C.isOne(); }, // identity
3153 [](const APInt &C) { return C.isZero(); }); // absorber
3154 if (Folded)
3155 return Folded;
3156
3157#ifndef NDEBUG
3158 // Keep track of operands after constant folding, for verification when adding
3159 // use-specific flags.
3160 const SmallVector<SCEVUse, 8> OrigOps(Ops.begin(), Ops.end());
3161#endif
3162
3163 // Delay expensive flag strengthening until necessary.
3164 auto ComputeFlags = [this, OrigFlags](const ArrayRef<SCEVUse> Ops) {
3165 return StrengthenNoWrapFlags(this, scMulExpr, Ops, OrigFlags);
3166 };
3167
3168 // Limit recursion calls depth.
3170 return {getOrCreateMulExpr(Ops, ComputeFlags(Ops)), UseFlags};
3171
3172 if (SCEV *S = findExistingSCEVInCache(scMulExpr, Ops)) {
3173 // Don't strengthen flags if we have no new information.
3174 SCEVMulExpr *Mul = static_cast<SCEVMulExpr *>(S);
3175 if (Mul->getNoWrapFlags(OrigFlags) != OrigFlags)
3176 Mul->setNoWrapFlags(ComputeFlags(Ops));
3177 return {S, UseFlags};
3178 }
3179
3180 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3181 if (Ops.size() == 2) {
3182 // C1*(C2+V) -> C1*C2 + C1*V
3183 // If any of Add's ops are Adds or Muls with a constant, apply this
3184 // transformation as well.
3185 //
3186 // TODO: There are some cases where this transformation is not
3187 // profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of
3188 // this transformation should be narrowed down.
3189 const SCEV *Op0, *Op1;
3190 if (match(Ops[1], m_scev_Add(m_SCEV(Op0), m_SCEV(Op1))) &&
3192 const SCEV *LHS = getMulExpr(LHSC, Op0, SCEV::FlagAnyWrap, Depth + 1);
3193 const SCEV *RHS = getMulExpr(LHSC, Op1, SCEV::FlagAnyWrap, Depth + 1);
3194 return getAddExpr(LHS, RHS, SCEV::FlagAnyWrap, Depth + 1);
3195 }
3196
3197 if (Ops[0]->isAllOnesValue()) {
3198 // If we have a mul by -1 of an add, try distributing the -1 among the
3199 // add operands.
3200 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
3202 bool AnyFolded = false;
3203 for (const SCEV *AddOp : Add->operands()) {
3204 const SCEV *Mul = getMulExpr(Ops[0], SCEVUse(AddOp),
3206 if (!isa<SCEVMulExpr>(Mul)) AnyFolded = true;
3207 NewOps.push_back(Mul);
3208 }
3209 if (AnyFolded)
3210 return getAddExpr(NewOps, SCEV::FlagAnyWrap, Depth + 1);
3211 } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
3212 // Negation preserves a recurrence's no self-wrap property.
3214 for (const SCEV *AddRecOp : AddRec->operands())
3215 Operands.push_back(getMulExpr(Ops[0], SCEVUse(AddRecOp),
3216 SCEV::FlagAnyWrap, Depth + 1));
3217 // Let M be the minimum representable signed value. AddRec with nsw
3218 // multiplied by -1 can have signed overflow if and only if it takes a
3219 // value of M: M * (-1) would stay M and (M + 1) * (-1) would be the
3220 // maximum signed value. In all other cases signed overflow is
3221 // impossible.
3222 auto FlagsMask = SCEV::FlagNW;
3223 if (AddRec->hasNoSignedWrap()) {
3224 auto MinInt =
3225 APInt::getSignedMinValue(getTypeSizeInBits(AddRec->getType()));
3226 if (getSignedRangeMin(AddRec) != MinInt)
3227 FlagsMask = setFlags(FlagsMask, SCEV::FlagNSW);
3228 }
3229 return getAddRecExpr(Operands, AddRec->getLoop(),
3230 AddRec->getNoWrapFlags(FlagsMask));
3231 }
3232 }
3233
3234 // Try to push the constant operand into a ZExt: C * zext (A + B) ->
3235 // zext (C*A + C*B) if trunc (C) * (A + B) does not unsigned-wrap.
3236 const SCEVAddExpr *InnerAdd;
3237 if (match(Ops[1], m_scev_ZExt(m_scev_Add(InnerAdd)))) {
3238 const SCEV *NarrowC = getTruncateExpr(LHSC, InnerAdd->getType());
3239 if (isa<SCEVConstant>(InnerAdd->getOperand(0)) &&
3240 getZeroExtendExpr(NarrowC, Ops[1]->getType()) == LHSC &&
3241 hasFlags(StrengthenNoWrapFlags(this, scMulExpr, {NarrowC, InnerAdd},
3243 SCEV::FlagNUW)) {
3244 const SCEV *Res =
3245 getMulExpr(NarrowC, InnerAdd, SCEV::FlagNUW, Depth + 1);
3246 return getZeroExtendExpr(Res, Ops[1]->getType(), Depth + 1);
3247 };
3248 }
3249
3250 // Try to fold (C1 * D /u C2) -> C1/C2 * D, if C1 and C2 are powers-of-2,
3251 // D is a multiple of C2, and C1 is a multiple of C2. If C2 is a multiple
3252 // of C1, fold to (D /u (C2 /u C1)).
3253 const SCEV *D;
3254 APInt C1V = LHSC->getAPInt();
3255 // (C1 * D /u C2) == -1 * -C1 * D /u C2 when C1 != INT_MIN. Don't treat -1
3256 // as -1 * 1, as it won't enable additional folds.
3257 if (C1V.isNegative() && !C1V.isMinSignedValue() && !C1V.isAllOnes())
3258 C1V = C1V.abs();
3259 const SCEVConstant *C2;
3260 if (C1V.isPowerOf2() &&
3262 C2->getAPInt().isPowerOf2() &&
3263 C1V.logBase2() <= getMinTrailingZeros(D)) {
3264 const SCEV *NewMul = nullptr;
3265 if (C1V.uge(C2->getAPInt())) {
3266 NewMul = getMulExpr(getUDivExpr(getConstant(C1V), C2), D);
3267 } else if (C2->getAPInt().logBase2() <= getMinTrailingZeros(D)) {
3268 assert(C1V.ugt(1) && "C1 <= 1 should have been folded earlier");
3269 NewMul = getUDivExpr(D, getUDivExpr(C2, getConstant(C1V)));
3270 }
3271 if (NewMul)
3272 return C1V == LHSC->getAPInt() ? NewMul : getNegativeSCEV(NewMul);
3273 }
3274 }
3275 }
3276
3277 // Skip over the add expression until we get to a multiply.
3278 unsigned Idx = 0;
3279 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
3280 ++Idx;
3281
3282 // If there are mul operands inline them all into this expression.
3283 if (Idx < Ops.size()) {
3284 bool DeletedMul = false;
3285 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
3286 if (Ops.size() > MulOpsInlineThreshold)
3287 break;
3288 // If we have an mul, expand the mul operands onto the end of the
3289 // operands list.
3290 Ops.erase(Ops.begin()+Idx);
3291 append_range(Ops, Mul->operands());
3292 DeletedMul = true;
3293 }
3294
3295 // If we deleted at least one mul, we added operands to the end of the
3296 // list, and they are not necessarily sorted. Recurse to resort and
3297 // resimplify any operands we just acquired.
3298 if (DeletedMul)
3299 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3300 }
3301
3302 // If there are any add recurrences in the operands list, see if any other
3303 // added values are loop invariant. If so, we can fold them into the
3304 // recurrence.
3305 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
3306 ++Idx;
3307
3308 // Scan over all recurrences, trying to fold loop invariants into them.
3309 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
3310 // Scan all of the other operands to this mul and add them to the vector
3311 // if they are loop invariant w.r.t. the recurrence.
3313 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
3314 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3315 if (isAvailableAtLoopEntry(Ops[i], AddRec->getLoop())) {
3316 LIOps.push_back(Ops[i]);
3317 Ops.erase(Ops.begin()+i);
3318 --i; --e;
3319 }
3320
3321 // If we found some loop invariants, fold them into the recurrence.
3322 if (!LIOps.empty()) {
3323 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
3325 NewOps.reserve(AddRec->getNumOperands());
3326 const SCEV *Scale = getMulExpr(LIOps, SCEV::FlagAnyWrap, Depth + 1);
3327
3328 // If both the mul and addrec are nuw, we can preserve nuw.
3329 // If both the mul and addrec are nsw, we can only preserve nsw if either
3330 // a) they are also nuw, or
3331 // b) all multiplications of addrec operands with scale are nsw.
3332 SCEV::NoWrapFlags Flags =
3333 AddRec->getNoWrapFlags(ComputeFlags({Scale, AddRec}));
3334
3335 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
3336 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i),
3337 SCEV::FlagAnyWrap, Depth + 1));
3338
3339 if (hasFlags(Flags, SCEV::FlagNSW) && !hasFlags(Flags, SCEV::FlagNUW)) {
3341 Instruction::Mul, getSignedRange(Scale),
3343 if (!NSWRegion.contains(getSignedRange(AddRec->getOperand(i))))
3344 Flags = clearFlags(Flags, SCEV::FlagNSW);
3345 }
3346 }
3347
3348 const SCEV *NewRec = getAddRecExpr(NewOps, AddRec->getLoop(), Flags);
3349
3350 // If all of the other operands were loop invariant, we are done.
3351 if (Ops.size() == 1) return NewRec;
3352
3353 // Otherwise, multiply the folded AddRec by the non-invariant parts.
3354 for (unsigned i = 0;; ++i)
3355 if (Ops[i] == AddRec) {
3356 Ops[i] = NewRec;
3357 break;
3358 }
3359 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3360 }
3361
3362 // Okay, if there weren't any loop invariants to be folded, check to see
3363 // if there are multiple AddRec's with the same loop induction variable
3364 // being multiplied together. If so, we can fold them.
3365
3366 // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
3367 // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
3368 // choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
3369 // ]]],+,...up to x=2n}.
3370 // Note that the arguments to choose() are always integers with values
3371 // known at compile time, never SCEV objects.
3372 //
3373 // The implementation avoids pointless extra computations when the two
3374 // addrec's are of different length (mathematically, it's equivalent to
3375 // an infinite stream of zeros on the right).
3376 bool OpsModified = false;
3377 for (unsigned OtherIdx = Idx+1;
3378 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
3379 ++OtherIdx) {
3380 const SCEVAddRecExpr *OtherAddRec =
3381 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
3382 if (!OtherAddRec || OtherAddRec->getLoop() != AddRec->getLoop())
3383 continue;
3384
3385 // Limit max number of arguments to avoid creation of unreasonably big
3386 // SCEVAddRecs with very complex operands.
3387 if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
3388 MaxAddRecSize || hasHugeExpression({AddRec, OtherAddRec}))
3389 continue;
3390
3391 bool Overflow = false;
3392 Type *Ty = AddRec->getType();
3393 bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
3394 SmallVector<SCEVUse, 7> AddRecOps;
3395 for (int x = 0, xe = AddRec->getNumOperands() +
3396 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
3398 for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
3399 uint64_t Coeff1 = Choose(x, 2*x - y, Overflow);
3400 for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
3401 ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
3402 z < ze && !Overflow; ++z) {
3403 uint64_t Coeff2 = Choose(2*x - y, x-z, Overflow);
3404 uint64_t Coeff;
3405 if (LargerThan64Bits)
3406 Coeff = umul_ov(Coeff1, Coeff2, Overflow);
3407 else
3408 Coeff = Coeff1*Coeff2;
3409 const SCEV *CoeffTerm = getConstant(Ty, Coeff);
3410 const SCEV *Term1 = AddRec->getOperand(y-z);
3411 const SCEV *Term2 = OtherAddRec->getOperand(z);
3412 SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2,
3413 SCEV::FlagAnyWrap, Depth + 1));
3414 }
3415 }
3416 if (SumOps.empty())
3417 SumOps.push_back(getZero(Ty));
3418 AddRecOps.push_back(getAddExpr(SumOps, SCEV::FlagAnyWrap, Depth + 1));
3419 }
3420 if (!Overflow) {
3421 const SCEV *NewAddRec = getAddRecExpr(AddRecOps, AddRec->getLoop(),
3423 if (Ops.size() == 2) return NewAddRec;
3424 Ops[Idx] = NewAddRec;
3425 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
3426 OpsModified = true;
3427 AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
3428 if (!AddRec)
3429 break;
3430 }
3431 }
3432 if (OpsModified)
3433 return getMulExpr(Ops, SCEV::FlagAnyWrap, Depth + 1);
3434
3435 // Otherwise couldn't fold anything into this recurrence. Move onto the
3436 // next one.
3437 }
3438
3439 // Okay, it looks like we really DO need an mul expr. Check to see if we
3440 // already have one, otherwise create a new one.
3441 assert((UseFlags == SCEV::FlagAnyWrap || equal(OrigOps, Ops)) &&
3442 "Tried to add SCEVUse flags after operands changed");
3443 return {getOrCreateMulExpr(Ops, ComputeFlags(Ops)), UseFlags};
3444}
3445
3446/// Represents an unsigned remainder expression based on unsigned division.
3448 assert(getEffectiveSCEVType(LHS->getType()) ==
3449 getEffectiveSCEVType(RHS->getType()) &&
3450 "SCEVURemExpr operand types don't match!");
3451
3452 // Short-circuit easy cases
3453 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3454 // If constant is one, the result is trivial
3455 if (RHSC->getValue()->isOne())
3456 return getZero(LHS->getType()); // X urem 1 --> 0
3457
3458 // If constant is a power of two, fold into a zext(trunc(LHS)).
3459 if (RHSC->getAPInt().isPowerOf2()) {
3460 Type *FullTy = LHS->getType();
3461 Type *TruncTy =
3462 IntegerType::get(getContext(), RHSC->getAPInt().logBase2());
3463 return getZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy);
3464 }
3465 }
3466
3467 // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
3468 const SCEV *UDiv = getUDivExpr(LHS, RHS);
3469 const SCEV *Mult = getMulExpr(UDiv, RHS, SCEV::FlagNUW);
3470 return getMinusSCEV(LHS, Mult, SCEV::FlagNUW);
3471}
3472
3473/// Get a canonical unsigned division expression, or something simpler if
3474/// possible.
3476 assert(!LHS->getType()->isPointerTy() &&
3477 "SCEVUDivExpr operand can't be pointer!");
3478 assert(LHS->getType() == RHS->getType() &&
3479 "SCEVUDivExpr operand types don't match!");
3480
3481 if (SCEV *S = findExistingSCEVInCache(scUDivExpr, {LHS, RHS}))
3482 return S;
3483
3484 // 0 udiv Y == 0
3485 if (match(LHS, m_scev_Zero()))
3486 return LHS;
3487
3488 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3489 if (RHSC->getValue()->isOne())
3490 return LHS; // X udiv 1 --> x
3491 // If the denominator is zero, the result of the udiv is undefined. Don't
3492 // try to analyze it, because the resolution chosen here may differ from
3493 // the resolution chosen in other parts of the compiler.
3494 if (!RHSC->getValue()->isZero()) {
3495 // Determine if the division can be folded into the operands of
3496 // its operands.
3497 // TODO: Generalize this to non-constants by using known-bits information.
3498 Type *Ty = LHS->getType();
3499 unsigned LZ = RHSC->getAPInt().countl_zero();
3500 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
3501 // For non-power-of-two values, effectively round the value up to the
3502 // nearest power of two.
3503 if (!RHSC->getAPInt().isPowerOf2())
3504 ++MaxShiftAmt;
3505 IntegerType *ExtTy =
3506 IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
3507 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
3508 if (const SCEVConstant *Step =
3509 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
3510 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
3511 const APInt &StepInt = Step->getAPInt();
3512 const APInt &DivInt = RHSC->getAPInt();
3513 if (!StepInt.urem(DivInt) &&
3514 getZeroExtendExpr(AR, ExtTy) ==
3515 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3516 getZeroExtendExpr(Step, ExtTy),
3517 AR->getLoop(), SCEV::FlagAnyWrap)) {
3519 for (const SCEV *Op : AR->operands())
3520 Operands.push_back(getUDivExpr(Op, RHS));
3521 return getAddRecExpr(Operands, AR->getLoop(), SCEV::FlagNW);
3522 }
3523 /// Get a canonical UDivExpr for a recurrence.
3524 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
3525 const APInt *StartRem;
3526 if (!DivInt.urem(StepInt) && match(getURemExpr(AR->getStart(), Step),
3527 m_scev_APInt(StartRem))) {
3528 bool NoWrap =
3529 getZeroExtendExpr(AR, ExtTy) ==
3530 getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3531 getZeroExtendExpr(Step, ExtTy), AR->getLoop(),
3533
3534 // With N <= C and both N, C as powers-of-2, the transformation
3535 // {X,+,N}/C => {(X - X%N),+,N}/C preserves division results even
3536 // if wrapping occurs, as the division results remain equivalent for
3537 // all offsets in [[(X - X%N), X).
3538 bool CanFoldWithWrap = StepInt.ule(DivInt) && // N <= C
3539 StepInt.isPowerOf2() && DivInt.isPowerOf2();
3540 // Only fold if the subtraction can be folded in the start
3541 // expression.
3542 const SCEV *NewStart =
3543 getMinusSCEV(AR->getStart(), getConstant(*StartRem));
3544 if (*StartRem != 0 && (NoWrap || CanFoldWithWrap) &&
3545 !isa<SCEVAddExpr>(NewStart)) {
3546 const SCEV *NewLHS =
3547 getAddRecExpr(NewStart, Step, AR->getLoop(),
3548 NoWrap ? SCEV::FlagNW : SCEV::FlagAnyWrap);
3549 if (LHS != NewLHS)
3550 return getUDivExpr(NewLHS, RHS);
3551 }
3552 }
3553 }
3554 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
3555 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
3556 if (M->hasNoUnsignedWrap()) {
3557 // Find an operand that's safely divisible.
3558 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
3559 const SCEV *Op = M->getOperand(i);
3560 const SCEV *Div = getUDivExpr(Op, RHSC);
3561 if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
3562 SmallVector<SCEVUse, 4> Operands(M->operands());
3563 Operands[i] = Div;
3564 return getMulExpr(Operands);
3565 }
3566 }
3567
3568 // Even if it's not divisible, try to remove a common factor.
3569 if (const auto *LHSC = dyn_cast<SCEVConstant>(M->getOperand(0))) {
3570 APInt Factor = APIntOps::GreatestCommonDivisor(LHSC->getAPInt(),
3571 RHSC->getAPInt());
3572 if (!Factor.isIntN(1)) {
3573 SmallVector<SCEVUse, 2> NewOperands;
3574 NewOperands.push_back(getConstant(LHSC->getAPInt().udiv(Factor)));
3575 append_range(NewOperands, M->operands().drop_front());
3576 const SCEV *NewMul = getMulExpr(NewOperands);
3577 return getUDivExpr(NewMul,
3578 getConstant(RHSC->getAPInt().udiv(Factor)));
3579 }
3580 }
3581 }
3582 }
3583
3584 // (A/B)/C --> A/(B*C) if safe and B*C can be folded.
3585 if (const SCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(LHS)) {
3586 if (auto *DivisorConstant =
3587 dyn_cast<SCEVConstant>(OtherDiv->getRHS())) {
3588 bool Overflow = false;
3589 APInt NewRHS =
3590 DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
3591 if (Overflow) {
3592 return getConstant(RHSC->getType(), 0, false);
3593 }
3594 return getUDivExpr(OtherDiv->getLHS(), getConstant(NewRHS));
3595 }
3596 }
3597
3598 // (A+B)/C --> (A/C + B/C) if the add does not unsigned wrap and A/C and
3599 // B/C can be folded.
3600 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
3601 if (A->hasNoUnsignedWrap()) {
3603 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
3604 const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
3605 if (isa<SCEVUDivExpr>(Op) ||
3606 getMulExpr(Op, RHS) != A->getOperand(i))
3607 break;
3608 Operands.push_back(Op);
3609 }
3610 if (Operands.size() == A->getNumOperands())
3611 return getAddExpr(Operands);
3612 }
3613 }
3614
3615 // ((N - M) + (M * A)) / N --> ((N - 1) + (M * A)) / N
3616 // This is an idiom for rounding A up to the next multiple of N, where A
3617 // is aready known to be a multiple of M. In this case, instcombine can
3618 // see that some low bits of the added constant are unused, so can clear
3619 // them, but we want to canonicalise to set the low bits. This makes the
3620 // pattern easier to match, without needing to check for known bits in
3621 // A*M.
3622 const APInt &N = RHSC->getAPInt();
3623 const APInt *NMinusM, *M;
3624 const SCEV *A;
3625 if (match(LHS, m_scev_Add(m_scev_APInt(NMinusM),
3626 m_scev_Mul(m_scev_APInt(M), m_SCEV(A))))) {
3627 if (N.isPowerOf2() && M->isPowerOf2() && M->ult(N) &&
3628 *NMinusM == N - *M) {
3629 return getUDivExpr(
3631 RHS);
3632 }
3633 }
3634
3635 // Fold if both operands are constant.
3636 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS))
3637 return getConstant(LHSC->getAPInt().udiv(RHSC->getAPInt()));
3638 }
3639 }
3640
3641 // ((-C + (C smax %x)) /u %x) evaluates to zero, for any positive constant C.
3642 const APInt *NegC, *C;
3643 if (match(LHS,
3646 NegC->isNegative() && !NegC->isMinSignedValue() && *C == -*NegC)
3647 return getZero(LHS->getType());
3648
3649 // (%a * %b)<nuw> / %b -> %a
3650 const auto *Mul = dyn_cast<SCEVMulExpr>(LHS);
3651 if (Mul && Mul->hasNoUnsignedWrap()) {
3652 for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
3653 if (Mul->getOperand(i) == RHS) {
3655 append_range(Operands, Mul->operands().take_front(i));
3656 append_range(Operands, Mul->operands().drop_front(i + 1));
3657 return getMulExpr(Operands);
3658 }
3659 }
3660 }
3661
3662 // TODO: Generalize to handle any common factors.
3663 // udiv (mul nuw a, vscale), (mul nuw b, vscale) --> udiv a, b
3664 const SCEV *NewLHS, *NewRHS;
3665 if (match(LHS, m_scev_c_NUWMul(m_SCEV(NewLHS), m_SCEVVScale())) &&
3666 match(RHS, m_scev_c_NUWMul(m_SCEV(NewRHS), m_SCEVVScale())))
3667 return getUDivExpr(NewLHS, NewRHS);
3668
3669 return getOrCreateUDivExpr(LHS, RHS);
3670}
3671
3672/// Get a canonical unsigned division expression, or something simpler if
3673/// possible. There is no representation for an exact udiv in SCEV IR, but we
3674/// can attempt to optimize it prior to construction.
3676 // Currently there is no exact specific logic.
3677
3678 return getUDivExpr(LHS, RHS);
3679}
3680
3681/// Get an add recurrence expression for the specified loop. Simplify the
3682/// expression as much as possible.
3684 const Loop *L, SCEVFlags Flags) {
3686 Operands.push_back(Start);
3687 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
3688 if (StepChrec->getLoop() == L) {
3689 append_range(Operands, StepChrec->operands());
3690 // The use flags describe the two-operand recurrence, not the flattened
3691 // one built here, so drop them just like the expression's NUW/NSW.
3692 return getAddRecExpr(Operands, L,
3693 maskFlags(Flags.ExprFlags, SCEV::FlagNW));
3694 }
3695
3696 Operands.push_back(Step);
3697 return getAddRecExpr(Operands, L, Flags);
3698}
3699
3700/// Get an add recurrence expression for the specified loop. Simplify the
3701/// expression as much as possible.
3703 const Loop *L, SCEVFlags NWFlags) {
3704 SCEV::NoWrapFlags Flags = NWFlags.ExprFlags;
3705 SCEV::NoWrapFlags UseFlags = NWFlags.UseFlags;
3706 assert(!(UseFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
3707 "only nuw or nsw allowed");
3708 if (Operands.size() == 1) return Operands[0];
3709#ifndef NDEBUG
3711 for (const SCEV *Op : llvm::drop_begin(Operands)) {
3712 assert(getEffectiveSCEVType(Op->getType()) == ETy &&
3713 "SCEVAddRecExpr operand types don't match!");
3714 assert(!Op->getType()->isPointerTy() && "Step must be integer");
3715 }
3716 for (const SCEV *Op : Operands)
3718 "SCEVAddRecExpr operand is not available at loop entry!");
3719
3720 // Keep track of the original operands, for verification when adding
3721 // use-specific flags.
3722 const SmallVector<SCEVUse, 4> OrigOperands(Operands.begin(), Operands.end());
3723#endif
3724
3725 if (Operands.back()->isZero()) {
3726 Operands.pop_back();
3727 return getAddRecExpr(Operands, L, SCEV::FlagAnyWrap); // {X,+,0} --> X
3728 }
3729
3730 // It's tempting to want to call getConstantMaxBackedgeTakenCount count here and
3731 // use that information to infer NUW and NSW flags. However, computing a
3732 // BE count requires calling getAddRecExpr, so we may not yet have a
3733 // meaningful BE count at this point (and if we don't, we'd be stuck
3734 // with a SCEVCouldNotCompute as the cached BE count).
3735
3736 Flags = StrengthenNoWrapFlags(this, scAddRecExpr, Operands, Flags);
3737
3738 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
3739 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
3740 const Loop *NestedLoop = NestedAR->getLoop();
3741 if (L->contains(NestedLoop)
3742 ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
3743 : (!NestedLoop->contains(L) &&
3744 DT.dominates(L->getHeader(), NestedLoop->getHeader()))) {
3745 SmallVector<SCEVUse, 4> NestedOperands(NestedAR->operands());
3746 Operands[0] = NestedAR->getStart();
3747 // AddRecs require their operands be loop-invariant with respect to their
3748 // loops. Don't perform this transformation if it would break this
3749 // requirement.
3750 bool AllInvariant = all_of(
3751 Operands, [&](const SCEV *Op) { return isLoopInvariant(Op, L); });
3752
3753 if (AllInvariant) {
3754 // Create a recurrence for the outer loop with the same step size.
3755 //
3756 // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
3757 // inner recurrence has the same property.
3758 SCEV::NoWrapFlags OuterFlags =
3759 maskFlags(Flags, SCEV::FlagNW | NestedAR->getNoWrapFlags());
3760
3761 NestedOperands[0] = getAddRecExpr(Operands, L, OuterFlags);
3762 AllInvariant = all_of(NestedOperands, [&](const SCEV *Op) {
3763 return isLoopInvariant(Op, NestedLoop);
3764 });
3765
3766 if (AllInvariant) {
3767 // Ok, both add recurrences are valid after the transformation.
3768 //
3769 // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
3770 // the outer recurrence has the same property.
3771 SCEV::NoWrapFlags InnerFlags =
3772 maskFlags(NestedAR->getNoWrapFlags(), SCEV::FlagNW | Flags);
3773 return getAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
3774 }
3775 }
3776 // Reset Operands to its original state.
3777 Operands[0] = NestedAR;
3778 }
3779 }
3780
3781 // Okay, it looks like we really DO need an addrec expr. Check to see if we
3782 // already have one, otherwise create a new one.
3783 assert((UseFlags == SCEV::FlagAnyWrap || equal(OrigOperands, Operands)) &&
3784 "Tried to add SCEVUse flags after operands changed");
3785 return {getOrCreateAddRecExpr(Operands, L, Flags), UseFlags};
3786}
3787
3789 ArrayRef<SCEVUse> IndexExprs) {
3790 const SCEV *BaseExpr = getSCEV(GEP->getPointerOperand());
3791 // getSCEV(Base)->getType() has the same address space as Base->getType()
3792 // because SCEV::getType() preserves the address space.
3793 GEPNoWrapFlags NW = GEP->getNoWrapFlags();
3794 if (NW != GEPNoWrapFlags::none()) {
3795 // We'd like to propagate flags from the IR to the corresponding SCEV nodes,
3796 // but to do that, we have to ensure that said flag is valid in the entire
3797 // defined scope of the SCEV.
3798 // TODO: non-instructions have global scope. We might be able to prove
3799 // some global scope cases
3800 auto *GEPI = dyn_cast<Instruction>(GEP);
3801 if (!GEPI || !isSCEVExprNeverPoison(GEPI))
3802 NW = GEPNoWrapFlags::none();
3803 }
3804
3805 return getGEPExpr(BaseExpr, IndexExprs, GEP->getSourceElementType(), NW);
3806}
3807
3809 ArrayRef<SCEVUse> IndexExprs,
3810 Type *SrcElementTy, GEPNoWrapFlags NW) {
3812 if (NW.hasNoUnsignedSignedWrap())
3813 OffsetWrap = setFlags(OffsetWrap, SCEV::FlagNSW);
3814 if (NW.hasNoUnsignedWrap())
3815 OffsetWrap = setFlags(OffsetWrap, SCEV::FlagNUW);
3816
3817 Type *CurTy = BaseExpr->getType();
3818 Type *IntIdxTy = getEffectiveSCEVType(BaseExpr->getType());
3819 bool FirstIter = true;
3821 for (SCEVUse IndexExpr : IndexExprs) {
3822 // Compute the (potentially symbolic) offset in bytes for this index.
3823 if (StructType *STy = dyn_cast<StructType>(CurTy)) {
3824 // For a struct, add the member offset.
3825 ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
3826 unsigned FieldNo = Index->getZExtValue();
3827 const SCEV *FieldOffset = getOffsetOfExpr(IntIdxTy, STy, FieldNo);
3828 Offsets.push_back(FieldOffset);
3829
3830 // Update CurTy to the type of the field at Index.
3831 CurTy = STy->getTypeAtIndex(Index);
3832 } else {
3833 // Update CurTy to its element type.
3834 if (FirstIter) {
3835 assert(isa<PointerType>(CurTy) &&
3836 "The first index of a GEP indexes a pointer");
3837 CurTy = SrcElementTy;
3838 FirstIter = false;
3839 } else {
3840 CurTy = GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0);
3841 }
3842 // For an array, add the element offset, explicitly scaled.
3843 const SCEV *ElementSize = getSizeOfExpr(IntIdxTy, CurTy);
3844 // Getelementptr indices are signed.
3845 IndexExpr = getTruncateOrSignExtend(IndexExpr, IntIdxTy);
3846
3847 // Multiply the index by the element size to compute the element offset.
3848 const SCEV *LocalOffset = getMulExpr(IndexExpr, ElementSize, OffsetWrap);
3849 Offsets.push_back(LocalOffset);
3850 }
3851 }
3852
3853 // Handle degenerate case of GEP without offsets.
3854 if (Offsets.empty())
3855 return BaseExpr;
3856
3857 // Add the offsets together, assuming nsw if inbounds.
3858 const SCEV *Offset = getAddExpr(Offsets, OffsetWrap);
3859 // Add the base address and the offset. We cannot use the nsw flag, as the
3860 // base address is unsigned. However, if we know that the offset is
3861 // non-negative, we can use nuw.
3862 bool NUW = NW.hasNoUnsignedWrap() ||
3865 const SCEV *GEPExpr = getAddExpr(BaseExpr, Offset, BaseWrap);
3866 assert(BaseExpr->getType() == GEPExpr->getType() &&
3867 "GEP should not change type mid-flight.");
3868 return GEPExpr;
3869}
3870
3871SCEV *ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType,
3874 ID.AddInteger(SCEVType);
3875 for (SCEVUse Op : Ops)
3876 ID.AddPointer(Op.getOpaqueValue());
3878 return UniqueSCEVs.lookup(ID, Token);
3879}
3880
3881const SCEV *ScalarEvolution::getAbsExpr(const SCEV *Op, bool IsNSW) {
3883 return getSMaxExpr(Op, getNegativeSCEV(Op, Flags));
3884}
3885
3888 assert(SCEVMinMaxExpr::isMinMaxType(Kind) && "Not a SCEVMinMaxExpr!");
3889 assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!");
3890 if (Ops.size() == 1) return Ops[0];
3891#ifndef NDEBUG
3892 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
3893 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
3894 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
3895 "Operand types don't match!");
3896 assert(Ops[0]->getType()->isPointerTy() ==
3897 Ops[i]->getType()->isPointerTy() &&
3898 "min/max should be consistently pointerish");
3899 }
3900#endif
3901
3902 bool IsSigned = Kind == scSMaxExpr || Kind == scSMinExpr;
3903 bool IsMax = Kind == scSMaxExpr || Kind == scUMaxExpr;
3904
3905 const SCEV *Folded = constantFoldAndGroupOps(
3906 *this, LI, DT, Ops,
3907 [&](const APInt &C1, const APInt &C2) {
3908 switch (Kind) {
3909 case scSMaxExpr:
3910 return APIntOps::smax(C1, C2);
3911 case scSMinExpr:
3912 return APIntOps::smin(C1, C2);
3913 case scUMaxExpr:
3914 return APIntOps::umax(C1, C2);
3915 case scUMinExpr:
3916 return APIntOps::umin(C1, C2);
3917 default:
3918 llvm_unreachable("Unknown SCEV min/max opcode");
3919 }
3920 },
3921 [&](const APInt &C) {
3922 // identity
3923 if (IsMax)
3924 return IsSigned ? C.isMinSignedValue() : C.isMinValue();
3925 else
3926 return IsSigned ? C.isMaxSignedValue() : C.isMaxValue();
3927 },
3928 [&](const APInt &C) {
3929 // absorber
3930 if (IsMax)
3931 return IsSigned ? C.isMaxSignedValue() : C.isMaxValue();
3932 else
3933 return IsSigned ? C.isMinSignedValue() : C.isMinValue();
3934 });
3935 if (Folded)
3936 return Folded;
3937
3938 // Check if we have created the same expression before.
3939 if (const SCEV *S = findExistingSCEVInCache(Kind, Ops)) {
3940 return S;
3941 }
3942
3943 // Find the first operation of the same kind
3944 unsigned Idx = 0;
3945 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind)
3946 ++Idx;
3947
3948 // Check to see if one of the operands is of the same kind. If so, expand its
3949 // operands onto our operand list, and recurse to simplify.
3950 if (Idx < Ops.size()) {
3951 bool DeletedAny = false;
3952 while (Ops[Idx]->getSCEVType() == Kind) {
3953 const SCEVMinMaxExpr *SMME = cast<SCEVMinMaxExpr>(Ops[Idx]);
3954 Ops.erase(Ops.begin()+Idx);
3955 append_range(Ops, SMME->operands());
3956 DeletedAny = true;
3957 }
3958
3959 if (DeletedAny)
3960 return getMinMaxExpr(Kind, Ops);
3961 }
3962
3963 // Okay, check to see if the same value occurs in the operand list twice. If
3964 // so, delete one. Since we sorted the list, these values are required to
3965 // be adjacent.
3970 llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred;
3971 llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred;
3972 for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) {
3973 if (Ops[i] == Ops[i + 1] ||
3974 isKnownViaNonRecursiveReasoning(FirstPred, Ops[i], Ops[i + 1])) {
3975 // X op Y op Y --> X op Y
3976 // X op Y --> X, if we know X, Y are ordered appropriately
3977 Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2);
3978 --i;
3979 --e;
3980 } else if (isKnownViaNonRecursiveReasoning(SecondPred, Ops[i],
3981 Ops[i + 1])) {
3982 // X op Y --> Y, if we know X, Y are ordered appropriately
3983 Ops.erase(Ops.begin() + i, Ops.begin() + i + 1);
3984 --i;
3985 --e;
3986 }
3987 }
3988
3989 if (Ops.size() == 1) return Ops[0];
3990
3991 assert(!Ops.empty() && "Reduced smax down to nothing!");
3992
3993 // Okay, it looks like we really DO need an expr. Check to see if we
3994 // already have one, otherwise create a new one.
3996 ID.AddInteger(Kind);
3997 for (SCEVUse Op : Ops)
3998 ID.AddPointer(Op.getOpaqueValue());
4000 const SCEV *ExistingSCEV = UniqueSCEVs.lookup(ID, Token);
4001 if (ExistingSCEV)
4002 return ExistingSCEV;
4003 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Ops.size());
4005 SCEV *S = new (SCEVAllocator)
4006 SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
4007
4008 UniqueSCEVs.insert(S, Token);
4009 S->computeAndSetCanonical(*this);
4010 registerUser(S, Ops);
4011 return S;
4012}
4013
4014namespace {
4015
4016class SCEVSequentialMinMaxDeduplicatingVisitor final
4017 : public SCEVVisitor<SCEVSequentialMinMaxDeduplicatingVisitor,
4018 std::optional<const SCEV *>> {
4019 using RetVal = std::optional<const SCEV *>;
4020
4021 ScalarEvolution &SE;
4022 const SCEVTypes RootKind; // Must be a sequential min/max expression.
4023 const SCEVTypes NonSequentialRootKind; // Non-sequential variant of RootKind.
4025
4026 bool canRecurseInto(SCEVTypes Kind) const {
4027 // We can only recurse into the SCEV expression of the same effective type
4028 // as the type of our root SCEV expression.
4029 return RootKind == Kind || NonSequentialRootKind == Kind;
4030 };
4031
4032 RetVal visit(const SCEV *S) {
4033 // Has the whole operand been seen already?
4034 if (!SeenOps.insert(S).second)
4035 return std::nullopt;
4037 SCEVTypes Kind = S->getSCEVType();
4038
4039 if (!canRecurseInto(Kind))
4040 return S;
4041
4042 auto *NAry = cast<SCEVNAryExpr>(S);
4043 SmallVector<SCEVUse> NewOps;
4044 bool Changed = visit(Kind, NAry->operands(), NewOps);
4045
4046 if (!Changed)
4047 return S;
4048 if (NewOps.empty())
4049 return std::nullopt;
4050
4052 ? SE.getSequentialMinMaxExpr(Kind, NewOps)
4053 : SE.getMinMaxExpr(Kind, NewOps);
4054 }
4055 return S;
4056 }
4057
4058public:
4059 SCEVSequentialMinMaxDeduplicatingVisitor(ScalarEvolution &SE,
4060 SCEVTypes RootKind)
4061 : SE(SE), RootKind(RootKind),
4062 NonSequentialRootKind(
4063 SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(
4064 RootKind)) {}
4065
4066 bool /*Changed*/ visit(SCEVTypes Kind, ArrayRef<SCEVUse> OrigOps,
4067 SmallVectorImpl<SCEVUse> &NewOps) {
4068 bool Changed = false;
4070 Ops.reserve(OrigOps.size());
4071
4072 for (const SCEV *Op : OrigOps) {
4073 RetVal NewOp = visit(Op);
4074 if (NewOp != Op)
4075 Changed = true;
4076 if (NewOp)
4077 Ops.emplace_back(*NewOp);
4078 }
4079
4080 if (Changed)
4081 NewOps = std::move(Ops);
4082 return Changed;
4083 }
4084};
4085
4086} // namespace
4087
4089 switch (Kind) {
4090 case scConstant:
4091 case scVScale:
4092 case scTruncate:
4093 case scZeroExtend:
4094 case scSignExtend:
4095 case scPtrToAddr:
4096 case scAddExpr:
4097 case scMulExpr:
4098 case scUDivExpr:
4099 case scAddRecExpr:
4100 case scUMaxExpr:
4101 case scSMaxExpr:
4102 case scUMinExpr:
4103 case scSMinExpr:
4104 case scUnknown:
4105 // If any operand is poison, the whole expression is poison.
4106 return true;
4108 // FIXME: if the *first* operand is poison, the whole expression is poison.
4109 return false; // Pessimistically, say that it does not propagate poison.
4110 case scCouldNotCompute:
4111 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
4112 }
4113 llvm_unreachable("Unknown SCEV kind!");
4114}
4115
4116namespace {
4117// The only way poison may be introduced in a SCEV expression is from a
4118// poison SCEVUnknown (ConstantExprs are also represented as SCEVUnknown,
4119// not SCEVConstant). Notably, nowrap flags in SCEV nodes can *not*
4120// introduce poison -- they encode guaranteed, non-speculated knowledge.
4121//
4122// Additionally, all SCEV nodes propagate poison from inputs to outputs,
4123// with the notable exception of umin_seq, where only poison from the first
4124// operand is (unconditionally) propagated.
4125struct SCEVPoisonCollector {
4126 bool LookThroughMaybePoisonBlocking;
4127 SmallPtrSet<const SCEVUnknown *, 4> MaybePoison;
4128 SCEVPoisonCollector(bool LookThroughMaybePoisonBlocking)
4129 : LookThroughMaybePoisonBlocking(LookThroughMaybePoisonBlocking) {}
4130
4131 bool follow(const SCEV *S) {
4132 if (!LookThroughMaybePoisonBlocking &&
4134 return false;
4135
4136 if (auto *SU = dyn_cast<SCEVUnknown>(S)) {
4137 if (!isGuaranteedNotToBePoison(SU->getValue()))
4138 MaybePoison.insert(SU);
4139 }
4140 return true;
4141 }
4142 bool isDone() const { return false; }
4143};
4144} // namespace
4145
4146/// Return true if V is poison given that AssumedPoison is already poison.
4147static bool impliesPoison(const SCEV *AssumedPoison, const SCEV *S) {
4148 // First collect all SCEVs that might result in AssumedPoison to be poison.
4149 // We need to look through potentially poison-blocking operations here,
4150 // because we want to find all SCEVs that *might* result in poison, not only
4151 // those that are *required* to.
4152 SCEVPoisonCollector PC1(/* LookThroughMaybePoisonBlocking */ true);
4153 visitAll(AssumedPoison, PC1);
4154
4155 // AssumedPoison is never poison. As the assumption is false, the implication
4156 // is true. Don't bother walking the other SCEV in this case.
4157 if (PC1.MaybePoison.empty())
4158 return true;
4159
4160 // Collect all SCEVs in S that, if poison, *will* result in S being poison
4161 // as well. We cannot look through potentially poison-blocking operations
4162 // here, as their arguments only *may* make the result poison.
4163 SCEVPoisonCollector PC2(/* LookThroughMaybePoisonBlocking */ false);
4164 visitAll(S, PC2);
4165
4166 // Make sure that no matter which SCEV in PC1.MaybePoison is actually poison,
4167 // it will also make S poison by being part of PC2.MaybePoison.
4168 return llvm::set_is_subset(PC1.MaybePoison, PC2.MaybePoison);
4169}
4170
4172 SmallPtrSetImpl<const Value *> &Result, const SCEV *S) {
4173 SCEVPoisonCollector PC(/* LookThroughMaybePoisonBlocking */ false);
4174 visitAll(S, PC);
4175 for (const SCEVUnknown *SU : PC.MaybePoison)
4176 Result.insert(SU->getValue());
4177}
4178
4180 const SCEV *S, Instruction *I,
4181 SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts) {
4182 // If the instruction cannot be poison, it's always safe to reuse.
4184 return true;
4185
4186 // Otherwise, it is possible that I is more poisonous that S. Collect the
4187 // poison-contributors of S, and then check whether I has any additional
4188 // poison-contributors. Poison that is contributed through poison-generating
4189 // flags is handled by dropping those flags instead.
4191 getPoisonGeneratingValues(PoisonVals, S);
4192
4193 SmallVector<Value *> Worklist;
4195 Worklist.push_back(I);
4196 while (!Worklist.empty()) {
4197 Value *V = Worklist.pop_back_val();
4198 if (!Visited.insert(V).second)
4199 continue;
4200
4201 // Avoid walking large instruction graphs.
4202 if (Visited.size() > 16)
4203 return false;
4204
4205 // Either the value can't be poison, or the S would also be poison if it
4206 // is.
4207 if (PoisonVals.contains(V) || ::isGuaranteedNotToBePoison(V))
4208 continue;
4209
4210 auto *I = dyn_cast<Instruction>(V);
4211 if (!I)
4212 return false;
4213
4214 // Disjoint or instructions are interpreted as adds by SCEV. However, we
4215 // can't replace an arbitrary add with disjoint or, even if we drop the
4216 // flag. We would need to convert the or into an add.
4217 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(I))
4218 if (PDI->isDisjoint())
4219 return false;
4220
4221 // FIXME: Ignore vscale, even though it technically could be poison. Do this
4222 // because SCEV currently assumes it can't be poison. Remove this special
4223 // case once we proper model when vscale can be poison.
4224 if (auto *II = dyn_cast<IntrinsicInst>(I);
4225 II && II->getIntrinsicID() == Intrinsic::vscale)
4226 continue;
4227
4228 if (canCreatePoison(cast<Operator>(I), /*ConsiderFlagsAndMetadata*/ false))
4229 return false;
4230
4231 // If the instruction can't create poison, we can recurse to its operands.
4232 if (I->hasPoisonGeneratingAnnotations())
4233 DropPoisonGeneratingInsts.push_back(I);
4234
4235 llvm::append_range(Worklist, I->operands());
4236 }
4237 return true;
4238}
4239
4240const SCEV *
4243 assert(SCEVSequentialMinMaxExpr::isSequentialMinMaxType(Kind) &&
4244 "Not a SCEVSequentialMinMaxExpr!");
4245 assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!");
4246 if (Ops.size() == 1)
4247 return Ops[0];
4248#ifndef NDEBUG
4249 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
4250 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
4251 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
4252 "Operand types don't match!");
4253 assert(Ops[0]->getType()->isPointerTy() ==
4254 Ops[i]->getType()->isPointerTy() &&
4255 "min/max should be consistently pointerish");
4256 }
4257#endif
4258
4259 // Note that SCEVSequentialMinMaxExpr is *NOT* commutative,
4260 // so we can *NOT* do any kind of sorting of the expressions!
4261
4262 // Check if we have created the same expression before.
4263 if (const SCEV *S = findExistingSCEVInCache(Kind, Ops))
4264 return S;
4265
4266 // FIXME: there are *some* simplifications that we can do here.
4267
4268 // Keep only the first instance of an operand.
4269 {
4270 SCEVSequentialMinMaxDeduplicatingVisitor Deduplicator(*this, Kind);
4271 bool Changed = Deduplicator.visit(Kind, Ops, Ops);
4272 if (Changed)
4273 return getSequentialMinMaxExpr(Kind, Ops);
4274 }
4275
4276 // Check to see if one of the operands is of the same kind. If so, expand its
4277 // operands onto our operand list, and recurse to simplify.
4278 {
4279 unsigned Idx = 0;
4280 bool DeletedAny = false;
4281 while (Idx < Ops.size()) {
4282 if (Ops[Idx]->getSCEVType() != Kind) {
4283 ++Idx;
4284 continue;
4285 }
4286 const auto *SMME = cast<SCEVSequentialMinMaxExpr>(Ops[Idx]);
4287 Ops.erase(Ops.begin() + Idx);
4288 Ops.insert(Ops.begin() + Idx, SMME->operands().begin(),
4289 SMME->operands().end());
4290 DeletedAny = true;
4291 }
4292
4293 if (DeletedAny)
4294 return getSequentialMinMaxExpr(Kind, Ops);
4295 }
4296
4297 const SCEV *SaturationPoint;
4299 switch (Kind) {
4301 SaturationPoint = getZero(Ops[0]->getType());
4302 Pred = ICmpInst::ICMP_ULE;
4303 break;
4304 default:
4305 llvm_unreachable("Not a sequential min/max type.");
4306 }
4307
4308 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
4309 if (!isGuaranteedNotToCauseUB(Ops[i]))
4310 continue;
4311 // We can replace %x umin_seq %y with %x umin %y if either:
4312 // * %y being poison implies %x is also poison.
4313 // * %x cannot be the saturating value (e.g. zero for umin).
4314 if (::impliesPoison(Ops[i], Ops[i - 1]) ||
4315 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_NE, Ops[i - 1],
4316 SaturationPoint)) {
4317 SmallVector<SCEVUse, 2> SeqOps = {Ops[i - 1], Ops[i]};
4318 Ops[i - 1] = getMinMaxExpr(
4320 SeqOps);
4321 Ops.erase(Ops.begin() + i);
4322 return getSequentialMinMaxExpr(Kind, Ops);
4323 }
4324 // Fold %x umin_seq %y to %x if %x ule %y.
4325 // TODO: We might be able to prove the predicate for a later operand.
4326 if (isKnownViaNonRecursiveReasoning(Pred, Ops[i - 1], Ops[i])) {
4327 Ops.erase(Ops.begin() + i);
4328 return getSequentialMinMaxExpr(Kind, Ops);
4329 }
4330 }
4331
4332 // Okay, it looks like we really DO need an expr. Check to see if we
4333 // already have one, otherwise create a new one.
4335 ID.AddInteger(Kind);
4336 for (SCEVUse Op : Ops)
4337 ID.AddPointer(Op.getOpaqueValue());
4339 const SCEV *ExistingSCEV = UniqueSCEVs.lookup(ID, Token);
4340 if (ExistingSCEV)
4341 return ExistingSCEV;
4342
4343 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Ops.size());
4345 SCEV *S = new (SCEVAllocator)
4346 SCEVSequentialMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
4347
4348 UniqueSCEVs.insert(S, Token);
4349 S->computeAndSetCanonical(*this);
4350 registerUser(S, Ops);
4351 return S;
4352}
4353
4358
4362
4367
4371
4376
4380
4382 bool Sequential) {
4383 SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
4384 return getUMinExpr(Ops, Sequential);
4385}
4386
4392
4393const SCEV *
4395 const SCEV *Res = getConstant(IntTy, Size.getKnownMinValue());
4396 if (Size.isScalable())
4397 Res = getMulExpr(Res, getVScale(IntTy));
4398 return Res;
4399}
4400
4402 return getSizeOfExpr(IntTy, getDataLayout().getTypeAllocSize(AllocTy));
4403}
4404
4406 return getSizeOfExpr(IntTy, getDataLayout().getTypeStoreSize(StoreTy));
4407}
4408
4410 StructType *STy,
4411 unsigned FieldNo) {
4412 // We can bypass creating a target-independent constant expression and then
4413 // folding it back into a ConstantInt. This is just a compile-time
4414 // optimization.
4415 const StructLayout *SL = getDataLayout().getStructLayout(STy);
4416 assert(!SL->getSizeInBits().isScalable() &&
4417 "Cannot get offset for structure containing scalable vector types");
4418 return getConstant(IntTy, SL->getElementOffset(FieldNo));
4419}
4420
4422 // Don't attempt to do anything other than create a SCEVUnknown object
4423 // here. createSCEV only calls getUnknown after checking for all other
4424 // interesting possibilities, and any other code that calls getUnknown
4425 // is doing so in order to hide a value from SCEV canonicalization.
4426
4429 ID.AddPointer(V);
4431 if (SCEV *S = UniqueSCEVs.lookup(ID, Token)) {
4432 assert(cast<SCEVUnknown>(S)->getValue() == V &&
4433 "Stale SCEVUnknown in uniquing map!");
4434 return S;
4435 }
4436 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(SCEVAllocator), V, this,
4437 FirstUnknown);
4438 FirstUnknown = cast<SCEVUnknown>(S);
4439 UniqueSCEVs.insert(S, Token);
4440 S->computeAndSetCanonical(*this);
4441 return S;
4442}
4443
4444//===----------------------------------------------------------------------===//
4445// Basic SCEV Analysis and PHI Idiom Recognition Code
4446//
4447
4448/// Test if values of the given type are analyzable within the SCEV
4449/// framework. This primarily includes integer types, and it can optionally
4450/// include pointer types if the ScalarEvolution class has access to
4451/// target-specific information.
4453 // Integers and pointers are always SCEVable.
4454 return Ty->isIntOrPtrTy();
4455}
4456
4457/// Return the size in bits of the specified type, for which isSCEVable must
4458/// return true.
4460 assert(isSCEVable(Ty) && "Type is not SCEVable!");
4461 if (Ty->isPointerTy())
4463 return getDataLayout().getTypeSizeInBits(Ty);
4464}
4465
4466/// Return a type with the same bitwidth as the given type and which represents
4467/// how SCEV will treat the given type, for which isSCEVable must return
4468/// true. For pointer types, this is the pointer index sized integer type.
4470 assert(isSCEVable(Ty) && "Type is not SCEVable!");
4471
4472 if (Ty->isIntegerTy())
4473 return Ty;
4474
4475 // The only other support type is pointer.
4476 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
4477 return getDataLayout().getIndexType(Ty);
4478}
4479
4481 return getTypeSizeInBits(T1) >= getTypeSizeInBits(T2) ? T1 : T2;
4482}
4483
4485 const SCEV *B) {
4486 /// For a valid use point to exist, the defining scope of one operand
4487 /// must dominate the other.
4488 bool PreciseA, PreciseB;
4489 auto *ScopeA = getDefiningScopeBound({A}, PreciseA);
4490 auto *ScopeB = getDefiningScopeBound({B}, PreciseB);
4491 if (!PreciseA || !PreciseB)
4492 // Can't tell.
4493 return false;
4494 return (ScopeA == ScopeB) || DT.dominates(ScopeA, ScopeB) ||
4495 DT.dominates(ScopeB, ScopeA);
4496}
4497
4499 return CouldNotCompute.get();
4500}
4501
4502bool ScalarEvolution::checkValidity(const SCEV *S) const {
4503 bool ContainsNulls = SCEVExprContains(S, [](const SCEV *S) {
4504 auto *SU = dyn_cast<SCEVUnknown>(S);
4505 return SU && SU->getValue() == nullptr;
4506 });
4507
4508 return !ContainsNulls;
4509}
4510
4512 HasRecMapType::iterator I = HasRecMap.find(S);
4513 if (I != HasRecMap.end())
4514 return I->second;
4515
4516 bool FoundAddRec =
4517 SCEVExprContains(S, [](const SCEV *S) { return isa<SCEVAddRecExpr>(S); });
4518 HasRecMap.insert({S, FoundAddRec});
4519 return FoundAddRec;
4520}
4521
4522/// Return the ValueOffsetPair set for \p S. \p S can be represented
4523/// by the value and offset from any ValueOffsetPair in the set.
4524ArrayRef<Value *> ScalarEvolution::getSCEVValues(const SCEV *S) {
4525 ExprValueMapType::iterator SI = ExprValueMap.find_as(S);
4526 if (SI == ExprValueMap.end())
4527 return {};
4528 return SI->second.getArrayRef();
4529}
4530
4531/// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
4532/// cannot be used separately. eraseValueFromMap should be used to remove
4533/// V from ValueExprMap and ExprValueMap at the same time.
4534void ScalarEvolution::eraseValueFromMap(Value *V) {
4535 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
4536 if (I != ValueExprMap.end()) {
4537 auto EVIt = ExprValueMap.find(I->second);
4538 bool Removed = EVIt->second.remove(V);
4539 (void) Removed;
4540 assert(Removed && "Value not in ExprValueMap?");
4541 ValueExprMap.erase(I);
4542 }
4543}
4544
4545void ScalarEvolution::insertValueToMap(Value *V, const SCEV *S) {
4546 // A recursive query may have already computed the SCEV. It should be
4547 // equivalent, but may not necessarily be exactly the same, e.g. due to lazily
4548 // inferred nowrap flags.
4549 auto It = ValueExprMap.find_as(V);
4550 if (It == ValueExprMap.end()) {
4551 ValueExprMap.insert({SCEVCallbackVH(V, this), S});
4552 ExprValueMap[S].insert(V);
4553 }
4554}
4555
4556/// Return an existing SCEV if it exists, otherwise analyze the expression and
4557/// create a new one.
4559 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
4560
4561 if (const SCEV *S = getExistingSCEV(V))
4562 return S;
4563 return createSCEVIter(V);
4564}
4565
4567 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
4568
4569 ValueExprMapType::iterator I = ValueExprMap.find_as(V);
4570 if (I != ValueExprMap.end()) {
4571 const SCEV *S = I->second;
4572 assert(checkValidity(S) &&
4573 "existing SCEV has not been properly invalidated");
4574 return S;
4575 }
4576 return nullptr;
4577}
4578
4579/// Return a SCEV corresponding to -V = -1*V
4581 SCEV::NoWrapFlags Flags) {
4582 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
4583 return getConstant(
4584 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
4585
4586 Type *Ty = V->getType();
4587 Ty = getEffectiveSCEVType(Ty);
4588 return getMulExpr(V, getMinusOne(Ty), Flags);
4589}
4590
4591/// If Expr computes ~A, return A else return nullptr
4592static const SCEV *MatchNotExpr(const SCEV *Expr) {
4593 const SCEV *MulOp;
4594 if (match(Expr, m_scev_Add(m_scev_AllOnes(),
4595 m_scev_Mul(m_scev_AllOnes(), m_SCEV(MulOp)))))
4596 return MulOp;
4597 return nullptr;
4598}
4599
4600/// Return a SCEV corresponding to ~V = -1-V
4602 assert(!V->getType()->isPointerTy() && "Can't negate pointer");
4603
4604 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(V))
4605 return getConstant(
4606 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
4607
4608 // Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y)
4609 if (const SCEVMinMaxExpr *MME = dyn_cast<SCEVMinMaxExpr>(V)) {
4610 auto MatchMinMaxNegation = [&](const SCEVMinMaxExpr *MME) {
4611 SmallVector<SCEVUse, 2> MatchedOperands;
4612 for (const SCEV *Operand : MME->operands()) {
4613 const SCEV *Matched = MatchNotExpr(Operand);
4614 if (!Matched)
4615 return (const SCEV *)nullptr;
4616 MatchedOperands.push_back(Matched);
4617 }
4618 return getMinMaxExpr(SCEVMinMaxExpr::negate(MME->getSCEVType()),
4619 MatchedOperands);
4620 };
4621 if (const SCEV *Replaced = MatchMinMaxNegation(MME))
4622 return Replaced;
4623 }
4624
4625 Type *Ty = V->getType();
4626 Ty = getEffectiveSCEVType(Ty);
4627 return getMinusSCEV(getMinusOne(Ty), V);
4628}
4629
4631 assert(P->getType()->isPointerTy());
4632
4633 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(P)) {
4634 // The base of an AddRec is the first operand.
4635 SmallVector<SCEVUse> Ops{AddRec->operands()};
4636 Ops[0] = removePointerBase(Ops[0]);
4637 // Don't try to transfer nowrap flags for now. We could in some cases
4638 // (for example, if pointer operand of the AddRec is a SCEVUnknown).
4639 return getAddRecExpr(Ops, AddRec->getLoop(), SCEV::FlagAnyWrap);
4640 }
4641 if (auto *Add = dyn_cast<SCEVAddExpr>(P)) {
4642 // The base of an Add is the pointer operand.
4643 SmallVector<SCEVUse> Ops{Add->operands()};
4644 SCEVUse *PtrOp = nullptr;
4645 for (SCEVUse &AddOp : Ops) {
4646 if (AddOp->getType()->isPointerTy()) {
4647 assert(!PtrOp && "Cannot have multiple pointer ops");
4648 PtrOp = &AddOp;
4649 }
4650 }
4651 *PtrOp = removePointerBase(*PtrOp);
4652 // Don't try to transfer nowrap flags for now. We could in some cases
4653 // (for example, if the pointer operand of the Add is a SCEVUnknown).
4654 return getAddExpr(Ops);
4655 }
4656 // Any other expression must be a pointer base.
4657 return getZero(P->getType());
4658}
4659
4661 SCEV::NoWrapFlags Flags,
4662 unsigned Depth) {
4663 // Fast path: X - X --> 0.
4664 if (LHS == RHS)
4665 return getZero(LHS->getType());
4666
4667 // If we subtract two pointers with different pointer bases, bail.
4668 // Eventually, we're going to add an assertion to getMulExpr that we
4669 // can't multiply by a pointer.
4670 if (RHS->getType()->isPointerTy()) {
4671 if (!LHS->getType()->isPointerTy() ||
4672 getPointerBase(LHS) != getPointerBase(RHS))
4673 return getCouldNotCompute();
4674 LHS = removePointerBase(LHS);
4675 RHS = removePointerBase(RHS);
4676 }
4677
4678 // We represent LHS - RHS as LHS + (-1)*RHS. This transformation
4679 // makes it so that we cannot make much use of NUW.
4680 auto AddFlags = SCEV::FlagAnyWrap;
4681 const bool RHSIsNotMinSigned =
4683 if (hasFlags(Flags, SCEV::FlagNSW)) {
4684 // Let M be the minimum representable signed value. Then (-1)*RHS
4685 // signed-wraps if and only if RHS is M. That can happen even for
4686 // a NSW subtraction because e.g. (-1)*M signed-wraps even though
4687 // -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
4688 // (-1)*RHS, we need to prove that RHS != M.
4689 //
4690 // If LHS is non-negative and we know that LHS - RHS does not
4691 // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
4692 // either by proving that RHS > M or that LHS >= 0.
4693 if (RHSIsNotMinSigned || isKnownNonNegative(LHS)) {
4694 AddFlags = SCEV::FlagNSW;
4695 }
4696 }
4697
4698 // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
4699 // RHS is NSW and LHS >= 0.
4700 //
4701 // The difficulty here is that the NSW flag may have been proven
4702 // relative to a loop that is to be found in a recurrence in LHS and
4703 // not in RHS. Applying NSW to (-1)*M may then let the NSW have a
4704 // larger scope than intended.
4705 auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
4706
4707 return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth);
4708}
4709
4711 unsigned Depth) {
4712 Type *SrcTy = V->getType();
4713 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4714 "Cannot truncate or zero extend with non-integer arguments!");
4715 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4716 return V; // No conversion
4717 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
4718 return getTruncateExpr(V, Ty, Depth);
4719 return getZeroExtendExpr(V, Ty, Depth);
4720}
4721
4723 unsigned Depth) {
4724 Type *SrcTy = V->getType();
4725 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4726 "Cannot truncate or zero extend with non-integer arguments!");
4727 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4728 return V; // No conversion
4729 if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
4730 return getTruncateExpr(V, Ty, Depth);
4731 return getSignExtendExpr(V, Ty, Depth);
4732}
4733
4735 Type *SrcTy = V->getType();
4736 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4737 "Cannot noop or zero extend with non-integer arguments!");
4739 "getNoopOrZeroExtend cannot truncate!");
4740 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4741 return V; // No conversion
4742 return getZeroExtendExpr(V, Ty);
4743}
4744
4746 Type *SrcTy = V->getType();
4747 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4748 "Cannot noop or sign extend with non-integer arguments!");
4750 "getNoopOrSignExtend cannot truncate!");
4751 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4752 return V; // No conversion
4753 return getSignExtendExpr(V, Ty);
4754}
4755
4757 Type *SrcTy = V->getType();
4758 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4759 "Cannot noop or any extend with non-integer arguments!");
4761 "getNoopOrAnyExtend cannot truncate!");
4762 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4763 return V; // No conversion
4764 return getAnyExtendExpr(V, Ty);
4765}
4766
4768 Type *SrcTy = V->getType();
4769 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4770 "Cannot truncate or noop with non-integer arguments!");
4772 "getTruncateOrNoop cannot extend!");
4773 if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4774 return V; // No conversion
4775 return getTruncateExpr(V, Ty);
4776}
4777
4779 const SCEV *RHS) {
4780 const SCEV *PromotedLHS = LHS;
4781 const SCEV *PromotedRHS = RHS;
4782
4783 if (getTypeSizeInBits(LHS->getType()) > getTypeSizeInBits(RHS->getType()))
4784 PromotedRHS = getZeroExtendExpr(RHS, LHS->getType());
4785 else
4786 PromotedLHS = getNoopOrZeroExtend(LHS, RHS->getType());
4787
4788 return getUMaxExpr(PromotedLHS, PromotedRHS);
4789}
4790
4792 const SCEV *RHS,
4793 bool Sequential) {
4794 SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
4795 return getUMinFromMismatchedTypes(Ops, Sequential);
4796}
4797
4798const SCEV *
4800 bool Sequential) {
4801 assert(!Ops.empty() && "At least one operand must be!");
4802 // Trivial case.
4803 if (Ops.size() == 1)
4804 return Ops[0];
4805
4806 // Find the max type first.
4807 Type *MaxType = nullptr;
4808 for (SCEVUse S : Ops)
4809 if (MaxType)
4810 MaxType = getWiderType(MaxType, S->getType());
4811 else
4812 MaxType = S->getType();
4813 assert(MaxType && "Failed to find maximum type!");
4814
4815 // Extend all ops to max type.
4816 SmallVector<SCEVUse, 2> PromotedOps;
4817 for (SCEVUse S : Ops)
4818 PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType));
4819
4820 // Generate umin.
4821 return getUMinExpr(PromotedOps, Sequential);
4822}
4823
4825 // A pointer operand may evaluate to a nonpointer expression, such as null.
4826 if (!V->getType()->isPointerTy())
4827 return V;
4828
4829 while (true) {
4830 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
4831 V = AddRec->getStart();
4832 } else if (auto *Add = dyn_cast<SCEVAddExpr>(V)) {
4833 const SCEV *PtrOp = nullptr;
4834 for (const SCEV *AddOp : Add->operands()) {
4835 if (AddOp->getType()->isPointerTy()) {
4836 assert(!PtrOp && "Cannot have multiple pointer ops");
4837 PtrOp = AddOp;
4838 }
4839 }
4840 assert(PtrOp && "Must have pointer op");
4841 V = PtrOp;
4842 } else // Not something we can look further into.
4843 return V;
4844 }
4845}
4846
4847/// Push users of the given Instruction onto the given Worklist.
4851 // Push the def-use children onto the Worklist stack.
4852 for (User *U : I->users()) {
4853 auto *UserInsn = cast<Instruction>(U);
4854 if (Visited.insert(UserInsn).second)
4855 Worklist.push_back(UserInsn);
4856 }
4857}
4858
4859namespace {
4860
4861/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start
4862/// expression in case its Loop is L. If it is not L then
4863/// if IgnoreOtherLoops is true then use AddRec itself
4864/// otherwise rewrite cannot be done.
4865/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4866class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> {
4867public:
4868 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
4869 bool IgnoreOtherLoops = true) {
4870 SCEVInitRewriter Rewriter(L, SE);
4871 const SCEV *Result = Rewriter.visit(S);
4872 if (Rewriter.hasSeenLoopVariantSCEVUnknown())
4873 return SE.getCouldNotCompute();
4874 return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops
4875 ? SE.getCouldNotCompute()
4876 : Result;
4877 }
4878
4879 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4880 if (!SE.isLoopInvariant(Expr, L))
4881 SeenLoopVariantSCEVUnknown = true;
4882 return Expr;
4883 }
4884
4885 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4886 // Only re-write AddRecExprs for this loop.
4887 if (Expr->getLoop() == L)
4888 return Expr->getStart();
4889 SeenOtherLoops = true;
4890 return Expr;
4891 }
4892
4893 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4894
4895 bool hasSeenOtherLoops() { return SeenOtherLoops; }
4896
4897private:
4898 explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE)
4899 : SCEVRewriteVisitor(SE), L(L) {}
4900
4901 const Loop *L;
4902 bool SeenLoopVariantSCEVUnknown = false;
4903 bool SeenOtherLoops = false;
4904};
4905
4906/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post
4907/// increment expression in case its Loop is L. If it is not L then
4908/// use AddRec itself.
4909/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4910class SCEVPostIncRewriter : public SCEVRewriteVisitor<SCEVPostIncRewriter> {
4911public:
4912 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) {
4913 SCEVPostIncRewriter Rewriter(L, SE);
4914 const SCEV *Result = Rewriter.visit(S);
4915 return Rewriter.hasSeenLoopVariantSCEVUnknown()
4916 ? SE.getCouldNotCompute()
4917 : Result;
4918 }
4919
4920 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4921 if (!SE.isLoopInvariant(Expr, L))
4922 SeenLoopVariantSCEVUnknown = true;
4923 return Expr;
4924 }
4925
4926 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4927 // Only re-write AddRecExprs for this loop.
4928 if (Expr->getLoop() == L)
4929 return Expr->getPostIncExpr(SE);
4930 SeenOtherLoops = true;
4931 return Expr;
4932 }
4933
4934 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4935
4936 bool hasSeenOtherLoops() { return SeenOtherLoops; }
4937
4938private:
4939 explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE)
4940 : SCEVRewriteVisitor(SE), L(L) {}
4941
4942 const Loop *L;
4943 bool SeenLoopVariantSCEVUnknown = false;
4944 bool SeenOtherLoops = false;
4945};
4946
4947/// This class evaluates the compare condition by matching it against the
4948/// condition of loop latch. If there is a match we assume a true value
4949/// for the condition while building SCEV nodes.
4950class SCEVBackedgeConditionFolder
4951 : public SCEVRewriteVisitor<SCEVBackedgeConditionFolder> {
4952public:
4953 static const SCEV *rewrite(const SCEV *S, const Loop *L,
4954 ScalarEvolution &SE) {
4955 bool IsPosBECond = false;
4956 Value *BECond = nullptr;
4957 if (BasicBlock *Latch = L->getLoopLatch()) {
4958 if (CondBrInst *BI = dyn_cast<CondBrInst>(Latch->getTerminator())) {
4959 assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
4960 "Both outgoing branches should not target same header!");
4961 BECond = BI->getCondition();
4962 IsPosBECond = BI->getSuccessor(0) == L->getHeader();
4963 } else {
4964 return S;
4965 }
4966 }
4967 SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE);
4968 return Rewriter.visit(S);
4969 }
4970
4971 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4972 const SCEV *Result = Expr;
4973 bool InvariantF = SE.isLoopInvariant(Expr, L);
4974
4975 if (!InvariantF) {
4977 switch (I->getOpcode()) {
4978 case Instruction::Select: {
4979 SelectInst *SI = cast<SelectInst>(I);
4980 std::optional<const SCEV *> Res =
4981 compareWithBackedgeCondition(SI->getCondition());
4982 if (Res) {
4983 bool IsOne = cast<SCEVConstant>(*Res)->getValue()->isOne();
4984 Result = SE.getSCEV(IsOne ? SI->getTrueValue() : SI->getFalseValue());
4985 }
4986 break;
4987 }
4988 default: {
4989 std::optional<const SCEV *> Res = compareWithBackedgeCondition(I);
4990 if (Res)
4991 Result = *Res;
4992 break;
4993 }
4994 }
4995 }
4996 return Result;
4997 }
4998
4999private:
5000 explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond,
5001 bool IsPosBECond, ScalarEvolution &SE)
5002 : SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond),
5003 IsPositiveBECond(IsPosBECond) {}
5004
5005 std::optional<const SCEV *> compareWithBackedgeCondition(Value *IC);
5006
5007 const Loop *L;
5008 /// Loop back condition.
5009 Value *BackedgeCond = nullptr;
5010 /// Set to true if loop back is on positive branch condition.
5011 bool IsPositiveBECond;
5012};
5013
5014std::optional<const SCEV *>
5015SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) {
5016
5017 // If value matches the backedge condition for loop latch,
5018 // then return a constant evolution node based on loopback
5019 // branch taken.
5020 if (BackedgeCond == IC)
5021 return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext()))
5023 return std::nullopt;
5024}
5025
5026class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> {
5027public:
5028 static const SCEV *rewrite(const SCEV *S, const Loop *L,
5029 ScalarEvolution &SE) {
5030 SCEVShiftRewriter Rewriter(L, SE);
5031 const SCEV *Result = Rewriter.visit(S);
5032 return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
5033 }
5034
5035 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
5036 // Only allow AddRecExprs for this loop.
5037 if (!SE.isLoopInvariant(Expr, L))
5038 Valid = false;
5039 return Expr;
5040 }
5041
5042 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
5043 if (Expr->getLoop() == L && Expr->isAffine())
5044 return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE));
5045 Valid = false;
5046 return Expr;
5047 }
5048
5049 bool isValid() { return Valid; }
5050
5051private:
5052 explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE)
5053 : SCEVRewriteVisitor(SE), L(L) {}
5054
5055 const Loop *L;
5056 bool Valid = true;
5057};
5058
5059} // end anonymous namespace
5060
5061void ScalarEvolution::inferNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
5062 if (!AR->isAffine())
5063 return;
5064
5065 // Force computation of ranges, which will also perform range-based flag
5066 // inference.
5067 if (!AR->hasNoSignedWrap())
5068 (void)getSignedRange(AR);
5069
5070 if (!AR->hasNoUnsignedWrap())
5071 (void)getUnsignedRange(AR);
5072
5073 if (!AR->hasNoSelfWrap()) {
5074 const SCEV *BECount = getConstantMaxBackedgeTakenCount(AR->getLoop());
5075 if (const SCEVConstant *BECountMax = dyn_cast<SCEVConstant>(BECount)) {
5076 ConstantRange StepCR = getSignedRange(AR->getStepRecurrence(*this));
5077 const APInt &BECountAP = BECountMax->getAPInt();
5078 unsigned NoOverflowBitWidth =
5079 BECountAP.getActiveBits() + StepCR.getMinSignedBits();
5080 if (NoOverflowBitWidth <= getTypeSizeInBits(AR->getType()))
5081 const_cast<SCEVAddRecExpr *>(AR)->setNoWrapFlags(SCEV::FlagNW);
5082 }
5083 }
5084}
5085
5087ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) {
5089
5090 if (AR->hasNoSignedWrap())
5091 return Result;
5092
5093 if (!AR->isAffine())
5094 return Result;
5095
5096 // This function can be expensive, only try to prove NSW once per AddRec.
5097 if (!SignedWrapViaInductionTried.insert(AR).second)
5098 return Result;
5099
5100 const SCEV *Step = AR->getStepRecurrence(*this);
5101 const Loop *L = AR->getLoop();
5102
5103 // Check whether the backedge-taken count is SCEVCouldNotCompute.
5104 // Note that this serves two purposes: It filters out loops that are
5105 // simply not analyzable, and it covers the case where this code is
5106 // being called from within backedge-taken count analysis, such that
5107 // attempting to ask for the backedge-taken count would likely result
5108 // in infinite recursion. In the later case, the analysis code will
5109 // cope with a conservative value, and it will take care to purge
5110 // that value once it has finished.
5111 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
5112
5113 // Normally, in the cases we can prove no-overflow via a
5114 // backedge guarding condition, we can also compute a backedge
5115 // taken count for the loop. The exceptions are assumptions and
5116 // guards present in the loop -- SCEV is not great at exploiting
5117 // these to compute max backedge taken counts, but can still use
5118 // these to prove lack of overflow. Use this fact to avoid
5119 // doing extra work that may not pay off.
5120
5121 if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards &&
5122 AC.assumptions().empty())
5123 return Result;
5124
5125 // If the backedge is guarded by a comparison with the pre-inc value the
5126 // addrec is safe. Also, if the entry is guarded by a comparison with the
5127 // start value and the backedge is guarded by a comparison with the post-inc
5128 // value, the addrec is safe.
5130 const SCEV *OverflowLimit =
5131 getSignedOverflowLimitForStep(Step, &Pred, this);
5132 if (OverflowLimit &&
5133 (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
5134 isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
5135 Result = setFlags(Result, SCEV::FlagNSW);
5136 }
5137 return Result;
5138}
5140ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) {
5142
5143 if (AR->hasNoUnsignedWrap())
5144 return Result;
5145
5146 if (!AR->isAffine())
5147 return Result;
5148
5149 // This function can be expensive, only try to prove NUW once per AddRec.
5150 if (!UnsignedWrapViaInductionTried.insert(AR).second)
5151 return Result;
5152
5153 const SCEV *Step = AR->getStepRecurrence(*this);
5154 const Loop *L = AR->getLoop();
5155
5156 // Check whether the backedge-taken count is SCEVCouldNotCompute.
5157 // Note that this serves two purposes: It filters out loops that are
5158 // simply not analyzable, and it covers the case where this code is
5159 // being called from within backedge-taken count analysis, such that
5160 // attempting to ask for the backedge-taken count would likely result
5161 // in infinite recursion. In the later case, the analysis code will
5162 // cope with a conservative value, and it will take care to purge
5163 // that value once it has finished.
5164 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
5165
5166 // Normally, in the cases we can prove no-overflow via a
5167 // backedge guarding condition, we can also compute a backedge
5168 // taken count for the loop. The exceptions are assumptions and
5169 // guards present in the loop -- SCEV is not great at exploiting
5170 // these to compute max backedge taken counts, but can still use
5171 // these to prove lack of overflow. Use this fact to avoid
5172 // doing extra work that may not pay off.
5173
5174 if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards &&
5175 AC.assumptions().empty())
5176 return Result;
5177
5178 // If the backedge is guarded by a comparison with the pre-inc value the
5179 // addrec is safe. Also, if the entry is guarded by a comparison with the
5180 // start value and the backedge is guarded by a comparison with the post-inc
5181 // value, the addrec is safe.
5182 if (isKnownPositive(Step)) {
5184 const SCEV *OverflowLimit =
5185 getUnsignedOverflowLimitForStep(Step, &Pred, this);
5186 if (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
5187 isKnownOnEveryIteration(Pred, AR, OverflowLimit))
5188 Result = setFlags(Result, SCEV::FlagNUW);
5189 }
5190 return Result;
5191}
5192
5193namespace {
5194
5195/// Represents an abstract binary operation. This may exist as a
5196/// normal instruction or constant expression, or may have been
5197/// derived from an expression tree.
5198struct BinaryOp {
5199 unsigned Opcode;
5200 Value *LHS;
5201 Value *RHS;
5202 bool IsNSW = false;
5203 bool IsNUW = false;
5204
5205 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
5206 /// constant expression.
5207 Operator *Op = nullptr;
5208
5209 explicit BinaryOp(Operator *Op)
5210 : Opcode(Op->getOpcode()), LHS(Op->getOperand(0)), RHS(Op->getOperand(1)),
5211 Op(Op) {
5212 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
5213 IsNSW = OBO->hasNoSignedWrap();
5214 IsNUW = OBO->hasNoUnsignedWrap();
5215 }
5216 }
5217
5218 explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false,
5219 bool IsNUW = false)
5220 : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
5221};
5222
5223} // end anonymous namespace
5224
5225/// Try to map \p V into a BinaryOp, and return \c std::nullopt on failure.
5226static std::optional<BinaryOp> MatchBinaryOp(Value *V, const DataLayout &DL,
5227 AssumptionCache &AC,
5228 const DominatorTree &DT,
5229 const Instruction *CxtI) {
5230 auto *Op = dyn_cast<Operator>(V);
5231 if (!Op)
5232 return std::nullopt;
5233
5234 // Implementation detail: all the cleverness here should happen without
5235 // creating new SCEV expressions -- our caller knowns tricks to avoid creating
5236 // SCEV expressions when possible, and we should not break that.
5237
5238 switch (Op->getOpcode()) {
5239 case Instruction::Add:
5240 case Instruction::Sub:
5241 case Instruction::Mul:
5242 case Instruction::UDiv:
5243 case Instruction::URem:
5244 case Instruction::And:
5245 case Instruction::AShr:
5246 case Instruction::Shl:
5247 return BinaryOp(Op);
5248
5249 case Instruction::Or: {
5250 // Convert or disjoint into add nuw nsw.
5251 if (cast<PossiblyDisjointInst>(Op)->isDisjoint()) {
5252 BinaryOp BinOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1),
5253 /*IsNSW=*/true, /*IsNUW=*/true);
5254 // Keep the reference to the original instruction so that we can later
5255 // check whether it can produce poison value or not.
5256 BinOp.Op = Op;
5257 return BinOp;
5258 }
5259 return BinaryOp(Op);
5260 }
5261
5262 case Instruction::Xor:
5263 if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1)))
5264 // If the RHS of the xor is a signmask, then this is just an add.
5265 // Instcombine turns add of signmask into xor as a strength reduction step.
5266 if (RHSC->getValue().isSignMask())
5267 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
5268 // Binary `xor` is a bit-wise `add`.
5269 if (V->getType()->isIntegerTy(1))
5270 return BinaryOp(Instruction::Add, Op->getOperand(0), Op->getOperand(1));
5271 return BinaryOp(Op);
5272
5273 case Instruction::LShr:
5274 // Turn logical shift right of a constant into a unsigned divide.
5275 if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
5276 uint32_t BitWidth = cast<IntegerType>(Op->getType())->getBitWidth();
5277
5278 // If the shift count is not less than the bitwidth, the result of
5279 // the shift is undefined. Don't try to analyze it, because the
5280 // resolution chosen here may differ from the resolution chosen in
5281 // other parts of the compiler.
5282 if (SA->getValue().ult(BitWidth)) {
5283 Constant *X =
5284 ConstantInt::get(SA->getContext(),
5285 APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
5286 return BinaryOp(Instruction::UDiv, Op->getOperand(0), X);
5287 }
5288 }
5289 return BinaryOp(Op);
5290
5291 case Instruction::ExtractValue: {
5292 auto *EVI = cast<ExtractValueInst>(Op);
5293 if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
5294 break;
5295
5296 auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand());
5297 if (!WO)
5298 break;
5299
5300 Instruction::BinaryOps BinOp = WO->getBinaryOp();
5301 bool Signed = WO->isSigned();
5302 // TODO: Should add nuw/nsw flags for mul as well.
5303 if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT))
5304 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS());
5305
5306 // Now that we know that all uses of the arithmetic-result component of
5307 // CI are guarded by the overflow check, we can go ahead and pretend
5308 // that the arithmetic is non-overflowing.
5309 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(),
5310 /* IsNSW = */ Signed, /* IsNUW = */ !Signed);
5311 }
5312
5313 default:
5314 break;
5315 }
5316
5317 // Recognise intrinsic loop.decrement.reg, and as this has exactly the same
5318 // semantics as a Sub, return a binary sub expression.
5319 if (auto *II = dyn_cast<IntrinsicInst>(V))
5320 if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg)
5321 return BinaryOp(Instruction::Sub, II->getOperand(0), II->getOperand(1));
5322
5323 return std::nullopt;
5324}
5325
5326/// Helper function to createAddRecFromPHIWithCasts. We have a phi
5327/// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
5328/// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the
5329/// way. This function checks if \p Op, an operand of this SCEVAddExpr,
5330/// follows one of the following patterns:
5331/// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
5332/// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
5333/// If the SCEV expression of \p Op conforms with one of the expected patterns
5334/// we return the type of the truncation operation, and indicate whether the
5335/// truncated type should be treated as signed/unsigned by setting
5336/// \p Signed to true/false, respectively.
5337static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
5338 bool &Signed, ScalarEvolution &SE) {
5339 // The case where Op == SymbolicPHI (that is, with no type conversions on
5340 // the way) is handled by the regular add recurrence creating logic and
5341 // would have already been triggered in createAddRecForPHI. Reaching it here
5342 // means that createAddRecFromPHI had failed for this PHI before (e.g.,
5343 // because one of the other operands of the SCEVAddExpr updating this PHI is
5344 // not invariant).
5345 //
5346 // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in
5347 // this case predicates that allow us to prove that Op == SymbolicPHI will
5348 // be added.
5349 if (Op == SymbolicPHI)
5350 return nullptr;
5351
5352 unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType());
5353 unsigned NewBits = SE.getTypeSizeInBits(Op->getType());
5354 if (SourceBits != NewBits)
5355 return nullptr;
5356
5357 if (match(Op, m_scev_SExt(m_scev_Trunc(m_scev_Specific(SymbolicPHI))))) {
5358 Signed = true;
5359 return cast<SCEVCastExpr>(Op)->getOperand()->getType();
5360 }
5361 if (match(Op, m_scev_ZExt(m_scev_Trunc(m_scev_Specific(SymbolicPHI))))) {
5362 Signed = false;
5363 return cast<SCEVCastExpr>(Op)->getOperand()->getType();
5364 }
5365 return nullptr;
5366}
5367
5368static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
5369 if (!PN->getType()->isIntegerTy())
5370 return nullptr;
5371 const Loop *L = LI.getLoopFor(PN->getParent());
5372 if (!L || L->getHeader() != PN->getParent())
5373 return nullptr;
5374 return L;
5375}
5376
5377// Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
5378// computation that updates the phi follows the following pattern:
5379// (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
5380// which correspond to a phi->trunc->sext/zext->add->phi update chain.
5381// If so, try to see if it can be rewritten as an AddRecExpr under some
5382// Predicates. If successful, return them as a pair. Also cache the results
5383// of the analysis.
5384//
5385// Example usage scenario:
5386// Say the Rewriter is called for the following SCEV:
5387// 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
5388// where:
5389// %X = phi i64 (%Start, %BEValue)
5390// It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
5391// and call this function with %SymbolicPHI = %X.
5392//
5393// The analysis will find that the value coming around the backedge has
5394// the following SCEV:
5395// BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
5396// Upon concluding that this matches the desired pattern, the function
5397// will return the pair {NewAddRec, SmallPredsVec} where:
5398// NewAddRec = {%Start,+,%Step}
5399// SmallPredsVec = {P1, P2, P3} as follows:
5400// P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
5401// P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
5402// P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
5403// The returned pair means that SymbolicPHI can be rewritten into NewAddRec
5404// under the predicates {P1,P2,P3}.
5405// This predicated rewrite will be cached in PredicatedSCEVRewrites:
5406// PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)}
5407//
5408// TODO's:
5409//
5410// 1) Extend the Induction descriptor to also support inductions that involve
5411// casts: When needed (namely, when we are called in the context of the
5412// vectorizer induction analysis), a Set of cast instructions will be
5413// populated by this method, and provided back to isInductionPHI. This is
5414// needed to allow the vectorizer to properly record them to be ignored by
5415// the cost model and to avoid vectorizing them (otherwise these casts,
5416// which are redundant under the runtime overflow checks, will be
5417// vectorized, which can be costly).
5418//
5419// 2) Support additional induction/PHISCEV patterns: We also want to support
5420// inductions where the sext-trunc / zext-trunc operations (partly) occur
5421// after the induction update operation (the induction increment):
5422//
5423// (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
5424// which correspond to a phi->add->trunc->sext/zext->phi update chain.
5425//
5426// (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
5427// which correspond to a phi->trunc->add->sext/zext->phi update chain.
5428//
5429// 3) Outline common code with createAddRecFromPHI to avoid duplication.
5430std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5431ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) {
5433
5434 // *** Part1: Analyze if we have a phi-with-cast pattern for which we can
5435 // return an AddRec expression under some predicate.
5436
5437 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
5438 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
5439 assert(L && "Expecting an integer loop header phi");
5440
5441 // The loop may have multiple entrances or multiple exits; we can analyze
5442 // this phi as an addrec if it has a unique entry value and a unique
5443 // backedge value.
5444 Value *BEValueV = nullptr, *StartValueV = nullptr;
5445 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5446 Value *V = PN->getIncomingValue(i);
5447 if (L->contains(PN->getIncomingBlock(i))) {
5448 if (!BEValueV) {
5449 BEValueV = V;
5450 } else if (BEValueV != V) {
5451 BEValueV = nullptr;
5452 break;
5453 }
5454 } else if (!StartValueV) {
5455 StartValueV = V;
5456 } else if (StartValueV != V) {
5457 StartValueV = nullptr;
5458 break;
5459 }
5460 }
5461 if (!BEValueV || !StartValueV)
5462 return std::nullopt;
5463
5464 const SCEV *BEValue = getSCEV(BEValueV);
5465
5466 // If the value coming around the backedge is an add with the symbolic
5467 // value we just inserted, possibly with casts that we can ignore under
5468 // an appropriate runtime guard, then we found a simple induction variable!
5469 const auto *Add = dyn_cast<SCEVAddExpr>(BEValue);
5470 if (!Add)
5471 return std::nullopt;
5472
5473 // If there is a single occurrence of the symbolic value, possibly
5474 // casted, replace it with a recurrence.
5475 unsigned FoundIndex = Add->getNumOperands();
5476 Type *TruncTy = nullptr;
5477 bool Signed;
5478 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5479 if ((TruncTy =
5480 isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI, Signed, *this)))
5481 if (FoundIndex == e) {
5482 FoundIndex = i;
5483 break;
5484 }
5485
5486 if (FoundIndex == Add->getNumOperands())
5487 return std::nullopt;
5488
5489 // Create an add with everything but the specified operand.
5491 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5492 if (i != FoundIndex)
5493 Ops.push_back(Add->getOperand(i));
5494 const SCEV *Accum = getAddExpr(Ops);
5495
5496 // The runtime checks will not be valid if the step amount is
5497 // varying inside the loop.
5498 if (!isLoopInvariant(Accum, L))
5499 return std::nullopt;
5500
5501 // *** Part2: Create the predicates
5502
5503 // Analysis was successful: we have a phi-with-cast pattern for which we
5504 // can return an AddRec expression under the following predicates:
5505 //
5506 // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
5507 // fits within the truncated type (does not overflow) for i = 0 to n-1.
5508 // P2: An Equal predicate that guarantees that
5509 // Start = (Ext ix (Trunc iy (Start) to ix) to iy)
5510 // P3: An Equal predicate that guarantees that
5511 // Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
5512 //
5513 // As we next prove, the above predicates guarantee that:
5514 // Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
5515 //
5516 //
5517 // More formally, we want to prove that:
5518 // Expr(i+1) = Start + (i+1) * Accum
5519 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5520 //
5521 // Given that:
5522 // 1) Expr(0) = Start
5523 // 2) Expr(1) = Start + Accum
5524 // = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
5525 // 3) Induction hypothesis (step i):
5526 // Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum
5527 //
5528 // Proof:
5529 // Expr(i+1) =
5530 // = Start + (i+1)*Accum
5531 // = (Start + i*Accum) + Accum
5532 // = Expr(i) + Accum
5533 // = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum
5534 // :: from step i
5535 //
5536 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum
5537 //
5538 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
5539 // + (Ext ix (Trunc iy (Accum) to ix) to iy)
5540 // + Accum :: from P3
5541 //
5542 // = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy)
5543 // + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
5544 //
5545 // = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
5546 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5547 //
5548 // By induction, the same applies to all iterations 1<=i<n:
5549 //
5550
5551 // Create a truncated addrec for which we will add a no overflow check (P1).
5552 const SCEV *StartVal = getSCEV(StartValueV);
5553 const SCEV *PHISCEV =
5554 getAddRecExpr(getTruncateExpr(StartVal, TruncTy),
5555 getTruncateExpr(Accum, TruncTy), L, SCEV::FlagAnyWrap);
5556
5557 // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
5558 // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
5559 // will be constant.
5560 //
5561 // If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
5562 // add P1.
5563 if (const auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
5567 const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags);
5568 Predicates.push_back(AddRecPred);
5569 }
5570
5571 // Create the Equal Predicates P2,P3:
5572
5573 // It is possible that the predicates P2 and/or P3 are computable at
5574 // compile time due to StartVal and/or Accum being constants.
5575 // If either one is, then we can check that now and escape if either P2
5576 // or P3 is false.
5577
5578 // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
5579 // for each of StartVal and Accum
5580 auto getExtendedExpr = [&](const SCEV *Expr,
5581 bool CreateSignExtend) -> const SCEV * {
5582 assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant");
5583 const SCEV *TruncatedExpr = getTruncateExpr(Expr, TruncTy);
5584 const SCEV *ExtendedExpr =
5585 CreateSignExtend ? getSignExtendExpr(TruncatedExpr, Expr->getType())
5586 : getZeroExtendExpr(TruncatedExpr, Expr->getType());
5587 return ExtendedExpr;
5588 };
5589
5590 // Given:
5591 // ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
5592 // = getExtendedExpr(Expr)
5593 // Determine whether the predicate P: Expr == ExtendedExpr
5594 // is known to be false at compile time
5595 auto PredIsKnownFalse = [&](const SCEV *Expr,
5596 const SCEV *ExtendedExpr) -> bool {
5597 return Expr != ExtendedExpr &&
5598 isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr);
5599 };
5600
5601 const SCEV *StartExtended = getExtendedExpr(StartVal, Signed);
5602 if (PredIsKnownFalse(StartVal, StartExtended)) {
5603 LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";);
5604 return std::nullopt;
5605 }
5606
5607 // The Step is always Signed (because the overflow checks are either
5608 // NSSW or NUSW)
5609 const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true);
5610 if (PredIsKnownFalse(Accum, AccumExtended)) {
5611 LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";);
5612 return std::nullopt;
5613 }
5614
5615 auto AppendPredicate = [&](const SCEV *Expr,
5616 const SCEV *ExtendedExpr) -> void {
5617 if (Expr != ExtendedExpr &&
5618 !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) {
5619 const SCEVPredicate *Pred = getEqualPredicate(Expr, ExtendedExpr);
5620 LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred);
5621 Predicates.push_back(Pred);
5622 }
5623 };
5624
5625 AppendPredicate(StartVal, StartExtended);
5626 AppendPredicate(Accum, AccumExtended);
5627
5628 // *** Part3: Predicates are ready. Now go ahead and create the new addrec in
5629 // which the casts had been folded away. The caller can rewrite SymbolicPHI
5630 // into NewAR if it will also add the runtime overflow checks specified in
5631 // Predicates.
5632 const SCEV *NewAR = getAddRecExpr(StartVal, Accum, L, SCEV::FlagAnyWrap);
5633
5634 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
5635 std::make_pair(NewAR, Predicates);
5636 // Remember the result of the analysis for this SCEV at this locayyytion.
5637 PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite;
5638 return PredRewrite;
5639}
5640
5641std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5643 auto *PN = cast<PHINode>(SymbolicPHI->getValue());
5644 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
5645 if (!L)
5646 return std::nullopt;
5647
5648 // Check to see if we already analyzed this PHI.
5649 auto I = PredicatedSCEVRewrites.find({SymbolicPHI, L});
5650 if (I != PredicatedSCEVRewrites.end()) {
5651 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
5652 I->second;
5653 // Analysis was done before and failed to create an AddRec:
5654 if (Rewrite.first == SymbolicPHI)
5655 return std::nullopt;
5656 // Analysis was done before and succeeded to create an AddRec under
5657 // a predicate:
5658 assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec");
5659 assert(!(Rewrite.second).empty() && "Expected to find Predicates");
5660 return Rewrite;
5661 }
5662
5663 std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5664 Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
5665
5666 // Record in the cache that the analysis failed
5667 if (!Rewrite) {
5669 PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
5670 return std::nullopt;
5671 }
5672
5673 return Rewrite;
5674}
5675
5676// FIXME: This utility is currently required because the Rewriter currently
5677// does not rewrite this expression:
5678// {0, +, (sext ix (trunc iy to ix) to iy)}
5679// into {0, +, %step},
5680// even when the following Equal predicate exists:
5681// "%step == (sext ix (trunc iy to ix) to iy)".
5683 const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2,
5684 ArrayRef<const SCEVPredicate *> NoWrapPreds) const {
5685 if (AR1 == AR2)
5686 return true;
5687
5688 SCEVUnionPredicate NoWrapUnionPred(NoWrapPreds, SE);
5689 SCEVUnionPredicate AllPreds = Preds->getUnionWith(&NoWrapUnionPred, SE);
5690 auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool {
5691 if (Expr1 != Expr2 &&
5692 !AllPreds.implies(SE.getEqualPredicate(Expr1, Expr2), SE) &&
5693 !AllPreds.implies(SE.getEqualPredicate(Expr2, Expr1), SE))
5694 return false;
5695 return true;
5696 };
5697
5698 if (!areExprsEqual(AR1->getStart(), AR2->getStart()) ||
5699 !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE)))
5700 return false;
5701 return true;
5702}
5703
5704static SCEV::NoWrapFlags
5707 GEPNoWrapFlags NW = GEP->getNoWrapFlags();
5708 // If the increment has any nowrap flags, then we know the address
5709 // space cannot be wrapped around.
5710 if (NW != GEPNoWrapFlags::none())
5712 // If the GEP is nuw or nusw with non-negative offset, we know that
5713 // no unsigned wrap occurs. We cannot set the nsw flag as only the
5714 // offset is treated as signed, while the base is unsigned.
5715 if (NW.hasNoUnsignedWrap() ||
5716 (NW.hasNoUnsignedSignedWrap() && SE.isKnownNonNegative(Accum)))
5718
5719 return Flags;
5720}
5721
5722/// A helper function for createAddRecFromPHI to handle simple cases.
5723///
5724/// This function tries to find an AddRec expression for the simplest (yet most
5725/// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
5726/// If it fails, createAddRecFromPHI will use a more general, but slow,
5727/// technique for finding the AddRec expression.
5728const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
5729 Value *BEValueV,
5730 Value *StartValueV) {
5731 const Loop *L = LI.getLoopFor(PN->getParent());
5732 assert(L && L->getHeader() == PN->getParent());
5733 assert(BEValueV && StartValueV);
5734
5735 const SCEV *Accum = nullptr;
5737 if (auto BO = MatchBinaryOp(BEValueV, getDataLayout(), AC, DT, PN)) {
5738 if (BO->Opcode != Instruction::Add)
5739 return nullptr;
5740
5741 if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
5742 Accum = getSCEV(BO->RHS);
5743 else if (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
5744 Accum = getSCEV(BO->LHS);
5745
5746 if (!Accum)
5747 return nullptr;
5748
5749 if (BO->IsNUW)
5750 Flags = setFlags(Flags, SCEV::FlagNUW);
5751 if (BO->IsNSW)
5752 Flags = setFlags(Flags, SCEV::FlagNSW);
5753 } else {
5754 // Handle pointer induction variable: PN = PHI(Start, gep PN,
5755 // LoopInvariant).
5756 auto *GEP = dyn_cast<GEPOperator>(BEValueV);
5757 if (!GEP || GEP->getPointerOperand() != PN || GEP->getNumIndices() != 1)
5758 return nullptr;
5759 Value *Idx = *GEP->idx_begin();
5760 if (!L->isLoopInvariant(Idx))
5761 return nullptr;
5762
5763 Type *IntIdxTy = getEffectiveSCEVType(GEP->getType());
5764 Accum = getMulExpr(getTruncateOrSignExtend(getSCEV(Idx), IntIdxTy),
5765 getSizeOfExpr(IntIdxTy, GEP->getSourceElementType()));
5766 Flags = getNoWrapFlagsForGEP(GEP, Accum, *this);
5767 }
5768
5769 const SCEV *StartVal = getSCEV(StartValueV);
5770 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
5771 insertValueToMap(PN, PHISCEV);
5772
5773 if (auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV))
5774 inferNoWrapViaConstantRanges(AR);
5775
5776 // We can add Flags to the post-inc expression only if we
5777 // know that it is *undefined behavior* for BEValueV to
5778 // overflow.
5779 if (auto *BEInst = dyn_cast<Instruction>(BEValueV)) {
5780 assert(isLoopInvariant(Accum, L) &&
5781 "Accum is defined outside L, but is not invariant?");
5782 if (isAddRecNeverPoison(BEInst, L))
5783 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
5784 }
5785
5786 return PHISCEV;
5787}
5788
5789const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
5790 const Loop *L = LI.getLoopFor(PN->getParent());
5791 if (!L || L->getHeader() != PN->getParent())
5792 return nullptr;
5793
5794 // The loop may have multiple entrances or multiple exits; we can analyze
5795 // this phi as an addrec if it has a unique entry value and a unique
5796 // backedge value.
5797 Value *BEValueV = nullptr, *StartValueV = nullptr;
5798 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5799 Value *V = PN->getIncomingValue(i);
5800 if (L->contains(PN->getIncomingBlock(i))) {
5801 if (!BEValueV) {
5802 BEValueV = V;
5803 } else if (BEValueV != V) {
5804 BEValueV = nullptr;
5805 break;
5806 }
5807 } else if (!StartValueV) {
5808 StartValueV = V;
5809 } else if (StartValueV != V) {
5810 StartValueV = nullptr;
5811 break;
5812 }
5813 }
5814 if (!BEValueV || !StartValueV)
5815 return nullptr;
5816
5817 assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
5818 "PHI node already processed?");
5819
5820 // First, try to find AddRec expression without creating a fictituos symbolic
5821 // value for PN.
5822 if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
5823 return S;
5824
5825 // Handle PHI node value symbolically.
5826 const SCEV *SymbolicName = getUnknown(PN);
5827 insertValueToMap(PN, SymbolicName);
5828
5829 // Using this symbolic name for the PHI, analyze the value coming around
5830 // the back-edge.
5831 const SCEV *BEValue = getSCEV(BEValueV);
5832
5833 // NOTE: If BEValue is loop invariant, we know that the PHI node just
5834 // has a special value for the first iteration of the loop.
5835
5836 // If the value coming around the backedge is an add with the symbolic
5837 // value we just inserted, then we found a simple induction variable!
5838 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
5839 // If there is a single occurrence of the symbolic value, replace it
5840 // with a recurrence.
5841 unsigned FoundIndex = Add->getNumOperands();
5842 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5843 if (Add->getOperand(i) == SymbolicName)
5844 if (FoundIndex == e) {
5845 FoundIndex = i;
5846 break;
5847 }
5848
5849 if (FoundIndex != Add->getNumOperands()) {
5850 // Create an add with everything but the specified operand.
5852 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5853 if (i != FoundIndex)
5854 Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i),
5855 L, *this));
5856 const SCEV *Accum = getAddExpr(Ops);
5857
5858 // This is not a valid addrec if the step amount is varying each
5859 // loop iteration, but is not itself an addrec in this loop.
5860 if (isLoopInvariant(Accum, L) ||
5861 (isa<SCEVAddRecExpr>(Accum) &&
5862 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
5864
5865 if (auto BO = MatchBinaryOp(BEValueV, getDataLayout(), AC, DT, PN)) {
5866 if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
5867 if (BO->IsNUW)
5868 Flags = setFlags(Flags, SCEV::FlagNUW);
5869 if (BO->IsNSW)
5870 Flags = setFlags(Flags, SCEV::FlagNSW);
5871 }
5872 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
5873 if (GEP->getOperand(0) == PN)
5874 Flags = getNoWrapFlagsForGEP(GEP, Accum, *this);
5875
5876 // We cannot transfer nuw and nsw flags from subtraction
5877 // operations -- sub nuw X, Y is not the same as add nuw X, -Y
5878 // for instance.
5879 }
5880
5881 const SCEV *StartVal = getSCEV(StartValueV);
5882 const SCEV *PHISCEV = getAddRecExpr(StartVal, Accum, L, Flags);
5883
5884 // Okay, for the entire analysis of this edge we assumed the PHI
5885 // to be symbolic. We now need to go back and purge all of the
5886 // entries for the scalars that use the symbolic expression.
5887 forgetMemoizedResults({SymbolicName});
5888 insertValueToMap(PN, PHISCEV);
5889
5890 if (auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV))
5891 inferNoWrapViaConstantRanges(AR);
5892
5893 // We can add Flags to the post-inc expression only if we
5894 // know that it is *undefined behavior* for BEValueV to
5895 // overflow.
5896 if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
5897 if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
5898 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
5899
5900 return PHISCEV;
5901 }
5902 }
5903 } else {
5904 // Otherwise, this could be a loop like this:
5905 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
5906 // In this case, j = {1,+,1} and BEValue is j.
5907 // Because the other in-value of i (0) fits the evolution of BEValue
5908 // i really is an addrec evolution.
5909 //
5910 // We can generalize this saying that i is the shifted value of BEValue
5911 // by one iteration:
5912 // PHI(f(0), f({1,+,1})) --> f({0,+,1})
5913
5914 // Do not allow refinement in rewriting of BEValue.
5915 const SCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
5916 const SCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this, false);
5917 if (Shifted != getCouldNotCompute() && Start != getCouldNotCompute() &&
5918 isGuaranteedNotToCauseUB(Shifted) && ::impliesPoison(Shifted, Start)) {
5919 const SCEV *StartVal = getSCEV(StartValueV);
5920 if (Start == StartVal) {
5921 // Okay, for the entire analysis of this edge we assumed the PHI
5922 // to be symbolic. We now need to go back and purge all of the
5923 // entries for the scalars that use the symbolic expression.
5924 forgetMemoizedResults({SymbolicName});
5925 insertValueToMap(PN, Shifted);
5926 return Shifted;
5927 }
5928 }
5929 }
5930
5931 // Remove the temporary PHI node SCEV that has been inserted while intending
5932 // to create an AddRecExpr for this PHI node. We can not keep this temporary
5933 // as it will prevent later (possibly simpler) SCEV expressions to be added
5934 // to the ValueExprMap.
5935 eraseValueFromMap(PN);
5936
5937 return nullptr;
5938}
5939
5940// Try to match a control flow sequence that branches out at BI and merges back
5941// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful
5942// match.
5944 Value *&C, Value *&LHS, Value *&RHS) {
5945 C = BI->getCondition();
5946
5947 BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0));
5948 BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1));
5949
5950 Use &LeftUse = Merge->getOperandUse(0);
5951 Use &RightUse = Merge->getOperandUse(1);
5952
5953 if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) {
5954 LHS = LeftUse;
5955 RHS = RightUse;
5956 return true;
5957 }
5958
5959 if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) {
5960 LHS = RightUse;
5961 RHS = LeftUse;
5962 return true;
5963 }
5964
5965 return false;
5966}
5967
5969 Value *&Cond, Value *&LHS,
5970 Value *&RHS) {
5971 auto IsReachable =
5972 [&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); };
5973 if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) {
5974 // Try to match
5975 //
5976 // br %cond, label %left, label %right
5977 // left:
5978 // br label %merge
5979 // right:
5980 // br label %merge
5981 // merge:
5982 // V = phi [ %x, %left ], [ %y, %right ]
5983 //
5984 // as "select %cond, %x, %y"
5985
5986 BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
5987 assert(IDom && "At least the entry block should dominate PN");
5988
5989 auto *BI = dyn_cast<CondBrInst>(IDom->getTerminator());
5990 return BI && BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS);
5991 }
5992 return false;
5993}
5994
5995const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
5996 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
5997 if (getOperandsForSelectLikePHI(DT, PN, Cond, LHS, RHS) &&
6000 return createNodeForSelectOrPHI(PN, Cond, LHS, RHS);
6001
6002 return nullptr;
6003}
6004
6006 BinaryOperator *CommonInst = nullptr;
6007 // Check if instructions are identical.
6008 for (Value *Incoming : PN->incoming_values()) {
6009 auto *IncomingInst = dyn_cast<BinaryOperator>(Incoming);
6010 if (!IncomingInst)
6011 return nullptr;
6012 if (CommonInst) {
6013 if (!CommonInst->isIdenticalToWhenDefined(IncomingInst))
6014 return nullptr; // Not identical, give up
6015 } else {
6016 // Remember binary operator
6017 CommonInst = IncomingInst;
6018 }
6019 }
6020 return CommonInst;
6021}
6022
6023/// Returns SCEV for the first operand of a phi if all phi operands have
6024/// identical opcodes and operands
6025/// eg.
6026/// a: %add = %a + %b
6027/// br %c
6028/// b: %add1 = %a + %b
6029/// br %c
6030/// c: %phi = phi [%add, a], [%add1, b]
6031/// scev(%phi) => scev(%add)
6032const SCEV *
6033ScalarEvolution::createNodeForPHIWithIdenticalOperands(PHINode *PN) {
6034 BinaryOperator *CommonInst = getCommonInstForPHI(PN);
6035 if (!CommonInst)
6036 return nullptr;
6037
6038 // Check if SCEV exprs for instructions are identical.
6039 const SCEV *CommonSCEV = getSCEV(CommonInst);
6040 bool SCEVExprsIdentical =
6042 [this, CommonSCEV](Value *V) { return CommonSCEV == getSCEV(V); });
6043 return SCEVExprsIdentical ? CommonSCEV : nullptr;
6044}
6045
6046const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
6047 if (const SCEV *S = createAddRecFromPHI(PN))
6048 return S;
6049
6050 // We do not allow simplifying phi (undef, X) to X here, to avoid reusing the
6051 // phi node for X.
6052 if (Value *V = simplifyInstruction(
6053 PN, {getDataLayout(), &TLI, &DT, &AC, /*CtxI=*/nullptr,
6054 /*UseInstrInfo=*/true, /*CanUseUndef=*/false}))
6055 return getSCEV(V);
6056
6057 if (const SCEV *S = createNodeForPHIWithIdenticalOperands(PN))
6058 return S;
6059
6060 if (const SCEV *S = createNodeFromSelectLikePHI(PN))
6061 return S;
6062
6063 // If it's not a loop phi, we can't handle it yet.
6064 return getUnknown(PN);
6065}
6066
6067bool SCEVMinMaxExprContains(const SCEV *Root, const SCEV *OperandToFind,
6068 SCEVTypes RootKind) {
6069 struct FindClosure {
6070 const SCEV *OperandToFind;
6071 const SCEVTypes RootKind; // Must be a sequential min/max expression.
6072 const SCEVTypes NonSequentialRootKind; // Non-seq variant of RootKind.
6073
6074 bool Found = false;
6075
6076 bool canRecurseInto(SCEVTypes Kind) const {
6077 // We can only recurse into the SCEV expression of the same effective type
6078 // as the type of our root SCEV expression, and into zero-extensions.
6079 return RootKind == Kind || NonSequentialRootKind == Kind ||
6080 scZeroExtend == Kind;
6081 };
6082
6083 FindClosure(const SCEV *OperandToFind, SCEVTypes RootKind)
6084 : OperandToFind(OperandToFind), RootKind(RootKind),
6085 NonSequentialRootKind(
6087 RootKind)) {}
6088
6089 bool follow(const SCEV *S) {
6090 Found = S == OperandToFind;
6091
6092 return !isDone() && canRecurseInto(S->getSCEVType());
6093 }
6094
6095 bool isDone() const { return Found; }
6096 };
6097
6098 FindClosure FC(OperandToFind, RootKind);
6099 visitAll(Root, FC);
6100 return FC.Found;
6101}
6102
6103std::optional<const SCEV *>
6104ScalarEvolution::createNodeForSelectOrPHIInstWithICmpInstCond(Type *Ty,
6105 ICmpInst *Cond,
6106 Value *TrueVal,
6107 Value *FalseVal) {
6108 // Try to match some simple smax or umax patterns.
6109 auto *ICI = Cond;
6110
6111 Value *LHS = ICI->getOperand(0);
6112 Value *RHS = ICI->getOperand(1);
6113
6114 switch (ICI->getPredicate()) {
6115 case ICmpInst::ICMP_SLT:
6116 case ICmpInst::ICMP_SLE:
6117 case ICmpInst::ICMP_ULT:
6118 case ICmpInst::ICMP_ULE:
6119 std::swap(LHS, RHS);
6120 [[fallthrough]];
6121 case ICmpInst::ICMP_SGT:
6122 case ICmpInst::ICMP_SGE:
6123 case ICmpInst::ICMP_UGT:
6124 case ICmpInst::ICMP_UGE:
6125 // a > b ? a+x : b+x -> max(a, b)+x
6126 // a > b ? b+x : a+x -> min(a, b)+x
6128 bool Signed = ICI->isSigned();
6129 const SCEV *LA = getSCEV(TrueVal);
6130 const SCEV *RA = getSCEV(FalseVal);
6131 const SCEV *LS = getSCEV(LHS);
6132 const SCEV *RS = getSCEV(RHS);
6133 if (LA->getType()->isPointerTy()) {
6134 // FIXME: Handle cases where LS/RS are pointers not equal to LA/RA.
6135 // Need to make sure we can't produce weird expressions involving
6136 // negated pointers.
6137 if (LA == LS && RA == RS)
6138 return Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS);
6139 if (LA == RS && RA == LS)
6140 return Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS);
6141 }
6142 auto CoerceOperand = [&](const SCEV *Op) -> const SCEV * {
6143 if (Op->getType()->isPointerTy()) {
6146 return Op;
6147 }
6148 if (Signed)
6149 Op = getNoopOrSignExtend(Op, Ty);
6150 else
6151 Op = getNoopOrZeroExtend(Op, Ty);
6152 return Op;
6153 };
6154 LS = CoerceOperand(LS);
6155 RS = CoerceOperand(RS);
6157 break;
6158 const SCEV *LDiff = getMinusSCEV(LA, LS);
6159 const SCEV *RDiff = getMinusSCEV(RA, RS);
6160 if (LDiff == RDiff)
6161 return getAddExpr(Signed ? getSMaxExpr(LS, RS) : getUMaxExpr(LS, RS),
6162 LDiff);
6163 LDiff = getMinusSCEV(LA, RS);
6164 RDiff = getMinusSCEV(RA, LS);
6165 if (LDiff == RDiff)
6166 return getAddExpr(Signed ? getSMinExpr(LS, RS) : getUMinExpr(LS, RS),
6167 LDiff);
6168 }
6169 break;
6170 case ICmpInst::ICMP_NE:
6171 // x != 0 ? x+y : C+y -> x == 0 ? C+y : x+y
6172 std::swap(TrueVal, FalseVal);
6173 [[fallthrough]];
6174 case ICmpInst::ICMP_EQ:
6175 // x == 0 ? C+y : x+y -> umax(x, C)+y iff C u<= 1
6178 const SCEV *X = getNoopOrZeroExtend(getSCEV(LHS), Ty);
6179 const SCEV *TrueValExpr = getSCEV(TrueVal); // C+y
6180 const SCEV *FalseValExpr = getSCEV(FalseVal); // x+y
6181 const SCEV *Y = getMinusSCEV(FalseValExpr, X); // y = (x+y)-x
6182 const SCEV *C = getMinusSCEV(TrueValExpr, Y); // C = (C+y)-y
6183 if (isa<SCEVConstant>(C) && cast<SCEVConstant>(C)->getAPInt().ule(1))
6184 return getAddExpr(getUMaxExpr(X, C), Y);
6185 }
6186 // x == 0 ? 0 : umin (..., x, ...) -> umin_seq(x, umin (...))
6187 // x == 0 ? 0 : umin_seq(..., x, ...) -> umin_seq(x, umin_seq(...))
6188 // x == 0 ? 0 : umin (..., umin_seq(..., x, ...), ...)
6189 // -> umin_seq(x, umin (..., umin_seq(...), ...))
6191 isa<ConstantInt>(TrueVal) && cast<ConstantInt>(TrueVal)->isZero()) {
6192 const SCEV *X = getSCEV(LHS);
6193 while (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(X))
6194 X = ZExt->getOperand();
6195 if (getTypeSizeInBits(X->getType()) <= getTypeSizeInBits(Ty)) {
6196 const SCEV *FalseValExpr = getSCEV(FalseVal);
6197 if (SCEVMinMaxExprContains(FalseValExpr, X, scSequentialUMinExpr))
6198 return getUMinExpr(getNoopOrZeroExtend(X, Ty), FalseValExpr,
6199 /*Sequential=*/true);
6200 }
6201 }
6202 break;
6203 default:
6204 break;
6205 }
6206
6207 return std::nullopt;
6208}
6209
6210static std::optional<const SCEV *>
6212 const SCEV *TrueExpr, const SCEV *FalseExpr) {
6213 assert(CondExpr->getType()->isIntegerTy(1) &&
6214 TrueExpr->getType() == FalseExpr->getType() &&
6215 TrueExpr->getType()->isIntegerTy(1) &&
6216 "Unexpected operands of a select.");
6217
6218 // i1 cond ? i1 x : i1 C --> C + (i1 cond ? (i1 x - i1 C) : i1 0)
6219 // --> C + (umin_seq cond, x - C)
6220 //
6221 // i1 cond ? i1 C : i1 x --> C + (i1 cond ? i1 0 : (i1 x - i1 C))
6222 // --> C + (i1 ~cond ? (i1 x - i1 C) : i1 0)
6223 // --> C + (umin_seq ~cond, x - C)
6224
6225 // FIXME: while we can't legally model the case where both of the hands
6226 // are fully variable, we only require that the *difference* is constant.
6227 if (!isa<SCEVConstant>(TrueExpr) && !isa<SCEVConstant>(FalseExpr))
6228 return std::nullopt;
6229
6230 const SCEV *X, *C;
6231 if (isa<SCEVConstant>(TrueExpr)) {
6232 CondExpr = SE->getNotSCEV(CondExpr);
6233 X = FalseExpr;
6234 C = TrueExpr;
6235 } else {
6236 X = TrueExpr;
6237 C = FalseExpr;
6238 }
6239 return SE->getAddExpr(C, SE->getUMinExpr(CondExpr, SE->getMinusSCEV(X, C),
6240 /*Sequential=*/true));
6241}
6242
6243static std::optional<const SCEV *>
6245 Value *FalseVal) {
6246 if (!isa<ConstantInt>(TrueVal) && !isa<ConstantInt>(FalseVal))
6247 return std::nullopt;
6248
6249 const auto *SECond = SE->getSCEV(Cond);
6250 const auto *SETrue = SE->getSCEV(TrueVal);
6251 const auto *SEFalse = SE->getSCEV(FalseVal);
6252 return createNodeForSelectViaUMinSeq(SE, SECond, SETrue, SEFalse);
6253}
6254
6255const SCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq(
6256 Value *V, Value *Cond, Value *TrueVal, Value *FalseVal) {
6257 assert(Cond->getType()->isIntegerTy(1) && "Select condition is not an i1?");
6258 assert(TrueVal->getType() == FalseVal->getType() &&
6259 V->getType() == TrueVal->getType() &&
6260 "Types of select hands and of the result must match.");
6261
6262 // For now, only deal with i1-typed `select`s.
6263 if (!V->getType()->isIntegerTy(1))
6264 return getUnknown(V);
6265
6266 if (std::optional<const SCEV *> S =
6267 createNodeForSelectViaUMinSeq(this, Cond, TrueVal, FalseVal))
6268 return *S;
6269
6270 return getUnknown(V);
6271}
6272
6273const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond,
6274 Value *TrueVal,
6275 Value *FalseVal) {
6276 // Handle "constant" branch or select. This can occur for instance when a
6277 // loop pass transforms an inner loop and moves on to process the outer loop.
6278 if (auto *CI = dyn_cast<ConstantInt>(Cond))
6279 return getSCEV(CI->isOne() ? TrueVal : FalseVal);
6280
6281 if (auto *I = dyn_cast<Instruction>(V)) {
6282 if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
6283 if (std::optional<const SCEV *> S =
6284 createNodeForSelectOrPHIInstWithICmpInstCond(I->getType(), ICI,
6285 TrueVal, FalseVal))
6286 return *S;
6287 }
6288 }
6289
6290 return createNodeForSelectOrPHIViaUMinSeq(V, Cond, TrueVal, FalseVal);
6291}
6292
6293/// Expand GEP instructions into add and multiply operations. This allows them
6294/// to be analyzed by regular SCEV code.
6295const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
6296 assert(GEP->getSourceElementType()->isSized() &&
6297 "GEP source element type must be sized");
6298
6299 SmallVector<SCEVUse, 4> IndexExprs;
6300 for (Value *Index : GEP->indices())
6301 IndexExprs.push_back(getSCEV(Index));
6302 return getGEPExpr(GEP, IndexExprs);
6303}
6304
6305APInt ScalarEvolution::getConstantMultipleImpl(const SCEV *S,
6306 const Instruction *CtxI) {
6308 auto GetShiftedByZeros = [BitWidth](uint32_t TrailingZeros) {
6309 return TrailingZeros >= BitWidth
6311 : APInt::getOneBitSet(BitWidth, TrailingZeros);
6312 };
6313 auto GetGCDMultiple = [this, CtxI](const SCEVNAryExpr *N) {
6314 // The result is GCD of all operands results.
6315 APInt Res = getConstantMultiple(N->getOperand(0), CtxI);
6316 for (unsigned I = 1, E = N->getNumOperands(); I < E && Res != 1; ++I)
6318 Res, getConstantMultiple(N->getOperand(I), CtxI));
6319 return Res;
6320 };
6321
6322 switch (S->getSCEVType()) {
6323 case scConstant:
6324 return cast<SCEVConstant>(S)->getAPInt();
6325 case scPtrToAddr:
6326 return getConstantMultiple(cast<SCEVCastExpr>(S)->getOperand());
6327 case scUDivExpr:
6328 case scVScale:
6329 return APInt(BitWidth, 1);
6330 case scTruncate: {
6331 // Only multiples that are a power of 2 will hold after truncation.
6332 const SCEVTruncateExpr *T = cast<SCEVTruncateExpr>(S);
6333 uint32_t TZ = getMinTrailingZeros(T->getOperand(), CtxI);
6334 return GetShiftedByZeros(TZ);
6335 }
6336 case scZeroExtend: {
6337 const SCEVZeroExtendExpr *Z = cast<SCEVZeroExtendExpr>(S);
6338 return getConstantMultiple(Z->getOperand(), CtxI).zext(BitWidth);
6339 }
6340 case scSignExtend: {
6341 // Only multiples that are a power of 2 will hold after sext.
6342 const SCEVSignExtendExpr *E = cast<SCEVSignExtendExpr>(S);
6343 uint32_t TZ = getMinTrailingZeros(E->getOperand(), CtxI);
6344 return GetShiftedByZeros(TZ);
6345 }
6346 case scMulExpr: {
6347 const SCEVMulExpr *M = cast<SCEVMulExpr>(S);
6348 if (M->hasNoUnsignedWrap()) {
6349 // The result is the product of all operand results.
6350 APInt Res = getConstantMultiple(M->getOperand(0), CtxI);
6351 for (const SCEV *Operand : M->operands().drop_front())
6352 Res = Res * getConstantMultiple(Operand, CtxI);
6353 return Res;
6354 }
6355
6356 // If there are no wrap guarentees, find the trailing zeros, which is the
6357 // sum of trailing zeros for all its operands.
6358 uint32_t TZ = 0;
6359 for (const SCEV *Operand : M->operands())
6360 TZ += getMinTrailingZeros(Operand, CtxI);
6361 return GetShiftedByZeros(TZ);
6362 }
6363 case scAddExpr:
6364 case scAddRecExpr: {
6365 const SCEVNAryExpr *N = cast<SCEVNAryExpr>(S);
6366 if (N->hasNoUnsignedWrap())
6367 return GetGCDMultiple(N);
6368 // Find the trailing bits, which is the minimum of its operands.
6369 uint32_t TZ = getMinTrailingZeros(N->getOperand(0), CtxI);
6370 for (const SCEV *Operand : N->operands().drop_front())
6371 TZ = std::min(TZ, getMinTrailingZeros(Operand, CtxI));
6372 return GetShiftedByZeros(TZ);
6373 }
6374 case scUMaxExpr:
6375 case scSMaxExpr:
6376 case scUMinExpr:
6377 case scSMinExpr:
6379 return GetGCDMultiple(cast<SCEVNAryExpr>(S));
6380 case scUnknown: {
6381 // Ask ValueTracking for known bits. SCEVUnknown only become available at
6382 // the point their underlying IR instruction has been defined. If CtxI was
6383 // not provided, use:
6384 // * the first instruction in the entry block if it is an argument
6385 // * the instruction itself otherwise.
6386 const SCEVUnknown *U = cast<SCEVUnknown>(S);
6387 if (!CtxI) {
6388 if (isa<Argument>(U->getValue()))
6389 CtxI = &*F.getEntryBlock().begin();
6390 else if (auto *I = dyn_cast<Instruction>(U->getValue()))
6391 CtxI = I;
6392 }
6393 unsigned Known =
6394 computeKnownBits(U->getValue(),
6395 SimplifyQuery(getDataLayout(), &DT, &AC, CtxI)
6396 .allowEphemerals(true))
6397 .countMinTrailingZeros();
6398 return GetShiftedByZeros(Known);
6399 }
6400 case scCouldNotCompute:
6401 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6402 }
6403 llvm_unreachable("Unknown SCEV kind!");
6404}
6405
6407 const Instruction *CtxI) {
6408 // Skip looking up and updating the cache if there is a context instruction,
6409 // as the result will only be valid in the specified context.
6410 if (CtxI)
6411 return getConstantMultipleImpl(S, CtxI);
6412
6413 auto I = ConstantMultipleCache.find(S);
6414 if (I != ConstantMultipleCache.end())
6415 return I->second;
6416
6417 APInt Result = getConstantMultipleImpl(S, CtxI);
6418 auto InsertPair = ConstantMultipleCache.insert({S, Result});
6419 assert(InsertPair.second && "Should insert a new key");
6420 return InsertPair.first->second;
6421}
6422
6424 APInt Multiple = getConstantMultiple(S);
6425 return Multiple == 0 ? APInt(Multiple.getBitWidth(), 1) : Multiple;
6426}
6427
6429 const Instruction *CtxI) {
6430 return std::min(getConstantMultiple(S, CtxI).countTrailingZeros(),
6431 (unsigned)getTypeSizeInBits(S->getType()));
6432}
6433
6434/// Helper method to assign a range to V from metadata present in the IR.
6435static std::optional<ConstantRange> GetRangeFromMetadata(Value *V) {
6437 if (MDNode *MD = I->getMetadata(LLVMContext::MD_range))
6438 return getConstantRangeFromMetadata(*MD);
6439 if (const auto *CB = dyn_cast<CallBase>(V))
6440 if (std::optional<ConstantRange> Range = CB->getRange())
6441 return Range;
6442 }
6443 if (auto *A = dyn_cast<Argument>(V))
6444 if (std::optional<ConstantRange> Range = A->getRange())
6445 return Range;
6446
6447 return std::nullopt;
6448}
6449
6451 SCEV::NoWrapFlags Flags) {
6452 if (AddRec->getNoWrapFlags(Flags) != Flags) {
6453 AddRec->setNoWrapFlags(Flags);
6454 UnsignedRanges.erase(AddRec);
6455 SignedRanges.erase(AddRec);
6456 ConstantMultipleCache.erase(AddRec);
6457 }
6458}
6459
6460ConstantRange ScalarEvolution::
6461getRangeForUnknownRecurrence(const SCEVUnknown *U) {
6462 const DataLayout &DL = getDataLayout();
6463
6464 unsigned BitWidth = getTypeSizeInBits(U->getType());
6465 const ConstantRange FullSet(BitWidth, /*isFullSet=*/true);
6466
6467 // Match a simple recurrence of the form: <start, ShiftOp, Step>, and then
6468 // use information about the trip count to improve our available range. Note
6469 // that the trip count independent cases are already handled by known bits.
6470 // WARNING: The definition of recurrence used here is subtly different than
6471 // the one used by AddRec (and thus most of this file). Step is allowed to
6472 // be arbitrarily loop varying here, where AddRec allows only loop invariant
6473 // and other addrecs in the same loop (for non-affine addrecs). The code
6474 // below intentionally handles the case where step is not loop invariant.
6475 auto *P = dyn_cast<PHINode>(U->getValue());
6476 if (!P)
6477 return FullSet;
6478
6479 // Make sure that no Phi input comes from an unreachable block. Otherwise,
6480 // even the values that are not available in these blocks may come from them,
6481 // and this leads to false-positive recurrence test.
6482 for (auto *Pred : predecessors(P->getParent()))
6483 if (!DT.isReachableFromEntry(Pred))
6484 return FullSet;
6485
6486 BinaryOperator *BO;
6487 Value *Start, *Step;
6488 if (!matchSimpleRecurrence(P, BO, Start, Step))
6489 return FullSet;
6490
6491 // If we found a recurrence in reachable code, we must be in a loop. Note
6492 // that BO might be in some subloop of L, and that's completely okay.
6493 auto *L = LI.getLoopFor(P->getParent());
6494 assert(L && L->getHeader() == P->getParent());
6495 if (!L->contains(BO->getParent()))
6496 // NOTE: This bailout should be an assert instead. However, asserting
6497 // the condition here exposes a case where LoopFusion is querying SCEV
6498 // with malformed loop information during the midst of the transform.
6499 // There doesn't appear to be an obvious fix, so for the moment bailout
6500 // until the caller issue can be fixed. PR49566 tracks the bug.
6501 return FullSet;
6502
6503 // TODO: Extend to other opcodes such as mul, and div
6504 switch (BO->getOpcode()) {
6505 default:
6506 return FullSet;
6507 case Instruction::AShr:
6508 case Instruction::LShr:
6509 case Instruction::Shl:
6510 break;
6511 };
6512
6513 if (BO->getOperand(0) != P)
6514 // TODO: Handle the power function forms some day.
6515 return FullSet;
6516
6517 unsigned TC = getSmallConstantMaxTripCount(L);
6518 if (!TC || TC >= BitWidth)
6519 return FullSet;
6520
6521 auto KnownStart = computeKnownBits(Start, DL, &AC, nullptr, &DT);
6522 auto KnownStep = computeKnownBits(Step, DL, &AC, nullptr, &DT);
6523 assert(KnownStart.getBitWidth() == BitWidth &&
6524 KnownStep.getBitWidth() == BitWidth);
6525
6526 // Compute total shift amount, being careful of overflow and bitwidths.
6527 auto MaxShiftAmt = KnownStep.getMaxValue();
6528 APInt TCAP(BitWidth, TC-1);
6529 bool Overflow = false;
6530 auto TotalShift = MaxShiftAmt.umul_ov(TCAP, Overflow);
6531 if (Overflow)
6532 return FullSet;
6533
6534 switch (BO->getOpcode()) {
6535 default:
6536 llvm_unreachable("filtered out above");
6537 case Instruction::AShr: {
6538 // For each ashr, three cases:
6539 // shift = 0 => unchanged value
6540 // saturation => 0 or -1
6541 // other => a value closer to zero (of the same sign)
6542 // Thus, the end value is closer to zero than the start.
6543 auto KnownEnd = KnownBits::ashr(KnownStart,
6544 KnownBits::makeConstant(TotalShift));
6545 if (KnownStart.isNonNegative())
6546 // Analogous to lshr (simply not yet canonicalized)
6547 return ConstantRange::getNonEmpty(KnownEnd.getMinValue(),
6548 KnownStart.getMaxValue() + 1);
6549 if (KnownStart.isNegative())
6550 // End >=u Start && End <=s Start
6551 return ConstantRange::getNonEmpty(KnownStart.getMinValue(),
6552 KnownEnd.getMaxValue() + 1);
6553 break;
6554 }
6555 case Instruction::LShr: {
6556 // For each lshr, three cases:
6557 // shift = 0 => unchanged value
6558 // saturation => 0
6559 // other => a smaller positive number
6560 // Thus, the low end of the unsigned range is the last value produced.
6561 auto KnownEnd = KnownBits::lshr(KnownStart,
6562 KnownBits::makeConstant(TotalShift));
6563 return ConstantRange::getNonEmpty(KnownEnd.getMinValue(),
6564 KnownStart.getMaxValue() + 1);
6565 }
6566 case Instruction::Shl: {
6567 // Iff no bits are shifted out, value increases on every shift.
6568 auto KnownEnd = KnownBits::shl(KnownStart,
6569 KnownBits::makeConstant(TotalShift));
6570 if (TotalShift.ult(KnownStart.countMinLeadingZeros()))
6571 return ConstantRange(KnownStart.getMinValue(),
6572 KnownEnd.getMaxValue() + 1);
6573 break;
6574 }
6575 };
6576 return FullSet;
6577}
6578
6579// The goal of this function is to check if recursively visiting the operands
6580// of this PHI might lead to an infinite loop. If we do see such a loop,
6581// there's no good way to break it, so we avoid analyzing such cases.
6582//
6583// getRangeRef previously used a visited set to avoid infinite loops, but this
6584// caused other issues: the result was dependent on the order of getRangeRef
6585// calls, and the interaction with createSCEVIter could cause a stack overflow
6586// in some cases (see issue #148253).
6587//
6588// FIXME: The way this is implemented is overly conservative; this checks
6589// for a few obviously safe patterns, but anything that doesn't lead to
6590// recursion is fine.
6592 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
6594 return true;
6595
6596 if (all_of(PHI->operands(),
6597 [&](Value *Operand) { return DT.dominates(Operand, PHI); }))
6598 return true;
6599
6600 return false;
6601}
6602
6603const ConstantRange &
6604ScalarEvolution::getRangeRefIter(const SCEV *S,
6605 ScalarEvolution::RangeSignHint SignHint) {
6606 DenseMap<const SCEV *, ConstantRange> &Cache =
6607 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
6608 : SignedRanges;
6609 SmallVector<SCEVUse> WorkList;
6610 SmallPtrSet<const SCEV *, 8> Seen;
6611
6612 // Add Expr to the worklist, if Expr is either an N-ary expression or a
6613 // SCEVUnknown PHI node.
6614 auto AddToWorklist = [&WorkList, &Seen, &Cache](const SCEV *Expr) {
6615 if (!Seen.insert(Expr).second)
6616 return;
6617 if (Cache.contains(Expr))
6618 return;
6619 switch (Expr->getSCEVType()) {
6620 case scUnknown:
6622 break;
6623 [[fallthrough]];
6624 case scConstant:
6625 case scVScale:
6626 case scTruncate:
6627 case scZeroExtend:
6628 case scSignExtend:
6629 case scPtrToAddr:
6630 case scAddExpr:
6631 case scMulExpr:
6632 case scUDivExpr:
6633 case scAddRecExpr:
6634 case scUMaxExpr:
6635 case scSMaxExpr:
6636 case scUMinExpr:
6637 case scSMinExpr:
6639 WorkList.push_back(Expr);
6640 break;
6641 case scCouldNotCompute:
6642 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6643 }
6644 };
6645 AddToWorklist(S);
6646
6647 // Build worklist by queuing operands of N-ary expressions and phi nodes.
6648 for (unsigned I = 0; I != WorkList.size(); ++I) {
6649 const SCEV *P = WorkList[I];
6650 auto *UnknownS = dyn_cast<SCEVUnknown>(P);
6651 // If it is not a `SCEVUnknown`, just recurse into operands.
6652 if (!UnknownS) {
6653 for (const SCEV *Op : P->operands())
6654 AddToWorklist(Op);
6655 continue;
6656 }
6657 // `SCEVUnknown`'s require special treatment.
6658 if (PHINode *P = dyn_cast<PHINode>(UnknownS->getValue())) {
6659 if (!RangeRefPHIAllowedOperands(DT, P))
6660 continue;
6661 for (auto &Op : reverse(P->operands()))
6662 AddToWorklist(getSCEV(Op));
6663 }
6664 }
6665
6666 if (!WorkList.empty()) {
6667 // Use getRangeRef to compute ranges for items in the worklist in reverse
6668 // order. This will force ranges for earlier operands to be computed before
6669 // their users in most cases.
6670 for (const SCEV *P : reverse(drop_begin(WorkList))) {
6671 getRangeRef(P, SignHint);
6672 }
6673 }
6674
6675 return getRangeRef(S, SignHint, 0);
6676}
6677
6678const APInt *ScalarEvolution::getConstantAPIntOrNull(const SCEV *S) {
6679 if (const auto *C = dyn_cast<SCEVConstant>(S))
6680 return &C->getAPInt();
6681 return nullptr;
6682}
6683
6684/// Determine the range for a particular SCEV. If SignHint is
6685/// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
6686/// with a "cleaner" unsigned (resp. signed) representation.
6687const ConstantRange &ScalarEvolution::getRangeRef(
6688 const SCEV *S, ScalarEvolution::RangeSignHint SignHint, unsigned Depth) {
6689 DenseMap<const SCEV *, ConstantRange> &Cache =
6690 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
6691 : SignedRanges;
6693 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? ConstantRange::Unsigned
6695
6696 // See if we've computed this range already.
6697 auto I = Cache.find(S);
6698 if (I != Cache.end())
6699 return I->second;
6700
6701 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(S))
6702 return setRange(C, SignHint, ConstantRange(C->getAPInt()));
6703
6704 // Switch to iteratively computing the range for S, if it is part of a deeply
6705 // nested expression.
6707 return getRangeRefIter(S, SignHint);
6708
6709 unsigned BitWidth = getTypeSizeInBits(S->getType());
6710 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
6711 using OBO = OverflowingBinaryOperator;
6712
6713 // If the value has known zeros, the maximum value will have those known zeros
6714 // as well.
6715 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
6716 APInt Multiple = getNonZeroConstantMultiple(S);
6717 APInt Remainder = APInt::getMaxValue(BitWidth).urem(Multiple);
6718 if (!Remainder.isZero())
6719 ConservativeResult =
6720 ConstantRange(APInt::getMinValue(BitWidth),
6721 APInt::getMaxValue(BitWidth) - Remainder + 1);
6722 }
6723 else {
6724 uint32_t TZ = getMinTrailingZeros(S);
6725 if (TZ != 0) {
6726 ConservativeResult = ConstantRange(
6728 APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
6729 }
6730 }
6731
6732 switch (S->getSCEVType()) {
6733 case scConstant:
6734 llvm_unreachable("Already handled above.");
6735 case scVScale:
6736 return setRange(S, SignHint, getVScaleRange(&F, BitWidth));
6737 case scTruncate: {
6738 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(S);
6739 ConstantRange X = getRangeRef(Trunc->getOperand(), SignHint, Depth + 1);
6740 return setRange(
6741 Trunc, SignHint,
6742 ConservativeResult.intersectWith(X.truncate(BitWidth), RangeType));
6743 }
6744 case scZeroExtend: {
6745 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(S);
6746 ConstantRange X = getRangeRef(ZExt->getOperand(), SignHint, Depth + 1);
6747 return setRange(
6748 ZExt, SignHint,
6749 ConservativeResult.intersectWith(X.zeroExtend(BitWidth), RangeType));
6750 }
6751 case scSignExtend: {
6752 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(S);
6753 ConstantRange X = getRangeRef(SExt->getOperand(), SignHint, Depth + 1);
6754 return setRange(
6755 SExt, SignHint,
6756 ConservativeResult.intersectWith(X.signExtend(BitWidth), RangeType));
6757 }
6758 case scPtrToAddr: {
6759 const SCEVCastExpr *Cast = cast<SCEVCastExpr>(S);
6760 ConstantRange X = getRangeRef(Cast->getOperand(), SignHint, Depth + 1);
6761 return setRange(Cast, SignHint, X);
6762 }
6763 case scAddExpr: {
6764 const SCEVAddExpr *Add = cast<SCEVAddExpr>(S);
6765 // Check if this is a URem pattern: A - (A / B) * B, which is always < B.
6766 const SCEV *URemLHS = nullptr, *URemRHS = nullptr;
6767 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED &&
6768 match(S, m_scev_URem(m_SCEV(URemLHS), m_SCEV(URemRHS), *this))) {
6769 ConstantRange LHSRange = getRangeRef(URemLHS, SignHint, Depth + 1);
6770 ConstantRange RHSRange = getRangeRef(URemRHS, SignHint, Depth + 1);
6771 ConservativeResult =
6772 ConservativeResult.intersectWith(LHSRange.urem(RHSRange), RangeType);
6773 }
6774 ConstantRange X = getRangeRef(Add->getOperand(0), SignHint, Depth + 1);
6775 unsigned WrapType = OBO::AnyWrap;
6776 if (Add->hasNoSignedWrap())
6777 WrapType |= OBO::NoSignedWrap;
6778 if (Add->hasNoUnsignedWrap())
6779 WrapType |= OBO::NoUnsignedWrap;
6780 for (const SCEV *Op : drop_begin(Add->operands()))
6781 X = X.addWithNoWrap(getRangeRef(Op, SignHint, Depth + 1), WrapType,
6782 RangeType);
6783 return setRange(Add, SignHint,
6784 ConservativeResult.intersectWith(X, RangeType));
6785 }
6786 case scMulExpr: {
6787 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(S);
6788 ConstantRange X = getRangeRef(Mul->getOperand(0), SignHint, Depth + 1);
6789 for (const SCEV *Op : drop_begin(Mul->operands()))
6790 X = X.multiply(getRangeRef(Op, SignHint, Depth + 1));
6791 return setRange(Mul, SignHint,
6792 ConservativeResult.intersectWith(X, RangeType));
6793 }
6794 case scUDivExpr: {
6795 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
6796 ConstantRange X = getRangeRef(UDiv->getLHS(), SignHint, Depth + 1);
6797 ConstantRange Y = getRangeRef(UDiv->getRHS(), SignHint, Depth + 1);
6798 return setRange(UDiv, SignHint,
6799 ConservativeResult.intersectWith(X.udiv(Y), RangeType));
6800 }
6801 case scAddRecExpr: {
6802 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(S);
6803 // If there's no unsigned wrap, the value will never be less than its
6804 // initial value.
6805 if (AddRec->hasNoUnsignedWrap()) {
6806 APInt UnsignedMinValue = getUnsignedRangeMin(AddRec->getStart());
6807 if (!UnsignedMinValue.isZero())
6808 ConservativeResult = ConservativeResult.intersectWith(
6809 ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), RangeType);
6810 }
6811
6812 // If there's no signed wrap, and all the operands except initial value have
6813 // the same sign or zero, the value won't ever be:
6814 // 1: smaller than initial value if operands are non negative,
6815 // 2: bigger than initial value if operands are non positive.
6816 // For both cases, value can not cross signed min/max boundary.
6817 if (AddRec->hasNoSignedWrap()) {
6818 bool AllNonNeg = true;
6819 bool AllNonPos = true;
6820 for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) {
6821 if (!isKnownNonNegative(AddRec->getOperand(i)))
6822 AllNonNeg = false;
6823 if (!isKnownNonPositive(AddRec->getOperand(i)))
6824 AllNonPos = false;
6825 }
6826 if (AllNonNeg)
6827 ConservativeResult = ConservativeResult.intersectWith(
6830 RangeType);
6831 else if (AllNonPos)
6832 ConservativeResult = ConservativeResult.intersectWith(
6834 getSignedRangeMax(AddRec->getStart()) +
6835 1),
6836 RangeType);
6837 }
6838
6839 // TODO: non-affine addrec
6840 if (AddRec->isAffine()) {
6841 const SCEV *MaxBEScev =
6843 if (!isa<SCEVCouldNotCompute>(MaxBEScev)) {
6844 APInt MaxBECount = cast<SCEVConstant>(MaxBEScev)->getAPInt();
6845
6846 // Adjust MaxBECount to the same bitwidth as AddRec. We can truncate if
6847 // MaxBECount's active bits are all <= AddRec's bit width.
6848 if (MaxBECount.getBitWidth() > BitWidth &&
6849 MaxBECount.getActiveBits() <= BitWidth)
6850 MaxBECount = MaxBECount.trunc(BitWidth);
6851 else if (MaxBECount.getBitWidth() < BitWidth)
6852 MaxBECount = MaxBECount.zext(BitWidth);
6853
6854 if (MaxBECount.getBitWidth() == BitWidth) {
6855 auto [RangeFromAffine, Flags] = getRangeForAffineAR(
6856 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount);
6857 ConservativeResult =
6858 ConservativeResult.intersectWith(RangeFromAffine, RangeType);
6859 const_cast<SCEVAddRecExpr *>(AddRec)->setNoWrapFlags(Flags);
6860
6861 auto RangeFromFactoring = getRangeViaFactoring(
6862 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount);
6863 ConservativeResult =
6864 ConservativeResult.intersectWith(RangeFromFactoring, RangeType);
6865 }
6866 }
6867
6868 // Now try symbolic BE count and more powerful methods.
6870 const SCEV *SymbolicMaxBECount =
6872 if (!isa<SCEVCouldNotCompute>(SymbolicMaxBECount) &&
6873 getTypeSizeInBits(MaxBEScev->getType()) <= BitWidth &&
6874 AddRec->hasNoSelfWrap()) {
6875 auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR(
6876 AddRec, SymbolicMaxBECount, BitWidth, SignHint);
6877 ConservativeResult =
6878 ConservativeResult.intersectWith(RangeFromAffineNew, RangeType);
6879 }
6880 }
6881 }
6882
6883 return setRange(AddRec, SignHint, std::move(ConservativeResult));
6884 }
6885 case scUMaxExpr:
6886 case scSMaxExpr:
6887 case scUMinExpr:
6888 case scSMinExpr:
6889 case scSequentialUMinExpr: {
6891 switch (S->getSCEVType()) {
6892 case scUMaxExpr:
6893 ID = Intrinsic::umax;
6894 break;
6895 case scSMaxExpr:
6896 ID = Intrinsic::smax;
6897 break;
6898 case scUMinExpr:
6900 ID = Intrinsic::umin;
6901 break;
6902 case scSMinExpr:
6903 ID = Intrinsic::smin;
6904 break;
6905 default:
6906 llvm_unreachable("Unknown SCEVMinMaxExpr/SCEVSequentialMinMaxExpr.");
6907 }
6908
6909 const auto *NAry = cast<SCEVNAryExpr>(S);
6910 ConstantRange X = getRangeRef(NAry->getOperand(0), SignHint, Depth + 1);
6911 for (unsigned i = 1, e = NAry->getNumOperands(); i != e; ++i)
6912 X = X.intrinsic(
6913 ID, {X, getRangeRef(NAry->getOperand(i), SignHint, Depth + 1)});
6914 return setRange(S, SignHint,
6915 ConservativeResult.intersectWith(X, RangeType));
6916 }
6917 case scUnknown: {
6918 const SCEVUnknown *U = cast<SCEVUnknown>(S);
6919 Value *V = U->getValue();
6920
6921 // Check if the IR explicitly contains !range metadata.
6922 std::optional<ConstantRange> MDRange = GetRangeFromMetadata(V);
6923 if (MDRange)
6924 ConservativeResult =
6925 ConservativeResult.intersectWith(*MDRange, RangeType);
6926
6927 // Use facts about recurrences in the underlying IR. Note that add
6928 // recurrences are AddRecExprs and thus don't hit this path. This
6929 // primarily handles shift recurrences.
6930 auto CR = getRangeForUnknownRecurrence(U);
6931 ConservativeResult = ConservativeResult.intersectWith(CR);
6932
6933 // See if ValueTracking can give us a useful range.
6934 const DataLayout &DL = getDataLayout();
6935 KnownBits Known = computeKnownBits(V, DL, &AC, nullptr, &DT);
6936 if (Known.getBitWidth() != BitWidth)
6937 Known = Known.zextOrTrunc(BitWidth);
6938
6939 // ValueTracking may be able to compute a tighter result for the number of
6940 // sign bits than for the value of those sign bits.
6941 unsigned NS = ComputeNumSignBits(V, DL, &AC, nullptr, &DT);
6942 if (U->getType()->isPointerTy()) {
6943 // If the pointer size is larger than the index size type, this can cause
6944 // NS to be larger than BitWidth. So compensate for this.
6945 unsigned ptrSize = DL.getPointerTypeSizeInBits(U->getType());
6946 int ptrIdxDiff = ptrSize - BitWidth;
6947 if (ptrIdxDiff > 0 && ptrSize > BitWidth && NS > (unsigned)ptrIdxDiff)
6948 NS -= ptrIdxDiff;
6949 }
6950
6951 if (NS > 1) {
6952 // If we know any of the sign bits, we know all of the sign bits.
6953 if (!Known.Zero.getHiBits(NS).isZero())
6954 Known.Zero.setHighBits(NS);
6955 if (!Known.One.getHiBits(NS).isZero())
6956 Known.One.setHighBits(NS);
6957 }
6958
6959 if (Known.getMinValue() != Known.getMaxValue() + 1)
6960 ConservativeResult = ConservativeResult.intersectWith(
6961 ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1),
6962 RangeType);
6963 if (NS > 1)
6964 ConservativeResult = ConservativeResult.intersectWith(
6965 ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
6966 APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1),
6967 RangeType);
6968
6969 if (U->getType()->isPointerTy() && SignHint == HINT_RANGE_UNSIGNED) {
6970 // Strengthen the range if the underlying IR value is a
6971 // global/alloca/heap allocation using the size of the object.
6972 bool CanBeNull;
6973 uint64_t DerefBytes = V->getPointerDereferenceableBytes(
6974 DL, CanBeNull, /*CanBeFreed=*/nullptr);
6975 if (DerefBytes > 1 && isUIntN(BitWidth, DerefBytes)) {
6976 // The highest address the object can start is DerefBytes bytes before
6977 // the end (unsigned max value). If this value is not a multiple of the
6978 // alignment, the last possible start value is the next lowest multiple
6979 // of the alignment. Note: The computations below cannot overflow,
6980 // because if they would there's no possible start address for the
6981 // object.
6982 APInt MaxVal =
6983 APInt::getMaxValue(BitWidth) - APInt(BitWidth, DerefBytes);
6984 uint64_t Align = U->getValue()->getPointerAlignment(DL).value();
6985 uint64_t Rem = MaxVal.urem(Align);
6986 MaxVal -= APInt(BitWidth, Rem);
6987 APInt MinVal = APInt::getZero(BitWidth);
6988 if (llvm::isKnownNonZero(V, DL))
6989 MinVal = Align;
6990 ConservativeResult = ConservativeResult.intersectWith(
6991 ConstantRange::getNonEmpty(MinVal, MaxVal + 1), RangeType);
6992 }
6993 }
6994
6995 // A range of Phi is a subset of union of all ranges of its input.
6996 if (PHINode *Phi = dyn_cast<PHINode>(V)) {
6997 // SCEVExpander sometimes creates SCEVUnknowns that are secretly
6998 // AddRecs; return the range for the corresponding AddRec.
6999 if (auto *AR = dyn_cast<SCEVAddRecExpr>(getSCEV(V)))
7000 return getRangeRef(AR, SignHint, Depth + 1);
7001
7002 // Make sure that we do not run over cycled Phis.
7003 if (RangeRefPHIAllowedOperands(DT, Phi)) {
7004 ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false);
7005
7006 for (const auto &Op : Phi->operands()) {
7007 auto OpRange = getRangeRef(getSCEV(Op), SignHint, Depth + 1);
7008 RangeFromOps = RangeFromOps.unionWith(OpRange);
7009 // No point to continue if we already have a full set.
7010 if (RangeFromOps.isFullSet())
7011 break;
7012 }
7013 ConservativeResult =
7014 ConservativeResult.intersectWith(RangeFromOps, RangeType);
7015 }
7016 }
7017
7018 // vscale can't be equal to zero
7019 if (const auto *II = dyn_cast<IntrinsicInst>(V))
7020 if (II->getIntrinsicID() == Intrinsic::vscale) {
7021 ConstantRange Disallowed = APInt::getZero(BitWidth);
7022 ConservativeResult = ConservativeResult.difference(Disallowed);
7023 }
7024
7025 return setRange(U, SignHint, std::move(ConservativeResult));
7026 }
7027 case scCouldNotCompute:
7028 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
7029 }
7030
7031 return setRange(S, SignHint, std::move(ConservativeResult));
7032}
7033
7034// Given a StartRange, Step and MaxBECount for an expression compute a range of
7035// values that the expression can take. Initially, the expression has a value
7036// from StartRange and then is changed by Step up to MaxBECount times. Signed
7037// argument defines if we treat Step as signed or unsigned. The second return
7038// value indicates that no wrapping occurred.
7039static std::pair<ConstantRange, bool>
7041 const APInt &MaxBECount, bool Signed) {
7042 unsigned BitWidth = Step.getBitWidth();
7043 assert(BitWidth == StartRange.getBitWidth() &&
7044 BitWidth == MaxBECount.getBitWidth() && "mismatched bit widths");
7045 // If either Step or MaxBECount is 0, then the expression won't change, and we
7046 // just need to return the initial range.
7047 if (Step == 0 || MaxBECount == 0)
7048 return {StartRange, true};
7049
7050 // If we don't know anything about the initial value (i.e. StartRange is
7051 // FullRange), then we don't know anything about the final range either.
7052 // Return FullRange.
7053 if (StartRange.isFullSet())
7054 return {ConstantRange::getFull(BitWidth), false};
7055
7056 // If Step is signed and negative, then we use its absolute value, but we also
7057 // note that we're moving in the opposite direction.
7058 bool Descending = Signed && Step.isNegative();
7059
7060 if (Signed)
7061 // This is correct even for INT_SMIN. Let's look at i8 to illustrate this:
7062 // abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128.
7063 // This equations hold true due to the well-defined wrap-around behavior of
7064 // APInt.
7065 Step = Step.abs();
7066
7067 // Check if Offset is more than full span of BitWidth. If it is, the
7068 // expression is guaranteed to overflow.
7069 if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount))
7070 return {ConstantRange::getFull(BitWidth), false};
7071
7072 // Offset is by how much the expression can change. Checks above guarantee no
7073 // overflow here.
7074 APInt Offset = Step * MaxBECount;
7075
7076 // Minimum value of the final range will match the minimal value of StartRange
7077 // if the expression is increasing and will be decreased by Offset otherwise.
7078 // Maximum value of the final range will match the maximal value of StartRange
7079 // if the expression is decreasing and will be increased by Offset otherwise.
7080 APInt StartLower = StartRange.getLower();
7081 APInt StartUpper = StartRange.getUpper() - 1;
7082 bool Overflow;
7083 APInt MovedBoundary;
7084 if (Signed) {
7085 // This does not use sadd_ov, as we want to check overflow for a signed
7086 // start with an unsigned offset.
7087 if (Descending) {
7088 MovedBoundary = StartLower - std::move(Offset);
7089 Overflow = MovedBoundary.sgt(StartLower) || StartRange.isSignWrappedSet();
7090 } else {
7091 MovedBoundary = StartUpper + std::move(Offset);
7092 Overflow = MovedBoundary.slt(StartUpper) || StartRange.isSignWrappedSet();
7093 }
7094 } else {
7095 MovedBoundary = StartUpper.uadd_ov(std::move(Offset), Overflow);
7096 Overflow |= StartRange.isWrappedSet();
7097 }
7098
7099 // It's possible that the new minimum/maximum value will fall into the initial
7100 // range (due to wrap around). This means that the expression can take any
7101 // value in this bitwidth, and we have to return full range.
7102 if (StartRange.contains(MovedBoundary))
7103 return {ConstantRange::getFull(BitWidth), false};
7104
7105 APInt NewLower =
7106 Descending ? std::move(MovedBoundary) : std::move(StartLower);
7107 APInt NewUpper =
7108 Descending ? std::move(StartUpper) : std::move(MovedBoundary);
7109 NewUpper += 1;
7110
7111 // No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
7112 return {ConstantRange::getNonEmpty(std::move(NewLower), std::move(NewUpper)),
7113 !Overflow};
7114}
7115
7116std::pair<ConstantRange, SCEV::NoWrapFlags>
7117ScalarEvolution::getRangeForAffineAR(const SCEV *Start, const SCEV *Step,
7118 const APInt &MaxBECount) {
7119 assert(getTypeSizeInBits(Start->getType()) ==
7120 getTypeSizeInBits(Step->getType()) &&
7121 getTypeSizeInBits(Start->getType()) == MaxBECount.getBitWidth() &&
7122 "mismatched bit widths");
7123
7124 // First, consider step signed.
7125 ConstantRange StartSRange = getSignedRange(Start);
7126 ConstantRange StepSRange = getSignedRange(Step);
7127
7128 // If Step can be both positive and negative, we need to find ranges for the
7129 // maximum absolute step values in both directions and union them.
7130 auto [SR1, NSW1] = getRangeForAffineARHelper(
7131 StepSRange.getSignedMin(), StartSRange, MaxBECount, /*Signed=*/true);
7132 auto [SR2, NSW2] = getRangeForAffineARHelper(StepSRange.getSignedMax(),
7133 StartSRange, MaxBECount,
7134 /*Signed=*/true);
7135 ConstantRange SR = SR1.unionWith(SR2);
7136
7137 // Next, consider step unsigned.
7138 auto [UR, NUW] = getRangeForAffineARHelper(
7139 getUnsignedRangeMax(Step), getUnsignedRange(Start), MaxBECount,
7140 /*Signed=*/false);
7141
7143 if (NUW)
7145 if (NSW1 && NSW2)
7147
7148 // Finally, intersect signed and unsigned ranges.
7150}
7151
7152ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR(
7153 const SCEVAddRecExpr *AddRec, const SCEV *MaxBECount, unsigned BitWidth,
7154 ScalarEvolution::RangeSignHint SignHint) {
7155 assert(AddRec->isAffine() && "Non-affine AddRecs are not suppored!\n");
7156 assert(AddRec->hasNoSelfWrap() &&
7157 "This only works for non-self-wrapping AddRecs!");
7158 const bool IsSigned = SignHint == HINT_RANGE_SIGNED;
7159 const SCEV *Step = AddRec->getStepRecurrence(*this);
7160 // Only deal with constant step to save compile time.
7161 if (!isa<SCEVConstant>(Step))
7162 return ConstantRange::getFull(BitWidth);
7163 // Let's make sure that we can prove that we do not self-wrap during
7164 // MaxBECount iterations. We need this because MaxBECount is a maximum
7165 // iteration count estimate, and we might infer nw from some exit for which we
7166 // do not know max exit count (or any other side reasoning).
7167 // TODO: Turn into assert at some point.
7168 if (getTypeSizeInBits(MaxBECount->getType()) >
7169 getTypeSizeInBits(AddRec->getType()))
7170 return ConstantRange::getFull(BitWidth);
7171 MaxBECount = getNoopOrZeroExtend(MaxBECount, AddRec->getType());
7172 const SCEV *RangeWidth = getMinusOne(AddRec->getType());
7173 const SCEV *StepAbs = getUMinExpr(Step, getNegativeSCEV(Step));
7174 const SCEV *MaxItersWithoutWrap = getUDivExpr(RangeWidth, StepAbs);
7175 if (!isKnownPredicateViaConstantRanges(ICmpInst::ICMP_ULE, MaxBECount,
7176 MaxItersWithoutWrap))
7177 return ConstantRange::getFull(BitWidth);
7178
7179 ICmpInst::Predicate LEPred =
7181 ICmpInst::Predicate GEPred =
7183 const SCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
7184
7185 // We know that there is no self-wrap. Let's take Start and End values and
7186 // look at all intermediate values V1, V2, ..., Vn that IndVar takes during
7187 // the iteration. They either lie inside the range [Min(Start, End),
7188 // Max(Start, End)] or outside it:
7189 //
7190 // Case 1: RangeMin ... Start V1 ... VN End ... RangeMax;
7191 // Case 2: RangeMin Vk ... V1 Start ... End Vn ... Vk + 1 RangeMax;
7192 //
7193 // No self wrap flag guarantees that the intermediate values cannot be BOTH
7194 // outside and inside the range [Min(Start, End), Max(Start, End)]. Using that
7195 // knowledge, let's try to prove that we are dealing with Case 1. It is so if
7196 // Start <= End and step is positive, or Start >= End and step is negative.
7197 const SCEV *Start = applyLoopGuards(AddRec->getStart(), AddRec->getLoop());
7198 ConstantRange StartRange = getRangeRef(Start, SignHint);
7199 ConstantRange EndRange = getRangeRef(End, SignHint);
7200 ConstantRange RangeBetween = StartRange.unionWith(EndRange);
7201 // If they already cover full iteration space, we will know nothing useful
7202 // even if we prove what we want to prove.
7203 if (RangeBetween.isFullSet())
7204 return RangeBetween;
7205 // Only deal with ranges that do not wrap (i.e. RangeMin < RangeMax).
7206 bool IsWrappedSet = IsSigned ? RangeBetween.isSignWrappedSet()
7207 : RangeBetween.isWrappedSet();
7208 if (IsWrappedSet)
7209 return ConstantRange::getFull(BitWidth);
7210
7211 if (isKnownPositive(Step) &&
7212 isKnownPredicateViaConstantRanges(LEPred, Start, End))
7213 return RangeBetween;
7214 if (isKnownNegative(Step) &&
7215 isKnownPredicateViaConstantRanges(GEPred, Start, End))
7216 return RangeBetween;
7217 return ConstantRange::getFull(BitWidth);
7218}
7219
7220ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
7221 const SCEV *Step,
7222 const APInt &MaxBECount) {
7223 // RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
7224 // == RangeOf({A,+,P}) union RangeOf({B,+,Q})
7225
7226 unsigned BitWidth = MaxBECount.getBitWidth();
7227 assert(getTypeSizeInBits(Start->getType()) == BitWidth &&
7228 getTypeSizeInBits(Step->getType()) == BitWidth &&
7229 "mismatched bit widths");
7230
7231 struct SelectPattern {
7232 Value *Condition = nullptr;
7233 APInt TrueValue;
7234 APInt FalseValue;
7235
7236 explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth,
7237 const SCEV *S) {
7238 std::optional<unsigned> CastOp;
7239 APInt Offset(BitWidth, 0);
7240
7242 "Should be!");
7243
7244 // Peel off a constant offset. In the future we could consider being
7245 // smarter here and handle {Start+Step,+,Step} too.
7246 const APInt *Off;
7247 if (match(S, m_scev_Add(m_scev_APInt(Off), m_SCEV(S))))
7248 Offset = *Off;
7249
7250 // Peel off a cast operation
7251 if (auto *SCast = dyn_cast<SCEVIntegralCastExpr>(S)) {
7252 CastOp = SCast->getSCEVType();
7253 S = SCast->getOperand();
7254 }
7255
7256 using namespace llvm::PatternMatch;
7257
7258 auto *SU = dyn_cast<SCEVUnknown>(S);
7259 const APInt *TrueVal, *FalseVal;
7260 if (!SU ||
7261 !match(SU->getValue(), m_Select(m_Value(Condition), m_APInt(TrueVal),
7262 m_APInt(FalseVal)))) {
7263 Condition = nullptr;
7264 return;
7265 }
7266
7267 TrueValue = *TrueVal;
7268 FalseValue = *FalseVal;
7269
7270 // Re-apply the cast we peeled off earlier
7271 if (CastOp)
7272 switch (*CastOp) {
7273 default:
7274 llvm_unreachable("Unknown SCEV cast type!");
7275
7276 case scTruncate:
7277 TrueValue = TrueValue.trunc(BitWidth);
7278 FalseValue = FalseValue.trunc(BitWidth);
7279 break;
7280 case scZeroExtend:
7281 TrueValue = TrueValue.zext(BitWidth);
7282 FalseValue = FalseValue.zext(BitWidth);
7283 break;
7284 case scSignExtend:
7285 TrueValue = TrueValue.sext(BitWidth);
7286 FalseValue = FalseValue.sext(BitWidth);
7287 break;
7288 }
7289
7290 // Re-apply the constant offset we peeled off earlier
7291 TrueValue += Offset;
7292 FalseValue += Offset;
7293 }
7294
7295 bool isRecognized() { return Condition != nullptr; }
7296 };
7297
7298 SelectPattern StartPattern(*this, BitWidth, Start);
7299 if (!StartPattern.isRecognized())
7300 return ConstantRange::getFull(BitWidth);
7301
7302 SelectPattern StepPattern(*this, BitWidth, Step);
7303 if (!StepPattern.isRecognized())
7304 return ConstantRange::getFull(BitWidth);
7305
7306 if (StartPattern.Condition != StepPattern.Condition) {
7307 // We don't handle this case today; but we could, by considering four
7308 // possibilities below instead of two. I'm not sure if there are cases where
7309 // that will help over what getRange already does, though.
7310 return ConstantRange::getFull(BitWidth);
7311 }
7312
7313 // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
7314 // construct arbitrary general SCEV expressions here. This function is called
7315 // from deep in the call stack, and calling getSCEV (on a sext instruction,
7316 // say) can end up caching a suboptimal value.
7317
7318 // FIXME: without the explicit `this` receiver below, MSVC errors out with
7319 // C2352 and C2512 (otherwise it isn't needed).
7320
7321 const SCEV *TrueStart = this->getConstant(StartPattern.TrueValue);
7322 const SCEV *TrueStep = this->getConstant(StepPattern.TrueValue);
7323 const SCEV *FalseStart = this->getConstant(StartPattern.FalseValue);
7324 const SCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
7325
7326 ConstantRange TrueRange =
7327 this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount).first;
7328 ConstantRange FalseRange =
7329 this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount).first;
7330
7331 return TrueRange.unionWith(FalseRange);
7332}
7333
7334SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) {
7335 if (isa<ConstantExpr>(V)) return SCEV::FlagAnyWrap;
7336 const BinaryOperator *BinOp = cast<BinaryOperator>(V);
7337
7338 // Return early if there are no flags to propagate to the SCEV.
7340 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(BinOp);
7341 PDI && PDI->isDisjoint()) {
7343 } else {
7344 if (BinOp->hasNoUnsignedWrap())
7346 if (BinOp->hasNoSignedWrap())
7348 }
7349 if (Flags == SCEV::FlagAnyWrap)
7350 return SCEV::FlagAnyWrap;
7351
7352 return isSCEVExprNeverPoison(BinOp) ? Flags : SCEV::FlagAnyWrap;
7353}
7354
7355const Instruction *
7356ScalarEvolution::getNonTrivialDefiningScopeBound(const SCEV *S) {
7357 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S))
7358 return &*AddRec->getLoop()->getHeader()->begin();
7359 if (auto *U = dyn_cast<SCEVUnknown>(S))
7360 if (auto *I = dyn_cast<Instruction>(U->getValue()))
7361 return I;
7362 return nullptr;
7363}
7364
7365const Instruction *ScalarEvolution::getDefiningScopeBound(ArrayRef<SCEVUse> Ops,
7366 bool &Precise) {
7367 Precise = true;
7368 // Do a bounded search of the def relation of the requested SCEVs.
7369 SmallPtrSet<const SCEV *, 16> Visited;
7370 SmallVector<SCEVUse> Worklist;
7371 auto pushOp = [&](const SCEV *S) {
7372 if (!Visited.insert(S).second)
7373 return;
7374 // Threshold of 30 here is arbitrary.
7375 if (Visited.size() > 30) {
7376 Precise = false;
7377 return;
7378 }
7379 Worklist.push_back(S);
7380 };
7381
7382 for (SCEVUse S : Ops)
7383 pushOp(S);
7384
7385 const Instruction *Bound = nullptr;
7386 while (!Worklist.empty()) {
7387 SCEVUse S = Worklist.pop_back_val();
7388 if (auto *DefI = getNonTrivialDefiningScopeBound(S)) {
7389 if (!Bound || DT.dominates(Bound, DefI))
7390 Bound = DefI;
7391 } else {
7392 for (SCEVUse Op : S->operands())
7393 pushOp(Op);
7394 }
7395 }
7396 return Bound ? Bound : &*F.getEntryBlock().begin();
7397}
7398
7399const Instruction *
7400ScalarEvolution::getDefiningScopeBound(ArrayRef<SCEVUse> Ops) {
7401 bool Discard;
7402 return getDefiningScopeBound(Ops, Discard);
7403}
7404
7405bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A,
7406 const Instruction *B) {
7407 if (A->getParent() == B->getParent() &&
7409 B->getIterator()))
7410 return true;
7411
7412 auto *BLoop = LI.getLoopFor(B->getParent());
7413 if (BLoop && BLoop->getHeader() == B->getParent() &&
7414 BLoop->getLoopPreheader() == A->getParent() &&
7416 A->getParent()->end()) &&
7417 isGuaranteedToTransferExecutionToSuccessor(B->getParent()->begin(),
7418 B->getIterator()))
7419 return true;
7420 return false;
7421}
7422
7424 SCEVPoisonCollector PC(/* LookThroughMaybePoisonBlocking */ true);
7425 visitAll(Op, PC);
7426 return PC.MaybePoison.empty();
7427}
7428
7429bool ScalarEvolution::isGuaranteedNotToCauseUB(const SCEV *Op) {
7430 return !SCEVExprContains(Op, [this](const SCEV *S) {
7431 const SCEV *Op1;
7432 bool M = match(S, m_scev_UDiv(m_SCEV(), m_SCEV(Op1)));
7433 // The UDiv may be UB if the divisor is poison or zero. Unless the divisor
7434 // is a non-zero constant, we have to assume the UDiv may be UB.
7435 return M && (!isKnownNonZero(Op1) || !isGuaranteedNotToBePoison(Op1));
7436 });
7437}
7438
7439bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
7440 // Only proceed if we can prove that I does not yield poison.
7442 return false;
7443
7444 // At this point we know that if I is executed, then it does not wrap
7445 // according to at least one of NSW or NUW. If I is not executed, then we do
7446 // not know if the calculation that I represents would wrap. Multiple
7447 // instructions can map to the same SCEV. If we apply NSW or NUW from I to
7448 // the SCEV, we must guarantee no wrapping for that SCEV also when it is
7449 // derived from other instructions that map to the same SCEV. We cannot make
7450 // that guarantee for cases where I is not executed. So we need to find a
7451 // upper bound on the defining scope for the SCEV, and prove that I is
7452 // executed every time we enter that scope. When the bounding scope is a
7453 // loop (the common case), this is equivalent to proving I executes on every
7454 // iteration of that loop.
7455 SmallVector<SCEVUse> SCEVOps;
7456 for (const Use &Op : I->operands()) {
7457 // I could be an extractvalue from a call to an overflow intrinsic.
7458 // TODO: We can do better here in some cases.
7459 if (isSCEVable(Op->getType()))
7460 SCEVOps.push_back(getSCEV(Op));
7461 }
7462 auto *DefI = getDefiningScopeBound(SCEVOps);
7463 return isGuaranteedToTransferExecutionTo(DefI, I);
7464}
7465
7466bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) {
7467 // If we know that \c I can never be poison period, then that's enough.
7468 if (isSCEVExprNeverPoison(I))
7469 return true;
7470
7471 // If the loop only has one exit, then we know that, if the loop is entered,
7472 // any instruction dominating that exit will be executed. If any such
7473 // instruction would result in UB, the addrec cannot be poison.
7474 //
7475 // This is basically the same reasoning as in isSCEVExprNeverPoison(), but
7476 // also handles uses outside the loop header (they just need to dominate the
7477 // single exit).
7478
7479 auto *ExitingBB = L->getExitingBlock();
7480 if (!ExitingBB || !loopHasNoAbnormalExits(L))
7481 return false;
7482
7483 SmallPtrSet<const Value *, 16> KnownPoison;
7485
7486 // We start by assuming \c I, the post-inc add recurrence, is poison. Only
7487 // things that are known to be poison under that assumption go on the
7488 // Worklist.
7489 KnownPoison.insert(I);
7490 Worklist.push_back(I);
7491
7492 while (!Worklist.empty()) {
7493 const Instruction *Poison = Worklist.pop_back_val();
7494
7495 for (const Use &U : Poison->uses()) {
7496 const Instruction *PoisonUser = cast<Instruction>(U.getUser());
7497 if (mustTriggerUB(PoisonUser, KnownPoison) &&
7498 DT.dominates(PoisonUser->getParent(), ExitingBB))
7499 return true;
7500
7501 if (propagatesPoison(U) && L->contains(PoisonUser))
7502 if (KnownPoison.insert(PoisonUser).second)
7503 Worklist.push_back(PoisonUser);
7504 }
7505 }
7506
7507 return false;
7508}
7509
7510ScalarEvolution::LoopProperties
7511ScalarEvolution::getLoopProperties(const Loop *L) {
7512 using LoopProperties = ScalarEvolution::LoopProperties;
7513
7514 auto Itr = LoopPropertiesCache.find(L);
7515 if (Itr == LoopPropertiesCache.end()) {
7516 auto HasSideEffects = [](Instruction *I) {
7517 if (auto *SI = dyn_cast<StoreInst>(I))
7518 return !SI->isSimple();
7519
7520 if (I->mayThrow())
7521 return true;
7522
7523 // Non-volatile memset / memcpy do not count as side-effect for forward
7524 // progress.
7525 if (isa<MemIntrinsic>(I) && !I->isVolatile())
7526 return false;
7527
7528 return I->mayWriteToMemory();
7529 };
7530
7531 LoopProperties LP = {/* HasNoAbnormalExits */ true,
7532 /*HasNoSideEffects*/ true};
7533
7534 for (auto *BB : L->getBlocks())
7535 for (auto &I : *BB) {
7537 LP.HasNoAbnormalExits = false;
7538 if (HasSideEffects(&I))
7539 LP.HasNoSideEffects = false;
7540 if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects)
7541 break; // We're already as pessimistic as we can get.
7542 }
7543
7544 auto InsertPair = LoopPropertiesCache.insert({L, LP});
7545 assert(InsertPair.second && "We just checked!");
7546 Itr = InsertPair.first;
7547 }
7548
7549 return Itr->second;
7550}
7551
7553 // A mustprogress loop without side effects must be finite.
7554 // TODO: The check used here is very conservative. It's only *specific*
7555 // side effects which are well defined in infinite loops.
7556 return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L));
7557}
7558
7559const SCEV *ScalarEvolution::createSCEVIter(Value *V) {
7560 // Worklist item with a Value and a bool indicating whether all operands have
7561 // been visited already.
7564
7565 Stack.emplace_back(V, false);
7566 while (!Stack.empty()) {
7567 auto E = Stack.back();
7568 Value *CurV = E.getPointer();
7569
7570 if (getExistingSCEV(CurV)) {
7571 Stack.pop_back();
7572 continue;
7573 }
7574
7576 const SCEV *CreatedSCEV = nullptr;
7577 // If all operands have been visited already, create the SCEV.
7578 if (E.getInt()) {
7579 CreatedSCEV = createSCEV(CurV);
7580 } else {
7581 // Otherwise get the operands we need to create SCEV's for before creating
7582 // the SCEV for CurV. If the SCEV for CurV can be constructed trivially,
7583 // just use it.
7584 CreatedSCEV = getOperandsToCreate(CurV, Ops);
7585 }
7586
7587 if (CreatedSCEV) {
7588 insertValueToMap(CurV, CreatedSCEV);
7589 Stack.pop_back();
7590 } else {
7591 Stack.back().setInt(true);
7592 // Queue its operands which need to be constructed.
7593 for (Value *Op : Ops)
7594 Stack.emplace_back(Op, false);
7595 }
7596 }
7597
7598 return getExistingSCEV(V);
7599}
7600
7601const SCEV *
7602ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops) {
7603 if (!isSCEVable(V->getType()))
7604 return getUnknown(V);
7605
7606 if (Instruction *I = dyn_cast<Instruction>(V)) {
7607 // Don't attempt to analyze instructions in blocks that aren't
7608 // reachable. Such instructions don't matter, and they aren't required
7609 // to obey basic rules for definitions dominating uses which this
7610 // analysis depends on.
7611 if (!DT.isReachableFromEntry(I->getParent()))
7612 return getUnknown(PoisonValue::get(V->getType()));
7613 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
7614 return getConstant(CI);
7615 else if (isa<GlobalAlias>(V))
7616 return getUnknown(V);
7617 else if (!isa<ConstantExpr>(V))
7618 return getUnknown(V);
7619
7621 if (auto BO =
7623 bool IsConstArg = isa<ConstantInt>(BO->RHS);
7624 switch (BO->Opcode) {
7625 case Instruction::Add:
7626 case Instruction::Mul: {
7627 // For additions and multiplications, traverse add/mul chains for which we
7628 // can potentially create a single SCEV, to reduce the number of
7629 // get{Add,Mul}Expr calls.
7630 do {
7631 if (BO->Op) {
7632 if (BO->Op != V && getExistingSCEV(BO->Op)) {
7633 Ops.push_back(BO->Op);
7634 break;
7635 }
7636 }
7637 Ops.push_back(BO->RHS);
7638 auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT,
7640 if (!NewBO ||
7641 (BO->Opcode == Instruction::Add &&
7642 (NewBO->Opcode != Instruction::Add &&
7643 NewBO->Opcode != Instruction::Sub)) ||
7644 (BO->Opcode == Instruction::Mul &&
7645 NewBO->Opcode != Instruction::Mul)) {
7646 Ops.push_back(BO->LHS);
7647 break;
7648 }
7649 // CreateSCEV calls getNoWrapFlagsFromUB, which under certain conditions
7650 // requires a SCEV for the LHS.
7651 if (BO->Op && (BO->IsNSW || BO->IsNUW)) {
7652 auto *I = dyn_cast<Instruction>(BO->Op);
7653 if (I && programUndefinedIfPoison(I)) {
7654 Ops.push_back(BO->LHS);
7655 break;
7656 }
7657 }
7658 BO = NewBO;
7659 } while (true);
7660 return nullptr;
7661 }
7662 case Instruction::Sub:
7663 case Instruction::UDiv:
7664 case Instruction::URem:
7665 break;
7666 case Instruction::AShr:
7667 case Instruction::Shl:
7668 case Instruction::Xor:
7669 if (!IsConstArg)
7670 return nullptr;
7671 break;
7672 case Instruction::And:
7673 case Instruction::Or:
7674 if (!IsConstArg && !BO->LHS->getType()->isIntegerTy(1))
7675 return nullptr;
7676 break;
7677 case Instruction::LShr:
7678 return getUnknown(V);
7679 default:
7680 llvm_unreachable("Unhandled binop");
7681 break;
7682 }
7683
7684 Ops.push_back(BO->LHS);
7685 Ops.push_back(BO->RHS);
7686 return nullptr;
7687 }
7688
7689 switch (U->getOpcode()) {
7690 case Instruction::Trunc:
7691 case Instruction::ZExt:
7692 case Instruction::SExt:
7693 case Instruction::PtrToAddr:
7694 case Instruction::PtrToInt:
7695 Ops.push_back(U->getOperand(0));
7696 return nullptr;
7697
7698 case Instruction::BitCast:
7699 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType())) {
7700 Ops.push_back(U->getOperand(0));
7701 return nullptr;
7702 }
7703 return getUnknown(V);
7704
7705 case Instruction::SDiv:
7706 case Instruction::SRem:
7707 Ops.push_back(U->getOperand(0));
7708 Ops.push_back(U->getOperand(1));
7709 return nullptr;
7710
7711 case Instruction::GetElementPtr:
7712 assert(cast<GEPOperator>(U)->getSourceElementType()->isSized() &&
7713 "GEP source element type must be sized");
7714 llvm::append_range(Ops, U->operands());
7715 return nullptr;
7716
7717 case Instruction::IntToPtr:
7718 return getUnknown(V);
7719
7720 case Instruction::PHI:
7721 // getNodeForPHI has four ways to turn a PHI into a SCEV; retrieve the
7722 // relevant nodes for each of them.
7723 //
7724 // The first is just to call simplifyInstruction, and get something back
7725 // that isn't a PHI.
7726 if (Value *V = simplifyInstruction(
7727 cast<PHINode>(U),
7728 {getDataLayout(), &TLI, &DT, &AC, /*CtxI=*/nullptr,
7729 /*UseInstrInfo=*/true, /*CanUseUndef=*/false})) {
7730 assert(V);
7731 Ops.push_back(V);
7732 return nullptr;
7733 }
7734 // The second is createNodeForPHIWithIdenticalOperands: this looks for
7735 // operands which all perform the same operation, but haven't been
7736 // CSE'ed for whatever reason.
7737 if (BinaryOperator *BO = getCommonInstForPHI(cast<PHINode>(U))) {
7738 assert(BO);
7739 Ops.push_back(BO);
7740 return nullptr;
7741 }
7742 // The third is createNodeFromSelectLikePHI; this takes a PHI which
7743 // is equivalent to a select, and analyzes it like a select.
7744 {
7745 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
7747 assert(Cond);
7748 assert(LHS);
7749 assert(RHS);
7750 if (auto *CondICmp = dyn_cast<ICmpInst>(Cond)) {
7751 Ops.push_back(CondICmp->getOperand(0));
7752 Ops.push_back(CondICmp->getOperand(1));
7753 }
7754 Ops.push_back(Cond);
7755 Ops.push_back(LHS);
7756 Ops.push_back(RHS);
7757 return nullptr;
7758 }
7759 }
7760 // The fourth way is createAddRecFromPHI. It's complicated to handle here,
7761 // so just construct it recursively.
7762 //
7763 // In addition to getNodeForPHI, also construct nodes which might be needed
7764 // by getRangeRef.
7766 for (Value *V : cast<PHINode>(U)->operands())
7767 Ops.push_back(V);
7768 return nullptr;
7769 }
7770 return nullptr;
7771
7772 case Instruction::Select: {
7773 // Check if U is a select that can be simplified to a SCEVUnknown.
7774 auto CanSimplifyToUnknown = [this, U]() {
7775 if (U->getType()->isIntegerTy(1) || isa<ConstantInt>(U->getOperand(0)))
7776 return false;
7777
7778 auto *ICI = dyn_cast<ICmpInst>(U->getOperand(0));
7779 if (!ICI)
7780 return false;
7781 Value *LHS = ICI->getOperand(0);
7782 Value *RHS = ICI->getOperand(1);
7783 if (ICI->getPredicate() == CmpInst::ICMP_EQ ||
7784 ICI->getPredicate() == CmpInst::ICMP_NE) {
7786 return true;
7787 } else if (getTypeSizeInBits(LHS->getType()) >
7788 getTypeSizeInBits(U->getType()))
7789 return true;
7790 return false;
7791 };
7792 if (CanSimplifyToUnknown())
7793 return getUnknown(U);
7794
7795 llvm::append_range(Ops, U->operands());
7796 return nullptr;
7797 break;
7798 }
7799 case Instruction::Call:
7800 case Instruction::Invoke:
7801 if (Value *RV = cast<CallBase>(U)->getReturnedArgOperand()) {
7802 Ops.push_back(RV);
7803 return nullptr;
7804 }
7805
7806 if (auto *II = dyn_cast<IntrinsicInst>(U)) {
7807 switch (II->getIntrinsicID()) {
7808 case Intrinsic::abs:
7809 Ops.push_back(II->getArgOperand(0));
7810 return nullptr;
7811 case Intrinsic::umax:
7812 case Intrinsic::umin:
7813 case Intrinsic::smax:
7814 case Intrinsic::smin:
7815 case Intrinsic::usub_sat:
7816 case Intrinsic::uadd_sat:
7817 Ops.push_back(II->getArgOperand(0));
7818 Ops.push_back(II->getArgOperand(1));
7819 return nullptr;
7820 case Intrinsic::start_loop_iterations:
7821 case Intrinsic::annotation:
7822 case Intrinsic::ptr_annotation:
7823 Ops.push_back(II->getArgOperand(0));
7824 return nullptr;
7825 default:
7826 break;
7827 }
7828 }
7829 break;
7830 }
7831
7832 return nullptr;
7833}
7834
7835const SCEV *ScalarEvolution::createSCEV(Value *V) {
7836 if (!isSCEVable(V->getType()))
7837 return getUnknown(V);
7838
7839 if (Instruction *I = dyn_cast<Instruction>(V)) {
7840 // Don't attempt to analyze instructions in blocks that aren't
7841 // reachable. Such instructions don't matter, and they aren't required
7842 // to obey basic rules for definitions dominating uses which this
7843 // analysis depends on.
7844 if (!DT.isReachableFromEntry(I->getParent()))
7845 return getUnknown(PoisonValue::get(V->getType()));
7846 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(V))
7847 return getConstant(CI);
7848 else if (isa<GlobalAlias>(V))
7849 return getUnknown(V);
7850 else if (!isa<ConstantExpr>(V))
7851 return getUnknown(V);
7852
7853 const SCEV *LHS;
7854 const SCEV *RHS;
7855
7857 if (auto BO =
7859 switch (BO->Opcode) {
7860 case Instruction::Add: {
7861 // The simple thing to do would be to just call getSCEV on both operands
7862 // and call getAddExpr with the result. However if we're looking at a
7863 // bunch of things all added together, this can be quite inefficient,
7864 // because it leads to N-1 getAddExpr calls for N ultimate operands.
7865 // Instead, gather up all the operands and make a single getAddExpr call.
7866 // LLVM IR canonical form means we need only traverse the left operands.
7868 do {
7869 if (BO->Op) {
7870 if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
7871 AddOps.push_back(OpSCEV);
7872 break;
7873 }
7874
7875 // If a NUW or NSW flag can be applied to the SCEV for this
7876 // addition, then compute the SCEV for this addition by itself
7877 // with a separate call to getAddExpr. We need to do that
7878 // instead of pushing the operands of the addition onto AddOps,
7879 // since the flags are only known to apply to this particular
7880 // addition - they may not apply to other additions that can be
7881 // formed with operands from AddOps.
7882 const SCEV *RHS = getSCEV(BO->RHS);
7883 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
7884 if (Flags != SCEV::FlagAnyWrap) {
7885 const SCEV *LHS = getSCEV(BO->LHS);
7886 if (BO->Opcode == Instruction::Sub)
7887 AddOps.push_back(getMinusSCEV(LHS, RHS, Flags));
7888 else
7889 AddOps.push_back(getAddExpr(LHS, RHS, Flags));
7890 break;
7891 }
7892 }
7893
7894 if (BO->Opcode == Instruction::Sub)
7895 AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS)));
7896 else
7897 AddOps.push_back(getSCEV(BO->RHS));
7898
7899 auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT,
7901 if (!NewBO || (NewBO->Opcode != Instruction::Add &&
7902 NewBO->Opcode != Instruction::Sub)) {
7903 AddOps.push_back(getSCEV(BO->LHS));
7904 break;
7905 }
7906 BO = NewBO;
7907 } while (true);
7908
7909 return getAddExpr(AddOps);
7910 }
7911
7912 case Instruction::Mul: {
7914 do {
7915 if (BO->Op) {
7916 if (auto *OpSCEV = getExistingSCEV(BO->Op)) {
7917 MulOps.push_back(OpSCEV);
7918 break;
7919 }
7920
7921 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(BO->Op);
7922 if (Flags != SCEV::FlagAnyWrap) {
7923 LHS = getSCEV(BO->LHS);
7924 RHS = getSCEV(BO->RHS);
7925 MulOps.push_back(getMulExpr(LHS, RHS, Flags));
7926 break;
7927 }
7928 }
7929
7930 MulOps.push_back(getSCEV(BO->RHS));
7931 auto NewBO = MatchBinaryOp(BO->LHS, getDataLayout(), AC, DT,
7933 if (!NewBO || NewBO->Opcode != Instruction::Mul) {
7934 MulOps.push_back(getSCEV(BO->LHS));
7935 break;
7936 }
7937 BO = NewBO;
7938 } while (true);
7939
7940 return getMulExpr(MulOps);
7941 }
7942 case Instruction::UDiv:
7943 LHS = getSCEV(BO->LHS);
7944 RHS = getSCEV(BO->RHS);
7945 return getUDivExpr(LHS, RHS);
7946 case Instruction::URem:
7947 LHS = getSCEV(BO->LHS);
7948 RHS = getSCEV(BO->RHS);
7949 return getURemExpr(LHS, RHS);
7950 case Instruction::Sub: {
7952 if (BO->Op)
7953 Flags = getNoWrapFlagsFromUB(BO->Op);
7954
7955 // Try to use ptrtoaddr for subtracts with at least one ptrtoint
7956 // operand. While we don't model ptrtoint directly in SCEV, the
7957 // difference between two pointer addresses is well-defined.
7958 Value *PtrLHS = nullptr, *PtrRHS = nullptr;
7959 bool HasPtrLHS = match(BO->LHS, m_PtrToInt(m_Value(PtrLHS)));
7960 bool HasPtrRHS = match(BO->RHS, m_PtrToInt(m_Value(PtrRHS)));
7961 if (HasPtrLHS || HasPtrRHS) {
7962 // Convert a ptrtoint operand (OrigOp) to ptrtoaddr of its pointer
7963 // PtrOp. When only one side is ptrtoint (BothPtr is false), skip
7964 // SCEVUnknown pointers since wrapping them in ptrtoaddr adds no
7965 // useful structure.
7966 auto GetOp = [&](bool HasPtr, Value *PtrOp, Value *OrigOp,
7967 bool BothPtr) -> const SCEV * {
7968 if (!HasPtr)
7969 return getSCEV(OrigOp);
7970 const SCEV *PtrSCEV = getSCEV(PtrOp);
7971 if (BothPtr || !isa<SCEVUnknown>(PtrSCEV)) {
7972 const SCEV *Addr = getPtrToAddrExpr(PtrSCEV);
7973 if (!isa<SCEVCouldNotCompute>(Addr) &&
7974 getTypeSizeInBits(OrigOp->getType()) <=
7975 getTypeSizeInBits(Addr->getType()))
7976 return getTruncateOrNoop(Addr, OrigOp->getType());
7977 }
7978 return getSCEV(OrigOp);
7979 };
7980 const SCEV *L = GetOp(HasPtrLHS, PtrLHS, BO->LHS, HasPtrRHS);
7981 const SCEV *R = GetOp(HasPtrRHS, PtrRHS, BO->RHS, HasPtrLHS);
7982 return getMinusSCEV(L, R, Flags);
7983 }
7984
7985 LHS = getSCEV(BO->LHS);
7986 RHS = getSCEV(BO->RHS);
7987 return getMinusSCEV(LHS, RHS, Flags);
7988 }
7989 case Instruction::And:
7990 // For an expression like x&255 that merely masks off the high bits,
7991 // use zext(trunc(x)) as the SCEV expression.
7992 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
7993 if (CI->isZero())
7994 return getSCEV(BO->RHS);
7995 if (CI->isMinusOne())
7996 return getSCEV(BO->LHS);
7997 const APInt &A = CI->getValue();
7998
7999 // Instcombine's ShrinkDemandedConstant may strip bits out of
8000 // constants, obscuring what would otherwise be a low-bits mask.
8001 // Use computeKnownBits to compute what ShrinkDemandedConstant
8002 // knew about to reconstruct a low-bits mask value.
8003 unsigned LZ = A.countl_zero();
8004 unsigned TZ = A.countr_zero();
8005 unsigned BitWidth = A.getBitWidth();
8006 KnownBits Known(BitWidth);
8007 computeKnownBits(BO->LHS, Known, getDataLayout(), &AC, nullptr, &DT);
8008
8009 APInt EffectiveMask =
8010 APInt::getLowBitsSet(BitWidth, BitWidth - LZ - TZ).shl(TZ);
8011 if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) {
8012 const SCEV *MulCount = getConstant(APInt::getOneBitSet(BitWidth, TZ));
8013 const SCEV *LHS = getSCEV(BO->LHS);
8014 const SCEV *ShiftedLHS = nullptr;
8015 if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) {
8016 if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) {
8017 // For an expression like (x * 8) & 8, simplify the multiply.
8018 unsigned MulZeros = OpC->getAPInt().countr_zero();
8019 unsigned GCD = std::min(MulZeros, TZ);
8020 APInt DivAmt = APInt::getOneBitSet(BitWidth, TZ - GCD);
8022 MulOps.push_back(getConstant(OpC->getAPInt().ashr(GCD)));
8023 append_range(MulOps, LHSMul->operands().drop_front());
8024 const SCEV *NewMul = getMulExpr(MulOps, LHSMul->getNoWrapFlags());
8025 ShiftedLHS = getUDivExpr(NewMul, getConstant(DivAmt));
8026 }
8027 }
8028 if (!ShiftedLHS)
8029 ShiftedLHS = getUDivExpr(LHS, MulCount);
8030 return getMulExpr(
8032 getTruncateExpr(ShiftedLHS,
8033 IntegerType::get(getContext(), BitWidth - LZ - TZ)),
8034 BO->LHS->getType()),
8035 MulCount);
8036 }
8037 }
8038 // Binary `and` is a bit-wise `umin`.
8039 if (BO->LHS->getType()->isIntegerTy(1)) {
8040 LHS = getSCEV(BO->LHS);
8041 RHS = getSCEV(BO->RHS);
8042 return getUMinExpr(LHS, RHS);
8043 }
8044 break;
8045
8046 case Instruction::Or:
8047 // Binary `or` is a bit-wise `umax`.
8048 if (BO->LHS->getType()->isIntegerTy(1)) {
8049 LHS = getSCEV(BO->LHS);
8050 RHS = getSCEV(BO->RHS);
8051 return getUMaxExpr(LHS, RHS);
8052 }
8053 break;
8054
8055 case Instruction::Xor:
8056 if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
8057 // If the RHS of xor is -1, then this is a not operation.
8058 if (CI->isMinusOne())
8059 return getNotSCEV(getSCEV(BO->LHS));
8060
8061 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
8062 // This is a variant of the check for xor with -1, and it handles
8063 // the case where instcombine has trimmed non-demanded bits out
8064 // of an xor with -1.
8065 if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS))
8066 if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1)))
8067 if (LBO->getOpcode() == Instruction::And &&
8068 LCI->getValue() == CI->getValue())
8069 if (const SCEVZeroExtendExpr *Z =
8071 Type *UTy = BO->LHS->getType();
8072 const SCEV *Z0 = Z->getOperand();
8073 Type *Z0Ty = Z0->getType();
8074 unsigned Z0TySize = getTypeSizeInBits(Z0Ty);
8075
8076 // If C is a low-bits mask, the zero extend is serving to
8077 // mask off the high bits. Complement the operand and
8078 // re-apply the zext.
8079 if (CI->getValue().isMask(Z0TySize))
8080 return getZeroExtendExpr(getNotSCEV(Z0), UTy);
8081
8082 // If C is a single bit, it may be in the sign-bit position
8083 // before the zero-extend. In this case, represent the xor
8084 // using an add, which is equivalent, and re-apply the zext.
8085 APInt Trunc = CI->getValue().trunc(Z0TySize);
8086 if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
8087 Trunc.isSignMask())
8088 return getZeroExtendExpr(getAddExpr(Z0, getConstant(Trunc)),
8089 UTy);
8090 }
8091 }
8092 break;
8093
8094 case Instruction::Shl:
8095 // Turn shift left of a constant amount into a multiply.
8096 if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) {
8097 uint32_t BitWidth = cast<IntegerType>(SA->getType())->getBitWidth();
8098
8099 // If the shift count is not less than the bitwidth, the result of
8100 // the shift is undefined. Don't try to analyze it, because the
8101 // resolution chosen here may differ from the resolution chosen in
8102 // other parts of the compiler.
8103 if (SA->getValue().uge(BitWidth))
8104 break;
8105
8106 // We can safely preserve the nuw flag in all cases. It's also safe to
8107 // turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation
8108 // requires special handling. It can be preserved as long as we're not
8109 // left shifting by bitwidth - 1.
8110 auto Flags = SCEV::FlagAnyWrap;
8111 if (BO->Op) {
8112 auto MulFlags = getNoWrapFlagsFromUB(BO->Op);
8113 if (any(MulFlags & SCEV::FlagNSW) &&
8114 (any(MulFlags & SCEV::FlagNUW) ||
8115 SA->getValue().ult(BitWidth - 1)))
8117 if (any(MulFlags & SCEV::FlagNUW))
8119 }
8120
8121 ConstantInt *X = ConstantInt::get(
8122 getContext(), APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
8123 return getMulExpr(getSCEV(BO->LHS), getConstant(X), Flags);
8124 }
8125 break;
8126
8127 case Instruction::AShr:
8128 // AShr X, C, where C is a constant.
8129 ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS);
8130 if (!CI)
8131 break;
8132
8133 Type *OuterTy = BO->LHS->getType();
8135 // If the shift count is not less than the bitwidth, the result of
8136 // the shift is undefined. Don't try to analyze it, because the
8137 // resolution chosen here may differ from the resolution chosen in
8138 // other parts of the compiler.
8139 if (CI->getValue().uge(BitWidth))
8140 break;
8141
8142 if (CI->isZero())
8143 return getSCEV(BO->LHS); // shift by zero --> noop
8144
8145 uint64_t AShrAmt = CI->getZExtValue();
8146 Type *TruncTy = IntegerType::get(getContext(), BitWidth - AShrAmt);
8147
8148 Operator *L = dyn_cast<Operator>(BO->LHS);
8149 const SCEV *AddTruncateExpr = nullptr;
8150 ConstantInt *ShlAmtCI = nullptr;
8151 const SCEV *AddConstant = nullptr;
8152
8153 if (L && L->getOpcode() == Instruction::Add) {
8154 // X = Shl A, n
8155 // Y = Add X, c
8156 // Z = AShr Y, m
8157 // n, c and m are constants.
8158
8159 Operator *LShift = dyn_cast<Operator>(L->getOperand(0));
8160 ConstantInt *AddOperandCI = dyn_cast<ConstantInt>(L->getOperand(1));
8161 if (LShift && LShift->getOpcode() == Instruction::Shl) {
8162 if (AddOperandCI) {
8163 const SCEV *ShlOp0SCEV = getSCEV(LShift->getOperand(0));
8164 ShlAmtCI = dyn_cast<ConstantInt>(LShift->getOperand(1));
8165 // since we truncate to TruncTy, the AddConstant should be of the
8166 // same type, so create a new Constant with type same as TruncTy.
8167 // Also, the Add constant should be shifted right by AShr amount.
8168 APInt AddOperand = AddOperandCI->getValue().ashr(AShrAmt);
8169 AddConstant = getConstant(AddOperand.trunc(BitWidth - AShrAmt));
8170 // we model the expression as sext(add(trunc(A), c << n)), since the
8171 // sext(trunc) part is already handled below, we create a
8172 // AddExpr(TruncExp) which will be used later.
8173 AddTruncateExpr = getTruncateExpr(ShlOp0SCEV, TruncTy);
8174 }
8175 }
8176 } else if (L && L->getOpcode() == Instruction::Shl) {
8177 // X = Shl A, n
8178 // Y = AShr X, m
8179 // Both n and m are constant.
8180
8181 const SCEV *ShlOp0SCEV = getSCEV(L->getOperand(0));
8182 ShlAmtCI = dyn_cast<ConstantInt>(L->getOperand(1));
8183 AddTruncateExpr = getTruncateExpr(ShlOp0SCEV, TruncTy);
8184 }
8185
8186 if (AddTruncateExpr && ShlAmtCI) {
8187 // We can merge the two given cases into a single SCEV statement,
8188 // incase n = m, the mul expression will be 2^0, so it gets resolved to
8189 // a simpler case. The following code handles the two cases:
8190 //
8191 // 1) For a two-shift sext-inreg, i.e. n = m,
8192 // use sext(trunc(x)) as the SCEV expression.
8193 //
8194 // 2) When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV
8195 // expression. We already checked that ShlAmt < BitWidth, so
8196 // the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as
8197 // ShlAmt - AShrAmt < Amt.
8198 const APInt &ShlAmt = ShlAmtCI->getValue();
8199 if (ShlAmt.ult(BitWidth) && ShlAmt.uge(AShrAmt)) {
8200 APInt Mul = APInt::getOneBitSet(BitWidth - AShrAmt,
8201 ShlAmtCI->getZExtValue() - AShrAmt);
8202 const SCEV *CompositeExpr =
8203 getMulExpr(AddTruncateExpr, getConstant(Mul));
8204 if (L->getOpcode() != Instruction::Shl)
8205 CompositeExpr = getAddExpr(CompositeExpr, AddConstant);
8206
8207 return getSignExtendExpr(CompositeExpr, OuterTy);
8208 }
8209 }
8210 break;
8211 }
8212 }
8213
8214 switch (U->getOpcode()) {
8215 case Instruction::Trunc:
8216 return getTruncateExpr(getSCEV(U->getOperand(0)), U->getType());
8217
8218 case Instruction::ZExt:
8219 return getZeroExtendExpr(getSCEV(U->getOperand(0)), U->getType());
8220
8221 case Instruction::SExt:
8222 if (auto BO = MatchBinaryOp(U->getOperand(0), getDataLayout(), AC, DT,
8224 // The NSW flag of a subtract does not always survive the conversion to
8225 // A + (-1)*B. By pushing sign extension onto its operands we are much
8226 // more likely to preserve NSW and allow later AddRec optimisations.
8227 //
8228 // NOTE: This is effectively duplicating this logic from getSignExtend:
8229 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
8230 // but by that point the NSW information has potentially been lost.
8231 if (BO->Opcode == Instruction::Sub && BO->IsNSW) {
8232 Type *Ty = U->getType();
8233 auto *V1 = getSignExtendExpr(getSCEV(BO->LHS), Ty);
8234 auto *V2 = getSignExtendExpr(getSCEV(BO->RHS), Ty);
8235 return getMinusSCEV(V1, V2, SCEV::FlagNSW);
8236 }
8237 }
8238 return getSignExtendExpr(getSCEV(U->getOperand(0)), U->getType());
8239
8240 case Instruction::BitCast:
8241 // BitCasts are no-op casts so we just eliminate the cast.
8242 if (isSCEVable(U->getType()) && isSCEVable(U->getOperand(0)->getType()))
8243 return getSCEV(U->getOperand(0));
8244 break;
8245
8246 case Instruction::PtrToAddr: {
8247 const SCEV *IntOp = getPtrToAddrExpr(getSCEV(U->getOperand(0)));
8248 if (isa<SCEVCouldNotCompute>(IntOp))
8249 return getUnknown(V);
8250 return IntOp;
8251 }
8252
8253 case Instruction::PtrToInt:
8254 // SCEV only models ptrtoaddr.
8255 return getUnknown(V);
8256
8257 case Instruction::IntToPtr:
8258 // Just don't deal with inttoptr casts.
8259 return getUnknown(V);
8260
8261 case Instruction::SDiv:
8262 // If both operands are non-negative, this is just an udiv.
8263 if (isKnownNonNegative(getSCEV(U->getOperand(0))) &&
8264 isKnownNonNegative(getSCEV(U->getOperand(1))))
8265 return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1)));
8266 break;
8267
8268 case Instruction::SRem:
8269 // If both operands are non-negative, this is just an urem.
8270 if (isKnownNonNegative(getSCEV(U->getOperand(0))) &&
8271 isKnownNonNegative(getSCEV(U->getOperand(1))))
8272 return getURemExpr(getSCEV(U->getOperand(0)), getSCEV(U->getOperand(1)));
8273 break;
8274
8275 case Instruction::GetElementPtr:
8276 return createNodeForGEP(cast<GEPOperator>(U));
8277
8278 case Instruction::PHI:
8279 return createNodeForPHI(cast<PHINode>(U));
8280
8281 case Instruction::Select:
8282 return createNodeForSelectOrPHI(U, U->getOperand(0), U->getOperand(1),
8283 U->getOperand(2));
8284
8285 case Instruction::Call:
8286 case Instruction::Invoke:
8287 if (Value *RV = cast<CallBase>(U)->getReturnedArgOperand())
8288 return getSCEV(RV);
8289
8290 if (auto *II = dyn_cast<IntrinsicInst>(U)) {
8291 switch (II->getIntrinsicID()) {
8292 case Intrinsic::abs:
8293 return getAbsExpr(
8294 getSCEV(II->getArgOperand(0)),
8295 /*IsNSW=*/cast<ConstantInt>(II->getArgOperand(1))->isOne());
8296 case Intrinsic::umax:
8297 LHS = getSCEV(II->getArgOperand(0));
8298 RHS = getSCEV(II->getArgOperand(1));
8299 return getUMaxExpr(LHS, RHS);
8300 case Intrinsic::umin:
8301 LHS = getSCEV(II->getArgOperand(0));
8302 RHS = getSCEV(II->getArgOperand(1));
8303 return getUMinExpr(LHS, RHS);
8304 case Intrinsic::smax:
8305 LHS = getSCEV(II->getArgOperand(0));
8306 RHS = getSCEV(II->getArgOperand(1));
8307 return getSMaxExpr(LHS, RHS);
8308 case Intrinsic::smin:
8309 LHS = getSCEV(II->getArgOperand(0));
8310 RHS = getSCEV(II->getArgOperand(1));
8311 return getSMinExpr(LHS, RHS);
8312 case Intrinsic::usub_sat: {
8313 const SCEV *X = getSCEV(II->getArgOperand(0));
8314 const SCEV *Y = getSCEV(II->getArgOperand(1));
8315 const SCEV *ClampedY = getUMinExpr(X, Y);
8316 return getMinusSCEV(X, ClampedY, SCEV::FlagNUW);
8317 }
8318 case Intrinsic::uadd_sat: {
8319 const SCEV *X = getSCEV(II->getArgOperand(0));
8320 const SCEV *Y = getSCEV(II->getArgOperand(1));
8321 const SCEV *ClampedX = getUMinExpr(X, getNotSCEV(Y));
8322 return getAddExpr(ClampedX, Y, SCEV::FlagNUW);
8323 }
8324 case Intrinsic::start_loop_iterations:
8325 case Intrinsic::annotation:
8326 case Intrinsic::ptr_annotation:
8327 // A start_loop_iterations or llvm.annotation or llvm.prt.annotation is
8328 // just eqivalent to the first operand for SCEV purposes.
8329 return getSCEV(II->getArgOperand(0));
8330 case Intrinsic::vscale:
8331 return getVScale(II->getType());
8332 default:
8333 break;
8334 }
8335 }
8336 break;
8337 }
8338
8339 return getUnknown(V);
8340}
8341
8342//===----------------------------------------------------------------------===//
8343// Iteration Count Computation Code
8344//
8345
8347 if (isa<SCEVCouldNotCompute>(ExitCount))
8348 return getCouldNotCompute();
8349
8350 auto *ExitCountType = ExitCount->getType();
8351 assert(ExitCountType->isIntegerTy());
8352 auto *EvalTy = Type::getIntNTy(ExitCountType->getContext(),
8353 1 + ExitCountType->getScalarSizeInBits());
8354 return getTripCountFromExitCount(ExitCount, EvalTy, nullptr);
8355}
8356
8358 Type *EvalTy,
8359 const Loop *L) {
8360 if (isa<SCEVCouldNotCompute>(ExitCount))
8361 return getCouldNotCompute();
8362
8363 unsigned ExitCountSize = getTypeSizeInBits(ExitCount->getType());
8364 unsigned EvalSize = EvalTy->getPrimitiveSizeInBits();
8365
8366 auto CanAddOneWithoutOverflow = [&]() {
8367 ConstantRange ExitCountRange =
8368 getRangeRef(ExitCount, RangeSignHint::HINT_RANGE_UNSIGNED);
8369 if (!ExitCountRange.contains(APInt::getMaxValue(ExitCountSize)))
8370 return true;
8371
8372 return L && isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, ExitCount,
8373 getMinusOne(ExitCount->getType()));
8374 };
8375
8376 // If we need to zero extend the backedge count, check if we can add one to
8377 // it prior to zero extending without overflow. Provided this is safe, it
8378 // allows better simplification of the +1.
8379 if (EvalSize > ExitCountSize && CanAddOneWithoutOverflow())
8380 return getZeroExtendExpr(
8381 getAddExpr(ExitCount, getOne(ExitCount->getType())), EvalTy);
8382
8383 // Get the total trip count from the count by adding 1. This may wrap.
8384 return getAddExpr(getTruncateOrZeroExtend(ExitCount, EvalTy), getOne(EvalTy));
8385}
8386
8387static unsigned getConstantTripCount(const SCEVConstant *ExitCount) {
8388 if (!ExitCount)
8389 return 0;
8390
8391 ConstantInt *ExitConst = ExitCount->getValue();
8392
8393 // Guard against huge trip counts.
8394 if (ExitConst->getValue().getActiveBits() > 32)
8395 return 0;
8396
8397 // In case of integer overflow, this returns 0, which is correct.
8398 return ((unsigned)ExitConst->getZExtValue()) + 1;
8399}
8400
8402 auto *ExitCount = dyn_cast<SCEVConstant>(getBackedgeTakenCount(L, Exact));
8403 return getConstantTripCount(ExitCount);
8404}
8405
8406unsigned
8408 const BasicBlock *ExitingBlock) {
8409 assert(ExitingBlock && "Must pass a non-null exiting block!");
8410 assert(L->isLoopExiting(ExitingBlock) &&
8411 "Exiting block must actually branch out of the loop!");
8412 const SCEVConstant *ExitCount =
8413 dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock));
8414 return getConstantTripCount(ExitCount);
8415}
8416
8418 const Loop *L, SmallVectorImpl<const SCEVPredicate *> *Predicates) {
8419
8420 const auto *MaxExitCount =
8421 Predicates ? getPredicatedConstantMaxBackedgeTakenCount(L, *Predicates)
8423 return getConstantTripCount(dyn_cast<SCEVConstant>(MaxExitCount));
8424}
8425
8427 SmallVector<BasicBlock *, 8> ExitingBlocks;
8428 L->getExitingBlocks(ExitingBlocks);
8429
8430 // An exit with an uncomputable exit count makes the result 1.
8431 if (ExitingBlocks.empty() ||
8432 any_of(ExitingBlocks, [this, L](BasicBlock *ExitingBB) {
8433 return isa<SCEVCouldNotCompute>(getExitCount(L, ExitingBB));
8434 }))
8435 return 1;
8436
8437 LoopGuards Guards = LoopGuards::collect(L, *this);
8438 unsigned Res = 0;
8439 for (BasicBlock *ExitingBB : ExitingBlocks)
8440 Res = std::gcd(
8441 Res, getSmallConstantTripMultiple(getExitCount(L, ExitingBB), Guards));
8442 return Res;
8443}
8444
8445unsigned
8447 const LoopGuards &Guards) {
8448 assert(!isa<SCEVCouldNotCompute>(ExitCount) && "Must be computable!");
8449
8450 // Get the trip count
8451 const SCEV *TCExpr =
8452 getTripCountFromExitCount(applyLoopGuards(ExitCount, Guards));
8453
8454 APInt Multiple = getNonZeroConstantMultiple(TCExpr);
8455 // If a trip multiple is huge (>=2^32), the trip count is still divisible by
8456 // the greatest power of 2 divisor less than 2^32.
8457 return Multiple.getActiveBits() > 32
8458 ? 1U << std::min(31U, Multiple.countTrailingZeros())
8459 : (unsigned)Multiple.getZExtValue();
8460}
8461
8463 const SCEV *ExitCount) {
8464 if (isa<SCEVCouldNotCompute>(ExitCount))
8465 return 1;
8466
8467 return getSmallConstantTripMultiple(ExitCount, LoopGuards::collect(L, *this));
8468}
8469
8470/// Returns the largest constant divisor of the trip count of this loop as a
8471/// normal unsigned value, if possible. This means that the actual trip count is
8472/// always a multiple of the returned value (don't forget the trip count could
8473/// very well be zero as well!).
8474///
8475/// Returns 1 if the trip count is unknown or not guaranteed to be the
8476/// multiple of a constant (which is also the case if the trip count is simply
8477/// constant, use getSmallConstantTripCount for that case), Will also return 1
8478/// if the trip count is very large (>= 2^32).
8479///
8480/// As explained in the comments for getSmallConstantTripCount, this assumes
8481/// that control exits the loop via ExitingBlock.
8482unsigned
8484 const BasicBlock *ExitingBlock) {
8485 assert(ExitingBlock && "Must pass a non-null exiting block!");
8486 assert(L->isLoopExiting(ExitingBlock) &&
8487 "Exiting block must actually branch out of the loop!");
8488 const SCEV *ExitCount = getExitCount(L, ExitingBlock);
8489 return getSmallConstantTripMultiple(L, ExitCount);
8490}
8491
8493 const BasicBlock *ExitingBlock,
8494 ExitCountKind Kind) {
8495 switch (Kind) {
8496 case Exact:
8497 return getBackedgeTakenInfo(L).getExact(ExitingBlock, this);
8498 case SymbolicMaximum:
8499 return getBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock, this);
8500 case ConstantMaximum:
8501 return getBackedgeTakenInfo(L).getConstantMax(ExitingBlock, this);
8502 };
8503 llvm_unreachable("Invalid ExitCountKind!");
8504}
8505
8507 const Loop *L, const BasicBlock *ExitingBlock,
8509 switch (Kind) {
8510 case Exact:
8511 return getPredicatedBackedgeTakenInfo(L).getExact(ExitingBlock, this,
8512 Predicates);
8513 case SymbolicMaximum:
8514 return getPredicatedBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock, this,
8515 Predicates);
8516 case ConstantMaximum:
8517 return getPredicatedBackedgeTakenInfo(L).getConstantMax(ExitingBlock, this,
8518 Predicates);
8519 };
8520 llvm_unreachable("Invalid ExitCountKind!");
8521}
8522
8525 return getPredicatedBackedgeTakenInfo(L).getExact(L, this, &Preds);
8526}
8527
8529 ExitCountKind Kind) {
8530 switch (Kind) {
8531 case Exact:
8532 return getBackedgeTakenInfo(L).getExact(L, this);
8533 case ConstantMaximum:
8534 return getBackedgeTakenInfo(L).getConstantMax(this);
8535 case SymbolicMaximum:
8536 return getBackedgeTakenInfo(L).getSymbolicMax(L, this);
8537 };
8538 llvm_unreachable("Invalid ExitCountKind!");
8539}
8540
8543 return getPredicatedBackedgeTakenInfo(L).getSymbolicMax(L, this, &Preds);
8544}
8545
8548 return getPredicatedBackedgeTakenInfo(L).getConstantMax(this, &Preds);
8549}
8550
8552 return getBackedgeTakenInfo(L).isConstantMaxOrZero(this);
8553}
8554
8555/// Push PHI nodes in the header of the given loop onto the given Worklist.
8556static void PushLoopPHIs(const Loop *L,
8559 BasicBlock *Header = L->getHeader();
8560
8561 // Push all Loop-header PHIs onto the Worklist stack.
8562 for (PHINode &PN : Header->phis())
8563 if (Visited.insert(&PN).second)
8564 Worklist.push_back(&PN);
8565}
8566
8567ScalarEvolution::BackedgeTakenInfo &
8568ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) {
8569 auto &BTI = getBackedgeTakenInfo(L);
8570 if (BTI.hasFullInfo())
8571 return BTI;
8572
8573 auto Pair = PredicatedBackedgeTakenCounts.try_emplace(L);
8574
8575 if (!Pair.second)
8576 return Pair.first->second;
8577
8578 BackedgeTakenInfo Result =
8579 computeBackedgeTakenCount(L, /*AllowPredicates=*/true);
8580
8581 return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result);
8582}
8583
8584ScalarEvolution::BackedgeTakenInfo &
8585ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
8586 // Initially insert an invalid entry for this loop. If the insertion
8587 // succeeds, proceed to actually compute a backedge-taken count and
8588 // update the value. The temporary CouldNotCompute value tells SCEV
8589 // code elsewhere that it shouldn't attempt to request a new
8590 // backedge-taken count, which could result in infinite recursion.
8591 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
8592 BackedgeTakenCounts.try_emplace(L);
8593 if (!Pair.second)
8594 return Pair.first->second;
8595
8596 // computeBackedgeTakenCount may allocate memory for its result. Inserting it
8597 // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
8598 // must be cleared in this scope.
8599 BackedgeTakenInfo Result = computeBackedgeTakenCount(L);
8600
8601 // Now that we know more about the trip count for this loop, forget any
8602 // existing SCEV values for PHI nodes in this loop since they are only
8603 // conservative estimates made without the benefit of trip count
8604 // information. This invalidation is not necessary for correctness, and is
8605 // only done to produce more precise results.
8606 if (Result.hasAnyInfo()) {
8607 // Invalidate any expression using an addrec in this loop.
8608 SmallVector<SCEVUse, 8> ToForget;
8609 auto LoopUsersIt = LoopUsers.find(L);
8610 if (LoopUsersIt != LoopUsers.end())
8611 append_range(ToForget, LoopUsersIt->second);
8612 forgetMemoizedResults(ToForget);
8613
8614 // Invalidate constant-evolved loop header phis.
8615 for (PHINode &PN : L->getHeader()->phis())
8616 ConstantEvolutionLoopExitValue.erase(&PN);
8617 }
8618
8619 // Re-lookup the insert position, since the call to
8620 // computeBackedgeTakenCount above could result in a
8621 // recusive call to getBackedgeTakenInfo (on a different
8622 // loop), which would invalidate the iterator computed
8623 // earlier.
8624 return BackedgeTakenCounts.find(L)->second = std::move(Result);
8625}
8626
8628 // This method is intended to forget all info about loops. It should
8629 // invalidate caches as if the following happened:
8630 // - The trip counts of all loops have changed arbitrarily
8631 // - Every llvm::Value has been updated in place to produce a different
8632 // result.
8633 BackedgeTakenCounts.clear();
8634 PredicatedBackedgeTakenCounts.clear();
8635 BECountUsers.clear();
8636 LoopPropertiesCache.clear();
8637 ConstantEvolutionLoopExitValue.clear();
8638 ValueExprMap.clear();
8639 ValuesAtScopes.clear();
8640 ValuesAtScopesUsers.clear();
8641 LoopDispositions.clear();
8642 BlockDispositions.clear();
8643 UnsignedRanges.clear();
8644 SignedRanges.clear();
8645 ExprValueMap.clear();
8646 HasRecMap.clear();
8647 ConstantMultipleCache.clear();
8648 PredicatedSCEVRewrites.clear();
8649 FoldCache.clear();
8650 FoldCacheUser.clear();
8651}
8652void ScalarEvolution::visitAndClearUsers(
8655 SmallVectorImpl<SCEVUse> &ToForget) {
8656 while (!Worklist.empty()) {
8657 Instruction *I = Worklist.pop_back_val();
8658 if (!isSCEVable(I->getType()) && !isa<WithOverflowInst>(I))
8659 continue;
8660
8662 ValueExprMap.find_as(static_cast<Value *>(I));
8663 if (It != ValueExprMap.end()) {
8664 ToForget.push_back(It->second);
8665 eraseValueFromMap(It->first);
8666 if (PHINode *PN = dyn_cast<PHINode>(I))
8667 ConstantEvolutionLoopExitValue.erase(PN);
8668 }
8669
8670 PushDefUseChildren(I, Worklist, Visited);
8671 }
8672}
8673
8675 SmallVector<const Loop *, 16> LoopWorklist(1, L);
8678 SmallVector<SCEVUse, 16> ToForget;
8679
8680 // Iterate over all the loops and sub-loops to drop SCEV information.
8681 while (!LoopWorklist.empty()) {
8682 auto *CurrL = LoopWorklist.pop_back_val();
8683
8684 // Drop any stored trip count value.
8685 forgetBackedgeTakenCounts(CurrL, /* Predicated */ false);
8686 forgetBackedgeTakenCounts(CurrL, /* Predicated */ true);
8687
8688 // Drop information about predicated SCEV rewrites for this loop.
8689 PredicatedSCEVRewrites.remove_if(
8690 [&](const auto &Entry) { return Entry.first.second == CurrL; });
8691
8692 auto LoopUsersItr = LoopUsers.find(CurrL);
8693 if (LoopUsersItr != LoopUsers.end())
8694 llvm::append_range(ToForget, LoopUsersItr->second);
8695
8696 // Drop information about expressions based on loop-header PHIs.
8697 PushLoopPHIs(CurrL, Worklist, Visited);
8698 visitAndClearUsers(Worklist, Visited, ToForget);
8699
8700 LoopPropertiesCache.erase(CurrL);
8701 // Forget all contained loops too, to avoid dangling entries in the
8702 // ValuesAtScopes map.
8703 LoopWorklist.append(CurrL->begin(), CurrL->end());
8704 }
8705 forgetMemoizedResults(ToForget);
8706}
8707
8709 forgetLoop(L->getOutermostLoop());
8710}
8711
8714 if (!I) return;
8715
8716 // Drop information about expressions based on loop-header PHIs.
8719 SmallVector<SCEVUse, 8> ToForget;
8720 Worklist.push_back(I);
8721 Visited.insert(I);
8722 visitAndClearUsers(Worklist, Visited, ToForget);
8723
8724 forgetMemoizedResults(ToForget);
8725}
8726
8730 SmallVector<SCEVUse, 8> ToForget;
8731 for (Value *V : Values)
8732 if (auto *I = dyn_cast<Instruction>(V))
8733 if (Visited.insert(I).second)
8734 Worklist.push_back(I);
8735 visitAndClearUsers(Worklist, Visited, ToForget);
8736
8737 forgetMemoizedResults(ToForget);
8738}
8739
8741 // If SCEV looked through a trivial LCSSA phi node, we might have SCEV's
8742 // directly using a SCEVUnknown/SCEVAddRec defined in the loop. After an
8743 // extra predecessor is added, this is no longer valid. Find all Unknowns and
8744 // AddRecs defined in the loop and invalidate any SCEV's making use of them.
8745 auto InvalidateValue = [&](Value *Val) {
8746 if (!isSCEVable(Val->getType()))
8747 return;
8748 if (const SCEV *S = getExistingSCEV(Val)) {
8749 struct InvalidationRootCollector {
8750 Loop *L;
8752
8753 InvalidationRootCollector(Loop *L) : L(L) {}
8754
8755 bool follow(const SCEV *S) {
8756 if (auto *SU = dyn_cast<SCEVUnknown>(S)) {
8757 if (auto *I = dyn_cast<Instruction>(SU->getValue()))
8758 if (L->contains(I))
8759 Roots.push_back(S);
8760 } else if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
8761 if (L->contains(AddRec->getLoop()))
8762 Roots.push_back(S);
8763 }
8764 return true;
8765 }
8766 bool isDone() const { return false; }
8767 };
8768
8769 InvalidationRootCollector C(L);
8770 visitAll(S, C);
8771 forgetMemoizedResults(C.Roots);
8772 }
8773 };
8774
8775 InvalidateValue(V);
8776
8777 // If V has a non-SCEV-able type (e.g. {i64, i1} from a with.overflow
8778 // intrinsic), its users (e.g. extractvalue) may have stale SCEV
8779 // expressions referencing loop-internal values.
8780 if (!isSCEVable(V->getType()) &&
8781 any_of(V->incoming_values(), IsaPred<WithOverflowInst>))
8782 for (User *U : V->users())
8783 InvalidateValue(U);
8784 // Also perform the normal invalidation.
8785 forgetValue(V);
8786}
8787
8788void ScalarEvolution::forgetLoopDispositions() { LoopDispositions.clear(); }
8789
8791 // Unless a specific value is passed to invalidation, completely clear both
8792 // caches.
8793 if (!V) {
8794 BlockDispositions.clear();
8795 LoopDispositions.clear();
8796 return;
8797 }
8798
8799 if (!isSCEVable(V->getType()))
8800 return;
8801
8802 const SCEV *S = getExistingSCEV(V);
8803 if (!S)
8804 return;
8805
8806 // Invalidate the block and loop dispositions cached for S. Dispositions of
8807 // S's users may change if S's disposition changes (i.e. a user may change to
8808 // loop-invariant, if S changes to loop invariant), so also invalidate
8809 // dispositions of S's users recursively.
8810 SmallVector<SCEVUse, 8> Worklist = {S};
8812 while (!Worklist.empty()) {
8813 const SCEV *Curr = Worklist.pop_back_val();
8814 bool LoopDispoRemoved = LoopDispositions.erase(Curr);
8815 bool BlockDispoRemoved = BlockDispositions.erase(Curr);
8816 if (!LoopDispoRemoved && !BlockDispoRemoved)
8817 continue;
8818 auto Users = SCEVUsers.find(Curr);
8819 if (Users != SCEVUsers.end())
8820 for (const auto *User : Users->second)
8821 if (Seen.insert(User).second)
8822 Worklist.push_back(User);
8823 }
8824}
8825
8826/// Get the exact loop backedge taken count considering all loop exits. A
8827/// computable result can only be returned for loops with all exiting blocks
8828/// dominating the latch. howFarToZero assumes that the limit of each loop test
8829/// is never skipped. This is a valid assumption as long as the loop exits via
8830/// that test. For precise results, it is the caller's responsibility to specify
8831/// the relevant loop exiting block using getExact(ExitingBlock, SE).
8832const SCEV *ScalarEvolution::BackedgeTakenInfo::getExact(
8833 const Loop *L, ScalarEvolution *SE,
8835 // If any exits were not computable, the loop is not computable.
8836 if (!isComplete() || ExitNotTaken.empty())
8837 return SE->getCouldNotCompute();
8838
8839 const BasicBlock *Latch = L->getLoopLatch();
8840 // All exiting blocks we have collected must dominate the only backedge.
8841 if (!Latch)
8842 return SE->getCouldNotCompute();
8843
8844 // All exiting blocks we have gathered dominate loop's latch, so exact trip
8845 // count is simply a minimum out of all these calculated exit counts.
8847 for (const auto &ENT : ExitNotTaken) {
8848 const SCEV *BECount = ENT.ExactNotTaken;
8849 assert(BECount != SE->getCouldNotCompute() && "Bad exit SCEV!");
8850 assert(SE->DT.dominates(ENT.ExitingBlock, Latch) &&
8851 "We should only have known counts for exiting blocks that dominate "
8852 "latch!");
8853
8854 Ops.push_back(BECount);
8855
8856 if (Preds)
8857 append_range(*Preds, ENT.Predicates);
8858
8859 assert((Preds || ENT.hasAlwaysTruePredicate()) &&
8860 "Predicate should be always true!");
8861 }
8862
8863 // If an earlier exit exits on the first iteration (exit count zero), then
8864 // a later poison exit count should not propagate into the result. This are
8865 // exactly the semantics provided by umin_seq.
8866 return SE->getUMinFromMismatchedTypes(Ops, /* Sequential */ true);
8867}
8868
8869const ScalarEvolution::ExitNotTakenInfo *
8870ScalarEvolution::BackedgeTakenInfo::getExitNotTaken(
8871 const BasicBlock *ExitingBlock,
8872 SmallVectorImpl<const SCEVPredicate *> *Predicates) const {
8873 for (const auto &ENT : ExitNotTaken)
8874 if (ENT.ExitingBlock == ExitingBlock) {
8875 if (ENT.hasAlwaysTruePredicate())
8876 return &ENT;
8877 else if (Predicates) {
8878 append_range(*Predicates, ENT.Predicates);
8879 return &ENT;
8880 }
8881 }
8882
8883 return nullptr;
8884}
8885
8886/// getConstantMax - Get the constant max backedge taken count for the loop.
8887const SCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax(
8888 ScalarEvolution *SE,
8889 SmallVectorImpl<const SCEVPredicate *> *Predicates) const {
8890 if (!getConstantMax())
8891 return SE->getCouldNotCompute();
8892
8893 for (const auto &ENT : ExitNotTaken)
8894 if (!ENT.hasAlwaysTruePredicate()) {
8895 if (!Predicates)
8896 return SE->getCouldNotCompute();
8897 append_range(*Predicates, ENT.Predicates);
8898 }
8899
8900 assert((isa<SCEVCouldNotCompute>(getConstantMax()) ||
8901 isa<SCEVConstant>(getConstantMax())) &&
8902 "No point in having a non-constant max backedge taken count!");
8903 return getConstantMax();
8904}
8905
8906const SCEV *ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(
8907 const Loop *L, ScalarEvolution *SE,
8908 SmallVectorImpl<const SCEVPredicate *> *Predicates) {
8909 if (!SymbolicMax) {
8910 // Form an expression for the maximum exit count possible for this loop. We
8911 // merge the max and exact information to approximate a version of
8912 // getConstantMaxBackedgeTakenCount which isn't restricted to just
8913 // constants.
8914 SmallVector<SCEVUse, 4> ExitCounts;
8915
8916 for (const auto &ENT : ExitNotTaken) {
8917 const SCEV *ExitCount = ENT.SymbolicMaxNotTaken;
8918 if (!isa<SCEVCouldNotCompute>(ExitCount)) {
8919 assert(SE->DT.dominates(ENT.ExitingBlock, L->getLoopLatch()) &&
8920 "We should only have known counts for exiting blocks that "
8921 "dominate latch!");
8922 ExitCounts.push_back(ExitCount);
8923 if (Predicates)
8924 append_range(*Predicates, ENT.Predicates);
8925
8926 assert((Predicates || ENT.hasAlwaysTruePredicate()) &&
8927 "Predicate should be always true!");
8928 }
8929 }
8930 if (ExitCounts.empty())
8931 SymbolicMax = SE->getCouldNotCompute();
8932 else
8933 SymbolicMax =
8934 SE->getUMinFromMismatchedTypes(ExitCounts, /*Sequential*/ true);
8935 }
8936 return SymbolicMax;
8937}
8938
8939bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero(
8940 ScalarEvolution *SE) const {
8941 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
8942 return !ENT.hasAlwaysTruePredicate();
8943 };
8944 return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue);
8945}
8946
8949
8951 const SCEV *E, const SCEV *ConstantMaxNotTaken,
8952 const SCEV *SymbolicMaxNotTaken, bool MaxOrZero,
8956 // If we prove the max count is zero, so is the symbolic bound. This happens
8957 // in practice due to differences in a) how context sensitive we've chosen
8958 // to be and b) how we reason about bounds implied by UB.
8959 if (ConstantMaxNotTaken->isZero()) {
8960 this->ExactNotTaken = E = ConstantMaxNotTaken;
8961 this->SymbolicMaxNotTaken = SymbolicMaxNotTaken = ConstantMaxNotTaken;
8962 }
8963
8966 "Exact is not allowed to be less precise than Constant Max");
8969 "Exact is not allowed to be less precise than Symbolic Max");
8972 "Symbolic Max is not allowed to be less precise than Constant Max");
8975 "No point in having a non-constant max backedge taken count!");
8977 for (const auto PredList : PredLists)
8978 for (const auto *P : PredList) {
8979 if (SeenPreds.contains(P))
8980 continue;
8981 assert(!isa<SCEVUnionPredicate>(P) && "Only add leaf predicates here!");
8982 SeenPreds.insert(P);
8983 Predicates.push_back(P);
8984 }
8985 assert((isa<SCEVCouldNotCompute>(E) || !E->getType()->isPointerTy()) &&
8986 "Backedge count should be int");
8988 !ConstantMaxNotTaken->getType()->isPointerTy()) &&
8989 "Max backedge count should be int");
8990}
8991
8999
9000/// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
9001/// computable exit into a persistent ExitNotTakenInfo array.
9002ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
9004 bool IsComplete, const SCEV *ConstantMax, bool MaxOrZero)
9005 : ConstantMax(ConstantMax), IsComplete(IsComplete), MaxOrZero(MaxOrZero) {
9006 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
9007
9008 ExitNotTaken.reserve(ExitCounts.size());
9009 std::transform(ExitCounts.begin(), ExitCounts.end(),
9010 std::back_inserter(ExitNotTaken),
9011 [&](const EdgeExitInfo &EEI) {
9012 BasicBlock *ExitBB = EEI.first;
9013 const ExitLimit &EL = EEI.second;
9014 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken,
9015 EL.ConstantMaxNotTaken, EL.SymbolicMaxNotTaken,
9016 EL.Predicates);
9017 });
9018 assert((isa<SCEVCouldNotCompute>(ConstantMax) ||
9019 isa<SCEVConstant>(ConstantMax)) &&
9020 "No point in having a non-constant max backedge taken count!");
9021}
9022
9023/// Compute the number of times the backedge of the specified loop will execute.
9024ScalarEvolution::BackedgeTakenInfo
9025ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
9026 bool AllowPredicates) {
9027 SmallVector<BasicBlock *, 8> ExitingBlocks;
9028 L->getExitingBlocks(ExitingBlocks);
9029
9030 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
9031
9033 bool CouldComputeBECount = true;
9034 BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
9035 const SCEV *MustExitMaxBECount = nullptr;
9036 const SCEV *MayExitMaxBECount = nullptr;
9037 bool MustExitMaxOrZero = false;
9038 bool IsOnlyExit = ExitingBlocks.size() == 1;
9039
9040 // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
9041 // and compute maxBECount.
9042 // Do a union of all the predicates here.
9043 for (BasicBlock *ExitBB : ExitingBlocks) {
9044 // We canonicalize untaken exits to br (constant), ignore them so that
9045 // proving an exit untaken doesn't negatively impact our ability to reason
9046 // about the loop as whole.
9047 if (auto *BI = dyn_cast<CondBrInst>(ExitBB->getTerminator()))
9048 if (auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
9049 bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
9050 if (ExitIfTrue == CI->isZero())
9051 continue;
9052 }
9053
9054 ExitLimit EL = computeExitLimit(L, ExitBB, IsOnlyExit, AllowPredicates);
9055
9056 assert((AllowPredicates || EL.Predicates.empty()) &&
9057 "Predicated exit limit when predicates are not allowed!");
9058
9059 // 1. For each exit that can be computed, add an entry to ExitCounts.
9060 // CouldComputeBECount is true only if all exits can be computed.
9061 if (EL.ExactNotTaken != getCouldNotCompute())
9062 ++NumExitCountsComputed;
9063 else
9064 // We couldn't compute an exact value for this exit, so
9065 // we won't be able to compute an exact value for the loop.
9066 CouldComputeBECount = false;
9067 // Remember exit count if either exact or symbolic is known. Because
9068 // Exact always implies symbolic, only check symbolic.
9069 if (EL.SymbolicMaxNotTaken != getCouldNotCompute())
9070 ExitCounts.emplace_back(ExitBB, EL);
9071 else {
9072 assert(EL.ExactNotTaken == getCouldNotCompute() &&
9073 "Exact is known but symbolic isn't?");
9074 ++NumExitCountsNotComputed;
9075 }
9076
9077 // 2. Derive the loop's MaxBECount from each exit's max number of
9078 // non-exiting iterations. Partition the loop exits into two kinds:
9079 // LoopMustExits and LoopMayExits.
9080 //
9081 // If the exit dominates the loop latch, it is a LoopMustExit otherwise it
9082 // is a LoopMayExit. If any computable LoopMustExit is found, then
9083 // MaxBECount is the minimum EL.ConstantMaxNotTaken of computable
9084 // LoopMustExits. Otherwise, MaxBECount is conservatively the maximum
9085 // EL.ConstantMaxNotTaken, where CouldNotCompute is considered greater than
9086 // any
9087 // computable EL.ConstantMaxNotTaken.
9088 if (EL.ConstantMaxNotTaken != getCouldNotCompute() && Latch &&
9089 DT.dominates(ExitBB, Latch)) {
9090 if (!MustExitMaxBECount) {
9091 MustExitMaxBECount = EL.ConstantMaxNotTaken;
9092 MustExitMaxOrZero = EL.MaxOrZero;
9093 } else {
9094 MustExitMaxBECount = getUMinFromMismatchedTypes(MustExitMaxBECount,
9095 EL.ConstantMaxNotTaken);
9096 }
9097 } else if (MayExitMaxBECount != getCouldNotCompute()) {
9098 if (!MayExitMaxBECount || EL.ConstantMaxNotTaken == getCouldNotCompute())
9099 MayExitMaxBECount = EL.ConstantMaxNotTaken;
9100 else {
9101 MayExitMaxBECount = getUMaxFromMismatchedTypes(MayExitMaxBECount,
9102 EL.ConstantMaxNotTaken);
9103 }
9104 }
9105 }
9106 const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
9107 (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute());
9108 // The loop backedge will be taken the maximum or zero times if there's
9109 // a single exit that must be taken the maximum or zero times.
9110 bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1);
9111
9112 // Remember which SCEVs are used in exit limits for invalidation purposes.
9113 // We only care about non-constant SCEVs here, so we can ignore
9114 // EL.ConstantMaxNotTaken
9115 // and MaxBECount, which must be SCEVConstant.
9116 for (const auto &Pair : ExitCounts) {
9117 if (!isa<SCEVConstant>(Pair.second.ExactNotTaken))
9118 BECountUsers[Pair.second.ExactNotTaken].insert({L, AllowPredicates});
9119 if (!isa<SCEVConstant>(Pair.second.SymbolicMaxNotTaken))
9120 BECountUsers[Pair.second.SymbolicMaxNotTaken].insert(
9121 {L, AllowPredicates});
9122 }
9123 return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount,
9124 MaxBECount, MaxOrZero);
9125}
9126
9127ScalarEvolution::ExitLimit
9128ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
9129 bool IsOnlyExit, bool AllowPredicates) {
9130 assert(L->contains(ExitingBlock) && "Exit count for non-loop block?");
9131 // If our exiting block does not dominate the latch, then its connection with
9132 // loop's exit limit may be far from trivial.
9133 const BasicBlock *Latch = L->getLoopLatch();
9134 if (!Latch || !DT.dominates(ExitingBlock, Latch))
9135 return getCouldNotCompute();
9136
9137 Instruction *Term = ExitingBlock->getTerminator();
9138 if (CondBrInst *BI = dyn_cast<CondBrInst>(Term)) {
9139 bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
9140 assert(ExitIfTrue == L->contains(BI->getSuccessor(1)) &&
9141 "It should have one successor in loop and one exit block!");
9142 // Proceed to the next level to examine the exit condition expression.
9143 return computeExitLimitFromCond(L, BI->getCondition(), ExitIfTrue,
9144 /*ControlsOnlyExit=*/IsOnlyExit,
9145 AllowPredicates);
9146 }
9147
9148 if (SwitchInst *SI = dyn_cast<SwitchInst>(Term)) {
9149 // For switch, make sure that there is a single exit from the loop.
9150 BasicBlock *Exit = nullptr;
9151 for (auto *SBB : successors(ExitingBlock))
9152 if (!L->contains(SBB)) {
9153 if (Exit) // Multiple exit successors.
9154 return getCouldNotCompute();
9155 Exit = SBB;
9156 }
9157 assert(Exit && "Exiting block must have at least one exit");
9158 return computeExitLimitFromSingleExitSwitch(
9159 L, SI, Exit, /*ControlsOnlyExit=*/IsOnlyExit);
9160 }
9161
9162 return getCouldNotCompute();
9163}
9164
9166 const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit,
9167 bool AllowPredicates) {
9168 ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates);
9169 return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue,
9170 ControlsOnlyExit, AllowPredicates);
9171}
9172
9173std::optional<ScalarEvolution::ExitLimit>
9174ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond,
9175 bool ExitIfTrue, bool ControlsOnlyExit,
9176 bool AllowPredicates) {
9177 (void)this->L;
9178 (void)this->ExitIfTrue;
9179 (void)this->AllowPredicates;
9180
9181 assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
9182 this->AllowPredicates == AllowPredicates &&
9183 "Variance in assumed invariant key components!");
9184 auto Itr = TripCountMap.find({ExitCond, ControlsOnlyExit});
9185 if (Itr == TripCountMap.end())
9186 return std::nullopt;
9187 return Itr->second;
9188}
9189
9190void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond,
9191 bool ExitIfTrue,
9192 bool ControlsOnlyExit,
9193 bool AllowPredicates,
9194 const ExitLimit &EL) {
9195 assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
9196 this->AllowPredicates == AllowPredicates &&
9197 "Variance in assumed invariant key components!");
9198
9199 auto InsertResult = TripCountMap.insert({{ExitCond, ControlsOnlyExit}, EL});
9200 assert(InsertResult.second && "Expected successful insertion!");
9201 (void)InsertResult;
9202 (void)ExitIfTrue;
9203}
9204
9205ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
9206 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
9207 bool ControlsOnlyExit, bool AllowPredicates) {
9208
9209 if (auto MaybeEL = Cache.find(L, ExitCond, ExitIfTrue, ControlsOnlyExit,
9210 AllowPredicates))
9211 return *MaybeEL;
9212
9213 ExitLimit EL = computeExitLimitFromCondImpl(
9214 Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates);
9215 Cache.insert(L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates, EL);
9216 return EL;
9217}
9218
9219ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
9220 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
9221 bool ControlsOnlyExit, bool AllowPredicates) {
9222 // Handle BinOp conditions (And, Or).
9223 if (auto LimitFromBinOp = computeExitLimitFromCondFromBinOp(
9224 Cache, L, ExitCond, ExitIfTrue, AllowPredicates))
9225 return *LimitFromBinOp;
9226
9227 // With an icmp, it may be feasible to compute an exact backedge-taken count.
9228 // Proceed to the next level to examine the icmp.
9229 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) {
9230 ExitLimit EL =
9231 computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsOnlyExit);
9232 if (EL.hasFullInfo() || !AllowPredicates)
9233 return EL;
9234
9235 // Try again, but use SCEV predicates this time.
9236 return computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue,
9237 ControlsOnlyExit,
9238 /*AllowPredicates=*/true);
9239 }
9240
9241 // Check for a constant condition. These are normally stripped out by
9242 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
9243 // preserve the CFG and is temporarily leaving constant conditions
9244 // in place.
9245 if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
9246 if (ExitIfTrue == !CI->getZExtValue())
9247 // The backedge is always taken.
9248 return getCouldNotCompute();
9249 // The backedge is never taken.
9250 return getZero(CI->getType());
9251 }
9252
9253 // If we're exiting based on the overflow flag of an x.with.overflow intrinsic
9254 // with a constant step, we can form an equivalent icmp predicate and figure
9255 // out how many iterations will be taken before we exit.
9256 const WithOverflowInst *WO;
9257 const APInt *C;
9258 if (match(ExitCond, m_ExtractValue<1>(m_WithOverflowInst(WO))) &&
9259 match(WO->getRHS(), m_APInt(C))) {
9260 ConstantRange NWR =
9262 WO->getNoWrapKind());
9263 CmpInst::Predicate Pred;
9264 APInt NewRHSC, Offset;
9265 NWR.getEquivalentICmp(Pred, NewRHSC, Offset);
9266 if (!ExitIfTrue)
9267 Pred = ICmpInst::getInversePredicate(Pred);
9268 auto *LHS = getSCEV(WO->getLHS());
9269 if (Offset != 0)
9271 auto EL = computeExitLimitFromICmp(L, Pred, LHS, getConstant(NewRHSC),
9272 ControlsOnlyExit, AllowPredicates);
9273 if (EL.hasAnyInfo())
9274 return EL;
9275 }
9276
9277 // If it's not an integer or pointer comparison then compute it the hard way.
9278 return computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
9279}
9280
9281std::optional<ScalarEvolution::ExitLimit>
9282ScalarEvolution::computeExitLimitFromCondFromBinOp(ExitLimitCacheTy &Cache,
9283 const Loop *L,
9284 Value *ExitCond,
9285 bool ExitIfTrue,
9286 bool AllowPredicates) {
9287 // Check if the controlling expression for this loop is an And or Or.
9288 Value *Op0, *Op1;
9289 bool IsAnd;
9290 if (match(ExitCond, m_LogicalAnd(m_Value(Op0), m_Value(Op1))))
9291 IsAnd = true;
9292 else if (match(ExitCond, m_LogicalOr(m_Value(Op0), m_Value(Op1))))
9293 IsAnd = false;
9294 else
9295 return std::nullopt;
9296
9297 // A sub-condition of a non-trivial binop never solely controls the exit,
9298 // whether we exit always depends on both conditions.
9299 ExitLimit EL0 = computeExitLimitFromCondCached(
9300 Cache, L, Op0, ExitIfTrue, /*ControlsOnlyExit=*/false, AllowPredicates);
9301 ExitLimit EL1 = computeExitLimitFromCondCached(
9302 Cache, L, Op1, ExitIfTrue, /*ControlsOnlyExit=*/false, AllowPredicates);
9303
9304 // EitherMayExit is true in these two cases:
9305 // br (and Op0 Op1), loop, exit
9306 // br (or Op0 Op1), exit, loop
9307 bool EitherMayExit = IsAnd ^ ExitIfTrue;
9308
9309 const SCEV *BECount = getCouldNotCompute();
9310 const SCEV *ConstantMaxBECount = getCouldNotCompute();
9311 const SCEV *SymbolicMaxBECount = getCouldNotCompute();
9312 if (EitherMayExit) {
9313 bool UseSequentialUMin = !isa<BinaryOperator>(ExitCond);
9314 // Both conditions must be same for the loop to continue executing.
9315 // Choose the less conservative count.
9316 if (EL0.ExactNotTaken != getCouldNotCompute() &&
9317 EL1.ExactNotTaken != getCouldNotCompute()) {
9318 BECount = getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken,
9319 UseSequentialUMin);
9320 }
9321 if (EL0.ConstantMaxNotTaken == getCouldNotCompute())
9322 ConstantMaxBECount = EL1.ConstantMaxNotTaken;
9323 else if (EL1.ConstantMaxNotTaken == getCouldNotCompute())
9324 ConstantMaxBECount = EL0.ConstantMaxNotTaken;
9325 else
9326 ConstantMaxBECount = getUMinFromMismatchedTypes(EL0.ConstantMaxNotTaken,
9327 EL1.ConstantMaxNotTaken);
9328 if (EL0.SymbolicMaxNotTaken == getCouldNotCompute())
9329 SymbolicMaxBECount = EL1.SymbolicMaxNotTaken;
9330 else if (EL1.SymbolicMaxNotTaken == getCouldNotCompute())
9331 SymbolicMaxBECount = EL0.SymbolicMaxNotTaken;
9332 else
9333 SymbolicMaxBECount = getUMinFromMismatchedTypes(
9334 EL0.SymbolicMaxNotTaken, EL1.SymbolicMaxNotTaken, UseSequentialUMin);
9335 } else {
9336 // Both conditions must be same at the same time for the loop to exit.
9337 // For now, be conservative.
9338 if (EL0.ExactNotTaken == EL1.ExactNotTaken)
9339 BECount = EL0.ExactNotTaken;
9340 }
9341
9342 // There are cases (e.g. PR26207) where computeExitLimitFromCond is able
9343 // to be more aggressive when computing BECount than when computing
9344 // ConstantMaxBECount. In these cases it is possible for EL0.ExactNotTaken
9345 // and
9346 // EL1.ExactNotTaken to match, but for EL0.ConstantMaxNotTaken and
9347 // EL1.ConstantMaxNotTaken to not.
9348 if (isa<SCEVCouldNotCompute>(ConstantMaxBECount) &&
9349 !isa<SCEVCouldNotCompute>(BECount))
9350 ConstantMaxBECount = getConstant(getUnsignedRangeMax(BECount));
9351 if (isa<SCEVCouldNotCompute>(SymbolicMaxBECount))
9352 SymbolicMaxBECount =
9353 isa<SCEVCouldNotCompute>(BECount) ? ConstantMaxBECount : BECount;
9354 return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false,
9355 {ArrayRef(EL0.Predicates), ArrayRef(EL1.Predicates)});
9356}
9357
9358ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
9359 const Loop *L, ICmpInst *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit,
9360 bool AllowPredicates) {
9361 // If the condition was exit on true, convert the condition to exit on false
9362 CmpPredicate Pred;
9363 if (!ExitIfTrue)
9364 Pred = ExitCond->getCmpPredicate();
9365 else
9366 Pred = ExitCond->getInverseCmpPredicate();
9367 const ICmpInst::Predicate OriginalPred = Pred;
9368
9369 const SCEV *LHS = getSCEV(ExitCond->getOperand(0));
9370 const SCEV *RHS = getSCEV(ExitCond->getOperand(1));
9371
9372 ExitLimit EL = computeExitLimitFromICmp(L, Pred, LHS, RHS, ControlsOnlyExit,
9373 AllowPredicates);
9374 if (EL.hasAnyInfo())
9375 return EL;
9376
9377 auto *ExhaustiveCount =
9378 computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
9379
9380 if (!isa<SCEVCouldNotCompute>(ExhaustiveCount))
9381 return ExhaustiveCount;
9382
9383 return computeShiftCompareExitLimit(ExitCond->getOperand(0),
9384 ExitCond->getOperand(1), L, OriginalPred);
9385}
9386ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
9387 const Loop *L, CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS,
9388 bool ControlsOnlyExit, bool AllowPredicates) {
9389
9390 // Try to evaluate any dependencies out of the loop.
9391 LHS = getSCEVAtScope(LHS, L);
9392 RHS = getSCEVAtScope(RHS, L);
9393
9394 // At this point, we would like to compute how many iterations of the
9395 // loop the predicate will return true for these inputs.
9396 if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) {
9397 // If there is a loop-invariant, force it into the RHS.
9398 std::swap(LHS, RHS);
9400 }
9401
9402 bool ControllingFiniteLoop = ControlsOnlyExit && loopHasNoAbnormalExits(L) &&
9404 // Simplify the operands before analyzing them.
9405 (void)SimplifyICmpOperands(Pred, LHS, RHS, /*Depth=*/0);
9406
9407 // If we have a comparison of a chrec against a constant, try to use value
9408 // ranges to answer this query.
9409 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
9410 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
9411 if (AddRec->getLoop() == L) {
9412 // Form the constant range.
9413 ConstantRange CompRange =
9414 ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt());
9415
9416 const SCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
9417 if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
9418 }
9419
9420 // If this loop must exit based on this condition (or execute undefined
9421 // behaviour), see if we can improve wrap flags. This is essentially
9422 // a must execute style proof.
9423 if (ControllingFiniteLoop && isLoopInvariant(RHS, L)) {
9424 // If we can prove the test sequence produced must repeat the same values
9425 // on self-wrap of the IV, then we can infer that IV doesn't self wrap
9426 // because if it did, we'd have an infinite (undefined) loop.
9427 // TODO: We can peel off any functions which are invertible *in L*. Loop
9428 // invariant terms are effectively constants for our purposes here.
9429 SCEVUse InnerLHS = LHS;
9430 if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS))
9431 InnerLHS = ZExt->getOperand();
9432 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS);
9433 AR && !AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() &&
9434 isKnownToBeAPowerOfTwo(AR->getStepRecurrence(*this), /*OrZero=*/true,
9435 /*OrNegative=*/true)) {
9436 auto Flags = AR->getNoWrapFlags();
9437 Flags = setFlags(Flags, SCEV::FlagNW);
9440 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
9441 }
9442
9443 // For a slt/ult condition with a positive step, can we prove nsw/nuw?
9444 // From no-self-wrap, this follows trivially from the fact that every
9445 // (un)signed-wrapped, but not self-wrapped value must be LT than the
9446 // last value before (un)signed wrap. Since we know that last value
9447 // didn't exit, nor will any smaller one.
9448 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT) {
9449 auto WrapType = Pred == ICmpInst::ICMP_SLT ? SCEV::FlagNSW : SCEV::FlagNUW;
9450 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS);
9451 AR && AR->getLoop() == L && AR->isAffine() &&
9452 !AR->getNoWrapFlags(WrapType) && AR->hasNoSelfWrap() &&
9453 isKnownPositive(AR->getStepRecurrence(*this))) {
9454 auto Flags = AR->getNoWrapFlags();
9455 Flags = setFlags(Flags, WrapType);
9458 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
9459 }
9460 }
9461 }
9462
9463 switch (Pred) {
9464 case ICmpInst::ICMP_NE: { // while (X != Y)
9465 // Convert to: while (X-Y != 0)
9466 if (LHS->getType()->isPointerTy()) {
9469 return LHS;
9470 }
9471 if (RHS->getType()->isPointerTy()) {
9474 return RHS;
9475 }
9476 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit,
9477 AllowPredicates);
9478 if (EL.hasAnyInfo())
9479 return EL;
9480 break;
9481 }
9482 case ICmpInst::ICMP_EQ: { // while (X == Y)
9483 // Convert to: while (X-Y == 0)
9484 if (LHS->getType()->isPointerTy()) {
9487 return LHS;
9488 }
9489 if (RHS->getType()->isPointerTy()) {
9492 return RHS;
9493 }
9494 ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L);
9495 if (EL.hasAnyInfo()) return EL;
9496 break;
9497 }
9498 case ICmpInst::ICMP_SLE:
9499 case ICmpInst::ICMP_ULE:
9500 // Since the loop is finite, an invariant RHS cannot include the boundary
9501 // value, otherwise it would loop forever.
9502 if (!EnableFiniteLoopControl || !ControllingFiniteLoop ||
9503 !isLoopInvariant(RHS, L)) {
9504 // Otherwise, perform the addition in a wider type, to avoid overflow.
9505 // If the LHS is an addrec with the appropriate nowrap flag, the
9506 // extension will be sunk into it and the exit count can be analyzed.
9507 auto *OldType = dyn_cast<IntegerType>(LHS->getType());
9508 if (!OldType)
9509 break;
9510 // Prefer doubling the bitwidth over adding a single bit to make it more
9511 // likely that we use a legal type.
9512 auto *NewType =
9513 Type::getIntNTy(OldType->getContext(), OldType->getBitWidth() * 2);
9514 if (ICmpInst::isSigned(Pred)) {
9515 LHS = getSignExtendExpr(LHS, NewType);
9516 RHS = getSignExtendExpr(RHS, NewType);
9517 } else {
9518 LHS = getZeroExtendExpr(LHS, NewType);
9519 RHS = getZeroExtendExpr(RHS, NewType);
9520 }
9521 }
9523 [[fallthrough]];
9524 case ICmpInst::ICMP_SLT:
9525 case ICmpInst::ICMP_ULT: { // while (X < Y)
9526 bool IsSigned = ICmpInst::isSigned(Pred);
9527 ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsOnlyExit,
9528 AllowPredicates);
9529 if (EL.hasAnyInfo())
9530 return EL;
9531 break;
9532 }
9533 case ICmpInst::ICMP_SGE:
9534 case ICmpInst::ICMP_UGE:
9535 // Since the loop is finite, an invariant RHS cannot include the boundary
9536 // value, otherwise it would loop forever.
9537 if (!EnableFiniteLoopControl || !ControllingFiniteLoop ||
9538 !isLoopInvariant(RHS, L))
9539 break;
9541 [[fallthrough]];
9542 case ICmpInst::ICMP_SGT:
9543 case ICmpInst::ICMP_UGT: { // while (X > Y)
9544 bool IsSigned = ICmpInst::isSigned(Pred);
9545 ExitLimit EL = howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsOnlyExit,
9546 AllowPredicates);
9547 if (EL.hasAnyInfo())
9548 return EL;
9549 break;
9550 }
9551 default:
9552 break;
9553 }
9554
9555 return getCouldNotCompute();
9556}
9557
9558ScalarEvolution::ExitLimit
9559ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L,
9560 SwitchInst *Switch,
9561 BasicBlock *ExitingBlock,
9562 bool ControlsOnlyExit) {
9563 assert(!L->contains(ExitingBlock) && "Not an exiting block!");
9564
9565 // Give up if the exit is the default dest of a switch.
9566 if (Switch->getDefaultDest() == ExitingBlock)
9567 return getCouldNotCompute();
9568
9569 assert(L->contains(Switch->getDefaultDest()) &&
9570 "Default case must not exit the loop!");
9571 const SCEV *LHS = getSCEVAtScope(Switch->getCondition(), L);
9572 const SCEV *RHS = getConstant(Switch->findCaseDest(ExitingBlock));
9573
9574 // while (X != Y) --> while (X-Y != 0)
9575 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit);
9576 if (EL.hasAnyInfo())
9577 return EL;
9578
9579 return getCouldNotCompute();
9580}
9581
9582static ConstantInt *
9584 ScalarEvolution &SE) {
9585 const SCEV *InVal = SE.getConstant(C);
9586 const SCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
9588 "Evaluation of SCEV at constant didn't fold correctly?");
9589 return cast<SCEVConstant>(Val)->getValue();
9590}
9591
9592ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit(
9593 Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) {
9594 ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV);
9595 if (!RHS)
9596 return getCouldNotCompute();
9597
9598 const BasicBlock *Latch = L->getLoopLatch();
9599 if (!Latch)
9600 return getCouldNotCompute();
9601
9602 const BasicBlock *Predecessor = L->getLoopPredecessor();
9603 if (!Predecessor)
9604 return getCouldNotCompute();
9605
9606 // Return true if V is of the form "LHS `shift_op` <positive constant>".
9607 // Return LHS in OutLHS, shift_op in OutOpCode, and the shift amount in
9608 // OutShiftAmt.
9609 auto MatchPositiveShift = [](Value *V, Value *&OutLHS,
9610 Instruction::BinaryOps &OutOpCode,
9611 unsigned &OutShiftAmt) {
9612 using namespace PatternMatch;
9613
9614 ConstantInt *ShiftAmt;
9615 if (match(V, m_LShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
9616 OutOpCode = Instruction::LShr;
9617 else if (match(V, m_AShr(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
9618 OutOpCode = Instruction::AShr;
9619 else if (match(V, m_Shl(m_Value(OutLHS), m_ConstantInt(ShiftAmt))))
9620 OutOpCode = Instruction::Shl;
9621 else
9622 return false;
9623
9624 uint64_t Amt = ShiftAmt->getValue().getLimitedValue();
9625 if (Amt == 0 || Amt >= OutLHS->getType()->getScalarSizeInBits())
9626 return false;
9627 OutShiftAmt = Amt;
9628 return true;
9629 };
9630
9631 // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in
9632 //
9633 // loop:
9634 // %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ]
9635 // %iv.shifted = lshr i32 %iv, <positive constant>
9636 //
9637 // Return true on a successful match. Return the corresponding PHI node (%iv
9638 // above) in PNOut, the opcode of the shift operation in OpCodeOut, and the
9639 // shift amount in ShiftAmtOut.
9640 auto MatchShiftRecurrence = [&](Value *V, PHINode *&PNOut,
9641 Instruction::BinaryOps &OpCodeOut,
9642 unsigned &ShiftAmtOut) {
9643 std::optional<Instruction::BinaryOps> PostShiftOpCode;
9644
9645 {
9647 Value *V;
9648 unsigned Amt;
9649
9650 // If we encounter a shift instruction, "peel off" the shift operation,
9651 // and remember that we did so. Later when we inspect %iv's backedge
9652 // value, we will make sure that the backedge value uses the same
9653 // operation.
9654 //
9655 // Note: the peeled shift operation does not have to be the same
9656 // instruction as the one feeding into the PHI's backedge value. We only
9657 // really care about it being the same *kind* of shift instruction --
9658 // that's all that is required for our later inferences to hold.
9659 if (MatchPositiveShift(LHS, V, OpC, Amt)) {
9660 PostShiftOpCode = OpC;
9661 LHS = V;
9662 }
9663 }
9664
9665 PNOut = dyn_cast<PHINode>(LHS);
9666 if (!PNOut || PNOut->getParent() != L->getHeader())
9667 return false;
9668
9669 Value *BEValue = PNOut->getIncomingValueForBlock(Latch);
9670 Value *OpLHS;
9671
9672 return
9673 // The backedge value for the PHI node must be a shift by a positive
9674 // amount
9675 MatchPositiveShift(BEValue, OpLHS, OpCodeOut, ShiftAmtOut) &&
9676
9677 // of the PHI node itself
9678 OpLHS == PNOut &&
9679
9680 // and the kind of shift should be match the kind of shift we peeled
9681 // off, if any.
9682 (!PostShiftOpCode || *PostShiftOpCode == OpCodeOut);
9683 };
9684
9685 PHINode *PN;
9687 unsigned ShiftAmt;
9688 if (!MatchShiftRecurrence(LHS, PN, OpCode, ShiftAmt))
9689 return getCouldNotCompute();
9690
9691 const DataLayout &DL = getDataLayout();
9692
9693 // The key rationale for this optimization is that for some kinds of shift
9694 // recurrences, the value of the recurrence "stabilizes" to either 0 or -1
9695 // within a finite number of iterations. If the condition guarding the
9696 // backedge (in the sense that the backedge is taken if the condition is true)
9697 // is false for the value the shift recurrence stabilizes to, then we know
9698 // that the backedge is taken only a finite number of times.
9699
9700 ConstantInt *StableValue = nullptr;
9701 switch (OpCode) {
9702 default:
9703 llvm_unreachable("Impossible case!");
9704
9705 case Instruction::AShr: {
9706 // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most
9707 // bitwidth(K) iterations.
9708 Value *FirstValue = PN->getIncomingValueForBlock(Predecessor);
9709 KnownBits Known = computeKnownBits(FirstValue, DL, &AC,
9710 Predecessor->getTerminator(), &DT);
9711 auto *Ty = cast<IntegerType>(RHS->getType());
9712 if (Known.isNonNegative())
9713 StableValue = ConstantInt::get(Ty, 0);
9714 else if (Known.isNegative())
9715 StableValue = ConstantInt::get(Ty, -1, true);
9716 else
9717 return getCouldNotCompute();
9718
9719 break;
9720 }
9721 case Instruction::LShr:
9722 case Instruction::Shl:
9723 // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>}
9724 // stabilize to 0 in at most bitwidth(K) iterations.
9725 StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0);
9726 break;
9727 }
9728
9729 auto *Result =
9730 ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI);
9731 assert(Result->getType()->isIntegerTy(1) &&
9732 "Otherwise cannot be an operand to a branch instruction");
9733
9734 if (Result->isNullValue()) {
9735 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
9736 unsigned MaxBTC = BitWidth;
9737
9738 // For right-shift recurrences (lshr/ashr with non-negative start), we can
9739 // compute a tighter max backedge-taken count from the range of the start
9740 // value. After k shifts of ShiftAmt, value = start >> (k * ShiftAmt).
9741 // The value reaches 0 (the stable value) when k * ShiftAmt >=
9742 // activeBits(start), so max BTC = ceil(activeBits(maxStart) / ShiftAmt).
9743 if (OpCode == Instruction::LShr || OpCode == Instruction::AShr) {
9744 Value *StartValue = PN->getIncomingValueForBlock(Predecessor);
9745 const SCEV *StartSCEV = getSCEV(StartValue);
9746 APInt MaxStart = getUnsignedRangeMax(StartSCEV);
9747 if (MaxStart.isStrictlyPositive()) {
9748 unsigned ActiveBits = MaxStart.getActiveBits();
9749 unsigned RangeBTC = divideCeil(ActiveBits, ShiftAmt);
9750 MaxBTC = std::min(MaxBTC, RangeBTC);
9751 }
9752 }
9753
9754 const SCEV *UpperBound =
9756 return ExitLimit(getCouldNotCompute(), UpperBound, UpperBound, false);
9757 }
9758
9759 return getCouldNotCompute();
9760}
9761
9762/// Return true if we can constant fold an instruction of the specified type,
9763/// assuming that all operands were constants.
9764static bool canConstantFold(const Instruction *I,
9765 const TargetLibraryInfo *TLI) {
9769 return true;
9770
9771 if (const CallInst *CI = dyn_cast<CallInst>(I))
9772 if (const Function *F = CI->getCalledFunction())
9773 return canConstantFoldCallTo(CI, F, TLI);
9774 return false;
9775}
9776
9777/// Determine whether this instruction can constant evolve within this loop
9778/// assuming its operands can all constant evolve.
9779static bool canConstantEvolve(Instruction *I, const Loop *L,
9780 const TargetLibraryInfo *TLI) {
9781 // An instruction outside of the loop can't be derived from a loop PHI.
9782 if (!L->contains(I)) return false;
9783
9784 if (isa<PHINode>(I)) {
9785 // We don't currently keep track of the control flow needed to evaluate
9786 // PHIs, so we cannot handle PHIs inside of loops.
9787 return L->getHeader() == I->getParent();
9788 }
9789
9790 // If we won't be able to constant fold this expression even if the operands
9791 // are constants, bail early.
9792 return canConstantFold(I, TLI);
9793}
9794
9795/// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
9796/// recursing through each instruction operand until reaching a loop header phi.
9797static PHINode *
9800 const TargetLibraryInfo *TLI, unsigned Depth) {
9802 return nullptr;
9803
9804 // Otherwise, we can evaluate this instruction if all of its operands are
9805 // constant or derived from a PHI node themselves.
9806 PHINode *PHI = nullptr;
9807 for (Value *Op : UseInst->operands()) {
9808 if (isa<Constant>(Op)) continue;
9809
9811 if (!OpInst || !canConstantEvolve(OpInst, L, TLI))
9812 return nullptr;
9813
9814 PHINode *P = dyn_cast<PHINode>(OpInst);
9815 if (!P)
9816 // If this operand is already visited, reuse the prior result.
9817 // We may have P != PHI if this is the deepest point at which the
9818 // inconsistent paths meet.
9819 P = PHIMap.lookup(OpInst);
9820 if (!P) {
9821 // Recurse and memoize the results, whether a phi is found or not.
9822 // This recursive call invalidates pointers into PHIMap.
9823 P = getConstantEvolvingPHIOperands(OpInst, L, PHIMap, TLI, Depth + 1);
9824 PHIMap[OpInst] = P;
9825 }
9826 if (!P)
9827 return nullptr; // Not evolving from PHI
9828 if (PHI && PHI != P)
9829 return nullptr; // Evolving from multiple different PHIs.
9830 PHI = P;
9831 }
9832 // This is a expression evolving from a constant PHI!
9833 return PHI;
9834}
9835
9836/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
9837/// in the loop that V is derived from. We allow arbitrary operations along the
9838/// way, but the operands of an operation must either be constants or a value
9839/// derived from a constant PHI. If this expression does not fit with these
9840/// constraints, return null.
9842 const TargetLibraryInfo *TLI) {
9844 if (!I || !canConstantEvolve(I, L, TLI))
9845 return nullptr;
9846
9847 if (PHINode *PN = dyn_cast<PHINode>(I))
9848 return PN;
9849
9850 // Record non-constant instructions contained by the loop.
9852 return getConstantEvolvingPHIOperands(I, L, PHIMap, TLI, 0);
9853}
9854
9855/// EvaluateExpression - Given an expression that passes the
9856/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
9857/// in the loop has the value PHIVal. If we can't fold this expression for some
9858/// reason, return null.
9861 const DataLayout &DL,
9862 const TargetLibraryInfo *TLI) {
9863 // Convenient constant check, but redundant for recursive calls.
9864 if (Constant *C = dyn_cast<Constant>(V)) return C;
9866 if (!I) return nullptr;
9867
9868 if (Constant *C = Vals.lookup(I)) return C;
9869
9870 // An instruction inside the loop depends on a value outside the loop that we
9871 // weren't given a mapping for, or a value such as a call inside the loop.
9872 if (!canConstantEvolve(I, L, TLI))
9873 return nullptr;
9874
9875 // An unmapped PHI can be due to a branch or another loop inside this loop,
9876 // or due to this not being the initial iteration through a loop where we
9877 // couldn't compute the evolution of this particular PHI last time.
9878 if (isa<PHINode>(I)) return nullptr;
9879
9880 std::vector<Constant*> Operands(I->getNumOperands());
9881
9882 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
9883 Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i));
9884 if (!Operand) {
9885 Operands[i] = dyn_cast<Constant>(I->getOperand(i));
9886 if (!Operands[i]) return nullptr;
9887 continue;
9888 }
9889 Constant *C = EvaluateExpression(Operand, L, Vals, DL, TLI);
9890 Vals[Operand] = C;
9891 if (!C) return nullptr;
9892 Operands[i] = C;
9893 }
9894
9895 return ConstantFoldInstOperands(I, Operands, DL, TLI,
9896 /*AllowNonDeterministic=*/false);
9897}
9898
9899
9900// If every incoming value to PN except the one for BB is a specific Constant,
9901// return that, else return nullptr.
9903 Constant *IncomingVal = nullptr;
9904
9905 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
9906 if (PN->getIncomingBlock(i) == BB)
9907 continue;
9908
9909 auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i));
9910 if (!CurrentVal)
9911 return nullptr;
9912
9913 if (IncomingVal != CurrentVal) {
9914 if (IncomingVal)
9915 return nullptr;
9916 IncomingVal = CurrentVal;
9917 }
9918 }
9919
9920 return IncomingVal;
9921}
9922
9923/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
9924/// in the header of its containing loop, we know the loop executes a
9925/// constant number of times, and the PHI node is just a recurrence
9926/// involving constants, fold it.
9927Constant *
9928ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
9929 const APInt &BEs,
9930 const Loop *L) {
9931 auto [I, Inserted] = ConstantEvolutionLoopExitValue.try_emplace(PN);
9932 if (!Inserted)
9933 return I->second;
9934
9936 return nullptr; // Not going to evaluate it.
9937
9938 Constant *&RetVal = I->second;
9939
9940 DenseMap<Instruction *, Constant *> CurrentIterVals;
9941 BasicBlock *Header = L->getHeader();
9942 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
9943
9944 BasicBlock *Latch = L->getLoopLatch();
9945 if (!Latch)
9946 return nullptr;
9947
9948 for (PHINode &PHI : Header->phis()) {
9949 if (auto *StartCST = getOtherIncomingValue(&PHI, Latch))
9950 CurrentIterVals[&PHI] = StartCST;
9951 }
9952 if (!CurrentIterVals.count(PN))
9953 return RetVal = nullptr;
9954
9955 Value *BEValue = PN->getIncomingValueForBlock(Latch);
9956
9957 // Execute the loop symbolically to determine the exit value.
9958 assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) &&
9959 "BEs is <= MaxBruteForceIterations which is an 'unsigned'!");
9960
9961 unsigned NumIterations = BEs.getZExtValue(); // must be in range
9962 unsigned IterationNum = 0;
9963 const DataLayout &DL = getDataLayout();
9964 for (; ; ++IterationNum) {
9965 if (IterationNum == NumIterations)
9966 return RetVal = CurrentIterVals[PN]; // Got exit value!
9967
9968 // Compute the value of the PHIs for the next iteration.
9969 // EvaluateExpression adds non-phi values to the CurrentIterVals map.
9970 DenseMap<Instruction *, Constant *> NextIterVals;
9971 Constant *NextPHI =
9972 EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
9973 if (!NextPHI)
9974 return nullptr; // Couldn't evaluate!
9975 NextIterVals[PN] = NextPHI;
9976
9977 bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
9978
9979 // Also evaluate the other PHI nodes. However, we don't get to stop if we
9980 // cease to be able to evaluate one of them or if they stop evolving,
9981 // because that doesn't necessarily prevent us from computing PN.
9983 for (const auto &I : CurrentIterVals) {
9984 PHINode *PHI = dyn_cast<PHINode>(I.first);
9985 if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
9986 PHIsToCompute.emplace_back(PHI, I.second);
9987 }
9988 // We use two distinct loops because EvaluateExpression may invalidate any
9989 // iterators into CurrentIterVals.
9990 for (const auto &I : PHIsToCompute) {
9991 PHINode *PHI = I.first;
9992 Constant *&NextPHI = NextIterVals[PHI];
9993 if (!NextPHI) { // Not already computed.
9994 Value *BEValue = PHI->getIncomingValueForBlock(Latch);
9995 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
9996 }
9997 if (NextPHI != I.second)
9998 StoppedEvolving = false;
9999 }
10000
10001 // If all entries in CurrentIterVals == NextIterVals then we can stop
10002 // iterating, the loop can't continue to change.
10003 if (StoppedEvolving)
10004 return RetVal = CurrentIterVals[PN];
10005
10006 CurrentIterVals.swap(NextIterVals);
10007 }
10008}
10009
10010const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L,
10011 Value *Cond,
10012 bool ExitWhen) {
10013 PHINode *PN = getConstantEvolvingPHI(Cond, L, &TLI);
10014 if (!PN) return getCouldNotCompute();
10015
10016 // If the loop is canonicalized, the PHI will have exactly two entries.
10017 // That's the only form we support here.
10018 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
10019
10020 DenseMap<Instruction *, Constant *> CurrentIterVals;
10021 BasicBlock *Header = L->getHeader();
10022 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
10023
10024 BasicBlock *Latch = L->getLoopLatch();
10025 assert(Latch && "Should follow from NumIncomingValues == 2!");
10026
10027 for (PHINode &PHI : Header->phis()) {
10028 if (auto *StartCST = getOtherIncomingValue(&PHI, Latch))
10029 CurrentIterVals[&PHI] = StartCST;
10030 }
10031 if (!CurrentIterVals.count(PN))
10032 return getCouldNotCompute();
10033
10034 // Okay, we find a PHI node that defines the trip count of this loop. Execute
10035 // the loop symbolically to determine when the condition gets a value of
10036 // "ExitWhen".
10037 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
10038 const DataLayout &DL = getDataLayout();
10039 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
10040 auto *CondVal = dyn_cast_or_null<ConstantInt>(
10041 EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI));
10042
10043 // Couldn't symbolically evaluate.
10044 if (!CondVal) return getCouldNotCompute();
10045
10046 if (CondVal->getValue() == uint64_t(ExitWhen)) {
10047 ++NumBruteForceTripCountsComputed;
10048 return getConstant(Type::getInt32Ty(getContext()), IterationNum);
10049 }
10050
10051 // Update all the PHI nodes for the next iteration.
10052 DenseMap<Instruction *, Constant *> NextIterVals;
10053
10054 // Create a list of which PHIs we need to compute. We want to do this before
10055 // calling EvaluateExpression on them because that may invalidate iterators
10056 // into CurrentIterVals.
10057 SmallVector<PHINode *, 8> PHIsToCompute;
10058 for (const auto &I : CurrentIterVals) {
10059 PHINode *PHI = dyn_cast<PHINode>(I.first);
10060 if (!PHI || PHI->getParent() != Header) continue;
10061 PHIsToCompute.push_back(PHI);
10062 }
10063 for (PHINode *PHI : PHIsToCompute) {
10064 Constant *&NextPHI = NextIterVals[PHI];
10065 if (NextPHI) continue; // Already computed!
10066
10067 Value *BEValue = PHI->getIncomingValueForBlock(Latch);
10068 NextPHI = EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
10069 }
10070 CurrentIterVals.swap(NextIterVals);
10071 }
10072
10073 // Too many iterations were needed to evaluate.
10074 return getCouldNotCompute();
10075}
10076
10078 auto &Values = ValuesAtScopes[V];
10079 // Check to see if we've folded this expression at this loop before.
10080 for (auto &LS : Values)
10081 if (LS.first == L)
10082 return LS.second ? LS.second : SCEVUse(V);
10083
10084 Values.emplace_back(L, nullptr);
10085
10086 // Otherwise compute it.
10087 SCEVUse C = computeSCEVAtScope(V, L);
10088 for (auto &LS : reverse(ValuesAtScopes[V]))
10089 if (LS.first == L) {
10090 LS.second = C;
10091 // Record the dependency under the bare expression: invalidation walks
10092 // expressions, and any use flags on C do not change which expression
10093 // this is the value at scope of.
10094 if (!isa<SCEVConstant>(C))
10095 ValuesAtScopesUsers[C.getPointer()].push_back({L, V});
10096 break;
10097 }
10098 return C;
10099}
10100
10101/// This builds up a Constant using the ConstantExpr interface. That way, we
10102/// will return Constants for objects which aren't represented by a
10103/// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
10104/// Returns NULL if the SCEV isn't representable as a Constant.
10106 switch (V->getSCEVType()) {
10107 case scCouldNotCompute:
10108 case scAddRecExpr:
10109 case scVScale:
10110 return nullptr;
10111 case scConstant:
10112 return cast<SCEVConstant>(V)->getValue();
10113 case scUnknown:
10115 case scPtrToAddr: {
10117 if (Constant *CastOp = BuildConstantFromSCEV(P2I->getOperand()))
10118 return ConstantExpr::getPtrToAddr(CastOp, P2I->getType());
10119
10120 return nullptr;
10121 }
10122 case scTruncate: {
10124 if (Constant *CastOp = BuildConstantFromSCEV(ST->getOperand()))
10125 return ConstantExpr::getTrunc(CastOp, ST->getType());
10126 return nullptr;
10127 }
10128 case scAddExpr: {
10129 const SCEVAddExpr *SA = cast<SCEVAddExpr>(V);
10130 Constant *C = nullptr;
10131 for (const SCEV *Op : SA->operands()) {
10133 if (!OpC)
10134 return nullptr;
10135 if (!C) {
10136 C = OpC;
10137 continue;
10138 }
10139 assert(!C->getType()->isPointerTy() &&
10140 "Can only have one pointer, and it must be last");
10141 if (OpC->getType()->isPointerTy()) {
10142 // The offsets have been converted to bytes. We can add bytes using
10143 // an i8 GEP.
10144 C = ConstantExpr::getPtrAdd(OpC, C);
10145 } else {
10146 C = ConstantExpr::getAdd(C, OpC);
10147 }
10148 }
10149 return C;
10150 }
10151 case scMulExpr:
10152 case scSignExtend:
10153 case scZeroExtend:
10154 case scUDivExpr:
10155 case scSMaxExpr:
10156 case scUMaxExpr:
10157 case scSMinExpr:
10158 case scUMinExpr:
10160 return nullptr;
10161 }
10162 llvm_unreachable("Unknown SCEV kind!");
10163}
10164
10165const SCEV *ScalarEvolution::getWithOperands(const SCEV *S,
10166 SmallVectorImpl<SCEVUse> &NewOps) {
10167 switch (S->getSCEVType()) {
10168 case scTruncate:
10169 case scZeroExtend:
10170 case scSignExtend:
10171 case scPtrToAddr:
10172 return getCastExpr(S->getSCEVType(), NewOps[0], S->getType());
10173 case scAddRecExpr: {
10174 auto *AddRec = cast<SCEVAddRecExpr>(S);
10175 return getAddRecExpr(NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags());
10176 }
10177 case scAddExpr:
10178 return getAddExpr(NewOps, cast<SCEVAddExpr>(S)->getNoWrapFlags());
10179 case scMulExpr:
10180 return getMulExpr(NewOps, cast<SCEVMulExpr>(S)->getNoWrapFlags());
10181 case scUDivExpr:
10182 return getUDivExpr(NewOps[0], NewOps[1]);
10183 case scUMaxExpr:
10184 case scSMaxExpr:
10185 case scUMinExpr:
10186 case scSMinExpr:
10187 return getMinMaxExpr(S->getSCEVType(), NewOps);
10189 return getSequentialMinMaxExpr(S->getSCEVType(), NewOps);
10190 case scConstant:
10191 case scVScale:
10192 case scUnknown:
10193 return S;
10194 case scCouldNotCompute:
10195 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
10196 }
10197 llvm_unreachable("Unknown SCEV kind!");
10198}
10199
10200SCEVUse ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
10201 switch (V->getSCEVType()) {
10202 case scConstant:
10203 case scVScale:
10204 return V;
10205 case scAddRecExpr: {
10206 // If this is a loop recurrence for a loop that does not contain L, then we
10207 // are dealing with the final value computed by the loop.
10208 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(V);
10209 // First, attempt to evaluate each operand.
10210 // Avoid performing the look-up in the common case where the specified
10211 // expression has no loop-variant portions.
10212 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
10213 SCEVUse OpAtScope = getSCEVAtScope(AddRec->getOperand(i), L);
10214 if (OpAtScope == AddRec->getOperand(i))
10215 continue;
10216
10217 // Okay, at least one of these operands is loop variant but might be
10218 // foldable. Build a new instance of the folded commutative expression.
10220 NewOps.reserve(AddRec->getNumOperands());
10221 append_range(NewOps, AddRec->operands().take_front(i));
10222 NewOps.push_back(OpAtScope);
10223 for (++i; i != e; ++i)
10224 NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L));
10225
10226 const SCEV *FoldedRec = getAddRecExpr(
10227 NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags(SCEV::FlagNW));
10228 AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec);
10229 // The addrec may be folded to a nonrecurrence, for example, if the
10230 // induction variable is multiplied by zero after constant folding. Go
10231 // ahead and return the folded value.
10232 if (!AddRec)
10233 return FoldedRec;
10234 break;
10235 }
10236
10237 // If the scope is outside the addrec's loop, evaluate it by using the
10238 // loop exit value of the addrec.
10239 if (!AddRec->getLoop()->contains(L)) {
10240 SCEVUse ExitValue = AddRec->getExitValue(*this);
10241 if (isa<SCEVCouldNotCompute>(ExitValue))
10242 return AddRec;
10243 return ExitValue;
10244 }
10245
10246 return AddRec;
10247 }
10248 case scTruncate:
10249 case scZeroExtend:
10250 case scSignExtend:
10251 case scPtrToAddr:
10252 case scAddExpr:
10253 case scMulExpr:
10254 case scUDivExpr:
10255 case scUMaxExpr:
10256 case scSMaxExpr:
10257 case scUMinExpr:
10258 case scSMinExpr:
10259 case scSequentialUMinExpr: {
10260 ArrayRef<SCEVUse> Ops = V->operands();
10261 // Avoid performing the look-up in the common case where the specified
10262 // expression has no loop-variant portions.
10263 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
10264 SCEVUse OpAtScope = getSCEVAtScope(Ops[i].getPointer(), L);
10265 if (OpAtScope != Ops[i].getPointer()) {
10266 // Okay, at least one of these operands is loop variant but might be
10267 // foldable. Build a new instance of the folded commutative expression.
10269 NewOps.reserve(Ops.size());
10270 append_range(NewOps, Ops.take_front(i));
10271 NewOps.push_back(OpAtScope);
10272
10273 for (++i; i != e; ++i) {
10274 OpAtScope = getSCEVAtScope(Ops[i].getPointer(), L);
10275 NewOps.push_back(OpAtScope);
10276 }
10277
10278 return getWithOperands(V, NewOps);
10279 }
10280 }
10281 // If we got here, all operands are loop invariant.
10282 return V;
10283 }
10284 case scUnknown: {
10285 // If this instruction is evolved from a constant-evolving PHI, compute the
10286 // exit value from the loop without using SCEVs.
10287 const SCEVUnknown *SU = cast<SCEVUnknown>(V);
10289 if (!I)
10290 return V; // This is some other type of SCEVUnknown, just return it.
10291
10292 if (PHINode *PN = dyn_cast<PHINode>(I)) {
10293 const Loop *CurrLoop = this->LI[I->getParent()];
10294 // Looking for loop exit value.
10295 if (CurrLoop && CurrLoop->getParentLoop() == L &&
10296 PN->getParent() == CurrLoop->getHeader()) {
10297 // Okay, there is no closed form solution for the PHI node. Check
10298 // to see if the loop that contains it has a known backedge-taken
10299 // count. If so, we may be able to force computation of the exit
10300 // value.
10301 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(CurrLoop);
10302 // This trivial case can show up in some degenerate cases where
10303 // the incoming IR has not yet been fully simplified.
10304 if (BackedgeTakenCount->isZero()) {
10305 Value *InitValue = nullptr;
10306 bool MultipleInitValues = false;
10307 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
10308 if (!CurrLoop->contains(PN->getIncomingBlock(i))) {
10309 if (!InitValue)
10310 InitValue = PN->getIncomingValue(i);
10311 else if (InitValue != PN->getIncomingValue(i)) {
10312 MultipleInitValues = true;
10313 break;
10314 }
10315 }
10316 }
10317 if (!MultipleInitValues && InitValue)
10318 return getSCEV(InitValue);
10319 }
10320 // Do we have a loop invariant value flowing around the backedge
10321 // for a loop which must execute the backedge?
10322 if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) &&
10323 isKnownNonZero(BackedgeTakenCount) &&
10324 PN->getNumIncomingValues() == 2) {
10325
10326 unsigned InLoopPred =
10327 CurrLoop->contains(PN->getIncomingBlock(0)) ? 0 : 1;
10328 Value *BackedgeVal = PN->getIncomingValue(InLoopPred);
10329 if (CurrLoop->isLoopInvariant(BackedgeVal))
10330 return getSCEV(BackedgeVal);
10331 }
10332 if (auto *BTCC = dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
10333 // Okay, we know how many times the containing loop executes. If
10334 // this is a constant evolving PHI node, get the final value at
10335 // the specified iteration number.
10336 Constant *RV =
10337 getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), CurrLoop);
10338 if (RV)
10339 return getSCEV(RV);
10340 }
10341 }
10342 }
10343
10344 // Okay, this is an expression that we cannot symbolically evaluate
10345 // into a SCEV. Check to see if it's possible to symbolically evaluate
10346 // the arguments into constants, and if so, try to constant propagate the
10347 // result. This is particularly useful for computing loop exit values.
10348 if (!canConstantFold(I, &TLI))
10349 return V; // This is some other type of SCEVUnknown, just return it.
10350
10351 SmallVector<Constant *, 4> Operands;
10352 Operands.reserve(I->getNumOperands());
10353 bool MadeImprovement = false;
10354 for (Value *Op : I->operands()) {
10355 if (Constant *C = dyn_cast<Constant>(Op)) {
10356 Operands.push_back(C);
10357 continue;
10358 }
10359
10360 // If any of the operands is non-constant and if they are
10361 // non-integer and non-pointer, don't even try to analyze them
10362 // with scev techniques.
10363 if (!isSCEVable(Op->getType()))
10364 return V;
10365
10366 const SCEV *OrigV = getSCEV(Op);
10367 const SCEV *OpV = getSCEVAtScope(OrigV, L);
10368 MadeImprovement |= OrigV != OpV;
10369
10371 if (!C)
10372 return V;
10373 assert(C->getType() == Op->getType() && "Type mismatch");
10374 Operands.push_back(C);
10375 }
10376
10377 // Check to see if getSCEVAtScope actually made an improvement.
10378 if (!MadeImprovement)
10379 return V; // This is some other type of SCEVUnknown, just return it.
10380
10381 Constant *C = nullptr;
10382 const DataLayout &DL = getDataLayout();
10383 C = ConstantFoldInstOperands(I, Operands, DL, &TLI,
10384 /*AllowNonDeterministic=*/false);
10385 if (!C)
10386 return V;
10387 return getSCEV(C);
10388 }
10389 case scCouldNotCompute:
10390 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
10391 }
10392 llvm_unreachable("Unknown SCEV type!");
10393}
10394
10396 return getSCEVAtScope(getSCEV(V), L);
10397}
10398
10399const SCEV *ScalarEvolution::stripInjectiveFunctions(const SCEV *S) const {
10401 return stripInjectiveFunctions(ZExt->getOperand());
10403 return stripInjectiveFunctions(SExt->getOperand());
10404 return S;
10405}
10406
10407/// Finds the minimum unsigned root of the following equation:
10408///
10409/// A * X = B (mod N)
10410///
10411/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
10412/// A and B isn't important.
10413///
10414/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
10415static const SCEV *
10418 ScalarEvolution &SE, const Loop *L) {
10419 uint32_t BW = A.getBitWidth();
10420 assert(BW == SE.getTypeSizeInBits(B->getType()));
10421 assert(A != 0 && "A must be non-zero.");
10422
10423 // 1. D = gcd(A, N)
10424 //
10425 // The gcd of A and N may have only one prime factor: 2. The number of
10426 // trailing zeros in A is its multiplicity
10427 uint32_t Mult2 = A.countr_zero();
10428 // D = 2^Mult2
10429
10430 // 2. Check if B is divisible by D.
10431 //
10432 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
10433 // is not less than multiplicity of this prime factor for D.
10434 unsigned MinTZ = SE.getMinTrailingZeros(B);
10435 // Try again with the terminator of the loop predecessor for context-specific
10436 // result, if MinTZ s too small.
10437 if (MinTZ < Mult2 && L->getLoopPredecessor())
10438 MinTZ = SE.getMinTrailingZeros(B, L->getLoopPredecessor()->getTerminator());
10439 if (MinTZ < Mult2) {
10440 // Check if we can prove there's no remainder using URem.
10441 const SCEV *URem =
10442 SE.getURemExpr(B, SE.getConstant(APInt::getOneBitSet(BW, Mult2)));
10443 const SCEV *Zero = SE.getZero(B->getType());
10444 if (!SE.isKnownPredicate(CmpInst::ICMP_EQ, URem, Zero)) {
10445 // Try to add a predicate ensuring B is a multiple of 1 << Mult2.
10446 if (!Predicates)
10447 return SE.getCouldNotCompute();
10448
10449 // Avoid adding a predicate that is known to be false.
10450 if (SE.isKnownPredicate(CmpInst::ICMP_NE, URem, Zero))
10451 return SE.getCouldNotCompute();
10452 Predicates->push_back(SE.getEqualPredicate(URem, Zero));
10453 }
10454 }
10455
10456 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
10457 // modulo (N / D).
10458 //
10459 // If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent
10460 // (N / D) in general. The inverse itself always fits into BW bits, though,
10461 // so we immediately truncate it.
10462 APInt AD = A.lshr(Mult2).trunc(BW - Mult2); // AD = A / D
10463 APInt I = AD.multiplicativeInverse().zext(BW);
10464
10465 // 4. Compute the minimum unsigned root of the equation:
10466 // I * (B / D) mod (N / D)
10467 // To simplify the computation, we factor out the divide by D:
10468 // (I * B mod N) / D
10469 const SCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2));
10470 return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)), D);
10471}
10472
10473/// For a given quadratic addrec, generate coefficients of the corresponding
10474/// quadratic equation, multiplied by a common value to ensure that they are
10475/// integers.
10476/// The returned value is a tuple { A, B, C, M, BitWidth }, where
10477/// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C
10478/// were multiplied by, and BitWidth is the bit width of the original addrec
10479/// coefficients.
10480/// This function returns std::nullopt if the addrec coefficients are not
10481/// compile- time constants.
10482static std::optional<std::tuple<APInt, APInt, APInt, APInt, unsigned>>
10484 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
10485 const SCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
10486 const SCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
10487 const SCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
10488 LLVM_DEBUG(dbgs() << __func__ << ": analyzing quadratic addrec: "
10489 << *AddRec << '\n');
10490
10491 // We currently can only solve this if the coefficients are constants.
10492 if (!LC || !MC || !NC) {
10493 LLVM_DEBUG(dbgs() << __func__ << ": coefficients are not constant\n");
10494 return std::nullopt;
10495 }
10496
10497 APInt L = LC->getAPInt();
10498 APInt M = MC->getAPInt();
10499 APInt N = NC->getAPInt();
10500 assert(!N.isZero() && "This is not a quadratic addrec");
10501
10502 unsigned BitWidth = LC->getAPInt().getBitWidth();
10503 unsigned NewWidth = BitWidth + 1;
10504 LLVM_DEBUG(dbgs() << __func__ << ": addrec coeff bw: "
10505 << BitWidth << '\n');
10506 // The sign-extension (as opposed to a zero-extension) here matches the
10507 // extension used in SolveQuadraticEquationWrap (with the same motivation).
10508 N = N.sext(NewWidth);
10509 M = M.sext(NewWidth);
10510 L = L.sext(NewWidth);
10511
10512 // The increments are M, M+N, M+2N, ..., so the accumulated values are
10513 // L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is,
10514 // L+M, L+2M+N, L+3M+3N, ...
10515 // After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N.
10516 //
10517 // The equation Acc = 0 is then
10518 // L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0.
10519 // In a quadratic form it becomes:
10520 // N n^2 + (2M-N) n + 2L = 0.
10521
10522 APInt A = N;
10523 APInt B = 2 * M - A;
10524 APInt C = 2 * L;
10525 APInt T = APInt(NewWidth, 2);
10526 LLVM_DEBUG(dbgs() << __func__ << ": equation " << A << "x^2 + " << B
10527 << "x + " << C << ", coeff bw: " << NewWidth
10528 << ", multiplied by " << T << '\n');
10529 return std::make_tuple(A, B, C, T, BitWidth);
10530}
10531
10532/// Helper function to compare optional APInts:
10533/// (a) if X and Y both exist, return min(X, Y),
10534/// (b) if neither X nor Y exist, return std::nullopt,
10535/// (c) if exactly one of X and Y exists, return that value.
10536static std::optional<APInt> MinOptional(std::optional<APInt> X,
10537 std::optional<APInt> Y) {
10538 if (X && Y) {
10539 unsigned W = std::max(X->getBitWidth(), Y->getBitWidth());
10540 APInt XW = X->sext(W);
10541 APInt YW = Y->sext(W);
10542 return XW.slt(YW) ? *X : *Y;
10543 }
10544 if (!X && !Y)
10545 return std::nullopt;
10546 return X ? *X : *Y;
10547}
10548
10549/// Helper function to truncate an optional APInt to a given BitWidth.
10550/// When solving addrec-related equations, it is preferable to return a value
10551/// that has the same bit width as the original addrec's coefficients. If the
10552/// solution fits in the original bit width, truncate it (except for i1).
10553/// Returning a value of a different bit width may inhibit some optimizations.
10554///
10555/// In general, a solution to a quadratic equation generated from an addrec
10556/// may require BW+1 bits, where BW is the bit width of the addrec's
10557/// coefficients. The reason is that the coefficients of the quadratic
10558/// equation are BW+1 bits wide (to avoid truncation when converting from
10559/// the addrec to the equation).
10560static std::optional<APInt> TruncIfPossible(std::optional<APInt> X,
10561 unsigned BitWidth) {
10562 if (!X)
10563 return std::nullopt;
10564 unsigned W = X->getBitWidth();
10566 return X->trunc(BitWidth);
10567 return X;
10568}
10569
10570/// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n
10571/// iterations. The values L, M, N are assumed to be signed, and they
10572/// should all have the same bit widths.
10573/// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW,
10574/// where BW is the bit width of the addrec's coefficients.
10575/// If the calculated value is a BW-bit integer (for BW > 1), it will be
10576/// returned as such, otherwise the bit width of the returned value may
10577/// be greater than BW.
10578///
10579/// This function returns std::nullopt if
10580/// (a) the addrec coefficients are not constant, or
10581/// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases
10582/// like x^2 = 5, no integer solutions exist, in other cases an integer
10583/// solution may exist, but SolveQuadraticEquationWrap may fail to find it.
10584static std::optional<APInt>
10586 APInt A, B, C, M;
10587 unsigned BitWidth;
10588 auto T = GetQuadraticEquation(AddRec);
10589 if (!T)
10590 return std::nullopt;
10591
10592 std::tie(A, B, C, M, BitWidth) = *T;
10593 LLVM_DEBUG(dbgs() << __func__ << ": solving for unsigned overflow\n");
10594 std::optional<APInt> X =
10596 if (!X)
10597 return std::nullopt;
10598
10599 ConstantInt *CX = ConstantInt::get(SE.getContext(), *X);
10600 ConstantInt *V = EvaluateConstantChrecAtConstant(AddRec, CX, SE);
10601 if (!V->isZero())
10602 return std::nullopt;
10603
10604 return TruncIfPossible(X, BitWidth);
10605}
10606
10607/// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n
10608/// iterations. The values M, N are assumed to be signed, and they
10609/// should all have the same bit widths.
10610/// Find the least n such that c(n) does not belong to the given range,
10611/// while c(n-1) does.
10612///
10613/// This function returns std::nullopt if
10614/// (a) the addrec coefficients are not constant, or
10615/// (b) SolveQuadraticEquationWrap was unable to find a solution for the
10616/// bounds of the range.
10617static std::optional<APInt>
10619 const ConstantRange &Range, ScalarEvolution &SE) {
10620 assert(AddRec->getOperand(0)->isZero() &&
10621 "Starting value of addrec should be 0");
10622 LLVM_DEBUG(dbgs() << __func__ << ": solving boundary crossing for range "
10623 << Range << ", addrec " << *AddRec << '\n');
10624 // This case is handled in getNumIterationsInRange. Here we can assume that
10625 // we start in the range.
10626 assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) &&
10627 "Addrec's initial value should be in range");
10628
10629 APInt A, B, C, M;
10630 unsigned BitWidth;
10631 auto T = GetQuadraticEquation(AddRec);
10632 if (!T)
10633 return std::nullopt;
10634
10635 // Be careful about the return value: there can be two reasons for not
10636 // returning an actual number. First, if no solutions to the equations
10637 // were found, and second, if the solutions don't leave the given range.
10638 // The first case means that the actual solution is "unknown", the second
10639 // means that it's known, but not valid. If the solution is unknown, we
10640 // cannot make any conclusions.
10641 // Return a pair: the optional solution and a flag indicating if the
10642 // solution was found.
10643 auto SolveForBoundary =
10644 [&](APInt Bound) -> std::pair<std::optional<APInt>, bool> {
10645 // Solve for signed overflow and unsigned overflow, pick the lower
10646 // solution.
10647 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: checking boundary "
10648 << Bound << " (before multiplying by " << M << ")\n");
10649 Bound *= M; // The quadratic equation multiplier.
10650
10651 std::optional<APInt> SO;
10652 if (BitWidth > 1) {
10653 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
10654 "signed overflow\n");
10656 }
10657 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
10658 "unsigned overflow\n");
10659 std::optional<APInt> UO =
10661
10662 auto LeavesRange = [&] (const APInt &X) {
10663 ConstantInt *C0 = ConstantInt::get(SE.getContext(), X);
10664 ConstantInt *V0 = EvaluateConstantChrecAtConstant(AddRec, C0, SE);
10665 if (Range.contains(V0->getValue()))
10666 return false;
10667 // X should be at least 1, so X-1 is non-negative.
10668 ConstantInt *C1 = ConstantInt::get(SE.getContext(), X-1);
10670 if (Range.contains(V1->getValue()))
10671 return true;
10672 return false;
10673 };
10674
10675 // If SolveQuadraticEquationWrap returns std::nullopt, it means that there
10676 // can be a solution, but the function failed to find it. We cannot treat it
10677 // as "no solution".
10678 if (!SO || !UO)
10679 return {std::nullopt, false};
10680
10681 // Check the smaller value first to see if it leaves the range.
10682 // At this point, both SO and UO must have values.
10683 std::optional<APInt> Min = MinOptional(SO, UO);
10684 if (LeavesRange(*Min))
10685 return { Min, true };
10686 std::optional<APInt> Max = Min == SO ? UO : SO;
10687 if (LeavesRange(*Max))
10688 return { Max, true };
10689
10690 // Solutions were found, but were eliminated, hence the "true".
10691 return {std::nullopt, true};
10692 };
10693
10694 std::tie(A, B, C, M, BitWidth) = *T;
10695 // Lower bound is inclusive, subtract 1 to represent the exiting value.
10696 APInt Lower = Range.getLower().sext(A.getBitWidth()) - 1;
10697 APInt Upper = Range.getUpper().sext(A.getBitWidth());
10698 auto SL = SolveForBoundary(Lower);
10699 auto SU = SolveForBoundary(Upper);
10700 // If any of the solutions was unknown, no meaninigful conclusions can
10701 // be made.
10702 if (!SL.second || !SU.second)
10703 return std::nullopt;
10704
10705 // Claim: The correct solution is not some value between Min and Max.
10706 //
10707 // Justification: Assuming that Min and Max are different values, one of
10708 // them is when the first signed overflow happens, the other is when the
10709 // first unsigned overflow happens. Crossing the range boundary is only
10710 // possible via an overflow (treating 0 as a special case of it, modeling
10711 // an overflow as crossing k*2^W for some k).
10712 //
10713 // The interesting case here is when Min was eliminated as an invalid
10714 // solution, but Max was not. The argument is that if there was another
10715 // overflow between Min and Max, it would also have been eliminated if
10716 // it was considered.
10717 //
10718 // For a given boundary, it is possible to have two overflows of the same
10719 // type (signed/unsigned) without having the other type in between: this
10720 // can happen when the vertex of the parabola is between the iterations
10721 // corresponding to the overflows. This is only possible when the two
10722 // overflows cross k*2^W for the same k. In such case, if the second one
10723 // left the range (and was the first one to do so), the first overflow
10724 // would have to enter the range, which would mean that either we had left
10725 // the range before or that we started outside of it. Both of these cases
10726 // are contradictions.
10727 //
10728 // Claim: In the case where SolveForBoundary returns std::nullopt, the correct
10729 // solution is not some value between the Max for this boundary and the
10730 // Min of the other boundary.
10731 //
10732 // Justification: Assume that we had such Max_A and Min_B corresponding
10733 // to range boundaries A and B and such that Max_A < Min_B. If there was
10734 // a solution between Max_A and Min_B, it would have to be caused by an
10735 // overflow corresponding to either A or B. It cannot correspond to B,
10736 // since Min_B is the first occurrence of such an overflow. If it
10737 // corresponded to A, it would have to be either a signed or an unsigned
10738 // overflow that is larger than both eliminated overflows for A. But
10739 // between the eliminated overflows and this overflow, the values would
10740 // cover the entire value space, thus crossing the other boundary, which
10741 // is a contradiction.
10742
10743 return TruncIfPossible(MinOptional(SL.first, SU.first), BitWidth);
10744}
10745
10746ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
10747 const Loop *L,
10748 bool ControlsOnlyExit,
10749 bool AllowPredicates) {
10750
10751 // This is only used for loops with a "x != y" exit test. The exit condition
10752 // is now expressed as a single expression, V = x-y. So the exit test is
10753 // effectively V != 0. We know and take advantage of the fact that this
10754 // expression only being used in a comparison by zero context.
10755
10757 // If the value is a constant
10758 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
10759 // If the value is already zero, the branch will execute zero times.
10760 if (C->getValue()->isZero()) return C;
10761 return getCouldNotCompute(); // Otherwise it will loop infinitely.
10762 }
10763
10764 const SCEVAddRecExpr *AddRec =
10765 dyn_cast<SCEVAddRecExpr>(stripInjectiveFunctions(V));
10766
10767 if (!AddRec && AllowPredicates)
10768 // Try to make this an AddRec using runtime tests, in the first X
10769 // iterations of this loop, where X is the SCEV expression found by the
10770 // algorithm below.
10771 AddRec = convertSCEVToAddRecWithPredicates(V, L, Predicates);
10772
10773 if (!AddRec || AddRec->getLoop() != L)
10774 return getCouldNotCompute();
10775
10776 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
10777 // the quadratic equation to solve it.
10778 if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
10779 // We can only use this value if the chrec ends up with an exact zero
10780 // value at this index. When solving for "X*X != 5", for example, we
10781 // should not accept a root of 2.
10782 if (auto S = SolveQuadraticAddRecExact(AddRec, *this)) {
10783 const auto *R = cast<SCEVConstant>(getConstant(*S));
10784 return ExitLimit(R, R, R, false, Predicates);
10785 }
10786 return getCouldNotCompute();
10787 }
10788
10789 // Otherwise we can only handle this if it is affine.
10790 if (!AddRec->isAffine())
10791 return getCouldNotCompute();
10792
10793 // If this is an affine expression, the execution count of this branch is
10794 // the minimum unsigned root of the following equation:
10795 //
10796 // Start + Step*N = 0 (mod 2^BW)
10797 //
10798 // equivalent to:
10799 //
10800 // Step*N = -Start (mod 2^BW)
10801 //
10802 // where BW is the common bit width of Start and Step.
10803
10804 // Get the initial value for the loop.
10805 const SCEV *Start = getSCEVAtScope(AddRec->getStart(), L->getParentLoop());
10806 const SCEV *Step = getSCEVAtScope(AddRec->getOperand(1), L->getParentLoop());
10807
10808 if (!isLoopInvariant(Step, L))
10809 return getCouldNotCompute();
10810
10811 LoopGuards Guards = LoopGuards::collect(L, *this);
10812 // Specialize step for this loop so we get context sensitive facts below.
10813 const SCEV *StepWLG = applyLoopGuards(Step, Guards);
10814
10815 // For positive steps (counting up until unsigned overflow):
10816 // N = -Start/Step (as unsigned)
10817 // For negative steps (counting down to zero):
10818 // N = Start/-Step
10819 // First compute the unsigned distance from zero in the direction of Step.
10820 bool CountDown = isKnownNegative(StepWLG);
10821 if (!CountDown && !isKnownNonNegative(StepWLG))
10822 return getCouldNotCompute();
10823
10824 const SCEV *Distance = CountDown ? Start : getNegativeSCEV(Start);
10825 // Handle unitary steps, which cannot wraparound.
10826 // 1*N = -Start; -1*N = Start (mod 2^BW), so:
10827 // N = Distance (as unsigned)
10828
10829 if (match(Step, m_CombineOr(m_scev_One(), m_scev_AllOnes()))) {
10830 APInt MaxBECount = getUnsignedRangeMax(applyLoopGuards(Distance, Guards));
10831 MaxBECount = APIntOps::umin(MaxBECount, getUnsignedRangeMax(Distance));
10832
10833 // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
10834 // we end up with a loop whose backedge-taken count is n - 1. Detect this
10835 // case, and see if we can improve the bound.
10836 //
10837 // Explicitly handling this here is necessary because getUnsignedRange
10838 // isn't context-sensitive; it doesn't know that we only care about the
10839 // range inside the loop.
10840 const SCEV *Zero = getZero(Distance->getType());
10841 const SCEV *One = getOne(Distance->getType());
10842 const SCEV *DistancePlusOne = getAddExpr(Distance, One);
10843 if (isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
10844 // If Distance + 1 doesn't overflow, we can compute the maximum distance
10845 // as "unsigned_max(Distance + 1) - 1". Also apply the loop guards to
10846 // Distance + 1; the range of Distance itself may be a wrapped set even
10847 // when the guards bound Distance + 1 tightly.
10848 APInt Max = APIntOps::umin(
10849 getUnsignedRangeMax(applyLoopGuards(DistancePlusOne, Guards)),
10850 getUnsignedRangeMax(DistancePlusOne));
10851 MaxBECount = APIntOps::umin(MaxBECount, Max - 1);
10852 }
10853 return ExitLimit(Distance, getConstant(MaxBECount), Distance, false,
10854 Predicates);
10855 }
10856
10857 // If the condition controls loop exit (the loop exits only if the expression
10858 // is true) and the addition is no-wrap we can use unsigned divide to
10859 // compute the backedge count. In this case, the step may not divide the
10860 // distance, but we don't care because if the condition is "missed" the loop
10861 // will have undefined behavior due to wrapping.
10862 if (ControlsOnlyExit && AddRec->hasNoSelfWrap() &&
10863 loopHasNoAbnormalExits(AddRec->getLoop())) {
10864
10865 // If the stride is zero and the start is non-zero, the loop must be
10866 // infinite. In C++, most loops are finite by assumption, in which case the
10867 // step being zero implies UB must execute if the loop is entered.
10868 if (!(loopIsFiniteByAssumption(L) && isKnownNonZero(Start)) &&
10869 !isKnownNonZero(StepWLG))
10870 return getCouldNotCompute();
10871
10872 const SCEV *Exact =
10873 getUDivExpr(Distance, CountDown ? getNegativeSCEV(Step) : Step);
10874 const SCEV *ConstantMax = getCouldNotCompute();
10875 if (Exact != getCouldNotCompute()) {
10876 APInt MaxInt = getUnsignedRangeMax(applyLoopGuards(Exact, Guards));
10877 ConstantMax =
10879 }
10880 const SCEV *SymbolicMax =
10881 isa<SCEVCouldNotCompute>(Exact) ? ConstantMax : Exact;
10882 return ExitLimit(Exact, ConstantMax, SymbolicMax, false, Predicates);
10883 }
10884
10885 // Solve the general equation.
10886 const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
10887 if (!StepC || StepC->getValue()->isZero())
10888 return getCouldNotCompute();
10889 const SCEV *E = SolveLinEquationWithOverflow(
10890 StepC->getAPInt(), getNegativeSCEV(Start),
10891 AllowPredicates ? &Predicates : nullptr, *this, L);
10892
10893 const SCEV *M = E;
10894 if (E != getCouldNotCompute()) {
10895 APInt MaxWithGuards = getUnsignedRangeMax(applyLoopGuards(E, Guards));
10896 M = getConstant(APIntOps::umin(MaxWithGuards, getUnsignedRangeMax(E)));
10897 }
10898 auto *S = isa<SCEVCouldNotCompute>(E) ? M : E;
10899 return ExitLimit(E, M, S, false, Predicates);
10900}
10901
10902ScalarEvolution::ExitLimit
10903ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) {
10904 // Loops that look like: while (X == 0) are very strange indeed. We don't
10905 // handle them yet except for the trivial case. This could be expanded in the
10906 // future as needed.
10907
10908 // If the value is a constant, check to see if it is known to be non-zero
10909 // already. If so, the backedge will execute zero times.
10910 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
10911 if (!C->getValue()->isZero())
10912 return getZero(C->getType());
10913 return getCouldNotCompute(); // Otherwise it will loop infinitely.
10914 }
10915
10916 // We could implement others, but I really doubt anyone writes loops like
10917 // this, and if they did, they would already be constant folded.
10918 return getCouldNotCompute();
10919}
10920
10921std::pair<const BasicBlock *, const BasicBlock *>
10922ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB)
10923 const {
10924 // If the block has a unique predecessor, then there is no path from the
10925 // predecessor to the block that does not go through the direct edge
10926 // from the predecessor to the block.
10927 if (const BasicBlock *Pred = BB->getSinglePredecessor())
10928 return {Pred, BB};
10929
10930 // A loop's header is defined to be a block that dominates the loop.
10931 // If the header has a unique predecessor outside the loop, it must be
10932 // a block that has exactly one successor that can reach the loop.
10933 if (const Loop *L = LI.getLoopFor(BB))
10934 return {L->getLoopPredecessor(), L->getHeader()};
10935
10936 return {nullptr, BB};
10937}
10938
10939/// SCEV structural equivalence is usually sufficient for testing whether two
10940/// expressions are equal, however for the purposes of looking for a condition
10941/// guarding a loop, it can be useful to be a little more general, since a
10942/// front-end may have replicated the controlling expression.
10943static bool HasSameValue(const SCEV *A, const SCEV *B) {
10944 // Quick check to see if they are the same SCEV.
10945 if (A == B) return true;
10946
10947 auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) {
10948 // Not all instructions that are "identical" compute the same value. For
10949 // instance, two distinct alloca instructions allocating the same type are
10950 // identical and do not read memory; but compute distinct values.
10951 return A->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A));
10952 };
10953
10954 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
10955 // two different instructions with the same value. Check for this case.
10956 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
10957 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
10958 if (const Instruction *AI = dyn_cast<Instruction>(AU->getValue()))
10959 if (const Instruction *BI = dyn_cast<Instruction>(BU->getValue()))
10960 if (ComputesEqualValues(AI, BI))
10961 return true;
10962
10963 // Otherwise assume they may have a different value.
10964 return false;
10965}
10966
10967static bool MatchBinarySub(const SCEV *S, SCEVUse &LHS, SCEVUse &RHS) {
10968 const SCEV *Op0, *Op1;
10969 if (!match(S, m_scev_Add(m_SCEV(Op0), m_SCEV(Op1))))
10970 return false;
10971 if (match(Op0, m_scev_Mul(m_scev_AllOnes(), m_SCEV(RHS)))) {
10972 LHS = Op1;
10973 return true;
10974 }
10975 if (match(Op1, m_scev_Mul(m_scev_AllOnes(), m_SCEV(RHS)))) {
10976 LHS = Op0;
10977 return true;
10978 }
10979 return false;
10980}
10981
10983 SCEVUse &RHS, unsigned Depth) {
10984 bool Changed = false;
10985 // Simplifies ICMP to trivial true or false by turning it into '0 == 0' or
10986 // '0 != 0'.
10987 auto TrivialCase = [&](bool TriviallyTrue) {
10989 Pred = TriviallyTrue ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
10990 return true;
10991 };
10992 // If we hit the max recursion limit bail out.
10993 if (Depth >= 3)
10994 return false;
10995
10996 const SCEV *NewLHS, *NewRHS;
10997 if (match(LHS, m_scev_c_Mul(m_SCEV(NewLHS), m_SCEVVScale())) &&
10998 match(RHS, m_scev_c_Mul(m_SCEV(NewRHS), m_SCEVVScale()))) {
10999 const SCEVMulExpr *LMul = cast<SCEVMulExpr>(LHS);
11000 const SCEVMulExpr *RMul = cast<SCEVMulExpr>(RHS);
11001
11002 // (X * vscale) pred (Y * vscale) ==> X pred Y
11003 // when both multiples are NSW.
11004 // (X * vscale) uicmp/eq/ne (Y * vscale) ==> X uicmp/eq/ne Y
11005 // when both multiples are NUW.
11006 if ((LMul->hasNoSignedWrap() && RMul->hasNoSignedWrap()) ||
11007 (LMul->hasNoUnsignedWrap() && RMul->hasNoUnsignedWrap() &&
11008 !ICmpInst::isSigned(Pred))) {
11009 LHS = NewLHS;
11010 RHS = NewRHS;
11011 Changed = true;
11012 }
11013 }
11014
11015 // Canonicalize a constant to the right side.
11016 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
11017 // Check for both operands constant.
11018 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
11019 if (!ICmpInst::compare(LHSC->getAPInt(), RHSC->getAPInt(), Pred))
11020 return TrivialCase(false);
11021 return TrivialCase(true);
11022 }
11023 // Otherwise swap the operands to put the constant on the right.
11024 std::swap(LHS, RHS);
11026 Changed = true;
11027 }
11028
11029 // (K + A) pred (K + B) --> A pred B
11030 // For equality, no flags are needed.
11031 // For signed, both adds must be NSW. For unsigned, both must be NUW.
11032 {
11033 const SCEVConstant *C = nullptr;
11034 if (match(LHS, m_scev_Add(m_SCEVConstant(C), m_SCEV(NewLHS))) &&
11035 match(RHS, m_scev_Add(m_scev_Specific(C), m_SCEV(NewRHS)))) {
11036 const auto *LAdd = cast<SCEVAddExpr>(LHS);
11037 const auto *RAdd = cast<SCEVAddExpr>(RHS);
11038 if (ICmpInst::isEquality(Pred) ||
11039 (ICmpInst::isSigned(Pred) && LAdd->hasNoSignedWrap() &&
11040 RAdd->hasNoSignedWrap()) ||
11041 (ICmpInst::isUnsigned(Pred) && LAdd->hasNoUnsignedWrap() &&
11042 RAdd->hasNoUnsignedWrap())) {
11043 LHS = NewLHS;
11044 RHS = NewRHS;
11045 Changed = true;
11046 }
11047 }
11048 }
11049
11050 // (C * A) pred (C * B) --> A pred B
11051 // For equality predicates, both muls must be NUW or both must be NSW
11052 // (either suffices to make multiplication by C injective; C == 0 is
11053 // impossible because SCEV folds 0 * X to 0).
11054 // For signed ordering, C must be positive and both muls must be NSW.
11055 // For unsigned ordering, both muls must be NUW.
11056 {
11057 const SCEVConstant *C = nullptr;
11058 if (match(LHS, m_scev_Mul(m_SCEVConstant(C), m_SCEV(NewLHS))) &&
11059 match(RHS, m_scev_Mul(m_scev_Specific(C), m_SCEV(NewRHS)))) {
11060 const auto *LMul = cast<SCEVMulExpr>(LHS);
11061 const auto *RMul = cast<SCEVMulExpr>(RHS);
11062 bool BothNUW = LMul->hasNoUnsignedWrap() && RMul->hasNoUnsignedWrap();
11063 bool BothNSW = LMul->hasNoSignedWrap() && RMul->hasNoSignedWrap();
11064 if ((ICmpInst::isEquality(Pred) && (BothNUW || BothNSW)) ||
11065 (ICmpInst::isSigned(Pred) && BothNSW &&
11066 C->getAPInt().isStrictlyPositive()) ||
11067 (ICmpInst::isUnsigned(Pred) && BothNUW)) {
11068 LHS = NewLHS;
11069 RHS = NewRHS;
11070 Changed = true;
11071 }
11072 }
11073 }
11074
11075 // If we're comparing an addrec with a value which is loop-invariant in the
11076 // addrec's loop, put the addrec on the left. Also make a dominance check,
11077 // as both operands could be addrecs loop-invariant in each other's loop.
11078 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) {
11079 const Loop *L = AR->getLoop();
11080 if (isLoopInvariant(LHS, L) && properlyDominates(LHS, L->getHeader())) {
11081 std::swap(LHS, RHS);
11083 Changed = true;
11084 }
11085 }
11086
11087 // If there's a constant operand, canonicalize comparisons with boundary
11088 // cases, and canonicalize *-or-equal comparisons to regular comparisons.
11089 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
11090 const APInt &RA = RC->getAPInt();
11091
11092 bool SimplifiedByConstantRange = false;
11093
11094 if (!ICmpInst::isEquality(Pred)) {
11096 if (ExactCR.isFullSet())
11097 return TrivialCase(true);
11098 if (ExactCR.isEmptySet())
11099 return TrivialCase(false);
11100
11101 APInt NewRHS;
11102 CmpInst::Predicate NewPred;
11103 if (ExactCR.getEquivalentICmp(NewPred, NewRHS) &&
11104 ICmpInst::isEquality(NewPred)) {
11105 // We were able to convert an inequality to an equality.
11106 Pred = NewPred;
11107 RHS = getConstant(NewRHS);
11108 Changed = SimplifiedByConstantRange = true;
11109 }
11110 }
11111
11112 if (!SimplifiedByConstantRange) {
11113 switch (Pred) {
11114 default:
11115 break;
11116 case ICmpInst::ICMP_EQ:
11117 case ICmpInst::ICMP_NE:
11118 // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
11119 if (RA.isZero() && MatchBinarySub(LHS, LHS, RHS))
11120 Changed = true;
11121 break;
11122
11123 // The "Should have been caught earlier!" messages refer to the fact
11124 // that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
11125 // should have fired on the corresponding cases, and canonicalized the
11126 // check to trivial case.
11127
11128 case ICmpInst::ICMP_UGE:
11129 assert(!RA.isMinValue() && "Should have been caught earlier!");
11130 Pred = ICmpInst::ICMP_UGT;
11131 RHS = getConstant(RA - 1);
11132 Changed = true;
11133 break;
11134 case ICmpInst::ICMP_ULE:
11135 assert(!RA.isMaxValue() && "Should have been caught earlier!");
11136 Pred = ICmpInst::ICMP_ULT;
11137 RHS = getConstant(RA + 1);
11138 Changed = true;
11139 break;
11140 case ICmpInst::ICMP_SGE:
11141 assert(!RA.isMinSignedValue() && "Should have been caught earlier!");
11142 Pred = ICmpInst::ICMP_SGT;
11143 RHS = getConstant(RA - 1);
11144 Changed = true;
11145 break;
11146 case ICmpInst::ICMP_SLE:
11147 assert(!RA.isMaxSignedValue() && "Should have been caught earlier!");
11148 Pred = ICmpInst::ICMP_SLT;
11149 RHS = getConstant(RA + 1);
11150 Changed = true;
11151 break;
11152 }
11153 }
11154 }
11155
11156 // a /u b == 0 => a < b
11157 // a /u b != 0 => a >= b
11158 if (ICmpInst::isEquality(Pred) && RHS->isZero() &&
11159 match(LHS, m_scev_UDiv(m_SCEV(LHS), m_SCEV(RHS)))) {
11161 Changed = true;
11162 }
11163
11164 // Check for obvious equality.
11165 if (HasSameValue(LHS, RHS)) {
11166 if (ICmpInst::isTrueWhenEqual(Pred))
11167 return TrivialCase(true);
11169 return TrivialCase(false);
11170 }
11171
11172 // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
11173 // adding or subtracting 1 from one of the operands.
11174 switch (Pred) {
11175 case ICmpInst::ICMP_SLE:
11176 if (!getSignedRangeMax(RHS).isMaxSignedValue()) {
11177 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
11179 Pred = ICmpInst::ICMP_SLT;
11180 Changed = true;
11181 } else if (!getSignedRangeMin(LHS).isMinSignedValue()) {
11182 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS,
11184 Pred = ICmpInst::ICMP_SLT;
11185 Changed = true;
11186 }
11187 break;
11188 case ICmpInst::ICMP_SGE:
11189 if (!getSignedRangeMin(RHS).isMinSignedValue()) {
11190 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS,
11192 Pred = ICmpInst::ICMP_SGT;
11193 Changed = true;
11194 } else if (!getSignedRangeMax(LHS).isMaxSignedValue()) {
11195 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
11197 Pred = ICmpInst::ICMP_SGT;
11198 Changed = true;
11199 }
11200 break;
11201 case ICmpInst::ICMP_ULE:
11202 if (!getUnsignedRangeMax(RHS).isMaxValue()) {
11203 RHS = getAddExpr(getConstant(RHS->getType(), 1, true), RHS,
11205 Pred = ICmpInst::ICMP_ULT;
11206 Changed = true;
11207 } else if (!getUnsignedRangeMin(LHS).isMinValue()) {
11208 LHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), LHS);
11209 Pred = ICmpInst::ICMP_ULT;
11210 Changed = true;
11211 }
11212 break;
11213 case ICmpInst::ICMP_UGE:
11214 // If RHS is an op we can fold the -1, try that first.
11215 // Otherwise prefer LHS to preserve the nuw flag.
11216 if ((isa<SCEVConstant>(RHS) ||
11218 isa<SCEVConstant>(cast<SCEVNAryExpr>(RHS)->getOperand(0)))) &&
11219 !getUnsignedRangeMin(RHS).isMinValue()) {
11220 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
11221 Pred = ICmpInst::ICMP_UGT;
11222 Changed = true;
11223 } else if (!getUnsignedRangeMax(LHS).isMaxValue()) {
11224 LHS = getAddExpr(getConstant(RHS->getType(), 1, true), LHS,
11226 Pred = ICmpInst::ICMP_UGT;
11227 Changed = true;
11228 } else if (!getUnsignedRangeMin(RHS).isMinValue()) {
11229 RHS = getAddExpr(getConstant(RHS->getType(), (uint64_t)-1, true), RHS);
11230 Pred = ICmpInst::ICMP_UGT;
11231 Changed = true;
11232 }
11233 break;
11234 default:
11235 break;
11236 }
11237
11238 // TODO: More simplifications are possible here.
11239
11240 // Recursively simplify until we either hit a recursion limit or nothing
11241 // changes.
11242 if (Changed)
11243 (void)SimplifyICmpOperands(Pred, LHS, RHS, Depth + 1);
11244
11245 return Changed;
11246}
11247
11249 return getSignedRangeMax(S).isNegative();
11250}
11251
11255
11257 return !getSignedRangeMin(S).isNegative();
11258}
11259
11263
11265 // Query push down for cases where the unsigned range is
11266 // less than sufficient.
11267 if (const auto *SExt = dyn_cast<SCEVSignExtendExpr>(S))
11268 return isKnownNonZero(SExt->getOperand(0));
11269 return getUnsignedRangeMin(S) != 0;
11270}
11271
11273 bool OrNegative) {
11274 auto NonRecursive = [OrNegative](const SCEV *S) {
11275 if (auto *C = dyn_cast<SCEVConstant>(S))
11276 return C->getAPInt().isPowerOf2() ||
11277 (OrNegative && C->getAPInt().isNegatedPowerOf2());
11278
11279 // vscale is a power-of-two.
11280 return isa<SCEVVScale>(S);
11281 };
11282
11283 if (NonRecursive(S))
11284 return true;
11285
11286 auto *Mul = dyn_cast<SCEVMulExpr>(S);
11287 if (!Mul)
11288 return false;
11289 return all_of(Mul->operands(), NonRecursive) && (OrZero || isKnownNonZero(S));
11290}
11291
11293 const SCEV *S, uint64_t M,
11295 if (M == 0)
11296 return false;
11297 if (M == 1)
11298 return true;
11299
11300 // For a constant, check that "S % M == 0".
11301 if (auto *Cst = dyn_cast<SCEVConstant>(S)) {
11302 APInt C = Cst->getAPInt();
11303 return C.urem(M) == 0;
11304 }
11305
11306 // Basic tests have failed.
11307 // Check "S % M == 0" at compile time and record runtime Assumptions.
11308 auto *STy = dyn_cast<IntegerType>(S->getType());
11309 const SCEV *SmodM =
11310 getURemExpr(S, getConstant(ConstantInt::get(STy, M, false)));
11311 const SCEV *Zero = getZero(STy);
11312
11313 // Check whether "S % M == 0" is known at compile time.
11314 if (isKnownPredicate(ICmpInst::ICMP_EQ, SmodM, Zero))
11315 return true;
11316
11317 // Check whether "S % M != 0" is known at compile time.
11318 if (isKnownPredicate(ICmpInst::ICMP_NE, SmodM, Zero))
11319 return false;
11320
11321 if (!Predicates)
11322 return false;
11323
11324 // Look through Add and AddRec expressions with nuw to improve the
11325 // precision of added predicates. S is a multiple of M if S starts with a
11326 // multiple of M and at every iteration step S only adds multiples of M.
11329 all_of(S->operands(),
11330 [&](SCEVUse Op) { return isKnownMultipleOf(Op, M, Predicates); }))
11331 return true;
11332
11333 // Similarly, look through Mul with nuw, where any operand being a
11334 // known-multiple is sufficient.
11335 if (auto *Mul = dyn_cast<SCEVMulExpr>(S))
11336 if (Mul->hasNoUnsignedWrap() && any_of(S->operands(), [&](SCEVUse Op) {
11337 return isKnownMultipleOf(Op, M, Predicates);
11338 }))
11339 return true;
11340
11341 // Similarly, look through MinMax, with no wrapping arithmetic to consider.
11342 if (isa<SCEVMinMaxExpr>(S) && all_of(S->operands(), [&](SCEVUse Op) {
11343 return isKnownMultipleOf(Op, M, Predicates);
11344 }))
11345 return true;
11346
11348
11349 // Detect redundant predicates.
11350 for (auto *A : *Predicates)
11351 if (A->implies(P, *this))
11352 return true;
11353
11354 // Only record non-redundant predicates.
11355 Predicates->push_back(P);
11356 return true;
11357}
11358
11360 return ((isKnownNonNegative(S1) && isKnownNonNegative(S2)) ||
11362}
11363
11364std::pair<const SCEV *, const SCEV *>
11366 // Compute SCEV on entry of loop L.
11367 const SCEV *Start = SCEVInitRewriter::rewrite(S, L, *this);
11368 if (Start == getCouldNotCompute())
11369 return { Start, Start };
11370 // Compute post increment SCEV for loop L.
11371 const SCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, *this);
11372 assert(PostInc != getCouldNotCompute() && "Unexpected could not compute");
11373 return { Start, PostInc };
11374}
11375
11377 SCEVUse RHS) {
11378 // First collect all loops.
11380 getUsedLoops(LHS, LoopsUsed);
11381 getUsedLoops(RHS, LoopsUsed);
11382
11383 if (LoopsUsed.empty())
11384 return false;
11385
11386 // Domination relationship must be a linear order on collected loops.
11387#ifndef NDEBUG
11388 for (const auto *L1 : LoopsUsed)
11389 for (const auto *L2 : LoopsUsed)
11390 assert((DT.dominates(L1->getHeader(), L2->getHeader()) ||
11391 DT.dominates(L2->getHeader(), L1->getHeader())) &&
11392 "Domination relationship is not a linear order");
11393#endif
11394
11395 const Loop *MDL =
11396 *llvm::max_element(LoopsUsed, [&](const Loop *L1, const Loop *L2) {
11397 return DT.properlyDominates(L1->getHeader(), L2->getHeader());
11398 });
11399
11400 // Get init and post increment value for LHS.
11401 auto SplitLHS = SplitIntoInitAndPostInc(MDL, LHS);
11402 // if LHS contains unknown non-invariant SCEV then bail out.
11403 if (SplitLHS.first == getCouldNotCompute())
11404 return false;
11405 assert (SplitLHS.second != getCouldNotCompute() && "Unexpected CNC");
11406 // Get init and post increment value for RHS.
11407 auto SplitRHS = SplitIntoInitAndPostInc(MDL, RHS);
11408 // if RHS contains unknown non-invariant SCEV then bail out.
11409 if (SplitRHS.first == getCouldNotCompute())
11410 return false;
11411 assert (SplitRHS.second != getCouldNotCompute() && "Unexpected CNC");
11412 // It is possible that init SCEV contains an invariant load but it does
11413 // not dominate MDL and is not available at MDL loop entry, so we should
11414 // check it here.
11415 if (!isAvailableAtLoopEntry(SplitLHS.first, MDL) ||
11416 !isAvailableAtLoopEntry(SplitRHS.first, MDL))
11417 return false;
11418
11419 // It seems backedge guard check is faster than entry one so in some cases
11420 // it can speed up whole estimation by short circuit
11421 return isLoopBackedgeGuardedByCond(MDL, Pred, SplitLHS.second,
11422 SplitRHS.second) &&
11423 isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first);
11424}
11425
11427 SCEVUse RHS) {
11428 // Canonicalize the inputs first.
11429 (void)SimplifyICmpOperands(Pred, LHS, RHS);
11430
11431 return isKnownViaInduction(Pred, LHS, RHS) ||
11432 isKnownPredicateViaSplitting(Pred, LHS, RHS) ||
11433 isKnownViaNonRecursiveReasoning(Pred, LHS, RHS);
11434}
11435
11437 const SCEV *LHS,
11438 const SCEV *RHS) {
11439 if (isKnownPredicate(Pred, LHS, RHS))
11440 return true;
11442 return false;
11443 return std::nullopt;
11444}
11445
11447 const SCEV *RHS,
11448 const Instruction *CtxI) {
11449 // TODO: Analyze guards and assumes from Context's block.
11450 return isKnownPredicate(Pred, LHS, RHS) ||
11451 isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS);
11452}
11453
11454std::optional<bool>
11456 const SCEV *RHS, const Instruction *CtxI) {
11457 std::optional<bool> KnownWithoutContext = evaluatePredicate(Pred, LHS, RHS);
11458 if (KnownWithoutContext)
11459 return KnownWithoutContext;
11460
11461 if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred, LHS, RHS))
11462 return true;
11464 CtxI->getParent(), ICmpInst::getInverseCmpPredicate(Pred), LHS, RHS))
11465 return false;
11466 return std::nullopt;
11467}
11468
11470 const SCEVAddRecExpr *LHS,
11471 const SCEV *RHS) {
11472 const Loop *L = LHS->getLoop();
11473 return isLoopEntryGuardedByCond(L, Pred, LHS->getStart(), RHS) &&
11474 isLoopBackedgeGuardedByCond(L, Pred, LHS->getPostIncExpr(*this), RHS);
11475}
11476
11477std::optional<ScalarEvolution::MonotonicPredicateType>
11479 ICmpInst::Predicate Pred) {
11480 auto Result = getMonotonicPredicateTypeImpl(LHS, Pred);
11481
11482#ifndef NDEBUG
11483 // Verify an invariant: inverting the predicate should turn a monotonically
11484 // increasing change to a monotonically decreasing one, and vice versa.
11485 if (Result) {
11486 auto ResultSwapped =
11487 getMonotonicPredicateTypeImpl(LHS, ICmpInst::getSwappedPredicate(Pred));
11488
11489 assert(*ResultSwapped != *Result &&
11490 "monotonicity should flip as we flip the predicate");
11491 }
11492#endif
11493
11494 return Result;
11495}
11496
11497std::optional<ScalarEvolution::MonotonicPredicateType>
11498ScalarEvolution::getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS,
11499 ICmpInst::Predicate Pred) {
11500 // A zero step value for LHS means the induction variable is essentially a
11501 // loop invariant value. We don't really depend on the predicate actually
11502 // flipping from false to true (for increasing predicates, and the other way
11503 // around for decreasing predicates), all we care about is that *if* the
11504 // predicate changes then it only changes from false to true.
11505 //
11506 // A zero step value in itself is not very useful, but there may be places
11507 // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be
11508 // as general as possible.
11509
11510 // Only handle LE/LT/GE/GT predicates.
11511 if (!ICmpInst::isRelational(Pred))
11512 return std::nullopt;
11513
11514 bool IsGreater = ICmpInst::isGE(Pred) || ICmpInst::isGT(Pred);
11515 assert((IsGreater || ICmpInst::isLE(Pred) || ICmpInst::isLT(Pred)) &&
11516 "Should be greater or less!");
11517
11518 // Check that AR does not wrap.
11519 if (ICmpInst::isUnsigned(Pred)) {
11520 if (!LHS->hasNoUnsignedWrap())
11521 return std::nullopt;
11523 }
11524 assert(ICmpInst::isSigned(Pred) &&
11525 "Relational predicate is either signed or unsigned!");
11526 if (!LHS->hasNoSignedWrap())
11527 return std::nullopt;
11528
11529 const SCEV *Step = LHS->getStepRecurrence(*this);
11530
11531 if (isKnownNonNegative(Step))
11533
11534 if (isKnownNonPositive(Step))
11536
11537 return std::nullopt;
11538}
11539
11540std::optional<ScalarEvolution::LoopInvariantPredicate>
11542 const SCEV *RHS, const Loop *L,
11543 const Instruction *CtxI) {
11544 // If there is a loop-invariant, force it into the RHS, otherwise bail out.
11545 if (!isLoopInvariant(RHS, L)) {
11546 if (!isLoopInvariant(LHS, L))
11547 return std::nullopt;
11548
11549 std::swap(LHS, RHS);
11551 }
11552
11553 const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS);
11554 if (!ArLHS || ArLHS->getLoop() != L)
11555 return std::nullopt;
11556
11557 auto MonotonicType = getMonotonicPredicateType(ArLHS, Pred);
11558 if (!MonotonicType)
11559 return std::nullopt;
11560 // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to
11561 // true as the loop iterates, and the backedge is control dependent on
11562 // "ArLHS `Pred` RHS" == true then we can reason as follows:
11563 //
11564 // * if the predicate was false in the first iteration then the predicate
11565 // is never evaluated again, since the loop exits without taking the
11566 // backedge.
11567 // * if the predicate was true in the first iteration then it will
11568 // continue to be true for all future iterations since it is
11569 // monotonically increasing.
11570 //
11571 // For both the above possibilities, we can replace the loop varying
11572 // predicate with its value on the first iteration of the loop (which is
11573 // loop invariant).
11574 //
11575 // A similar reasoning applies for a monotonically decreasing predicate, by
11576 // replacing true with false and false with true in the above two bullets.
11578 auto P = Increasing ? Pred : ICmpInst::getInverseCmpPredicate(Pred);
11579
11580 if (isLoopBackedgeGuardedByCond(L, P, LHS, RHS))
11582 RHS);
11583
11584 if (!CtxI)
11585 return std::nullopt;
11586 // Try to prove via context.
11587 // TODO: Support other cases.
11588 switch (Pred) {
11589 default:
11590 break;
11591 case ICmpInst::ICMP_ULE:
11592 case ICmpInst::ICMP_ULT: {
11593 assert(ArLHS->hasNoUnsignedWrap() && "Is a requirement of monotonicity!");
11594 // Given preconditions
11595 // (1) ArLHS does not cross the border of positive and negative parts of
11596 // range because of:
11597 // - Positive step; (TODO: lift this limitation)
11598 // - nuw - does not cross zero boundary;
11599 // - nsw - does not cross SINT_MAX boundary;
11600 // (2) ArLHS <s RHS
11601 // (3) RHS >=s 0
11602 // we can replace the loop variant ArLHS <u RHS condition with loop
11603 // invariant Start(ArLHS) <u RHS.
11604 //
11605 // Because of (1) there are two options:
11606 // - ArLHS is always negative. It means that ArLHS <u RHS is always false;
11607 // - ArLHS is always non-negative. Because of (3) RHS is also non-negative.
11608 // It means that ArLHS <s RHS <=> ArLHS <u RHS.
11609 // Because of (2) ArLHS <u RHS is trivially true.
11610 // All together it means that ArLHS <u RHS <=> Start(ArLHS) >=s 0.
11611 // We can strengthen this to Start(ArLHS) <u RHS.
11612 auto SignFlippedPred = ICmpInst::getFlippedSignednessPredicate(Pred);
11613 if (ArLHS->hasNoSignedWrap() && ArLHS->isAffine() &&
11614 isKnownPositive(ArLHS->getStepRecurrence(*this)) &&
11615 isKnownNonNegative(RHS) &&
11616 isKnownPredicateAt(SignFlippedPred, ArLHS, RHS, CtxI))
11618 RHS);
11619 }
11620 }
11621
11622 return std::nullopt;
11623}
11624
11625std::optional<ScalarEvolution::LoopInvariantPredicate>
11627 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
11628 const Instruction *CtxI, const SCEV *MaxIter) {
11630 Pred, LHS, RHS, L, CtxI, MaxIter))
11631 return LIP;
11632 if (auto *UMin = dyn_cast<SCEVUMinExpr>(MaxIter))
11633 // Number of iterations expressed as UMIN isn't always great for expressing
11634 // the value on the last iteration. If the straightforward approach didn't
11635 // work, try the following trick: if the a predicate is invariant for X, it
11636 // is also invariant for umin(X, ...). So try to find something that works
11637 // among subexpressions of MaxIter expressed as umin.
11638 for (SCEVUse Op : UMin->operands())
11640 Pred, LHS, RHS, L, CtxI, Op))
11641 return LIP;
11642 return std::nullopt;
11643}
11644
11645std::optional<ScalarEvolution::LoopInvariantPredicate>
11647 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
11648 const Instruction *CtxI, const SCEV *MaxIter) {
11649 // Try to prove the following set of facts:
11650 // - The predicate is monotonic in the iteration space.
11651 // - If the check does not fail on the 1st iteration:
11652 // - No overflow will happen during first MaxIter iterations;
11653 // - It will not fail on the MaxIter'th iteration.
11654 // If the check does fail on the 1st iteration, we leave the loop and no
11655 // other checks matter.
11656
11657 // If there is a loop-invariant, force it into the RHS, otherwise bail out.
11658 if (!isLoopInvariant(RHS, L)) {
11659 if (!isLoopInvariant(LHS, L))
11660 return std::nullopt;
11661
11662 std::swap(LHS, RHS);
11664 }
11665
11666 auto *AR = dyn_cast<SCEVAddRecExpr>(LHS);
11667 if (!AR || AR->getLoop() != L)
11668 return std::nullopt;
11669
11670 // Even if both are valid, we need to consistently chose the unsigned or the
11671 // signed predicate below, not mixtures of both. For now, prefer the unsigned
11672 // predicate.
11673 Pred = Pred.dropSameSign();
11674
11675 // The predicate must be relational (i.e. <, <=, >=, >).
11676 if (!ICmpInst::isRelational(Pred))
11677 return std::nullopt;
11678
11679 // TODO: Support steps other than +/- 1.
11680 const SCEV *Step = AR->getStepRecurrence(*this);
11681 auto *One = getOne(Step->getType());
11682 auto *MinusOne = getNegativeSCEV(One);
11683 if (Step != One && Step != MinusOne)
11684 return std::nullopt;
11685
11686 // Type mismatch here means that MaxIter is potentially larger than max
11687 // unsigned value in start type, which mean we cannot prove no wrap for the
11688 // indvar.
11689 if (AR->getType() != MaxIter->getType())
11690 return std::nullopt;
11691
11692 // Value of IV on suggested last iteration.
11693 const SCEV *Last = AR->evaluateAtIteration(MaxIter, *this);
11694 // Does it still meet the requirement?
11695 if (!isLoopBackedgeGuardedByCond(L, Pred, Last, RHS))
11696 return std::nullopt;
11697 // Because step is +/- 1 and MaxIter has same type as Start (i.e. it does
11698 // not exceed max unsigned value of this type), this effectively proves
11699 // that there is no wrap during the iteration. To prove that there is no
11700 // signed/unsigned wrap, we need to check that
11701 // Start <= Last for step = 1 or Start >= Last for step = -1.
11702 ICmpInst::Predicate NoOverflowPred =
11704 if (Step == MinusOne)
11705 NoOverflowPred = ICmpInst::getSwappedPredicate(NoOverflowPred);
11706 const SCEV *Start = AR->getStart();
11707 if (!isKnownPredicateAt(NoOverflowPred, Start, Last, CtxI))
11708 return std::nullopt;
11709
11710 // Everything is fine.
11711 return ScalarEvolution::LoopInvariantPredicate(Pred, Start, RHS);
11712}
11713
11714bool ScalarEvolution::isKnownPredicateViaConstantRanges(CmpPredicate Pred,
11715 SCEVUse LHS,
11716 SCEVUse RHS) {
11717 if (HasSameValue(LHS, RHS))
11718 return ICmpInst::isTrueWhenEqual(Pred);
11719
11720 auto CheckRange = [&](bool IsSigned) {
11721 auto RangeLHS = IsSigned ? getSignedRange(LHS) : getUnsignedRange(LHS);
11722 auto RangeRHS = IsSigned ? getSignedRange(RHS) : getUnsignedRange(RHS);
11723 return RangeLHS.icmp(Pred, RangeRHS);
11724 };
11725
11726 // The check at the top of the function catches the case where the values are
11727 // known to be equal.
11728 if (Pred == CmpInst::ICMP_EQ)
11729 return false;
11730
11731 if (Pred == CmpInst::ICMP_NE) {
11732 if (CheckRange(true) || CheckRange(false))
11733 return true;
11734 auto *Diff = getMinusSCEV(LHS, RHS);
11735 return !isa<SCEVCouldNotCompute>(Diff) && isKnownNonZero(Diff);
11736 }
11737
11738 return CheckRange(CmpInst::isSigned(Pred));
11739}
11740
11741bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
11743 // Match X to (A + C1)<ExpectedFlags> and Y to (A + C2)<ExpectedFlags>, where
11744 // C1 and C2 are constant integers. If either X or Y are not add expressions,
11745 // consider them as X + 0 and Y + 0 respectively. C1 and C2 are returned via
11746 // OutC1 and OutC2.
11747 auto MatchBinaryAddToConst = [this](SCEVUse X, SCEVUse Y, APInt &OutC1,
11748 APInt &OutC2,
11749 SCEV::NoWrapFlags ExpectedFlags) {
11750 SCEVUse XNonConstOp, XConstOp;
11751 SCEVUse YNonConstOp, YConstOp;
11752 SCEV::NoWrapFlags XFlagsPresent;
11753 SCEV::NoWrapFlags YFlagsPresent;
11754
11755 if (!splitBinaryAdd(X, XConstOp, XNonConstOp, XFlagsPresent)) {
11756 XConstOp = getZero(X->getType());
11757 XNonConstOp = X;
11758 XFlagsPresent = ExpectedFlags;
11759 }
11760 if (!isa<SCEVConstant>(XConstOp))
11761 return false;
11762
11763 if (!splitBinaryAdd(Y, YConstOp, YNonConstOp, YFlagsPresent)) {
11764 YConstOp = getZero(Y->getType());
11765 YNonConstOp = Y;
11766 YFlagsPresent = ExpectedFlags;
11767 }
11768
11769 if (YNonConstOp != XNonConstOp)
11770 return false;
11771
11772 if (!isa<SCEVConstant>(YConstOp))
11773 return false;
11774
11775 // When matching ADDs with NUW flags (and unsigned predicates), only the
11776 // second ADD (with the larger constant) requires NUW.
11777 if ((YFlagsPresent & ExpectedFlags) != ExpectedFlags)
11778 return false;
11779 if (ExpectedFlags != SCEV::FlagNUW &&
11780 (XFlagsPresent & ExpectedFlags) != ExpectedFlags) {
11781 return false;
11782 }
11783
11784 OutC1 = cast<SCEVConstant>(XConstOp)->getAPInt();
11785 OutC2 = cast<SCEVConstant>(YConstOp)->getAPInt();
11786
11787 return true;
11788 };
11789
11790 APInt C1;
11791 APInt C2;
11792
11793 switch (Pred) {
11794 default:
11795 break;
11796
11797 case ICmpInst::ICMP_SGE:
11798 std::swap(LHS, RHS);
11799 [[fallthrough]];
11800 case ICmpInst::ICMP_SLE:
11801 // (X + C1)<nsw> s<= (X + C2)<nsw> if C1 s<= C2.
11802 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.sle(C2))
11803 return true;
11804
11805 break;
11806
11807 case ICmpInst::ICMP_SGT:
11808 std::swap(LHS, RHS);
11809 [[fallthrough]];
11810 case ICmpInst::ICMP_SLT:
11811 // (X + C1)<nsw> s< (X + C2)<nsw> if C1 s< C2.
11812 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.slt(C2))
11813 return true;
11814
11815 break;
11816
11817 case ICmpInst::ICMP_UGE:
11818 std::swap(LHS, RHS);
11819 [[fallthrough]];
11820 case ICmpInst::ICMP_ULE:
11821 // (X + C1) u<= (X + C2)<nuw> for C1 u<= C2.
11822 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNUW) && C1.ule(C2))
11823 return true;
11824
11825 break;
11826
11827 case ICmpInst::ICMP_UGT:
11828 std::swap(LHS, RHS);
11829 [[fallthrough]];
11830 case ICmpInst::ICMP_ULT:
11831 // (X + C1) u< (X + C2)<nuw> if C1 u< C2.
11832 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNUW) && C1.ult(C2))
11833 return true;
11834 break;
11835 }
11836
11837 return false;
11838}
11839
11840bool ScalarEvolution::isKnownPredicateViaSplitting(CmpPredicate Pred,
11842 if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate)
11843 return false;
11844
11845 // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on
11846 // the stack can result in exponential time complexity.
11847 SaveAndRestore Restore(ProvingSplitPredicate, true);
11848
11849 // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L
11850 //
11851 // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use
11852 // isKnownPredicate. isKnownPredicate is more powerful, but also more
11853 // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the
11854 // interesting cases seen in practice. We can consider "upgrading" L >= 0 to
11855 // use isKnownPredicate later if needed.
11856 return isKnownNonNegative(RHS) &&
11859}
11860
11861bool ScalarEvolution::isImpliedViaGuard(const BasicBlock *BB, CmpPredicate Pred,
11862 const SCEV *LHS, const SCEV *RHS) {
11863 // No need to even try if we know the module has no guards.
11864 if (!HasGuards)
11865 return false;
11866
11867 return any_of(*BB, [&](const Instruction &I) {
11868 using namespace llvm::PatternMatch;
11869
11870 Value *Condition;
11872 m_Value(Condition))) &&
11873 isImpliedCond(Pred, LHS, RHS, Condition, false);
11874 });
11875}
11876
11877/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
11878/// protected by a conditional between LHS and RHS. This is used to
11879/// to eliminate casts.
11881 CmpPredicate Pred,
11882 const SCEV *LHS,
11883 const SCEV *RHS) {
11884 // Interpret a null as meaning no loop, where there is obviously no guard
11885 // (interprocedural conditions notwithstanding). Do not bother about
11886 // unreachable loops.
11887 if (!L || !DT.isReachableFromEntry(L->getHeader()))
11888 return true;
11889
11890 if (VerifyIR)
11891 assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
11892 "This cannot be done on broken IR!");
11893
11894
11895 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
11896 return true;
11897
11898 BasicBlock *Latch = L->getLoopLatch();
11899 if (!Latch)
11900 return false;
11901
11902 CondBrInst *LoopContinuePredicate =
11904 if (LoopContinuePredicate &&
11905 isImpliedCond(Pred, LHS, RHS, LoopContinuePredicate->getCondition(),
11906 LoopContinuePredicate->getSuccessor(0) != L->getHeader()))
11907 return true;
11908
11909 // We don't want more than one activation of the following loops on the stack
11910 // -- that can lead to O(n!) time complexity.
11911 if (WalkingBEDominatingConds)
11912 return false;
11913
11914 SaveAndRestore ClearOnExit(WalkingBEDominatingConds, true);
11915
11916 // See if we can exploit a trip count to prove the predicate.
11917 const auto &BETakenInfo = getBackedgeTakenInfo(L);
11918 const SCEV *LatchBECount = BETakenInfo.getExact(Latch, this);
11919 if (LatchBECount != getCouldNotCompute()) {
11920 // We know that Latch branches back to the loop header exactly
11921 // LatchBECount times. This means the backdege condition at Latch is
11922 // equivalent to "{0,+,1} u< LatchBECount".
11923 Type *Ty = LatchBECount->getType();
11924 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
11925 const SCEV *LoopCounter =
11926 getAddRecExpr(getZero(Ty), getOne(Ty), L, NoWrapFlags);
11927 if (isImpliedCond(Pred, LHS, RHS, ICmpInst::ICMP_ULT, LoopCounter,
11928 LatchBECount))
11929 return true;
11930 }
11931
11932 // Check conditions due to any @llvm.assume intrinsics.
11933 for (auto &AssumeVH : AC.assumptions()) {
11934 if (!AssumeVH)
11935 continue;
11936 auto *CI = cast<CallInst>(AssumeVH);
11937 if (!DT.dominates(CI, Latch->getTerminator()))
11938 continue;
11939
11940 if (isImpliedCond(Pred, LHS, RHS, CI->getArgOperand(0), false))
11941 return true;
11942 }
11943
11944 if (isImpliedViaGuard(Latch, Pred, LHS, RHS))
11945 return true;
11946
11947 for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()];
11948 DTN != HeaderDTN; DTN = DTN->getIDom()) {
11949 assert(DTN && "should reach the loop header before reaching the root!");
11950
11951 BasicBlock *BB = DTN->getBlock();
11952 if (isImpliedViaGuard(BB, Pred, LHS, RHS))
11953 return true;
11954
11955 BasicBlock *PBB = BB->getSinglePredecessor();
11956 if (!PBB)
11957 continue;
11958
11960 if (!ContBr || ContBr->getSuccessor(0) == ContBr->getSuccessor(1))
11961 continue;
11962
11963 // If we have an edge `E` within the loop body that dominates the only
11964 // latch, the condition guarding `E` also guards the backedge. This
11965 // reasoning works only for loops with a single latch.
11966 // We're constructively (and conservatively) enumerating edges within the
11967 // loop body that dominate the latch. The dominator tree better agree
11968 // with us on this:
11969 assert(DT.dominates(BasicBlockEdge(PBB, BB), Latch) && "should be!");
11970 if (isImpliedCond(Pred, LHS, RHS, ContBr->getCondition(),
11971 BB != ContBr->getSuccessor(0)))
11972 return true;
11973 }
11974
11975 return false;
11976}
11977
11979 CmpPredicate Pred,
11980 const SCEV *LHS,
11981 const SCEV *RHS) {
11982 // Do not bother proving facts for unreachable code.
11983 if (!DT.isReachableFromEntry(BB))
11984 return true;
11985 if (VerifyIR)
11986 assert(!verifyFunction(*BB->getParent(), &dbgs()) &&
11987 "This cannot be done on broken IR!");
11988
11989 // If we cannot prove strict comparison (e.g. a > b), maybe we can prove
11990 // the facts (a >= b && a != b) separately. A typical situation is when the
11991 // non-strict comparison is known from ranges and non-equality is known from
11992 // dominating predicates. If we are proving strict comparison, we always try
11993 // to prove non-equality and non-strict comparison separately.
11994 CmpPredicate NonStrictPredicate = ICmpInst::getNonStrictCmpPredicate(Pred);
11995 const bool ProvingStrictComparison =
11996 Pred != NonStrictPredicate.dropSameSign();
11997 bool ProvedNonStrictComparison = false;
11998 bool ProvedNonEquality = false;
11999
12000 auto SplitAndProve = [&](std::function<bool(CmpPredicate)> Fn) -> bool {
12001 if (!ProvedNonStrictComparison)
12002 ProvedNonStrictComparison = Fn(NonStrictPredicate);
12003 if (!ProvedNonEquality)
12004 ProvedNonEquality = Fn(ICmpInst::ICMP_NE);
12005 if (ProvedNonStrictComparison && ProvedNonEquality)
12006 return true;
12007 return false;
12008 };
12009
12010 if (ProvingStrictComparison) {
12011 auto ProofFn = [&](CmpPredicate P) {
12012 return isKnownViaNonRecursiveReasoning(P, LHS, RHS);
12013 };
12014 if (SplitAndProve(ProofFn))
12015 return true;
12016 }
12017
12018 // Try to prove (Pred, LHS, RHS) using isImpliedCond.
12019 auto ProveViaCond = [&](const Value *Condition, bool Inverse) {
12020 const Instruction *CtxI = &BB->front();
12021 if (isImpliedCond(Pred, LHS, RHS, Condition, Inverse, CtxI))
12022 return true;
12023 if (ProvingStrictComparison) {
12024 auto ProofFn = [&](CmpPredicate P) {
12025 return isImpliedCond(P, LHS, RHS, Condition, Inverse, CtxI);
12026 };
12027 if (SplitAndProve(ProofFn))
12028 return true;
12029 }
12030 return false;
12031 };
12032
12033 // Starting at the block's predecessor, climb up the predecessor chain, as long
12034 // as there are predecessors that can be found that have unique successors
12035 // leading to the original block.
12036 const Loop *ContainingLoop = LI.getLoopFor(BB);
12037 const BasicBlock *PredBB;
12038 if (ContainingLoop && ContainingLoop->getHeader() == BB)
12039 PredBB = ContainingLoop->getLoopPredecessor();
12040 else
12041 PredBB = BB->getSinglePredecessor();
12042 for (std::pair<const BasicBlock *, const BasicBlock *> Pair(PredBB, BB);
12043 Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
12044 const CondBrInst *BlockEntryPredicate =
12045 dyn_cast<CondBrInst>(Pair.first->getTerminator());
12046 if (!BlockEntryPredicate)
12047 continue;
12048
12049 if (ProveViaCond(BlockEntryPredicate->getCondition(),
12050 BlockEntryPredicate->getSuccessor(0) != Pair.second))
12051 return true;
12052 }
12053
12054 // Check conditions due to any @llvm.assume intrinsics.
12055 for (auto &AssumeVH : AC.assumptions()) {
12056 if (!AssumeVH)
12057 continue;
12058 auto *CI = cast<CallInst>(AssumeVH);
12059 if (!DT.dominates(CI, BB))
12060 continue;
12061
12062 if (ProveViaCond(CI->getArgOperand(0), false))
12063 return true;
12064 }
12065
12066 // Check conditions due to any @llvm.experimental.guard intrinsics.
12067 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
12068 F.getParent(), Intrinsic::experimental_guard);
12069 if (GuardDecl)
12070 for (const auto *GU : GuardDecl->users())
12071 if (const auto *Guard = dyn_cast<IntrinsicInst>(GU))
12072 if (Guard->getFunction() == BB->getParent() && DT.dominates(Guard, BB))
12073 if (ProveViaCond(Guard->getArgOperand(0), false))
12074 return true;
12075 return false;
12076}
12077
12079 const SCEV *LHS,
12080 const SCEV *RHS) {
12081 // Interpret a null as meaning no loop, where there is obviously no guard
12082 // (interprocedural conditions notwithstanding).
12083 if (!L)
12084 return false;
12085
12086 // Both LHS and RHS must be available at loop entry.
12088 "LHS is not available at Loop Entry");
12090 "RHS is not available at Loop Entry");
12091
12092 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
12093 return true;
12094
12095 return isBasicBlockEntryGuardedByCond(L->getHeader(), Pred, LHS, RHS);
12096}
12097
12098bool ScalarEvolution::isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
12099 const SCEV *RHS,
12100 const Value *FoundCondValue, bool Inverse,
12101 const Instruction *CtxI) {
12102 // False conditions implies anything. Do not bother analyzing it further.
12103 if (FoundCondValue ==
12104 ConstantInt::getBool(FoundCondValue->getContext(), Inverse))
12105 return true;
12106
12107 if (!PendingLoopPredicates.insert(FoundCondValue).second)
12108 return false;
12109
12110 llvm::scope_exit ClearOnExit(
12111 [&]() { PendingLoopPredicates.erase(FoundCondValue); });
12112
12113 // Recursively handle And and Or conditions.
12114 const Value *Op0, *Op1;
12115 if (match(FoundCondValue, m_LogicalAnd(m_Value(Op0), m_Value(Op1)))) {
12116 if (!Inverse)
12117 return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) ||
12118 isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI);
12119 } else if (match(FoundCondValue, m_LogicalOr(m_Value(Op0), m_Value(Op1)))) {
12120 if (Inverse)
12121 return isImpliedCond(Pred, LHS, RHS, Op0, Inverse, CtxI) ||
12122 isImpliedCond(Pred, LHS, RHS, Op1, Inverse, CtxI);
12123 }
12124
12125 const ICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue);
12126 if (!ICI) return false;
12127
12128 // Now that we found a conditional branch that dominates the loop or controls
12129 // the loop latch. Check to see if it is the comparison we are looking for.
12130 CmpPredicate FoundPred;
12131 if (Inverse)
12132 FoundPred = ICI->getInverseCmpPredicate();
12133 else
12134 FoundPred = ICI->getCmpPredicate();
12135
12136 const SCEV *FoundLHS = getSCEV(ICI->getOperand(0));
12137 const SCEV *FoundRHS = getSCEV(ICI->getOperand(1));
12138
12139 return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS, CtxI);
12140}
12141
12142bool ScalarEvolution::isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
12143 const SCEV *RHS, CmpPredicate FoundPred,
12144 const SCEV *FoundLHS, const SCEV *FoundRHS,
12145 const Instruction *CtxI) {
12146 // Balance the types.
12147 if (getTypeSizeInBits(LHS->getType()) <
12148 getTypeSizeInBits(FoundLHS->getType())) {
12149 // For unsigned and equality predicates, try to prove that both found
12150 // operands fit into narrow unsigned range. If so, try to prove facts in
12151 // narrow types.
12152 if (!CmpInst::isSigned(FoundPred) && !FoundLHS->getType()->isPointerTy() &&
12153 !FoundRHS->getType()->isPointerTy()) {
12154 auto *NarrowType = LHS->getType();
12155 auto *WideType = FoundLHS->getType();
12156 auto BitWidth = getTypeSizeInBits(NarrowType);
12157 const SCEV *MaxValue = getZeroExtendExpr(
12159 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundLHS,
12160 MaxValue) &&
12161 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundRHS,
12162 MaxValue)) {
12163 const SCEV *TruncFoundLHS = getTruncateExpr(FoundLHS, NarrowType);
12164 const SCEV *TruncFoundRHS = getTruncateExpr(FoundRHS, NarrowType);
12165 // We cannot preserve samesign after truncation.
12166 if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred.dropSameSign(),
12167 TruncFoundLHS, TruncFoundRHS, CtxI))
12168 return true;
12169 }
12170 }
12171
12172 if (LHS->getType()->isPointerTy() || RHS->getType()->isPointerTy())
12173 return false;
12174 if (CmpInst::isSigned(Pred)) {
12175 LHS = getSignExtendExpr(LHS, FoundLHS->getType());
12176 RHS = getSignExtendExpr(RHS, FoundLHS->getType());
12177 } else {
12178 LHS = getZeroExtendExpr(LHS, FoundLHS->getType());
12179 RHS = getZeroExtendExpr(RHS, FoundLHS->getType());
12180 }
12181 } else if (getTypeSizeInBits(LHS->getType()) >
12182 getTypeSizeInBits(FoundLHS->getType())) {
12183 if (FoundLHS->getType()->isPointerTy() || FoundRHS->getType()->isPointerTy())
12184 return false;
12185 if (CmpInst::isSigned(FoundPred)) {
12186 FoundLHS = getSignExtendExpr(FoundLHS, LHS->getType());
12187 FoundRHS = getSignExtendExpr(FoundRHS, LHS->getType());
12188 } else {
12189 FoundLHS = getZeroExtendExpr(FoundLHS, LHS->getType());
12190 FoundRHS = getZeroExtendExpr(FoundRHS, LHS->getType());
12191 }
12192 }
12193 return isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, FoundLHS,
12194 FoundRHS, CtxI);
12195}
12196
12197bool ScalarEvolution::isImpliedCondBalancedTypes(
12198 CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS, CmpPredicate FoundPred,
12199 SCEVUse FoundLHS, SCEVUse FoundRHS, const Instruction *CtxI) {
12201 getTypeSizeInBits(FoundLHS->getType()) &&
12202 "Types should be balanced!");
12203 // Canonicalize the query to match the way instcombine will have
12204 // canonicalized the comparison.
12205 if (SimplifyICmpOperands(Pred, LHS, RHS))
12206 if (LHS == RHS)
12207 return CmpInst::isTrueWhenEqual(Pred);
12208 if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS))
12209 if (FoundLHS == FoundRHS)
12210 return CmpInst::isFalseWhenEqual(FoundPred);
12211
12212 // Check to see if we can make the LHS or RHS match.
12213 if (LHS == FoundRHS || RHS == FoundLHS) {
12214 if (isa<SCEVConstant>(RHS)) {
12215 std::swap(FoundLHS, FoundRHS);
12216 FoundPred = ICmpInst::getSwappedCmpPredicate(FoundPred);
12217 } else {
12218 std::swap(LHS, RHS);
12220 }
12221 }
12222
12223 // Check whether the found predicate is the same as the desired predicate.
12224 if (auto P = CmpPredicate::getMatching(FoundPred, Pred))
12225 return isImpliedCondOperands(*P, LHS, RHS, FoundLHS, FoundRHS, CtxI);
12226
12227 // Check whether swapping the found predicate makes it the same as the
12228 // desired predicate.
12229 if (auto P = CmpPredicate::getMatching(
12230 ICmpInst::getSwappedCmpPredicate(FoundPred), Pred)) {
12231 // We can write the implication
12232 // 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS
12233 // using one of the following ways:
12234 // 1. LHS Pred RHS <- FoundRHS Pred FoundLHS
12235 // 2. RHS SwapPred LHS <- FoundLHS SwapPred FoundRHS
12236 // 3. LHS Pred RHS <- ~FoundLHS Pred ~FoundRHS
12237 // 4. ~LHS SwapPred ~RHS <- FoundLHS SwapPred FoundRHS
12238 // Forms 1. and 2. require swapping the operands of one condition. Don't
12239 // do this if it would break canonical constant/addrec ordering.
12241 return isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(*P), RHS,
12242 LHS, FoundLHS, FoundRHS, CtxI);
12243 if (!isa<SCEVConstant>(FoundRHS) && !isa<SCEVAddRecExpr>(FoundLHS))
12244 return isImpliedCondOperands(*P, LHS, RHS, FoundRHS, FoundLHS, CtxI);
12245
12246 // There's no clear preference between forms 3. and 4., try both. Avoid
12247 // forming getNotSCEV of pointer values as the resulting subtract is
12248 // not legal.
12249 if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() &&
12250 isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(*P),
12251 getNotSCEV(LHS), getNotSCEV(RHS), FoundLHS,
12252 FoundRHS, CtxI))
12253 return true;
12254
12255 if (!FoundLHS->getType()->isPointerTy() &&
12256 !FoundRHS->getType()->isPointerTy() &&
12257 isImpliedCondOperands(*P, LHS, RHS, getNotSCEV(FoundLHS),
12258 getNotSCEV(FoundRHS), CtxI))
12259 return true;
12260
12261 return false;
12262 }
12263
12264 auto IsSignFlippedPredicate = [](CmpInst::Predicate P1,
12266 assert(P1 != P2 && "Handled earlier!");
12267 return CmpInst::isRelational(P2) &&
12269 };
12270 if (IsSignFlippedPredicate(Pred, FoundPred)) {
12271 // Unsigned comparison is the same as signed comparison when both the
12272 // operands are non-negative or negative.
12273 if (haveSameSign(FoundLHS, FoundRHS))
12274 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI);
12275 // Create local copies that we can freely swap and canonicalize our
12276 // conditions to "le/lt".
12277 CmpPredicate CanonicalPred = Pred, CanonicalFoundPred = FoundPred;
12278 const SCEV *CanonicalLHS = LHS, *CanonicalRHS = RHS,
12279 *CanonicalFoundLHS = FoundLHS, *CanonicalFoundRHS = FoundRHS;
12280 if (ICmpInst::isGT(CanonicalPred) || ICmpInst::isGE(CanonicalPred)) {
12281 CanonicalPred = ICmpInst::getSwappedCmpPredicate(CanonicalPred);
12282 CanonicalFoundPred = ICmpInst::getSwappedCmpPredicate(CanonicalFoundPred);
12283 std::swap(CanonicalLHS, CanonicalRHS);
12284 std::swap(CanonicalFoundLHS, CanonicalFoundRHS);
12285 }
12286 assert((ICmpInst::isLT(CanonicalPred) || ICmpInst::isLE(CanonicalPred)) &&
12287 "Must be!");
12288 assert((ICmpInst::isLT(CanonicalFoundPred) ||
12289 ICmpInst::isLE(CanonicalFoundPred)) &&
12290 "Must be!");
12291 if (ICmpInst::isSigned(CanonicalPred) && isKnownNonNegative(CanonicalRHS))
12292 // Use implication:
12293 // x <u y && y >=s 0 --> x <s y.
12294 // If we can prove the left part, the right part is also proven.
12295 return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS,
12296 CanonicalRHS, CanonicalFoundLHS,
12297 CanonicalFoundRHS);
12298 if (ICmpInst::isUnsigned(CanonicalPred) && isKnownNegative(CanonicalRHS))
12299 // Use implication:
12300 // x <s y && y <s 0 --> x <u y.
12301 // If we can prove the left part, the right part is also proven.
12302 return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS,
12303 CanonicalRHS, CanonicalFoundLHS,
12304 CanonicalFoundRHS);
12305 }
12306
12307 // Check if we can make progress by sharpening ranges.
12308 if (FoundPred == ICmpInst::ICMP_NE &&
12309 (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) {
12310
12311 const SCEVConstant *C = nullptr;
12312 const SCEV *V = nullptr;
12313
12314 if (isa<SCEVConstant>(FoundLHS)) {
12315 C = cast<SCEVConstant>(FoundLHS);
12316 V = FoundRHS;
12317 } else {
12318 C = cast<SCEVConstant>(FoundRHS);
12319 V = FoundLHS;
12320 }
12321
12322 // The guarding predicate tells us that C != V. If the known range
12323 // of V is [C, t), we can sharpen the range to [C + 1, t). The
12324 // range we consider has to correspond to same signedness as the
12325 // predicate we're interested in folding.
12326
12327 APInt Min = ICmpInst::isSigned(Pred) ?
12329
12330 if (Min == C->getAPInt()) {
12331 // Given (V >= Min && V != Min) we conclude V >= (Min + 1).
12332 // This is true even if (Min + 1) wraps around -- in case of
12333 // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)).
12334
12335 APInt SharperMin = Min + 1;
12336
12337 switch (Pred) {
12338 case ICmpInst::ICMP_SGE:
12339 case ICmpInst::ICMP_UGE:
12340 // We know V `Pred` SharperMin. If this implies LHS `Pred`
12341 // RHS, we're done.
12342 if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(SharperMin),
12343 CtxI))
12344 return true;
12345 [[fallthrough]];
12346
12347 case ICmpInst::ICMP_SGT:
12348 case ICmpInst::ICMP_UGT:
12349 // We know from the range information that (V `Pred` Min ||
12350 // V == Min). We know from the guarding condition that !(V
12351 // == Min). This gives us
12352 //
12353 // V `Pred` Min || V == Min && !(V == Min)
12354 // => V `Pred` Min
12355 //
12356 // If V `Pred` Min implies LHS `Pred` RHS, we're done.
12357
12358 if (isImpliedCondOperands(Pred, LHS, RHS, V, getConstant(Min), CtxI))
12359 return true;
12360 break;
12361
12362 // `LHS < RHS` and `LHS <= RHS` are handled in the same way as `RHS > LHS` and `RHS >= LHS` respectively.
12363 case ICmpInst::ICMP_SLE:
12364 case ICmpInst::ICMP_ULE:
12365 if (isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(Pred), RHS,
12366 LHS, V, getConstant(SharperMin), CtxI))
12367 return true;
12368 [[fallthrough]];
12369
12370 case ICmpInst::ICMP_SLT:
12371 case ICmpInst::ICMP_ULT:
12372 if (isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(Pred), RHS,
12373 LHS, V, getConstant(Min), CtxI))
12374 return true;
12375 break;
12376
12377 default:
12378 // No change
12379 break;
12380 }
12381 }
12382 }
12383
12384 // Check whether the actual condition is beyond sufficient.
12385 if (FoundPred == ICmpInst::ICMP_EQ)
12386 if (ICmpInst::isTrueWhenEqual(Pred))
12387 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
12388 return true;
12389 if (Pred == ICmpInst::ICMP_NE)
12390 if (!ICmpInst::isTrueWhenEqual(FoundPred))
12391 if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
12392 return true;
12393
12394 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS))
12395 return true;
12396
12397 // Otherwise assume the worst.
12398 return false;
12399}
12400
12401bool ScalarEvolution::splitBinaryAdd(SCEVUse Expr, SCEVUse &L, SCEVUse &R,
12402 SCEV::NoWrapFlags &Flags) {
12403 if (!match(Expr, m_scev_Add(m_SCEV(L), m_SCEV(R))))
12404 return false;
12405
12406 Flags = cast<SCEVAddExpr>(Expr)->getNoWrapFlags();
12407 return true;
12408}
12409
12410std::optional<APInt>
12412 // We avoid subtracting expressions here because this function is usually
12413 // fairly deep in the call stack (i.e. is called many times).
12414
12415 unsigned BW = getTypeSizeInBits(More->getType());
12416 APInt Diff(BW, 0);
12417 APInt DiffMul(BW, 1);
12418 // Try various simplifications to reduce the difference to a constant. Limit
12419 // the number of allowed simplifications to keep compile-time low.
12420 for (unsigned I = 0; I < 8; ++I) {
12421 if (More == Less)
12422 return Diff;
12423
12424 // Reduce addrecs with identical steps to their start value.
12426 const auto *LAR = cast<SCEVAddRecExpr>(Less);
12427 const auto *MAR = cast<SCEVAddRecExpr>(More);
12428
12429 if (LAR->getLoop() != MAR->getLoop())
12430 return std::nullopt;
12431
12432 // We look at affine expressions only; not for correctness but to keep
12433 // getStepRecurrence cheap.
12434 if (!LAR->isAffine() || !MAR->isAffine())
12435 return std::nullopt;
12436
12437 if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
12438 return std::nullopt;
12439
12440 Less = LAR->getStart();
12441 More = MAR->getStart();
12442 continue;
12443 }
12444
12445 // Try to match a common constant multiply.
12446 auto MatchConstMul =
12447 [](const SCEV *S) -> std::optional<std::pair<const SCEV *, APInt>> {
12448 const APInt *C;
12449 const SCEV *Op;
12450 if (match(S, m_scev_Mul(m_scev_APInt(C), m_SCEV(Op))))
12451 return {{Op, *C}};
12452 return std::nullopt;
12453 };
12454 if (auto MatchedMore = MatchConstMul(More)) {
12455 if (auto MatchedLess = MatchConstMul(Less)) {
12456 if (MatchedMore->second == MatchedLess->second) {
12457 More = MatchedMore->first;
12458 Less = MatchedLess->first;
12459 DiffMul *= MatchedMore->second;
12460 continue;
12461 }
12462 }
12463 }
12464
12465 // Try to cancel out common factors in two add expressions.
12467 auto Add = [&](const SCEV *S, int Mul) {
12468 if (auto *C = dyn_cast<SCEVConstant>(S)) {
12469 if (Mul == 1) {
12470 Diff += C->getAPInt() * DiffMul;
12471 } else {
12472 assert(Mul == -1);
12473 Diff -= C->getAPInt() * DiffMul;
12474 }
12475 } else
12476 Multiplicity[S] += Mul;
12477 };
12478 auto Decompose = [&](const SCEV *S, int Mul) {
12479 if (isa<SCEVAddExpr>(S)) {
12480 for (const SCEV *Op : S->operands())
12481 Add(Op, Mul);
12482 } else
12483 Add(S, Mul);
12484 };
12485 Decompose(More, 1);
12486 Decompose(Less, -1);
12487
12488 // Check whether all the non-constants cancel out, or reduce to new
12489 // More/Less values.
12490 const SCEV *NewMore = nullptr, *NewLess = nullptr;
12491 for (const auto &[S, Mul] : Multiplicity) {
12492 if (Mul == 0)
12493 continue;
12494 if (Mul == 1) {
12495 if (NewMore)
12496 return std::nullopt;
12497 NewMore = S;
12498 } else if (Mul == -1) {
12499 if (NewLess)
12500 return std::nullopt;
12501 NewLess = S;
12502 } else
12503 return std::nullopt;
12504 }
12505
12506 // Values stayed the same, no point in trying further.
12507 if (NewMore == More || NewLess == Less)
12508 return std::nullopt;
12509
12510 More = NewMore;
12511 Less = NewLess;
12512
12513 // Reduced to constant.
12514 if (!More && !Less)
12515 return Diff;
12516
12517 // Left with variable on only one side, bail out.
12518 if (!More || !Less)
12519 return std::nullopt;
12520 }
12521
12522 // Did not reduce to constant.
12523 return std::nullopt;
12524}
12525
12526bool ScalarEvolution::isImpliedCondOperandsViaAddRecStart(
12527 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const SCEV *FoundLHS,
12528 const SCEV *FoundRHS, const Instruction *CtxI) {
12529 // Try to recognize the following pattern:
12530 //
12531 // FoundRHS = ...
12532 // ...
12533 // loop:
12534 // FoundLHS = {Start,+,W}
12535 // context_bb: // Basic block from the same loop
12536 // known(Pred, FoundLHS, FoundRHS)
12537 //
12538 // If some predicate is known in the context of a loop, it is also known on
12539 // each iteration of this loop, including the first iteration. Therefore, in
12540 // this case, `FoundLHS Pred FoundRHS` implies `Start Pred FoundRHS`. Try to
12541 // prove the original pred using this fact.
12542 if (!CtxI)
12543 return false;
12544 const BasicBlock *ContextBB = CtxI->getParent();
12545 // Make sure AR varies in the context block.
12546 if (auto *AR = dyn_cast<SCEVAddRecExpr>(FoundLHS)) {
12547 const Loop *L = AR->getLoop();
12548 const auto *Latch = L->getLoopLatch();
12549 // Make sure that context belongs to the loop and executes on 1st iteration
12550 // (if it ever executes at all).
12551 if (!L->contains(ContextBB) || !Latch || !DT.dominates(ContextBB, Latch))
12552 return false;
12553 if (!isAvailableAtLoopEntry(FoundRHS, AR->getLoop()))
12554 return false;
12555 return isImpliedCondOperands(Pred, LHS, RHS, AR->getStart(), FoundRHS);
12556 }
12557
12558 if (auto *AR = dyn_cast<SCEVAddRecExpr>(FoundRHS)) {
12559 const Loop *L = AR->getLoop();
12560 const auto *Latch = L->getLoopLatch();
12561 // Make sure that context belongs to the loop and executes on 1st iteration
12562 // (if it ever executes at all).
12563 if (!L->contains(ContextBB) || !Latch || !DT.dominates(ContextBB, Latch))
12564 return false;
12565 if (!isAvailableAtLoopEntry(FoundLHS, AR->getLoop()))
12566 return false;
12567 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, AR->getStart());
12568 }
12569
12570 return false;
12571}
12572
12573bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(CmpPredicate Pred,
12574 const SCEV *LHS,
12575 const SCEV *RHS,
12576 const SCEV *FoundLHS,
12577 const SCEV *FoundRHS) {
12578 if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT)
12579 return false;
12580
12581 const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS);
12582 if (!AddRecLHS)
12583 return false;
12584
12585 const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS);
12586 if (!AddRecFoundLHS)
12587 return false;
12588
12589 // We'd like to let SCEV reason about control dependencies, so we constrain
12590 // both the inequalities to be about add recurrences on the same loop. This
12591 // way we can use isLoopEntryGuardedByCond later.
12592
12593 const Loop *L = AddRecFoundLHS->getLoop();
12594 if (L != AddRecLHS->getLoop())
12595 return false;
12596
12597 // FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1)
12598 //
12599 // FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
12600 // ... (2)
12601 //
12602 // Informal proof for (2), assuming (1) [*]:
12603 //
12604 // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
12605 //
12606 // Then
12607 //
12608 // FoundLHS s< FoundRHS s< INT_MIN - C
12609 // <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ]
12610 // <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
12611 // <=> (FoundLHS + INT_MIN + C + INT_MIN) s<
12612 // (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
12613 // <=> FoundLHS + C s< FoundRHS + C
12614 //
12615 // [*]: (1) can be proved by ruling out overflow.
12616 //
12617 // [**]: This can be proved by analyzing all the four possibilities:
12618 // (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
12619 // (A s>= 0, B s>= 0).
12620 //
12621 // Note:
12622 // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
12623 // will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS
12624 // = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS
12625 // s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is
12626 // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
12627 // C)".
12628
12629 std::optional<APInt> LDiff = computeConstantDifference(LHS, FoundLHS);
12630 if (!LDiff)
12631 return false;
12632 std::optional<APInt> RDiff = computeConstantDifference(RHS, FoundRHS);
12633 if (!RDiff || *LDiff != *RDiff)
12634 return false;
12635
12636 if (LDiff->isMinValue())
12637 return true;
12638
12639 APInt FoundRHSLimit;
12640
12641 if (Pred == CmpInst::ICMP_ULT) {
12642 FoundRHSLimit = -(*RDiff);
12643 } else {
12644 assert(Pred == CmpInst::ICMP_SLT && "Checked above!");
12645 FoundRHSLimit = APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff;
12646 }
12647
12648 // Try to prove (1) or (2), as needed.
12649 return isAvailableAtLoopEntry(FoundRHS, L) &&
12650 isLoopEntryGuardedByCond(L, Pred, FoundRHS,
12651 getConstant(FoundRHSLimit));
12652}
12653
12654bool ScalarEvolution::isImpliedViaMerge(CmpPredicate Pred, const SCEV *LHS,
12655 const SCEV *RHS, const SCEV *FoundLHS,
12656 const SCEV *FoundRHS, unsigned Depth) {
12657 const PHINode *LPhi = nullptr, *RPhi = nullptr;
12658
12659 llvm::scope_exit ClearOnExit([&]() {
12660 if (LPhi) {
12661 bool Erased = PendingMerges.erase(LPhi);
12662 assert(Erased && "Failed to erase LPhi!");
12663 (void)Erased;
12664 }
12665 if (RPhi) {
12666 bool Erased = PendingMerges.erase(RPhi);
12667 assert(Erased && "Failed to erase RPhi!");
12668 (void)Erased;
12669 }
12670 });
12671
12672 // Find respective Phis and check that they are not being pending.
12673 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS))
12674 if (auto *Phi = dyn_cast<PHINode>(LU->getValue())) {
12675 if (!PendingMerges.insert(Phi).second)
12676 return false;
12677 LPhi = Phi;
12678 }
12679 if (const SCEVUnknown *RU = dyn_cast<SCEVUnknown>(RHS))
12680 if (auto *Phi = dyn_cast<PHINode>(RU->getValue())) {
12681 // If we detect a loop of Phi nodes being processed by this method, for
12682 // example:
12683 //
12684 // %a = phi i32 [ %some1, %preheader ], [ %b, %latch ]
12685 // %b = phi i32 [ %some2, %preheader ], [ %a, %latch ]
12686 //
12687 // we don't want to deal with a case that complex, so return conservative
12688 // answer false.
12689 if (!PendingMerges.insert(Phi).second)
12690 return false;
12691 RPhi = Phi;
12692 }
12693
12694 // If none of LHS, RHS is a Phi, nothing to do here.
12695 if (!LPhi && !RPhi)
12696 return false;
12697
12698 // If there is a SCEVUnknown Phi we are interested in, make it left.
12699 if (!LPhi) {
12700 std::swap(LHS, RHS);
12701 std::swap(FoundLHS, FoundRHS);
12702 std::swap(LPhi, RPhi);
12704 }
12705
12706 assert(LPhi && "LPhi should definitely be a SCEVUnknown Phi!");
12707 const BasicBlock *LBB = LPhi->getParent();
12708 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
12709
12710 auto ProvedEasily = [&](const SCEV *S1, const SCEV *S2) {
12711 return isKnownViaNonRecursiveReasoning(Pred, S1, S2) ||
12712 isImpliedCondOperandsViaRanges(Pred, S1, S2, Pred, FoundLHS, FoundRHS) ||
12713 isImpliedViaOperations(Pred, S1, S2, FoundLHS, FoundRHS, Depth);
12714 };
12715
12716 if (RPhi && RPhi->getParent() == LBB) {
12717 // Case one: RHS is also a SCEVUnknown Phi from the same basic block.
12718 // If we compare two Phis from the same block, and for each entry block
12719 // the predicate is true for incoming values from this block, then the
12720 // predicate is also true for the Phis.
12721 for (const BasicBlock *IncBB : predecessors(LBB)) {
12722 const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
12723 const SCEV *R = getSCEV(RPhi->getIncomingValueForBlock(IncBB));
12724 if (!ProvedEasily(L, R))
12725 return false;
12726 }
12727 } else if (RAR && RAR->getLoop()->getHeader() == LBB) {
12728 // Case two: RHS is also a Phi from the same basic block, and it is an
12729 // AddRec. It means that there is a loop which has both AddRec and Unknown
12730 // PHIs, for it we can compare incoming values of AddRec from above the loop
12731 // and latch with their respective incoming values of LPhi.
12732 // TODO: Generalize to handle loops with many inputs in a header.
12733 if (LPhi->getNumIncomingValues() != 2) return false;
12734
12735 auto *RLoop = RAR->getLoop();
12736 auto *Predecessor = RLoop->getLoopPredecessor();
12737 assert(Predecessor && "Loop with AddRec with no predecessor?");
12738 const SCEV *L1 = getSCEV(LPhi->getIncomingValueForBlock(Predecessor));
12739 if (!ProvedEasily(L1, RAR->getStart()))
12740 return false;
12741 auto *Latch = RLoop->getLoopLatch();
12742 assert(Latch && "Loop with AddRec with no latch?");
12743 const SCEV *L2 = getSCEV(LPhi->getIncomingValueForBlock(Latch));
12744 if (!ProvedEasily(L2, RAR->getPostIncExpr(*this)))
12745 return false;
12746 } else {
12747 // In all other cases go over inputs of LHS and compare each of them to RHS,
12748 // the predicate is true for (LHS, RHS) if it is true for all such pairs.
12749 // At this point RHS is either a non-Phi, or it is a Phi from some block
12750 // different from LBB.
12751 for (const BasicBlock *IncBB : predecessors(LBB)) {
12752 // Check that RHS is available in this block.
12753 if (!dominates(RHS, IncBB))
12754 return false;
12755 const SCEV *L = getSCEV(LPhi->getIncomingValueForBlock(IncBB));
12756 // Make sure L does not refer to a value from a potentially previous
12757 // iteration of a loop.
12758 if (!properlyDominates(L, LBB))
12759 return false;
12760 // Addrecs are considered to properly dominate their loop, so are missed
12761 // by the previous check. Discard any values that have computable
12762 // evolution in this loop.
12763 if (auto *Loop = LI.getLoopFor(LBB))
12765 return false;
12766 if (!ProvedEasily(L, RHS))
12767 return false;
12768 }
12769 }
12770 return true;
12771}
12772
12773bool ScalarEvolution::isImpliedCondOperandsViaShift(CmpPredicate Pred,
12774 const SCEV *LHS,
12775 const SCEV *RHS,
12776 const SCEV *FoundLHS,
12777 const SCEV *FoundRHS) {
12778 // We want to imply LHS < RHS from LHS < (RHS >> shiftvalue). First, make
12779 // sure that we are dealing with same LHS.
12780 if (RHS == FoundRHS) {
12781 std::swap(LHS, RHS);
12782 std::swap(FoundLHS, FoundRHS);
12784 }
12785 if (LHS != FoundLHS)
12786 return false;
12787
12788 auto *SUFoundRHS = dyn_cast<SCEVUnknown>(FoundRHS);
12789 if (!SUFoundRHS)
12790 return false;
12791
12792 Value *Shiftee, *ShiftValue;
12793
12794 using namespace PatternMatch;
12795 if (match(SUFoundRHS->getValue(),
12796 m_LShr(m_Value(Shiftee), m_Value(ShiftValue)))) {
12797 auto *ShifteeS = getSCEV(Shiftee);
12798 // Prove one of the following:
12799 // LHS <u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <u RHS
12800 // LHS <=u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <=u RHS
12801 // LHS <s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0
12802 // ---> LHS <s RHS
12803 // LHS <=s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0
12804 // ---> LHS <=s RHS
12805 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
12806 return isKnownPredicate(ICmpInst::ICMP_ULE, ShifteeS, RHS);
12807 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
12808 if (isKnownNonNegative(ShifteeS))
12809 return isKnownPredicate(ICmpInst::ICMP_SLE, ShifteeS, RHS);
12810 }
12811
12812 return false;
12813}
12814
12815bool ScalarEvolution::isImpliedCondOperandsViaMatchingDiff(
12816 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const SCEV *FoundLHS,
12817 const SCEV *FoundRHS) {
12818 // Only valid for equality predicates: (A == B) implies (C == D) when
12819 // the SCEV difference A - B equals C - D (they check the same
12820 // underlying relationship at every iteration).
12821 if (!ICmpInst::isEquality(Pred))
12822 return false;
12823
12824 // Restrict to cases involving loop recurrences - that's where this
12825 // pattern arises (correlated IV comparisons). This avoids calling
12826 // getMinusSCEV on arbitrary non-loop expressions.
12828 (!isa<SCEVAddRecExpr>(FoundLHS) && !isa<SCEVAddRecExpr>(FoundRHS)))
12829 return false;
12830
12831 // AddRecs from different loops can never produce matching differences.
12832 const SCEVAddRecExpr *QueryAddRec = dyn_cast<SCEVAddRecExpr>(LHS);
12833 if (!QueryAddRec)
12834 QueryAddRec = cast<SCEVAddRecExpr>(RHS);
12835 const SCEVAddRecExpr *FoundAddRec = dyn_cast<SCEVAddRecExpr>(FoundLHS);
12836 if (!FoundAddRec)
12837 FoundAddRec = cast<SCEVAddRecExpr>(FoundRHS);
12838 if (QueryAddRec->getLoop() != FoundAddRec->getLoop())
12839 return false;
12840
12841 // If the strides differ, the differences can never match.
12842 if (QueryAddRec->getStepRecurrence(*this) !=
12843 FoundAddRec->getStepRecurrence(*this))
12844 return false;
12845
12846 // Compute differences. For pointer-typed operands sharing the same base,
12847 // getMinusSCEV strips the common base and returns an integer SCEV.
12848 // For example, {base,+,8} - (base+8*n) = {-8n,+,8}
12849 const SCEV *FoundDiff = getMinusSCEV(FoundLHS, FoundRHS);
12850 if (isa<SCEVCouldNotCompute>(FoundDiff))
12851 return false;
12852
12853 const SCEV *Diff = getMinusSCEV(LHS, RHS);
12854 if (isa<SCEVCouldNotCompute>(Diff))
12855 return false;
12856
12857 return Diff == FoundDiff;
12858}
12859
12860bool ScalarEvolution::isImpliedCondOperands(CmpPredicate Pred, const SCEV *LHS,
12861 const SCEV *RHS,
12862 const SCEV *FoundLHS,
12863 const SCEV *FoundRHS,
12864 const Instruction *CtxI) {
12865 return isImpliedCondOperandsViaRanges(Pred, LHS, RHS, Pred, FoundLHS,
12866 FoundRHS) ||
12867 isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS,
12868 FoundRHS) ||
12869 isImpliedCondOperandsViaShift(Pred, LHS, RHS, FoundLHS, FoundRHS) ||
12870 isImpliedCondOperandsViaAddRecStart(Pred, LHS, RHS, FoundLHS, FoundRHS,
12871 CtxI) ||
12872 isImpliedCondOperandsViaMatchingDiff(Pred, LHS, RHS, FoundLHS,
12873 FoundRHS) ||
12874 isImpliedCondOperandsHelper(Pred, LHS, RHS, FoundLHS, FoundRHS);
12875}
12876
12877/// Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values?
12878template <typename MinMaxExprType>
12879static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr,
12880 const SCEV *Candidate) {
12881 const MinMaxExprType *MinMaxExpr = dyn_cast<MinMaxExprType>(MaybeMinMaxExpr);
12882 if (!MinMaxExpr)
12883 return false;
12884
12885 return is_contained(MinMaxExpr->operands(), Candidate);
12886}
12887
12889 CmpPredicate Pred, const SCEV *LHS,
12890 const SCEV *RHS) {
12891 // If both sides are affine addrecs for the same loop, with equal
12892 // steps, and we know the recurrences don't wrap, then we only
12893 // need to check the predicate on the starting values.
12894
12895 if (!ICmpInst::isRelational(Pred))
12896 return false;
12897
12898 const SCEV *LStart, *RStart, *Step;
12899 const Loop *L;
12900 if (!match(LHS,
12901 m_scev_AffineAddRec(m_SCEV(LStart), m_SCEV(Step), m_Loop(L))) ||
12903 m_SpecificLoop(L))))
12904 return false;
12909 if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW))
12910 return false;
12911
12912 return SE.isKnownPredicate(Pred, LStart, RStart);
12913}
12914
12915/// Is LHS `Pred` RHS true because one of them is an AddRec that is known not to
12916/// go below its own start value?
12918 CmpPredicate Pred,
12919 const SCEV *LHS,
12920 const SCEV *RHS) {
12921 // Normalize to (AddRec Pred Start).
12924 std::swap(LHS, RHS);
12925 }
12926
12927 // The recurrence is equal to Start in the first iteration, so only the
12928 // non-strict predicate holds.
12929 if (Pred != ICmpInst::ICMP_UGE && Pred != ICmpInst::ICMP_SGE)
12930 return false;
12931
12932 const auto *AR = dyn_cast<SCEVAddRecExpr>(LHS);
12933 if (!AR || AR->getStart() != RHS)
12934 return false;
12935
12936 return SE.getMonotonicPredicateType(AR, Pred) ==
12938}
12939
12940/// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max
12941/// expression?
12943 const SCEV *LHS, const SCEV *RHS) {
12944 switch (Pred) {
12945 default:
12946 return false;
12947
12948 case ICmpInst::ICMP_SGE:
12949 std::swap(LHS, RHS);
12950 [[fallthrough]];
12951 case ICmpInst::ICMP_SLE:
12952 return
12953 // min(A, ...) <= A
12955 // A <= max(A, ...)
12957
12958 case ICmpInst::ICMP_UGE:
12959 std::swap(LHS, RHS);
12960 [[fallthrough]];
12961 case ICmpInst::ICMP_ULE:
12962 return
12963 // min(A, ...) <= A
12964 // FIXME: what about umin_seq?
12966 // A <= max(A, ...)
12968
12969 case ICmpInst::ICMP_UGT:
12970 std::swap(LHS, RHS);
12971 [[fallthrough]];
12972 case ICmpInst::ICMP_ULT:
12973 // umin(Ops) u<= each Op, so proving Op u< RHS for any Op proves
12974 // umin(Ops) u< RHS.
12975 //
12976 // Use computeConstantDifference instead of the more powerful
12977 // isKnownPredicate to keep this check cheap: isKnownPredicateViaMinOrMax
12978 // is called from isKnownViaNonRecursiveReasoning, so recursing into
12979 // the full predicate prover would be expensive.
12980 if (const auto *Min = dyn_cast<SCEVUMinExpr>(LHS)) {
12981 for (SCEVUse Op : Min->operands()) {
12982 std::optional<APInt> Diff = SE.computeConstantDifference(RHS, Op);
12983 // When Op and RHS share a common base differing by a
12984 // constant offset D (RHS - Op = D), Op u< RHS holds iff D != 0 and
12985 // RHS >= D (unsigned), i.e. the subtraction doesn't underflow.
12986 if (Diff && !Diff->isZero() && SE.getUnsignedRangeMin(RHS).uge(*Diff))
12987 return true;
12988 }
12989 }
12990 return false;
12991 }
12992
12993 llvm_unreachable("covered switch fell through?!");
12994}
12995
12996bool ScalarEvolution::isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS,
12997 const SCEV *RHS,
12998 const SCEV *FoundLHS,
12999 const SCEV *FoundRHS,
13000 unsigned Depth) {
13003 "LHS and RHS have different sizes?");
13004 assert(getTypeSizeInBits(FoundLHS->getType()) ==
13005 getTypeSizeInBits(FoundRHS->getType()) &&
13006 "FoundLHS and FoundRHS have different sizes?");
13007 // We want to avoid hurting the compile time with analysis of too big trees.
13009 return false;
13010
13011 // We only want to work with GT comparison so far.
13012 if (ICmpInst::isLT(Pred)) {
13014 std::swap(LHS, RHS);
13015 std::swap(FoundLHS, FoundRHS);
13016 }
13017
13019
13020 // For unsigned, try to reduce it to corresponding signed comparison.
13021 if (P == ICmpInst::ICMP_UGT)
13022 // We can replace unsigned predicate with its signed counterpart if all
13023 // involved values are non-negative.
13024 // TODO: We could have better support for unsigned.
13025 if (isKnownNonNegative(FoundLHS) && isKnownNonNegative(FoundRHS)) {
13026 // Knowing that both FoundLHS and FoundRHS are non-negative, and knowing
13027 // FoundLHS >u FoundRHS, we also know that FoundLHS >s FoundRHS. Let us
13028 // use this fact to prove that LHS and RHS are non-negative.
13029 const SCEV *MinusOne = getMinusOne(LHS->getType());
13030 if (isImpliedCondOperands(ICmpInst::ICMP_SGT, LHS, MinusOne, FoundLHS,
13031 FoundRHS) &&
13032 isImpliedCondOperands(ICmpInst::ICMP_SGT, RHS, MinusOne, FoundLHS,
13033 FoundRHS))
13035 }
13036
13037 if (P != ICmpInst::ICMP_SGT)
13038 return false;
13039
13040 auto GetOpFromSExt = [&](const SCEV *S) -> const SCEV * {
13041 if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(S))
13042 return Ext->getOperand();
13043 // TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off
13044 // the constant in some cases.
13045 return S;
13046 };
13047
13048 // Acquire values from extensions.
13049 auto *OrigLHS = LHS;
13050 auto *OrigFoundLHS = FoundLHS;
13051 LHS = GetOpFromSExt(LHS);
13052 FoundLHS = GetOpFromSExt(FoundLHS);
13053
13054 // Is the SGT predicate can be proved trivially or using the found context.
13055 auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) {
13056 return isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGT, S1, S2) ||
13057 isImpliedViaOperations(ICmpInst::ICMP_SGT, S1, S2, OrigFoundLHS,
13058 FoundRHS, Depth + 1);
13059 };
13060
13061 if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(LHS)) {
13062 // We want to avoid creation of any new non-constant SCEV. Since we are
13063 // going to compare the operands to RHS, we should be certain that we don't
13064 // need any size extensions for this. So let's decline all cases when the
13065 // sizes of types of LHS and RHS do not match.
13066 // TODO: Maybe try to get RHS from sext to catch more cases?
13068 return false;
13069
13070 // Should not overflow.
13071 if (!LHSAddExpr->hasNoSignedWrap())
13072 return false;
13073
13074 SCEVUse LL = LHSAddExpr->getOperand(0);
13075 SCEVUse LR = LHSAddExpr->getOperand(1);
13076 auto *MinusOne = getMinusOne(RHS->getType());
13077
13078 // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context.
13079 auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) {
13080 return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS);
13081 };
13082 // Try to prove the following rule:
13083 // (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS).
13084 // (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS).
13085 if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL))
13086 return true;
13087 } else if (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(LHS)) {
13088 Value *LL, *LR;
13089 // FIXME: Once we have SDiv implemented, we can get rid of this matching.
13090
13091 using namespace llvm::PatternMatch;
13092
13093 if (match(LHSUnknownExpr->getValue(), m_SDiv(m_Value(LL), m_Value(LR)))) {
13094 // Rules for division.
13095 // We are going to perform some comparisons with Denominator and its
13096 // derivative expressions. In general case, creating a SCEV for it may
13097 // lead to a complex analysis of the entire graph, and in particular it
13098 // can request trip count recalculation for the same loop. This would
13099 // cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid
13100 // this, we only want to create SCEVs that are constants in this section.
13101 // So we bail if Denominator is not a constant.
13102 if (!isa<ConstantInt>(LR))
13103 return false;
13104
13105 auto *Denominator = cast<SCEVConstant>(getSCEV(LR));
13106
13107 // We want to make sure that LHS = FoundLHS / Denominator. If it is so,
13108 // then a SCEV for the numerator already exists and matches with FoundLHS.
13109 auto *Numerator = getExistingSCEV(LL);
13110 if (!Numerator || Numerator->getType() != FoundLHS->getType())
13111 return false;
13112
13113 // Make sure that the numerator matches with FoundLHS and the denominator
13114 // is positive.
13115 if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator))
13116 return false;
13117
13118 auto *DTy = Denominator->getType();
13119 auto *FRHSTy = FoundRHS->getType();
13120 if (DTy->isPointerTy() != FRHSTy->isPointerTy())
13121 // One of types is a pointer and another one is not. We cannot extend
13122 // them properly to a wider type, so let us just reject this case.
13123 // TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help
13124 // to avoid this check.
13125 return false;
13126
13127 // Given that:
13128 // FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0.
13129 auto *WTy = getWiderType(DTy, FRHSTy);
13130 auto *DenominatorExt = getNoopOrSignExtend(Denominator, WTy);
13131 auto *FoundRHSExt = getNoopOrSignExtend(FoundRHS, WTy);
13132
13133 // Try to prove the following rule:
13134 // (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS).
13135 // For example, given that FoundLHS > 2. It means that FoundLHS is at
13136 // least 3. If we divide it by Denominator < 4, we will have at least 1.
13137 auto *DenomMinusTwo = getMinusSCEV(DenominatorExt, getConstant(WTy, 2));
13138 if (isKnownNonPositive(RHS) &&
13139 IsSGTViaContext(FoundRHSExt, DenomMinusTwo))
13140 return true;
13141
13142 // Try to prove the following rule:
13143 // (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS).
13144 // For example, given that FoundLHS > -3. Then FoundLHS is at least -2.
13145 // If we divide it by Denominator > 2, then:
13146 // 1. If FoundLHS is negative, then the result is 0.
13147 // 2. If FoundLHS is non-negative, then the result is non-negative.
13148 // Anyways, the result is non-negative.
13149 auto *MinusOne = getMinusOne(WTy);
13150 auto *NegDenomMinusOne = getMinusSCEV(MinusOne, DenominatorExt);
13151 if (isKnownNegative(RHS) &&
13152 IsSGTViaContext(FoundRHSExt, NegDenomMinusOne))
13153 return true;
13154 }
13155 }
13156
13157 // If our expression contained SCEVUnknown Phis, and we split it down and now
13158 // need to prove something for them, try to prove the predicate for every
13159 // possible incoming values of those Phis.
13160 if (isImpliedViaMerge(Pred, OrigLHS, RHS, OrigFoundLHS, FoundRHS, Depth + 1))
13161 return true;
13162
13163 return false;
13164}
13165
13167 const SCEV *RHS) {
13168 // zext x u<= sext x, sext x s<= zext x
13169 const SCEV *Op;
13170 switch (Pred) {
13171 case ICmpInst::ICMP_SGE:
13172 std::swap(LHS, RHS);
13173 [[fallthrough]];
13174 case ICmpInst::ICMP_SLE: {
13175 // If operand >=s 0 then ZExt == SExt. If operand <s 0 then SExt <s ZExt.
13176 return match(LHS, m_scev_SExt(m_SCEV(Op))) &&
13178 }
13179 case ICmpInst::ICMP_UGE:
13180 std::swap(LHS, RHS);
13181 [[fallthrough]];
13182 case ICmpInst::ICMP_ULE: {
13183 // If operand >=u 0 then ZExt == SExt. If operand <u 0 then ZExt <u SExt.
13184 return match(LHS, m_scev_ZExt(m_SCEV(Op))) &&
13186 }
13187 default:
13188 return false;
13189 };
13190 llvm_unreachable("unhandled case");
13191}
13192
13193bool ScalarEvolution::isKnownViaNonRecursiveReasoning(CmpPredicate Pred,
13194 SCEVUse LHS,
13195 SCEVUse RHS) {
13196 return isKnownPredicateExtendIdiom(Pred, LHS, RHS) ||
13197 isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
13198 IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) ||
13199 IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) ||
13201 isKnownPredicateViaNoOverflow(Pred, LHS, RHS);
13202}
13203
13204bool ScalarEvolution::isImpliedCondOperandsHelper(CmpPredicate Pred,
13205 const SCEV *LHS,
13206 const SCEV *RHS,
13207 const SCEV *FoundLHS,
13208 const SCEV *FoundRHS) {
13209 switch (Pred) {
13210 default:
13211 llvm_unreachable("Unexpected CmpPredicate value!");
13212 case ICmpInst::ICMP_EQ:
13213 case ICmpInst::ICMP_NE:
13214 if (HasSameValue(LHS, FoundLHS) && HasSameValue(RHS, FoundRHS))
13215 return true;
13216 break;
13217 case ICmpInst::ICMP_SLT:
13218 case ICmpInst::ICMP_SLE:
13219 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
13220 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS))
13221 return true;
13222 break;
13223 case ICmpInst::ICMP_SGT:
13224 case ICmpInst::ICMP_SGE:
13225 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
13226 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS))
13227 return true;
13228 break;
13229 case ICmpInst::ICMP_ULT:
13230 case ICmpInst::ICMP_ULE:
13231 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
13232 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS))
13233 return true;
13234 break;
13235 case ICmpInst::ICMP_UGT:
13236 case ICmpInst::ICMP_UGE:
13237 if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
13238 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS))
13239 return true;
13240 break;
13241 }
13242
13243 // Maybe it can be proved via operations?
13244 if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS))
13245 return true;
13246
13247 return false;
13248}
13249
13250bool ScalarEvolution::isImpliedCondOperandsViaRanges(
13251 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, CmpPredicate FoundPred,
13252 const SCEV *FoundLHS, const SCEV *FoundRHS) {
13253 if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS))
13254 // The restriction on `FoundRHS` be lifted easily -- it exists only to
13255 // reduce the compile time impact of this optimization.
13256 return false;
13257
13258 std::optional<APInt> Addend = computeConstantDifference(LHS, FoundLHS);
13259 if (!Addend)
13260 return false;
13261
13262 const APInt &ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt();
13263
13264 // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the
13265 // antecedent "`FoundLHS` `FoundPred` `FoundRHS`".
13266 ConstantRange FoundLHSRange =
13267 ConstantRange::makeExactICmpRegion(FoundPred, ConstFoundRHS);
13268
13269 // Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`:
13270 ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend));
13271
13272 // We can also compute the range of values for `LHS` that satisfy the
13273 // consequent, "`LHS` `Pred` `RHS`":
13274 const APInt &ConstRHS = cast<SCEVConstant>(RHS)->getAPInt();
13275 // The antecedent implies the consequent if every value of `LHS` that
13276 // satisfies the antecedent also satisfies the consequent.
13277 return LHSRange.icmp(Pred, ConstRHS);
13278}
13279
13280bool ScalarEvolution::canIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
13281 bool IsSigned) {
13282 assert(isKnownPositive(Stride) && "Positive stride expected!");
13283
13284 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
13285 const SCEV *One = getOne(Stride->getType());
13286
13287 if (IsSigned) {
13288 APInt MaxRHS = getSignedRangeMax(RHS);
13289 APInt MaxValue = APInt::getSignedMaxValue(BitWidth);
13290 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
13291
13292 // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow!
13293 return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS);
13294 }
13295
13296 APInt MaxRHS = getUnsignedRangeMax(RHS);
13297 APInt MaxValue = APInt::getMaxValue(BitWidth);
13298 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
13299
13300 // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow!
13301 return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS);
13302}
13303
13304bool ScalarEvolution::canIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
13305 bool IsSigned) {
13306
13307 unsigned BitWidth = getTypeSizeInBits(RHS->getType());
13308 const SCEV *One = getOne(Stride->getType());
13309
13310 if (IsSigned) {
13311 APInt MinRHS = getSignedRangeMin(RHS);
13312 APInt MinValue = APInt::getSignedMinValue(BitWidth);
13313 APInt MaxStrideMinusOne = getSignedRangeMax(getMinusSCEV(Stride, One));
13314
13315 // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow!
13316 return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS);
13317 }
13318
13319 APInt MinRHS = getUnsignedRangeMin(RHS);
13320 APInt MinValue = APInt::getMinValue(BitWidth);
13321 APInt MaxStrideMinusOne = getUnsignedRangeMax(getMinusSCEV(Stride, One));
13322
13323 // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow!
13324 return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS);
13325}
13326
13328 // umin(N, 1) + floor((N - umin(N, 1)) / D)
13329 // This is equivalent to "1 + floor((N - 1) / D)" for N != 0. The umin
13330 // expression fixes the case of N=0.
13331 const SCEV *MinNOne = getUMinExpr(N, getOne(N->getType()));
13332 const SCEV *NMinusOne = getMinusSCEV(N, MinNOne);
13333 return getAddExpr(MinNOne, getUDivExpr(NMinusOne, D));
13334}
13335
13336const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
13337 const SCEV *Stride,
13338 const SCEV *End,
13339 unsigned BitWidth,
13340 bool IsSigned) {
13341 // The logic in this function assumes we can represent a positive stride.
13342 // If we can't, the backedge-taken count must be zero.
13343 if (IsSigned && BitWidth == 1)
13344 return getZero(Stride->getType());
13345
13346 // This code below only been closely audited for negative strides in the
13347 // unsigned comparison case, it may be correct for signed comparison, but
13348 // that needs to be established.
13349 if (IsSigned && isKnownNegative(Stride))
13350 return getCouldNotCompute();
13351
13352 // Calculate the maximum backedge count based on the range of values
13353 // permitted by Start, End, and Stride.
13354 APInt MinStart =
13355 IsSigned ? getSignedRangeMin(Start) : getUnsignedRangeMin(Start);
13356
13357 APInt MinStride =
13358 IsSigned ? getSignedRangeMin(Stride) : getUnsignedRangeMin(Stride);
13359
13360 // We assume either the stride is positive, or the backedge-taken count
13361 // is zero. So force StrideForMaxBECount to be at least one.
13362 APInt One(BitWidth, 1);
13363 APInt StrideForMaxBECount = IsSigned ? APIntOps::smax(One, MinStride)
13364 : APIntOps::umax(One, MinStride);
13365
13366 APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(BitWidth)
13367 : APInt::getMaxValue(BitWidth);
13368 APInt Limit = MaxValue - (StrideForMaxBECount - 1);
13369
13370 // Although End can be a MAX expression we estimate MaxEnd considering only
13371 // the case End = RHS of the loop termination condition. This is safe because
13372 // in the other case (End - Start) is zero, leading to a zero maximum backedge
13373 // taken count.
13374 APInt MaxEnd = IsSigned ? APIntOps::smin(getSignedRangeMax(End), Limit)
13375 : APIntOps::umin(getUnsignedRangeMax(End), Limit);
13376
13377 // MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride)
13378 MaxEnd = IsSigned ? APIntOps::smax(MaxEnd, MinStart)
13379 : APIntOps::umax(MaxEnd, MinStart);
13380
13381 return getUDivCeilSCEV(getConstant(MaxEnd - MinStart) /* Delta */,
13382 getConstant(StrideForMaxBECount) /* Step */);
13383}
13384
13386ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
13387 const Loop *L, bool IsSigned,
13388 bool ControlsOnlyExit, bool AllowPredicates) {
13390
13392 bool PredicatedIV = false;
13393 if (!IV) {
13394 if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS)) {
13395 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(ZExt->getOperand());
13396 if (AR && AR->getLoop() == L && AR->isAffine()) {
13397 auto canProveNUW = [&]() {
13398 // We can use the comparison to infer no-wrap flags only if it fully
13399 // controls the loop exit.
13400 if (!ControlsOnlyExit)
13401 return false;
13402
13403 if (!isLoopInvariant(RHS, L))
13404 return false;
13405
13406 if (!isKnownNonZero(AR->getStepRecurrence(*this)))
13407 // We need the sequence defined by AR to strictly increase in the
13408 // unsigned integer domain for the logic below to hold.
13409 return false;
13410
13411 const unsigned InnerBitWidth = getTypeSizeInBits(AR->getType());
13412 const unsigned OuterBitWidth = getTypeSizeInBits(RHS->getType());
13413 // If RHS <=u Limit, then there must exist a value V in the sequence
13414 // defined by AR (e.g. {Start,+,Step}) such that V >u RHS, and
13415 // V <=u UINT_MAX. Thus, we must exit the loop before unsigned
13416 // overflow occurs. This limit also implies that a signed comparison
13417 // (in the wide bitwidth) is equivalent to an unsigned comparison as
13418 // the high bits on both sides must be zero.
13419 APInt StrideMax = getUnsignedRangeMax(AR->getStepRecurrence(*this));
13420 APInt Limit = APInt::getMaxValue(InnerBitWidth) - (StrideMax - 1);
13421 Limit = Limit.zext(OuterBitWidth);
13422 return getUnsignedRangeMax(applyLoopGuards(RHS, L)).ule(Limit);
13423 };
13424 auto Flags = AR->getNoWrapFlags();
13425 if (!hasFlags(Flags, SCEV::FlagNUW) && canProveNUW())
13426 Flags = setFlags(Flags, SCEV::FlagNUW);
13427
13428 setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
13429 if (AR->hasNoUnsignedWrap()) {
13430 // Emulate what getZeroExtendExpr would have done during construction
13431 // if we'd been able to infer the fact just above at that time.
13432 const SCEV *Step = AR->getStepRecurrence(*this);
13433 Type *Ty = ZExt->getType();
13434 const SCEV *S = getAddRecExpr(
13436 getZeroExtendExpr(Step, Ty, 0), L, AR->getNoWrapFlags());
13438 }
13439 }
13440 }
13441 }
13442
13443
13444 if (!IV && AllowPredicates) {
13445 // Try to make this an AddRec using runtime tests, in the first X
13446 // iterations of this loop, where X is the SCEV expression found by the
13447 // algorithm below.
13448 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
13449 PredicatedIV = true;
13450 }
13451
13452 // Avoid weird loops
13453 if (!IV || IV->getLoop() != L || !IV->isAffine())
13454 return getCouldNotCompute();
13455
13456 // A precondition of this method is that the condition being analyzed
13457 // reaches an exiting branch which dominates the latch. Given that, we can
13458 // assume that an increment which violates the nowrap specification and
13459 // produces poison must cause undefined behavior when the resulting poison
13460 // value is branched upon and thus we can conclude that the backedge is
13461 // taken no more often than would be required to produce that poison value.
13462 // Note that a well defined loop can exit on the iteration which violates
13463 // the nowrap specification if there is another exit (either explicit or
13464 // implicit/exceptional) which causes the loop to execute before the
13465 // exiting instruction we're analyzing would trigger UB.
13466 auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW;
13467 bool NoWrap = ControlsOnlyExit && any(IV->getNoWrapFlags(WrapType));
13469
13470 const SCEV *Stride = IV->getStepRecurrence(*this);
13471 const SCEV *GuardedStride = Stride;
13472
13473 // Whether the IV may reach the maximum value before the exit is taken.
13474 bool IVMayOverflow = true;
13475
13476 bool PositiveStride = isKnownPositive(Stride);
13477 // A dominating guard may prove the stride positive.
13478 if (!PositiveStride) {
13479 const SCEV *LoopGuardedStride = applyLoopGuards(Stride, L);
13480 if (isKnownPositive(LoopGuardedStride)) {
13481 GuardedStride = LoopGuardedStride;
13482 PositiveStride = true;
13483 // Encode the context-sensitive stride > 0 fact into the expression
13484 Stride = getUMaxExpr(Stride, getOne(Stride->getType()));
13485 }
13486 }
13487
13488 // Avoid negative or zero stride values.
13489 if (!PositiveStride) {
13490 // We can compute the correct backedge taken count for loops with unknown
13491 // strides if we can prove that the loop is not an infinite loop with side
13492 // effects. Here's the loop structure we are trying to handle -
13493 //
13494 // i = start
13495 // do {
13496 // A[i] = i;
13497 // i += s;
13498 // } while (i < end);
13499 //
13500 // The backedge taken count for such loops is evaluated as -
13501 // (max(end, start + stride) - start - 1) /u stride
13502 //
13503 // The additional preconditions that we need to check to prove correctness
13504 // of the above formula is as follows -
13505 //
13506 // a) IV is either nuw or nsw depending upon signedness (indicated by the
13507 // NoWrap flag).
13508 // b) the loop is guaranteed to be finite (e.g. is mustprogress and has
13509 // no side effects within the loop)
13510 // c) loop has a single static exit (with no abnormal exits)
13511 //
13512 // Precondition a) implies that if the stride is negative, this is a single
13513 // trip loop. The backedge taken count formula reduces to zero in this case.
13514 //
13515 // Precondition b) and c) combine to imply that if rhs is invariant in L,
13516 // then a zero stride means the backedge can't be taken without executing
13517 // undefined behavior.
13518 //
13519 // The positive stride case is the same as isKnownPositive(Stride) returning
13520 // true (original behavior of the function).
13521 //
13522 if (PredicatedIV || !NoWrap || !loopIsFiniteByAssumption(L) ||
13524 return getCouldNotCompute();
13525
13526 if (!isKnownNonZero(Stride)) {
13527 // If we have a step of zero, and RHS isn't invariant in L, we don't know
13528 // if it might eventually be greater than start and if so, on which
13529 // iteration. We can't even produce a useful upper bound.
13530 if (!isLoopInvariant(RHS, L))
13531 return getCouldNotCompute();
13532
13533 // We allow a potentially zero stride, but we need to divide by stride
13534 // below. Since the loop can't be infinite and this check must control
13535 // the sole exit, we can infer the exit must be taken on the first
13536 // iteration (e.g. backedge count = 0) if the stride is zero. Given that,
13537 // we know the numerator in the divides below must be zero, so we can
13538 // pick an arbitrary non-zero value for the denominator (e.g. stride)
13539 // and produce the right result.
13540 // FIXME: Handle the case where Stride is poison?
13541 auto wouldZeroStrideBeUB = [&]() {
13542 // Proof by contradiction. Suppose the stride were zero. If we can
13543 // prove that the backedge *is* taken on the first iteration, then since
13544 // we know this condition controls the sole exit, we must have an
13545 // infinite loop. We can't have a (well defined) infinite loop per
13546 // check just above.
13547 // Note: The (Start - Stride) term is used to get the start' term from
13548 // (start' + stride,+,stride). Remember that we only care about the
13549 // result of this expression when stride == 0 at runtime.
13550 auto *StartIfZero = getMinusSCEV(IV->getStart(), Stride);
13551 return isLoopEntryGuardedByCond(L, Cond, StartIfZero, RHS);
13552 };
13553 if (!wouldZeroStrideBeUB()) {
13554 Stride = getUMaxExpr(Stride, getOne(Stride->getType()));
13555 }
13556 }
13557 } else {
13558 // Avoid proven overflow cases: this will ensure that the backedge taken
13559 // count will not generate any unsigned overflow.
13560 IVMayOverflow = canIVOverflowOnLT(RHS, GuardedStride, IsSigned);
13561 if (IVMayOverflow && !NoWrap)
13562 return getCouldNotCompute();
13563 }
13564
13565 // On all paths just preceeding, we established the following invariant:
13566 // IV can be assumed not to overflow up to and including the exiting
13567 // iteration. We proved this in one of two ways:
13568 // 1) We can show overflow doesn't occur before the exiting iteration
13569 // 1a) canIVOverflowOnLT, and b) step of one
13570 // 2) We can show that if overflow occurs, the loop must execute UB
13571 // before any possible exit.
13572 // Note that we have not yet proved RHS invariant (in general).
13573
13574 const SCEV *Start = IV->getStart();
13575
13576 // Preserve pointer-typed Start/RHS to pass to isLoopEntryGuardedByCond.
13577 // If we convert to integers, isLoopEntryGuardedByCond will miss some cases.
13578 // Use integer-typed versions for actual computation; we can't subtract
13579 // pointers in general.
13580 const SCEV *OrigStart = Start;
13581 const SCEV *OrigRHS = RHS;
13582 if (Start->getType()->isPointerTy()) {
13583 Start = getPtrToAddrExpr(Start);
13584 if (isa<SCEVCouldNotCompute>(Start))
13585 return Start;
13586 }
13587 if (RHS->getType()->isPointerTy()) {
13590 return RHS;
13591 }
13592
13593 const SCEV *End = nullptr, *BECount = getCouldNotCompute(),
13594 *BECountIfBackedgeTaken = getCouldNotCompute();
13595 if (!isLoopInvariant(RHS, L)) {
13596 const auto *RHSAddRec = dyn_cast<SCEVAddRecExpr>(RHS);
13597 if (PositiveStride && RHSAddRec != nullptr && RHSAddRec->getLoop() == L &&
13598 any(RHSAddRec->getNoWrapFlags())) {
13599 // The structure of loop we are trying to calculate backedge count of:
13600 //
13601 // left = left_start
13602 // right = right_start
13603 //
13604 // while(left < right){
13605 // ... do something here ...
13606 // left += s1; // stride of left is s1 (s1 > 0)
13607 // right += s2; // stride of right is s2 (s2 < 0)
13608 // }
13609 //
13610
13611 const SCEV *RHSStart = RHSAddRec->getStart();
13612 const SCEV *RHSStride = RHSAddRec->getStepRecurrence(*this);
13613
13614 // If Stride - RHSStride is positive and does not overflow, we can write
13615 // backedge count as ->
13616 // ceil((End - Start) /u (Stride - RHSStride))
13617 // Where, End = max(RHSStart, Start)
13618
13619 // Check if RHSStride < 0 and Stride - RHSStride will not overflow.
13620 if (isKnownNegative(RHSStride) &&
13621 willNotOverflow(Instruction::Sub, /*Signed=*/true, Stride,
13622 RHSStride)) {
13623
13624 const SCEV *Denominator = getMinusSCEV(Stride, RHSStride);
13625 if (isKnownPositive(Denominator)) {
13626 End = IsSigned ? getSMaxExpr(RHSStart, Start)
13627 : getUMaxExpr(RHSStart, Start);
13628
13629 // We can do this because End >= Start, as End = max(RHSStart, Start)
13630 const SCEV *Delta = getMinusSCEV(End, Start);
13631
13632 BECount = getUDivCeilSCEV(Delta, Denominator);
13633 BECountIfBackedgeTaken =
13634 getUDivCeilSCEV(getMinusSCEV(RHSStart, Start), Denominator);
13635 }
13636 }
13637 }
13638 } else {
13639 // Let End = max(RHS,Start). We use the expression (End-Start)/Stride to
13640 // describe the backedge count: if the backedge is taken at least once then
13641 // End is RHS, and if not End is Start so we get a backedge count of zero.
13642 //
13643 // AddingStrideMinusOneMayOverflow has the following preconditions:
13644 //
13645 // 1. If IsSigned, Start <=s End; otherwise, Start <=u End
13646 // 2. The index variable doesn't overflow.
13647 //
13648 // Therefore, we know N exists such that
13649 // (Start + Stride * N) >= End, and computing "(Start + Stride * N)"
13650 // doesn't overflow.
13651 //
13652 // Using this information, try to prove whether the addition in
13653 // "(End - Start) + (Stride - 1)" has unsigned overflow.
13654 //
13655 // If the IV cannot overflow, RHS is at least Stride - 1 below the maximum
13656 // value, so the distance End - Start is at most UMAX - (Stride - 1) and
13657 // the (Stride - 1) addition below cannot overflow.
13658 const SCEV *One = getOne(Stride->getType());
13659 bool AddingStrideMinusOneMayOverflow = IVMayOverflow && [&] {
13660 if (isKnownToBeAPowerOfTwo(Stride)) {
13661 // Suppose Stride is a power of two, and Start/End are unsigned
13662 // integers. Let UMAX be the largest representable unsigned
13663 // integer.
13664 //
13665 // By the preconditions of this function, we know
13666 // "(Start + Stride * N) >= End", and this doesn't overflow.
13667 // As a formula:
13668 //
13669 // End <= (Start + Stride * N) <= UMAX
13670 //
13671 // Subtracting Start from all the terms:
13672 //
13673 // End - Start <= Stride * N <= UMAX - Start
13674 //
13675 // Since Start is unsigned, UMAX - Start <= UMAX. Therefore:
13676 //
13677 // End - Start <= Stride * N <= UMAX
13678 //
13679 // Stride * N is a multiple of Stride. Therefore,
13680 //
13681 // End - Start <= Stride * N <= UMAX - (UMAX mod Stride)
13682 //
13683 // Since Stride is a power of two, UMAX + 1 is divisible by
13684 // Stride. Therefore, UMAX mod Stride == Stride - 1. So we can
13685 // write:
13686 //
13687 // End - Start <= Stride * N <= UMAX - Stride - 1
13688 //
13689 // Dropping the middle term:
13690 //
13691 // End - Start <= UMAX - Stride - 1
13692 //
13693 // Adding Stride - 1 to both sides:
13694 //
13695 // (End - Start) + (Stride - 1) <= UMAX
13696 //
13697 // In other words, the addition doesn't have unsigned overflow.
13698 //
13699 // A similar proof works if we treat Start/End as signed values.
13700 // Just rewrite steps before "End - Start <= Stride * N <= UMAX"
13701 // to use signed max instead of unsigned max. Note that we're
13702 // trying to prove a lack of unsigned overflow in either case.
13703 return false;
13704 }
13705 if (Start == Stride || Start == getMinusSCEV(Stride, One)) {
13706 // If Start is equal to Stride, (End - Start) + (Stride - 1) == End
13707 // - 1. If !IsSigned, 0 <u Stride == Start <=u End; so 0 <u End - 1
13708 // <u End. If IsSigned, 0 <s Stride == Start <=s End; so 0 <s End -
13709 // 1 <s End.
13710 //
13711 // If Start is equal to Stride - 1, (End - Start) + Stride - 1 ==
13712 // End.
13713 return false;
13714 }
13715 return true;
13716 }();
13717
13718 auto *OrigStartMinusStride = getMinusSCEV(OrigStart, Stride);
13719 assert(isAvailableAtLoopEntry(OrigStartMinusStride, L) && "Must be!");
13720 assert(isAvailableAtLoopEntry(OrigStart, L) && "Must be!");
13721 assert(isAvailableAtLoopEntry(OrigRHS, L) && "Must be!");
13722 // Can we prove Start - Stride < RHS, and either Start - Stride < Start or
13723 // (via !AddingStrideMinusOneMayOverflow) that (RHS - Start) + (Stride - 1)
13724 // does not overflow?
13725 if ((!AddingStrideMinusOneMayOverflow ||
13726 isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigStart)) &&
13727 isLoopEntryGuardedByCond(L, Cond, OrigStartMinusStride, OrigRHS)) {
13728 // In this case, we can use a refined formula for computing backedge
13729 // taken count. The general formula remains:
13730 // "End-Start /uceiling Stride"
13731 // We want to use the alternate formula:
13732 // "((RHS - 1) - (Start - Stride)) /u Stride"
13733 // Let's do a quick case analysis to show these are equivalent under
13734 // our preconditions.
13735 // * For RHS <= Start (End is Start), the backedge-taken count must be
13736 // zero. Together with the precondition "Start - Stride < RHS", we have
13737 // "Start - Stride < RHS <= Start". Subtracting Start - Stride from
13738 // all sides we get "0 < RHS - (Start - Stride) <= Stride".
13739 // Subtracting 1 we get "0 <= (RHS - 1) - (Start - Stride) < Stride".
13740 // So dividing that by Stride gives zero.
13741 //
13742 // * For RHS > Start (End is RHS), the backedge count must be
13743 // "RHS-Start /uceil Stride", so it is sufficient to show that the
13744 // numerator "((RHS - 1) - (Start - Stride))" does not overflow.
13745 //
13746 // If "Start - Stride < Start" holds, we have
13747 // "RHS > Start > Start - Stride". As such
13748 // "RHS - (Start - Stride) - 1" does not overflow, which is the
13749 // reassociated numerator.
13750 //
13751 // Otherwise !AddingStrideMinusOneMayOverflow guarantees that
13752 // "(End - Start) + (Stride - 1)" does not overflow unsigned. Here
13753 // "End" is "RHS", as "RHS > Start", so this is the reassociated
13754 // numerator. Neither sub-term wraps unsigned: "RHS - Start"
13755 // due to "RHS > Start", and "Stride - 1", as Stride is non-zero.
13756 const SCEV *MinusOne = getMinusOne(Stride->getType());
13757 const SCEV *Numerator =
13758 getMinusSCEV(getAddExpr(RHS, MinusOne), getMinusSCEV(Start, Stride));
13759 BECount = getUDivExpr(Numerator, Stride);
13760 }
13761
13762 if (isa<SCEVCouldNotCompute>(BECount)) {
13763 auto canProveRHSGreaterThanEqualStart = [&]() {
13764 auto CondGE = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
13765 const SCEV *GuardedRHS = applyLoopGuards(OrigRHS, L);
13766 const SCEV *GuardedStart = applyLoopGuards(OrigStart, L);
13767
13768 if (isLoopEntryGuardedByCond(L, CondGE, OrigRHS, OrigStart) ||
13769 isKnownPredicate(CondGE, GuardedRHS, GuardedStart))
13770 return true;
13771
13772 // (RHS > Start - 1) implies RHS >= Start.
13773 // * "RHS >= Start" is trivially equivalent to "RHS > Start - 1" if
13774 // "Start - 1" doesn't overflow.
13775 // * For signed comparison, if Start - 1 does overflow, it's equal
13776 // to INT_MAX, and "RHS >s INT_MAX" is trivially false.
13777 // * For unsigned comparison, if Start - 1 does overflow, it's equal
13778 // to UINT_MAX, and "RHS >u UINT_MAX" is trivially false.
13779 //
13780 // FIXME: Should isLoopEntryGuardedByCond do this for us?
13781 auto CondGT = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
13782 const SCEV *StartMinusOne =
13783 getAddExpr(OrigStart, getMinusOne(OrigStart->getType()));
13784 return isLoopEntryGuardedByCond(L, CondGT, OrigRHS, StartMinusOne);
13785 };
13786
13787 // If we know that RHS >= Start in the context of loop, then we know
13788 // that max(RHS, Start) = RHS at this point.
13789 if (canProveRHSGreaterThanEqualStart()) {
13790 End = RHS;
13791 } else {
13792 // If RHS < Start, the backedge will be taken zero times. So in
13793 // general, we can write the backedge-taken count as:
13794 //
13795 // RHS >= Start ? ceil(RHS - Start) / Stride : 0
13796 //
13797 // We convert it to the following to make it more convenient for SCEV:
13798 //
13799 // ceil(max(RHS, Start) - Start) / Stride
13800 End = IsSigned ? getSMaxExpr(RHS, Start) : getUMaxExpr(RHS, Start);
13801
13802 // See what would happen if we assume the backedge is taken. This is
13803 // used to compute MaxBECount.
13804 BECountIfBackedgeTaken =
13805 getUDivCeilSCEV(getMinusSCEV(RHS, Start), Stride);
13806 }
13807
13808 const SCEV *Delta = getMinusSCEV(End, Start);
13809 if (!AddingStrideMinusOneMayOverflow) {
13810 // floor((D + (S - 1)) / S)
13811 // We prefer this formulation if it's legal because it's fewer
13812 // operations.
13813 BECount =
13814 getUDivExpr(getAddExpr(Delta, getMinusSCEV(Stride, One)), Stride);
13815 } else {
13816 BECount = getUDivCeilSCEV(Delta, Stride);
13817 }
13818 }
13819 }
13820
13821 const SCEV *ConstantMaxBECount;
13822 bool MaxOrZero = false;
13823 if (isa<SCEVConstant>(BECount)) {
13824 ConstantMaxBECount = BECount;
13825 } else if (isa<SCEVConstant>(BECountIfBackedgeTaken)) {
13826 // If we know exactly how many times the backedge will be taken if it's
13827 // taken at least once, then the backedge count will either be that or
13828 // zero.
13829 ConstantMaxBECount = BECountIfBackedgeTaken;
13830 MaxOrZero = true;
13831 } else {
13832 ConstantMaxBECount = computeMaxBECountForLT(
13833 Start, Stride, RHS, getTypeSizeInBits(LHS->getType()), IsSigned);
13834 }
13835
13836 if (isa<SCEVCouldNotCompute>(ConstantMaxBECount) &&
13837 !isa<SCEVCouldNotCompute>(BECount))
13838 ConstantMaxBECount = getConstant(getUnsignedRangeMax(BECount));
13839
13840 const SCEV *SymbolicMaxBECount =
13841 isa<SCEVCouldNotCompute>(BECount) ? ConstantMaxBECount : BECount;
13842 return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, MaxOrZero,
13843 Predicates);
13844}
13845
13846ScalarEvolution::ExitLimit ScalarEvolution::howManyGreaterThans(
13847 const SCEV *LHS, const SCEV *RHS, const Loop *L, bool IsSigned,
13848 bool ControlsOnlyExit, bool AllowPredicates) {
13850 // We handle only IV > Invariant
13851 if (!isLoopInvariant(RHS, L))
13852 return getCouldNotCompute();
13853
13854 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
13855 if (!IV && AllowPredicates)
13856 // Try to make this an AddRec using runtime tests, in the first X
13857 // iterations of this loop, where X is the SCEV expression found by the
13858 // algorithm below.
13859 IV = convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
13860
13861 // Avoid weird loops
13862 if (!IV || IV->getLoop() != L || !IV->isAffine())
13863 return getCouldNotCompute();
13864
13865 auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW;
13866 bool NoWrap = ControlsOnlyExit && any(IV->getNoWrapFlags(WrapType));
13868
13869 const SCEV *Stride = getNegativeSCEV(IV->getStepRecurrence(*this));
13870
13871 // Avoid negative or zero stride values
13872 if (!isKnownPositive(Stride))
13873 return getCouldNotCompute();
13874
13875 // Avoid proven overflow cases: this will ensure that the backedge taken count
13876 // will not generate any unsigned overflow. Relaxed no-overflow conditions
13877 // exploit NoWrapFlags, allowing to optimize in presence of undefined
13878 // behaviors like the case of C language.
13879 bool MayAddOverflow = false;
13880 const SCEV *Start = IV->getStart();
13881 const SCEV *End = RHS;
13882 if (!Stride->isOne() && canIVOverflowOnGT(RHS, Stride, IsSigned)) {
13883 if (!NoWrap)
13884 return getCouldNotCompute();
13885 MayAddOverflow = true;
13886 }
13887
13888 if (!isLoopEntryGuardedByCond(L, Cond, getAddExpr(Start, Stride), RHS)) {
13889 // If we know that Start >= RHS in the context of loop, then we know that
13890 // min(RHS, Start) = RHS at this point.
13892 L, IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, Start, RHS))
13893 End = RHS;
13894 else
13895 End = IsSigned ? getSMinExpr(RHS, Start) : getUMinExpr(RHS, Start);
13896 }
13897
13898 if (Start->getType()->isPointerTy()) {
13899 Start = getPtrToAddrExpr(Start);
13900 if (isa<SCEVCouldNotCompute>(Start))
13901 return Start;
13902 }
13903 if (End->getType()->isPointerTy()) {
13904 End = getPtrToAddrExpr(End);
13905 if (isa<SCEVCouldNotCompute>(End))
13906 return End;
13907 }
13908
13909 const SCEV *Delta = getMinusSCEV(Start, End);
13910 const SCEV *BECount;
13911 if (MayAddOverflow) {
13912 // The ceiling division instead needs Start >= End, so that (Start - End) is
13913 // the exact unsigned distance between them.
13915 L, IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, Start, End))
13916 return getCouldNotCompute();
13917 BECount = getUDivCeilSCEV(Delta, Stride);
13918 } else {
13919 // Compute ((Start - End) + (Stride - 1)) / Stride, if the IV cannot
13920 // overflow as it requires fewer operations.
13921 const SCEV *One = getOne(Stride->getType());
13922 BECount = getUDivExpr(getAddExpr(Delta, getMinusSCEV(Stride, One)), Stride);
13923 }
13924
13925 APInt MaxStart = IsSigned ? getSignedRangeMax(Start)
13927
13928 APInt MinStride = IsSigned ? getSignedRangeMin(Stride)
13929 : getUnsignedRangeMin(Stride);
13930
13931 unsigned BitWidth = getTypeSizeInBits(LHS->getType());
13932 APInt Limit = IsSigned ? APInt::getSignedMinValue(BitWidth) + (MinStride - 1)
13933 : APInt::getMinValue(BitWidth) + (MinStride - 1);
13934
13935 // Although End can be a MIN expression we estimate MinEnd considering only
13936 // the case End = RHS. This is safe because in the other case (Start - End)
13937 // is zero, leading to a zero maximum backedge taken count.
13938 APInt MinEnd =
13939 IsSigned ? APIntOps::smax(getSignedRangeMin(RHS), Limit)
13940 : APIntOps::umax(getUnsignedRangeMin(RHS), Limit);
13941
13942 const SCEV *ConstantMaxBECount =
13943 isa<SCEVConstant>(BECount)
13944 ? BECount
13945 : getUDivCeilSCEV(getConstant(MaxStart - MinEnd),
13946 getConstant(MinStride));
13947
13948 if (isa<SCEVCouldNotCompute>(ConstantMaxBECount))
13949 ConstantMaxBECount = BECount;
13950 const SCEV *SymbolicMaxBECount =
13951 isa<SCEVCouldNotCompute>(BECount) ? ConstantMaxBECount : BECount;
13952
13953 return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false,
13954 Predicates);
13955}
13956
13958 ScalarEvolution &SE) const {
13959 if (Range.isFullSet()) // Infinite loop.
13960 return SE.getCouldNotCompute();
13961
13962 // If the start is a non-zero constant, shift the range to simplify things.
13963 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
13964 if (!SC->getValue()->isZero()) {
13966 Operands[0] = SE.getZero(SC->getType());
13967 const SCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(),
13969 if (const auto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
13970 return ShiftedAddRec->getNumIterationsInRange(
13971 Range.subtract(SC->getAPInt()), SE);
13972 // This is strange and shouldn't happen.
13973 return SE.getCouldNotCompute();
13974 }
13975
13976 // The only time we can solve this is when we have all constant indices.
13977 // Otherwise, we cannot determine the overflow conditions.
13979 return SE.getCouldNotCompute();
13980
13981 // Okay at this point we know that all elements of the chrec are constants and
13982 // that the start element is zero.
13983
13984 // First check to see if the range contains zero. If not, the first
13985 // iteration exits.
13986 unsigned BitWidth = SE.getTypeSizeInBits(getType());
13987 if (!Range.contains(APInt(BitWidth, 0)))
13988 return SE.getZero(getType());
13989
13990 if (isAffine()) {
13991 // If this is an affine expression then we have this situation:
13992 // Solve {0,+,A} in Range === Ax in Range
13993
13994 // We know that zero is in the range. If A is positive then we know that
13995 // the upper value of the range must be the first possible exit value.
13996 // If A is negative then the lower of the range is the last possible loop
13997 // value. Also note that we already checked for a full range.
13998 APInt A = cast<SCEVConstant>(getOperand(1))->getAPInt();
13999 APInt End = A.sge(1) ? (Range.getUpper() - 1) : Range.getLower();
14000
14001 // The exit value should be (End+A)/A.
14002 APInt ExitVal = (End + A).udiv(A);
14003 ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
14004
14005 // Evaluate at the exit value. If we really did fall out of the valid
14006 // range, then we computed our trip count, otherwise wrap around or other
14007 // things must have happened.
14008 ConstantInt *Val = EvaluateConstantChrecAtConstant(this, ExitValue, SE);
14009 if (Range.contains(Val->getValue()))
14010 return SE.getCouldNotCompute(); // Something strange happened
14011
14012 // Ensure that the previous value is in the range.
14013 assert(Range.contains(
14015 ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) &&
14016 "Linear scev computation is off in a bad way!");
14017 return SE.getConstant(ExitValue);
14018 }
14019
14020 if (isQuadratic()) {
14021 if (auto S = SolveQuadraticAddRecRange(this, Range, SE))
14022 return SE.getConstant(*S);
14023 }
14024
14025 return SE.getCouldNotCompute();
14026}
14027
14028const SCEVAddRecExpr *
14030 assert(getNumOperands() > 1 && "AddRec with zero step?");
14031 // There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)),
14032 // but in this case we cannot guarantee that the value returned will be an
14033 // AddRec because SCEV does not have a fixed point where it stops
14034 // simplification: it is legal to return ({rec1} + {rec2}). For example, it
14035 // may happen if we reach arithmetic depth limit while simplifying. So we
14036 // construct the returned value explicitly.
14038 // If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and
14039 // (this + Step) is {A+B,+,B+C,+...,+,N}.
14040 for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i)
14041 Ops.push_back(SE.getAddExpr(getOperand(i), getOperand(i + 1)));
14042 // We know that the last operand is not a constant zero (otherwise it would
14043 // have been popped out earlier). This guarantees us that if the result has
14044 // the same last operand, then it will also not be popped out, meaning that
14045 // the returned value will be an AddRec.
14046 const SCEV *Last = getOperand(getNumOperands() - 1);
14047 assert(!Last->isZero() && "Recurrency with zero step?");
14048 Ops.push_back(Last);
14051}
14052
14053// Return true when S contains at least an undef value.
14055 return SCEVExprContains(
14056 S, [](const SCEV *S) { return match(S, m_scev_UndefOrPoison()); });
14057}
14058
14059// Return true when S contains a value that is a nullptr.
14061 return SCEVExprContains(S, [](const SCEV *S) {
14062 if (const auto *SU = dyn_cast<SCEVUnknown>(S))
14063 return SU->getValue() == nullptr;
14064 return false;
14065 });
14066}
14067
14068/// Return the size of an element read or written by Inst.
14070 if (!isa<LoadInst, StoreInst>(Inst))
14071 return nullptr;
14073 return getSizeOfExpr(ETy, getLoadStoreType(Inst));
14074}
14075
14076//===----------------------------------------------------------------------===//
14077// SCEVCallbackVH Class Implementation
14078//===----------------------------------------------------------------------===//
14079
14081 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
14082 if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
14083 SE->ConstantEvolutionLoopExitValue.erase(PN);
14084 SE->eraseValueFromMap(getValPtr());
14085 // this now dangles!
14086}
14087
14088void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) {
14089 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
14090
14091 // Forget all the expressions associated with users of the old value,
14092 // so that future queries will recompute the expressions using the new
14093 // value.
14094 SE->forgetValue(getValPtr());
14095 // this now dangles!
14096}
14097
14098ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
14099 : CallbackVH(V), SE(se) {}
14100
14101//===----------------------------------------------------------------------===//
14102// ScalarEvolution Class Implementation
14103//===----------------------------------------------------------------------===//
14104
14107 LoopInfo &LI)
14108 : F(F), DL(F.getDataLayout()), TLI(TLI), AC(AC), DT(DT), LI(LI),
14109 CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64),
14110 LoopDispositions(64), BlockDispositions(64) {
14111 // To use guards for proving predicates, we need to scan every instruction in
14112 // relevant basic blocks, and not just terminators. Doing this is a waste of
14113 // time if the IR does not actually contain any calls to
14114 // @llvm.experimental.guard, so do a quick check and remember this beforehand.
14115 //
14116 // This pessimizes the case where a pass that preserves ScalarEvolution wants
14117 // to _add_ guards to the module when there weren't any before, and wants
14118 // ScalarEvolution to optimize based on those guards. For now we prefer to be
14119 // efficient in lieu of being smart in that rather obscure case.
14120
14121 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
14122 F.getParent(), Intrinsic::experimental_guard);
14123 HasGuards = GuardDecl && !GuardDecl->use_empty();
14124}
14125
14127 : F(Arg.F), DL(Arg.DL), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC),
14128 DT(Arg.DT), LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)),
14129 ValueExprMap(std::move(Arg.ValueExprMap)),
14130 PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
14131 PendingMerges(std::move(Arg.PendingMerges)),
14132 ConstantMultipleCache(std::move(Arg.ConstantMultipleCache)),
14133 BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)),
14134 PredicatedBackedgeTakenCounts(
14135 std::move(Arg.PredicatedBackedgeTakenCounts)),
14136 BECountUsers(std::move(Arg.BECountUsers)),
14137 ConstantEvolutionLoopExitValue(
14138 std::move(Arg.ConstantEvolutionLoopExitValue)),
14139 ValuesAtScopes(std::move(Arg.ValuesAtScopes)),
14140 ValuesAtScopesUsers(std::move(Arg.ValuesAtScopesUsers)),
14141 LoopDispositions(std::move(Arg.LoopDispositions)),
14142 LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)),
14143 BlockDispositions(std::move(Arg.BlockDispositions)),
14144 SCEVUsers(std::move(Arg.SCEVUsers)),
14145 UnsignedRanges(std::move(Arg.UnsignedRanges)),
14146 SignedRanges(std::move(Arg.SignedRanges)),
14147 UniqueSCEVs(std::move(Arg.UniqueSCEVs)),
14148 UniquePreds(std::move(Arg.UniquePreds)),
14149 SCEVAllocator(std::move(Arg.SCEVAllocator)),
14150 ConstantSCEVs(std::move(Arg.ConstantSCEVs)),
14151 LoopUsers(std::move(Arg.LoopUsers)),
14152 PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)),
14153 FirstUnknown(Arg.FirstUnknown) {
14154 Arg.FirstUnknown = nullptr;
14155}
14156
14158 // Iterate through all the SCEVUnknown instances and call their
14159 // destructors, so that they release their references to their values.
14160 for (SCEVUnknown *U = FirstUnknown; U;) {
14161 SCEVUnknown *Tmp = U;
14162 U = U->Next;
14163 Tmp->~SCEVUnknown();
14164 }
14165 FirstUnknown = nullptr;
14166
14167 ExprValueMap.clear();
14168 ValueExprMap.clear();
14169 HasRecMap.clear();
14170 BackedgeTakenCounts.clear();
14171 PredicatedBackedgeTakenCounts.clear();
14172
14173 assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
14174 assert(PendingMerges.empty() && "isImpliedViaMerge garbage");
14175 assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!");
14176 assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!");
14177}
14178
14182
14183/// When printing a top-level SCEV for trip counts, it's helpful to include
14184/// a type for constants which are otherwise hard to disambiguate.
14185static void PrintSCEVWithTypeHint(raw_ostream &OS, const SCEV* S) {
14186 if (isa<SCEVConstant>(S))
14187 OS << *S->getType() << " ";
14188 OS << *S;
14189}
14190
14192 const Loop *L) {
14193 // Print all inner loops first
14194 for (Loop *I : *L)
14195 PrintLoopInfo(OS, SE, I);
14196
14197 OS << "Loop ";
14198 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14199 OS << ": ";
14200
14201 SmallVector<BasicBlock *, 8> ExitingBlocks;
14202 L->getExitingBlocks(ExitingBlocks);
14203 if (ExitingBlocks.size() != 1)
14204 OS << "<multiple exits> ";
14205
14206 auto *BTC = SE->getBackedgeTakenCount(L);
14207 if (!isa<SCEVCouldNotCompute>(BTC)) {
14208 OS << "backedge-taken count is ";
14209 PrintSCEVWithTypeHint(OS, BTC);
14210 } else
14211 OS << "Unpredictable backedge-taken count.";
14212 OS << "\n";
14213
14214 if (ExitingBlocks.size() > 1)
14215 for (BasicBlock *ExitingBlock : ExitingBlocks) {
14216 OS << " exit count for " << ExitingBlock->getName() << ": ";
14217 const SCEV *EC = SE->getExitCount(L, ExitingBlock);
14218 PrintSCEVWithTypeHint(OS, EC);
14219 if (isa<SCEVCouldNotCompute>(EC)) {
14220 // Retry with predicates.
14222 EC = SE->getPredicatedExitCount(L, ExitingBlock, &Predicates);
14223 if (!isa<SCEVCouldNotCompute>(EC)) {
14224 OS << "\n predicated exit count for " << ExitingBlock->getName()
14225 << ": ";
14226 PrintSCEVWithTypeHint(OS, EC);
14227 OS << "\n Predicates:\n";
14228 for (const auto *P : Predicates)
14229 P->print(OS, 4);
14230 }
14231 }
14232 OS << "\n";
14233 }
14234
14235 OS << "Loop ";
14236 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14237 OS << ": ";
14238
14239 auto *ConstantBTC = SE->getConstantMaxBackedgeTakenCount(L);
14240 if (!isa<SCEVCouldNotCompute>(ConstantBTC)) {
14241 OS << "constant max backedge-taken count is ";
14242 PrintSCEVWithTypeHint(OS, ConstantBTC);
14244 OS << ", actual taken count either this or zero.";
14245 } else {
14246 OS << "Unpredictable constant max backedge-taken count. ";
14247 }
14248
14249 OS << "\n"
14250 "Loop ";
14251 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14252 OS << ": ";
14253
14254 auto *SymbolicBTC = SE->getSymbolicMaxBackedgeTakenCount(L);
14255 if (!isa<SCEVCouldNotCompute>(SymbolicBTC)) {
14256 OS << "symbolic max backedge-taken count is ";
14257 PrintSCEVWithTypeHint(OS, SymbolicBTC);
14259 OS << ", actual taken count either this or zero.";
14260 } else {
14261 OS << "Unpredictable symbolic max backedge-taken count. ";
14262 }
14263 OS << "\n";
14264
14265 if (ExitingBlocks.size() > 1)
14266 for (BasicBlock *ExitingBlock : ExitingBlocks) {
14267 OS << " symbolic max exit count for " << ExitingBlock->getName() << ": ";
14268 auto *ExitBTC = SE->getExitCount(L, ExitingBlock,
14270 PrintSCEVWithTypeHint(OS, ExitBTC);
14271 if (isa<SCEVCouldNotCompute>(ExitBTC)) {
14272 // Retry with predicates.
14274 ExitBTC = SE->getPredicatedExitCount(L, ExitingBlock, &Predicates,
14276 if (!isa<SCEVCouldNotCompute>(ExitBTC)) {
14277 OS << "\n predicated symbolic max exit count for "
14278 << ExitingBlock->getName() << ": ";
14279 PrintSCEVWithTypeHint(OS, ExitBTC);
14280 OS << "\n Predicates:\n";
14281 for (const auto *P : Predicates)
14282 P->print(OS, 4);
14283 }
14284 }
14285 OS << "\n";
14286 }
14287
14289 auto *PBT = SE->getPredicatedBackedgeTakenCount(L, Preds);
14290 if (PBT != BTC) {
14291 OS << "Loop ";
14292 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14293 OS << ": ";
14294 if (!isa<SCEVCouldNotCompute>(PBT)) {
14295 OS << "Predicated backedge-taken count is ";
14296 PrintSCEVWithTypeHint(OS, PBT);
14297 } else
14298 OS << "Unpredictable predicated backedge-taken count.";
14299 OS << "\n";
14300 OS << " Predicates:\n";
14301 for (const auto *P : Preds)
14302 P->print(OS, 4);
14303 }
14304 Preds.clear();
14305
14306 auto *PredConstantMax =
14308 if (PredConstantMax != ConstantBTC) {
14309 OS << "Loop ";
14310 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14311 OS << ": ";
14312 if (!isa<SCEVCouldNotCompute>(PredConstantMax)) {
14313 OS << "Predicated constant max backedge-taken count is ";
14314 PrintSCEVWithTypeHint(OS, PredConstantMax);
14315 } else
14316 OS << "Unpredictable predicated constant max backedge-taken count.";
14317 OS << "\n";
14318 OS << " Predicates:\n";
14319 for (const auto *P : Preds)
14320 P->print(OS, 4);
14321 }
14322 Preds.clear();
14323
14324 auto *PredSymbolicMax =
14326 if (SymbolicBTC != PredSymbolicMax) {
14327 OS << "Loop ";
14328 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14329 OS << ": ";
14330 if (!isa<SCEVCouldNotCompute>(PredSymbolicMax)) {
14331 OS << "Predicated symbolic max backedge-taken count is ";
14332 PrintSCEVWithTypeHint(OS, PredSymbolicMax);
14333 } else
14334 OS << "Unpredictable predicated symbolic max backedge-taken count.";
14335 OS << "\n";
14336 OS << " Predicates:\n";
14337 for (const auto *P : Preds)
14338 P->print(OS, 4);
14339 }
14340
14342 OS << "Loop ";
14343 L->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14344 OS << ": ";
14345 OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n";
14346 }
14347}
14348
14349namespace llvm {
14350// Note: these overloaded operators need to be in the llvm namespace for them
14351// to be resolved correctly. If we put them outside the llvm namespace, the
14352//
14353// OS << ": " << SE.getLoopDisposition(SV, InnerL);
14354//
14355// code below "breaks" and start printing raw enum values as opposed to the
14356// string values.
14359 switch (LD) {
14361 OS << "Variant";
14362 break;
14364 OS << "Invariant";
14365 break;
14367 OS << "Uniform";
14368 break;
14370 OS << "Computable";
14371 break;
14372 }
14373 return OS;
14374}
14375
14378 switch (BD) {
14380 OS << "DoesNotDominate";
14381 break;
14383 OS << "Dominates";
14384 break;
14386 OS << "ProperlyDominates";
14387 break;
14388 }
14389 return OS;
14390}
14391} // namespace llvm
14392
14394 // ScalarEvolution's implementation of the print method is to print
14395 // out SCEV values of all instructions that are interesting. Doing
14396 // this potentially causes it to create new SCEV objects though,
14397 // which technically conflicts with the const qualifier. This isn't
14398 // observable from outside the class though, so casting away the
14399 // const isn't dangerous.
14400 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
14401
14402 if (ClassifyExpressions) {
14403 OS << "Classifying expressions for: ";
14404 F.printAsOperand(OS, /*PrintType=*/false);
14405 OS << "\n";
14406 for (Instruction &I : instructions(F))
14407 if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
14408 OS << I << '\n';
14409 OS << " --> ";
14410 const SCEV *SV = SE.getSCEV(&I);
14411 SV->print(OS);
14412 if (!isa<SCEVCouldNotCompute>(SV)) {
14413 OS << " U: ";
14414 SE.getUnsignedRange(SV).print(OS);
14415 OS << " S: ";
14416 SE.getSignedRange(SV).print(OS);
14417 }
14418
14419 const Loop *L = LI.getLoopFor(I.getParent());
14420
14421 SCEVUse AtUse = SE.getSCEVAtScope(SV, L);
14422 if (AtUse != SV) {
14423 OS << " --> ";
14424 OS << AtUse;
14425 if (!isa<SCEVCouldNotCompute>(AtUse)) {
14426 OS << " U: ";
14427 SE.getUnsignedRange(AtUse).print(OS);
14428 OS << " S: ";
14429 SE.getSignedRange(AtUse).print(OS);
14430 }
14431 }
14432
14433 if (L) {
14434 OS << "\t\t" "Exits: ";
14435 SCEVUse ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
14436 if (!SE.isLoopInvariant(ExitValue, L)) {
14437 OS << "<<Unknown>>";
14438 } else {
14439 OS << ExitValue;
14440 }
14441
14442 ListSeparator LS(", ", "\t\tLoopDispositions: { ");
14443 for (const auto *Iter = L; Iter; Iter = Iter->getParentLoop()) {
14444 OS << LS;
14445 Iter->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14446 OS << ": " << SE.getLoopDisposition(SV, Iter);
14447 }
14448
14449 for (const auto *InnerL : depth_first(L)) {
14450 if (InnerL == L)
14451 continue;
14452 OS << LS;
14453 InnerL->getHeader()->printAsOperand(OS, /*PrintType=*/false);
14454 OS << ": " << SE.getLoopDisposition(SV, InnerL);
14455 }
14456
14457 OS << " }";
14458 }
14459
14460 OS << "\n";
14461 }
14462 }
14463
14464 OS << "Determining loop execution counts for: ";
14465 F.printAsOperand(OS, /*PrintType=*/false);
14466 OS << "\n";
14467 for (Loop *I : LI)
14468 PrintLoopInfo(OS, &SE, I);
14469}
14470
14473 auto &Values = LoopDispositions[S];
14474 for (auto &V : Values) {
14475 if (V.getPointer() == L)
14476 return V.getInt();
14477 }
14478 Values.emplace_back(L, LoopVariant);
14479 LoopDisposition D = computeLoopDisposition(S, L);
14480 auto &Values2 = LoopDispositions[S];
14481 for (auto &V : llvm::reverse(Values2)) {
14482 if (V.getPointer() == L) {
14483 V.setInt(D);
14484 break;
14485 }
14486 }
14487 return D;
14488}
14489
14491ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) {
14492 switch (S->getSCEVType()) {
14493 case scConstant:
14494 case scVScale:
14495 return LoopInvariant;
14496 case scAddRecExpr: {
14497 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
14498
14499 // If L is the addrec's loop, it's computable.
14500 if (AR->getLoop() == L)
14501 return LoopComputable;
14502
14503 // Add recurrences are never invariant in the function-body (null loop).
14504 if (!L)
14505 return LoopVariant;
14506
14507 // Everything that is not defined at loop entry is variant.
14508 if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader())) {
14509 if (L->contains(AR->getLoop()) &&
14510 llvm::all_of(AR->operands(),
14511 [&](const SCEV *Op) { return isLoopUniform(Op, L); }))
14512 return LoopUniform;
14513
14514 return LoopVariant;
14515 }
14516 assert(!L->contains(AR->getLoop()) && "Containing loop's header does not"
14517 " dominate the contained loop's header?");
14518
14519 // This recurrence is invariant w.r.t. L if AR's loop contains L.
14520 if (AR->getLoop()->contains(L))
14521 return LoopInvariant;
14522
14523 // This recurrence is variant w.r.t. L if any of its operands
14524 // are variant.
14525 for (SCEVUse Op : AR->operands())
14526 if (!isLoopInvariant(Op, L))
14527 return LoopVariant;
14528
14529 // Otherwise it's loop-invariant.
14530 return LoopInvariant;
14531 }
14532 case scTruncate:
14533 case scZeroExtend:
14534 case scSignExtend:
14535 case scPtrToAddr:
14536 case scAddExpr:
14537 case scMulExpr:
14538 case scUDivExpr:
14539 case scUMaxExpr:
14540 case scSMaxExpr:
14541 case scUMinExpr:
14542 case scSMinExpr:
14543 case scSequentialUMinExpr: {
14544 bool HasVarying = false;
14545 bool HasUniform = false;
14546 for (SCEVUse Op : S->operands()) {
14548 if (D == LoopVariant)
14549 return LoopVariant;
14550 if (D == LoopComputable)
14551 HasVarying = true;
14552 if (D == LoopUniform)
14553 HasUniform = true;
14554 }
14555 return HasVarying ? (HasUniform ? LoopVariant : LoopComputable)
14556 : (HasUniform ? LoopUniform : LoopInvariant);
14557 }
14558 case scUnknown:
14559 // All non-instruction values are loop invariant. All instructions are loop
14560 // invariant if they are not contained in the specified loop.
14561 // Instructions are never considered invariant in the function body
14562 // (null loop) because they are defined within the "loop".
14564 return (L && !L->contains(I)) ? LoopInvariant : LoopVariant;
14565 return LoopInvariant;
14566 case scCouldNotCompute:
14567 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
14568 }
14569 llvm_unreachable("Unknown SCEV kind!");
14570}
14571
14572bool ScalarEvolution::isLoopUniform(const SCEV *S, const Loop *L) {
14574 return D == LoopUniform || D == LoopInvariant;
14575}
14576
14578 return getLoopDisposition(S, L) == LoopInvariant;
14579}
14580
14582 return getLoopDisposition(S, L) == LoopComputable;
14583}
14584
14587 auto &Values = BlockDispositions[S];
14588 for (auto &V : Values) {
14589 if (V.getPointer() == BB)
14590 return V.getInt();
14591 }
14592 Values.emplace_back(BB, DoesNotDominateBlock);
14593 BlockDisposition D = computeBlockDisposition(S, BB);
14594 auto &Values2 = BlockDispositions[S];
14595 for (auto &V : llvm::reverse(Values2)) {
14596 if (V.getPointer() == BB) {
14597 V.setInt(D);
14598 break;
14599 }
14600 }
14601 return D;
14602}
14603
14605ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
14606 switch (S->getSCEVType()) {
14607 case scConstant:
14608 case scVScale:
14610 case scAddRecExpr: {
14611 // This uses a "dominates" query instead of "properly dominates" query
14612 // to test for proper dominance too, because the instruction which
14613 // produces the addrec's value is a PHI, and a PHI effectively properly
14614 // dominates its entire containing block.
14615 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
14616 if (!DT.dominates(AR->getLoop()->getHeader(), BB))
14617 return DoesNotDominateBlock;
14618
14619 // Fall through into SCEVNAryExpr handling.
14620 [[fallthrough]];
14621 }
14622 case scTruncate:
14623 case scZeroExtend:
14624 case scSignExtend:
14625 case scPtrToAddr:
14626 case scAddExpr:
14627 case scMulExpr:
14628 case scUDivExpr:
14629 case scUMaxExpr:
14630 case scSMaxExpr:
14631 case scUMinExpr:
14632 case scSMinExpr:
14633 case scSequentialUMinExpr: {
14634 bool Proper = true;
14635 for (const SCEV *NAryOp : S->operands()) {
14637 if (D == DoesNotDominateBlock)
14638 return DoesNotDominateBlock;
14639 if (D == DominatesBlock)
14640 Proper = false;
14641 }
14642 return Proper ? ProperlyDominatesBlock : DominatesBlock;
14643 }
14644 case scUnknown:
14645 if (Instruction *I =
14647 if (I->getParent() == BB)
14648 return DominatesBlock;
14649 if (DT.properlyDominates(I->getParent(), BB))
14651 return DoesNotDominateBlock;
14652 }
14654 case scCouldNotCompute:
14655 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
14656 }
14657 llvm_unreachable("Unknown SCEV kind!");
14658}
14659
14660bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) {
14661 return getBlockDisposition(S, BB) >= DominatesBlock;
14662}
14663
14666}
14667
14668bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const {
14669 return SCEVExprContains(S, [&](const SCEV *Expr) { return Expr == Op; });
14670}
14671
14672void ScalarEvolution::forgetBackedgeTakenCounts(const Loop *L,
14673 bool Predicated) {
14674 auto &BECounts =
14675 Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts;
14676 auto It = BECounts.find(L);
14677 if (It != BECounts.end()) {
14678 for (const ExitNotTakenInfo &ENT : It->second.ExitNotTaken) {
14679 for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) {
14680 if (!isa<SCEVConstant>(S)) {
14681 auto UserIt = BECountUsers.find(S);
14682 assert(UserIt != BECountUsers.end());
14683 UserIt->second.erase({L, Predicated});
14684 }
14685 }
14686 }
14687 BECounts.erase(It);
14688 }
14689}
14690
14691void ScalarEvolution::forgetMemoizedResults(ArrayRef<SCEVUse> SCEVs) {
14692 SmallPtrSet<const SCEV *, 8> ToForget(llvm::from_range, SCEVs);
14693 SmallVector<SCEVUse, 8> Worklist(ToForget.begin(), ToForget.end());
14694
14695 while (!Worklist.empty()) {
14696 const SCEV *Curr = Worklist.pop_back_val();
14697 auto Users = SCEVUsers.find(Curr);
14698 if (Users != SCEVUsers.end())
14699 for (const auto *User : Users->second)
14700 if (ToForget.insert(User).second)
14701 Worklist.push_back(User);
14702 }
14703
14704 for (const auto *S : ToForget)
14705 forgetMemoizedResultsImpl(S);
14706
14707 PredicatedSCEVRewrites.remove_if(
14708 [&](const auto &Entry) { return ToForget.count(Entry.first.first); });
14709}
14710
14711void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) {
14712 LoopDispositions.erase(S);
14713 BlockDispositions.erase(S);
14714 UnsignedRanges.erase(S);
14715 SignedRanges.erase(S);
14716 HasRecMap.erase(S);
14717 ConstantMultipleCache.erase(S);
14718
14719 if (auto *AR = dyn_cast<SCEVAddRecExpr>(S)) {
14720 UnsignedWrapViaInductionTried.erase(AR);
14721 SignedWrapViaInductionTried.erase(AR);
14722 }
14723
14724 auto ExprIt = ExprValueMap.find(S);
14725 if (ExprIt != ExprValueMap.end()) {
14726 for (Value *V : ExprIt->second) {
14727 auto ValueIt = ValueExprMap.find_as(V);
14728 if (ValueIt != ValueExprMap.end())
14729 ValueExprMap.erase(ValueIt);
14730 }
14731 ExprValueMap.erase(ExprIt);
14732 }
14733
14734 auto ScopeIt = ValuesAtScopes.find(S);
14735 if (ScopeIt != ValuesAtScopes.end()) {
14736 for (const auto &Pair : ScopeIt->second)
14737 if (!isa_and_nonnull<SCEVConstant>(Pair.second))
14738 llvm::erase(ValuesAtScopesUsers[Pair.second.getPointer()],
14739 std::make_pair(Pair.first, S));
14740 ValuesAtScopes.erase(ScopeIt);
14741 }
14742
14743 auto ScopeUserIt = ValuesAtScopesUsers.find(S);
14744 if (ScopeUserIt != ValuesAtScopesUsers.end()) {
14745 for (const auto &Pair : ScopeUserIt->second)
14746 // The recorded value at scope is a use of S, which may carry no-wrap
14747 // flags that are not part of this key.
14748 llvm::erase_if(ValuesAtScopes[Pair.second], [&](const auto &LS) {
14749 return LS.first == Pair.first && LS.second.getPointer() == S;
14750 });
14751 ValuesAtScopesUsers.erase(ScopeUserIt);
14752 }
14753
14754 auto BEUsersIt = BECountUsers.find(S);
14755 if (BEUsersIt != BECountUsers.end()) {
14756 // Work on a copy, as forgetBackedgeTakenCounts() will modify the original.
14757 auto Copy = BEUsersIt->second;
14758 for (const auto &Pair : Copy)
14759 forgetBackedgeTakenCounts(Pair.getPointer(), Pair.getInt());
14760 BECountUsers.erase(BEUsersIt);
14761 }
14762
14763 auto FoldUser = FoldCacheUser.find(S);
14764 if (FoldUser != FoldCacheUser.end())
14765 for (auto &KV : FoldUser->second)
14766 FoldCache.erase(KV);
14767 FoldCacheUser.erase(S);
14768}
14769
14770void
14771ScalarEvolution::getUsedLoops(const SCEV *S,
14772 SmallPtrSetImpl<const Loop *> &LoopsUsed) {
14773 struct FindUsedLoops {
14774 FindUsedLoops(SmallPtrSetImpl<const Loop *> &LoopsUsed)
14775 : LoopsUsed(LoopsUsed) {}
14776 SmallPtrSetImpl<const Loop *> &LoopsUsed;
14777 bool follow(const SCEV *S) {
14778 if (auto *AR = dyn_cast<SCEVAddRecExpr>(S))
14779 LoopsUsed.insert(AR->getLoop());
14780 return true;
14781 }
14782
14783 bool isDone() const { return false; }
14784 };
14785
14786 FindUsedLoops F(LoopsUsed);
14787 SCEVTraversal<FindUsedLoops>(F).visitAll(S);
14788}
14789
14790void ScalarEvolution::getReachableBlocks(
14793 Worklist.push_back(&F.getEntryBlock());
14794 while (!Worklist.empty()) {
14795 BasicBlock *BB = Worklist.pop_back_val();
14796 if (!Reachable.insert(BB).second)
14797 continue;
14798
14799 Value *Cond;
14800 BasicBlock *TrueBB, *FalseBB;
14801 if (match(BB->getTerminator(), m_Br(m_Value(Cond), m_BasicBlock(TrueBB),
14802 m_BasicBlock(FalseBB)))) {
14803 if (auto *C = dyn_cast<ConstantInt>(Cond)) {
14804 Worklist.push_back(C->isOne() ? TrueBB : FalseBB);
14805 continue;
14806 }
14807
14808 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
14809 const SCEV *L = getSCEV(Cmp->getOperand(0));
14810 const SCEV *R = getSCEV(Cmp->getOperand(1));
14811 if (isKnownPredicateViaConstantRanges(Cmp->getCmpPredicate(), L, R)) {
14812 Worklist.push_back(TrueBB);
14813 continue;
14814 }
14815 if (isKnownPredicateViaConstantRanges(Cmp->getInverseCmpPredicate(), L,
14816 R)) {
14817 Worklist.push_back(FalseBB);
14818 continue;
14819 }
14820 }
14821 }
14822
14823 append_range(Worklist, successors(BB));
14824 }
14825}
14826
14828 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
14829 ScalarEvolution SE2(F, TLI, AC, DT, LI);
14830
14831 SmallVector<Loop *, 8> LoopStack(LI.begin(), LI.end());
14832
14833 // Map's SCEV expressions from one ScalarEvolution "universe" to another.
14834 struct SCEVMapper : public SCEVRewriteVisitor<SCEVMapper> {
14835 SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor<SCEVMapper>(SE) {}
14836
14837 const SCEV *visitConstant(const SCEVConstant *Constant) {
14838 return SE.getConstant(Constant->getAPInt());
14839 }
14840
14841 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
14842 return SE.getUnknown(Expr->getValue());
14843 }
14844
14845 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
14846 return SE.getCouldNotCompute();
14847 }
14848 };
14849
14850 SCEVMapper SCM(SE2);
14851 SmallPtrSet<BasicBlock *, 16> ReachableBlocks;
14852 SE2.getReachableBlocks(ReachableBlocks, F);
14853
14854 auto GetDelta = [&](const SCEV *Old, const SCEV *New) -> const SCEV * {
14855 if (containsUndefs(Old) || containsUndefs(New)) {
14856 // SCEV treats "undef" as an unknown but consistent value (i.e. it does
14857 // not propagate undef aggressively). This means we can (and do) fail
14858 // verification in cases where a transform makes a value go from "undef"
14859 // to "undef+1" (say). The transform is fine, since in both cases the
14860 // result is "undef", but SCEV thinks the value increased by 1.
14861 return nullptr;
14862 }
14863
14864 // Unless VerifySCEVStrict is set, we only compare constant deltas.
14865 const SCEV *Delta = SE2.getMinusSCEV(Old, New);
14866 if (!VerifySCEVStrict && !isa<SCEVConstant>(Delta))
14867 return nullptr;
14868
14869 return Delta;
14870 };
14871
14872 while (!LoopStack.empty()) {
14873 auto *L = LoopStack.pop_back_val();
14874 llvm::append_range(LoopStack, *L);
14875
14876 // Only verify BECounts in reachable loops. For an unreachable loop,
14877 // any BECount is legal.
14878 if (!ReachableBlocks.contains(L->getHeader()))
14879 continue;
14880
14881 // Only verify cached BECounts. Computing new BECounts may change the
14882 // results of subsequent SCEV uses.
14883 auto It = BackedgeTakenCounts.find(L);
14884 if (It == BackedgeTakenCounts.end())
14885 continue;
14886
14887 auto *CurBECount =
14888 SCM.visit(It->second.getExact(L, const_cast<ScalarEvolution *>(this)));
14889 auto *NewBECount = SE2.getBackedgeTakenCount(L);
14890
14891 if (CurBECount == SE2.getCouldNotCompute() ||
14892 NewBECount == SE2.getCouldNotCompute()) {
14893 // NB! This situation is legal, but is very suspicious -- whatever pass
14894 // change the loop to make a trip count go from could not compute to
14895 // computable or vice-versa *should have* invalidated SCEV. However, we
14896 // choose not to assert here (for now) since we don't want false
14897 // positives.
14898 continue;
14899 }
14900
14901 if (SE.getTypeSizeInBits(CurBECount->getType()) >
14902 SE.getTypeSizeInBits(NewBECount->getType()))
14903 NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType());
14904 else if (SE.getTypeSizeInBits(CurBECount->getType()) <
14905 SE.getTypeSizeInBits(NewBECount->getType()))
14906 CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType());
14907
14908 const SCEV *Delta = GetDelta(CurBECount, NewBECount);
14909 if (Delta && !Delta->isZero()) {
14910 dbgs() << "Trip Count for " << *L << " Changed!\n";
14911 dbgs() << "Old: " << *CurBECount << "\n";
14912 dbgs() << "New: " << *NewBECount << "\n";
14913 dbgs() << "Delta: " << *Delta << "\n";
14914 std::abort();
14915 }
14916 }
14917
14918 // Collect all valid loops currently in LoopInfo.
14919 SmallPtrSet<Loop *, 32> ValidLoops;
14920 SmallVector<Loop *, 32> Worklist(LI.begin(), LI.end());
14921 while (!Worklist.empty()) {
14922 Loop *L = Worklist.pop_back_val();
14923 if (ValidLoops.insert(L).second)
14924 Worklist.append(L->begin(), L->end());
14925 }
14926 for (const auto &KV : ValueExprMap) {
14927#ifndef NDEBUG
14928 // Check for SCEV expressions referencing invalid/deleted loops.
14929 if (auto *AR = dyn_cast<SCEVAddRecExpr>(KV.second)) {
14930 assert(ValidLoops.contains(AR->getLoop()) &&
14931 "AddRec references invalid loop");
14932 }
14933#endif
14934
14935 // Check that the value is also part of the reverse map.
14936 auto It = ExprValueMap.find(KV.second);
14937 if (It == ExprValueMap.end() || !It->second.contains(KV.first)) {
14938 dbgs() << "Value " << *KV.first
14939 << " is in ValueExprMap but not in ExprValueMap\n";
14940 std::abort();
14941 }
14942
14943 if (auto *I = dyn_cast<Instruction>(&*KV.first)) {
14944 if (!ReachableBlocks.contains(I->getParent()))
14945 continue;
14946 const SCEV *OldSCEV = SCM.visit(KV.second);
14947 const SCEV *NewSCEV = SE2.getSCEV(I);
14948 const SCEV *Delta = GetDelta(OldSCEV, NewSCEV);
14949 if (Delta && !Delta->isZero()) {
14950 dbgs() << "SCEV for value " << *I << " changed!\n"
14951 << "Old: " << *OldSCEV << "\n"
14952 << "New: " << *NewSCEV << "\n"
14953 << "Delta: " << *Delta << "\n";
14954 std::abort();
14955 }
14956 }
14957 }
14958
14959 for (const auto &KV : ExprValueMap) {
14960 for (Value *V : KV.second) {
14961 const SCEV *S = ValueExprMap.lookup(V);
14962 if (!S) {
14963 dbgs() << "Value " << *V
14964 << " is in ExprValueMap but not in ValueExprMap\n";
14965 std::abort();
14966 }
14967 if (S != KV.first) {
14968 dbgs() << "Value " << *V << " mapped to " << *S << " rather than "
14969 << *KV.first << "\n";
14970 std::abort();
14971 }
14972 }
14973 }
14974
14975 // Verify integrity of SCEV users.
14976 for (const auto &S : UniqueSCEVs) {
14977 for (SCEVUse Op : S.operands()) {
14978 // We do not store dependencies of constants.
14979 if (isa<SCEVConstant>(Op))
14980 continue;
14981 auto It = SCEVUsers.find(Op);
14982 if (It != SCEVUsers.end() && It->second.count(&S))
14983 continue;
14984 dbgs() << "Use of operand " << *Op << " by user " << S
14985 << " is not being tracked!\n";
14986 std::abort();
14987 }
14988 }
14989
14990 // Verify integrity of ValuesAtScopes users.
14991 for (const auto &ValueAndVec : ValuesAtScopes) {
14992 const SCEV *Value = ValueAndVec.first;
14993 for (const auto &LoopAndValueAtScope : ValueAndVec.second) {
14994 const Loop *L = LoopAndValueAtScope.first;
14995 SCEVUse ValueAtScope = LoopAndValueAtScope.second;
14996 if (!isa<SCEVConstant>(ValueAtScope)) {
14997 auto It = ValuesAtScopesUsers.find(ValueAtScope.getPointer());
14998 if (It != ValuesAtScopesUsers.end() &&
14999 is_contained(It->second, std::make_pair(L, Value)))
15000 continue;
15001 dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
15002 << *ValueAtScope << " missing in ValuesAtScopesUsers\n";
15003 std::abort();
15004 }
15005 }
15006 }
15007
15008 for (const auto &ValueAtScopeAndVec : ValuesAtScopesUsers) {
15009 const SCEV *ValueAtScope = ValueAtScopeAndVec.first;
15010 for (const auto &LoopAndValue : ValueAtScopeAndVec.second) {
15011 const Loop *L = LoopAndValue.first;
15012 const SCEV *Value = LoopAndValue.second;
15014 auto It = ValuesAtScopes.find(Value);
15015 // The recorded value at scope may carry no-wrap flags that are not part
15016 // of the key it is recorded under.
15017 if (It != ValuesAtScopes.end() && any_of(It->second, [&](const auto &LS) {
15018 return LS.first == L && LS.second.getPointer() == ValueAtScope;
15019 }))
15020 continue;
15021 dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
15022 << *ValueAtScope << " missing in ValuesAtScopes\n";
15023 std::abort();
15024 }
15025 }
15026
15027 // Verify integrity of BECountUsers.
15028 auto VerifyBECountUsers = [&](bool Predicated) {
15029 auto &BECounts =
15030 Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts;
15031 for (const auto &LoopAndBEInfo : BECounts) {
15032 for (const ExitNotTakenInfo &ENT : LoopAndBEInfo.second.ExitNotTaken) {
15033 for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) {
15034 if (!isa<SCEVConstant>(S)) {
15035 auto UserIt = BECountUsers.find(S);
15036 if (UserIt != BECountUsers.end() &&
15037 UserIt->second.contains({ LoopAndBEInfo.first, Predicated }))
15038 continue;
15039 dbgs() << "Value " << *S << " for loop " << *LoopAndBEInfo.first
15040 << " missing from BECountUsers\n";
15041 std::abort();
15042 }
15043 }
15044 }
15045 }
15046 };
15047 VerifyBECountUsers(/* Predicated */ false);
15048 VerifyBECountUsers(/* Predicated */ true);
15049
15050 // Verify intergity of loop disposition cache.
15051 for (auto &[S, Values] : LoopDispositions) {
15052 for (auto [Loop, CachedDisposition] : Values) {
15053 const auto RecomputedDisposition = SE2.getLoopDisposition(S, Loop);
15054 if (CachedDisposition != RecomputedDisposition) {
15055 dbgs() << "Cached disposition of " << *S << " for loop " << *Loop
15056 << " is incorrect: cached " << CachedDisposition << ", actual "
15057 << RecomputedDisposition << "\n";
15058 std::abort();
15059 }
15060 }
15061 }
15062
15063 // Verify integrity of the block disposition cache.
15064 for (auto &[S, Values] : BlockDispositions) {
15065 for (auto [BB, CachedDisposition] : Values) {
15066 const auto RecomputedDisposition = SE2.getBlockDisposition(S, BB);
15067 if (CachedDisposition != RecomputedDisposition) {
15068 dbgs() << "Cached disposition of " << *S << " for block %"
15069 << BB->getName() << " is incorrect: cached " << CachedDisposition
15070 << ", actual " << RecomputedDisposition << "\n";
15071 std::abort();
15072 }
15073 }
15074 }
15075
15076 // Verify FoldCache/FoldCacheUser caches.
15077 for (auto [FoldID, Expr] : FoldCache) {
15078 auto I = FoldCacheUser.find(Expr);
15079 if (I == FoldCacheUser.end()) {
15080 dbgs() << "Missing entry in FoldCacheUser for cached expression " << *Expr
15081 << "!\n";
15082 std::abort();
15083 }
15084 if (!is_contained(I->second, FoldID)) {
15085 dbgs() << "Missing FoldID in cached users of " << *Expr << "!\n";
15086 std::abort();
15087 }
15088 }
15089 for (auto [Expr, IDs] : FoldCacheUser) {
15090 for (auto &FoldID : IDs) {
15091 const SCEV *S = FoldCache.lookup(FoldID);
15092 if (!S) {
15093 dbgs() << "Missing entry in FoldCache for expression " << *Expr
15094 << "!\n";
15095 std::abort();
15096 }
15097 if (S != Expr) {
15098 dbgs() << "Entry in FoldCache doesn't match FoldCacheUser: " << *S
15099 << " != " << *Expr << "!\n";
15100 std::abort();
15101 }
15102 }
15103 }
15104
15105 // Verify that ConstantMultipleCache computations are correct. We check that
15106 // cached multiples and recomputed multiples are multiples of each other to
15107 // verify correctness. It is possible that a recomputed multiple is different
15108 // from the cached multiple due to strengthened no wrap flags or changes in
15109 // KnownBits computations.
15110 for (auto [S, Multiple] : ConstantMultipleCache) {
15111 APInt RecomputedMultiple = SE2.getConstantMultiple(S);
15112 if ((Multiple != 0 && RecomputedMultiple != 0 &&
15113 Multiple.urem(RecomputedMultiple) != 0 &&
15114 RecomputedMultiple.urem(Multiple) != 0)) {
15115 dbgs() << "Incorrect cached computation in ConstantMultipleCache for "
15116 << *S << " : Computed " << RecomputedMultiple
15117 << " but cache contains " << Multiple << "!\n";
15118 std::abort();
15119 }
15120 }
15121}
15122
15124 Function &F, const PreservedAnalyses &PA,
15125 FunctionAnalysisManager::Invalidator &Inv) {
15126 // Invalidate the ScalarEvolution object whenever it isn't preserved or one
15127 // of its dependencies is invalidated.
15128 auto PAC = PA.getChecker<ScalarEvolutionAnalysis>();
15129 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
15130 Inv.invalidate<AssumptionAnalysis>(F, PA) ||
15131 Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
15132 Inv.invalidate<LoopAnalysis>(F, PA);
15133}
15134
15135AnalysisKey ScalarEvolutionAnalysis::Key;
15136
15139 auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
15140 auto &AC = AM.getResult<AssumptionAnalysis>(F);
15141 auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
15142 auto &LI = AM.getResult<LoopAnalysis>(F);
15143 return ScalarEvolution(F, TLI, AC, DT, LI);
15144}
15145
15151
15154 // For compatibility with opt's -analyze feature under legacy pass manager
15155 // which was not ported to NPM. This keeps tests using
15156 // update_analyze_test_checks.py working.
15157 OS << "Printing analysis 'Scalar Evolution Analysis' for function '"
15158 << F.getName() << "':\n";
15160 return PreservedAnalyses::all();
15161}
15162
15164 "Scalar Evolution Analysis", false, true)
15170 "Scalar Evolution Analysis", false, true)
15171
15172char ScalarEvolutionWrapperPass::ID = 0;
15173
15175
15177 SE.reset(new ScalarEvolution(
15179 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
15181 getAnalysis<LoopInfoWrapperPass>().getLoopInfo()));
15182 return false;
15183}
15184
15186
15188 SE->print(OS);
15189}
15190
15192 if (!VerifySCEV)
15193 return;
15194
15195 SE->verify();
15196}
15197
15205
15207 const SCEV *RHS) {
15208 return getComparePredicate(ICmpInst::ICMP_EQ, LHS, RHS);
15209}
15210
15211const SCEVPredicate *
15213 const SCEV *LHS, const SCEV *RHS) {
15215 assert(LHS->getType() == RHS->getType() &&
15216 "Type mismatch between LHS and RHS");
15217 // Unique this node based on the arguments
15218 ID.AddInteger(SCEVPredicate::P_Compare);
15219 ID.AddInteger(Pred);
15220 ID.AddPointer(LHS);
15221 ID.AddPointer(RHS);
15223 if (const auto *S = UniquePreds.lookup(ID, Token))
15224 return S;
15225 SCEVComparePredicate *Eq = new (SCEVAllocator)
15226 SCEVComparePredicate(ID.Intern(SCEVAllocator), Pred, LHS, RHS);
15227 UniquePreds.insert(Eq, Token);
15228 return Eq;
15229}
15230
15232 const SCEVAddRecExpr *AR,
15235 // Unique this node based on the arguments
15237 ID.AddPointer(AR);
15238 ID.AddInteger(AddedFlags);
15240 if (const auto *S = UniquePreds.lookup(ID, Token))
15241 return S;
15242 auto *OF = new (SCEVAllocator)
15243 SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags);
15244 UniquePreds.insert(OF, Token);
15245 return OF;
15246}
15247
15248namespace {
15249
15250class SCEVPredicateRewriter : public SCEVRewriteVisitor<SCEVPredicateRewriter> {
15251public:
15252
15253 /// Rewrites \p S in the context of a loop L and the SCEV predication
15254 /// infrastructure.
15255 ///
15256 /// If \p Pred is non-null, the SCEV expression is rewritten to respect the
15257 /// equivalences present in \p Pred.
15258 ///
15259 /// If \p NewPreds is non-null, rewrite is free to add further predicates to
15260 /// \p NewPreds such that the result will be an AddRecExpr.
15261 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
15263 const SCEVPredicate *Pred) {
15264 SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred);
15265 return Rewriter.visit(S);
15266 }
15267
15268 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
15269 if (Pred) {
15270 if (auto *U = dyn_cast<SCEVUnionPredicate>(Pred)) {
15271 for (const auto *Pred : U->getPredicates())
15272 if (const auto *IPred = dyn_cast<SCEVComparePredicate>(Pred))
15273 if (IPred->getLHS() == Expr &&
15274 IPred->getPredicate() == ICmpInst::ICMP_EQ)
15275 return IPred->getRHS();
15276 } else if (const auto *IPred = dyn_cast<SCEVComparePredicate>(Pred)) {
15277 if (IPred->getLHS() == Expr &&
15278 IPred->getPredicate() == ICmpInst::ICMP_EQ)
15279 return IPred->getRHS();
15280 }
15281 }
15282 return convertToAddRecWithPreds(Expr);
15283 }
15284
15285 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
15286 const SCEV *Operand = visit(Expr->getOperand());
15287 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
15288 if (AR && AR->getLoop() == L && AR->isAffine()) {
15289 // This couldn't be folded because the operand didn't have the nuw
15290 // flag. Add the nusw flag as an assumption that we could make.
15291 const SCEV *Step = AR->getStepRecurrence(SE);
15292 Type *Ty = Expr->getType();
15293 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNUSW))
15294 return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty),
15295 SE.getSignExtendExpr(Step, Ty), L,
15296 AR->getNoWrapFlags());
15297 }
15298 return SE.getZeroExtendExpr(Operand, Expr->getType());
15299 }
15300
15301 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
15302 const SCEV *Operand = visit(Expr->getOperand());
15303 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
15304 if (AR && AR->getLoop() == L && AR->isAffine()) {
15305 // This couldn't be folded because the operand didn't have the nsw
15306 // flag. Add the nssw flag as an assumption that we could make.
15307 const SCEV *Step = AR->getStepRecurrence(SE);
15308 Type *Ty = Expr->getType();
15309 if (addOverflowAssumption(AR, SCEVWrapPredicate::IncrementNSSW))
15310 return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty),
15311 SE.getSignExtendExpr(Step, Ty), L,
15312 AR->getNoWrapFlags());
15313 }
15314 return SE.getSignExtendExpr(Operand, Expr->getType());
15315 }
15316
15317private:
15318 explicit SCEVPredicateRewriter(
15319 const Loop *L, ScalarEvolution &SE,
15320 SmallVectorImpl<const SCEVPredicate *> *NewPreds,
15321 const SCEVPredicate *Pred)
15322 : SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {}
15323
15324 bool addOverflowAssumption(const SCEVPredicate *P) {
15325 if (!NewPreds) {
15326 // Check if we've already made this assumption.
15327 return Pred && Pred->implies(P, SE);
15328 }
15329 NewPreds->push_back(P);
15330 return true;
15331 }
15332
15333 bool addOverflowAssumption(const SCEVAddRecExpr *AR,
15335 auto *A = SE.getWrapPredicate(AR, AddedFlags);
15336 return addOverflowAssumption(A);
15337 }
15338
15339 // If \p Expr represents a PHINode, we try to see if it can be represented
15340 // as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible
15341 // to add this predicate as a runtime overflow check, we return the AddRec.
15342 // If \p Expr does not meet these conditions (is not a PHI node, or we
15343 // couldn't create an AddRec for it, or couldn't add the predicate), we just
15344 // return \p Expr.
15345 const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) {
15346 if (!isa<PHINode>(Expr->getValue()))
15347 return Expr;
15348 std::optional<
15349 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
15350 PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr);
15351 if (!PredicatedRewrite)
15352 return Expr;
15353 for (const auto *P : PredicatedRewrite->second){
15354 // Wrap predicates from outer loops are not supported.
15355 if (auto *WP = dyn_cast<const SCEVWrapPredicate>(P)) {
15356 if (L != WP->getExpr()->getLoop())
15357 return Expr;
15358 }
15359 if (!addOverflowAssumption(P))
15360 return Expr;
15361 }
15362 return PredicatedRewrite->first;
15363 }
15364
15365 SmallVectorImpl<const SCEVPredicate *> *NewPreds;
15366 const SCEVPredicate *Pred;
15367 const Loop *L;
15368};
15369
15370} // end anonymous namespace
15371
15372const SCEV *
15374 const SCEVPredicate &Preds) {
15375 return SCEVPredicateRewriter::rewrite(S, L, *this, nullptr, &Preds);
15376}
15377
15379 const SCEV *S, const Loop *L,
15382 S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds, nullptr);
15383 auto *AddRec = dyn_cast<SCEVAddRecExpr>(S);
15384
15385 if (!AddRec)
15386 return nullptr;
15387
15388 // Check if any of the transformed predicates is known to be false. In that
15389 // case, it doesn't make sense to convert to a predicated AddRec, as the
15390 // versioned loop will never execute.
15391 for (const SCEVPredicate *Pred : TransformPreds) {
15392 auto *WrapPred = dyn_cast<SCEVWrapPredicate>(Pred);
15393 if (!WrapPred || WrapPred->getFlags() != SCEVWrapPredicate::IncrementNSSW)
15394 continue;
15395
15396 const SCEVAddRecExpr *AddRecToCheck = WrapPred->getExpr();
15397 const SCEV *ExitCount = getBackedgeTakenCount(AddRecToCheck->getLoop());
15398 if (isa<SCEVCouldNotCompute>(ExitCount))
15399 continue;
15400
15401 const SCEV *Step = AddRecToCheck->getStepRecurrence(*this);
15402 if (!Step->isOne())
15403 continue;
15404
15405 ExitCount = getTruncateOrSignExtend(ExitCount, Step->getType());
15406 const SCEV *Add = getAddExpr(AddRecToCheck->getStart(), ExitCount);
15407 if (isKnownPredicate(CmpInst::ICMP_SLT, Add, AddRecToCheck->getStart()))
15408 return nullptr;
15409 }
15410
15411 // Since the transformation was successful, we can now transfer the SCEV
15412 // predicates.
15413 Preds.append(TransformPreds.begin(), TransformPreds.end());
15414
15415 return AddRec;
15416}
15417
15418/// SCEV predicates
15422
15424 const ICmpInst::Predicate Pred,
15425 const SCEV *LHS, const SCEV *RHS)
15426 : SCEVPredicate(ID, P_Compare), Pred(Pred), LHS(LHS), RHS(RHS) {
15427 assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match");
15428 assert(LHS != RHS && "LHS and RHS are the same SCEV");
15429}
15430
15432 ScalarEvolution &SE) const {
15433 const auto *Op = dyn_cast<SCEVComparePredicate>(N);
15434
15435 if (!Op)
15436 return false;
15437
15438 if (Pred != ICmpInst::ICMP_EQ)
15439 return false;
15440
15441 return Op->LHS == LHS && Op->RHS == RHS;
15442}
15443
15444bool SCEVComparePredicate::isAlwaysTrue() const { return false; }
15445
15447 if (Pred == ICmpInst::ICMP_EQ)
15448 OS.indent(Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n";
15449 else
15450 OS.indent(Depth) << "Compare predicate: " << *LHS << " " << Pred << ") "
15451 << *RHS << "\n";
15452
15453}
15454
15456 const SCEVAddRecExpr *AR,
15457 IncrementWrapFlags Flags)
15458 : SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {}
15459
15460const SCEVAddRecExpr *SCEVWrapPredicate::getExpr() const { return AR; }
15461
15463 ScalarEvolution &SE) const {
15464 const auto *Op = dyn_cast<SCEVWrapPredicate>(N);
15465 if (!Op || setFlags(Flags, Op->Flags) != Flags)
15466 return false;
15467
15468 if (Op->AR == AR)
15469 return true;
15470
15471 if (Flags != SCEVWrapPredicate::IncrementNSSW &&
15473 return false;
15474
15475 const SCEV *Start = AR->getStart();
15476 const SCEV *OpStart = Op->AR->getStart();
15477 if (Start->getType()->isPointerTy() != OpStart->getType()->isPointerTy())
15478 return false;
15479
15480 // Reject pointers to different address spaces.
15481 if (Start->getType()->isPointerTy() && Start->getType() != OpStart->getType())
15482 return false;
15483
15484 // NUSW/NSSW on a wider-type AddRec does not imply the same on a
15485 // narrower-type AddRec.
15486 if (SE.getTypeSizeInBits(AR->getType()) >
15487 SE.getTypeSizeInBits(Op->AR->getType()))
15488 return false;
15489
15490 const SCEV *Step = AR->getStepRecurrence(SE);
15491 const SCEV *OpStep = Op->AR->getStepRecurrence(SE);
15492 if (!SE.isKnownPositive(Step) || !SE.isKnownPositive(OpStep))
15493 return false;
15494
15495 // If both steps are positive, this implies N, if N's start and step are
15496 // ULE/SLE (for NSUW/NSSW) than this'.
15497 Type *WiderTy = SE.getWiderType(Step->getType(), OpStep->getType());
15498 Step = SE.getNoopOrZeroExtend(Step, WiderTy);
15499 OpStep = SE.getNoopOrZeroExtend(OpStep, WiderTy);
15500
15501 bool IsNUW = Flags == SCEVWrapPredicate::IncrementNUSW;
15502 OpStart = IsNUW ? SE.getNoopOrZeroExtend(OpStart, WiderTy)
15503 : SE.getNoopOrSignExtend(OpStart, WiderTy);
15504 Start = IsNUW ? SE.getNoopOrZeroExtend(Start, WiderTy)
15505 : SE.getNoopOrSignExtend(Start, WiderTy);
15507 return SE.isKnownPredicate(Pred, OpStep, Step) &&
15508 SE.isKnownPredicate(Pred, OpStart, Start);
15509}
15510
15512 SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags();
15513 IncrementWrapFlags IFlags = Flags;
15514
15515 if (ScalarEvolution::setFlags(ScevFlags, SCEV::FlagNSW) == ScevFlags)
15516 IFlags = clearFlags(IFlags, IncrementNSSW);
15517
15518 return IFlags == IncrementAnyWrap;
15519}
15520
15521void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const {
15522 OS.indent(Depth) << *getExpr() << " Added Flags: ";
15524 OS << "<nusw>";
15526 OS << "<nssw>";
15527 OS << "\n";
15528}
15529
15532 ScalarEvolution &SE) {
15533 IncrementWrapFlags ImpliedFlags = IncrementAnyWrap;
15534 SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags();
15535
15536 // We can safely transfer the NSW flag as NSSW.
15537 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNSW) == StaticFlags)
15538 ImpliedFlags = IncrementNSSW;
15539
15540 if (ScalarEvolution::setFlags(StaticFlags, SCEV::FlagNUW) == StaticFlags) {
15541 // If the increment is positive, the SCEV NUW flag will also imply the
15542 // WrapPredicate NUSW flag.
15543 if (const auto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE)))
15544 if (Step->getValue()->getValue().isNonNegative())
15545 ImpliedFlags = setFlags(ImpliedFlags, IncrementNUSW);
15546 }
15547
15548 return ImpliedFlags;
15549}
15550
15551/// Union predicates don't get cached so create a dummy set ID for it.
15553 ScalarEvolution &SE)
15555 for (const auto *P : Preds)
15556 add(P, SE);
15557}
15558
15560 return all_of(Preds,
15561 [](const SCEVPredicate *I) { return I->isAlwaysTrue(); });
15562}
15563
15565 ScalarEvolution &SE) const {
15566 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N))
15567 return all_of(Set->Preds, [this, &SE](const SCEVPredicate *I) {
15568 return this->implies(I, SE);
15569 });
15570
15571 if (any_of(Preds,
15572 [N, &SE](const SCEVPredicate *I) { return I->implies(N, SE); }))
15573 return true;
15574
15575 // A wrap predicate may be implied by a wrap predicate in Preds after applying
15576 // equal predicates.
15577 const auto *NWrap = dyn_cast<SCEVWrapPredicate>(N);
15578 if (!NWrap)
15579 return false;
15580 const Loop *L = NWrap->getExpr()->getLoop();
15581 return any_of(Preds, [&](const SCEVPredicate *I) {
15582 const auto *IWrap = dyn_cast<SCEVWrapPredicate>(I);
15583 if (!IWrap)
15584 return false;
15585 const auto *RewrittenAR = dyn_cast<SCEVAddRecExpr>(
15586 SE.rewriteUsingPredicate(IWrap->getExpr(), L, *this));
15587 return RewrittenAR &&
15588 SE.getWrapPredicate(RewrittenAR, IWrap->getFlags())->implies(N, SE);
15589 });
15590}
15591
15593 for (const auto *Pred : Preds)
15594 Pred->print(OS, Depth);
15595}
15596
15597void SCEVUnionPredicate::add(const SCEVPredicate *N, ScalarEvolution &SE) {
15598 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(N)) {
15599 for (const auto *Pred : Set->Preds)
15600 add(Pred, SE);
15601 return;
15602 }
15603
15604 // Implication checks are quadratic in the number of predicates. Stop doing
15605 // them if there are many predicates, as they should be too expensive to use
15606 // anyway at that point.
15607 bool CheckImplies = Preds.size() < 16;
15608
15609 // Only add predicate if it is not already implied by this union predicate.
15610 if (CheckImplies && implies(N, SE))
15611 return;
15612
15613 // Build a new vector containing the current predicates, except the ones that
15614 // are implied by the new predicate N.
15616 for (auto *P : Preds) {
15617 if (CheckImplies && N->implies(P, SE))
15618 continue;
15619 PrunedPreds.push_back(P);
15620 }
15621 Preds = std::move(PrunedPreds);
15622 Preds.push_back(N);
15623}
15624
15626 Loop &L)
15627 : SE(SE), L(L) {
15629 Preds = std::make_unique<SCEVUnionPredicate>(Empty, SE);
15630}
15631
15633 for (const SCEV *Op : Ops)
15634 // We do not expect that forgetting cached data for SCEVConstants will ever
15635 // open any prospects for sharpening or introduce any correctness issues,
15636 // so we don't bother storing their dependencies.
15637 if (!isa<SCEVConstant>(Op))
15638 SCEVUsers[Op].insert(User);
15639}
15640
15642 const SCEV *Expr = SE.getSCEV(V);
15643 return getPredicatedSCEV(Expr);
15644}
15645
15647 RewriteEntry &Entry = RewriteMap[Expr];
15648
15649 // If we already have an entry and the version matches, return it.
15650 if (Entry.second && Generation == Entry.first)
15651 return Entry.second;
15652
15653 // We found an entry but it's stale. Rewrite the stale entry
15654 // according to the current predicate.
15655 if (Entry.second)
15656 Expr = Entry.second;
15657
15658 const SCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, *Preds);
15659 Entry = {Generation, NewSCEV};
15660
15661 return NewSCEV;
15662}
15663
15665 if (!BackedgeCount) {
15667 BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, Preds);
15668 for (const auto *P : Preds)
15669 addPredicate(*P);
15670 }
15671 return BackedgeCount;
15672}
15673
15675 if (!SymbolicMaxBackedgeCount) {
15677 SymbolicMaxBackedgeCount =
15678 SE.getPredicatedSymbolicMaxBackedgeTakenCount(&L, Preds);
15679 for (const auto *P : Preds)
15680 addPredicate(*P);
15681 }
15682 return SymbolicMaxBackedgeCount;
15683}
15684
15686 if (!SmallConstantMaxTripCount) {
15688 SmallConstantMaxTripCount = SE.getSmallConstantMaxTripCount(&L, &Preds);
15689 for (const auto *P : Preds)
15690 addPredicate(*P);
15691 }
15692 return *SmallConstantMaxTripCount;
15693}
15694
15696 if (Preds->implies(&Pred, SE))
15697 return;
15698
15699 SmallVector<const SCEVPredicate *, 4> NewPreds(Preds->getPredicates());
15700 NewPreds.push_back(&Pred);
15701 Preds = std::make_unique<SCEVUnionPredicate>(NewPreds, SE);
15702 updateGeneration();
15703}
15704
15707 for (const SCEVPredicate *P : Preds)
15708 addPredicate(*P);
15709}
15710
15712 return *Preds;
15713}
15714
15715void PredicatedScalarEvolution::updateGeneration() {
15716 // If the generation number wrapped recompute everything.
15717 if (++Generation == 0) {
15718 for (auto &II : RewriteMap) {
15719 const SCEV *Rewritten = II.second.second;
15720 II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, *Preds)};
15721 }
15722 }
15723}
15724
15727 const auto *AR = dyn_cast<SCEVAddRecExpr>(getSCEV(V));
15728 if (!AR)
15729 return false;
15730
15732 Flags, SCEVWrapPredicate::getImpliedFlags(AR, SE));
15733
15735}
15736
15739 const SCEV *Expr = this->getSCEV(V);
15741 auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds);
15742
15743 if (!New)
15744 return nullptr;
15745
15746 if (ExtraPreds) {
15747 ExtraPreds->append(NewPreds);
15748 return New;
15749 }
15750
15751 addPredicates(NewPreds);
15752
15753 RewriteMap[SE.getSCEV(V)] = {Generation, New};
15754 return New;
15755}
15756
15759 : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L),
15760 Preds(std::make_unique<SCEVUnionPredicate>(Init.Preds->getPredicates(),
15761 SE)),
15762 Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) {}
15763
15765 // For each block.
15766 for (auto *BB : L.getBlocks())
15767 for (auto &I : *BB) {
15768 if (!SE.isSCEVable(I.getType()))
15769 continue;
15770
15771 auto *Expr = SE.getSCEV(&I);
15772 auto II = RewriteMap.find(Expr);
15773
15774 if (II == RewriteMap.end())
15775 continue;
15776
15777 // Don't print things that are not interesting.
15778 if (II->second.second == Expr)
15779 continue;
15780
15781 OS.indent(Depth) << "[PSE]" << I << ":\n";
15782 OS.indent(Depth + 2) << *Expr << "\n";
15783 OS.indent(Depth + 2) << "--> " << *II->second.second << "\n";
15784 }
15785}
15786
15789 BasicBlock *Header = L->getHeader();
15790 BasicBlock *Pred = L->getLoopPredecessor();
15791 LoopGuards Guards(SE);
15792 if (!Pred)
15793 return Guards;
15795 collectFromBlock(SE, Guards, Header, Pred, VisitedBlocks);
15796 return Guards;
15797}
15798
15799void ScalarEvolution::LoopGuards::collectFromPHI(
15803 unsigned Depth) {
15804 if (!SE.isSCEVable(Phi.getType()))
15805 return;
15806
15807 using MinMaxPattern = std::pair<const SCEVConstant *, SCEVTypes>;
15808 auto GetMinMaxConst = [&](unsigned IncomingIdx) -> MinMaxPattern {
15809 const BasicBlock *InBlock = Phi.getIncomingBlock(IncomingIdx);
15810 if (!VisitedBlocks.insert(InBlock).second)
15811 return {nullptr, scCouldNotCompute};
15812
15813 // Avoid analyzing unreachable blocks so that we don't get trapped
15814 // traversing cycles with ill-formed dominance or infinite cycles
15815 if (!SE.DT.isReachableFromEntry(InBlock))
15816 return {nullptr, scCouldNotCompute};
15817
15818 auto [G, Inserted] = IncomingGuards.try_emplace(InBlock, LoopGuards(SE));
15819 if (Inserted)
15820 collectFromBlock(SE, G->second, Phi.getParent(), InBlock, VisitedBlocks,
15821 Depth + 1);
15822 auto &RewriteMap = G->second.RewriteMap;
15823 if (RewriteMap.empty())
15824 return {nullptr, scCouldNotCompute};
15825 auto S = RewriteMap.find(SE.getSCEV(Phi.getIncomingValue(IncomingIdx)));
15826 if (S == RewriteMap.end())
15827 return {nullptr, scCouldNotCompute};
15828 auto *SM = dyn_cast_if_present<SCEVMinMaxExpr>(S->second);
15829 if (!SM)
15830 return {nullptr, scCouldNotCompute};
15831 if (const SCEVConstant *C0 = dyn_cast<SCEVConstant>(SM->getOperand(0)))
15832 return {C0, SM->getSCEVType()};
15833 return {nullptr, scCouldNotCompute};
15834 };
15835 auto MergeMinMaxConst = [](MinMaxPattern P1,
15836 MinMaxPattern P2) -> MinMaxPattern {
15837 auto [C1, T1] = P1;
15838 auto [C2, T2] = P2;
15839 if (!C1 || !C2 || T1 != T2)
15840 return {nullptr, scCouldNotCompute};
15841 switch (T1) {
15842 case scUMaxExpr:
15843 return {C1->getAPInt().ult(C2->getAPInt()) ? C1 : C2, T1};
15844 case scSMaxExpr:
15845 return {C1->getAPInt().slt(C2->getAPInt()) ? C1 : C2, T1};
15846 case scUMinExpr:
15847 return {C1->getAPInt().ugt(C2->getAPInt()) ? C1 : C2, T1};
15848 case scSMinExpr:
15849 return {C1->getAPInt().sgt(C2->getAPInt()) ? C1 : C2, T1};
15850 default:
15851 llvm_unreachable("Trying to merge non-MinMaxExpr SCEVs.");
15852 }
15853 };
15854 auto P = GetMinMaxConst(0);
15855 for (unsigned int In = 1; In < Phi.getNumIncomingValues(); In++) {
15856 if (!P.first)
15857 break;
15858 P = MergeMinMaxConst(P, GetMinMaxConst(In));
15859 }
15860 if (P.first) {
15861 const SCEV *LHS = SE.getSCEV(const_cast<PHINode *>(&Phi));
15862 SmallVector<SCEVUse, 2> Ops({P.first, LHS});
15863 const SCEV *RHS = SE.getMinMaxExpr(P.second, Ops);
15864 Guards.RewriteMap.insert({LHS, RHS});
15865 }
15866}
15867
15868// Return a new SCEV that modifies \p Expr to the closest number divides by
15869// \p Divisor and less or equal than Expr. For now, only handle constant
15870// Expr.
15872 const APInt &DivisorVal,
15873 ScalarEvolution &SE) {
15874 const APInt *ExprVal;
15875 if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15876 DivisorVal.isNonPositive())
15877 return Expr;
15878 APInt Rem = ExprVal->urem(DivisorVal);
15879 // return the SCEV: Expr - Expr % Divisor
15880 return SE.getConstant(*ExprVal - Rem);
15881}
15882
15883// Return a new SCEV that modifies \p Expr to the closest number divides by
15884// \p Divisor and greater or equal than Expr. For now, only handle constant
15885// Expr.
15886static const SCEV *getNextSCEVDivisibleByDivisor(const SCEV *Expr,
15887 const APInt &DivisorVal,
15888 ScalarEvolution &SE) {
15889 const APInt *ExprVal;
15890 if (!match(Expr, m_scev_APInt(ExprVal)) || ExprVal->isNegative() ||
15891 DivisorVal.isNonPositive())
15892 return Expr;
15893 APInt Rem = ExprVal->urem(DivisorVal);
15894 if (Rem.isZero())
15895 return Expr;
15896 // return the SCEV: Expr + Divisor - Expr % Divisor
15897 return SE.getConstant(*ExprVal + DivisorVal - Rem);
15898}
15899
15901 ICmpInst::Predicate Predicate, const SCEV *LHS, const SCEV *RHS,
15904 // If we have LHS == 0, check if LHS is computing a property of some unknown
15905 // SCEV %v which we can rewrite %v to express explicitly.
15907 return false;
15908 // If LHS is A % B, i.e. A % B == 0, rewrite A to (A /u B) * B to
15909 // explicitly express that.
15910 const SCEVUnknown *URemLHS = nullptr;
15911 const SCEV *URemRHS = nullptr;
15912 if (!match(LHS, m_scev_URem(m_SCEVUnknown(URemLHS), m_SCEV(URemRHS), SE)))
15913 return false;
15914
15915 const SCEV *Multiple =
15916 SE.getMulExpr(SE.getUDivExpr(URemLHS, URemRHS), URemRHS);
15917 DivInfo[URemLHS] = Multiple;
15918 if (auto *C = dyn_cast<SCEVConstant>(URemRHS))
15919 Multiples[URemLHS] = C->getAPInt();
15920 return true;
15921}
15922
15923// Check if the condition is a divisibility guard (A % B == 0).
15924static bool isDivisibilityGuard(const SCEV *LHS, const SCEV *RHS,
15925 ScalarEvolution &SE) {
15926 const SCEV *X, *Y;
15927 return match(LHS, m_scev_URem(m_SCEV(X), m_SCEV(Y), SE)) && RHS->isZero();
15928}
15929
15930// Apply divisibility by \p Divisor on MinMaxExpr with constant values,
15931// recursively. This is done by aligning up/down the constant value to the
15932// Divisor.
15933static const SCEV *applyDivisibilityOnMinMaxExpr(const SCEV *MinMaxExpr,
15934 APInt Divisor,
15935 ScalarEvolution &SE) {
15936 // Return true if \p Expr is a MinMax SCEV expression with a non-negative
15937 // constant operand. If so, return in \p SCTy the SCEV type and in \p RHS
15938 // the non-constant operand and in \p LHS the constant operand.
15939 auto IsMinMaxSCEVWithNonNegativeConstant =
15940 [&](const SCEV *Expr, SCEVTypes &SCTy, const SCEV *&LHS,
15941 const SCEV *&RHS) {
15942 if (auto *MinMax = dyn_cast<SCEVMinMaxExpr>(Expr)) {
15943 if (MinMax->getNumOperands() != 2)
15944 return false;
15945 if (auto *C = dyn_cast<SCEVConstant>(MinMax->getOperand(0))) {
15946 if (C->getAPInt().isNegative())
15947 return false;
15948 SCTy = MinMax->getSCEVType();
15949 LHS = MinMax->getOperand(0);
15950 RHS = MinMax->getOperand(1);
15951 return true;
15952 }
15953 }
15954 return false;
15955 };
15956
15957 const SCEV *MinMaxLHS = nullptr, *MinMaxRHS = nullptr;
15958 SCEVTypes SCTy;
15959 if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS,
15960 MinMaxRHS))
15961 return MinMaxExpr;
15962 auto IsMin = isa<SCEVSMinExpr>(MinMaxExpr) || isa<SCEVUMinExpr>(MinMaxExpr);
15963 assert(SE.isKnownNonNegative(MinMaxLHS) && "Expected non-negative operand!");
15964 auto *DivisibleExpr =
15965 IsMin ? getPreviousSCEVDivisibleByDivisor(MinMaxLHS, Divisor, SE)
15966 : getNextSCEVDivisibleByDivisor(MinMaxLHS, Divisor, SE);
15968 applyDivisibilityOnMinMaxExpr(MinMaxRHS, Divisor, SE), DivisibleExpr};
15969 return SE.getMinMaxExpr(SCTy, Ops);
15970}
15971
15972void ScalarEvolution::LoopGuards::collectFromBlock(
15973 ScalarEvolution &SE, ScalarEvolution::LoopGuards &Guards,
15974 const BasicBlock *Block, const BasicBlock *Pred,
15975 SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks, unsigned Depth) {
15976
15978
15979 SmallVector<SCEVUse> ExprsToRewrite;
15980 auto CollectCondition = [&](ICmpInst::Predicate Predicate, const SCEV *LHS,
15981 const SCEV *RHS,
15982 DenseMap<const SCEV *, const SCEV *> &RewriteMap,
15983 const LoopGuards &DivGuards) {
15984 // WARNING: It is generally unsound to apply any wrap flags to the proposed
15985 // replacement SCEV which isn't directly implied by the structure of that
15986 // SCEV. In particular, using contextual facts to imply flags is *NOT*
15987 // legal. See the scoping rules for flags in the header to understand why.
15988
15989 // Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
15990 // and \p FromRewritten are the same (i.e. there has been no rewrite
15991 // registered for \p From), then puts this value in the list of rewritten
15992 // expressions.
15993 auto AddRewrite = [&](const SCEV *From, const SCEV *FromRewritten,
15994 const SCEV *To) {
15995 if (From == FromRewritten)
15996 ExprsToRewrite.push_back(From);
15997 RewriteMap[From] = To;
15998 };
15999
16000 // Checks whether \p S has already been rewritten. In that case returns the
16001 // existing rewrite because we want to chain further rewrites onto the
16002 // already rewritten value. Otherwise returns \p S.
16003 auto GetMaybeRewritten = [&](const SCEV *S) {
16004 return RewriteMap.lookup_or(S, S);
16005 };
16006
16007 // Check for a condition of the form (-C1 + X < C2). InstCombine will
16008 // create this form when combining two checks of the form (X u< C2 + C1) and
16009 // (X >=u C1).
16010 auto MatchRangeCheckIdiom = [&](ICmpInst::Predicate Pred,
16011 const SCEV *MatchLHS,
16012 const SCEV *MatchRHS) {
16013 const SCEVConstant *C1;
16014 const SCEVUnknown *LHSUnknown;
16015 auto *C2 = dyn_cast<SCEVConstant>(MatchRHS);
16016 if (!match(MatchLHS,
16017 m_scev_Add(m_SCEVConstant(C1), m_SCEVUnknown(LHSUnknown))) ||
16018 !C2)
16019 return false;
16020
16021 auto ExactRegion =
16022 ConstantRange::makeExactICmpRegion(Pred, C2->getAPInt())
16023 .sub(C1->getAPInt());
16024
16025 // Tighten the raw range with what we already know about LHSUnknown
16026 // from prior guards recorded in RewriteMap, or from SCEV's own range
16027 // analysis.
16028 const SCEV *RewrittenLHS = GetMaybeRewritten(LHSUnknown);
16029 ExactRegion = ExactRegion.intersectWith(SE.getUnsignedRange(RewrittenLHS),
16031
16032 // Bail if the guard is inconsistent with prior facts, or if the range
16033 // is still not a monotonic non-wrapping interval after tightening.
16034 if (ExactRegion.isEmptySet() || ExactRegion.isWrappedSet() ||
16035 ExactRegion.isFullSet())
16036 return false;
16037
16038 const SCEV *RegionMin = SE.getConstant(ExactRegion.getUnsignedMin());
16039 const SCEV *RegionMax = SE.getConstant(ExactRegion.getUnsignedMax());
16040 const SCEV *ClampedLHS =
16041 SE.getUMaxExpr(RegionMin, SE.getUMinExpr(RewrittenLHS, RegionMax));
16042 AddRewrite(LHSUnknown, RewrittenLHS, ClampedLHS);
16043 return true;
16044 };
16045 if (MatchRangeCheckIdiom(Predicate, LHS, RHS))
16046 return;
16047
16048 // Do not apply information for constants or if RHS contains an AddRec.
16050 return;
16051
16052 // If RHS is SCEVUnknown, make sure the information is applied to it.
16054 std::swap(LHS, RHS);
16056 }
16057
16058 const SCEV *RewrittenLHS = GetMaybeRewritten(LHS);
16059 // Apply divisibility information when computing the constant multiple.
16060 const APInt &DividesBy =
16061 SE.getConstantMultiple(DivGuards.rewrite(RewrittenLHS));
16062
16063 // Collect rewrites for LHS and its transitive operands based on the
16064 // condition.
16065 // For min/max expressions, also apply the guard to its operands:
16066 // 'min(a, b) >= c' -> '(a >= c) and (b >= c)',
16067 // 'min(a, b) > c' -> '(a > c) and (b > c)',
16068 // 'max(a, b) <= c' -> '(a <= c) and (b <= c)',
16069 // 'max(a, b) < c' -> '(a < c) and (b < c)'.
16070
16071 // We cannot express strict predicates in SCEV, so instead we replace them
16072 // with non-strict ones against plus or minus one of RHS depending on the
16073 // predicate.
16074 const SCEV *One = SE.getOne(RHS->getType());
16075 switch (Predicate) {
16076 case CmpInst::ICMP_ULT:
16077 if (RHS->getType()->isPointerTy())
16078 return;
16079 RHS = SE.getUMaxExpr(RHS, One);
16080 [[fallthrough]];
16081 case CmpInst::ICMP_SLT: {
16082 RHS = SE.getMinusSCEV(RHS, One);
16083 RHS = getPreviousSCEVDivisibleByDivisor(RHS, DividesBy, SE);
16084 break;
16085 }
16086 case CmpInst::ICMP_UGT:
16087 case CmpInst::ICMP_SGT:
16088 RHS = SE.getAddExpr(RHS, One);
16089 RHS = getNextSCEVDivisibleByDivisor(RHS, DividesBy, SE);
16090 break;
16091 case CmpInst::ICMP_ULE:
16092 case CmpInst::ICMP_SLE:
16093 RHS = getPreviousSCEVDivisibleByDivisor(RHS, DividesBy, SE);
16094 break;
16095 case CmpInst::ICMP_UGE:
16096 case CmpInst::ICMP_SGE:
16097 RHS = getNextSCEVDivisibleByDivisor(RHS, DividesBy, SE);
16098 break;
16099 default:
16100 break;
16101 }
16102
16103 SmallVector<SCEVUse, 16> Worklist(1, LHS);
16104 SmallPtrSet<const SCEV *, 16> Visited;
16105
16106 auto EnqueueOperands = [&Worklist](const SCEVNAryExpr *S) {
16107 append_range(Worklist, S->operands());
16108 };
16109
16110 while (!Worklist.empty()) {
16111 const SCEV *From = Worklist.pop_back_val();
16112 if (isa<SCEVConstant>(From))
16113 continue;
16114 if (!Visited.insert(From).second)
16115 continue;
16116 const SCEV *FromRewritten = GetMaybeRewritten(From);
16117 const SCEV *To = nullptr;
16118
16119 switch (Predicate) {
16120 case CmpInst::ICMP_ULT:
16121 case CmpInst::ICMP_ULE:
16122 To = SE.getUMinExpr(FromRewritten, RHS);
16123 if (auto *UMax = dyn_cast<SCEVUMaxExpr>(FromRewritten))
16124 EnqueueOperands(UMax);
16125 break;
16126 case CmpInst::ICMP_SLT:
16127 case CmpInst::ICMP_SLE:
16128 To = SE.getSMinExpr(FromRewritten, RHS);
16129 if (auto *SMax = dyn_cast<SCEVSMaxExpr>(FromRewritten))
16130 EnqueueOperands(SMax);
16131 break;
16132 case CmpInst::ICMP_UGT:
16133 case CmpInst::ICMP_UGE:
16134 To = SE.getUMaxExpr(FromRewritten, RHS);
16135 if (auto *UMin = dyn_cast<SCEVUMinExpr>(FromRewritten))
16136 EnqueueOperands(UMin);
16137 break;
16138 case CmpInst::ICMP_SGT:
16139 case CmpInst::ICMP_SGE:
16140 To = SE.getSMaxExpr(FromRewritten, RHS);
16141 if (auto *SMin = dyn_cast<SCEVSMinExpr>(FromRewritten))
16142 EnqueueOperands(SMin);
16143 break;
16144 case CmpInst::ICMP_EQ:
16146 To = RHS;
16147 break;
16148 case CmpInst::ICMP_NE:
16149 if (match(RHS, m_scev_Zero())) {
16150 const SCEV *OneAlignedUp =
16151 getNextSCEVDivisibleByDivisor(One, DividesBy, SE);
16152 To = SE.getUMaxExpr(FromRewritten, OneAlignedUp);
16153 } else {
16154 // LHS != RHS can be rewritten as (LHS - RHS) = UMax(1, LHS - RHS),
16155 // but creating the subtraction eagerly is expensive. Track the
16156 // inequalities in a separate map, and materialize the rewrite lazily
16157 // when encountering a suitable subtraction while re-writing.
16158 if (LHS->getType()->isPointerTy()) {
16159 LHS = SE.getPtrToAddrExpr(LHS);
16160 RHS = SE.getPtrToAddrExpr(RHS);
16162 break;
16163 }
16164 const SCEVConstant *C;
16165 const SCEV *A, *B;
16168 RHS = A;
16169 LHS = B;
16170 }
16171 if (LHS > RHS)
16172 std::swap(LHS, RHS);
16173 Guards.NotEqual.insert({LHS, RHS});
16174 continue;
16175 }
16176 break;
16177 default:
16178 break;
16179 }
16180
16181 if (To)
16182 AddRewrite(From, FromRewritten, To);
16183 }
16184 };
16185
16187 // First, collect information from assumptions dominating the loop.
16188 for (auto &AssumeVH : SE.AC.assumptions()) {
16189 if (!AssumeVH)
16190 continue;
16191 auto *AssumeI = cast<CallInst>(AssumeVH);
16192 if (!SE.DT.dominates(AssumeI, Block))
16193 continue;
16194 Terms.emplace_back(AssumeI->getOperand(0), true);
16195 }
16196
16197 // Second, collect information from llvm.experimental.guards dominating the loop.
16198 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
16199 SE.F.getParent(), Intrinsic::experimental_guard);
16200 if (GuardDecl)
16201 for (const auto *GU : GuardDecl->users())
16202 if (const auto *Guard = dyn_cast<IntrinsicInst>(GU))
16203 if (Guard->getFunction() == Block->getParent() &&
16204 SE.DT.dominates(Guard, Block))
16205 Terms.emplace_back(Guard->getArgOperand(0), true);
16206
16207 // Third, collect conditions from dominating branches. Starting at the loop
16208 // predecessor, climb up the predecessor chain, as long as there are
16209 // predecessors that can be found that have unique successors leading to the
16210 // original header.
16211 // TODO: share this logic with isLoopEntryGuardedByCond.
16212 unsigned NumCollectedConditions = 0;
16214 std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
16215 for (; Pair.first;
16216 Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
16217 VisitedBlocks.insert(Pair.second);
16218 const CondBrInst *LoopEntryPredicate =
16219 dyn_cast<CondBrInst>(Pair.first->getTerminator());
16220 if (!LoopEntryPredicate)
16221 continue;
16222
16223 Terms.emplace_back(LoopEntryPredicate->getCondition(),
16224 LoopEntryPredicate->getSuccessor(0) == Pair.second);
16225 NumCollectedConditions++;
16226
16227 // If we are recursively collecting guards stop after 2
16228 // conditions to limit compile-time impact for now.
16229 if (Depth > 0 && NumCollectedConditions == 2)
16230 break;
16231 }
16232 // Finally, if we stopped climbing the predecessor chain because
16233 // there wasn't a unique one to continue, try to collect conditions
16234 // for PHINodes by recursively following all of their incoming
16235 // blocks and try to merge the found conditions to build a new one
16236 // for the Phi.
16237 if (Pair.second->hasNPredecessorsOrMore(2) &&
16239 SmallDenseMap<const BasicBlock *, LoopGuards> IncomingGuards;
16240 for (auto &Phi : Pair.second->phis())
16241 collectFromPHI(SE, Guards, Phi, VisitedBlocks, IncomingGuards, Depth);
16242 }
16243
16244 // Now apply the information from the collected conditions to
16245 // Guards.RewriteMap. Conditions are processed in reverse order, so the
16246 // earliest conditions is processed first, except guards with divisibility
16247 // information, which are moved to the back. This ensures the SCEVs with the
16248 // shortest dependency chains are constructed first.
16250 GuardsToProcess;
16251 for (auto [Term, EnterIfTrue] : reverse(Terms)) {
16252 SmallVector<Value *, 8> Worklist;
16253 SmallPtrSet<Value *, 8> Visited;
16254 Worklist.push_back(Term);
16255 while (!Worklist.empty()) {
16256 Value *Cond = Worklist.pop_back_val();
16257 if (!Visited.insert(Cond).second)
16258 continue;
16259
16260 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
16261 auto Predicate =
16262 EnterIfTrue ? Cmp->getPredicate() : Cmp->getInversePredicate();
16263 const auto *LHS = SE.getSCEV(Cmp->getOperand(0));
16264 const auto *RHS = SE.getSCEV(Cmp->getOperand(1));
16265 // If LHS is a constant, apply information to the other expression.
16266 // TODO: If LHS is not a constant, check if using CompareSCEVComplexity
16267 // can improve results.
16268 if (isa<SCEVConstant>(LHS)) {
16269 std::swap(LHS, RHS);
16271 }
16272 GuardsToProcess.emplace_back(Predicate, LHS, RHS);
16273 continue;
16274 }
16275
16276 Value *L, *R;
16277 if (EnterIfTrue ? match(Cond, m_LogicalAnd(m_Value(L), m_Value(R)))
16278 : match(Cond, m_LogicalOr(m_Value(L), m_Value(R)))) {
16279 Worklist.push_back(L);
16280 Worklist.push_back(R);
16281 }
16282 }
16283 }
16284
16285 // Process divisibility guards in reverse order to populate DivGuards early.
16286 DenseMap<const SCEV *, APInt> Multiples;
16287 LoopGuards DivGuards(SE);
16288 for (const auto &[Predicate, LHS, RHS] : GuardsToProcess) {
16289 if (!isDivisibilityGuard(LHS, RHS, SE))
16290 continue;
16291 collectDivisibilityInformation(Predicate, LHS, RHS, DivGuards.RewriteMap,
16292 Multiples, SE);
16293 }
16294
16295 for (const auto &[Predicate, LHS, RHS] : GuardsToProcess)
16296 CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap, DivGuards);
16297
16298 // Apply divisibility information last. This ensures it is applied to the
16299 // outermost expression after other rewrites for the given value.
16300 for (const auto &[K, Divisor] : Multiples) {
16301 const SCEV *DivisorSCEV = SE.getConstant(Divisor);
16302 Guards.RewriteMap[K] =
16304 Guards.rewrite(K), Divisor, SE),
16305 DivisorSCEV),
16306 DivisorSCEV);
16307 ExprsToRewrite.push_back(K);
16308 }
16309
16310 // Let the rewriter preserve NUW/NSW flags if the unsigned/signed ranges of
16311 // the replacement expressions are contained in the ranges of the replaced
16312 // expressions.
16313 Guards.PreserveNUW = true;
16314 Guards.PreserveNSW = true;
16315 for (const SCEV *Expr : ExprsToRewrite) {
16316 const SCEV *RewriteTo = Guards.RewriteMap[Expr];
16317 Guards.PreserveNUW &=
16318 SE.getUnsignedRange(Expr).contains(SE.getUnsignedRange(RewriteTo));
16319 Guards.PreserveNSW &=
16320 SE.getSignedRange(Expr).contains(SE.getSignedRange(RewriteTo));
16321 }
16322
16323 // Now that all rewrite information is collect, rewrite the collected
16324 // expressions with the information in the map. This applies information to
16325 // sub-expressions.
16326 if (ExprsToRewrite.size() > 1) {
16327 for (const SCEV *Expr : ExprsToRewrite) {
16328 const SCEV *RewriteTo = Guards.RewriteMap[Expr];
16329 Guards.RewriteMap.erase(Expr);
16330 Guards.RewriteMap.insert({Expr, Guards.rewrite(RewriteTo)});
16331 }
16332 }
16333}
16334
16336 /// A rewriter to replace SCEV expressions in Map with the corresponding entry
16337 /// in the map. It skips AddRecExpr because we cannot guarantee that the
16338 /// replacement is loop invariant in the loop of the AddRec.
16339 class SCEVLoopGuardRewriter
16340 : public SCEVRewriteVisitor<SCEVLoopGuardRewriter> {
16343
16345
16346 public:
16347 SCEVLoopGuardRewriter(ScalarEvolution &SE,
16348 const ScalarEvolution::LoopGuards &Guards)
16349 : SCEVRewriteVisitor(SE), Map(Guards.RewriteMap),
16350 NotEqual(Guards.NotEqual) {
16351 if (Guards.PreserveNUW)
16352 FlagMask = ScalarEvolution::setFlags(FlagMask, SCEV::FlagNUW);
16353 if (Guards.PreserveNSW)
16354 FlagMask = ScalarEvolution::setFlags(FlagMask, SCEV::FlagNSW);
16355 }
16356
16357 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; }
16358
16359 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
16360 return Map.lookup_or(Expr, Expr);
16361 }
16362
16363 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
16364 if (const SCEV *S = Map.lookup(Expr))
16365 return S;
16366
16367 // If we didn't find the extact ZExt expr in the map, check if there's
16368 // an entry for a smaller ZExt we can use instead.
16369 Type *Ty = Expr->getType();
16370 const SCEV *Op = Expr->getOperand(0);
16371 unsigned Bitwidth = Ty->getScalarSizeInBits() / 2;
16372 while (Bitwidth % 8 == 0 && Bitwidth >= 8 &&
16373 Bitwidth > Op->getType()->getScalarSizeInBits()) {
16374 Type *NarrowTy = IntegerType::get(SE.getContext(), Bitwidth);
16375 auto *NarrowExt = SE.getZeroExtendExpr(Op, NarrowTy);
16376 if (const SCEV *S = Map.lookup(NarrowExt))
16377 return SE.getZeroExtendExpr(S, Ty);
16378 Bitwidth = Bitwidth / 2;
16379 }
16380
16382 Expr);
16383 }
16384
16385 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
16386 if (const SCEV *S = Map.lookup(Expr))
16387 return S;
16389 Expr);
16390 }
16391
16392 const SCEV *visitUMinExpr(const SCEVUMinExpr *Expr) {
16393 if (const SCEV *S = Map.lookup(Expr))
16394 return S;
16396 }
16397
16398 const SCEV *visitSMinExpr(const SCEVSMinExpr *Expr) {
16399 if (const SCEV *S = Map.lookup(Expr))
16400 return S;
16402 }
16403
16404 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
16405 if (const SCEV *S = Map.lookup(Expr))
16406 return S;
16407
16408 // Helper to check if S is a subtraction (A - B) where A != B, and if so,
16409 // return UMax(S, 1).
16410 auto RewriteSubtraction = [&](const SCEV *S) -> const SCEV * {
16411 SCEVUse LHS, RHS;
16412 if (MatchBinarySub(S, LHS, RHS)) {
16413 if (LHS > RHS)
16414 std::swap(LHS, RHS);
16415 if (NotEqual.contains({LHS, RHS})) {
16416 const SCEV *OneAlignedUp = getNextSCEVDivisibleByDivisor(
16417 SE.getOne(S->getType()), SE.getConstantMultiple(S), SE);
16418 return SE.getUMaxExpr(OneAlignedUp, S);
16419 }
16420 }
16421 return nullptr;
16422 };
16423
16424 // Check if Expr itself is a subtraction pattern with guard info.
16425 if (const SCEV *Rewritten = RewriteSubtraction(Expr))
16426 return Rewritten;
16427
16428 // Trip count expressions sometimes consist of adding 3 operands, i.e.
16429 // (Const + A + B). There may be guard info for A + B, and if so, apply
16430 // it.
16431 // TODO: Could more generally apply guards to Add sub-expressions.
16432 if (isa<SCEVConstant>(Expr->getOperand(0))) {
16433 if (Expr->getNumOperands() == 3) {
16434 const SCEV *Add =
16435 SE.getAddExpr(Expr->getOperand(1), Expr->getOperand(2));
16436 if (const SCEV *Rewritten = RewriteSubtraction(Add))
16437 return SE.getAddExpr(
16438 Expr->getOperand(0), Rewritten,
16439 ScalarEvolution::maskFlags(Expr->getNoWrapFlags(), FlagMask));
16440 if (const SCEV *S = Map.lookup(Add))
16441 return SE.getAddExpr(Expr->getOperand(0), S);
16442 }
16443
16444 // For expressions of the form (Const + A), check if we have guard info
16445 // for (Const + 1 + A), and rewrite to ((Const + 1 + A) - 1). This makes
16446 // sure we don't lose information when rewriting expressions based on
16447 // back-edge taken counts in some cases.
16448 if (Expr->getNumOperands() == 2) {
16449 const SCEV *S = nullptr;
16450 // Handle (-1 + 1 + A) without constructing SCEVs.
16451 if (match(Expr->getOperand(0), m_scev_AllOnes())) {
16452 S = Map.lookup(Expr->getOperand(1));
16453 } else {
16454 const SCEV *NewC =
16455 SE.getAddExpr(Expr->getOperand(0), SE.getOne(Expr->getType()));
16456 S = Map.lookup(SE.getAddExpr(NewC, Expr->getOperand(1)));
16457 }
16458 if (S)
16459 return SE.getAddExpr(S, SE.getMinusOne(Expr->getType()));
16460 }
16461 }
16463 bool Changed = false;
16464 for (SCEVUse Op : Expr->operands()) {
16465 Operands.push_back(
16467 Changed |= Op != Operands.back();
16468 }
16469 // We are only replacing operands with equivalent values, so transfer the
16470 // flags from the original expression.
16471 return !Changed ? Expr
16472 : SE.getAddExpr(Operands,
16474 Expr->getNoWrapFlags(), FlagMask));
16475 }
16476
16477 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
16479 bool Changed = false;
16480 for (SCEVUse Op : Expr->operands()) {
16481 Operands.push_back(
16483 Changed |= Op != Operands.back();
16484 }
16485 // We are only replacing operands with equivalent values, so transfer the
16486 // flags from the original expression.
16487 return !Changed ? Expr
16488 : SE.getMulExpr(Operands,
16490 Expr->getNoWrapFlags(), FlagMask));
16491 }
16492 };
16493
16494 if (RewriteMap.empty() && NotEqual.empty())
16495 return Expr;
16496
16497 SCEVLoopGuardRewriter Rewriter(SE, *this);
16498 return Rewriter.visit(Expr);
16499}
16500
16501const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
16502 return applyLoopGuards(Expr, LoopGuards::collect(L, *this));
16503}
16504
16506 const LoopGuards &Guards) {
16507 return Guards.rewrite(Expr);
16508}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
constexpr LLT S1
Rewrite undef for PHI
This file implements a class to represent arbitrary precision integral constant values and operations...
@ PostInc
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:678
This file contains the declarations for the subclasses of Constant, which represent the different fla...
SmallPtrSet< const BasicBlock *, 8 > VisitedBlocks
This file defines the DenseMap class.
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
static bool isSigned(unsigned Opcode)
This file defines a hash set that can be used to remove duplication of nodes in a graph.
#define op(i)
Hexagon Common GEP
Value * getPointer(Value *Ptr)
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This defines the Use class.
iv Induction Variable Users
Definition IVUsers.cpp:48
static bool hasNoUnsignedWrap(BinaryOperator &I)
static constexpr Value * getValue(Ty &ValueOrUse)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition Lint.cpp:540
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define T
#define T1
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
ppc ctr loops verify
PowerPC Reduce CR logical Operation
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static DominatorTree getDomTree(Function &F)
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
SI Fold Operands
SI optimize exec mask operations pre RA
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
This file contains some templates that are useful if you are working with the STL at all.
This file provides utility classes that use RAII to save and restore values.
bool SCEVMinMaxExprContains(const SCEV *Root, const SCEV *OperandToFind, SCEVTypes RootKind)
static cl::opt< unsigned > MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden, cl::desc("Max coefficients in AddRec during evolving"), cl::init(8))
static cl::opt< unsigned > RangeIterThreshold("scev-range-iter-threshold", cl::Hidden, cl::desc("Threshold for switching to iteratively computing SCEV ranges"), cl::init(32))
static const Loop * isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI)
static unsigned getConstantTripCount(const SCEVConstant *ExitCount)
static int CompareValueComplexity(const LoopInfo *const LI, Value *LV, Value *RV, unsigned Depth)
Compare the two values LV and RV in terms of their "complexity" where "complexity" is a partial (and ...
static const SCEV * getNextSCEVDivisibleByDivisor(const SCEV *Expr, const APInt &DivisorVal, ScalarEvolution &SE)
static void PushLoopPHIs(const Loop *L, SmallVectorImpl< Instruction * > &Worklist, SmallPtrSetImpl< Instruction * > &Visited)
Push PHI nodes in the header of the given loop onto the given Worklist.
static void insertFoldCacheEntry(const ScalarEvolution::FoldID &ID, const SCEV *S, DenseMap< ScalarEvolution::FoldID, const SCEV * > &FoldCache, DenseMap< const SCEV *, SmallVector< ScalarEvolution::FoldID, 2 > > &FoldCacheUser)
static cl::opt< bool > ClassifyExpressions("scalar-evolution-classify-expressions", cl::Hidden, cl::init(true), cl::desc("When printing analysis, include information on every instruction"))
static bool hasHugeExpression(ArrayRef< SCEVUse > Ops)
Returns true if Ops contains a huge SCEV (the subtree of S contains at least HugeExprThreshold nodes)...
static cl::opt< unsigned > AddOpsInlineThreshold("scev-addops-inline-threshold", cl::Hidden, cl::desc("Threshold for inlining addition operands into a SCEV"), cl::init(500))
static cl::opt< unsigned > MaxLoopGuardCollectionDepth("scalar-evolution-max-loop-guard-collection-depth", cl::Hidden, cl::desc("Maximum depth for recursive loop guard collection"), cl::init(1))
static SCEV::NoWrapFlags getNoWrapFlagsForGEP(GEPOperator *GEP, const SCEV *Accum, ScalarEvolution &SE)
static cl::opt< bool > VerifyIR("scev-verify-ir", cl::Hidden, cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"), cl::init(false))
static bool RangeRefPHIAllowedOperands(DominatorTree &DT, PHINode *PHI)
static bool IsKnownPredicateViaAddRecMonotonicity(ScalarEvolution &SE, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Is LHS Pred RHS true because one of them is an AddRec that is known not to go below its own start val...
static std::optional< APInt > MinOptional(std::optional< APInt > X, std::optional< APInt > Y)
Helper function to compare optional APInts: (a) if X and Y both exist, return min(X,...
static PHINode * getConstantEvolvingPHI(Value *V, const Loop *L, const TargetLibraryInfo *TLI)
getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node in the loop that V is deri...
static bool canConstantFold(const Instruction *I, const TargetLibraryInfo *TLI)
Return true if we can constant fold an instruction of the specified type, assuming that all operands ...
static cl::opt< unsigned > MulOpsInlineThreshold("scev-mulops-inline-threshold", cl::Hidden, cl::desc("Threshold for inlining multiplication operands into a SCEV"), cl::init(32))
static BinaryOperator * getCommonInstForPHI(PHINode *PN)
static PHINode * getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L, DenseMap< Instruction *, PHINode * > &PHIMap, const TargetLibraryInfo *TLI, unsigned Depth)
getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by recursing through each instructi...
static bool isDivisibilityGuard(const SCEV *LHS, const SCEV *RHS, ScalarEvolution &SE)
static std::optional< const SCEV * > createNodeForSelectViaUMinSeq(ScalarEvolution *SE, const SCEV *CondExpr, const SCEV *TrueExpr, const SCEV *FalseExpr)
static Constant * BuildConstantFromSCEV(const SCEV *V)
This builds up a Constant using the ConstantExpr interface.
static ConstantInt * EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, ScalarEvolution &SE)
static const SCEV * BinomialCoefficient(const SCEV *It, unsigned K, ScalarEvolution &SE, Type *ResultTy)
Compute BC(It, K). The result has width W. Assume, K > 0.
static cl::opt< unsigned > MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden, cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"), cl::init(8))
static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr, const SCEV *Candidate)
Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values?
static const SCEV * SolveLinEquationWithOverflow(const APInt &A, const SCEV *B, SmallVectorImpl< const SCEVPredicate * > *Predicates, ScalarEvolution &SE, const Loop *L)
Finds the minimum unsigned root of the following equation:
static cl::opt< unsigned > MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, cl::desc("Maximum number of iterations SCEV will " "symbolically execute a constant " "derived loop"), cl::init(100))
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
static void PrintSCEVWithTypeHint(raw_ostream &OS, const SCEV *S)
When printing a top-level SCEV for trip counts, it's helpful to include a type for constants which ar...
static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, const Loop *L)
static SCEV::NoWrapFlags StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type, ArrayRef< SCEVUse > Ops, SCEV::NoWrapFlags Flags)
static bool containsConstantInAddMulChain(const SCEV *StartExpr)
Determine if any of the operands in this SCEV are a constant or if any of the add or multiply express...
static const SCEV * getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty, ScalarEvolution *SE, unsigned Depth)
static bool CollectAddOperandsWithScales(SmallDenseMap< SCEVUse, APInt, 16 > &M, SmallVectorImpl< SCEVUse > &NewOps, APInt &AccumulatedConstant, ArrayRef< SCEVUse > Ops, const APInt &Scale, ScalarEvolution &SE)
Process the given Ops list, which is a list of operands to be added under the given scale,...
static const SCEV * constantFoldAndGroupOps(ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT, SmallVectorImpl< SCEVUse > &Ops, FoldT Fold, IsIdentityT IsIdentity, IsAbsorberT IsAbsorber)
Performs a number of common optimizations on the passed Ops.
static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
static const SCEV * getPreStartForExtend(const SCEVAddRecExpr *AR, ScalarEvolution *SE, unsigned Depth)
static void GroupByComplexity(SmallVectorImpl< SCEVUse > &Ops, LoopInfo *LI, DominatorTree &DT)
Given a list of SCEV objects, order them by their complexity, and group objects of the same complexit...
static bool collectDivisibilityInformation(ICmpInst::Predicate Predicate, const SCEV *LHS, const SCEV *RHS, DenseMap< const SCEV *, const SCEV * > &DivInfo, DenseMap< const SCEV *, APInt > &Multiples, ScalarEvolution &SE)
static cl::opt< unsigned > MaxSCEVOperationsImplicationDepth("scalar-evolution-max-scev-operations-implication-depth", cl::Hidden, cl::desc("Maximum depth of recursive SCEV operations implication analysis"), cl::init(2))
static void PushDefUseChildren(Instruction *I, SmallVectorImpl< Instruction * > &Worklist, SmallPtrSetImpl< Instruction * > &Visited)
Push users of the given Instruction onto the given Worklist.
static std::optional< APInt > SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec, const ConstantRange &Range, ScalarEvolution &SE)
Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n iterations.
static cl::opt< bool > UseContextForNoWrapFlagInference("scalar-evolution-use-context-for-no-wrap-flag-strenghening", cl::Hidden, cl::desc("Infer nuw/nsw flags using context where suitable"), cl::init(true))
static cl::opt< bool > EnableFiniteLoopControl("scalar-evolution-finite-loop", cl::Hidden, cl::desc("Handle <= and >= in finite loops"), cl::init(true))
static bool getOperandsForSelectLikePHI(DominatorTree &DT, PHINode *PN, Value *&Cond, Value *&LHS, Value *&RHS)
static std::optional< std::tuple< APInt, APInt, APInt, APInt, unsigned > > GetQuadraticEquation(const SCEVAddRecExpr *AddRec)
For a given quadratic addrec, generate coefficients of the corresponding quadratic equation,...
static bool isKnownPredicateExtendIdiom(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
static std::optional< BinaryOp > MatchBinaryOp(Value *V, const DataLayout &DL, AssumptionCache &AC, const DominatorTree &DT, const Instruction *CxtI)
Try to map V into a BinaryOp, and return std::nullopt on failure.
static std::optional< APInt > SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE)
Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n iterations.
static std::optional< APInt > TruncIfPossible(std::optional< APInt > X, unsigned BitWidth)
Helper function to truncate an optional APInt to a given BitWidth.
static cl::opt< unsigned > MaxSCEVCompareDepth("scalar-evolution-max-scev-compare-depth", cl::Hidden, cl::desc("Maximum depth of recursive SCEV complexity comparisons"), cl::init(32))
static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, const SCEVConstant *ConstantTerm, const SCEVAddExpr *WholeAddExpr)
static cl::opt< unsigned > MaxConstantEvolvingDepth("scalar-evolution-max-constant-evolving-depth", cl::Hidden, cl::desc("Maximum depth of recursive constant evolving"), cl::init(32))
static bool canConstantEvolve(Instruction *I, const Loop *L, const TargetLibraryInfo *TLI)
Determine whether this instruction can constant evolve within this loop assuming its operands can all...
static bool MatchBinarySub(const SCEV *S, SCEVUse &LHS, SCEVUse &RHS)
static std::optional< ConstantRange > GetRangeFromMetadata(Value *V)
Helper method to assign a range to V from metadata present in the IR.
static cl::opt< unsigned > HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden, cl::desc("Size of the expression which is considered huge"), cl::init(4096))
static Type * isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI, bool &Signed, ScalarEvolution &SE)
Helper function to createAddRecFromPHIWithCasts.
static Constant * EvaluateExpression(Value *V, const Loop *L, DenseMap< Instruction *, Constant * > &Vals, const DataLayout &DL, const TargetLibraryInfo *TLI)
EvaluateExpression - Given an expression that passes the getConstantEvolvingPHI predicate,...
static const SCEV * getPreviousSCEVDivisibleByDivisor(const SCEV *Expr, const APInt &DivisorVal, ScalarEvolution &SE)
static const SCEV * MatchNotExpr(const SCEV *Expr)
If Expr computes ~A, return A else return nullptr.
static std::pair< ConstantRange, bool > getRangeForAffineARHelper(APInt Step, const ConstantRange &StartRange, const APInt &MaxBECount, bool Signed)
static cl::opt< unsigned > MaxValueCompareDepth("scalar-evolution-max-value-compare-depth", cl::Hidden, cl::desc("Maximum depth of recursive value complexity comparisons"), cl::init(2))
static const SCEV * applyDivisibilityOnMinMaxExpr(const SCEV *MinMaxExpr, APInt Divisor, ScalarEvolution &SE)
static cl::opt< bool, true > VerifySCEVOpt("verify-scev", cl::Hidden, cl::location(VerifySCEV), cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"))
static const SCEV * getSignedOverflowLimitForStep(const SCEV *Step, ICmpInst::Predicate *Pred, ScalarEvolution *SE)
static cl::opt< unsigned > MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden, cl::desc("Maximum depth of recursive arithmetics"), cl::init(32))
static bool HasSameValue(const SCEV *A, const SCEV *B)
SCEV structural equivalence is usually sufficient for testing whether two expressions are equal,...
static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow)
Compute the result of "n choose k", the binomial coefficient.
static std::optional< int > CompareSCEVComplexity(const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS, DominatorTree &DT, unsigned Depth=0)
static bool scevUnconditionallyPropagatesPoisonFromOperands(SCEVTypes Kind)
static cl::opt< bool > VerifySCEVStrict("verify-scev-strict", cl::Hidden, cl::desc("Enable stricter verification with -verify-scev is passed"))
static Constant * getOtherIncomingValue(PHINode *PN, BasicBlock *BB)
static cl::opt< bool > UseExpensiveRangeSharpening("scalar-evolution-use-expensive-range-sharpening", cl::Hidden, cl::init(false), cl::desc("Use more powerful methods of sharpening expression ranges. May " "be costly in terms of compile time"))
static const SCEV * getUnsignedOverflowLimitForStep(const SCEV *Step, ICmpInst::Predicate *Pred, ScalarEvolution *SE)
static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Is LHS Pred RHS true on the virtue of LHS or RHS being a Min or Max expression?
static bool BrPHIToSelect(DominatorTree &DT, CondBrInst *BI, PHINode *Merge, Value *&C, Value *&LHS, Value *&RHS)
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
static bool InBlock(const Value *V, const BasicBlock *BB)
Provides some synthesis utilities to produce sequences of values.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
Virtual Register Rewriter
Value * RHS
Value * LHS
BinaryOperator * Mul
static const uint32_t IV[8]
Definition blake3_impl.h:83
SCEVCastSinkingRewriter(ScalarEvolution &SE, Type *TargetTy, ConversionFn CreatePtrCast)
static const SCEV * rewrite(const SCEV *Scev, ScalarEvolution &SE, Type *TargetTy, ConversionFn CreatePtrCast)
const SCEV * visitUnknown(const SCEVUnknown *Expr)
const SCEV * visitAddExpr(const SCEVAddExpr *Expr)
const SCEV * visit(const SCEV *S)
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:2009
LLVM_ABI APInt zext(unsigned width) const
Zero extend to a new width.
Definition APInt.cpp:1057
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:420
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1561
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1533
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:970
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:203
APInt abs() const
Get the absolute value.
Definition APInt.h:1816
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1206
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:368
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1187
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:377
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition APInt.h:463
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1695
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1509
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1116
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:206
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:213
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:326
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1171
LLVM_ABI APInt uadd_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1973
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:216
bool isNonPositive() const
Determine if this APInt Value is non-positive (<= 0).
Definition APInt.h:358
unsigned countTrailingZeros() const
Definition APInt.h:1668
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:353
unsigned logBase2() const
Definition APInt.h:1782
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition APInt.h:472
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:830
LLVM_ABI APInt multiplicativeInverse() const
Definition APInt.cpp:1303
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1155
LLVM_ABI APInt sext(unsigned width) const
Sign extend to a new width.
Definition APInt.cpp:1030
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:876
bool isPowerOf2() const
Check if this APInt's value is a power of two greater than zero.
Definition APInt.h:437
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition APInt.h:303
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
Definition APInt.h:338
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1135
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:197
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
Definition APInt.h:429
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1242
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:236
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1226
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
AnalysisUsage & addRequiredTransitive()
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:130
size_t size() const
Get the array size.
Definition ArrayRef.h:141
iterator begin() const
Definition ArrayRef.h:129
A function analysis which provides an AssumptionCache.
An immutable pass that tracks lazily created AssumptionCache objects.
A cache of @llvm.assume calls within a function.
MutableArrayRef< WeakVH > assumptions()
Access the list of assumption handles currently tracked for this function.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:446
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
const Instruction & front() const
Definition BasicBlock.h:469
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
LLVM_ABI unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:409
This class represents a function call, abstracting a target machine's calling convention.
virtual void deleted()
Callback for Value destruction.
void setValPtr(Value *P)
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:512
This class is the base class for the comparison instructions.
Definition InstrTypes.h:728
bool isFalseWhenEqual() const
This is just a convenience.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ ICMP_NE
not equal
Definition InstrTypes.h:762
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
bool isSigned() const
Definition InstrTypes.h:993
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
bool isTrueWhenEqual() const
This is just a convenience.
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
bool isUnsigned() const
Definition InstrTypes.h:999
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition InstrTypes.h:989
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
Conditional Branch instruction.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned i) const
static LLVM_ABI Constant * getNot(Constant *C)
static Constant * getPtrAdd(Constant *Ptr, Constant *Offset, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReduced=nullptr)
Create a getelementptr i8, ptr, offset constant expression.
Definition Constants.h:1501
static LLVM_ABI Constant * getPtrToAddr(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getNeg(Constant *C, bool HasNSW=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
This is the shared class of boolean and integer constants.
Definition Constants.h:87
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition Constants.h:219
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition Constants.h:159
static LLVM_ABI ConstantInt * getBool(LLVMContext &Context, bool V)
This class represents a range of values.
LLVM_ABI ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
LLVM_ABI ConstantRange zextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
LLVM_ABI bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
const APInt & getLower() const
Return the lower value for this range.
LLVM_ABI ConstantRange urem(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned remainder operation of...
LLVM_ABI bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
LLVM_ABI ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
LLVM_ABI bool isSignWrappedSet() const
Return true if this set wraps around the signed domain.
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI bool isWrappedSet() const
Return true if this set wraps around the unsigned domain.
LLVM_ABI void print(raw_ostream &OS) const
Print out the bounds to a stream.
LLVM_ABI ConstantRange truncate(uint32_t BitWidth, unsigned NoWrapKind=0) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
LLVM_ABI ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
const APInt & getUpper() const
Return the upper value for this range.
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
static LLVM_ABI ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind)
Produce the largest range containing all X such that "X BinOp Y" is guaranteed not to wrap (overflow)...
LLVM_ABI unsigned getMinSignedBits() const
Compute the maximal number of bits needed to represent every value in this signed range.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
LLVM_ABI ConstantRange sextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
static LLVM_ABI ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
This is an important base class in LLVM.
Definition Constant.h:43
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:791
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:285
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:258
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:348
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:168
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive,...
Definition DenseMap.h:271
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition DenseMap.h:254
iterator end()
Definition DenseMap.h:176
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition DenseMap.h:249
void swap(DerivedT &RHS)
Definition DenseMap.h:486
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:319
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:277
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
LLVM_ABI bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a single (scalar) element from a VectorType value.
This instruction extracts a struct member or array element value from an aggregate value.
Insertion token: a failed lookup fills it in, the matching insert consumes it.
Definition FoldingSet.h:284
This class describes a reference to an interned FoldingSetNodeID, which can be a useful to store node...
Definition FoldingSet.h:123
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:162
void AddInteger(signed I)
Definition FoldingSet.h:190
This class represents a freeze function that returns random concrete value if an operand is either a ...
FunctionPass(char &pid)
Definition Pass.h:316
Represents flags for the getelementptr instruction/expression.
bool hasNoUnsignedSignedWrap() const
bool hasNoUnsignedWrap() const
static GEPNoWrapFlags none()
static LLVM_ABI Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
Module * getParent()
Get the module that this global value is contained inside of...
static bool isPrivateLinkage(LinkageTypes Linkage)
static bool isInternalLinkage(LinkageTypes Linkage)
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getCmpPredicate() const
static bool isGE(Predicate P)
Return true if the predicate is SGE or UGE.
CmpPredicate getSwappedCmpPredicate() const
static LLVM_ABI bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static bool isLT(Predicate P)
Return true if the predicate is SLT or ULT.
CmpPredicate getInverseCmpPredicate() const
Predicate getNonStrictCmpPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static CmpPredicate getInverseCmpPredicate(CmpPredicate Pred)
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
static bool isLE(Predicate P)
Return true if the predicate is SLE or ULE.
This instruction inserts a single (scalar) element into a VectorType value.
This instruction inserts a struct field of array element value into an aggregate value.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
LLVM_ABI bool isIdenticalToWhenDefined(const Instruction *I, bool IntersectAttrs=false) const LLVM_READONLY
This is like isIdenticalTo, except that it ignores the SubclassOptionalData flags,...
Class to represent integer types.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition Type.cpp:338
A helper class to return the specified delimiter string after the first invocation of operator String...
An instruction for reading from memory.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:594
bool contains(const LoopT *L) const
Return true if the specified loop is contained within this loop.
BlockT * getHeader() const
unsigned getLoopDepth() const
Return the nesting level of this loop.
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it.
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:619
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition LoopInfo.cpp:67
Metadata node.
Definition Metadata.h:1081
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
Definition Operator.h:113
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
Definition Operator.h:107
iterator_range< const_block_iterator > blocks() const
op_range incoming_values()
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
PointerIntPair - This class implements a pair of a pointer and small integer.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
LLVM_ABI void addPredicate(const SCEVPredicate &Pred)
Adds a new predicate.
LLVM_ABI const SCEVPredicate & getPredicate() const
LLVM_ABI const SCEV * getPredicatedSCEV(const SCEV *Expr)
Returns the rewritten SCEV for Expr in the context of the current SCEV predicate.
LLVM_ABI bool areAddRecsEqualWithPreds(const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2, ArrayRef< const SCEVPredicate * > ExtraPreds={}) const
Check if AR1 and AR2 are equal, while taking into account Equal predicates in Preds and ExtraPreds.
LLVM_ABI bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags)
Returns true if we've statically proved that V doesn't wrap.
LLVM_ABI const SCEVAddRecExpr * getAsAddRec(Value *V, SmallVectorImpl< const SCEVPredicate * > *WrapPredsAdded=nullptr)
Attempts to produce an AddRecExpr for V by adding additional SCEV predicates.
LLVM_ABI void print(raw_ostream &OS, unsigned Depth) const
Print the SCEV mappings done by the Predicated Scalar Evolution.
LLVM_ABI PredicatedScalarEvolution(ScalarEvolution &SE, Loop &L)
LLVM_ABI unsigned getSmallConstantMaxTripCount()
Returns the upper bound of the loop trip count as a normal unsigned value, or 0 if the trip count is ...
LLVM_ABI void addPredicates(ArrayRef< const SCEVPredicate * > Preds)
Adds all predicates in Preds.
LLVM_ABI const SCEV * getBackedgeTakenCount()
Get the (predicated) backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSymbolicMaxBackedgeTakenCount()
Get the (predicated) symbolic max backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
constexpr bool isValid() const
Definition Register.h:112
This node represents an addition of some number of SCEVs.
This node represents a polynomial recurrence on the trip count of the specified loop.
LLVM_ABI SCEVUse getExitValue(ScalarEvolution &SE) const
Return the value of this recurrences when its loop exits, i.e.
LLVM_ABI const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a recurrence without clearing any previously set flags.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
bool isQuadratic() const
Return true if this represents an expression A + B*x + C*x^2 where A, B and C are loop invariant valu...
LLVM_ABI const SCEV * getNumIterationsInRange(const ConstantRange &Range, ScalarEvolution &SE) const
Return the number of iterations of this loop that produce values in the specified constant range.
LLVM_ABI const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
Return an expression representing the value of this expression one iteration of the loop ahead.
SCEVUse getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
This is the base class for unary cast operator classes.
LLVM_ABI SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, SCEVUse op, Type *ty)
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a non-recurrence without clearing previously set flags.
This class represents an assumption that the expression LHS Pred RHS evaluates to true,...
SCEVComparePredicate(const FoldingSetNodeIDRef ID, const ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
bool isAlwaysTrue() const override
Returns true if the predicate is always true.
void print(raw_ostream &OS, unsigned Depth=0) const override
Prints a textual representation of this predicate with an indentation of Depth.
bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override
Implementation of the SCEVPredicate interface.
This class represents a constant integer value.
ConstantInt * getValue() const
const APInt & getAPInt() const
This is the base class for unary integral cast operator classes.
LLVM_ABI SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, SCEVUse op, Type *ty)
This node is the base class min/max selections.
static enum SCEVTypes negate(enum SCEVTypes T)
This node represents multiplication of some number of SCEVs.
This node is a base class providing common functionality for n'ary operators.
ArrayRef< SCEVUse > operands() const
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
SCEVUse getOperand(unsigned i) const
This class represents an assumption made using SCEV expressions which can be checked at run-time.
SCEVPredicate(const SCEVPredicate &)=default
virtual bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const =0
Returns true if this predicate implies N.
SCEVPredicateKind Kind
This class represents a cast from a pointer to a pointer-sized integer value, without capturing the p...
This visitor recursively visits a SCEV expression and re-writes it.
const SCEV * visitSignExtendExpr(const SCEVSignExtendExpr *Expr)
const SCEV * visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr)
const SCEV * visitSMinExpr(const SCEVSMinExpr *Expr)
const SCEV * visitUMinExpr(const SCEVUMinExpr *Expr)
This class represents a signed minimum selection.
This node is the base class for sequential/in-order min/max selections.
static SCEVTypes getEquivalentNonSequentialSCEVType(SCEVTypes Ty)
This class represents a sign extension of a small integer value to a larger integer value.
Visit all nodes in the expression tree using worklist traversal.
This class represents a truncation of an integer value to a smaller integer value.
This class represents a binary unsigned division operation.
This class represents an unsigned minimum selection.
This class represents a composition of other SCEV predicates, and is the class that most clients will...
void print(raw_ostream &OS, unsigned Depth) const override
Prints a textual representation of this predicate with an indentation of Depth.
bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override
Returns true if this predicate implies N.
SCEVUnionPredicate(ArrayRef< const SCEVPredicate * > Preds, ScalarEvolution &SE)
Union predicates don't get cached so create a dummy set ID for it.
bool isAlwaysTrue() const override
Implementation of the SCEVPredicate interface.
SCEVUnionPredicate getUnionWith(const SCEVPredicate *N, ScalarEvolution &SE) const
Returns a new SCEVUnionPredicate that is the union of this predicate and the given predicate N.
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
This class represents the value of vscale, as used when defining the length of a scalable vector or r...
This class represents an assumption made on an AddRec expression.
IncrementWrapFlags
Similar to SCEV::NoWrapFlags, but with slightly different semantics for FlagNUSW.
SCEVWrapPredicate(const FoldingSetNodeIDRef ID, const SCEVAddRecExpr *AR, IncrementWrapFlags Flags)
bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override
Returns true if this predicate implies N.
static SCEVWrapPredicate::IncrementWrapFlags setFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, SCEVWrapPredicate::IncrementWrapFlags OnFlags)
void print(raw_ostream &OS, unsigned Depth=0) const override
Prints a textual representation of this predicate with an indentation of Depth.
bool isAlwaysTrue() const override
Returns true if the predicate is always true.
const SCEVAddRecExpr * getExpr() const
Implementation of the SCEVPredicate interface.
static SCEVWrapPredicate::IncrementWrapFlags clearFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, SCEVWrapPredicate::IncrementWrapFlags OffFlags)
Convenient IncrementWrapFlags manipulation methods.
static SCEVWrapPredicate::IncrementWrapFlags getImpliedFlags(const SCEVAddRecExpr *AR, ScalarEvolution &SE)
Returns the set of SCEVWrapPredicate no wrap flags implied by a SCEVAddRecExpr.
IncrementWrapFlags getFlags() const
Returns the set assumed no overflow flags.
This class represents a zero extension of a small integer value to a larger integer value.
This class represents an analyzed expression in the program.
unsigned short getExpressionSize() const
SCEVNoWrapFlags NoWrapFlags
LLVM_ABI bool isOne() const
Return true if the expression is a constant one.
SCEV(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, unsigned short ExpressionSize, Type *Ty)
static constexpr auto FlagNUW
LLVM_ABI void computeAndSetCanonical(ScalarEvolution &SE)
Compute and set the canonical SCEV, by constructing a SCEV with the same operands,...
LLVM_ABI bool isZero() const
Return true if the expression is a constant zero.
const SCEV * CanonicalSCEV
Pointer to the canonical version of the SCEV, i.e.
static constexpr auto FlagAnyWrap
LLVM_ABI void dump() const
This method is used for debugging.
LLVM_ABI bool isAllOnesValue() const
Return true if the expression is a constant all-ones value.
LLVM_ABI bool isNonConstantNegative() const
Return true if the specified scev is negated, but not a constant.
static constexpr auto FlagNSW
LLVM_ABI ArrayRef< SCEVUse > operands() const
Return operands of this SCEV expression.
Type * getType() const
Return the LLVM type of this SCEV expression.
LLVM_ABI void print(raw_ostream &OS) const
Print out the internal representation of this scalar to the specified stream.
SCEVTypes getSCEVType() const
static constexpr auto FlagNW
Analysis pass that exposes the ScalarEvolution for a function.
LLVM_ABI ScalarEvolution run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
void print(raw_ostream &OS, const Module *=nullptr) const override
print - Print out the internal state of the pass.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
static LLVM_ABI LoopGuards collect(const Loop *L, ScalarEvolution &SE)
Collect rewrite map for loop guards for loop L, together with flags indicating if NUW and NSW can be ...
LLVM_ABI const SCEV * rewrite(const SCEV *Expr) const
Try to apply the collected loop guards to Expr.
The main scalar evolution driver.
LLVM_ABI const SCEV * getUDivExpr(SCEVUse LHS, SCEVUse RHS)
Get a canonical unsigned division expression, or something simpler if possible.
const SCEV * getConstantMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEVConstant that is greater than or equal to (i.e.
static bool hasFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags TestFlags)
const DataLayout & getDataLayout() const
Return the DataLayout associated with the module this SCEV instance is operating on.
LLVM_ABI bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI bool isKnownOnEveryIteration(CmpPredicate Pred, const SCEVAddRecExpr *LHS, const SCEV *RHS)
Test if the condition described by Pred, LHS, RHS is known to be true on every iteration of the loop ...
LLVM_ABI const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterationsImpl(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
LLVM_ABI const SCEV * getZeroExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getUDivCeilSCEV(const SCEV *N, const SCEV *D)
Compute ceil(N / D).
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterations(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L at given Context duri...
LLVM_ABI Type * getWiderType(Type *Ty1, Type *Ty2) const
LLVM_ABI const SCEV * getAbsExpr(const SCEV *Op, bool IsNSW)
LLVM_ABI bool isKnownNonPositive(const SCEV *S)
Test if the given expression is known to be non-positive.
LLVM_ABI bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
LLVM_ABI const SCEV * getPredicatedConstantMaxBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getConstantMaxBackedgeTakenCount, except it will add a set of SCEV predicates to Predicate...
LLVM_ABI const SCEV * removePointerBase(const SCEV *S)
Compute an expression equivalent to S - getPointerBase(S).
LLVM_ABI bool isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS.
LLVM_ABI bool isKnownNonZero(const SCEV *S)
Test if the given expression is known to be non-zero.
LLVM_ABI const SCEV * getURemExpr(SCEVUse LHS, SCEVUse RHS)
Represents an unsigned remainder expression based on unsigned division.
LLVM_ABI const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
LLVM_ABI const SCEV * getSMinExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI void setNoWrapFlags(SCEVAddRecExpr *AddRec, SCEV::NoWrapFlags Flags)
Update no-wrap flags of an AddRec.
LLVM_ABI const SCEV * getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
Promote the operands to the wider of the types using zero-extension, and then perform a umax operatio...
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?
LLVM_ABI ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, bool AllowPredicates=false)
Compute the number of times the backedge of the specified loop will execute if its exit condition wer...
LLVM_ABI const SCEV * getMinMaxExpr(SCEVTypes Kind, SmallVectorImpl< SCEVUse > &Operands)
LLVM_ABI const SCEVPredicate * getEqualPredicate(const SCEV *LHS, const SCEV *RHS)
LLVM_ABI unsigned getSmallConstantTripMultiple(const Loop *L, const SCEV *ExitCount)
Returns the largest constant divisor of the trip count as a normal unsigned value,...
LLVM_ABI SCEVUse getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
LLVM_ABI uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
LLVM_ABI void registerUser(const SCEV *User, ArrayRef< SCEVUse > Ops)
Notify this ScalarEvolution that User directly uses SCEVs in Ops.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getPredicatedBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getBackedgeTakenCount, except it will add a set of SCEV predicates to Predicates that are ...
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
ConstantRange getSignedRange(const SCEV *S)
Determine the signed range for a particular SCEV.
LLVM_ABI const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
static LLVM_ABI bool isGuaranteedNotToBePoison(const SCEV *Op)
Returns true if Op is guaranteed to not be poison.
bool loopHasNoAbnormalExits(const Loop *L)
Return true if the loop has no abnormal exits.
LLVM_ABI const SCEV * getTripCountFromExitCount(const SCEV *ExitCount)
A version of getTripCountFromExitCount below which always picks an evaluation type which can not resu...
LLVM_ABI ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI)
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI void forgetValues(ArrayRef< Value * > Values)
Batched forgetValue: invalidates all Values in one shared def-use walk, avoiding the redundant re-tra...
LLVM_ABI const SCEV * getSequentialMinMaxExpr(SCEVTypes Kind, SmallVectorImpl< SCEVUse > &Operands)
LLVM_ABI const SCEV * getCastExpr(SCEVTypes Kind, SCEVUse Op, Type *Ty)
LLVM_ABI std::optional< bool > evaluatePredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Check whether the condition described by Pred, LHS, and RHS is true or false in the given Context.
LLVM_ABI unsigned getSmallConstantMaxTripCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Returns the upper bound of the loop trip count as a normal unsigned value.
LLVM_ABI bool isKnownMultipleOf(const SCEV *S, uint64_t M, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Check that S is a multiple of M.
LLVM_ABI bool isBackedgeTakenCountMaxOrZero(const Loop *L)
Return true if the backedge taken count is either the value returned by getConstantMaxBackedgeTakenCo...
LLVM_ABI void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
LLVM_ABI bool SimplifyICmpOperands(CmpPredicate &Pred, SCEVUse &LHS, SCEVUse &RHS, unsigned Depth=0)
Simplify LHS and RHS in a comparison with predicate Pred.
APInt getUnsignedRangeMin(const SCEV *S)
Determine the min of the unsigned range for a particular SCEV.
LLVM_ABI const SCEV * getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo)
Return an expression for offsetof on the given field with type IntTy.
LLVM_ABI LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
Return the "disposition" of the given SCEV with respect to the given loop.
LLVM_ABI bool containsAddRecurrence(const SCEV *S)
Return true if the SCEV is a scAddRecExpr or it contains scAddRecExpr.
LLVM_ABI const SCEV * getTruncateExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool hasOperand(const SCEV *S, const SCEV *Op) const
Test whether the given SCEV has Op as a direct or indirect operand.
LLVM_ABI const SCEV * getZeroExtendExprImpl(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
LLVM_ABI const SCEVPredicate * getComparePredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
LLVM_ABI bool haveSameSign(const SCEV *S1, const SCEV *S2)
Return true if we know that S1 and S2 must have the same sign.
LLVM_ABI const SCEV * getNotSCEV(const SCEV *V)
Return the SCEV object corresponding to ~V.
LLVM_ABI const SCEV * getElementCount(Type *Ty, ElementCount EC, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
LLVM_ABI bool instructionCouldExistWithOperands(const SCEV *A, const SCEV *B)
Return true if there exists a point in the program at which both A and B could be operands to the sam...
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
LLVM_ABI void print(raw_ostream &OS) const
LLVM_ABI const SCEV * getAnyExtendExpr(SCEVUse Op, Type *Ty)
getAnyExtendExpr - Return a SCEV for the given operand extended with unspecified bits out to the give...
LLVM_ABI const SCEV * getPredicatedExitCount(const Loop *L, const BasicBlock *ExitingBlock, SmallVectorImpl< const SCEVPredicate * > *Predicates, ExitCountKind Kind=Exact)
Same as above except this uses the predicated backedge taken info and may require predicates.
static SCEV::NoWrapFlags clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags)
LLVM_ABI void forgetTopmostLoop(const Loop *L)
LLVM_ABI void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
APInt getSignedRangeMin(const SCEV *S)
Determine the min of the signed range for a particular SCEV.
LLVM_ABI bool isLoopUniform(const SCEV *S, const Loop *L)
Returns true if the given SCEV is loop-uniform with respect to the specified loop L.
LLVM_ABI const SCEV * getNoopOrAnyExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
LLVM_ABI const SCEV * getSignExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getUMaxExpr(SCEVUse LHS, SCEVUse RHS)
static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags Mask)
Convenient NoWrapFlags manipulation.
LLVM_ABI std::optional< LoopInvariantPredicate > getLoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI=nullptr)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L, return a LoopInvaria...
LLVM_ABI const SCEV * getStoreSizeOfExpr(Type *IntTy, Type *StoreTy)
Return an expression for the store size of StoreTy that is type IntTy.
LLVM_ABI const SCEVPredicate * getWrapPredicate(const SCEVAddRecExpr *AR, SCEVWrapPredicate::IncrementWrapFlags AddedFlags)
LLVM_ABI bool isLoopBackedgeGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether the backedge of the loop is protected by a conditional between LHS and RHS.
LLVM_ABI APInt getNonZeroConstantMultiple(const SCEV *S)
const SCEV * getMinusOne(Type *Ty)
Return a SCEV for the constant -1 of a specific type.
static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags)
LLVM_ABI bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count.
LLVM_ABI BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB)
Return the "disposition" of the given SCEV with respect to the given block.
LLVM_ABI const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
LLVM_ABI const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
Promote the operands to the wider of the types using zero-extension, and then perform a umin operatio...
LLVM_ABI bool loopIsFiniteByAssumption(const Loop *L)
Return true if this loop is finite by assumption.
LLVM_ABI const SCEV * getExistingSCEV(Value *V)
Return an existing SCEV for V if there is one, otherwise return nullptr.
LLVM_ABI APInt getConstantMultiple(const SCEV *S, const Instruction *CtxI=nullptr)
Returns the max constant multiple of S.
LoopDisposition
An enum describing the relationship between a SCEV and a loop.
@ LoopComputable
The SCEV varies predictably with the loop.
@ LoopVariant
The SCEV is loop-variant (unknown).
@ LoopInvariant
The SCEV is loop-invariant.
@ LoopUniform
The SCEV is loop-uniform.
LLVM_ABI bool isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero=false, bool OrNegative=false)
Test if the given expression is known to be a power of 2.
LLVM_ABI std::optional< SCEV::NoWrapFlags > getStrengthenedNoWrapFlagsFromBinOp(const OverflowingBinaryOperator *OBO)
Parse NSW/NUW flags from add/sub/mul IR binary operation Op into SCEV no-wrap flags,...
LLVM_ABI void forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V)
Forget LCSSA phi node V of loop L to which a new predecessor was added, such that it may no longer be...
LLVM_ABI bool containsUndefs(const SCEV *S) const
Return true if the SCEV expression contains an undef value.
LLVM_ABI std::optional< MonotonicPredicateType > getMonotonicPredicateType(const SCEVAddRecExpr *LHS, ICmpInst::Predicate Pred)
If, for all loop invariant X, the predicate "LHS `Pred` X" is monotonically increasing or decreasing,...
LLVM_ABI const SCEV * getCouldNotCompute()
LLVM_ABI bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L)
Determine if the SCEV can be evaluated at loop's entry.
LLVM_ABI uint32_t getMinTrailingZeros(const SCEV *S, const Instruction *CtxI=nullptr)
Determine the minimum number of zero bits that S is guaranteed to end in (at every loop iteration).
BlockDisposition
An enum describing the relationship between a SCEV and a basic block.
@ DominatesBlock
The SCEV dominates the block.
@ ProperlyDominatesBlock
The SCEV properly dominates the block.
@ DoesNotDominateBlock
The SCEV does not dominate the block.
LLVM_ABI const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
LLVM_ABI void getPoisonGeneratingValues(SmallPtrSetImpl< const Value * > &Result, const SCEV *S)
Return the set of Values that, if poison, will definitively result in S being poison as well.
LLVM_ABI void forgetLoopDispositions()
Called when the client has changed the disposition of values in this loop.
LLVM_ABI const SCEV * getVScale(Type *Ty)
LLVM_ABI unsigned getSmallConstantTripCount(const Loop *L)
Returns the exact trip count of the loop if we can compute it, and the result is a small constant.
LLVM_ABI bool hasComputableLoopEvolution(const SCEV *S, const Loop *L)
Return true if the given SCEV changes value in a known way in the specified loop.
LLVM_ABI const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
LLVM_ABI void forgetAllLoops()
LLVM_ABI const SCEV * getSignExtendExprImpl(SCEVUse Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool dominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV dominate the specified basic block.
APInt getUnsignedRangeMax(const SCEV *S)
Determine the max of the unsigned range for a particular SCEV.
ExitCountKind
The terms "backedge taken count" and "exit count" are used interchangeably to refer to the number of ...
@ SymbolicMaximum
An expression which provides an upper bound on the exact trip count.
@ ConstantMaximum
A constant which provides an upper bound on the exact trip count.
@ Exact
An expression exactly describing the number of times the backedge has executed when a loop is exited.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
LLVM_ABI const SCEV * getPtrToAddrExpr(const SCEV *Op)
LLVM_ABI const SCEVAddRecExpr * convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Preds)
Tries to convert the S expression to an AddRec expression, adding additional predicates to Preds as r...
LLVM_ABI const SCEV * getSMaxExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI const SCEV * getElementSize(Instruction *Inst)
Return the size of an element read or written by Inst.
LLVM_ABI const SCEV * getSizeOfExpr(Type *IntTy, TypeSize Size)
Return an expression for a TypeSize.
LLVM_ABI std::optional< bool > evaluatePredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Check whether the condition described by Pred, LHS, and RHS is true or false.
LLVM_ABI const SCEV * getUnknown(Value *V)
LLVM_ABI std::optional< std::pair< const SCEV *, SmallVector< const SCEVPredicate *, 3 > > > createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI)
Checks if SymbolicPHI can be rewritten as an AddRecExpr under some Predicates.
LLVM_ABI const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI bool isKnownViaInduction(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
We'd like to check the predicate on every iteration of the most dominated loop between loops used in ...
LLVM_ABI std::optional< APInt > computeConstantDifference(const SCEV *LHS, const SCEV *RHS)
Compute LHS - RHS and returns the result as an APInt if it is a constant, and std::nullopt if it isn'...
LLVM_ABI bool properlyDominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV properly dominate the specified basic block.
LLVM_ABI const SCEV * getUDivExactExpr(SCEVUse LHS, SCEVUse RHS)
Get a canonical unsigned division expression, or something simpler if possible.
LLVM_ABI const SCEV * rewriteUsingPredicate(const SCEV *S, const Loop *L, const SCEVPredicate &A)
Re-writes the SCEV according to the Predicates in A.
LLVM_ABI std::pair< const SCEV *, const SCEV * > SplitIntoInitAndPostInc(const Loop *L, const SCEV *S)
Splits SCEV expression S into two SCEVs.
LLVM_ABI bool canReuseInstruction(const SCEV *S, Instruction *I, SmallVectorImpl< Instruction * > &DropPoisonGeneratingInsts)
Check whether it is poison-safe to represent the expression S using the instruction I.
LLVM_ABI bool isKnownPredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI const SCEV * getPredicatedSymbolicMaxBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getSymbolicMaxBackedgeTakenCount, except it will add a set of SCEV predicates to Predicate...
LLVM_ABI SCEVUse getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlags Flags={}, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getGEPExpr(GEPOperator *GEP, ArrayRef< SCEVUse > IndexExprs)
Returns an expression for a GEP.
LLVM_ABI const SCEV * getUMinExpr(SCEVUse LHS, SCEVUse RHS, bool Sequential=false)
LLVM_ABI bool isBasicBlockEntryGuardedByCond(const BasicBlock *BB, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the basic block is protected by a conditional between LHS and RHS.
LLVM_ABI const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
LLVM_ABI bool containsErasedValue(const SCEV *S) const
Return true if the SCEV expression contains a Value that has been optimised out and is now a nullptr.
LLVM_ABI SCEVUse getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L, SCEVFlags Flags)
Get an add recurrence expression for the specified loop.
LLVM_ABI SCEVUse getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlags Flags={}, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
const SCEV * getSymbolicMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEV that is greater than or equal to (i.e.
APInt getSignedRangeMax(const SCEV *S)
Determine the max of the signed range for a particular SCEV.
LLVM_ABI void verify() const
LLVMContext & getContext() const
This class represents the LLVM 'select' instruction.
Implements a dense probed hash-table based set with some number of buckets stored inline.
Definition DenseSet.h:293
size_type size() const
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void reserve(size_type N)
iterator erase(const_iterator CI)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
iterator insert(iterator I, T &&Elt)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:743
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:774
TypeSize getSizeInBits() const
Definition DataLayout.h:754
Class to represent struct types.
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:299
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:296
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:303
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
op_range operands()
Definition User.h:267
Use & Op()
Definition User.h:171
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
iterator_range< user_iterator > users()
Definition Value.h:428
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition Value.h:545
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
An efficient, type-erasing, non-owning reference to a callable.
const ParentTy * getParent() const
Definition ilist_node.h:34
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition APInt.h:2275
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition APInt.h:2280
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition APInt.h:2285
LLVM_ABI std::optional< APInt > SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, unsigned RangeWidth)
Let q(n) = An^2 + Bn + C, and BW = bit width of the value range (e.g.
Definition APInt.cpp:2850
LLVM_ABI APInt GreatestCommonDivisor(APInt A, APInt B, bool IsSigned=false)
Compute GCD of two APInt values.
Definition APInt.cpp:826
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2290
constexpr bool any(E Val)
@ Entry
Definition COFF.h:862
int getMinValue(MCInstrInfo const &MCII, MCInst const &MCI)
Return the minimum value of an extendable operand.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
bool match(Val *V, const Pattern &P)
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
auto m_BasicBlock()
Match an arbitrary basic block value and ignore it.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
auto m_Value()
Match an arbitrary value and ignore it.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
match_bind< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
brc_match< Cond_t, match_bind< BasicBlock >, match_bind< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
bind_cst_ty m_scev_APInt(const APInt *&C)
Match an SCEV constant and bind it to an APInt.
cst_pred_ty< is_all_ones > m_scev_AllOnes()
Match an integer with all bits set.
SCEVUnaryExpr_match< SCEVZeroExtendExpr, Op0_t > m_scev_ZExt(const Op0_t &Op0)
is_undef_or_poison m_scev_UndefOrPoison()
Match an SCEVUnknown wrapping undef or poison.
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
specificloop_ty m_SpecificLoop(const Loop *L)
SCEVUnaryExpr_match< SCEVSignExtendExpr, Op0_t > m_scev_SExt(const Op0_t &Op0)
match_bind< const SCEVMulExpr > m_scev_Mul(const SCEVMulExpr *&V)
cst_pred_ty< is_zero > m_scev_Zero()
Match an integer 0.
SCEVUnaryExpr_match< SCEVTruncateExpr, Op0_t > m_scev_Trunc(const Op0_t &Op0)
bool match(const SCEV *S, const Pattern &P)
SCEVBinaryExpr_match< SCEVUDivExpr, Op0_t, Op1_t > m_scev_UDiv(const Op0_t &Op0, const Op1_t &Op1)
specificscev_ty m_scev_Specific(const SCEV *S)
Match if we have a specific specified SCEV.
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
match_bind< const SCEVUnknown > m_SCEVUnknown(const SCEVUnknown *&V)
SCEVBinaryExpr_match< SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagNUW, true > m_scev_c_NUWMul(const Op0_t &Op0, const Op1_t &Op1)
match_bind< const SCEVAddExpr > m_scev_Add(const SCEVAddExpr *&V)
SCEVBinaryExpr_match< SCEVSMaxExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true > m_scev_SMax(const Op0_t &Op0, const Op1_t &Op1)
SCEVBinaryExpr_match< SCEVMulExpr, Op0_t, Op1_t, SCEV::FlagAnyWrap, true > m_scev_c_Mul(const Op0_t &Op0, const Op1_t &Op1)
SCEVURem_match< Op0_t, Op1_t > m_scev_URem(Op0_t LHS, Op1_t RHS, ScalarEvolution &SE)
Match the mathematical pattern A - (A / B) * B, where A and B can be arbitrary expressions.
@ Valid
The data is already valid.
initializer< Ty > init(const Ty &Val)
LocationClass< Ty > location(Ty &L)
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
Definition CoroShape.h:32
constexpr double e
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
unsigned getOpcode(const VPValue *V)
Return the instruction opcode for the recipe defining V or 0 for unsupported recipes and VPValues not...
This is an optimization pass for GlobalISel generic memory operations.
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:577
void stable_sort(R &&Range)
Definition STLExtras.h:2132
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
SaveAndRestore(T &) -> SaveAndRestore< T >
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
@ Known
Known to have no common set bits.
@ Dead
Unused definition.
InterleavedRange< Range > interleaved(const Range &R, StringRef Separator=", ", StringRef Prefix="", StringRef Suffix="")
Output range R as a sequence of interleaved elements.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
auto successors(const MachineBasicBlock *BB)
scope_exit(Callable) -> scope_exit< Callable >
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
@ BinaryOp
One of the operands is a binary op.
constexpr from_range_t from_range
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2224
constexpr bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition MathExtras.h:244
LLVM_ABI Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
void * PointerTy
LLVM_ABI bool VerifySCEV
auto uninitialized_copy(R &&Src, IterTy Dst)
Definition STLExtras.h:2127
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
LLVM_ABI bool canConstantFoldCallTo(const CallBase *Call, const Function *F, const TargetLibraryInfo *TLI=nullptr)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
LLVM_ABI Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
DomTreeNodeBase< BasicBlock > DomTreeNode
Definition Dominators.h:65
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition STLExtras.h:2216
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI bool isMustProgress(const Loop *L)
Return true if this loop can be assumed to make progress.
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI bool isFinite(const Loop *L)
Return true if this loop can be assumed to run for a finite number of iterations.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
unsigned short computeExpressionSize(ArrayRef< SCEVUse > Args)
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:383
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr T divideCeil(U Numerator, V Denominator)
Returns the integer ceil(Numerator / Denominator).
Definition MathExtras.h:389
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ Mul
Product of integers.
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ Add
Sum of integers.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition STLExtras.h:2028
DWARFExpression::Operation Op
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2104
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2035
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2208
constexpr bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition MathExtras.h:249
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
iterator_range< df_iterator< T > > depth_first(const T &G)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition STLExtras.h:2162
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI Constant * ConstantFoldInstOperands(const Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
SCEVUseT< const SCEV * > SCEVUse
bool SCEVExprContains(const SCEV *Root, PredTy Pred)
Return true if any node in Root satisfies the predicate Pred.
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
#define N
#define NC
Definition regutils.h:42
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:315
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
An object of this class is returned by queries that could not be answered.
static LLVM_ABI bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
The no-wrap flags to apply when creating a SCEV expression, to the expression and use respectively.
SCEVNoWrapFlags UseFlags
Flags only applied to a SCEVUse.
SCEVNoWrapFlags ExprFlags
Flags applied directly to a SCEV expression, must be valid wherever the expression is valid.
SCEVPtrT getPointer() const
This class defines a simple visitor class that may be used for various SCEV analysis purposes.
A utility class that uses RAII to save and restore the value of a variable.
Information about the number of loop iterations for which a loop exit's branch condition evaluates to...
LLVM_ABI ExitLimit(const SCEV *E)
Construct either an exact exit limit from a constant, or an unknown one from a SCEVCouldNotCompute.
SmallVector< const SCEVPredicate *, 4 > Predicates
A vector of predicate guards for this ExitLimit.