1//===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of the scalar evolution analysis
10// engine, which is used primarily to analyze expressions involving induction
11// variables in loops.
12//
13// There are several aspects to this library. First is the representation of
14// scalar expressions, which are represented as subclasses of the SCEV class.
15// These classes are used to represent certain types of subexpressions that we
16// can handle. We only create one SCEV of a particular shape, so
17// pointer-comparisons for equality are legal.
18//
19// One important aspect of the SCEV objects is that they are never cyclic, even
20// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
21// the PHI node is one of the idioms that we can represent (e.g., a polynomial
22// recurrence) then we represent it directly as a recurrence node, otherwise we
23// represent it as a SCEVUnknown node.
24//
25// In addition to being able to represent expressions of various types, we also
26// have folders that are used to build the *canonical* representation for a
27// particular expression. These folders are capable of using a variety of
28// rewrite rules to simplify the expressions.
29//
30// Once the folders are defined, we can implement the more interesting
31// higher-level code, such as the code that recognizes PHI nodes of various
32// types, computes the execution count of a loop, etc.
33//
34// TODO: We should use these routines and value representations to implement
35// dependence analysis!
36//
37//===----------------------------------------------------------------------===//
38//
39// There are several good references for the techniques used in this analysis.
40//
41// Chains of recurrences -- a method to expedite the evaluation
42// of closed-form functions
43// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
44//
45// On computational properties of chains of recurrences
46// Eugene V. Zima
47//
48// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
49// Robert A. van Engelen
50//
51// Efficient Symbolic Analysis for Optimizing Compilers
52// Robert A. van Engelen
53//
54// Using the chains of recurrences algebra for data dependence testing and
55// induction variable substitution
56// MS Thesis, Johnie Birch
57//
58//===----------------------------------------------------------------------===//
59
60#include "llvm/Analysis/ScalarEvolution.h"
61#include "llvm/ADT/APInt.h"
62#include "llvm/ADT/ArrayRef.h"
63#include "llvm/ADT/DenseMap.h"
64#include "llvm/ADT/DepthFirstIterator.h"
65#include "llvm/ADT/FoldingSet.h"
66#include "llvm/ADT/STLExtras.h"
67#include "llvm/ADT/ScopeExit.h"
68#include "llvm/ADT/Sequence.h"
69#include "llvm/ADT/SmallPtrSet.h"
70#include "llvm/ADT/SmallVector.h"
71#include "llvm/ADT/Statistic.h"
72#include "llvm/ADT/StringExtras.h"
73#include "llvm/ADT/StringRef.h"
74#include "llvm/Analysis/AssumptionCache.h"
75#include "llvm/Analysis/ConstantFolding.h"
76#include "llvm/Analysis/InstructionSimplify.h"
77#include "llvm/Analysis/LoopInfo.h"
78#include "llvm/Analysis/MemoryBuiltins.h"
79#include "llvm/Analysis/ScalarEvolutionExpressions.h"
80#include "llvm/Analysis/ScalarEvolutionPatternMatch.h"
81#include "llvm/Analysis/TargetLibraryInfo.h"
82#include "llvm/Analysis/ValueTracking.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"
88#include "llvm/IR/ConstantRange.h"
89#include "llvm/IR/Constants.h"
90#include "llvm/IR/DataLayout.h"
91#include "llvm/IR/DerivedTypes.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"
96#include "llvm/IR/InstIterator.h"
97#include "llvm/IR/InstrTypes.h"
98#include "llvm/IR/Instruction.h"
99#include "llvm/IR/Instructions.h"
100#include "llvm/IR/IntrinsicInst.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"
110#include "llvm/InitializePasses.h"
111#include "llvm/Pass.h"
112#include "llvm/Support/Casting.h"
113#include "llvm/Support/CommandLine.h"
114#include "llvm/Support/Compiler.h"
115#include "llvm/Support/Debug.h"
116#include "llvm/Support/ErrorHandling.h"
117#include "llvm/Support/InterleavedRange.h"
118#include "llvm/Support/KnownBits.h"
119#include "llvm/Support/SaveAndRestore.h"
120#include "llvm/Support/raw_ostream.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
153static cl::opt<unsigned>
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(Val: 100));
159
160static cl::opt<bool, true> VerifySCEVOpt(
161 "verify-scev", cl::Hidden, cl::location(L&: VerifySCEV),
162 cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
163static cl::opt<bool> VerifySCEVStrict(
164 "verify-scev-strict", cl::Hidden,
165 cl::desc("Enable stricter verification with -verify-scev is passed"));
166
167static cl::opt<bool> VerifyIR(
168 "scev-verify-ir", cl::Hidden,
169 cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"),
170 cl::init(Val: false));
171
172static cl::opt<unsigned> MulOpsInlineThreshold(
173 "scev-mulops-inline-threshold", cl::Hidden,
174 cl::desc("Threshold for inlining multiplication operands into a SCEV"),
175 cl::init(Val: 32));
176
177static cl::opt<unsigned> AddOpsInlineThreshold(
178 "scev-addops-inline-threshold", cl::Hidden,
179 cl::desc("Threshold for inlining addition operands into a SCEV"),
180 cl::init(Val: 500));
181
182static cl::opt<unsigned> MaxSCEVCompareDepth(
183 "scalar-evolution-max-scev-compare-depth", cl::Hidden,
184 cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
185 cl::init(Val: 32));
186
187static cl::opt<unsigned> MaxSCEVOperationsImplicationDepth(
188 "scalar-evolution-max-scev-operations-implication-depth", cl::Hidden,
189 cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
190 cl::init(Val: 2));
191
192static cl::opt<unsigned> MaxValueCompareDepth(
193 "scalar-evolution-max-value-compare-depth", cl::Hidden,
194 cl::desc("Maximum depth of recursive value complexity comparisons"),
195 cl::init(Val: 2));
196
197static cl::opt<unsigned>
198 MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden,
199 cl::desc("Maximum depth of recursive arithmetics"),
200 cl::init(Val: 32));
201
202static cl::opt<unsigned> MaxConstantEvolvingDepth(
203 "scalar-evolution-max-constant-evolving-depth", cl::Hidden,
204 cl::desc("Maximum depth of recursive constant evolving"), cl::init(Val: 32));
205
206static cl::opt<unsigned>
207 MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden,
208 cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"),
209 cl::init(Val: 8));
210
211static cl::opt<unsigned>
212 MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden,
213 cl::desc("Max coefficients in AddRec during evolving"),
214 cl::init(Val: 8));
215
216static cl::opt<unsigned>
217 HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden,
218 cl::desc("Size of the expression which is considered huge"),
219 cl::init(Val: 4096));
220
221static cl::opt<unsigned> RangeIterThreshold(
222 "scev-range-iter-threshold", cl::Hidden,
223 cl::desc("Threshold for switching to iteratively computing SCEV ranges"),
224 cl::init(Val: 32));
225
226static cl::opt<unsigned> MaxLoopGuardCollectionDepth(
227 "scalar-evolution-max-loop-guard-collection-depth", cl::Hidden,
228 cl::desc("Maximum depth for recursive loop guard collection"), cl::init(Val: 1));
229
230static cl::opt<bool>
231ClassifyExpressions("scalar-evolution-classify-expressions",
232 cl::Hidden, cl::init(Val: true),
233 cl::desc("When printing analysis, include information on every instruction"));
234
235static cl::opt<bool> UseExpensiveRangeSharpening(
236 "scalar-evolution-use-expensive-range-sharpening", cl::Hidden,
237 cl::init(Val: 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<unsigned> MaxPhiSCCAnalysisSize(
242 "scalar-evolution-max-scc-analysis-depth", cl::Hidden,
243 cl::desc("Maximum amount of nodes to process while searching SCEVUnknown "
244 "Phi strongly connected components"),
245 cl::init(Val: 8));
246
247static cl::opt<bool>
248 EnableFiniteLoopControl("scalar-evolution-finite-loop", cl::Hidden,
249 cl::desc("Handle <= and >= in finite loops"),
250 cl::init(Val: true));
251
252static cl::opt<bool> UseContextForNoWrapFlagInference(
253 "scalar-evolution-use-context-for-no-wrap-flag-strenghening", cl::Hidden,
254 cl::desc("Infer nuw/nsw flags using context where suitable"),
255 cl::init(Val: true));
256
257//===----------------------------------------------------------------------===//
258// SCEV class definitions
259//===----------------------------------------------------------------------===//
260
261#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
262LLVM_DUMP_METHOD void SCEVUse::dump() const {
263 print(dbgs());
264 dbgs() << '\n';
265}
266#endif
267
268void SCEVUse::print(raw_ostream &OS) const {
269 getPointer()->print(OS);
270 SCEV::NoWrapFlags Flags = static_cast<SCEV::NoWrapFlags>(getInt());
271 if (Flags & SCEV::FlagNUW)
272 OS << "(u nuw)";
273 if (Flags & SCEV::FlagNSW)
274 OS << "(u nsw)";
275}
276
277//===----------------------------------------------------------------------===//
278// Implementation of the SCEV class.
279//
280
281#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
282LLVM_DUMP_METHOD void SCEV::dump() const {
283 print(dbgs());
284 dbgs() << '\n';
285}
286#endif
287
288void SCEV::print(raw_ostream &OS) const {
289 switch (getSCEVType()) {
290 case scConstant:
291 cast<SCEVConstant>(Val: this)->getValue()->printAsOperand(O&: OS, PrintType: false);
292 return;
293 case scVScale:
294 OS << "vscale";
295 return;
296 case scPtrToAddr:
297 case scPtrToInt: {
298 const SCEVCastExpr *PtrCast = cast<SCEVCastExpr>(Val: this);
299 const SCEV *Op = PtrCast->getOperand();
300 StringRef OpS = getSCEVType() == scPtrToAddr ? "addr" : "int";
301 OS << "(ptrto" << OpS << " " << *Op->getType() << " " << *Op << " to "
302 << *PtrCast->getType() << ")";
303 return;
304 }
305 case scTruncate: {
306 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Val: this);
307 const SCEV *Op = Trunc->getOperand();
308 OS << "(trunc " << *Op->getType() << " " << *Op << " to "
309 << *Trunc->getType() << ")";
310 return;
311 }
312 case scZeroExtend: {
313 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(Val: this);
314 const SCEV *Op = ZExt->getOperand();
315 OS << "(zext " << *Op->getType() << " " << *Op << " to "
316 << *ZExt->getType() << ")";
317 return;
318 }
319 case scSignExtend: {
320 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(Val: this);
321 const SCEV *Op = SExt->getOperand();
322 OS << "(sext " << *Op->getType() << " " << *Op << " to "
323 << *SExt->getType() << ")";
324 return;
325 }
326 case scAddRecExpr: {
327 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(Val: this);
328 OS << "{" << *AR->getOperand(i: 0);
329 for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
330 OS << ",+," << *AR->getOperand(i);
331 OS << "}<";
332 if (AR->hasNoUnsignedWrap())
333 OS << "nuw><";
334 if (AR->hasNoSignedWrap())
335 OS << "nsw><";
336 if (AR->hasNoSelfWrap() &&
337 !AR->getNoWrapFlags(Mask: (NoWrapFlags)(FlagNUW | FlagNSW)))
338 OS << "nw><";
339 AR->getLoop()->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
340 OS << ">";
341 return;
342 }
343 case scAddExpr:
344 case scMulExpr:
345 case scUMaxExpr:
346 case scSMaxExpr:
347 case scUMinExpr:
348 case scSMinExpr:
349 case scSequentialUMinExpr: {
350 const SCEVNAryExpr *NAry = cast<SCEVNAryExpr>(Val: this);
351 const char *OpStr = nullptr;
352 switch (NAry->getSCEVType()) {
353 case scAddExpr: OpStr = " + "; break;
354 case scMulExpr: OpStr = " * "; break;
355 case scUMaxExpr: OpStr = " umax "; break;
356 case scSMaxExpr: OpStr = " smax "; break;
357 case scUMinExpr:
358 OpStr = " umin ";
359 break;
360 case scSMinExpr:
361 OpStr = " smin ";
362 break;
363 case scSequentialUMinExpr:
364 OpStr = " umin_seq ";
365 break;
366 default:
367 llvm_unreachable("There are no other nary expression types.");
368 }
369 OS << "("
370 << llvm::interleaved(R: llvm::make_pointee_range(Range: NAry->operands()), Separator: OpStr)
371 << ")";
372 switch (NAry->getSCEVType()) {
373 case scAddExpr:
374 case scMulExpr:
375 if (NAry->hasNoUnsignedWrap())
376 OS << "<nuw>";
377 if (NAry->hasNoSignedWrap())
378 OS << "<nsw>";
379 break;
380 default:
381 // Nothing to print for other nary expressions.
382 break;
383 }
384 return;
385 }
386 case scUDivExpr: {
387 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(Val: this);
388 OS << "(" << *UDiv->getLHS() << " /u " << *UDiv->getRHS() << ")";
389 return;
390 }
391 case scUnknown:
392 cast<SCEVUnknown>(Val: this)->getValue()->printAsOperand(O&: OS, PrintType: false);
393 return;
394 case scCouldNotCompute:
395 OS << "***COULDNOTCOMPUTE***";
396 return;
397 }
398 llvm_unreachable("Unknown SCEV kind!");
399}
400
401Type *SCEV::getType() const {
402 switch (getSCEVType()) {
403 case scConstant:
404 return cast<SCEVConstant>(Val: this)->getType();
405 case scVScale:
406 return cast<SCEVVScale>(Val: this)->getType();
407 case scPtrToAddr:
408 case scPtrToInt:
409 case scTruncate:
410 case scZeroExtend:
411 case scSignExtend:
412 return cast<SCEVCastExpr>(Val: this)->getType();
413 case scAddRecExpr:
414 return cast<SCEVAddRecExpr>(Val: this)->getType();
415 case scMulExpr:
416 return cast<SCEVMulExpr>(Val: this)->getType();
417 case scUMaxExpr:
418 case scSMaxExpr:
419 case scUMinExpr:
420 case scSMinExpr:
421 return cast<SCEVMinMaxExpr>(Val: this)->getType();
422 case scSequentialUMinExpr:
423 return cast<SCEVSequentialMinMaxExpr>(Val: this)->getType();
424 case scAddExpr:
425 return cast<SCEVAddExpr>(Val: this)->getType();
426 case scUDivExpr:
427 return cast<SCEVUDivExpr>(Val: this)->getType();
428 case scUnknown:
429 return cast<SCEVUnknown>(Val: this)->getType();
430 case scCouldNotCompute:
431 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
432 }
433 llvm_unreachable("Unknown SCEV kind!");
434}
435
436ArrayRef<SCEVUse> SCEV::operands() const {
437 switch (getSCEVType()) {
438 case scConstant:
439 case scVScale:
440 case scUnknown:
441 return {};
442 case scPtrToAddr:
443 case scPtrToInt:
444 case scTruncate:
445 case scZeroExtend:
446 case scSignExtend:
447 return cast<SCEVCastExpr>(Val: this)->operands();
448 case scAddRecExpr:
449 case scAddExpr:
450 case scMulExpr:
451 case scUMaxExpr:
452 case scSMaxExpr:
453 case scUMinExpr:
454 case scSMinExpr:
455 case scSequentialUMinExpr:
456 return cast<SCEVNAryExpr>(Val: this)->operands();
457 case scUDivExpr:
458 return cast<SCEVUDivExpr>(Val: this)->operands();
459 case scCouldNotCompute:
460 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
461 }
462 llvm_unreachable("Unknown SCEV kind!");
463}
464
465bool SCEV::isZero() const { return match(S: this, P: m_scev_Zero()); }
466
467bool SCEV::isOne() const { return match(S: this, P: m_scev_One()); }
468
469bool SCEV::isAllOnesValue() const { return match(S: this, P: m_scev_AllOnes()); }
470
471bool SCEV::isNonConstantNegative() const {
472 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Val: this);
473 if (!Mul) return false;
474
475 // If there is a constant factor, it will be first.
476 const SCEVConstant *SC = dyn_cast<SCEVConstant>(Val: Mul->getOperand(i: 0));
477 if (!SC) return false;
478
479 // Return true if the value is negative, this matches things like (-42 * V).
480 return SC->getAPInt().isNegative();
481}
482
483SCEVCouldNotCompute::SCEVCouldNotCompute() :
484 SCEV(FoldingSetNodeIDRef(), scCouldNotCompute, 0) {}
485
486bool SCEVCouldNotCompute::classof(const SCEV *S) {
487 return S->getSCEVType() == scCouldNotCompute;
488}
489
490const SCEV *ScalarEvolution::getConstant(ConstantInt *V) {
491 FoldingSetNodeID ID;
492 ID.AddInteger(I: scConstant);
493 ID.AddPointer(Ptr: V);
494 void *IP = nullptr;
495 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) return S;
496 SCEV *S = new (SCEVAllocator) SCEVConstant(ID.Intern(Allocator&: SCEVAllocator), V);
497 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
498 return S;
499}
500
501const SCEV *ScalarEvolution::getConstant(const APInt &Val) {
502 return getConstant(V: ConstantInt::get(Context&: getContext(), V: Val));
503}
504
505const SCEV *
506ScalarEvolution::getConstant(Type *Ty, uint64_t V, bool isSigned) {
507 IntegerType *ITy = cast<IntegerType>(Val: getEffectiveSCEVType(Ty));
508 // TODO: Avoid implicit trunc?
509 // See https://github.com/llvm/llvm-project/issues/112510.
510 return getConstant(
511 V: ConstantInt::get(Ty: ITy, V, IsSigned: isSigned, /*ImplicitTrunc=*/true));
512}
513
514const SCEV *ScalarEvolution::getVScale(Type *Ty) {
515 FoldingSetNodeID ID;
516 ID.AddInteger(I: scVScale);
517 ID.AddPointer(Ptr: Ty);
518 void *IP = nullptr;
519 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP))
520 return S;
521 SCEV *S = new (SCEVAllocator) SCEVVScale(ID.Intern(Allocator&: SCEVAllocator), Ty);
522 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
523 return S;
524}
525
526const SCEV *ScalarEvolution::getElementCount(Type *Ty, ElementCount EC,
527 SCEV::NoWrapFlags Flags) {
528 const SCEV *Res = getConstant(Ty, V: EC.getKnownMinValue());
529 if (EC.isScalable())
530 Res = getMulExpr(LHS: Res, RHS: getVScale(Ty), Flags);
531 return Res;
532}
533
534SCEVCastExpr::SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy,
535 SCEVUse op, Type *ty)
536 : SCEV(ID, SCEVTy, computeExpressionSize(Args: op)), Op(op), Ty(ty) {}
537
538SCEVPtrToAddrExpr::SCEVPtrToAddrExpr(const FoldingSetNodeIDRef ID,
539 const SCEV *Op, Type *ITy)
540 : SCEVCastExpr(ID, scPtrToAddr, Op, ITy) {
541 assert(getOperand()->getType()->isPointerTy() && Ty->isIntegerTy() &&
542 "Must be a non-bit-width-changing pointer-to-integer cast!");
543}
544
545SCEVPtrToIntExpr::SCEVPtrToIntExpr(const FoldingSetNodeIDRef ID, SCEVUse Op,
546 Type *ITy)
547 : SCEVCastExpr(ID, scPtrToInt, Op, ITy) {
548 assert(getOperand()->getType()->isPointerTy() && Ty->isIntegerTy() &&
549 "Must be a non-bit-width-changing pointer-to-integer cast!");
550}
551
552SCEVIntegralCastExpr::SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID,
553 SCEVTypes SCEVTy, SCEVUse op,
554 Type *ty)
555 : SCEVCastExpr(ID, SCEVTy, op, ty) {}
556
557SCEVTruncateExpr::SCEVTruncateExpr(const FoldingSetNodeIDRef ID, SCEVUse op,
558 Type *ty)
559 : SCEVIntegralCastExpr(ID, scTruncate, op, ty) {
560 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
561 "Cannot truncate non-integer value!");
562}
563
564SCEVZeroExtendExpr::SCEVZeroExtendExpr(const FoldingSetNodeIDRef ID, SCEVUse op,
565 Type *ty)
566 : SCEVIntegralCastExpr(ID, scZeroExtend, op, ty) {
567 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
568 "Cannot zero extend non-integer value!");
569}
570
571SCEVSignExtendExpr::SCEVSignExtendExpr(const FoldingSetNodeIDRef ID, SCEVUse op,
572 Type *ty)
573 : SCEVIntegralCastExpr(ID, scSignExtend, op, ty) {
574 assert(getOperand()->getType()->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
575 "Cannot sign extend non-integer value!");
576}
577
578void SCEVUnknown::deleted() {
579 // Clear this SCEVUnknown from various maps.
580 SE->forgetMemoizedResults(SCEVs: {this});
581
582 // Remove this SCEVUnknown from the uniquing map.
583 SE->UniqueSCEVs.RemoveNode(N: this);
584
585 // Release the value.
586 setValPtr(nullptr);
587}
588
589void SCEVUnknown::allUsesReplacedWith(Value *New) {
590 // Clear this SCEVUnknown from various maps.
591 SE->forgetMemoizedResults(SCEVs: {this});
592
593 // Remove this SCEVUnknown from the uniquing map.
594 SE->UniqueSCEVs.RemoveNode(N: this);
595
596 // Replace the value pointer in case someone is still using this SCEVUnknown.
597 setValPtr(New);
598}
599
600//===----------------------------------------------------------------------===//
601// SCEV Utilities
602//===----------------------------------------------------------------------===//
603
604/// Compare the two values \p LV and \p RV in terms of their "complexity" where
605/// "complexity" is a partial (and somewhat ad-hoc) relation used to order
606/// operands in SCEV expressions.
607static int CompareValueComplexity(const LoopInfo *const LI, Value *LV,
608 Value *RV, unsigned Depth) {
609 if (Depth > MaxValueCompareDepth)
610 return 0;
611
612 // Order pointer values after integer values. This helps SCEVExpander form
613 // GEPs.
614 bool LIsPointer = LV->getType()->isPointerTy(),
615 RIsPointer = RV->getType()->isPointerTy();
616 if (LIsPointer != RIsPointer)
617 return (int)LIsPointer - (int)RIsPointer;
618
619 // Compare getValueID values.
620 unsigned LID = LV->getValueID(), RID = RV->getValueID();
621 if (LID != RID)
622 return (int)LID - (int)RID;
623
624 // Sort arguments by their position.
625 if (const auto *LA = dyn_cast<Argument>(Val: LV)) {
626 const auto *RA = cast<Argument>(Val: RV);
627 unsigned LArgNo = LA->getArgNo(), RArgNo = RA->getArgNo();
628 return (int)LArgNo - (int)RArgNo;
629 }
630
631 if (const auto *LGV = dyn_cast<GlobalValue>(Val: LV)) {
632 const auto *RGV = cast<GlobalValue>(Val: RV);
633
634 if (auto L = LGV->getLinkage() - RGV->getLinkage())
635 return L;
636
637 const auto IsGVNameSemantic = [&](const GlobalValue *GV) {
638 auto LT = GV->getLinkage();
639 return !(GlobalValue::isPrivateLinkage(Linkage: LT) ||
640 GlobalValue::isInternalLinkage(Linkage: LT));
641 };
642
643 // Use the names to distinguish the two values, but only if the
644 // names are semantically important.
645 if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
646 return LGV->getName().compare(RHS: RGV->getName());
647 }
648
649 // For instructions, compare their loop depth, and their operand count. This
650 // is pretty loose.
651 if (const auto *LInst = dyn_cast<Instruction>(Val: LV)) {
652 const auto *RInst = cast<Instruction>(Val: RV);
653
654 // Compare loop depths.
655 const BasicBlock *LParent = LInst->getParent(),
656 *RParent = RInst->getParent();
657 if (LParent != RParent) {
658 unsigned LDepth = LI->getLoopDepth(BB: LParent),
659 RDepth = LI->getLoopDepth(BB: RParent);
660 if (LDepth != RDepth)
661 return (int)LDepth - (int)RDepth;
662 }
663
664 // Compare the number of operands.
665 unsigned LNumOps = LInst->getNumOperands(),
666 RNumOps = RInst->getNumOperands();
667 if (LNumOps != RNumOps)
668 return (int)LNumOps - (int)RNumOps;
669
670 for (unsigned Idx : seq(Size: LNumOps)) {
671 int Result = CompareValueComplexity(LI, LV: LInst->getOperand(i: Idx),
672 RV: RInst->getOperand(i: Idx), Depth: Depth + 1);
673 if (Result != 0)
674 return Result;
675 }
676 }
677
678 return 0;
679}
680
681// Return negative, zero, or positive, if LHS is less than, equal to, or greater
682// than RHS, respectively. A three-way result allows recursive comparisons to be
683// more efficient.
684// If the max analysis depth was reached, return std::nullopt, assuming we do
685// not know if they are equivalent for sure.
686static std::optional<int>
687CompareSCEVComplexity(const LoopInfo *const LI, const SCEV *LHS,
688 const SCEV *RHS, DominatorTree &DT, unsigned Depth = 0) {
689 // Fast-path: SCEVs are uniqued so we can do a quick equality check.
690 if (LHS == RHS)
691 return 0;
692
693 // Primarily, sort the SCEVs by their getSCEVType().
694 SCEVTypes LType = LHS->getSCEVType(), RType = RHS->getSCEVType();
695 if (LType != RType)
696 return (int)LType - (int)RType;
697
698 if (Depth > MaxSCEVCompareDepth)
699 return std::nullopt;
700
701 // Aside from the getSCEVType() ordering, the particular ordering
702 // isn't very important except that it's beneficial to be consistent,
703 // so that (a + b) and (b + a) don't end up as different expressions.
704 switch (LType) {
705 case scUnknown: {
706 const SCEVUnknown *LU = cast<SCEVUnknown>(Val: LHS);
707 const SCEVUnknown *RU = cast<SCEVUnknown>(Val: RHS);
708
709 int X =
710 CompareValueComplexity(LI, LV: LU->getValue(), RV: RU->getValue(), Depth: Depth + 1);
711 return X;
712 }
713
714 case scConstant: {
715 const SCEVConstant *LC = cast<SCEVConstant>(Val: LHS);
716 const SCEVConstant *RC = cast<SCEVConstant>(Val: RHS);
717
718 // Compare constant values.
719 const APInt &LA = LC->getAPInt();
720 const APInt &RA = RC->getAPInt();
721 unsigned LBitWidth = LA.getBitWidth(), RBitWidth = RA.getBitWidth();
722 if (LBitWidth != RBitWidth)
723 return (int)LBitWidth - (int)RBitWidth;
724 return LA.ult(RHS: RA) ? -1 : 1;
725 }
726
727 case scVScale: {
728 const auto *LTy = cast<IntegerType>(Val: cast<SCEVVScale>(Val: LHS)->getType());
729 const auto *RTy = cast<IntegerType>(Val: cast<SCEVVScale>(Val: RHS)->getType());
730 return LTy->getBitWidth() - RTy->getBitWidth();
731 }
732
733 case scAddRecExpr: {
734 const SCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(Val: LHS);
735 const SCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(Val: RHS);
736
737 // There is always a dominance between two recs that are used by one SCEV,
738 // so we can safely sort recs by loop header dominance. We require such
739 // order in getAddExpr.
740 const Loop *LLoop = LA->getLoop(), *RLoop = RA->getLoop();
741 if (LLoop != RLoop) {
742 const BasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
743 assert(LHead != RHead && "Two loops share the same header?");
744 if (DT.dominates(A: LHead, B: RHead))
745 return 1;
746 assert(DT.dominates(RHead, LHead) &&
747 "No dominance between recurrences used by one SCEV?");
748 return -1;
749 }
750
751 [[fallthrough]];
752 }
753
754 case scTruncate:
755 case scZeroExtend:
756 case scSignExtend:
757 case scPtrToAddr:
758 case scPtrToInt:
759 case scAddExpr:
760 case scMulExpr:
761 case scUDivExpr:
762 case scSMaxExpr:
763 case scUMaxExpr:
764 case scSMinExpr:
765 case scUMinExpr:
766 case scSequentialUMinExpr: {
767 ArrayRef<SCEVUse> LOps = LHS->operands();
768 ArrayRef<SCEVUse> ROps = RHS->operands();
769
770 // Lexicographically compare n-ary-like expressions.
771 unsigned LNumOps = LOps.size(), RNumOps = ROps.size();
772 if (LNumOps != RNumOps)
773 return (int)LNumOps - (int)RNumOps;
774
775 for (unsigned i = 0; i != LNumOps; ++i) {
776 auto X = CompareSCEVComplexity(LI, LHS: LOps[i].getPointer(),
777 RHS: ROps[i].getPointer(), DT, Depth: Depth + 1);
778 if (X != 0)
779 return X;
780 }
781 return 0;
782 }
783
784 case scCouldNotCompute:
785 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
786 }
787 llvm_unreachable("Unknown SCEV kind!");
788}
789
790/// Given a list of SCEV objects, order them by their complexity, and group
791/// objects of the same complexity together by value. When this routine is
792/// finished, we know that any duplicates in the vector are consecutive and that
793/// complexity is monotonically increasing.
794///
795/// Note that we go take special precautions to ensure that we get deterministic
796/// results from this routine. In other words, we don't want the results of
797/// this to depend on where the addresses of various SCEV objects happened to
798/// land in memory.
799static void GroupByComplexity(SmallVectorImpl<SCEVUse> &Ops, LoopInfo *LI,
800 DominatorTree &DT) {
801 if (Ops.size() < 2) return; // Noop
802
803 // Whether LHS has provably less complexity than RHS.
804 auto IsLessComplex = [&](SCEVUse LHS, SCEVUse RHS) {
805 auto Complexity = CompareSCEVComplexity(LI, LHS, RHS, DT);
806 return Complexity && *Complexity < 0;
807 };
808 if (Ops.size() == 2) {
809 // This is the common case, which also happens to be trivially simple.
810 // Special case it.
811 SCEVUse &LHS = Ops[0], &RHS = Ops[1];
812 if (IsLessComplex(RHS, LHS))
813 std::swap(a&: LHS, b&: RHS);
814 return;
815 }
816
817 // Do the rough sort by complexity.
818 llvm::stable_sort(
819 Range&: Ops, C: [&](SCEVUse LHS, SCEVUse RHS) { return IsLessComplex(LHS, RHS); });
820
821 // Now that we are sorted by complexity, group elements of the same
822 // complexity. Note that this is, at worst, N^2, but the vector is likely to
823 // be extremely short in practice. Note that we take this approach because we
824 // do not want to depend on the addresses of the objects we are grouping.
825 for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
826 const SCEV *S = Ops[i];
827 unsigned Complexity = S->getSCEVType();
828
829 // If there are any objects of the same complexity and same value as this
830 // one, group them.
831 for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
832 if (Ops[j] == S) { // Found a duplicate.
833 // Move it to immediately after i'th element.
834 std::swap(a&: Ops[i+1], b&: Ops[j]);
835 ++i; // no need to rescan it.
836 if (i == e-2) return; // Done!
837 }
838 }
839 }
840}
841
842/// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at
843/// least HugeExprThreshold nodes).
844static bool hasHugeExpression(ArrayRef<SCEVUse> Ops) {
845 return any_of(Range&: Ops, P: [](const SCEV *S) {
846 return S->getExpressionSize() >= HugeExprThreshold;
847 });
848}
849
850/// Performs a number of common optimizations on the passed \p Ops. If the
851/// whole expression reduces down to a single operand, it will be returned.
852///
853/// The following optimizations are performed:
854/// * Fold constants using the \p Fold function.
855/// * Remove identity constants satisfying \p IsIdentity.
856/// * If a constant satisfies \p IsAbsorber, return it.
857/// * Sort operands by complexity.
858template <typename FoldT, typename IsIdentityT, typename IsAbsorberT>
859static const SCEV *
860constantFoldAndGroupOps(ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT,
861 SmallVectorImpl<SCEVUse> &Ops, FoldT Fold,
862 IsIdentityT IsIdentity, IsAbsorberT IsAbsorber) {
863 const SCEVConstant *Folded = nullptr;
864 for (unsigned Idx = 0; Idx < Ops.size();) {
865 const SCEV *Op = Ops[Idx];
866 if (const auto *C = dyn_cast<SCEVConstant>(Val: Op)) {
867 if (!Folded)
868 Folded = C;
869 else
870 Folded = cast<SCEVConstant>(
871 SE.getConstant(Fold(Folded->getAPInt(), C->getAPInt())));
872 Ops.erase(CI: Ops.begin() + Idx);
873 continue;
874 }
875 ++Idx;
876 }
877
878 if (Ops.empty()) {
879 assert(Folded && "Must have folded value");
880 return Folded;
881 }
882
883 if (Folded && IsAbsorber(Folded->getAPInt()))
884 return Folded;
885
886 GroupByComplexity(Ops, LI: &LI, DT);
887 if (Folded && !IsIdentity(Folded->getAPInt()))
888 Ops.insert(I: Ops.begin(), Elt: Folded);
889
890 return Ops.size() == 1 ? Ops[0] : nullptr;
891}
892
893//===----------------------------------------------------------------------===//
894// Simple SCEV method implementations
895//===----------------------------------------------------------------------===//
896
897/// Compute BC(It, K). The result has width W. Assume, K > 0.
898static const SCEV *BinomialCoefficient(const SCEV *It, unsigned K,
899 ScalarEvolution &SE,
900 Type *ResultTy) {
901 // Handle the simplest case efficiently.
902 if (K == 1)
903 return SE.getTruncateOrZeroExtend(V: It, Ty: ResultTy);
904
905 // We are using the following formula for BC(It, K):
906 //
907 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
908 //
909 // Suppose, W is the bitwidth of the return value. We must be prepared for
910 // overflow. Hence, we must assure that the result of our computation is
911 // equal to the accurate one modulo 2^W. Unfortunately, division isn't
912 // safe in modular arithmetic.
913 //
914 // However, this code doesn't use exactly that formula; the formula it uses
915 // is something like the following, where T is the number of factors of 2 in
916 // K! (i.e. trailing zeros in the binary representation of K!), and ^ is
917 // exponentiation:
918 //
919 // BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
920 //
921 // This formula is trivially equivalent to the previous formula. However,
922 // this formula can be implemented much more efficiently. The trick is that
923 // K! / 2^T is odd, and exact division by an odd number *is* safe in modular
924 // arithmetic. To do exact division in modular arithmetic, all we have
925 // to do is multiply by the inverse. Therefore, this step can be done at
926 // width W.
927 //
928 // The next issue is how to safely do the division by 2^T. The way this
929 // is done is by doing the multiplication step at a width of at least W + T
930 // bits. This way, the bottom W+T bits of the product are accurate. Then,
931 // when we perform the division by 2^T (which is equivalent to a right shift
932 // by T), the bottom W bits are accurate. Extra bits are okay; they'll get
933 // truncated out after the division by 2^T.
934 //
935 // In comparison to just directly using the first formula, this technique
936 // is much more efficient; using the first formula requires W * K bits,
937 // but this formula less than W + K bits. Also, the first formula requires
938 // a division step, whereas this formula only requires multiplies and shifts.
939 //
940 // It doesn't matter whether the subtraction step is done in the calculation
941 // width or the input iteration count's width; if the subtraction overflows,
942 // the result must be zero anyway. We prefer here to do it in the width of
943 // the induction variable because it helps a lot for certain cases; CodeGen
944 // isn't smart enough to ignore the overflow, which leads to much less
945 // efficient code if the width of the subtraction is wider than the native
946 // register width.
947 //
948 // (It's possible to not widen at all by pulling out factors of 2 before
949 // the multiplication; for example, K=2 can be calculated as
950 // It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
951 // extra arithmetic, so it's not an obvious win, and it gets
952 // much more complicated for K > 3.)
953
954 // Protection from insane SCEVs; this bound is conservative,
955 // but it probably doesn't matter.
956 if (K > 1000)
957 return SE.getCouldNotCompute();
958
959 unsigned W = SE.getTypeSizeInBits(Ty: ResultTy);
960
961 // Calculate K! / 2^T and T; we divide out the factors of two before
962 // multiplying for calculating K! / 2^T to avoid overflow.
963 // Other overflow doesn't matter because we only care about the bottom
964 // W bits of the result.
965 APInt OddFactorial(W, 1);
966 unsigned T = 1;
967 for (unsigned i = 3; i <= K; ++i) {
968 unsigned TwoFactors = countr_zero(Val: i);
969 T += TwoFactors;
970 OddFactorial *= (i >> TwoFactors);
971 }
972
973 // We need at least W + T bits for the multiplication step
974 unsigned CalculationBits = W + T;
975
976 // Calculate 2^T, at width T+W.
977 APInt DivFactor = APInt::getOneBitSet(numBits: CalculationBits, BitNo: T);
978
979 // Calculate the multiplicative inverse of K! / 2^T;
980 // this multiplication factor will perform the exact division by
981 // K! / 2^T.
982 APInt MultiplyFactor = OddFactorial.multiplicativeInverse();
983
984 // Calculate the product, at width T+W
985 IntegerType *CalculationTy = IntegerType::get(C&: SE.getContext(),
986 NumBits: CalculationBits);
987 const SCEV *Dividend = SE.getTruncateOrZeroExtend(V: It, Ty: CalculationTy);
988 for (unsigned i = 1; i != K; ++i) {
989 const SCEV *S = SE.getMinusSCEV(LHS: It, RHS: SE.getConstant(Ty: It->getType(), V: i));
990 Dividend = SE.getMulExpr(LHS: Dividend,
991 RHS: SE.getTruncateOrZeroExtend(V: S, Ty: CalculationTy));
992 }
993
994 // Divide by 2^T
995 const SCEV *DivResult = SE.getUDivExpr(LHS: Dividend, RHS: SE.getConstant(Val: DivFactor));
996
997 // Truncate the result, and divide by K! / 2^T.
998
999 return SE.getMulExpr(LHS: SE.getConstant(Val: MultiplyFactor),
1000 RHS: SE.getTruncateOrZeroExtend(V: DivResult, Ty: ResultTy));
1001}
1002
1003/// Return the value of this chain of recurrences at the specified iteration
1004/// number. We can evaluate this recurrence by multiplying each element in the
1005/// chain by the binomial coefficient corresponding to it. In other words, we
1006/// can evaluate {A,+,B,+,C,+,D} as:
1007///
1008/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
1009///
1010/// where BC(It, k) stands for binomial coefficient.
1011const SCEV *SCEVAddRecExpr::evaluateAtIteration(const SCEV *It,
1012 ScalarEvolution &SE) const {
1013 return evaluateAtIteration(Operands: operands(), It, SE);
1014}
1015
1016const SCEV *SCEVAddRecExpr::evaluateAtIteration(ArrayRef<SCEVUse> Operands,
1017 const SCEV *It,
1018 ScalarEvolution &SE) {
1019 assert(Operands.size() > 0);
1020 const SCEV *Result = Operands[0].getPointer();
1021 for (unsigned i = 1, e = Operands.size(); i != e; ++i) {
1022 // The computation is correct in the face of overflow provided that the
1023 // multiplication is performed _after_ the evaluation of the binomial
1024 // coefficient.
1025 const SCEV *Coeff = BinomialCoefficient(It, K: i, SE, ResultTy: Result->getType());
1026 if (isa<SCEVCouldNotCompute>(Val: Coeff))
1027 return Coeff;
1028
1029 Result =
1030 SE.getAddExpr(LHS: Result, RHS: SE.getMulExpr(LHS: Operands[i].getPointer(), RHS: Coeff));
1031 }
1032 return Result;
1033}
1034
1035//===----------------------------------------------------------------------===//
1036// SCEV Expression folder implementations
1037//===----------------------------------------------------------------------===//
1038
1039/// The SCEVCastSinkingRewriter takes a scalar evolution expression,
1040/// which computes a pointer-typed value, and rewrites the whole expression
1041/// tree so that *all* the computations are done on integers, and the only
1042/// pointer-typed operands in the expression are SCEVUnknown.
1043/// The CreatePtrCast callback is invoked to create the actual conversion
1044/// (ptrtoint or ptrtoaddr) at the SCEVUnknown leaves.
1045class SCEVCastSinkingRewriter
1046 : public SCEVRewriteVisitor<SCEVCastSinkingRewriter> {
1047 using Base = SCEVRewriteVisitor<SCEVCastSinkingRewriter>;
1048 using ConversionFn = function_ref<const SCEV *(const SCEVUnknown *)>;
1049 Type *TargetTy;
1050 ConversionFn CreatePtrCast;
1051
1052public:
1053 SCEVCastSinkingRewriter(ScalarEvolution &SE, Type *TargetTy,
1054 ConversionFn CreatePtrCast)
1055 : Base(SE), TargetTy(TargetTy), CreatePtrCast(std::move(CreatePtrCast)) {}
1056
1057 static const SCEV *rewrite(const SCEV *Scev, ScalarEvolution &SE,
1058 Type *TargetTy, ConversionFn CreatePtrCast) {
1059 SCEVCastSinkingRewriter Rewriter(SE, TargetTy, std::move(CreatePtrCast));
1060 return Rewriter.visit(S: Scev);
1061 }
1062
1063 const SCEV *visit(const SCEV *S) {
1064 Type *STy = S->getType();
1065 // If the expression is not pointer-typed, just keep it as-is.
1066 if (!STy->isPointerTy())
1067 return S;
1068 // Else, recursively sink the cast down into it.
1069 return Base::visit(S);
1070 }
1071
1072 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
1073 // Preserve wrap flags on rewritten SCEVAddExpr, which the default
1074 // implementation drops.
1075 SmallVector<SCEVUse, 2> Operands;
1076 bool Changed = false;
1077 for (SCEVUse Op : Expr->operands()) {
1078 Operands.push_back(Elt: visit(S: Op.getPointer()));
1079 Changed |= Op.getPointer() != Operands.back();
1080 }
1081 return !Changed ? Expr : SE.getAddExpr(Ops&: Operands, Flags: Expr->getNoWrapFlags());
1082 }
1083
1084 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
1085 SmallVector<SCEVUse, 2> Operands;
1086 bool Changed = false;
1087 for (SCEVUse Op : Expr->operands()) {
1088 Operands.push_back(Elt: visit(S: Op.getPointer()));
1089 Changed |= Op.getPointer() != Operands.back();
1090 }
1091 return !Changed ? Expr : SE.getMulExpr(Ops&: Operands, Flags: Expr->getNoWrapFlags());
1092 }
1093
1094 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
1095 assert(Expr->getType()->isPointerTy() &&
1096 "Should only reach pointer-typed SCEVUnknown's.");
1097 // Perform some basic constant folding. If the operand of the cast is a
1098 // null pointer, don't create a cast SCEV expression (that will be left
1099 // as-is), but produce a zero constant.
1100 if (isa<ConstantPointerNull>(Val: Expr->getValue()))
1101 return SE.getZero(Ty: TargetTy);
1102 return CreatePtrCast(Expr);
1103 }
1104};
1105
1106const SCEV *ScalarEvolution::getLosslessPtrToIntExpr(const SCEV *Op) {
1107 assert(Op->getType()->isPointerTy() && "Op must be a pointer");
1108
1109 // It isn't legal for optimizations to construct new ptrtoint expressions
1110 // for non-integral pointers.
1111 if (getDataLayout().isNonIntegralPointerType(Ty: Op->getType()))
1112 return getCouldNotCompute();
1113
1114 Type *IntPtrTy = getDataLayout().getIntPtrType(Op->getType());
1115
1116 // We can only trivially model ptrtoint if SCEV's effective (integer) type
1117 // is sufficiently wide to represent all possible pointer values.
1118 // We could theoretically teach SCEV to truncate wider pointers, but
1119 // that isn't implemented for now.
1120 if (getDataLayout().getTypeSizeInBits(Ty: getEffectiveSCEVType(Ty: Op->getType())) !=
1121 getDataLayout().getTypeSizeInBits(Ty: IntPtrTy))
1122 return getCouldNotCompute();
1123
1124 // Use the rewriter to sink the cast down to SCEVUnknown leaves.
1125 const SCEV *IntOp = SCEVCastSinkingRewriter::rewrite(
1126 Scev: Op, SE&: *this, TargetTy: IntPtrTy, CreatePtrCast: [this, IntPtrTy](const SCEVUnknown *U) {
1127 FoldingSetNodeID ID;
1128 ID.AddInteger(I: scPtrToInt);
1129 ID.AddPointer(Ptr: U);
1130 void *IP = nullptr;
1131 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP))
1132 return S;
1133 SCEV *S = new (SCEVAllocator)
1134 SCEVPtrToIntExpr(ID.Intern(Allocator&: SCEVAllocator), U, IntPtrTy);
1135 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
1136 registerUser(User: S, Ops: U);
1137 return static_cast<const SCEV *>(S);
1138 });
1139 assert(IntOp->getType()->isIntegerTy() &&
1140 "We must have succeeded in sinking the cast, "
1141 "and ending up with an integer-typed expression!");
1142 return IntOp;
1143}
1144
1145const SCEV *ScalarEvolution::getPtrToAddrExpr(const SCEV *Op) {
1146 assert(Op->getType()->isPointerTy() && "Op must be a pointer");
1147
1148 // Treat pointers with unstable representation conservatively, since the
1149 // address bits may change.
1150 if (DL.hasUnstableRepresentation(Ty: Op->getType()))
1151 return getCouldNotCompute();
1152
1153 Type *Ty = DL.getAddressType(PtrTy: Op->getType());
1154
1155 // Use the rewriter to sink the cast down to SCEVUnknown leaves.
1156 // The rewriter handles null pointer constant folding.
1157 const SCEV *IntOp = SCEVCastSinkingRewriter::rewrite(
1158 Scev: Op, SE&: *this, TargetTy: Ty, CreatePtrCast: [this, Ty](const SCEVUnknown *U) {
1159 FoldingSetNodeID ID;
1160 ID.AddInteger(I: scPtrToAddr);
1161 ID.AddPointer(Ptr: U);
1162 ID.AddPointer(Ptr: Ty);
1163 void *IP = nullptr;
1164 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP))
1165 return S;
1166 SCEV *S = new (SCEVAllocator)
1167 SCEVPtrToAddrExpr(ID.Intern(Allocator&: SCEVAllocator), U, Ty);
1168 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
1169 registerUser(User: S, Ops: U);
1170 return static_cast<const SCEV *>(S);
1171 });
1172 assert(IntOp->getType()->isIntegerTy() &&
1173 "We must have succeeded in sinking the cast, "
1174 "and ending up with an integer-typed expression!");
1175 return IntOp;
1176}
1177
1178const SCEV *ScalarEvolution::getPtrToIntExpr(const SCEV *Op, Type *Ty) {
1179 assert(Ty->isIntegerTy() && "Target type must be an integer type!");
1180
1181 const SCEV *IntOp = getLosslessPtrToIntExpr(Op);
1182 if (isa<SCEVCouldNotCompute>(Val: IntOp))
1183 return IntOp;
1184
1185 return getTruncateOrZeroExtend(V: IntOp, Ty);
1186}
1187
1188const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op, Type *Ty,
1189 unsigned Depth) {
1190 assert(getTypeSizeInBits(Op->getType()) > getTypeSizeInBits(Ty) &&
1191 "This is not a truncating conversion!");
1192 assert(isSCEVable(Ty) &&
1193 "This is not a conversion to a SCEVable type!");
1194 assert(!Op->getType()->isPointerTy() && "Can't truncate pointer!");
1195 Ty = getEffectiveSCEVType(Ty);
1196
1197 FoldingSetNodeID ID;
1198 ID.AddInteger(I: scTruncate);
1199 ID.AddPointer(Ptr: Op);
1200 ID.AddPointer(Ptr: Ty);
1201 void *IP = nullptr;
1202 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) return S;
1203
1204 // Fold if the operand is constant.
1205 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Val: Op))
1206 return getConstant(
1207 V: cast<ConstantInt>(Val: ConstantExpr::getTrunc(C: SC->getValue(), Ty)));
1208
1209 // trunc(trunc(x)) --> trunc(x)
1210 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Val: Op))
1211 return getTruncateExpr(Op: ST->getOperand(), Ty, Depth: Depth + 1);
1212
1213 // trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
1214 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Val: Op))
1215 return getTruncateOrSignExtend(V: SS->getOperand(), Ty, Depth: Depth + 1);
1216
1217 // trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
1218 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Val: Op))
1219 return getTruncateOrZeroExtend(V: SZ->getOperand(), Ty, Depth: Depth + 1);
1220
1221 if (Depth > MaxCastDepth) {
1222 SCEV *S =
1223 new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(Allocator&: SCEVAllocator), Op, Ty);
1224 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
1225 registerUser(User: S, Ops: Op);
1226 return S;
1227 }
1228
1229 // trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and
1230 // trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN),
1231 // if after transforming we have at most one truncate, not counting truncates
1232 // that replace other casts.
1233 if (isa<SCEVAddExpr>(Val: Op) || isa<SCEVMulExpr>(Val: Op)) {
1234 auto *CommOp = cast<SCEVCommutativeExpr>(Val: Op);
1235 SmallVector<SCEVUse, 4> Operands;
1236 unsigned numTruncs = 0;
1237 for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2;
1238 ++i) {
1239 const SCEV *S = getTruncateExpr(Op: CommOp->getOperand(i), Ty, Depth: Depth + 1);
1240 if (!isa<SCEVIntegralCastExpr>(Val: CommOp->getOperand(i)) &&
1241 isa<SCEVTruncateExpr>(Val: S))
1242 numTruncs++;
1243 Operands.push_back(Elt: S);
1244 }
1245 if (numTruncs < 2) {
1246 if (isa<SCEVAddExpr>(Val: Op))
1247 return getAddExpr(Ops&: Operands);
1248 if (isa<SCEVMulExpr>(Val: Op))
1249 return getMulExpr(Ops&: Operands);
1250 llvm_unreachable("Unexpected SCEV type for Op.");
1251 }
1252 // Although we checked in the beginning that ID is not in the cache, it is
1253 // possible that during recursion and different modification ID was inserted
1254 // into the cache. So if we find it, just return it.
1255 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP))
1256 return S;
1257 }
1258
1259 // If the input value is a chrec scev, truncate the chrec's operands.
1260 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Val: Op)) {
1261 SmallVector<SCEVUse, 4> Operands;
1262 for (const SCEV *Op : AddRec->operands())
1263 Operands.push_back(Elt: getTruncateExpr(Op, Ty, Depth: Depth + 1));
1264 return getAddRecExpr(Operands, L: AddRec->getLoop(), Flags: SCEV::FlagAnyWrap);
1265 }
1266
1267 // Return zero if truncating to known zeros.
1268 uint32_t MinTrailingZeros = getMinTrailingZeros(S: Op);
1269 if (MinTrailingZeros >= getTypeSizeInBits(Ty))
1270 return getZero(Ty);
1271
1272 // The cast wasn't folded; create an explicit cast node. We can reuse
1273 // the existing insert position since if we get here, we won't have
1274 // made any changes which would invalidate it.
1275 SCEV *S = new (SCEVAllocator) SCEVTruncateExpr(ID.Intern(Allocator&: SCEVAllocator),
1276 Op, Ty);
1277 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
1278 registerUser(User: S, Ops: Op);
1279 return S;
1280}
1281
1282// Get the limit of a recurrence such that incrementing by Step cannot cause
1283// signed overflow as long as the value of the recurrence within the
1284// loop does not exceed this limit before incrementing.
1285static const SCEV *getSignedOverflowLimitForStep(const SCEV *Step,
1286 ICmpInst::Predicate *Pred,
1287 ScalarEvolution *SE) {
1288 unsigned BitWidth = SE->getTypeSizeInBits(Ty: Step->getType());
1289 if (SE->isKnownPositive(S: Step)) {
1290 *Pred = ICmpInst::ICMP_SLT;
1291 return SE->getConstant(Val: APInt::getSignedMinValue(numBits: BitWidth) -
1292 SE->getSignedRangeMax(S: Step));
1293 }
1294 if (SE->isKnownNegative(S: Step)) {
1295 *Pred = ICmpInst::ICMP_SGT;
1296 return SE->getConstant(Val: APInt::getSignedMaxValue(numBits: BitWidth) -
1297 SE->getSignedRangeMin(S: Step));
1298 }
1299 return nullptr;
1300}
1301
1302// Get the limit of a recurrence such that incrementing by Step cannot cause
1303// unsigned overflow as long as the value of the recurrence within the loop does
1304// not exceed this limit before incrementing.
1305static const SCEV *getUnsignedOverflowLimitForStep(const SCEV *Step,
1306 ICmpInst::Predicate *Pred,
1307 ScalarEvolution *SE) {
1308 unsigned BitWidth = SE->getTypeSizeInBits(Ty: Step->getType());
1309 *Pred = ICmpInst::ICMP_ULT;
1310
1311 return SE->getConstant(Val: APInt::getMinValue(numBits: BitWidth) -
1312 SE->getUnsignedRangeMax(S: Step));
1313}
1314
1315namespace {
1316
1317struct ExtendOpTraitsBase {
1318 typedef const SCEV *(ScalarEvolution::*GetExtendExprTy)(const SCEV *, Type *,
1319 unsigned);
1320};
1321
1322// Used to make code generic over signed and unsigned overflow.
1323template <typename ExtendOp> struct ExtendOpTraits {
1324 // Members present:
1325 //
1326 // static const SCEV::NoWrapFlags WrapType;
1327 //
1328 // static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1329 //
1330 // static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1331 // ICmpInst::Predicate *Pred,
1332 // ScalarEvolution *SE);
1333};
1334
1335template <>
1336struct ExtendOpTraits<SCEVSignExtendExpr> : public ExtendOpTraitsBase {
1337 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNSW;
1338
1339 static const GetExtendExprTy GetExtendExpr;
1340
1341 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1342 ICmpInst::Predicate *Pred,
1343 ScalarEvolution *SE) {
1344 return getSignedOverflowLimitForStep(Step, Pred, SE);
1345 }
1346};
1347
1348const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1349 SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
1350
1351template <>
1352struct ExtendOpTraits<SCEVZeroExtendExpr> : public ExtendOpTraitsBase {
1353 static const SCEV::NoWrapFlags WrapType = SCEV::FlagNUW;
1354
1355 static const GetExtendExprTy GetExtendExpr;
1356
1357 static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1358 ICmpInst::Predicate *Pred,
1359 ScalarEvolution *SE) {
1360 return getUnsignedOverflowLimitForStep(Step, Pred, SE);
1361 }
1362};
1363
1364const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1365 SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
1366
1367} // end anonymous namespace
1368
1369// The recurrence AR has been shown to have no signed/unsigned wrap or something
1370// close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1371// easily prove NSW/NUW for its preincrement or postincrement sibling. This
1372// allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1373// Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1374// expression "Step + sext/zext(PreIncAR)" is congruent with
1375// "sext/zext(PostIncAR)"
1376template <typename ExtendOpTy>
1377static const SCEV *getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty,
1378 ScalarEvolution *SE, unsigned Depth) {
1379 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1380 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1381
1382 const Loop *L = AR->getLoop();
1383 const SCEV *Start = AR->getStart();
1384 const SCEV *Step = AR->getStepRecurrence(SE&: *SE);
1385
1386 // Check for a simple looking step prior to loop entry.
1387 const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Val: Start);
1388 if (!SA)
1389 return nullptr;
1390
1391 // Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1392 // subtraction is expensive. For this purpose, perform a quick and dirty
1393 // difference, by checking for Step in the operand list. Note, that
1394 // SA might have repeated ops, like %a + %a + ..., so only remove one.
1395 SmallVector<SCEVUse, 4> DiffOps(SA->operands());
1396 for (auto It = DiffOps.begin(); It != DiffOps.end(); ++It)
1397 if (*It == Step) {
1398 DiffOps.erase(CI: It);
1399 break;
1400 }
1401
1402 if (DiffOps.size() == SA->getNumOperands())
1403 return nullptr;
1404
1405 // Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1406 // `Step`:
1407
1408 // 1. NSW/NUW flags on the step increment.
1409 auto PreStartFlags =
1410 ScalarEvolution::maskFlags(Flags: SA->getNoWrapFlags(), Mask: SCEV::FlagNUW);
1411 const SCEV *PreStart = SE->getAddExpr(Ops&: DiffOps, Flags: PreStartFlags);
1412 const SCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1413 Val: SE->getAddRecExpr(Start: PreStart, Step, L, Flags: SCEV::FlagAnyWrap));
1414
1415 // "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1416 // "S+X does not sign/unsign-overflow".
1417 //
1418
1419 const SCEV *BECount = SE->getBackedgeTakenCount(L);
1420 if (PreAR && PreAR->getNoWrapFlags(Mask: WrapType) &&
1421 !isa<SCEVCouldNotCompute>(Val: BECount) && SE->isKnownPositive(S: BECount))
1422 return PreStart;
1423
1424 // 2. Direct overflow check on the step operation's expression.
1425 unsigned BitWidth = SE->getTypeSizeInBits(Ty: AR->getType());
1426 Type *WideTy = IntegerType::get(C&: SE->getContext(), NumBits: BitWidth * 2);
1427 const SCEV *OperandExtendedStart =
1428 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy, Depth),
1429 (SE->*GetExtendExpr)(Step, WideTy, Depth));
1430 if ((SE->*GetExtendExpr)(Start, WideTy, Depth) == OperandExtendedStart) {
1431 if (PreAR && AR->getNoWrapFlags(Mask: WrapType)) {
1432 // If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1433 // or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1434 // `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact.
1435 SE->setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(PreAR), Flags: WrapType);
1436 }
1437 return PreStart;
1438 }
1439
1440 // 3. Loop precondition.
1441 ICmpInst::Predicate Pred;
1442 const SCEV *OverflowLimit =
1443 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1444
1445 if (OverflowLimit &&
1446 SE->isLoopEntryGuardedByCond(L, Pred, LHS: PreStart, RHS: OverflowLimit))
1447 return PreStart;
1448
1449 return nullptr;
1450}
1451
1452// Get the normalized zero or sign extended expression for this AddRec's Start.
1453template <typename ExtendOpTy>
1454static const SCEV *getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty,
1455 ScalarEvolution *SE,
1456 unsigned Depth) {
1457 auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1458
1459 const SCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE, Depth);
1460 if (!PreStart)
1461 return (SE->*GetExtendExpr)(AR->getStart(), Ty, Depth);
1462
1463 return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(SE&: *SE), Ty,
1464 Depth),
1465 (SE->*GetExtendExpr)(PreStart, Ty, Depth));
1466}
1467
1468// Try to prove away overflow by looking at "nearby" add recurrences. A
1469// motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1470// does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1471//
1472// Formally:
1473//
1474// {S,+,X} == {S-T,+,X} + T
1475// => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1476//
1477// If ({S-T,+,X} + T) does not overflow ... (1)
1478//
1479// RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1480//
1481// If {S-T,+,X} does not overflow ... (2)
1482//
1483// RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1484// == {Ext(S-T)+Ext(T),+,Ext(X)}
1485//
1486// If (S-T)+T does not overflow ... (3)
1487//
1488// RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1489// == {Ext(S),+,Ext(X)} == LHS
1490//
1491// Thus, if (1), (2) and (3) are true for some T, then
1492// Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1493//
1494// (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1495// does not overflow" restricted to the 0th iteration. Therefore we only need
1496// to check for (1) and (2).
1497//
1498// In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1499// is `Delta` (defined below).
1500template <typename ExtendOpTy>
1501bool ScalarEvolution::proveNoWrapByVaryingStart(const SCEV *Start,
1502 const SCEV *Step,
1503 const Loop *L) {
1504 auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1505
1506 // We restrict `Start` to a constant to prevent SCEV from spending too much
1507 // time here. It is correct (but more expensive) to continue with a
1508 // non-constant `Start` and do a general SCEV subtraction to compute
1509 // `PreStart` below.
1510 const SCEVConstant *StartC = dyn_cast<SCEVConstant>(Val: Start);
1511 if (!StartC)
1512 return false;
1513
1514 APInt StartAI = StartC->getAPInt();
1515
1516 for (unsigned Delta : {-2, -1, 1, 2}) {
1517 const SCEV *PreStart = getConstant(Val: StartAI - Delta);
1518
1519 FoldingSetNodeID ID;
1520 ID.AddInteger(I: scAddRecExpr);
1521 ID.AddPointer(Ptr: PreStart);
1522 ID.AddPointer(Ptr: Step);
1523 ID.AddPointer(Ptr: L);
1524 void *IP = nullptr;
1525 const auto *PreAR =
1526 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP));
1527
1528 // Give up if we don't already have the add recurrence we need because
1529 // actually constructing an add recurrence is relatively expensive.
1530 if (PreAR && PreAR->getNoWrapFlags(Mask: WrapType)) { // proves (2)
1531 const SCEV *DeltaS = getConstant(Ty: StartC->getType(), V: Delta);
1532 ICmpInst::Predicate Pred = ICmpInst::BAD_ICMP_PREDICATE;
1533 const SCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1534 DeltaS, &Pred, this);
1535 if (Limit && isKnownPredicate(Pred, LHS: PreAR, RHS: Limit)) // proves (1)
1536 return true;
1537 }
1538 }
1539
1540 return false;
1541}
1542
1543// Finds an integer D for an expression (C + x + y + ...) such that the top
1544// level addition in (D + (C - D + x + y + ...)) would not wrap (signed or
1545// unsigned) and the number of trailing zeros of (C - D + x + y + ...) is
1546// maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and
1547// the (C + x + y + ...) expression is \p WholeAddExpr.
1548static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1549 const SCEVConstant *ConstantTerm,
1550 const SCEVAddExpr *WholeAddExpr) {
1551 const APInt &C = ConstantTerm->getAPInt();
1552 const unsigned BitWidth = C.getBitWidth();
1553 // Find number of trailing zeros of (x + y + ...) w/o the C first:
1554 uint32_t TZ = BitWidth;
1555 for (unsigned I = 1, E = WholeAddExpr->getNumOperands(); I < E && TZ; ++I)
1556 TZ = std::min(a: TZ, b: SE.getMinTrailingZeros(S: WholeAddExpr->getOperand(i: I)));
1557 if (TZ) {
1558 // Set D to be as many least significant bits of C as possible while still
1559 // guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap:
1560 return TZ < BitWidth ? C.trunc(width: TZ).zext(width: BitWidth) : C;
1561 }
1562 return APInt(BitWidth, 0);
1563}
1564
1565// Finds an integer D for an affine AddRec expression {C,+,x} such that the top
1566// level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the
1567// number of trailing zeros of (C - D + x * n) is maximized, where C is the \p
1568// ConstantStart, x is an arbitrary \p Step, and n is the loop trip count.
1569static APInt extractConstantWithoutWrapping(ScalarEvolution &SE,
1570 const APInt &ConstantStart,
1571 const SCEV *Step) {
1572 const unsigned BitWidth = ConstantStart.getBitWidth();
1573 const uint32_t TZ = SE.getMinTrailingZeros(S: Step);
1574 if (TZ)
1575 return TZ < BitWidth ? ConstantStart.trunc(width: TZ).zext(width: BitWidth)
1576 : ConstantStart;
1577 return APInt(BitWidth, 0);
1578}
1579
1580static void insertFoldCacheEntry(
1581 const ScalarEvolution::FoldID &ID, const SCEV *S,
1582 DenseMap<ScalarEvolution::FoldID, const SCEV *> &FoldCache,
1583 DenseMap<const SCEV *, SmallVector<ScalarEvolution::FoldID, 2>>
1584 &FoldCacheUser) {
1585 auto I = FoldCache.insert(KV: {ID, S});
1586 if (!I.second) {
1587 // Remove FoldCacheUser entry for ID when replacing an existing FoldCache
1588 // entry.
1589 auto &UserIDs = FoldCacheUser[I.first->second];
1590 assert(count(UserIDs, ID) == 1 && "unexpected duplicates in UserIDs");
1591 for (unsigned I = 0; I != UserIDs.size(); ++I)
1592 if (UserIDs[I] == ID) {
1593 std::swap(a&: UserIDs[I], b&: UserIDs.back());
1594 break;
1595 }
1596 UserIDs.pop_back();
1597 I.first->second = S;
1598 }
1599 FoldCacheUser[S].push_back(Elt: ID);
1600}
1601
1602const SCEV *
1603ScalarEvolution::getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1604 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1605 "This is not an extending conversion!");
1606 assert(isSCEVable(Ty) &&
1607 "This is not a conversion to a SCEVable type!");
1608 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1609 Ty = getEffectiveSCEVType(Ty);
1610
1611 FoldID ID(scZeroExtend, Op, Ty);
1612 if (const SCEV *S = FoldCache.lookup(Val: ID))
1613 return S;
1614
1615 const SCEV *S = getZeroExtendExprImpl(Op, Ty, Depth);
1616 if (!isa<SCEVZeroExtendExpr>(Val: S))
1617 insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser);
1618 return S;
1619}
1620
1621const SCEV *ScalarEvolution::getZeroExtendExprImpl(const SCEV *Op, Type *Ty,
1622 unsigned Depth) {
1623 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1624 "This is not an extending conversion!");
1625 assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!");
1626 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1627
1628 // Fold if the operand is constant.
1629 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Val: Op))
1630 return getConstant(Val: SC->getAPInt().zext(width: getTypeSizeInBits(Ty)));
1631
1632 // zext(zext(x)) --> zext(x)
1633 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Val: Op))
1634 return getZeroExtendExpr(Op: SZ->getOperand(), Ty, Depth: Depth + 1);
1635
1636 // Before doing any expensive analysis, check to see if we've already
1637 // computed a SCEV for this Op and Ty.
1638 FoldingSetNodeID ID;
1639 ID.AddInteger(I: scZeroExtend);
1640 ID.AddPointer(Ptr: Op);
1641 ID.AddPointer(Ptr: Ty);
1642 void *IP = nullptr;
1643 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) return S;
1644 if (Depth > MaxCastDepth) {
1645 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(Allocator&: SCEVAllocator),
1646 Op, Ty);
1647 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
1648 registerUser(User: S, Ops: Op);
1649 return S;
1650 }
1651
1652 // zext(trunc(x)) --> zext(x) or x or trunc(x)
1653 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Val: Op)) {
1654 // It's possible the bits taken off by the truncate were all zero bits. If
1655 // so, we should be able to simplify this further.
1656 const SCEV *X = ST->getOperand();
1657 ConstantRange CR = getUnsignedRange(S: X);
1658 unsigned TruncBits = getTypeSizeInBits(Ty: ST->getType());
1659 unsigned NewBits = getTypeSizeInBits(Ty);
1660 if (CR.truncate(BitWidth: TruncBits).zeroExtend(BitWidth: NewBits).contains(
1661 CR: CR.zextOrTrunc(BitWidth: NewBits)))
1662 return getTruncateOrZeroExtend(V: X, Ty, Depth);
1663 }
1664
1665 // If the input value is a chrec scev, and we can prove that the value
1666 // did not overflow the old, smaller, value, we can zero extend all of the
1667 // operands (often constants). This allows analysis of something like
1668 // this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
1669 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: Op))
1670 if (AR->isAffine()) {
1671 const SCEV *Start = AR->getStart();
1672 const SCEV *Step = AR->getStepRecurrence(SE&: *this);
1673 unsigned BitWidth = getTypeSizeInBits(Ty: AR->getType());
1674 const Loop *L = AR->getLoop();
1675
1676 // If we have special knowledge that this addrec won't overflow,
1677 // we don't need to do any further analysis.
1678 if (AR->hasNoUnsignedWrap()) {
1679 Start =
1680 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, SE: this, Depth: Depth + 1);
1681 Step = getZeroExtendExpr(Op: Step, Ty, Depth: Depth + 1);
1682 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
1683 }
1684
1685 // Check whether the backedge-taken count is SCEVCouldNotCompute.
1686 // Note that this serves two purposes: It filters out loops that are
1687 // simply not analyzable, and it covers the case where this code is
1688 // being called from within backedge-taken count analysis, such that
1689 // attempting to ask for the backedge-taken count would likely result
1690 // in infinite recursion. In the later case, the analysis code will
1691 // cope with a conservative value, and it will take care to purge
1692 // that value once it has finished.
1693 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
1694 if (!isa<SCEVCouldNotCompute>(Val: MaxBECount)) {
1695 // Manually compute the final value for AR, checking for overflow.
1696
1697 // Check whether the backedge-taken count can be losslessly casted to
1698 // the addrec's type. The count is always unsigned.
1699 const SCEV *CastedMaxBECount =
1700 getTruncateOrZeroExtend(V: MaxBECount, Ty: Start->getType(), Depth);
1701 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
1702 V: CastedMaxBECount, Ty: MaxBECount->getType(), Depth);
1703 if (MaxBECount == RecastedMaxBECount) {
1704 Type *WideTy = IntegerType::get(C&: getContext(), NumBits: BitWidth * 2);
1705 // Check whether Start+Step*MaxBECount has no unsigned overflow.
1706 const SCEV *ZMul = getMulExpr(LHS: CastedMaxBECount, RHS: Step,
1707 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
1708 const SCEV *ZAdd = getZeroExtendExpr(Op: getAddExpr(LHS: Start, RHS: ZMul,
1709 Flags: SCEV::FlagAnyWrap,
1710 Depth: Depth + 1),
1711 Ty: WideTy, Depth: Depth + 1);
1712 const SCEV *WideStart = getZeroExtendExpr(Op: Start, Ty: WideTy, Depth: Depth + 1);
1713 const SCEV *WideMaxBECount =
1714 getZeroExtendExpr(Op: CastedMaxBECount, Ty: WideTy, Depth: Depth + 1);
1715 const SCEV *OperandExtendedAdd =
1716 getAddExpr(LHS: WideStart,
1717 RHS: getMulExpr(LHS: WideMaxBECount,
1718 RHS: getZeroExtendExpr(Op: Step, Ty: WideTy, Depth: Depth + 1),
1719 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1),
1720 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
1721 if (ZAdd == OperandExtendedAdd) {
1722 // Cache knowledge of AR NUW, which is propagated to this AddRec.
1723 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: SCEV::FlagNUW);
1724 // Return the expression with the addrec on the outside.
1725 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, SE: this,
1726 Depth: Depth + 1);
1727 Step = getZeroExtendExpr(Op: Step, Ty, Depth: Depth + 1);
1728 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
1729 }
1730 // Similar to above, only this time treat the step value as signed.
1731 // This covers loops that count down.
1732 OperandExtendedAdd =
1733 getAddExpr(LHS: WideStart,
1734 RHS: getMulExpr(LHS: WideMaxBECount,
1735 RHS: getSignExtendExpr(Op: Step, Ty: WideTy, Depth: Depth + 1),
1736 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1),
1737 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
1738 if (ZAdd == OperandExtendedAdd) {
1739 // Cache knowledge of AR NW, which is propagated to this AddRec.
1740 // Negative step causes unsigned wrap, but it still can't self-wrap.
1741 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: SCEV::FlagNW);
1742 // Return the expression with the addrec on the outside.
1743 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, SE: this,
1744 Depth: Depth + 1);
1745 Step = getSignExtendExpr(Op: Step, Ty, Depth: Depth + 1);
1746 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
1747 }
1748 }
1749 }
1750
1751 // Normally, in the cases we can prove no-overflow via a
1752 // backedge guarding condition, we can also compute a backedge
1753 // taken count for the loop. The exceptions are assumptions and
1754 // guards present in the loop -- SCEV is not great at exploiting
1755 // these to compute max backedge taken counts, but can still use
1756 // these to prove lack of overflow. Use this fact to avoid
1757 // doing extra work that may not pay off.
1758 if (!isa<SCEVCouldNotCompute>(Val: MaxBECount) || HasGuards ||
1759 !AC.assumptions().empty()) {
1760
1761 auto NewFlags = proveNoUnsignedWrapViaInduction(AR);
1762 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: NewFlags);
1763 if (AR->hasNoUnsignedWrap()) {
1764 // Same as nuw case above - duplicated here to avoid a compile time
1765 // issue. It's not clear that the order of checks does matter, but
1766 // it's one of two issue possible causes for a change which was
1767 // reverted. Be conservative for the moment.
1768 Start =
1769 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, SE: this, Depth: Depth + 1);
1770 Step = getZeroExtendExpr(Op: Step, Ty, Depth: Depth + 1);
1771 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
1772 }
1773
1774 // For a negative step, we can extend the operands iff doing so only
1775 // traverses values in the range zext([0,UINT_MAX]).
1776 if (isKnownNegative(S: Step)) {
1777 const SCEV *N = getConstant(Val: APInt::getMaxValue(numBits: BitWidth) -
1778 getSignedRangeMin(S: Step));
1779 if (isLoopBackedgeGuardedByCond(L, Pred: ICmpInst::ICMP_UGT, LHS: AR, RHS: N) ||
1780 isKnownOnEveryIteration(Pred: ICmpInst::ICMP_UGT, LHS: AR, RHS: N)) {
1781 // Cache knowledge of AR NW, which is propagated to this
1782 // AddRec. Negative step causes unsigned wrap, but it
1783 // still can't self-wrap.
1784 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: SCEV::FlagNW);
1785 // Return the expression with the addrec on the outside.
1786 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, SE: this,
1787 Depth: Depth + 1);
1788 Step = getSignExtendExpr(Op: Step, Ty, Depth: Depth + 1);
1789 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
1790 }
1791 }
1792 }
1793
1794 // zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step}))<nuw><nsw>
1795 // if D + (C - D + Step * n) could be proven to not unsigned wrap
1796 // where D maximizes the number of trailing zeros of (C - D + Step * n)
1797 if (const auto *SC = dyn_cast<SCEVConstant>(Val: Start)) {
1798 const APInt &C = SC->getAPInt();
1799 const APInt &D = extractConstantWithoutWrapping(SE&: *this, ConstantStart: C, Step);
1800 if (D != 0) {
1801 const SCEV *SZExtD = getZeroExtendExpr(Op: getConstant(Val: D), Ty, Depth);
1802 const SCEV *SResidual =
1803 getAddRecExpr(Start: getConstant(Val: C - D), Step, L, Flags: AR->getNoWrapFlags());
1804 const SCEV *SZExtR = getZeroExtendExpr(Op: SResidual, Ty, Depth: Depth + 1);
1805 return getAddExpr(LHS: SZExtD, RHS: SZExtR,
1806 Flags: (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1807 Depth: Depth + 1);
1808 }
1809 }
1810
1811 if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1812 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: SCEV::FlagNUW);
1813 Start =
1814 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, SE: this, Depth: Depth + 1);
1815 Step = getZeroExtendExpr(Op: Step, Ty, Depth: Depth + 1);
1816 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
1817 }
1818 }
1819
1820 // zext(A % B) --> zext(A) % zext(B)
1821 {
1822 const SCEV *LHS;
1823 const SCEV *RHS;
1824 if (match(S: Op, P: m_scev_URem(LHS: m_SCEV(V&: LHS), RHS: m_SCEV(V&: RHS), SE&: *this)))
1825 return getURemExpr(LHS: getZeroExtendExpr(Op: LHS, Ty, Depth: Depth + 1),
1826 RHS: getZeroExtendExpr(Op: RHS, Ty, Depth: Depth + 1));
1827 }
1828
1829 // zext(A / B) --> zext(A) / zext(B).
1830 if (auto *Div = dyn_cast<SCEVUDivExpr>(Val: Op))
1831 return getUDivExpr(LHS: getZeroExtendExpr(Op: Div->getLHS(), Ty, Depth: Depth + 1),
1832 RHS: getZeroExtendExpr(Op: Div->getRHS(), Ty, Depth: Depth + 1));
1833
1834 if (auto *SA = dyn_cast<SCEVAddExpr>(Val: Op)) {
1835 // zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
1836 if (SA->hasNoUnsignedWrap()) {
1837 // If the addition does not unsign overflow then we can, by definition,
1838 // commute the zero extension with the addition operation.
1839 SmallVector<SCEVUse, 4> Ops;
1840 for (SCEVUse Op : SA->operands())
1841 Ops.push_back(Elt: getZeroExtendExpr(Op, Ty, Depth: Depth + 1));
1842 return getAddExpr(Ops, Flags: SCEV::FlagNUW, Depth: Depth + 1);
1843 }
1844
1845 // zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...))
1846 // if D + (C - D + x + y + ...) could be proven to not unsigned wrap
1847 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1848 //
1849 // Often address arithmetics contain expressions like
1850 // (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))).
1851 // This transformation is useful while proving that such expressions are
1852 // equal or differ by a small constant amount, see LoadStoreVectorizer pass.
1853 if (const auto *SC = dyn_cast<SCEVConstant>(Val: SA->getOperand(i: 0))) {
1854 const APInt &D = extractConstantWithoutWrapping(SE&: *this, ConstantTerm: SC, WholeAddExpr: SA);
1855 if (D != 0) {
1856 const SCEV *SZExtD = getZeroExtendExpr(Op: getConstant(Val: D), Ty, Depth);
1857 const SCEV *SResidual =
1858 getAddExpr(LHS: getConstant(Val: -D), RHS: SA, Flags: SCEV::FlagAnyWrap, Depth);
1859 const SCEV *SZExtR = getZeroExtendExpr(Op: SResidual, Ty, Depth: Depth + 1);
1860 return getAddExpr(LHS: SZExtD, RHS: SZExtR,
1861 Flags: (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
1862 Depth: Depth + 1);
1863 }
1864 }
1865 }
1866
1867 if (auto *SM = dyn_cast<SCEVMulExpr>(Val: Op)) {
1868 // zext((A * B * ...)<nuw>) --> (zext(A) * zext(B) * ...)<nuw>
1869 if (SM->hasNoUnsignedWrap()) {
1870 // If the multiply does not unsign overflow then we can, by definition,
1871 // commute the zero extension with the multiply operation.
1872 SmallVector<SCEVUse, 4> Ops;
1873 for (SCEVUse Op : SM->operands())
1874 Ops.push_back(Elt: getZeroExtendExpr(Op, Ty, Depth: Depth + 1));
1875 return getMulExpr(Ops, Flags: SCEV::FlagNUW, Depth: Depth + 1);
1876 }
1877
1878 // zext(2^K * (trunc X to iN)) to iM ->
1879 // 2^K * (zext(trunc X to i{N-K}) to iM)<nuw>
1880 //
1881 // Proof:
1882 //
1883 // zext(2^K * (trunc X to iN)) to iM
1884 // = zext((trunc X to iN) << K) to iM
1885 // = zext((trunc X to i{N-K}) << K)<nuw> to iM
1886 // (because shl removes the top K bits)
1887 // = zext((2^K * (trunc X to i{N-K}))<nuw>) to iM
1888 // = (2^K * (zext(trunc X to i{N-K}) to iM))<nuw>.
1889 //
1890 const APInt *C;
1891 const SCEV *TruncRHS;
1892 if (match(V: SM,
1893 P: m_scev_Mul(Op0: m_scev_APInt(C), Op1: m_scev_Trunc(Op0: m_SCEV(V&: TruncRHS)))) &&
1894 C->isPowerOf2()) {
1895 int NewTruncBits =
1896 getTypeSizeInBits(Ty: SM->getOperand(i: 1)->getType()) - C->logBase2();
1897 Type *NewTruncTy = IntegerType::get(C&: getContext(), NumBits: NewTruncBits);
1898 return getMulExpr(
1899 LHS: getZeroExtendExpr(Op: SM->getOperand(i: 0), Ty),
1900 RHS: getZeroExtendExpr(Op: getTruncateExpr(Op: TruncRHS, Ty: NewTruncTy), Ty),
1901 Flags: SCEV::FlagNUW, Depth: Depth + 1);
1902 }
1903 }
1904
1905 // zext(umin(x, y)) -> umin(zext(x), zext(y))
1906 // zext(umax(x, y)) -> umax(zext(x), zext(y))
1907 if (isa<SCEVUMinExpr>(Val: Op) || isa<SCEVUMaxExpr>(Val: Op)) {
1908 auto *MinMax = cast<SCEVMinMaxExpr>(Val: Op);
1909 SmallVector<SCEVUse, 4> Operands;
1910 for (SCEVUse Operand : MinMax->operands())
1911 Operands.push_back(Elt: getZeroExtendExpr(Op: Operand, Ty));
1912 if (isa<SCEVUMinExpr>(Val: MinMax))
1913 return getUMinExpr(Operands);
1914 return getUMaxExpr(Operands);
1915 }
1916
1917 // zext(umin_seq(x, y)) -> umin_seq(zext(x), zext(y))
1918 if (auto *MinMax = dyn_cast<SCEVSequentialMinMaxExpr>(Val: Op)) {
1919 assert(isa<SCEVSequentialUMinExpr>(MinMax) && "Not supported!");
1920 SmallVector<SCEVUse, 4> Operands;
1921 for (SCEVUse Operand : MinMax->operands())
1922 Operands.push_back(Elt: getZeroExtendExpr(Op: Operand, Ty));
1923 return getUMinExpr(Operands, /*Sequential*/ true);
1924 }
1925
1926 // The cast wasn't folded; create an explicit cast node.
1927 // Recompute the insert position, as it may have been invalidated.
1928 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) return S;
1929 SCEV *S = new (SCEVAllocator) SCEVZeroExtendExpr(ID.Intern(Allocator&: SCEVAllocator),
1930 Op, Ty);
1931 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
1932 registerUser(User: S, Ops: Op);
1933 return S;
1934}
1935
1936const SCEV *
1937ScalarEvolution::getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth) {
1938 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1939 "This is not an extending conversion!");
1940 assert(isSCEVable(Ty) &&
1941 "This is not a conversion to a SCEVable type!");
1942 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1943 Ty = getEffectiveSCEVType(Ty);
1944
1945 FoldID ID(scSignExtend, Op, Ty);
1946 if (const SCEV *S = FoldCache.lookup(Val: ID))
1947 return S;
1948
1949 const SCEV *S = getSignExtendExprImpl(Op, Ty, Depth);
1950 if (!isa<SCEVSignExtendExpr>(Val: S))
1951 insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser);
1952 return S;
1953}
1954
1955const SCEV *ScalarEvolution::getSignExtendExprImpl(const SCEV *Op, Type *Ty,
1956 unsigned Depth) {
1957 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
1958 "This is not an extending conversion!");
1959 assert(isSCEVable(Ty) && "This is not a conversion to a SCEVable type!");
1960 assert(!Op->getType()->isPointerTy() && "Can't extend pointer!");
1961 Ty = getEffectiveSCEVType(Ty);
1962
1963 // Fold if the operand is constant.
1964 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Val: Op))
1965 return getConstant(Val: SC->getAPInt().sext(width: getTypeSizeInBits(Ty)));
1966
1967 // sext(sext(x)) --> sext(x)
1968 if (const SCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Val: Op))
1969 return getSignExtendExpr(Op: SS->getOperand(), Ty, Depth: Depth + 1);
1970
1971 // sext(zext(x)) --> zext(x)
1972 if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Val: Op))
1973 return getZeroExtendExpr(Op: SZ->getOperand(), Ty, Depth: Depth + 1);
1974
1975 // Before doing any expensive analysis, check to see if we've already
1976 // computed a SCEV for this Op and Ty.
1977 FoldingSetNodeID ID;
1978 ID.AddInteger(I: scSignExtend);
1979 ID.AddPointer(Ptr: Op);
1980 ID.AddPointer(Ptr: Ty);
1981 void *IP = nullptr;
1982 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) return S;
1983 // Limit recursion depth.
1984 if (Depth > MaxCastDepth) {
1985 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(Allocator&: SCEVAllocator),
1986 Op, Ty);
1987 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
1988 registerUser(User: S, Ops: Op);
1989 return S;
1990 }
1991
1992 // sext(trunc(x)) --> sext(x) or x or trunc(x)
1993 if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Val: Op)) {
1994 // It's possible the bits taken off by the truncate were all sign bits. If
1995 // so, we should be able to simplify this further.
1996 const SCEV *X = ST->getOperand();
1997 ConstantRange CR = getSignedRange(S: X);
1998 unsigned TruncBits = getTypeSizeInBits(Ty: ST->getType());
1999 unsigned NewBits = getTypeSizeInBits(Ty);
2000 if (CR.truncate(BitWidth: TruncBits).signExtend(BitWidth: NewBits).contains(
2001 CR: CR.sextOrTrunc(BitWidth: NewBits)))
2002 return getTruncateOrSignExtend(V: X, Ty, Depth);
2003 }
2004
2005 if (auto *SA = dyn_cast<SCEVAddExpr>(Val: Op)) {
2006 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
2007 if (SA->hasNoSignedWrap()) {
2008 // If the addition does not sign overflow then we can, by definition,
2009 // commute the sign extension with the addition operation.
2010 SmallVector<SCEVUse, 4> Ops;
2011 for (SCEVUse Op : SA->operands())
2012 Ops.push_back(Elt: getSignExtendExpr(Op, Ty, Depth: Depth + 1));
2013 return getAddExpr(Ops, Flags: SCEV::FlagNSW, Depth: Depth + 1);
2014 }
2015
2016 // sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...))
2017 // if D + (C - D + x + y + ...) could be proven to not signed wrap
2018 // where D maximizes the number of trailing zeros of (C - D + x + y + ...)
2019 //
2020 // For instance, this will bring two seemingly different expressions:
2021 // 1 + sext(5 + 20 * %x + 24 * %y) and
2022 // sext(6 + 20 * %x + 24 * %y)
2023 // to the same form:
2024 // 2 + sext(4 + 20 * %x + 24 * %y)
2025 if (const auto *SC = dyn_cast<SCEVConstant>(Val: SA->getOperand(i: 0))) {
2026 const APInt &D = extractConstantWithoutWrapping(SE&: *this, ConstantTerm: SC, WholeAddExpr: SA);
2027 if (D != 0) {
2028 const SCEV *SSExtD = getSignExtendExpr(Op: getConstant(Val: D), Ty, Depth);
2029 const SCEV *SResidual =
2030 getAddExpr(LHS: getConstant(Val: -D), RHS: SA, Flags: SCEV::FlagAnyWrap, Depth);
2031 const SCEV *SSExtR = getSignExtendExpr(Op: SResidual, Ty, Depth: Depth + 1);
2032 return getAddExpr(LHS: SSExtD, RHS: SSExtR,
2033 Flags: (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
2034 Depth: Depth + 1);
2035 }
2036 }
2037 }
2038 // If the input value is a chrec scev, and we can prove that the value
2039 // did not overflow the old, smaller, value, we can sign extend all of the
2040 // operands (often constants). This allows analysis of something like
2041 // this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
2042 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: Op))
2043 if (AR->isAffine()) {
2044 const SCEV *Start = AR->getStart();
2045 const SCEV *Step = AR->getStepRecurrence(SE&: *this);
2046 unsigned BitWidth = getTypeSizeInBits(Ty: AR->getType());
2047 const Loop *L = AR->getLoop();
2048
2049 // If we have special knowledge that this addrec won't overflow,
2050 // we don't need to do any further analysis.
2051 if (AR->hasNoSignedWrap()) {
2052 Start =
2053 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, SE: this, Depth: Depth + 1);
2054 Step = getSignExtendExpr(Op: Step, Ty, Depth: Depth + 1);
2055 return getAddRecExpr(Start, Step, L, Flags: SCEV::FlagNSW);
2056 }
2057
2058 // Check whether the backedge-taken count is SCEVCouldNotCompute.
2059 // Note that this serves two purposes: It filters out loops that are
2060 // simply not analyzable, and it covers the case where this code is
2061 // being called from within backedge-taken count analysis, such that
2062 // attempting to ask for the backedge-taken count would likely result
2063 // in infinite recursion. In the later case, the analysis code will
2064 // cope with a conservative value, and it will take care to purge
2065 // that value once it has finished.
2066 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
2067 if (!isa<SCEVCouldNotCompute>(Val: MaxBECount)) {
2068 // Manually compute the final value for AR, checking for
2069 // overflow.
2070
2071 // Check whether the backedge-taken count can be losslessly casted to
2072 // the addrec's type. The count is always unsigned.
2073 const SCEV *CastedMaxBECount =
2074 getTruncateOrZeroExtend(V: MaxBECount, Ty: Start->getType(), Depth);
2075 const SCEV *RecastedMaxBECount = getTruncateOrZeroExtend(
2076 V: CastedMaxBECount, Ty: MaxBECount->getType(), Depth);
2077 if (MaxBECount == RecastedMaxBECount) {
2078 Type *WideTy = IntegerType::get(C&: getContext(), NumBits: BitWidth * 2);
2079 // Check whether Start+Step*MaxBECount has no signed overflow.
2080 const SCEV *SMul = getMulExpr(LHS: CastedMaxBECount, RHS: Step,
2081 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2082 const SCEV *SAdd = getSignExtendExpr(Op: getAddExpr(LHS: Start, RHS: SMul,
2083 Flags: SCEV::FlagAnyWrap,
2084 Depth: Depth + 1),
2085 Ty: WideTy, Depth: Depth + 1);
2086 const SCEV *WideStart = getSignExtendExpr(Op: Start, Ty: WideTy, Depth: Depth + 1);
2087 const SCEV *WideMaxBECount =
2088 getZeroExtendExpr(Op: CastedMaxBECount, Ty: WideTy, Depth: Depth + 1);
2089 const SCEV *OperandExtendedAdd =
2090 getAddExpr(LHS: WideStart,
2091 RHS: getMulExpr(LHS: WideMaxBECount,
2092 RHS: getSignExtendExpr(Op: Step, Ty: WideTy, Depth: Depth + 1),
2093 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1),
2094 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2095 if (SAdd == OperandExtendedAdd) {
2096 // Cache knowledge of AR NSW, which is propagated to this AddRec.
2097 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: SCEV::FlagNSW);
2098 // Return the expression with the addrec on the outside.
2099 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, SE: this,
2100 Depth: Depth + 1);
2101 Step = getSignExtendExpr(Op: Step, Ty, Depth: Depth + 1);
2102 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
2103 }
2104 // Similar to above, only this time treat the step value as unsigned.
2105 // This covers loops that count up with an unsigned step.
2106 OperandExtendedAdd =
2107 getAddExpr(LHS: WideStart,
2108 RHS: getMulExpr(LHS: WideMaxBECount,
2109 RHS: getZeroExtendExpr(Op: Step, Ty: WideTy, Depth: Depth + 1),
2110 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1),
2111 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2112 if (SAdd == OperandExtendedAdd) {
2113 // If AR wraps around then
2114 //
2115 // abs(Step) * MaxBECount > unsigned-max(AR->getType())
2116 // => SAdd != OperandExtendedAdd
2117 //
2118 // Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
2119 // (SAdd == OperandExtendedAdd => AR is NW)
2120
2121 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: SCEV::FlagNW);
2122
2123 // Return the expression with the addrec on the outside.
2124 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, SE: this,
2125 Depth: Depth + 1);
2126 Step = getZeroExtendExpr(Op: Step, Ty, Depth: Depth + 1);
2127 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
2128 }
2129 }
2130 }
2131
2132 auto NewFlags = proveNoSignedWrapViaInduction(AR);
2133 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: NewFlags);
2134 if (AR->hasNoSignedWrap()) {
2135 // Same as nsw case above - duplicated here to avoid a compile time
2136 // issue. It's not clear that the order of checks does matter, but
2137 // it's one of two issue possible causes for a change which was
2138 // reverted. Be conservative for the moment.
2139 Start =
2140 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, SE: this, Depth: Depth + 1);
2141 Step = getSignExtendExpr(Op: Step, Ty, Depth: Depth + 1);
2142 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
2143 }
2144
2145 // sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step}))<nuw><nsw>
2146 // if D + (C - D + Step * n) could be proven to not signed wrap
2147 // where D maximizes the number of trailing zeros of (C - D + Step * n)
2148 if (const auto *SC = dyn_cast<SCEVConstant>(Val: Start)) {
2149 const APInt &C = SC->getAPInt();
2150 const APInt &D = extractConstantWithoutWrapping(SE&: *this, ConstantStart: C, Step);
2151 if (D != 0) {
2152 const SCEV *SSExtD = getSignExtendExpr(Op: getConstant(Val: D), Ty, Depth);
2153 const SCEV *SResidual =
2154 getAddRecExpr(Start: getConstant(Val: C - D), Step, L, Flags: AR->getNoWrapFlags());
2155 const SCEV *SSExtR = getSignExtendExpr(Op: SResidual, Ty, Depth: Depth + 1);
2156 return getAddExpr(LHS: SSExtD, RHS: SSExtR,
2157 Flags: (SCEV::NoWrapFlags)(SCEV::FlagNSW | SCEV::FlagNUW),
2158 Depth: Depth + 1);
2159 }
2160 }
2161
2162 if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
2163 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags: SCEV::FlagNSW);
2164 Start =
2165 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty, SE: this, Depth: Depth + 1);
2166 Step = getSignExtendExpr(Op: Step, Ty, Depth: Depth + 1);
2167 return getAddRecExpr(Start, Step, L, Flags: AR->getNoWrapFlags());
2168 }
2169 }
2170
2171 // If the input value is provably positive and we could not simplify
2172 // away the sext build a zext instead.
2173 if (isKnownNonNegative(S: Op))
2174 return getZeroExtendExpr(Op, Ty, Depth: Depth + 1);
2175
2176 // sext(smin(x, y)) -> smin(sext(x), sext(y))
2177 // sext(smax(x, y)) -> smax(sext(x), sext(y))
2178 if (isa<SCEVSMinExpr>(Val: Op) || isa<SCEVSMaxExpr>(Val: Op)) {
2179 auto *MinMax = cast<SCEVMinMaxExpr>(Val: Op);
2180 SmallVector<SCEVUse, 4> Operands;
2181 for (SCEVUse Operand : MinMax->operands())
2182 Operands.push_back(Elt: getSignExtendExpr(Op: Operand, Ty));
2183 if (isa<SCEVSMinExpr>(Val: MinMax))
2184 return getSMinExpr(Operands);
2185 return getSMaxExpr(Operands);
2186 }
2187
2188 // The cast wasn't folded; create an explicit cast node.
2189 // Recompute the insert position, as it may have been invalidated.
2190 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) return S;
2191 SCEV *S = new (SCEVAllocator) SCEVSignExtendExpr(ID.Intern(Allocator&: SCEVAllocator),
2192 Op, Ty);
2193 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
2194 registerUser(User: S, Ops: Op);
2195 return S;
2196}
2197
2198const SCEV *ScalarEvolution::getCastExpr(SCEVTypes Kind, const SCEV *Op,
2199 Type *Ty) {
2200 switch (Kind) {
2201 case scTruncate:
2202 return getTruncateExpr(Op, Ty);
2203 case scZeroExtend:
2204 return getZeroExtendExpr(Op, Ty);
2205 case scSignExtend:
2206 return getSignExtendExpr(Op, Ty);
2207 case scPtrToInt:
2208 return getPtrToIntExpr(Op, Ty);
2209 default:
2210 llvm_unreachable("Not a SCEV cast expression!");
2211 }
2212}
2213
2214/// getAnyExtendExpr - Return a SCEV for the given operand extended with
2215/// unspecified bits out to the given type.
2216const SCEV *ScalarEvolution::getAnyExtendExpr(const SCEV *Op,
2217 Type *Ty) {
2218 assert(getTypeSizeInBits(Op->getType()) < getTypeSizeInBits(Ty) &&
2219 "This is not an extending conversion!");
2220 assert(isSCEVable(Ty) &&
2221 "This is not a conversion to a SCEVable type!");
2222 Ty = getEffectiveSCEVType(Ty);
2223
2224 // Sign-extend negative constants.
2225 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Val: Op))
2226 if (SC->getAPInt().isNegative())
2227 return getSignExtendExpr(Op, Ty);
2228
2229 // Peel off a truncate cast.
2230 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Val: Op)) {
2231 const SCEV *NewOp = T->getOperand();
2232 if (getTypeSizeInBits(Ty: NewOp->getType()) < getTypeSizeInBits(Ty))
2233 return getAnyExtendExpr(Op: NewOp, Ty);
2234 return getTruncateOrNoop(V: NewOp, Ty);
2235 }
2236
2237 // Next try a zext cast. If the cast is folded, use it.
2238 const SCEV *ZExt = getZeroExtendExpr(Op, Ty);
2239 if (!isa<SCEVZeroExtendExpr>(Val: ZExt))
2240 return ZExt;
2241
2242 // Next try a sext cast. If the cast is folded, use it.
2243 const SCEV *SExt = getSignExtendExpr(Op, Ty);
2244 if (!isa<SCEVSignExtendExpr>(Val: SExt))
2245 return SExt;
2246
2247 // Force the cast to be folded into the operands of an addrec.
2248 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: Op)) {
2249 SmallVector<SCEVUse, 4> Ops;
2250 for (const SCEV *Op : AR->operands())
2251 Ops.push_back(Elt: getAnyExtendExpr(Op, Ty));
2252 return getAddRecExpr(Operands&: Ops, L: AR->getLoop(), Flags: SCEV::FlagNW);
2253 }
2254
2255 // If the expression is obviously signed, use the sext cast value.
2256 if (isa<SCEVSMaxExpr>(Val: Op))
2257 return SExt;
2258
2259 // Absent any other information, use the zext cast value.
2260 return ZExt;
2261}
2262
2263/// Process the given Ops list, which is a list of operands to be added under
2264/// the given scale, update the given map. This is a helper function for
2265/// getAddRecExpr. As an example of what it does, given a sequence of operands
2266/// that would form an add expression like this:
2267///
2268/// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
2269///
2270/// where A and B are constants, update the map with these values:
2271///
2272/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
2273///
2274/// and add 13 + A*B*29 to AccumulatedConstant.
2275/// This will allow getAddRecExpr to produce this:
2276///
2277/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
2278///
2279/// This form often exposes folding opportunities that are hidden in
2280/// the original operand list.
2281///
2282/// Return true iff it appears that any interesting folding opportunities
2283/// may be exposed. This helps getAddRecExpr short-circuit extra work in
2284/// the common case where no interesting opportunities are present, and
2285/// is also used as a check to avoid infinite recursion.
2286static bool CollectAddOperandsWithScales(SmallDenseMap<SCEVUse, APInt, 16> &M,
2287 SmallVectorImpl<SCEVUse> &NewOps,
2288 APInt &AccumulatedConstant,
2289 ArrayRef<SCEVUse> Ops,
2290 const APInt &Scale,
2291 ScalarEvolution &SE) {
2292 bool Interesting = false;
2293
2294 // Iterate over the add operands. They are sorted, with constants first.
2295 unsigned i = 0;
2296 while (const SCEVConstant *C = dyn_cast<SCEVConstant>(Val: Ops[i])) {
2297 ++i;
2298 // Pull a buried constant out to the outside.
2299 if (Scale != 1 || AccumulatedConstant != 0 || C->getValue()->isZero())
2300 Interesting = true;
2301 AccumulatedConstant += Scale * C->getAPInt();
2302 }
2303
2304 // Next comes everything else. We're especially interested in multiplies
2305 // here, but they're in the middle, so just visit the rest with one loop.
2306 for (; i != Ops.size(); ++i) {
2307 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Val: Ops[i]);
2308 if (Mul && isa<SCEVConstant>(Val: Mul->getOperand(i: 0))) {
2309 APInt NewScale =
2310 Scale * cast<SCEVConstant>(Val: Mul->getOperand(i: 0))->getAPInt();
2311 if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Val: Mul->getOperand(i: 1))) {
2312 // A multiplication of a constant with another add; recurse.
2313 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Val: Mul->getOperand(i: 1));
2314 Interesting |= CollectAddOperandsWithScales(
2315 M, NewOps, AccumulatedConstant, Ops: Add->operands(), Scale: NewScale, SE);
2316 } else {
2317 // A multiplication of a constant with some other value. Update
2318 // the map.
2319 SmallVector<SCEVUse, 4> MulOps(drop_begin(RangeOrContainer: Mul->operands()));
2320 const SCEV *Key = SE.getMulExpr(Ops&: MulOps);
2321 auto Pair = M.insert(KV: {Key, NewScale});
2322 if (Pair.second) {
2323 NewOps.push_back(Elt: Pair.first->first);
2324 } else {
2325 Pair.first->second += NewScale;
2326 // The map already had an entry for this value, which may indicate
2327 // a folding opportunity.
2328 Interesting = true;
2329 }
2330 }
2331 } else {
2332 // An ordinary operand. Update the map.
2333 auto Pair = M.insert(KV: {Ops[i], Scale});
2334 if (Pair.second) {
2335 NewOps.push_back(Elt: Pair.first->first);
2336 } else {
2337 Pair.first->second += Scale;
2338 // The map already had an entry for this value, which may indicate
2339 // a folding opportunity.
2340 Interesting = true;
2341 }
2342 }
2343 }
2344
2345 return Interesting;
2346}
2347
2348bool ScalarEvolution::willNotOverflow(Instruction::BinaryOps BinOp, bool Signed,
2349 const SCEV *LHS, const SCEV *RHS,
2350 const Instruction *CtxI) {
2351 const SCEV *(ScalarEvolution::*Operation)(SCEVUse, SCEVUse, SCEV::NoWrapFlags,
2352 unsigned);
2353 switch (BinOp) {
2354 default:
2355 llvm_unreachable("Unsupported binary op");
2356 case Instruction::Add:
2357 Operation = &ScalarEvolution::getAddExpr;
2358 break;
2359 case Instruction::Sub:
2360 Operation = &ScalarEvolution::getMinusSCEV;
2361 break;
2362 case Instruction::Mul:
2363 Operation = &ScalarEvolution::getMulExpr;
2364 break;
2365 }
2366
2367 const SCEV *(ScalarEvolution::*Extension)(const SCEV *, Type *, unsigned) =
2368 Signed ? &ScalarEvolution::getSignExtendExpr
2369 : &ScalarEvolution::getZeroExtendExpr;
2370
2371 // Check ext(LHS op RHS) == ext(LHS) op ext(RHS)
2372 auto *NarrowTy = cast<IntegerType>(Val: LHS->getType());
2373 auto *WideTy =
2374 IntegerType::get(C&: NarrowTy->getContext(), NumBits: NarrowTy->getBitWidth() * 2);
2375
2376 const SCEV *A = (this->*Extension)(
2377 (this->*Operation)(LHS, RHS, SCEV::FlagAnyWrap, 0), WideTy, 0);
2378 const SCEV *LHSB = (this->*Extension)(LHS, WideTy, 0);
2379 const SCEV *RHSB = (this->*Extension)(RHS, WideTy, 0);
2380 const SCEV *B = (this->*Operation)(LHSB, RHSB, SCEV::FlagAnyWrap, 0);
2381 if (A == B)
2382 return true;
2383 // Can we use context to prove the fact we need?
2384 if (!CtxI)
2385 return false;
2386 // TODO: Support mul.
2387 if (BinOp == Instruction::Mul)
2388 return false;
2389 auto *RHSC = dyn_cast<SCEVConstant>(Val: RHS);
2390 // TODO: Lift this limitation.
2391 if (!RHSC)
2392 return false;
2393 APInt C = RHSC->getAPInt();
2394 unsigned NumBits = C.getBitWidth();
2395 bool IsSub = (BinOp == Instruction::Sub);
2396 bool IsNegativeConst = (Signed && C.isNegative());
2397 // Compute the direction and magnitude by which we need to check overflow.
2398 bool OverflowDown = IsSub ^ IsNegativeConst;
2399 APInt Magnitude = C;
2400 if (IsNegativeConst) {
2401 if (C == APInt::getSignedMinValue(numBits: NumBits))
2402 // TODO: SINT_MIN on inversion gives the same negative value, we don't
2403 // want to deal with that.
2404 return false;
2405 Magnitude = -C;
2406 }
2407
2408 ICmpInst::Predicate Pred = Signed ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
2409 if (OverflowDown) {
2410 // To avoid overflow down, we need to make sure that MIN + Magnitude <= LHS.
2411 APInt Min = Signed ? APInt::getSignedMinValue(numBits: NumBits)
2412 : APInt::getMinValue(numBits: NumBits);
2413 APInt Limit = Min + Magnitude;
2414 return isKnownPredicateAt(Pred, LHS: getConstant(Val: Limit), RHS: LHS, CtxI);
2415 } else {
2416 // To avoid overflow up, we need to make sure that LHS <= MAX - Magnitude.
2417 APInt Max = Signed ? APInt::getSignedMaxValue(numBits: NumBits)
2418 : APInt::getMaxValue(numBits: NumBits);
2419 APInt Limit = Max - Magnitude;
2420 return isKnownPredicateAt(Pred, LHS, RHS: getConstant(Val: Limit), CtxI);
2421 }
2422}
2423
2424std::optional<SCEV::NoWrapFlags>
2425ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp(
2426 const OverflowingBinaryOperator *OBO) {
2427 // It cannot be done any better.
2428 if (OBO->hasNoUnsignedWrap() && OBO->hasNoSignedWrap())
2429 return std::nullopt;
2430
2431 SCEV::NoWrapFlags Flags = SCEV::NoWrapFlags::FlagAnyWrap;
2432
2433 if (OBO->hasNoUnsignedWrap())
2434 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNUW);
2435 if (OBO->hasNoSignedWrap())
2436 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNSW);
2437
2438 bool Deduced = false;
2439
2440 if (OBO->getOpcode() != Instruction::Add &&
2441 OBO->getOpcode() != Instruction::Sub &&
2442 OBO->getOpcode() != Instruction::Mul)
2443 return std::nullopt;
2444
2445 const SCEV *LHS = getSCEV(V: OBO->getOperand(i_nocapture: 0));
2446 const SCEV *RHS = getSCEV(V: OBO->getOperand(i_nocapture: 1));
2447
2448 const Instruction *CtxI =
2449 UseContextForNoWrapFlagInference ? dyn_cast<Instruction>(Val: OBO) : nullptr;
2450 if (!OBO->hasNoUnsignedWrap() &&
2451 willNotOverflow(BinOp: (Instruction::BinaryOps)OBO->getOpcode(),
2452 /* Signed */ false, LHS, RHS, CtxI)) {
2453 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNUW);
2454 Deduced = true;
2455 }
2456
2457 if (!OBO->hasNoSignedWrap() &&
2458 willNotOverflow(BinOp: (Instruction::BinaryOps)OBO->getOpcode(),
2459 /* Signed */ true, LHS, RHS, CtxI)) {
2460 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNSW);
2461 Deduced = true;
2462 }
2463
2464 if (Deduced)
2465 return Flags;
2466 return std::nullopt;
2467}
2468
2469// We're trying to construct a SCEV of type `Type' with `Ops' as operands and
2470// `OldFlags' as can't-wrap behavior. Infer a more aggressive set of
2471// can't-overflow flags for the operation if possible.
2472static SCEV::NoWrapFlags StrengthenNoWrapFlags(ScalarEvolution *SE,
2473 SCEVTypes Type,
2474 ArrayRef<SCEVUse> Ops,
2475 SCEV::NoWrapFlags Flags) {
2476 using namespace std::placeholders;
2477
2478 using OBO = OverflowingBinaryOperator;
2479
2480 bool CanAnalyze =
2481 Type == scAddExpr || Type == scAddRecExpr || Type == scMulExpr;
2482 (void)CanAnalyze;
2483 assert(CanAnalyze && "don't call from other places!");
2484
2485 int SignOrUnsignMask = SCEV::FlagNUW | SCEV::FlagNSW;
2486 SCEV::NoWrapFlags SignOrUnsignWrap =
2487 ScalarEvolution::maskFlags(Flags, Mask: SignOrUnsignMask);
2488
2489 // If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
2490 auto IsKnownNonNegative = [&](SCEVUse U) {
2491 return SE->isKnownNonNegative(S: U);
2492 };
2493
2494 if (SignOrUnsignWrap == SCEV::FlagNSW && all_of(Range&: Ops, P: IsKnownNonNegative))
2495 Flags =
2496 ScalarEvolution::setFlags(Flags, OnFlags: (SCEV::NoWrapFlags)SignOrUnsignMask);
2497
2498 SignOrUnsignWrap = ScalarEvolution::maskFlags(Flags, Mask: SignOrUnsignMask);
2499
2500 if (SignOrUnsignWrap != SignOrUnsignMask &&
2501 (Type == scAddExpr || Type == scMulExpr) && Ops.size() == 2 &&
2502 isa<SCEVConstant>(Val: Ops[0])) {
2503
2504 auto Opcode = [&] {
2505 switch (Type) {
2506 case scAddExpr:
2507 return Instruction::Add;
2508 case scMulExpr:
2509 return Instruction::Mul;
2510 default:
2511 llvm_unreachable("Unexpected SCEV op.");
2512 }
2513 }();
2514
2515 const APInt &C = cast<SCEVConstant>(Val: Ops[0])->getAPInt();
2516
2517 // (A <opcode> C) --> (A <opcode> C)<nsw> if the op doesn't sign overflow.
2518 if (!(SignOrUnsignWrap & SCEV::FlagNSW)) {
2519 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2520 BinOp: Opcode, Other: C, NoWrapKind: OBO::NoSignedWrap);
2521 if (NSWRegion.contains(CR: SE->getSignedRange(S: Ops[1])))
2522 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNSW);
2523 }
2524
2525 // (A <opcode> C) --> (A <opcode> C)<nuw> if the op doesn't unsign overflow.
2526 if (!(SignOrUnsignWrap & SCEV::FlagNUW)) {
2527 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
2528 BinOp: Opcode, Other: C, NoWrapKind: OBO::NoUnsignedWrap);
2529 if (NUWRegion.contains(CR: SE->getUnsignedRange(S: Ops[1])))
2530 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNUW);
2531 }
2532 }
2533
2534 // <0,+,nonnegative><nw> is also nuw
2535 // TODO: Add corresponding nsw case
2536 if (Type == scAddRecExpr && ScalarEvolution::hasFlags(Flags, TestFlags: SCEV::FlagNW) &&
2537 !ScalarEvolution::hasFlags(Flags, TestFlags: SCEV::FlagNUW) && Ops.size() == 2 &&
2538 Ops[0]->isZero() && IsKnownNonNegative(Ops[1]))
2539 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNUW);
2540
2541 // both (udiv X, Y) * Y and Y * (udiv X, Y) are always NUW
2542 if (Type == scMulExpr && !ScalarEvolution::hasFlags(Flags, TestFlags: SCEV::FlagNUW) &&
2543 Ops.size() == 2) {
2544 if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Val: Ops[0]))
2545 if (UDiv->getOperand(i: 1) == Ops[1])
2546 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNUW);
2547 if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Val: Ops[1]))
2548 if (UDiv->getOperand(i: 1) == Ops[0])
2549 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNUW);
2550 }
2551
2552 return Flags;
2553}
2554
2555bool ScalarEvolution::isAvailableAtLoopEntry(const SCEV *S, const Loop *L) {
2556 return isLoopInvariant(S, L) && properlyDominates(S, BB: L->getHeader());
2557}
2558
2559/// Get a canonical add expression, or something simpler if possible.
2560const SCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<SCEVUse> &Ops,
2561 SCEV::NoWrapFlags OrigFlags,
2562 unsigned Depth) {
2563 assert(!(OrigFlags & ~(SCEV::FlagNUW | SCEV::FlagNSW)) &&
2564 "only nuw or nsw allowed");
2565 assert(!Ops.empty() && "Cannot get empty add!");
2566 if (Ops.size() == 1) return Ops[0];
2567#ifndef NDEBUG
2568 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
2569 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2570 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2571 "SCEVAddExpr operand types don't match!");
2572 unsigned NumPtrs = count_if(
2573 Ops, [](const SCEV *Op) { return Op->getType()->isPointerTy(); });
2574 assert(NumPtrs <= 1 && "add has at most one pointer operand");
2575#endif
2576
2577 const SCEV *Folded = constantFoldAndGroupOps(
2578 SE&: *this, LI, DT, Ops,
2579 Fold: [](const APInt &C1, const APInt &C2) { return C1 + C2; },
2580 IsIdentity: [](const APInt &C) { return C.isZero(); }, // identity
2581 IsAbsorber: [](const APInt &C) { return false; }); // absorber
2582 if (Folded)
2583 return Folded;
2584
2585 unsigned Idx = isa<SCEVConstant>(Val: Ops[0]) ? 1 : 0;
2586
2587 // Delay expensive flag strengthening until necessary.
2588 auto ComputeFlags = [this, OrigFlags](ArrayRef<SCEVUse> Ops) {
2589 return StrengthenNoWrapFlags(SE: this, Type: scAddExpr, Ops, Flags: OrigFlags);
2590 };
2591
2592 // Limit recursion calls depth.
2593 if (Depth > MaxArithDepth || hasHugeExpression(Ops))
2594 return getOrCreateAddExpr(Ops, Flags: ComputeFlags(Ops));
2595
2596 if (SCEV *S = findExistingSCEVInCache(SCEVType: scAddExpr, Ops)) {
2597 // Don't strengthen flags if we have no new information.
2598 SCEVAddExpr *Add = static_cast<SCEVAddExpr *>(S);
2599 if (Add->getNoWrapFlags(Mask: OrigFlags) != OrigFlags)
2600 Add->setNoWrapFlags(ComputeFlags(Ops));
2601 return S;
2602 }
2603
2604 // Okay, check to see if the same value occurs in the operand list more than
2605 // once. If so, merge them together into an multiply expression. Since we
2606 // sorted the list, these values are required to be adjacent.
2607 Type *Ty = Ops[0]->getType();
2608 bool FoundMatch = false;
2609 for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
2610 if (Ops[i] == Ops[i+1]) { // X + Y + Y --> X + Y*2
2611 // Scan ahead to count how many equal operands there are.
2612 unsigned Count = 2;
2613 while (i+Count != e && Ops[i+Count] == Ops[i])
2614 ++Count;
2615 // Merge the values into a multiply.
2616 SCEVUse Scale = getConstant(Ty, V: Count);
2617 const SCEV *Mul = getMulExpr(LHS: Scale, RHS: Ops[i], Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2618 if (Ops.size() == Count)
2619 return Mul;
2620 Ops[i] = Mul;
2621 Ops.erase(CS: Ops.begin()+i+1, CE: Ops.begin()+i+Count);
2622 --i; e -= Count - 1;
2623 FoundMatch = true;
2624 }
2625 if (FoundMatch)
2626 return getAddExpr(Ops, OrigFlags, Depth: Depth + 1);
2627
2628 // Check for truncates. If all the operands are truncated from the same
2629 // type, see if factoring out the truncate would permit the result to be
2630 // folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y)
2631 // if the contents of the resulting outer trunc fold to something simple.
2632 auto FindTruncSrcType = [&]() -> Type * {
2633 // We're ultimately looking to fold an addrec of truncs and muls of only
2634 // constants and truncs, so if we find any other types of SCEV
2635 // as operands of the addrec then we bail and return nullptr here.
2636 // Otherwise, we return the type of the operand of a trunc that we find.
2637 if (auto *T = dyn_cast<SCEVTruncateExpr>(Val&: Ops[Idx]))
2638 return T->getOperand()->getType();
2639 if (const auto *Mul = dyn_cast<SCEVMulExpr>(Val&: Ops[Idx])) {
2640 SCEVUse LastOp = Mul->getOperand(i: Mul->getNumOperands() - 1);
2641 if (const auto *T = dyn_cast<SCEVTruncateExpr>(Val&: LastOp))
2642 return T->getOperand()->getType();
2643 }
2644 return nullptr;
2645 };
2646 if (auto *SrcType = FindTruncSrcType()) {
2647 SmallVector<SCEVUse, 8> LargeOps;
2648 bool Ok = true;
2649 // Check all the operands to see if they can be represented in the
2650 // source type of the truncate.
2651 for (const SCEV *Op : Ops) {
2652 if (const SCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Val: Op)) {
2653 if (T->getOperand()->getType() != SrcType) {
2654 Ok = false;
2655 break;
2656 }
2657 LargeOps.push_back(Elt: T->getOperand());
2658 } else if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Val: Op)) {
2659 LargeOps.push_back(Elt: getAnyExtendExpr(Op: C, Ty: SrcType));
2660 } else if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Val: Op)) {
2661 SmallVector<SCEVUse, 8> LargeMulOps;
2662 for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2663 if (const SCEVTruncateExpr *T =
2664 dyn_cast<SCEVTruncateExpr>(Val: M->getOperand(i: j))) {
2665 if (T->getOperand()->getType() != SrcType) {
2666 Ok = false;
2667 break;
2668 }
2669 LargeMulOps.push_back(Elt: T->getOperand());
2670 } else if (const auto *C = dyn_cast<SCEVConstant>(Val: M->getOperand(i: j))) {
2671 LargeMulOps.push_back(Elt: getAnyExtendExpr(Op: C, Ty: SrcType));
2672 } else {
2673 Ok = false;
2674 break;
2675 }
2676 }
2677 if (Ok)
2678 LargeOps.push_back(Elt: getMulExpr(Ops&: LargeMulOps, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1));
2679 } else {
2680 Ok = false;
2681 break;
2682 }
2683 }
2684 if (Ok) {
2685 // Evaluate the expression in the larger type.
2686 const SCEV *Fold = getAddExpr(Ops&: LargeOps, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2687 // If it folds to something simple, use it. Otherwise, don't.
2688 if (isa<SCEVConstant>(Val: Fold) || isa<SCEVUnknown>(Val: Fold))
2689 return getTruncateExpr(Op: Fold, Ty);
2690 }
2691 }
2692
2693 if (Ops.size() == 2) {
2694 // Check if we have an expression of the form ((X + C1) - C2), where C1 and
2695 // C2 can be folded in a way that allows retaining wrapping flags of (X +
2696 // C1).
2697 const SCEV *A = Ops[0];
2698 const SCEV *B = Ops[1];
2699 auto *AddExpr = dyn_cast<SCEVAddExpr>(Val: B);
2700 auto *C = dyn_cast<SCEVConstant>(Val: A);
2701 if (AddExpr && C && isa<SCEVConstant>(Val: AddExpr->getOperand(i: 0))) {
2702 auto C1 = cast<SCEVConstant>(Val: AddExpr->getOperand(i: 0))->getAPInt();
2703 auto C2 = C->getAPInt();
2704 SCEV::NoWrapFlags PreservedFlags = SCEV::FlagAnyWrap;
2705
2706 APInt ConstAdd = C1 + C2;
2707 auto AddFlags = AddExpr->getNoWrapFlags();
2708 // Adding a smaller constant is NUW if the original AddExpr was NUW.
2709 if (ScalarEvolution::hasFlags(Flags: AddFlags, TestFlags: SCEV::FlagNUW) &&
2710 ConstAdd.ule(RHS: C1)) {
2711 PreservedFlags =
2712 ScalarEvolution::setFlags(Flags: PreservedFlags, OnFlags: SCEV::FlagNUW);
2713 }
2714
2715 // Adding a constant with the same sign and small magnitude is NSW, if the
2716 // original AddExpr was NSW.
2717 if (ScalarEvolution::hasFlags(Flags: AddFlags, TestFlags: SCEV::FlagNSW) &&
2718 C1.isSignBitSet() == ConstAdd.isSignBitSet() &&
2719 ConstAdd.abs().ule(RHS: C1.abs())) {
2720 PreservedFlags =
2721 ScalarEvolution::setFlags(Flags: PreservedFlags, OnFlags: SCEV::FlagNSW);
2722 }
2723
2724 if (PreservedFlags != SCEV::FlagAnyWrap) {
2725 SmallVector<SCEVUse, 4> NewOps(AddExpr->operands());
2726 NewOps[0] = getConstant(Val: ConstAdd);
2727 return getAddExpr(Ops&: NewOps, OrigFlags: PreservedFlags);
2728 }
2729 }
2730
2731 // Try to push the constant operand into a ZExt: A + zext (-A + B) -> zext
2732 // (B), if trunc (A) + -A + B does not unsigned-wrap.
2733 const SCEVAddExpr *InnerAdd;
2734 if (match(S: B, P: m_scev_ZExt(Op0: m_scev_Add(V&: InnerAdd)))) {
2735 const SCEV *NarrowA = getTruncateExpr(Op: A, Ty: InnerAdd->getType());
2736 if (NarrowA == getNegativeSCEV(V: InnerAdd->getOperand(i: 0)) &&
2737 getZeroExtendExpr(Op: NarrowA, Ty: B->getType()) == A &&
2738 hasFlags(Flags: StrengthenNoWrapFlags(SE: this, Type: scAddExpr, Ops: {NarrowA, InnerAdd},
2739 Flags: SCEV::FlagAnyWrap),
2740 TestFlags: SCEV::FlagNUW)) {
2741 return getZeroExtendExpr(Op: getAddExpr(LHS: NarrowA, RHS: InnerAdd), Ty: B->getType());
2742 }
2743 }
2744 }
2745
2746 // Canonicalize (-1 * urem X, Y) + X --> (Y * X/Y)
2747 const SCEV *Y;
2748 if (Ops.size() == 2 &&
2749 match(U: Ops[0],
2750 P: m_scev_Mul(Op0: m_scev_AllOnes(),
2751 Op1: m_scev_URem(LHS: m_scev_Specific(S: Ops[1]), RHS: m_SCEV(V&: Y), SE&: *this))))
2752 return getMulExpr(LHS: Y, RHS: getUDivExpr(LHS: Ops[1], RHS: Y));
2753
2754 // Skip past any other cast SCEVs.
2755 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddExpr)
2756 ++Idx;
2757
2758 // If there are add operands they would be next.
2759 if (Idx < Ops.size()) {
2760 bool DeletedAdd = false;
2761 // If the original flags and all inlined SCEVAddExprs are NUW, use the
2762 // common NUW flag for expression after inlining. Other flags cannot be
2763 // preserved, because they may depend on the original order of operations.
2764 SCEV::NoWrapFlags CommonFlags = maskFlags(Flags: OrigFlags, Mask: SCEV::FlagNUW);
2765 while (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Val&: Ops[Idx])) {
2766 if (Ops.size() > AddOpsInlineThreshold ||
2767 Add->getNumOperands() > AddOpsInlineThreshold)
2768 break;
2769 // If we have an add, expand the add operands onto the end of the operands
2770 // list.
2771 Ops.erase(CI: Ops.begin()+Idx);
2772 append_range(C&: Ops, R: Add->operands());
2773 DeletedAdd = true;
2774 CommonFlags = maskFlags(Flags: CommonFlags, Mask: Add->getNoWrapFlags());
2775 }
2776
2777 // If we deleted at least one add, we added operands to the end of the list,
2778 // and they are not necessarily sorted. Recurse to resort and resimplify
2779 // any operands we just acquired.
2780 if (DeletedAdd)
2781 return getAddExpr(Ops, OrigFlags: CommonFlags, Depth: Depth + 1);
2782 }
2783
2784 // Skip over the add expression until we get to a multiply.
2785 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
2786 ++Idx;
2787
2788 // Check to see if there are any folding opportunities present with
2789 // operands multiplied by constant values.
2790 if (Idx < Ops.size() && isa<SCEVMulExpr>(Val: Ops[Idx])) {
2791 uint64_t BitWidth = getTypeSizeInBits(Ty);
2792 SmallDenseMap<SCEVUse, APInt, 16> M;
2793 SmallVector<SCEVUse, 8> NewOps;
2794 APInt AccumulatedConstant(BitWidth, 0);
2795 if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2796 Ops, Scale: APInt(BitWidth, 1), SE&: *this)) {
2797 struct APIntCompare {
2798 bool operator()(const APInt &LHS, const APInt &RHS) const {
2799 return LHS.ult(RHS);
2800 }
2801 };
2802
2803 // Some interesting folding opportunity is present, so its worthwhile to
2804 // re-generate the operands list. Group the operands by constant scale,
2805 // to avoid multiplying by the same constant scale multiple times.
2806 std::map<APInt, SmallVector<SCEVUse, 4>, APIntCompare> MulOpLists;
2807 for (const SCEV *NewOp : NewOps)
2808 MulOpLists[M.find(Val: NewOp)->second].push_back(Elt: NewOp);
2809 // Re-generate the operands list.
2810 Ops.clear();
2811 if (AccumulatedConstant != 0)
2812 Ops.push_back(Elt: getConstant(Val: AccumulatedConstant));
2813 for (auto &MulOp : MulOpLists) {
2814 if (MulOp.first == 1) {
2815 Ops.push_back(Elt: getAddExpr(Ops&: MulOp.second, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1));
2816 } else if (MulOp.first != 0) {
2817 Ops.push_back(Elt: getMulExpr(
2818 LHS: getConstant(Val: MulOp.first),
2819 RHS: getAddExpr(Ops&: MulOp.second, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1),
2820 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1));
2821 }
2822 }
2823 if (Ops.empty())
2824 return getZero(Ty);
2825 if (Ops.size() == 1)
2826 return Ops[0];
2827 return getAddExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2828 }
2829 }
2830
2831 // If we are adding something to a multiply expression, make sure the
2832 // something is not already an operand of the multiply. If so, merge it into
2833 // the multiply.
2834 for (; Idx < Ops.size() && isa<SCEVMulExpr>(Val: Ops[Idx]); ++Idx) {
2835 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Val&: Ops[Idx]);
2836 for (unsigned MulOp = 0, e = Mul->getNumOperands(); MulOp != e; ++MulOp) {
2837 const SCEV *MulOpSCEV = Mul->getOperand(i: MulOp);
2838 if (isa<SCEVConstant>(Val: MulOpSCEV))
2839 continue;
2840 for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
2841 if (MulOpSCEV == Ops[AddOp]) {
2842 // Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
2843 const SCEV *InnerMul = Mul->getOperand(i: MulOp == 0);
2844 if (Mul->getNumOperands() != 2) {
2845 // If the multiply has more than two operands, we must get the
2846 // Y*Z term.
2847 SmallVector<SCEVUse, 4> MulOps(Mul->operands().take_front(N: MulOp));
2848 append_range(C&: MulOps, R: Mul->operands().drop_front(N: MulOp + 1));
2849 InnerMul = getMulExpr(Ops&: MulOps, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2850 }
2851 const SCEV *AddOne =
2852 getAddExpr(LHS: getOne(Ty), RHS: InnerMul, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2853 const SCEV *OuterMul = getMulExpr(LHS: AddOne, RHS: MulOpSCEV,
2854 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2855 if (Ops.size() == 2) return OuterMul;
2856 if (AddOp < Idx) {
2857 Ops.erase(CI: Ops.begin()+AddOp);
2858 Ops.erase(CI: Ops.begin()+Idx-1);
2859 } else {
2860 Ops.erase(CI: Ops.begin()+Idx);
2861 Ops.erase(CI: Ops.begin()+AddOp-1);
2862 }
2863 Ops.push_back(Elt: OuterMul);
2864 return getAddExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2865 }
2866
2867 // Check this multiply against other multiplies being added together.
2868 for (unsigned OtherMulIdx = Idx+1;
2869 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Val: Ops[OtherMulIdx]);
2870 ++OtherMulIdx) {
2871 const SCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Val&: Ops[OtherMulIdx]);
2872 // If MulOp occurs in OtherMul, we can fold the two multiplies
2873 // together.
2874 for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
2875 OMulOp != e; ++OMulOp)
2876 if (OtherMul->getOperand(i: OMulOp) == MulOpSCEV) {
2877 // Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
2878 const SCEV *InnerMul1 = Mul->getOperand(i: MulOp == 0);
2879 if (Mul->getNumOperands() != 2) {
2880 SmallVector<SCEVUse, 4> MulOps(Mul->operands().take_front(N: MulOp));
2881 append_range(C&: MulOps, R: Mul->operands().drop_front(N: MulOp+1));
2882 InnerMul1 = getMulExpr(Ops&: MulOps, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2883 }
2884 const SCEV *InnerMul2 = OtherMul->getOperand(i: OMulOp == 0);
2885 if (OtherMul->getNumOperands() != 2) {
2886 SmallVector<SCEVUse, 4> MulOps(
2887 OtherMul->operands().take_front(N: OMulOp));
2888 append_range(C&: MulOps, R: OtherMul->operands().drop_front(N: OMulOp+1));
2889 InnerMul2 = getMulExpr(Ops&: MulOps, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2890 }
2891 const SCEV *InnerMulSum =
2892 getAddExpr(LHS: InnerMul1, RHS: InnerMul2, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2893 const SCEV *OuterMul = getMulExpr(LHS: MulOpSCEV, RHS: InnerMulSum,
2894 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2895 if (Ops.size() == 2) return OuterMul;
2896 Ops.erase(CI: Ops.begin()+Idx);
2897 Ops.erase(CI: Ops.begin()+OtherMulIdx-1);
2898 Ops.push_back(Elt: OuterMul);
2899 return getAddExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2900 }
2901 }
2902 }
2903 }
2904
2905 // If there are any add recurrences in the operands list, see if any other
2906 // added values are loop invariant. If so, we can fold them into the
2907 // recurrence.
2908 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
2909 ++Idx;
2910
2911 // Scan over all recurrences, trying to fold loop invariants into them.
2912 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Val: Ops[Idx]); ++Idx) {
2913 // Scan all of the other operands to this add and add them to the vector if
2914 // they are loop invariant w.r.t. the recurrence.
2915 SmallVector<SCEVUse, 8> LIOps;
2916 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Val&: Ops[Idx]);
2917 const Loop *AddRecLoop = AddRec->getLoop();
2918 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2919 if (isAvailableAtLoopEntry(S: Ops[i], L: AddRecLoop)) {
2920 LIOps.push_back(Elt: Ops[i]);
2921 Ops.erase(CI: Ops.begin()+i);
2922 --i; --e;
2923 }
2924
2925 // If we found some loop invariants, fold them into the recurrence.
2926 if (!LIOps.empty()) {
2927 // Compute nowrap flags for the addition of the loop-invariant ops and
2928 // the addrec. Temporarily push it as an operand for that purpose. These
2929 // flags are valid in the scope of the addrec only.
2930 LIOps.push_back(Elt: AddRec);
2931 SCEV::NoWrapFlags Flags = ComputeFlags(LIOps);
2932 LIOps.pop_back();
2933
2934 // NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
2935 LIOps.push_back(Elt: AddRec->getStart());
2936
2937 SmallVector<SCEVUse, 4> AddRecOps(AddRec->operands());
2938
2939 // It is not in general safe to propagate flags valid on an add within
2940 // the addrec scope to one outside it. We must prove that the inner
2941 // scope is guaranteed to execute if the outer one does to be able to
2942 // safely propagate. We know the program is undefined if poison is
2943 // produced on the inner scoped addrec. We also know that *for this use*
2944 // the outer scoped add can't overflow (because of the flags we just
2945 // computed for the inner scoped add) without the program being undefined.
2946 // Proving that entry to the outer scope neccesitates entry to the inner
2947 // scope, thus proves the program undefined if the flags would be violated
2948 // in the outer scope.
2949 SCEV::NoWrapFlags AddFlags = Flags;
2950 if (AddFlags != SCEV::FlagAnyWrap) {
2951 auto *DefI = getDefiningScopeBound(Ops: LIOps);
2952 auto *ReachI = &*AddRecLoop->getHeader()->begin();
2953 if (!isGuaranteedToTransferExecutionTo(A: DefI, B: ReachI))
2954 AddFlags = SCEV::FlagAnyWrap;
2955 }
2956 AddRecOps[0] = getAddExpr(Ops&: LIOps, OrigFlags: AddFlags, Depth: Depth + 1);
2957
2958 // Build the new addrec. Propagate the NUW and NSW flags if both the
2959 // outer add and the inner addrec are guaranteed to have no overflow.
2960 // Always propagate NW.
2961 Flags = AddRec->getNoWrapFlags(Mask: setFlags(Flags, OnFlags: SCEV::FlagNW));
2962 const SCEV *NewRec = getAddRecExpr(Operands&: AddRecOps, L: AddRecLoop, Flags);
2963
2964 // If all of the other operands were loop invariant, we are done.
2965 if (Ops.size() == 1) return NewRec;
2966
2967 // Otherwise, add the folded AddRec by the non-invariant parts.
2968 for (unsigned i = 0;; ++i)
2969 if (Ops[i] == AddRec) {
2970 Ops[i] = NewRec;
2971 break;
2972 }
2973 return getAddExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
2974 }
2975
2976 // Okay, if there weren't any loop invariants to be folded, check to see if
2977 // there are multiple AddRec's with the same loop induction variable being
2978 // added together. If so, we can fold them.
2979 for (unsigned OtherIdx = Idx+1;
2980 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Val: Ops[OtherIdx]);
2981 ++OtherIdx) {
2982 // We expect the AddRecExpr's to be sorted in reverse dominance order,
2983 // so that the 1st found AddRecExpr is dominated by all others.
2984 assert(DT.dominates(
2985 cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),
2986 AddRec->getLoop()->getHeader()) &&
2987 "AddRecExprs are not sorted in reverse dominance order?");
2988 if (AddRecLoop == cast<SCEVAddRecExpr>(Val&: Ops[OtherIdx])->getLoop()) {
2989 // Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L>
2990 SmallVector<SCEVUse, 4> AddRecOps(AddRec->operands());
2991 for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Val: Ops[OtherIdx]);
2992 ++OtherIdx) {
2993 const auto *OtherAddRec = cast<SCEVAddRecExpr>(Val&: Ops[OtherIdx]);
2994 if (OtherAddRec->getLoop() == AddRecLoop) {
2995 for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2996 i != e; ++i) {
2997 if (i >= AddRecOps.size()) {
2998 append_range(C&: AddRecOps, R: OtherAddRec->operands().drop_front(N: i));
2999 break;
3000 }
3001 AddRecOps[i] =
3002 getAddExpr(LHS: AddRecOps[i], RHS: OtherAddRec->getOperand(i),
3003 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3004 }
3005 Ops.erase(CI: Ops.begin() + OtherIdx); --OtherIdx;
3006 }
3007 }
3008 // Step size has changed, so we cannot guarantee no self-wraparound.
3009 Ops[Idx] = getAddRecExpr(Operands&: AddRecOps, L: AddRecLoop, Flags: SCEV::FlagAnyWrap);
3010 return getAddExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3011 }
3012 }
3013
3014 // Otherwise couldn't fold anything into this recurrence. Move onto the
3015 // next one.
3016 }
3017
3018 // Okay, it looks like we really DO need an add expr. Check to see if we
3019 // already have one, otherwise create a new one.
3020 return getOrCreateAddExpr(Ops, Flags: ComputeFlags(Ops));
3021}
3022
3023const SCEV *ScalarEvolution::getOrCreateAddExpr(ArrayRef<SCEVUse> Ops,
3024 SCEV::NoWrapFlags Flags) {
3025 FoldingSetNodeID ID;
3026 ID.AddInteger(I: scAddExpr);
3027 for (const SCEV *Op : Ops)
3028 ID.AddPointer(Ptr: Op);
3029 void *IP = nullptr;
3030 SCEVAddExpr *S =
3031 static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP));
3032 if (!S) {
3033 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Num: Ops.size());
3034 llvm::uninitialized_copy(Src&: Ops, Dst: O);
3035 S = new (SCEVAllocator)
3036 SCEVAddExpr(ID.Intern(Allocator&: SCEVAllocator), O, Ops.size());
3037 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
3038 registerUser(User: S, Ops);
3039 }
3040 S->setNoWrapFlags(Flags);
3041 return S;
3042}
3043
3044const SCEV *ScalarEvolution::getOrCreateAddRecExpr(ArrayRef<SCEVUse> Ops,
3045 const Loop *L,
3046 SCEV::NoWrapFlags Flags) {
3047 FoldingSetNodeID ID;
3048 ID.AddInteger(I: scAddRecExpr);
3049 for (const SCEV *Op : Ops)
3050 ID.AddPointer(Ptr: Op);
3051 ID.AddPointer(Ptr: L);
3052 void *IP = nullptr;
3053 SCEVAddRecExpr *S =
3054 static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP));
3055 if (!S) {
3056 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Num: Ops.size());
3057 llvm::uninitialized_copy(Src&: Ops, Dst: O);
3058 S = new (SCEVAllocator)
3059 SCEVAddRecExpr(ID.Intern(Allocator&: SCEVAllocator), O, Ops.size(), L);
3060 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
3061 LoopUsers[L].push_back(Elt: S);
3062 registerUser(User: S, Ops);
3063 }
3064 setNoWrapFlags(AddRec: S, Flags);
3065 return S;
3066}
3067
3068const SCEV *ScalarEvolution::getOrCreateMulExpr(ArrayRef<SCEVUse> Ops,
3069 SCEV::NoWrapFlags Flags) {
3070 FoldingSetNodeID ID;
3071 ID.AddInteger(I: scMulExpr);
3072 for (const SCEV *Op : Ops)
3073 ID.AddPointer(Ptr: Op);
3074 void *IP = nullptr;
3075 SCEVMulExpr *S =
3076 static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP));
3077 if (!S) {
3078 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Num: Ops.size());
3079 llvm::uninitialized_copy(Src&: Ops, Dst: O);
3080 S = new (SCEVAllocator) SCEVMulExpr(ID.Intern(Allocator&: SCEVAllocator),
3081 O, Ops.size());
3082 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
3083 registerUser(User: S, Ops);
3084 }
3085 S->setNoWrapFlags(Flags);
3086 return S;
3087}
3088
3089static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow) {
3090 uint64_t k = i*j;
3091 if (j > 1 && k / j != i) Overflow = true;
3092 return k;
3093}
3094
3095/// Compute the result of "n choose k", the binomial coefficient. If an
3096/// intermediate computation overflows, Overflow will be set and the return will
3097/// be garbage. Overflow is not cleared on absence of overflow.
3098static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow) {
3099 // We use the multiplicative formula:
3100 // n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
3101 // At each iteration, we take the n-th term of the numeral and divide by the
3102 // (k-n)th term of the denominator. This division will always produce an
3103 // integral result, and helps reduce the chance of overflow in the
3104 // intermediate computations. However, we can still overflow even when the
3105 // final result would fit.
3106
3107 if (n == 0 || n == k) return 1;
3108 if (k > n) return 0;
3109
3110 if (k > n/2)
3111 k = n-k;
3112
3113 uint64_t r = 1;
3114 for (uint64_t i = 1; i <= k; ++i) {
3115 r = umul_ov(i: r, j: n-(i-1), Overflow);
3116 r /= i;
3117 }
3118 return r;
3119}
3120
3121/// Determine if any of the operands in this SCEV are a constant or if
3122/// any of the add or multiply expressions in this SCEV contain a constant.
3123static bool containsConstantInAddMulChain(const SCEV *StartExpr) {
3124 struct FindConstantInAddMulChain {
3125 bool FoundConstant = false;
3126
3127 bool follow(const SCEV *S) {
3128 FoundConstant |= isa<SCEVConstant>(Val: S);
3129 return isa<SCEVAddExpr>(Val: S) || isa<SCEVMulExpr>(Val: S);
3130 }
3131
3132 bool isDone() const {
3133 return FoundConstant;
3134 }
3135 };
3136
3137 FindConstantInAddMulChain F;
3138 SCEVTraversal<FindConstantInAddMulChain> ST(F);
3139 ST.visitAll(Root: StartExpr);
3140 return F.FoundConstant;
3141}
3142
3143/// Get a canonical multiply expression, or something simpler if possible.
3144const SCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<SCEVUse> &Ops,
3145 SCEV::NoWrapFlags OrigFlags,
3146 unsigned Depth) {
3147 assert(OrigFlags == maskFlags(OrigFlags, SCEV::FlagNUW | SCEV::FlagNSW) &&
3148 "only nuw or nsw allowed");
3149 assert(!Ops.empty() && "Cannot get empty mul!");
3150 if (Ops.size() == 1) return Ops[0];
3151#ifndef NDEBUG
3152 Type *ETy = Ops[0]->getType();
3153 assert(!ETy->isPointerTy());
3154 for (unsigned i = 1, e = Ops.size(); i != e; ++i)
3155 assert(Ops[i]->getType() == ETy &&
3156 "SCEVMulExpr operand types don't match!");
3157#endif
3158
3159 const SCEV *Folded = constantFoldAndGroupOps(
3160 SE&: *this, LI, DT, Ops,
3161 Fold: [](const APInt &C1, const APInt &C2) { return C1 * C2; },
3162 IsIdentity: [](const APInt &C) { return C.isOne(); }, // identity
3163 IsAbsorber: [](const APInt &C) { return C.isZero(); }); // absorber
3164 if (Folded)
3165 return Folded;
3166
3167 // Delay expensive flag strengthening until necessary.
3168 auto ComputeFlags = [this, OrigFlags](const ArrayRef<SCEVUse> Ops) {
3169 return StrengthenNoWrapFlags(SE: this, Type: scMulExpr, Ops, Flags: OrigFlags);
3170 };
3171
3172 // Limit recursion calls depth.
3173 if (Depth > MaxArithDepth || hasHugeExpression(Ops))
3174 return getOrCreateMulExpr(Ops, Flags: ComputeFlags(Ops));
3175
3176 if (SCEV *S = findExistingSCEVInCache(SCEVType: scMulExpr, Ops)) {
3177 // Don't strengthen flags if we have no new information.
3178 SCEVMulExpr *Mul = static_cast<SCEVMulExpr *>(S);
3179 if (Mul->getNoWrapFlags(Mask: OrigFlags) != OrigFlags)
3180 Mul->setNoWrapFlags(ComputeFlags(Ops));
3181 return S;
3182 }
3183
3184 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Val&: Ops[0])) {
3185 if (Ops.size() == 2) {
3186 // C1*(C2+V) -> C1*C2 + C1*V
3187 // If any of Add's ops are Adds or Muls with a constant, apply this
3188 // transformation as well.
3189 //
3190 // TODO: There are some cases where this transformation is not
3191 // profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of
3192 // this transformation should be narrowed down.
3193 const SCEV *Op0, *Op1;
3194 if (match(U: Ops[1], P: m_scev_Add(Op0: m_SCEV(V&: Op0), Op1: m_SCEV(V&: Op1))) &&
3195 containsConstantInAddMulChain(StartExpr: Ops[1])) {
3196 const SCEV *LHS = getMulExpr(LHS: LHSC, RHS: Op0, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3197 const SCEV *RHS = getMulExpr(LHS: LHSC, RHS: Op1, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3198 return getAddExpr(LHS, RHS, Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3199 }
3200
3201 if (Ops[0]->isAllOnesValue()) {
3202 // If we have a mul by -1 of an add, try distributing the -1 among the
3203 // add operands.
3204 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Val&: Ops[1])) {
3205 SmallVector<SCEVUse, 4> NewOps;
3206 bool AnyFolded = false;
3207 for (const SCEV *AddOp : Add->operands()) {
3208 const SCEV *Mul = getMulExpr(LHS: Ops[0], RHS: SCEVUse(AddOp),
3209 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3210 if (!isa<SCEVMulExpr>(Val: Mul)) AnyFolded = true;
3211 NewOps.push_back(Elt: Mul);
3212 }
3213 if (AnyFolded)
3214 return getAddExpr(Ops&: NewOps, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3215 } else if (const auto *AddRec = dyn_cast<SCEVAddRecExpr>(Val&: Ops[1])) {
3216 // Negation preserves a recurrence's no self-wrap property.
3217 SmallVector<SCEVUse, 4> Operands;
3218 for (const SCEV *AddRecOp : AddRec->operands())
3219 Operands.push_back(Elt: getMulExpr(LHS: Ops[0], RHS: SCEVUse(AddRecOp),
3220 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1));
3221 // Let M be the minimum representable signed value. AddRec with nsw
3222 // multiplied by -1 can have signed overflow if and only if it takes a
3223 // value of M: M * (-1) would stay M and (M + 1) * (-1) would be the
3224 // maximum signed value. In all other cases signed overflow is
3225 // impossible.
3226 auto FlagsMask = SCEV::FlagNW;
3227 if (hasFlags(Flags: AddRec->getNoWrapFlags(), TestFlags: SCEV::FlagNSW)) {
3228 auto MinInt =
3229 APInt::getSignedMinValue(numBits: getTypeSizeInBits(Ty: AddRec->getType()));
3230 if (getSignedRangeMin(S: AddRec) != MinInt)
3231 FlagsMask = setFlags(Flags: FlagsMask, OnFlags: SCEV::FlagNSW);
3232 }
3233 return getAddRecExpr(Operands, L: AddRec->getLoop(),
3234 Flags: AddRec->getNoWrapFlags(Mask: FlagsMask));
3235 }
3236 }
3237
3238 // Try to push the constant operand into a ZExt: C * zext (A + B) ->
3239 // zext (C*A + C*B) if trunc (C) * (A + B) does not unsigned-wrap.
3240 const SCEVAddExpr *InnerAdd;
3241 if (match(U: Ops[1], P: m_scev_ZExt(Op0: m_scev_Add(V&: InnerAdd)))) {
3242 const SCEV *NarrowC = getTruncateExpr(Op: LHSC, Ty: InnerAdd->getType());
3243 if (isa<SCEVConstant>(Val: InnerAdd->getOperand(i: 0)) &&
3244 getZeroExtendExpr(Op: NarrowC, Ty: Ops[1]->getType()) == LHSC &&
3245 hasFlags(Flags: StrengthenNoWrapFlags(SE: this, Type: scMulExpr, Ops: {NarrowC, InnerAdd},
3246 Flags: SCEV::FlagAnyWrap),
3247 TestFlags: SCEV::FlagNUW)) {
3248 auto *Res = getMulExpr(LHS: NarrowC, RHS: InnerAdd, Flags: SCEV::FlagNUW, Depth: Depth + 1);
3249 return getZeroExtendExpr(Op: Res, Ty: Ops[1]->getType(), Depth: Depth + 1);
3250 };
3251 }
3252
3253 // Try to fold (C1 * D /u C2) -> C1/C2 * D, if C1 and C2 are powers-of-2,
3254 // D is a multiple of C2, and C1 is a multiple of C2. If C2 is a multiple
3255 // of C1, fold to (D /u (C2 /u C1)).
3256 const SCEV *D;
3257 APInt C1V = LHSC->getAPInt();
3258 // (C1 * D /u C2) == -1 * -C1 * D /u C2 when C1 != INT_MIN. Don't treat -1
3259 // as -1 * 1, as it won't enable additional folds.
3260 if (C1V.isNegative() && !C1V.isMinSignedValue() && !C1V.isAllOnes())
3261 C1V = C1V.abs();
3262 const SCEVConstant *C2;
3263 if (C1V.isPowerOf2() &&
3264 match(U: Ops[1], P: m_scev_UDiv(Op0: m_SCEV(V&: D), Op1: m_SCEVConstant(V&: C2))) &&
3265 C2->getAPInt().isPowerOf2() &&
3266 C1V.logBase2() <= getMinTrailingZeros(S: D)) {
3267 const SCEV *NewMul = nullptr;
3268 if (C1V.uge(RHS: C2->getAPInt())) {
3269 NewMul = getMulExpr(LHS: getUDivExpr(LHS: getConstant(Val: C1V), RHS: C2), RHS: D);
3270 } else if (C2->getAPInt().logBase2() <= getMinTrailingZeros(S: D)) {
3271 assert(C1V.ugt(1) && "C1 <= 1 should have been folded earlier");
3272 NewMul = getUDivExpr(LHS: D, RHS: getUDivExpr(LHS: C2, RHS: getConstant(Val: C1V)));
3273 }
3274 if (NewMul)
3275 return C1V == LHSC->getAPInt() ? NewMul : getNegativeSCEV(V: NewMul);
3276 }
3277 }
3278 }
3279
3280 // Skip over the add expression until we get to a multiply.
3281 unsigned Idx = 0;
3282 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scMulExpr)
3283 ++Idx;
3284
3285 // If there are mul operands inline them all into this expression.
3286 if (Idx < Ops.size()) {
3287 bool DeletedMul = false;
3288 while (const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Val&: Ops[Idx])) {
3289 if (Ops.size() > MulOpsInlineThreshold)
3290 break;
3291 // If we have an mul, expand the mul operands onto the end of the
3292 // operands list.
3293 Ops.erase(CI: Ops.begin()+Idx);
3294 append_range(C&: Ops, R: Mul->operands());
3295 DeletedMul = true;
3296 }
3297
3298 // If we deleted at least one mul, we added operands to the end of the
3299 // list, and they are not necessarily sorted. Recurse to resort and
3300 // resimplify any operands we just acquired.
3301 if (DeletedMul)
3302 return getMulExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3303 }
3304
3305 // If there are any add recurrences in the operands list, see if any other
3306 // added values are loop invariant. If so, we can fold them into the
3307 // recurrence.
3308 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < scAddRecExpr)
3309 ++Idx;
3310
3311 // Scan over all recurrences, trying to fold loop invariants into them.
3312 for (; Idx < Ops.size() && isa<SCEVAddRecExpr>(Val: Ops[Idx]); ++Idx) {
3313 // Scan all of the other operands to this mul and add them to the vector
3314 // if they are loop invariant w.r.t. the recurrence.
3315 SmallVector<SCEVUse, 8> LIOps;
3316 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Val&: Ops[Idx]);
3317 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3318 if (isAvailableAtLoopEntry(S: Ops[i], L: AddRec->getLoop())) {
3319 LIOps.push_back(Elt: Ops[i]);
3320 Ops.erase(CI: Ops.begin()+i);
3321 --i; --e;
3322 }
3323
3324 // If we found some loop invariants, fold them into the recurrence.
3325 if (!LIOps.empty()) {
3326 // NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
3327 SmallVector<SCEVUse, 4> NewOps;
3328 NewOps.reserve(N: AddRec->getNumOperands());
3329 const SCEV *Scale = getMulExpr(Ops&: LIOps, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3330
3331 // If both the mul and addrec are nuw, we can preserve nuw.
3332 // If both the mul and addrec are nsw, we can only preserve nsw if either
3333 // a) they are also nuw, or
3334 // b) all multiplications of addrec operands with scale are nsw.
3335 SCEV::NoWrapFlags Flags =
3336 AddRec->getNoWrapFlags(Mask: ComputeFlags({Scale, AddRec}));
3337
3338 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
3339 NewOps.push_back(Elt: getMulExpr(LHS: Scale, RHS: AddRec->getOperand(i),
3340 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1));
3341
3342 if (hasFlags(Flags, TestFlags: SCEV::FlagNSW) && !hasFlags(Flags, TestFlags: SCEV::FlagNUW)) {
3343 ConstantRange NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
3344 BinOp: Instruction::Mul, Other: getSignedRange(S: Scale),
3345 NoWrapKind: OverflowingBinaryOperator::NoSignedWrap);
3346 if (!NSWRegion.contains(CR: getSignedRange(S: AddRec->getOperand(i))))
3347 Flags = clearFlags(Flags, OffFlags: SCEV::FlagNSW);
3348 }
3349 }
3350
3351 const SCEV *NewRec = getAddRecExpr(Operands&: NewOps, L: AddRec->getLoop(), Flags);
3352
3353 // If all of the other operands were loop invariant, we are done.
3354 if (Ops.size() == 1) return NewRec;
3355
3356 // Otherwise, multiply the folded AddRec by the non-invariant parts.
3357 for (unsigned i = 0;; ++i)
3358 if (Ops[i] == AddRec) {
3359 Ops[i] = NewRec;
3360 break;
3361 }
3362 return getMulExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3363 }
3364
3365 // Okay, if there weren't any loop invariants to be folded, check to see
3366 // if there are multiple AddRec's with the same loop induction variable
3367 // being multiplied together. If so, we can fold them.
3368
3369 // {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
3370 // = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
3371 // choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
3372 // ]]],+,...up to x=2n}.
3373 // Note that the arguments to choose() are always integers with values
3374 // known at compile time, never SCEV objects.
3375 //
3376 // The implementation avoids pointless extra computations when the two
3377 // addrec's are of different length (mathematically, it's equivalent to
3378 // an infinite stream of zeros on the right).
3379 bool OpsModified = false;
3380 for (unsigned OtherIdx = Idx+1;
3381 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Val: Ops[OtherIdx]);
3382 ++OtherIdx) {
3383 const SCEVAddRecExpr *OtherAddRec =
3384 dyn_cast<SCEVAddRecExpr>(Val&: Ops[OtherIdx]);
3385 if (!OtherAddRec || OtherAddRec->getLoop() != AddRec->getLoop())
3386 continue;
3387
3388 // Limit max number of arguments to avoid creation of unreasonably big
3389 // SCEVAddRecs with very complex operands.
3390 if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
3391 MaxAddRecSize || hasHugeExpression(Ops: {AddRec, OtherAddRec}))
3392 continue;
3393
3394 bool Overflow = false;
3395 Type *Ty = AddRec->getType();
3396 bool LargerThan64Bits = getTypeSizeInBits(Ty) > 64;
3397 SmallVector<SCEVUse, 7> AddRecOps;
3398 for (int x = 0, xe = AddRec->getNumOperands() +
3399 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
3400 SmallVector<SCEVUse, 7> SumOps;
3401 for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
3402 uint64_t Coeff1 = Choose(n: x, k: 2*x - y, Overflow);
3403 for (int z = std::max(a: y-x, b: y-(int)AddRec->getNumOperands()+1),
3404 ze = std::min(a: x+1, b: (int)OtherAddRec->getNumOperands());
3405 z < ze && !Overflow; ++z) {
3406 uint64_t Coeff2 = Choose(n: 2*x - y, k: x-z, Overflow);
3407 uint64_t Coeff;
3408 if (LargerThan64Bits)
3409 Coeff = umul_ov(i: Coeff1, j: Coeff2, Overflow);
3410 else
3411 Coeff = Coeff1*Coeff2;
3412 const SCEV *CoeffTerm = getConstant(Ty, V: Coeff);
3413 const SCEV *Term1 = AddRec->getOperand(i: y-z);
3414 const SCEV *Term2 = OtherAddRec->getOperand(i: z);
3415 SumOps.push_back(Elt: getMulExpr(Op0: CoeffTerm, Op1: Term1, Op2: Term2,
3416 Flags: SCEV::FlagAnyWrap, Depth: Depth + 1));
3417 }
3418 }
3419 if (SumOps.empty())
3420 SumOps.push_back(Elt: getZero(Ty));
3421 AddRecOps.push_back(Elt: getAddExpr(Ops&: SumOps, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1));
3422 }
3423 if (!Overflow) {
3424 const SCEV *NewAddRec = getAddRecExpr(Operands&: AddRecOps, L: AddRec->getLoop(),
3425 Flags: SCEV::FlagAnyWrap);
3426 if (Ops.size() == 2) return NewAddRec;
3427 Ops[Idx] = NewAddRec;
3428 Ops.erase(CI: Ops.begin() + OtherIdx); --OtherIdx;
3429 OpsModified = true;
3430 AddRec = dyn_cast<SCEVAddRecExpr>(Val: NewAddRec);
3431 if (!AddRec)
3432 break;
3433 }
3434 }
3435 if (OpsModified)
3436 return getMulExpr(Ops, OrigFlags: SCEV::FlagAnyWrap, Depth: Depth + 1);
3437
3438 // Otherwise couldn't fold anything into this recurrence. Move onto the
3439 // next one.
3440 }
3441
3442 // Okay, it looks like we really DO need an mul expr. Check to see if we
3443 // already have one, otherwise create a new one.
3444 return getOrCreateMulExpr(Ops, Flags: ComputeFlags(Ops));
3445}
3446
3447/// Represents an unsigned remainder expression based on unsigned division.
3448const SCEV *ScalarEvolution::getURemExpr(SCEVUse LHS, SCEVUse RHS) {
3449 assert(getEffectiveSCEVType(LHS->getType()) ==
3450 getEffectiveSCEVType(RHS->getType()) &&
3451 "SCEVURemExpr operand types don't match!");
3452
3453 // Short-circuit easy cases
3454 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Val&: RHS)) {
3455 // If constant is one, the result is trivial
3456 if (RHSC->getValue()->isOne())
3457 return getZero(Ty: LHS->getType()); // X urem 1 --> 0
3458
3459 // If constant is a power of two, fold into a zext(trunc(LHS)).
3460 if (RHSC->getAPInt().isPowerOf2()) {
3461 Type *FullTy = LHS->getType();
3462 Type *TruncTy =
3463 IntegerType::get(C&: getContext(), NumBits: RHSC->getAPInt().logBase2());
3464 return getZeroExtendExpr(Op: getTruncateExpr(Op: LHS, Ty: TruncTy), Ty: FullTy);
3465 }
3466 }
3467
3468 // Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
3469 const SCEV *UDiv = getUDivExpr(LHS, RHS);
3470 const SCEV *Mult = getMulExpr(LHS: UDiv, RHS, Flags: SCEV::FlagNUW);
3471 return getMinusSCEV(LHS, RHS: Mult, Flags: SCEV::FlagNUW);
3472}
3473
3474/// Get a canonical unsigned division expression, or something simpler if
3475/// possible.
3476const SCEV *ScalarEvolution::getUDivExpr(SCEVUse LHS, SCEVUse RHS) {
3477 assert(!LHS->getType()->isPointerTy() &&
3478 "SCEVUDivExpr operand can't be pointer!");
3479 assert(LHS->getType() == RHS->getType() &&
3480 "SCEVUDivExpr operand types don't match!");
3481
3482 FoldingSetNodeID ID;
3483 ID.AddInteger(I: scUDivExpr);
3484 ID.AddPointer(Ptr: LHS);
3485 ID.AddPointer(Ptr: RHS);
3486 void *IP = nullptr;
3487 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP))
3488 return S;
3489
3490 // 0 udiv Y == 0
3491 if (match(U: LHS, P: m_scev_Zero()))
3492 return LHS;
3493
3494 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Val&: RHS)) {
3495 if (RHSC->getValue()->isOne())
3496 return LHS; // X udiv 1 --> x
3497 // If the denominator is zero, the result of the udiv is undefined. Don't
3498 // try to analyze it, because the resolution chosen here may differ from
3499 // the resolution chosen in other parts of the compiler.
3500 if (!RHSC->getValue()->isZero()) {
3501 // Determine if the division can be folded into the operands of
3502 // its operands.
3503 // TODO: Generalize this to non-constants by using known-bits information.
3504 Type *Ty = LHS->getType();
3505 unsigned LZ = RHSC->getAPInt().countl_zero();
3506 unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ - 1;
3507 // For non-power-of-two values, effectively round the value up to the
3508 // nearest power of two.
3509 if (!RHSC->getAPInt().isPowerOf2())
3510 ++MaxShiftAmt;
3511 IntegerType *ExtTy =
3512 IntegerType::get(C&: getContext(), NumBits: getTypeSizeInBits(Ty) + MaxShiftAmt);
3513 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val&: LHS))
3514 if (const SCEVConstant *Step =
3515 dyn_cast<SCEVConstant>(Val: AR->getStepRecurrence(SE&: *this))) {
3516 // {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
3517 const APInt &StepInt = Step->getAPInt();
3518 const APInt &DivInt = RHSC->getAPInt();
3519 if (!StepInt.urem(RHS: DivInt) &&
3520 getZeroExtendExpr(Op: AR, Ty: ExtTy) ==
3521 getAddRecExpr(Start: getZeroExtendExpr(Op: AR->getStart(), Ty: ExtTy),
3522 Step: getZeroExtendExpr(Op: Step, Ty: ExtTy),
3523 L: AR->getLoop(), Flags: SCEV::FlagAnyWrap)) {
3524 SmallVector<SCEVUse, 4> Operands;
3525 for (const SCEV *Op : AR->operands())
3526 Operands.push_back(Elt: getUDivExpr(LHS: Op, RHS));
3527 return getAddRecExpr(Operands, L: AR->getLoop(), Flags: SCEV::FlagNW);
3528 }
3529 /// Get a canonical UDivExpr for a recurrence.
3530 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
3531 const APInt *StartRem;
3532 if (!DivInt.urem(RHS: StepInt) && match(S: getURemExpr(LHS: AR->getStart(), RHS: Step),
3533 P: m_scev_APInt(C&: StartRem))) {
3534 bool NoWrap =
3535 getZeroExtendExpr(Op: AR, Ty: ExtTy) ==
3536 getAddRecExpr(Start: getZeroExtendExpr(Op: AR->getStart(), Ty: ExtTy),
3537 Step: getZeroExtendExpr(Op: Step, Ty: ExtTy), L: AR->getLoop(),
3538 Flags: SCEV::FlagAnyWrap);
3539
3540 // With N <= C and both N, C as powers-of-2, the transformation
3541 // {X,+,N}/C => {(X - X%N),+,N}/C preserves division results even
3542 // if wrapping occurs, as the division results remain equivalent for
3543 // all offsets in [[(X - X%N), X).
3544 bool CanFoldWithWrap = StepInt.ule(RHS: DivInt) && // N <= C
3545 StepInt.isPowerOf2() && DivInt.isPowerOf2();
3546 // Only fold if the subtraction can be folded in the start
3547 // expression.
3548 const SCEV *NewStart =
3549 getMinusSCEV(LHS: AR->getStart(), RHS: getConstant(Val: *StartRem));
3550 if (*StartRem != 0 && (NoWrap || CanFoldWithWrap) &&
3551 !isa<SCEVAddExpr>(Val: NewStart)) {
3552 const SCEV *NewLHS =
3553 getAddRecExpr(Start: NewStart, Step, L: AR->getLoop(),
3554 Flags: NoWrap ? SCEV::FlagNW : SCEV::FlagAnyWrap);
3555 if (LHS != NewLHS) {
3556 LHS = NewLHS;
3557
3558 // Reset the ID to include the new LHS, and check if it is
3559 // already cached.
3560 ID.clear();
3561 ID.AddInteger(I: scUDivExpr);
3562 ID.AddPointer(Ptr: LHS);
3563 ID.AddPointer(Ptr: RHS);
3564 IP = nullptr;
3565 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP))
3566 return S;
3567 }
3568 }
3569 }
3570 }
3571 // (A*B)/C --> A*(B/C) if safe and B/C can be folded.
3572 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Val&: LHS)) {
3573 SmallVector<SCEVUse, 4> Operands;
3574 for (const SCEV *Op : M->operands())
3575 Operands.push_back(Elt: getZeroExtendExpr(Op, Ty: ExtTy));
3576 if (getZeroExtendExpr(Op: M, Ty: ExtTy) == getMulExpr(Ops&: Operands))
3577 // Find an operand that's safely divisible.
3578 for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
3579 const SCEV *Op = M->getOperand(i);
3580 const SCEV *Div = getUDivExpr(LHS: Op, RHS: RHSC);
3581 if (!isa<SCEVUDivExpr>(Val: Div) && getMulExpr(LHS: Div, RHS: RHSC) == Op) {
3582 Operands = SmallVector<SCEVUse, 4>(M->operands());
3583 Operands[i] = Div;
3584 return getMulExpr(Ops&: Operands);
3585 }
3586 }
3587 }
3588
3589 // (A/B)/C --> A/(B*C) if safe and B*C can be folded.
3590 if (const SCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(Val&: LHS)) {
3591 if (auto *DivisorConstant =
3592 dyn_cast<SCEVConstant>(Val: OtherDiv->getRHS())) {
3593 bool Overflow = false;
3594 APInt NewRHS =
3595 DivisorConstant->getAPInt().umul_ov(RHS: RHSC->getAPInt(), Overflow);
3596 if (Overflow) {
3597 return getConstant(Ty: RHSC->getType(), V: 0, isSigned: false);
3598 }
3599 return getUDivExpr(LHS: OtherDiv->getLHS(), RHS: getConstant(Val: NewRHS));
3600 }
3601 }
3602
3603 // (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
3604 if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(Val&: LHS)) {
3605 SmallVector<SCEVUse, 4> Operands;
3606 for (const SCEV *Op : A->operands())
3607 Operands.push_back(Elt: getZeroExtendExpr(Op, Ty: ExtTy));
3608 if (getZeroExtendExpr(Op: A, Ty: ExtTy) == getAddExpr(Ops&: Operands)) {
3609 Operands.clear();
3610 for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
3611 const SCEV *Op = getUDivExpr(LHS: A->getOperand(i), RHS);
3612 if (isa<SCEVUDivExpr>(Val: Op) ||
3613 getMulExpr(LHS: Op, RHS) != A->getOperand(i))
3614 break;
3615 Operands.push_back(Elt: Op);
3616 }
3617 if (Operands.size() == A->getNumOperands())
3618 return getAddExpr(Ops&: Operands);
3619 }
3620 }
3621
3622 // Fold if both operands are constant.
3623 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Val&: LHS))
3624 return getConstant(Val: LHSC->getAPInt().udiv(RHS: RHSC->getAPInt()));
3625 }
3626 }
3627
3628 // ((-C + (C smax %x)) /u %x) evaluates to zero, for any positive constant C.
3629 const APInt *NegC, *C;
3630 if (match(U: LHS,
3631 P: m_scev_Add(Op0: m_scev_APInt(C&: NegC),
3632 Op1: m_scev_SMax(Op0: m_scev_APInt(C), Op1: m_scev_Specific(S: RHS)))) &&
3633 NegC->isNegative() && !NegC->isMinSignedValue() && *C == -*NegC)
3634 return getZero(Ty: LHS->getType());
3635
3636 // TODO: Generalize to handle any common factors.
3637 // udiv (mul nuw a, vscale), (mul nuw b, vscale) --> udiv a, b
3638 const SCEV *NewLHS, *NewRHS;
3639 if (match(U: LHS, P: m_scev_c_NUWMul(Op0: m_SCEV(V&: NewLHS), Op1: m_SCEVVScale())) &&
3640 match(U: RHS, P: m_scev_c_NUWMul(Op0: m_SCEV(V&: NewRHS), Op1: m_SCEVVScale())))
3641 return getUDivExpr(LHS: NewLHS, RHS: NewRHS);
3642
3643 // The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs
3644 // changes). Make sure we get a new one.
3645 IP = nullptr;
3646 if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) return S;
3647 SCEV *S = new (SCEVAllocator) SCEVUDivExpr(ID.Intern(Allocator&: SCEVAllocator),
3648 LHS, RHS);
3649 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
3650 registerUser(User: S, Ops: ArrayRef<SCEVUse>({LHS, RHS}));
3651 return S;
3652}
3653
3654APInt gcd(const SCEVConstant *C1, const SCEVConstant *C2) {
3655 APInt A = C1->getAPInt().abs();
3656 APInt B = C2->getAPInt().abs();
3657 uint32_t ABW = A.getBitWidth();
3658 uint32_t BBW = B.getBitWidth();
3659
3660 if (ABW > BBW)
3661 B = B.zext(width: ABW);
3662 else if (ABW < BBW)
3663 A = A.zext(width: BBW);
3664
3665 return APIntOps::GreatestCommonDivisor(A: std::move(A), B: std::move(B));
3666}
3667
3668/// Get a canonical unsigned division expression, or something simpler if
3669/// possible. There is no representation for an exact udiv in SCEV IR, but we
3670/// can attempt to remove factors from the LHS and RHS. We can't do this when
3671/// it's not exact because the udiv may be clearing bits.
3672const SCEV *ScalarEvolution::getUDivExactExpr(SCEVUse LHS, SCEVUse RHS) {
3673 // TODO: we could try to find factors in all sorts of things, but for now we
3674 // just deal with u/exact (multiply, constant). See SCEVDivision towards the
3675 // end of this file for inspiration.
3676
3677 const SCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Val&: LHS);
3678 if (!Mul || !Mul->hasNoUnsignedWrap())
3679 return getUDivExpr(LHS, RHS);
3680
3681 if (const SCEVConstant *RHSCst = dyn_cast<SCEVConstant>(Val&: RHS)) {
3682 // If the mulexpr multiplies by a constant, then that constant must be the
3683 // first element of the mulexpr.
3684 if (const auto *LHSCst = dyn_cast<SCEVConstant>(Val: Mul->getOperand(i: 0))) {
3685 if (LHSCst == RHSCst) {
3686 SmallVector<SCEVUse, 2> Operands(drop_begin(RangeOrContainer: Mul->operands()));
3687 return getMulExpr(Ops&: Operands);
3688 }
3689
3690 // We can't just assume that LHSCst divides RHSCst cleanly, it could be
3691 // that there's a factor provided by one of the other terms. We need to
3692 // check.
3693 APInt Factor = gcd(C1: LHSCst, C2: RHSCst);
3694 if (!Factor.isIntN(N: 1)) {
3695 LHSCst =
3696 cast<SCEVConstant>(Val: getConstant(Val: LHSCst->getAPInt().udiv(RHS: Factor)));
3697 RHSCst =
3698 cast<SCEVConstant>(Val: getConstant(Val: RHSCst->getAPInt().udiv(RHS: Factor)));
3699 SmallVector<SCEVUse, 2> Operands;
3700 Operands.push_back(Elt: LHSCst);
3701 append_range(C&: Operands, R: Mul->operands().drop_front());
3702 LHS = getMulExpr(Ops&: Operands);
3703 RHS = RHSCst;
3704 Mul = dyn_cast<SCEVMulExpr>(Val&: LHS);
3705 if (!Mul)
3706 return getUDivExactExpr(LHS, RHS);
3707 }
3708 }
3709 }
3710
3711 for (int i = 0, e = Mul->getNumOperands(); i != e; ++i) {
3712 if (Mul->getOperand(i) == RHS) {
3713 SmallVector<SCEVUse, 2> Operands;
3714 append_range(C&: Operands, R: Mul->operands().take_front(N: i));
3715 append_range(C&: Operands, R: Mul->operands().drop_front(N: i + 1));
3716 return getMulExpr(Ops&: Operands);
3717 }
3718 }
3719
3720 return getUDivExpr(LHS, RHS);
3721}
3722
3723/// Get an add recurrence expression for the specified loop. Simplify the
3724/// expression as much as possible.
3725const SCEV *ScalarEvolution::getAddRecExpr(SCEVUse Start, SCEVUse Step,
3726 const Loop *L,
3727 SCEV::NoWrapFlags Flags) {
3728 SmallVector<SCEVUse, 4> Operands;
3729 Operands.push_back(Elt: Start);
3730 if (const SCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Val&: Step))
3731 if (StepChrec->getLoop() == L) {
3732 append_range(C&: Operands, R: StepChrec->operands());
3733 return getAddRecExpr(Operands, L, Flags: maskFlags(Flags, Mask: SCEV::FlagNW));
3734 }
3735
3736 Operands.push_back(Elt: Step);
3737 return getAddRecExpr(Operands, L, Flags);
3738}
3739
3740/// Get an add recurrence expression for the specified loop. Simplify the
3741/// expression as much as possible.
3742const SCEV *ScalarEvolution::getAddRecExpr(SmallVectorImpl<SCEVUse> &Operands,
3743 const Loop *L,
3744 SCEV::NoWrapFlags Flags) {
3745 if (Operands.size() == 1) return Operands[0];
3746#ifndef NDEBUG
3747 Type *ETy = getEffectiveSCEVType(Operands[0]->getType());
3748 for (const SCEV *Op : llvm::drop_begin(Operands)) {
3749 assert(getEffectiveSCEVType(Op->getType()) == ETy &&
3750 "SCEVAddRecExpr operand types don't match!");
3751 assert(!Op->getType()->isPointerTy() && "Step must be integer");
3752 }
3753 for (const SCEV *Op : Operands)
3754 assert(isAvailableAtLoopEntry(Op, L) &&
3755 "SCEVAddRecExpr operand is not available at loop entry!");
3756#endif
3757
3758 if (Operands.back()->isZero()) {
3759 Operands.pop_back();
3760 return getAddRecExpr(Operands, L, Flags: SCEV::FlagAnyWrap); // {X,+,0} --> X
3761 }
3762
3763 // It's tempting to want to call getConstantMaxBackedgeTakenCount count here and
3764 // use that information to infer NUW and NSW flags. However, computing a
3765 // BE count requires calling getAddRecExpr, so we may not yet have a
3766 // meaningful BE count at this point (and if we don't, we'd be stuck
3767 // with a SCEVCouldNotCompute as the cached BE count).
3768
3769 Flags = StrengthenNoWrapFlags(SE: this, Type: scAddRecExpr, Ops: Operands, Flags);
3770
3771 // Canonicalize nested AddRecs in by nesting them in order of loop depth.
3772 if (const SCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Val&: Operands[0])) {
3773 const Loop *NestedLoop = NestedAR->getLoop();
3774 if (L->contains(L: NestedLoop)
3775 ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
3776 : (!NestedLoop->contains(L) &&
3777 DT.dominates(A: L->getHeader(), B: NestedLoop->getHeader()))) {
3778 SmallVector<SCEVUse, 4> NestedOperands(NestedAR->operands());
3779 Operands[0] = NestedAR->getStart();
3780 // AddRecs require their operands be loop-invariant with respect to their
3781 // loops. Don't perform this transformation if it would break this
3782 // requirement.
3783 bool AllInvariant = all_of(
3784 Range&: Operands, P: [&](const SCEV *Op) { return isLoopInvariant(S: Op, L); });
3785
3786 if (AllInvariant) {
3787 // Create a recurrence for the outer loop with the same step size.
3788 //
3789 // The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
3790 // inner recurrence has the same property.
3791 SCEV::NoWrapFlags OuterFlags =
3792 maskFlags(Flags, Mask: SCEV::FlagNW | NestedAR->getNoWrapFlags());
3793
3794 NestedOperands[0] = getAddRecExpr(Operands, L, Flags: OuterFlags);
3795 AllInvariant = all_of(Range&: NestedOperands, P: [&](const SCEV *Op) {
3796 return isLoopInvariant(S: Op, L: NestedLoop);
3797 });
3798
3799 if (AllInvariant) {
3800 // Ok, both add recurrences are valid after the transformation.
3801 //
3802 // The inner recurrence keeps its NW flag but only keeps NUW/NSW if
3803 // the outer recurrence has the same property.
3804 SCEV::NoWrapFlags InnerFlags =
3805 maskFlags(Flags: NestedAR->getNoWrapFlags(), Mask: SCEV::FlagNW | Flags);
3806 return getAddRecExpr(Operands&: NestedOperands, L: NestedLoop, Flags: InnerFlags);
3807 }
3808 }
3809 // Reset Operands to its original state.
3810 Operands[0] = NestedAR;
3811 }
3812 }
3813
3814 // Okay, it looks like we really DO need an addrec expr. Check to see if we
3815 // already have one, otherwise create a new one.
3816 return getOrCreateAddRecExpr(Ops: Operands, L, Flags);
3817}
3818
3819const SCEV *ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3820 ArrayRef<SCEVUse> IndexExprs) {
3821 const SCEV *BaseExpr = getSCEV(V: GEP->getPointerOperand());
3822 // getSCEV(Base)->getType() has the same address space as Base->getType()
3823 // because SCEV::getType() preserves the address space.
3824 GEPNoWrapFlags NW = GEP->getNoWrapFlags();
3825 if (NW != GEPNoWrapFlags::none()) {
3826 // We'd like to propagate flags from the IR to the corresponding SCEV nodes,
3827 // but to do that, we have to ensure that said flag is valid in the entire
3828 // defined scope of the SCEV.
3829 // TODO: non-instructions have global scope. We might be able to prove
3830 // some global scope cases
3831 auto *GEPI = dyn_cast<Instruction>(Val: GEP);
3832 if (!GEPI || !isSCEVExprNeverPoison(I: GEPI))
3833 NW = GEPNoWrapFlags::none();
3834 }
3835
3836 return getGEPExpr(BaseExpr, IndexExprs, SrcElementTy: GEP->getSourceElementType(), NW);
3837}
3838
3839const SCEV *ScalarEvolution::getGEPExpr(SCEVUse BaseExpr,
3840 ArrayRef<SCEVUse> IndexExprs,
3841 Type *SrcElementTy, GEPNoWrapFlags NW) {
3842 SCEV::NoWrapFlags OffsetWrap = SCEV::FlagAnyWrap;
3843 if (NW.hasNoUnsignedSignedWrap())
3844 OffsetWrap = setFlags(Flags: OffsetWrap, OnFlags: SCEV::FlagNSW);
3845 if (NW.hasNoUnsignedWrap())
3846 OffsetWrap = setFlags(Flags: OffsetWrap, OnFlags: SCEV::FlagNUW);
3847
3848 Type *CurTy = BaseExpr->getType();
3849 Type *IntIdxTy = getEffectiveSCEVType(Ty: BaseExpr->getType());
3850 bool FirstIter = true;
3851 SmallVector<SCEVUse, 4> Offsets;
3852 for (SCEVUse IndexExpr : IndexExprs) {
3853 // Compute the (potentially symbolic) offset in bytes for this index.
3854 if (StructType *STy = dyn_cast<StructType>(Val: CurTy)) {
3855 // For a struct, add the member offset.
3856 ConstantInt *Index = cast<SCEVConstant>(Val&: IndexExpr)->getValue();
3857 unsigned FieldNo = Index->getZExtValue();
3858 const SCEV *FieldOffset = getOffsetOfExpr(IntTy: IntIdxTy, STy, FieldNo);
3859 Offsets.push_back(Elt: FieldOffset);
3860
3861 // Update CurTy to the type of the field at Index.
3862 CurTy = STy->getTypeAtIndex(V: Index);
3863 } else {
3864 // Update CurTy to its element type.
3865 if (FirstIter) {
3866 assert(isa<PointerType>(CurTy) &&
3867 "The first index of a GEP indexes a pointer");
3868 CurTy = SrcElementTy;
3869 FirstIter = false;
3870 } else {
3871 CurTy = GetElementPtrInst::getTypeAtIndex(Ty: CurTy, Idx: (uint64_t)0);
3872 }
3873 // For an array, add the element offset, explicitly scaled.
3874 const SCEV *ElementSize = getSizeOfExpr(IntTy: IntIdxTy, AllocTy: CurTy);
3875 // Getelementptr indices are signed.
3876 IndexExpr = getTruncateOrSignExtend(V: IndexExpr, Ty: IntIdxTy);
3877
3878 // Multiply the index by the element size to compute the element offset.
3879 const SCEV *LocalOffset = getMulExpr(LHS: IndexExpr, RHS: ElementSize, Flags: OffsetWrap);
3880 Offsets.push_back(Elt: LocalOffset);
3881 }
3882 }
3883
3884 // Handle degenerate case of GEP without offsets.
3885 if (Offsets.empty())
3886 return BaseExpr;
3887
3888 // Add the offsets together, assuming nsw if inbounds.
3889 const SCEV *Offset = getAddExpr(Ops&: Offsets, OrigFlags: OffsetWrap);
3890 // Add the base address and the offset. We cannot use the nsw flag, as the
3891 // base address is unsigned. However, if we know that the offset is
3892 // non-negative, we can use nuw.
3893 bool NUW = NW.hasNoUnsignedWrap() ||
3894 (NW.hasNoUnsignedSignedWrap() && isKnownNonNegative(S: Offset));
3895 SCEV::NoWrapFlags BaseWrap = NUW ? SCEV::FlagNUW : SCEV::FlagAnyWrap;
3896 auto *GEPExpr = getAddExpr(LHS: BaseExpr, RHS: Offset, Flags: BaseWrap);
3897 assert(BaseExpr->getType() == GEPExpr->getType() &&
3898 "GEP should not change type mid-flight.");
3899 return GEPExpr;
3900}
3901
3902SCEV *ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType,
3903 ArrayRef<const SCEV *> Ops) {
3904 FoldingSetNodeID ID;
3905 ID.AddInteger(I: SCEVType);
3906 for (const SCEV *Op : Ops)
3907 ID.AddPointer(Ptr: Op);
3908 void *IP = nullptr;
3909 return UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP);
3910}
3911
3912SCEV *ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType,
3913 ArrayRef<SCEVUse> Ops) {
3914 FoldingSetNodeID ID;
3915 ID.AddInteger(I: SCEVType);
3916 for (const SCEV *Op : Ops)
3917 ID.AddPointer(Ptr: Op);
3918 void *IP = nullptr;
3919 return UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP);
3920}
3921
3922const SCEV *ScalarEvolution::getAbsExpr(const SCEV *Op, bool IsNSW) {
3923 SCEV::NoWrapFlags Flags = IsNSW ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
3924 return getSMaxExpr(LHS: Op, RHS: getNegativeSCEV(V: Op, Flags));
3925}
3926
3927const SCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind,
3928 SmallVectorImpl<SCEVUse> &Ops) {
3929 assert(SCEVMinMaxExpr::isMinMaxType(Kind) && "Not a SCEVMinMaxExpr!");
3930 assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!");
3931 if (Ops.size() == 1) return Ops[0];
3932#ifndef NDEBUG
3933 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
3934 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
3935 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
3936 "Operand types don't match!");
3937 assert(Ops[0]->getType()->isPointerTy() ==
3938 Ops[i]->getType()->isPointerTy() &&
3939 "min/max should be consistently pointerish");
3940 }
3941#endif
3942
3943 bool IsSigned = Kind == scSMaxExpr || Kind == scSMinExpr;
3944 bool IsMax = Kind == scSMaxExpr || Kind == scUMaxExpr;
3945
3946 const SCEV *Folded = constantFoldAndGroupOps(
3947 SE&: *this, LI, DT, Ops,
3948 Fold: [&](const APInt &C1, const APInt &C2) {
3949 switch (Kind) {
3950 case scSMaxExpr:
3951 return APIntOps::smax(A: C1, B: C2);
3952 case scSMinExpr:
3953 return APIntOps::smin(A: C1, B: C2);
3954 case scUMaxExpr:
3955 return APIntOps::umax(A: C1, B: C2);
3956 case scUMinExpr:
3957 return APIntOps::umin(A: C1, B: C2);
3958 default:
3959 llvm_unreachable("Unknown SCEV min/max opcode");
3960 }
3961 },
3962 IsIdentity: [&](const APInt &C) {
3963 // identity
3964 if (IsMax)
3965 return IsSigned ? C.isMinSignedValue() : C.isMinValue();
3966 else
3967 return IsSigned ? C.isMaxSignedValue() : C.isMaxValue();
3968 },
3969 IsAbsorber: [&](const APInt &C) {
3970 // absorber
3971 if (IsMax)
3972 return IsSigned ? C.isMaxSignedValue() : C.isMaxValue();
3973 else
3974 return IsSigned ? C.isMinSignedValue() : C.isMinValue();
3975 });
3976 if (Folded)
3977 return Folded;
3978
3979 // Check if we have created the same expression before.
3980 if (const SCEV *S = findExistingSCEVInCache(SCEVType: Kind, Ops)) {
3981 return S;
3982 }
3983
3984 // Find the first operation of the same kind
3985 unsigned Idx = 0;
3986 while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind)
3987 ++Idx;
3988
3989 // Check to see if one of the operands is of the same kind. If so, expand its
3990 // operands onto our operand list, and recurse to simplify.
3991 if (Idx < Ops.size()) {
3992 bool DeletedAny = false;
3993 while (Ops[Idx]->getSCEVType() == Kind) {
3994 const SCEVMinMaxExpr *SMME = cast<SCEVMinMaxExpr>(Val&: Ops[Idx]);
3995 Ops.erase(CI: Ops.begin()+Idx);
3996 append_range(C&: Ops, R: SMME->operands());
3997 DeletedAny = true;
3998 }
3999
4000 if (DeletedAny)
4001 return getMinMaxExpr(Kind, Ops);
4002 }
4003
4004 // Okay, check to see if the same value occurs in the operand list twice. If
4005 // so, delete one. Since we sorted the list, these values are required to
4006 // be adjacent.
4007 llvm::CmpInst::Predicate GEPred =
4008 IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
4009 llvm::CmpInst::Predicate LEPred =
4010 IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
4011 llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred;
4012 llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred;
4013 for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) {
4014 if (Ops[i] == Ops[i + 1] ||
4015 isKnownViaNonRecursiveReasoning(Pred: FirstPred, LHS: Ops[i], RHS: Ops[i + 1])) {
4016 // X op Y op Y --> X op Y
4017 // X op Y --> X, if we know X, Y are ordered appropriately
4018 Ops.erase(CS: Ops.begin() + i + 1, CE: Ops.begin() + i + 2);
4019 --i;
4020 --e;
4021 } else if (isKnownViaNonRecursiveReasoning(Pred: SecondPred, LHS: Ops[i],
4022 RHS: Ops[i + 1])) {
4023 // X op Y --> Y, if we know X, Y are ordered appropriately
4024 Ops.erase(CS: Ops.begin() + i, CE: Ops.begin() + i + 1);
4025 --i;
4026 --e;
4027 }
4028 }
4029
4030 if (Ops.size() == 1) return Ops[0];
4031
4032 assert(!Ops.empty() && "Reduced smax down to nothing!");
4033
4034 // Okay, it looks like we really DO need an expr. Check to see if we
4035 // already have one, otherwise create a new one.
4036 FoldingSetNodeID ID;
4037 ID.AddInteger(I: Kind);
4038 for (const SCEV *Op : Ops)
4039 ID.AddPointer(Ptr: Op);
4040 void *IP = nullptr;
4041 const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP);
4042 if (ExistingSCEV)
4043 return ExistingSCEV;
4044 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Num: Ops.size());
4045 llvm::uninitialized_copy(Src&: Ops, Dst: O);
4046 SCEV *S = new (SCEVAllocator)
4047 SCEVMinMaxExpr(ID.Intern(Allocator&: SCEVAllocator), Kind, O, Ops.size());
4048
4049 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
4050 registerUser(User: S, Ops);
4051 return S;
4052}
4053
4054namespace {
4055
4056class SCEVSequentialMinMaxDeduplicatingVisitor final
4057 : public SCEVVisitor<SCEVSequentialMinMaxDeduplicatingVisitor,
4058 std::optional<const SCEV *>> {
4059 using RetVal = std::optional<const SCEV *>;
4060 using Base = SCEVVisitor<SCEVSequentialMinMaxDeduplicatingVisitor, RetVal>;
4061
4062 ScalarEvolution &SE;
4063 const SCEVTypes RootKind; // Must be a sequential min/max expression.
4064 const SCEVTypes NonSequentialRootKind; // Non-sequential variant of RootKind.
4065 SmallPtrSet<const SCEV *, 16> SeenOps;
4066
4067 bool canRecurseInto(SCEVTypes Kind) const {
4068 // We can only recurse into the SCEV expression of the same effective type
4069 // as the type of our root SCEV expression.
4070 return RootKind == Kind || NonSequentialRootKind == Kind;
4071 };
4072
4073 RetVal visitAnyMinMaxExpr(const SCEV *S) {
4074 assert((isa<SCEVMinMaxExpr>(S) || isa<SCEVSequentialMinMaxExpr>(S)) &&
4075 "Only for min/max expressions.");
4076 SCEVTypes Kind = S->getSCEVType();
4077
4078 if (!canRecurseInto(Kind))
4079 return S;
4080
4081 auto *NAry = cast<SCEVNAryExpr>(Val: S);
4082 SmallVector<SCEVUse> NewOps;
4083 bool Changed = visit(Kind, OrigOps: NAry->operands(), NewOps);
4084
4085 if (!Changed)
4086 return S;
4087 if (NewOps.empty())
4088 return std::nullopt;
4089
4090 return isa<SCEVSequentialMinMaxExpr>(Val: S)
4091 ? SE.getSequentialMinMaxExpr(Kind, Operands&: NewOps)
4092 : SE.getMinMaxExpr(Kind, Ops&: NewOps);
4093 }
4094
4095 RetVal visit(const SCEV *S) {
4096 // Has the whole operand been seen already?
4097 if (!SeenOps.insert(Ptr: S).second)
4098 return std::nullopt;
4099 return Base::visit(S);
4100 }
4101
4102public:
4103 SCEVSequentialMinMaxDeduplicatingVisitor(ScalarEvolution &SE,
4104 SCEVTypes RootKind)
4105 : SE(SE), RootKind(RootKind),
4106 NonSequentialRootKind(
4107 SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(
4108 Ty: RootKind)) {}
4109
4110 bool /*Changed*/ visit(SCEVTypes Kind, ArrayRef<SCEVUse> OrigOps,
4111 SmallVectorImpl<SCEVUse> &NewOps) {
4112 bool Changed = false;
4113 SmallVector<SCEVUse> Ops;
4114 Ops.reserve(N: OrigOps.size());
4115
4116 for (const SCEV *Op : OrigOps) {
4117 RetVal NewOp = visit(S: Op);
4118 if (NewOp != Op)
4119 Changed = true;
4120 if (NewOp)
4121 Ops.emplace_back(Args&: *NewOp);
4122 }
4123
4124 if (Changed)
4125 NewOps = std::move(Ops);
4126 return Changed;
4127 }
4128
4129 RetVal visitConstant(const SCEVConstant *Constant) { return Constant; }
4130
4131 RetVal visitVScale(const SCEVVScale *VScale) { return VScale; }
4132
4133 RetVal visitPtrToAddrExpr(const SCEVPtrToAddrExpr *Expr) { return Expr; }
4134
4135 RetVal visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) { return Expr; }
4136
4137 RetVal visitTruncateExpr(const SCEVTruncateExpr *Expr) { return Expr; }
4138
4139 RetVal visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) { return Expr; }
4140
4141 RetVal visitSignExtendExpr(const SCEVSignExtendExpr *Expr) { return Expr; }
4142
4143 RetVal visitAddExpr(const SCEVAddExpr *Expr) { return Expr; }
4144
4145 RetVal visitMulExpr(const SCEVMulExpr *Expr) { return Expr; }
4146
4147 RetVal visitUDivExpr(const SCEVUDivExpr *Expr) { return Expr; }
4148
4149 RetVal visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; }
4150
4151 RetVal visitSMaxExpr(const SCEVSMaxExpr *Expr) {
4152 return visitAnyMinMaxExpr(S: Expr);
4153 }
4154
4155 RetVal visitUMaxExpr(const SCEVUMaxExpr *Expr) {
4156 return visitAnyMinMaxExpr(S: Expr);
4157 }
4158
4159 RetVal visitSMinExpr(const SCEVSMinExpr *Expr) {
4160 return visitAnyMinMaxExpr(S: Expr);
4161 }
4162
4163 RetVal visitUMinExpr(const SCEVUMinExpr *Expr) {
4164 return visitAnyMinMaxExpr(S: Expr);
4165 }
4166
4167 RetVal visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr) {
4168 return visitAnyMinMaxExpr(S: Expr);
4169 }
4170
4171 RetVal visitUnknown(const SCEVUnknown *Expr) { return Expr; }
4172
4173 RetVal visitCouldNotCompute(const SCEVCouldNotCompute *Expr) { return Expr; }
4174};
4175
4176} // namespace
4177
4178static bool scevUnconditionallyPropagatesPoisonFromOperands(SCEVTypes Kind) {
4179 switch (Kind) {
4180 case scConstant:
4181 case scVScale:
4182 case scTruncate:
4183 case scZeroExtend:
4184 case scSignExtend:
4185 case scPtrToAddr:
4186 case scPtrToInt:
4187 case scAddExpr:
4188 case scMulExpr:
4189 case scUDivExpr:
4190 case scAddRecExpr:
4191 case scUMaxExpr:
4192 case scSMaxExpr:
4193 case scUMinExpr:
4194 case scSMinExpr:
4195 case scUnknown:
4196 // If any operand is poison, the whole expression is poison.
4197 return true;
4198 case scSequentialUMinExpr:
4199 // FIXME: if the *first* operand is poison, the whole expression is poison.
4200 return false; // Pessimistically, say that it does not propagate poison.
4201 case scCouldNotCompute:
4202 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
4203 }
4204 llvm_unreachable("Unknown SCEV kind!");
4205}
4206
4207namespace {
4208// The only way poison may be introduced in a SCEV expression is from a
4209// poison SCEVUnknown (ConstantExprs are also represented as SCEVUnknown,
4210// not SCEVConstant). Notably, nowrap flags in SCEV nodes can *not*
4211// introduce poison -- they encode guaranteed, non-speculated knowledge.
4212//
4213// Additionally, all SCEV nodes propagate poison from inputs to outputs,
4214// with the notable exception of umin_seq, where only poison from the first
4215// operand is (unconditionally) propagated.
4216struct SCEVPoisonCollector {
4217 bool LookThroughMaybePoisonBlocking;
4218 SmallPtrSet<const SCEVUnknown *, 4> MaybePoison;
4219 SCEVPoisonCollector(bool LookThroughMaybePoisonBlocking)
4220 : LookThroughMaybePoisonBlocking(LookThroughMaybePoisonBlocking) {}
4221
4222 bool follow(const SCEV *S) {
4223 if (!LookThroughMaybePoisonBlocking &&
4224 !scevUnconditionallyPropagatesPoisonFromOperands(Kind: S->getSCEVType()))
4225 return false;
4226
4227 if (auto *SU = dyn_cast<SCEVUnknown>(Val: S)) {
4228 if (!isGuaranteedNotToBePoison(V: SU->getValue()))
4229 MaybePoison.insert(Ptr: SU);
4230 }
4231 return true;
4232 }
4233 bool isDone() const { return false; }
4234};
4235} // namespace
4236
4237/// Return true if V is poison given that AssumedPoison is already poison.
4238static bool impliesPoison(const SCEV *AssumedPoison, const SCEV *S) {
4239 // First collect all SCEVs that might result in AssumedPoison to be poison.
4240 // We need to look through potentially poison-blocking operations here,
4241 // because we want to find all SCEVs that *might* result in poison, not only
4242 // those that are *required* to.
4243 SCEVPoisonCollector PC1(/* LookThroughMaybePoisonBlocking */ true);
4244 visitAll(Root: AssumedPoison, Visitor&: PC1);
4245
4246 // AssumedPoison is never poison. As the assumption is false, the implication
4247 // is true. Don't bother walking the other SCEV in this case.
4248 if (PC1.MaybePoison.empty())
4249 return true;
4250
4251 // Collect all SCEVs in S that, if poison, *will* result in S being poison
4252 // as well. We cannot look through potentially poison-blocking operations
4253 // here, as their arguments only *may* make the result poison.
4254 SCEVPoisonCollector PC2(/* LookThroughMaybePoisonBlocking */ false);
4255 visitAll(Root: S, Visitor&: PC2);
4256
4257 // Make sure that no matter which SCEV in PC1.MaybePoison is actually poison,
4258 // it will also make S poison by being part of PC2.MaybePoison.
4259 return llvm::set_is_subset(S1: PC1.MaybePoison, S2: PC2.MaybePoison);
4260}
4261
4262void ScalarEvolution::getPoisonGeneratingValues(
4263 SmallPtrSetImpl<const Value *> &Result, const SCEV *S) {
4264 SCEVPoisonCollector PC(/* LookThroughMaybePoisonBlocking */ false);
4265 visitAll(Root: S, Visitor&: PC);
4266 for (const SCEVUnknown *SU : PC.MaybePoison)
4267 Result.insert(Ptr: SU->getValue());
4268}
4269
4270bool ScalarEvolution::canReuseInstruction(
4271 const SCEV *S, Instruction *I,
4272 SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts) {
4273 // If the instruction cannot be poison, it's always safe to reuse.
4274 if (programUndefinedIfPoison(Inst: I))
4275 return true;
4276
4277 // Otherwise, it is possible that I is more poisonous that S. Collect the
4278 // poison-contributors of S, and then check whether I has any additional
4279 // poison-contributors. Poison that is contributed through poison-generating
4280 // flags is handled by dropping those flags instead.
4281 SmallPtrSet<const Value *, 8> PoisonVals;
4282 getPoisonGeneratingValues(Result&: PoisonVals, S);
4283
4284 SmallVector<Value *> Worklist;
4285 SmallPtrSet<Value *, 8> Visited;
4286 Worklist.push_back(Elt: I);
4287 while (!Worklist.empty()) {
4288 Value *V = Worklist.pop_back_val();
4289 if (!Visited.insert(Ptr: V).second)
4290 continue;
4291
4292 // Avoid walking large instruction graphs.
4293 if (Visited.size() > 16)
4294 return false;
4295
4296 // Either the value can't be poison, or the S would also be poison if it
4297 // is.
4298 if (PoisonVals.contains(Ptr: V) || ::isGuaranteedNotToBePoison(V))
4299 continue;
4300
4301 auto *I = dyn_cast<Instruction>(Val: V);
4302 if (!I)
4303 return false;
4304
4305 // Disjoint or instructions are interpreted as adds by SCEV. However, we
4306 // can't replace an arbitrary add with disjoint or, even if we drop the
4307 // flag. We would need to convert the or into an add.
4308 if (auto *PDI = dyn_cast<PossiblyDisjointInst>(Val: I))
4309 if (PDI->isDisjoint())
4310 return false;
4311
4312 // FIXME: Ignore vscale, even though it technically could be poison. Do this
4313 // because SCEV currently assumes it can't be poison. Remove this special
4314 // case once we proper model when vscale can be poison.
4315 if (auto *II = dyn_cast<IntrinsicInst>(Val: I);
4316 II && II->getIntrinsicID() == Intrinsic::vscale)
4317 continue;
4318
4319 if (canCreatePoison(Op: cast<Operator>(Val: I), /*ConsiderFlagsAndMetadata*/ false))
4320 return false;
4321
4322 // If the instruction can't create poison, we can recurse to its operands.
4323 if (I->hasPoisonGeneratingAnnotations())
4324 DropPoisonGeneratingInsts.push_back(Elt: I);
4325
4326 llvm::append_range(C&: Worklist, R: I->operands());
4327 }
4328 return true;
4329}
4330
4331const SCEV *
4332ScalarEvolution::getSequentialMinMaxExpr(SCEVTypes Kind,
4333 SmallVectorImpl<SCEVUse> &Ops) {
4334 assert(SCEVSequentialMinMaxExpr::isSequentialMinMaxType(Kind) &&
4335 "Not a SCEVSequentialMinMaxExpr!");
4336 assert(!Ops.empty() && "Cannot get empty (u|s)(min|max)!");
4337 if (Ops.size() == 1)
4338 return Ops[0];
4339#ifndef NDEBUG
4340 Type *ETy = getEffectiveSCEVType(Ops[0]->getType());
4341 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
4342 assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
4343 "Operand types don't match!");
4344 assert(Ops[0]->getType()->isPointerTy() ==
4345 Ops[i]->getType()->isPointerTy() &&
4346 "min/max should be consistently pointerish");
4347 }
4348#endif
4349
4350 // Note that SCEVSequentialMinMaxExpr is *NOT* commutative,
4351 // so we can *NOT* do any kind of sorting of the expressions!
4352
4353 // Check if we have created the same expression before.
4354 if (const SCEV *S = findExistingSCEVInCache(SCEVType: Kind, Ops))
4355 return S;
4356
4357 // FIXME: there are *some* simplifications that we can do here.
4358
4359 // Keep only the first instance of an operand.
4360 {
4361 SCEVSequentialMinMaxDeduplicatingVisitor Deduplicator(*this, Kind);
4362 bool Changed = Deduplicator.visit(Kind, OrigOps: Ops, NewOps&: Ops);
4363 if (Changed)
4364 return getSequentialMinMaxExpr(Kind, Ops);
4365 }
4366
4367 // Check to see if one of the operands is of the same kind. If so, expand its
4368 // operands onto our operand list, and recurse to simplify.
4369 {
4370 unsigned Idx = 0;
4371 bool DeletedAny = false;
4372 while (Idx < Ops.size()) {
4373 if (Ops[Idx]->getSCEVType() != Kind) {
4374 ++Idx;
4375 continue;
4376 }
4377 const auto *SMME = cast<SCEVSequentialMinMaxExpr>(Val&: Ops[Idx]);
4378 Ops.erase(CI: Ops.begin() + Idx);
4379 Ops.insert(I: Ops.begin() + Idx, From: SMME->operands().begin(),
4380 To: SMME->operands().end());
4381 DeletedAny = true;
4382 }
4383
4384 if (DeletedAny)
4385 return getSequentialMinMaxExpr(Kind, Ops);
4386 }
4387
4388 const SCEV *SaturationPoint;
4389 ICmpInst::Predicate Pred;
4390 switch (Kind) {
4391 case scSequentialUMinExpr:
4392 SaturationPoint = getZero(Ty: Ops[0]->getType());
4393 Pred = ICmpInst::ICMP_ULE;
4394 break;
4395 default:
4396 llvm_unreachable("Not a sequential min/max type.");
4397 }
4398
4399 for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
4400 if (!isGuaranteedNotToCauseUB(Op: Ops[i]))
4401 continue;
4402 // We can replace %x umin_seq %y with %x umin %y if either:
4403 // * %y being poison implies %x is also poison.
4404 // * %x cannot be the saturating value (e.g. zero for umin).
4405 if (::impliesPoison(AssumedPoison: Ops[i], S: Ops[i - 1]) ||
4406 isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_NE, LHS: Ops[i - 1],
4407 RHS: SaturationPoint)) {
4408 SmallVector<SCEVUse, 2> SeqOps = {Ops[i - 1], Ops[i]};
4409 Ops[i - 1] = getMinMaxExpr(
4410 Kind: SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(Ty: Kind),
4411 Ops&: SeqOps);
4412 Ops.erase(CI: Ops.begin() + i);
4413 return getSequentialMinMaxExpr(Kind, Ops);
4414 }
4415 // Fold %x umin_seq %y to %x if %x ule %y.
4416 // TODO: We might be able to prove the predicate for a later operand.
4417 if (isKnownViaNonRecursiveReasoning(Pred, LHS: Ops[i - 1], RHS: Ops[i])) {
4418 Ops.erase(CI: Ops.begin() + i);
4419 return getSequentialMinMaxExpr(Kind, Ops);
4420 }
4421 }
4422
4423 // Okay, it looks like we really DO need an expr. Check to see if we
4424 // already have one, otherwise create a new one.
4425 FoldingSetNodeID ID;
4426 ID.AddInteger(I: Kind);
4427 for (const SCEV *Op : Ops)
4428 ID.AddPointer(Ptr: Op);
4429 void *IP = nullptr;
4430 const SCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP);
4431 if (ExistingSCEV)
4432 return ExistingSCEV;
4433
4434 SCEVUse *O = SCEVAllocator.Allocate<SCEVUse>(Num: Ops.size());
4435 llvm::uninitialized_copy(Src&: Ops, Dst: O);
4436 SCEV *S = new (SCEVAllocator)
4437 SCEVSequentialMinMaxExpr(ID.Intern(Allocator&: SCEVAllocator), Kind, O, Ops.size());
4438
4439 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
4440 registerUser(User: S, Ops);
4441 return S;
4442}
4443
4444const SCEV *ScalarEvolution::getSMaxExpr(SCEVUse LHS, SCEVUse RHS) {
4445 SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
4446 return getMinMaxExpr(Kind: scSMaxExpr, Ops);
4447}
4448
4449const SCEV *ScalarEvolution::getSMaxExpr(SmallVectorImpl<SCEVUse> &Ops) {
4450 return getMinMaxExpr(Kind: scSMaxExpr, Ops);
4451}
4452
4453const SCEV *ScalarEvolution::getUMaxExpr(SCEVUse LHS, SCEVUse RHS) {
4454 SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
4455 return getMinMaxExpr(Kind: scUMaxExpr, Ops);
4456}
4457
4458const SCEV *ScalarEvolution::getUMaxExpr(SmallVectorImpl<SCEVUse> &Ops) {
4459 return getMinMaxExpr(Kind: scUMaxExpr, Ops);
4460}
4461
4462const SCEV *ScalarEvolution::getSMinExpr(SCEVUse LHS, SCEVUse RHS) {
4463 SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
4464 return getMinMaxExpr(Kind: scSMinExpr, Ops);
4465}
4466
4467const SCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl<SCEVUse> &Ops) {
4468 return getMinMaxExpr(Kind: scSMinExpr, Ops);
4469}
4470
4471const SCEV *ScalarEvolution::getUMinExpr(SCEVUse LHS, SCEVUse RHS,
4472 bool Sequential) {
4473 SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
4474 return getUMinExpr(Operands&: Ops, Sequential);
4475}
4476
4477const SCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl<SCEVUse> &Ops,
4478 bool Sequential) {
4479 return Sequential ? getSequentialMinMaxExpr(Kind: scSequentialUMinExpr, Ops)
4480 : getMinMaxExpr(Kind: scUMinExpr, Ops);
4481}
4482
4483const SCEV *
4484ScalarEvolution::getSizeOfExpr(Type *IntTy, TypeSize Size) {
4485 const SCEV *Res = getConstant(Ty: IntTy, V: Size.getKnownMinValue());
4486 if (Size.isScalable())
4487 Res = getMulExpr(LHS: Res, RHS: getVScale(Ty: IntTy));
4488 return Res;
4489}
4490
4491const SCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy, Type *AllocTy) {
4492 return getSizeOfExpr(IntTy, Size: getDataLayout().getTypeAllocSize(Ty: AllocTy));
4493}
4494
4495const SCEV *ScalarEvolution::getStoreSizeOfExpr(Type *IntTy, Type *StoreTy) {
4496 return getSizeOfExpr(IntTy, Size: getDataLayout().getTypeStoreSize(Ty: StoreTy));
4497}
4498
4499const SCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
4500 StructType *STy,
4501 unsigned FieldNo) {
4502 // We can bypass creating a target-independent constant expression and then
4503 // folding it back into a ConstantInt. This is just a compile-time
4504 // optimization.
4505 const StructLayout *SL = getDataLayout().getStructLayout(Ty: STy);
4506 assert(!SL->getSizeInBits().isScalable() &&
4507 "Cannot get offset for structure containing scalable vector types");
4508 return getConstant(Ty: IntTy, V: SL->getElementOffset(Idx: FieldNo));
4509}
4510
4511const SCEV *ScalarEvolution::getUnknown(Value *V) {
4512 // Don't attempt to do anything other than create a SCEVUnknown object
4513 // here. createSCEV only calls getUnknown after checking for all other
4514 // interesting possibilities, and any other code that calls getUnknown
4515 // is doing so in order to hide a value from SCEV canonicalization.
4516
4517 FoldingSetNodeID ID;
4518 ID.AddInteger(I: scUnknown);
4519 ID.AddPointer(Ptr: V);
4520 void *IP = nullptr;
4521 if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, InsertPos&: IP)) {
4522 assert(cast<SCEVUnknown>(S)->getValue() == V &&
4523 "Stale SCEVUnknown in uniquing map!");
4524 return S;
4525 }
4526 SCEV *S = new (SCEVAllocator) SCEVUnknown(ID.Intern(Allocator&: SCEVAllocator), V, this,
4527 FirstUnknown);
4528 FirstUnknown = cast<SCEVUnknown>(Val: S);
4529 UniqueSCEVs.InsertNode(N: S, InsertPos: IP);
4530 return S;
4531}
4532
4533//===----------------------------------------------------------------------===//
4534// Basic SCEV Analysis and PHI Idiom Recognition Code
4535//
4536
4537/// Test if values of the given type are analyzable within the SCEV
4538/// framework. This primarily includes integer types, and it can optionally
4539/// include pointer types if the ScalarEvolution class has access to
4540/// target-specific information.
4541bool ScalarEvolution::isSCEVable(Type *Ty) const {
4542 // Integers and pointers are always SCEVable.
4543 return Ty->isIntOrPtrTy();
4544}
4545
4546/// Return the size in bits of the specified type, for which isSCEVable must
4547/// return true.
4548uint64_t ScalarEvolution::getTypeSizeInBits(Type *Ty) const {
4549 assert(isSCEVable(Ty) && "Type is not SCEVable!");
4550 if (Ty->isPointerTy())
4551 return getDataLayout().getIndexTypeSizeInBits(Ty);
4552 return getDataLayout().getTypeSizeInBits(Ty);
4553}
4554
4555/// Return a type with the same bitwidth as the given type and which represents
4556/// how SCEV will treat the given type, for which isSCEVable must return
4557/// true. For pointer types, this is the pointer index sized integer type.
4558Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const {
4559 assert(isSCEVable(Ty) && "Type is not SCEVable!");
4560
4561 if (Ty->isIntegerTy())
4562 return Ty;
4563
4564 // The only other support type is pointer.
4565 assert(Ty->isPointerTy() && "Unexpected non-pointer non-integer type!");
4566 return getDataLayout().getIndexType(PtrTy: Ty);
4567}
4568
4569Type *ScalarEvolution::getWiderType(Type *T1, Type *T2) const {
4570 return getTypeSizeInBits(Ty: T1) >= getTypeSizeInBits(Ty: T2) ? T1 : T2;
4571}
4572
4573bool ScalarEvolution::instructionCouldExistWithOperands(const SCEV *A,
4574 const SCEV *B) {
4575 /// For a valid use point to exist, the defining scope of one operand
4576 /// must dominate the other.
4577 bool PreciseA, PreciseB;
4578 auto *ScopeA = getDefiningScopeBound(Ops: {A}, Precise&: PreciseA);
4579 auto *ScopeB = getDefiningScopeBound(Ops: {B}, Precise&: PreciseB);
4580 if (!PreciseA || !PreciseB)
4581 // Can't tell.
4582 return false;
4583 return (ScopeA == ScopeB) || DT.dominates(Def: ScopeA, User: ScopeB) ||
4584 DT.dominates(Def: ScopeB, User: ScopeA);
4585}
4586
4587const SCEV *ScalarEvolution::getCouldNotCompute() {
4588 return CouldNotCompute.get();
4589}
4590
4591bool ScalarEvolution::checkValidity(const SCEV *S) const {
4592 bool ContainsNulls = SCEVExprContains(Root: S, Pred: [](const SCEV *S) {
4593 auto *SU = dyn_cast<SCEVUnknown>(Val: S);
4594 return SU && SU->getValue() == nullptr;
4595 });
4596
4597 return !ContainsNulls;
4598}
4599
4600bool ScalarEvolution::containsAddRecurrence(const SCEV *S) {
4601 HasRecMapType::iterator I = HasRecMap.find(Val: S);
4602 if (I != HasRecMap.end())
4603 return I->second;
4604
4605 bool FoundAddRec =
4606 SCEVExprContains(Root: S, Pred: [](const SCEV *S) { return isa<SCEVAddRecExpr>(Val: S); });
4607 HasRecMap.insert(KV: {S, FoundAddRec});
4608 return FoundAddRec;
4609}
4610
4611/// Return the ValueOffsetPair set for \p S. \p S can be represented
4612/// by the value and offset from any ValueOffsetPair in the set.
4613ArrayRef<Value *> ScalarEvolution::getSCEVValues(const SCEV *S) {
4614 ExprValueMapType::iterator SI = ExprValueMap.find_as(Val: S);
4615 if (SI == ExprValueMap.end())
4616 return {};
4617 return SI->second.getArrayRef();
4618}
4619
4620/// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
4621/// cannot be used separately. eraseValueFromMap should be used to remove
4622/// V from ValueExprMap and ExprValueMap at the same time.
4623void ScalarEvolution::eraseValueFromMap(Value *V) {
4624 ValueExprMapType::iterator I = ValueExprMap.find_as(Val: V);
4625 if (I != ValueExprMap.end()) {
4626 auto EVIt = ExprValueMap.find(Val: I->second);
4627 bool Removed = EVIt->second.remove(X: V);
4628 (void) Removed;
4629 assert(Removed && "Value not in ExprValueMap?");
4630 ValueExprMap.erase(I);
4631 }
4632}
4633
4634void ScalarEvolution::insertValueToMap(Value *V, const SCEV *S) {
4635 // A recursive query may have already computed the SCEV. It should be
4636 // equivalent, but may not necessarily be exactly the same, e.g. due to lazily
4637 // inferred nowrap flags.
4638 auto It = ValueExprMap.find_as(Val: V);
4639 if (It == ValueExprMap.end()) {
4640 ValueExprMap.insert(KV: {SCEVCallbackVH(V, this), S});
4641 ExprValueMap[S].insert(X: V);
4642 }
4643}
4644
4645/// Return an existing SCEV if it exists, otherwise analyze the expression and
4646/// create a new one.
4647const SCEV *ScalarEvolution::getSCEV(Value *V) {
4648 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
4649
4650 if (const SCEV *S = getExistingSCEV(V))
4651 return S;
4652 return createSCEVIter(V);
4653}
4654
4655const SCEV *ScalarEvolution::getExistingSCEV(Value *V) {
4656 assert(isSCEVable(V->getType()) && "Value is not SCEVable!");
4657
4658 ValueExprMapType::iterator I = ValueExprMap.find_as(Val: V);
4659 if (I != ValueExprMap.end()) {
4660 const SCEV *S = I->second;
4661 assert(checkValidity(S) &&
4662 "existing SCEV has not been properly invalidated");
4663 return S;
4664 }
4665 return nullptr;
4666}
4667
4668/// Return a SCEV corresponding to -V = -1*V
4669const SCEV *ScalarEvolution::getNegativeSCEV(const SCEV *V,
4670 SCEV::NoWrapFlags Flags) {
4671 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(Val: V))
4672 return getConstant(
4673 V: cast<ConstantInt>(Val: ConstantExpr::getNeg(C: VC->getValue())));
4674
4675 Type *Ty = V->getType();
4676 Ty = getEffectiveSCEVType(Ty);
4677 return getMulExpr(LHS: V, RHS: getMinusOne(Ty), Flags);
4678}
4679
4680/// If Expr computes ~A, return A else return nullptr
4681static const SCEV *MatchNotExpr(const SCEV *Expr) {
4682 const SCEV *MulOp;
4683 if (match(S: Expr, P: m_scev_Add(Op0: m_scev_AllOnes(),
4684 Op1: m_scev_Mul(Op0: m_scev_AllOnes(), Op1: m_SCEV(V&: MulOp)))))
4685 return MulOp;
4686 return nullptr;
4687}
4688
4689/// Return a SCEV corresponding to ~V = -1-V
4690const SCEV *ScalarEvolution::getNotSCEV(const SCEV *V) {
4691 assert(!V->getType()->isPointerTy() && "Can't negate pointer");
4692
4693 if (const SCEVConstant *VC = dyn_cast<SCEVConstant>(Val: V))
4694 return getConstant(
4695 V: cast<ConstantInt>(Val: ConstantExpr::getNot(C: VC->getValue())));
4696
4697 // Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y)
4698 if (const SCEVMinMaxExpr *MME = dyn_cast<SCEVMinMaxExpr>(Val: V)) {
4699 auto MatchMinMaxNegation = [&](const SCEVMinMaxExpr *MME) {
4700 SmallVector<SCEVUse, 2> MatchedOperands;
4701 for (const SCEV *Operand : MME->operands()) {
4702 const SCEV *Matched = MatchNotExpr(Expr: Operand);
4703 if (!Matched)
4704 return (const SCEV *)nullptr;
4705 MatchedOperands.push_back(Elt: Matched);
4706 }
4707 return getMinMaxExpr(Kind: SCEVMinMaxExpr::negate(T: MME->getSCEVType()),
4708 Ops&: MatchedOperands);
4709 };
4710 if (const SCEV *Replaced = MatchMinMaxNegation(MME))
4711 return Replaced;
4712 }
4713
4714 Type *Ty = V->getType();
4715 Ty = getEffectiveSCEVType(Ty);
4716 return getMinusSCEV(LHS: getMinusOne(Ty), RHS: V);
4717}
4718
4719const SCEV *ScalarEvolution::removePointerBase(const SCEV *P) {
4720 assert(P->getType()->isPointerTy());
4721
4722 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Val: P)) {
4723 // The base of an AddRec is the first operand.
4724 SmallVector<SCEVUse> Ops{AddRec->operands()};
4725 Ops[0] = removePointerBase(P: Ops[0]);
4726 // Don't try to transfer nowrap flags for now. We could in some cases
4727 // (for example, if pointer operand of the AddRec is a SCEVUnknown).
4728 return getAddRecExpr(Operands&: Ops, L: AddRec->getLoop(), Flags: SCEV::FlagAnyWrap);
4729 }
4730 if (auto *Add = dyn_cast<SCEVAddExpr>(Val: P)) {
4731 // The base of an Add is the pointer operand.
4732 SmallVector<SCEVUse> Ops{Add->operands()};
4733 SCEVUse *PtrOp = nullptr;
4734 for (SCEVUse &AddOp : Ops) {
4735 if (AddOp->getType()->isPointerTy()) {
4736 assert(!PtrOp && "Cannot have multiple pointer ops");
4737 PtrOp = &AddOp;
4738 }
4739 }
4740 *PtrOp = removePointerBase(P: *PtrOp);
4741 // Don't try to transfer nowrap flags for now. We could in some cases
4742 // (for example, if the pointer operand of the Add is a SCEVUnknown).
4743 return getAddExpr(Ops);
4744 }
4745 // Any other expression must be a pointer base.
4746 return getZero(Ty: P->getType());
4747}
4748
4749const SCEV *ScalarEvolution::getMinusSCEV(SCEVUse LHS, SCEVUse RHS,
4750 SCEV::NoWrapFlags Flags,
4751 unsigned Depth) {
4752 // Fast path: X - X --> 0.
4753 if (LHS == RHS)
4754 return getZero(Ty: LHS->getType());
4755
4756 // If we subtract two pointers with different pointer bases, bail.
4757 // Eventually, we're going to add an assertion to getMulExpr that we
4758 // can't multiply by a pointer.
4759 if (RHS->getType()->isPointerTy()) {
4760 if (!LHS->getType()->isPointerTy() ||
4761 getPointerBase(V: LHS) != getPointerBase(V: RHS))
4762 return getCouldNotCompute();
4763 LHS = removePointerBase(P: LHS);
4764 RHS = removePointerBase(P: RHS);
4765 }
4766
4767 // We represent LHS - RHS as LHS + (-1)*RHS. This transformation
4768 // makes it so that we cannot make much use of NUW.
4769 auto AddFlags = SCEV::FlagAnyWrap;
4770 const bool RHSIsNotMinSigned =
4771 !getSignedRangeMin(S: RHS).isMinSignedValue();
4772 if (hasFlags(Flags, TestFlags: SCEV::FlagNSW)) {
4773 // Let M be the minimum representable signed value. Then (-1)*RHS
4774 // signed-wraps if and only if RHS is M. That can happen even for
4775 // a NSW subtraction because e.g. (-1)*M signed-wraps even though
4776 // -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
4777 // (-1)*RHS, we need to prove that RHS != M.
4778 //
4779 // If LHS is non-negative and we know that LHS - RHS does not
4780 // signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
4781 // either by proving that RHS > M or that LHS >= 0.
4782 if (RHSIsNotMinSigned || isKnownNonNegative(S: LHS)) {
4783 AddFlags = SCEV::FlagNSW;
4784 }
4785 }
4786
4787 // FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
4788 // RHS is NSW and LHS >= 0.
4789 //
4790 // The difficulty here is that the NSW flag may have been proven
4791 // relative to a loop that is to be found in a recurrence in LHS and
4792 // not in RHS. Applying NSW to (-1)*M may then let the NSW have a
4793 // larger scope than intended.
4794 auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
4795
4796 return getAddExpr(LHS, RHS: getNegativeSCEV(V: RHS, Flags: NegFlags), Flags: AddFlags, Depth);
4797}
4798
4799const SCEV *ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty,
4800 unsigned Depth) {
4801 Type *SrcTy = V->getType();
4802 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4803 "Cannot truncate or zero extend with non-integer arguments!");
4804 if (getTypeSizeInBits(Ty: SrcTy) == getTypeSizeInBits(Ty))
4805 return V; // No conversion
4806 if (getTypeSizeInBits(Ty: SrcTy) > getTypeSizeInBits(Ty))
4807 return getTruncateExpr(Op: V, Ty, Depth);
4808 return getZeroExtendExpr(Op: V, Ty, Depth);
4809}
4810
4811const SCEV *ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, Type *Ty,
4812 unsigned Depth) {
4813 Type *SrcTy = V->getType();
4814 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4815 "Cannot truncate or zero extend with non-integer arguments!");
4816 if (getTypeSizeInBits(Ty: SrcTy) == getTypeSizeInBits(Ty))
4817 return V; // No conversion
4818 if (getTypeSizeInBits(Ty: SrcTy) > getTypeSizeInBits(Ty))
4819 return getTruncateExpr(Op: V, Ty, Depth);
4820 return getSignExtendExpr(Op: V, Ty, Depth);
4821}
4822
4823const SCEV *
4824ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
4825 Type *SrcTy = V->getType();
4826 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4827 "Cannot noop or zero extend with non-integer arguments!");
4828 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
4829 "getNoopOrZeroExtend cannot truncate!");
4830 if (getTypeSizeInBits(Ty: SrcTy) == getTypeSizeInBits(Ty))
4831 return V; // No conversion
4832 return getZeroExtendExpr(Op: V, Ty);
4833}
4834
4835const SCEV *
4836ScalarEvolution::getNoopOrSignExtend(const SCEV *V, Type *Ty) {
4837 Type *SrcTy = V->getType();
4838 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4839 "Cannot noop or sign extend with non-integer arguments!");
4840 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
4841 "getNoopOrSignExtend cannot truncate!");
4842 if (getTypeSizeInBits(Ty: SrcTy) == getTypeSizeInBits(Ty))
4843 return V; // No conversion
4844 return getSignExtendExpr(Op: V, Ty);
4845}
4846
4847const SCEV *
4848ScalarEvolution::getNoopOrAnyExtend(const SCEV *V, Type *Ty) {
4849 Type *SrcTy = V->getType();
4850 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4851 "Cannot noop or any extend with non-integer arguments!");
4852 assert(getTypeSizeInBits(SrcTy) <= getTypeSizeInBits(Ty) &&
4853 "getNoopOrAnyExtend cannot truncate!");
4854 if (getTypeSizeInBits(Ty: SrcTy) == getTypeSizeInBits(Ty))
4855 return V; // No conversion
4856 return getAnyExtendExpr(Op: V, Ty);
4857}
4858
4859const SCEV *
4860ScalarEvolution::getTruncateOrNoop(const SCEV *V, Type *Ty) {
4861 Type *SrcTy = V->getType();
4862 assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4863 "Cannot truncate or noop with non-integer arguments!");
4864 assert(getTypeSizeInBits(SrcTy) >= getTypeSizeInBits(Ty) &&
4865 "getTruncateOrNoop cannot extend!");
4866 if (getTypeSizeInBits(Ty: SrcTy) == getTypeSizeInBits(Ty))
4867 return V; // No conversion
4868 return getTruncateExpr(Op: V, Ty);
4869}
4870
4871const SCEV *ScalarEvolution::getUMaxFromMismatchedTypes(const SCEV *LHS,
4872 const SCEV *RHS) {
4873 const SCEV *PromotedLHS = LHS;
4874 const SCEV *PromotedRHS = RHS;
4875
4876 if (getTypeSizeInBits(Ty: LHS->getType()) > getTypeSizeInBits(Ty: RHS->getType()))
4877 PromotedRHS = getZeroExtendExpr(Op: RHS, Ty: LHS->getType());
4878 else
4879 PromotedLHS = getNoopOrZeroExtend(V: LHS, Ty: RHS->getType());
4880
4881 return getUMaxExpr(LHS: PromotedLHS, RHS: PromotedRHS);
4882}
4883
4884const SCEV *ScalarEvolution::getUMinFromMismatchedTypes(const SCEV *LHS,
4885 const SCEV *RHS,
4886 bool Sequential) {
4887 SmallVector<SCEVUse, 2> Ops = {LHS, RHS};
4888 return getUMinFromMismatchedTypes(Ops, Sequential);
4889}
4890
4891const SCEV *
4892ScalarEvolution::getUMinFromMismatchedTypes(SmallVectorImpl<SCEVUse> &Ops,
4893 bool Sequential) {
4894 assert(!Ops.empty() && "At least one operand must be!");
4895 // Trivial case.
4896 if (Ops.size() == 1)
4897 return Ops[0];
4898
4899 // Find the max type first.
4900 Type *MaxType = nullptr;
4901 for (SCEVUse S : Ops)
4902 if (MaxType)
4903 MaxType = getWiderType(T1: MaxType, T2: S->getType());
4904 else
4905 MaxType = S->getType();
4906 assert(MaxType && "Failed to find maximum type!");
4907
4908 // Extend all ops to max type.
4909 SmallVector<SCEVUse, 2> PromotedOps;
4910 for (SCEVUse S : Ops)
4911 PromotedOps.push_back(Elt: getNoopOrZeroExtend(V: S, Ty: MaxType));
4912
4913 // Generate umin.
4914 return getUMinExpr(Ops&: PromotedOps, Sequential);
4915}
4916
4917const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
4918 // A pointer operand may evaluate to a nonpointer expression, such as null.
4919 if (!V->getType()->isPointerTy())
4920 return V;
4921
4922 while (true) {
4923 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Val: V)) {
4924 V = AddRec->getStart();
4925 } else if (auto *Add = dyn_cast<SCEVAddExpr>(Val: V)) {
4926 const SCEV *PtrOp = nullptr;
4927 for (const SCEV *AddOp : Add->operands()) {
4928 if (AddOp->getType()->isPointerTy()) {
4929 assert(!PtrOp && "Cannot have multiple pointer ops");
4930 PtrOp = AddOp;
4931 }
4932 }
4933 assert(PtrOp && "Must have pointer op");
4934 V = PtrOp;
4935 } else // Not something we can look further into.
4936 return V;
4937 }
4938}
4939
4940/// Push users of the given Instruction onto the given Worklist.
4941static void PushDefUseChildren(Instruction *I,
4942 SmallVectorImpl<Instruction *> &Worklist,
4943 SmallPtrSetImpl<Instruction *> &Visited) {
4944 // Push the def-use children onto the Worklist stack.
4945 for (User *U : I->users()) {
4946 auto *UserInsn = cast<Instruction>(Val: U);
4947 if (Visited.insert(Ptr: UserInsn).second)
4948 Worklist.push_back(Elt: UserInsn);
4949 }
4950}
4951
4952namespace {
4953
4954/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start
4955/// expression in case its Loop is L. If it is not L then
4956/// if IgnoreOtherLoops is true then use AddRec itself
4957/// otherwise rewrite cannot be done.
4958/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4959class SCEVInitRewriter : public SCEVRewriteVisitor<SCEVInitRewriter> {
4960public:
4961 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
4962 bool IgnoreOtherLoops = true) {
4963 SCEVInitRewriter Rewriter(L, SE);
4964 const SCEV *Result = Rewriter.visit(S);
4965 if (Rewriter.hasSeenLoopVariantSCEVUnknown())
4966 return SE.getCouldNotCompute();
4967 return Rewriter.hasSeenOtherLoops() && !IgnoreOtherLoops
4968 ? SE.getCouldNotCompute()
4969 : Result;
4970 }
4971
4972 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
4973 if (!SE.isLoopInvariant(S: Expr, L))
4974 SeenLoopVariantSCEVUnknown = true;
4975 return Expr;
4976 }
4977
4978 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
4979 // Only re-write AddRecExprs for this loop.
4980 if (Expr->getLoop() == L)
4981 return Expr->getStart();
4982 SeenOtherLoops = true;
4983 return Expr;
4984 }
4985
4986 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
4987
4988 bool hasSeenOtherLoops() { return SeenOtherLoops; }
4989
4990private:
4991 explicit SCEVInitRewriter(const Loop *L, ScalarEvolution &SE)
4992 : SCEVRewriteVisitor(SE), L(L) {}
4993
4994 const Loop *L;
4995 bool SeenLoopVariantSCEVUnknown = false;
4996 bool SeenOtherLoops = false;
4997};
4998
4999/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post
5000/// increment expression in case its Loop is L. If it is not L then
5001/// use AddRec itself.
5002/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
5003class SCEVPostIncRewriter : public SCEVRewriteVisitor<SCEVPostIncRewriter> {
5004public:
5005 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE) {
5006 SCEVPostIncRewriter Rewriter(L, SE);
5007 const SCEV *Result = Rewriter.visit(S);
5008 return Rewriter.hasSeenLoopVariantSCEVUnknown()
5009 ? SE.getCouldNotCompute()
5010 : Result;
5011 }
5012
5013 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
5014 if (!SE.isLoopInvariant(S: Expr, L))
5015 SeenLoopVariantSCEVUnknown = true;
5016 return Expr;
5017 }
5018
5019 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
5020 // Only re-write AddRecExprs for this loop.
5021 if (Expr->getLoop() == L)
5022 return Expr->getPostIncExpr(SE);
5023 SeenOtherLoops = true;
5024 return Expr;
5025 }
5026
5027 bool hasSeenLoopVariantSCEVUnknown() { return SeenLoopVariantSCEVUnknown; }
5028
5029 bool hasSeenOtherLoops() { return SeenOtherLoops; }
5030
5031private:
5032 explicit SCEVPostIncRewriter(const Loop *L, ScalarEvolution &SE)
5033 : SCEVRewriteVisitor(SE), L(L) {}
5034
5035 const Loop *L;
5036 bool SeenLoopVariantSCEVUnknown = false;
5037 bool SeenOtherLoops = false;
5038};
5039
5040/// This class evaluates the compare condition by matching it against the
5041/// condition of loop latch. If there is a match we assume a true value
5042/// for the condition while building SCEV nodes.
5043class SCEVBackedgeConditionFolder
5044 : public SCEVRewriteVisitor<SCEVBackedgeConditionFolder> {
5045public:
5046 static const SCEV *rewrite(const SCEV *S, const Loop *L,
5047 ScalarEvolution &SE) {
5048 bool IsPosBECond = false;
5049 Value *BECond = nullptr;
5050 if (BasicBlock *Latch = L->getLoopLatch()) {
5051 if (CondBrInst *BI = dyn_cast<CondBrInst>(Val: Latch->getTerminator())) {
5052 assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
5053 "Both outgoing branches should not target same header!");
5054 BECond = BI->getCondition();
5055 IsPosBECond = BI->getSuccessor(i: 0) == L->getHeader();
5056 } else {
5057 return S;
5058 }
5059 }
5060 SCEVBackedgeConditionFolder Rewriter(L, BECond, IsPosBECond, SE);
5061 return Rewriter.visit(S);
5062 }
5063
5064 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
5065 const SCEV *Result = Expr;
5066 bool InvariantF = SE.isLoopInvariant(S: Expr, L);
5067
5068 if (!InvariantF) {
5069 Instruction *I = cast<Instruction>(Val: Expr->getValue());
5070 switch (I->getOpcode()) {
5071 case Instruction::Select: {
5072 SelectInst *SI = cast<SelectInst>(Val: I);
5073 std::optional<const SCEV *> Res =
5074 compareWithBackedgeCondition(IC: SI->getCondition());
5075 if (Res) {
5076 bool IsOne = cast<SCEVConstant>(Val: *Res)->getValue()->isOne();
5077 Result = SE.getSCEV(V: IsOne ? SI->getTrueValue() : SI->getFalseValue());
5078 }
5079 break;
5080 }
5081 default: {
5082 std::optional<const SCEV *> Res = compareWithBackedgeCondition(IC: I);
5083 if (Res)
5084 Result = *Res;
5085 break;
5086 }
5087 }
5088 }
5089 return Result;
5090 }
5091
5092private:
5093 explicit SCEVBackedgeConditionFolder(const Loop *L, Value *BECond,
5094 bool IsPosBECond, ScalarEvolution &SE)
5095 : SCEVRewriteVisitor(SE), L(L), BackedgeCond(BECond),
5096 IsPositiveBECond(IsPosBECond) {}
5097
5098 std::optional<const SCEV *> compareWithBackedgeCondition(Value *IC);
5099
5100 const Loop *L;
5101 /// Loop back condition.
5102 Value *BackedgeCond = nullptr;
5103 /// Set to true if loop back is on positive branch condition.
5104 bool IsPositiveBECond;
5105};
5106
5107std::optional<const SCEV *>
5108SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) {
5109
5110 // If value matches the backedge condition for loop latch,
5111 // then return a constant evolution node based on loopback
5112 // branch taken.
5113 if (BackedgeCond == IC)
5114 return IsPositiveBECond ? SE.getOne(Ty: Type::getInt1Ty(C&: SE.getContext()))
5115 : SE.getZero(Ty: Type::getInt1Ty(C&: SE.getContext()));
5116 return std::nullopt;
5117}
5118
5119class SCEVShiftRewriter : public SCEVRewriteVisitor<SCEVShiftRewriter> {
5120public:
5121 static const SCEV *rewrite(const SCEV *S, const Loop *L,
5122 ScalarEvolution &SE) {
5123 SCEVShiftRewriter Rewriter(L, SE);
5124 const SCEV *Result = Rewriter.visit(S);
5125 return Rewriter.isValid() ? Result : SE.getCouldNotCompute();
5126 }
5127
5128 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
5129 // Only allow AddRecExprs for this loop.
5130 if (!SE.isLoopInvariant(S: Expr, L))
5131 Valid = false;
5132 return Expr;
5133 }
5134
5135 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) {
5136 if (Expr->getLoop() == L && Expr->isAffine())
5137 return SE.getMinusSCEV(LHS: Expr, RHS: Expr->getStepRecurrence(SE));
5138 Valid = false;
5139 return Expr;
5140 }
5141
5142 bool isValid() { return Valid; }
5143
5144private:
5145 explicit SCEVShiftRewriter(const Loop *L, ScalarEvolution &SE)
5146 : SCEVRewriteVisitor(SE), L(L) {}
5147
5148 const Loop *L;
5149 bool Valid = true;
5150};
5151
5152} // end anonymous namespace
5153
5154SCEV::NoWrapFlags
5155ScalarEvolution::proveNoWrapViaConstantRanges(const SCEVAddRecExpr *AR) {
5156 if (!AR->isAffine())
5157 return SCEV::FlagAnyWrap;
5158
5159 using OBO = OverflowingBinaryOperator;
5160
5161 SCEV::NoWrapFlags Result = SCEV::FlagAnyWrap;
5162
5163 if (!AR->hasNoSelfWrap()) {
5164 const SCEV *BECount = getConstantMaxBackedgeTakenCount(L: AR->getLoop());
5165 if (const SCEVConstant *BECountMax = dyn_cast<SCEVConstant>(Val: BECount)) {
5166 ConstantRange StepCR = getSignedRange(S: AR->getStepRecurrence(SE&: *this));
5167 const APInt &BECountAP = BECountMax->getAPInt();
5168 unsigned NoOverflowBitWidth =
5169 BECountAP.getActiveBits() + StepCR.getMinSignedBits();
5170 if (NoOverflowBitWidth <= getTypeSizeInBits(Ty: AR->getType()))
5171 Result = ScalarEvolution::setFlags(Flags: Result, OnFlags: SCEV::FlagNW);
5172 }
5173 }
5174
5175 if (!AR->hasNoSignedWrap()) {
5176 ConstantRange AddRecRange = getSignedRange(S: AR);
5177 ConstantRange IncRange = getSignedRange(S: AR->getStepRecurrence(SE&: *this));
5178
5179 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
5180 BinOp: Instruction::Add, Other: IncRange, NoWrapKind: OBO::NoSignedWrap);
5181 if (NSWRegion.contains(CR: AddRecRange))
5182 Result = ScalarEvolution::setFlags(Flags: Result, OnFlags: SCEV::FlagNSW);
5183 }
5184
5185 if (!AR->hasNoUnsignedWrap()) {
5186 ConstantRange AddRecRange = getUnsignedRange(S: AR);
5187 ConstantRange IncRange = getUnsignedRange(S: AR->getStepRecurrence(SE&: *this));
5188
5189 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
5190 BinOp: Instruction::Add, Other: IncRange, NoWrapKind: OBO::NoUnsignedWrap);
5191 if (NUWRegion.contains(CR: AddRecRange))
5192 Result = ScalarEvolution::setFlags(Flags: Result, OnFlags: SCEV::FlagNUW);
5193 }
5194
5195 return Result;
5196}
5197
5198SCEV::NoWrapFlags
5199ScalarEvolution::proveNoSignedWrapViaInduction(const SCEVAddRecExpr *AR) {
5200 SCEV::NoWrapFlags Result = AR->getNoWrapFlags();
5201
5202 if (AR->hasNoSignedWrap())
5203 return Result;
5204
5205 if (!AR->isAffine())
5206 return Result;
5207
5208 // This function can be expensive, only try to prove NSW once per AddRec.
5209 if (!SignedWrapViaInductionTried.insert(Ptr: AR).second)
5210 return Result;
5211
5212 const SCEV *Step = AR->getStepRecurrence(SE&: *this);
5213 const Loop *L = AR->getLoop();
5214
5215 // Check whether the backedge-taken count is SCEVCouldNotCompute.
5216 // Note that this serves two purposes: It filters out loops that are
5217 // simply not analyzable, and it covers the case where this code is
5218 // being called from within backedge-taken count analysis, such that
5219 // attempting to ask for the backedge-taken count would likely result
5220 // in infinite recursion. In the later case, the analysis code will
5221 // cope with a conservative value, and it will take care to purge
5222 // that value once it has finished.
5223 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
5224
5225 // Normally, in the cases we can prove no-overflow via a
5226 // backedge guarding condition, we can also compute a backedge
5227 // taken count for the loop. The exceptions are assumptions and
5228 // guards present in the loop -- SCEV is not great at exploiting
5229 // these to compute max backedge taken counts, but can still use
5230 // these to prove lack of overflow. Use this fact to avoid
5231 // doing extra work that may not pay off.
5232
5233 if (isa<SCEVCouldNotCompute>(Val: MaxBECount) && !HasGuards &&
5234 AC.assumptions().empty())
5235 return Result;
5236
5237 // If the backedge is guarded by a comparison with the pre-inc value the
5238 // addrec is safe. Also, if the entry is guarded by a comparison with the
5239 // start value and the backedge is guarded by a comparison with the post-inc
5240 // value, the addrec is safe.
5241 ICmpInst::Predicate Pred;
5242 const SCEV *OverflowLimit =
5243 getSignedOverflowLimitForStep(Step, Pred: &Pred, SE: this);
5244 if (OverflowLimit &&
5245 (isLoopBackedgeGuardedByCond(L, Pred, LHS: AR, RHS: OverflowLimit) ||
5246 isKnownOnEveryIteration(Pred, LHS: AR, RHS: OverflowLimit))) {
5247 Result = setFlags(Flags: Result, OnFlags: SCEV::FlagNSW);
5248 }
5249 return Result;
5250}
5251SCEV::NoWrapFlags
5252ScalarEvolution::proveNoUnsignedWrapViaInduction(const SCEVAddRecExpr *AR) {
5253 SCEV::NoWrapFlags Result = AR->getNoWrapFlags();
5254
5255 if (AR->hasNoUnsignedWrap())
5256 return Result;
5257
5258 if (!AR->isAffine())
5259 return Result;
5260
5261 // This function can be expensive, only try to prove NUW once per AddRec.
5262 if (!UnsignedWrapViaInductionTried.insert(Ptr: AR).second)
5263 return Result;
5264
5265 const SCEV *Step = AR->getStepRecurrence(SE&: *this);
5266 unsigned BitWidth = getTypeSizeInBits(Ty: AR->getType());
5267 const Loop *L = AR->getLoop();
5268
5269 // Check whether the backedge-taken count is SCEVCouldNotCompute.
5270 // Note that this serves two purposes: It filters out loops that are
5271 // simply not analyzable, and it covers the case where this code is
5272 // being called from within backedge-taken count analysis, such that
5273 // attempting to ask for the backedge-taken count would likely result
5274 // in infinite recursion. In the later case, the analysis code will
5275 // cope with a conservative value, and it will take care to purge
5276 // that value once it has finished.
5277 const SCEV *MaxBECount = getConstantMaxBackedgeTakenCount(L);
5278
5279 // Normally, in the cases we can prove no-overflow via a
5280 // backedge guarding condition, we can also compute a backedge
5281 // taken count for the loop. The exceptions are assumptions and
5282 // guards present in the loop -- SCEV is not great at exploiting
5283 // these to compute max backedge taken counts, but can still use
5284 // these to prove lack of overflow. Use this fact to avoid
5285 // doing extra work that may not pay off.
5286
5287 if (isa<SCEVCouldNotCompute>(Val: MaxBECount) && !HasGuards &&
5288 AC.assumptions().empty())
5289 return Result;
5290
5291 // If the backedge is guarded by a comparison with the pre-inc value the
5292 // addrec is safe. Also, if the entry is guarded by a comparison with the
5293 // start value and the backedge is guarded by a comparison with the post-inc
5294 // value, the addrec is safe.
5295 if (isKnownPositive(S: Step)) {
5296 const SCEV *N = getConstant(Val: APInt::getMinValue(numBits: BitWidth) -
5297 getUnsignedRangeMax(S: Step));
5298 if (isLoopBackedgeGuardedByCond(L, Pred: ICmpInst::ICMP_ULT, LHS: AR, RHS: N) ||
5299 isKnownOnEveryIteration(Pred: ICmpInst::ICMP_ULT, LHS: AR, RHS: N)) {
5300 Result = setFlags(Flags: Result, OnFlags: SCEV::FlagNUW);
5301 }
5302 }
5303
5304 return Result;
5305}
5306
5307namespace {
5308
5309/// Represents an abstract binary operation. This may exist as a
5310/// normal instruction or constant expression, or may have been
5311/// derived from an expression tree.
5312struct BinaryOp {
5313 unsigned Opcode;
5314 Value *LHS;
5315 Value *RHS;
5316 bool IsNSW = false;
5317 bool IsNUW = false;
5318
5319 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
5320 /// constant expression.
5321 Operator *Op = nullptr;
5322
5323 explicit BinaryOp(Operator *Op)
5324 : Opcode(Op->getOpcode()), LHS(Op->getOperand(i: 0)), RHS(Op->getOperand(i: 1)),
5325 Op(Op) {
5326 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Val: Op)) {
5327 IsNSW = OBO->hasNoSignedWrap();
5328 IsNUW = OBO->hasNoUnsignedWrap();
5329 }
5330 }
5331
5332 explicit BinaryOp(unsigned Opcode, Value *LHS, Value *RHS, bool IsNSW = false,
5333 bool IsNUW = false)
5334 : Opcode(Opcode), LHS(LHS), RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
5335};
5336
5337} // end anonymous namespace
5338
5339/// Try to map \p V into a BinaryOp, and return \c std::nullopt on failure.
5340static std::optional<BinaryOp> MatchBinaryOp(Value *V, const DataLayout &DL,
5341 AssumptionCache &AC,
5342 const DominatorTree &DT,
5343 const Instruction *CxtI) {
5344 auto *Op = dyn_cast<Operator>(Val: V);
5345 if (!Op)
5346 return std::nullopt;
5347
5348 // Implementation detail: all the cleverness here should happen without
5349 // creating new SCEV expressions -- our caller knowns tricks to avoid creating
5350 // SCEV expressions when possible, and we should not break that.
5351
5352 switch (Op->getOpcode()) {
5353 case Instruction::Add:
5354 case Instruction::Sub:
5355 case Instruction::Mul:
5356 case Instruction::UDiv:
5357 case Instruction::URem:
5358 case Instruction::And:
5359 case Instruction::AShr:
5360 case Instruction::Shl:
5361 return BinaryOp(Op);
5362
5363 case Instruction::Or: {
5364 // Convert or disjoint into add nuw nsw.
5365 if (cast<PossiblyDisjointInst>(Val: Op)->isDisjoint())
5366 return BinaryOp(Instruction::Add, Op->getOperand(i: 0), Op->getOperand(i: 1),
5367 /*IsNSW=*/true, /*IsNUW=*/true);
5368 return BinaryOp(Op);
5369 }
5370
5371 case Instruction::Xor:
5372 if (auto *RHSC = dyn_cast<ConstantInt>(Val: Op->getOperand(i: 1)))
5373 // If the RHS of the xor is a signmask, then this is just an add.
5374 // Instcombine turns add of signmask into xor as a strength reduction step.
5375 if (RHSC->getValue().isSignMask())
5376 return BinaryOp(Instruction::Add, Op->getOperand(i: 0), Op->getOperand(i: 1));
5377 // Binary `xor` is a bit-wise `add`.
5378 if (V->getType()->isIntegerTy(Bitwidth: 1))
5379 return BinaryOp(Instruction::Add, Op->getOperand(i: 0), Op->getOperand(i: 1));
5380 return BinaryOp(Op);
5381
5382 case Instruction::LShr:
5383 // Turn logical shift right of a constant into a unsigned divide.
5384 if (ConstantInt *SA = dyn_cast<ConstantInt>(Val: Op->getOperand(i: 1))) {
5385 uint32_t BitWidth = cast<IntegerType>(Val: Op->getType())->getBitWidth();
5386
5387 // If the shift count is not less than the bitwidth, the result of
5388 // the shift is undefined. Don't try to analyze it, because the
5389 // resolution chosen here may differ from the resolution chosen in
5390 // other parts of the compiler.
5391 if (SA->getValue().ult(RHS: BitWidth)) {
5392 Constant *X =
5393 ConstantInt::get(Context&: SA->getContext(),
5394 V: APInt::getOneBitSet(numBits: BitWidth, BitNo: SA->getZExtValue()));
5395 return BinaryOp(Instruction::UDiv, Op->getOperand(i: 0), X);
5396 }
5397 }
5398 return BinaryOp(Op);
5399
5400 case Instruction::ExtractValue: {
5401 auto *EVI = cast<ExtractValueInst>(Val: Op);
5402 if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
5403 break;
5404
5405 auto *WO = dyn_cast<WithOverflowInst>(Val: EVI->getAggregateOperand());
5406 if (!WO)
5407 break;
5408
5409 Instruction::BinaryOps BinOp = WO->getBinaryOp();
5410 bool Signed = WO->isSigned();
5411 // TODO: Should add nuw/nsw flags for mul as well.
5412 if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT))
5413 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS());
5414
5415 // Now that we know that all uses of the arithmetic-result component of
5416 // CI are guarded by the overflow check, we can go ahead and pretend
5417 // that the arithmetic is non-overflowing.
5418 return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(),
5419 /* IsNSW = */ Signed, /* IsNUW = */ !Signed);
5420 }
5421
5422 default:
5423 break;
5424 }
5425
5426 // Recognise intrinsic loop.decrement.reg, and as this has exactly the same
5427 // semantics as a Sub, return a binary sub expression.
5428 if (auto *II = dyn_cast<IntrinsicInst>(Val: V))
5429 if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg)
5430 return BinaryOp(Instruction::Sub, II->getOperand(i_nocapture: 0), II->getOperand(i_nocapture: 1));
5431
5432 return std::nullopt;
5433}
5434
5435/// Helper function to createAddRecFromPHIWithCasts. We have a phi
5436/// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
5437/// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the
5438/// way. This function checks if \p Op, an operand of this SCEVAddExpr,
5439/// follows one of the following patterns:
5440/// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
5441/// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
5442/// If the SCEV expression of \p Op conforms with one of the expected patterns
5443/// we return the type of the truncation operation, and indicate whether the
5444/// truncated type should be treated as signed/unsigned by setting
5445/// \p Signed to true/false, respectively.
5446static Type *isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI,
5447 bool &Signed, ScalarEvolution &SE) {
5448 // The case where Op == SymbolicPHI (that is, with no type conversions on
5449 // the way) is handled by the regular add recurrence creating logic and
5450 // would have already been triggered in createAddRecForPHI. Reaching it here
5451 // means that createAddRecFromPHI had failed for this PHI before (e.g.,
5452 // because one of the other operands of the SCEVAddExpr updating this PHI is
5453 // not invariant).
5454 //
5455 // Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in
5456 // this case predicates that allow us to prove that Op == SymbolicPHI will
5457 // be added.
5458 if (Op == SymbolicPHI)
5459 return nullptr;
5460
5461 unsigned SourceBits = SE.getTypeSizeInBits(Ty: SymbolicPHI->getType());
5462 unsigned NewBits = SE.getTypeSizeInBits(Ty: Op->getType());
5463 if (SourceBits != NewBits)
5464 return nullptr;
5465
5466 if (match(S: Op, P: m_scev_SExt(Op0: m_scev_Trunc(Op0: m_scev_Specific(S: SymbolicPHI))))) {
5467 Signed = true;
5468 return cast<SCEVCastExpr>(Val: Op)->getOperand()->getType();
5469 }
5470 if (match(S: Op, P: m_scev_ZExt(Op0: m_scev_Trunc(Op0: m_scev_Specific(S: SymbolicPHI))))) {
5471 Signed = false;
5472 return cast<SCEVCastExpr>(Val: Op)->getOperand()->getType();
5473 }
5474 return nullptr;
5475}
5476
5477static const Loop *isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI) {
5478 if (!PN->getType()->isIntegerTy())
5479 return nullptr;
5480 const Loop *L = LI.getLoopFor(BB: PN->getParent());
5481 if (!L || L->getHeader() != PN->getParent())
5482 return nullptr;
5483 return L;
5484}
5485
5486// Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
5487// computation that updates the phi follows the following pattern:
5488// (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
5489// which correspond to a phi->trunc->sext/zext->add->phi update chain.
5490// If so, try to see if it can be rewritten as an AddRecExpr under some
5491// Predicates. If successful, return them as a pair. Also cache the results
5492// of the analysis.
5493//
5494// Example usage scenario:
5495// Say the Rewriter is called for the following SCEV:
5496// 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
5497// where:
5498// %X = phi i64 (%Start, %BEValue)
5499// It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
5500// and call this function with %SymbolicPHI = %X.
5501//
5502// The analysis will find that the value coming around the backedge has
5503// the following SCEV:
5504// BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
5505// Upon concluding that this matches the desired pattern, the function
5506// will return the pair {NewAddRec, SmallPredsVec} where:
5507// NewAddRec = {%Start,+,%Step}
5508// SmallPredsVec = {P1, P2, P3} as follows:
5509// P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
5510// P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
5511// P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
5512// The returned pair means that SymbolicPHI can be rewritten into NewAddRec
5513// under the predicates {P1,P2,P3}.
5514// This predicated rewrite will be cached in PredicatedSCEVRewrites:
5515// PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)}
5516//
5517// TODO's:
5518//
5519// 1) Extend the Induction descriptor to also support inductions that involve
5520// casts: When needed (namely, when we are called in the context of the
5521// vectorizer induction analysis), a Set of cast instructions will be
5522// populated by this method, and provided back to isInductionPHI. This is
5523// needed to allow the vectorizer to properly record them to be ignored by
5524// the cost model and to avoid vectorizing them (otherwise these casts,
5525// which are redundant under the runtime overflow checks, will be
5526// vectorized, which can be costly).
5527//
5528// 2) Support additional induction/PHISCEV patterns: We also want to support
5529// inductions where the sext-trunc / zext-trunc operations (partly) occur
5530// after the induction update operation (the induction increment):
5531//
5532// (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
5533// which correspond to a phi->add->trunc->sext/zext->phi update chain.
5534//
5535// (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
5536// which correspond to a phi->trunc->add->sext/zext->phi update chain.
5537//
5538// 3) Outline common code with createAddRecFromPHI to avoid duplication.
5539std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5540ScalarEvolution::createAddRecFromPHIWithCastsImpl(const SCEVUnknown *SymbolicPHI) {
5541 SmallVector<const SCEVPredicate *, 3> Predicates;
5542
5543 // *** Part1: Analyze if we have a phi-with-cast pattern for which we can
5544 // return an AddRec expression under some predicate.
5545
5546 auto *PN = cast<PHINode>(Val: SymbolicPHI->getValue());
5547 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
5548 assert(L && "Expecting an integer loop header phi");
5549
5550 // The loop may have multiple entrances or multiple exits; we can analyze
5551 // this phi as an addrec if it has a unique entry value and a unique
5552 // backedge value.
5553 Value *BEValueV = nullptr, *StartValueV = nullptr;
5554 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5555 Value *V = PN->getIncomingValue(i);
5556 if (L->contains(BB: PN->getIncomingBlock(i))) {
5557 if (!BEValueV) {
5558 BEValueV = V;
5559 } else if (BEValueV != V) {
5560 BEValueV = nullptr;
5561 break;
5562 }
5563 } else if (!StartValueV) {
5564 StartValueV = V;
5565 } else if (StartValueV != V) {
5566 StartValueV = nullptr;
5567 break;
5568 }
5569 }
5570 if (!BEValueV || !StartValueV)
5571 return std::nullopt;
5572
5573 const SCEV *BEValue = getSCEV(V: BEValueV);
5574
5575 // If the value coming around the backedge is an add with the symbolic
5576 // value we just inserted, possibly with casts that we can ignore under
5577 // an appropriate runtime guard, then we found a simple induction variable!
5578 const auto *Add = dyn_cast<SCEVAddExpr>(Val: BEValue);
5579 if (!Add)
5580 return std::nullopt;
5581
5582 // If there is a single occurrence of the symbolic value, possibly
5583 // casted, replace it with a recurrence.
5584 unsigned FoundIndex = Add->getNumOperands();
5585 Type *TruncTy = nullptr;
5586 bool Signed;
5587 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5588 if ((TruncTy =
5589 isSimpleCastedPHI(Op: Add->getOperand(i), SymbolicPHI, Signed, SE&: *this)))
5590 if (FoundIndex == e) {
5591 FoundIndex = i;
5592 break;
5593 }
5594
5595 if (FoundIndex == Add->getNumOperands())
5596 return std::nullopt;
5597
5598 // Create an add with everything but the specified operand.
5599 SmallVector<SCEVUse, 8> Ops;
5600 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5601 if (i != FoundIndex)
5602 Ops.push_back(Elt: Add->getOperand(i));
5603 const SCEV *Accum = getAddExpr(Ops);
5604
5605 // The runtime checks will not be valid if the step amount is
5606 // varying inside the loop.
5607 if (!isLoopInvariant(S: Accum, L))
5608 return std::nullopt;
5609
5610 // *** Part2: Create the predicates
5611
5612 // Analysis was successful: we have a phi-with-cast pattern for which we
5613 // can return an AddRec expression under the following predicates:
5614 //
5615 // P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
5616 // fits within the truncated type (does not overflow) for i = 0 to n-1.
5617 // P2: An Equal predicate that guarantees that
5618 // Start = (Ext ix (Trunc iy (Start) to ix) to iy)
5619 // P3: An Equal predicate that guarantees that
5620 // Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
5621 //
5622 // As we next prove, the above predicates guarantee that:
5623 // Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
5624 //
5625 //
5626 // More formally, we want to prove that:
5627 // Expr(i+1) = Start + (i+1) * Accum
5628 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5629 //
5630 // Given that:
5631 // 1) Expr(0) = Start
5632 // 2) Expr(1) = Start + Accum
5633 // = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
5634 // 3) Induction hypothesis (step i):
5635 // Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum
5636 //
5637 // Proof:
5638 // Expr(i+1) =
5639 // = Start + (i+1)*Accum
5640 // = (Start + i*Accum) + Accum
5641 // = Expr(i) + Accum
5642 // = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum
5643 // :: from step i
5644 //
5645 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum
5646 //
5647 // = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
5648 // + (Ext ix (Trunc iy (Accum) to ix) to iy)
5649 // + Accum :: from P3
5650 //
5651 // = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy)
5652 // + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
5653 //
5654 // = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
5655 // = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5656 //
5657 // By induction, the same applies to all iterations 1<=i<n:
5658 //
5659
5660 // Create a truncated addrec for which we will add a no overflow check (P1).
5661 const SCEV *StartVal = getSCEV(V: StartValueV);
5662 const SCEV *PHISCEV =
5663 getAddRecExpr(Start: getTruncateExpr(Op: StartVal, Ty: TruncTy),
5664 Step: getTruncateExpr(Op: Accum, Ty: TruncTy), L, Flags: SCEV::FlagAnyWrap);
5665
5666 // PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
5667 // ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
5668 // will be constant.
5669 //
5670 // If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
5671 // add P1.
5672 if (const auto *AR = dyn_cast<SCEVAddRecExpr>(Val: PHISCEV)) {
5673 SCEVWrapPredicate::IncrementWrapFlags AddedFlags =
5674 Signed ? SCEVWrapPredicate::IncrementNSSW
5675 : SCEVWrapPredicate::IncrementNUSW;
5676 const SCEVPredicate *AddRecPred = getWrapPredicate(AR, AddedFlags);
5677 Predicates.push_back(Elt: AddRecPred);
5678 }
5679
5680 // Create the Equal Predicates P2,P3:
5681
5682 // It is possible that the predicates P2 and/or P3 are computable at
5683 // compile time due to StartVal and/or Accum being constants.
5684 // If either one is, then we can check that now and escape if either P2
5685 // or P3 is false.
5686
5687 // Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
5688 // for each of StartVal and Accum
5689 auto getExtendedExpr = [&](const SCEV *Expr,
5690 bool CreateSignExtend) -> const SCEV * {
5691 assert(isLoopInvariant(Expr, L) && "Expr is expected to be invariant");
5692 const SCEV *TruncatedExpr = getTruncateExpr(Op: Expr, Ty: TruncTy);
5693 const SCEV *ExtendedExpr =
5694 CreateSignExtend ? getSignExtendExpr(Op: TruncatedExpr, Ty: Expr->getType())
5695 : getZeroExtendExpr(Op: TruncatedExpr, Ty: Expr->getType());
5696 return ExtendedExpr;
5697 };
5698
5699 // Given:
5700 // ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
5701 // = getExtendedExpr(Expr)
5702 // Determine whether the predicate P: Expr == ExtendedExpr
5703 // is known to be false at compile time
5704 auto PredIsKnownFalse = [&](const SCEV *Expr,
5705 const SCEV *ExtendedExpr) -> bool {
5706 return Expr != ExtendedExpr &&
5707 isKnownPredicate(Pred: ICmpInst::ICMP_NE, LHS: Expr, RHS: ExtendedExpr);
5708 };
5709
5710 const SCEV *StartExtended = getExtendedExpr(StartVal, Signed);
5711 if (PredIsKnownFalse(StartVal, StartExtended)) {
5712 LLVM_DEBUG(dbgs() << "P2 is compile-time false\n";);
5713 return std::nullopt;
5714 }
5715
5716 // The Step is always Signed (because the overflow checks are either
5717 // NSSW or NUSW)
5718 const SCEV *AccumExtended = getExtendedExpr(Accum, /*CreateSignExtend=*/true);
5719 if (PredIsKnownFalse(Accum, AccumExtended)) {
5720 LLVM_DEBUG(dbgs() << "P3 is compile-time false\n";);
5721 return std::nullopt;
5722 }
5723
5724 auto AppendPredicate = [&](const SCEV *Expr,
5725 const SCEV *ExtendedExpr) -> void {
5726 if (Expr != ExtendedExpr &&
5727 !isKnownPredicate(Pred: ICmpInst::ICMP_EQ, LHS: Expr, RHS: ExtendedExpr)) {
5728 const SCEVPredicate *Pred = getEqualPredicate(LHS: Expr, RHS: ExtendedExpr);
5729 LLVM_DEBUG(dbgs() << "Added Predicate: " << *Pred);
5730 Predicates.push_back(Elt: Pred);
5731 }
5732 };
5733
5734 AppendPredicate(StartVal, StartExtended);
5735 AppendPredicate(Accum, AccumExtended);
5736
5737 // *** Part3: Predicates are ready. Now go ahead and create the new addrec in
5738 // which the casts had been folded away. The caller can rewrite SymbolicPHI
5739 // into NewAR if it will also add the runtime overflow checks specified in
5740 // Predicates.
5741 auto *NewAR = getAddRecExpr(Start: StartVal, Step: Accum, L, Flags: SCEV::FlagAnyWrap);
5742
5743 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
5744 std::make_pair(x&: NewAR, y&: Predicates);
5745 // Remember the result of the analysis for this SCEV at this locayyytion.
5746 PredicatedSCEVRewrites[{SymbolicPHI, L}] = PredRewrite;
5747 return PredRewrite;
5748}
5749
5750std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5751ScalarEvolution::createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI) {
5752 auto *PN = cast<PHINode>(Val: SymbolicPHI->getValue());
5753 const Loop *L = isIntegerLoopHeaderPHI(PN, LI);
5754 if (!L)
5755 return std::nullopt;
5756
5757 // Check to see if we already analyzed this PHI.
5758 auto I = PredicatedSCEVRewrites.find(Val: {SymbolicPHI, L});
5759 if (I != PredicatedSCEVRewrites.end()) {
5760 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
5761 I->second;
5762 // Analysis was done before and failed to create an AddRec:
5763 if (Rewrite.first == SymbolicPHI)
5764 return std::nullopt;
5765 // Analysis was done before and succeeded to create an AddRec under
5766 // a predicate:
5767 assert(isa<SCEVAddRecExpr>(Rewrite.first) && "Expected an AddRec");
5768 assert(!(Rewrite.second).empty() && "Expected to find Predicates");
5769 return Rewrite;
5770 }
5771
5772 std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5773 Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
5774
5775 // Record in the cache that the analysis failed
5776 if (!Rewrite) {
5777 SmallVector<const SCEVPredicate *, 3> Predicates;
5778 PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
5779 return std::nullopt;
5780 }
5781
5782 return Rewrite;
5783}
5784
5785// FIXME: This utility is currently required because the Rewriter currently
5786// does not rewrite this expression:
5787// {0, +, (sext ix (trunc iy to ix) to iy)}
5788// into {0, +, %step},
5789// even when the following Equal predicate exists:
5790// "%step == (sext ix (trunc iy to ix) to iy)".
5791bool PredicatedScalarEvolution::areAddRecsEqualWithPreds(
5792 const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const {
5793 if (AR1 == AR2)
5794 return true;
5795
5796 auto areExprsEqual = [&](const SCEV *Expr1, const SCEV *Expr2) -> bool {
5797 if (Expr1 != Expr2 &&
5798 !Preds->implies(N: SE.getEqualPredicate(LHS: Expr1, RHS: Expr2), SE) &&
5799 !Preds->implies(N: SE.getEqualPredicate(LHS: Expr2, RHS: Expr1), SE))
5800 return false;
5801 return true;
5802 };
5803
5804 if (!areExprsEqual(AR1->getStart(), AR2->getStart()) ||
5805 !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE)))
5806 return false;
5807 return true;
5808}
5809
5810/// A helper function for createAddRecFromPHI to handle simple cases.
5811///
5812/// This function tries to find an AddRec expression for the simplest (yet most
5813/// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
5814/// If it fails, createAddRecFromPHI will use a more general, but slow,
5815/// technique for finding the AddRec expression.
5816const SCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
5817 Value *BEValueV,
5818 Value *StartValueV) {
5819 const Loop *L = LI.getLoopFor(BB: PN->getParent());
5820 assert(L && L->getHeader() == PN->getParent());
5821 assert(BEValueV && StartValueV);
5822
5823 auto BO = MatchBinaryOp(V: BEValueV, DL: getDataLayout(), AC, DT, CxtI: PN);
5824 if (!BO)
5825 return nullptr;
5826
5827 if (BO->Opcode != Instruction::Add)
5828 return nullptr;
5829
5830 const SCEV *Accum = nullptr;
5831 if (BO->LHS == PN && L->isLoopInvariant(V: BO->RHS))
5832 Accum = getSCEV(V: BO->RHS);
5833 else if (BO->RHS == PN && L->isLoopInvariant(V: BO->LHS))
5834 Accum = getSCEV(V: BO->LHS);
5835
5836 if (!Accum)
5837 return nullptr;
5838
5839 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5840 if (BO->IsNUW)
5841 Flags = setFlags(Flags, OnFlags: SCEV::FlagNUW);
5842 if (BO->IsNSW)
5843 Flags = setFlags(Flags, OnFlags: SCEV::FlagNSW);
5844
5845 const SCEV *StartVal = getSCEV(V: StartValueV);
5846 const SCEV *PHISCEV = getAddRecExpr(Start: StartVal, Step: Accum, L, Flags);
5847 insertValueToMap(V: PN, S: PHISCEV);
5848
5849 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Val: PHISCEV)) {
5850 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR),
5851 Flags: (SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
5852 proveNoWrapViaConstantRanges(AR)));
5853 }
5854
5855 // We can add Flags to the post-inc expression only if we
5856 // know that it is *undefined behavior* for BEValueV to
5857 // overflow.
5858 if (auto *BEInst = dyn_cast<Instruction>(Val: BEValueV)) {
5859 assert(isLoopInvariant(Accum, L) &&
5860 "Accum is defined outside L, but is not invariant?");
5861 if (isAddRecNeverPoison(I: BEInst, L))
5862 (void)getAddRecExpr(Start: getAddExpr(LHS: StartVal, RHS: Accum), Step: Accum, L, Flags);
5863 }
5864
5865 return PHISCEV;
5866}
5867
5868const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
5869 const Loop *L = LI.getLoopFor(BB: PN->getParent());
5870 if (!L || L->getHeader() != PN->getParent())
5871 return nullptr;
5872
5873 // The loop may have multiple entrances or multiple exits; we can analyze
5874 // this phi as an addrec if it has a unique entry value and a unique
5875 // backedge value.
5876 Value *BEValueV = nullptr, *StartValueV = nullptr;
5877 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5878 Value *V = PN->getIncomingValue(i);
5879 if (L->contains(BB: PN->getIncomingBlock(i))) {
5880 if (!BEValueV) {
5881 BEValueV = V;
5882 } else if (BEValueV != V) {
5883 BEValueV = nullptr;
5884 break;
5885 }
5886 } else if (!StartValueV) {
5887 StartValueV = V;
5888 } else if (StartValueV != V) {
5889 StartValueV = nullptr;
5890 break;
5891 }
5892 }
5893 if (!BEValueV || !StartValueV)
5894 return nullptr;
5895
5896 assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
5897 "PHI node already processed?");
5898
5899 // First, try to find AddRec expression without creating a fictituos symbolic
5900 // value for PN.
5901 if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
5902 return S;
5903
5904 // Handle PHI node value symbolically.
5905 const SCEV *SymbolicName = getUnknown(V: PN);
5906 insertValueToMap(V: PN, S: SymbolicName);
5907
5908 // Using this symbolic name for the PHI, analyze the value coming around
5909 // the back-edge.
5910 const SCEV *BEValue = getSCEV(V: BEValueV);
5911
5912 // NOTE: If BEValue is loop invariant, we know that the PHI node just
5913 // has a special value for the first iteration of the loop.
5914
5915 // If the value coming around the backedge is an add with the symbolic
5916 // value we just inserted, then we found a simple induction variable!
5917 if (const SCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Val: BEValue)) {
5918 // If there is a single occurrence of the symbolic value, replace it
5919 // with a recurrence.
5920 unsigned FoundIndex = Add->getNumOperands();
5921 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5922 if (Add->getOperand(i) == SymbolicName)
5923 if (FoundIndex == e) {
5924 FoundIndex = i;
5925 break;
5926 }
5927
5928 if (FoundIndex != Add->getNumOperands()) {
5929 // Create an add with everything but the specified operand.
5930 SmallVector<SCEVUse, 8> Ops;
5931 for (unsigned i = 0, e = Add->getNumOperands(); i != e; ++i)
5932 if (i != FoundIndex)
5933 Ops.push_back(Elt: SCEVBackedgeConditionFolder::rewrite(S: Add->getOperand(i),
5934 L, SE&: *this));
5935 const SCEV *Accum = getAddExpr(Ops);
5936
5937 // This is not a valid addrec if the step amount is varying each
5938 // loop iteration, but is not itself an addrec in this loop.
5939 if (isLoopInvariant(S: Accum, L) ||
5940 (isa<SCEVAddRecExpr>(Val: Accum) &&
5941 cast<SCEVAddRecExpr>(Val: Accum)->getLoop() == L)) {
5942 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
5943
5944 if (auto BO = MatchBinaryOp(V: BEValueV, DL: getDataLayout(), AC, DT, CxtI: PN)) {
5945 if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
5946 if (BO->IsNUW)
5947 Flags = setFlags(Flags, OnFlags: SCEV::FlagNUW);
5948 if (BO->IsNSW)
5949 Flags = setFlags(Flags, OnFlags: SCEV::FlagNSW);
5950 }
5951 } else if (GEPOperator *GEP = dyn_cast<GEPOperator>(Val: BEValueV)) {
5952 if (GEP->getOperand(i_nocapture: 0) == PN) {
5953 GEPNoWrapFlags NW = GEP->getNoWrapFlags();
5954 // If the increment has any nowrap flags, then we know the address
5955 // space cannot be wrapped around.
5956 if (NW != GEPNoWrapFlags::none())
5957 Flags = setFlags(Flags, OnFlags: SCEV::FlagNW);
5958 // If the GEP is nuw or nusw with non-negative offset, we know that
5959 // no unsigned wrap occurs. We cannot set the nsw flag as only the
5960 // offset is treated as signed, while the base is unsigned.
5961 if (NW.hasNoUnsignedWrap() ||
5962 (NW.hasNoUnsignedSignedWrap() && isKnownNonNegative(S: Accum)))
5963 Flags = setFlags(Flags, OnFlags: SCEV::FlagNUW);
5964 }
5965
5966 // We cannot transfer nuw and nsw flags from subtraction
5967 // operations -- sub nuw X, Y is not the same as add nuw X, -Y
5968 // for instance.
5969 }
5970
5971 const SCEV *StartVal = getSCEV(V: StartValueV);
5972 const SCEV *PHISCEV = getAddRecExpr(Start: StartVal, Step: Accum, L, Flags);
5973
5974 // Okay, for the entire analysis of this edge we assumed the PHI
5975 // to be symbolic. We now need to go back and purge all of the
5976 // entries for the scalars that use the symbolic expression.
5977 forgetMemoizedResults(SCEVs: {SymbolicName});
5978 insertValueToMap(V: PN, S: PHISCEV);
5979
5980 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Val: PHISCEV)) {
5981 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR),
5982 Flags: (SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
5983 proveNoWrapViaConstantRanges(AR)));
5984 }
5985
5986 // We can add Flags to the post-inc expression only if we
5987 // know that it is *undefined behavior* for BEValueV to
5988 // overflow.
5989 if (auto *BEInst = dyn_cast<Instruction>(Val: BEValueV))
5990 if (isLoopInvariant(S: Accum, L) && isAddRecNeverPoison(I: BEInst, L))
5991 (void)getAddRecExpr(Start: getAddExpr(LHS: StartVal, RHS: Accum), Step: Accum, L, Flags);
5992
5993 return PHISCEV;
5994 }
5995 }
5996 } else {
5997 // Otherwise, this could be a loop like this:
5998 // i = 0; for (j = 1; ..; ++j) { .... i = j; }
5999 // In this case, j = {1,+,1} and BEValue is j.
6000 // Because the other in-value of i (0) fits the evolution of BEValue
6001 // i really is an addrec evolution.
6002 //
6003 // We can generalize this saying that i is the shifted value of BEValue
6004 // by one iteration:
6005 // PHI(f(0), f({1,+,1})) --> f({0,+,1})
6006
6007 // Do not allow refinement in rewriting of BEValue.
6008 const SCEV *Shifted = SCEVShiftRewriter::rewrite(S: BEValue, L, SE&: *this);
6009 const SCEV *Start = SCEVInitRewriter::rewrite(S: Shifted, L, SE&: *this, IgnoreOtherLoops: false);
6010 if (Shifted != getCouldNotCompute() && Start != getCouldNotCompute() &&
6011 isGuaranteedNotToCauseUB(Op: Shifted) && ::impliesPoison(AssumedPoison: Shifted, S: Start)) {
6012 const SCEV *StartVal = getSCEV(V: StartValueV);
6013 if (Start == StartVal) {
6014 // Okay, for the entire analysis of this edge we assumed the PHI
6015 // to be symbolic. We now need to go back and purge all of the
6016 // entries for the scalars that use the symbolic expression.
6017 forgetMemoizedResults(SCEVs: {SymbolicName});
6018 insertValueToMap(V: PN, S: Shifted);
6019 return Shifted;
6020 }
6021 }
6022 }
6023
6024 // Remove the temporary PHI node SCEV that has been inserted while intending
6025 // to create an AddRecExpr for this PHI node. We can not keep this temporary
6026 // as it will prevent later (possibly simpler) SCEV expressions to be added
6027 // to the ValueExprMap.
6028 eraseValueFromMap(V: PN);
6029
6030 return nullptr;
6031}
6032
6033// Try to match a control flow sequence that branches out at BI and merges back
6034// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful
6035// match.
6036static bool BrPHIToSelect(DominatorTree &DT, CondBrInst *BI, PHINode *Merge,
6037 Value *&C, Value *&LHS, Value *&RHS) {
6038 C = BI->getCondition();
6039
6040 BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(i: 0));
6041 BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(i: 1));
6042
6043 Use &LeftUse = Merge->getOperandUse(i: 0);
6044 Use &RightUse = Merge->getOperandUse(i: 1);
6045
6046 if (DT.dominates(BBE: LeftEdge, U: LeftUse) && DT.dominates(BBE: RightEdge, U: RightUse)) {
6047 LHS = LeftUse;
6048 RHS = RightUse;
6049 return true;
6050 }
6051
6052 if (DT.dominates(BBE: LeftEdge, U: RightUse) && DT.dominates(BBE: RightEdge, U: LeftUse)) {
6053 LHS = RightUse;
6054 RHS = LeftUse;
6055 return true;
6056 }
6057
6058 return false;
6059}
6060
6061static bool getOperandsForSelectLikePHI(DominatorTree &DT, PHINode *PN,
6062 Value *&Cond, Value *&LHS,
6063 Value *&RHS) {
6064 auto IsReachable =
6065 [&](BasicBlock *BB) { return DT.isReachableFromEntry(A: BB); };
6066 if (PN->getNumIncomingValues() == 2 && all_of(Range: PN->blocks(), P: IsReachable)) {
6067 // Try to match
6068 //
6069 // br %cond, label %left, label %right
6070 // left:
6071 // br label %merge
6072 // right:
6073 // br label %merge
6074 // merge:
6075 // V = phi [ %x, %left ], [ %y, %right ]
6076 //
6077 // as "select %cond, %x, %y"
6078
6079 BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
6080 assert(IDom && "At least the entry block should dominate PN");
6081
6082 auto *BI = dyn_cast<CondBrInst>(Val: IDom->getTerminator());
6083 return BI && BrPHIToSelect(DT, BI, Merge: PN, C&: Cond, LHS, RHS);
6084 }
6085 return false;
6086}
6087
6088const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
6089 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
6090 if (getOperandsForSelectLikePHI(DT, PN, Cond, LHS, RHS) &&
6091 properlyDominates(S: getSCEV(V: LHS), BB: PN->getParent()) &&
6092 properlyDominates(S: getSCEV(V: RHS), BB: PN->getParent()))
6093 return createNodeForSelectOrPHI(V: PN, Cond, TrueVal: LHS, FalseVal: RHS);
6094
6095 return nullptr;
6096}
6097
6098static BinaryOperator *getCommonInstForPHI(PHINode *PN) {
6099 BinaryOperator *CommonInst = nullptr;
6100 // Check if instructions are identical.
6101 for (Value *Incoming : PN->incoming_values()) {
6102 auto *IncomingInst = dyn_cast<BinaryOperator>(Val: Incoming);
6103 if (!IncomingInst)
6104 return nullptr;
6105 if (CommonInst) {
6106 if (!CommonInst->isIdenticalToWhenDefined(I: IncomingInst))
6107 return nullptr; // Not identical, give up
6108 } else {
6109 // Remember binary operator
6110 CommonInst = IncomingInst;
6111 }
6112 }
6113 return CommonInst;
6114}
6115
6116/// Returns SCEV for the first operand of a phi if all phi operands have
6117/// identical opcodes and operands
6118/// eg.
6119/// a: %add = %a + %b
6120/// br %c
6121/// b: %add1 = %a + %b
6122/// br %c
6123/// c: %phi = phi [%add, a], [%add1, b]
6124/// scev(%phi) => scev(%add)
6125const SCEV *
6126ScalarEvolution::createNodeForPHIWithIdenticalOperands(PHINode *PN) {
6127 BinaryOperator *CommonInst = getCommonInstForPHI(PN);
6128 if (!CommonInst)
6129 return nullptr;
6130
6131 // Check if SCEV exprs for instructions are identical.
6132 const SCEV *CommonSCEV = getSCEV(V: CommonInst);
6133 bool SCEVExprsIdentical =
6134 all_of(Range: drop_begin(RangeOrContainer: PN->incoming_values()),
6135 P: [this, CommonSCEV](Value *V) { return CommonSCEV == getSCEV(V); });
6136 return SCEVExprsIdentical ? CommonSCEV : nullptr;
6137}
6138
6139const SCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
6140 if (const SCEV *S = createAddRecFromPHI(PN))
6141 return S;
6142
6143 // We do not allow simplifying phi (undef, X) to X here, to avoid reusing the
6144 // phi node for X.
6145 if (Value *V = simplifyInstruction(
6146 I: PN, Q: {getDataLayout(), &TLI, &DT, &AC, /*CtxI=*/nullptr,
6147 /*UseInstrInfo=*/true, /*CanUseUndef=*/false}))
6148 return getSCEV(V);
6149
6150 if (const SCEV *S = createNodeForPHIWithIdenticalOperands(PN))
6151 return S;
6152
6153 if (const SCEV *S = createNodeFromSelectLikePHI(PN))
6154 return S;
6155
6156 // If it's not a loop phi, we can't handle it yet.
6157 return getUnknown(V: PN);
6158}
6159
6160bool SCEVMinMaxExprContains(const SCEV *Root, const SCEV *OperandToFind,
6161 SCEVTypes RootKind) {
6162 struct FindClosure {
6163 const SCEV *OperandToFind;
6164 const SCEVTypes RootKind; // Must be a sequential min/max expression.
6165 const SCEVTypes NonSequentialRootKind; // Non-seq variant of RootKind.
6166
6167 bool Found = false;
6168
6169 bool canRecurseInto(SCEVTypes Kind) const {
6170 // We can only recurse into the SCEV expression of the same effective type
6171 // as the type of our root SCEV expression, and into zero-extensions.
6172 return RootKind == Kind || NonSequentialRootKind == Kind ||
6173 scZeroExtend == Kind;
6174 };
6175
6176 FindClosure(const SCEV *OperandToFind, SCEVTypes RootKind)
6177 : OperandToFind(OperandToFind), RootKind(RootKind),
6178 NonSequentialRootKind(
6179 SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(
6180 Ty: RootKind)) {}
6181
6182 bool follow(const SCEV *S) {
6183 Found = S == OperandToFind;
6184
6185 return !isDone() && canRecurseInto(Kind: S->getSCEVType());
6186 }
6187
6188 bool isDone() const { return Found; }
6189 };
6190
6191 FindClosure FC(OperandToFind, RootKind);
6192 visitAll(Root, Visitor&: FC);
6193 return FC.Found;
6194}
6195
6196std::optional<const SCEV *>
6197ScalarEvolution::createNodeForSelectOrPHIInstWithICmpInstCond(Type *Ty,
6198 ICmpInst *Cond,
6199 Value *TrueVal,
6200 Value *FalseVal) {
6201 // Try to match some simple smax or umax patterns.
6202 auto *ICI = Cond;
6203
6204 Value *LHS = ICI->getOperand(i_nocapture: 0);
6205 Value *RHS = ICI->getOperand(i_nocapture: 1);
6206
6207 switch (ICI->getPredicate()) {
6208 case ICmpInst::ICMP_SLT:
6209 case ICmpInst::ICMP_SLE:
6210 case ICmpInst::ICMP_ULT:
6211 case ICmpInst::ICMP_ULE:
6212 std::swap(a&: LHS, b&: RHS);
6213 [[fallthrough]];
6214 case ICmpInst::ICMP_SGT:
6215 case ICmpInst::ICMP_SGE:
6216 case ICmpInst::ICMP_UGT:
6217 case ICmpInst::ICMP_UGE:
6218 // a > b ? a+x : b+x -> max(a, b)+x
6219 // a > b ? b+x : a+x -> min(a, b)+x
6220 if (getTypeSizeInBits(Ty: LHS->getType()) <= getTypeSizeInBits(Ty)) {
6221 bool Signed = ICI->isSigned();
6222 const SCEV *LA = getSCEV(V: TrueVal);
6223 const SCEV *RA = getSCEV(V: FalseVal);
6224 const SCEV *LS = getSCEV(V: LHS);
6225 const SCEV *RS = getSCEV(V: RHS);
6226 if (LA->getType()->isPointerTy()) {
6227 // FIXME: Handle cases where LS/RS are pointers not equal to LA/RA.
6228 // Need to make sure we can't produce weird expressions involving
6229 // negated pointers.
6230 if (LA == LS && RA == RS)
6231 return Signed ? getSMaxExpr(LHS: LS, RHS: RS) : getUMaxExpr(LHS: LS, RHS: RS);
6232 if (LA == RS && RA == LS)
6233 return Signed ? getSMinExpr(LHS: LS, RHS: RS) : getUMinExpr(LHS: LS, RHS: RS);
6234 }
6235 auto CoerceOperand = [&](const SCEV *Op) -> const SCEV * {
6236 if (Op->getType()->isPointerTy()) {
6237 Op = getLosslessPtrToIntExpr(Op);
6238 if (isa<SCEVCouldNotCompute>(Val: Op))
6239 return Op;
6240 }
6241 if (Signed)
6242 Op = getNoopOrSignExtend(V: Op, Ty);
6243 else
6244 Op = getNoopOrZeroExtend(V: Op, Ty);
6245 return Op;
6246 };
6247 LS = CoerceOperand(LS);
6248 RS = CoerceOperand(RS);
6249 if (isa<SCEVCouldNotCompute>(Val: LS) || isa<SCEVCouldNotCompute>(Val: RS))
6250 break;
6251 const SCEV *LDiff = getMinusSCEV(LHS: LA, RHS: LS);
6252 const SCEV *RDiff = getMinusSCEV(LHS: RA, RHS: RS);
6253 if (LDiff == RDiff)
6254 return getAddExpr(LHS: Signed ? getSMaxExpr(LHS: LS, RHS: RS) : getUMaxExpr(LHS: LS, RHS: RS),
6255 RHS: LDiff);
6256 LDiff = getMinusSCEV(LHS: LA, RHS: RS);
6257 RDiff = getMinusSCEV(LHS: RA, RHS: LS);
6258 if (LDiff == RDiff)
6259 return getAddExpr(LHS: Signed ? getSMinExpr(LHS: LS, RHS: RS) : getUMinExpr(LHS: LS, RHS: RS),
6260 RHS: LDiff);
6261 }
6262 break;
6263 case ICmpInst::ICMP_NE:
6264 // x != 0 ? x+y : C+y -> x == 0 ? C+y : x+y
6265 std::swap(a&: TrueVal, b&: FalseVal);
6266 [[fallthrough]];
6267 case ICmpInst::ICMP_EQ:
6268 // x == 0 ? C+y : x+y -> umax(x, C)+y iff C u<= 1
6269 if (getTypeSizeInBits(Ty: LHS->getType()) <= getTypeSizeInBits(Ty) &&
6270 isa<ConstantInt>(Val: RHS) && cast<ConstantInt>(Val: RHS)->isZero()) {
6271 const SCEV *X = getNoopOrZeroExtend(V: getSCEV(V: LHS), Ty);
6272 const SCEV *TrueValExpr = getSCEV(V: TrueVal); // C+y
6273 const SCEV *FalseValExpr = getSCEV(V: FalseVal); // x+y
6274 const SCEV *Y = getMinusSCEV(LHS: FalseValExpr, RHS: X); // y = (x+y)-x
6275 const SCEV *C = getMinusSCEV(LHS: TrueValExpr, RHS: Y); // C = (C+y)-y
6276 if (isa<SCEVConstant>(Val: C) && cast<SCEVConstant>(Val: C)->getAPInt().ule(RHS: 1))
6277 return getAddExpr(LHS: getUMaxExpr(LHS: X, RHS: C), RHS: Y);
6278 }
6279 // x == 0 ? 0 : umin (..., x, ...) -> umin_seq(x, umin (...))
6280 // x == 0 ? 0 : umin_seq(..., x, ...) -> umin_seq(x, umin_seq(...))
6281 // x == 0 ? 0 : umin (..., umin_seq(..., x, ...), ...)
6282 // -> umin_seq(x, umin (..., umin_seq(...), ...))
6283 if (isa<ConstantInt>(Val: RHS) && cast<ConstantInt>(Val: RHS)->isZero() &&
6284 isa<ConstantInt>(Val: TrueVal) && cast<ConstantInt>(Val: TrueVal)->isZero()) {
6285 const SCEV *X = getSCEV(V: LHS);
6286 while (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(Val: X))
6287 X = ZExt->getOperand();
6288 if (getTypeSizeInBits(Ty: X->getType()) <= getTypeSizeInBits(Ty)) {
6289 const SCEV *FalseValExpr = getSCEV(V: FalseVal);
6290 if (SCEVMinMaxExprContains(Root: FalseValExpr, OperandToFind: X, RootKind: scSequentialUMinExpr))
6291 return getUMinExpr(LHS: getNoopOrZeroExtend(V: X, Ty), RHS: FalseValExpr,
6292 /*Sequential=*/true);
6293 }
6294 }
6295 break;
6296 default:
6297 break;
6298 }
6299
6300 return std::nullopt;
6301}
6302
6303static std::optional<const SCEV *>
6304createNodeForSelectViaUMinSeq(ScalarEvolution *SE, const SCEV *CondExpr,
6305 const SCEV *TrueExpr, const SCEV *FalseExpr) {
6306 assert(CondExpr->getType()->isIntegerTy(1) &&
6307 TrueExpr->getType() == FalseExpr->getType() &&
6308 TrueExpr->getType()->isIntegerTy(1) &&
6309 "Unexpected operands of a select.");
6310
6311 // i1 cond ? i1 x : i1 C --> C + (i1 cond ? (i1 x - i1 C) : i1 0)
6312 // --> C + (umin_seq cond, x - C)
6313 //
6314 // i1 cond ? i1 C : i1 x --> C + (i1 cond ? i1 0 : (i1 x - i1 C))
6315 // --> C + (i1 ~cond ? (i1 x - i1 C) : i1 0)
6316 // --> C + (umin_seq ~cond, x - C)
6317
6318 // FIXME: while we can't legally model the case where both of the hands
6319 // are fully variable, we only require that the *difference* is constant.
6320 if (!isa<SCEVConstant>(Val: TrueExpr) && !isa<SCEVConstant>(Val: FalseExpr))
6321 return std::nullopt;
6322
6323 const SCEV *X, *C;
6324 if (isa<SCEVConstant>(Val: TrueExpr)) {
6325 CondExpr = SE->getNotSCEV(V: CondExpr);
6326 X = FalseExpr;
6327 C = TrueExpr;
6328 } else {
6329 X = TrueExpr;
6330 C = FalseExpr;
6331 }
6332 return SE->getAddExpr(LHS: C, RHS: SE->getUMinExpr(LHS: CondExpr, RHS: SE->getMinusSCEV(LHS: X, RHS: C),
6333 /*Sequential=*/true));
6334}
6335
6336static std::optional<const SCEV *>
6337createNodeForSelectViaUMinSeq(ScalarEvolution *SE, Value *Cond, Value *TrueVal,
6338 Value *FalseVal) {
6339 if (!isa<ConstantInt>(Val: TrueVal) && !isa<ConstantInt>(Val: FalseVal))
6340 return std::nullopt;
6341
6342 const auto *SECond = SE->getSCEV(V: Cond);
6343 const auto *SETrue = SE->getSCEV(V: TrueVal);
6344 const auto *SEFalse = SE->getSCEV(V: FalseVal);
6345 return createNodeForSelectViaUMinSeq(SE, CondExpr: SECond, TrueExpr: SETrue, FalseExpr: SEFalse);
6346}
6347
6348const SCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq(
6349 Value *V, Value *Cond, Value *TrueVal, Value *FalseVal) {
6350 assert(Cond->getType()->isIntegerTy(1) && "Select condition is not an i1?");
6351 assert(TrueVal->getType() == FalseVal->getType() &&
6352 V->getType() == TrueVal->getType() &&
6353 "Types of select hands and of the result must match.");
6354
6355 // For now, only deal with i1-typed `select`s.
6356 if (!V->getType()->isIntegerTy(Bitwidth: 1))
6357 return getUnknown(V);
6358
6359 if (std::optional<const SCEV *> S =
6360 createNodeForSelectViaUMinSeq(SE: this, Cond, TrueVal, FalseVal))
6361 return *S;
6362
6363 return getUnknown(V);
6364}
6365
6366const SCEV *ScalarEvolution::createNodeForSelectOrPHI(Value *V, Value *Cond,
6367 Value *TrueVal,
6368 Value *FalseVal) {
6369 // Handle "constant" branch or select. This can occur for instance when a
6370 // loop pass transforms an inner loop and moves on to process the outer loop.
6371 if (auto *CI = dyn_cast<ConstantInt>(Val: Cond))
6372 return getSCEV(V: CI->isOne() ? TrueVal : FalseVal);
6373
6374 if (auto *I = dyn_cast<Instruction>(Val: V)) {
6375 if (auto *ICI = dyn_cast<ICmpInst>(Val: Cond)) {
6376 if (std::optional<const SCEV *> S =
6377 createNodeForSelectOrPHIInstWithICmpInstCond(Ty: I->getType(), Cond: ICI,
6378 TrueVal, FalseVal))
6379 return *S;
6380 }
6381 }
6382
6383 return createNodeForSelectOrPHIViaUMinSeq(V, Cond, TrueVal, FalseVal);
6384}
6385
6386/// Expand GEP instructions into add and multiply operations. This allows them
6387/// to be analyzed by regular SCEV code.
6388const SCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
6389 assert(GEP->getSourceElementType()->isSized() &&
6390 "GEP source element type must be sized");
6391
6392 SmallVector<SCEVUse, 4> IndexExprs;
6393 for (Value *Index : GEP->indices())
6394 IndexExprs.push_back(Elt: getSCEV(V: Index));
6395 return getGEPExpr(GEP, IndexExprs);
6396}
6397
6398APInt ScalarEvolution::getConstantMultipleImpl(const SCEV *S,
6399 const Instruction *CtxI) {
6400 uint64_t BitWidth = getTypeSizeInBits(Ty: S->getType());
6401 auto GetShiftedByZeros = [BitWidth](uint32_t TrailingZeros) {
6402 return TrailingZeros >= BitWidth
6403 ? APInt::getZero(numBits: BitWidth)
6404 : APInt::getOneBitSet(numBits: BitWidth, BitNo: TrailingZeros);
6405 };
6406 auto GetGCDMultiple = [this, CtxI](const SCEVNAryExpr *N) {
6407 // The result is GCD of all operands results.
6408 APInt Res = getConstantMultiple(S: N->getOperand(i: 0), CtxI);
6409 for (unsigned I = 1, E = N->getNumOperands(); I < E && Res != 1; ++I)
6410 Res = APIntOps::GreatestCommonDivisor(
6411 A: Res, B: getConstantMultiple(S: N->getOperand(i: I), CtxI));
6412 return Res;
6413 };
6414
6415 switch (S->getSCEVType()) {
6416 case scConstant:
6417 return cast<SCEVConstant>(Val: S)->getAPInt();
6418 case scPtrToAddr:
6419 case scPtrToInt:
6420 return getConstantMultiple(S: cast<SCEVCastExpr>(Val: S)->getOperand());
6421 case scUDivExpr:
6422 case scVScale:
6423 return APInt(BitWidth, 1);
6424 case scTruncate: {
6425 // Only multiples that are a power of 2 will hold after truncation.
6426 const SCEVTruncateExpr *T = cast<SCEVTruncateExpr>(Val: S);
6427 uint32_t TZ = getMinTrailingZeros(S: T->getOperand(), CtxI);
6428 return GetShiftedByZeros(TZ);
6429 }
6430 case scZeroExtend: {
6431 const SCEVZeroExtendExpr *Z = cast<SCEVZeroExtendExpr>(Val: S);
6432 return getConstantMultiple(S: Z->getOperand(), CtxI).zext(width: BitWidth);
6433 }
6434 case scSignExtend: {
6435 // Only multiples that are a power of 2 will hold after sext.
6436 const SCEVSignExtendExpr *E = cast<SCEVSignExtendExpr>(Val: S);
6437 uint32_t TZ = getMinTrailingZeros(S: E->getOperand(), CtxI);
6438 return GetShiftedByZeros(TZ);
6439 }
6440 case scMulExpr: {
6441 const SCEVMulExpr *M = cast<SCEVMulExpr>(Val: S);
6442 if (M->hasNoUnsignedWrap()) {
6443 // The result is the product of all operand results.
6444 APInt Res = getConstantMultiple(S: M->getOperand(i: 0), CtxI);
6445 for (const SCEV *Operand : M->operands().drop_front())
6446 Res = Res * getConstantMultiple(S: Operand, CtxI);
6447 return Res;
6448 }
6449
6450 // If there are no wrap guarentees, find the trailing zeros, which is the
6451 // sum of trailing zeros for all its operands.
6452 uint32_t TZ = 0;
6453 for (const SCEV *Operand : M->operands())
6454 TZ += getMinTrailingZeros(S: Operand, CtxI);
6455 return GetShiftedByZeros(TZ);
6456 }
6457 case scAddExpr:
6458 case scAddRecExpr: {
6459 const SCEVNAryExpr *N = cast<SCEVNAryExpr>(Val: S);
6460 if (N->hasNoUnsignedWrap())
6461 return GetGCDMultiple(N);
6462 // Find the trailing bits, which is the minimum of its operands.
6463 uint32_t TZ = getMinTrailingZeros(S: N->getOperand(i: 0), CtxI);
6464 for (const SCEV *Operand : N->operands().drop_front())
6465 TZ = std::min(a: TZ, b: getMinTrailingZeros(S: Operand, CtxI));
6466 return GetShiftedByZeros(TZ);
6467 }
6468 case scUMaxExpr:
6469 case scSMaxExpr:
6470 case scUMinExpr:
6471 case scSMinExpr:
6472 case scSequentialUMinExpr:
6473 return GetGCDMultiple(cast<SCEVNAryExpr>(Val: S));
6474 case scUnknown: {
6475 // Ask ValueTracking for known bits. SCEVUnknown only become available at
6476 // the point their underlying IR instruction has been defined. If CtxI was
6477 // not provided, use:
6478 // * the first instruction in the entry block if it is an argument
6479 // * the instruction itself otherwise.
6480 const SCEVUnknown *U = cast<SCEVUnknown>(Val: S);
6481 if (!CtxI) {
6482 if (isa<Argument>(Val: U->getValue()))
6483 CtxI = &*F.getEntryBlock().begin();
6484 else if (auto *I = dyn_cast<Instruction>(Val: U->getValue()))
6485 CtxI = I;
6486 }
6487 unsigned Known =
6488 computeKnownBits(V: U->getValue(), DL: getDataLayout(), AC: &AC, CxtI: CtxI, DT: &DT)
6489 .countMinTrailingZeros();
6490 return GetShiftedByZeros(Known);
6491 }
6492 case scCouldNotCompute:
6493 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6494 }
6495 llvm_unreachable("Unknown SCEV kind!");
6496}
6497
6498APInt ScalarEvolution::getConstantMultiple(const SCEV *S,
6499 const Instruction *CtxI) {
6500 // Skip looking up and updating the cache if there is a context instruction,
6501 // as the result will only be valid in the specified context.
6502 if (CtxI)
6503 return getConstantMultipleImpl(S, CtxI);
6504
6505 auto I = ConstantMultipleCache.find(Val: S);
6506 if (I != ConstantMultipleCache.end())
6507 return I->second;
6508
6509 APInt Result = getConstantMultipleImpl(S, CtxI);
6510 auto InsertPair = ConstantMultipleCache.insert(KV: {S, Result});
6511 assert(InsertPair.second && "Should insert a new key");
6512 return InsertPair.first->second;
6513}
6514
6515APInt ScalarEvolution::getNonZeroConstantMultiple(const SCEV *S) {
6516 APInt Multiple = getConstantMultiple(S);
6517 return Multiple == 0 ? APInt(Multiple.getBitWidth(), 1) : Multiple;
6518}
6519
6520uint32_t ScalarEvolution::getMinTrailingZeros(const SCEV *S,
6521 const Instruction *CtxI) {
6522 return std::min(a: getConstantMultiple(S, CtxI).countTrailingZeros(),
6523 b: (unsigned)getTypeSizeInBits(Ty: S->getType()));
6524}
6525
6526/// Helper method to assign a range to V from metadata present in the IR.
6527static std::optional<ConstantRange> GetRangeFromMetadata(Value *V) {
6528 if (Instruction *I = dyn_cast<Instruction>(Val: V)) {
6529 if (MDNode *MD = I->getMetadata(KindID: LLVMContext::MD_range))
6530 return getConstantRangeFromMetadata(RangeMD: *MD);
6531 if (const auto *CB = dyn_cast<CallBase>(Val: V))
6532 if (std::optional<ConstantRange> Range = CB->getRange())
6533 return Range;
6534 }
6535 if (auto *A = dyn_cast<Argument>(Val: V))
6536 if (std::optional<ConstantRange> Range = A->getRange())
6537 return Range;
6538
6539 return std::nullopt;
6540}
6541
6542void ScalarEvolution::setNoWrapFlags(SCEVAddRecExpr *AddRec,
6543 SCEV::NoWrapFlags Flags) {
6544 if (AddRec->getNoWrapFlags(Mask: Flags) != Flags) {
6545 AddRec->setNoWrapFlags(Flags);
6546 UnsignedRanges.erase(Val: AddRec);
6547 SignedRanges.erase(Val: AddRec);
6548 ConstantMultipleCache.erase(Val: AddRec);
6549 }
6550}
6551
6552ConstantRange ScalarEvolution::
6553getRangeForUnknownRecurrence(const SCEVUnknown *U) {
6554 const DataLayout &DL = getDataLayout();
6555
6556 unsigned BitWidth = getTypeSizeInBits(Ty: U->getType());
6557 const ConstantRange FullSet(BitWidth, /*isFullSet=*/true);
6558
6559 // Match a simple recurrence of the form: <start, ShiftOp, Step>, and then
6560 // use information about the trip count to improve our available range. Note
6561 // that the trip count independent cases are already handled by known bits.
6562 // WARNING: The definition of recurrence used here is subtly different than
6563 // the one used by AddRec (and thus most of this file). Step is allowed to
6564 // be arbitrarily loop varying here, where AddRec allows only loop invariant
6565 // and other addrecs in the same loop (for non-affine addrecs). The code
6566 // below intentionally handles the case where step is not loop invariant.
6567 auto *P = dyn_cast<PHINode>(Val: U->getValue());
6568 if (!P)
6569 return FullSet;
6570
6571 // Make sure that no Phi input comes from an unreachable block. Otherwise,
6572 // even the values that are not available in these blocks may come from them,
6573 // and this leads to false-positive recurrence test.
6574 for (auto *Pred : predecessors(BB: P->getParent()))
6575 if (!DT.isReachableFromEntry(A: Pred))
6576 return FullSet;
6577
6578 BinaryOperator *BO;
6579 Value *Start, *Step;
6580 if (!matchSimpleRecurrence(P, BO, Start, Step))
6581 return FullSet;
6582
6583 // If we found a recurrence in reachable code, we must be in a loop. Note
6584 // that BO might be in some subloop of L, and that's completely okay.
6585 auto *L = LI.getLoopFor(BB: P->getParent());
6586 assert(L && L->getHeader() == P->getParent());
6587 if (!L->contains(BB: BO->getParent()))
6588 // NOTE: This bailout should be an assert instead. However, asserting
6589 // the condition here exposes a case where LoopFusion is querying SCEV
6590 // with malformed loop information during the midst of the transform.
6591 // There doesn't appear to be an obvious fix, so for the moment bailout
6592 // until the caller issue can be fixed. PR49566 tracks the bug.
6593 return FullSet;
6594
6595 // TODO: Extend to other opcodes such as mul, and div
6596 switch (BO->getOpcode()) {
6597 default:
6598 return FullSet;
6599 case Instruction::AShr:
6600 case Instruction::LShr:
6601 case Instruction::Shl:
6602 break;
6603 };
6604
6605 if (BO->getOperand(i_nocapture: 0) != P)
6606 // TODO: Handle the power function forms some day.
6607 return FullSet;
6608
6609 unsigned TC = getSmallConstantMaxTripCount(L);
6610 if (!TC || TC >= BitWidth)
6611 return FullSet;
6612
6613 auto KnownStart = computeKnownBits(V: Start, DL, AC: &AC, CxtI: nullptr, DT: &DT);
6614 auto KnownStep = computeKnownBits(V: Step, DL, AC: &AC, CxtI: nullptr, DT: &DT);
6615 assert(KnownStart.getBitWidth() == BitWidth &&
6616 KnownStep.getBitWidth() == BitWidth);
6617
6618 // Compute total shift amount, being careful of overflow and bitwidths.
6619 auto MaxShiftAmt = KnownStep.getMaxValue();
6620 APInt TCAP(BitWidth, TC-1);
6621 bool Overflow = false;
6622 auto TotalShift = MaxShiftAmt.umul_ov(RHS: TCAP, Overflow);
6623 if (Overflow)
6624 return FullSet;
6625
6626 switch (BO->getOpcode()) {
6627 default:
6628 llvm_unreachable("filtered out above");
6629 case Instruction::AShr: {
6630 // For each ashr, three cases:
6631 // shift = 0 => unchanged value
6632 // saturation => 0 or -1
6633 // other => a value closer to zero (of the same sign)
6634 // Thus, the end value is closer to zero than the start.
6635 auto KnownEnd = KnownBits::ashr(LHS: KnownStart,
6636 RHS: KnownBits::makeConstant(C: TotalShift));
6637 if (KnownStart.isNonNegative())
6638 // Analogous to lshr (simply not yet canonicalized)
6639 return ConstantRange::getNonEmpty(Lower: KnownEnd.getMinValue(),
6640 Upper: KnownStart.getMaxValue() + 1);
6641 if (KnownStart.isNegative())
6642 // End >=u Start && End <=s Start
6643 return ConstantRange::getNonEmpty(Lower: KnownStart.getMinValue(),
6644 Upper: KnownEnd.getMaxValue() + 1);
6645 break;
6646 }
6647 case Instruction::LShr: {
6648 // For each lshr, three cases:
6649 // shift = 0 => unchanged value
6650 // saturation => 0
6651 // other => a smaller positive number
6652 // Thus, the low end of the unsigned range is the last value produced.
6653 auto KnownEnd = KnownBits::lshr(LHS: KnownStart,
6654 RHS: KnownBits::makeConstant(C: TotalShift));
6655 return ConstantRange::getNonEmpty(Lower: KnownEnd.getMinValue(),
6656 Upper: KnownStart.getMaxValue() + 1);
6657 }
6658 case Instruction::Shl: {
6659 // Iff no bits are shifted out, value increases on every shift.
6660 auto KnownEnd = KnownBits::shl(LHS: KnownStart,
6661 RHS: KnownBits::makeConstant(C: TotalShift));
6662 if (TotalShift.ult(RHS: KnownStart.countMinLeadingZeros()))
6663 return ConstantRange(KnownStart.getMinValue(),
6664 KnownEnd.getMaxValue() + 1);
6665 break;
6666 }
6667 };
6668 return FullSet;
6669}
6670
6671// The goal of this function is to check if recursively visiting the operands
6672// of this PHI might lead to an infinite loop. If we do see such a loop,
6673// there's no good way to break it, so we avoid analyzing such cases.
6674//
6675// getRangeRef previously used a visited set to avoid infinite loops, but this
6676// caused other issues: the result was dependent on the order of getRangeRef
6677// calls, and the interaction with createSCEVIter could cause a stack overflow
6678// in some cases (see issue #148253).
6679//
6680// FIXME: The way this is implemented is overly conservative; this checks
6681// for a few obviously safe patterns, but anything that doesn't lead to
6682// recursion is fine.
6683static bool RangeRefPHIAllowedOperands(DominatorTree &DT, PHINode *PHI) {
6684 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
6685 if (getOperandsForSelectLikePHI(DT, PN: PHI, Cond, LHS, RHS))
6686 return true;
6687
6688 if (all_of(Range: PHI->operands(),
6689 P: [&](Value *Operand) { return DT.dominates(Def: Operand, User: PHI); }))
6690 return true;
6691
6692 return false;
6693}
6694
6695const ConstantRange &
6696ScalarEvolution::getRangeRefIter(const SCEV *S,
6697 ScalarEvolution::RangeSignHint SignHint) {
6698 DenseMap<const SCEV *, ConstantRange> &Cache =
6699 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
6700 : SignedRanges;
6701 SmallVector<SCEVUse> WorkList;
6702 SmallPtrSet<const SCEV *, 8> Seen;
6703
6704 // Add Expr to the worklist, if Expr is either an N-ary expression or a
6705 // SCEVUnknown PHI node.
6706 auto AddToWorklist = [&WorkList, &Seen, &Cache](const SCEV *Expr) {
6707 if (!Seen.insert(Ptr: Expr).second)
6708 return;
6709 if (Cache.contains(Val: Expr))
6710 return;
6711 switch (Expr->getSCEVType()) {
6712 case scUnknown:
6713 if (!isa<PHINode>(Val: cast<SCEVUnknown>(Val: Expr)->getValue()))
6714 break;
6715 [[fallthrough]];
6716 case scConstant:
6717 case scVScale:
6718 case scTruncate:
6719 case scZeroExtend:
6720 case scSignExtend:
6721 case scPtrToAddr:
6722 case scPtrToInt:
6723 case scAddExpr:
6724 case scMulExpr:
6725 case scUDivExpr:
6726 case scAddRecExpr:
6727 case scUMaxExpr:
6728 case scSMaxExpr:
6729 case scUMinExpr:
6730 case scSMinExpr:
6731 case scSequentialUMinExpr:
6732 WorkList.push_back(Elt: Expr);
6733 break;
6734 case scCouldNotCompute:
6735 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6736 }
6737 };
6738 AddToWorklist(S);
6739
6740 // Build worklist by queuing operands of N-ary expressions and phi nodes.
6741 for (unsigned I = 0; I != WorkList.size(); ++I) {
6742 const SCEV *P = WorkList[I];
6743 auto *UnknownS = dyn_cast<SCEVUnknown>(Val: P);
6744 // If it is not a `SCEVUnknown`, just recurse into operands.
6745 if (!UnknownS) {
6746 for (const SCEV *Op : P->operands())
6747 AddToWorklist(Op);
6748 continue;
6749 }
6750 // `SCEVUnknown`'s require special treatment.
6751 if (PHINode *P = dyn_cast<PHINode>(Val: UnknownS->getValue())) {
6752 if (!RangeRefPHIAllowedOperands(DT, PHI: P))
6753 continue;
6754 for (auto &Op : reverse(C: P->operands()))
6755 AddToWorklist(getSCEV(V: Op));
6756 }
6757 }
6758
6759 if (!WorkList.empty()) {
6760 // Use getRangeRef to compute ranges for items in the worklist in reverse
6761 // order. This will force ranges for earlier operands to be computed before
6762 // their users in most cases.
6763 for (const SCEV *P : reverse(C: drop_begin(RangeOrContainer&: WorkList))) {
6764 getRangeRef(S: P, Hint: SignHint);
6765 }
6766 }
6767
6768 return getRangeRef(S, Hint: SignHint, Depth: 0);
6769}
6770
6771/// Determine the range for a particular SCEV. If SignHint is
6772/// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
6773/// with a "cleaner" unsigned (resp. signed) representation.
6774const ConstantRange &ScalarEvolution::getRangeRef(
6775 const SCEV *S, ScalarEvolution::RangeSignHint SignHint, unsigned Depth) {
6776 DenseMap<const SCEV *, ConstantRange> &Cache =
6777 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
6778 : SignedRanges;
6779 ConstantRange::PreferredRangeType RangeType =
6780 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? ConstantRange::Unsigned
6781 : ConstantRange::Signed;
6782
6783 // See if we've computed this range already.
6784 DenseMap<const SCEV *, ConstantRange>::iterator I = Cache.find(Val: S);
6785 if (I != Cache.end())
6786 return I->second;
6787
6788 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Val: S))
6789 return setRange(S: C, Hint: SignHint, CR: ConstantRange(C->getAPInt()));
6790
6791 // Switch to iteratively computing the range for S, if it is part of a deeply
6792 // nested expression.
6793 if (Depth > RangeIterThreshold)
6794 return getRangeRefIter(S, SignHint);
6795
6796 unsigned BitWidth = getTypeSizeInBits(Ty: S->getType());
6797 ConstantRange ConservativeResult(BitWidth, /*isFullSet=*/true);
6798 using OBO = OverflowingBinaryOperator;
6799
6800 // If the value has known zeros, the maximum value will have those known zeros
6801 // as well.
6802 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
6803 APInt Multiple = getNonZeroConstantMultiple(S);
6804 APInt Remainder = APInt::getMaxValue(numBits: BitWidth).urem(RHS: Multiple);
6805 if (!Remainder.isZero())
6806 ConservativeResult =
6807 ConstantRange(APInt::getMinValue(numBits: BitWidth),
6808 APInt::getMaxValue(numBits: BitWidth) - Remainder + 1);
6809 }
6810 else {
6811 uint32_t TZ = getMinTrailingZeros(S);
6812 if (TZ != 0) {
6813 ConservativeResult = ConstantRange(
6814 APInt::getSignedMinValue(numBits: BitWidth),
6815 APInt::getSignedMaxValue(numBits: BitWidth).ashr(ShiftAmt: TZ).shl(shiftAmt: TZ) + 1);
6816 }
6817 }
6818
6819 switch (S->getSCEVType()) {
6820 case scConstant:
6821 llvm_unreachable("Already handled above.");
6822 case scVScale:
6823 return setRange(S, Hint: SignHint, CR: getVScaleRange(F: &F, BitWidth));
6824 case scTruncate: {
6825 const SCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(Val: S);
6826 ConstantRange X = getRangeRef(S: Trunc->getOperand(), SignHint, Depth: Depth + 1);
6827 return setRange(
6828 S: Trunc, Hint: SignHint,
6829 CR: ConservativeResult.intersectWith(CR: X.truncate(BitWidth), Type: RangeType));
6830 }
6831 case scZeroExtend: {
6832 const SCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(Val: S);
6833 ConstantRange X = getRangeRef(S: ZExt->getOperand(), SignHint, Depth: Depth + 1);
6834 return setRange(
6835 S: ZExt, Hint: SignHint,
6836 CR: ConservativeResult.intersectWith(CR: X.zeroExtend(BitWidth), Type: RangeType));
6837 }
6838 case scSignExtend: {
6839 const SCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(Val: S);
6840 ConstantRange X = getRangeRef(S: SExt->getOperand(), SignHint, Depth: Depth + 1);
6841 return setRange(
6842 S: SExt, Hint: SignHint,
6843 CR: ConservativeResult.intersectWith(CR: X.signExtend(BitWidth), Type: RangeType));
6844 }
6845 case scPtrToAddr:
6846 case scPtrToInt: {
6847 const SCEVCastExpr *Cast = cast<SCEVCastExpr>(Val: S);
6848 ConstantRange X = getRangeRef(S: Cast->getOperand(), SignHint, Depth: Depth + 1);
6849 return setRange(S: Cast, Hint: SignHint, CR: X);
6850 }
6851 case scAddExpr: {
6852 const SCEVAddExpr *Add = cast<SCEVAddExpr>(Val: S);
6853 // Check if this is a URem pattern: A - (A / B) * B, which is always < B.
6854 const SCEV *URemLHS = nullptr, *URemRHS = nullptr;
6855 if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED &&
6856 match(S, P: m_scev_URem(LHS: m_SCEV(V&: URemLHS), RHS: m_SCEV(V&: URemRHS), SE&: *this))) {
6857 ConstantRange LHSRange = getRangeRef(S: URemLHS, SignHint, Depth: Depth + 1);
6858 ConstantRange RHSRange = getRangeRef(S: URemRHS, SignHint, Depth: Depth + 1);
6859 ConservativeResult =
6860 ConservativeResult.intersectWith(CR: LHSRange.urem(Other: RHSRange), Type: RangeType);
6861 }
6862 ConstantRange X = getRangeRef(S: Add->getOperand(i: 0), SignHint, Depth: Depth + 1);
6863 unsigned WrapType = OBO::AnyWrap;
6864 if (Add->hasNoSignedWrap())
6865 WrapType |= OBO::NoSignedWrap;
6866 if (Add->hasNoUnsignedWrap())
6867 WrapType |= OBO::NoUnsignedWrap;
6868 for (const SCEV *Op : drop_begin(RangeOrContainer: Add->operands()))
6869 X = X.addWithNoWrap(Other: getRangeRef(S: Op, SignHint, Depth: Depth + 1), NoWrapKind: WrapType,
6870 RangeType);
6871 return setRange(S: Add, Hint: SignHint,
6872 CR: ConservativeResult.intersectWith(CR: X, Type: RangeType));
6873 }
6874 case scMulExpr: {
6875 const SCEVMulExpr *Mul = cast<SCEVMulExpr>(Val: S);
6876 ConstantRange X = getRangeRef(S: Mul->getOperand(i: 0), SignHint, Depth: Depth + 1);
6877 for (const SCEV *Op : drop_begin(RangeOrContainer: Mul->operands()))
6878 X = X.multiply(Other: getRangeRef(S: Op, SignHint, Depth: Depth + 1));
6879 return setRange(S: Mul, Hint: SignHint,
6880 CR: ConservativeResult.intersectWith(CR: X, Type: RangeType));
6881 }
6882 case scUDivExpr: {
6883 const SCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(Val: S);
6884 ConstantRange X = getRangeRef(S: UDiv->getLHS(), SignHint, Depth: Depth + 1);
6885 ConstantRange Y = getRangeRef(S: UDiv->getRHS(), SignHint, Depth: Depth + 1);
6886 return setRange(S: UDiv, Hint: SignHint,
6887 CR: ConservativeResult.intersectWith(CR: X.udiv(Other: Y), Type: RangeType));
6888 }
6889 case scAddRecExpr: {
6890 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Val: S);
6891 // If there's no unsigned wrap, the value will never be less than its
6892 // initial value.
6893 if (AddRec->hasNoUnsignedWrap()) {
6894 APInt UnsignedMinValue = getUnsignedRangeMin(S: AddRec->getStart());
6895 if (!UnsignedMinValue.isZero())
6896 ConservativeResult = ConservativeResult.intersectWith(
6897 CR: ConstantRange(UnsignedMinValue, APInt(BitWidth, 0)), Type: RangeType);
6898 }
6899
6900 // If there's no signed wrap, and all the operands except initial value have
6901 // the same sign or zero, the value won't ever be:
6902 // 1: smaller than initial value if operands are non negative,
6903 // 2: bigger than initial value if operands are non positive.
6904 // For both cases, value can not cross signed min/max boundary.
6905 if (AddRec->hasNoSignedWrap()) {
6906 bool AllNonNeg = true;
6907 bool AllNonPos = true;
6908 for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) {
6909 if (!isKnownNonNegative(S: AddRec->getOperand(i)))
6910 AllNonNeg = false;
6911 if (!isKnownNonPositive(S: AddRec->getOperand(i)))
6912 AllNonPos = false;
6913 }
6914 if (AllNonNeg)
6915 ConservativeResult = ConservativeResult.intersectWith(
6916 CR: ConstantRange::getNonEmpty(Lower: getSignedRangeMin(S: AddRec->getStart()),
6917 Upper: APInt::getSignedMinValue(numBits: BitWidth)),
6918 Type: RangeType);
6919 else if (AllNonPos)
6920 ConservativeResult = ConservativeResult.intersectWith(
6921 CR: ConstantRange::getNonEmpty(Lower: APInt::getSignedMinValue(numBits: BitWidth),
6922 Upper: getSignedRangeMax(S: AddRec->getStart()) +
6923 1),
6924 Type: RangeType);
6925 }
6926
6927 // TODO: non-affine addrec
6928 if (AddRec->isAffine()) {
6929 const SCEV *MaxBEScev =
6930 getConstantMaxBackedgeTakenCount(L: AddRec->getLoop());
6931 if (!isa<SCEVCouldNotCompute>(Val: MaxBEScev)) {
6932 APInt MaxBECount = cast<SCEVConstant>(Val: MaxBEScev)->getAPInt();
6933
6934 // Adjust MaxBECount to the same bitwidth as AddRec. We can truncate if
6935 // MaxBECount's active bits are all <= AddRec's bit width.
6936 if (MaxBECount.getBitWidth() > BitWidth &&
6937 MaxBECount.getActiveBits() <= BitWidth)
6938 MaxBECount = MaxBECount.trunc(width: BitWidth);
6939 else if (MaxBECount.getBitWidth() < BitWidth)
6940 MaxBECount = MaxBECount.zext(width: BitWidth);
6941
6942 if (MaxBECount.getBitWidth() == BitWidth) {
6943 auto RangeFromAffine = getRangeForAffineAR(
6944 Start: AddRec->getStart(), Step: AddRec->getStepRecurrence(SE&: *this), MaxBECount);
6945 ConservativeResult =
6946 ConservativeResult.intersectWith(CR: RangeFromAffine, Type: RangeType);
6947
6948 auto RangeFromFactoring = getRangeViaFactoring(
6949 Start: AddRec->getStart(), Step: AddRec->getStepRecurrence(SE&: *this), MaxBECount);
6950 ConservativeResult =
6951 ConservativeResult.intersectWith(CR: RangeFromFactoring, Type: RangeType);
6952 }
6953 }
6954
6955 // Now try symbolic BE count and more powerful methods.
6956 if (UseExpensiveRangeSharpening) {
6957 const SCEV *SymbolicMaxBECount =
6958 getSymbolicMaxBackedgeTakenCount(L: AddRec->getLoop());
6959 if (!isa<SCEVCouldNotCompute>(Val: SymbolicMaxBECount) &&
6960 getTypeSizeInBits(Ty: MaxBEScev->getType()) <= BitWidth &&
6961 AddRec->hasNoSelfWrap()) {
6962 auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR(
6963 AddRec, MaxBECount: SymbolicMaxBECount, BitWidth, SignHint);
6964 ConservativeResult =
6965 ConservativeResult.intersectWith(CR: RangeFromAffineNew, Type: RangeType);
6966 }
6967 }
6968 }
6969
6970 return setRange(S: AddRec, Hint: SignHint, CR: std::move(ConservativeResult));
6971 }
6972 case scUMaxExpr:
6973 case scSMaxExpr:
6974 case scUMinExpr:
6975 case scSMinExpr:
6976 case scSequentialUMinExpr: {
6977 Intrinsic::ID ID;
6978 switch (S->getSCEVType()) {
6979 case scUMaxExpr:
6980 ID = Intrinsic::umax;
6981 break;
6982 case scSMaxExpr:
6983 ID = Intrinsic::smax;
6984 break;
6985 case scUMinExpr:
6986 case scSequentialUMinExpr:
6987 ID = Intrinsic::umin;
6988 break;
6989 case scSMinExpr:
6990 ID = Intrinsic::smin;
6991 break;
6992 default:
6993 llvm_unreachable("Unknown SCEVMinMaxExpr/SCEVSequentialMinMaxExpr.");
6994 }
6995
6996 const auto *NAry = cast<SCEVNAryExpr>(Val: S);
6997 ConstantRange X = getRangeRef(S: NAry->getOperand(i: 0), SignHint, Depth: Depth + 1);
6998 for (unsigned i = 1, e = NAry->getNumOperands(); i != e; ++i)
6999 X = X.intrinsic(
7000 IntrinsicID: ID, Ops: {X, getRangeRef(S: NAry->getOperand(i), SignHint, Depth: Depth + 1)});
7001 return setRange(S, Hint: SignHint,
7002 CR: ConservativeResult.intersectWith(CR: X, Type: RangeType));
7003 }
7004 case scUnknown: {
7005 const SCEVUnknown *U = cast<SCEVUnknown>(Val: S);
7006 Value *V = U->getValue();
7007
7008 // Check if the IR explicitly contains !range metadata.
7009 std::optional<ConstantRange> MDRange = GetRangeFromMetadata(V);
7010 if (MDRange)
7011 ConservativeResult =
7012 ConservativeResult.intersectWith(CR: *MDRange, Type: RangeType);
7013
7014 // Use facts about recurrences in the underlying IR. Note that add
7015 // recurrences are AddRecExprs and thus don't hit this path. This
7016 // primarily handles shift recurrences.
7017 auto CR = getRangeForUnknownRecurrence(U);
7018 ConservativeResult = ConservativeResult.intersectWith(CR);
7019
7020 // See if ValueTracking can give us a useful range.
7021 const DataLayout &DL = getDataLayout();
7022 KnownBits Known = computeKnownBits(V, DL, AC: &AC, CxtI: nullptr, DT: &DT);
7023 if (Known.getBitWidth() != BitWidth)
7024 Known = Known.zextOrTrunc(BitWidth);
7025
7026 // ValueTracking may be able to compute a tighter result for the number of
7027 // sign bits than for the value of those sign bits.
7028 unsigned NS = ComputeNumSignBits(Op: V, DL, AC: &AC, CxtI: nullptr, DT: &DT);
7029 if (U->getType()->isPointerTy()) {
7030 // If the pointer size is larger than the index size type, this can cause
7031 // NS to be larger than BitWidth. So compensate for this.
7032 unsigned ptrSize = DL.getPointerTypeSizeInBits(U->getType());
7033 int ptrIdxDiff = ptrSize - BitWidth;
7034 if (ptrIdxDiff > 0 && ptrSize > BitWidth && NS > (unsigned)ptrIdxDiff)
7035 NS -= ptrIdxDiff;
7036 }
7037
7038 if (NS > 1) {
7039 // If we know any of the sign bits, we know all of the sign bits.
7040 if (!Known.Zero.getHiBits(numBits: NS).isZero())
7041 Known.Zero.setHighBits(NS);
7042 if (!Known.One.getHiBits(numBits: NS).isZero())
7043 Known.One.setHighBits(NS);
7044 }
7045
7046 if (Known.getMinValue() != Known.getMaxValue() + 1)
7047 ConservativeResult = ConservativeResult.intersectWith(
7048 CR: ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1),
7049 Type: RangeType);
7050 if (NS > 1)
7051 ConservativeResult = ConservativeResult.intersectWith(
7052 CR: ConstantRange(APInt::getSignedMinValue(numBits: BitWidth).ashr(ShiftAmt: NS - 1),
7053 APInt::getSignedMaxValue(numBits: BitWidth).ashr(ShiftAmt: NS - 1) + 1),
7054 Type: RangeType);
7055
7056 if (U->getType()->isPointerTy() && SignHint == HINT_RANGE_UNSIGNED) {
7057 // Strengthen the range if the underlying IR value is a
7058 // global/alloca/heap allocation using the size of the object.
7059 bool CanBeNull, CanBeFreed;
7060 uint64_t DerefBytes =
7061 V->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
7062 if (DerefBytes > 1 && isUIntN(N: BitWidth, x: DerefBytes)) {
7063 // The highest address the object can start is DerefBytes bytes before
7064 // the end (unsigned max value). If this value is not a multiple of the
7065 // alignment, the last possible start value is the next lowest multiple
7066 // of the alignment. Note: The computations below cannot overflow,
7067 // because if they would there's no possible start address for the
7068 // object.
7069 APInt MaxVal =
7070 APInt::getMaxValue(numBits: BitWidth) - APInt(BitWidth, DerefBytes);
7071 uint64_t Align = U->getValue()->getPointerAlignment(DL).value();
7072 uint64_t Rem = MaxVal.urem(RHS: Align);
7073 MaxVal -= APInt(BitWidth, Rem);
7074 APInt MinVal = APInt::getZero(numBits: BitWidth);
7075 if (llvm::isKnownNonZero(V, Q: DL))
7076 MinVal = Align;
7077 ConservativeResult = ConservativeResult.intersectWith(
7078 CR: ConstantRange::getNonEmpty(Lower: MinVal, Upper: MaxVal + 1), Type: RangeType);
7079 }
7080 }
7081
7082 // A range of Phi is a subset of union of all ranges of its input.
7083 if (PHINode *Phi = dyn_cast<PHINode>(Val: V)) {
7084 // SCEVExpander sometimes creates SCEVUnknowns that are secretly
7085 // AddRecs; return the range for the corresponding AddRec.
7086 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Val: getSCEV(V)))
7087 return getRangeRef(S: AR, SignHint, Depth: Depth + 1);
7088
7089 // Make sure that we do not run over cycled Phis.
7090 if (RangeRefPHIAllowedOperands(DT, PHI: Phi)) {
7091 ConstantRange RangeFromOps(BitWidth, /*isFullSet=*/false);
7092
7093 for (const auto &Op : Phi->operands()) {
7094 auto OpRange = getRangeRef(S: getSCEV(V: Op), SignHint, Depth: Depth + 1);
7095 RangeFromOps = RangeFromOps.unionWith(CR: OpRange);
7096 // No point to continue if we already have a full set.
7097 if (RangeFromOps.isFullSet())
7098 break;
7099 }
7100 ConservativeResult =
7101 ConservativeResult.intersectWith(CR: RangeFromOps, Type: RangeType);
7102 }
7103 }
7104
7105 // vscale can't be equal to zero
7106 if (const auto *II = dyn_cast<IntrinsicInst>(Val: V))
7107 if (II->getIntrinsicID() == Intrinsic::vscale) {
7108 ConstantRange Disallowed = APInt::getZero(numBits: BitWidth);
7109 ConservativeResult = ConservativeResult.difference(CR: Disallowed);
7110 }
7111
7112 return setRange(S: U, Hint: SignHint, CR: std::move(ConservativeResult));
7113 }
7114 case scCouldNotCompute:
7115 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
7116 }
7117
7118 return setRange(S, Hint: SignHint, CR: std::move(ConservativeResult));
7119}
7120
7121// Given a StartRange, Step and MaxBECount for an expression compute a range of
7122// values that the expression can take. Initially, the expression has a value
7123// from StartRange and then is changed by Step up to MaxBECount times. Signed
7124// argument defines if we treat Step as signed or unsigned.
7125static ConstantRange getRangeForAffineARHelper(APInt Step,
7126 const ConstantRange &StartRange,
7127 const APInt &MaxBECount,
7128 bool Signed) {
7129 unsigned BitWidth = Step.getBitWidth();
7130 assert(BitWidth == StartRange.getBitWidth() &&
7131 BitWidth == MaxBECount.getBitWidth() && "mismatched bit widths");
7132 // If either Step or MaxBECount is 0, then the expression won't change, and we
7133 // just need to return the initial range.
7134 if (Step == 0 || MaxBECount == 0)
7135 return StartRange;
7136
7137 // If we don't know anything about the initial value (i.e. StartRange is
7138 // FullRange), then we don't know anything about the final range either.
7139 // Return FullRange.
7140 if (StartRange.isFullSet())
7141 return ConstantRange::getFull(BitWidth);
7142
7143 // If Step is signed and negative, then we use its absolute value, but we also
7144 // note that we're moving in the opposite direction.
7145 bool Descending = Signed && Step.isNegative();
7146
7147 if (Signed)
7148 // This is correct even for INT_SMIN. Let's look at i8 to illustrate this:
7149 // abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128.
7150 // This equations hold true due to the well-defined wrap-around behavior of
7151 // APInt.
7152 Step = Step.abs();
7153
7154 // Check if Offset is more than full span of BitWidth. If it is, the
7155 // expression is guaranteed to overflow.
7156 if (APInt::getMaxValue(numBits: StartRange.getBitWidth()).udiv(RHS: Step).ult(RHS: MaxBECount))
7157 return ConstantRange::getFull(BitWidth);
7158
7159 // Offset is by how much the expression can change. Checks above guarantee no
7160 // overflow here.
7161 APInt Offset = Step * MaxBECount;
7162
7163 // Minimum value of the final range will match the minimal value of StartRange
7164 // if the expression is increasing and will be decreased by Offset otherwise.
7165 // Maximum value of the final range will match the maximal value of StartRange
7166 // if the expression is decreasing and will be increased by Offset otherwise.
7167 APInt StartLower = StartRange.getLower();
7168 APInt StartUpper = StartRange.getUpper() - 1;
7169 APInt MovedBoundary = Descending ? (StartLower - std::move(Offset))
7170 : (StartUpper + std::move(Offset));
7171
7172 // It's possible that the new minimum/maximum value will fall into the initial
7173 // range (due to wrap around). This means that the expression can take any
7174 // value in this bitwidth, and we have to return full range.
7175 if (StartRange.contains(Val: MovedBoundary))
7176 return ConstantRange::getFull(BitWidth);
7177
7178 APInt NewLower =
7179 Descending ? std::move(MovedBoundary) : std::move(StartLower);
7180 APInt NewUpper =
7181 Descending ? std::move(StartUpper) : std::move(MovedBoundary);
7182 NewUpper += 1;
7183
7184 // No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
7185 return ConstantRange::getNonEmpty(Lower: std::move(NewLower), Upper: std::move(NewUpper));
7186}
7187
7188ConstantRange ScalarEvolution::getRangeForAffineAR(const SCEV *Start,
7189 const SCEV *Step,
7190 const APInt &MaxBECount) {
7191 assert(getTypeSizeInBits(Start->getType()) ==
7192 getTypeSizeInBits(Step->getType()) &&
7193 getTypeSizeInBits(Start->getType()) == MaxBECount.getBitWidth() &&
7194 "mismatched bit widths");
7195
7196 // First, consider step signed.
7197 ConstantRange StartSRange = getSignedRange(S: Start);
7198 ConstantRange StepSRange = getSignedRange(S: Step);
7199
7200 // If Step can be both positive and negative, we need to find ranges for the
7201 // maximum absolute step values in both directions and union them.
7202 ConstantRange SR = getRangeForAffineARHelper(
7203 Step: StepSRange.getSignedMin(), StartRange: StartSRange, MaxBECount, /* Signed = */ true);
7204 SR = SR.unionWith(CR: getRangeForAffineARHelper(Step: StepSRange.getSignedMax(),
7205 StartRange: StartSRange, MaxBECount,
7206 /* Signed = */ true));
7207
7208 // Next, consider step unsigned.
7209 ConstantRange UR = getRangeForAffineARHelper(
7210 Step: getUnsignedRangeMax(S: Step), StartRange: getUnsignedRange(S: Start), MaxBECount,
7211 /* Signed = */ false);
7212
7213 // Finally, intersect signed and unsigned ranges.
7214 return SR.intersectWith(CR: UR, Type: ConstantRange::Smallest);
7215}
7216
7217ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR(
7218 const SCEVAddRecExpr *AddRec, const SCEV *MaxBECount, unsigned BitWidth,
7219 ScalarEvolution::RangeSignHint SignHint) {
7220 assert(AddRec->isAffine() && "Non-affine AddRecs are not suppored!\n");
7221 assert(AddRec->hasNoSelfWrap() &&
7222 "This only works for non-self-wrapping AddRecs!");
7223 const bool IsSigned = SignHint == HINT_RANGE_SIGNED;
7224 const SCEV *Step = AddRec->getStepRecurrence(SE&: *this);
7225 // Only deal with constant step to save compile time.
7226 if (!isa<SCEVConstant>(Val: Step))
7227 return ConstantRange::getFull(BitWidth);
7228 // Let's make sure that we can prove that we do not self-wrap during
7229 // MaxBECount iterations. We need this because MaxBECount is a maximum
7230 // iteration count estimate, and we might infer nw from some exit for which we
7231 // do not know max exit count (or any other side reasoning).
7232 // TODO: Turn into assert at some point.
7233 if (getTypeSizeInBits(Ty: MaxBECount->getType()) >
7234 getTypeSizeInBits(Ty: AddRec->getType()))
7235 return ConstantRange::getFull(BitWidth);
7236 MaxBECount = getNoopOrZeroExtend(V: MaxBECount, Ty: AddRec->getType());
7237 const SCEV *RangeWidth = getMinusOne(Ty: AddRec->getType());
7238 const SCEV *StepAbs = getUMinExpr(LHS: Step, RHS: getNegativeSCEV(V: Step));
7239 const SCEV *MaxItersWithoutWrap = getUDivExpr(LHS: RangeWidth, RHS: StepAbs);
7240 if (!isKnownPredicateViaConstantRanges(Pred: ICmpInst::ICMP_ULE, LHS: MaxBECount,
7241 RHS: MaxItersWithoutWrap))
7242 return ConstantRange::getFull(BitWidth);
7243
7244 ICmpInst::Predicate LEPred =
7245 IsSigned ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
7246 ICmpInst::Predicate GEPred =
7247 IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
7248 const SCEV *End = AddRec->evaluateAtIteration(It: MaxBECount, SE&: *this);
7249
7250 // We know that there is no self-wrap. Let's take Start and End values and
7251 // look at all intermediate values V1, V2, ..., Vn that IndVar takes during
7252 // the iteration. They either lie inside the range [Min(Start, End),
7253 // Max(Start, End)] or outside it:
7254 //
7255 // Case 1: RangeMin ... Start V1 ... VN End ... RangeMax;
7256 // Case 2: RangeMin Vk ... V1 Start ... End Vn ... Vk + 1 RangeMax;
7257 //
7258 // No self wrap flag guarantees that the intermediate values cannot be BOTH
7259 // outside and inside the range [Min(Start, End), Max(Start, End)]. Using that
7260 // knowledge, let's try to prove that we are dealing with Case 1. It is so if
7261 // Start <= End and step is positive, or Start >= End and step is negative.
7262 const SCEV *Start = applyLoopGuards(Expr: AddRec->getStart(), L: AddRec->getLoop());
7263 ConstantRange StartRange = getRangeRef(S: Start, SignHint);
7264 ConstantRange EndRange = getRangeRef(S: End, SignHint);
7265 ConstantRange RangeBetween = StartRange.unionWith(CR: EndRange);
7266 // If they already cover full iteration space, we will know nothing useful
7267 // even if we prove what we want to prove.
7268 if (RangeBetween.isFullSet())
7269 return RangeBetween;
7270 // Only deal with ranges that do not wrap (i.e. RangeMin < RangeMax).
7271 bool IsWrappedSet = IsSigned ? RangeBetween.isSignWrappedSet()
7272 : RangeBetween.isWrappedSet();
7273 if (IsWrappedSet)
7274 return ConstantRange::getFull(BitWidth);
7275
7276 if (isKnownPositive(S: Step) &&
7277 isKnownPredicateViaConstantRanges(Pred: LEPred, LHS: Start, RHS: End))
7278 return RangeBetween;
7279 if (isKnownNegative(S: Step) &&
7280 isKnownPredicateViaConstantRanges(Pred: GEPred, LHS: Start, RHS: End))
7281 return RangeBetween;
7282 return ConstantRange::getFull(BitWidth);
7283}
7284
7285ConstantRange ScalarEvolution::getRangeViaFactoring(const SCEV *Start,
7286 const SCEV *Step,
7287 const APInt &MaxBECount) {
7288 // RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
7289 // == RangeOf({A,+,P}) union RangeOf({B,+,Q})
7290
7291 unsigned BitWidth = MaxBECount.getBitWidth();
7292 assert(getTypeSizeInBits(Start->getType()) == BitWidth &&
7293 getTypeSizeInBits(Step->getType()) == BitWidth &&
7294 "mismatched bit widths");
7295
7296 struct SelectPattern {
7297 Value *Condition = nullptr;
7298 APInt TrueValue;
7299 APInt FalseValue;
7300
7301 explicit SelectPattern(ScalarEvolution &SE, unsigned BitWidth,
7302 const SCEV *S) {
7303 std::optional<unsigned> CastOp;
7304 APInt Offset(BitWidth, 0);
7305
7306 assert(SE.getTypeSizeInBits(S->getType()) == BitWidth &&
7307 "Should be!");
7308
7309 // Peel off a constant offset. In the future we could consider being
7310 // smarter here and handle {Start+Step,+,Step} too.
7311 const APInt *Off;
7312 if (match(S, P: m_scev_Add(Op0: m_scev_APInt(C&: Off), Op1: m_SCEV(V&: S))))
7313 Offset = *Off;
7314
7315 // Peel off a cast operation
7316 if (auto *SCast = dyn_cast<SCEVIntegralCastExpr>(Val: S)) {
7317 CastOp = SCast->getSCEVType();
7318 S = SCast->getOperand();
7319 }
7320
7321 using namespace llvm::PatternMatch;
7322
7323 auto *SU = dyn_cast<SCEVUnknown>(Val: S);
7324 const APInt *TrueVal, *FalseVal;
7325 if (!SU ||
7326 !match(V: SU->getValue(), P: m_Select(C: m_Value(V&: Condition), L: m_APInt(Res&: TrueVal),
7327 R: m_APInt(Res&: FalseVal)))) {
7328 Condition = nullptr;
7329 return;
7330 }
7331
7332 TrueValue = *TrueVal;
7333 FalseValue = *FalseVal;
7334
7335 // Re-apply the cast we peeled off earlier
7336 if (CastOp)
7337 switch (*CastOp) {
7338 default:
7339 llvm_unreachable("Unknown SCEV cast type!");
7340
7341 case scTruncate:
7342 TrueValue = TrueValue.trunc(width: BitWidth);
7343 FalseValue = FalseValue.trunc(width: BitWidth);
7344 break;
7345 case scZeroExtend:
7346 TrueValue = TrueValue.zext(width: BitWidth);
7347 FalseValue = FalseValue.zext(width: BitWidth);
7348 break;
7349 case scSignExtend:
7350 TrueValue = TrueValue.sext(width: BitWidth);
7351 FalseValue = FalseValue.sext(width: BitWidth);
7352 break;
7353 }
7354
7355 // Re-apply the constant offset we peeled off earlier
7356 TrueValue += Offset;
7357 FalseValue += Offset;
7358 }
7359
7360 bool isRecognized() { return Condition != nullptr; }
7361 };
7362
7363 SelectPattern StartPattern(*this, BitWidth, Start);
7364 if (!StartPattern.isRecognized())
7365 return ConstantRange::getFull(BitWidth);
7366
7367 SelectPattern StepPattern(*this, BitWidth, Step);
7368 if (!StepPattern.isRecognized())
7369 return ConstantRange::getFull(BitWidth);
7370
7371 if (StartPattern.Condition != StepPattern.Condition) {
7372 // We don't handle this case today; but we could, by considering four
7373 // possibilities below instead of two. I'm not sure if there are cases where
7374 // that will help over what getRange already does, though.
7375 return ConstantRange::getFull(BitWidth);
7376 }
7377
7378 // NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
7379 // construct arbitrary general SCEV expressions here. This function is called
7380 // from deep in the call stack, and calling getSCEV (on a sext instruction,
7381 // say) can end up caching a suboptimal value.
7382
7383 // FIXME: without the explicit `this` receiver below, MSVC errors out with
7384 // C2352 and C2512 (otherwise it isn't needed).
7385
7386 const SCEV *TrueStart = this->getConstant(Val: StartPattern.TrueValue);
7387 const SCEV *TrueStep = this->getConstant(Val: StepPattern.TrueValue);
7388 const SCEV *FalseStart = this->getConstant(Val: StartPattern.FalseValue);
7389 const SCEV *FalseStep = this->getConstant(Val: StepPattern.FalseValue);
7390
7391 ConstantRange TrueRange =
7392 this->getRangeForAffineAR(Start: TrueStart, Step: TrueStep, MaxBECount);
7393 ConstantRange FalseRange =
7394 this->getRangeForAffineAR(Start: FalseStart, Step: FalseStep, MaxBECount);
7395
7396 return TrueRange.unionWith(CR: FalseRange);
7397}
7398
7399SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(const Value *V) {
7400 if (isa<ConstantExpr>(Val: V)) return SCEV::FlagAnyWrap;
7401 const BinaryOperator *BinOp = cast<BinaryOperator>(Val: V);
7402
7403 // Return early if there are no flags to propagate to the SCEV.
7404 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
7405 if (BinOp->hasNoUnsignedWrap())
7406 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNUW);
7407 if (BinOp->hasNoSignedWrap())
7408 Flags = ScalarEvolution::setFlags(Flags, OnFlags: SCEV::FlagNSW);
7409 if (Flags == SCEV::FlagAnyWrap)
7410 return SCEV::FlagAnyWrap;
7411
7412 return isSCEVExprNeverPoison(I: BinOp) ? Flags : SCEV::FlagAnyWrap;
7413}
7414
7415const Instruction *
7416ScalarEvolution::getNonTrivialDefiningScopeBound(const SCEV *S) {
7417 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Val: S))
7418 return &*AddRec->getLoop()->getHeader()->begin();
7419 if (auto *U = dyn_cast<SCEVUnknown>(Val: S))
7420 if (auto *I = dyn_cast<Instruction>(Val: U->getValue()))
7421 return I;
7422 return nullptr;
7423}
7424
7425const Instruction *ScalarEvolution::getDefiningScopeBound(ArrayRef<SCEVUse> Ops,
7426 bool &Precise) {
7427 Precise = true;
7428 // Do a bounded search of the def relation of the requested SCEVs.
7429 SmallPtrSet<const SCEV *, 16> Visited;
7430 SmallVector<SCEVUse> Worklist;
7431 auto pushOp = [&](const SCEV *S) {
7432 if (!Visited.insert(Ptr: S).second)
7433 return;
7434 // Threshold of 30 here is arbitrary.
7435 if (Visited.size() > 30) {
7436 Precise = false;
7437 return;
7438 }
7439 Worklist.push_back(Elt: S);
7440 };
7441
7442 for (SCEVUse S : Ops)
7443 pushOp(S);
7444
7445 const Instruction *Bound = nullptr;
7446 while (!Worklist.empty()) {
7447 SCEVUse S = Worklist.pop_back_val();
7448 if (auto *DefI = getNonTrivialDefiningScopeBound(S)) {
7449 if (!Bound || DT.dominates(Def: Bound, User: DefI))
7450 Bound = DefI;
7451 } else {
7452 for (SCEVUse Op : S->operands())
7453 pushOp(Op);
7454 }
7455 }
7456 return Bound ? Bound : &*F.getEntryBlock().begin();
7457}
7458
7459const Instruction *
7460ScalarEvolution::getDefiningScopeBound(ArrayRef<SCEVUse> Ops) {
7461 bool Discard;
7462 return getDefiningScopeBound(Ops, Precise&: Discard);
7463}
7464
7465bool ScalarEvolution::isGuaranteedToTransferExecutionTo(const Instruction *A,
7466 const Instruction *B) {
7467 if (A->getParent() == B->getParent() &&
7468 isGuaranteedToTransferExecutionToSuccessor(Begin: A->getIterator(),
7469 End: B->getIterator()))
7470 return true;
7471
7472 auto *BLoop = LI.getLoopFor(BB: B->getParent());
7473 if (BLoop && BLoop->getHeader() == B->getParent() &&
7474 BLoop->getLoopPreheader() == A->getParent() &&
7475 isGuaranteedToTransferExecutionToSuccessor(Begin: A->getIterator(),
7476 End: A->getParent()->end()) &&
7477 isGuaranteedToTransferExecutionToSuccessor(Begin: B->getParent()->begin(),
7478 End: B->getIterator()))
7479 return true;
7480 return false;
7481}
7482
7483bool ScalarEvolution::isGuaranteedNotToBePoison(const SCEV *Op) {
7484 SCEVPoisonCollector PC(/* LookThroughMaybePoisonBlocking */ true);
7485 visitAll(Root: Op, Visitor&: PC);
7486 return PC.MaybePoison.empty();
7487}
7488
7489bool ScalarEvolution::isGuaranteedNotToCauseUB(const SCEV *Op) {
7490 return !SCEVExprContains(Root: Op, Pred: [this](const SCEV *S) {
7491 const SCEV *Op1;
7492 bool M = match(S, P: m_scev_UDiv(Op0: m_SCEV(), Op1: m_SCEV(V&: Op1)));
7493 // The UDiv may be UB if the divisor is poison or zero. Unless the divisor
7494 // is a non-zero constant, we have to assume the UDiv may be UB.
7495 return M && (!isKnownNonZero(S: Op1) || !isGuaranteedNotToBePoison(Op: Op1));
7496 });
7497}
7498
7499bool ScalarEvolution::isSCEVExprNeverPoison(const Instruction *I) {
7500 // Only proceed if we can prove that I does not yield poison.
7501 if (!programUndefinedIfPoison(Inst: I))
7502 return false;
7503
7504 // At this point we know that if I is executed, then it does not wrap
7505 // according to at least one of NSW or NUW. If I is not executed, then we do
7506 // not know if the calculation that I represents would wrap. Multiple
7507 // instructions can map to the same SCEV. If we apply NSW or NUW from I to
7508 // the SCEV, we must guarantee no wrapping for that SCEV also when it is
7509 // derived from other instructions that map to the same SCEV. We cannot make
7510 // that guarantee for cases where I is not executed. So we need to find a
7511 // upper bound on the defining scope for the SCEV, and prove that I is
7512 // executed every time we enter that scope. When the bounding scope is a
7513 // loop (the common case), this is equivalent to proving I executes on every
7514 // iteration of that loop.
7515 SmallVector<SCEVUse> SCEVOps;
7516 for (const Use &Op : I->operands()) {
7517 // I could be an extractvalue from a call to an overflow intrinsic.
7518 // TODO: We can do better here in some cases.
7519 if (isSCEVable(Ty: Op->getType()))
7520 SCEVOps.push_back(Elt: getSCEV(V: Op));
7521 }
7522 auto *DefI = getDefiningScopeBound(Ops: SCEVOps);
7523 return isGuaranteedToTransferExecutionTo(A: DefI, B: I);
7524}
7525
7526bool ScalarEvolution::isAddRecNeverPoison(const Instruction *I, const Loop *L) {
7527 // If we know that \c I can never be poison period, then that's enough.
7528 if (isSCEVExprNeverPoison(I))
7529 return true;
7530
7531 // If the loop only has one exit, then we know that, if the loop is entered,
7532 // any instruction dominating that exit will be executed. If any such
7533 // instruction would result in UB, the addrec cannot be poison.
7534 //
7535 // This is basically the same reasoning as in isSCEVExprNeverPoison(), but
7536 // also handles uses outside the loop header (they just need to dominate the
7537 // single exit).
7538
7539 auto *ExitingBB = L->getExitingBlock();
7540 if (!ExitingBB || !loopHasNoAbnormalExits(L))
7541 return false;
7542
7543 SmallPtrSet<const Value *, 16> KnownPoison;
7544 SmallVector<const Instruction *, 8> Worklist;
7545
7546 // We start by assuming \c I, the post-inc add recurrence, is poison. Only
7547 // things that are known to be poison under that assumption go on the
7548 // Worklist.
7549 KnownPoison.insert(Ptr: I);
7550 Worklist.push_back(Elt: I);
7551
7552 while (!Worklist.empty()) {
7553 const Instruction *Poison = Worklist.pop_back_val();
7554
7555 for (const Use &U : Poison->uses()) {
7556 const Instruction *PoisonUser = cast<Instruction>(Val: U.getUser());
7557 if (mustTriggerUB(I: PoisonUser, KnownPoison) &&
7558 DT.dominates(A: PoisonUser->getParent(), B: ExitingBB))
7559 return true;
7560
7561 if (propagatesPoison(PoisonOp: U) && L->contains(Inst: PoisonUser))
7562 if (KnownPoison.insert(Ptr: PoisonUser).second)
7563 Worklist.push_back(Elt: PoisonUser);
7564 }
7565 }
7566
7567 return false;
7568}
7569
7570ScalarEvolution::LoopProperties
7571ScalarEvolution::getLoopProperties(const Loop *L) {
7572 using LoopProperties = ScalarEvolution::LoopProperties;
7573
7574 auto Itr = LoopPropertiesCache.find(Val: L);
7575 if (Itr == LoopPropertiesCache.end()) {
7576 auto HasSideEffects = [](Instruction *I) {
7577 if (auto *SI = dyn_cast<StoreInst>(Val: I))
7578 return !SI->isSimple();
7579
7580 if (I->mayThrow())
7581 return true;
7582
7583 // Non-volatile memset / memcpy do not count as side-effect for forward
7584 // progress.
7585 if (isa<MemIntrinsic>(Val: I) && !I->isVolatile())
7586 return false;
7587
7588 return I->mayWriteToMemory();
7589 };
7590
7591 LoopProperties LP = {/* HasNoAbnormalExits */ true,
7592 /*HasNoSideEffects*/ true};
7593
7594 for (auto *BB : L->getBlocks())
7595 for (auto &I : *BB) {
7596 if (!isGuaranteedToTransferExecutionToSuccessor(I: &I))
7597 LP.HasNoAbnormalExits = false;
7598 if (HasSideEffects(&I))
7599 LP.HasNoSideEffects = false;
7600 if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects)
7601 break; // We're already as pessimistic as we can get.
7602 }
7603
7604 auto InsertPair = LoopPropertiesCache.insert(KV: {L, LP});
7605 assert(InsertPair.second && "We just checked!");
7606 Itr = InsertPair.first;
7607 }
7608
7609 return Itr->second;
7610}
7611
7612bool ScalarEvolution::loopIsFiniteByAssumption(const Loop *L) {
7613 // A mustprogress loop without side effects must be finite.
7614 // TODO: The check used here is very conservative. It's only *specific*
7615 // side effects which are well defined in infinite loops.
7616 return isFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L));
7617}
7618
7619const SCEV *ScalarEvolution::createSCEVIter(Value *V) {
7620 // Worklist item with a Value and a bool indicating whether all operands have
7621 // been visited already.
7622 using PointerTy = PointerIntPair<Value *, 1, bool>;
7623 SmallVector<PointerTy> Stack;
7624
7625 Stack.emplace_back(Args&: V, Args: true);
7626 Stack.emplace_back(Args&: V, Args: false);
7627 while (!Stack.empty()) {
7628 auto E = Stack.pop_back_val();
7629 Value *CurV = E.getPointer();
7630
7631 if (getExistingSCEV(V: CurV))
7632 continue;
7633
7634 SmallVector<Value *> Ops;
7635 const SCEV *CreatedSCEV = nullptr;
7636 // If all operands have been visited already, create the SCEV.
7637 if (E.getInt()) {
7638 CreatedSCEV = createSCEV(V: CurV);
7639 } else {
7640 // Otherwise get the operands we need to create SCEV's for before creating
7641 // the SCEV for CurV. If the SCEV for CurV can be constructed trivially,
7642 // just use it.
7643 CreatedSCEV = getOperandsToCreate(V: CurV, Ops);
7644 }
7645
7646 if (CreatedSCEV) {
7647 insertValueToMap(V: CurV, S: CreatedSCEV);
7648 } else {
7649 // Queue CurV for SCEV creation, followed by its's operands which need to
7650 // be constructed first.
7651 Stack.emplace_back(Args&: CurV, Args: true);
7652 for (Value *Op : Ops)
7653 Stack.emplace_back(Args&: Op, Args: false);
7654 }
7655 }
7656
7657 return getExistingSCEV(V);
7658}
7659
7660const SCEV *
7661ScalarEvolution::getOperandsToCreate(Value *V, SmallVectorImpl<Value *> &Ops) {
7662 if (!isSCEVable(Ty: V->getType()))
7663 return getUnknown(V);
7664
7665 if (Instruction *I = dyn_cast<Instruction>(Val: V)) {
7666 // Don't attempt to analyze instructions in blocks that aren't
7667 // reachable. Such instructions don't matter, and they aren't required
7668 // to obey basic rules for definitions dominating uses which this
7669 // analysis depends on.
7670 if (!DT.isReachableFromEntry(A: I->getParent()))
7671 return getUnknown(V: PoisonValue::get(T: V->getType()));
7672 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: V))
7673 return getConstant(V: CI);
7674 else if (isa<GlobalAlias>(Val: V))
7675 return getUnknown(V);
7676 else if (!isa<ConstantExpr>(Val: V))
7677 return getUnknown(V);
7678
7679 Operator *U = cast<Operator>(Val: V);
7680 if (auto BO =
7681 MatchBinaryOp(V: U, DL: getDataLayout(), AC, DT, CxtI: dyn_cast<Instruction>(Val: V))) {
7682 bool IsConstArg = isa<ConstantInt>(Val: BO->RHS);
7683 switch (BO->Opcode) {
7684 case Instruction::Add:
7685 case Instruction::Mul: {
7686 // For additions and multiplications, traverse add/mul chains for which we
7687 // can potentially create a single SCEV, to reduce the number of
7688 // get{Add,Mul}Expr calls.
7689 do {
7690 if (BO->Op) {
7691 if (BO->Op != V && getExistingSCEV(V: BO->Op)) {
7692 Ops.push_back(Elt: BO->Op);
7693 break;
7694 }
7695 }
7696 Ops.push_back(Elt: BO->RHS);
7697 auto NewBO = MatchBinaryOp(V: BO->LHS, DL: getDataLayout(), AC, DT,
7698 CxtI: dyn_cast<Instruction>(Val: V));
7699 if (!NewBO ||
7700 (BO->Opcode == Instruction::Add &&
7701 (NewBO->Opcode != Instruction::Add &&
7702 NewBO->Opcode != Instruction::Sub)) ||
7703 (BO->Opcode == Instruction::Mul &&
7704 NewBO->Opcode != Instruction::Mul)) {
7705 Ops.push_back(Elt: BO->LHS);
7706 break;
7707 }
7708 // CreateSCEV calls getNoWrapFlagsFromUB, which under certain conditions
7709 // requires a SCEV for the LHS.
7710 if (BO->Op && (BO->IsNSW || BO->IsNUW)) {
7711 auto *I = dyn_cast<Instruction>(Val: BO->Op);
7712 if (I && programUndefinedIfPoison(Inst: I)) {
7713 Ops.push_back(Elt: BO->LHS);
7714 break;
7715 }
7716 }
7717 BO = NewBO;
7718 } while (true);
7719 return nullptr;
7720 }
7721 case Instruction::Sub:
7722 case Instruction::UDiv:
7723 case Instruction::URem:
7724 break;
7725 case Instruction::AShr:
7726 case Instruction::Shl:
7727 case Instruction::Xor:
7728 if (!IsConstArg)
7729 return nullptr;
7730 break;
7731 case Instruction::And:
7732 case Instruction::Or:
7733 if (!IsConstArg && !BO->LHS->getType()->isIntegerTy(Bitwidth: 1))
7734 return nullptr;
7735 break;
7736 case Instruction::LShr:
7737 return getUnknown(V);
7738 default:
7739 llvm_unreachable("Unhandled binop");
7740 break;
7741 }
7742
7743 Ops.push_back(Elt: BO->LHS);
7744 Ops.push_back(Elt: BO->RHS);
7745 return nullptr;
7746 }
7747
7748 switch (U->getOpcode()) {
7749 case Instruction::Trunc:
7750 case Instruction::ZExt:
7751 case Instruction::SExt:
7752 case Instruction::PtrToAddr:
7753 case Instruction::PtrToInt:
7754 Ops.push_back(Elt: U->getOperand(i: 0));
7755 return nullptr;
7756
7757 case Instruction::BitCast:
7758 if (isSCEVable(Ty: U->getType()) && isSCEVable(Ty: U->getOperand(i: 0)->getType())) {
7759 Ops.push_back(Elt: U->getOperand(i: 0));
7760 return nullptr;
7761 }
7762 return getUnknown(V);
7763
7764 case Instruction::SDiv:
7765 case Instruction::SRem:
7766 Ops.push_back(Elt: U->getOperand(i: 0));
7767 Ops.push_back(Elt: U->getOperand(i: 1));
7768 return nullptr;
7769
7770 case Instruction::GetElementPtr:
7771 assert(cast<GEPOperator>(U)->getSourceElementType()->isSized() &&
7772 "GEP source element type must be sized");
7773 llvm::append_range(C&: Ops, R: U->operands());
7774 return nullptr;
7775
7776 case Instruction::IntToPtr:
7777 return getUnknown(V);
7778
7779 case Instruction::PHI:
7780 // getNodeForPHI has four ways to turn a PHI into a SCEV; retrieve the
7781 // relevant nodes for each of them.
7782 //
7783 // The first is just to call simplifyInstruction, and get something back
7784 // that isn't a PHI.
7785 if (Value *V = simplifyInstruction(
7786 I: cast<PHINode>(Val: U),
7787 Q: {getDataLayout(), &TLI, &DT, &AC, /*CtxI=*/nullptr,
7788 /*UseInstrInfo=*/true, /*CanUseUndef=*/false})) {
7789 assert(V);
7790 Ops.push_back(Elt: V);
7791 return nullptr;
7792 }
7793 // The second is createNodeForPHIWithIdenticalOperands: this looks for
7794 // operands which all perform the same operation, but haven't been
7795 // CSE'ed for whatever reason.
7796 if (BinaryOperator *BO = getCommonInstForPHI(PN: cast<PHINode>(Val: U))) {
7797 assert(BO);
7798 Ops.push_back(Elt: BO);
7799 return nullptr;
7800 }
7801 // The third is createNodeFromSelectLikePHI; this takes a PHI which
7802 // is equivalent to a select, and analyzes it like a select.
7803 {
7804 Value *Cond = nullptr, *LHS = nullptr, *RHS = nullptr;
7805 if (getOperandsForSelectLikePHI(DT, PN: cast<PHINode>(Val: U), Cond, LHS, RHS)) {
7806 assert(Cond);
7807 assert(LHS);
7808 assert(RHS);
7809 if (auto *CondICmp = dyn_cast<ICmpInst>(Val: Cond)) {
7810 Ops.push_back(Elt: CondICmp->getOperand(i_nocapture: 0));
7811 Ops.push_back(Elt: CondICmp->getOperand(i_nocapture: 1));
7812 }
7813 Ops.push_back(Elt: Cond);
7814 Ops.push_back(Elt: LHS);
7815 Ops.push_back(Elt: RHS);
7816 return nullptr;
7817 }
7818 }
7819 // The fourth way is createAddRecFromPHI. It's complicated to handle here,
7820 // so just construct it recursively.
7821 //
7822 // In addition to getNodeForPHI, also construct nodes which might be needed
7823 // by getRangeRef.
7824 if (RangeRefPHIAllowedOperands(DT, PHI: cast<PHINode>(Val: U))) {
7825 for (Value *V : cast<PHINode>(Val: U)->operands())
7826 Ops.push_back(Elt: V);
7827 return nullptr;
7828 }
7829 return nullptr;
7830
7831 case Instruction::Select: {
7832 // Check if U is a select that can be simplified to a SCEVUnknown.
7833 auto CanSimplifyToUnknown = [this, U]() {
7834 if (U->getType()->isIntegerTy(Bitwidth: 1) || isa<ConstantInt>(Val: U->getOperand(i: 0)))
7835 return false;
7836
7837 auto *ICI = dyn_cast<ICmpInst>(Val: U->getOperand(i: 0));
7838 if (!ICI)
7839 return false;
7840 Value *LHS = ICI->getOperand(i_nocapture: 0);
7841 Value *RHS = ICI->getOperand(i_nocapture: 1);
7842 if (ICI->getPredicate() == CmpInst::ICMP_EQ ||
7843 ICI->getPredicate() == CmpInst::ICMP_NE) {
7844 if (!(isa<ConstantInt>(Val: RHS) && cast<ConstantInt>(Val: RHS)->isZero()))
7845 return true;
7846 } else if (getTypeSizeInBits(Ty: LHS->getType()) >
7847 getTypeSizeInBits(Ty: U->getType()))
7848 return true;
7849 return false;
7850 };
7851 if (CanSimplifyToUnknown())
7852 return getUnknown(V: U);
7853
7854 llvm::append_range(C&: Ops, R: U->operands());
7855 return nullptr;
7856 break;
7857 }
7858 case Instruction::Call:
7859 case Instruction::Invoke:
7860 if (Value *RV = cast<CallBase>(Val: U)->getReturnedArgOperand()) {
7861 Ops.push_back(Elt: RV);
7862 return nullptr;
7863 }
7864
7865 if (auto *II = dyn_cast<IntrinsicInst>(Val: U)) {
7866 switch (II->getIntrinsicID()) {
7867 case Intrinsic::abs:
7868 Ops.push_back(Elt: II->getArgOperand(i: 0));
7869 return nullptr;
7870 case Intrinsic::umax:
7871 case Intrinsic::umin:
7872 case Intrinsic::smax:
7873 case Intrinsic::smin:
7874 case Intrinsic::usub_sat:
7875 case Intrinsic::uadd_sat:
7876 Ops.push_back(Elt: II->getArgOperand(i: 0));
7877 Ops.push_back(Elt: II->getArgOperand(i: 1));
7878 return nullptr;
7879 case Intrinsic::start_loop_iterations:
7880 case Intrinsic::annotation:
7881 case Intrinsic::ptr_annotation:
7882 Ops.push_back(Elt: II->getArgOperand(i: 0));
7883 return nullptr;
7884 default:
7885 break;
7886 }
7887 }
7888 break;
7889 }
7890
7891 return nullptr;
7892}
7893
7894const SCEV *ScalarEvolution::createSCEV(Value *V) {
7895 if (!isSCEVable(Ty: V->getType()))
7896 return getUnknown(V);
7897
7898 if (Instruction *I = dyn_cast<Instruction>(Val: V)) {
7899 // Don't attempt to analyze instructions in blocks that aren't
7900 // reachable. Such instructions don't matter, and they aren't required
7901 // to obey basic rules for definitions dominating uses which this
7902 // analysis depends on.
7903 if (!DT.isReachableFromEntry(A: I->getParent()))
7904 return getUnknown(V: PoisonValue::get(T: V->getType()));
7905 } else if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: V))
7906 return getConstant(V: CI);
7907 else if (isa<GlobalAlias>(Val: V))
7908 return getUnknown(V);
7909 else if (!isa<ConstantExpr>(Val: V))
7910 return getUnknown(V);
7911
7912 const SCEV *LHS;
7913 const SCEV *RHS;
7914
7915 Operator *U = cast<Operator>(Val: V);
7916 if (auto BO =
7917 MatchBinaryOp(V: U, DL: getDataLayout(), AC, DT, CxtI: dyn_cast<Instruction>(Val: V))) {
7918 switch (BO->Opcode) {
7919 case Instruction::Add: {
7920 // The simple thing to do would be to just call getSCEV on both operands
7921 // and call getAddExpr with the result. However if we're looking at a
7922 // bunch of things all added together, this can be quite inefficient,
7923 // because it leads to N-1 getAddExpr calls for N ultimate operands.
7924 // Instead, gather up all the operands and make a single getAddExpr call.
7925 // LLVM IR canonical form means we need only traverse the left operands.
7926 SmallVector<SCEVUse, 4> AddOps;
7927 do {
7928 if (BO->Op) {
7929 if (auto *OpSCEV = getExistingSCEV(V: BO->Op)) {
7930 AddOps.push_back(Elt: OpSCEV);
7931 break;
7932 }
7933
7934 // If a NUW or NSW flag can be applied to the SCEV for this
7935 // addition, then compute the SCEV for this addition by itself
7936 // with a separate call to getAddExpr. We need to do that
7937 // instead of pushing the operands of the addition onto AddOps,
7938 // since the flags are only known to apply to this particular
7939 // addition - they may not apply to other additions that can be
7940 // formed with operands from AddOps.
7941 const SCEV *RHS = getSCEV(V: BO->RHS);
7942 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(V: BO->Op);
7943 if (Flags != SCEV::FlagAnyWrap) {
7944 const SCEV *LHS = getSCEV(V: BO->LHS);
7945 if (BO->Opcode == Instruction::Sub)
7946 AddOps.push_back(Elt: getMinusSCEV(LHS, RHS, Flags));
7947 else
7948 AddOps.push_back(Elt: getAddExpr(LHS, RHS, Flags));
7949 break;
7950 }
7951 }
7952
7953 if (BO->Opcode == Instruction::Sub)
7954 AddOps.push_back(Elt: getNegativeSCEV(V: getSCEV(V: BO->RHS)));
7955 else
7956 AddOps.push_back(Elt: getSCEV(V: BO->RHS));
7957
7958 auto NewBO = MatchBinaryOp(V: BO->LHS, DL: getDataLayout(), AC, DT,
7959 CxtI: dyn_cast<Instruction>(Val: V));
7960 if (!NewBO || (NewBO->Opcode != Instruction::Add &&
7961 NewBO->Opcode != Instruction::Sub)) {
7962 AddOps.push_back(Elt: getSCEV(V: BO->LHS));
7963 break;
7964 }
7965 BO = NewBO;
7966 } while (true);
7967
7968 return getAddExpr(Ops&: AddOps);
7969 }
7970
7971 case Instruction::Mul: {
7972 SmallVector<SCEVUse, 4> MulOps;
7973 do {
7974 if (BO->Op) {
7975 if (auto *OpSCEV = getExistingSCEV(V: BO->Op)) {
7976 MulOps.push_back(Elt: OpSCEV);
7977 break;
7978 }
7979
7980 SCEV::NoWrapFlags Flags = getNoWrapFlagsFromUB(V: BO->Op);
7981 if (Flags != SCEV::FlagAnyWrap) {
7982 LHS = getSCEV(V: BO->LHS);
7983 RHS = getSCEV(V: BO->RHS);
7984 MulOps.push_back(Elt: getMulExpr(LHS, RHS, Flags));
7985 break;
7986 }
7987 }
7988
7989 MulOps.push_back(Elt: getSCEV(V: BO->RHS));
7990 auto NewBO = MatchBinaryOp(V: BO->LHS, DL: getDataLayout(), AC, DT,
7991 CxtI: dyn_cast<Instruction>(Val: V));
7992 if (!NewBO || NewBO->Opcode != Instruction::Mul) {
7993 MulOps.push_back(Elt: getSCEV(V: BO->LHS));
7994 break;
7995 }
7996 BO = NewBO;
7997 } while (true);
7998
7999 return getMulExpr(Ops&: MulOps);
8000 }
8001 case Instruction::UDiv:
8002 LHS = getSCEV(V: BO->LHS);
8003 RHS = getSCEV(V: BO->RHS);
8004 return getUDivExpr(LHS, RHS);
8005 case Instruction::URem:
8006 LHS = getSCEV(V: BO->LHS);
8007 RHS = getSCEV(V: BO->RHS);
8008 return getURemExpr(LHS, RHS);
8009 case Instruction::Sub: {
8010 SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap;
8011 if (BO->Op)
8012 Flags = getNoWrapFlagsFromUB(V: BO->Op);
8013 LHS = getSCEV(V: BO->LHS);
8014 RHS = getSCEV(V: BO->RHS);
8015 return getMinusSCEV(LHS, RHS, Flags);
8016 }
8017 case Instruction::And:
8018 // For an expression like x&255 that merely masks off the high bits,
8019 // use zext(trunc(x)) as the SCEV expression.
8020 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: BO->RHS)) {
8021 if (CI->isZero())
8022 return getSCEV(V: BO->RHS);
8023 if (CI->isMinusOne())
8024 return getSCEV(V: BO->LHS);
8025 const APInt &A = CI->getValue();
8026
8027 // Instcombine's ShrinkDemandedConstant may strip bits out of
8028 // constants, obscuring what would otherwise be a low-bits mask.
8029 // Use computeKnownBits to compute what ShrinkDemandedConstant
8030 // knew about to reconstruct a low-bits mask value.
8031 unsigned LZ = A.countl_zero();
8032 unsigned TZ = A.countr_zero();
8033 unsigned BitWidth = A.getBitWidth();
8034 KnownBits Known(BitWidth);
8035 computeKnownBits(V: BO->LHS, Known, DL: getDataLayout(), AC: &AC, CxtI: nullptr, DT: &DT);
8036
8037 APInt EffectiveMask =
8038 APInt::getLowBitsSet(numBits: BitWidth, loBitsSet: BitWidth - LZ - TZ).shl(shiftAmt: TZ);
8039 if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) {
8040 const SCEV *MulCount = getConstant(Val: APInt::getOneBitSet(numBits: BitWidth, BitNo: TZ));
8041 const SCEV *LHS = getSCEV(V: BO->LHS);
8042 const SCEV *ShiftedLHS = nullptr;
8043 if (auto *LHSMul = dyn_cast<SCEVMulExpr>(Val: LHS)) {
8044 if (auto *OpC = dyn_cast<SCEVConstant>(Val: LHSMul->getOperand(i: 0))) {
8045 // For an expression like (x * 8) & 8, simplify the multiply.
8046 unsigned MulZeros = OpC->getAPInt().countr_zero();
8047 unsigned GCD = std::min(a: MulZeros, b: TZ);
8048 APInt DivAmt = APInt::getOneBitSet(numBits: BitWidth, BitNo: TZ - GCD);
8049 SmallVector<SCEVUse, 4> MulOps;
8050 MulOps.push_back(Elt: getConstant(Val: OpC->getAPInt().ashr(ShiftAmt: GCD)));
8051 append_range(C&: MulOps, R: LHSMul->operands().drop_front());
8052 auto *NewMul = getMulExpr(Ops&: MulOps, OrigFlags: LHSMul->getNoWrapFlags());
8053 ShiftedLHS = getUDivExpr(LHS: NewMul, RHS: getConstant(Val: DivAmt));
8054 }
8055 }
8056 if (!ShiftedLHS)
8057 ShiftedLHS = getUDivExpr(LHS, RHS: MulCount);
8058 return getMulExpr(
8059 LHS: getZeroExtendExpr(
8060 Op: getTruncateExpr(Op: ShiftedLHS,
8061 Ty: IntegerType::get(C&: getContext(), NumBits: BitWidth - LZ - TZ)),
8062 Ty: BO->LHS->getType()),
8063 RHS: MulCount);
8064 }
8065 }
8066 // Binary `and` is a bit-wise `umin`.
8067 if (BO->LHS->getType()->isIntegerTy(Bitwidth: 1)) {
8068 LHS = getSCEV(V: BO->LHS);
8069 RHS = getSCEV(V: BO->RHS);
8070 return getUMinExpr(LHS, RHS);
8071 }
8072 break;
8073
8074 case Instruction::Or:
8075 // Binary `or` is a bit-wise `umax`.
8076 if (BO->LHS->getType()->isIntegerTy(Bitwidth: 1)) {
8077 LHS = getSCEV(V: BO->LHS);
8078 RHS = getSCEV(V: BO->RHS);
8079 return getUMaxExpr(LHS, RHS);
8080 }
8081 break;
8082
8083 case Instruction::Xor:
8084 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: BO->RHS)) {
8085 // If the RHS of xor is -1, then this is a not operation.
8086 if (CI->isMinusOne())
8087 return getNotSCEV(V: getSCEV(V: BO->LHS));
8088
8089 // Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
8090 // This is a variant of the check for xor with -1, and it handles
8091 // the case where instcombine has trimmed non-demanded bits out
8092 // of an xor with -1.
8093 if (auto *LBO = dyn_cast<BinaryOperator>(Val: BO->LHS))
8094 if (ConstantInt *LCI = dyn_cast<ConstantInt>(Val: LBO->getOperand(i_nocapture: 1)))
8095 if (LBO->getOpcode() == Instruction::And &&
8096 LCI->getValue() == CI->getValue())
8097 if (const SCEVZeroExtendExpr *Z =
8098 dyn_cast<SCEVZeroExtendExpr>(Val: getSCEV(V: BO->LHS))) {
8099 Type *UTy = BO->LHS->getType();
8100 const SCEV *Z0 = Z->getOperand();
8101 Type *Z0Ty = Z0->getType();
8102 unsigned Z0TySize = getTypeSizeInBits(Ty: Z0Ty);
8103
8104 // If C is a low-bits mask, the zero extend is serving to
8105 // mask off the high bits. Complement the operand and
8106 // re-apply the zext.
8107 if (CI->getValue().isMask(numBits: Z0TySize))
8108 return getZeroExtendExpr(Op: getNotSCEV(V: Z0), Ty: UTy);
8109
8110 // If C is a single bit, it may be in the sign-bit position
8111 // before the zero-extend. In this case, represent the xor
8112 // using an add, which is equivalent, and re-apply the zext.
8113 APInt Trunc = CI->getValue().trunc(width: Z0TySize);
8114 if (Trunc.zext(width: getTypeSizeInBits(Ty: UTy)) == CI->getValue() &&
8115 Trunc.isSignMask())
8116 return getZeroExtendExpr(Op: getAddExpr(LHS: Z0, RHS: getConstant(Val: Trunc)),
8117 Ty: UTy);
8118 }
8119 }
8120 break;
8121
8122 case Instruction::Shl:
8123 // Turn shift left of a constant amount into a multiply.
8124 if (ConstantInt *SA = dyn_cast<ConstantInt>(Val: BO->RHS)) {
8125 uint32_t BitWidth = cast<IntegerType>(Val: SA->getType())->getBitWidth();
8126
8127 // If the shift count is not less than the bitwidth, the result of
8128 // the shift is undefined. Don't try to analyze it, because the
8129 // resolution chosen here may differ from the resolution chosen in
8130 // other parts of the compiler.
8131 if (SA->getValue().uge(RHS: BitWidth))
8132 break;
8133
8134 // We can safely preserve the nuw flag in all cases. It's also safe to
8135 // turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation
8136 // requires special handling. It can be preserved as long as we're not
8137 // left shifting by bitwidth - 1.
8138 auto Flags = SCEV::FlagAnyWrap;
8139 if (BO->Op) {
8140 auto MulFlags = getNoWrapFlagsFromUB(V: BO->Op);
8141 if ((MulFlags & SCEV::FlagNSW) &&
8142 ((MulFlags & SCEV::FlagNUW) || SA->getValue().ult(RHS: BitWidth - 1)))
8143 Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNSW);
8144 if (MulFlags & SCEV::FlagNUW)
8145 Flags = (SCEV::NoWrapFlags)(Flags | SCEV::FlagNUW);
8146 }
8147
8148 ConstantInt *X = ConstantInt::get(
8149 Context&: getContext(), V: APInt::getOneBitSet(numBits: BitWidth, BitNo: SA->getZExtValue()));
8150 return getMulExpr(LHS: getSCEV(V: BO->LHS), RHS: getConstant(V: X), Flags);
8151 }
8152 break;
8153
8154 case Instruction::AShr:
8155 // AShr X, C, where C is a constant.
8156 ConstantInt *CI = dyn_cast<ConstantInt>(Val: BO->RHS);
8157 if (!CI)
8158 break;
8159
8160 Type *OuterTy = BO->LHS->getType();
8161 uint64_t BitWidth = getTypeSizeInBits(Ty: OuterTy);
8162 // If the shift count is not less than the bitwidth, the result of
8163 // the shift is undefined. Don't try to analyze it, because the
8164 // resolution chosen here may differ from the resolution chosen in
8165 // other parts of the compiler.
8166 if (CI->getValue().uge(RHS: BitWidth))
8167 break;
8168
8169 if (CI->isZero())
8170 return getSCEV(V: BO->LHS); // shift by zero --> noop
8171
8172 uint64_t AShrAmt = CI->getZExtValue();
8173 Type *TruncTy = IntegerType::get(C&: getContext(), NumBits: BitWidth - AShrAmt);
8174
8175 Operator *L = dyn_cast<Operator>(Val: BO->LHS);
8176 const SCEV *AddTruncateExpr = nullptr;
8177 ConstantInt *ShlAmtCI = nullptr;
8178 const SCEV *AddConstant = nullptr;
8179
8180 if (L && L->getOpcode() == Instruction::Add) {
8181 // X = Shl A, n
8182 // Y = Add X, c
8183 // Z = AShr Y, m
8184 // n, c and m are constants.
8185
8186 Operator *LShift = dyn_cast<Operator>(Val: L->getOperand(i: 0));
8187 ConstantInt *AddOperandCI = dyn_cast<ConstantInt>(Val: L->getOperand(i: 1));
8188 if (LShift && LShift->getOpcode() == Instruction::Shl) {
8189 if (AddOperandCI) {
8190 const SCEV *ShlOp0SCEV = getSCEV(V: LShift->getOperand(i: 0));
8191 ShlAmtCI = dyn_cast<ConstantInt>(Val: LShift->getOperand(i: 1));
8192 // since we truncate to TruncTy, the AddConstant should be of the
8193 // same type, so create a new Constant with type same as TruncTy.
8194 // Also, the Add constant should be shifted right by AShr amount.
8195 APInt AddOperand = AddOperandCI->getValue().ashr(ShiftAmt: AShrAmt);
8196 AddConstant = getConstant(Val: AddOperand.trunc(width: BitWidth - AShrAmt));
8197 // we model the expression as sext(add(trunc(A), c << n)), since the
8198 // sext(trunc) part is already handled below, we create a
8199 // AddExpr(TruncExp) which will be used later.
8200 AddTruncateExpr = getTruncateExpr(Op: ShlOp0SCEV, Ty: TruncTy);
8201 }
8202 }
8203 } else if (L && L->getOpcode() == Instruction::Shl) {
8204 // X = Shl A, n
8205 // Y = AShr X, m
8206 // Both n and m are constant.
8207
8208 const SCEV *ShlOp0SCEV = getSCEV(V: L->getOperand(i: 0));
8209 ShlAmtCI = dyn_cast<ConstantInt>(Val: L->getOperand(i: 1));
8210 AddTruncateExpr = getTruncateExpr(Op: ShlOp0SCEV, Ty: TruncTy);
8211 }
8212
8213 if (AddTruncateExpr && ShlAmtCI) {
8214 // We can merge the two given cases into a single SCEV statement,
8215 // incase n = m, the mul expression will be 2^0, so it gets resolved to
8216 // a simpler case. The following code handles the two cases:
8217 //
8218 // 1) For a two-shift sext-inreg, i.e. n = m,
8219 // use sext(trunc(x)) as the SCEV expression.
8220 //
8221 // 2) When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV
8222 // expression. We already checked that ShlAmt < BitWidth, so
8223 // the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as
8224 // ShlAmt - AShrAmt < Amt.
8225 const APInt &ShlAmt = ShlAmtCI->getValue();
8226 if (ShlAmt.ult(RHS: BitWidth) && ShlAmt.uge(RHS: AShrAmt)) {
8227 APInt Mul = APInt::getOneBitSet(numBits: BitWidth - AShrAmt,
8228 BitNo: ShlAmtCI->getZExtValue() - AShrAmt);
8229 const SCEV *CompositeExpr =
8230 getMulExpr(LHS: AddTruncateExpr, RHS: getConstant(Val: Mul));
8231 if (L->getOpcode() != Instruction::Shl)
8232 CompositeExpr = getAddExpr(LHS: CompositeExpr, RHS: AddConstant);
8233
8234 return getSignExtendExpr(Op: CompositeExpr, Ty: OuterTy);
8235 }
8236 }
8237 break;
8238 }
8239 }
8240
8241 switch (U->getOpcode()) {
8242 case Instruction::Trunc:
8243 return getTruncateExpr(Op: getSCEV(V: U->getOperand(i: 0)), Ty: U->getType());
8244
8245 case Instruction::ZExt:
8246 return getZeroExtendExpr(Op: getSCEV(V: U->getOperand(i: 0)), Ty: U->getType());
8247
8248 case Instruction::SExt:
8249 if (auto BO = MatchBinaryOp(V: U->getOperand(i: 0), DL: getDataLayout(), AC, DT,
8250 CxtI: dyn_cast<Instruction>(Val: V))) {
8251 // The NSW flag of a subtract does not always survive the conversion to
8252 // A + (-1)*B. By pushing sign extension onto its operands we are much
8253 // more likely to preserve NSW and allow later AddRec optimisations.
8254 //
8255 // NOTE: This is effectively duplicating this logic from getSignExtend:
8256 // sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
8257 // but by that point the NSW information has potentially been lost.
8258 if (BO->Opcode == Instruction::Sub && BO->IsNSW) {
8259 Type *Ty = U->getType();
8260 auto *V1 = getSignExtendExpr(Op: getSCEV(V: BO->LHS), Ty);
8261 auto *V2 = getSignExtendExpr(Op: getSCEV(V: BO->RHS), Ty);
8262 return getMinusSCEV(LHS: V1, RHS: V2, Flags: SCEV::FlagNSW);
8263 }
8264 }
8265 return getSignExtendExpr(Op: getSCEV(V: U->getOperand(i: 0)), Ty: U->getType());
8266
8267 case Instruction::BitCast:
8268 // BitCasts are no-op casts so we just eliminate the cast.
8269 if (isSCEVable(Ty: U->getType()) && isSCEVable(Ty: U->getOperand(i: 0)->getType()))
8270 return getSCEV(V: U->getOperand(i: 0));
8271 break;
8272
8273 case Instruction::PtrToAddr: {
8274 const SCEV *IntOp = getPtrToAddrExpr(Op: getSCEV(V: U->getOperand(i: 0)));
8275 if (isa<SCEVCouldNotCompute>(Val: IntOp))
8276 return getUnknown(V);
8277 return IntOp;
8278 }
8279
8280 case Instruction::PtrToInt: {
8281 // Pointer to integer cast is straight-forward, so do model it.
8282 const SCEV *Op = getSCEV(V: U->getOperand(i: 0));
8283 Type *DstIntTy = U->getType();
8284 // But only if effective SCEV (integer) type is wide enough to represent
8285 // all possible pointer values.
8286 const SCEV *IntOp = getPtrToIntExpr(Op, Ty: DstIntTy);
8287 if (isa<SCEVCouldNotCompute>(Val: IntOp))
8288 return getUnknown(V);
8289 return IntOp;
8290 }
8291 case Instruction::IntToPtr:
8292 // Just don't deal with inttoptr casts.
8293 return getUnknown(V);
8294
8295 case Instruction::SDiv:
8296 // If both operands are non-negative, this is just an udiv.
8297 if (isKnownNonNegative(S: getSCEV(V: U->getOperand(i: 0))) &&
8298 isKnownNonNegative(S: getSCEV(V: U->getOperand(i: 1))))
8299 return getUDivExpr(LHS: getSCEV(V: U->getOperand(i: 0)), RHS: getSCEV(V: U->getOperand(i: 1)));
8300 break;
8301
8302 case Instruction::SRem:
8303 // If both operands are non-negative, this is just an urem.
8304 if (isKnownNonNegative(S: getSCEV(V: U->getOperand(i: 0))) &&
8305 isKnownNonNegative(S: getSCEV(V: U->getOperand(i: 1))))
8306 return getURemExpr(LHS: getSCEV(V: U->getOperand(i: 0)), RHS: getSCEV(V: U->getOperand(i: 1)));
8307 break;
8308
8309 case Instruction::GetElementPtr:
8310 return createNodeForGEP(GEP: cast<GEPOperator>(Val: U));
8311
8312 case Instruction::PHI:
8313 return createNodeForPHI(PN: cast<PHINode>(Val: U));
8314
8315 case Instruction::Select:
8316 return createNodeForSelectOrPHI(V: U, Cond: U->getOperand(i: 0), TrueVal: U->getOperand(i: 1),
8317 FalseVal: U->getOperand(i: 2));
8318
8319 case Instruction::Call:
8320 case Instruction::Invoke:
8321 if (Value *RV = cast<CallBase>(Val: U)->getReturnedArgOperand())
8322 return getSCEV(V: RV);
8323
8324 if (auto *II = dyn_cast<IntrinsicInst>(Val: U)) {
8325 switch (II->getIntrinsicID()) {
8326 case Intrinsic::abs:
8327 return getAbsExpr(
8328 Op: getSCEV(V: II->getArgOperand(i: 0)),
8329 /*IsNSW=*/cast<ConstantInt>(Val: II->getArgOperand(i: 1))->isOne());
8330 case Intrinsic::umax:
8331 LHS = getSCEV(V: II->getArgOperand(i: 0));
8332 RHS = getSCEV(V: II->getArgOperand(i: 1));
8333 return getUMaxExpr(LHS, RHS);
8334 case Intrinsic::umin:
8335 LHS = getSCEV(V: II->getArgOperand(i: 0));
8336 RHS = getSCEV(V: II->getArgOperand(i: 1));
8337 return getUMinExpr(LHS, RHS);
8338 case Intrinsic::smax:
8339 LHS = getSCEV(V: II->getArgOperand(i: 0));
8340 RHS = getSCEV(V: II->getArgOperand(i: 1));
8341 return getSMaxExpr(LHS, RHS);
8342 case Intrinsic::smin:
8343 LHS = getSCEV(V: II->getArgOperand(i: 0));
8344 RHS = getSCEV(V: II->getArgOperand(i: 1));
8345 return getSMinExpr(LHS, RHS);
8346 case Intrinsic::usub_sat: {
8347 const SCEV *X = getSCEV(V: II->getArgOperand(i: 0));
8348 const SCEV *Y = getSCEV(V: II->getArgOperand(i: 1));
8349 const SCEV *ClampedY = getUMinExpr(LHS: X, RHS: Y);
8350 return getMinusSCEV(LHS: X, RHS: ClampedY, Flags: SCEV::FlagNUW);
8351 }
8352 case Intrinsic::uadd_sat: {
8353 const SCEV *X = getSCEV(V: II->getArgOperand(i: 0));
8354 const SCEV *Y = getSCEV(V: II->getArgOperand(i: 1));
8355 const SCEV *ClampedX = getUMinExpr(LHS: X, RHS: getNotSCEV(V: Y));
8356 return getAddExpr(LHS: ClampedX, RHS: Y, Flags: SCEV::FlagNUW);
8357 }
8358 case Intrinsic::start_loop_iterations:
8359 case Intrinsic::annotation:
8360 case Intrinsic::ptr_annotation:
8361 // A start_loop_iterations or llvm.annotation or llvm.prt.annotation is
8362 // just eqivalent to the first operand for SCEV purposes.
8363 return getSCEV(V: II->getArgOperand(i: 0));
8364 case Intrinsic::vscale:
8365 return getVScale(Ty: II->getType());
8366 default:
8367 break;
8368 }
8369 }
8370 break;
8371 }
8372
8373 return getUnknown(V);
8374}
8375
8376//===----------------------------------------------------------------------===//
8377// Iteration Count Computation Code
8378//
8379
8380const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount) {
8381 if (isa<SCEVCouldNotCompute>(Val: ExitCount))
8382 return getCouldNotCompute();
8383
8384 auto *ExitCountType = ExitCount->getType();
8385 assert(ExitCountType->isIntegerTy());
8386 auto *EvalTy = Type::getIntNTy(C&: ExitCountType->getContext(),
8387 N: 1 + ExitCountType->getScalarSizeInBits());
8388 return getTripCountFromExitCount(ExitCount, EvalTy, L: nullptr);
8389}
8390
8391const SCEV *ScalarEvolution::getTripCountFromExitCount(const SCEV *ExitCount,
8392 Type *EvalTy,
8393 const Loop *L) {
8394 if (isa<SCEVCouldNotCompute>(Val: ExitCount))
8395 return getCouldNotCompute();
8396
8397 unsigned ExitCountSize = getTypeSizeInBits(Ty: ExitCount->getType());
8398 unsigned EvalSize = EvalTy->getPrimitiveSizeInBits();
8399
8400 auto CanAddOneWithoutOverflow = [&]() {
8401 ConstantRange ExitCountRange =
8402 getRangeRef(S: ExitCount, SignHint: RangeSignHint::HINT_RANGE_UNSIGNED);
8403 if (!ExitCountRange.contains(Val: APInt::getMaxValue(numBits: ExitCountSize)))
8404 return true;
8405
8406 return L && isLoopEntryGuardedByCond(L, Pred: ICmpInst::ICMP_NE, LHS: ExitCount,
8407 RHS: getMinusOne(Ty: ExitCount->getType()));
8408 };
8409
8410 // If we need to zero extend the backedge count, check if we can add one to
8411 // it prior to zero extending without overflow. Provided this is safe, it
8412 // allows better simplification of the +1.
8413 if (EvalSize > ExitCountSize && CanAddOneWithoutOverflow())
8414 return getZeroExtendExpr(
8415 Op: getAddExpr(LHS: ExitCount, RHS: getOne(Ty: ExitCount->getType())), Ty: EvalTy);
8416
8417 // Get the total trip count from the count by adding 1. This may wrap.
8418 return getAddExpr(LHS: getTruncateOrZeroExtend(V: ExitCount, Ty: EvalTy), RHS: getOne(Ty: EvalTy));
8419}
8420
8421static unsigned getConstantTripCount(const SCEVConstant *ExitCount) {
8422 if (!ExitCount)
8423 return 0;
8424
8425 ConstantInt *ExitConst = ExitCount->getValue();
8426
8427 // Guard against huge trip counts.
8428 if (ExitConst->getValue().getActiveBits() > 32)
8429 return 0;
8430
8431 // In case of integer overflow, this returns 0, which is correct.
8432 return ((unsigned)ExitConst->getZExtValue()) + 1;
8433}
8434
8435unsigned ScalarEvolution::getSmallConstantTripCount(const Loop *L) {
8436 auto *ExitCount = dyn_cast<SCEVConstant>(Val: getBackedgeTakenCount(L, Kind: Exact));
8437 return getConstantTripCount(ExitCount);
8438}
8439
8440unsigned
8441ScalarEvolution::getSmallConstantTripCount(const Loop *L,
8442 const BasicBlock *ExitingBlock) {
8443 assert(ExitingBlock && "Must pass a non-null exiting block!");
8444 assert(L->isLoopExiting(ExitingBlock) &&
8445 "Exiting block must actually branch out of the loop!");
8446 const SCEVConstant *ExitCount =
8447 dyn_cast<SCEVConstant>(Val: getExitCount(L, ExitingBlock));
8448 return getConstantTripCount(ExitCount);
8449}
8450
8451unsigned ScalarEvolution::getSmallConstantMaxTripCount(
8452 const Loop *L, SmallVectorImpl<const SCEVPredicate *> *Predicates) {
8453
8454 const auto *MaxExitCount =
8455 Predicates ? getPredicatedConstantMaxBackedgeTakenCount(L, Predicates&: *Predicates)
8456 : getConstantMaxBackedgeTakenCount(L);
8457 return getConstantTripCount(ExitCount: dyn_cast<SCEVConstant>(Val: MaxExitCount));
8458}
8459
8460unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) {
8461 SmallVector<BasicBlock *, 8> ExitingBlocks;
8462 L->getExitingBlocks(ExitingBlocks);
8463
8464 std::optional<unsigned> Res;
8465 for (auto *ExitingBB : ExitingBlocks) {
8466 unsigned Multiple = getSmallConstantTripMultiple(L, ExitingBlock: ExitingBB);
8467 if (!Res)
8468 Res = Multiple;
8469 Res = std::gcd(m: *Res, n: Multiple);
8470 }
8471 return Res.value_or(u: 1);
8472}
8473
8474unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
8475 const SCEV *ExitCount) {
8476 if (isa<SCEVCouldNotCompute>(Val: ExitCount))
8477 return 1;
8478
8479 // Get the trip count
8480 const SCEV *TCExpr = getTripCountFromExitCount(ExitCount: applyLoopGuards(Expr: ExitCount, L));
8481
8482 APInt Multiple = getNonZeroConstantMultiple(S: TCExpr);
8483 // If a trip multiple is huge (>=2^32), the trip count is still divisible by
8484 // the greatest power of 2 divisor less than 2^32.
8485 return Multiple.getActiveBits() > 32
8486 ? 1U << std::min(a: 31U, b: Multiple.countTrailingZeros())
8487 : (unsigned)Multiple.getZExtValue();
8488}
8489
8490/// Returns the largest constant divisor of the trip count of this loop as a
8491/// normal unsigned value, if possible. This means that the actual trip count is
8492/// always a multiple of the returned value (don't forget the trip count could
8493/// very well be zero as well!).
8494///
8495/// Returns 1 if the trip count is unknown or not guaranteed to be the
8496/// multiple of a constant (which is also the case if the trip count is simply
8497/// constant, use getSmallConstantTripCount for that case), Will also return 1
8498/// if the trip count is very large (>= 2^32).
8499///
8500/// As explained in the comments for getSmallConstantTripCount, this assumes
8501/// that control exits the loop via ExitingBlock.
8502unsigned
8503ScalarEvolution::getSmallConstantTripMultiple(const Loop *L,
8504 const BasicBlock *ExitingBlock) {
8505 assert(ExitingBlock && "Must pass a non-null exiting block!");
8506 assert(L->isLoopExiting(ExitingBlock) &&
8507 "Exiting block must actually branch out of the loop!");
8508 const SCEV *ExitCount = getExitCount(L, ExitingBlock);
8509 return getSmallConstantTripMultiple(L, ExitCount);
8510}
8511
8512const SCEV *ScalarEvolution::getExitCount(const Loop *L,
8513 const BasicBlock *ExitingBlock,
8514 ExitCountKind Kind) {
8515 switch (Kind) {
8516 case Exact:
8517 return getBackedgeTakenInfo(L).getExact(ExitingBlock, SE: this);
8518 case SymbolicMaximum:
8519 return getBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock, SE: this);
8520 case ConstantMaximum:
8521 return getBackedgeTakenInfo(L).getConstantMax(ExitingBlock, SE: this);
8522 };
8523 llvm_unreachable("Invalid ExitCountKind!");
8524}
8525
8526const SCEV *ScalarEvolution::getPredicatedExitCount(
8527 const Loop *L, const BasicBlock *ExitingBlock,
8528 SmallVectorImpl<const SCEVPredicate *> *Predicates, ExitCountKind Kind) {
8529 switch (Kind) {
8530 case Exact:
8531 return getPredicatedBackedgeTakenInfo(L).getExact(ExitingBlock, SE: this,
8532 Predicates);
8533 case SymbolicMaximum:
8534 return getPredicatedBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock, SE: this,
8535 Predicates);
8536 case ConstantMaximum:
8537 return getPredicatedBackedgeTakenInfo(L).getConstantMax(ExitingBlock, SE: this,
8538 Predicates);
8539 };
8540 llvm_unreachable("Invalid ExitCountKind!");
8541}
8542
8543const SCEV *ScalarEvolution::getPredicatedBackedgeTakenCount(
8544 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Preds) {
8545 return getPredicatedBackedgeTakenInfo(L).getExact(L, SE: this, Predicates: &Preds);
8546}
8547
8548const SCEV *ScalarEvolution::getBackedgeTakenCount(const Loop *L,
8549 ExitCountKind Kind) {
8550 switch (Kind) {
8551 case Exact:
8552 return getBackedgeTakenInfo(L).getExact(L, SE: this);
8553 case ConstantMaximum:
8554 return getBackedgeTakenInfo(L).getConstantMax(SE: this);
8555 case SymbolicMaximum:
8556 return getBackedgeTakenInfo(L).getSymbolicMax(L, SE: this);
8557 };
8558 llvm_unreachable("Invalid ExitCountKind!");
8559}
8560
8561const SCEV *ScalarEvolution::getPredicatedSymbolicMaxBackedgeTakenCount(
8562 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Preds) {
8563 return getPredicatedBackedgeTakenInfo(L).getSymbolicMax(L, SE: this, Predicates: &Preds);
8564}
8565
8566const SCEV *ScalarEvolution::getPredicatedConstantMaxBackedgeTakenCount(
8567 const Loop *L, SmallVectorImpl<const SCEVPredicate *> &Preds) {
8568 return getPredicatedBackedgeTakenInfo(L).getConstantMax(SE: this, Predicates: &Preds);
8569}
8570
8571bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) {
8572 return getBackedgeTakenInfo(L).isConstantMaxOrZero(SE: this);
8573}
8574
8575/// Push PHI nodes in the header of the given loop onto the given Worklist.
8576static void PushLoopPHIs(const Loop *L,
8577 SmallVectorImpl<Instruction *> &Worklist,
8578 SmallPtrSetImpl<Instruction *> &Visited) {
8579 BasicBlock *Header = L->getHeader();
8580
8581 // Push all Loop-header PHIs onto the Worklist stack.
8582 for (PHINode &PN : Header->phis())
8583 if (Visited.insert(Ptr: &PN).second)
8584 Worklist.push_back(Elt: &PN);
8585}
8586
8587ScalarEvolution::BackedgeTakenInfo &
8588ScalarEvolution::getPredicatedBackedgeTakenInfo(const Loop *L) {
8589 auto &BTI = getBackedgeTakenInfo(L);
8590 if (BTI.hasFullInfo())
8591 return BTI;
8592
8593 auto Pair = PredicatedBackedgeTakenCounts.try_emplace(Key: L);
8594
8595 if (!Pair.second)
8596 return Pair.first->second;
8597
8598 BackedgeTakenInfo Result =
8599 computeBackedgeTakenCount(L, /*AllowPredicates=*/true);
8600
8601 return PredicatedBackedgeTakenCounts.find(Val: L)->second = std::move(Result);
8602}
8603
8604ScalarEvolution::BackedgeTakenInfo &
8605ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
8606 // Initially insert an invalid entry for this loop. If the insertion
8607 // succeeds, proceed to actually compute a backedge-taken count and
8608 // update the value. The temporary CouldNotCompute value tells SCEV
8609 // code elsewhere that it shouldn't attempt to request a new
8610 // backedge-taken count, which could result in infinite recursion.
8611 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator, bool> Pair =
8612 BackedgeTakenCounts.try_emplace(Key: L);
8613 if (!Pair.second)
8614 return Pair.first->second;
8615
8616 // computeBackedgeTakenCount may allocate memory for its result. Inserting it
8617 // into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
8618 // must be cleared in this scope.
8619 BackedgeTakenInfo Result = computeBackedgeTakenCount(L);
8620
8621 // Now that we know more about the trip count for this loop, forget any
8622 // existing SCEV values for PHI nodes in this loop since they are only
8623 // conservative estimates made without the benefit of trip count
8624 // information. This invalidation is not necessary for correctness, and is
8625 // only done to produce more precise results.
8626 if (Result.hasAnyInfo()) {
8627 // Invalidate any expression using an addrec in this loop.
8628 SmallVector<SCEVUse, 8> ToForget;
8629 auto LoopUsersIt = LoopUsers.find(Val: L);
8630 if (LoopUsersIt != LoopUsers.end())
8631 append_range(C&: ToForget, R&: LoopUsersIt->second);
8632 forgetMemoizedResults(SCEVs: ToForget);
8633
8634 // Invalidate constant-evolved loop header phis.
8635 for (PHINode &PN : L->getHeader()->phis())
8636 ConstantEvolutionLoopExitValue.erase(Val: &PN);
8637 }
8638
8639 // Re-lookup the insert position, since the call to
8640 // computeBackedgeTakenCount above could result in a
8641 // recusive call to getBackedgeTakenInfo (on a different
8642 // loop), which would invalidate the iterator computed
8643 // earlier.
8644 return BackedgeTakenCounts.find(Val: L)->second = std::move(Result);
8645}
8646
8647void ScalarEvolution::forgetAllLoops() {
8648 // This method is intended to forget all info about loops. It should
8649 // invalidate caches as if the following happened:
8650 // - The trip counts of all loops have changed arbitrarily
8651 // - Every llvm::Value has been updated in place to produce a different
8652 // result.
8653 BackedgeTakenCounts.clear();
8654 PredicatedBackedgeTakenCounts.clear();
8655 BECountUsers.clear();
8656 LoopPropertiesCache.clear();
8657 ConstantEvolutionLoopExitValue.clear();
8658 ValueExprMap.clear();
8659 ValuesAtScopes.clear();
8660 ValuesAtScopesUsers.clear();
8661 LoopDispositions.clear();
8662 BlockDispositions.clear();
8663 UnsignedRanges.clear();
8664 SignedRanges.clear();
8665 ExprValueMap.clear();
8666 HasRecMap.clear();
8667 ConstantMultipleCache.clear();
8668 PredicatedSCEVRewrites.clear();
8669 FoldCache.clear();
8670 FoldCacheUser.clear();
8671}
8672void ScalarEvolution::visitAndClearUsers(
8673 SmallVectorImpl<Instruction *> &Worklist,
8674 SmallPtrSetImpl<Instruction *> &Visited,
8675 SmallVectorImpl<SCEVUse> &ToForget) {
8676 while (!Worklist.empty()) {
8677 Instruction *I = Worklist.pop_back_val();
8678 if (!isSCEVable(Ty: I->getType()) && !isa<WithOverflowInst>(Val: I))
8679 continue;
8680
8681 ValueExprMapType::iterator It =
8682 ValueExprMap.find_as(Val: static_cast<Value *>(I));
8683 if (It != ValueExprMap.end()) {
8684 eraseValueFromMap(V: It->first);
8685 ToForget.push_back(Elt: It->second);
8686 if (PHINode *PN = dyn_cast<PHINode>(Val: I))
8687 ConstantEvolutionLoopExitValue.erase(Val: PN);
8688 }
8689
8690 PushDefUseChildren(I, Worklist, Visited);
8691 }
8692}
8693
8694void ScalarEvolution::forgetLoop(const Loop *L) {
8695 SmallVector<const Loop *, 16> LoopWorklist(1, L);
8696 SmallVector<Instruction *, 32> Worklist;
8697 SmallPtrSet<Instruction *, 16> Visited;
8698 SmallVector<SCEVUse, 16> ToForget;
8699
8700 // Iterate over all the loops and sub-loops to drop SCEV information.
8701 while (!LoopWorklist.empty()) {
8702 auto *CurrL = LoopWorklist.pop_back_val();
8703
8704 // Drop any stored trip count value.
8705 forgetBackedgeTakenCounts(L: CurrL, /* Predicated */ false);
8706 forgetBackedgeTakenCounts(L: CurrL, /* Predicated */ true);
8707
8708 // Drop information about predicated SCEV rewrites for this loop.
8709 for (auto I = PredicatedSCEVRewrites.begin();
8710 I != PredicatedSCEVRewrites.end();) {
8711 std::pair<const SCEV *, const Loop *> Entry = I->first;
8712 if (Entry.second == CurrL)
8713 PredicatedSCEVRewrites.erase(I: I++);
8714 else
8715 ++I;
8716 }
8717
8718 auto LoopUsersItr = LoopUsers.find(Val: CurrL);
8719 if (LoopUsersItr != LoopUsers.end())
8720 llvm::append_range(C&: ToForget, R&: LoopUsersItr->second);
8721
8722 // Drop information about expressions based on loop-header PHIs.
8723 PushLoopPHIs(L: CurrL, Worklist, Visited);
8724 visitAndClearUsers(Worklist, Visited, ToForget);
8725
8726 LoopPropertiesCache.erase(Val: CurrL);
8727 // Forget all contained loops too, to avoid dangling entries in the
8728 // ValuesAtScopes map.
8729 LoopWorklist.append(in_start: CurrL->begin(), in_end: CurrL->end());
8730 }
8731 forgetMemoizedResults(SCEVs: ToForget);
8732}
8733
8734void ScalarEvolution::forgetTopmostLoop(const Loop *L) {
8735 forgetLoop(L: L->getOutermostLoop());
8736}
8737
8738void ScalarEvolution::forgetValue(Value *V) {
8739 Instruction *I = dyn_cast<Instruction>(Val: V);
8740 if (!I) return;
8741
8742 // Drop information about expressions based on loop-header PHIs.
8743 SmallVector<Instruction *, 16> Worklist;
8744 SmallPtrSet<Instruction *, 8> Visited;
8745 SmallVector<SCEVUse, 8> ToForget;
8746 Worklist.push_back(Elt: I);
8747 Visited.insert(Ptr: I);
8748 visitAndClearUsers(Worklist, Visited, ToForget);
8749
8750 forgetMemoizedResults(SCEVs: ToForget);
8751}
8752
8753void ScalarEvolution::forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V) {
8754 if (!isSCEVable(Ty: V->getType()))
8755 return;
8756
8757 // If SCEV looked through a trivial LCSSA phi node, we might have SCEV's
8758 // directly using a SCEVUnknown/SCEVAddRec defined in the loop. After an
8759 // extra predecessor is added, this is no longer valid. Find all Unknowns and
8760 // AddRecs defined in the loop and invalidate any SCEV's making use of them.
8761 if (const SCEV *S = getExistingSCEV(V)) {
8762 struct InvalidationRootCollector {
8763 Loop *L;
8764 SmallVector<SCEVUse, 8> Roots;
8765
8766 InvalidationRootCollector(Loop *L) : L(L) {}
8767
8768 bool follow(const SCEV *S) {
8769 if (auto *SU = dyn_cast<SCEVUnknown>(Val: S)) {
8770 if (auto *I = dyn_cast<Instruction>(Val: SU->getValue()))
8771 if (L->contains(Inst: I))
8772 Roots.push_back(Elt: S);
8773 } else if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Val: S)) {
8774 if (L->contains(L: AddRec->getLoop()))
8775 Roots.push_back(Elt: S);
8776 }
8777 return true;
8778 }
8779 bool isDone() const { return false; }
8780 };
8781
8782 InvalidationRootCollector C(L);
8783 visitAll(Root: S, Visitor&: C);
8784 forgetMemoizedResults(SCEVs: C.Roots);
8785 }
8786
8787 // Also perform the normal invalidation.
8788 forgetValue(V);
8789}
8790
8791void ScalarEvolution::forgetLoopDispositions() { LoopDispositions.clear(); }
8792
8793void ScalarEvolution::forgetBlockAndLoopDispositions(Value *V) {
8794 // Unless a specific value is passed to invalidation, completely clear both
8795 // caches.
8796 if (!V) {
8797 BlockDispositions.clear();
8798 LoopDispositions.clear();
8799 return;
8800 }
8801
8802 if (!isSCEVable(Ty: V->getType()))
8803 return;
8804
8805 const SCEV *S = getExistingSCEV(V);
8806 if (!S)
8807 return;
8808
8809 // Invalidate the block and loop dispositions cached for S. Dispositions of
8810 // S's users may change if S's disposition changes (i.e. a user may change to
8811 // loop-invariant, if S changes to loop invariant), so also invalidate
8812 // dispositions of S's users recursively.
8813 SmallVector<SCEVUse, 8> Worklist = {S};
8814 SmallPtrSet<const SCEV *, 8> Seen = {S};
8815 while (!Worklist.empty()) {
8816 const SCEV *Curr = Worklist.pop_back_val();
8817 bool LoopDispoRemoved = LoopDispositions.erase(Val: Curr);
8818 bool BlockDispoRemoved = BlockDispositions.erase(Val: Curr);
8819 if (!LoopDispoRemoved && !BlockDispoRemoved)
8820 continue;
8821 auto Users = SCEVUsers.find(Val: Curr);
8822 if (Users != SCEVUsers.end())
8823 for (const auto *User : Users->second)
8824 if (Seen.insert(Ptr: User).second)
8825 Worklist.push_back(Elt: User);
8826 }
8827}
8828
8829/// Get the exact loop backedge taken count considering all loop exits. A
8830/// computable result can only be returned for loops with all exiting blocks
8831/// dominating the latch. howFarToZero assumes that the limit of each loop test
8832/// is never skipped. This is a valid assumption as long as the loop exits via
8833/// that test. For precise results, it is the caller's responsibility to specify
8834/// the relevant loop exiting block using getExact(ExitingBlock, SE).
8835const SCEV *ScalarEvolution::BackedgeTakenInfo::getExact(
8836 const Loop *L, ScalarEvolution *SE,
8837 SmallVectorImpl<const SCEVPredicate *> *Preds) const {
8838 // If any exits were not computable, the loop is not computable.
8839 if (!isComplete() || ExitNotTaken.empty())
8840 return SE->getCouldNotCompute();
8841
8842 const BasicBlock *Latch = L->getLoopLatch();
8843 // All exiting blocks we have collected must dominate the only backedge.
8844 if (!Latch)
8845 return SE->getCouldNotCompute();
8846
8847 // All exiting blocks we have gathered dominate loop's latch, so exact trip
8848 // count is simply a minimum out of all these calculated exit counts.
8849 SmallVector<SCEVUse, 2> Ops;
8850 for (const auto &ENT : ExitNotTaken) {
8851 const SCEV *BECount = ENT.ExactNotTaken;
8852 assert(BECount != SE->getCouldNotCompute() && "Bad exit SCEV!");
8853 assert(SE->DT.dominates(ENT.ExitingBlock, Latch) &&
8854 "We should only have known counts for exiting blocks that dominate "
8855 "latch!");
8856
8857 Ops.push_back(Elt: BECount);
8858
8859 if (Preds)
8860 append_range(C&: *Preds, R: ENT.Predicates);
8861
8862 assert((Preds || ENT.hasAlwaysTruePredicate()) &&
8863 "Predicate should be always true!");
8864 }
8865
8866 // If an earlier exit exits on the first iteration (exit count zero), then
8867 // a later poison exit count should not propagate into the result. This are
8868 // exactly the semantics provided by umin_seq.
8869 return SE->getUMinFromMismatchedTypes(Ops, /* Sequential */ true);
8870}
8871
8872const ScalarEvolution::ExitNotTakenInfo *
8873ScalarEvolution::BackedgeTakenInfo::getExitNotTaken(
8874 const BasicBlock *ExitingBlock,
8875 SmallVectorImpl<const SCEVPredicate *> *Predicates) const {
8876 for (const auto &ENT : ExitNotTaken)
8877 if (ENT.ExitingBlock == ExitingBlock) {
8878 if (ENT.hasAlwaysTruePredicate())
8879 return &ENT;
8880 else if (Predicates) {
8881 append_range(C&: *Predicates, R: ENT.Predicates);
8882 return &ENT;
8883 }
8884 }
8885
8886 return nullptr;
8887}
8888
8889/// getConstantMax - Get the constant max backedge taken count for the loop.
8890const SCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax(
8891 ScalarEvolution *SE,
8892 SmallVectorImpl<const SCEVPredicate *> *Predicates) const {
8893 if (!getConstantMax())
8894 return SE->getCouldNotCompute();
8895
8896 for (const auto &ENT : ExitNotTaken)
8897 if (!ENT.hasAlwaysTruePredicate()) {
8898 if (!Predicates)
8899 return SE->getCouldNotCompute();
8900 append_range(C&: *Predicates, R: ENT.Predicates);
8901 }
8902
8903 assert((isa<SCEVCouldNotCompute>(getConstantMax()) ||
8904 isa<SCEVConstant>(getConstantMax())) &&
8905 "No point in having a non-constant max backedge taken count!");
8906 return getConstantMax();
8907}
8908
8909const SCEV *ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(
8910 const Loop *L, ScalarEvolution *SE,
8911 SmallVectorImpl<const SCEVPredicate *> *Predicates) {
8912 if (!SymbolicMax) {
8913 // Form an expression for the maximum exit count possible for this loop. We
8914 // merge the max and exact information to approximate a version of
8915 // getConstantMaxBackedgeTakenCount which isn't restricted to just
8916 // constants.
8917 SmallVector<SCEVUse, 4> ExitCounts;
8918
8919 for (const auto &ENT : ExitNotTaken) {
8920 const SCEV *ExitCount = ENT.SymbolicMaxNotTaken;
8921 if (!isa<SCEVCouldNotCompute>(Val: ExitCount)) {
8922 assert(SE->DT.dominates(ENT.ExitingBlock, L->getLoopLatch()) &&
8923 "We should only have known counts for exiting blocks that "
8924 "dominate latch!");
8925 ExitCounts.push_back(Elt: ExitCount);
8926 if (Predicates)
8927 append_range(C&: *Predicates, R: ENT.Predicates);
8928
8929 assert((Predicates || ENT.hasAlwaysTruePredicate()) &&
8930 "Predicate should be always true!");
8931 }
8932 }
8933 if (ExitCounts.empty())
8934 SymbolicMax = SE->getCouldNotCompute();
8935 else
8936 SymbolicMax =
8937 SE->getUMinFromMismatchedTypes(Ops&: ExitCounts, /*Sequential*/ true);
8938 }
8939 return SymbolicMax;
8940}
8941
8942bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero(
8943 ScalarEvolution *SE) const {
8944 auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
8945 return !ENT.hasAlwaysTruePredicate();
8946 };
8947 return MaxOrZero && !any_of(Range: ExitNotTaken, P: PredicateNotAlwaysTrue);
8948}
8949
8950ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E)
8951 : ExitLimit(E, E, E, false) {}
8952
8953ScalarEvolution::ExitLimit::ExitLimit(
8954 const SCEV *E, const SCEV *ConstantMaxNotTaken,
8955 const SCEV *SymbolicMaxNotTaken, bool MaxOrZero,
8956 ArrayRef<ArrayRef<const SCEVPredicate *>> PredLists)
8957 : ExactNotTaken(E), ConstantMaxNotTaken(ConstantMaxNotTaken),
8958 SymbolicMaxNotTaken(SymbolicMaxNotTaken), MaxOrZero(MaxOrZero) {
8959 // If we prove the max count is zero, so is the symbolic bound. This happens
8960 // in practice due to differences in a) how context sensitive we've chosen
8961 // to be and b) how we reason about bounds implied by UB.
8962 if (ConstantMaxNotTaken->isZero()) {
8963 this->ExactNotTaken = E = ConstantMaxNotTaken;
8964 this->SymbolicMaxNotTaken = SymbolicMaxNotTaken = ConstantMaxNotTaken;
8965 }
8966
8967 assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
8968 !isa<SCEVCouldNotCompute>(ConstantMaxNotTaken)) &&
8969 "Exact is not allowed to be less precise than Constant Max");
8970 assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
8971 !isa<SCEVCouldNotCompute>(SymbolicMaxNotTaken)) &&
8972 "Exact is not allowed to be less precise than Symbolic Max");
8973 assert((isa<SCEVCouldNotCompute>(SymbolicMaxNotTaken) ||
8974 !isa<SCEVCouldNotCompute>(ConstantMaxNotTaken)) &&
8975 "Symbolic Max is not allowed to be less precise than Constant Max");
8976 assert((isa<SCEVCouldNotCompute>(ConstantMaxNotTaken) ||
8977 isa<SCEVConstant>(ConstantMaxNotTaken)) &&
8978 "No point in having a non-constant max backedge taken count!");
8979 SmallPtrSet<const SCEVPredicate *, 4> SeenPreds;
8980 for (const auto PredList : PredLists)
8981 for (const auto *P : PredList) {
8982 if (SeenPreds.contains(Ptr: P))
8983 continue;
8984 assert(!isa<SCEVUnionPredicate>(P) && "Only add leaf predicates here!");
8985 SeenPreds.insert(Ptr: P);
8986 Predicates.push_back(Elt: P);
8987 }
8988 assert((isa<SCEVCouldNotCompute>(E) || !E->getType()->isPointerTy()) &&
8989 "Backedge count should be int");
8990 assert((isa<SCEVCouldNotCompute>(ConstantMaxNotTaken) ||
8991 !ConstantMaxNotTaken->getType()->isPointerTy()) &&
8992 "Max backedge count should be int");
8993}
8994
8995ScalarEvolution::ExitLimit::ExitLimit(const SCEV *E,
8996 const SCEV *ConstantMaxNotTaken,
8997 const SCEV *SymbolicMaxNotTaken,
8998 bool MaxOrZero,
8999 ArrayRef<const SCEVPredicate *> PredList)
9000 : ExitLimit(E, ConstantMaxNotTaken, SymbolicMaxNotTaken, MaxOrZero,
9001 ArrayRef({PredList})) {}
9002
9003/// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
9004/// computable exit into a persistent ExitNotTakenInfo array.
9005ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
9006 ArrayRef<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo> ExitCounts,
9007 bool IsComplete, const SCEV *ConstantMax, bool MaxOrZero)
9008 : ConstantMax(ConstantMax), IsComplete(IsComplete), MaxOrZero(MaxOrZero) {
9009 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
9010
9011 ExitNotTaken.reserve(N: ExitCounts.size());
9012 std::transform(first: ExitCounts.begin(), last: ExitCounts.end(),
9013 result: std::back_inserter(x&: ExitNotTaken),
9014 unary_op: [&](const EdgeExitInfo &EEI) {
9015 BasicBlock *ExitBB = EEI.first;
9016 const ExitLimit &EL = EEI.second;
9017 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken,
9018 EL.ConstantMaxNotTaken, EL.SymbolicMaxNotTaken,
9019 EL.Predicates);
9020 });
9021 assert((isa<SCEVCouldNotCompute>(ConstantMax) ||
9022 isa<SCEVConstant>(ConstantMax)) &&
9023 "No point in having a non-constant max backedge taken count!");
9024}
9025
9026/// Compute the number of times the backedge of the specified loop will execute.
9027ScalarEvolution::BackedgeTakenInfo
9028ScalarEvolution::computeBackedgeTakenCount(const Loop *L,
9029 bool AllowPredicates) {
9030 SmallVector<BasicBlock *, 8> ExitingBlocks;
9031 L->getExitingBlocks(ExitingBlocks);
9032
9033 using EdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
9034
9035 SmallVector<EdgeExitInfo, 4> ExitCounts;
9036 bool CouldComputeBECount = true;
9037 BasicBlock *Latch = L->getLoopLatch(); // may be NULL.
9038 const SCEV *MustExitMaxBECount = nullptr;
9039 const SCEV *MayExitMaxBECount = nullptr;
9040 bool MustExitMaxOrZero = false;
9041 bool IsOnlyExit = ExitingBlocks.size() == 1;
9042
9043 // Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
9044 // and compute maxBECount.
9045 // Do a union of all the predicates here.
9046 for (BasicBlock *ExitBB : ExitingBlocks) {
9047 // We canonicalize untaken exits to br (constant), ignore them so that
9048 // proving an exit untaken doesn't negatively impact our ability to reason
9049 // about the loop as whole.
9050 if (auto *BI = dyn_cast<CondBrInst>(Val: ExitBB->getTerminator()))
9051 if (auto *CI = dyn_cast<ConstantInt>(Val: BI->getCondition())) {
9052 bool ExitIfTrue = !L->contains(BB: BI->getSuccessor(i: 0));
9053 if (ExitIfTrue == CI->isZero())
9054 continue;
9055 }
9056
9057 ExitLimit EL = computeExitLimit(L, ExitingBlock: ExitBB, IsOnlyExit, AllowPredicates);
9058
9059 assert((AllowPredicates || EL.Predicates.empty()) &&
9060 "Predicated exit limit when predicates are not allowed!");
9061
9062 // 1. For each exit that can be computed, add an entry to ExitCounts.
9063 // CouldComputeBECount is true only if all exits can be computed.
9064 if (EL.ExactNotTaken != getCouldNotCompute())
9065 ++NumExitCountsComputed;
9066 else
9067 // We couldn't compute an exact value for this exit, so
9068 // we won't be able to compute an exact value for the loop.
9069 CouldComputeBECount = false;
9070 // Remember exit count if either exact or symbolic is known. Because
9071 // Exact always implies symbolic, only check symbolic.
9072 if (EL.SymbolicMaxNotTaken != getCouldNotCompute())
9073 ExitCounts.emplace_back(Args&: ExitBB, Args&: EL);
9074 else {
9075 assert(EL.ExactNotTaken == getCouldNotCompute() &&
9076 "Exact is known but symbolic isn't?");
9077 ++NumExitCountsNotComputed;
9078 }
9079
9080 // 2. Derive the loop's MaxBECount from each exit's max number of
9081 // non-exiting iterations. Partition the loop exits into two kinds:
9082 // LoopMustExits and LoopMayExits.
9083 //
9084 // If the exit dominates the loop latch, it is a LoopMustExit otherwise it
9085 // is a LoopMayExit. If any computable LoopMustExit is found, then
9086 // MaxBECount is the minimum EL.ConstantMaxNotTaken of computable
9087 // LoopMustExits. Otherwise, MaxBECount is conservatively the maximum
9088 // EL.ConstantMaxNotTaken, where CouldNotCompute is considered greater than
9089 // any
9090 // computable EL.ConstantMaxNotTaken.
9091 if (EL.ConstantMaxNotTaken != getCouldNotCompute() && Latch &&
9092 DT.dominates(A: ExitBB, B: Latch)) {
9093 if (!MustExitMaxBECount) {
9094 MustExitMaxBECount = EL.ConstantMaxNotTaken;
9095 MustExitMaxOrZero = EL.MaxOrZero;
9096 } else {
9097 MustExitMaxBECount = getUMinFromMismatchedTypes(LHS: MustExitMaxBECount,
9098 RHS: EL.ConstantMaxNotTaken);
9099 }
9100 } else if (MayExitMaxBECount != getCouldNotCompute()) {
9101 if (!MayExitMaxBECount || EL.ConstantMaxNotTaken == getCouldNotCompute())
9102 MayExitMaxBECount = EL.ConstantMaxNotTaken;
9103 else {
9104 MayExitMaxBECount = getUMaxFromMismatchedTypes(LHS: MayExitMaxBECount,
9105 RHS: EL.ConstantMaxNotTaken);
9106 }
9107 }
9108 }
9109 const SCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
9110 (MayExitMaxBECount ? MayExitMaxBECount : getCouldNotCompute());
9111 // The loop backedge will be taken the maximum or zero times if there's
9112 // a single exit that must be taken the maximum or zero times.
9113 bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1);
9114
9115 // Remember which SCEVs are used in exit limits for invalidation purposes.
9116 // We only care about non-constant SCEVs here, so we can ignore
9117 // EL.ConstantMaxNotTaken
9118 // and MaxBECount, which must be SCEVConstant.
9119 for (const auto &Pair : ExitCounts) {
9120 if (!isa<SCEVConstant>(Val: Pair.second.ExactNotTaken))
9121 BECountUsers[Pair.second.ExactNotTaken].insert(Ptr: {L, AllowPredicates});
9122 if (!isa<SCEVConstant>(Val: Pair.second.SymbolicMaxNotTaken))
9123 BECountUsers[Pair.second.SymbolicMaxNotTaken].insert(
9124 Ptr: {L, AllowPredicates});
9125 }
9126 return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount,
9127 MaxBECount, MaxOrZero);
9128}
9129
9130ScalarEvolution::ExitLimit
9131ScalarEvolution::computeExitLimit(const Loop *L, BasicBlock *ExitingBlock,
9132 bool IsOnlyExit, bool AllowPredicates) {
9133 assert(L->contains(ExitingBlock) && "Exit count for non-loop block?");
9134 // If our exiting block does not dominate the latch, then its connection with
9135 // loop's exit limit may be far from trivial.
9136 const BasicBlock *Latch = L->getLoopLatch();
9137 if (!Latch || !DT.dominates(A: ExitingBlock, B: Latch))
9138 return getCouldNotCompute();
9139
9140 Instruction *Term = ExitingBlock->getTerminator();
9141 if (CondBrInst *BI = dyn_cast<CondBrInst>(Val: Term)) {
9142 bool ExitIfTrue = !L->contains(BB: BI->getSuccessor(i: 0));
9143 assert(ExitIfTrue == L->contains(BI->getSuccessor(1)) &&
9144 "It should have one successor in loop and one exit block!");
9145 // Proceed to the next level to examine the exit condition expression.
9146 return computeExitLimitFromCond(L, ExitCond: BI->getCondition(), ExitIfTrue,
9147 /*ControlsOnlyExit=*/IsOnlyExit,
9148 AllowPredicates);
9149 }
9150
9151 if (SwitchInst *SI = dyn_cast<SwitchInst>(Val: Term)) {
9152 // For switch, make sure that there is a single exit from the loop.
9153 BasicBlock *Exit = nullptr;
9154 for (auto *SBB : successors(BB: ExitingBlock))
9155 if (!L->contains(BB: SBB)) {
9156 if (Exit) // Multiple exit successors.
9157 return getCouldNotCompute();
9158 Exit = SBB;
9159 }
9160 assert(Exit && "Exiting block must have at least one exit");
9161 return computeExitLimitFromSingleExitSwitch(
9162 L, Switch: SI, ExitingBB: Exit, /*ControlsOnlyExit=*/IsSubExpr: IsOnlyExit);
9163 }
9164
9165 return getCouldNotCompute();
9166}
9167
9168ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCond(
9169 const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit,
9170 bool AllowPredicates) {
9171 ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates);
9172 return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue,
9173 ControlsOnlyExit, AllowPredicates);
9174}
9175
9176std::optional<ScalarEvolution::ExitLimit>
9177ScalarEvolution::ExitLimitCache::find(const Loop *L, Value *ExitCond,
9178 bool ExitIfTrue, bool ControlsOnlyExit,
9179 bool AllowPredicates) {
9180 (void)this->L;
9181 (void)this->ExitIfTrue;
9182 (void)this->AllowPredicates;
9183
9184 assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
9185 this->AllowPredicates == AllowPredicates &&
9186 "Variance in assumed invariant key components!");
9187 auto Itr = TripCountMap.find(Val: {ExitCond, ControlsOnlyExit});
9188 if (Itr == TripCountMap.end())
9189 return std::nullopt;
9190 return Itr->second;
9191}
9192
9193void ScalarEvolution::ExitLimitCache::insert(const Loop *L, Value *ExitCond,
9194 bool ExitIfTrue,
9195 bool ControlsOnlyExit,
9196 bool AllowPredicates,
9197 const ExitLimit &EL) {
9198 assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
9199 this->AllowPredicates == AllowPredicates &&
9200 "Variance in assumed invariant key components!");
9201
9202 auto InsertResult = TripCountMap.insert(KV: {{ExitCond, ControlsOnlyExit}, EL});
9203 assert(InsertResult.second && "Expected successful insertion!");
9204 (void)InsertResult;
9205 (void)ExitIfTrue;
9206}
9207
9208ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
9209 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
9210 bool ControlsOnlyExit, bool AllowPredicates) {
9211
9212 if (auto MaybeEL = Cache.find(L, ExitCond, ExitIfTrue, ControlsOnlyExit,
9213 AllowPredicates))
9214 return *MaybeEL;
9215
9216 ExitLimit EL = computeExitLimitFromCondImpl(
9217 Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates);
9218 Cache.insert(L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates, EL);
9219 return EL;
9220}
9221
9222ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
9223 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
9224 bool ControlsOnlyExit, bool AllowPredicates) {
9225 // Handle BinOp conditions (And, Or).
9226 if (auto LimitFromBinOp = computeExitLimitFromCondFromBinOp(
9227 Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates))
9228 return *LimitFromBinOp;
9229
9230 // With an icmp, it may be feasible to compute an exact backedge-taken count.
9231 // Proceed to the next level to examine the icmp.
9232 if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(Val: ExitCond)) {
9233 ExitLimit EL =
9234 computeExitLimitFromICmp(L, ExitCond: ExitCondICmp, ExitIfTrue, IsSubExpr: ControlsOnlyExit);
9235 if (EL.hasFullInfo() || !AllowPredicates)
9236 return EL;
9237
9238 // Try again, but use SCEV predicates this time.
9239 return computeExitLimitFromICmp(L, ExitCond: ExitCondICmp, ExitIfTrue,
9240 IsSubExpr: ControlsOnlyExit,
9241 /*AllowPredicates=*/true);
9242 }
9243
9244 // Check for a constant condition. These are normally stripped out by
9245 // SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
9246 // preserve the CFG and is temporarily leaving constant conditions
9247 // in place.
9248 if (ConstantInt *CI = dyn_cast<ConstantInt>(Val: ExitCond)) {
9249 if (ExitIfTrue == !CI->getZExtValue())
9250 // The backedge is always taken.
9251 return getCouldNotCompute();
9252 // The backedge is never taken.
9253 return getZero(Ty: CI->getType());
9254 }
9255
9256 // If we're exiting based on the overflow flag of an x.with.overflow intrinsic
9257 // with a constant step, we can form an equivalent icmp predicate and figure
9258 // out how many iterations will be taken before we exit.
9259 const WithOverflowInst *WO;
9260 const APInt *C;
9261 if (match(V: ExitCond, P: m_ExtractValue<1>(V: m_WithOverflowInst(I&: WO))) &&
9262 match(V: WO->getRHS(), P: m_APInt(Res&: C))) {
9263 ConstantRange NWR =
9264 ConstantRange::makeExactNoWrapRegion(BinOp: WO->getBinaryOp(), Other: *C,
9265 NoWrapKind: WO->getNoWrapKind());
9266 CmpInst::Predicate Pred;
9267 APInt NewRHSC, Offset;
9268 NWR.getEquivalentICmp(Pred, RHS&: NewRHSC, Offset);
9269 if (!ExitIfTrue)
9270 Pred = ICmpInst::getInversePredicate(pred: Pred);
9271 auto *LHS = getSCEV(V: WO->getLHS());
9272 if (Offset != 0)
9273 LHS = getAddExpr(LHS, RHS: getConstant(Val: Offset));
9274 auto EL = computeExitLimitFromICmp(L, Pred, LHS, RHS: getConstant(Val: NewRHSC),
9275 IsSubExpr: ControlsOnlyExit, AllowPredicates);
9276 if (EL.hasAnyInfo())
9277 return EL;
9278 }
9279
9280 // If it's not an integer or pointer comparison then compute it the hard way.
9281 return computeExitCountExhaustively(L, Cond: ExitCond, ExitWhen: ExitIfTrue);
9282}
9283
9284std::optional<ScalarEvolution::ExitLimit>
9285ScalarEvolution::computeExitLimitFromCondFromBinOp(
9286 ExitLimitCacheTy &Cache, const Loop *L, Value *ExitCond, bool ExitIfTrue,
9287 bool ControlsOnlyExit, bool AllowPredicates) {
9288 // Check if the controlling expression for this loop is an And or Or.
9289 Value *Op0, *Op1;
9290 bool IsAnd = false;
9291 if (match(V: ExitCond, P: m_LogicalAnd(L: m_Value(V&: Op0), R: m_Value(V&: Op1))))
9292 IsAnd = true;
9293 else if (match(V: ExitCond, P: m_LogicalOr(L: m_Value(V&: Op0), R: m_Value(V&: Op1))))
9294 IsAnd = false;
9295 else
9296 return std::nullopt;
9297
9298 // EitherMayExit is true in these two cases:
9299 // br (and Op0 Op1), loop, exit
9300 // br (or Op0 Op1), exit, loop
9301 bool EitherMayExit = IsAnd ^ ExitIfTrue;
9302 ExitLimit EL0 = computeExitLimitFromCondCached(
9303 Cache, L, ExitCond: Op0, ExitIfTrue, ControlsOnlyExit: ControlsOnlyExit && !EitherMayExit,
9304 AllowPredicates);
9305 ExitLimit EL1 = computeExitLimitFromCondCached(
9306 Cache, L, ExitCond: Op1, ExitIfTrue, ControlsOnlyExit: ControlsOnlyExit && !EitherMayExit,
9307 AllowPredicates);
9308
9309 // Be robust against unsimplified IR for the form "op i1 X, NeutralElement"
9310 const Constant *NeutralElement = ConstantInt::get(Ty: ExitCond->getType(), V: IsAnd);
9311 if (isa<ConstantInt>(Val: Op1))
9312 return Op1 == NeutralElement ? EL0 : EL1;
9313 if (isa<ConstantInt>(Val: Op0))
9314 return Op0 == NeutralElement ? EL1 : EL0;
9315
9316 const SCEV *BECount = getCouldNotCompute();
9317 const SCEV *ConstantMaxBECount = getCouldNotCompute();
9318 const SCEV *SymbolicMaxBECount = getCouldNotCompute();
9319 if (EitherMayExit) {
9320 bool UseSequentialUMin = !isa<BinaryOperator>(Val: ExitCond);
9321 // Both conditions must be same for the loop to continue executing.
9322 // Choose the less conservative count.
9323 if (EL0.ExactNotTaken != getCouldNotCompute() &&
9324 EL1.ExactNotTaken != getCouldNotCompute()) {
9325 BECount = getUMinFromMismatchedTypes(LHS: EL0.ExactNotTaken, RHS: EL1.ExactNotTaken,
9326 Sequential: UseSequentialUMin);
9327 }
9328 if (EL0.ConstantMaxNotTaken == getCouldNotCompute())
9329 ConstantMaxBECount = EL1.ConstantMaxNotTaken;
9330 else if (EL1.ConstantMaxNotTaken == getCouldNotCompute())
9331 ConstantMaxBECount = EL0.ConstantMaxNotTaken;
9332 else
9333 ConstantMaxBECount = getUMinFromMismatchedTypes(LHS: EL0.ConstantMaxNotTaken,
9334 RHS: EL1.ConstantMaxNotTaken);
9335 if (EL0.SymbolicMaxNotTaken == getCouldNotCompute())
9336 SymbolicMaxBECount = EL1.SymbolicMaxNotTaken;
9337 else if (EL1.SymbolicMaxNotTaken == getCouldNotCompute())
9338 SymbolicMaxBECount = EL0.SymbolicMaxNotTaken;
9339 else
9340 SymbolicMaxBECount = getUMinFromMismatchedTypes(
9341 LHS: EL0.SymbolicMaxNotTaken, RHS: EL1.SymbolicMaxNotTaken, Sequential: UseSequentialUMin);
9342 } else {
9343 // Both conditions must be same at the same time for the loop to exit.
9344 // For now, be conservative.
9345 if (EL0.ExactNotTaken == EL1.ExactNotTaken)
9346 BECount = EL0.ExactNotTaken;
9347 }
9348
9349 // There are cases (e.g. PR26207) where computeExitLimitFromCond is able
9350 // to be more aggressive when computing BECount than when computing
9351 // ConstantMaxBECount. In these cases it is possible for EL0.ExactNotTaken
9352 // and
9353 // EL1.ExactNotTaken to match, but for EL0.ConstantMaxNotTaken and
9354 // EL1.ConstantMaxNotTaken to not.
9355 if (isa<SCEVCouldNotCompute>(Val: ConstantMaxBECount) &&
9356 !isa<SCEVCouldNotCompute>(Val: BECount))
9357 ConstantMaxBECount = getConstant(Val: getUnsignedRangeMax(S: BECount));
9358 if (isa<SCEVCouldNotCompute>(Val: SymbolicMaxBECount))
9359 SymbolicMaxBECount =
9360 isa<SCEVCouldNotCompute>(Val: BECount) ? ConstantMaxBECount : BECount;
9361 return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false,
9362 {ArrayRef(EL0.Predicates), ArrayRef(EL1.Predicates)});
9363}
9364
9365ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
9366 const Loop *L, ICmpInst *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit,
9367 bool AllowPredicates) {
9368 // If the condition was exit on true, convert the condition to exit on false
9369 CmpPredicate Pred;
9370 if (!ExitIfTrue)
9371 Pred = ExitCond->getCmpPredicate();
9372 else
9373 Pred = ExitCond->getInverseCmpPredicate();
9374 const ICmpInst::Predicate OriginalPred = Pred;
9375
9376 const SCEV *LHS = getSCEV(V: ExitCond->getOperand(i_nocapture: 0));
9377 const SCEV *RHS = getSCEV(V: ExitCond->getOperand(i_nocapture: 1));
9378
9379 ExitLimit EL = computeExitLimitFromICmp(L, Pred, LHS, RHS, IsSubExpr: ControlsOnlyExit,
9380 AllowPredicates);
9381 if (EL.hasAnyInfo())
9382 return EL;
9383
9384 auto *ExhaustiveCount =
9385 computeExitCountExhaustively(L, Cond: ExitCond, ExitWhen: ExitIfTrue);
9386
9387 if (!isa<SCEVCouldNotCompute>(Val: ExhaustiveCount))
9388 return ExhaustiveCount;
9389
9390 return computeShiftCompareExitLimit(LHS: ExitCond->getOperand(i_nocapture: 0),
9391 RHS: ExitCond->getOperand(i_nocapture: 1), L, Pred: OriginalPred);
9392}
9393ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
9394 const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS,
9395 bool ControlsOnlyExit, bool AllowPredicates) {
9396
9397 // Try to evaluate any dependencies out of the loop.
9398 LHS = getSCEVAtScope(S: LHS, L);
9399 RHS = getSCEVAtScope(S: RHS, L);
9400
9401 // At this point, we would like to compute how many iterations of the
9402 // loop the predicate will return true for these inputs.
9403 if (isLoopInvariant(S: LHS, L) && !isLoopInvariant(S: RHS, L)) {
9404 // If there is a loop-invariant, force it into the RHS.
9405 std::swap(a&: LHS, b&: RHS);
9406 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
9407 }
9408
9409 bool ControllingFiniteLoop = ControlsOnlyExit && loopHasNoAbnormalExits(L) &&
9410 loopIsFiniteByAssumption(L);
9411 // Simplify the operands before analyzing them.
9412 (void)SimplifyICmpOperands(Pred, LHS, RHS, /*Depth=*/0);
9413
9414 // If we have a comparison of a chrec against a constant, try to use value
9415 // ranges to answer this query.
9416 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Val: RHS))
9417 if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Val: LHS))
9418 if (AddRec->getLoop() == L) {
9419 // Form the constant range.
9420 ConstantRange CompRange =
9421 ConstantRange::makeExactICmpRegion(Pred, Other: RHSC->getAPInt());
9422
9423 const SCEV *Ret = AddRec->getNumIterationsInRange(Range: CompRange, SE&: *this);
9424 if (!isa<SCEVCouldNotCompute>(Val: Ret)) return Ret;
9425 }
9426
9427 // If this loop must exit based on this condition (or execute undefined
9428 // behaviour), see if we can improve wrap flags. This is essentially
9429 // a must execute style proof.
9430 if (ControllingFiniteLoop && isLoopInvariant(S: RHS, L)) {
9431 // If we can prove the test sequence produced must repeat the same values
9432 // on self-wrap of the IV, then we can infer that IV doesn't self wrap
9433 // because if it did, we'd have an infinite (undefined) loop.
9434 // TODO: We can peel off any functions which are invertible *in L*. Loop
9435 // invariant terms are effectively constants for our purposes here.
9436 auto *InnerLHS = LHS;
9437 if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(Val: LHS))
9438 InnerLHS = ZExt->getOperand();
9439 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: InnerLHS);
9440 AR && !AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() &&
9441 isKnownToBeAPowerOfTwo(S: AR->getStepRecurrence(SE&: *this), /*OrZero=*/true,
9442 /*OrNegative=*/true)) {
9443 auto Flags = AR->getNoWrapFlags();
9444 Flags = setFlags(Flags, OnFlags: SCEV::FlagNW);
9445 SmallVector<SCEVUse> Operands{AR->operands()};
9446 Flags = StrengthenNoWrapFlags(SE: this, Type: scAddRecExpr, Ops: Operands, Flags);
9447 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags);
9448 }
9449
9450 // For a slt/ult condition with a positive step, can we prove nsw/nuw?
9451 // From no-self-wrap, this follows trivially from the fact that every
9452 // (un)signed-wrapped, but not self-wrapped value must be LT than the
9453 // last value before (un)signed wrap. Since we know that last value
9454 // didn't exit, nor will any smaller one.
9455 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_ULT) {
9456 auto WrapType = Pred == ICmpInst::ICMP_SLT ? SCEV::FlagNSW : SCEV::FlagNUW;
9457 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: LHS);
9458 AR && AR->getLoop() == L && AR->isAffine() &&
9459 !AR->getNoWrapFlags(Mask: WrapType) && AR->hasNoSelfWrap() &&
9460 isKnownPositive(S: AR->getStepRecurrence(SE&: *this))) {
9461 auto Flags = AR->getNoWrapFlags();
9462 Flags = setFlags(Flags, OnFlags: WrapType);
9463 SmallVector<SCEVUse> Operands{AR->operands()};
9464 Flags = StrengthenNoWrapFlags(SE: this, Type: scAddRecExpr, Ops: Operands, Flags);
9465 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags);
9466 }
9467 }
9468 }
9469
9470 switch (Pred) {
9471 case ICmpInst::ICMP_NE: { // while (X != Y)
9472 // Convert to: while (X-Y != 0)
9473 if (LHS->getType()->isPointerTy()) {
9474 LHS = getLosslessPtrToIntExpr(Op: LHS);
9475 if (isa<SCEVCouldNotCompute>(Val: LHS))
9476 return LHS;
9477 }
9478 if (RHS->getType()->isPointerTy()) {
9479 RHS = getLosslessPtrToIntExpr(Op: RHS);
9480 if (isa<SCEVCouldNotCompute>(Val: RHS))
9481 return RHS;
9482 }
9483 ExitLimit EL = howFarToZero(V: getMinusSCEV(LHS, RHS), L, IsSubExpr: ControlsOnlyExit,
9484 AllowPredicates);
9485 if (EL.hasAnyInfo())
9486 return EL;
9487 break;
9488 }
9489 case ICmpInst::ICMP_EQ: { // while (X == Y)
9490 // Convert to: while (X-Y == 0)
9491 if (LHS->getType()->isPointerTy()) {
9492 LHS = getLosslessPtrToIntExpr(Op: LHS);
9493 if (isa<SCEVCouldNotCompute>(Val: LHS))
9494 return LHS;
9495 }
9496 if (RHS->getType()->isPointerTy()) {
9497 RHS = getLosslessPtrToIntExpr(Op: RHS);
9498 if (isa<SCEVCouldNotCompute>(Val: RHS))
9499 return RHS;
9500 }
9501 ExitLimit EL = howFarToNonZero(V: getMinusSCEV(LHS, RHS), L);
9502 if (EL.hasAnyInfo()) return EL;
9503 break;
9504 }
9505 case ICmpInst::ICMP_SLE:
9506 case ICmpInst::ICMP_ULE:
9507 // Since the loop is finite, an invariant RHS cannot include the boundary
9508 // value, otherwise it would loop forever.
9509 if (!EnableFiniteLoopControl || !ControllingFiniteLoop ||
9510 !isLoopInvariant(S: RHS, L)) {
9511 // Otherwise, perform the addition in a wider type, to avoid overflow.
9512 // If the LHS is an addrec with the appropriate nowrap flag, the
9513 // extension will be sunk into it and the exit count can be analyzed.
9514 auto *OldType = dyn_cast<IntegerType>(Val: LHS->getType());
9515 if (!OldType)
9516 break;
9517 // Prefer doubling the bitwidth over adding a single bit to make it more
9518 // likely that we use a legal type.
9519 auto *NewType =
9520 Type::getIntNTy(C&: OldType->getContext(), N: OldType->getBitWidth() * 2);
9521 if (ICmpInst::isSigned(Pred)) {
9522 LHS = getSignExtendExpr(Op: LHS, Ty: NewType);
9523 RHS = getSignExtendExpr(Op: RHS, Ty: NewType);
9524 } else {
9525 LHS = getZeroExtendExpr(Op: LHS, Ty: NewType);
9526 RHS = getZeroExtendExpr(Op: RHS, Ty: NewType);
9527 }
9528 }
9529 RHS = getAddExpr(LHS: getOne(Ty: RHS->getType()), RHS);
9530 [[fallthrough]];
9531 case ICmpInst::ICMP_SLT:
9532 case ICmpInst::ICMP_ULT: { // while (X < Y)
9533 bool IsSigned = ICmpInst::isSigned(Pred);
9534 ExitLimit EL = howManyLessThans(LHS, RHS, L, isSigned: IsSigned, ControlsOnlyExit,
9535 AllowPredicates);
9536 if (EL.hasAnyInfo())
9537 return EL;
9538 break;
9539 }
9540 case ICmpInst::ICMP_SGE:
9541 case ICmpInst::ICMP_UGE:
9542 // Since the loop is finite, an invariant RHS cannot include the boundary
9543 // value, otherwise it would loop forever.
9544 if (!EnableFiniteLoopControl || !ControllingFiniteLoop ||
9545 !isLoopInvariant(S: RHS, L))
9546 break;
9547 RHS = getAddExpr(LHS: getMinusOne(Ty: RHS->getType()), RHS);
9548 [[fallthrough]];
9549 case ICmpInst::ICMP_SGT:
9550 case ICmpInst::ICMP_UGT: { // while (X > Y)
9551 bool IsSigned = ICmpInst::isSigned(Pred);
9552 ExitLimit EL = howManyGreaterThans(LHS, RHS, L, isSigned: IsSigned, IsSubExpr: ControlsOnlyExit,
9553 AllowPredicates);
9554 if (EL.hasAnyInfo())
9555 return EL;
9556 break;
9557 }
9558 default:
9559 break;
9560 }
9561
9562 return getCouldNotCompute();
9563}
9564
9565ScalarEvolution::ExitLimit
9566ScalarEvolution::computeExitLimitFromSingleExitSwitch(const Loop *L,
9567 SwitchInst *Switch,
9568 BasicBlock *ExitingBlock,
9569 bool ControlsOnlyExit) {
9570 assert(!L->contains(ExitingBlock) && "Not an exiting block!");
9571
9572 // Give up if the exit is the default dest of a switch.
9573 if (Switch->getDefaultDest() == ExitingBlock)
9574 return getCouldNotCompute();
9575
9576 assert(L->contains(Switch->getDefaultDest()) &&
9577 "Default case must not exit the loop!");
9578 const SCEV *LHS = getSCEVAtScope(V: Switch->getCondition(), L);
9579 const SCEV *RHS = getConstant(V: Switch->findCaseDest(BB: ExitingBlock));
9580
9581 // while (X != Y) --> while (X-Y != 0)
9582 ExitLimit EL = howFarToZero(V: getMinusSCEV(LHS, RHS), L, IsSubExpr: ControlsOnlyExit);
9583 if (EL.hasAnyInfo())
9584 return EL;
9585
9586 return getCouldNotCompute();
9587}
9588
9589static ConstantInt *
9590EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C,
9591 ScalarEvolution &SE) {
9592 const SCEV *InVal = SE.getConstant(V: C);
9593 const SCEV *Val = AddRec->evaluateAtIteration(It: InVal, SE);
9594 assert(isa<SCEVConstant>(Val) &&
9595 "Evaluation of SCEV at constant didn't fold correctly?");
9596 return cast<SCEVConstant>(Val)->getValue();
9597}
9598
9599ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit(
9600 Value *LHS, Value *RHSV, const Loop *L, ICmpInst::Predicate Pred) {
9601 ConstantInt *RHS = dyn_cast<ConstantInt>(Val: RHSV);
9602 if (!RHS)
9603 return getCouldNotCompute();
9604
9605 const BasicBlock *Latch = L->getLoopLatch();
9606 if (!Latch)
9607 return getCouldNotCompute();
9608
9609 const BasicBlock *Predecessor = L->getLoopPredecessor();
9610 if (!Predecessor)
9611 return getCouldNotCompute();
9612
9613 // Return true if V is of the form "LHS `shift_op` <positive constant>".
9614 // Return LHS in OutLHS and shift_opt in OutOpCode.
9615 auto MatchPositiveShift =
9616 [](Value *V, Value *&OutLHS, Instruction::BinaryOps &OutOpCode) {
9617
9618 using namespace PatternMatch;
9619
9620 ConstantInt *ShiftAmt;
9621 if (match(V, P: m_LShr(L: m_Value(V&: OutLHS), R: m_ConstantInt(CI&: ShiftAmt))))
9622 OutOpCode = Instruction::LShr;
9623 else if (match(V, P: m_AShr(L: m_Value(V&: OutLHS), R: m_ConstantInt(CI&: ShiftAmt))))
9624 OutOpCode = Instruction::AShr;
9625 else if (match(V, P: m_Shl(L: m_Value(V&: OutLHS), R: m_ConstantInt(CI&: ShiftAmt))))
9626 OutOpCode = Instruction::Shl;
9627 else
9628 return false;
9629
9630 return ShiftAmt->getValue().isStrictlyPositive();
9631 };
9632
9633 // Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in
9634 //
9635 // loop:
9636 // %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ]
9637 // %iv.shifted = lshr i32 %iv, <positive constant>
9638 //
9639 // Return true on a successful match. Return the corresponding PHI node (%iv
9640 // above) in PNOut and the opcode of the shift operation in OpCodeOut.
9641 auto MatchShiftRecurrence =
9642 [&](Value *V, PHINode *&PNOut, Instruction::BinaryOps &OpCodeOut) {
9643 std::optional<Instruction::BinaryOps> PostShiftOpCode;
9644
9645 {
9646 Instruction::BinaryOps OpC;
9647 Value *V;
9648
9649 // If we encounter a shift instruction, "peel off" the shift operation,
9650 // and remember that we did so. Later when we inspect %iv's backedge
9651 // value, we will make sure that the backedge value uses the same
9652 // operation.
9653 //
9654 // Note: the peeled shift operation does not have to be the same
9655 // instruction as the one feeding into the PHI's backedge value. We only
9656 // really care about it being the same *kind* of shift instruction --
9657 // that's all that is required for our later inferences to hold.
9658 if (MatchPositiveShift(LHS, V, OpC)) {
9659 PostShiftOpCode = OpC;
9660 LHS = V;
9661 }
9662 }
9663
9664 PNOut = dyn_cast<PHINode>(Val: LHS);
9665 if (!PNOut || PNOut->getParent() != L->getHeader())
9666 return false;
9667
9668 Value *BEValue = PNOut->getIncomingValueForBlock(BB: Latch);
9669 Value *OpLHS;
9670
9671 return
9672 // The backedge value for the PHI node must be a shift by a positive
9673 // amount
9674 MatchPositiveShift(BEValue, OpLHS, OpCodeOut) &&
9675
9676 // of the PHI node itself
9677 OpLHS == PNOut &&
9678
9679 // and the kind of shift should be match the kind of shift we peeled
9680 // off, if any.
9681 (!PostShiftOpCode || *PostShiftOpCode == OpCodeOut);
9682 };
9683
9684 PHINode *PN;
9685 Instruction::BinaryOps OpCode;
9686 if (!MatchShiftRecurrence(LHS, PN, OpCode))
9687 return getCouldNotCompute();
9688
9689 const DataLayout &DL = getDataLayout();
9690
9691 // The key rationale for this optimization is that for some kinds of shift
9692 // recurrences, the value of the recurrence "stabilizes" to either 0 or -1
9693 // within a finite number of iterations. If the condition guarding the
9694 // backedge (in the sense that the backedge is taken if the condition is true)
9695 // is false for the value the shift recurrence stabilizes to, then we know
9696 // that the backedge is taken only a finite number of times.
9697
9698 ConstantInt *StableValue = nullptr;
9699 switch (OpCode) {
9700 default:
9701 llvm_unreachable("Impossible case!");
9702
9703 case Instruction::AShr: {
9704 // {K,ashr,<positive-constant>} stabilizes to signum(K) in at most
9705 // bitwidth(K) iterations.
9706 Value *FirstValue = PN->getIncomingValueForBlock(BB: Predecessor);
9707 KnownBits Known = computeKnownBits(V: FirstValue, DL, AC: &AC,
9708 CxtI: Predecessor->getTerminator(), DT: &DT);
9709 auto *Ty = cast<IntegerType>(Val: RHS->getType());
9710 if (Known.isNonNegative())
9711 StableValue = ConstantInt::get(Ty, V: 0);
9712 else if (Known.isNegative())
9713 StableValue = ConstantInt::get(Ty, V: -1, IsSigned: true);
9714 else
9715 return getCouldNotCompute();
9716
9717 break;
9718 }
9719 case Instruction::LShr:
9720 case Instruction::Shl:
9721 // Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>}
9722 // stabilize to 0 in at most bitwidth(K) iterations.
9723 StableValue = ConstantInt::get(Ty: cast<IntegerType>(Val: RHS->getType()), V: 0);
9724 break;
9725 }
9726
9727 auto *Result =
9728 ConstantFoldCompareInstOperands(Predicate: Pred, LHS: StableValue, RHS, DL, TLI: &TLI);
9729 assert(Result->getType()->isIntegerTy(1) &&
9730 "Otherwise cannot be an operand to a branch instruction");
9731
9732 if (Result->isNullValue()) {
9733 unsigned BitWidth = getTypeSizeInBits(Ty: RHS->getType());
9734 const SCEV *UpperBound =
9735 getConstant(Ty: getEffectiveSCEVType(Ty: RHS->getType()), V: BitWidth);
9736 return ExitLimit(getCouldNotCompute(), UpperBound, UpperBound, false);
9737 }
9738
9739 return getCouldNotCompute();
9740}
9741
9742/// Return true if we can constant fold an instruction of the specified type,
9743/// assuming that all operands were constants.
9744static bool CanConstantFold(const Instruction *I) {
9745 if (isa<BinaryOperator>(Val: I) || isa<CmpInst>(Val: I) ||
9746 isa<SelectInst>(Val: I) || isa<CastInst>(Val: I) || isa<GetElementPtrInst>(Val: I) ||
9747 isa<LoadInst>(Val: I) || isa<ExtractValueInst>(Val: I))
9748 return true;
9749
9750 if (const CallInst *CI = dyn_cast<CallInst>(Val: I))
9751 if (const Function *F = CI->getCalledFunction())
9752 return canConstantFoldCallTo(Call: CI, F);
9753 return false;
9754}
9755
9756/// Determine whether this instruction can constant evolve within this loop
9757/// assuming its operands can all constant evolve.
9758static bool canConstantEvolve(Instruction *I, const Loop *L) {
9759 // An instruction outside of the loop can't be derived from a loop PHI.
9760 if (!L->contains(Inst: I)) return false;
9761
9762 if (isa<PHINode>(Val: I)) {
9763 // We don't currently keep track of the control flow needed to evaluate
9764 // PHIs, so we cannot handle PHIs inside of loops.
9765 return L->getHeader() == I->getParent();
9766 }
9767
9768 // If we won't be able to constant fold this expression even if the operands
9769 // are constants, bail early.
9770 return CanConstantFold(I);
9771}
9772
9773/// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
9774/// recursing through each instruction operand until reaching a loop header phi.
9775static PHINode *
9776getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L,
9777 DenseMap<Instruction *, PHINode *> &PHIMap,
9778 unsigned Depth) {
9779 if (Depth > MaxConstantEvolvingDepth)
9780 return nullptr;
9781
9782 // Otherwise, we can evaluate this instruction if all of its operands are
9783 // constant or derived from a PHI node themselves.
9784 PHINode *PHI = nullptr;
9785 for (Value *Op : UseInst->operands()) {
9786 if (isa<Constant>(Val: Op)) continue;
9787
9788 Instruction *OpInst = dyn_cast<Instruction>(Val: Op);
9789 if (!OpInst || !canConstantEvolve(I: OpInst, L)) return nullptr;
9790
9791 PHINode *P = dyn_cast<PHINode>(Val: OpInst);
9792 if (!P)
9793 // If this operand is already visited, reuse the prior result.
9794 // We may have P != PHI if this is the deepest point at which the
9795 // inconsistent paths meet.
9796 P = PHIMap.lookup(Val: OpInst);
9797 if (!P) {
9798 // Recurse and memoize the results, whether a phi is found or not.
9799 // This recursive call invalidates pointers into PHIMap.
9800 P = getConstantEvolvingPHIOperands(UseInst: OpInst, L, PHIMap, Depth: Depth + 1);
9801 PHIMap[OpInst] = P;
9802 }
9803 if (!P)
9804 return nullptr; // Not evolving from PHI
9805 if (PHI && PHI != P)
9806 return nullptr; // Evolving from multiple different PHIs.
9807 PHI = P;
9808 }
9809 // This is a expression evolving from a constant PHI!
9810 return PHI;
9811}
9812
9813/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
9814/// in the loop that V is derived from. We allow arbitrary operations along the
9815/// way, but the operands of an operation must either be constants or a value
9816/// derived from a constant PHI. If this expression does not fit with these
9817/// constraints, return null.
9818static PHINode *getConstantEvolvingPHI(Value *V, const Loop *L) {
9819 Instruction *I = dyn_cast<Instruction>(Val: V);
9820 if (!I || !canConstantEvolve(I, L)) return nullptr;
9821
9822 if (PHINode *PN = dyn_cast<PHINode>(Val: I))
9823 return PN;
9824
9825 // Record non-constant instructions contained by the loop.
9826 DenseMap<Instruction *, PHINode *> PHIMap;
9827 return getConstantEvolvingPHIOperands(UseInst: I, L, PHIMap, Depth: 0);
9828}
9829
9830/// EvaluateExpression - Given an expression that passes the
9831/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
9832/// in the loop has the value PHIVal. If we can't fold this expression for some
9833/// reason, return null.
9834static Constant *EvaluateExpression(Value *V, const Loop *L,
9835 DenseMap<Instruction *, Constant *> &Vals,
9836 const DataLayout &DL,
9837 const TargetLibraryInfo *TLI) {
9838 // Convenient constant check, but redundant for recursive calls.
9839 if (Constant *C = dyn_cast<Constant>(Val: V)) return C;
9840 Instruction *I = dyn_cast<Instruction>(Val: V);
9841 if (!I) return nullptr;
9842
9843 if (Constant *C = Vals.lookup(Val: I)) return C;
9844
9845 // An instruction inside the loop depends on a value outside the loop that we
9846 // weren't given a mapping for, or a value such as a call inside the loop.
9847 if (!canConstantEvolve(I, L)) return nullptr;
9848
9849 // An unmapped PHI can be due to a branch or another loop inside this loop,
9850 // or due to this not being the initial iteration through a loop where we
9851 // couldn't compute the evolution of this particular PHI last time.
9852 if (isa<PHINode>(Val: I)) return nullptr;
9853
9854 std::vector<Constant*> Operands(I->getNumOperands());
9855
9856 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
9857 Instruction *Operand = dyn_cast<Instruction>(Val: I->getOperand(i));
9858 if (!Operand) {
9859 Operands[i] = dyn_cast<Constant>(Val: I->getOperand(i));
9860 if (!Operands[i]) return nullptr;
9861 continue;
9862 }
9863 Constant *C = EvaluateExpression(V: Operand, L, Vals, DL, TLI);
9864 Vals[Operand] = C;
9865 if (!C) return nullptr;
9866 Operands[i] = C;
9867 }
9868
9869 return ConstantFoldInstOperands(I, Ops: Operands, DL, TLI,
9870 /*AllowNonDeterministic=*/false);
9871}
9872
9873
9874// If every incoming value to PN except the one for BB is a specific Constant,
9875// return that, else return nullptr.
9876static Constant *getOtherIncomingValue(PHINode *PN, BasicBlock *BB) {
9877 Constant *IncomingVal = nullptr;
9878
9879 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
9880 if (PN->getIncomingBlock(i) == BB)
9881 continue;
9882
9883 auto *CurrentVal = dyn_cast<Constant>(Val: PN->getIncomingValue(i));
9884 if (!CurrentVal)
9885 return nullptr;
9886
9887 if (IncomingVal != CurrentVal) {
9888 if (IncomingVal)
9889 return nullptr;
9890 IncomingVal = CurrentVal;
9891 }
9892 }
9893
9894 return IncomingVal;
9895}
9896
9897/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
9898/// in the header of its containing loop, we know the loop executes a
9899/// constant number of times, and the PHI node is just a recurrence
9900/// involving constants, fold it.
9901Constant *
9902ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
9903 const APInt &BEs,
9904 const Loop *L) {
9905 auto [I, Inserted] = ConstantEvolutionLoopExitValue.try_emplace(Key: PN);
9906 if (!Inserted)
9907 return I->second;
9908
9909 if (BEs.ugt(RHS: MaxBruteForceIterations))
9910 return nullptr; // Not going to evaluate it.
9911
9912 Constant *&RetVal = I->second;
9913
9914 DenseMap<Instruction *, Constant *> CurrentIterVals;
9915 BasicBlock *Header = L->getHeader();
9916 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
9917
9918 BasicBlock *Latch = L->getLoopLatch();
9919 if (!Latch)
9920 return nullptr;
9921
9922 for (PHINode &PHI : Header->phis()) {
9923 if (auto *StartCST = getOtherIncomingValue(PN: &PHI, BB: Latch))
9924 CurrentIterVals[&PHI] = StartCST;
9925 }
9926 if (!CurrentIterVals.count(Val: PN))
9927 return RetVal = nullptr;
9928
9929 Value *BEValue = PN->getIncomingValueForBlock(BB: Latch);
9930
9931 // Execute the loop symbolically to determine the exit value.
9932 assert(BEs.getActiveBits() < CHAR_BIT * sizeof(unsigned) &&
9933 "BEs is <= MaxBruteForceIterations which is an 'unsigned'!");
9934
9935 unsigned NumIterations = BEs.getZExtValue(); // must be in range
9936 unsigned IterationNum = 0;
9937 const DataLayout &DL = getDataLayout();
9938 for (; ; ++IterationNum) {
9939 if (IterationNum == NumIterations)
9940 return RetVal = CurrentIterVals[PN]; // Got exit value!
9941
9942 // Compute the value of the PHIs for the next iteration.
9943 // EvaluateExpression adds non-phi values to the CurrentIterVals map.
9944 DenseMap<Instruction *, Constant *> NextIterVals;
9945 Constant *NextPHI =
9946 EvaluateExpression(V: BEValue, L, Vals&: CurrentIterVals, DL, TLI: &TLI);
9947 if (!NextPHI)
9948 return nullptr; // Couldn't evaluate!
9949 NextIterVals[PN] = NextPHI;
9950
9951 bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
9952
9953 // Also evaluate the other PHI nodes. However, we don't get to stop if we
9954 // cease to be able to evaluate one of them or if they stop evolving,
9955 // because that doesn't necessarily prevent us from computing PN.
9956 SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute;
9957 for (const auto &I : CurrentIterVals) {
9958 PHINode *PHI = dyn_cast<PHINode>(Val: I.first);
9959 if (!PHI || PHI == PN || PHI->getParent() != Header) continue;
9960 PHIsToCompute.emplace_back(Args&: PHI, Args: I.second);
9961 }
9962 // We use two distinct loops because EvaluateExpression may invalidate any
9963 // iterators into CurrentIterVals.
9964 for (const auto &I : PHIsToCompute) {
9965 PHINode *PHI = I.first;
9966 Constant *&NextPHI = NextIterVals[PHI];
9967 if (!NextPHI) { // Not already computed.
9968 Value *BEValue = PHI->getIncomingValueForBlock(BB: Latch);
9969 NextPHI = EvaluateExpression(V: BEValue, L, Vals&: CurrentIterVals, DL, TLI: &TLI);
9970 }
9971 if (NextPHI != I.second)
9972 StoppedEvolving = false;
9973 }
9974
9975 // If all entries in CurrentIterVals == NextIterVals then we can stop
9976 // iterating, the loop can't continue to change.
9977 if (StoppedEvolving)
9978 return RetVal = CurrentIterVals[PN];
9979
9980 CurrentIterVals.swap(RHS&: NextIterVals);
9981 }
9982}
9983
9984const SCEV *ScalarEvolution::computeExitCountExhaustively(const Loop *L,
9985 Value *Cond,
9986 bool ExitWhen) {
9987 PHINode *PN = getConstantEvolvingPHI(V: Cond, L);
9988 if (!PN) return getCouldNotCompute();
9989
9990 // If the loop is canonicalized, the PHI will have exactly two entries.
9991 // That's the only form we support here.
9992 if (PN->getNumIncomingValues() != 2) return getCouldNotCompute();
9993
9994 DenseMap<Instruction *, Constant *> CurrentIterVals;
9995 BasicBlock *Header = L->getHeader();
9996 assert(PN->getParent() == Header && "Can't evaluate PHI not in loop header!");
9997
9998 BasicBlock *Latch = L->getLoopLatch();
9999 assert(Latch && "Should follow from NumIncomingValues == 2!");
10000
10001 for (PHINode &PHI : Header->phis()) {
10002 if (auto *StartCST = getOtherIncomingValue(PN: &PHI, BB: Latch))
10003 CurrentIterVals[&PHI] = StartCST;
10004 }
10005 if (!CurrentIterVals.count(Val: PN))
10006 return getCouldNotCompute();
10007
10008 // Okay, we find a PHI node that defines the trip count of this loop. Execute
10009 // the loop symbolically to determine when the condition gets a value of
10010 // "ExitWhen".
10011 unsigned MaxIterations = MaxBruteForceIterations; // Limit analysis.
10012 const DataLayout &DL = getDataLayout();
10013 for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
10014 auto *CondVal = dyn_cast_or_null<ConstantInt>(
10015 Val: EvaluateExpression(V: Cond, L, Vals&: CurrentIterVals, DL, TLI: &TLI));
10016
10017 // Couldn't symbolically evaluate.
10018 if (!CondVal) return getCouldNotCompute();
10019
10020 if (CondVal->getValue() == uint64_t(ExitWhen)) {
10021 ++NumBruteForceTripCountsComputed;
10022 return getConstant(Ty: Type::getInt32Ty(C&: getContext()), V: IterationNum);
10023 }
10024
10025 // Update all the PHI nodes for the next iteration.
10026 DenseMap<Instruction *, Constant *> NextIterVals;
10027
10028 // Create a list of which PHIs we need to compute. We want to do this before
10029 // calling EvaluateExpression on them because that may invalidate iterators
10030 // into CurrentIterVals.
10031 SmallVector<PHINode *, 8> PHIsToCompute;
10032 for (const auto &I : CurrentIterVals) {
10033 PHINode *PHI = dyn_cast<PHINode>(Val: I.first);
10034 if (!PHI || PHI->getParent() != Header) continue;
10035 PHIsToCompute.push_back(Elt: PHI);
10036 }
10037 for (PHINode *PHI : PHIsToCompute) {
10038 Constant *&NextPHI = NextIterVals[PHI];
10039 if (NextPHI) continue; // Already computed!
10040
10041 Value *BEValue = PHI->getIncomingValueForBlock(BB: Latch);
10042 NextPHI = EvaluateExpression(V: BEValue, L, Vals&: CurrentIterVals, DL, TLI: &TLI);
10043 }
10044 CurrentIterVals.swap(RHS&: NextIterVals);
10045 }
10046
10047 // Too many iterations were needed to evaluate.
10048 return getCouldNotCompute();
10049}
10050
10051const SCEV *ScalarEvolution::getSCEVAtScope(const SCEV *V, const Loop *L) {
10052 SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values =
10053 ValuesAtScopes[V];
10054 // Check to see if we've folded this expression at this loop before.
10055 for (auto &LS : Values)
10056 if (LS.first == L)
10057 return LS.second ? LS.second : V;
10058
10059 Values.emplace_back(Args&: L, Args: nullptr);
10060
10061 // Otherwise compute it.
10062 const SCEV *C = computeSCEVAtScope(S: V, L);
10063 for (auto &LS : reverse(C&: ValuesAtScopes[V]))
10064 if (LS.first == L) {
10065 LS.second = C;
10066 if (!isa<SCEVConstant>(Val: C))
10067 ValuesAtScopesUsers[C].push_back(Elt: {L, V});
10068 break;
10069 }
10070 return C;
10071}
10072
10073/// This builds up a Constant using the ConstantExpr interface. That way, we
10074/// will return Constants for objects which aren't represented by a
10075/// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
10076/// Returns NULL if the SCEV isn't representable as a Constant.
10077static Constant *BuildConstantFromSCEV(const SCEV *V) {
10078 switch (V->getSCEVType()) {
10079 case scCouldNotCompute:
10080 case scAddRecExpr:
10081 case scVScale:
10082 return nullptr;
10083 case scConstant:
10084 return cast<SCEVConstant>(Val: V)->getValue();
10085 case scUnknown:
10086 return dyn_cast<Constant>(Val: cast<SCEVUnknown>(Val: V)->getValue());
10087 case scPtrToAddr: {
10088 const SCEVPtrToAddrExpr *P2I = cast<SCEVPtrToAddrExpr>(Val: V);
10089 if (Constant *CastOp = BuildConstantFromSCEV(V: P2I->getOperand()))
10090 return ConstantExpr::getPtrToAddr(C: CastOp, Ty: P2I->getType());
10091
10092 return nullptr;
10093 }
10094 case scPtrToInt: {
10095 const SCEVPtrToIntExpr *P2I = cast<SCEVPtrToIntExpr>(Val: V);
10096 if (Constant *CastOp = BuildConstantFromSCEV(V: P2I->getOperand()))
10097 return ConstantExpr::getPtrToInt(C: CastOp, Ty: P2I->getType());
10098
10099 return nullptr;
10100 }
10101 case scTruncate: {
10102 const SCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(Val: V);
10103 if (Constant *CastOp = BuildConstantFromSCEV(V: ST->getOperand()))
10104 return ConstantExpr::getTrunc(C: CastOp, Ty: ST->getType());
10105 return nullptr;
10106 }
10107 case scAddExpr: {
10108 const SCEVAddExpr *SA = cast<SCEVAddExpr>(Val: V);
10109 Constant *C = nullptr;
10110 for (const SCEV *Op : SA->operands()) {
10111 Constant *OpC = BuildConstantFromSCEV(V: Op);
10112 if (!OpC)
10113 return nullptr;
10114 if (!C) {
10115 C = OpC;
10116 continue;
10117 }
10118 assert(!C->getType()->isPointerTy() &&
10119 "Can only have one pointer, and it must be last");
10120 if (OpC->getType()->isPointerTy()) {
10121 // The offsets have been converted to bytes. We can add bytes using
10122 // an i8 GEP.
10123 C = ConstantExpr::getPtrAdd(Ptr: OpC, Offset: C);
10124 } else {
10125 C = ConstantExpr::getAdd(C1: C, C2: OpC);
10126 }
10127 }
10128 return C;
10129 }
10130 case scMulExpr:
10131 case scSignExtend:
10132 case scZeroExtend:
10133 case scUDivExpr:
10134 case scSMaxExpr:
10135 case scUMaxExpr:
10136 case scSMinExpr:
10137 case scUMinExpr:
10138 case scSequentialUMinExpr:
10139 return nullptr;
10140 }
10141 llvm_unreachable("Unknown SCEV kind!");
10142}
10143
10144const SCEV *ScalarEvolution::getWithOperands(const SCEV *S,
10145 SmallVectorImpl<SCEVUse> &NewOps) {
10146 switch (S->getSCEVType()) {
10147 case scTruncate:
10148 case scZeroExtend:
10149 case scSignExtend:
10150 case scPtrToAddr:
10151 case scPtrToInt:
10152 return getCastExpr(Kind: S->getSCEVType(), Op: NewOps[0], Ty: S->getType());
10153 case scAddRecExpr: {
10154 auto *AddRec = cast<SCEVAddRecExpr>(Val: S);
10155 return getAddRecExpr(Operands&: NewOps, L: AddRec->getLoop(), Flags: AddRec->getNoWrapFlags());
10156 }
10157 case scAddExpr:
10158 return getAddExpr(Ops&: NewOps, OrigFlags: cast<SCEVAddExpr>(Val: S)->getNoWrapFlags());
10159 case scMulExpr:
10160 return getMulExpr(Ops&: NewOps, OrigFlags: cast<SCEVMulExpr>(Val: S)->getNoWrapFlags());
10161 case scUDivExpr:
10162 return getUDivExpr(LHS: NewOps[0], RHS: NewOps[1]);
10163 case scUMaxExpr:
10164 case scSMaxExpr:
10165 case scUMinExpr:
10166 case scSMinExpr:
10167 return getMinMaxExpr(Kind: S->getSCEVType(), Ops&: NewOps);
10168 case scSequentialUMinExpr:
10169 return getSequentialMinMaxExpr(Kind: S->getSCEVType(), Ops&: NewOps);
10170 case scConstant:
10171 case scVScale:
10172 case scUnknown:
10173 return S;
10174 case scCouldNotCompute:
10175 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
10176 }
10177 llvm_unreachable("Unknown SCEV kind!");
10178}
10179
10180const SCEV *ScalarEvolution::computeSCEVAtScope(const SCEV *V, const Loop *L) {
10181 switch (V->getSCEVType()) {
10182 case scConstant:
10183 case scVScale:
10184 return V;
10185 case scAddRecExpr: {
10186 // If this is a loop recurrence for a loop that does not contain L, then we
10187 // are dealing with the final value computed by the loop.
10188 const SCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Val: V);
10189 // First, attempt to evaluate each operand.
10190 // Avoid performing the look-up in the common case where the specified
10191 // expression has no loop-variant portions.
10192 for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
10193 const SCEV *OpAtScope = getSCEVAtScope(V: AddRec->getOperand(i), L);
10194 if (OpAtScope == AddRec->getOperand(i))
10195 continue;
10196
10197 // Okay, at least one of these operands is loop variant but might be
10198 // foldable. Build a new instance of the folded commutative expression.
10199 SmallVector<SCEVUse, 8> NewOps;
10200 NewOps.reserve(N: AddRec->getNumOperands());
10201 append_range(C&: NewOps, R: AddRec->operands().take_front(N: i));
10202 NewOps.push_back(Elt: OpAtScope);
10203 for (++i; i != e; ++i)
10204 NewOps.push_back(Elt: getSCEVAtScope(V: AddRec->getOperand(i), L));
10205
10206 const SCEV *FoldedRec = getAddRecExpr(
10207 Operands&: NewOps, L: AddRec->getLoop(), Flags: AddRec->getNoWrapFlags(Mask: SCEV::FlagNW));
10208 AddRec = dyn_cast<SCEVAddRecExpr>(Val: FoldedRec);
10209 // The addrec may be folded to a nonrecurrence, for example, if the
10210 // induction variable is multiplied by zero after constant folding. Go
10211 // ahead and return the folded value.
10212 if (!AddRec)
10213 return FoldedRec;
10214 break;
10215 }
10216
10217 // If the scope is outside the addrec's loop, evaluate it by using the
10218 // loop exit value of the addrec.
10219 if (!AddRec->getLoop()->contains(L)) {
10220 // To evaluate this recurrence, we need to know how many times the AddRec
10221 // loop iterates. Compute this now.
10222 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(L: AddRec->getLoop());
10223 if (BackedgeTakenCount == getCouldNotCompute())
10224 return AddRec;
10225
10226 // Then, evaluate the AddRec.
10227 return AddRec->evaluateAtIteration(It: BackedgeTakenCount, SE&: *this);
10228 }
10229
10230 return AddRec;
10231 }
10232 case scTruncate:
10233 case scZeroExtend:
10234 case scSignExtend:
10235 case scPtrToAddr:
10236 case scPtrToInt:
10237 case scAddExpr:
10238 case scMulExpr:
10239 case scUDivExpr:
10240 case scUMaxExpr:
10241 case scSMaxExpr:
10242 case scUMinExpr:
10243 case scSMinExpr:
10244 case scSequentialUMinExpr: {
10245 ArrayRef<SCEVUse> Ops = V->operands();
10246 // Avoid performing the look-up in the common case where the specified
10247 // expression has no loop-variant portions.
10248 for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
10249 const SCEV *OpAtScope = getSCEVAtScope(V: Ops[i].getPointer(), L);
10250 if (OpAtScope != Ops[i].getPointer()) {
10251 // Okay, at least one of these operands is loop variant but might be
10252 // foldable. Build a new instance of the folded commutative expression.
10253 SmallVector<SCEVUse, 8> NewOps;
10254 NewOps.reserve(N: Ops.size());
10255 append_range(C&: NewOps, R: Ops.take_front(N: i));
10256 NewOps.push_back(Elt: OpAtScope);
10257
10258 for (++i; i != e; ++i) {
10259 OpAtScope = getSCEVAtScope(V: Ops[i].getPointer(), L);
10260 NewOps.push_back(Elt: OpAtScope);
10261 }
10262
10263 return getWithOperands(S: V, NewOps);
10264 }
10265 }
10266 // If we got here, all operands are loop invariant.
10267 return V;
10268 }
10269 case scUnknown: {
10270 // If this instruction is evolved from a constant-evolving PHI, compute the
10271 // exit value from the loop without using SCEVs.
10272 const SCEVUnknown *SU = cast<SCEVUnknown>(Val: V);
10273 Instruction *I = dyn_cast<Instruction>(Val: SU->getValue());
10274 if (!I)
10275 return V; // This is some other type of SCEVUnknown, just return it.
10276
10277 if (PHINode *PN = dyn_cast<PHINode>(Val: I)) {
10278 const Loop *CurrLoop = this->LI[I->getParent()];
10279 // Looking for loop exit value.
10280 if (CurrLoop && CurrLoop->getParentLoop() == L &&
10281 PN->getParent() == CurrLoop->getHeader()) {
10282 // Okay, there is no closed form solution for the PHI node. Check
10283 // to see if the loop that contains it has a known backedge-taken
10284 // count. If so, we may be able to force computation of the exit
10285 // value.
10286 const SCEV *BackedgeTakenCount = getBackedgeTakenCount(L: CurrLoop);
10287 // This trivial case can show up in some degenerate cases where
10288 // the incoming IR has not yet been fully simplified.
10289 if (BackedgeTakenCount->isZero()) {
10290 Value *InitValue = nullptr;
10291 bool MultipleInitValues = false;
10292 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
10293 if (!CurrLoop->contains(BB: PN->getIncomingBlock(i))) {
10294 if (!InitValue)
10295 InitValue = PN->getIncomingValue(i);
10296 else if (InitValue != PN->getIncomingValue(i)) {
10297 MultipleInitValues = true;
10298 break;
10299 }
10300 }
10301 }
10302 if (!MultipleInitValues && InitValue)
10303 return getSCEV(V: InitValue);
10304 }
10305 // Do we have a loop invariant value flowing around the backedge
10306 // for a loop which must execute the backedge?
10307 if (!isa<SCEVCouldNotCompute>(Val: BackedgeTakenCount) &&
10308 isKnownNonZero(S: BackedgeTakenCount) &&
10309 PN->getNumIncomingValues() == 2) {
10310
10311 unsigned InLoopPred =
10312 CurrLoop->contains(BB: PN->getIncomingBlock(i: 0)) ? 0 : 1;
10313 Value *BackedgeVal = PN->getIncomingValue(i: InLoopPred);
10314 if (CurrLoop->isLoopInvariant(V: BackedgeVal))
10315 return getSCEV(V: BackedgeVal);
10316 }
10317 if (auto *BTCC = dyn_cast<SCEVConstant>(Val: BackedgeTakenCount)) {
10318 // Okay, we know how many times the containing loop executes. If
10319 // this is a constant evolving PHI node, get the final value at
10320 // the specified iteration number.
10321 Constant *RV =
10322 getConstantEvolutionLoopExitValue(PN, BEs: BTCC->getAPInt(), L: CurrLoop);
10323 if (RV)
10324 return getSCEV(V: RV);
10325 }
10326 }
10327 }
10328
10329 // Okay, this is an expression that we cannot symbolically evaluate
10330 // into a SCEV. Check to see if it's possible to symbolically evaluate
10331 // the arguments into constants, and if so, try to constant propagate the
10332 // result. This is particularly useful for computing loop exit values.
10333 if (!CanConstantFold(I))
10334 return V; // This is some other type of SCEVUnknown, just return it.
10335
10336 SmallVector<Constant *, 4> Operands;
10337 Operands.reserve(N: I->getNumOperands());
10338 bool MadeImprovement = false;
10339 for (Value *Op : I->operands()) {
10340 if (Constant *C = dyn_cast<Constant>(Val: Op)) {
10341 Operands.push_back(Elt: C);
10342 continue;
10343 }
10344
10345 // If any of the operands is non-constant and if they are
10346 // non-integer and non-pointer, don't even try to analyze them
10347 // with scev techniques.
10348 if (!isSCEVable(Ty: Op->getType()))
10349 return V;
10350
10351 const SCEV *OrigV = getSCEV(V: Op);
10352 const SCEV *OpV = getSCEVAtScope(V: OrigV, L);
10353 MadeImprovement |= OrigV != OpV;
10354
10355 Constant *C = BuildConstantFromSCEV(V: OpV);
10356 if (!C)
10357 return V;
10358 assert(C->getType() == Op->getType() && "Type mismatch");
10359 Operands.push_back(Elt: C);
10360 }
10361
10362 // Check to see if getSCEVAtScope actually made an improvement.
10363 if (!MadeImprovement)
10364 return V; // This is some other type of SCEVUnknown, just return it.
10365
10366 Constant *C = nullptr;
10367 const DataLayout &DL = getDataLayout();
10368 C = ConstantFoldInstOperands(I, Ops: Operands, DL, TLI: &TLI,
10369 /*AllowNonDeterministic=*/false);
10370 if (!C)
10371 return V;
10372 return getSCEV(V: C);
10373 }
10374 case scCouldNotCompute:
10375 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
10376 }
10377 llvm_unreachable("Unknown SCEV type!");
10378}
10379
10380const SCEV *ScalarEvolution::getSCEVAtScope(Value *V, const Loop *L) {
10381 return getSCEVAtScope(V: getSCEV(V), L);
10382}
10383
10384const SCEV *ScalarEvolution::stripInjectiveFunctions(const SCEV *S) const {
10385 if (const SCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Val: S))
10386 return stripInjectiveFunctions(S: ZExt->getOperand());
10387 if (const SCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Val: S))
10388 return stripInjectiveFunctions(S: SExt->getOperand());
10389 return S;
10390}
10391
10392/// Finds the minimum unsigned root of the following equation:
10393///
10394/// A * X = B (mod N)
10395///
10396/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
10397/// A and B isn't important.
10398///
10399/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
10400static const SCEV *
10401SolveLinEquationWithOverflow(const APInt &A, const SCEV *B,
10402 SmallVectorImpl<const SCEVPredicate *> *Predicates,
10403 ScalarEvolution &SE, const Loop *L) {
10404 uint32_t BW = A.getBitWidth();
10405 assert(BW == SE.getTypeSizeInBits(B->getType()));
10406 assert(A != 0 && "A must be non-zero.");
10407
10408 // 1. D = gcd(A, N)
10409 //
10410 // The gcd of A and N may have only one prime factor: 2. The number of
10411 // trailing zeros in A is its multiplicity
10412 uint32_t Mult2 = A.countr_zero();
10413 // D = 2^Mult2
10414
10415 // 2. Check if B is divisible by D.
10416 //
10417 // B is divisible by D if and only if the multiplicity of prime factor 2 for B
10418 // is not less than multiplicity of this prime factor for D.
10419 unsigned MinTZ = SE.getMinTrailingZeros(S: B);
10420 // Try again with the terminator of the loop predecessor for context-specific
10421 // result, if MinTZ s too small.
10422 if (MinTZ < Mult2 && L->getLoopPredecessor())
10423 MinTZ = SE.getMinTrailingZeros(S: B, CtxI: L->getLoopPredecessor()->getTerminator());
10424 if (MinTZ < Mult2) {
10425 // Check if we can prove there's no remainder using URem.
10426 const SCEV *URem =
10427 SE.getURemExpr(LHS: B, RHS: SE.getConstant(Val: APInt::getOneBitSet(numBits: BW, BitNo: Mult2)));
10428 const SCEV *Zero = SE.getZero(Ty: B->getType());
10429 if (!SE.isKnownPredicate(Pred: CmpInst::ICMP_EQ, LHS: URem, RHS: Zero)) {
10430 // Try to add a predicate ensuring B is a multiple of 1 << Mult2.
10431 if (!Predicates)
10432 return SE.getCouldNotCompute();
10433
10434 // Avoid adding a predicate that is known to be false.
10435 if (SE.isKnownPredicate(Pred: CmpInst::ICMP_NE, LHS: URem, RHS: Zero))
10436 return SE.getCouldNotCompute();
10437 Predicates->push_back(Elt: SE.getEqualPredicate(LHS: URem, RHS: Zero));
10438 }
10439 }
10440
10441 // 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
10442 // modulo (N / D).
10443 //
10444 // If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent
10445 // (N / D) in general. The inverse itself always fits into BW bits, though,
10446 // so we immediately truncate it.
10447 APInt AD = A.lshr(shiftAmt: Mult2).trunc(width: BW - Mult2); // AD = A / D
10448 APInt I = AD.multiplicativeInverse().zext(width: BW);
10449
10450 // 4. Compute the minimum unsigned root of the equation:
10451 // I * (B / D) mod (N / D)
10452 // To simplify the computation, we factor out the divide by D:
10453 // (I * B mod N) / D
10454 const SCEV *D = SE.getConstant(Val: APInt::getOneBitSet(numBits: BW, BitNo: Mult2));
10455 return SE.getUDivExactExpr(LHS: SE.getMulExpr(LHS: B, RHS: SE.getConstant(Val: I)), RHS: D);
10456}
10457
10458/// For a given quadratic addrec, generate coefficients of the corresponding
10459/// quadratic equation, multiplied by a common value to ensure that they are
10460/// integers.
10461/// The returned value is a tuple { A, B, C, M, BitWidth }, where
10462/// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C
10463/// were multiplied by, and BitWidth is the bit width of the original addrec
10464/// coefficients.
10465/// This function returns std::nullopt if the addrec coefficients are not
10466/// compile- time constants.
10467static std::optional<std::tuple<APInt, APInt, APInt, APInt, unsigned>>
10468GetQuadraticEquation(const SCEVAddRecExpr *AddRec) {
10469 assert(AddRec->getNumOperands() == 3 && "This is not a quadratic chrec!");
10470 const SCEVConstant *LC = dyn_cast<SCEVConstant>(Val: AddRec->getOperand(i: 0));
10471 const SCEVConstant *MC = dyn_cast<SCEVConstant>(Val: AddRec->getOperand(i: 1));
10472 const SCEVConstant *NC = dyn_cast<SCEVConstant>(Val: AddRec->getOperand(i: 2));
10473 LLVM_DEBUG(dbgs() << __func__ << ": analyzing quadratic addrec: "
10474 << *AddRec << '\n');
10475
10476 // We currently can only solve this if the coefficients are constants.
10477 if (!LC || !MC || !NC) {
10478 LLVM_DEBUG(dbgs() << __func__ << ": coefficients are not constant\n");
10479 return std::nullopt;
10480 }
10481
10482 APInt L = LC->getAPInt();
10483 APInt M = MC->getAPInt();
10484 APInt N = NC->getAPInt();
10485 assert(!N.isZero() && "This is not a quadratic addrec");
10486
10487 unsigned BitWidth = LC->getAPInt().getBitWidth();
10488 unsigned NewWidth = BitWidth + 1;
10489 LLVM_DEBUG(dbgs() << __func__ << ": addrec coeff bw: "
10490 << BitWidth << '\n');
10491 // The sign-extension (as opposed to a zero-extension) here matches the
10492 // extension used in SolveQuadraticEquationWrap (with the same motivation).
10493 N = N.sext(width: NewWidth);
10494 M = M.sext(width: NewWidth);
10495 L = L.sext(width: NewWidth);
10496
10497 // The increments are M, M+N, M+2N, ..., so the accumulated values are
10498 // L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is,
10499 // L+M, L+2M+N, L+3M+3N, ...
10500 // After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N.
10501 //
10502 // The equation Acc = 0 is then
10503 // L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0.
10504 // In a quadratic form it becomes:
10505 // N n^2 + (2M-N) n + 2L = 0.
10506
10507 APInt A = N;
10508 APInt B = 2 * M - A;
10509 APInt C = 2 * L;
10510 APInt T = APInt(NewWidth, 2);
10511 LLVM_DEBUG(dbgs() << __func__ << ": equation " << A << "x^2 + " << B
10512 << "x + " << C << ", coeff bw: " << NewWidth
10513 << ", multiplied by " << T << '\n');
10514 return std::make_tuple(args&: A, args&: B, args&: C, args&: T, args&: BitWidth);
10515}
10516
10517/// Helper function to compare optional APInts:
10518/// (a) if X and Y both exist, return min(X, Y),
10519/// (b) if neither X nor Y exist, return std::nullopt,
10520/// (c) if exactly one of X and Y exists, return that value.
10521static std::optional<APInt> MinOptional(std::optional<APInt> X,
10522 std::optional<APInt> Y) {
10523 if (X && Y) {
10524 unsigned W = std::max(a: X->getBitWidth(), b: Y->getBitWidth());
10525 APInt XW = X->sext(width: W);
10526 APInt YW = Y->sext(width: W);
10527 return XW.slt(RHS: YW) ? *X : *Y;
10528 }
10529 if (!X && !Y)
10530 return std::nullopt;
10531 return X ? *X : *Y;
10532}
10533
10534/// Helper function to truncate an optional APInt to a given BitWidth.
10535/// When solving addrec-related equations, it is preferable to return a value
10536/// that has the same bit width as the original addrec's coefficients. If the
10537/// solution fits in the original bit width, truncate it (except for i1).
10538/// Returning a value of a different bit width may inhibit some optimizations.
10539///
10540/// In general, a solution to a quadratic equation generated from an addrec
10541/// may require BW+1 bits, where BW is the bit width of the addrec's
10542/// coefficients. The reason is that the coefficients of the quadratic
10543/// equation are BW+1 bits wide (to avoid truncation when converting from
10544/// the addrec to the equation).
10545static std::optional<APInt> TruncIfPossible(std::optional<APInt> X,
10546 unsigned BitWidth) {
10547 if (!X)
10548 return std::nullopt;
10549 unsigned W = X->getBitWidth();
10550 if (BitWidth > 1 && BitWidth < W && X->isIntN(N: BitWidth))
10551 return X->trunc(width: BitWidth);
10552 return X;
10553}
10554
10555/// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n
10556/// iterations. The values L, M, N are assumed to be signed, and they
10557/// should all have the same bit widths.
10558/// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW,
10559/// where BW is the bit width of the addrec's coefficients.
10560/// If the calculated value is a BW-bit integer (for BW > 1), it will be
10561/// returned as such, otherwise the bit width of the returned value may
10562/// be greater than BW.
10563///
10564/// This function returns std::nullopt if
10565/// (a) the addrec coefficients are not constant, or
10566/// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases
10567/// like x^2 = 5, no integer solutions exist, in other cases an integer
10568/// solution may exist, but SolveQuadraticEquationWrap may fail to find it.
10569static std::optional<APInt>
10570SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE) {
10571 APInt A, B, C, M;
10572 unsigned BitWidth;
10573 auto T = GetQuadraticEquation(AddRec);
10574 if (!T)
10575 return std::nullopt;
10576
10577 std::tie(args&: A, args&: B, args&: C, args&: M, args&: BitWidth) = *T;
10578 LLVM_DEBUG(dbgs() << __func__ << ": solving for unsigned overflow\n");
10579 std::optional<APInt> X =
10580 APIntOps::SolveQuadraticEquationWrap(A, B, C, RangeWidth: BitWidth + 1);
10581 if (!X)
10582 return std::nullopt;
10583
10584 ConstantInt *CX = ConstantInt::get(Context&: SE.getContext(), V: *X);
10585 ConstantInt *V = EvaluateConstantChrecAtConstant(AddRec, C: CX, SE);
10586 if (!V->isZero())
10587 return std::nullopt;
10588
10589 return TruncIfPossible(X, BitWidth);
10590}
10591
10592/// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n
10593/// iterations. The values M, N are assumed to be signed, and they
10594/// should all have the same bit widths.
10595/// Find the least n such that c(n) does not belong to the given range,
10596/// while c(n-1) does.
10597///
10598/// This function returns std::nullopt if
10599/// (a) the addrec coefficients are not constant, or
10600/// (b) SolveQuadraticEquationWrap was unable to find a solution for the
10601/// bounds of the range.
10602static std::optional<APInt>
10603SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec,
10604 const ConstantRange &Range, ScalarEvolution &SE) {
10605 assert(AddRec->getOperand(0)->isZero() &&
10606 "Starting value of addrec should be 0");
10607 LLVM_DEBUG(dbgs() << __func__ << ": solving boundary crossing for range "
10608 << Range << ", addrec " << *AddRec << '\n');
10609 // This case is handled in getNumIterationsInRange. Here we can assume that
10610 // we start in the range.
10611 assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) &&
10612 "Addrec's initial value should be in range");
10613
10614 APInt A, B, C, M;
10615 unsigned BitWidth;
10616 auto T = GetQuadraticEquation(AddRec);
10617 if (!T)
10618 return std::nullopt;
10619
10620 // Be careful about the return value: there can be two reasons for not
10621 // returning an actual number. First, if no solutions to the equations
10622 // were found, and second, if the solutions don't leave the given range.
10623 // The first case means that the actual solution is "unknown", the second
10624 // means that it's known, but not valid. If the solution is unknown, we
10625 // cannot make any conclusions.
10626 // Return a pair: the optional solution and a flag indicating if the
10627 // solution was found.
10628 auto SolveForBoundary =
10629 [&](APInt Bound) -> std::pair<std::optional<APInt>, bool> {
10630 // Solve for signed overflow and unsigned overflow, pick the lower
10631 // solution.
10632 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: checking boundary "
10633 << Bound << " (before multiplying by " << M << ")\n");
10634 Bound *= M; // The quadratic equation multiplier.
10635
10636 std::optional<APInt> SO;
10637 if (BitWidth > 1) {
10638 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
10639 "signed overflow\n");
10640 SO = APIntOps::SolveQuadraticEquationWrap(A, B, C: -Bound, RangeWidth: BitWidth);
10641 }
10642 LLVM_DEBUG(dbgs() << "SolveQuadraticAddRecRange: solving for "
10643 "unsigned overflow\n");
10644 std::optional<APInt> UO =
10645 APIntOps::SolveQuadraticEquationWrap(A, B, C: -Bound, RangeWidth: BitWidth + 1);
10646
10647 auto LeavesRange = [&] (const APInt &X) {
10648 ConstantInt *C0 = ConstantInt::get(Context&: SE.getContext(), V: X);
10649 ConstantInt *V0 = EvaluateConstantChrecAtConstant(AddRec, C: C0, SE);
10650 if (Range.contains(Val: V0->getValue()))
10651 return false;
10652 // X should be at least 1, so X-1 is non-negative.
10653 ConstantInt *C1 = ConstantInt::get(Context&: SE.getContext(), V: X-1);
10654 ConstantInt *V1 = EvaluateConstantChrecAtConstant(AddRec, C: C1, SE);
10655 if (Range.contains(Val: V1->getValue()))
10656 return true;
10657 return false;
10658 };
10659
10660 // If SolveQuadraticEquationWrap returns std::nullopt, it means that there
10661 // can be a solution, but the function failed to find it. We cannot treat it
10662 // as "no solution".
10663 if (!SO || !UO)
10664 return {std::nullopt, false};
10665
10666 // Check the smaller value first to see if it leaves the range.
10667 // At this point, both SO and UO must have values.
10668 std::optional<APInt> Min = MinOptional(X: SO, Y: UO);
10669 if (LeavesRange(*Min))
10670 return { Min, true };
10671 std::optional<APInt> Max = Min == SO ? UO : SO;
10672 if (LeavesRange(*Max))
10673 return { Max, true };
10674
10675 // Solutions were found, but were eliminated, hence the "true".
10676 return {std::nullopt, true};
10677 };
10678
10679 std::tie(args&: A, args&: B, args&: C, args&: M, args&: BitWidth) = *T;
10680 // Lower bound is inclusive, subtract 1 to represent the exiting value.
10681 APInt Lower = Range.getLower().sext(width: A.getBitWidth()) - 1;
10682 APInt Upper = Range.getUpper().sext(width: A.getBitWidth());
10683 auto SL = SolveForBoundary(Lower);
10684 auto SU = SolveForBoundary(Upper);
10685 // If any of the solutions was unknown, no meaninigful conclusions can
10686 // be made.
10687 if (!SL.second || !SU.second)
10688 return std::nullopt;
10689
10690 // Claim: The correct solution is not some value between Min and Max.
10691 //
10692 // Justification: Assuming that Min and Max are different values, one of
10693 // them is when the first signed overflow happens, the other is when the
10694 // first unsigned overflow happens. Crossing the range boundary is only
10695 // possible via an overflow (treating 0 as a special case of it, modeling
10696 // an overflow as crossing k*2^W for some k).
10697 //
10698 // The interesting case here is when Min was eliminated as an invalid
10699 // solution, but Max was not. The argument is that if there was another
10700 // overflow between Min and Max, it would also have been eliminated if
10701 // it was considered.
10702 //
10703 // For a given boundary, it is possible to have two overflows of the same
10704 // type (signed/unsigned) without having the other type in between: this
10705 // can happen when the vertex of the parabola is between the iterations
10706 // corresponding to the overflows. This is only possible when the two
10707 // overflows cross k*2^W for the same k. In such case, if the second one
10708 // left the range (and was the first one to do so), the first overflow
10709 // would have to enter the range, which would mean that either we had left
10710 // the range before or that we started outside of it. Both of these cases
10711 // are contradictions.
10712 //
10713 // Claim: In the case where SolveForBoundary returns std::nullopt, the correct
10714 // solution is not some value between the Max for this boundary and the
10715 // Min of the other boundary.
10716 //
10717 // Justification: Assume that we had such Max_A and Min_B corresponding
10718 // to range boundaries A and B and such that Max_A < Min_B. If there was
10719 // a solution between Max_A and Min_B, it would have to be caused by an
10720 // overflow corresponding to either A or B. It cannot correspond to B,
10721 // since Min_B is the first occurrence of such an overflow. If it
10722 // corresponded to A, it would have to be either a signed or an unsigned
10723 // overflow that is larger than both eliminated overflows for A. But
10724 // between the eliminated overflows and this overflow, the values would
10725 // cover the entire value space, thus crossing the other boundary, which
10726 // is a contradiction.
10727
10728 return TruncIfPossible(X: MinOptional(X: SL.first, Y: SU.first), BitWidth);
10729}
10730
10731ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(const SCEV *V,
10732 const Loop *L,
10733 bool ControlsOnlyExit,
10734 bool AllowPredicates) {
10735
10736 // This is only used for loops with a "x != y" exit test. The exit condition
10737 // is now expressed as a single expression, V = x-y. So the exit test is
10738 // effectively V != 0. We know and take advantage of the fact that this
10739 // expression only being used in a comparison by zero context.
10740
10741 SmallVector<const SCEVPredicate *> Predicates;
10742 // If the value is a constant
10743 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Val: V)) {
10744 // If the value is already zero, the branch will execute zero times.
10745 if (C->getValue()->isZero()) return C;
10746 return getCouldNotCompute(); // Otherwise it will loop infinitely.
10747 }
10748
10749 const SCEVAddRecExpr *AddRec =
10750 dyn_cast<SCEVAddRecExpr>(Val: stripInjectiveFunctions(S: V));
10751
10752 if (!AddRec && AllowPredicates)
10753 // Try to make this an AddRec using runtime tests, in the first X
10754 // iterations of this loop, where X is the SCEV expression found by the
10755 // algorithm below.
10756 AddRec = convertSCEVToAddRecWithPredicates(S: V, L, Preds&: Predicates);
10757
10758 if (!AddRec || AddRec->getLoop() != L)
10759 return getCouldNotCompute();
10760
10761 // If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
10762 // the quadratic equation to solve it.
10763 if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
10764 // We can only use this value if the chrec ends up with an exact zero
10765 // value at this index. When solving for "X*X != 5", for example, we
10766 // should not accept a root of 2.
10767 if (auto S = SolveQuadraticAddRecExact(AddRec, SE&: *this)) {
10768 const auto *R = cast<SCEVConstant>(Val: getConstant(Val: *S));
10769 return ExitLimit(R, R, R, false, Predicates);
10770 }
10771 return getCouldNotCompute();
10772 }
10773
10774 // Otherwise we can only handle this if it is affine.
10775 if (!AddRec->isAffine())
10776 return getCouldNotCompute();
10777
10778 // If this is an affine expression, the execution count of this branch is
10779 // the minimum unsigned root of the following equation:
10780 //
10781 // Start + Step*N = 0 (mod 2^BW)
10782 //
10783 // equivalent to:
10784 //
10785 // Step*N = -Start (mod 2^BW)
10786 //
10787 // where BW is the common bit width of Start and Step.
10788
10789 // Get the initial value for the loop.
10790 const SCEV *Start = getSCEVAtScope(V: AddRec->getStart(), L: L->getParentLoop());
10791 const SCEV *Step = getSCEVAtScope(V: AddRec->getOperand(i: 1), L: L->getParentLoop());
10792
10793 if (!isLoopInvariant(S: Step, L))
10794 return getCouldNotCompute();
10795
10796 LoopGuards Guards = LoopGuards::collect(L, SE&: *this);
10797 // Specialize step for this loop so we get context sensitive facts below.
10798 const SCEV *StepWLG = applyLoopGuards(Expr: Step, Guards);
10799
10800 // For positive steps (counting up until unsigned overflow):
10801 // N = -Start/Step (as unsigned)
10802 // For negative steps (counting down to zero):
10803 // N = Start/-Step
10804 // First compute the unsigned distance from zero in the direction of Step.
10805 bool CountDown = isKnownNegative(S: StepWLG);
10806 if (!CountDown && !isKnownNonNegative(S: StepWLG))
10807 return getCouldNotCompute();
10808
10809 const SCEV *Distance = CountDown ? Start : getNegativeSCEV(V: Start);
10810 // Handle unitary steps, which cannot wraparound.
10811 // 1*N = -Start; -1*N = Start (mod 2^BW), so:
10812 // N = Distance (as unsigned)
10813
10814 if (match(S: Step, P: m_CombineOr(L: m_scev_One(), R: m_scev_AllOnes()))) {
10815 APInt MaxBECount = getUnsignedRangeMax(S: applyLoopGuards(Expr: Distance, Guards));
10816 MaxBECount = APIntOps::umin(A: MaxBECount, B: getUnsignedRangeMax(S: Distance));
10817
10818 // When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
10819 // we end up with a loop whose backedge-taken count is n - 1. Detect this
10820 // case, and see if we can improve the bound.
10821 //
10822 // Explicitly handling this here is necessary because getUnsignedRange
10823 // isn't context-sensitive; it doesn't know that we only care about the
10824 // range inside the loop.
10825 const SCEV *Zero = getZero(Ty: Distance->getType());
10826 const SCEV *One = getOne(Ty: Distance->getType());
10827 const SCEV *DistancePlusOne = getAddExpr(LHS: Distance, RHS: One);
10828 if (isLoopEntryGuardedByCond(L, Pred: ICmpInst::ICMP_NE, LHS: DistancePlusOne, RHS: Zero)) {
10829 // If Distance + 1 doesn't overflow, we can compute the maximum distance
10830 // as "unsigned_max(Distance + 1) - 1".
10831 ConstantRange CR = getUnsignedRange(S: DistancePlusOne);
10832 MaxBECount = APIntOps::umin(A: MaxBECount, B: CR.getUnsignedMax() - 1);
10833 }
10834 return ExitLimit(Distance, getConstant(Val: MaxBECount), Distance, false,
10835 Predicates);
10836 }
10837
10838 // If the condition controls loop exit (the loop exits only if the expression
10839 // is true) and the addition is no-wrap we can use unsigned divide to
10840 // compute the backedge count. In this case, the step may not divide the
10841 // distance, but we don't care because if the condition is "missed" the loop
10842 // will have undefined behavior due to wrapping.
10843 if (ControlsOnlyExit && AddRec->hasNoSelfWrap() &&
10844 loopHasNoAbnormalExits(L: AddRec->getLoop())) {
10845
10846 // If the stride is zero and the start is non-zero, the loop must be
10847 // infinite. In C++, most loops are finite by assumption, in which case the
10848 // step being zero implies UB must execute if the loop is entered.
10849 if (!(loopIsFiniteByAssumption(L) && isKnownNonZero(S: Start)) &&
10850 !isKnownNonZero(S: StepWLG))
10851 return getCouldNotCompute();
10852
10853 const SCEV *Exact =
10854 getUDivExpr(LHS: Distance, RHS: CountDown ? getNegativeSCEV(V: Step) : Step);
10855 const SCEV *ConstantMax = getCouldNotCompute();
10856 if (Exact != getCouldNotCompute()) {
10857 APInt MaxInt = getUnsignedRangeMax(S: applyLoopGuards(Expr: Exact, Guards));
10858 ConstantMax =
10859 getConstant(Val: APIntOps::umin(A: MaxInt, B: getUnsignedRangeMax(S: Exact)));
10860 }
10861 const SCEV *SymbolicMax =
10862 isa<SCEVCouldNotCompute>(Val: Exact) ? ConstantMax : Exact;
10863 return ExitLimit(Exact, ConstantMax, SymbolicMax, false, Predicates);
10864 }
10865
10866 // Solve the general equation.
10867 const SCEVConstant *StepC = dyn_cast<SCEVConstant>(Val: Step);
10868 if (!StepC || StepC->getValue()->isZero())
10869 return getCouldNotCompute();
10870 const SCEV *E = SolveLinEquationWithOverflow(
10871 A: StepC->getAPInt(), B: getNegativeSCEV(V: Start),
10872 Predicates: AllowPredicates ? &Predicates : nullptr, SE&: *this, L);
10873
10874 const SCEV *M = E;
10875 if (E != getCouldNotCompute()) {
10876 APInt MaxWithGuards = getUnsignedRangeMax(S: applyLoopGuards(Expr: E, Guards));
10877 M = getConstant(Val: APIntOps::umin(A: MaxWithGuards, B: getUnsignedRangeMax(S: E)));
10878 }
10879 auto *S = isa<SCEVCouldNotCompute>(Val: E) ? M : E;
10880 return ExitLimit(E, M, S, false, Predicates);
10881}
10882
10883ScalarEvolution::ExitLimit
10884ScalarEvolution::howFarToNonZero(const SCEV *V, const Loop *L) {
10885 // Loops that look like: while (X == 0) are very strange indeed. We don't
10886 // handle them yet except for the trivial case. This could be expanded in the
10887 // future as needed.
10888
10889 // If the value is a constant, check to see if it is known to be non-zero
10890 // already. If so, the backedge will execute zero times.
10891 if (const SCEVConstant *C = dyn_cast<SCEVConstant>(Val: V)) {
10892 if (!C->getValue()->isZero())
10893 return getZero(Ty: C->getType());
10894 return getCouldNotCompute(); // Otherwise it will loop infinitely.
10895 }
10896
10897 // We could implement others, but I really doubt anyone writes loops like
10898 // this, and if they did, they would already be constant folded.
10899 return getCouldNotCompute();
10900}
10901
10902std::pair<const BasicBlock *, const BasicBlock *>
10903ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(const BasicBlock *BB)
10904 const {
10905 // If the block has a unique predecessor, then there is no path from the
10906 // predecessor to the block that does not go through the direct edge
10907 // from the predecessor to the block.
10908 if (const BasicBlock *Pred = BB->getSinglePredecessor())
10909 return {Pred, BB};
10910
10911 // A loop's header is defined to be a block that dominates the loop.
10912 // If the header has a unique predecessor outside the loop, it must be
10913 // a block that has exactly one successor that can reach the loop.
10914 if (const Loop *L = LI.getLoopFor(BB))
10915 return {L->getLoopPredecessor(), L->getHeader()};
10916
10917 return {nullptr, BB};
10918}
10919
10920/// SCEV structural equivalence is usually sufficient for testing whether two
10921/// expressions are equal, however for the purposes of looking for a condition
10922/// guarding a loop, it can be useful to be a little more general, since a
10923/// front-end may have replicated the controlling expression.
10924static bool HasSameValue(const SCEV *A, const SCEV *B) {
10925 // Quick check to see if they are the same SCEV.
10926 if (A == B) return true;
10927
10928 auto ComputesEqualValues = [](const Instruction *A, const Instruction *B) {
10929 // Not all instructions that are "identical" compute the same value. For
10930 // instance, two distinct alloca instructions allocating the same type are
10931 // identical and do not read memory; but compute distinct values.
10932 return A->isIdenticalTo(I: B) && (isa<BinaryOperator>(Val: A) || isa<GetElementPtrInst>(Val: A));
10933 };
10934
10935 // Otherwise, if they're both SCEVUnknown, it's possible that they hold
10936 // two different instructions with the same value. Check for this case.
10937 if (const SCEVUnknown *AU = dyn_cast<SCEVUnknown>(Val: A))
10938 if (const SCEVUnknown *BU = dyn_cast<SCEVUnknown>(Val: B))
10939 if (const Instruction *AI = dyn_cast<Instruction>(Val: AU->getValue()))
10940 if (const Instruction *BI = dyn_cast<Instruction>(Val: BU->getValue()))
10941 if (ComputesEqualValues(AI, BI))
10942 return true;
10943
10944 // Otherwise assume they may have a different value.
10945 return false;
10946}
10947
10948static bool MatchBinarySub(const SCEV *S, const SCEV *&LHS, const SCEV *&RHS) {
10949 const SCEV *Op0, *Op1;
10950 if (!match(S, P: m_scev_Add(Op0: m_SCEV(V&: Op0), Op1: m_SCEV(V&: Op1))))
10951 return false;
10952 if (match(S: Op0, P: m_scev_Mul(Op0: m_scev_AllOnes(), Op1: m_SCEV(V&: RHS)))) {
10953 LHS = Op1;
10954 return true;
10955 }
10956 if (match(S: Op1, P: m_scev_Mul(Op0: m_scev_AllOnes(), Op1: m_SCEV(V&: RHS)))) {
10957 LHS = Op0;
10958 return true;
10959 }
10960 return false;
10961}
10962
10963bool ScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS,
10964 const SCEV *&RHS, unsigned Depth) {
10965 bool Changed = false;
10966 // Simplifies ICMP to trivial true or false by turning it into '0 == 0' or
10967 // '0 != 0'.
10968 auto TrivialCase = [&](bool TriviallyTrue) {
10969 LHS = RHS = getConstant(V: ConstantInt::getFalse(Context&: getContext()));
10970 Pred = TriviallyTrue ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE;
10971 return true;
10972 };
10973 // If we hit the max recursion limit bail out.
10974 if (Depth >= 3)
10975 return false;
10976
10977 const SCEV *NewLHS, *NewRHS;
10978 if (match(S: LHS, P: m_scev_c_Mul(Op0: m_SCEV(V&: NewLHS), Op1: m_SCEVVScale())) &&
10979 match(S: RHS, P: m_scev_c_Mul(Op0: m_SCEV(V&: NewRHS), Op1: m_SCEVVScale()))) {
10980 const SCEVMulExpr *LMul = cast<SCEVMulExpr>(Val: LHS);
10981 const SCEVMulExpr *RMul = cast<SCEVMulExpr>(Val: RHS);
10982
10983 // (X * vscale) pred (Y * vscale) ==> X pred Y
10984 // when both multiples are NSW.
10985 // (X * vscale) uicmp/eq/ne (Y * vscale) ==> X uicmp/eq/ne Y
10986 // when both multiples are NUW.
10987 if ((LMul->hasNoSignedWrap() && RMul->hasNoSignedWrap()) ||
10988 (LMul->hasNoUnsignedWrap() && RMul->hasNoUnsignedWrap() &&
10989 !ICmpInst::isSigned(Pred))) {
10990 LHS = NewLHS;
10991 RHS = NewRHS;
10992 Changed = true;
10993 }
10994 }
10995
10996 // Canonicalize a constant to the right side.
10997 if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(Val: LHS)) {
10998 // Check for both operands constant.
10999 if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(Val: RHS)) {
11000 if (!ICmpInst::compare(LHS: LHSC->getAPInt(), RHS: RHSC->getAPInt(), Pred))
11001 return TrivialCase(false);
11002 return TrivialCase(true);
11003 }
11004 // Otherwise swap the operands to put the constant on the right.
11005 std::swap(a&: LHS, b&: RHS);
11006 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
11007 Changed = true;
11008 }
11009
11010 // If we're comparing an addrec with a value which is loop-invariant in the
11011 // addrec's loop, put the addrec on the left. Also make a dominance check,
11012 // as both operands could be addrecs loop-invariant in each other's loop.
11013 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: RHS)) {
11014 const Loop *L = AR->getLoop();
11015 if (isLoopInvariant(S: LHS, L) && properlyDominates(S: LHS, BB: L->getHeader())) {
11016 std::swap(a&: LHS, b&: RHS);
11017 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
11018 Changed = true;
11019 }
11020 }
11021
11022 // If there's a constant operand, canonicalize comparisons with boundary
11023 // cases, and canonicalize *-or-equal comparisons to regular comparisons.
11024 if (const SCEVConstant *RC = dyn_cast<SCEVConstant>(Val: RHS)) {
11025 const APInt &RA = RC->getAPInt();
11026
11027 bool SimplifiedByConstantRange = false;
11028
11029 if (!ICmpInst::isEquality(P: Pred)) {
11030 ConstantRange ExactCR = ConstantRange::makeExactICmpRegion(Pred, Other: RA);
11031 if (ExactCR.isFullSet())
11032 return TrivialCase(true);
11033 if (ExactCR.isEmptySet())
11034 return TrivialCase(false);
11035
11036 APInt NewRHS;
11037 CmpInst::Predicate NewPred;
11038 if (ExactCR.getEquivalentICmp(Pred&: NewPred, RHS&: NewRHS) &&
11039 ICmpInst::isEquality(P: NewPred)) {
11040 // We were able to convert an inequality to an equality.
11041 Pred = NewPred;
11042 RHS = getConstant(Val: NewRHS);
11043 Changed = SimplifiedByConstantRange = true;
11044 }
11045 }
11046
11047 if (!SimplifiedByConstantRange) {
11048 switch (Pred) {
11049 default:
11050 break;
11051 case ICmpInst::ICMP_EQ:
11052 case ICmpInst::ICMP_NE:
11053 // Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
11054 if (RA.isZero() && MatchBinarySub(S: LHS, LHS, RHS))
11055 Changed = true;
11056 break;
11057
11058 // The "Should have been caught earlier!" messages refer to the fact
11059 // that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
11060 // should have fired on the corresponding cases, and canonicalized the
11061 // check to trivial case.
11062
11063 case ICmpInst::ICMP_UGE:
11064 assert(!RA.isMinValue() && "Should have been caught earlier!");
11065 Pred = ICmpInst::ICMP_UGT;
11066 RHS = getConstant(Val: RA - 1);
11067 Changed = true;
11068 break;
11069 case ICmpInst::ICMP_ULE:
11070 assert(!RA.isMaxValue() && "Should have been caught earlier!");
11071 Pred = ICmpInst::ICMP_ULT;
11072 RHS = getConstant(Val: RA + 1);
11073 Changed = true;
11074 break;
11075 case ICmpInst::ICMP_SGE:
11076 assert(!RA.isMinSignedValue() && "Should have been caught earlier!");
11077 Pred = ICmpInst::ICMP_SGT;
11078 RHS = getConstant(Val: RA - 1);
11079 Changed = true;
11080 break;
11081 case ICmpInst::ICMP_SLE:
11082 assert(!RA.isMaxSignedValue() && "Should have been caught earlier!");
11083 Pred = ICmpInst::ICMP_SLT;
11084 RHS = getConstant(Val: RA + 1);
11085 Changed = true;
11086 break;
11087 }
11088 }
11089 }
11090
11091 // Check for obvious equality.
11092 if (HasSameValue(A: LHS, B: RHS)) {
11093 if (ICmpInst::isTrueWhenEqual(predicate: Pred))
11094 return TrivialCase(true);
11095 if (ICmpInst::isFalseWhenEqual(predicate: Pred))
11096 return TrivialCase(false);
11097 }
11098
11099 // If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
11100 // adding or subtracting 1 from one of the operands.
11101 switch (Pred) {
11102 case ICmpInst::ICMP_SLE:
11103 if (!getSignedRangeMax(S: RHS).isMaxSignedValue()) {
11104 RHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: 1, isSigned: true), RHS,
11105 Flags: SCEV::FlagNSW);
11106 Pred = ICmpInst::ICMP_SLT;
11107 Changed = true;
11108 } else if (!getSignedRangeMin(S: LHS).isMinSignedValue()) {
11109 LHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: (uint64_t)-1, isSigned: true), RHS: LHS,
11110 Flags: SCEV::FlagNSW);
11111 Pred = ICmpInst::ICMP_SLT;
11112 Changed = true;
11113 }
11114 break;
11115 case ICmpInst::ICMP_SGE:
11116 if (!getSignedRangeMin(S: RHS).isMinSignedValue()) {
11117 RHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: (uint64_t)-1, isSigned: true), RHS,
11118 Flags: SCEV::FlagNSW);
11119 Pred = ICmpInst::ICMP_SGT;
11120 Changed = true;
11121 } else if (!getSignedRangeMax(S: LHS).isMaxSignedValue()) {
11122 LHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: 1, isSigned: true), RHS: LHS,
11123 Flags: SCEV::FlagNSW);
11124 Pred = ICmpInst::ICMP_SGT;
11125 Changed = true;
11126 }
11127 break;
11128 case ICmpInst::ICMP_ULE:
11129 if (!getUnsignedRangeMax(S: RHS).isMaxValue()) {
11130 RHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: 1, isSigned: true), RHS,
11131 Flags: SCEV::FlagNUW);
11132 Pred = ICmpInst::ICMP_ULT;
11133 Changed = true;
11134 } else if (!getUnsignedRangeMin(S: LHS).isMinValue()) {
11135 LHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: (uint64_t)-1, isSigned: true), RHS: LHS);
11136 Pred = ICmpInst::ICMP_ULT;
11137 Changed = true;
11138 }
11139 break;
11140 case ICmpInst::ICMP_UGE:
11141 // If RHS is an op we can fold the -1, try that first.
11142 // Otherwise prefer LHS to preserve the nuw flag.
11143 if ((isa<SCEVConstant>(Val: RHS) ||
11144 (isa<SCEVAddExpr, SCEVAddRecExpr>(Val: RHS) &&
11145 isa<SCEVConstant>(Val: cast<SCEVNAryExpr>(Val: RHS)->getOperand(i: 0)))) &&
11146 !getUnsignedRangeMin(S: RHS).isMinValue()) {
11147 RHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: (uint64_t)-1, isSigned: true), RHS);
11148 Pred = ICmpInst::ICMP_UGT;
11149 Changed = true;
11150 } else if (!getUnsignedRangeMax(S: LHS).isMaxValue()) {
11151 LHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: 1, isSigned: true), RHS: LHS,
11152 Flags: SCEV::FlagNUW);
11153 Pred = ICmpInst::ICMP_UGT;
11154 Changed = true;
11155 } else if (!getUnsignedRangeMin(S: RHS).isMinValue()) {
11156 RHS = getAddExpr(LHS: getConstant(Ty: RHS->getType(), V: (uint64_t)-1, isSigned: true), RHS);
11157 Pred = ICmpInst::ICMP_UGT;
11158 Changed = true;
11159 }
11160 break;
11161 default:
11162 break;
11163 }
11164
11165 // TODO: More simplifications are possible here.
11166
11167 // Recursively simplify until we either hit a recursion limit or nothing
11168 // changes.
11169 if (Changed)
11170 (void)SimplifyICmpOperands(Pred, LHS, RHS, Depth: Depth + 1);
11171
11172 return Changed;
11173}
11174
11175bool ScalarEvolution::isKnownNegative(const SCEV *S) {
11176 return getSignedRangeMax(S).isNegative();
11177}
11178
11179bool ScalarEvolution::isKnownPositive(const SCEV *S) {
11180 return getSignedRangeMin(S).isStrictlyPositive();
11181}
11182
11183bool ScalarEvolution::isKnownNonNegative(const SCEV *S) {
11184 return !getSignedRangeMin(S).isNegative();
11185}
11186
11187bool ScalarEvolution::isKnownNonPositive(const SCEV *S) {
11188 return !getSignedRangeMax(S).isStrictlyPositive();
11189}
11190
11191bool ScalarEvolution::isKnownNonZero(const SCEV *S) {
11192 // Query push down for cases where the unsigned range is
11193 // less than sufficient.
11194 if (const auto *SExt = dyn_cast<SCEVSignExtendExpr>(Val: S))
11195 return isKnownNonZero(S: SExt->getOperand(i: 0));
11196 return getUnsignedRangeMin(S) != 0;
11197}
11198
11199bool ScalarEvolution::isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero,
11200 bool OrNegative) {
11201 auto NonRecursive = [OrNegative](const SCEV *S) {
11202 if (auto *C = dyn_cast<SCEVConstant>(Val: S))
11203 return C->getAPInt().isPowerOf2() ||
11204 (OrNegative && C->getAPInt().isNegatedPowerOf2());
11205
11206 // vscale is a power-of-two.
11207 return isa<SCEVVScale>(Val: S);
11208 };
11209
11210 if (NonRecursive(S))
11211 return true;
11212
11213 auto *Mul = dyn_cast<SCEVMulExpr>(Val: S);
11214 if (!Mul)
11215 return false;
11216 return all_of(Range: Mul->operands(), P: NonRecursive) && (OrZero || isKnownNonZero(S));
11217}
11218
11219bool ScalarEvolution::isKnownMultipleOf(
11220 const SCEV *S, uint64_t M,
11221 SmallVectorImpl<const SCEVPredicate *> &Assumptions) {
11222 if (M == 0)
11223 return false;
11224 if (M == 1)
11225 return true;
11226
11227 // Recursively check AddRec operands. An AddRecExpr S is a multiple of M if S
11228 // starts with a multiple of M and at every iteration step S only adds
11229 // multiples of M.
11230 if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(Val: S))
11231 return isKnownMultipleOf(S: AddRec->getStart(), M, Assumptions) &&
11232 isKnownMultipleOf(S: AddRec->getStepRecurrence(SE&: *this), M, Assumptions);
11233
11234 // For a constant, check that "S % M == 0".
11235 if (auto *Cst = dyn_cast<SCEVConstant>(Val: S)) {
11236 APInt C = Cst->getAPInt();
11237 return C.urem(RHS: M) == 0;
11238 }
11239
11240 // TODO: Also check other SCEV expressions, i.e., SCEVAddRecExpr, etc.
11241
11242 // Basic tests have failed.
11243 // Check "S % M == 0" at compile time and record runtime Assumptions.
11244 auto *STy = dyn_cast<IntegerType>(Val: S->getType());
11245 const SCEV *SmodM =
11246 getURemExpr(LHS: S, RHS: getConstant(V: ConstantInt::get(Ty: STy, V: M, IsSigned: false)));
11247 const SCEV *Zero = getZero(Ty: STy);
11248
11249 // Check whether "S % M == 0" is known at compile time.
11250 if (isKnownPredicate(Pred: ICmpInst::ICMP_EQ, LHS: SmodM, RHS: Zero))
11251 return true;
11252
11253 // Check whether "S % M != 0" is known at compile time.
11254 if (isKnownPredicate(Pred: ICmpInst::ICMP_NE, LHS: SmodM, RHS: Zero))
11255 return false;
11256
11257 const SCEVPredicate *P = getComparePredicate(Pred: ICmpInst::ICMP_EQ, LHS: SmodM, RHS: Zero);
11258
11259 // Detect redundant predicates.
11260 for (auto *A : Assumptions)
11261 if (A->implies(N: P, SE&: *this))
11262 return true;
11263
11264 // Only record non-redundant predicates.
11265 Assumptions.push_back(Elt: P);
11266 return true;
11267}
11268
11269bool ScalarEvolution::haveSameSign(const SCEV *S1, const SCEV *S2) {
11270 return ((isKnownNonNegative(S: S1) && isKnownNonNegative(S: S2)) ||
11271 (isKnownNegative(S: S1) && isKnownNegative(S: S2)));
11272}
11273
11274std::pair<const SCEV *, const SCEV *>
11275ScalarEvolution::SplitIntoInitAndPostInc(const Loop *L, const SCEV *S) {
11276 // Compute SCEV on entry of loop L.
11277 const SCEV *Start = SCEVInitRewriter::rewrite(S, L, SE&: *this);
11278 if (Start == getCouldNotCompute())
11279 return { Start, Start };
11280 // Compute post increment SCEV for loop L.
11281 const SCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, SE&: *this);
11282 assert(PostInc != getCouldNotCompute() && "Unexpected could not compute");
11283 return { Start, PostInc };
11284}
11285
11286bool ScalarEvolution::isKnownViaInduction(CmpPredicate Pred, const SCEV *LHS,
11287 const SCEV *RHS) {
11288 // First collect all loops.
11289 SmallPtrSet<const Loop *, 8> LoopsUsed;
11290 getUsedLoops(S: LHS, LoopsUsed);
11291 getUsedLoops(S: RHS, LoopsUsed);
11292
11293 if (LoopsUsed.empty())
11294 return false;
11295
11296 // Domination relationship must be a linear order on collected loops.
11297#ifndef NDEBUG
11298 for (const auto *L1 : LoopsUsed)
11299 for (const auto *L2 : LoopsUsed)
11300 assert((DT.dominates(L1->getHeader(), L2->getHeader()) ||
11301 DT.dominates(L2->getHeader(), L1->getHeader())) &&
11302 "Domination relationship is not a linear order");
11303#endif
11304
11305 const Loop *MDL =
11306 *llvm::max_element(Range&: LoopsUsed, C: [&](const Loop *L1, const Loop *L2) {
11307 return DT.properlyDominates(A: L1->getHeader(), B: L2->getHeader());
11308 });
11309
11310 // Get init and post increment value for LHS.
11311 auto SplitLHS = SplitIntoInitAndPostInc(L: MDL, S: LHS);
11312 // if LHS contains unknown non-invariant SCEV then bail out.
11313 if (SplitLHS.first == getCouldNotCompute())
11314 return false;
11315 assert (SplitLHS.second != getCouldNotCompute() && "Unexpected CNC");
11316 // Get init and post increment value for RHS.
11317 auto SplitRHS = SplitIntoInitAndPostInc(L: MDL, S: RHS);
11318 // if RHS contains unknown non-invariant SCEV then bail out.
11319 if (SplitRHS.first == getCouldNotCompute())
11320 return false;
11321 assert (SplitRHS.second != getCouldNotCompute() && "Unexpected CNC");
11322 // It is possible that init SCEV contains an invariant load but it does
11323 // not dominate MDL and is not available at MDL loop entry, so we should
11324 // check it here.
11325 if (!isAvailableAtLoopEntry(S: SplitLHS.first, L: MDL) ||
11326 !isAvailableAtLoopEntry(S: SplitRHS.first, L: MDL))
11327 return false;
11328
11329 // It seems backedge guard check is faster than entry one so in some cases
11330 // it can speed up whole estimation by short circuit
11331 return isLoopBackedgeGuardedByCond(L: MDL, Pred, LHS: SplitLHS.second,
11332 RHS: SplitRHS.second) &&
11333 isLoopEntryGuardedByCond(L: MDL, Pred, LHS: SplitLHS.first, RHS: SplitRHS.first);
11334}
11335
11336bool ScalarEvolution::isKnownPredicate(CmpPredicate Pred, const SCEV *LHS,
11337 const SCEV *RHS) {
11338 // Canonicalize the inputs first.
11339 (void)SimplifyICmpOperands(Pred, LHS, RHS);
11340
11341 if (isKnownViaInduction(Pred, LHS, RHS))
11342 return true;
11343
11344 if (isKnownPredicateViaSplitting(Pred, LHS, RHS))
11345 return true;
11346
11347 // Otherwise see what can be done with some simple reasoning.
11348 return isKnownViaNonRecursiveReasoning(Pred, LHS, RHS);
11349}
11350
11351std::optional<bool> ScalarEvolution::evaluatePredicate(CmpPredicate Pred,
11352 const SCEV *LHS,
11353 const SCEV *RHS) {
11354 if (isKnownPredicate(Pred, LHS, RHS))
11355 return true;
11356 if (isKnownPredicate(Pred: ICmpInst::getInverseCmpPredicate(Pred), LHS, RHS))
11357 return false;
11358 return std::nullopt;
11359}
11360
11361bool ScalarEvolution::isKnownPredicateAt(CmpPredicate Pred, const SCEV *LHS,
11362 const SCEV *RHS,
11363 const Instruction *CtxI) {
11364 // TODO: Analyze guards and assumes from Context's block.
11365 return isKnownPredicate(Pred, LHS, RHS) ||
11366 isBasicBlockEntryGuardedByCond(BB: CtxI->getParent(), Pred, LHS, RHS);
11367}
11368
11369std::optional<bool>
11370ScalarEvolution::evaluatePredicateAt(CmpPredicate Pred, const SCEV *LHS,
11371 const SCEV *RHS, const Instruction *CtxI) {
11372 std::optional<bool> KnownWithoutContext = evaluatePredicate(Pred, LHS, RHS);
11373 if (KnownWithoutContext)
11374 return KnownWithoutContext;
11375
11376 if (isBasicBlockEntryGuardedByCond(BB: CtxI->getParent(), Pred, LHS, RHS))
11377 return true;
11378 if (isBasicBlockEntryGuardedByCond(
11379 BB: CtxI->getParent(), Pred: ICmpInst::getInverseCmpPredicate(Pred), LHS, RHS))
11380 return false;
11381 return std::nullopt;
11382}
11383
11384bool ScalarEvolution::isKnownOnEveryIteration(CmpPredicate Pred,
11385 const SCEVAddRecExpr *LHS,
11386 const SCEV *RHS) {
11387 const Loop *L = LHS->getLoop();
11388 return isLoopEntryGuardedByCond(L, Pred, LHS: LHS->getStart(), RHS) &&
11389 isLoopBackedgeGuardedByCond(L, Pred, LHS: LHS->getPostIncExpr(SE&: *this), RHS);
11390}
11391
11392std::optional<ScalarEvolution::MonotonicPredicateType>
11393ScalarEvolution::getMonotonicPredicateType(const SCEVAddRecExpr *LHS,
11394 ICmpInst::Predicate Pred) {
11395 auto Result = getMonotonicPredicateTypeImpl(LHS, Pred);
11396
11397#ifndef NDEBUG
11398 // Verify an invariant: inverting the predicate should turn a monotonically
11399 // increasing change to a monotonically decreasing one, and vice versa.
11400 if (Result) {
11401 auto ResultSwapped =
11402 getMonotonicPredicateTypeImpl(LHS, ICmpInst::getSwappedPredicate(Pred));
11403
11404 assert(*ResultSwapped != *Result &&
11405 "monotonicity should flip as we flip the predicate");
11406 }
11407#endif
11408
11409 return Result;
11410}
11411
11412std::optional<ScalarEvolution::MonotonicPredicateType>
11413ScalarEvolution::getMonotonicPredicateTypeImpl(const SCEVAddRecExpr *LHS,
11414 ICmpInst::Predicate Pred) {
11415 // A zero step value for LHS means the induction variable is essentially a
11416 // loop invariant value. We don't really depend on the predicate actually
11417 // flipping from false to true (for increasing predicates, and the other way
11418 // around for decreasing predicates), all we care about is that *if* the
11419 // predicate changes then it only changes from false to true.
11420 //
11421 // A zero step value in itself is not very useful, but there may be places
11422 // where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be
11423 // as general as possible.
11424
11425 // Only handle LE/LT/GE/GT predicates.
11426 if (!ICmpInst::isRelational(P: Pred))
11427 return std::nullopt;
11428
11429 bool IsGreater = ICmpInst::isGE(P: Pred) || ICmpInst::isGT(P: Pred);
11430 assert((IsGreater || ICmpInst::isLE(Pred) || ICmpInst::isLT(Pred)) &&
11431 "Should be greater or less!");
11432
11433 // Check that AR does not wrap.
11434 if (ICmpInst::isUnsigned(Pred)) {
11435 if (!LHS->hasNoUnsignedWrap())
11436 return std::nullopt;
11437 return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing;
11438 }
11439 assert(ICmpInst::isSigned(Pred) &&
11440 "Relational predicate is either signed or unsigned!");
11441 if (!LHS->hasNoSignedWrap())
11442 return std::nullopt;
11443
11444 const SCEV *Step = LHS->getStepRecurrence(SE&: *this);
11445
11446 if (isKnownNonNegative(S: Step))
11447 return IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing;
11448
11449 if (isKnownNonPositive(S: Step))
11450 return !IsGreater ? MonotonicallyIncreasing : MonotonicallyDecreasing;
11451
11452 return std::nullopt;
11453}
11454
11455std::optional<ScalarEvolution::LoopInvariantPredicate>
11456ScalarEvolution::getLoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS,
11457 const SCEV *RHS, const Loop *L,
11458 const Instruction *CtxI) {
11459 // If there is a loop-invariant, force it into the RHS, otherwise bail out.
11460 if (!isLoopInvariant(S: RHS, L)) {
11461 if (!isLoopInvariant(S: LHS, L))
11462 return std::nullopt;
11463
11464 std::swap(a&: LHS, b&: RHS);
11465 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
11466 }
11467
11468 const SCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(Val: LHS);
11469 if (!ArLHS || ArLHS->getLoop() != L)
11470 return std::nullopt;
11471
11472 auto MonotonicType = getMonotonicPredicateType(LHS: ArLHS, Pred);
11473 if (!MonotonicType)
11474 return std::nullopt;
11475 // If the predicate "ArLHS `Pred` RHS" monotonically increases from false to
11476 // true as the loop iterates, and the backedge is control dependent on
11477 // "ArLHS `Pred` RHS" == true then we can reason as follows:
11478 //
11479 // * if the predicate was false in the first iteration then the predicate
11480 // is never evaluated again, since the loop exits without taking the
11481 // backedge.
11482 // * if the predicate was true in the first iteration then it will
11483 // continue to be true for all future iterations since it is
11484 // monotonically increasing.
11485 //
11486 // For both the above possibilities, we can replace the loop varying
11487 // predicate with its value on the first iteration of the loop (which is
11488 // loop invariant).
11489 //
11490 // A similar reasoning applies for a monotonically decreasing predicate, by
11491 // replacing true with false and false with true in the above two bullets.
11492 bool Increasing = *MonotonicType == ScalarEvolution::MonotonicallyIncreasing;
11493 auto P = Increasing ? Pred : ICmpInst::getInverseCmpPredicate(Pred);
11494
11495 if (isLoopBackedgeGuardedByCond(L, Pred: P, LHS, RHS))
11496 return ScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(),
11497 RHS);
11498
11499 if (!CtxI)
11500 return std::nullopt;
11501 // Try to prove via context.
11502 // TODO: Support other cases.
11503 switch (Pred) {
11504 default:
11505 break;
11506 case ICmpInst::ICMP_ULE:
11507 case ICmpInst::ICMP_ULT: {
11508 assert(ArLHS->hasNoUnsignedWrap() && "Is a requirement of monotonicity!");
11509 // Given preconditions
11510 // (1) ArLHS does not cross the border of positive and negative parts of
11511 // range because of:
11512 // - Positive step; (TODO: lift this limitation)
11513 // - nuw - does not cross zero boundary;
11514 // - nsw - does not cross SINT_MAX boundary;
11515 // (2) ArLHS <s RHS
11516 // (3) RHS >=s 0
11517 // we can replace the loop variant ArLHS <u RHS condition with loop
11518 // invariant Start(ArLHS) <u RHS.
11519 //
11520 // Because of (1) there are two options:
11521 // - ArLHS is always negative. It means that ArLHS <u RHS is always false;
11522 // - ArLHS is always non-negative. Because of (3) RHS is also non-negative.
11523 // It means that ArLHS <s RHS <=> ArLHS <u RHS.
11524 // Because of (2) ArLHS <u RHS is trivially true.
11525 // All together it means that ArLHS <u RHS <=> Start(ArLHS) >=s 0.
11526 // We can strengthen this to Start(ArLHS) <u RHS.
11527 auto SignFlippedPred = ICmpInst::getFlippedSignednessPredicate(Pred);
11528 if (ArLHS->hasNoSignedWrap() && ArLHS->isAffine() &&
11529 isKnownPositive(S: ArLHS->getStepRecurrence(SE&: *this)) &&
11530 isKnownNonNegative(S: RHS) &&
11531 isKnownPredicateAt(Pred: SignFlippedPred, LHS: ArLHS, RHS, CtxI))
11532 return ScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(),
11533 RHS);
11534 }
11535 }
11536
11537 return std::nullopt;
11538}
11539
11540std::optional<ScalarEvolution::LoopInvariantPredicate>
11541ScalarEvolution::getLoopInvariantExitCondDuringFirstIterations(
11542 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
11543 const Instruction *CtxI, const SCEV *MaxIter) {
11544 if (auto LIP = getLoopInvariantExitCondDuringFirstIterationsImpl(
11545 Pred, LHS, RHS, L, CtxI, MaxIter))
11546 return LIP;
11547 if (auto *UMin = dyn_cast<SCEVUMinExpr>(Val: MaxIter))
11548 // Number of iterations expressed as UMIN isn't always great for expressing
11549 // the value on the last iteration. If the straightforward approach didn't
11550 // work, try the following trick: if the a predicate is invariant for X, it
11551 // is also invariant for umin(X, ...). So try to find something that works
11552 // among subexpressions of MaxIter expressed as umin.
11553 for (SCEVUse Op : UMin->operands())
11554 if (auto LIP = getLoopInvariantExitCondDuringFirstIterationsImpl(
11555 Pred, LHS, RHS, L, CtxI, MaxIter: Op))
11556 return LIP;
11557 return std::nullopt;
11558}
11559
11560std::optional<ScalarEvolution::LoopInvariantPredicate>
11561ScalarEvolution::getLoopInvariantExitCondDuringFirstIterationsImpl(
11562 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L,
11563 const Instruction *CtxI, const SCEV *MaxIter) {
11564 // Try to prove the following set of facts:
11565 // - The predicate is monotonic in the iteration space.
11566 // - If the check does not fail on the 1st iteration:
11567 // - No overflow will happen during first MaxIter iterations;
11568 // - It will not fail on the MaxIter'th iteration.
11569 // If the check does fail on the 1st iteration, we leave the loop and no
11570 // other checks matter.
11571
11572 // If there is a loop-invariant, force it into the RHS, otherwise bail out.
11573 if (!isLoopInvariant(S: RHS, L)) {
11574 if (!isLoopInvariant(S: LHS, L))
11575 return std::nullopt;
11576
11577 std::swap(a&: LHS, b&: RHS);
11578 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
11579 }
11580
11581 auto *AR = dyn_cast<SCEVAddRecExpr>(Val: LHS);
11582 if (!AR || AR->getLoop() != L)
11583 return std::nullopt;
11584
11585 // Even if both are valid, we need to consistently chose the unsigned or the
11586 // signed predicate below, not mixtures of both. For now, prefer the unsigned
11587 // predicate.
11588 Pred = Pred.dropSameSign();
11589
11590 // The predicate must be relational (i.e. <, <=, >=, >).
11591 if (!ICmpInst::isRelational(P: Pred))
11592 return std::nullopt;
11593
11594 // TODO: Support steps other than +/- 1.
11595 const SCEV *Step = AR->getStepRecurrence(SE&: *this);
11596 auto *One = getOne(Ty: Step->getType());
11597 auto *MinusOne = getNegativeSCEV(V: One);
11598 if (Step != One && Step != MinusOne)
11599 return std::nullopt;
11600
11601 // Type mismatch here means that MaxIter is potentially larger than max
11602 // unsigned value in start type, which mean we cannot prove no wrap for the
11603 // indvar.
11604 if (AR->getType() != MaxIter->getType())
11605 return std::nullopt;
11606
11607 // Value of IV on suggested last iteration.
11608 const SCEV *Last = AR->evaluateAtIteration(It: MaxIter, SE&: *this);
11609 // Does it still meet the requirement?
11610 if (!isLoopBackedgeGuardedByCond(L, Pred, LHS: Last, RHS))
11611 return std::nullopt;
11612 // Because step is +/- 1 and MaxIter has same type as Start (i.e. it does
11613 // not exceed max unsigned value of this type), this effectively proves
11614 // that there is no wrap during the iteration. To prove that there is no
11615 // signed/unsigned wrap, we need to check that
11616 // Start <= Last for step = 1 or Start >= Last for step = -1.
11617 ICmpInst::Predicate NoOverflowPred =
11618 CmpInst::isSigned(Pred) ? ICmpInst::ICMP_SLE : ICmpInst::ICMP_ULE;
11619 if (Step == MinusOne)
11620 NoOverflowPred = ICmpInst::getSwappedPredicate(pred: NoOverflowPred);
11621 const SCEV *Start = AR->getStart();
11622 if (!isKnownPredicateAt(Pred: NoOverflowPred, LHS: Start, RHS: Last, CtxI))
11623 return std::nullopt;
11624
11625 // Everything is fine.
11626 return ScalarEvolution::LoopInvariantPredicate(Pred, Start, RHS);
11627}
11628
11629bool ScalarEvolution::isKnownPredicateViaConstantRanges(CmpPredicate Pred,
11630 const SCEV *LHS,
11631 const SCEV *RHS) {
11632 if (HasSameValue(A: LHS, B: RHS))
11633 return ICmpInst::isTrueWhenEqual(predicate: Pred);
11634
11635 auto CheckRange = [&](bool IsSigned) {
11636 auto RangeLHS = IsSigned ? getSignedRange(S: LHS) : getUnsignedRange(S: LHS);
11637 auto RangeRHS = IsSigned ? getSignedRange(S: RHS) : getUnsignedRange(S: RHS);
11638 return RangeLHS.icmp(Pred, Other: RangeRHS);
11639 };
11640
11641 // The check at the top of the function catches the case where the values are
11642 // known to be equal.
11643 if (Pred == CmpInst::ICMP_EQ)
11644 return false;
11645
11646 if (Pred == CmpInst::ICMP_NE) {
11647 if (CheckRange(true) || CheckRange(false))
11648 return true;
11649 auto *Diff = getMinusSCEV(LHS, RHS);
11650 return !isa<SCEVCouldNotCompute>(Val: Diff) && isKnownNonZero(S: Diff);
11651 }
11652
11653 return CheckRange(CmpInst::isSigned(Pred));
11654}
11655
11656bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
11657 const SCEV *LHS,
11658 const SCEV *RHS) {
11659 // Match X to (A + C1)<ExpectedFlags> and Y to (A + C2)<ExpectedFlags>, where
11660 // C1 and C2 are constant integers. If either X or Y are not add expressions,
11661 // consider them as X + 0 and Y + 0 respectively. C1 and C2 are returned via
11662 // OutC1 and OutC2.
11663 auto MatchBinaryAddToConst = [this](const SCEV *X, const SCEV *Y,
11664 APInt &OutC1, APInt &OutC2,
11665 SCEV::NoWrapFlags ExpectedFlags) {
11666 const SCEV *XNonConstOp, *XConstOp;
11667 const SCEV *YNonConstOp, *YConstOp;
11668 SCEV::NoWrapFlags XFlagsPresent;
11669 SCEV::NoWrapFlags YFlagsPresent;
11670
11671 if (!splitBinaryAdd(Expr: X, L&: XConstOp, R&: XNonConstOp, Flags&: XFlagsPresent)) {
11672 XConstOp = getZero(Ty: X->getType());
11673 XNonConstOp = X;
11674 XFlagsPresent = ExpectedFlags;
11675 }
11676 if (!isa<SCEVConstant>(Val: XConstOp))
11677 return false;
11678
11679 if (!splitBinaryAdd(Expr: Y, L&: YConstOp, R&: YNonConstOp, Flags&: YFlagsPresent)) {
11680 YConstOp = getZero(Ty: Y->getType());
11681 YNonConstOp = Y;
11682 YFlagsPresent = ExpectedFlags;
11683 }
11684
11685 if (YNonConstOp != XNonConstOp)
11686 return false;
11687
11688 if (!isa<SCEVConstant>(Val: YConstOp))
11689 return false;
11690
11691 // When matching ADDs with NUW flags (and unsigned predicates), only the
11692 // second ADD (with the larger constant) requires NUW.
11693 if ((YFlagsPresent & ExpectedFlags) != ExpectedFlags)
11694 return false;
11695 if (ExpectedFlags != SCEV::FlagNUW &&
11696 (XFlagsPresent & ExpectedFlags) != ExpectedFlags) {
11697 return false;
11698 }
11699
11700 OutC1 = cast<SCEVConstant>(Val: XConstOp)->getAPInt();
11701 OutC2 = cast<SCEVConstant>(Val: YConstOp)->getAPInt();
11702
11703 return true;
11704 };
11705
11706 APInt C1;
11707 APInt C2;
11708
11709 switch (Pred) {
11710 default:
11711 break;
11712
11713 case ICmpInst::ICMP_SGE:
11714 std::swap(a&: LHS, b&: RHS);
11715 [[fallthrough]];
11716 case ICmpInst::ICMP_SLE:
11717 // (X + C1)<nsw> s<= (X + C2)<nsw> if C1 s<= C2.
11718 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.sle(RHS: C2))
11719 return true;
11720
11721 break;
11722
11723 case ICmpInst::ICMP_SGT:
11724 std::swap(a&: LHS, b&: RHS);
11725 [[fallthrough]];
11726 case ICmpInst::ICMP_SLT:
11727 // (X + C1)<nsw> s< (X + C2)<nsw> if C1 s< C2.
11728 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNSW) && C1.slt(RHS: C2))
11729 return true;
11730
11731 break;
11732
11733 case ICmpInst::ICMP_UGE:
11734 std::swap(a&: LHS, b&: RHS);
11735 [[fallthrough]];
11736 case ICmpInst::ICMP_ULE:
11737 // (X + C1) u<= (X + C2)<nuw> for C1 u<= C2.
11738 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNUW) && C1.ule(RHS: C2))
11739 return true;
11740
11741 break;
11742
11743 case ICmpInst::ICMP_UGT:
11744 std::swap(a&: LHS, b&: RHS);
11745 [[fallthrough]];
11746 case ICmpInst::ICMP_ULT:
11747 // (X + C1) u< (X + C2)<nuw> if C1 u< C2.
11748 if (MatchBinaryAddToConst(LHS, RHS, C1, C2, SCEV::FlagNUW) && C1.ult(RHS: C2))
11749 return true;
11750 break;
11751 }
11752
11753 return false;
11754}
11755
11756bool ScalarEvolution::isKnownPredicateViaSplitting(CmpPredicate Pred,
11757 const SCEV *LHS,
11758 const SCEV *RHS) {
11759 if (Pred != ICmpInst::ICMP_ULT || ProvingSplitPredicate)
11760 return false;
11761
11762 // Allowing arbitrary number of activations of isKnownPredicateViaSplitting on
11763 // the stack can result in exponential time complexity.
11764 SaveAndRestore Restore(ProvingSplitPredicate, true);
11765
11766 // If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L
11767 //
11768 // To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use
11769 // isKnownPredicate. isKnownPredicate is more powerful, but also more
11770 // expensive; and using isKnownNonNegative(RHS) is sufficient for most of the
11771 // interesting cases seen in practice. We can consider "upgrading" L >= 0 to
11772 // use isKnownPredicate later if needed.
11773 return isKnownNonNegative(S: RHS) &&
11774 isKnownPredicate(Pred: CmpInst::ICMP_SGE, LHS, RHS: getZero(Ty: LHS->getType())) &&
11775 isKnownPredicate(Pred: CmpInst::ICMP_SLT, LHS, RHS);
11776}
11777
11778bool ScalarEvolution::isImpliedViaGuard(const BasicBlock *BB, CmpPredicate Pred,
11779 const SCEV *LHS, const SCEV *RHS) {
11780 // No need to even try if we know the module has no guards.
11781 if (!HasGuards)
11782 return false;
11783
11784 return any_of(Range: *BB, P: [&](const Instruction &I) {
11785 using namespace llvm::PatternMatch;
11786
11787 Value *Condition;
11788 return match(V: &I, P: m_Intrinsic<Intrinsic::experimental_guard>(
11789 Op0: m_Value(V&: Condition))) &&
11790 isImpliedCond(Pred, LHS, RHS, FoundCondValue: Condition, Inverse: false);
11791 });
11792}
11793
11794/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
11795/// protected by a conditional between LHS and RHS. This is used to
11796/// to eliminate casts.
11797bool ScalarEvolution::isLoopBackedgeGuardedByCond(const Loop *L,
11798 CmpPredicate Pred,
11799 const SCEV *LHS,
11800 const SCEV *RHS) {
11801 // Interpret a null as meaning no loop, where there is obviously no guard
11802 // (interprocedural conditions notwithstanding). Do not bother about
11803 // unreachable loops.
11804 if (!L || !DT.isReachableFromEntry(A: L->getHeader()))
11805 return true;
11806
11807 if (VerifyIR)
11808 assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
11809 "This cannot be done on broken IR!");
11810
11811
11812 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
11813 return true;
11814
11815 BasicBlock *Latch = L->getLoopLatch();
11816 if (!Latch)
11817 return false;
11818
11819 CondBrInst *LoopContinuePredicate =
11820 dyn_cast<CondBrInst>(Val: Latch->getTerminator());
11821 if (LoopContinuePredicate &&
11822 isImpliedCond(Pred, LHS, RHS, FoundCondValue: LoopContinuePredicate->getCondition(),
11823 Inverse: LoopContinuePredicate->getSuccessor(i: 0) != L->getHeader()))
11824 return true;
11825
11826 // We don't want more than one activation of the following loops on the stack
11827 // -- that can lead to O(n!) time complexity.
11828 if (WalkingBEDominatingConds)
11829 return false;
11830
11831 SaveAndRestore ClearOnExit(WalkingBEDominatingConds, true);
11832
11833 // See if we can exploit a trip count to prove the predicate.
11834 const auto &BETakenInfo = getBackedgeTakenInfo(L);
11835 const SCEV *LatchBECount = BETakenInfo.getExact(ExitingBlock: Latch, SE: this);
11836 if (LatchBECount != getCouldNotCompute()) {
11837 // We know that Latch branches back to the loop header exactly
11838 // LatchBECount times. This means the backdege condition at Latch is
11839 // equivalent to "{0,+,1} u< LatchBECount".
11840 Type *Ty = LatchBECount->getType();
11841 auto NoWrapFlags = SCEV::NoWrapFlags(SCEV::FlagNUW | SCEV::FlagNW);
11842 const SCEV *LoopCounter =
11843 getAddRecExpr(Start: getZero(Ty), Step: getOne(Ty), L, Flags: NoWrapFlags);
11844 if (isImpliedCond(Pred, LHS, RHS, FoundPred: ICmpInst::ICMP_ULT, FoundLHS: LoopCounter,
11845 FoundRHS: LatchBECount))
11846 return true;
11847 }
11848
11849 // Check conditions due to any @llvm.assume intrinsics.
11850 for (auto &AssumeVH : AC.assumptions()) {
11851 if (!AssumeVH)
11852 continue;
11853 auto *CI = cast<CallInst>(Val&: AssumeVH);
11854 if (!DT.dominates(Def: CI, User: Latch->getTerminator()))
11855 continue;
11856
11857 if (isImpliedCond(Pred, LHS, RHS, FoundCondValue: CI->getArgOperand(i: 0), Inverse: false))
11858 return true;
11859 }
11860
11861 if (isImpliedViaGuard(BB: Latch, Pred, LHS, RHS))
11862 return true;
11863
11864 for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()];
11865 DTN != HeaderDTN; DTN = DTN->getIDom()) {
11866 assert(DTN && "should reach the loop header before reaching the root!");
11867
11868 BasicBlock *BB = DTN->getBlock();
11869 if (isImpliedViaGuard(BB, Pred, LHS, RHS))
11870 return true;
11871
11872 BasicBlock *PBB = BB->getSinglePredecessor();
11873 if (!PBB)
11874 continue;
11875
11876 CondBrInst *ContBr = dyn_cast<CondBrInst>(Val: PBB->getTerminator());
11877 if (!ContBr || ContBr->getSuccessor(i: 0) == ContBr->getSuccessor(i: 1))
11878 continue;
11879
11880 // If we have an edge `E` within the loop body that dominates the only
11881 // latch, the condition guarding `E` also guards the backedge. This
11882 // reasoning works only for loops with a single latch.
11883 // We're constructively (and conservatively) enumerating edges within the
11884 // loop body that dominate the latch. The dominator tree better agree
11885 // with us on this:
11886 assert(DT.dominates(BasicBlockEdge(PBB, BB), Latch) && "should be!");
11887 if (isImpliedCond(Pred, LHS, RHS, FoundCondValue: ContBr->getCondition(),
11888 Inverse: BB != ContBr->getSuccessor(i: 0)))
11889 return true;
11890 }
11891
11892 return false;
11893}
11894
11895bool ScalarEvolution::isBasicBlockEntryGuardedByCond(const BasicBlock *BB,
11896 CmpPredicate Pred,
11897 const SCEV *LHS,
11898 const SCEV *RHS) {
11899 // Do not bother proving facts for unreachable code.
11900 if (!DT.isReachableFromEntry(A: BB))
11901 return true;
11902 if (VerifyIR)
11903 assert(!verifyFunction(*BB->getParent(), &dbgs()) &&
11904 "This cannot be done on broken IR!");
11905
11906 // If we cannot prove strict comparison (e.g. a > b), maybe we can prove
11907 // the facts (a >= b && a != b) separately. A typical situation is when the
11908 // non-strict comparison is known from ranges and non-equality is known from
11909 // dominating predicates. If we are proving strict comparison, we always try
11910 // to prove non-equality and non-strict comparison separately.
11911 CmpPredicate NonStrictPredicate = ICmpInst::getNonStrictCmpPredicate(Pred);
11912 const bool ProvingStrictComparison =
11913 Pred != NonStrictPredicate.dropSameSign();
11914 bool ProvedNonStrictComparison = false;
11915 bool ProvedNonEquality = false;
11916
11917 auto SplitAndProve = [&](std::function<bool(CmpPredicate)> Fn) -> bool {
11918 if (!ProvedNonStrictComparison)
11919 ProvedNonStrictComparison = Fn(NonStrictPredicate);
11920 if (!ProvedNonEquality)
11921 ProvedNonEquality = Fn(ICmpInst::ICMP_NE);
11922 if (ProvedNonStrictComparison && ProvedNonEquality)
11923 return true;
11924 return false;
11925 };
11926
11927 if (ProvingStrictComparison) {
11928 auto ProofFn = [&](CmpPredicate P) {
11929 return isKnownViaNonRecursiveReasoning(Pred: P, LHS, RHS);
11930 };
11931 if (SplitAndProve(ProofFn))
11932 return true;
11933 }
11934
11935 // Try to prove (Pred, LHS, RHS) using isImpliedCond.
11936 auto ProveViaCond = [&](const Value *Condition, bool Inverse) {
11937 const Instruction *CtxI = &BB->front();
11938 if (isImpliedCond(Pred, LHS, RHS, FoundCondValue: Condition, Inverse, Context: CtxI))
11939 return true;
11940 if (ProvingStrictComparison) {
11941 auto ProofFn = [&](CmpPredicate P) {
11942 return isImpliedCond(Pred: P, LHS, RHS, FoundCondValue: Condition, Inverse, Context: CtxI);
11943 };
11944 if (SplitAndProve(ProofFn))
11945 return true;
11946 }
11947 return false;
11948 };
11949
11950 // Starting at the block's predecessor, climb up the predecessor chain, as long
11951 // as there are predecessors that can be found that have unique successors
11952 // leading to the original block.
11953 const Loop *ContainingLoop = LI.getLoopFor(BB);
11954 const BasicBlock *PredBB;
11955 if (ContainingLoop && ContainingLoop->getHeader() == BB)
11956 PredBB = ContainingLoop->getLoopPredecessor();
11957 else
11958 PredBB = BB->getSinglePredecessor();
11959 for (std::pair<const BasicBlock *, const BasicBlock *> Pair(PredBB, BB);
11960 Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(BB: Pair.first)) {
11961 const CondBrInst *BlockEntryPredicate =
11962 dyn_cast<CondBrInst>(Val: Pair.first->getTerminator());
11963 if (!BlockEntryPredicate)
11964 continue;
11965
11966 if (ProveViaCond(BlockEntryPredicate->getCondition(),
11967 BlockEntryPredicate->getSuccessor(i: 0) != Pair.second))
11968 return true;
11969 }
11970
11971 // Check conditions due to any @llvm.assume intrinsics.
11972 for (auto &AssumeVH : AC.assumptions()) {
11973 if (!AssumeVH)
11974 continue;
11975 auto *CI = cast<CallInst>(Val&: AssumeVH);
11976 if (!DT.dominates(Def: CI, BB))
11977 continue;
11978
11979 if (ProveViaCond(CI->getArgOperand(i: 0), false))
11980 return true;
11981 }
11982
11983 // Check conditions due to any @llvm.experimental.guard intrinsics.
11984 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
11985 M: F.getParent(), id: Intrinsic::experimental_guard);
11986 if (GuardDecl)
11987 for (const auto *GU : GuardDecl->users())
11988 if (const auto *Guard = dyn_cast<IntrinsicInst>(Val: GU))
11989 if (Guard->getFunction() == BB->getParent() && DT.dominates(Def: Guard, BB))
11990 if (ProveViaCond(Guard->getArgOperand(i: 0), false))
11991 return true;
11992 return false;
11993}
11994
11995bool ScalarEvolution::isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred,
11996 const SCEV *LHS,
11997 const SCEV *RHS) {
11998 // Interpret a null as meaning no loop, where there is obviously no guard
11999 // (interprocedural conditions notwithstanding).
12000 if (!L)
12001 return false;
12002
12003 // Both LHS and RHS must be available at loop entry.
12004 assert(isAvailableAtLoopEntry(LHS, L) &&
12005 "LHS is not available at Loop Entry");
12006 assert(isAvailableAtLoopEntry(RHS, L) &&
12007 "RHS is not available at Loop Entry");
12008
12009 if (isKnownViaNonRecursiveReasoning(Pred, LHS, RHS))
12010 return true;
12011
12012 return isBasicBlockEntryGuardedByCond(BB: L->getHeader(), Pred, LHS, RHS);
12013}
12014
12015bool ScalarEvolution::isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
12016 const SCEV *RHS,
12017 const Value *FoundCondValue, bool Inverse,
12018 const Instruction *CtxI) {
12019 // False conditions implies anything. Do not bother analyzing it further.
12020 if (FoundCondValue ==
12021 ConstantInt::getBool(Context&: FoundCondValue->getContext(), V: Inverse))
12022 return true;
12023
12024 if (!PendingLoopPredicates.insert(Ptr: FoundCondValue).second)
12025 return false;
12026
12027 llvm::scope_exit ClearOnExit(
12028 [&]() { PendingLoopPredicates.erase(Ptr: FoundCondValue); });
12029
12030 // Recursively handle And and Or conditions.
12031 const Value *Op0, *Op1;
12032 if (match(V: FoundCondValue, P: m_LogicalAnd(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
12033 if (!Inverse)
12034 return isImpliedCond(Pred, LHS, RHS, FoundCondValue: Op0, Inverse, CtxI) ||
12035 isImpliedCond(Pred, LHS, RHS, FoundCondValue: Op1, Inverse, CtxI);
12036 } else if (match(V: FoundCondValue, P: m_LogicalOr(L: m_Value(V&: Op0), R: m_Value(V&: Op1)))) {
12037 if (Inverse)
12038 return isImpliedCond(Pred, LHS, RHS, FoundCondValue: Op0, Inverse, CtxI) ||
12039 isImpliedCond(Pred, LHS, RHS, FoundCondValue: Op1, Inverse, CtxI);
12040 }
12041
12042 const ICmpInst *ICI = dyn_cast<ICmpInst>(Val: FoundCondValue);
12043 if (!ICI) return false;
12044
12045 // Now that we found a conditional branch that dominates the loop or controls
12046 // the loop latch. Check to see if it is the comparison we are looking for.
12047 CmpPredicate FoundPred;
12048 if (Inverse)
12049 FoundPred = ICI->getInverseCmpPredicate();
12050 else
12051 FoundPred = ICI->getCmpPredicate();
12052
12053 const SCEV *FoundLHS = getSCEV(V: ICI->getOperand(i_nocapture: 0));
12054 const SCEV *FoundRHS = getSCEV(V: ICI->getOperand(i_nocapture: 1));
12055
12056 return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS, Context: CtxI);
12057}
12058
12059bool ScalarEvolution::isImpliedCond(CmpPredicate Pred, const SCEV *LHS,
12060 const SCEV *RHS, CmpPredicate FoundPred,
12061 const SCEV *FoundLHS, const SCEV *FoundRHS,
12062 const Instruction *CtxI) {
12063 // Balance the types.
12064 if (getTypeSizeInBits(Ty: LHS->getType()) <
12065 getTypeSizeInBits(Ty: FoundLHS->getType())) {
12066 // For unsigned and equality predicates, try to prove that both found
12067 // operands fit into narrow unsigned range. If so, try to prove facts in
12068 // narrow types.
12069 if (!CmpInst::isSigned(Pred: FoundPred) && !FoundLHS->getType()->isPointerTy() &&
12070 !FoundRHS->getType()->isPointerTy()) {
12071 auto *NarrowType = LHS->getType();
12072 auto *WideType = FoundLHS->getType();
12073 auto BitWidth = getTypeSizeInBits(Ty: NarrowType);
12074 const SCEV *MaxValue = getZeroExtendExpr(
12075 Op: getConstant(Val: APInt::getMaxValue(numBits: BitWidth)), Ty: WideType);
12076 if (isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_ULE, LHS: FoundLHS,
12077 RHS: MaxValue) &&
12078 isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_ULE, LHS: FoundRHS,
12079 RHS: MaxValue)) {
12080 const SCEV *TruncFoundLHS = getTruncateExpr(Op: FoundLHS, Ty: NarrowType);
12081 const SCEV *TruncFoundRHS = getTruncateExpr(Op: FoundRHS, Ty: NarrowType);
12082 // We cannot preserve samesign after truncation.
12083 if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred: FoundPred.dropSameSign(),
12084 FoundLHS: TruncFoundLHS, FoundRHS: TruncFoundRHS, CtxI))
12085 return true;
12086 }
12087 }
12088
12089 if (LHS->getType()->isPointerTy() || RHS->getType()->isPointerTy())
12090 return false;
12091 if (CmpInst::isSigned(Pred)) {
12092 LHS = getSignExtendExpr(Op: LHS, Ty: FoundLHS->getType());
12093 RHS = getSignExtendExpr(Op: RHS, Ty: FoundLHS->getType());
12094 } else {
12095 LHS = getZeroExtendExpr(Op: LHS, Ty: FoundLHS->getType());
12096 RHS = getZeroExtendExpr(Op: RHS, Ty: FoundLHS->getType());
12097 }
12098 } else if (getTypeSizeInBits(Ty: LHS->getType()) >
12099 getTypeSizeInBits(Ty: FoundLHS->getType())) {
12100 if (FoundLHS->getType()->isPointerTy() || FoundRHS->getType()->isPointerTy())
12101 return false;
12102 if (CmpInst::isSigned(Pred: FoundPred)) {
12103 FoundLHS = getSignExtendExpr(Op: FoundLHS, Ty: LHS->getType());
12104 FoundRHS = getSignExtendExpr(Op: FoundRHS, Ty: LHS->getType());
12105 } else {
12106 FoundLHS = getZeroExtendExpr(Op: FoundLHS, Ty: LHS->getType());
12107 FoundRHS = getZeroExtendExpr(Op: FoundRHS, Ty: LHS->getType());
12108 }
12109 }
12110 return isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, FoundLHS,
12111 FoundRHS, CtxI);
12112}
12113
12114bool ScalarEvolution::isImpliedCondBalancedTypes(
12115 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, CmpPredicate FoundPred,
12116 const SCEV *FoundLHS, const SCEV *FoundRHS, const Instruction *CtxI) {
12117 assert(getTypeSizeInBits(LHS->getType()) ==
12118 getTypeSizeInBits(FoundLHS->getType()) &&
12119 "Types should be balanced!");
12120 // Canonicalize the query to match the way instcombine will have
12121 // canonicalized the comparison.
12122 if (SimplifyICmpOperands(Pred, LHS, RHS))
12123 if (LHS == RHS)
12124 return CmpInst::isTrueWhenEqual(predicate: Pred);
12125 if (SimplifyICmpOperands(Pred&: FoundPred, LHS&: FoundLHS, RHS&: FoundRHS))
12126 if (FoundLHS == FoundRHS)
12127 return CmpInst::isFalseWhenEqual(predicate: FoundPred);
12128
12129 // Check to see if we can make the LHS or RHS match.
12130 if (LHS == FoundRHS || RHS == FoundLHS) {
12131 if (isa<SCEVConstant>(Val: RHS)) {
12132 std::swap(a&: FoundLHS, b&: FoundRHS);
12133 FoundPred = ICmpInst::getSwappedCmpPredicate(Pred: FoundPred);
12134 } else {
12135 std::swap(a&: LHS, b&: RHS);
12136 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
12137 }
12138 }
12139
12140 // Check whether the found predicate is the same as the desired predicate.
12141 if (auto P = CmpPredicate::getMatching(A: FoundPred, B: Pred))
12142 return isImpliedCondOperands(Pred: *P, LHS, RHS, FoundLHS, FoundRHS, Context: CtxI);
12143
12144 // Check whether swapping the found predicate makes it the same as the
12145 // desired predicate.
12146 if (auto P = CmpPredicate::getMatching(
12147 A: ICmpInst::getSwappedCmpPredicate(Pred: FoundPred), B: Pred)) {
12148 // We can write the implication
12149 // 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS
12150 // using one of the following ways:
12151 // 1. LHS Pred RHS <- FoundRHS Pred FoundLHS
12152 // 2. RHS SwapPred LHS <- FoundLHS SwapPred FoundRHS
12153 // 3. LHS Pred RHS <- ~FoundLHS Pred ~FoundRHS
12154 // 4. ~LHS SwapPred ~RHS <- FoundLHS SwapPred FoundRHS
12155 // Forms 1. and 2. require swapping the operands of one condition. Don't
12156 // do this if it would break canonical constant/addrec ordering.
12157 if (!isa<SCEVConstant>(Val: RHS) && !isa<SCEVAddRecExpr>(Val: LHS))
12158 return isImpliedCondOperands(Pred: ICmpInst::getSwappedCmpPredicate(Pred: *P), LHS: RHS,
12159 RHS: LHS, FoundLHS, FoundRHS, Context: CtxI);
12160 if (!isa<SCEVConstant>(Val: FoundRHS) && !isa<SCEVAddRecExpr>(Val: FoundLHS))
12161 return isImpliedCondOperands(Pred: *P, LHS, RHS, FoundLHS: FoundRHS, FoundRHS: FoundLHS, Context: CtxI);
12162
12163 // There's no clear preference between forms 3. and 4., try both. Avoid
12164 // forming getNotSCEV of pointer values as the resulting subtract is
12165 // not legal.
12166 if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() &&
12167 isImpliedCondOperands(Pred: ICmpInst::getSwappedCmpPredicate(Pred: *P),
12168 LHS: getNotSCEV(V: LHS), RHS: getNotSCEV(V: RHS), FoundLHS,
12169 FoundRHS, Context: CtxI))
12170 return true;
12171
12172 if (!FoundLHS->getType()->isPointerTy() &&
12173 !FoundRHS->getType()->isPointerTy() &&
12174 isImpliedCondOperands(Pred: *P, LHS, RHS, FoundLHS: getNotSCEV(V: FoundLHS),
12175 FoundRHS: getNotSCEV(V: FoundRHS), Context: CtxI))
12176 return true;
12177
12178 return false;
12179 }
12180
12181 auto IsSignFlippedPredicate = [](CmpInst::Predicate P1,
12182 CmpInst::Predicate P2) {
12183 assert(P1 != P2 && "Handled earlier!");
12184 return CmpInst::isRelational(P: P2) &&
12185 P1 == ICmpInst::getFlippedSignednessPredicate(Pred: P2);
12186 };
12187 if (IsSignFlippedPredicate(Pred, FoundPred)) {
12188 // Unsigned comparison is the same as signed comparison when both the
12189 // operands are non-negative or negative.
12190 if (haveSameSign(S1: FoundLHS, S2: FoundRHS))
12191 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, Context: CtxI);
12192 // Create local copies that we can freely swap and canonicalize our
12193 // conditions to "le/lt".
12194 CmpPredicate CanonicalPred = Pred, CanonicalFoundPred = FoundPred;
12195 const SCEV *CanonicalLHS = LHS, *CanonicalRHS = RHS,
12196 *CanonicalFoundLHS = FoundLHS, *CanonicalFoundRHS = FoundRHS;
12197 if (ICmpInst::isGT(P: CanonicalPred) || ICmpInst::isGE(P: CanonicalPred)) {
12198 CanonicalPred = ICmpInst::getSwappedCmpPredicate(Pred: CanonicalPred);
12199 CanonicalFoundPred = ICmpInst::getSwappedCmpPredicate(Pred: CanonicalFoundPred);
12200 std::swap(a&: CanonicalLHS, b&: CanonicalRHS);
12201 std::swap(a&: CanonicalFoundLHS, b&: CanonicalFoundRHS);
12202 }
12203 assert((ICmpInst::isLT(CanonicalPred) || ICmpInst::isLE(CanonicalPred)) &&
12204 "Must be!");
12205 assert((ICmpInst::isLT(CanonicalFoundPred) ||
12206 ICmpInst::isLE(CanonicalFoundPred)) &&
12207 "Must be!");
12208 if (ICmpInst::isSigned(Pred: CanonicalPred) && isKnownNonNegative(S: CanonicalRHS))
12209 // Use implication:
12210 // x <u y && y >=s 0 --> x <s y.
12211 // If we can prove the left part, the right part is also proven.
12212 return isImpliedCondOperands(Pred: CanonicalFoundPred, LHS: CanonicalLHS,
12213 RHS: CanonicalRHS, FoundLHS: CanonicalFoundLHS,
12214 FoundRHS: CanonicalFoundRHS);
12215 if (ICmpInst::isUnsigned(Pred: CanonicalPred) && isKnownNegative(S: CanonicalRHS))
12216 // Use implication:
12217 // x <s y && y <s 0 --> x <u y.
12218 // If we can prove the left part, the right part is also proven.
12219 return isImpliedCondOperands(Pred: CanonicalFoundPred, LHS: CanonicalLHS,
12220 RHS: CanonicalRHS, FoundLHS: CanonicalFoundLHS,
12221 FoundRHS: CanonicalFoundRHS);
12222 }
12223
12224 // Check if we can make progress by sharpening ranges.
12225 if (FoundPred == ICmpInst::ICMP_NE &&
12226 (isa<SCEVConstant>(Val: FoundLHS) || isa<SCEVConstant>(Val: FoundRHS))) {
12227
12228 const SCEVConstant *C = nullptr;
12229 const SCEV *V = nullptr;
12230
12231 if (isa<SCEVConstant>(Val: FoundLHS)) {
12232 C = cast<SCEVConstant>(Val: FoundLHS);
12233 V = FoundRHS;
12234 } else {
12235 C = cast<SCEVConstant>(Val: FoundRHS);
12236 V = FoundLHS;
12237 }
12238
12239 // The guarding predicate tells us that C != V. If the known range
12240 // of V is [C, t), we can sharpen the range to [C + 1, t). The
12241 // range we consider has to correspond to same signedness as the
12242 // predicate we're interested in folding.
12243
12244 APInt Min = ICmpInst::isSigned(Pred) ?
12245 getSignedRangeMin(S: V) : getUnsignedRangeMin(S: V);
12246
12247 if (Min == C->getAPInt()) {
12248 // Given (V >= Min && V != Min) we conclude V >= (Min + 1).
12249 // This is true even if (Min + 1) wraps around -- in case of
12250 // wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)).
12251
12252 APInt SharperMin = Min + 1;
12253
12254 switch (Pred) {
12255 case ICmpInst::ICMP_SGE:
12256 case ICmpInst::ICMP_UGE:
12257 // We know V `Pred` SharperMin. If this implies LHS `Pred`
12258 // RHS, we're done.
12259 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS: V, FoundRHS: getConstant(Val: SharperMin),
12260 Context: CtxI))
12261 return true;
12262 [[fallthrough]];
12263
12264 case ICmpInst::ICMP_SGT:
12265 case ICmpInst::ICMP_UGT:
12266 // We know from the range information that (V `Pred` Min ||
12267 // V == Min). We know from the guarding condition that !(V
12268 // == Min). This gives us
12269 //
12270 // V `Pred` Min || V == Min && !(V == Min)
12271 // => V `Pred` Min
12272 //
12273 // If V `Pred` Min implies LHS `Pred` RHS, we're done.
12274
12275 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS: V, FoundRHS: getConstant(Val: Min), Context: CtxI))
12276 return true;
12277 break;
12278
12279 // `LHS < RHS` and `LHS <= RHS` are handled in the same way as `RHS > LHS` and `RHS >= LHS` respectively.
12280 case ICmpInst::ICMP_SLE:
12281 case ICmpInst::ICMP_ULE:
12282 if (isImpliedCondOperands(Pred: ICmpInst::getSwappedCmpPredicate(Pred), LHS: RHS,
12283 RHS: LHS, FoundLHS: V, FoundRHS: getConstant(Val: SharperMin), Context: CtxI))
12284 return true;
12285 [[fallthrough]];
12286
12287 case ICmpInst::ICMP_SLT:
12288 case ICmpInst::ICMP_ULT:
12289 if (isImpliedCondOperands(Pred: ICmpInst::getSwappedCmpPredicate(Pred), LHS: RHS,
12290 RHS: LHS, FoundLHS: V, FoundRHS: getConstant(Val: Min), Context: CtxI))
12291 return true;
12292 break;
12293
12294 default:
12295 // No change
12296 break;
12297 }
12298 }
12299 }
12300
12301 // Check whether the actual condition is beyond sufficient.
12302 if (FoundPred == ICmpInst::ICMP_EQ)
12303 if (ICmpInst::isTrueWhenEqual(predicate: Pred))
12304 if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, Context: CtxI))
12305 return true;
12306 if (Pred == ICmpInst::ICMP_NE)
12307 if (!ICmpInst::isTrueWhenEqual(predicate: FoundPred))
12308 if (isImpliedCondOperands(Pred: FoundPred, LHS, RHS, FoundLHS, FoundRHS, Context: CtxI))
12309 return true;
12310
12311 if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS))
12312 return true;
12313
12314 // Otherwise assume the worst.
12315 return false;
12316}
12317
12318bool ScalarEvolution::splitBinaryAdd(const SCEV *Expr,
12319 const SCEV *&L, const SCEV *&R,
12320 SCEV::NoWrapFlags &Flags) {
12321 if (!match(S: Expr, P: m_scev_Add(Op0: m_SCEV(V&: L), Op1: m_SCEV(V&: R))))
12322 return false;
12323
12324 Flags = cast<SCEVAddExpr>(Val: Expr)->getNoWrapFlags();
12325 return true;
12326}
12327
12328std::optional<APInt>
12329ScalarEvolution::computeConstantDifference(const SCEV *More, const SCEV *Less) {
12330 // We avoid subtracting expressions here because this function is usually
12331 // fairly deep in the call stack (i.e. is called many times).
12332
12333 unsigned BW = getTypeSizeInBits(Ty: More->getType());
12334 APInt Diff(BW, 0);
12335 APInt DiffMul(BW, 1);
12336 // Try various simplifications to reduce the difference to a constant. Limit
12337 // the number of allowed simplifications to keep compile-time low.
12338 for (unsigned I = 0; I < 8; ++I) {
12339 if (More == Less)
12340 return Diff;
12341
12342 // Reduce addrecs with identical steps to their start value.
12343 if (isa<SCEVAddRecExpr>(Val: Less) && isa<SCEVAddRecExpr>(Val: More)) {
12344 const auto *LAR = cast<SCEVAddRecExpr>(Val: Less);
12345 const auto *MAR = cast<SCEVAddRecExpr>(Val: More);
12346
12347 if (LAR->getLoop() != MAR->getLoop())
12348 return std::nullopt;
12349
12350 // We look at affine expressions only; not for correctness but to keep
12351 // getStepRecurrence cheap.
12352 if (!LAR->isAffine() || !MAR->isAffine())
12353 return std::nullopt;
12354
12355 if (LAR->getStepRecurrence(SE&: *this) != MAR->getStepRecurrence(SE&: *this))
12356 return std::nullopt;
12357
12358 Less = LAR->getStart();
12359 More = MAR->getStart();
12360 continue;
12361 }
12362
12363 // Try to match a common constant multiply.
12364 auto MatchConstMul =
12365 [](const SCEV *S) -> std::optional<std::pair<const SCEV *, APInt>> {
12366 const APInt *C;
12367 const SCEV *Op;
12368 if (match(S, P: m_scev_Mul(Op0: m_scev_APInt(C), Op1: m_SCEV(V&: Op))))
12369 return {{Op, *C}};
12370 return std::nullopt;
12371 };
12372 if (auto MatchedMore = MatchConstMul(More)) {
12373 if (auto MatchedLess = MatchConstMul(Less)) {
12374 if (MatchedMore->second == MatchedLess->second) {
12375 More = MatchedMore->first;
12376 Less = MatchedLess->first;
12377 DiffMul *= MatchedMore->second;
12378 continue;
12379 }
12380 }
12381 }
12382
12383 // Try to cancel out common factors in two add expressions.
12384 SmallDenseMap<const SCEV *, int, 8> Multiplicity;
12385 auto Add = [&](const SCEV *S, int Mul) {
12386 if (auto *C = dyn_cast<SCEVConstant>(Val: S)) {
12387 if (Mul == 1) {
12388 Diff += C->getAPInt() * DiffMul;
12389 } else {
12390 assert(Mul == -1);
12391 Diff -= C->getAPInt() * DiffMul;
12392 }
12393 } else
12394 Multiplicity[S] += Mul;
12395 };
12396 auto Decompose = [&](const SCEV *S, int Mul) {
12397 if (isa<SCEVAddExpr>(Val: S)) {
12398 for (const SCEV *Op : S->operands())
12399 Add(Op, Mul);
12400 } else
12401 Add(S, Mul);
12402 };
12403 Decompose(More, 1);
12404 Decompose(Less, -1);
12405
12406 // Check whether all the non-constants cancel out, or reduce to new
12407 // More/Less values.
12408 const SCEV *NewMore = nullptr, *NewLess = nullptr;
12409 for (const auto &[S, Mul] : Multiplicity) {
12410 if (Mul == 0)
12411 continue;
12412 if (Mul == 1) {
12413 if (NewMore)
12414 return std::nullopt;
12415 NewMore = S;
12416 } else if (Mul == -1) {
12417 if (NewLess)
12418 return std::nullopt;
12419 NewLess = S;
12420 } else
12421 return std::nullopt;
12422 }
12423
12424 // Values stayed the same, no point in trying further.
12425 if (NewMore == More || NewLess == Less)
12426 return std::nullopt;
12427
12428 More = NewMore;
12429 Less = NewLess;
12430
12431 // Reduced to constant.
12432 if (!More && !Less)
12433 return Diff;
12434
12435 // Left with variable on only one side, bail out.
12436 if (!More || !Less)
12437 return std::nullopt;
12438 }
12439
12440 // Did not reduce to constant.
12441 return std::nullopt;
12442}
12443
12444bool ScalarEvolution::isImpliedCondOperandsViaAddRecStart(
12445 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const SCEV *FoundLHS,
12446 const SCEV *FoundRHS, const Instruction *CtxI) {
12447 // Try to recognize the following pattern:
12448 //
12449 // FoundRHS = ...
12450 // ...
12451 // loop:
12452 // FoundLHS = {Start,+,W}
12453 // context_bb: // Basic block from the same loop
12454 // known(Pred, FoundLHS, FoundRHS)
12455 //
12456 // If some predicate is known in the context of a loop, it is also known on
12457 // each iteration of this loop, including the first iteration. Therefore, in
12458 // this case, `FoundLHS Pred FoundRHS` implies `Start Pred FoundRHS`. Try to
12459 // prove the original pred using this fact.
12460 if (!CtxI)
12461 return false;
12462 const BasicBlock *ContextBB = CtxI->getParent();
12463 // Make sure AR varies in the context block.
12464 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Val: FoundLHS)) {
12465 const Loop *L = AR->getLoop();
12466 const auto *Latch = L->getLoopLatch();
12467 // Make sure that context belongs to the loop and executes on 1st iteration
12468 // (if it ever executes at all).
12469 if (!L->contains(BB: ContextBB) || !Latch || !DT.dominates(A: ContextBB, B: Latch))
12470 return false;
12471 if (!isAvailableAtLoopEntry(S: FoundRHS, L: AR->getLoop()))
12472 return false;
12473 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS: AR->getStart(), FoundRHS);
12474 }
12475
12476 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Val: FoundRHS)) {
12477 const Loop *L = AR->getLoop();
12478 const auto *Latch = L->getLoopLatch();
12479 // Make sure that context belongs to the loop and executes on 1st iteration
12480 // (if it ever executes at all).
12481 if (!L->contains(BB: ContextBB) || !Latch || !DT.dominates(A: ContextBB, B: Latch))
12482 return false;
12483 if (!isAvailableAtLoopEntry(S: FoundLHS, L: AR->getLoop()))
12484 return false;
12485 return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS: AR->getStart());
12486 }
12487
12488 return false;
12489}
12490
12491bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(CmpPredicate Pred,
12492 const SCEV *LHS,
12493 const SCEV *RHS,
12494 const SCEV *FoundLHS,
12495 const SCEV *FoundRHS) {
12496 if (Pred != CmpInst::ICMP_SLT && Pred != CmpInst::ICMP_ULT)
12497 return false;
12498
12499 const auto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(Val: LHS);
12500 if (!AddRecLHS)
12501 return false;
12502
12503 const auto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(Val: FoundLHS);
12504 if (!AddRecFoundLHS)
12505 return false;
12506
12507 // We'd like to let SCEV reason about control dependencies, so we constrain
12508 // both the inequalities to be about add recurrences on the same loop. This
12509 // way we can use isLoopEntryGuardedByCond later.
12510
12511 const Loop *L = AddRecFoundLHS->getLoop();
12512 if (L != AddRecLHS->getLoop())
12513 return false;
12514
12515 // FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1)
12516 //
12517 // FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
12518 // ... (2)
12519 //
12520 // Informal proof for (2), assuming (1) [*]:
12521 //
12522 // We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
12523 //
12524 // Then
12525 //
12526 // FoundLHS s< FoundRHS s< INT_MIN - C
12527 // <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ]
12528 // <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
12529 // <=> (FoundLHS + INT_MIN + C + INT_MIN) s<
12530 // (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
12531 // <=> FoundLHS + C s< FoundRHS + C
12532 //
12533 // [*]: (1) can be proved by ruling out overflow.
12534 //
12535 // [**]: This can be proved by analyzing all the four possibilities:
12536 // (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
12537 // (A s>= 0, B s>= 0).
12538 //
12539 // Note:
12540 // Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
12541 // will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS
12542 // = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS
12543 // s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is
12544 // neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
12545 // C)".
12546
12547 std::optional<APInt> LDiff = computeConstantDifference(More: LHS, Less: FoundLHS);
12548 if (!LDiff)
12549 return false;
12550 std::optional<APInt> RDiff = computeConstantDifference(More: RHS, Less: FoundRHS);
12551 if (!RDiff || *LDiff != *RDiff)
12552 return false;
12553
12554 if (LDiff->isMinValue())
12555 return true;
12556
12557 APInt FoundRHSLimit;
12558
12559 if (Pred == CmpInst::ICMP_ULT) {
12560 FoundRHSLimit = -(*RDiff);
12561 } else {
12562 assert(Pred == CmpInst::ICMP_SLT && "Checked above!");
12563 FoundRHSLimit = APInt::getSignedMinValue(numBits: getTypeSizeInBits(Ty: RHS->getType())) - *RDiff;
12564 }
12565
12566 // Try to prove (1) or (2), as needed.
12567 return isAvailableAtLoopEntry(S: FoundRHS, L) &&
12568 isLoopEntryGuardedByCond(L, Pred, LHS: FoundRHS,
12569 RHS: getConstant(Val: FoundRHSLimit));
12570}
12571
12572bool ScalarEvolution::isImpliedViaMerge(CmpPredicate Pred, const SCEV *LHS,
12573 const SCEV *RHS, const SCEV *FoundLHS,
12574 const SCEV *FoundRHS, unsigned Depth) {
12575 const PHINode *LPhi = nullptr, *RPhi = nullptr;
12576
12577 llvm::scope_exit ClearOnExit([&]() {
12578 if (LPhi) {
12579 bool Erased = PendingMerges.erase(Ptr: LPhi);
12580 assert(Erased && "Failed to erase LPhi!");
12581 (void)Erased;
12582 }
12583 if (RPhi) {
12584 bool Erased = PendingMerges.erase(Ptr: RPhi);
12585 assert(Erased && "Failed to erase RPhi!");
12586 (void)Erased;
12587 }
12588 });
12589
12590 // Find respective Phis and check that they are not being pending.
12591 if (const SCEVUnknown *LU = dyn_cast<SCEVUnknown>(Val: LHS))
12592 if (auto *Phi = dyn_cast<PHINode>(Val: LU->getValue())) {
12593 if (!PendingMerges.insert(Ptr: Phi).second)
12594 return false;
12595 LPhi = Phi;
12596 }
12597 if (const SCEVUnknown *RU = dyn_cast<SCEVUnknown>(Val: RHS))
12598 if (auto *Phi = dyn_cast<PHINode>(Val: RU->getValue())) {
12599 // If we detect a loop of Phi nodes being processed by this method, for
12600 // example:
12601 //
12602 // %a = phi i32 [ %some1, %preheader ], [ %b, %latch ]
12603 // %b = phi i32 [ %some2, %preheader ], [ %a, %latch ]
12604 //
12605 // we don't want to deal with a case that complex, so return conservative
12606 // answer false.
12607 if (!PendingMerges.insert(Ptr: Phi).second)
12608 return false;
12609 RPhi = Phi;
12610 }
12611
12612 // If none of LHS, RHS is a Phi, nothing to do here.
12613 if (!LPhi && !RPhi)
12614 return false;
12615
12616 // If there is a SCEVUnknown Phi we are interested in, make it left.
12617 if (!LPhi) {
12618 std::swap(a&: LHS, b&: RHS);
12619 std::swap(a&: FoundLHS, b&: FoundRHS);
12620 std::swap(a&: LPhi, b&: RPhi);
12621 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
12622 }
12623
12624 assert(LPhi && "LPhi should definitely be a SCEVUnknown Phi!");
12625 const BasicBlock *LBB = LPhi->getParent();
12626 const SCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(Val: RHS);
12627
12628 auto ProvedEasily = [&](const SCEV *S1, const SCEV *S2) {
12629 return isKnownViaNonRecursiveReasoning(Pred, LHS: S1, RHS: S2) ||
12630 isImpliedCondOperandsViaRanges(Pred, LHS: S1, RHS: S2, FoundPred: Pred, FoundLHS, FoundRHS) ||
12631 isImpliedViaOperations(Pred, LHS: S1, RHS: S2, FoundLHS, FoundRHS, Depth);
12632 };
12633
12634 if (RPhi && RPhi->getParent() == LBB) {
12635 // Case one: RHS is also a SCEVUnknown Phi from the same basic block.
12636 // If we compare two Phis from the same block, and for each entry block
12637 // the predicate is true for incoming values from this block, then the
12638 // predicate is also true for the Phis.
12639 for (const BasicBlock *IncBB : predecessors(BB: LBB)) {
12640 const SCEV *L = getSCEV(V: LPhi->getIncomingValueForBlock(BB: IncBB));
12641 const SCEV *R = getSCEV(V: RPhi->getIncomingValueForBlock(BB: IncBB));
12642 if (!ProvedEasily(L, R))
12643 return false;
12644 }
12645 } else if (RAR && RAR->getLoop()->getHeader() == LBB) {
12646 // Case two: RHS is also a Phi from the same basic block, and it is an
12647 // AddRec. It means that there is a loop which has both AddRec and Unknown
12648 // PHIs, for it we can compare incoming values of AddRec from above the loop
12649 // and latch with their respective incoming values of LPhi.
12650 // TODO: Generalize to handle loops with many inputs in a header.
12651 if (LPhi->getNumIncomingValues() != 2) return false;
12652
12653 auto *RLoop = RAR->getLoop();
12654 auto *Predecessor = RLoop->getLoopPredecessor();
12655 assert(Predecessor && "Loop with AddRec with no predecessor?");
12656 const SCEV *L1 = getSCEV(V: LPhi->getIncomingValueForBlock(BB: Predecessor));
12657 if (!ProvedEasily(L1, RAR->getStart()))
12658 return false;
12659 auto *Latch = RLoop->getLoopLatch();
12660 assert(Latch && "Loop with AddRec with no latch?");
12661 const SCEV *L2 = getSCEV(V: LPhi->getIncomingValueForBlock(BB: Latch));
12662 if (!ProvedEasily(L2, RAR->getPostIncExpr(SE&: *this)))
12663 return false;
12664 } else {
12665 // In all other cases go over inputs of LHS and compare each of them to RHS,
12666 // the predicate is true for (LHS, RHS) if it is true for all such pairs.
12667 // At this point RHS is either a non-Phi, or it is a Phi from some block
12668 // different from LBB.
12669 for (const BasicBlock *IncBB : predecessors(BB: LBB)) {
12670 // Check that RHS is available in this block.
12671 if (!dominates(S: RHS, BB: IncBB))
12672 return false;
12673 const SCEV *L = getSCEV(V: LPhi->getIncomingValueForBlock(BB: IncBB));
12674 // Make sure L does not refer to a value from a potentially previous
12675 // iteration of a loop.
12676 if (!properlyDominates(S: L, BB: LBB))
12677 return false;
12678 // Addrecs are considered to properly dominate their loop, so are missed
12679 // by the previous check. Discard any values that have computable
12680 // evolution in this loop.
12681 if (auto *Loop = LI.getLoopFor(BB: LBB))
12682 if (hasComputableLoopEvolution(S: L, L: Loop))
12683 return false;
12684 if (!ProvedEasily(L, RHS))
12685 return false;
12686 }
12687 }
12688 return true;
12689}
12690
12691bool ScalarEvolution::isImpliedCondOperandsViaShift(CmpPredicate Pred,
12692 const SCEV *LHS,
12693 const SCEV *RHS,
12694 const SCEV *FoundLHS,
12695 const SCEV *FoundRHS) {
12696 // We want to imply LHS < RHS from LHS < (RHS >> shiftvalue). First, make
12697 // sure that we are dealing with same LHS.
12698 if (RHS == FoundRHS) {
12699 std::swap(a&: LHS, b&: RHS);
12700 std::swap(a&: FoundLHS, b&: FoundRHS);
12701 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
12702 }
12703 if (LHS != FoundLHS)
12704 return false;
12705
12706 auto *SUFoundRHS = dyn_cast<SCEVUnknown>(Val: FoundRHS);
12707 if (!SUFoundRHS)
12708 return false;
12709
12710 Value *Shiftee, *ShiftValue;
12711
12712 using namespace PatternMatch;
12713 if (match(V: SUFoundRHS->getValue(),
12714 P: m_LShr(L: m_Value(V&: Shiftee), R: m_Value(V&: ShiftValue)))) {
12715 auto *ShifteeS = getSCEV(V: Shiftee);
12716 // Prove one of the following:
12717 // LHS <u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <u RHS
12718 // LHS <=u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <=u RHS
12719 // LHS <s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0
12720 // ---> LHS <s RHS
12721 // LHS <=s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0
12722 // ---> LHS <=s RHS
12723 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
12724 return isKnownPredicate(Pred: ICmpInst::ICMP_ULE, LHS: ShifteeS, RHS);
12725 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
12726 if (isKnownNonNegative(S: ShifteeS))
12727 return isKnownPredicate(Pred: ICmpInst::ICMP_SLE, LHS: ShifteeS, RHS);
12728 }
12729
12730 return false;
12731}
12732
12733bool ScalarEvolution::isImpliedCondOperands(CmpPredicate Pred, const SCEV *LHS,
12734 const SCEV *RHS,
12735 const SCEV *FoundLHS,
12736 const SCEV *FoundRHS,
12737 const Instruction *CtxI) {
12738 return isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundPred: Pred, FoundLHS,
12739 FoundRHS) ||
12740 isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS,
12741 FoundRHS) ||
12742 isImpliedCondOperandsViaShift(Pred, LHS, RHS, FoundLHS, FoundRHS) ||
12743 isImpliedCondOperandsViaAddRecStart(Pred, LHS, RHS, FoundLHS, FoundRHS,
12744 CtxI) ||
12745 isImpliedCondOperandsHelper(Pred, LHS, RHS, FoundLHS, FoundRHS);
12746}
12747
12748/// Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values?
12749template <typename MinMaxExprType>
12750static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr,
12751 const SCEV *Candidate) {
12752 const MinMaxExprType *MinMaxExpr = dyn_cast<MinMaxExprType>(MaybeMinMaxExpr);
12753 if (!MinMaxExpr)
12754 return false;
12755
12756 return is_contained(MinMaxExpr->operands(), Candidate);
12757}
12758
12759static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE,
12760 CmpPredicate Pred, const SCEV *LHS,
12761 const SCEV *RHS) {
12762 // If both sides are affine addrecs for the same loop, with equal
12763 // steps, and we know the recurrences don't wrap, then we only
12764 // need to check the predicate on the starting values.
12765
12766 if (!ICmpInst::isRelational(P: Pred))
12767 return false;
12768
12769 const SCEV *LStart, *RStart, *Step;
12770 const Loop *L;
12771 if (!match(S: LHS,
12772 P: m_scev_AffineAddRec(Op0: m_SCEV(V&: LStart), Op1: m_SCEV(V&: Step), L: m_Loop(L))) ||
12773 !match(S: RHS, P: m_scev_AffineAddRec(Op0: m_SCEV(V&: RStart), Op1: m_scev_Specific(S: Step),
12774 L: m_SpecificLoop(L))))
12775 return false;
12776 const SCEVAddRecExpr *LAR = cast<SCEVAddRecExpr>(Val: LHS);
12777 const SCEVAddRecExpr *RAR = cast<SCEVAddRecExpr>(Val: RHS);
12778 SCEV::NoWrapFlags NW = ICmpInst::isSigned(Pred) ?
12779 SCEV::FlagNSW : SCEV::FlagNUW;
12780 if (!LAR->getNoWrapFlags(Mask: NW) || !RAR->getNoWrapFlags(Mask: NW))
12781 return false;
12782
12783 return SE.isKnownPredicate(Pred, LHS: LStart, RHS: RStart);
12784}
12785
12786/// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max
12787/// expression?
12788static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, CmpPredicate Pred,
12789 const SCEV *LHS, const SCEV *RHS) {
12790 switch (Pred) {
12791 default:
12792 return false;
12793
12794 case ICmpInst::ICMP_SGE:
12795 std::swap(a&: LHS, b&: RHS);
12796 [[fallthrough]];
12797 case ICmpInst::ICMP_SLE:
12798 return
12799 // min(A, ...) <= A
12800 IsMinMaxConsistingOf<SCEVSMinExpr>(MaybeMinMaxExpr: LHS, Candidate: RHS) ||
12801 // A <= max(A, ...)
12802 IsMinMaxConsistingOf<SCEVSMaxExpr>(MaybeMinMaxExpr: RHS, Candidate: LHS);
12803
12804 case ICmpInst::ICMP_UGE:
12805 std::swap(a&: LHS, b&: RHS);
12806 [[fallthrough]];
12807 case ICmpInst::ICMP_ULE:
12808 return
12809 // min(A, ...) <= A
12810 // FIXME: what about umin_seq?
12811 IsMinMaxConsistingOf<SCEVUMinExpr>(MaybeMinMaxExpr: LHS, Candidate: RHS) ||
12812 // A <= max(A, ...)
12813 IsMinMaxConsistingOf<SCEVUMaxExpr>(MaybeMinMaxExpr: RHS, Candidate: LHS);
12814 }
12815
12816 llvm_unreachable("covered switch fell through?!");
12817}
12818
12819bool ScalarEvolution::isImpliedViaOperations(CmpPredicate Pred, const SCEV *LHS,
12820 const SCEV *RHS,
12821 const SCEV *FoundLHS,
12822 const SCEV *FoundRHS,
12823 unsigned Depth) {
12824 assert(getTypeSizeInBits(LHS->getType()) ==
12825 getTypeSizeInBits(RHS->getType()) &&
12826 "LHS and RHS have different sizes?");
12827 assert(getTypeSizeInBits(FoundLHS->getType()) ==
12828 getTypeSizeInBits(FoundRHS->getType()) &&
12829 "FoundLHS and FoundRHS have different sizes?");
12830 // We want to avoid hurting the compile time with analysis of too big trees.
12831 if (Depth > MaxSCEVOperationsImplicationDepth)
12832 return false;
12833
12834 // We only want to work with GT comparison so far.
12835 if (ICmpInst::isLT(P: Pred)) {
12836 Pred = ICmpInst::getSwappedCmpPredicate(Pred);
12837 std::swap(a&: LHS, b&: RHS);
12838 std::swap(a&: FoundLHS, b&: FoundRHS);
12839 }
12840
12841 CmpInst::Predicate P = Pred.getPreferredSignedPredicate();
12842
12843 // For unsigned, try to reduce it to corresponding signed comparison.
12844 if (P == ICmpInst::ICMP_UGT)
12845 // We can replace unsigned predicate with its signed counterpart if all
12846 // involved values are non-negative.
12847 // TODO: We could have better support for unsigned.
12848 if (isKnownNonNegative(S: FoundLHS) && isKnownNonNegative(S: FoundRHS)) {
12849 // Knowing that both FoundLHS and FoundRHS are non-negative, and knowing
12850 // FoundLHS >u FoundRHS, we also know that FoundLHS >s FoundRHS. Let us
12851 // use this fact to prove that LHS and RHS are non-negative.
12852 const SCEV *MinusOne = getMinusOne(Ty: LHS->getType());
12853 if (isImpliedCondOperands(Pred: ICmpInst::ICMP_SGT, LHS, RHS: MinusOne, FoundLHS,
12854 FoundRHS) &&
12855 isImpliedCondOperands(Pred: ICmpInst::ICMP_SGT, LHS: RHS, RHS: MinusOne, FoundLHS,
12856 FoundRHS))
12857 P = ICmpInst::ICMP_SGT;
12858 }
12859
12860 if (P != ICmpInst::ICMP_SGT)
12861 return false;
12862
12863 auto GetOpFromSExt = [&](const SCEV *S) -> const SCEV * {
12864 if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(Val: S))
12865 return Ext->getOperand();
12866 // TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off
12867 // the constant in some cases.
12868 return S;
12869 };
12870
12871 // Acquire values from extensions.
12872 auto *OrigLHS = LHS;
12873 auto *OrigFoundLHS = FoundLHS;
12874 LHS = GetOpFromSExt(LHS);
12875 FoundLHS = GetOpFromSExt(FoundLHS);
12876
12877 // Is the SGT predicate can be proved trivially or using the found context.
12878 auto IsSGTViaContext = [&](const SCEV *S1, const SCEV *S2) {
12879 return isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_SGT, LHS: S1, RHS: S2) ||
12880 isImpliedViaOperations(Pred: ICmpInst::ICMP_SGT, LHS: S1, RHS: S2, FoundLHS: OrigFoundLHS,
12881 FoundRHS, Depth: Depth + 1);
12882 };
12883
12884 if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(Val: LHS)) {
12885 // We want to avoid creation of any new non-constant SCEV. Since we are
12886 // going to compare the operands to RHS, we should be certain that we don't
12887 // need any size extensions for this. So let's decline all cases when the
12888 // sizes of types of LHS and RHS do not match.
12889 // TODO: Maybe try to get RHS from sext to catch more cases?
12890 if (getTypeSizeInBits(Ty: LHS->getType()) != getTypeSizeInBits(Ty: RHS->getType()))
12891 return false;
12892
12893 // Should not overflow.
12894 if (!LHSAddExpr->hasNoSignedWrap())
12895 return false;
12896
12897 SCEVUse LL = LHSAddExpr->getOperand(i: 0);
12898 SCEVUse LR = LHSAddExpr->getOperand(i: 1);
12899 auto *MinusOne = getMinusOne(Ty: RHS->getType());
12900
12901 // Checks that S1 >= 0 && S2 > RHS, trivially or using the found context.
12902 auto IsSumGreaterThanRHS = [&](const SCEV *S1, const SCEV *S2) {
12903 return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS);
12904 };
12905 // Try to prove the following rule:
12906 // (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS).
12907 // (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS).
12908 if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL))
12909 return true;
12910 } else if (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(Val: LHS)) {
12911 Value *LL, *LR;
12912 // FIXME: Once we have SDiv implemented, we can get rid of this matching.
12913
12914 using namespace llvm::PatternMatch;
12915
12916 if (match(V: LHSUnknownExpr->getValue(), P: m_SDiv(L: m_Value(V&: LL), R: m_Value(V&: LR)))) {
12917 // Rules for division.
12918 // We are going to perform some comparisons with Denominator and its
12919 // derivative expressions. In general case, creating a SCEV for it may
12920 // lead to a complex analysis of the entire graph, and in particular it
12921 // can request trip count recalculation for the same loop. This would
12922 // cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid
12923 // this, we only want to create SCEVs that are constants in this section.
12924 // So we bail if Denominator is not a constant.
12925 if (!isa<ConstantInt>(Val: LR))
12926 return false;
12927
12928 auto *Denominator = cast<SCEVConstant>(Val: getSCEV(V: LR));
12929
12930 // We want to make sure that LHS = FoundLHS / Denominator. If it is so,
12931 // then a SCEV for the numerator already exists and matches with FoundLHS.
12932 auto *Numerator = getExistingSCEV(V: LL);
12933 if (!Numerator || Numerator->getType() != FoundLHS->getType())
12934 return false;
12935
12936 // Make sure that the numerator matches with FoundLHS and the denominator
12937 // is positive.
12938 if (!HasSameValue(A: Numerator, B: FoundLHS) || !isKnownPositive(S: Denominator))
12939 return false;
12940
12941 auto *DTy = Denominator->getType();
12942 auto *FRHSTy = FoundRHS->getType();
12943 if (DTy->isPointerTy() != FRHSTy->isPointerTy())
12944 // One of types is a pointer and another one is not. We cannot extend
12945 // them properly to a wider type, so let us just reject this case.
12946 // TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help
12947 // to avoid this check.
12948 return false;
12949
12950 // Given that:
12951 // FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0.
12952 auto *WTy = getWiderType(T1: DTy, T2: FRHSTy);
12953 auto *DenominatorExt = getNoopOrSignExtend(V: Denominator, Ty: WTy);
12954 auto *FoundRHSExt = getNoopOrSignExtend(V: FoundRHS, Ty: WTy);
12955
12956 // Try to prove the following rule:
12957 // (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS).
12958 // For example, given that FoundLHS > 2. It means that FoundLHS is at
12959 // least 3. If we divide it by Denominator < 4, we will have at least 1.
12960 auto *DenomMinusTwo = getMinusSCEV(LHS: DenominatorExt, RHS: getConstant(Ty: WTy, V: 2));
12961 if (isKnownNonPositive(S: RHS) &&
12962 IsSGTViaContext(FoundRHSExt, DenomMinusTwo))
12963 return true;
12964
12965 // Try to prove the following rule:
12966 // (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS).
12967 // For example, given that FoundLHS > -3. Then FoundLHS is at least -2.
12968 // If we divide it by Denominator > 2, then:
12969 // 1. If FoundLHS is negative, then the result is 0.
12970 // 2. If FoundLHS is non-negative, then the result is non-negative.
12971 // Anyways, the result is non-negative.
12972 auto *MinusOne = getMinusOne(Ty: WTy);
12973 auto *NegDenomMinusOne = getMinusSCEV(LHS: MinusOne, RHS: DenominatorExt);
12974 if (isKnownNegative(S: RHS) &&
12975 IsSGTViaContext(FoundRHSExt, NegDenomMinusOne))
12976 return true;
12977 }
12978 }
12979
12980 // If our expression contained SCEVUnknown Phis, and we split it down and now
12981 // need to prove something for them, try to prove the predicate for every
12982 // possible incoming values of those Phis.
12983 if (isImpliedViaMerge(Pred, LHS: OrigLHS, RHS, FoundLHS: OrigFoundLHS, FoundRHS, Depth: Depth + 1))
12984 return true;
12985
12986 return false;
12987}
12988
12989static bool isKnownPredicateExtendIdiom(CmpPredicate Pred, const SCEV *LHS,
12990 const SCEV *RHS) {
12991 // zext x u<= sext x, sext x s<= zext x
12992 const SCEV *Op;
12993 switch (Pred) {
12994 case ICmpInst::ICMP_SGE:
12995 std::swap(a&: LHS, b&: RHS);
12996 [[fallthrough]];
12997 case ICmpInst::ICMP_SLE: {
12998 // If operand >=s 0 then ZExt == SExt. If operand <s 0 then SExt <s ZExt.
12999 return match(S: LHS, P: m_scev_SExt(Op0: m_SCEV(V&: Op))) &&
13000 match(S: RHS, P: m_scev_ZExt(Op0: m_scev_Specific(S: Op)));
13001 }
13002 case ICmpInst::ICMP_UGE:
13003 std::swap(a&: LHS, b&: RHS);
13004 [[fallthrough]];
13005 case ICmpInst::ICMP_ULE: {
13006 // If operand >=u 0 then ZExt == SExt. If operand <u 0 then ZExt <u SExt.
13007 return match(S: LHS, P: m_scev_ZExt(Op0: m_SCEV(V&: Op))) &&
13008 match(S: RHS, P: m_scev_SExt(Op0: m_scev_Specific(S: Op)));
13009 }
13010 default:
13011 return false;
13012 };
13013 llvm_unreachable("unhandled case");
13014}
13015
13016bool ScalarEvolution::isKnownViaNonRecursiveReasoning(CmpPredicate Pred,
13017 const SCEV *LHS,
13018 const SCEV *RHS) {
13019 return isKnownPredicateExtendIdiom(Pred, LHS, RHS) ||
13020 isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
13021 IsKnownPredicateViaMinOrMax(SE&: *this, Pred, LHS, RHS) ||
13022 IsKnownPredicateViaAddRecStart(SE&: *this, Pred, LHS, RHS) ||
13023 isKnownPredicateViaNoOverflow(Pred, LHS, RHS);
13024}
13025
13026bool ScalarEvolution::isImpliedCondOperandsHelper(CmpPredicate Pred,
13027 const SCEV *LHS,
13028 const SCEV *RHS,
13029 const SCEV *FoundLHS,
13030 const SCEV *FoundRHS) {
13031 switch (Pred) {
13032 default:
13033 llvm_unreachable("Unexpected CmpPredicate value!");
13034 case ICmpInst::ICMP_EQ:
13035 case ICmpInst::ICMP_NE:
13036 if (HasSameValue(A: LHS, B: FoundLHS) && HasSameValue(A: RHS, B: FoundRHS))
13037 return true;
13038 break;
13039 case ICmpInst::ICMP_SLT:
13040 case ICmpInst::ICMP_SLE:
13041 if (isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_SLE, LHS, RHS: FoundLHS) &&
13042 isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_SGE, LHS: RHS, RHS: FoundRHS))
13043 return true;
13044 break;
13045 case ICmpInst::ICMP_SGT:
13046 case ICmpInst::ICMP_SGE:
13047 if (isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_SGE, LHS, RHS: FoundLHS) &&
13048 isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_SLE, LHS: RHS, RHS: FoundRHS))
13049 return true;
13050 break;
13051 case ICmpInst::ICMP_ULT:
13052 case ICmpInst::ICMP_ULE:
13053 if (isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_ULE, LHS, RHS: FoundLHS) &&
13054 isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_UGE, LHS: RHS, RHS: FoundRHS))
13055 return true;
13056 break;
13057 case ICmpInst::ICMP_UGT:
13058 case ICmpInst::ICMP_UGE:
13059 if (isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_UGE, LHS, RHS: FoundLHS) &&
13060 isKnownViaNonRecursiveReasoning(Pred: ICmpInst::ICMP_ULE, LHS: RHS, RHS: FoundRHS))
13061 return true;
13062 break;
13063 }
13064
13065 // Maybe it can be proved via operations?
13066 if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS))
13067 return true;
13068
13069 return false;
13070}
13071
13072bool ScalarEvolution::isImpliedCondOperandsViaRanges(
13073 CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, CmpPredicate FoundPred,
13074 const SCEV *FoundLHS, const SCEV *FoundRHS) {
13075 if (!isa<SCEVConstant>(Val: RHS) || !isa<SCEVConstant>(Val: FoundRHS))
13076 // The restriction on `FoundRHS` be lifted easily -- it exists only to
13077 // reduce the compile time impact of this optimization.
13078 return false;
13079
13080 std::optional<APInt> Addend = computeConstantDifference(More: LHS, Less: FoundLHS);
13081 if (!Addend)
13082 return false;
13083
13084 const APInt &ConstFoundRHS = cast<SCEVConstant>(Val: FoundRHS)->getAPInt();
13085
13086 // `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the
13087 // antecedent "`FoundLHS` `FoundPred` `FoundRHS`".
13088 ConstantRange FoundLHSRange =
13089 ConstantRange::makeExactICmpRegion(Pred: FoundPred, Other: ConstFoundRHS);
13090
13091 // Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`:
13092 ConstantRange LHSRange = FoundLHSRange.add(Other: ConstantRange(*Addend));
13093
13094 // We can also compute the range of values for `LHS` that satisfy the
13095 // consequent, "`LHS` `Pred` `RHS`":
13096 const APInt &ConstRHS = cast<SCEVConstant>(Val: RHS)->getAPInt();
13097 // The antecedent implies the consequent if every value of `LHS` that
13098 // satisfies the antecedent also satisfies the consequent.
13099 return LHSRange.icmp(Pred, Other: ConstRHS);
13100}
13101
13102bool ScalarEvolution::canIVOverflowOnLT(const SCEV *RHS, const SCEV *Stride,
13103 bool IsSigned) {
13104 assert(isKnownPositive(Stride) && "Positive stride expected!");
13105
13106 unsigned BitWidth = getTypeSizeInBits(Ty: RHS->getType());
13107 const SCEV *One = getOne(Ty: Stride->getType());
13108
13109 if (IsSigned) {
13110 APInt MaxRHS = getSignedRangeMax(S: RHS);
13111 APInt MaxValue = APInt::getSignedMaxValue(numBits: BitWidth);
13112 APInt MaxStrideMinusOne = getSignedRangeMax(S: getMinusSCEV(LHS: Stride, RHS: One));
13113
13114 // SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow!
13115 return (std::move(MaxValue) - MaxStrideMinusOne).slt(RHS: MaxRHS);
13116 }
13117
13118 APInt MaxRHS = getUnsignedRangeMax(S: RHS);
13119 APInt MaxValue = APInt::getMaxValue(numBits: BitWidth);
13120 APInt MaxStrideMinusOne = getUnsignedRangeMax(S: getMinusSCEV(LHS: Stride, RHS: One));
13121
13122 // UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow!
13123 return (std::move(MaxValue) - MaxStrideMinusOne).ult(RHS: MaxRHS);
13124}
13125
13126bool ScalarEvolution::canIVOverflowOnGT(const SCEV *RHS, const SCEV *Stride,
13127 bool IsSigned) {
13128
13129 unsigned BitWidth = getTypeSizeInBits(Ty: RHS->getType());
13130 const SCEV *One = getOne(Ty: Stride->getType());
13131
13132 if (IsSigned) {
13133 APInt MinRHS = getSignedRangeMin(S: RHS);
13134 APInt MinValue = APInt::getSignedMinValue(numBits: BitWidth);
13135 APInt MaxStrideMinusOne = getSignedRangeMax(S: getMinusSCEV(LHS: Stride, RHS: One));
13136
13137 // SMinRHS - SMaxStrideMinusOne < SMinValue => overflow!
13138 return (std::move(MinValue) + MaxStrideMinusOne).sgt(RHS: MinRHS);
13139 }
13140
13141 APInt MinRHS = getUnsignedRangeMin(S: RHS);
13142 APInt MinValue = APInt::getMinValue(numBits: BitWidth);
13143 APInt MaxStrideMinusOne = getUnsignedRangeMax(S: getMinusSCEV(LHS: Stride, RHS: One));
13144
13145 // UMinRHS - UMaxStrideMinusOne < UMinValue => overflow!
13146 return (std::move(MinValue) + MaxStrideMinusOne).ugt(RHS: MinRHS);
13147}
13148
13149const SCEV *ScalarEvolution::getUDivCeilSCEV(const SCEV *N, const SCEV *D) {
13150 // umin(N, 1) + floor((N - umin(N, 1)) / D)
13151 // This is equivalent to "1 + floor((N - 1) / D)" for N != 0. The umin
13152 // expression fixes the case of N=0.
13153 const SCEV *MinNOne = getUMinExpr(LHS: N, RHS: getOne(Ty: N->getType()));
13154 const SCEV *NMinusOne = getMinusSCEV(LHS: N, RHS: MinNOne);
13155 return getAddExpr(LHS: MinNOne, RHS: getUDivExpr(LHS: NMinusOne, RHS: D));
13156}
13157
13158const SCEV *ScalarEvolution::computeMaxBECountForLT(const SCEV *Start,
13159 const SCEV *Stride,
13160 const SCEV *End,
13161 unsigned BitWidth,
13162 bool IsSigned) {
13163 // The logic in this function assumes we can represent a positive stride.
13164 // If we can't, the backedge-taken count must be zero.
13165 if (IsSigned && BitWidth == 1)
13166 return getZero(Ty: Stride->getType());
13167
13168 // This code below only been closely audited for negative strides in the
13169 // unsigned comparison case, it may be correct for signed comparison, but
13170 // that needs to be established.
13171 if (IsSigned && isKnownNegative(S: Stride))
13172 return getCouldNotCompute();
13173
13174 // Calculate the maximum backedge count based on the range of values
13175 // permitted by Start, End, and Stride.
13176 APInt MinStart =
13177 IsSigned ? getSignedRangeMin(S: Start) : getUnsignedRangeMin(S: Start);
13178
13179 APInt MinStride =
13180 IsSigned ? getSignedRangeMin(S: Stride) : getUnsignedRangeMin(S: Stride);
13181
13182 // We assume either the stride is positive, or the backedge-taken count
13183 // is zero. So force StrideForMaxBECount to be at least one.
13184 APInt One(BitWidth, 1);
13185 APInt StrideForMaxBECount = IsSigned ? APIntOps::smax(A: One, B: MinStride)
13186 : APIntOps::umax(A: One, B: MinStride);
13187
13188 APInt MaxValue = IsSigned ? APInt::getSignedMaxValue(numBits: BitWidth)
13189 : APInt::getMaxValue(numBits: BitWidth);
13190 APInt Limit = MaxValue - (StrideForMaxBECount - 1);
13191
13192 // Although End can be a MAX expression we estimate MaxEnd considering only
13193 // the case End = RHS of the loop termination condition. This is safe because
13194 // in the other case (End - Start) is zero, leading to a zero maximum backedge
13195 // taken count.
13196 APInt MaxEnd = IsSigned ? APIntOps::smin(A: getSignedRangeMax(S: End), B: Limit)
13197 : APIntOps::umin(A: getUnsignedRangeMax(S: End), B: Limit);
13198
13199 // MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride)
13200 MaxEnd = IsSigned ? APIntOps::smax(A: MaxEnd, B: MinStart)
13201 : APIntOps::umax(A: MaxEnd, B: MinStart);
13202
13203 return getUDivCeilSCEV(N: getConstant(Val: MaxEnd - MinStart) /* Delta */,
13204 D: getConstant(Val: StrideForMaxBECount) /* Step */);
13205}
13206
13207ScalarEvolution::ExitLimit
13208ScalarEvolution::howManyLessThans(const SCEV *LHS, const SCEV *RHS,
13209 const Loop *L, bool IsSigned,
13210 bool ControlsOnlyExit, bool AllowPredicates) {
13211 SmallVector<const SCEVPredicate *> Predicates;
13212
13213 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(Val: LHS);
13214 bool PredicatedIV = false;
13215 if (!IV) {
13216 if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(Val: LHS)) {
13217 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: ZExt->getOperand());
13218 if (AR && AR->getLoop() == L && AR->isAffine()) {
13219 auto canProveNUW = [&]() {
13220 // We can use the comparison to infer no-wrap flags only if it fully
13221 // controls the loop exit.
13222 if (!ControlsOnlyExit)
13223 return false;
13224
13225 if (!isLoopInvariant(S: RHS, L))
13226 return false;
13227
13228 if (!isKnownNonZero(S: AR->getStepRecurrence(SE&: *this)))
13229 // We need the sequence defined by AR to strictly increase in the
13230 // unsigned integer domain for the logic below to hold.
13231 return false;
13232
13233 const unsigned InnerBitWidth = getTypeSizeInBits(Ty: AR->getType());
13234 const unsigned OuterBitWidth = getTypeSizeInBits(Ty: RHS->getType());
13235 // If RHS <=u Limit, then there must exist a value V in the sequence
13236 // defined by AR (e.g. {Start,+,Step}) such that V >u RHS, and
13237 // V <=u UINT_MAX. Thus, we must exit the loop before unsigned
13238 // overflow occurs. This limit also implies that a signed comparison
13239 // (in the wide bitwidth) is equivalent to an unsigned comparison as
13240 // the high bits on both sides must be zero.
13241 APInt StrideMax = getUnsignedRangeMax(S: AR->getStepRecurrence(SE&: *this));
13242 APInt Limit = APInt::getMaxValue(numBits: InnerBitWidth) - (StrideMax - 1);
13243 Limit = Limit.zext(width: OuterBitWidth);
13244 return getUnsignedRangeMax(S: applyLoopGuards(Expr: RHS, L)).ule(RHS: Limit);
13245 };
13246 auto Flags = AR->getNoWrapFlags();
13247 if (!hasFlags(Flags, TestFlags: SCEV::FlagNUW) && canProveNUW())
13248 Flags = setFlags(Flags, OnFlags: SCEV::FlagNUW);
13249
13250 setNoWrapFlags(AddRec: const_cast<SCEVAddRecExpr *>(AR), Flags);
13251 if (AR->hasNoUnsignedWrap()) {
13252 // Emulate what getZeroExtendExpr would have done during construction
13253 // if we'd been able to infer the fact just above at that time.
13254 const SCEV *Step = AR->getStepRecurrence(SE&: *this);
13255 Type *Ty = ZExt->getType();
13256 auto *S = getAddRecExpr(
13257 Start: getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty, SE: this, Depth: 0),
13258 Step: getZeroExtendExpr(Op: Step, Ty, Depth: 0), L, Flags: AR->getNoWrapFlags());
13259 IV = dyn_cast<SCEVAddRecExpr>(Val: S);
13260 }
13261 }
13262 }
13263 }
13264
13265
13266 if (!IV && AllowPredicates) {
13267 // Try to make this an AddRec using runtime tests, in the first X
13268 // iterations of this loop, where X is the SCEV expression found by the
13269 // algorithm below.
13270 IV = convertSCEVToAddRecWithPredicates(S: LHS, L, Preds&: Predicates);
13271 PredicatedIV = true;
13272 }
13273
13274 // Avoid weird loops
13275 if (!IV || IV->getLoop() != L || !IV->isAffine())
13276 return getCouldNotCompute();
13277
13278 // A precondition of this method is that the condition being analyzed
13279 // reaches an exiting branch which dominates the latch. Given that, we can
13280 // assume that an increment which violates the nowrap specification and
13281 // produces poison must cause undefined behavior when the resulting poison
13282 // value is branched upon and thus we can conclude that the backedge is
13283 // taken no more often than would be required to produce that poison value.
13284 // Note that a well defined loop can exit on the iteration which violates
13285 // the nowrap specification if there is another exit (either explicit or
13286 // implicit/exceptional) which causes the loop to execute before the
13287 // exiting instruction we're analyzing would trigger UB.
13288 auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW;
13289 bool NoWrap = ControlsOnlyExit && IV->getNoWrapFlags(Mask: WrapType);
13290 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT;
13291
13292 const SCEV *Stride = IV->getStepRecurrence(SE&: *this);
13293
13294 bool PositiveStride = isKnownPositive(S: Stride);
13295
13296 // Avoid negative or zero stride values.
13297 if (!PositiveStride) {
13298 // We can compute the correct backedge taken count for loops with unknown
13299 // strides if we can prove that the loop is not an infinite loop with side
13300 // effects. Here's the loop structure we are trying to handle -
13301 //
13302 // i = start
13303 // do {
13304 // A[i] = i;
13305 // i += s;
13306 // } while (i < end);
13307 //
13308 // The backedge taken count for such loops is evaluated as -
13309 // (max(end, start + stride) - start - 1) /u stride
13310 //
13311 // The additional preconditions that we need to check to prove correctness
13312 // of the above formula is as follows -
13313 //
13314 // a) IV is either nuw or nsw depending upon signedness (indicated by the
13315 // NoWrap flag).
13316 // b) the loop is guaranteed to be finite (e.g. is mustprogress and has
13317 // no side effects within the loop)
13318 // c) loop has a single static exit (with no abnormal exits)
13319 //
13320 // Precondition a) implies that if the stride is negative, this is a single
13321 // trip loop. The backedge taken count formula reduces to zero in this case.
13322 //
13323 // Precondition b) and c) combine to imply that if rhs is invariant in L,
13324 // then a zero stride means the backedge can't be taken without executing
13325 // undefined behavior.
13326 //
13327 // The positive stride case is the same as isKnownPositive(Stride) returning
13328 // true (original behavior of the function).
13329 //
13330 if (PredicatedIV || !NoWrap || !loopIsFiniteByAssumption(L) ||
13331 !loopHasNoAbnormalExits(L))
13332 return getCouldNotCompute();
13333
13334 if (!isKnownNonZero(S: Stride)) {
13335 // If we have a step of zero, and RHS isn't invariant in L, we don't know
13336 // if it might eventually be greater than start and if so, on which
13337 // iteration. We can't even produce a useful upper bound.
13338 if (!isLoopInvariant(S: RHS, L))
13339 return getCouldNotCompute();
13340
13341 // We allow a potentially zero stride, but we need to divide by stride
13342 // below. Since the loop can't be infinite and this check must control
13343 // the sole exit, we can infer the exit must be taken on the first
13344 // iteration (e.g. backedge count = 0) if the stride is zero. Given that,
13345 // we know the numerator in the divides below must be zero, so we can
13346 // pick an arbitrary non-zero value for the denominator (e.g. stride)
13347 // and produce the right result.
13348 // FIXME: Handle the case where Stride is poison?
13349 auto wouldZeroStrideBeUB = [&]() {
13350 // Proof by contradiction. Suppose the stride were zero. If we can
13351 // prove that the backedge *is* taken on the first iteration, then since
13352 // we know this condition controls the sole exit, we must have an
13353 // infinite loop. We can't have a (well defined) infinite loop per
13354 // check just above.
13355 // Note: The (Start - Stride) term is used to get the start' term from
13356 // (start' + stride,+,stride). Remember that we only care about the
13357 // result of this expression when stride == 0 at runtime.
13358 auto *StartIfZero = getMinusSCEV(LHS: IV->getStart(), RHS: Stride);
13359 return isLoopEntryGuardedByCond(L, Pred: Cond, LHS: StartIfZero, RHS);
13360 };
13361 if (!wouldZeroStrideBeUB()) {
13362 Stride = getUMaxExpr(LHS: Stride, RHS: getOne(Ty: Stride->getType()));
13363 }
13364 }
13365 } else if (!NoWrap) {
13366 // Avoid proven overflow cases: this will ensure that the backedge taken
13367 // count will not generate any unsigned overflow.
13368 if (canIVOverflowOnLT(RHS, Stride, IsSigned))
13369 return getCouldNotCompute();
13370 }
13371
13372 // On all paths just preceeding, we established the following invariant:
13373 // IV can be assumed not to overflow up to and including the exiting
13374 // iteration. We proved this in one of two ways:
13375 // 1) We can show overflow doesn't occur before the exiting iteration
13376 // 1a) canIVOverflowOnLT, and b) step of one
13377 // 2) We can show that if overflow occurs, the loop must execute UB
13378 // before any possible exit.
13379 // Note that we have not yet proved RHS invariant (in general).
13380
13381 const SCEV *Start = IV->getStart();
13382
13383 // Preserve pointer-typed Start/RHS to pass to isLoopEntryGuardedByCond.
13384 // If we convert to integers, isLoopEntryGuardedByCond will miss some cases.
13385 // Use integer-typed versions for actual computation; we can't subtract
13386 // pointers in general.
13387 const SCEV *OrigStart = Start;
13388 const SCEV *OrigRHS = RHS;
13389 if (Start->getType()->isPointerTy()) {
13390 Start = getLosslessPtrToIntExpr(Op: Start);
13391 if (isa<SCEVCouldNotCompute>(Val: Start))
13392 return Start;
13393 }
13394 if (RHS->getType()->isPointerTy()) {
13395 RHS = getLosslessPtrToIntExpr(Op: RHS);
13396 if (isa<SCEVCouldNotCompute>(Val: RHS))
13397 return RHS;
13398 }
13399
13400 const SCEV *End = nullptr, *BECount = nullptr,
13401 *BECountIfBackedgeTaken = nullptr;
13402 if (!isLoopInvariant(S: RHS, L)) {
13403 const auto *RHSAddRec = dyn_cast<SCEVAddRecExpr>(Val: RHS);
13404 if (PositiveStride && RHSAddRec != nullptr && RHSAddRec->getLoop() == L &&
13405 RHSAddRec->getNoWrapFlags()) {
13406 // The structure of loop we are trying to calculate backedge count of:
13407 //
13408 // left = left_start
13409 // right = right_start
13410 //
13411 // while(left < right){
13412 // ... do something here ...
13413 // left += s1; // stride of left is s1 (s1 > 0)
13414 // right += s2; // stride of right is s2 (s2 < 0)
13415 // }
13416 //
13417
13418 const SCEV *RHSStart = RHSAddRec->getStart();
13419 const SCEV *RHSStride = RHSAddRec->getStepRecurrence(SE&: *this);
13420
13421 // If Stride - RHSStride is positive and does not overflow, we can write
13422 // backedge count as ->
13423 // ceil((End - Start) /u (Stride - RHSStride))
13424 // Where, End = max(RHSStart, Start)
13425
13426 // Check if RHSStride < 0 and Stride - RHSStride will not overflow.
13427 if (isKnownNegative(S: RHSStride) &&
13428 willNotOverflow(BinOp: Instruction::Sub, /*Signed=*/true, LHS: Stride,
13429 RHS: RHSStride)) {
13430
13431 const SCEV *Denominator = getMinusSCEV(LHS: Stride, RHS: RHSStride);
13432 if (isKnownPositive(S: Denominator)) {
13433 End = IsSigned ? getSMaxExpr(LHS: RHSStart, RHS: Start)
13434 : getUMaxExpr(LHS: RHSStart, RHS: Start);
13435
13436 // We can do this because End >= Start, as End = max(RHSStart, Start)
13437 const SCEV *Delta = getMinusSCEV(LHS: End, RHS: Start);
13438
13439 BECount = getUDivCeilSCEV(N: Delta, D: Denominator);
13440 BECountIfBackedgeTaken =
13441 getUDivCeilSCEV(N: getMinusSCEV(LHS: RHSStart, RHS: Start), D: Denominator);
13442 }
13443 }
13444 }
13445 if (BECount == nullptr) {
13446 // If we cannot calculate ExactBECount, we can calculate the MaxBECount,
13447 // given the start, stride and max value for the end bound of the
13448 // loop (RHS), and the fact that IV does not overflow (which is
13449 // checked above).
13450 const SCEV *MaxBECount = computeMaxBECountForLT(
13451 Start, Stride, End: RHS, BitWidth: getTypeSizeInBits(Ty: LHS->getType()), IsSigned);
13452 return ExitLimit(getCouldNotCompute() /* ExactNotTaken */, MaxBECount,
13453 MaxBECount, false /*MaxOrZero*/, Predicates);
13454 }
13455 } else {
13456 // We use the expression (max(End,Start)-Start)/Stride to describe the
13457 // backedge count, as if the backedge is taken at least once
13458 // max(End,Start) is End and so the result is as above, and if not
13459 // max(End,Start) is Start so we get a backedge count of zero.
13460 auto *OrigStartMinusStride = getMinusSCEV(LHS: OrigStart, RHS: Stride);
13461 assert(isAvailableAtLoopEntry(OrigStartMinusStride, L) && "Must be!");
13462 assert(isAvailableAtLoopEntry(OrigStart, L) && "Must be!");
13463 assert(isAvailableAtLoopEntry(OrigRHS, L) && "Must be!");
13464 // Can we prove (max(RHS,Start) > Start - Stride?
13465 if (isLoopEntryGuardedByCond(L, Pred: Cond, LHS: OrigStartMinusStride, RHS: OrigStart) &&
13466 isLoopEntryGuardedByCond(L, Pred: Cond, LHS: OrigStartMinusStride, RHS: OrigRHS)) {
13467 // In this case, we can use a refined formula for computing backedge
13468 // taken count. The general formula remains:
13469 // "End-Start /uceiling Stride" where "End = max(RHS,Start)"
13470 // We want to use the alternate formula:
13471 // "((End - 1) - (Start - Stride)) /u Stride"
13472 // Let's do a quick case analysis to show these are equivalent under
13473 // our precondition that max(RHS,Start) > Start - Stride.
13474 // * For RHS <= Start, the backedge-taken count must be zero.
13475 // "((End - 1) - (Start - Stride)) /u Stride" reduces to
13476 // "((Start - 1) - (Start - Stride)) /u Stride" which simplies to
13477 // "Stride - 1 /u Stride" which is indeed zero for all non-zero values
13478 // of Stride. For 0 stride, we've use umin(1,Stride) above,
13479 // reducing this to the stride of 1 case.
13480 // * For RHS >= Start, the backedge count must be "RHS-Start /uceil
13481 // Stride".
13482 // "((End - 1) - (Start - Stride)) /u Stride" reduces to
13483 // "((RHS - 1) - (Start - Stride)) /u Stride" reassociates to
13484 // "((RHS - (Start - Stride) - 1) /u Stride".
13485 // Our preconditions trivially imply no overflow in that form.
13486 const SCEV *MinusOne = getMinusOne(Ty: Stride->getType());
13487 const SCEV *Numerator =
13488 getMinusSCEV(LHS: getAddExpr(LHS: RHS, RHS: MinusOne), RHS: getMinusSCEV(LHS: Start, RHS: Stride));
13489 BECount = getUDivExpr(LHS: Numerator, RHS: Stride);
13490 }
13491
13492 if (!BECount) {
13493 auto canProveRHSGreaterThanEqualStart = [&]() {
13494 auto CondGE = IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE;
13495 const SCEV *GuardedRHS = applyLoopGuards(Expr: OrigRHS, L);
13496 const SCEV *GuardedStart = applyLoopGuards(Expr: OrigStart, L);
13497
13498 if (isLoopEntryGuardedByCond(L, Pred: CondGE, LHS: OrigRHS, RHS: OrigStart) ||
13499 isKnownPredicate(Pred: CondGE, LHS: GuardedRHS, RHS: GuardedStart))
13500 return true;
13501
13502 // (RHS > Start - 1) implies RHS >= Start.
13503 // * "RHS >= Start" is trivially equivalent to "RHS > Start - 1" if
13504 // "Start - 1" doesn't overflow.
13505 // * For signed comparison, if Start - 1 does overflow, it's equal
13506 // to INT_MAX, and "RHS >s INT_MAX" is trivially false.
13507 // * For unsigned comparison, if Start - 1 does overflow, it's equal
13508 // to UINT_MAX, and "RHS >u UINT_MAX" is trivially false.
13509 //
13510 // FIXME: Should isLoopEntryGuardedByCond do this for us?
13511 auto CondGT = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
13512 auto *StartMinusOne =
13513 getAddExpr(LHS: OrigStart, RHS: getMinusOne(Ty: OrigStart->getType()));
13514 return isLoopEntryGuardedByCond(L, Pred: CondGT, LHS: OrigRHS, RHS: StartMinusOne);
13515 };
13516
13517 // If we know that RHS >= Start in the context of loop, then we know
13518 // that max(RHS, Start) = RHS at this point.
13519 if (canProveRHSGreaterThanEqualStart()) {
13520 End = RHS;
13521 } else {
13522 // If RHS < Start, the backedge will be taken zero times. So in
13523 // general, we can write the backedge-taken count as:
13524 //
13525 // RHS >= Start ? ceil(RHS - Start) / Stride : 0
13526 //
13527 // We convert it to the following to make it more convenient for SCEV:
13528 //
13529 // ceil(max(RHS, Start) - Start) / Stride
13530 End = IsSigned ? getSMaxExpr(LHS: RHS, RHS: Start) : getUMaxExpr(LHS: RHS, RHS: Start);
13531
13532 // See what would happen if we assume the backedge is taken. This is
13533 // used to compute MaxBECount.
13534 BECountIfBackedgeTaken =
13535 getUDivCeilSCEV(N: getMinusSCEV(LHS: RHS, RHS: Start), D: Stride);
13536 }
13537
13538 // At this point, we know:
13539 //
13540 // 1. If IsSigned, Start <=s End; otherwise, Start <=u End
13541 // 2. The index variable doesn't overflow.
13542 //
13543 // Therefore, we know N exists such that
13544 // (Start + Stride * N) >= End, and computing "(Start + Stride * N)"
13545 // doesn't overflow.
13546 //
13547 // Using this information, try to prove whether the addition in
13548 // "(Start - End) + (Stride - 1)" has unsigned overflow.
13549 const SCEV *One = getOne(Ty: Stride->getType());
13550 bool MayAddOverflow = [&] {
13551 if (isKnownToBeAPowerOfTwo(S: Stride)) {
13552 // Suppose Stride is a power of two, and Start/End are unsigned
13553 // integers. Let UMAX be the largest representable unsigned
13554 // integer.
13555 //
13556 // By the preconditions of this function, we know
13557 // "(Start + Stride * N) >= End", and this doesn't overflow.
13558 // As a formula:
13559 //
13560 // End <= (Start + Stride * N) <= UMAX
13561 //
13562 // Subtracting Start from all the terms:
13563 //
13564 // End - Start <= Stride * N <= UMAX - Start
13565 //
13566 // Since Start is unsigned, UMAX - Start <= UMAX. Therefore:
13567 //
13568 // End - Start <= Stride * N <= UMAX
13569 //
13570 // Stride * N is a multiple of Stride. Therefore,
13571 //
13572 // End - Start <= Stride * N <= UMAX - (UMAX mod Stride)
13573 //
13574 // Since Stride is a power of two, UMAX + 1 is divisible by
13575 // Stride. Therefore, UMAX mod Stride == Stride - 1. So we can
13576 // write:
13577 //
13578 // End - Start <= Stride * N <= UMAX - Stride - 1
13579 //
13580 // Dropping the middle term:
13581 //
13582 // End - Start <= UMAX - Stride - 1
13583 //
13584 // Adding Stride - 1 to both sides:
13585 //
13586 // (End - Start) + (Stride - 1) <= UMAX
13587 //
13588 // In other words, the addition doesn't have unsigned overflow.
13589 //
13590 // A similar proof works if we treat Start/End as signed values.
13591 // Just rewrite steps before "End - Start <= Stride * N <= UMAX"
13592 // to use signed max instead of unsigned max. Note that we're
13593 // trying to prove a lack of unsigned overflow in either case.
13594 return false;
13595 }
13596 if (Start == Stride || Start == getMinusSCEV(LHS: Stride, RHS: One)) {
13597 // If Start is equal to Stride, (End - Start) + (Stride - 1) == End
13598 // - 1. If !IsSigned, 0 <u Stride == Start <=u End; so 0 <u End - 1
13599 // <u End. If IsSigned, 0 <s Stride == Start <=s End; so 0 <s End -
13600 // 1 <s End.
13601 //
13602 // If Start is equal to Stride - 1, (End - Start) + Stride - 1 ==
13603 // End.
13604 return false;
13605 }
13606 return true;
13607 }();
13608
13609 const SCEV *Delta = getMinusSCEV(LHS: End, RHS: Start);
13610 if (!MayAddOverflow) {
13611 // floor((D + (S - 1)) / S)
13612 // We prefer this formulation if it's legal because it's fewer
13613 // operations.
13614 BECount =
13615 getUDivExpr(LHS: getAddExpr(LHS: Delta, RHS: getMinusSCEV(LHS: Stride, RHS: One)), RHS: Stride);
13616 } else {
13617 BECount = getUDivCeilSCEV(N: Delta, D: Stride);
13618 }
13619 }
13620 }
13621
13622 const SCEV *ConstantMaxBECount;
13623 bool MaxOrZero = false;
13624 if (isa<SCEVConstant>(Val: BECount)) {
13625 ConstantMaxBECount = BECount;
13626 } else if (BECountIfBackedgeTaken &&
13627 isa<SCEVConstant>(Val: BECountIfBackedgeTaken)) {
13628 // If we know exactly how many times the backedge will be taken if it's
13629 // taken at least once, then the backedge count will either be that or
13630 // zero.
13631 ConstantMaxBECount = BECountIfBackedgeTaken;
13632 MaxOrZero = true;
13633 } else {
13634 ConstantMaxBECount = computeMaxBECountForLT(
13635 Start, Stride, End: RHS, BitWidth: getTypeSizeInBits(Ty: LHS->getType()), IsSigned);
13636 }
13637
13638 if (isa<SCEVCouldNotCompute>(Val: ConstantMaxBECount) &&
13639 !isa<SCEVCouldNotCompute>(Val: BECount))
13640 ConstantMaxBECount = getConstant(Val: getUnsignedRangeMax(S: BECount));
13641
13642 const SCEV *SymbolicMaxBECount =
13643 isa<SCEVCouldNotCompute>(Val: BECount) ? ConstantMaxBECount : BECount;
13644 return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, MaxOrZero,
13645 Predicates);
13646}
13647
13648ScalarEvolution::ExitLimit ScalarEvolution::howManyGreaterThans(
13649 const SCEV *LHS, const SCEV *RHS, const Loop *L, bool IsSigned,
13650 bool ControlsOnlyExit, bool AllowPredicates) {
13651 SmallVector<const SCEVPredicate *> Predicates;
13652 // We handle only IV > Invariant
13653 if (!isLoopInvariant(S: RHS, L))
13654 return getCouldNotCompute();
13655
13656 const SCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(Val: LHS);
13657 if (!IV && AllowPredicates)
13658 // Try to make this an AddRec using runtime tests, in the first X
13659 // iterations of this loop, where X is the SCEV expression found by the
13660 // algorithm below.
13661 IV = convertSCEVToAddRecWithPredicates(S: LHS, L, Preds&: Predicates);
13662
13663 // Avoid weird loops
13664 if (!IV || IV->getLoop() != L || !IV->isAffine())
13665 return getCouldNotCompute();
13666
13667 auto WrapType = IsSigned ? SCEV::FlagNSW : SCEV::FlagNUW;
13668 bool NoWrap = ControlsOnlyExit && IV->getNoWrapFlags(Mask: WrapType);
13669 ICmpInst::Predicate Cond = IsSigned ? ICmpInst::ICMP_SGT : ICmpInst::ICMP_UGT;
13670
13671 const SCEV *Stride = getNegativeSCEV(V: IV->getStepRecurrence(SE&: *this));
13672
13673 // Avoid negative or zero stride values
13674 if (!isKnownPositive(S: Stride))
13675 return getCouldNotCompute();
13676
13677 // Avoid proven overflow cases: this will ensure that the backedge taken count
13678 // will not generate any unsigned overflow. Relaxed no-overflow conditions
13679 // exploit NoWrapFlags, allowing to optimize in presence of undefined
13680 // behaviors like the case of C language.
13681 if (!Stride->isOne() && !NoWrap)
13682 if (canIVOverflowOnGT(RHS, Stride, IsSigned))
13683 return getCouldNotCompute();
13684
13685 const SCEV *Start = IV->getStart();
13686 const SCEV *End = RHS;
13687 if (!isLoopEntryGuardedByCond(L, Pred: Cond, LHS: getAddExpr(LHS: Start, RHS: Stride), RHS)) {
13688 // If we know that Start >= RHS in the context of loop, then we know that
13689 // min(RHS, Start) = RHS at this point.
13690 if (isLoopEntryGuardedByCond(
13691 L, Pred: IsSigned ? ICmpInst::ICMP_SGE : ICmpInst::ICMP_UGE, LHS: Start, RHS))
13692 End = RHS;
13693 else
13694 End = IsSigned ? getSMinExpr(LHS: RHS, RHS: Start) : getUMinExpr(LHS: RHS, RHS: Start);
13695 }
13696
13697 if (Start->getType()->isPointerTy()) {
13698 Start = getLosslessPtrToIntExpr(Op: Start);
13699 if (isa<SCEVCouldNotCompute>(Val: Start))
13700 return Start;
13701 }
13702 if (End->getType()->isPointerTy()) {
13703 End = getLosslessPtrToIntExpr(Op: End);
13704 if (isa<SCEVCouldNotCompute>(Val: End))
13705 return End;
13706 }
13707
13708 // Compute ((Start - End) + (Stride - 1)) / Stride.
13709 // FIXME: This can overflow. Holding off on fixing this for now;
13710 // howManyGreaterThans will hopefully be gone soon.
13711 const SCEV *One = getOne(Ty: Stride->getType());
13712 const SCEV *BECount = getUDivExpr(
13713 LHS: getAddExpr(LHS: getMinusSCEV(LHS: Start, RHS: End), RHS: getMinusSCEV(LHS: Stride, RHS: One)), RHS: Stride);
13714
13715 APInt MaxStart = IsSigned ? getSignedRangeMax(S: Start)
13716 : getUnsignedRangeMax(S: Start);
13717
13718 APInt MinStride = IsSigned ? getSignedRangeMin(S: Stride)
13719 : getUnsignedRangeMin(S: Stride);
13720
13721 unsigned BitWidth = getTypeSizeInBits(Ty: LHS->getType());
13722 APInt Limit = IsSigned ? APInt::getSignedMinValue(numBits: BitWidth) + (MinStride - 1)
13723 : APInt::getMinValue(numBits: BitWidth) + (MinStride - 1);
13724
13725 // Although End can be a MIN expression we estimate MinEnd considering only
13726 // the case End = RHS. This is safe because in the other case (Start - End)
13727 // is zero, leading to a zero maximum backedge taken count.
13728 APInt MinEnd =
13729 IsSigned ? APIntOps::smax(A: getSignedRangeMin(S: RHS), B: Limit)
13730 : APIntOps::umax(A: getUnsignedRangeMin(S: RHS), B: Limit);
13731
13732 const SCEV *ConstantMaxBECount =
13733 isa<SCEVConstant>(Val: BECount)
13734 ? BECount
13735 : getUDivCeilSCEV(N: getConstant(Val: MaxStart - MinEnd),
13736 D: getConstant(Val: MinStride));
13737
13738 if (isa<SCEVCouldNotCompute>(Val: ConstantMaxBECount))
13739 ConstantMaxBECount = BECount;
13740 const SCEV *SymbolicMaxBECount =
13741 isa<SCEVCouldNotCompute>(Val: BECount) ? ConstantMaxBECount : BECount;
13742
13743 return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, false,
13744 Predicates);
13745}
13746
13747const SCEV *SCEVAddRecExpr::getNumIterationsInRange(const ConstantRange &Range,
13748 ScalarEvolution &SE) const {
13749 if (Range.isFullSet()) // Infinite loop.
13750 return SE.getCouldNotCompute();
13751
13752 // If the start is a non-zero constant, shift the range to simplify things.
13753 if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(Val: getStart()))
13754 if (!SC->getValue()->isZero()) {
13755 SmallVector<SCEVUse, 4> Operands(operands());
13756 Operands[0] = SE.getZero(Ty: SC->getType());
13757 const SCEV *Shifted = SE.getAddRecExpr(Operands, L: getLoop(),
13758 Flags: getNoWrapFlags(Mask: FlagNW));
13759 if (const auto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Val: Shifted))
13760 return ShiftedAddRec->getNumIterationsInRange(
13761 Range: Range.subtract(CI: SC->getAPInt()), SE);
13762 // This is strange and shouldn't happen.
13763 return SE.getCouldNotCompute();
13764 }
13765
13766 // The only time we can solve this is when we have all constant indices.
13767 // Otherwise, we cannot determine the overflow conditions.
13768 if (any_of(Range: operands(), P: [](const SCEV *Op) { return !isa<SCEVConstant>(Val: Op); }))
13769 return SE.getCouldNotCompute();
13770
13771 // Okay at this point we know that all elements of the chrec are constants and
13772 // that the start element is zero.
13773
13774 // First check to see if the range contains zero. If not, the first
13775 // iteration exits.
13776 unsigned BitWidth = SE.getTypeSizeInBits(Ty: getType());
13777 if (!Range.contains(Val: APInt(BitWidth, 0)))
13778 return SE.getZero(Ty: getType());
13779
13780 if (isAffine()) {
13781 // If this is an affine expression then we have this situation:
13782 // Solve {0,+,A} in Range === Ax in Range
13783
13784 // We know that zero is in the range. If A is positive then we know that
13785 // the upper value of the range must be the first possible exit value.
13786 // If A is negative then the lower of the range is the last possible loop
13787 // value. Also note that we already checked for a full range.
13788 APInt A = cast<SCEVConstant>(Val: getOperand(i: 1))->getAPInt();
13789 APInt End = A.sge(RHS: 1) ? (Range.getUpper() - 1) : Range.getLower();
13790
13791 // The exit value should be (End+A)/A.
13792 APInt ExitVal = (End + A).udiv(RHS: A);
13793 ConstantInt *ExitValue = ConstantInt::get(Context&: SE.getContext(), V: ExitVal);
13794
13795 // Evaluate at the exit value. If we really did fall out of the valid
13796 // range, then we computed our trip count, otherwise wrap around or other
13797 // things must have happened.
13798 ConstantInt *Val = EvaluateConstantChrecAtConstant(AddRec: this, C: ExitValue, SE);
13799 if (Range.contains(Val: Val->getValue()))
13800 return SE.getCouldNotCompute(); // Something strange happened
13801
13802 // Ensure that the previous value is in the range.
13803 assert(Range.contains(
13804 EvaluateConstantChrecAtConstant(this,
13805 ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) &&
13806 "Linear scev computation is off in a bad way!");
13807 return SE.getConstant(V: ExitValue);
13808 }
13809
13810 if (isQuadratic()) {
13811 if (auto S = SolveQuadraticAddRecRange(AddRec: this, Range, SE))
13812 return SE.getConstant(Val: *S);
13813 }
13814
13815 return SE.getCouldNotCompute();
13816}
13817
13818const SCEVAddRecExpr *
13819SCEVAddRecExpr::getPostIncExpr(ScalarEvolution &SE) const {
13820 assert(getNumOperands() > 1 && "AddRec with zero step?");
13821 // There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)),
13822 // but in this case we cannot guarantee that the value returned will be an
13823 // AddRec because SCEV does not have a fixed point where it stops
13824 // simplification: it is legal to return ({rec1} + {rec2}). For example, it
13825 // may happen if we reach arithmetic depth limit while simplifying. So we
13826 // construct the returned value explicitly.
13827 SmallVector<SCEVUse, 3> Ops;
13828 // If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and
13829 // (this + Step) is {A+B,+,B+C,+...,+,N}.
13830 for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i)
13831 Ops.push_back(Elt: SE.getAddExpr(LHS: getOperand(i), RHS: getOperand(i: i + 1)));
13832 // We know that the last operand is not a constant zero (otherwise it would
13833 // have been popped out earlier). This guarantees us that if the result has
13834 // the same last operand, then it will also not be popped out, meaning that
13835 // the returned value will be an AddRec.
13836 const SCEV *Last = getOperand(i: getNumOperands() - 1);
13837 assert(!Last->isZero() && "Recurrency with zero step?");
13838 Ops.push_back(Elt: Last);
13839 return cast<SCEVAddRecExpr>(Val: SE.getAddRecExpr(Operands&: Ops, L: getLoop(),
13840 Flags: SCEV::FlagAnyWrap));
13841}
13842
13843// Return true when S contains at least an undef value.
13844bool ScalarEvolution::containsUndefs(const SCEV *S) const {
13845 return SCEVExprContains(
13846 Root: S, Pred: [](const SCEV *S) { return match(S, P: m_scev_UndefOrPoison()); });
13847}
13848
13849// Return true when S contains a value that is a nullptr.
13850bool ScalarEvolution::containsErasedValue(const SCEV *S) const {
13851 return SCEVExprContains(Root: S, Pred: [](const SCEV *S) {
13852 if (const auto *SU = dyn_cast<SCEVUnknown>(Val: S))
13853 return SU->getValue() == nullptr;
13854 return false;
13855 });
13856}
13857
13858/// Return the size of an element read or written by Inst.
13859const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) {
13860 Type *Ty;
13861 if (StoreInst *Store = dyn_cast<StoreInst>(Val: Inst))
13862 Ty = Store->getValueOperand()->getType();
13863 else if (LoadInst *Load = dyn_cast<LoadInst>(Val: Inst))
13864 Ty = Load->getType();
13865 else
13866 return nullptr;
13867
13868 Type *ETy = getEffectiveSCEVType(Ty: PointerType::getUnqual(C&: Inst->getContext()));
13869 return getSizeOfExpr(IntTy: ETy, AllocTy: Ty);
13870}
13871
13872//===----------------------------------------------------------------------===//
13873// SCEVCallbackVH Class Implementation
13874//===----------------------------------------------------------------------===//
13875
13876void ScalarEvolution::SCEVCallbackVH::deleted() {
13877 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
13878 if (PHINode *PN = dyn_cast<PHINode>(Val: getValPtr()))
13879 SE->ConstantEvolutionLoopExitValue.erase(Val: PN);
13880 SE->eraseValueFromMap(V: getValPtr());
13881 // this now dangles!
13882}
13883
13884void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) {
13885 assert(SE && "SCEVCallbackVH called with a null ScalarEvolution!");
13886
13887 // Forget all the expressions associated with users of the old value,
13888 // so that future queries will recompute the expressions using the new
13889 // value.
13890 SE->forgetValue(V: getValPtr());
13891 // this now dangles!
13892}
13893
13894ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V, ScalarEvolution *se)
13895 : CallbackVH(V), SE(se) {}
13896
13897//===----------------------------------------------------------------------===//
13898// ScalarEvolution Class Implementation
13899//===----------------------------------------------------------------------===//
13900
13901ScalarEvolution::ScalarEvolution(Function &F, TargetLibraryInfo &TLI,
13902 AssumptionCache &AC, DominatorTree &DT,
13903 LoopInfo &LI)
13904 : F(F), DL(F.getDataLayout()), TLI(TLI), AC(AC), DT(DT), LI(LI),
13905 CouldNotCompute(new SCEVCouldNotCompute()), ValuesAtScopes(64),
13906 LoopDispositions(64), BlockDispositions(64) {
13907 // To use guards for proving predicates, we need to scan every instruction in
13908 // relevant basic blocks, and not just terminators. Doing this is a waste of
13909 // time if the IR does not actually contain any calls to
13910 // @llvm.experimental.guard, so do a quick check and remember this beforehand.
13911 //
13912 // This pessimizes the case where a pass that preserves ScalarEvolution wants
13913 // to _add_ guards to the module when there weren't any before, and wants
13914 // ScalarEvolution to optimize based on those guards. For now we prefer to be
13915 // efficient in lieu of being smart in that rather obscure case.
13916
13917 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
13918 M: F.getParent(), id: Intrinsic::experimental_guard);
13919 HasGuards = GuardDecl && !GuardDecl->use_empty();
13920}
13921
13922ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
13923 : F(Arg.F), DL(Arg.DL), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC),
13924 DT(Arg.DT), LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)),
13925 ValueExprMap(std::move(Arg.ValueExprMap)),
13926 PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
13927 PendingMerges(std::move(Arg.PendingMerges)),
13928 ConstantMultipleCache(std::move(Arg.ConstantMultipleCache)),
13929 BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)),
13930 PredicatedBackedgeTakenCounts(
13931 std::move(Arg.PredicatedBackedgeTakenCounts)),
13932 BECountUsers(std::move(Arg.BECountUsers)),
13933 ConstantEvolutionLoopExitValue(
13934 std::move(Arg.ConstantEvolutionLoopExitValue)),
13935 ValuesAtScopes(std::move(Arg.ValuesAtScopes)),
13936 ValuesAtScopesUsers(std::move(Arg.ValuesAtScopesUsers)),
13937 LoopDispositions(std::move(Arg.LoopDispositions)),
13938 LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)),
13939 BlockDispositions(std::move(Arg.BlockDispositions)),
13940 SCEVUsers(std::move(Arg.SCEVUsers)),
13941 UnsignedRanges(std::move(Arg.UnsignedRanges)),
13942 SignedRanges(std::move(Arg.SignedRanges)),
13943 UniqueSCEVs(std::move(Arg.UniqueSCEVs)),
13944 UniquePreds(std::move(Arg.UniquePreds)),
13945 SCEVAllocator(std::move(Arg.SCEVAllocator)),
13946 LoopUsers(std::move(Arg.LoopUsers)),
13947 PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)),
13948 FirstUnknown(Arg.FirstUnknown) {
13949 Arg.FirstUnknown = nullptr;
13950}
13951
13952ScalarEvolution::~ScalarEvolution() {
13953 // Iterate through all the SCEVUnknown instances and call their
13954 // destructors, so that they release their references to their values.
13955 for (SCEVUnknown *U = FirstUnknown; U;) {
13956 SCEVUnknown *Tmp = U;
13957 U = U->Next;
13958 Tmp->~SCEVUnknown();
13959 }
13960 FirstUnknown = nullptr;
13961
13962 ExprValueMap.clear();
13963 ValueExprMap.clear();
13964 HasRecMap.clear();
13965 BackedgeTakenCounts.clear();
13966 PredicatedBackedgeTakenCounts.clear();
13967
13968 assert(PendingLoopPredicates.empty() && "isImpliedCond garbage");
13969 assert(PendingMerges.empty() && "isImpliedViaMerge garbage");
13970 assert(!WalkingBEDominatingConds && "isLoopBackedgeGuardedByCond garbage!");
13971 assert(!ProvingSplitPredicate && "ProvingSplitPredicate garbage!");
13972}
13973
13974bool ScalarEvolution::hasLoopInvariantBackedgeTakenCount(const Loop *L) {
13975 return !isa<SCEVCouldNotCompute>(Val: getBackedgeTakenCount(L));
13976}
13977
13978/// When printing a top-level SCEV for trip counts, it's helpful to include
13979/// a type for constants which are otherwise hard to disambiguate.
13980static void PrintSCEVWithTypeHint(raw_ostream &OS, const SCEV* S) {
13981 if (isa<SCEVConstant>(Val: S))
13982 OS << *S->getType() << " ";
13983 OS << *S;
13984}
13985
13986static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE,
13987 const Loop *L) {
13988 // Print all inner loops first
13989 for (Loop *I : *L)
13990 PrintLoopInfo(OS, SE, L: I);
13991
13992 OS << "Loop ";
13993 L->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
13994 OS << ": ";
13995
13996 SmallVector<BasicBlock *, 8> ExitingBlocks;
13997 L->getExitingBlocks(ExitingBlocks);
13998 if (ExitingBlocks.size() != 1)
13999 OS << "<multiple exits> ";
14000
14001 auto *BTC = SE->getBackedgeTakenCount(L);
14002 if (!isa<SCEVCouldNotCompute>(Val: BTC)) {
14003 OS << "backedge-taken count is ";
14004 PrintSCEVWithTypeHint(OS, S: BTC);
14005 } else
14006 OS << "Unpredictable backedge-taken count.";
14007 OS << "\n";
14008
14009 if (ExitingBlocks.size() > 1)
14010 for (BasicBlock *ExitingBlock : ExitingBlocks) {
14011 OS << " exit count for " << ExitingBlock->getName() << ": ";
14012 const SCEV *EC = SE->getExitCount(L, ExitingBlock);
14013 PrintSCEVWithTypeHint(OS, S: EC);
14014 if (isa<SCEVCouldNotCompute>(Val: EC)) {
14015 // Retry with predicates.
14016 SmallVector<const SCEVPredicate *> Predicates;
14017 EC = SE->getPredicatedExitCount(L, ExitingBlock, Predicates: &Predicates);
14018 if (!isa<SCEVCouldNotCompute>(Val: EC)) {
14019 OS << "\n predicated exit count for " << ExitingBlock->getName()
14020 << ": ";
14021 PrintSCEVWithTypeHint(OS, S: EC);
14022 OS << "\n Predicates:\n";
14023 for (const auto *P : Predicates)
14024 P->print(OS, Depth: 4);
14025 }
14026 }
14027 OS << "\n";
14028 }
14029
14030 OS << "Loop ";
14031 L->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14032 OS << ": ";
14033
14034 auto *ConstantBTC = SE->getConstantMaxBackedgeTakenCount(L);
14035 if (!isa<SCEVCouldNotCompute>(Val: ConstantBTC)) {
14036 OS << "constant max backedge-taken count is ";
14037 PrintSCEVWithTypeHint(OS, S: ConstantBTC);
14038 if (SE->isBackedgeTakenCountMaxOrZero(L))
14039 OS << ", actual taken count either this or zero.";
14040 } else {
14041 OS << "Unpredictable constant max backedge-taken count. ";
14042 }
14043
14044 OS << "\n"
14045 "Loop ";
14046 L->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14047 OS << ": ";
14048
14049 auto *SymbolicBTC = SE->getSymbolicMaxBackedgeTakenCount(L);
14050 if (!isa<SCEVCouldNotCompute>(Val: SymbolicBTC)) {
14051 OS << "symbolic max backedge-taken count is ";
14052 PrintSCEVWithTypeHint(OS, S: SymbolicBTC);
14053 if (SE->isBackedgeTakenCountMaxOrZero(L))
14054 OS << ", actual taken count either this or zero.";
14055 } else {
14056 OS << "Unpredictable symbolic max backedge-taken count. ";
14057 }
14058 OS << "\n";
14059
14060 if (ExitingBlocks.size() > 1)
14061 for (BasicBlock *ExitingBlock : ExitingBlocks) {
14062 OS << " symbolic max exit count for " << ExitingBlock->getName() << ": ";
14063 auto *ExitBTC = SE->getExitCount(L, ExitingBlock,
14064 Kind: ScalarEvolution::SymbolicMaximum);
14065 PrintSCEVWithTypeHint(OS, S: ExitBTC);
14066 if (isa<SCEVCouldNotCompute>(Val: ExitBTC)) {
14067 // Retry with predicates.
14068 SmallVector<const SCEVPredicate *> Predicates;
14069 ExitBTC = SE->getPredicatedExitCount(L, ExitingBlock, Predicates: &Predicates,
14070 Kind: ScalarEvolution::SymbolicMaximum);
14071 if (!isa<SCEVCouldNotCompute>(Val: ExitBTC)) {
14072 OS << "\n predicated symbolic max exit count for "
14073 << ExitingBlock->getName() << ": ";
14074 PrintSCEVWithTypeHint(OS, S: ExitBTC);
14075 OS << "\n Predicates:\n";
14076 for (const auto *P : Predicates)
14077 P->print(OS, Depth: 4);
14078 }
14079 }
14080 OS << "\n";
14081 }
14082
14083 SmallVector<const SCEVPredicate *, 4> Preds;
14084 auto *PBT = SE->getPredicatedBackedgeTakenCount(L, Preds);
14085 if (PBT != BTC) {
14086 assert(!Preds.empty() && "Different predicated BTC, but no predicates");
14087 OS << "Loop ";
14088 L->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14089 OS << ": ";
14090 if (!isa<SCEVCouldNotCompute>(Val: PBT)) {
14091 OS << "Predicated backedge-taken count is ";
14092 PrintSCEVWithTypeHint(OS, S: PBT);
14093 } else
14094 OS << "Unpredictable predicated backedge-taken count.";
14095 OS << "\n";
14096 OS << " Predicates:\n";
14097 for (const auto *P : Preds)
14098 P->print(OS, Depth: 4);
14099 }
14100 Preds.clear();
14101
14102 auto *PredConstantMax =
14103 SE->getPredicatedConstantMaxBackedgeTakenCount(L, Preds);
14104 if (PredConstantMax != ConstantBTC) {
14105 assert(!Preds.empty() &&
14106 "different predicated constant max BTC but no predicates");
14107 OS << "Loop ";
14108 L->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14109 OS << ": ";
14110 if (!isa<SCEVCouldNotCompute>(Val: PredConstantMax)) {
14111 OS << "Predicated constant max backedge-taken count is ";
14112 PrintSCEVWithTypeHint(OS, S: PredConstantMax);
14113 } else
14114 OS << "Unpredictable predicated constant max backedge-taken count.";
14115 OS << "\n";
14116 OS << " Predicates:\n";
14117 for (const auto *P : Preds)
14118 P->print(OS, Depth: 4);
14119 }
14120 Preds.clear();
14121
14122 auto *PredSymbolicMax =
14123 SE->getPredicatedSymbolicMaxBackedgeTakenCount(L, Preds);
14124 if (SymbolicBTC != PredSymbolicMax) {
14125 assert(!Preds.empty() &&
14126 "Different predicated symbolic max BTC, but no predicates");
14127 OS << "Loop ";
14128 L->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14129 OS << ": ";
14130 if (!isa<SCEVCouldNotCompute>(Val: PredSymbolicMax)) {
14131 OS << "Predicated symbolic max backedge-taken count is ";
14132 PrintSCEVWithTypeHint(OS, S: PredSymbolicMax);
14133 } else
14134 OS << "Unpredictable predicated symbolic max backedge-taken count.";
14135 OS << "\n";
14136 OS << " Predicates:\n";
14137 for (const auto *P : Preds)
14138 P->print(OS, Depth: 4);
14139 }
14140
14141 if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
14142 OS << "Loop ";
14143 L->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14144 OS << ": ";
14145 OS << "Trip multiple is " << SE->getSmallConstantTripMultiple(L) << "\n";
14146 }
14147}
14148
14149namespace llvm {
14150// Note: these overloaded operators need to be in the llvm namespace for them
14151// to be resolved correctly. If we put them outside the llvm namespace, the
14152//
14153// OS << ": " << SE.getLoopDisposition(SV, InnerL);
14154//
14155// code below "breaks" and start printing raw enum values as opposed to the
14156// string values.
14157static raw_ostream &operator<<(raw_ostream &OS,
14158 ScalarEvolution::LoopDisposition LD) {
14159 switch (LD) {
14160 case ScalarEvolution::LoopVariant:
14161 OS << "Variant";
14162 break;
14163 case ScalarEvolution::LoopInvariant:
14164 OS << "Invariant";
14165 break;
14166 case ScalarEvolution::LoopComputable:
14167 OS << "Computable";
14168 break;
14169 }
14170 return OS;
14171}
14172
14173static raw_ostream &operator<<(raw_ostream &OS,
14174 llvm::ScalarEvolution::BlockDisposition BD) {
14175 switch (BD) {
14176 case ScalarEvolution::DoesNotDominateBlock:
14177 OS << "DoesNotDominate";
14178 break;
14179 case ScalarEvolution::DominatesBlock:
14180 OS << "Dominates";
14181 break;
14182 case ScalarEvolution::ProperlyDominatesBlock:
14183 OS << "ProperlyDominates";
14184 break;
14185 }
14186 return OS;
14187}
14188} // namespace llvm
14189
14190void ScalarEvolution::print(raw_ostream &OS) const {
14191 // ScalarEvolution's implementation of the print method is to print
14192 // out SCEV values of all instructions that are interesting. Doing
14193 // this potentially causes it to create new SCEV objects though,
14194 // which technically conflicts with the const qualifier. This isn't
14195 // observable from outside the class though, so casting away the
14196 // const isn't dangerous.
14197 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
14198
14199 if (ClassifyExpressions) {
14200 OS << "Classifying expressions for: ";
14201 F.printAsOperand(O&: OS, /*PrintType=*/false);
14202 OS << "\n";
14203 for (Instruction &I : instructions(F))
14204 if (isSCEVable(Ty: I.getType()) && !isa<CmpInst>(Val: I)) {
14205 OS << I << '\n';
14206 OS << " --> ";
14207 const SCEV *SV = SE.getSCEV(V: &I);
14208 SV->print(OS);
14209 if (!isa<SCEVCouldNotCompute>(Val: SV)) {
14210 OS << " U: ";
14211 SE.getUnsignedRange(S: SV).print(OS);
14212 OS << " S: ";
14213 SE.getSignedRange(S: SV).print(OS);
14214 }
14215
14216 const Loop *L = LI.getLoopFor(BB: I.getParent());
14217
14218 const SCEV *AtUse = SE.getSCEVAtScope(V: SV, L);
14219 if (AtUse != SV) {
14220 OS << " --> ";
14221 AtUse->print(OS);
14222 if (!isa<SCEVCouldNotCompute>(Val: AtUse)) {
14223 OS << " U: ";
14224 SE.getUnsignedRange(S: AtUse).print(OS);
14225 OS << " S: ";
14226 SE.getSignedRange(S: AtUse).print(OS);
14227 }
14228 }
14229
14230 if (L) {
14231 OS << "\t\t" "Exits: ";
14232 const SCEV *ExitValue = SE.getSCEVAtScope(V: SV, L: L->getParentLoop());
14233 if (!SE.isLoopInvariant(S: ExitValue, L)) {
14234 OS << "<<Unknown>>";
14235 } else {
14236 OS << *ExitValue;
14237 }
14238
14239 ListSeparator LS(", ", "\t\tLoopDispositions: { ");
14240 for (const auto *Iter = L; Iter; Iter = Iter->getParentLoop()) {
14241 OS << LS;
14242 Iter->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14243 OS << ": " << SE.getLoopDisposition(S: SV, L: Iter);
14244 }
14245
14246 for (const auto *InnerL : depth_first(G: L)) {
14247 if (InnerL == L)
14248 continue;
14249 OS << LS;
14250 InnerL->getHeader()->printAsOperand(O&: OS, /*PrintType=*/false);
14251 OS << ": " << SE.getLoopDisposition(S: SV, L: InnerL);
14252 }
14253
14254 OS << " }";
14255 }
14256
14257 OS << "\n";
14258 }
14259 }
14260
14261 OS << "Determining loop execution counts for: ";
14262 F.printAsOperand(O&: OS, /*PrintType=*/false);
14263 OS << "\n";
14264 for (Loop *I : LI)
14265 PrintLoopInfo(OS, SE: &SE, L: I);
14266}
14267
14268ScalarEvolution::LoopDisposition
14269ScalarEvolution::getLoopDisposition(const SCEV *S, const Loop *L) {
14270 auto &Values = LoopDispositions[S];
14271 for (auto &V : Values) {
14272 if (V.getPointer() == L)
14273 return V.getInt();
14274 }
14275 Values.emplace_back(Args&: L, Args: LoopVariant);
14276 LoopDisposition D = computeLoopDisposition(S, L);
14277 auto &Values2 = LoopDispositions[S];
14278 for (auto &V : llvm::reverse(C&: Values2)) {
14279 if (V.getPointer() == L) {
14280 V.setInt(D);
14281 break;
14282 }
14283 }
14284 return D;
14285}
14286
14287ScalarEvolution::LoopDisposition
14288ScalarEvolution::computeLoopDisposition(const SCEV *S, const Loop *L) {
14289 switch (S->getSCEVType()) {
14290 case scConstant:
14291 case scVScale:
14292 return LoopInvariant;
14293 case scAddRecExpr: {
14294 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(Val: S);
14295
14296 // If L is the addrec's loop, it's computable.
14297 if (AR->getLoop() == L)
14298 return LoopComputable;
14299
14300 // Add recurrences are never invariant in the function-body (null loop).
14301 if (!L)
14302 return LoopVariant;
14303
14304 // Everything that is not defined at loop entry is variant.
14305 if (DT.dominates(A: L->getHeader(), B: AR->getLoop()->getHeader()))
14306 return LoopVariant;
14307 assert(!L->contains(AR->getLoop()) && "Containing loop's header does not"
14308 " dominate the contained loop's header?");
14309
14310 // This recurrence is invariant w.r.t. L if AR's loop contains L.
14311 if (AR->getLoop()->contains(L))
14312 return LoopInvariant;
14313
14314 // This recurrence is variant w.r.t. L if any of its operands
14315 // are variant.
14316 for (SCEVUse Op : AR->operands())
14317 if (!isLoopInvariant(S: Op, L))
14318 return LoopVariant;
14319
14320 // Otherwise it's loop-invariant.
14321 return LoopInvariant;
14322 }
14323 case scTruncate:
14324 case scZeroExtend:
14325 case scSignExtend:
14326 case scPtrToAddr:
14327 case scPtrToInt:
14328 case scAddExpr:
14329 case scMulExpr:
14330 case scUDivExpr:
14331 case scUMaxExpr:
14332 case scSMaxExpr:
14333 case scUMinExpr:
14334 case scSMinExpr:
14335 case scSequentialUMinExpr: {
14336 bool HasVarying = false;
14337 for (SCEVUse Op : S->operands()) {
14338 LoopDisposition D = getLoopDisposition(S: Op, L);
14339 if (D == LoopVariant)
14340 return LoopVariant;
14341 if (D == LoopComputable)
14342 HasVarying = true;
14343 }
14344 return HasVarying ? LoopComputable : LoopInvariant;
14345 }
14346 case scUnknown:
14347 // All non-instruction values are loop invariant. All instructions are loop
14348 // invariant if they are not contained in the specified loop.
14349 // Instructions are never considered invariant in the function body
14350 // (null loop) because they are defined within the "loop".
14351 if (auto *I = dyn_cast<Instruction>(Val: cast<SCEVUnknown>(Val: S)->getValue()))
14352 return (L && !L->contains(Inst: I)) ? LoopInvariant : LoopVariant;
14353 return LoopInvariant;
14354 case scCouldNotCompute:
14355 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
14356 }
14357 llvm_unreachable("Unknown SCEV kind!");
14358}
14359
14360bool ScalarEvolution::isLoopInvariant(const SCEV *S, const Loop *L) {
14361 return getLoopDisposition(S, L) == LoopInvariant;
14362}
14363
14364bool ScalarEvolution::hasComputableLoopEvolution(const SCEV *S, const Loop *L) {
14365 return getLoopDisposition(S, L) == LoopComputable;
14366}
14367
14368ScalarEvolution::BlockDisposition
14369ScalarEvolution::getBlockDisposition(const SCEV *S, const BasicBlock *BB) {
14370 auto &Values = BlockDispositions[S];
14371 for (auto &V : Values) {
14372 if (V.getPointer() == BB)
14373 return V.getInt();
14374 }
14375 Values.emplace_back(Args&: BB, Args: DoesNotDominateBlock);
14376 BlockDisposition D = computeBlockDisposition(S, BB);
14377 auto &Values2 = BlockDispositions[S];
14378 for (auto &V : llvm::reverse(C&: Values2)) {
14379 if (V.getPointer() == BB) {
14380 V.setInt(D);
14381 break;
14382 }
14383 }
14384 return D;
14385}
14386
14387ScalarEvolution::BlockDisposition
14388ScalarEvolution::computeBlockDisposition(const SCEV *S, const BasicBlock *BB) {
14389 switch (S->getSCEVType()) {
14390 case scConstant:
14391 case scVScale:
14392 return ProperlyDominatesBlock;
14393 case scAddRecExpr: {
14394 // This uses a "dominates" query instead of "properly dominates" query
14395 // to test for proper dominance too, because the instruction which
14396 // produces the addrec's value is a PHI, and a PHI effectively properly
14397 // dominates its entire containing block.
14398 const SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(Val: S);
14399 if (!DT.dominates(A: AR->getLoop()->getHeader(), B: BB))
14400 return DoesNotDominateBlock;
14401
14402 // Fall through into SCEVNAryExpr handling.
14403 [[fallthrough]];
14404 }
14405 case scTruncate:
14406 case scZeroExtend:
14407 case scSignExtend:
14408 case scPtrToAddr:
14409 case scPtrToInt:
14410 case scAddExpr:
14411 case scMulExpr:
14412 case scUDivExpr:
14413 case scUMaxExpr:
14414 case scSMaxExpr:
14415 case scUMinExpr:
14416 case scSMinExpr:
14417 case scSequentialUMinExpr: {
14418 bool Proper = true;
14419 for (const SCEV *NAryOp : S->operands()) {
14420 BlockDisposition D = getBlockDisposition(S: NAryOp, BB);
14421 if (D == DoesNotDominateBlock)
14422 return DoesNotDominateBlock;
14423 if (D == DominatesBlock)
14424 Proper = false;
14425 }
14426 return Proper ? ProperlyDominatesBlock : DominatesBlock;
14427 }
14428 case scUnknown:
14429 if (Instruction *I =
14430 dyn_cast<Instruction>(Val: cast<SCEVUnknown>(Val: S)->getValue())) {
14431 if (I->getParent() == BB)
14432 return DominatesBlock;
14433 if (DT.properlyDominates(A: I->getParent(), B: BB))
14434 return ProperlyDominatesBlock;
14435 return DoesNotDominateBlock;
14436 }
14437 return ProperlyDominatesBlock;
14438 case scCouldNotCompute:
14439 llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
14440 }
14441 llvm_unreachable("Unknown SCEV kind!");
14442}
14443
14444bool ScalarEvolution::dominates(const SCEV *S, const BasicBlock *BB) {
14445 return getBlockDisposition(S, BB) >= DominatesBlock;
14446}
14447
14448bool ScalarEvolution::properlyDominates(const SCEV *S, const BasicBlock *BB) {
14449 return getBlockDisposition(S, BB) == ProperlyDominatesBlock;
14450}
14451
14452bool ScalarEvolution::hasOperand(const SCEV *S, const SCEV *Op) const {
14453 return SCEVExprContains(Root: S, Pred: [&](const SCEV *Expr) { return Expr == Op; });
14454}
14455
14456void ScalarEvolution::forgetBackedgeTakenCounts(const Loop *L,
14457 bool Predicated) {
14458 auto &BECounts =
14459 Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts;
14460 auto It = BECounts.find(Val: L);
14461 if (It != BECounts.end()) {
14462 for (const ExitNotTakenInfo &ENT : It->second.ExitNotTaken) {
14463 for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) {
14464 if (!isa<SCEVConstant>(Val: S)) {
14465 auto UserIt = BECountUsers.find(Val: S);
14466 assert(UserIt != BECountUsers.end());
14467 UserIt->second.erase(Ptr: {L, Predicated});
14468 }
14469 }
14470 }
14471 BECounts.erase(I: It);
14472 }
14473}
14474
14475void ScalarEvolution::forgetMemoizedResults(ArrayRef<SCEVUse> SCEVs) {
14476 SmallPtrSet<const SCEV *, 8> ToForget(llvm::from_range, SCEVs);
14477 SmallVector<SCEVUse, 8> Worklist(ToForget.begin(), ToForget.end());
14478
14479 while (!Worklist.empty()) {
14480 const SCEV *Curr = Worklist.pop_back_val();
14481 auto Users = SCEVUsers.find(Val: Curr);
14482 if (Users != SCEVUsers.end())
14483 for (const auto *User : Users->second)
14484 if (ToForget.insert(Ptr: User).second)
14485 Worklist.push_back(Elt: User);
14486 }
14487
14488 for (const auto *S : ToForget)
14489 forgetMemoizedResultsImpl(S);
14490
14491 for (auto I = PredicatedSCEVRewrites.begin();
14492 I != PredicatedSCEVRewrites.end();) {
14493 std::pair<const SCEV *, const Loop *> Entry = I->first;
14494 if (ToForget.count(Ptr: Entry.first))
14495 PredicatedSCEVRewrites.erase(I: I++);
14496 else
14497 ++I;
14498 }
14499}
14500
14501void ScalarEvolution::forgetMemoizedResultsImpl(const SCEV *S) {
14502 LoopDispositions.erase(Val: S);
14503 BlockDispositions.erase(Val: S);
14504 UnsignedRanges.erase(Val: S);
14505 SignedRanges.erase(Val: S);
14506 HasRecMap.erase(Val: S);
14507 ConstantMultipleCache.erase(Val: S);
14508
14509 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Val: S)) {
14510 UnsignedWrapViaInductionTried.erase(Ptr: AR);
14511 SignedWrapViaInductionTried.erase(Ptr: AR);
14512 }
14513
14514 auto ExprIt = ExprValueMap.find(Val: S);
14515 if (ExprIt != ExprValueMap.end()) {
14516 for (Value *V : ExprIt->second) {
14517 auto ValueIt = ValueExprMap.find_as(Val: V);
14518 if (ValueIt != ValueExprMap.end())
14519 ValueExprMap.erase(I: ValueIt);
14520 }
14521 ExprValueMap.erase(I: ExprIt);
14522 }
14523
14524 auto ScopeIt = ValuesAtScopes.find(Val: S);
14525 if (ScopeIt != ValuesAtScopes.end()) {
14526 for (const auto &Pair : ScopeIt->second)
14527 if (!isa_and_nonnull<SCEVConstant>(Val: Pair.second))
14528 llvm::erase(C&: ValuesAtScopesUsers[Pair.second],
14529 V: std::make_pair(x: Pair.first, y&: S));
14530 ValuesAtScopes.erase(I: ScopeIt);
14531 }
14532
14533 auto ScopeUserIt = ValuesAtScopesUsers.find(Val: S);
14534 if (ScopeUserIt != ValuesAtScopesUsers.end()) {
14535 for (const auto &Pair : ScopeUserIt->second)
14536 llvm::erase(C&: ValuesAtScopes[Pair.second], V: std::make_pair(x: Pair.first, y&: S));
14537 ValuesAtScopesUsers.erase(I: ScopeUserIt);
14538 }
14539
14540 auto BEUsersIt = BECountUsers.find(Val: S);
14541 if (BEUsersIt != BECountUsers.end()) {
14542 // Work on a copy, as forgetBackedgeTakenCounts() will modify the original.
14543 auto Copy = BEUsersIt->second;
14544 for (const auto &Pair : Copy)
14545 forgetBackedgeTakenCounts(L: Pair.getPointer(), Predicated: Pair.getInt());
14546 BECountUsers.erase(I: BEUsersIt);
14547 }
14548
14549 auto FoldUser = FoldCacheUser.find(Val: S);
14550 if (FoldUser != FoldCacheUser.end())
14551 for (auto &KV : FoldUser->second)
14552 FoldCache.erase(Val: KV);
14553 FoldCacheUser.erase(Val: S);
14554}
14555
14556void
14557ScalarEvolution::getUsedLoops(const SCEV *S,
14558 SmallPtrSetImpl<const Loop *> &LoopsUsed) {
14559 struct FindUsedLoops {
14560 FindUsedLoops(SmallPtrSetImpl<const Loop *> &LoopsUsed)
14561 : LoopsUsed(LoopsUsed) {}
14562 SmallPtrSetImpl<const Loop *> &LoopsUsed;
14563 bool follow(const SCEV *S) {
14564 if (auto *AR = dyn_cast<SCEVAddRecExpr>(Val: S))
14565 LoopsUsed.insert(Ptr: AR->getLoop());
14566 return true;
14567 }
14568
14569 bool isDone() const { return false; }
14570 };
14571
14572 FindUsedLoops F(LoopsUsed);
14573 SCEVTraversal<FindUsedLoops>(F).visitAll(Root: S);
14574}
14575
14576void ScalarEvolution::getReachableBlocks(
14577 SmallPtrSetImpl<BasicBlock *> &Reachable, Function &F) {
14578 SmallVector<BasicBlock *> Worklist;
14579 Worklist.push_back(Elt: &F.getEntryBlock());
14580 while (!Worklist.empty()) {
14581 BasicBlock *BB = Worklist.pop_back_val();
14582 if (!Reachable.insert(Ptr: BB).second)
14583 continue;
14584
14585 Value *Cond;
14586 BasicBlock *TrueBB, *FalseBB;
14587 if (match(V: BB->getTerminator(), P: m_Br(C: m_Value(V&: Cond), T: m_BasicBlock(V&: TrueBB),
14588 F: m_BasicBlock(V&: FalseBB)))) {
14589 if (auto *C = dyn_cast<ConstantInt>(Val: Cond)) {
14590 Worklist.push_back(Elt: C->isOne() ? TrueBB : FalseBB);
14591 continue;
14592 }
14593
14594 if (auto *Cmp = dyn_cast<ICmpInst>(Val: Cond)) {
14595 const SCEV *L = getSCEV(V: Cmp->getOperand(i_nocapture: 0));
14596 const SCEV *R = getSCEV(V: Cmp->getOperand(i_nocapture: 1));
14597 if (isKnownPredicateViaConstantRanges(Pred: Cmp->getCmpPredicate(), LHS: L, RHS: R)) {
14598 Worklist.push_back(Elt: TrueBB);
14599 continue;
14600 }
14601 if (isKnownPredicateViaConstantRanges(Pred: Cmp->getInverseCmpPredicate(), LHS: L,
14602 RHS: R)) {
14603 Worklist.push_back(Elt: FalseBB);
14604 continue;
14605 }
14606 }
14607 }
14608
14609 append_range(C&: Worklist, R: successors(BB));
14610 }
14611}
14612
14613void ScalarEvolution::verify() const {
14614 ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
14615 ScalarEvolution SE2(F, TLI, AC, DT, LI);
14616
14617 SmallVector<Loop *, 8> LoopStack(LI.begin(), LI.end());
14618
14619 // Map's SCEV expressions from one ScalarEvolution "universe" to another.
14620 struct SCEVMapper : public SCEVRewriteVisitor<SCEVMapper> {
14621 SCEVMapper(ScalarEvolution &SE) : SCEVRewriteVisitor<SCEVMapper>(SE) {}
14622
14623 const SCEV *visitConstant(const SCEVConstant *Constant) {
14624 return SE.getConstant(Val: Constant->getAPInt());
14625 }
14626
14627 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
14628 return SE.getUnknown(V: Expr->getValue());
14629 }
14630
14631 const SCEV *visitCouldNotCompute(const SCEVCouldNotCompute *Expr) {
14632 return SE.getCouldNotCompute();
14633 }
14634 };
14635
14636 SCEVMapper SCM(SE2);
14637 SmallPtrSet<BasicBlock *, 16> ReachableBlocks;
14638 SE2.getReachableBlocks(Reachable&: ReachableBlocks, F);
14639
14640 auto GetDelta = [&](const SCEV *Old, const SCEV *New) -> const SCEV * {
14641 if (containsUndefs(S: Old) || containsUndefs(S: New)) {
14642 // SCEV treats "undef" as an unknown but consistent value (i.e. it does
14643 // not propagate undef aggressively). This means we can (and do) fail
14644 // verification in cases where a transform makes a value go from "undef"
14645 // to "undef+1" (say). The transform is fine, since in both cases the
14646 // result is "undef", but SCEV thinks the value increased by 1.
14647 return nullptr;
14648 }
14649
14650 // Unless VerifySCEVStrict is set, we only compare constant deltas.
14651 const SCEV *Delta = SE2.getMinusSCEV(LHS: Old, RHS: New);
14652 if (!VerifySCEVStrict && !isa<SCEVConstant>(Val: Delta))
14653 return nullptr;
14654
14655 return Delta;
14656 };
14657
14658 while (!LoopStack.empty()) {
14659 auto *L = LoopStack.pop_back_val();
14660 llvm::append_range(C&: LoopStack, R&: *L);
14661
14662 // Only verify BECounts in reachable loops. For an unreachable loop,
14663 // any BECount is legal.
14664 if (!ReachableBlocks.contains(Ptr: L->getHeader()))
14665 continue;
14666
14667 // Only verify cached BECounts. Computing new BECounts may change the
14668 // results of subsequent SCEV uses.
14669 auto It = BackedgeTakenCounts.find(Val: L);
14670 if (It == BackedgeTakenCounts.end())
14671 continue;
14672
14673 auto *CurBECount =
14674 SCM.visit(S: It->second.getExact(L, SE: const_cast<ScalarEvolution *>(this)));
14675 auto *NewBECount = SE2.getBackedgeTakenCount(L);
14676
14677 if (CurBECount == SE2.getCouldNotCompute() ||
14678 NewBECount == SE2.getCouldNotCompute()) {
14679 // NB! This situation is legal, but is very suspicious -- whatever pass
14680 // change the loop to make a trip count go from could not compute to
14681 // computable or vice-versa *should have* invalidated SCEV. However, we
14682 // choose not to assert here (for now) since we don't want false
14683 // positives.
14684 continue;
14685 }
14686
14687 if (SE.getTypeSizeInBits(Ty: CurBECount->getType()) >
14688 SE.getTypeSizeInBits(Ty: NewBECount->getType()))
14689 NewBECount = SE2.getZeroExtendExpr(Op: NewBECount, Ty: CurBECount->getType());
14690 else if (SE.getTypeSizeInBits(Ty: CurBECount->getType()) <
14691 SE.getTypeSizeInBits(Ty: NewBECount->getType()))
14692 CurBECount = SE2.getZeroExtendExpr(Op: CurBECount, Ty: NewBECount->getType());
14693
14694 const SCEV *Delta = GetDelta(CurBECount, NewBECount);
14695 if (Delta && !Delta->isZero()) {
14696 dbgs() << "Trip Count for " << *L << " Changed!\n";
14697 dbgs() << "Old: " << *CurBECount << "\n";
14698 dbgs() << "New: " << *NewBECount << "\n";
14699 dbgs() << "Delta: " << *Delta << "\n";
14700 std::abort();
14701 }
14702 }
14703
14704 // Collect all valid loops currently in LoopInfo.
14705 SmallPtrSet<Loop *, 32> ValidLoops;
14706 SmallVector<Loop *, 32> Worklist(LI.begin(), LI.end());
14707 while (!Worklist.empty()) {
14708 Loop *L = Worklist.pop_back_val();
14709 if (ValidLoops.insert(Ptr: L).second)
14710 Worklist.append(in_start: L->begin(), in_end: L->end());
14711 }
14712 for (const auto &KV : ValueExprMap) {
14713#ifndef NDEBUG
14714 // Check for SCEV expressions referencing invalid/deleted loops.
14715 if (auto *AR = dyn_cast<SCEVAddRecExpr>(KV.second)) {
14716 assert(ValidLoops.contains(AR->getLoop()) &&
14717 "AddRec references invalid loop");
14718 }
14719#endif
14720
14721 // Check that the value is also part of the reverse map.
14722 auto It = ExprValueMap.find(Val: KV.second);
14723 if (It == ExprValueMap.end() || !It->second.contains(key: KV.first)) {
14724 dbgs() << "Value " << *KV.first
14725 << " is in ValueExprMap but not in ExprValueMap\n";
14726 std::abort();
14727 }
14728
14729 if (auto *I = dyn_cast<Instruction>(Val: &*KV.first)) {
14730 if (!ReachableBlocks.contains(Ptr: I->getParent()))
14731 continue;
14732 const SCEV *OldSCEV = SCM.visit(S: KV.second);
14733 const SCEV *NewSCEV = SE2.getSCEV(V: I);
14734 const SCEV *Delta = GetDelta(OldSCEV, NewSCEV);
14735 if (Delta && !Delta->isZero()) {
14736 dbgs() << "SCEV for value " << *I << " changed!\n"
14737 << "Old: " << *OldSCEV << "\n"
14738 << "New: " << *NewSCEV << "\n"
14739 << "Delta: " << *Delta << "\n";
14740 std::abort();
14741 }
14742 }
14743 }
14744
14745 for (const auto &KV : ExprValueMap) {
14746 for (Value *V : KV.second) {
14747 const SCEV *S = ValueExprMap.lookup(Val: V);
14748 if (!S) {
14749 dbgs() << "Value " << *V
14750 << " is in ExprValueMap but not in ValueExprMap\n";
14751 std::abort();
14752 }
14753 if (S != KV.first) {
14754 dbgs() << "Value " << *V << " mapped to " << *S << " rather than "
14755 << *KV.first << "\n";
14756 std::abort();
14757 }
14758 }
14759 }
14760
14761 // Verify integrity of SCEV users.
14762 for (const auto &S : UniqueSCEVs) {
14763 for (SCEVUse Op : S.operands()) {
14764 // We do not store dependencies of constants.
14765 if (isa<SCEVConstant>(Val: Op))
14766 continue;
14767 auto It = SCEVUsers.find(Val: Op);
14768 if (It != SCEVUsers.end() && It->second.count(Ptr: &S))
14769 continue;
14770 dbgs() << "Use of operand " << *Op << " by user " << S
14771 << " is not being tracked!\n";
14772 std::abort();
14773 }
14774 }
14775
14776 // Verify integrity of ValuesAtScopes users.
14777 for (const auto &ValueAndVec : ValuesAtScopes) {
14778 const SCEV *Value = ValueAndVec.first;
14779 for (const auto &LoopAndValueAtScope : ValueAndVec.second) {
14780 const Loop *L = LoopAndValueAtScope.first;
14781 const SCEV *ValueAtScope = LoopAndValueAtScope.second;
14782 if (!isa<SCEVConstant>(Val: ValueAtScope)) {
14783 auto It = ValuesAtScopesUsers.find(Val: ValueAtScope);
14784 if (It != ValuesAtScopesUsers.end() &&
14785 is_contained(Range: It->second, Element: std::make_pair(x&: L, y&: Value)))
14786 continue;
14787 dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
14788 << *ValueAtScope << " missing in ValuesAtScopesUsers\n";
14789 std::abort();
14790 }
14791 }
14792 }
14793
14794 for (const auto &ValueAtScopeAndVec : ValuesAtScopesUsers) {
14795 const SCEV *ValueAtScope = ValueAtScopeAndVec.first;
14796 for (const auto &LoopAndValue : ValueAtScopeAndVec.second) {
14797 const Loop *L = LoopAndValue.first;
14798 const SCEV *Value = LoopAndValue.second;
14799 assert(!isa<SCEVConstant>(Value));
14800 auto It = ValuesAtScopes.find(Val: Value);
14801 if (It != ValuesAtScopes.end() &&
14802 is_contained(Range: It->second, Element: std::make_pair(x&: L, y&: ValueAtScope)))
14803 continue;
14804 dbgs() << "Value: " << *Value << ", Loop: " << *L << ", ValueAtScope: "
14805 << *ValueAtScope << " missing in ValuesAtScopes\n";
14806 std::abort();
14807 }
14808 }
14809
14810 // Verify integrity of BECountUsers.
14811 auto VerifyBECountUsers = [&](bool Predicated) {
14812 auto &BECounts =
14813 Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts;
14814 for (const auto &LoopAndBEInfo : BECounts) {
14815 for (const ExitNotTakenInfo &ENT : LoopAndBEInfo.second.ExitNotTaken) {
14816 for (const SCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) {
14817 if (!isa<SCEVConstant>(Val: S)) {
14818 auto UserIt = BECountUsers.find(Val: S);
14819 if (UserIt != BECountUsers.end() &&
14820 UserIt->second.contains(Ptr: { LoopAndBEInfo.first, Predicated }))
14821 continue;
14822 dbgs() << "Value " << *S << " for loop " << *LoopAndBEInfo.first
14823 << " missing from BECountUsers\n";
14824 std::abort();
14825 }
14826 }
14827 }
14828 }
14829 };
14830 VerifyBECountUsers(/* Predicated */ false);
14831 VerifyBECountUsers(/* Predicated */ true);
14832
14833 // Verify intergity of loop disposition cache.
14834 for (auto &[S, Values] : LoopDispositions) {
14835 for (auto [Loop, CachedDisposition] : Values) {
14836 const auto RecomputedDisposition = SE2.getLoopDisposition(S, L: Loop);
14837 if (CachedDisposition != RecomputedDisposition) {
14838 dbgs() << "Cached disposition of " << *S << " for loop " << *Loop
14839 << " is incorrect: cached " << CachedDisposition << ", actual "
14840 << RecomputedDisposition << "\n";
14841 std::abort();
14842 }
14843 }
14844 }
14845
14846 // Verify integrity of the block disposition cache.
14847 for (auto &[S, Values] : BlockDispositions) {
14848 for (auto [BB, CachedDisposition] : Values) {
14849 const auto RecomputedDisposition = SE2.getBlockDisposition(S, BB);
14850 if (CachedDisposition != RecomputedDisposition) {
14851 dbgs() << "Cached disposition of " << *S << " for block %"
14852 << BB->getName() << " is incorrect: cached " << CachedDisposition
14853 << ", actual " << RecomputedDisposition << "\n";
14854 std::abort();
14855 }
14856 }
14857 }
14858
14859 // Verify FoldCache/FoldCacheUser caches.
14860 for (auto [FoldID, Expr] : FoldCache) {
14861 auto I = FoldCacheUser.find(Val: Expr);
14862 if (I == FoldCacheUser.end()) {
14863 dbgs() << "Missing entry in FoldCacheUser for cached expression " << *Expr
14864 << "!\n";
14865 std::abort();
14866 }
14867 if (!is_contained(Range: I->second, Element: FoldID)) {
14868 dbgs() << "Missing FoldID in cached users of " << *Expr << "!\n";
14869 std::abort();
14870 }
14871 }
14872 for (auto [Expr, IDs] : FoldCacheUser) {
14873 for (auto &FoldID : IDs) {
14874 const SCEV *S = FoldCache.lookup(Val: FoldID);
14875 if (!S) {
14876 dbgs() << "Missing entry in FoldCache for expression " << *Expr
14877 << "!\n";
14878 std::abort();
14879 }
14880 if (S != Expr) {
14881 dbgs() << "Entry in FoldCache doesn't match FoldCacheUser: " << *S
14882 << " != " << *Expr << "!\n";
14883 std::abort();
14884 }
14885 }
14886 }
14887
14888 // Verify that ConstantMultipleCache computations are correct. We check that
14889 // cached multiples and recomputed multiples are multiples of each other to
14890 // verify correctness. It is possible that a recomputed multiple is different
14891 // from the cached multiple due to strengthened no wrap flags or changes in
14892 // KnownBits computations.
14893 for (auto [S, Multiple] : ConstantMultipleCache) {
14894 APInt RecomputedMultiple = SE2.getConstantMultiple(S);
14895 if ((Multiple != 0 && RecomputedMultiple != 0 &&
14896 Multiple.urem(RHS: RecomputedMultiple) != 0 &&
14897 RecomputedMultiple.urem(RHS: Multiple) != 0)) {
14898 dbgs() << "Incorrect cached computation in ConstantMultipleCache for "
14899 << *S << " : Computed " << RecomputedMultiple
14900 << " but cache contains " << Multiple << "!\n";
14901 std::abort();
14902 }
14903 }
14904}
14905
14906bool ScalarEvolution::invalidate(
14907 Function &F, const PreservedAnalyses &PA,
14908 FunctionAnalysisManager::Invalidator &Inv) {
14909 // Invalidate the ScalarEvolution object whenever it isn't preserved or one
14910 // of its dependencies is invalidated.
14911 auto PAC = PA.getChecker<ScalarEvolutionAnalysis>();
14912 return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
14913 Inv.invalidate<AssumptionAnalysis>(IR&: F, PA) ||
14914 Inv.invalidate<DominatorTreeAnalysis>(IR&: F, PA) ||
14915 Inv.invalidate<LoopAnalysis>(IR&: F, PA);
14916}
14917
14918AnalysisKey ScalarEvolutionAnalysis::Key;
14919
14920ScalarEvolution ScalarEvolutionAnalysis::run(Function &F,
14921 FunctionAnalysisManager &AM) {
14922 auto &TLI = AM.getResult<TargetLibraryAnalysis>(IR&: F);
14923 auto &AC = AM.getResult<AssumptionAnalysis>(IR&: F);
14924 auto &DT = AM.getResult<DominatorTreeAnalysis>(IR&: F);
14925 auto &LI = AM.getResult<LoopAnalysis>(IR&: F);
14926 return ScalarEvolution(F, TLI, AC, DT, LI);
14927}
14928
14929PreservedAnalyses
14930ScalarEvolutionVerifierPass::run(Function &F, FunctionAnalysisManager &AM) {
14931 AM.getResult<ScalarEvolutionAnalysis>(IR&: F).verify();
14932 return PreservedAnalyses::all();
14933}
14934
14935PreservedAnalyses
14936ScalarEvolutionPrinterPass::run(Function &F, FunctionAnalysisManager &AM) {
14937 // For compatibility with opt's -analyze feature under legacy pass manager
14938 // which was not ported to NPM. This keeps tests using
14939 // update_analyze_test_checks.py working.
14940 OS << "Printing analysis 'Scalar Evolution Analysis' for function '"
14941 << F.getName() << "':\n";
14942 AM.getResult<ScalarEvolutionAnalysis>(IR&: F).print(OS);
14943 return PreservedAnalyses::all();
14944}
14945
14946INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass, "scalar-evolution",
14947 "Scalar Evolution Analysis", false, true)
14948INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
14949INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
14950INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
14951INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
14952INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution",
14953 "Scalar Evolution Analysis", false, true)
14954
14955char ScalarEvolutionWrapperPass::ID = 0;
14956
14957ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() : FunctionPass(ID) {}
14958
14959bool ScalarEvolutionWrapperPass::runOnFunction(Function &F) {
14960 SE.reset(p: new ScalarEvolution(
14961 F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
14962 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
14963 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
14964 getAnalysis<LoopInfoWrapperPass>().getLoopInfo()));
14965 return false;
14966}
14967
14968void ScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); }
14969
14970void ScalarEvolutionWrapperPass::print(raw_ostream &OS, const Module *) const {
14971 SE->print(OS);
14972}
14973
14974void ScalarEvolutionWrapperPass::verifyAnalysis() const {
14975 if (!VerifySCEV)
14976 return;
14977
14978 SE->verify();
14979}
14980
14981void ScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
14982 AU.setPreservesAll();
14983 AU.addRequiredTransitive<AssumptionCacheTracker>();
14984 AU.addRequiredTransitive<LoopInfoWrapperPass>();
14985 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
14986 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
14987}
14988
14989const SCEVPredicate *ScalarEvolution::getEqualPredicate(const SCEV *LHS,
14990 const SCEV *RHS) {
14991 return getComparePredicate(Pred: ICmpInst::ICMP_EQ, LHS, RHS);
14992}
14993
14994const SCEVPredicate *
14995ScalarEvolution::getComparePredicate(const ICmpInst::Predicate Pred,
14996 const SCEV *LHS, const SCEV *RHS) {
14997 FoldingSetNodeID ID;
14998 assert(LHS->getType() == RHS->getType() &&
14999 "Type mismatch between LHS and RHS");
15000 // Unique this node based on the arguments
15001 ID.AddInteger(I: SCEVPredicate::P_Compare);
15002 ID.AddInteger(I: Pred);
15003 ID.AddPointer(Ptr: LHS);
15004 ID.AddPointer(Ptr: RHS);
15005 void *IP = nullptr;
15006 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, InsertPos&: IP))
15007 return S;
15008 SCEVComparePredicate *Eq = new (SCEVAllocator)
15009 SCEVComparePredicate(ID.Intern(Allocator&: SCEVAllocator), Pred, LHS, RHS);
15010 UniquePreds.InsertNode(N: Eq, InsertPos: IP);
15011 return Eq;
15012}
15013
15014const SCEVPredicate *ScalarEvolution::getWrapPredicate(
15015 const SCEVAddRecExpr *AR,
15016 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
15017 FoldingSetNodeID ID;
15018 // Unique this node based on the arguments
15019 ID.AddInteger(I: SCEVPredicate::P_Wrap);
15020 ID.AddPointer(Ptr: AR);
15021 ID.AddInteger(I: AddedFlags);
15022 void *IP = nullptr;
15023 if (const auto *S = UniquePreds.FindNodeOrInsertPos(ID, InsertPos&: IP))
15024 return S;
15025 auto *OF = new (SCEVAllocator)
15026 SCEVWrapPredicate(ID.Intern(Allocator&: SCEVAllocator), AR, AddedFlags);
15027 UniquePreds.InsertNode(N: OF, InsertPos: IP);
15028 return OF;
15029}
15030
15031namespace {
15032
15033class SCEVPredicateRewriter : public SCEVRewriteVisitor<SCEVPredicateRewriter> {
15034public:
15035
15036 /// Rewrites \p S in the context of a loop L and the SCEV predication
15037 /// infrastructure.
15038 ///
15039 /// If \p Pred is non-null, the SCEV expression is rewritten to respect the
15040 /// equivalences present in \p Pred.
15041 ///
15042 /// If \p NewPreds is non-null, rewrite is free to add further predicates to
15043 /// \p NewPreds such that the result will be an AddRecExpr.
15044 static const SCEV *rewrite(const SCEV *S, const Loop *L, ScalarEvolution &SE,
15045 SmallVectorImpl<const SCEVPredicate *> *NewPreds,
15046 const SCEVPredicate *Pred) {
15047 SCEVPredicateRewriter Rewriter(L, SE, NewPreds, Pred);
15048 return Rewriter.visit(S);
15049 }
15050
15051 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
15052 if (Pred) {
15053 if (auto *U = dyn_cast<SCEVUnionPredicate>(Val: Pred)) {
15054 for (const auto *Pred : U->getPredicates())
15055 if (const auto *IPred = dyn_cast<SCEVComparePredicate>(Val: Pred))
15056 if (IPred->getLHS() == Expr &&
15057 IPred->getPredicate() == ICmpInst::ICMP_EQ)
15058 return IPred->getRHS();
15059 } else if (const auto *IPred = dyn_cast<SCEVComparePredicate>(Val: Pred)) {
15060 if (IPred->getLHS() == Expr &&
15061 IPred->getPredicate() == ICmpInst::ICMP_EQ)
15062 return IPred->getRHS();
15063 }
15064 }
15065 return convertToAddRecWithPreds(Expr);
15066 }
15067
15068 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
15069 const SCEV *Operand = visit(S: Expr->getOperand());
15070 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: Operand);
15071 if (AR && AR->getLoop() == L && AR->isAffine()) {
15072 // This couldn't be folded because the operand didn't have the nuw
15073 // flag. Add the nusw flag as an assumption that we could make.
15074 const SCEV *Step = AR->getStepRecurrence(SE);
15075 Type *Ty = Expr->getType();
15076 if (addOverflowAssumption(AR, AddedFlags: SCEVWrapPredicate::IncrementNUSW))
15077 return SE.getAddRecExpr(Start: SE.getZeroExtendExpr(Op: AR->getStart(), Ty),
15078 Step: SE.getSignExtendExpr(Op: Step, Ty), L,
15079 Flags: AR->getNoWrapFlags());
15080 }
15081 return SE.getZeroExtendExpr(Op: Operand, Ty: Expr->getType());
15082 }
15083
15084 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
15085 const SCEV *Operand = visit(S: Expr->getOperand());
15086 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Val: Operand);
15087 if (AR && AR->getLoop() == L && AR->isAffine()) {
15088 // This couldn't be folded because the operand didn't have the nsw
15089 // flag. Add the nssw flag as an assumption that we could make.
15090 const SCEV *Step = AR->getStepRecurrence(SE);
15091 Type *Ty = Expr->getType();
15092 if (addOverflowAssumption(AR, AddedFlags: SCEVWrapPredicate::IncrementNSSW))
15093 return SE.getAddRecExpr(Start: SE.getSignExtendExpr(Op: AR->getStart(), Ty),
15094 Step: SE.getSignExtendExpr(Op: Step, Ty), L,
15095 Flags: AR->getNoWrapFlags());
15096 }
15097 return SE.getSignExtendExpr(Op: Operand, Ty: Expr->getType());
15098 }
15099
15100private:
15101 explicit SCEVPredicateRewriter(
15102 const Loop *L, ScalarEvolution &SE,
15103 SmallVectorImpl<const SCEVPredicate *> *NewPreds,
15104 const SCEVPredicate *Pred)
15105 : SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred), L(L) {}
15106
15107 bool addOverflowAssumption(const SCEVPredicate *P) {
15108 if (!NewPreds) {
15109 // Check if we've already made this assumption.
15110 return Pred && Pred->implies(N: P, SE);
15111 }
15112 NewPreds->push_back(Elt: P);
15113 return true;
15114 }
15115
15116 bool addOverflowAssumption(const SCEVAddRecExpr *AR,
15117 SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
15118 auto *A = SE.getWrapPredicate(AR, AddedFlags);
15119 return addOverflowAssumption(P: A);
15120 }
15121
15122 // If \p Expr represents a PHINode, we try to see if it can be represented
15123 // as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible
15124 // to add this predicate as a runtime overflow check, we return the AddRec.
15125 // If \p Expr does not meet these conditions (is not a PHI node, or we
15126 // couldn't create an AddRec for it, or couldn't add the predicate), we just
15127 // return \p Expr.
15128 const SCEV *convertToAddRecWithPreds(const SCEVUnknown *Expr) {
15129 if (!isa<PHINode>(Val: Expr->getValue()))
15130 return Expr;
15131 std::optional<
15132 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
15133 PredicatedRewrite = SE.createAddRecFromPHIWithCasts(SymbolicPHI: Expr);
15134 if (!PredicatedRewrite)
15135 return Expr;
15136 for (const auto *P : PredicatedRewrite->second){
15137 // Wrap predicates from outer loops are not supported.
15138 if (auto *WP = dyn_cast<const SCEVWrapPredicate>(Val: P)) {
15139 if (L != WP->getExpr()->getLoop())
15140 return Expr;
15141 }
15142 if (!addOverflowAssumption(P))
15143 return Expr;
15144 }
15145 return PredicatedRewrite->first;
15146 }
15147
15148 SmallVectorImpl<const SCEVPredicate *> *NewPreds;
15149 const SCEVPredicate *Pred;
15150 const Loop *L;
15151};
15152
15153} // end anonymous namespace
15154
15155const SCEV *
15156ScalarEvolution::rewriteUsingPredicate(const SCEV *S, const Loop *L,
15157 const SCEVPredicate &Preds) {
15158 return SCEVPredicateRewriter::rewrite(S, L, SE&: *this, NewPreds: nullptr, Pred: &Preds);
15159}
15160
15161const SCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates(
15162 const SCEV *S, const Loop *L,
15163 SmallVectorImpl<const SCEVPredicate *> &Preds) {
15164 SmallVector<const SCEVPredicate *> TransformPreds;
15165 S = SCEVPredicateRewriter::rewrite(S, L, SE&: *this, NewPreds: &TransformPreds, Pred: nullptr);
15166 auto *AddRec = dyn_cast<SCEVAddRecExpr>(Val: S);
15167
15168 if (!AddRec)
15169 return nullptr;
15170
15171 // Check if any of the transformed predicates is known to be false. In that
15172 // case, it doesn't make sense to convert to a predicated AddRec, as the
15173 // versioned loop will never execute.
15174 for (const SCEVPredicate *Pred : TransformPreds) {
15175 auto *WrapPred = dyn_cast<SCEVWrapPredicate>(Val: Pred);
15176 if (!WrapPred || WrapPred->getFlags() != SCEVWrapPredicate::IncrementNSSW)
15177 continue;
15178
15179 const SCEVAddRecExpr *AddRecToCheck = WrapPred->getExpr();
15180 const SCEV *ExitCount = getBackedgeTakenCount(L: AddRecToCheck->getLoop());
15181 if (isa<SCEVCouldNotCompute>(Val: ExitCount))
15182 continue;
15183
15184 const SCEV *Step = AddRecToCheck->getStepRecurrence(SE&: *this);
15185 if (!Step->isOne())
15186 continue;
15187
15188 ExitCount = getTruncateOrSignExtend(V: ExitCount, Ty: Step->getType());
15189 const SCEV *Add = getAddExpr(LHS: AddRecToCheck->getStart(), RHS: ExitCount);
15190 if (isKnownPredicate(Pred: CmpInst::ICMP_SLT, LHS: Add, RHS: AddRecToCheck->getStart()))
15191 return nullptr;
15192 }
15193
15194 // Since the transformation was successful, we can now transfer the SCEV
15195 // predicates.
15196 Preds.append(in_start: TransformPreds.begin(), in_end: TransformPreds.end());
15197
15198 return AddRec;
15199}
15200
15201/// SCEV predicates
15202SCEVPredicate::SCEVPredicate(const FoldingSetNodeIDRef ID,
15203 SCEVPredicateKind Kind)
15204 : FastID(ID), Kind(Kind) {}
15205
15206SCEVComparePredicate::SCEVComparePredicate(const FoldingSetNodeIDRef ID,
15207 const ICmpInst::Predicate Pred,
15208 const SCEV *LHS, const SCEV *RHS)
15209 : SCEVPredicate(ID, P_Compare), Pred(Pred), LHS(LHS), RHS(RHS) {
15210 assert(LHS->getType() == RHS->getType() && "LHS and RHS types don't match");
15211 assert(LHS != RHS && "LHS and RHS are the same SCEV");
15212}
15213
15214bool SCEVComparePredicate::implies(const SCEVPredicate *N,
15215 ScalarEvolution &SE) const {
15216 const auto *Op = dyn_cast<SCEVComparePredicate>(Val: N);
15217
15218 if (!Op)
15219 return false;
15220
15221 if (Pred != ICmpInst::ICMP_EQ)
15222 return false;
15223
15224 return Op->LHS == LHS && Op->RHS == RHS;
15225}
15226
15227bool SCEVComparePredicate::isAlwaysTrue() const { return false; }
15228
15229void SCEVComparePredicate::print(raw_ostream &OS, unsigned Depth) const {
15230 if (Pred == ICmpInst::ICMP_EQ)
15231 OS.indent(NumSpaces: Depth) << "Equal predicate: " << *LHS << " == " << *RHS << "\n";
15232 else
15233 OS.indent(NumSpaces: Depth) << "Compare predicate: " << *LHS << " " << Pred << ") "
15234 << *RHS << "\n";
15235
15236}
15237
15238SCEVWrapPredicate::SCEVWrapPredicate(const FoldingSetNodeIDRef ID,
15239 const SCEVAddRecExpr *AR,
15240 IncrementWrapFlags Flags)
15241 : SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {}
15242
15243const SCEVAddRecExpr *SCEVWrapPredicate::getExpr() const { return AR; }
15244
15245bool SCEVWrapPredicate::implies(const SCEVPredicate *N,
15246 ScalarEvolution &SE) const {
15247 const auto *Op = dyn_cast<SCEVWrapPredicate>(Val: N);
15248 if (!Op || setFlags(Flags, OnFlags: Op->Flags) != Flags)
15249 return false;
15250
15251 if (Op->AR == AR)
15252 return true;
15253
15254 if (Flags != SCEVWrapPredicate::IncrementNSSW &&
15255 Flags != SCEVWrapPredicate::IncrementNUSW)
15256 return false;
15257
15258 const SCEV *Start = AR->getStart();
15259 const SCEV *OpStart = Op->AR->getStart();
15260 if (Start->getType()->isPointerTy() != OpStart->getType()->isPointerTy())
15261 return false;
15262
15263 // Reject pointers to different address spaces.
15264 if (Start->getType()->isPointerTy() && Start->getType() != OpStart->getType())
15265 return false;
15266
15267 const SCEV *Step = AR->getStepRecurrence(SE);
15268 const SCEV *OpStep = Op->AR->getStepRecurrence(SE);
15269 if (!SE.isKnownPositive(S: Step) || !SE.isKnownPositive(S: OpStep))
15270 return false;
15271
15272 // If both steps are positive, this implies N, if N's start and step are
15273 // ULE/SLE (for NSUW/NSSW) than this'.
15274 Type *WiderTy = SE.getWiderType(T1: Step->getType(), T2: OpStep->getType());
15275 Step = SE.getNoopOrZeroExtend(V: Step, Ty: WiderTy);
15276 OpStep = SE.getNoopOrZeroExtend(V: OpStep, Ty: WiderTy);
15277
15278 bool IsNUW = Flags == SCEVWrapPredicate::IncrementNUSW;
15279 OpStart = IsNUW ? SE.getNoopOrZeroExtend(V: OpStart, Ty: WiderTy)
15280 : SE.getNoopOrSignExtend(V: OpStart, Ty: WiderTy);
15281 Start = IsNUW ? SE.getNoopOrZeroExtend(V: Start, Ty: WiderTy)
15282 : SE.getNoopOrSignExtend(V: Start, Ty: WiderTy);
15283 CmpInst::Predicate Pred = IsNUW ? CmpInst::ICMP_ULE : CmpInst::ICMP_SLE;
15284 return SE.isKnownPredicate(Pred, LHS: OpStep, RHS: Step) &&
15285 SE.isKnownPredicate(Pred, LHS: OpStart, RHS: Start);
15286}
15287
15288bool SCEVWrapPredicate::isAlwaysTrue() const {
15289 SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags();
15290 IncrementWrapFlags IFlags = Flags;
15291
15292 if (ScalarEvolution::setFlags(Flags: ScevFlags, OnFlags: SCEV::FlagNSW) == ScevFlags)
15293 IFlags = clearFlags(Flags: IFlags, OffFlags: IncrementNSSW);
15294
15295 return IFlags == IncrementAnyWrap;
15296}
15297
15298void SCEVWrapPredicate::print(raw_ostream &OS, unsigned Depth) const {
15299 OS.indent(NumSpaces: Depth) << *getExpr() << " Added Flags: ";
15300 if (SCEVWrapPredicate::IncrementNUSW & getFlags())
15301 OS << "<nusw>";
15302 if (SCEVWrapPredicate::IncrementNSSW & getFlags())
15303 OS << "<nssw>";
15304 OS << "\n";
15305}
15306
15307SCEVWrapPredicate::IncrementWrapFlags
15308SCEVWrapPredicate::getImpliedFlags(const SCEVAddRecExpr *AR,
15309 ScalarEvolution &SE) {
15310 IncrementWrapFlags ImpliedFlags = IncrementAnyWrap;
15311 SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags();
15312
15313 // We can safely transfer the NSW flag as NSSW.
15314 if (ScalarEvolution::setFlags(Flags: StaticFlags, OnFlags: SCEV::FlagNSW) == StaticFlags)
15315 ImpliedFlags = IncrementNSSW;
15316
15317 if (ScalarEvolution::setFlags(Flags: StaticFlags, OnFlags: SCEV::FlagNUW) == StaticFlags) {
15318 // If the increment is positive, the SCEV NUW flag will also imply the
15319 // WrapPredicate NUSW flag.
15320 if (const auto *Step = dyn_cast<SCEVConstant>(Val: AR->getStepRecurrence(SE)))
15321 if (Step->getValue()->getValue().isNonNegative())
15322 ImpliedFlags = setFlags(Flags: ImpliedFlags, OnFlags: IncrementNUSW);
15323 }
15324
15325 return ImpliedFlags;
15326}
15327
15328/// Union predicates don't get cached so create a dummy set ID for it.
15329SCEVUnionPredicate::SCEVUnionPredicate(ArrayRef<const SCEVPredicate *> Preds,
15330 ScalarEvolution &SE)
15331 : SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) {
15332 for (const auto *P : Preds)
15333 add(N: P, SE);
15334}
15335
15336bool SCEVUnionPredicate::isAlwaysTrue() const {
15337 return all_of(Range: Preds,
15338 P: [](const SCEVPredicate *I) { return I->isAlwaysTrue(); });
15339}
15340
15341bool SCEVUnionPredicate::implies(const SCEVPredicate *N,
15342 ScalarEvolution &SE) const {
15343 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(Val: N))
15344 return all_of(Range: Set->Preds, P: [this, &SE](const SCEVPredicate *I) {
15345 return this->implies(N: I, SE);
15346 });
15347
15348 return any_of(Range: Preds,
15349 P: [N, &SE](const SCEVPredicate *I) { return I->implies(N, SE); });
15350}
15351
15352void SCEVUnionPredicate::print(raw_ostream &OS, unsigned Depth) const {
15353 for (const auto *Pred : Preds)
15354 Pred->print(OS, Depth);
15355}
15356
15357void SCEVUnionPredicate::add(const SCEVPredicate *N, ScalarEvolution &SE) {
15358 if (const auto *Set = dyn_cast<SCEVUnionPredicate>(Val: N)) {
15359 for (const auto *Pred : Set->Preds)
15360 add(N: Pred, SE);
15361 return;
15362 }
15363
15364 // Implication checks are quadratic in the number of predicates. Stop doing
15365 // them if there are many predicates, as they should be too expensive to use
15366 // anyway at that point.
15367 bool CheckImplies = Preds.size() < 16;
15368
15369 // Only add predicate if it is not already implied by this union predicate.
15370 if (CheckImplies && implies(N, SE))
15371 return;
15372
15373 // Build a new vector containing the current predicates, except the ones that
15374 // are implied by the new predicate N.
15375 SmallVector<const SCEVPredicate *> PrunedPreds;
15376 for (auto *P : Preds) {
15377 if (CheckImplies && N->implies(N: P, SE))
15378 continue;
15379 PrunedPreds.push_back(Elt: P);
15380 }
15381 Preds = std::move(PrunedPreds);
15382 Preds.push_back(Elt: N);
15383}
15384
15385PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE,
15386 Loop &L)
15387 : SE(SE), L(L) {
15388 SmallVector<const SCEVPredicate*, 4> Empty;
15389 Preds = std::make_unique<SCEVUnionPredicate>(args&: Empty, args&: SE);
15390}
15391
15392void ScalarEvolution::registerUser(const SCEV *User,
15393 ArrayRef<const SCEV *> Ops) {
15394 for (const auto *Op : Ops)
15395 // We do not expect that forgetting cached data for SCEVConstants will ever
15396 // open any prospects for sharpening or introduce any correctness issues,
15397 // so we don't bother storing their dependencies.
15398 if (!isa<SCEVConstant>(Val: Op))
15399 SCEVUsers[Op].insert(Ptr: User);
15400}
15401
15402void ScalarEvolution::registerUser(const SCEV *User, ArrayRef<SCEVUse> Ops) {
15403 for (const SCEV *Op : Ops)
15404 // We do not expect that forgetting cached data for SCEVConstants will ever
15405 // open any prospects for sharpening or introduce any correctness issues,
15406 // so we don't bother storing their dependencies.
15407 if (!isa<SCEVConstant>(Val: Op))
15408 SCEVUsers[Op].insert(Ptr: User);
15409}
15410
15411const SCEV *PredicatedScalarEvolution::getSCEV(Value *V) {
15412 const SCEV *Expr = SE.getSCEV(V);
15413 return getPredicatedSCEV(Expr);
15414}
15415
15416const SCEV *PredicatedScalarEvolution::getPredicatedSCEV(const SCEV *Expr) {
15417 RewriteEntry &Entry = RewriteMap[Expr];
15418
15419 // If we already have an entry and the version matches, return it.
15420 if (Entry.second && Generation == Entry.first)
15421 return Entry.second;
15422
15423 // We found an entry but it's stale. Rewrite the stale entry
15424 // according to the current predicate.
15425 if (Entry.second)
15426 Expr = Entry.second;
15427
15428 const SCEV *NewSCEV = SE.rewriteUsingPredicate(S: Expr, L: &L, Preds: *Preds);
15429 Entry = {Generation, NewSCEV};
15430
15431 return NewSCEV;
15432}
15433
15434const SCEV *PredicatedScalarEvolution::getBackedgeTakenCount() {
15435 if (!BackedgeCount) {
15436 SmallVector<const SCEVPredicate *, 4> Preds;
15437 BackedgeCount = SE.getPredicatedBackedgeTakenCount(L: &L, Preds);
15438 for (const auto *P : Preds)
15439 addPredicate(Pred: *P);
15440 }
15441 return BackedgeCount;
15442}
15443
15444const SCEV *PredicatedScalarEvolution::getSymbolicMaxBackedgeTakenCount() {
15445 if (!SymbolicMaxBackedgeCount) {
15446 SmallVector<const SCEVPredicate *, 4> Preds;
15447 SymbolicMaxBackedgeCount =
15448 SE.getPredicatedSymbolicMaxBackedgeTakenCount(L: &L, Preds);
15449 for (const auto *P : Preds)
15450 addPredicate(Pred: *P);
15451 }
15452 return SymbolicMaxBackedgeCount;
15453}
15454
15455unsigned PredicatedScalarEvolution::getSmallConstantMaxTripCount() {
15456 if (!SmallConstantMaxTripCount) {
15457 SmallVector<const SCEVPredicate *, 4> Preds;
15458 SmallConstantMaxTripCount = SE.getSmallConstantMaxTripCount(L: &L, Predicates: &Preds);
15459 for (const auto *P : Preds)
15460 addPredicate(Pred: *P);
15461 }
15462 return *SmallConstantMaxTripCount;
15463}
15464
15465void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) {
15466 if (Preds->implies(N: &Pred, SE))
15467 return;
15468
15469 SmallVector<const SCEVPredicate *, 4> NewPreds(Preds->getPredicates());
15470 NewPreds.push_back(Elt: &Pred);
15471 Preds = std::make_unique<SCEVUnionPredicate>(args&: NewPreds, args&: SE);
15472 updateGeneration();
15473}
15474
15475const SCEVPredicate &PredicatedScalarEvolution::getPredicate() const {
15476 return *Preds;
15477}
15478
15479void PredicatedScalarEvolution::updateGeneration() {
15480 // If the generation number wrapped recompute everything.
15481 if (++Generation == 0) {
15482 for (auto &II : RewriteMap) {
15483 const SCEV *Rewritten = II.second.second;
15484 II.second = {Generation, SE.rewriteUsingPredicate(S: Rewritten, L: &L, Preds: *Preds)};
15485 }
15486 }
15487}
15488
15489void PredicatedScalarEvolution::setNoOverflow(
15490 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
15491 const SCEV *Expr = getSCEV(V);
15492 const auto *AR = cast<SCEVAddRecExpr>(Val: Expr);
15493
15494 auto ImpliedFlags = SCEVWrapPredicate::getImpliedFlags(AR, SE);
15495
15496 // Clear the statically implied flags.
15497 Flags = SCEVWrapPredicate::clearFlags(Flags, OffFlags: ImpliedFlags);
15498 addPredicate(Pred: *SE.getWrapPredicate(AR, AddedFlags: Flags));
15499
15500 auto II = FlagsMap.insert(KV: {V, Flags});
15501 if (!II.second)
15502 II.first->second = SCEVWrapPredicate::setFlags(Flags, OnFlags: II.first->second);
15503}
15504
15505bool PredicatedScalarEvolution::hasNoOverflow(
15506 Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags) {
15507 const SCEV *Expr = getSCEV(V);
15508 const auto *AR = cast<SCEVAddRecExpr>(Val: Expr);
15509
15510 Flags = SCEVWrapPredicate::clearFlags(
15511 Flags, OffFlags: SCEVWrapPredicate::getImpliedFlags(AR, SE));
15512
15513 auto II = FlagsMap.find(Val: V);
15514
15515 if (II != FlagsMap.end())
15516 Flags = SCEVWrapPredicate::clearFlags(Flags, OffFlags: II->second);
15517
15518 return Flags == SCEVWrapPredicate::IncrementAnyWrap;
15519}
15520
15521const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) {
15522 const SCEV *Expr = this->getSCEV(V);
15523 SmallVector<const SCEVPredicate *, 4> NewPreds;
15524 auto *New = SE.convertSCEVToAddRecWithPredicates(S: Expr, L: &L, Preds&: NewPreds);
15525
15526 if (!New)
15527 return nullptr;
15528
15529 for (const auto *P : NewPreds)
15530 addPredicate(Pred: *P);
15531
15532 RewriteMap[SE.getSCEV(V)] = {Generation, New};
15533 return New;
15534}
15535
15536PredicatedScalarEvolution::PredicatedScalarEvolution(
15537 const PredicatedScalarEvolution &Init)
15538 : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L),
15539 Preds(std::make_unique<SCEVUnionPredicate>(args: Init.Preds->getPredicates(),
15540 args&: SE)),
15541 Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) {
15542 for (auto I : Init.FlagsMap)
15543 FlagsMap.insert(KV: I);
15544}
15545
15546void PredicatedScalarEvolution::print(raw_ostream &OS, unsigned Depth) const {
15547 // For each block.
15548 for (auto *BB : L.getBlocks())
15549 for (auto &I : *BB) {
15550 if (!SE.isSCEVable(Ty: I.getType()))
15551 continue;
15552
15553 auto *Expr = SE.getSCEV(V: &I);
15554 auto II = RewriteMap.find(Val: Expr);
15555
15556 if (II == RewriteMap.end())
15557 continue;
15558
15559 // Don't print things that are not interesting.
15560 if (II->second.second == Expr)
15561 continue;
15562
15563 OS.indent(NumSpaces: Depth) << "[PSE]" << I << ":\n";
15564 OS.indent(NumSpaces: Depth + 2) << *Expr << "\n";
15565 OS.indent(NumSpaces: Depth + 2) << "--> " << *II->second.second << "\n";
15566 }
15567}
15568
15569ScalarEvolution::LoopGuards
15570ScalarEvolution::LoopGuards::collect(const Loop *L, ScalarEvolution &SE) {
15571 BasicBlock *Header = L->getHeader();
15572 BasicBlock *Pred = L->getLoopPredecessor();
15573 LoopGuards Guards(SE);
15574 if (!Pred)
15575 return Guards;
15576 SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
15577 collectFromBlock(SE, Guards, Block: Header, Pred, VisitedBlocks);
15578 return Guards;
15579}
15580
15581void ScalarEvolution::LoopGuards::collectFromPHI(
15582 ScalarEvolution &SE, ScalarEvolution::LoopGuards &Guards,
15583 const PHINode &Phi, SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks,
15584 SmallDenseMap<const BasicBlock *, LoopGuards> &IncomingGuards,
15585 unsigned Depth) {
15586 if (!SE.isSCEVable(Ty: Phi.getType()))
15587 return;
15588
15589 using MinMaxPattern = std::pair<const SCEVConstant *, SCEVTypes>;
15590 auto GetMinMaxConst = [&](unsigned IncomingIdx) -> MinMaxPattern {
15591 const BasicBlock *InBlock = Phi.getIncomingBlock(i: IncomingIdx);
15592 if (!VisitedBlocks.insert(Ptr: InBlock).second)
15593 return {nullptr, scCouldNotCompute};
15594
15595 // Avoid analyzing unreachable blocks so that we don't get trapped
15596 // traversing cycles with ill-formed dominance or infinite cycles
15597 if (!SE.DT.isReachableFromEntry(A: InBlock))
15598 return {nullptr, scCouldNotCompute};
15599
15600 auto [G, Inserted] = IncomingGuards.try_emplace(Key: InBlock, Args: LoopGuards(SE));
15601 if (Inserted)
15602 collectFromBlock(SE, Guards&: G->second, Block: Phi.getParent(), Pred: InBlock, VisitedBlocks,
15603 Depth: Depth + 1);
15604 auto &RewriteMap = G->second.RewriteMap;
15605 if (RewriteMap.empty())
15606 return {nullptr, scCouldNotCompute};
15607 auto S = RewriteMap.find(Val: SE.getSCEV(V: Phi.getIncomingValue(i: IncomingIdx)));
15608 if (S == RewriteMap.end())
15609 return {nullptr, scCouldNotCompute};
15610 auto *SM = dyn_cast_if_present<SCEVMinMaxExpr>(Val: S->second);
15611 if (!SM)
15612 return {nullptr, scCouldNotCompute};
15613 if (const SCEVConstant *C0 = dyn_cast<SCEVConstant>(Val: SM->getOperand(i: 0)))
15614 return {C0, SM->getSCEVType()};
15615 return {nullptr, scCouldNotCompute};
15616 };
15617 auto MergeMinMaxConst = [](MinMaxPattern P1,
15618 MinMaxPattern P2) -> MinMaxPattern {
15619 auto [C1, T1] = P1;
15620 auto [C2, T2] = P2;
15621 if (!C1 || !C2 || T1 != T2)
15622 return {nullptr, scCouldNotCompute};
15623 switch (T1) {
15624 case scUMaxExpr:
15625 return {C1->getAPInt().ult(RHS: C2->getAPInt()) ? C1 : C2, T1};
15626 case scSMaxExpr:
15627 return {C1->getAPInt().slt(RHS: C2->getAPInt()) ? C1 : C2, T1};
15628 case scUMinExpr:
15629 return {C1->getAPInt().ugt(RHS: C2->getAPInt()) ? C1 : C2, T1};
15630 case scSMinExpr:
15631 return {C1->getAPInt().sgt(RHS: C2->getAPInt()) ? C1 : C2, T1};
15632 default:
15633 llvm_unreachable("Trying to merge non-MinMaxExpr SCEVs.");
15634 }
15635 };
15636 auto P = GetMinMaxConst(0);
15637 for (unsigned int In = 1; In < Phi.getNumIncomingValues(); In++) {
15638 if (!P.first)
15639 break;
15640 P = MergeMinMaxConst(P, GetMinMaxConst(In));
15641 }
15642 if (P.first) {
15643 const SCEV *LHS = SE.getSCEV(V: const_cast<PHINode *>(&Phi));
15644 SmallVector<SCEVUse, 2> Ops({P.first, LHS});
15645 const SCEV *RHS = SE.getMinMaxExpr(Kind: P.second, Ops);
15646 Guards.RewriteMap.insert(KV: {LHS, RHS});
15647 }
15648}
15649
15650// Return a new SCEV that modifies \p Expr to the closest number divides by
15651// \p Divisor and less or equal than Expr. For now, only handle constant
15652// Expr.
15653static const SCEV *getPreviousSCEVDivisibleByDivisor(const SCEV *Expr,
15654 const APInt &DivisorVal,
15655 ScalarEvolution &SE) {
15656 const APInt *ExprVal;
15657 if (!match(S: Expr, P: m_scev_APInt(C&: ExprVal)) || ExprVal->isNegative() ||
15658 DivisorVal.isNonPositive())
15659 return Expr;
15660 APInt Rem = ExprVal->urem(RHS: DivisorVal);
15661 // return the SCEV: Expr - Expr % Divisor
15662 return SE.getConstant(Val: *ExprVal - Rem);
15663}
15664
15665// Return a new SCEV that modifies \p Expr to the closest number divides by
15666// \p Divisor and greater or equal than Expr. For now, only handle constant
15667// Expr.
15668static const SCEV *getNextSCEVDivisibleByDivisor(const SCEV *Expr,
15669 const APInt &DivisorVal,
15670 ScalarEvolution &SE) {
15671 const APInt *ExprVal;
15672 if (!match(S: Expr, P: m_scev_APInt(C&: ExprVal)) || ExprVal->isNegative() ||
15673 DivisorVal.isNonPositive())
15674 return Expr;
15675 APInt Rem = ExprVal->urem(RHS: DivisorVal);
15676 if (Rem.isZero())
15677 return Expr;
15678 // return the SCEV: Expr + Divisor - Expr % Divisor
15679 return SE.getConstant(Val: *ExprVal + DivisorVal - Rem);
15680}
15681
15682static bool collectDivisibilityInformation(
15683 ICmpInst::Predicate Predicate, const SCEV *LHS, const SCEV *RHS,
15684 DenseMap<const SCEV *, const SCEV *> &DivInfo,
15685 DenseMap<const SCEV *, APInt> &Multiples, ScalarEvolution &SE) {
15686 // If we have LHS == 0, check if LHS is computing a property of some unknown
15687 // SCEV %v which we can rewrite %v to express explicitly.
15688 if (Predicate != CmpInst::ICMP_EQ || !match(S: RHS, P: m_scev_Zero()))
15689 return false;
15690 // If LHS is A % B, i.e. A % B == 0, rewrite A to (A /u B) * B to
15691 // explicitly express that.
15692 const SCEVUnknown *URemLHS = nullptr;
15693 const SCEV *URemRHS = nullptr;
15694 if (!match(S: LHS, P: m_scev_URem(LHS: m_SCEVUnknown(V&: URemLHS), RHS: m_SCEV(V&: URemRHS), SE)))
15695 return false;
15696
15697 const SCEV *Multiple =
15698 SE.getMulExpr(LHS: SE.getUDivExpr(LHS: URemLHS, RHS: URemRHS), RHS: URemRHS);
15699 DivInfo[URemLHS] = Multiple;
15700 if (auto *C = dyn_cast<SCEVConstant>(Val: URemRHS))
15701 Multiples[URemLHS] = C->getAPInt();
15702 return true;
15703}
15704
15705// Check if the condition is a divisibility guard (A % B == 0).
15706static bool isDivisibilityGuard(const SCEV *LHS, const SCEV *RHS,
15707 ScalarEvolution &SE) {
15708 const SCEV *X, *Y;
15709 return match(S: LHS, P: m_scev_URem(LHS: m_SCEV(V&: X), RHS: m_SCEV(V&: Y), SE)) && RHS->isZero();
15710}
15711
15712// Apply divisibility by \p Divisor on MinMaxExpr with constant values,
15713// recursively. This is done by aligning up/down the constant value to the
15714// Divisor.
15715static const SCEV *applyDivisibilityOnMinMaxExpr(const SCEV *MinMaxExpr,
15716 APInt Divisor,
15717 ScalarEvolution &SE) {
15718 // Return true if \p Expr is a MinMax SCEV expression with a non-negative
15719 // constant operand. If so, return in \p SCTy the SCEV type and in \p RHS
15720 // the non-constant operand and in \p LHS the constant operand.
15721 auto IsMinMaxSCEVWithNonNegativeConstant =
15722 [&](const SCEV *Expr, SCEVTypes &SCTy, const SCEV *&LHS,
15723 const SCEV *&RHS) {
15724 if (auto *MinMax = dyn_cast<SCEVMinMaxExpr>(Val: Expr)) {
15725 if (MinMax->getNumOperands() != 2)
15726 return false;
15727 if (auto *C = dyn_cast<SCEVConstant>(Val: MinMax->getOperand(i: 0))) {
15728 if (C->getAPInt().isNegative())
15729 return false;
15730 SCTy = MinMax->getSCEVType();
15731 LHS = MinMax->getOperand(i: 0);
15732 RHS = MinMax->getOperand(i: 1);
15733 return true;
15734 }
15735 }
15736 return false;
15737 };
15738
15739 const SCEV *MinMaxLHS = nullptr, *MinMaxRHS = nullptr;
15740 SCEVTypes SCTy;
15741 if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS,
15742 MinMaxRHS))
15743 return MinMaxExpr;
15744 auto IsMin = isa<SCEVSMinExpr>(Val: MinMaxExpr) || isa<SCEVUMinExpr>(Val: MinMaxExpr);
15745 assert(SE.isKnownNonNegative(MinMaxLHS) && "Expected non-negative operand!");
15746 auto *DivisibleExpr =
15747 IsMin ? getPreviousSCEVDivisibleByDivisor(Expr: MinMaxLHS, DivisorVal: Divisor, SE)
15748 : getNextSCEVDivisibleByDivisor(Expr: MinMaxLHS, DivisorVal: Divisor, SE);
15749 SmallVector<SCEVUse> Ops = {
15750 applyDivisibilityOnMinMaxExpr(MinMaxExpr: MinMaxRHS, Divisor, SE), DivisibleExpr};
15751 return SE.getMinMaxExpr(Kind: SCTy, Ops);
15752}
15753
15754void ScalarEvolution::LoopGuards::collectFromBlock(
15755 ScalarEvolution &SE, ScalarEvolution::LoopGuards &Guards,
15756 const BasicBlock *Block, const BasicBlock *Pred,
15757 SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks, unsigned Depth) {
15758
15759 assert(SE.DT.isReachableFromEntry(Block) && SE.DT.isReachableFromEntry(Pred));
15760
15761 SmallVector<SCEVUse> ExprsToRewrite;
15762 auto CollectCondition = [&](ICmpInst::Predicate Predicate, const SCEV *LHS,
15763 const SCEV *RHS,
15764 DenseMap<const SCEV *, const SCEV *> &RewriteMap,
15765 const LoopGuards &DivGuards) {
15766 // WARNING: It is generally unsound to apply any wrap flags to the proposed
15767 // replacement SCEV which isn't directly implied by the structure of that
15768 // SCEV. In particular, using contextual facts to imply flags is *NOT*
15769 // legal. See the scoping rules for flags in the header to understand why.
15770
15771 // Check for a condition of the form (-C1 + X < C2). InstCombine will
15772 // create this form when combining two checks of the form (X u< C2 + C1) and
15773 // (X >=u C1).
15774 auto MatchRangeCheckIdiom = [&SE, Predicate, LHS, RHS, &RewriteMap,
15775 &ExprsToRewrite]() {
15776 const SCEVConstant *C1;
15777 const SCEVUnknown *LHSUnknown;
15778 auto *C2 = dyn_cast<SCEVConstant>(Val: RHS);
15779 if (!match(S: LHS,
15780 P: m_scev_Add(Op0: m_SCEVConstant(V&: C1), Op1: m_SCEVUnknown(V&: LHSUnknown))) ||
15781 !C2)
15782 return false;
15783
15784 auto ExactRegion =
15785 ConstantRange::makeExactICmpRegion(Pred: Predicate, Other: C2->getAPInt())
15786 .sub(Other: C1->getAPInt());
15787
15788 // Bail out, unless we have a non-wrapping, monotonic range.
15789 if (ExactRegion.isWrappedSet() || ExactRegion.isFullSet())
15790 return false;
15791 auto [I, Inserted] = RewriteMap.try_emplace(Key: LHSUnknown);
15792 const SCEV *RewrittenLHS = Inserted ? LHSUnknown : I->second;
15793 I->second = SE.getUMaxExpr(
15794 LHS: SE.getConstant(Val: ExactRegion.getUnsignedMin()),
15795 RHS: SE.getUMinExpr(LHS: RewrittenLHS,
15796 RHS: SE.getConstant(Val: ExactRegion.getUnsignedMax())));
15797 ExprsToRewrite.push_back(Elt: LHSUnknown);
15798 return true;
15799 };
15800 if (MatchRangeCheckIdiom())
15801 return;
15802
15803 // Do not apply information for constants or if RHS contains an AddRec.
15804 if (isa<SCEVConstant>(Val: LHS) || SE.containsAddRecurrence(S: RHS))
15805 return;
15806
15807 // If RHS is SCEVUnknown, make sure the information is applied to it.
15808 if (!isa<SCEVUnknown>(Val: LHS) && isa<SCEVUnknown>(Val: RHS)) {
15809 std::swap(a&: LHS, b&: RHS);
15810 Predicate = CmpInst::getSwappedPredicate(pred: Predicate);
15811 }
15812
15813 // Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
15814 // and \p FromRewritten are the same (i.e. there has been no rewrite
15815 // registered for \p From), then puts this value in the list of rewritten
15816 // expressions.
15817 auto AddRewrite = [&](const SCEV *From, const SCEV *FromRewritten,
15818 const SCEV *To) {
15819 if (From == FromRewritten)
15820 ExprsToRewrite.push_back(Elt: From);
15821 RewriteMap[From] = To;
15822 };
15823
15824 // Checks whether \p S has already been rewritten. In that case returns the
15825 // existing rewrite because we want to chain further rewrites onto the
15826 // already rewritten value. Otherwise returns \p S.
15827 auto GetMaybeRewritten = [&](const SCEV *S) {
15828 return RewriteMap.lookup_or(Val: S, Default&: S);
15829 };
15830
15831 const SCEV *RewrittenLHS = GetMaybeRewritten(LHS);
15832 // Apply divisibility information when computing the constant multiple.
15833 const APInt &DividesBy =
15834 SE.getConstantMultiple(S: DivGuards.rewrite(Expr: RewrittenLHS));
15835
15836 // Collect rewrites for LHS and its transitive operands based on the
15837 // condition.
15838 // For min/max expressions, also apply the guard to its operands:
15839 // 'min(a, b) >= c' -> '(a >= c) and (b >= c)',
15840 // 'min(a, b) > c' -> '(a > c) and (b > c)',
15841 // 'max(a, b) <= c' -> '(a <= c) and (b <= c)',
15842 // 'max(a, b) < c' -> '(a < c) and (b < c)'.
15843
15844 // We cannot express strict predicates in SCEV, so instead we replace them
15845 // with non-strict ones against plus or minus one of RHS depending on the
15846 // predicate.
15847 const SCEV *One = SE.getOne(Ty: RHS->getType());
15848 switch (Predicate) {
15849 case CmpInst::ICMP_ULT:
15850 if (RHS->getType()->isPointerTy())
15851 return;
15852 RHS = SE.getUMaxExpr(LHS: RHS, RHS: One);
15853 [[fallthrough]];
15854 case CmpInst::ICMP_SLT: {
15855 RHS = SE.getMinusSCEV(LHS: RHS, RHS: One);
15856 RHS = getPreviousSCEVDivisibleByDivisor(Expr: RHS, DivisorVal: DividesBy, SE);
15857 break;
15858 }
15859 case CmpInst::ICMP_UGT:
15860 case CmpInst::ICMP_SGT:
15861 RHS = SE.getAddExpr(LHS: RHS, RHS: One);
15862 RHS = getNextSCEVDivisibleByDivisor(Expr: RHS, DivisorVal: DividesBy, SE);
15863 break;
15864 case CmpInst::ICMP_ULE:
15865 case CmpInst::ICMP_SLE:
15866 RHS = getPreviousSCEVDivisibleByDivisor(Expr: RHS, DivisorVal: DividesBy, SE);
15867 break;
15868 case CmpInst::ICMP_UGE:
15869 case CmpInst::ICMP_SGE:
15870 RHS = getNextSCEVDivisibleByDivisor(Expr: RHS, DivisorVal: DividesBy, SE);
15871 break;
15872 default:
15873 break;
15874 }
15875
15876 SmallVector<SCEVUse, 16> Worklist(1, LHS);
15877 SmallPtrSet<const SCEV *, 16> Visited;
15878
15879 auto EnqueueOperands = [&Worklist](const SCEVNAryExpr *S) {
15880 append_range(C&: Worklist, R: S->operands());
15881 };
15882
15883 while (!Worklist.empty()) {
15884 const SCEV *From = Worklist.pop_back_val();
15885 if (isa<SCEVConstant>(Val: From))
15886 continue;
15887 if (!Visited.insert(Ptr: From).second)
15888 continue;
15889 const SCEV *FromRewritten = GetMaybeRewritten(From);
15890 const SCEV *To = nullptr;
15891
15892 switch (Predicate) {
15893 case CmpInst::ICMP_ULT:
15894 case CmpInst::ICMP_ULE:
15895 To = SE.getUMinExpr(LHS: FromRewritten, RHS);
15896 if (auto *UMax = dyn_cast<SCEVUMaxExpr>(Val: FromRewritten))
15897 EnqueueOperands(UMax);
15898 break;
15899 case CmpInst::ICMP_SLT:
15900 case CmpInst::ICMP_SLE:
15901 To = SE.getSMinExpr(LHS: FromRewritten, RHS);
15902 if (auto *SMax = dyn_cast<SCEVSMaxExpr>(Val: FromRewritten))
15903 EnqueueOperands(SMax);
15904 break;
15905 case CmpInst::ICMP_UGT:
15906 case CmpInst::ICMP_UGE:
15907 To = SE.getUMaxExpr(LHS: FromRewritten, RHS);
15908 if (auto *UMin = dyn_cast<SCEVUMinExpr>(Val: FromRewritten))
15909 EnqueueOperands(UMin);
15910 break;
15911 case CmpInst::ICMP_SGT:
15912 case CmpInst::ICMP_SGE:
15913 To = SE.getSMaxExpr(LHS: FromRewritten, RHS);
15914 if (auto *SMin = dyn_cast<SCEVSMinExpr>(Val: FromRewritten))
15915 EnqueueOperands(SMin);
15916 break;
15917 case CmpInst::ICMP_EQ:
15918 if (isa<SCEVConstant>(Val: RHS))
15919 To = RHS;
15920 break;
15921 case CmpInst::ICMP_NE:
15922 if (match(S: RHS, P: m_scev_Zero())) {
15923 const SCEV *OneAlignedUp =
15924 getNextSCEVDivisibleByDivisor(Expr: One, DivisorVal: DividesBy, SE);
15925 To = SE.getUMaxExpr(LHS: FromRewritten, RHS: OneAlignedUp);
15926 } else {
15927 // LHS != RHS can be rewritten as (LHS - RHS) = UMax(1, LHS - RHS),
15928 // but creating the subtraction eagerly is expensive. Track the
15929 // inequalities in a separate map, and materialize the rewrite lazily
15930 // when encountering a suitable subtraction while re-writing.
15931 if (LHS->getType()->isPointerTy()) {
15932 LHS = SE.getLosslessPtrToIntExpr(Op: LHS);
15933 RHS = SE.getLosslessPtrToIntExpr(Op: RHS);
15934 if (isa<SCEVCouldNotCompute>(Val: LHS) || isa<SCEVCouldNotCompute>(Val: RHS))
15935 break;
15936 }
15937 const SCEVConstant *C;
15938 const SCEV *A, *B;
15939 if (match(S: RHS, P: m_scev_Add(Op0: m_SCEVConstant(V&: C), Op1: m_SCEV(V&: A))) &&
15940 match(S: LHS, P: m_scev_Add(Op0: m_scev_Specific(S: C), Op1: m_SCEV(V&: B)))) {
15941 RHS = A;
15942 LHS = B;
15943 }
15944 if (LHS > RHS)
15945 std::swap(a&: LHS, b&: RHS);
15946 Guards.NotEqual.insert(V: {LHS, RHS});
15947 continue;
15948 }
15949 break;
15950 default:
15951 break;
15952 }
15953
15954 if (To)
15955 AddRewrite(From, FromRewritten, To);
15956 }
15957 };
15958
15959 SmallVector<PointerIntPair<Value *, 1, bool>> Terms;
15960 // First, collect information from assumptions dominating the loop.
15961 for (auto &AssumeVH : SE.AC.assumptions()) {
15962 if (!AssumeVH)
15963 continue;
15964 auto *AssumeI = cast<CallInst>(Val&: AssumeVH);
15965 if (!SE.DT.dominates(Def: AssumeI, BB: Block))
15966 continue;
15967 Terms.emplace_back(Args: AssumeI->getOperand(i_nocapture: 0), Args: true);
15968 }
15969
15970 // Second, collect information from llvm.experimental.guards dominating the loop.
15971 auto *GuardDecl = Intrinsic::getDeclarationIfExists(
15972 M: SE.F.getParent(), id: Intrinsic::experimental_guard);
15973 if (GuardDecl)
15974 for (const auto *GU : GuardDecl->users())
15975 if (const auto *Guard = dyn_cast<IntrinsicInst>(Val: GU))
15976 if (Guard->getFunction() == Block->getParent() &&
15977 SE.DT.dominates(Def: Guard, BB: Block))
15978 Terms.emplace_back(Args: Guard->getArgOperand(i: 0), Args: true);
15979
15980 // Third, collect conditions from dominating branches. Starting at the loop
15981 // predecessor, climb up the predecessor chain, as long as there are
15982 // predecessors that can be found that have unique successors leading to the
15983 // original header.
15984 // TODO: share this logic with isLoopEntryGuardedByCond.
15985 unsigned NumCollectedConditions = 0;
15986 VisitedBlocks.insert(Ptr: Block);
15987 std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
15988 for (; Pair.first;
15989 Pair = SE.getPredecessorWithUniqueSuccessorForBB(BB: Pair.first)) {
15990 VisitedBlocks.insert(Ptr: Pair.second);
15991 const CondBrInst *LoopEntryPredicate =
15992 dyn_cast<CondBrInst>(Val: Pair.first->getTerminator());
15993 if (!LoopEntryPredicate)
15994 continue;
15995
15996 Terms.emplace_back(Args: LoopEntryPredicate->getCondition(),
15997 Args: LoopEntryPredicate->getSuccessor(i: 0) == Pair.second);
15998 NumCollectedConditions++;
15999
16000 // If we are recursively collecting guards stop after 2
16001 // conditions to limit compile-time impact for now.
16002 if (Depth > 0 && NumCollectedConditions == 2)
16003 break;
16004 }
16005 // Finally, if we stopped climbing the predecessor chain because
16006 // there wasn't a unique one to continue, try to collect conditions
16007 // for PHINodes by recursively following all of their incoming
16008 // blocks and try to merge the found conditions to build a new one
16009 // for the Phi.
16010 if (Pair.second->hasNPredecessorsOrMore(N: 2) &&
16011 Depth < MaxLoopGuardCollectionDepth) {
16012 SmallDenseMap<const BasicBlock *, LoopGuards> IncomingGuards;
16013 for (auto &Phi : Pair.second->phis())
16014 collectFromPHI(SE, Guards, Phi, VisitedBlocks, IncomingGuards, Depth);
16015 }
16016
16017 // Now apply the information from the collected conditions to
16018 // Guards.RewriteMap. Conditions are processed in reverse order, so the
16019 // earliest conditions is processed first, except guards with divisibility
16020 // information, which are moved to the back. This ensures the SCEVs with the
16021 // shortest dependency chains are constructed first.
16022 SmallVector<std::tuple<CmpInst::Predicate, const SCEV *, const SCEV *>>
16023 GuardsToProcess;
16024 for (auto [Term, EnterIfTrue] : reverse(C&: Terms)) {
16025 SmallVector<Value *, 8> Worklist;
16026 SmallPtrSet<Value *, 8> Visited;
16027 Worklist.push_back(Elt: Term);
16028 while (!Worklist.empty()) {
16029 Value *Cond = Worklist.pop_back_val();
16030 if (!Visited.insert(Ptr: Cond).second)
16031 continue;
16032
16033 if (auto *Cmp = dyn_cast<ICmpInst>(Val: Cond)) {
16034 auto Predicate =
16035 EnterIfTrue ? Cmp->getPredicate() : Cmp->getInversePredicate();
16036 const auto *LHS = SE.getSCEV(V: Cmp->getOperand(i_nocapture: 0));
16037 const auto *RHS = SE.getSCEV(V: Cmp->getOperand(i_nocapture: 1));
16038 // If LHS is a constant, apply information to the other expression.
16039 // TODO: If LHS is not a constant, check if using CompareSCEVComplexity
16040 // can improve results.
16041 if (isa<SCEVConstant>(Val: LHS)) {
16042 std::swap(a&: LHS, b&: RHS);
16043 Predicate = CmpInst::getSwappedPredicate(pred: Predicate);
16044 }
16045 GuardsToProcess.emplace_back(Args&: Predicate, Args&: LHS, Args&: RHS);
16046 continue;
16047 }
16048
16049 Value *L, *R;
16050 if (EnterIfTrue ? match(V: Cond, P: m_LogicalAnd(L: m_Value(V&: L), R: m_Value(V&: R)))
16051 : match(V: Cond, P: m_LogicalOr(L: m_Value(V&: L), R: m_Value(V&: R)))) {
16052 Worklist.push_back(Elt: L);
16053 Worklist.push_back(Elt: R);
16054 }
16055 }
16056 }
16057
16058 // Process divisibility guards in reverse order to populate DivGuards early.
16059 DenseMap<const SCEV *, APInt> Multiples;
16060 LoopGuards DivGuards(SE);
16061 for (const auto &[Predicate, LHS, RHS] : GuardsToProcess) {
16062 if (!isDivisibilityGuard(LHS, RHS, SE))
16063 continue;
16064 collectDivisibilityInformation(Predicate, LHS, RHS, DivInfo&: DivGuards.RewriteMap,
16065 Multiples, SE);
16066 }
16067
16068 for (const auto &[Predicate, LHS, RHS] : GuardsToProcess)
16069 CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap, DivGuards);
16070
16071 // Apply divisibility information last. This ensures it is applied to the
16072 // outermost expression after other rewrites for the given value.
16073 for (const auto &[K, Divisor] : Multiples) {
16074 const SCEV *DivisorSCEV = SE.getConstant(Val: Divisor);
16075 Guards.RewriteMap[K] =
16076 SE.getMulExpr(LHS: SE.getUDivExpr(LHS: applyDivisibilityOnMinMaxExpr(
16077 MinMaxExpr: Guards.rewrite(Expr: K), Divisor, SE),
16078 RHS: DivisorSCEV),
16079 RHS: DivisorSCEV);
16080 ExprsToRewrite.push_back(Elt: K);
16081 }
16082
16083 // Let the rewriter preserve NUW/NSW flags if the unsigned/signed ranges of
16084 // the replacement expressions are contained in the ranges of the replaced
16085 // expressions.
16086 Guards.PreserveNUW = true;
16087 Guards.PreserveNSW = true;
16088 for (const SCEV *Expr : ExprsToRewrite) {
16089 const SCEV *RewriteTo = Guards.RewriteMap[Expr];
16090 Guards.PreserveNUW &=
16091 SE.getUnsignedRange(S: Expr).contains(CR: SE.getUnsignedRange(S: RewriteTo));
16092 Guards.PreserveNSW &=
16093 SE.getSignedRange(S: Expr).contains(CR: SE.getSignedRange(S: RewriteTo));
16094 }
16095
16096 // Now that all rewrite information is collect, rewrite the collected
16097 // expressions with the information in the map. This applies information to
16098 // sub-expressions.
16099 if (ExprsToRewrite.size() > 1) {
16100 for (const SCEV *Expr : ExprsToRewrite) {
16101 const SCEV *RewriteTo = Guards.RewriteMap[Expr];
16102 Guards.RewriteMap.erase(Val: Expr);
16103 Guards.RewriteMap.insert(KV: {Expr, Guards.rewrite(Expr: RewriteTo)});
16104 }
16105 }
16106}
16107
16108const SCEV *ScalarEvolution::LoopGuards::rewrite(const SCEV *Expr) const {
16109 /// A rewriter to replace SCEV expressions in Map with the corresponding entry
16110 /// in the map. It skips AddRecExpr because we cannot guarantee that the
16111 /// replacement is loop invariant in the loop of the AddRec.
16112 class SCEVLoopGuardRewriter
16113 : public SCEVRewriteVisitor<SCEVLoopGuardRewriter> {
16114 const DenseMap<const SCEV *, const SCEV *> &Map;
16115 const SmallDenseSet<std::pair<const SCEV *, const SCEV *>> &NotEqual;
16116
16117 SCEV::NoWrapFlags FlagMask = SCEV::FlagAnyWrap;
16118
16119 public:
16120 SCEVLoopGuardRewriter(ScalarEvolution &SE,
16121 const ScalarEvolution::LoopGuards &Guards)
16122 : SCEVRewriteVisitor(SE), Map(Guards.RewriteMap),
16123 NotEqual(Guards.NotEqual) {
16124 if (Guards.PreserveNUW)
16125 FlagMask = ScalarEvolution::setFlags(Flags: FlagMask, OnFlags: SCEV::FlagNUW);
16126 if (Guards.PreserveNSW)
16127 FlagMask = ScalarEvolution::setFlags(Flags: FlagMask, OnFlags: SCEV::FlagNSW);
16128 }
16129
16130 const SCEV *visitAddRecExpr(const SCEVAddRecExpr *Expr) { return Expr; }
16131
16132 const SCEV *visitUnknown(const SCEVUnknown *Expr) {
16133 return Map.lookup_or(Val: Expr, Default&: Expr);
16134 }
16135
16136 const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr) {
16137 if (const SCEV *S = Map.lookup(Val: Expr))
16138 return S;
16139
16140 // If we didn't find the extact ZExt expr in the map, check if there's
16141 // an entry for a smaller ZExt we can use instead.
16142 Type *Ty = Expr->getType();
16143 const SCEV *Op = Expr->getOperand(i: 0);
16144 unsigned Bitwidth = Ty->getScalarSizeInBits() / 2;
16145 while (Bitwidth % 8 == 0 && Bitwidth >= 8 &&
16146 Bitwidth > Op->getType()->getScalarSizeInBits()) {
16147 Type *NarrowTy = IntegerType::get(C&: SE.getContext(), NumBits: Bitwidth);
16148 auto *NarrowExt = SE.getZeroExtendExpr(Op, Ty: NarrowTy);
16149 if (const SCEV *S = Map.lookup(Val: NarrowExt))
16150 return SE.getZeroExtendExpr(Op: S, Ty);
16151 Bitwidth = Bitwidth / 2;
16152 }
16153
16154 return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitZeroExtendExpr(
16155 Expr);
16156 }
16157
16158 const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *Expr) {
16159 if (const SCEV *S = Map.lookup(Val: Expr))
16160 return S;
16161 return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSignExtendExpr(
16162 Expr);
16163 }
16164
16165 const SCEV *visitUMinExpr(const SCEVUMinExpr *Expr) {
16166 if (const SCEV *S = Map.lookup(Val: Expr))
16167 return S;
16168 return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitUMinExpr(Expr);
16169 }
16170
16171 const SCEV *visitSMinExpr(const SCEVSMinExpr *Expr) {
16172 if (const SCEV *S = Map.lookup(Val: Expr))
16173 return S;
16174 return SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSMinExpr(Expr);
16175 }
16176
16177 const SCEV *visitAddExpr(const SCEVAddExpr *Expr) {
16178 // Helper to check if S is a subtraction (A - B) where A != B, and if so,
16179 // return UMax(S, 1).
16180 auto RewriteSubtraction = [&](const SCEV *S) -> const SCEV * {
16181 const SCEV *LHS, *RHS;
16182 if (MatchBinarySub(S, LHS, RHS)) {
16183 if (LHS > RHS)
16184 std::swap(a&: LHS, b&: RHS);
16185 if (NotEqual.contains(V: {LHS, RHS})) {
16186 const SCEV *OneAlignedUp = getNextSCEVDivisibleByDivisor(
16187 Expr: SE.getOne(Ty: S->getType()), DivisorVal: SE.getConstantMultiple(S), SE);
16188 return SE.getUMaxExpr(LHS: OneAlignedUp, RHS: S);
16189 }
16190 }
16191 return nullptr;
16192 };
16193
16194 // Check if Expr itself is a subtraction pattern with guard info.
16195 if (const SCEV *Rewritten = RewriteSubtraction(Expr))
16196 return Rewritten;
16197
16198 // Trip count expressions sometimes consist of adding 3 operands, i.e.
16199 // (Const + A + B). There may be guard info for A + B, and if so, apply
16200 // it.
16201 // TODO: Could more generally apply guards to Add sub-expressions.
16202 if (isa<SCEVConstant>(Val: Expr->getOperand(i: 0)) &&
16203 Expr->getNumOperands() == 3) {
16204 const SCEV *Add =
16205 SE.getAddExpr(LHS: Expr->getOperand(i: 1), RHS: Expr->getOperand(i: 2));
16206 if (const SCEV *Rewritten = RewriteSubtraction(Add))
16207 return SE.getAddExpr(
16208 LHS: Expr->getOperand(i: 0), RHS: Rewritten,
16209 Flags: ScalarEvolution::maskFlags(Flags: Expr->getNoWrapFlags(), Mask: FlagMask));
16210 if (const SCEV *S = Map.lookup(Val: Add))
16211 return SE.getAddExpr(LHS: Expr->getOperand(i: 0), RHS: S);
16212 }
16213 SmallVector<SCEVUse, 2> Operands;
16214 bool Changed = false;
16215 for (SCEVUse Op : Expr->operands()) {
16216 Operands.push_back(
16217 Elt: SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visit(S: Op));
16218 Changed |= Op != Operands.back();
16219 }
16220 // We are only replacing operands with equivalent values, so transfer the
16221 // flags from the original expression.
16222 return !Changed ? Expr
16223 : SE.getAddExpr(Ops&: Operands,
16224 OrigFlags: ScalarEvolution::maskFlags(
16225 Flags: Expr->getNoWrapFlags(), Mask: FlagMask));
16226 }
16227
16228 const SCEV *visitMulExpr(const SCEVMulExpr *Expr) {
16229 SmallVector<SCEVUse, 2> Operands;
16230 bool Changed = false;
16231 for (SCEVUse Op : Expr->operands()) {
16232 Operands.push_back(
16233 Elt: SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visit(S: Op));
16234 Changed |= Op != Operands.back();
16235 }
16236 // We are only replacing operands with equivalent values, so transfer the
16237 // flags from the original expression.
16238 return !Changed ? Expr
16239 : SE.getMulExpr(Ops&: Operands,
16240 OrigFlags: ScalarEvolution::maskFlags(
16241 Flags: Expr->getNoWrapFlags(), Mask: FlagMask));
16242 }
16243 };
16244
16245 if (RewriteMap.empty() && NotEqual.empty())
16246 return Expr;
16247
16248 SCEVLoopGuardRewriter Rewriter(SE, *this);
16249 return Rewriter.visit(S: Expr);
16250}
16251
16252const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr, const Loop *L) {
16253 return applyLoopGuards(Expr, Guards: LoopGuards::collect(L, SE&: *this));
16254}
16255
16256const SCEV *ScalarEvolution::applyLoopGuards(const SCEV *Expr,
16257 const LoopGuards &Guards) {
16258 return Guards.rewrite(Expr);
16259}
16260