Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
ScalarEvolution.cpp
Go to the documentation of this file.
1//===- ScalarEvolution.cpp - Scalar Evolution Analysis --------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation of the scalar evolution analysis
10// engine, which is used primarily to analyze expressions involving induction
11// variables in loops.
12//
13// There are several aspects to this library. First is the representation of
14// scalar expressions, which are represented as subclasses of the SCEV class.
15// These classes are used to represent certain types of subexpressions that we
16// can handle. We only create one SCEV of a particular shape, so
17// pointer-comparisons for equality are legal.
18//
19// One important aspect of the SCEV objects is that they are never cyclic, even
20// if there is a cycle in the dataflow for an expression (ie, a PHI node). If
21// the PHI node is one of the idioms that we can represent (e.g., a polynomial
22// recurrence) then we represent it directly as a recurrence node, otherwise we
23// represent it as a SCEVUnknown node.
24//
25// In addition to being able to represent expressions of various types, we also
26// have folders that are used to build the *canonical* representation for a
27// particular expression. These folders are capable of using a variety of
28// rewrite rules to simplify the expressions.
29//
30// Once the folders are defined, we can implement the more interesting
31// higher-level code, such as the code that recognizes PHI nodes of various
32// types, computes the execution count of a loop, etc.
33//
34// TODO: We should use these routines and value representations to implement
35// dependence analysis!
36//
37//===----------------------------------------------------------------------===//
38//
39// There are several good references for the techniques used in this analysis.
40//
41// Chains of recurrences -- a method to expedite the evaluation
42// of closed-form functions
43// Olaf Bachmann, Paul S. Wang, Eugene V. Zima
44//
45// On computational properties of chains of recurrences
46// Eugene V. Zima
47//
48// Symbolic Evaluation of Chains of Recurrences for Loop Optimization
49// Robert A. van Engelen
50//
51// Efficient Symbolic Analysis for Optimizing Compilers
52// Robert A. van Engelen
53//
54// Using the chains of recurrences algebra for data dependence testing and
55// induction variable substitution
56// MS Thesis, Johnie Birch
57//
58//===----------------------------------------------------------------------===//
59
60#include "llvm/Analysis/ScalarEvolution.h"
61#include "llvm/ADT/APInt.h"
62#include "llvm/ADT/ArrayRef.h"
63#include "llvm/ADT/DenseMap.h"
64#include "llvm/ADT/DepthFirstIterator.h"
65#include "llvm/ADT/EquivalenceClasses.h"
66#include "llvm/ADT/FoldingSet.h"
67#include "llvm/ADT/STLExtras.h"
68#include "llvm/ADT/ScopeExit.h"
69#include "llvm/ADT/Sequence.h"
70#include "llvm/ADT/SmallPtrSet.h"
71#include "llvm/ADT/SmallSet.h"
72#include "llvm/ADT/SmallVector.h"
73#include "llvm/ADT/Statistic.h"
74#include "llvm/ADT/StringExtras.h"
75#include "llvm/ADT/StringRef.h"
76#include "llvm/Analysis/AssumptionCache.h"
77#include "llvm/Analysis/ConstantFolding.h"
78#include "llvm/Analysis/InstructionSimplify.h"
79#include "llvm/Analysis/LoopInfo.h"
80#include "llvm/Analysis/MemoryBuiltins.h"
81#include "llvm/Analysis/ScalarEvolutionExpressions.h"
82#include "llvm/Analysis/ScalarEvolutionPatternMatch.h"
83#include "llvm/Analysis/TargetLibraryInfo.h"
84#include "llvm/Analysis/ValueTracking.h"
85#include "llvm/Config/llvm-config.h"
86#include "llvm/IR/Argument.h"
87#include "llvm/IR/BasicBlock.h"
88#include "llvm/IR/CFG.h"
89#include "llvm/IR/Constant.h"
90#include "llvm/IR/ConstantRange.h"
91#include "llvm/IR/Constants.h"
92#include "llvm/IR/DataLayout.h"
93#include "llvm/IR/DerivedTypes.h"
94#include "llvm/IR/Dominators.h"
95#include "llvm/IR/Function.h"
96#include "llvm/IR/GlobalAlias.h"
97#include "llvm/IR/GlobalValue.h"
98#include "llvm/IR/InstIterator.h"
99#include "llvm/IR/InstrTypes.h"
100#include "llvm/IR/Instruction.h"
101#include "llvm/IR/Instructions.h"
102#include "llvm/IR/IntrinsicInst.h"
103#include "llvm/IR/Intrinsics.h"
104#include "llvm/IR/LLVMContext.h"
105#include "llvm/IR/Operator.h"
106#include "llvm/IR/PatternMatch.h"
107#include "llvm/IR/Type.h"
108#include "llvm/IR/Use.h"
109#include "llvm/IR/User.h"
110#include "llvm/IR/Value.h"
111#include "llvm/IR/Verifier.h"
112#include "llvm/InitializePasses.h"
113#include "llvm/Pass.h"
114#include "llvm/Support/Casting.h"
115#include "llvm/Support/CommandLine.h"
116#include "llvm/Support/Compiler.h"
117#include "llvm/Support/Debug.h"
118#include "llvm/Support/ErrorHandling.h"
119#include "llvm/Support/KnownBits.h"
120#include "llvm/Support/SaveAndRestore.h"
121#include "llvm/Support/raw_ostream.h"
122#include <algorithm>
123#include <cassert>
124#include <climits>
125#include <cstdint>
126#include <cstdlib>
127#include <map>
128#include <memory>
129#include <numeric>
130#include <optional>
131#include <tuple>
132#include <utility>
133#include <vector>
134
135using namespacellvm;
136using namespacePatternMatch;
137using namespaceSCEVPatternMatch;
138
139#define DEBUG_TYPE "scalar-evolution"
140
141STATISTIC(NumExitCountsComputed,
142"Number of loop exits with predictable exit counts");
143STATISTIC(NumExitCountsNotComputed,
144"Number of loop exits without predictable exit counts");
145STATISTIC(NumBruteForceTripCountsComputed,
146"Number of loops with trip counts computed by force");
147
148#ifdef EXPENSIVE_CHECKS
149boolllvm::VerifySCEV =true;
150#else
151boolllvm::VerifySCEV =false;
152#endif
153
154staticcl::opt<unsigned>
155MaxBruteForceIterations("scalar-evolution-max-iterations",cl::ReallyHidden,
156cl::desc("Maximum number of iterations SCEV will "
157"symbolically execute a constant "
158"derived loop"),
159cl::init(100));
160
161staticcl::opt<bool, true>VerifySCEVOpt(
162"verify-scev",cl::Hidden,cl::location(VerifySCEV),
163cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"));
164staticcl::opt<bool>VerifySCEVStrict(
165"verify-scev-strict",cl::Hidden,
166cl::desc("Enable stricter verification with -verify-scev is passed"));
167
168staticcl::opt<bool>VerifyIR(
169"scev-verify-ir",cl::Hidden,
170cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"),
171cl::init(false));
172
173staticcl::opt<unsigned>MulOpsInlineThreshold(
174"scev-mulops-inline-threshold",cl::Hidden,
175cl::desc("Threshold for inlining multiplication operands into a SCEV"),
176cl::init(32));
177
178staticcl::opt<unsigned>AddOpsInlineThreshold(
179"scev-addops-inline-threshold",cl::Hidden,
180cl::desc("Threshold for inlining addition operands into a SCEV"),
181cl::init(500));
182
183staticcl::opt<unsigned>MaxSCEVCompareDepth(
184"scalar-evolution-max-scev-compare-depth",cl::Hidden,
185cl::desc("Maximum depth of recursive SCEV complexity comparisons"),
186cl::init(32));
187
188staticcl::opt<unsigned>MaxSCEVOperationsImplicationDepth(
189"scalar-evolution-max-scev-operations-implication-depth",cl::Hidden,
190cl::desc("Maximum depth of recursive SCEV operations implication analysis"),
191cl::init(2));
192
193staticcl::opt<unsigned>MaxValueCompareDepth(
194"scalar-evolution-max-value-compare-depth",cl::Hidden,
195cl::desc("Maximum depth of recursive value complexity comparisons"),
196cl::init(2));
197
198staticcl::opt<unsigned>
199MaxArithDepth("scalar-evolution-max-arith-depth",cl::Hidden,
200cl::desc("Maximum depth of recursive arithmetics"),
201cl::init(32));
202
203staticcl::opt<unsigned>MaxConstantEvolvingDepth(
204"scalar-evolution-max-constant-evolving-depth",cl::Hidden,
205cl::desc("Maximum depth of recursive constant evolving"),cl::init(32));
206
207staticcl::opt<unsigned>
208MaxCastDepth("scalar-evolution-max-cast-depth",cl::Hidden,
209cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"),
210cl::init(8));
211
212staticcl::opt<unsigned>
213MaxAddRecSize("scalar-evolution-max-add-rec-size",cl::Hidden,
214cl::desc("Max coefficients in AddRec during evolving"),
215cl::init(8));
216
217staticcl::opt<unsigned>
218HugeExprThreshold("scalar-evolution-huge-expr-threshold",cl::Hidden,
219cl::desc("Size of the expression which is considered huge"),
220cl::init(4096));
221
222staticcl::opt<unsigned>RangeIterThreshold(
223"scev-range-iter-threshold",cl::Hidden,
224cl::desc("Threshold for switching to iteratively computing SCEV ranges"),
225cl::init(32));
226
227staticcl::opt<unsigned>MaxLoopGuardCollectionDepth(
228"scalar-evolution-max-loop-guard-collection-depth",cl::Hidden,
229cl::desc("Maximum depth for recursive loop guard collection"),cl::init(1));
230
231staticcl::opt<bool>
232ClassifyExpressions("scalar-evolution-classify-expressions",
233cl::Hidden,cl::init(true),
234cl::desc("When printing analysis, include information on every instruction"));
235
236staticcl::opt<bool>UseExpensiveRangeSharpening(
237"scalar-evolution-use-expensive-range-sharpening",cl::Hidden,
238cl::init(false),
239cl::desc("Use more powerful methods of sharpening expression ranges. May "
240"be costly in terms of compile time"));
241
242staticcl::opt<unsigned>MaxPhiSCCAnalysisSize(
243"scalar-evolution-max-scc-analysis-depth",cl::Hidden,
244cl::desc("Maximum amount of nodes to process while searching SCEVUnknown "
245"Phi strongly connected components"),
246cl::init(8));
247
248staticcl::opt<bool>
249EnableFiniteLoopControl("scalar-evolution-finite-loop",cl::Hidden,
250cl::desc("Handle <= and >= in finite loops"),
251cl::init(true));
252
253staticcl::opt<bool>UseContextForNoWrapFlagInference(
254"scalar-evolution-use-context-for-no-wrap-flag-strenghening",cl::Hidden,
255cl::desc("Infer nuw/nsw flags using context where suitable"),
256cl::init(true));
257
258//===----------------------------------------------------------------------===//
259// SCEV class definitions
260//===----------------------------------------------------------------------===//
261
262//===----------------------------------------------------------------------===//
263// Implementation of the SCEV class.
264//
265
266#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
267LLVM_DUMP_METHODvoidSCEV::dump() const{
268print(dbgs());
269dbgs() <<'\n';
270}
271#endif
272
273voidSCEV::print(raw_ostream &OS) const{
274switch (getSCEVType()) {
275casescConstant:
276 cast<SCEVConstant>(this)->getValue()->printAsOperand(OS,false);
277return;
278casescVScale:
279OS <<"vscale";
280return;
281casescPtrToInt: {
282constSCEVPtrToIntExpr *PtrToInt = cast<SCEVPtrToIntExpr>(this);
283constSCEV *Op = PtrToInt->getOperand();
284OS <<"(ptrtoint " << *Op->getType() <<" " << *Op <<" to "
285 << *PtrToInt->getType() <<")";
286return;
287 }
288casescTruncate: {
289constSCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(this);
290constSCEV *Op = Trunc->getOperand();
291OS <<"(trunc " << *Op->getType() <<" " << *Op <<" to "
292 << *Trunc->getType() <<")";
293return;
294 }
295casescZeroExtend: {
296constSCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(this);
297constSCEV *Op = ZExt->getOperand();
298OS <<"(zext " << *Op->getType() <<" " << *Op <<" to "
299 << *ZExt->getType() <<")";
300return;
301 }
302casescSignExtend: {
303constSCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(this);
304constSCEV *Op = SExt->getOperand();
305OS <<"(sext " << *Op->getType() <<" " << *Op <<" to "
306 << *SExt->getType() <<")";
307return;
308 }
309casescAddRecExpr: {
310constSCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(this);
311OS <<"{" << *AR->getOperand(0);
312for (unsigned i = 1, e = AR->getNumOperands(); i != e; ++i)
313OS <<",+," << *AR->getOperand(i);
314OS <<"}<";
315if (AR->hasNoUnsignedWrap())
316OS <<"nuw><";
317if (AR->hasNoSignedWrap())
318OS <<"nsw><";
319if (AR->hasNoSelfWrap() &&
320 !AR->getNoWrapFlags((NoWrapFlags)(FlagNUW |FlagNSW)))
321OS <<"nw><";
322 AR->getLoop()->getHeader()->printAsOperand(OS,/*PrintType=*/false);
323OS <<">";
324return;
325 }
326casescAddExpr:
327casescMulExpr:
328casescUMaxExpr:
329casescSMaxExpr:
330casescUMinExpr:
331casescSMinExpr:
332casescSequentialUMinExpr: {
333constSCEVNAryExpr *NAry = cast<SCEVNAryExpr>(this);
334constchar *OpStr =nullptr;
335switch (NAry->getSCEVType()) {
336casescAddExpr: OpStr =" + ";break;
337casescMulExpr: OpStr =" * ";break;
338casescUMaxExpr: OpStr =" umax ";break;
339casescSMaxExpr: OpStr =" smax ";break;
340casescUMinExpr:
341 OpStr =" umin ";
342break;
343casescSMinExpr:
344 OpStr =" smin ";
345break;
346casescSequentialUMinExpr:
347 OpStr =" umin_seq ";
348break;
349default:
350llvm_unreachable("There are no other nary expression types.");
351 }
352OS <<"(";
353 ListSeparator LS(OpStr);
354for (constSCEV *Op : NAry->operands())
355OS << LS << *Op;
356OS <<")";
357switch (NAry->getSCEVType()) {
358casescAddExpr:
359casescMulExpr:
360if (NAry->hasNoUnsignedWrap())
361OS <<"<nuw>";
362if (NAry->hasNoSignedWrap())
363OS <<"<nsw>";
364break;
365default:
366// Nothing to print for other nary expressions.
367break;
368 }
369return;
370 }
371casescUDivExpr: {
372constSCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(this);
373OS <<"(" << *UDiv->getLHS() <<" /u " << *UDiv->getRHS() <<")";
374return;
375 }
376casescUnknown:
377 cast<SCEVUnknown>(this)->getValue()->printAsOperand(OS,false);
378return;
379casescCouldNotCompute:
380OS <<"***COULDNOTCOMPUTE***";
381return;
382 }
383llvm_unreachable("Unknown SCEV kind!");
384}
385
386Type *SCEV::getType() const{
387switch (getSCEVType()) {
388casescConstant:
389return cast<SCEVConstant>(this)->getType();
390casescVScale:
391return cast<SCEVVScale>(this)->getType();
392casescPtrToInt:
393casescTruncate:
394casescZeroExtend:
395casescSignExtend:
396return cast<SCEVCastExpr>(this)->getType();
397casescAddRecExpr:
398return cast<SCEVAddRecExpr>(this)->getType();
399casescMulExpr:
400return cast<SCEVMulExpr>(this)->getType();
401casescUMaxExpr:
402casescSMaxExpr:
403casescUMinExpr:
404casescSMinExpr:
405return cast<SCEVMinMaxExpr>(this)->getType();
406casescSequentialUMinExpr:
407return cast<SCEVSequentialMinMaxExpr>(this)->getType();
408casescAddExpr:
409return cast<SCEVAddExpr>(this)->getType();
410casescUDivExpr:
411return cast<SCEVUDivExpr>(this)->getType();
412casescUnknown:
413return cast<SCEVUnknown>(this)->getType();
414casescCouldNotCompute:
415llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
416 }
417llvm_unreachable("Unknown SCEV kind!");
418}
419
420ArrayRef<const SCEV *>SCEV::operands() const{
421switch (getSCEVType()) {
422casescConstant:
423casescVScale:
424casescUnknown:
425return {};
426casescPtrToInt:
427casescTruncate:
428casescZeroExtend:
429casescSignExtend:
430return cast<SCEVCastExpr>(this)->operands();
431casescAddRecExpr:
432casescAddExpr:
433casescMulExpr:
434casescUMaxExpr:
435casescSMaxExpr:
436casescUMinExpr:
437casescSMinExpr:
438casescSequentialUMinExpr:
439return cast<SCEVNAryExpr>(this)->operands();
440casescUDivExpr:
441return cast<SCEVUDivExpr>(this)->operands();
442casescCouldNotCompute:
443llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
444 }
445llvm_unreachable("Unknown SCEV kind!");
446}
447
448boolSCEV::isZero() const{returnmatch(this,m_scev_Zero()); }
449
450boolSCEV::isOne() const{returnmatch(this,m_scev_One()); }
451
452boolSCEV::isAllOnesValue() const{returnmatch(this,m_scev_AllOnes()); }
453
454boolSCEV::isNonConstantNegative() const{
455constSCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(this);
456if (!Mul)returnfalse;
457
458// If there is a constant factor, it will be first.
459constSCEVConstant *SC = dyn_cast<SCEVConstant>(Mul->getOperand(0));
460if (!SC)returnfalse;
461
462// Return true if the value is negative, this matches things like (-42 * V).
463return SC->getAPInt().isNegative();
464}
465
466SCEVCouldNotCompute::SCEVCouldNotCompute() :
467SCEV(FoldingSetNodeIDRef(),scCouldNotCompute, 0) {}
468
469boolSCEVCouldNotCompute::classof(constSCEV *S) {
470return S->getSCEVType() ==scCouldNotCompute;
471}
472
473constSCEV *ScalarEvolution::getConstant(ConstantInt *V) {
474FoldingSetNodeIDID;
475ID.AddInteger(scConstant);
476ID.AddPointer(V);
477void *IP =nullptr;
478if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))return S;
479SCEV *S =new (SCEVAllocator)SCEVConstant(ID.Intern(SCEVAllocator), V);
480 UniqueSCEVs.InsertNode(S, IP);
481return S;
482}
483
484constSCEV *ScalarEvolution::getConstant(constAPInt &Val) {
485returngetConstant(ConstantInt::get(getContext(), Val));
486}
487
488constSCEV *
489ScalarEvolution::getConstant(Type *Ty,uint64_t V,boolisSigned) {
490IntegerType *ITy = cast<IntegerType>(getEffectiveSCEVType(Ty));
491returngetConstant(ConstantInt::get(ITy, V,isSigned));
492}
493
494constSCEV *ScalarEvolution::getVScale(Type *Ty) {
495FoldingSetNodeIDID;
496ID.AddInteger(scVScale);
497ID.AddPointer(Ty);
498void *IP =nullptr;
499if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
500return S;
501SCEV *S =new (SCEVAllocator)SCEVVScale(ID.Intern(SCEVAllocator), Ty);
502 UniqueSCEVs.InsertNode(S, IP);
503return S;
504}
505
506constSCEV *ScalarEvolution::getElementCount(Type *Ty,ElementCount EC) {
507constSCEV *Res =getConstant(Ty, EC.getKnownMinValue());
508if (EC.isScalable())
509 Res =getMulExpr(Res,getVScale(Ty));
510return Res;
511}
512
513SCEVCastExpr::SCEVCastExpr(constFoldingSetNodeIDRefID,SCEVTypes SCEVTy,
514constSCEV *op,Type *ty)
515 :SCEV(ID, SCEVTy,computeExpressionSize(op)),Op(op), Ty(ty) {}
516
517SCEVPtrToIntExpr::SCEVPtrToIntExpr(constFoldingSetNodeIDRefID,constSCEV *Op,
518Type *ITy)
519 :SCEVCastExpr(ID,scPtrToInt,Op, ITy) {
520assert(getOperand()->getType()->isPointerTy() &&Ty->isIntegerTy() &&
521"Must be a non-bit-width-changing pointer-to-integer cast!");
522}
523
524SCEVIntegralCastExpr::SCEVIntegralCastExpr(constFoldingSetNodeIDRefID,
525SCEVTypes SCEVTy,constSCEV *op,
526Type *ty)
527 :SCEVCastExpr(ID, SCEVTy,op, ty) {}
528
529SCEVTruncateExpr::SCEVTruncateExpr(constFoldingSetNodeIDRefID,constSCEV *op,
530Type *ty)
531 :SCEVIntegralCastExpr(ID,scTruncate,op, ty) {
532assert(getOperand()->getType()->isIntOrPtrTy() &&Ty->isIntOrPtrTy() &&
533"Cannot truncate non-integer value!");
534}
535
536SCEVZeroExtendExpr::SCEVZeroExtendExpr(constFoldingSetNodeIDRefID,
537constSCEV *op,Type *ty)
538 :SCEVIntegralCastExpr(ID,scZeroExtend,op, ty) {
539assert(getOperand()->getType()->isIntOrPtrTy() &&Ty->isIntOrPtrTy() &&
540"Cannot zero extend non-integer value!");
541}
542
543SCEVSignExtendExpr::SCEVSignExtendExpr(constFoldingSetNodeIDRefID,
544constSCEV *op,Type *ty)
545 :SCEVIntegralCastExpr(ID,scSignExtend,op, ty) {
546assert(getOperand()->getType()->isIntOrPtrTy() &&Ty->isIntOrPtrTy() &&
547"Cannot sign extend non-integer value!");
548}
549
550void SCEVUnknown::deleted() {
551// Clear this SCEVUnknown from various maps.
552 SE->forgetMemoizedResults(this);
553
554// Remove this SCEVUnknown from the uniquing map.
555 SE->UniqueSCEVs.RemoveNode(this);
556
557// Release the value.
558setValPtr(nullptr);
559}
560
561void SCEVUnknown::allUsesReplacedWith(Value *New) {
562// Clear this SCEVUnknown from various maps.
563 SE->forgetMemoizedResults(this);
564
565// Remove this SCEVUnknown from the uniquing map.
566 SE->UniqueSCEVs.RemoveNode(this);
567
568// Replace the value pointer in case someone is still using this SCEVUnknown.
569setValPtr(New);
570}
571
572//===----------------------------------------------------------------------===//
573// SCEV Utilities
574//===----------------------------------------------------------------------===//
575
576/// Compare the two values \p LV and \p RV in terms of their "complexity" where
577/// "complexity" is a partial (and somewhat ad-hoc) relation used to order
578/// operands in SCEV expressions.
579staticintCompareValueComplexity(constLoopInfo *const LI,Value *LV,
580Value *RV,unsignedDepth) {
581if (Depth >MaxValueCompareDepth)
582return 0;
583
584// Order pointer values after integer values. This helps SCEVExpander form
585// GEPs.
586bool LIsPointer = LV->getType()->isPointerTy(),
587 RIsPointer = RV->getType()->isPointerTy();
588if (LIsPointer != RIsPointer)
589return (int)LIsPointer - (int)RIsPointer;
590
591// Compare getValueID values.
592unsigned LID = LV->getValueID(), RID = RV->getValueID();
593if (LID != RID)
594return (int)LID - (int)RID;
595
596// Sort arguments by their position.
597if (constauto *LA = dyn_cast<Argument>(LV)) {
598constauto *RA = cast<Argument>(RV);
599unsigned LArgNo = LA->getArgNo(), RArgNo =RA->getArgNo();
600return (int)LArgNo - (int)RArgNo;
601 }
602
603if (constauto *LGV = dyn_cast<GlobalValue>(LV)) {
604constauto *RGV = cast<GlobalValue>(RV);
605
606constauto IsGVNameSemantic = [&](constGlobalValue *GV) {
607auto LT = GV->getLinkage();
608return !(GlobalValue::isPrivateLinkage(LT) ||
609GlobalValue::isInternalLinkage(LT));
610 };
611
612// Use the names to distinguish the two values, but only if the
613// names are semantically important.
614if (IsGVNameSemantic(LGV) && IsGVNameSemantic(RGV))
615return LGV->getName().compare(RGV->getName());
616 }
617
618// For instructions, compare their loop depth, and their operand count. This
619// is pretty loose.
620if (constauto *LInst = dyn_cast<Instruction>(LV)) {
621constauto *RInst = cast<Instruction>(RV);
622
623// Compare loop depths.
624constBasicBlock *LParent = LInst->getParent(),
625 *RParent = RInst->getParent();
626if (LParent != RParent) {
627unsigned LDepth = LI->getLoopDepth(LParent),
628 RDepth = LI->getLoopDepth(RParent);
629if (LDepth != RDepth)
630return (int)LDepth - (int)RDepth;
631 }
632
633// Compare the number of operands.
634unsigned LNumOps = LInst->getNumOperands(),
635 RNumOps = RInst->getNumOperands();
636if (LNumOps != RNumOps)
637return (int)LNumOps - (int)RNumOps;
638
639for (unsignedIdx :seq(LNumOps)) {
640int Result =CompareValueComplexity(LI, LInst->getOperand(Idx),
641 RInst->getOperand(Idx),Depth + 1);
642if (Result != 0)
643return Result;
644 }
645 }
646
647return 0;
648}
649
650// Return negative, zero, or positive, if LHS is less than, equal to, or greater
651// than RHS, respectively. A three-way result allows recursive comparisons to be
652// more efficient.
653// If the max analysis depth was reached, return std::nullopt, assuming we do
654// not know if they are equivalent for sure.
655static std::optional<int>
656CompareSCEVComplexity(EquivalenceClasses<const SCEV *> &EqCacheSCEV,
657constLoopInfo *const LI,constSCEV *LHS,
658constSCEV *RHS,DominatorTree &DT,unsignedDepth = 0) {
659// Fast-path: SCEVs are uniqued so we can do a quick equality check.
660if (LHS ==RHS)
661return 0;
662
663// Primarily, sort the SCEVs by their getSCEVType().
664SCEVTypes LType =LHS->getSCEVType(), RType =RHS->getSCEVType();
665if (LType != RType)
666return (int)LType - (int)RType;
667
668if (EqCacheSCEV.isEquivalent(LHS,RHS))
669return 0;
670
671if (Depth >MaxSCEVCompareDepth)
672return std::nullopt;
673
674// Aside from the getSCEVType() ordering, the particular ordering
675// isn't very important except that it's beneficial to be consistent,
676// so that (a + b) and (b + a) don't end up as different expressions.
677switch (LType) {
678casescUnknown: {
679constSCEVUnknown *LU = cast<SCEVUnknown>(LHS);
680constSCEVUnknown *RU = cast<SCEVUnknown>(RHS);
681
682intX =
683CompareValueComplexity(LI, LU->getValue(), RU->getValue(),Depth + 1);
684if (X == 0)
685 EqCacheSCEV.unionSets(LHS,RHS);
686returnX;
687 }
688
689casescConstant: {
690constSCEVConstant *LC = cast<SCEVConstant>(LHS);
691constSCEVConstant *RC = cast<SCEVConstant>(RHS);
692
693// Compare constant values.
694constAPInt &LA = LC->getAPInt();
695constAPInt &RA = RC->getAPInt();
696unsigned LBitWidth = LA.getBitWidth(), RBitWidth =RA.getBitWidth();
697if (LBitWidth != RBitWidth)
698return (int)LBitWidth - (int)RBitWidth;
699return LA.ult(RA) ? -1 : 1;
700 }
701
702casescVScale: {
703constauto *LTy = cast<IntegerType>(cast<SCEVVScale>(LHS)->getType());
704constauto *RTy = cast<IntegerType>(cast<SCEVVScale>(RHS)->getType());
705return LTy->getBitWidth() - RTy->getBitWidth();
706 }
707
708casescAddRecExpr: {
709constSCEVAddRecExpr *LA = cast<SCEVAddRecExpr>(LHS);
710constSCEVAddRecExpr *RA = cast<SCEVAddRecExpr>(RHS);
711
712// There is always a dominance between two recs that are used by one SCEV,
713// so we can safely sort recs by loop header dominance. We require such
714// order in getAddExpr.
715constLoop *LLoop = LA->getLoop(), *RLoop =RA->getLoop();
716if (LLoop != RLoop) {
717constBasicBlock *LHead = LLoop->getHeader(), *RHead = RLoop->getHeader();
718assert(LHead != RHead &&"Two loops share the same header?");
719if (DT.dominates(LHead, RHead))
720return 1;
721assert(DT.dominates(RHead, LHead) &&
722"No dominance between recurrences used by one SCEV?");
723return -1;
724 }
725
726 [[fallthrough]];
727 }
728
729casescTruncate:
730casescZeroExtend:
731casescSignExtend:
732casescPtrToInt:
733casescAddExpr:
734casescMulExpr:
735casescUDivExpr:
736casescSMaxExpr:
737casescUMaxExpr:
738casescSMinExpr:
739casescUMinExpr:
740casescSequentialUMinExpr: {
741ArrayRef<const SCEV *> LOps =LHS->operands();
742ArrayRef<const SCEV *> ROps =RHS->operands();
743
744// Lexicographically compare n-ary-like expressions.
745unsigned LNumOps = LOps.size(), RNumOps = ROps.size();
746if (LNumOps != RNumOps)
747return (int)LNumOps - (int)RNumOps;
748
749for (unsigned i = 0; i != LNumOps; ++i) {
750autoX =CompareSCEVComplexity(EqCacheSCEV, LI, LOps[i], ROps[i], DT,
751Depth + 1);
752if (X != 0)
753returnX;
754 }
755 EqCacheSCEV.unionSets(LHS,RHS);
756return 0;
757 }
758
759casescCouldNotCompute:
760llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
761 }
762llvm_unreachable("Unknown SCEV kind!");
763}
764
765/// Given a list of SCEV objects, order them by their complexity, and group
766/// objects of the same complexity together by value. When this routine is
767/// finished, we know that any duplicates in the vector are consecutive and that
768/// complexity is monotonically increasing.
769///
770/// Note that we go take special precautions to ensure that we get deterministic
771/// results from this routine. In other words, we don't want the results of
772/// this to depend on where the addresses of various SCEV objects happened to
773/// land in memory.
774staticvoidGroupByComplexity(SmallVectorImpl<const SCEV *> &Ops,
775LoopInfo *LI,DominatorTree &DT) {
776if (Ops.size() < 2)return;// Noop
777
778EquivalenceClasses<const SCEV *> EqCacheSCEV;
779
780// Whether LHS has provably less complexity than RHS.
781auto IsLessComplex = [&](constSCEV *LHS,constSCEV *RHS) {
782auto Complexity =CompareSCEVComplexity(EqCacheSCEV, LI,LHS,RHS, DT);
783return Complexity && *Complexity < 0;
784 };
785if (Ops.size() == 2) {
786// This is the common case, which also happens to be trivially simple.
787// Special case it.
788constSCEV *&LHS = Ops[0], *&RHS = Ops[1];
789if (IsLessComplex(RHS,LHS))
790std::swap(LHS,RHS);
791return;
792 }
793
794// Do the rough sort by complexity.
795llvm::stable_sort(Ops, [&](constSCEV *LHS,constSCEV *RHS) {
796return IsLessComplex(LHS,RHS);
797 });
798
799// Now that we are sorted by complexity, group elements of the same
800// complexity. Note that this is, at worst, N^2, but the vector is likely to
801// be extremely short in practice. Note that we take this approach because we
802// do not want to depend on the addresses of the objects we are grouping.
803for (unsigned i = 0, e = Ops.size(); i != e-2; ++i) {
804constSCEV *S = Ops[i];
805unsigned Complexity = S->getSCEVType();
806
807// If there are any objects of the same complexity and same value as this
808// one, group them.
809for (unsigned j = i+1; j != e && Ops[j]->getSCEVType() == Complexity; ++j) {
810if (Ops[j] == S) {// Found a duplicate.
811// Move it to immediately after i'th element.
812std::swap(Ops[i+1], Ops[j]);
813 ++i;// no need to rescan it.
814if (i == e-2)return;// Done!
815 }
816 }
817 }
818}
819
820/// Returns true if \p Ops contains a huge SCEV (the subtree of S contains at
821/// least HugeExprThreshold nodes).
822staticboolhasHugeExpression(ArrayRef<const SCEV *> Ops) {
823returnany_of(Ops, [](constSCEV *S) {
824return S->getExpressionSize() >=HugeExprThreshold;
825 });
826}
827
828/// Performs a number of common optimizations on the passed \p Ops. If the
829/// whole expression reduces down to a single operand, it will be returned.
830///
831/// The following optimizations are performed:
832/// * Fold constants using the \p Fold function.
833/// * Remove identity constants satisfying \p IsIdentity.
834/// * If a constant satisfies \p IsAbsorber, return it.
835/// * Sort operands by complexity.
836template <typename FoldT,typename IsIdentityT,typename IsAbsorberT>
837staticconstSCEV *
838constantFoldAndGroupOps(ScalarEvolution &SE,LoopInfo &LI,DominatorTree &DT,
839SmallVectorImpl<const SCEV *> &Ops, FoldT Fold,
840 IsIdentityT IsIdentity, IsAbsorberT IsAbsorber) {
841constSCEVConstant *Folded =nullptr;
842for (unsignedIdx = 0;Idx < Ops.size();) {
843constSCEV *Op = Ops[Idx];
844if (constauto *C = dyn_cast<SCEVConstant>(Op)) {
845if (!Folded)
846 Folded =C;
847else
848 Folded = cast<SCEVConstant>(
849 SE.getConstant(Fold(Folded->getAPInt(),C->getAPInt())));
850 Ops.erase(Ops.begin() +Idx);
851continue;
852 }
853 ++Idx;
854 }
855
856if (Ops.empty()) {
857assert(Folded &&"Must have folded value");
858return Folded;
859 }
860
861if (Folded && IsAbsorber(Folded->getAPInt()))
862return Folded;
863
864GroupByComplexity(Ops, &LI, DT);
865if (Folded && !IsIdentity(Folded->getAPInt()))
866 Ops.insert(Ops.begin(), Folded);
867
868return Ops.size() == 1 ? Ops[0] :nullptr;
869}
870
871//===----------------------------------------------------------------------===//
872// Simple SCEV method implementations
873//===----------------------------------------------------------------------===//
874
875/// Compute BC(It, K). The result has width W. Assume, K > 0.
876staticconstSCEV *BinomialCoefficient(constSCEV *It,unsigned K,
877ScalarEvolution &SE,
878Type *ResultTy) {
879// Handle the simplest case efficiently.
880if (K == 1)
881return SE.getTruncateOrZeroExtend(It, ResultTy);
882
883// We are using the following formula for BC(It, K):
884//
885// BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / K!
886//
887// Suppose, W is the bitwidth of the return value. We must be prepared for
888// overflow. Hence, we must assure that the result of our computation is
889// equal to the accurate one modulo 2^W. Unfortunately, division isn't
890// safe in modular arithmetic.
891//
892// However, this code doesn't use exactly that formula; the formula it uses
893// is something like the following, where T is the number of factors of 2 in
894// K! (i.e. trailing zeros in the binary representation of K!), and ^ is
895// exponentiation:
896//
897// BC(It, K) = (It * (It - 1) * ... * (It - K + 1)) / 2^T / (K! / 2^T)
898//
899// This formula is trivially equivalent to the previous formula. However,
900// this formula can be implemented much more efficiently. The trick is that
901// K! / 2^T is odd, and exact division by an odd number *is* safe in modular
902// arithmetic. To do exact division in modular arithmetic, all we have
903// to do is multiply by the inverse. Therefore, this step can be done at
904// width W.
905//
906// The next issue is how to safely do the division by 2^T. The way this
907// is done is by doing the multiplication step at a width of at least W + T
908// bits. This way, the bottom W+T bits of the product are accurate. Then,
909// when we perform the division by 2^T (which is equivalent to a right shift
910// by T), the bottom W bits are accurate. Extra bits are okay; they'll get
911// truncated out after the division by 2^T.
912//
913// In comparison to just directly using the first formula, this technique
914// is much more efficient; using the first formula requires W * K bits,
915// but this formula less than W + K bits. Also, the first formula requires
916// a division step, whereas this formula only requires multiplies and shifts.
917//
918// It doesn't matter whether the subtraction step is done in the calculation
919// width or the input iteration count's width; if the subtraction overflows,
920// the result must be zero anyway. We prefer here to do it in the width of
921// the induction variable because it helps a lot for certain cases; CodeGen
922// isn't smart enough to ignore the overflow, which leads to much less
923// efficient code if the width of the subtraction is wider than the native
924// register width.
925//
926// (It's possible to not widen at all by pulling out factors of 2 before
927// the multiplication; for example, K=2 can be calculated as
928// It/2*(It+(It*INT_MIN/INT_MIN)+-1). However, it requires
929// extra arithmetic, so it's not an obvious win, and it gets
930// much more complicated for K > 3.)
931
932// Protection from insane SCEVs; this bound is conservative,
933// but it probably doesn't matter.
934if (K > 1000)
935return SE.getCouldNotCompute();
936
937unsigned W = SE.getTypeSizeInBits(ResultTy);
938
939// Calculate K! / 2^T and T; we divide out the factors of two before
940// multiplying for calculating K! / 2^T to avoid overflow.
941// Other overflow doesn't matter because we only care about the bottom
942// W bits of the result.
943APInt OddFactorial(W, 1);
944unsignedT = 1;
945for (unsigned i = 3; i <= K; ++i) {
946unsigned TwoFactors =countr_zero(i);
947T += TwoFactors;
948 OddFactorial *= (i >> TwoFactors);
949 }
950
951// We need at least W + T bits for the multiplication step
952unsigned CalculationBits = W +T;
953
954// Calculate 2^T, at width T+W.
955APInt DivFactor =APInt::getOneBitSet(CalculationBits,T);
956
957// Calculate the multiplicative inverse of K! / 2^T;
958// this multiplication factor will perform the exact division by
959// K! / 2^T.
960APInt MultiplyFactor = OddFactorial.multiplicativeInverse();
961
962// Calculate the product, at width T+W
963IntegerType *CalculationTy =IntegerType::get(SE.getContext(),
964 CalculationBits);
965constSCEV *Dividend = SE.getTruncateOrZeroExtend(It, CalculationTy);
966for (unsigned i = 1; i != K; ++i) {
967constSCEV *S = SE.getMinusSCEV(It, SE.getConstant(It->getType(), i));
968 Dividend = SE.getMulExpr(Dividend,
969 SE.getTruncateOrZeroExtend(S, CalculationTy));
970 }
971
972// Divide by 2^T
973constSCEV *DivResult = SE.getUDivExpr(Dividend, SE.getConstant(DivFactor));
974
975// Truncate the result, and divide by K! / 2^T.
976
977return SE.getMulExpr(SE.getConstant(MultiplyFactor),
978 SE.getTruncateOrZeroExtend(DivResult, ResultTy));
979}
980
981/// Return the value of this chain of recurrences at the specified iteration
982/// number. We can evaluate this recurrence by multiplying each element in the
983/// chain by the binomial coefficient corresponding to it. In other words, we
984/// can evaluate {A,+,B,+,C,+,D} as:
985///
986/// A*BC(It, 0) + B*BC(It, 1) + C*BC(It, 2) + D*BC(It, 3)
987///
988/// where BC(It, k) stands for binomial coefficient.
989constSCEV *SCEVAddRecExpr::evaluateAtIteration(constSCEV *It,
990ScalarEvolution &SE) const{
991returnevaluateAtIteration(operands(), It, SE);
992}
993
994constSCEV *
995SCEVAddRecExpr::evaluateAtIteration(ArrayRef<const SCEV *>Operands,
996constSCEV *It,ScalarEvolution &SE) {
997assert(Operands.size() > 0);
998constSCEV *Result =Operands[0];
999for (unsigned i = 1, e =Operands.size(); i != e; ++i) {
1000// The computation is correct in the face of overflow provided that the
1001// multiplication is performed _after_ the evaluation of the binomial
1002// coefficient.
1003constSCEV *Coeff =BinomialCoefficient(It, i, SE, Result->getType());
1004if (isa<SCEVCouldNotCompute>(Coeff))
1005return Coeff;
1006
1007 Result = SE.getAddExpr(Result, SE.getMulExpr(Operands[i], Coeff));
1008 }
1009return Result;
1010}
1011
1012//===----------------------------------------------------------------------===//
1013// SCEV Expression folder implementations
1014//===----------------------------------------------------------------------===//
1015
1016constSCEV *ScalarEvolution::getLosslessPtrToIntExpr(constSCEV *Op,
1017unsignedDepth) {
1018assert(Depth <= 1 &&
1019"getLosslessPtrToIntExpr() should self-recurse at most once.");
1020
1021// We could be called with an integer-typed operands during SCEV rewrites.
1022// Since the operand is an integer already, just perform zext/trunc/self cast.
1023if (!Op->getType()->isPointerTy())
1024returnOp;
1025
1026// What would be an ID for such a SCEV cast expression?
1027FoldingSetNodeIDID;
1028ID.AddInteger(scPtrToInt);
1029ID.AddPointer(Op);
1030
1031void *IP =nullptr;
1032
1033// Is there already an expression for such a cast?
1034if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1035return S;
1036
1037// It isn't legal for optimizations to construct new ptrtoint expressions
1038// for non-integral pointers.
1039if (getDataLayout().isNonIntegralPointerType(Op->getType()))
1040returngetCouldNotCompute();
1041
1042Type *IntPtrTy =getDataLayout().getIntPtrType(Op->getType());
1043
1044// We can only trivially model ptrtoint if SCEV's effective (integer) type
1045// is sufficiently wide to represent all possible pointer values.
1046// We could theoretically teach SCEV to truncate wider pointers, but
1047// that isn't implemented for now.
1048if (getDataLayout().getTypeSizeInBits(getEffectiveSCEVType(Op->getType())) !=
1049getDataLayout().getTypeSizeInBits(IntPtrTy))
1050returngetCouldNotCompute();
1051
1052// If not, is this expression something we can't reduce any further?
1053if (auto *U = dyn_cast<SCEVUnknown>(Op)) {
1054// Perform some basic constant folding. If the operand of the ptr2int cast
1055// is a null pointer, don't create a ptr2int SCEV expression (that will be
1056// left as-is), but produce a zero constant.
1057// NOTE: We could handle a more general case, but lack motivational cases.
1058if (isa<ConstantPointerNull>(U->getValue()))
1059returngetZero(IntPtrTy);
1060
1061// Create an explicit cast node.
1062// We can reuse the existing insert position since if we get here,
1063// we won't have made any changes which would invalidate it.
1064SCEV *S =new (SCEVAllocator)
1065SCEVPtrToIntExpr(ID.Intern(SCEVAllocator),Op, IntPtrTy);
1066 UniqueSCEVs.InsertNode(S, IP);
1067registerUser(S,Op);
1068return S;
1069 }
1070
1071assert(Depth == 0 &&"getLosslessPtrToIntExpr() should not self-recurse for "
1072"non-SCEVUnknown's.");
1073
1074// Otherwise, we've got some expression that is more complex than just a
1075// single SCEVUnknown. But we don't want to have a SCEVPtrToIntExpr of an
1076// arbitrary expression, we want to have SCEVPtrToIntExpr of an SCEVUnknown
1077// only, and the expressions must otherwise be integer-typed.
1078// So sink the cast down to the SCEVUnknown's.
1079
1080 /// The SCEVPtrToIntSinkingRewriter takes a scalar evolution expression,
1081 /// which computes a pointer-typed value, and rewrites the whole expression
1082 /// tree so that *all* the computations are done on integers, and the only
1083 /// pointer-typed operands in the expression are SCEVUnknown.
1084classSCEVPtrToIntSinkingRewriter
1085 :publicSCEVRewriteVisitor<SCEVPtrToIntSinkingRewriter> {
1086usingBase =SCEVRewriteVisitor<SCEVPtrToIntSinkingRewriter>;
1087
1088public:
1089 SCEVPtrToIntSinkingRewriter(ScalarEvolution &SE) :SCEVRewriteVisitor(SE) {}
1090
1091staticconstSCEV *rewrite(constSCEV *Scev,ScalarEvolution &SE) {
1092 SCEVPtrToIntSinkingRewriterRewriter(SE);
1093returnRewriter.visit(Scev);
1094 }
1095
1096constSCEV *visit(constSCEV *S) {
1097Type *STy = S->getType();
1098// If the expression is not pointer-typed, just keep it as-is.
1099if (!STy->isPointerTy())
1100return S;
1101// Else, recursively sink the cast down into it.
1102return Base::visit(S);
1103 }
1104
1105constSCEV *visitAddExpr(constSCEVAddExpr *Expr) {
1106SmallVector<const SCEV *, 2>Operands;
1107bool Changed =false;
1108for (constauto *Op : Expr->operands()) {
1109Operands.push_back(visit(Op));
1110 Changed |=Op !=Operands.back();
1111 }
1112return !Changed ? Expr : SE.getAddExpr(Operands, Expr->getNoWrapFlags());
1113 }
1114
1115constSCEV *visitMulExpr(constSCEVMulExpr *Expr) {
1116SmallVector<const SCEV *, 2>Operands;
1117bool Changed =false;
1118for (constauto *Op : Expr->operands()) {
1119Operands.push_back(visit(Op));
1120 Changed |=Op !=Operands.back();
1121 }
1122return !Changed ? Expr : SE.getMulExpr(Operands, Expr->getNoWrapFlags());
1123 }
1124
1125constSCEV *visitUnknown(constSCEVUnknown *Expr) {
1126assert(Expr->getType()->isPointerTy() &&
1127"Should only reach pointer-typed SCEVUnknown's.");
1128return SE.getLosslessPtrToIntExpr(Expr,/*Depth=*/1);
1129 }
1130 };
1131
1132// And actually perform the cast sinking.
1133constSCEV *IntOp = SCEVPtrToIntSinkingRewriter::rewrite(Op, *this);
1134assert(IntOp->getType()->isIntegerTy() &&
1135"We must have succeeded in sinking the cast, "
1136"and ending up with an integer-typed expression!");
1137return IntOp;
1138}
1139
1140constSCEV *ScalarEvolution::getPtrToIntExpr(constSCEV *Op,Type *Ty) {
1141assert(Ty->isIntegerTy() &&"Target type must be an integer type!");
1142
1143constSCEV *IntOp =getLosslessPtrToIntExpr(Op);
1144if (isa<SCEVCouldNotCompute>(IntOp))
1145return IntOp;
1146
1147returngetTruncateOrZeroExtend(IntOp, Ty);
1148}
1149
1150constSCEV *ScalarEvolution::getTruncateExpr(constSCEV *Op,Type *Ty,
1151unsignedDepth) {
1152assert(getTypeSizeInBits(Op->getType()) >getTypeSizeInBits(Ty) &&
1153"This is not a truncating conversion!");
1154assert(isSCEVable(Ty) &&
1155"This is not a conversion to a SCEVable type!");
1156assert(!Op->getType()->isPointerTy() &&"Can't truncate pointer!");
1157 Ty =getEffectiveSCEVType(Ty);
1158
1159FoldingSetNodeIDID;
1160ID.AddInteger(scTruncate);
1161ID.AddPointer(Op);
1162ID.AddPointer(Ty);
1163void *IP =nullptr;
1164if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))return S;
1165
1166// Fold if the operand is constant.
1167if (constSCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1168returngetConstant(
1169 cast<ConstantInt>(ConstantExpr::getTrunc(SC->getValue(), Ty)));
1170
1171// trunc(trunc(x)) --> trunc(x)
1172if (constSCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op))
1173returngetTruncateExpr(ST->getOperand(), Ty,Depth + 1);
1174
1175// trunc(sext(x)) --> sext(x) if widening or trunc(x) if narrowing
1176if (constSCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1177returngetTruncateOrSignExtend(SS->getOperand(), Ty,Depth + 1);
1178
1179// trunc(zext(x)) --> zext(x) if widening or trunc(x) if narrowing
1180if (constSCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1181returngetTruncateOrZeroExtend(SZ->getOperand(), Ty,Depth + 1);
1182
1183if (Depth >MaxCastDepth) {
1184SCEV *S =
1185new (SCEVAllocator)SCEVTruncateExpr(ID.Intern(SCEVAllocator),Op, Ty);
1186 UniqueSCEVs.InsertNode(S, IP);
1187registerUser(S,Op);
1188return S;
1189 }
1190
1191// trunc(x1 + ... + xN) --> trunc(x1) + ... + trunc(xN) and
1192// trunc(x1 * ... * xN) --> trunc(x1) * ... * trunc(xN),
1193// if after transforming we have at most one truncate, not counting truncates
1194// that replace other casts.
1195if (isa<SCEVAddExpr>(Op) || isa<SCEVMulExpr>(Op)) {
1196auto *CommOp = cast<SCEVCommutativeExpr>(Op);
1197SmallVector<const SCEV *, 4>Operands;
1198unsigned numTruncs = 0;
1199for (unsigned i = 0, e = CommOp->getNumOperands(); i != e && numTruncs < 2;
1200 ++i) {
1201constSCEV *S =getTruncateExpr(CommOp->getOperand(i), Ty,Depth + 1);
1202if (!isa<SCEVIntegralCastExpr>(CommOp->getOperand(i)) &&
1203 isa<SCEVTruncateExpr>(S))
1204 numTruncs++;
1205Operands.push_back(S);
1206 }
1207if (numTruncs < 2) {
1208if (isa<SCEVAddExpr>(Op))
1209returngetAddExpr(Operands);
1210if (isa<SCEVMulExpr>(Op))
1211returngetMulExpr(Operands);
1212llvm_unreachable("Unexpected SCEV type for Op.");
1213 }
1214// Although we checked in the beginning that ID is not in the cache, it is
1215// possible that during recursion and different modification ID was inserted
1216// into the cache. So if we find it, just return it.
1217if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
1218return S;
1219 }
1220
1221// If the input value is a chrec scev, truncate the chrec's operands.
1222if (constSCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
1223SmallVector<const SCEV *, 4>Operands;
1224for (constSCEV *Op : AddRec->operands())
1225Operands.push_back(getTruncateExpr(Op, Ty,Depth + 1));
1226returngetAddRecExpr(Operands, AddRec->getLoop(),SCEV::FlagAnyWrap);
1227 }
1228
1229// Return zero if truncating to known zeros.
1230uint32_t MinTrailingZeros =getMinTrailingZeros(Op);
1231if (MinTrailingZeros >=getTypeSizeInBits(Ty))
1232returngetZero(Ty);
1233
1234// The cast wasn't folded; create an explicit cast node. We can reuse
1235// the existing insert position since if we get here, we won't have
1236// made any changes which would invalidate it.
1237SCEV *S =new (SCEVAllocator)SCEVTruncateExpr(ID.Intern(SCEVAllocator),
1238Op, Ty);
1239 UniqueSCEVs.InsertNode(S, IP);
1240registerUser(S,Op);
1241return S;
1242}
1243
1244// Get the limit of a recurrence such that incrementing by Step cannot cause
1245// signed overflow as long as the value of the recurrence within the
1246// loop does not exceed this limit before incrementing.
1247staticconstSCEV *getSignedOverflowLimitForStep(constSCEV *Step,
1248ICmpInst::Predicate *Pred,
1249ScalarEvolution *SE) {
1250unsignedBitWidth = SE->getTypeSizeInBits(Step->getType());
1251if (SE->isKnownPositive(Step)) {
1252 *Pred =ICmpInst::ICMP_SLT;
1253return SE->getConstant(APInt::getSignedMinValue(BitWidth) -
1254 SE->getSignedRangeMax(Step));
1255 }
1256if (SE->isKnownNegative(Step)) {
1257 *Pred =ICmpInst::ICMP_SGT;
1258return SE->getConstant(APInt::getSignedMaxValue(BitWidth) -
1259 SE->getSignedRangeMin(Step));
1260 }
1261returnnullptr;
1262}
1263
1264// Get the limit of a recurrence such that incrementing by Step cannot cause
1265// unsigned overflow as long as the value of the recurrence within the loop does
1266// not exceed this limit before incrementing.
1267staticconstSCEV *getUnsignedOverflowLimitForStep(constSCEV *Step,
1268ICmpInst::Predicate *Pred,
1269ScalarEvolution *SE) {
1270unsignedBitWidth = SE->getTypeSizeInBits(Step->getType());
1271 *Pred =ICmpInst::ICMP_ULT;
1272
1273return SE->getConstant(APInt::getMinValue(BitWidth) -
1274 SE->getUnsignedRangeMax(Step));
1275}
1276
1277namespace{
1278
1279structExtendOpTraitsBase {
1280typedefconstSCEV *(ScalarEvolution::*GetExtendExprTy)(constSCEV *,Type *,
1281unsigned);
1282};
1283
1284// Used to make code generic over signed and unsigned overflow.
1285template <typename ExtendOp>structExtendOpTraits {
1286// Members present:
1287//
1288// static const SCEV::NoWrapFlags WrapType;
1289//
1290// static const ExtendOpTraitsBase::GetExtendExprTy GetExtendExpr;
1291//
1292// static const SCEV *getOverflowLimitForStep(const SCEV *Step,
1293// ICmpInst::Predicate *Pred,
1294// ScalarEvolution *SE);
1295};
1296
1297template <>
1298structExtendOpTraits<SCEVSignExtendExpr> :public ExtendOpTraitsBase {
1299staticconstSCEV::NoWrapFlags WrapType =SCEV::FlagNSW;
1300
1301staticconst GetExtendExprTy GetExtendExpr;
1302
1303staticconstSCEV *getOverflowLimitForStep(constSCEV *Step,
1304ICmpInst::Predicate *Pred,
1305ScalarEvolution *SE) {
1306returngetSignedOverflowLimitForStep(Step, Pred, SE);
1307 }
1308};
1309
1310const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1311SCEVSignExtendExpr>::GetExtendExpr = &ScalarEvolution::getSignExtendExpr;
1312
1313template <>
1314structExtendOpTraits<SCEVZeroExtendExpr> :public ExtendOpTraitsBase {
1315staticconstSCEV::NoWrapFlags WrapType =SCEV::FlagNUW;
1316
1317staticconst GetExtendExprTy GetExtendExpr;
1318
1319staticconstSCEV *getOverflowLimitForStep(constSCEV *Step,
1320ICmpInst::Predicate *Pred,
1321ScalarEvolution *SE) {
1322returngetUnsignedOverflowLimitForStep(Step, Pred, SE);
1323 }
1324};
1325
1326const ExtendOpTraitsBase::GetExtendExprTy ExtendOpTraits<
1327SCEVZeroExtendExpr>::GetExtendExpr = &ScalarEvolution::getZeroExtendExpr;
1328
1329}// end anonymous namespace
1330
1331// The recurrence AR has been shown to have no signed/unsigned wrap or something
1332// close to it. Typically, if we can prove NSW/NUW for AR, then we can just as
1333// easily prove NSW/NUW for its preincrement or postincrement sibling. This
1334// allows normalizing a sign/zero extended AddRec as such: {sext/zext(Step +
1335// Start),+,Step} => {(Step + sext/zext(Start),+,Step} As a result, the
1336// expression "Step + sext/zext(PreIncAR)" is congruent with
1337// "sext/zext(PostIncAR)"
1338template <typename ExtendOpTy>
1339staticconstSCEV *getPreStartForExtend(constSCEVAddRecExpr *AR,Type *Ty,
1340ScalarEvolution *SE,unsignedDepth) {
1341auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1342auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1343
1344constLoop *L = AR->getLoop();
1345constSCEV *Start = AR->getStart();
1346constSCEV *Step = AR->getStepRecurrence(*SE);
1347
1348// Check for a simple looking step prior to loop entry.
1349constSCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Start);
1350if (!SA)
1351returnnullptr;
1352
1353// Create an AddExpr for "PreStart" after subtracting Step. Full SCEV
1354// subtraction is expensive. For this purpose, perform a quick and dirty
1355// difference, by checking for Step in the operand list. Note, that
1356// SA might have repeated ops, like %a + %a + ..., so only remove one.
1357SmallVector<const SCEV *, 4> DiffOps(SA->operands());
1358for (auto It = DiffOps.begin(); It != DiffOps.end(); ++It)
1359if (*It == Step) {
1360 DiffOps.erase(It);
1361break;
1362 }
1363
1364if (DiffOps.size() == SA->getNumOperands())
1365returnnullptr;
1366
1367// Try to prove `WrapType` (SCEV::FlagNSW or SCEV::FlagNUW) on `PreStart` +
1368// `Step`:
1369
1370// 1. NSW/NUW flags on the step increment.
1371auto PreStartFlags =
1372ScalarEvolution::maskFlags(SA->getNoWrapFlags(),SCEV::FlagNUW);
1373constSCEV *PreStart = SE->getAddExpr(DiffOps, PreStartFlags);
1374constSCEVAddRecExpr *PreAR = dyn_cast<SCEVAddRecExpr>(
1375 SE->getAddRecExpr(PreStart, Step, L,SCEV::FlagAnyWrap));
1376
1377// "{S,+,X} is <nsw>/<nuw>" and "the backedge is taken at least once" implies
1378// "S+X does not sign/unsign-overflow".
1379//
1380
1381constSCEV *BECount = SE->getBackedgeTakenCount(L);
1382if (PreAR && PreAR->getNoWrapFlags(WrapType) &&
1383 !isa<SCEVCouldNotCompute>(BECount) && SE->isKnownPositive(BECount))
1384return PreStart;
1385
1386// 2. Direct overflow check on the step operation's expression.
1387unsignedBitWidth = SE->getTypeSizeInBits(AR->getType());
1388Type *WideTy =IntegerType::get(SE->getContext(),BitWidth * 2);
1389constSCEV *OperandExtendedStart =
1390 SE->getAddExpr((SE->*GetExtendExpr)(PreStart, WideTy,Depth),
1391 (SE->*GetExtendExpr)(Step, WideTy,Depth));
1392if ((SE->*GetExtendExpr)(Start, WideTy,Depth) == OperandExtendedStart) {
1393if (PreAR && AR->getNoWrapFlags(WrapType)) {
1394// If we know `AR` == {`PreStart`+`Step`,+,`Step`} is `WrapType` (FlagNSW
1395// or FlagNUW) and that `PreStart` + `Step` is `WrapType` too, then
1396// `PreAR` == {`PreStart`,+,`Step`} is also `WrapType`. Cache this fact.
1397 SE->setNoWrapFlags(const_cast<SCEVAddRecExpr *>(PreAR), WrapType);
1398 }
1399return PreStart;
1400 }
1401
1402// 3. Loop precondition.
1403ICmpInst::Predicate Pred;
1404constSCEV *OverflowLimit =
1405 ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(Step, &Pred, SE);
1406
1407if (OverflowLimit &&
1408 SE->isLoopEntryGuardedByCond(L, Pred, PreStart, OverflowLimit))
1409return PreStart;
1410
1411returnnullptr;
1412}
1413
1414// Get the normalized zero or sign extended expression for this AddRec's Start.
1415template <typename ExtendOpTy>
1416staticconstSCEV *getExtendAddRecStart(constSCEVAddRecExpr *AR,Type *Ty,
1417ScalarEvolution *SE,
1418unsignedDepth) {
1419auto GetExtendExpr = ExtendOpTraits<ExtendOpTy>::GetExtendExpr;
1420
1421constSCEV *PreStart = getPreStartForExtend<ExtendOpTy>(AR, Ty, SE,Depth);
1422if (!PreStart)
1423return (SE->*GetExtendExpr)(AR->getStart(), Ty,Depth);
1424
1425return SE->getAddExpr((SE->*GetExtendExpr)(AR->getStepRecurrence(*SE), Ty,
1426Depth),
1427 (SE->*GetExtendExpr)(PreStart, Ty,Depth));
1428}
1429
1430// Try to prove away overflow by looking at "nearby" add recurrences. A
1431// motivating example for this rule: if we know `{0,+,4}` is `ult` `-1` and it
1432// does not itself wrap then we can conclude that `{1,+,4}` is `nuw`.
1433//
1434// Formally:
1435//
1436// {S,+,X} == {S-T,+,X} + T
1437// => Ext({S,+,X}) == Ext({S-T,+,X} + T)
1438//
1439// If ({S-T,+,X} + T) does not overflow ... (1)
1440//
1441// RHS == Ext({S-T,+,X} + T) == Ext({S-T,+,X}) + Ext(T)
1442//
1443// If {S-T,+,X} does not overflow ... (2)
1444//
1445// RHS == Ext({S-T,+,X}) + Ext(T) == {Ext(S-T),+,Ext(X)} + Ext(T)
1446// == {Ext(S-T)+Ext(T),+,Ext(X)}
1447//
1448// If (S-T)+T does not overflow ... (3)
1449//
1450// RHS == {Ext(S-T)+Ext(T),+,Ext(X)} == {Ext(S-T+T),+,Ext(X)}
1451// == {Ext(S),+,Ext(X)} == LHS
1452//
1453// Thus, if (1), (2) and (3) are true for some T, then
1454// Ext({S,+,X}) == {Ext(S),+,Ext(X)}
1455//
1456// (3) is implied by (1) -- "(S-T)+T does not overflow" is simply "({S-T,+,X}+T)
1457// does not overflow" restricted to the 0th iteration. Therefore we only need
1458// to check for (1) and (2).
1459//
1460// In the current context, S is `Start`, X is `Step`, Ext is `ExtendOpTy` and T
1461// is `Delta` (defined below).
1462template <typename ExtendOpTy>
1463bool ScalarEvolution::proveNoWrapByVaryingStart(constSCEV *Start,
1464constSCEV *Step,
1465constLoop *L) {
1466auto WrapType = ExtendOpTraits<ExtendOpTy>::WrapType;
1467
1468// We restrict `Start` to a constant to prevent SCEV from spending too much
1469// time here. It is correct (but more expensive) to continue with a
1470// non-constant `Start` and do a general SCEV subtraction to compute
1471// `PreStart` below.
1472constSCEVConstant *StartC = dyn_cast<SCEVConstant>(Start);
1473if (!StartC)
1474returnfalse;
1475
1476APInt StartAI = StartC->getAPInt();
1477
1478for (unsigned Delta : {-2, -1, 1, 2}) {
1479constSCEV *PreStart =getConstant(StartAI - Delta);
1480
1481FoldingSetNodeIDID;
1482ID.AddInteger(scAddRecExpr);
1483ID.AddPointer(PreStart);
1484ID.AddPointer(Step);
1485ID.AddPointer(L);
1486void *IP =nullptr;
1487constauto *PreAR =
1488static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
1489
1490// Give up if we don't already have the add recurrence we need because
1491// actually constructing an add recurrence is relatively expensive.
1492if (PreAR && PreAR->getNoWrapFlags(WrapType)) {// proves (2)
1493constSCEV *DeltaS =getConstant(StartC->getType(), Delta);
1494ICmpInst::Predicate Pred =ICmpInst::BAD_ICMP_PREDICATE;
1495constSCEV *Limit = ExtendOpTraits<ExtendOpTy>::getOverflowLimitForStep(
1496 DeltaS, &Pred,this);
1497if (Limit &&isKnownPredicate(Pred, PreAR, Limit))// proves (1)
1498returntrue;
1499 }
1500 }
1501
1502returnfalse;
1503}
1504
1505// Finds an integer D for an expression (C + x + y + ...) such that the top
1506// level addition in (D + (C - D + x + y + ...)) would not wrap (signed or
1507// unsigned) and the number of trailing zeros of (C - D + x + y + ...) is
1508// maximized, where C is the \p ConstantTerm, x, y, ... are arbitrary SCEVs, and
1509// the (C + x + y + ...) expression is \p WholeAddExpr.
1510staticAPIntextractConstantWithoutWrapping(ScalarEvolution &SE,
1511constSCEVConstant *ConstantTerm,
1512constSCEVAddExpr *WholeAddExpr) {
1513constAPInt &C = ConstantTerm->getAPInt();
1514constunsignedBitWidth =C.getBitWidth();
1515// Find number of trailing zeros of (x + y + ...) w/o the C first:
1516uint32_t TZ =BitWidth;
1517for (unsignedI = 1, E = WholeAddExpr->getNumOperands();I < E && TZ; ++I)
1518 TZ = std::min(TZ, SE.getMinTrailingZeros(WholeAddExpr->getOperand(I)));
1519if (TZ) {
1520// Set D to be as many least significant bits of C as possible while still
1521// guaranteeing that adding D to (C - D + x + y + ...) won't cause a wrap:
1522return TZ <BitWidth ?C.trunc(TZ).zext(BitWidth) :C;
1523 }
1524returnAPInt(BitWidth, 0);
1525}
1526
1527// Finds an integer D for an affine AddRec expression {C,+,x} such that the top
1528// level addition in (D + {C-D,+,x}) would not wrap (signed or unsigned) and the
1529// number of trailing zeros of (C - D + x * n) is maximized, where C is the \p
1530// ConstantStart, x is an arbitrary \p Step, and n is the loop trip count.
1531staticAPIntextractConstantWithoutWrapping(ScalarEvolution &SE,
1532constAPInt &ConstantStart,
1533constSCEV *Step) {
1534constunsignedBitWidth = ConstantStart.getBitWidth();
1535constuint32_t TZ = SE.getMinTrailingZeros(Step);
1536if (TZ)
1537return TZ <BitWidth ? ConstantStart.trunc(TZ).zext(BitWidth)
1538 : ConstantStart;
1539returnAPInt(BitWidth, 0);
1540}
1541
1542staticvoidinsertFoldCacheEntry(
1543constScalarEvolution::FoldID &ID,constSCEV *S,
1544DenseMap<ScalarEvolution::FoldID, const SCEV *> &FoldCache,
1545DenseMap<constSCEV *,SmallVector<ScalarEvolution::FoldID, 2>>
1546 &FoldCacheUser) {
1547autoI = FoldCache.insert({ID, S});
1548if (!I.second) {
1549// Remove FoldCacheUser entry for ID when replacing an existing FoldCache
1550// entry.
1551auto &UserIDs = FoldCacheUser[I.first->second];
1552assert(count(UserIDs,ID) == 1 &&"unexpected duplicates in UserIDs");
1553for (unsignedI = 0;I != UserIDs.size(); ++I)
1554if (UserIDs[I] ==ID) {
1555std::swap(UserIDs[I], UserIDs.back());
1556break;
1557 }
1558 UserIDs.pop_back();
1559I.first->second = S;
1560 }
1561 FoldCacheUser[S].push_back(ID);
1562}
1563
1564constSCEV *
1565ScalarEvolution::getZeroExtendExpr(constSCEV *Op,Type *Ty,unsignedDepth) {
1566assert(getTypeSizeInBits(Op->getType()) <getTypeSizeInBits(Ty) &&
1567"This is not an extending conversion!");
1568assert(isSCEVable(Ty) &&
1569"This is not a conversion to a SCEVable type!");
1570assert(!Op->getType()->isPointerTy() &&"Can't extend pointer!");
1571 Ty =getEffectiveSCEVType(Ty);
1572
1573FoldIDID(scZeroExtend,Op, Ty);
1574auto Iter = FoldCache.find(ID);
1575if (Iter != FoldCache.end())
1576return Iter->second;
1577
1578constSCEV *S =getZeroExtendExprImpl(Op, Ty,Depth);
1579if (!isa<SCEVZeroExtendExpr>(S))
1580insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser);
1581return S;
1582}
1583
1584constSCEV *ScalarEvolution::getZeroExtendExprImpl(constSCEV *Op,Type *Ty,
1585unsignedDepth) {
1586assert(getTypeSizeInBits(Op->getType()) <getTypeSizeInBits(Ty) &&
1587"This is not an extending conversion!");
1588assert(isSCEVable(Ty) &&"This is not a conversion to a SCEVable type!");
1589assert(!Op->getType()->isPointerTy() &&"Can't extend pointer!");
1590
1591// Fold if the operand is constant.
1592if (constSCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1593returngetConstant(SC->getAPInt().zext(getTypeSizeInBits(Ty)));
1594
1595// zext(zext(x)) --> zext(x)
1596if (constSCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1597returngetZeroExtendExpr(SZ->getOperand(), Ty,Depth + 1);
1598
1599// Before doing any expensive analysis, check to see if we've already
1600// computed a SCEV for this Op and Ty.
1601FoldingSetNodeIDID;
1602ID.AddInteger(scZeroExtend);
1603ID.AddPointer(Op);
1604ID.AddPointer(Ty);
1605void *IP =nullptr;
1606if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))return S;
1607if (Depth >MaxCastDepth) {
1608SCEV *S =new (SCEVAllocator)SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1609Op, Ty);
1610 UniqueSCEVs.InsertNode(S, IP);
1611registerUser(S,Op);
1612return S;
1613 }
1614
1615// zext(trunc(x)) --> zext(x) or x or trunc(x)
1616if (constSCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1617// It's possible the bits taken off by the truncate were all zero bits. If
1618// so, we should be able to simplify this further.
1619constSCEV *X = ST->getOperand();
1620ConstantRange CR =getUnsignedRange(X);
1621unsigned TruncBits =getTypeSizeInBits(ST->getType());
1622unsigned NewBits =getTypeSizeInBits(Ty);
1623if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
1624 CR.zextOrTrunc(NewBits)))
1625returngetTruncateOrZeroExtend(X, Ty,Depth);
1626 }
1627
1628// If the input value is a chrec scev, and we can prove that the value
1629// did not overflow the old, smaller, value, we can zero extend all of the
1630// operands (often constants). This allows analysis of something like
1631// this: for (unsigned char X = 0; X < 100; ++X) { int Y = X; }
1632if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
1633if (AR->isAffine()) {
1634constSCEV *Start = AR->getStart();
1635constSCEV *Step = AR->getStepRecurrence(*this);
1636unsignedBitWidth =getTypeSizeInBits(AR->getType());
1637constLoop *L = AR->getLoop();
1638
1639// If we have special knowledge that this addrec won't overflow,
1640// we don't need to do any further analysis.
1641if (AR->hasNoUnsignedWrap()) {
1642 Start =
1643 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,this,Depth + 1);
1644 Step =getZeroExtendExpr(Step, Ty,Depth + 1);
1645returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1646 }
1647
1648// Check whether the backedge-taken count is SCEVCouldNotCompute.
1649// Note that this serves two purposes: It filters out loops that are
1650// simply not analyzable, and it covers the case where this code is
1651// being called from within backedge-taken count analysis, such that
1652// attempting to ask for the backedge-taken count would likely result
1653// in infinite recursion. In the later case, the analysis code will
1654// cope with a conservative value, and it will take care to purge
1655// that value once it has finished.
1656constSCEV *MaxBECount =getConstantMaxBackedgeTakenCount(L);
1657if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
1658// Manually compute the final value for AR, checking for overflow.
1659
1660// Check whether the backedge-taken count can be losslessly casted to
1661// the addrec's type. The count is always unsigned.
1662constSCEV *CastedMaxBECount =
1663getTruncateOrZeroExtend(MaxBECount, Start->getType(),Depth);
1664constSCEV *RecastedMaxBECount =getTruncateOrZeroExtend(
1665 CastedMaxBECount, MaxBECount->getType(),Depth);
1666if (MaxBECount == RecastedMaxBECount) {
1667Type *WideTy =IntegerType::get(getContext(),BitWidth * 2);
1668// Check whether Start+Step*MaxBECount has no unsigned overflow.
1669constSCEV *ZMul =getMulExpr(CastedMaxBECount, Step,
1670SCEV::FlagAnyWrap,Depth + 1);
1671constSCEV *ZAdd =getZeroExtendExpr(getAddExpr(Start, ZMul,
1672SCEV::FlagAnyWrap,
1673Depth + 1),
1674 WideTy,Depth + 1);
1675constSCEV *WideStart =getZeroExtendExpr(Start, WideTy,Depth + 1);
1676constSCEV *WideMaxBECount =
1677getZeroExtendExpr(CastedMaxBECount, WideTy,Depth + 1);
1678constSCEV *OperandExtendedAdd =
1679getAddExpr(WideStart,
1680getMulExpr(WideMaxBECount,
1681getZeroExtendExpr(Step, WideTy,Depth + 1),
1682SCEV::FlagAnyWrap,Depth + 1),
1683SCEV::FlagAnyWrap,Depth + 1);
1684if (ZAdd == OperandExtendedAdd) {
1685// Cache knowledge of AR NUW, which is propagated to this AddRec.
1686setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),SCEV::FlagNUW);
1687// Return the expression with the addrec on the outside.
1688 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,this,
1689Depth + 1);
1690 Step =getZeroExtendExpr(Step, Ty,Depth + 1);
1691returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1692 }
1693// Similar to above, only this time treat the step value as signed.
1694// This covers loops that count down.
1695 OperandExtendedAdd =
1696getAddExpr(WideStart,
1697getMulExpr(WideMaxBECount,
1698getSignExtendExpr(Step, WideTy,Depth + 1),
1699SCEV::FlagAnyWrap,Depth + 1),
1700SCEV::FlagAnyWrap,Depth + 1);
1701if (ZAdd == OperandExtendedAdd) {
1702// Cache knowledge of AR NW, which is propagated to this AddRec.
1703// Negative step causes unsigned wrap, but it still can't self-wrap.
1704setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),SCEV::FlagNW);
1705// Return the expression with the addrec on the outside.
1706 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,this,
1707Depth + 1);
1708 Step =getSignExtendExpr(Step, Ty,Depth + 1);
1709returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1710 }
1711 }
1712 }
1713
1714// Normally, in the cases we can prove no-overflow via a
1715// backedge guarding condition, we can also compute a backedge
1716// taken count for the loop. The exceptions are assumptions and
1717// guards present in the loop -- SCEV is not great at exploiting
1718// these to compute max backedge taken counts, but can still use
1719// these to prove lack of overflow. Use this fact to avoid
1720// doing extra work that may not pay off.
1721if (!isa<SCEVCouldNotCompute>(MaxBECount) || HasGuards ||
1722 !AC.assumptions().empty()) {
1723
1724auto NewFlags = proveNoUnsignedWrapViaInduction(AR);
1725setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
1726if (AR->hasNoUnsignedWrap()) {
1727// Same as nuw case above - duplicated here to avoid a compile time
1728// issue. It's not clear that the order of checks does matter, but
1729// it's one of two issue possible causes for a change which was
1730// reverted. Be conservative for the moment.
1731 Start =
1732 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,this,Depth + 1);
1733 Step =getZeroExtendExpr(Step, Ty,Depth + 1);
1734returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1735 }
1736
1737// For a negative step, we can extend the operands iff doing so only
1738// traverses values in the range zext([0,UINT_MAX]).
1739if (isKnownNegative(Step)) {
1740constSCEV *N =getConstant(APInt::getMaxValue(BitWidth) -
1741getSignedRangeMin(Step));
1742if (isLoopBackedgeGuardedByCond(L,ICmpInst::ICMP_UGT, AR,N) ||
1743isKnownOnEveryIteration(ICmpInst::ICMP_UGT, AR,N)) {
1744// Cache knowledge of AR NW, which is propagated to this
1745// AddRec. Negative step causes unsigned wrap, but it
1746// still can't self-wrap.
1747setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),SCEV::FlagNW);
1748// Return the expression with the addrec on the outside.
1749 Start = getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,this,
1750Depth + 1);
1751 Step =getSignExtendExpr(Step, Ty,Depth + 1);
1752returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1753 }
1754 }
1755 }
1756
1757// zext({C,+,Step}) --> (zext(D) + zext({C-D,+,Step}))<nuw><nsw>
1758// if D + (C - D + Step * n) could be proven to not unsigned wrap
1759// where D maximizes the number of trailing zeros of (C - D + Step * n)
1760if (constauto *SC = dyn_cast<SCEVConstant>(Start)) {
1761constAPInt &C = SC->getAPInt();
1762constAPInt &D =extractConstantWithoutWrapping(*this,C, Step);
1763if (D != 0) {
1764constSCEV *SZExtD =getZeroExtendExpr(getConstant(D), Ty,Depth);
1765constSCEV *SResidual =
1766getAddRecExpr(getConstant(C -D), Step, L, AR->getNoWrapFlags());
1767constSCEV *SZExtR =getZeroExtendExpr(SResidual, Ty,Depth + 1);
1768returngetAddExpr(SZExtD, SZExtR,
1769 (SCEV::NoWrapFlags)(SCEV::FlagNSW |SCEV::FlagNUW),
1770Depth + 1);
1771 }
1772 }
1773
1774if (proveNoWrapByVaryingStart<SCEVZeroExtendExpr>(Start, Step, L)) {
1775setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),SCEV::FlagNUW);
1776 Start =
1777 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,this,Depth + 1);
1778 Step =getZeroExtendExpr(Step, Ty,Depth + 1);
1779returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
1780 }
1781 }
1782
1783// zext(A % B) --> zext(A) % zext(B)
1784 {
1785constSCEV *LHS;
1786constSCEV *RHS;
1787if (matchURem(Op,LHS,RHS))
1788returngetURemExpr(getZeroExtendExpr(LHS, Ty,Depth + 1),
1789getZeroExtendExpr(RHS, Ty,Depth + 1));
1790 }
1791
1792// zext(A / B) --> zext(A) / zext(B).
1793if (auto *Div = dyn_cast<SCEVUDivExpr>(Op))
1794returngetUDivExpr(getZeroExtendExpr(Div->getLHS(), Ty,Depth + 1),
1795getZeroExtendExpr(Div->getRHS(), Ty,Depth + 1));
1796
1797if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1798// zext((A + B + ...)<nuw>) --> (zext(A) + zext(B) + ...)<nuw>
1799if (SA->hasNoUnsignedWrap()) {
1800// If the addition does not unsign overflow then we can, by definition,
1801// commute the zero extension with the addition operation.
1802SmallVector<const SCEV *, 4> Ops;
1803for (constauto *Op : SA->operands())
1804 Ops.push_back(getZeroExtendExpr(Op, Ty,Depth + 1));
1805returngetAddExpr(Ops,SCEV::FlagNUW,Depth + 1);
1806 }
1807
1808// zext(C + x + y + ...) --> (zext(D) + zext((C - D) + x + y + ...))
1809// if D + (C - D + x + y + ...) could be proven to not unsigned wrap
1810// where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1811//
1812// Often address arithmetics contain expressions like
1813// (zext (add (shl X, C1), C2)), for instance, (zext (5 + (4 * X))).
1814// This transformation is useful while proving that such expressions are
1815// equal or differ by a small constant amount, see LoadStoreVectorizer pass.
1816if (constauto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1817constAPInt &D =extractConstantWithoutWrapping(*this, SC, SA);
1818if (D != 0) {
1819constSCEV *SZExtD =getZeroExtendExpr(getConstant(D), Ty,Depth);
1820constSCEV *SResidual =
1821getAddExpr(getConstant(-D), SA,SCEV::FlagAnyWrap,Depth);
1822constSCEV *SZExtR =getZeroExtendExpr(SResidual, Ty,Depth + 1);
1823returngetAddExpr(SZExtD, SZExtR,
1824 (SCEV::NoWrapFlags)(SCEV::FlagNSW |SCEV::FlagNUW),
1825Depth + 1);
1826 }
1827 }
1828 }
1829
1830if (auto *SM = dyn_cast<SCEVMulExpr>(Op)) {
1831// zext((A * B * ...)<nuw>) --> (zext(A) * zext(B) * ...)<nuw>
1832if (SM->hasNoUnsignedWrap()) {
1833// If the multiply does not unsign overflow then we can, by definition,
1834// commute the zero extension with the multiply operation.
1835SmallVector<const SCEV *, 4> Ops;
1836for (constauto *Op : SM->operands())
1837 Ops.push_back(getZeroExtendExpr(Op, Ty,Depth + 1));
1838returngetMulExpr(Ops,SCEV::FlagNUW,Depth + 1);
1839 }
1840
1841// zext(2^K * (trunc X to iN)) to iM ->
1842// 2^K * (zext(trunc X to i{N-K}) to iM)<nuw>
1843//
1844// Proof:
1845//
1846// zext(2^K * (trunc X to iN)) to iM
1847// = zext((trunc X to iN) << K) to iM
1848// = zext((trunc X to i{N-K}) << K)<nuw> to iM
1849// (because shl removes the top K bits)
1850// = zext((2^K * (trunc X to i{N-K}))<nuw>) to iM
1851// = (2^K * (zext(trunc X to i{N-K}) to iM))<nuw>.
1852//
1853if (SM->getNumOperands() == 2)
1854if (auto *MulLHS = dyn_cast<SCEVConstant>(SM->getOperand(0)))
1855if (MulLHS->getAPInt().isPowerOf2())
1856if (auto *TruncRHS = dyn_cast<SCEVTruncateExpr>(SM->getOperand(1))) {
1857int NewTruncBits =getTypeSizeInBits(TruncRHS->getType()) -
1858 MulLHS->getAPInt().logBase2();
1859Type *NewTruncTy =IntegerType::get(getContext(), NewTruncBits);
1860returngetMulExpr(
1861getZeroExtendExpr(MulLHS, Ty),
1862getZeroExtendExpr(
1863getTruncateExpr(TruncRHS->getOperand(), NewTruncTy), Ty),
1864SCEV::FlagNUW,Depth + 1);
1865 }
1866 }
1867
1868// zext(umin(x, y)) -> umin(zext(x), zext(y))
1869// zext(umax(x, y)) -> umax(zext(x), zext(y))
1870if (isa<SCEVUMinExpr>(Op) || isa<SCEVUMaxExpr>(Op)) {
1871auto *MinMax = cast<SCEVMinMaxExpr>(Op);
1872SmallVector<const SCEV *, 4>Operands;
1873for (auto *Operand :MinMax->operands())
1874Operands.push_back(getZeroExtendExpr(Operand, Ty));
1875if (isa<SCEVUMinExpr>(MinMax))
1876returngetUMinExpr(Operands);
1877returngetUMaxExpr(Operands);
1878 }
1879
1880// zext(umin_seq(x, y)) -> umin_seq(zext(x), zext(y))
1881if (auto *MinMax = dyn_cast<SCEVSequentialMinMaxExpr>(Op)) {
1882assert(isa<SCEVSequentialUMinExpr>(MinMax) &&"Not supported!");
1883SmallVector<const SCEV *, 4>Operands;
1884for (auto *Operand :MinMax->operands())
1885Operands.push_back(getZeroExtendExpr(Operand, Ty));
1886returngetUMinExpr(Operands,/*Sequential*/true);
1887 }
1888
1889// The cast wasn't folded; create an explicit cast node.
1890// Recompute the insert position, as it may have been invalidated.
1891if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))return S;
1892SCEV *S =new (SCEVAllocator)SCEVZeroExtendExpr(ID.Intern(SCEVAllocator),
1893Op, Ty);
1894 UniqueSCEVs.InsertNode(S, IP);
1895registerUser(S,Op);
1896return S;
1897}
1898
1899constSCEV *
1900ScalarEvolution::getSignExtendExpr(constSCEV *Op,Type *Ty,unsignedDepth) {
1901assert(getTypeSizeInBits(Op->getType()) <getTypeSizeInBits(Ty) &&
1902"This is not an extending conversion!");
1903assert(isSCEVable(Ty) &&
1904"This is not a conversion to a SCEVable type!");
1905assert(!Op->getType()->isPointerTy() &&"Can't extend pointer!");
1906 Ty =getEffectiveSCEVType(Ty);
1907
1908FoldIDID(scSignExtend,Op, Ty);
1909auto Iter = FoldCache.find(ID);
1910if (Iter != FoldCache.end())
1911return Iter->second;
1912
1913constSCEV *S =getSignExtendExprImpl(Op, Ty,Depth);
1914if (!isa<SCEVSignExtendExpr>(S))
1915insertFoldCacheEntry(ID, S, FoldCache, FoldCacheUser);
1916return S;
1917}
1918
1919constSCEV *ScalarEvolution::getSignExtendExprImpl(constSCEV *Op,Type *Ty,
1920unsignedDepth) {
1921assert(getTypeSizeInBits(Op->getType()) <getTypeSizeInBits(Ty) &&
1922"This is not an extending conversion!");
1923assert(isSCEVable(Ty) &&"This is not a conversion to a SCEVable type!");
1924assert(!Op->getType()->isPointerTy() &&"Can't extend pointer!");
1925 Ty =getEffectiveSCEVType(Ty);
1926
1927// Fold if the operand is constant.
1928if (constSCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
1929returngetConstant(SC->getAPInt().sext(getTypeSizeInBits(Ty)));
1930
1931// sext(sext(x)) --> sext(x)
1932if (constSCEVSignExtendExpr *SS = dyn_cast<SCEVSignExtendExpr>(Op))
1933returngetSignExtendExpr(SS->getOperand(), Ty,Depth + 1);
1934
1935// sext(zext(x)) --> zext(x)
1936if (constSCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
1937returngetZeroExtendExpr(SZ->getOperand(), Ty,Depth + 1);
1938
1939// Before doing any expensive analysis, check to see if we've already
1940// computed a SCEV for this Op and Ty.
1941FoldingSetNodeIDID;
1942ID.AddInteger(scSignExtend);
1943ID.AddPointer(Op);
1944ID.AddPointer(Ty);
1945void *IP =nullptr;
1946if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))return S;
1947// Limit recursion depth.
1948if (Depth >MaxCastDepth) {
1949SCEV *S =new (SCEVAllocator)SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
1950Op, Ty);
1951 UniqueSCEVs.InsertNode(S, IP);
1952registerUser(S,Op);
1953return S;
1954 }
1955
1956// sext(trunc(x)) --> sext(x) or x or trunc(x)
1957if (constSCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
1958// It's possible the bits taken off by the truncate were all sign bits. If
1959// so, we should be able to simplify this further.
1960constSCEV *X = ST->getOperand();
1961ConstantRange CR =getSignedRange(X);
1962unsigned TruncBits =getTypeSizeInBits(ST->getType());
1963unsigned NewBits =getTypeSizeInBits(Ty);
1964if (CR.truncate(TruncBits).signExtend(NewBits).contains(
1965 CR.sextOrTrunc(NewBits)))
1966returngetTruncateOrSignExtend(X, Ty,Depth);
1967 }
1968
1969if (auto *SA = dyn_cast<SCEVAddExpr>(Op)) {
1970// sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
1971if (SA->hasNoSignedWrap()) {
1972// If the addition does not sign overflow then we can, by definition,
1973// commute the sign extension with the addition operation.
1974SmallVector<const SCEV *, 4> Ops;
1975for (constauto *Op : SA->operands())
1976 Ops.push_back(getSignExtendExpr(Op, Ty,Depth + 1));
1977returngetAddExpr(Ops,SCEV::FlagNSW,Depth + 1);
1978 }
1979
1980// sext(C + x + y + ...) --> (sext(D) + sext((C - D) + x + y + ...))
1981// if D + (C - D + x + y + ...) could be proven to not signed wrap
1982// where D maximizes the number of trailing zeros of (C - D + x + y + ...)
1983//
1984// For instance, this will bring two seemingly different expressions:
1985// 1 + sext(5 + 20 * %x + 24 * %y) and
1986// sext(6 + 20 * %x + 24 * %y)
1987// to the same form:
1988// 2 + sext(4 + 20 * %x + 24 * %y)
1989if (constauto *SC = dyn_cast<SCEVConstant>(SA->getOperand(0))) {
1990constAPInt &D =extractConstantWithoutWrapping(*this, SC, SA);
1991if (D != 0) {
1992constSCEV *SSExtD =getSignExtendExpr(getConstant(D), Ty,Depth);
1993constSCEV *SResidual =
1994getAddExpr(getConstant(-D), SA,SCEV::FlagAnyWrap,Depth);
1995constSCEV *SSExtR =getSignExtendExpr(SResidual, Ty,Depth + 1);
1996returngetAddExpr(SSExtD, SSExtR,
1997 (SCEV::NoWrapFlags)(SCEV::FlagNSW |SCEV::FlagNUW),
1998Depth + 1);
1999 }
2000 }
2001 }
2002// If the input value is a chrec scev, and we can prove that the value
2003// did not overflow the old, smaller, value, we can sign extend all of the
2004// operands (often constants). This allows analysis of something like
2005// this: for (signed char X = 0; X < 100; ++X) { int Y = X; }
2006if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op))
2007if (AR->isAffine()) {
2008constSCEV *Start = AR->getStart();
2009constSCEV *Step = AR->getStepRecurrence(*this);
2010unsignedBitWidth =getTypeSizeInBits(AR->getType());
2011constLoop *L = AR->getLoop();
2012
2013// If we have special knowledge that this addrec won't overflow,
2014// we don't need to do any further analysis.
2015if (AR->hasNoSignedWrap()) {
2016 Start =
2017 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,this,Depth + 1);
2018 Step =getSignExtendExpr(Step, Ty,Depth + 1);
2019returngetAddRecExpr(Start, Step, L,SCEV::FlagNSW);
2020 }
2021
2022// Check whether the backedge-taken count is SCEVCouldNotCompute.
2023// Note that this serves two purposes: It filters out loops that are
2024// simply not analyzable, and it covers the case where this code is
2025// being called from within backedge-taken count analysis, such that
2026// attempting to ask for the backedge-taken count would likely result
2027// in infinite recursion. In the later case, the analysis code will
2028// cope with a conservative value, and it will take care to purge
2029// that value once it has finished.
2030constSCEV *MaxBECount =getConstantMaxBackedgeTakenCount(L);
2031if (!isa<SCEVCouldNotCompute>(MaxBECount)) {
2032// Manually compute the final value for AR, checking for
2033// overflow.
2034
2035// Check whether the backedge-taken count can be losslessly casted to
2036// the addrec's type. The count is always unsigned.
2037constSCEV *CastedMaxBECount =
2038getTruncateOrZeroExtend(MaxBECount, Start->getType(),Depth);
2039constSCEV *RecastedMaxBECount =getTruncateOrZeroExtend(
2040 CastedMaxBECount, MaxBECount->getType(),Depth);
2041if (MaxBECount == RecastedMaxBECount) {
2042Type *WideTy =IntegerType::get(getContext(),BitWidth * 2);
2043// Check whether Start+Step*MaxBECount has no signed overflow.
2044constSCEV *SMul =getMulExpr(CastedMaxBECount, Step,
2045SCEV::FlagAnyWrap,Depth + 1);
2046constSCEV *SAdd =getSignExtendExpr(getAddExpr(Start, SMul,
2047SCEV::FlagAnyWrap,
2048Depth + 1),
2049 WideTy,Depth + 1);
2050constSCEV *WideStart =getSignExtendExpr(Start, WideTy,Depth + 1);
2051constSCEV *WideMaxBECount =
2052getZeroExtendExpr(CastedMaxBECount, WideTy,Depth + 1);
2053constSCEV *OperandExtendedAdd =
2054getAddExpr(WideStart,
2055getMulExpr(WideMaxBECount,
2056getSignExtendExpr(Step, WideTy,Depth + 1),
2057SCEV::FlagAnyWrap,Depth + 1),
2058SCEV::FlagAnyWrap,Depth + 1);
2059if (SAdd == OperandExtendedAdd) {
2060// Cache knowledge of AR NSW, which is propagated to this AddRec.
2061setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),SCEV::FlagNSW);
2062// Return the expression with the addrec on the outside.
2063 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,this,
2064Depth + 1);
2065 Step =getSignExtendExpr(Step, Ty,Depth + 1);
2066returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2067 }
2068// Similar to above, only this time treat the step value as unsigned.
2069// This covers loops that count up with an unsigned step.
2070 OperandExtendedAdd =
2071getAddExpr(WideStart,
2072getMulExpr(WideMaxBECount,
2073getZeroExtendExpr(Step, WideTy,Depth + 1),
2074SCEV::FlagAnyWrap,Depth + 1),
2075SCEV::FlagAnyWrap,Depth + 1);
2076if (SAdd == OperandExtendedAdd) {
2077// If AR wraps around then
2078//
2079// abs(Step) * MaxBECount > unsigned-max(AR->getType())
2080// => SAdd != OperandExtendedAdd
2081//
2082// Thus (AR is not NW => SAdd != OperandExtendedAdd) <=>
2083// (SAdd == OperandExtendedAdd => AR is NW)
2084
2085setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),SCEV::FlagNW);
2086
2087// Return the expression with the addrec on the outside.
2088 Start = getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,this,
2089Depth + 1);
2090 Step =getZeroExtendExpr(Step, Ty,Depth + 1);
2091returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2092 }
2093 }
2094 }
2095
2096auto NewFlags = proveNoSignedWrapViaInduction(AR);
2097setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), NewFlags);
2098if (AR->hasNoSignedWrap()) {
2099// Same as nsw case above - duplicated here to avoid a compile time
2100// issue. It's not clear that the order of checks does matter, but
2101// it's one of two issue possible causes for a change which was
2102// reverted. Be conservative for the moment.
2103 Start =
2104 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,this,Depth + 1);
2105 Step =getSignExtendExpr(Step, Ty,Depth + 1);
2106returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2107 }
2108
2109// sext({C,+,Step}) --> (sext(D) + sext({C-D,+,Step}))<nuw><nsw>
2110// if D + (C - D + Step * n) could be proven to not signed wrap
2111// where D maximizes the number of trailing zeros of (C - D + Step * n)
2112if (constauto *SC = dyn_cast<SCEVConstant>(Start)) {
2113constAPInt &C = SC->getAPInt();
2114constAPInt &D =extractConstantWithoutWrapping(*this,C, Step);
2115if (D != 0) {
2116constSCEV *SSExtD =getSignExtendExpr(getConstant(D), Ty,Depth);
2117constSCEV *SResidual =
2118getAddRecExpr(getConstant(C -D), Step, L, AR->getNoWrapFlags());
2119constSCEV *SSExtR =getSignExtendExpr(SResidual, Ty,Depth + 1);
2120returngetAddExpr(SSExtD, SSExtR,
2121 (SCEV::NoWrapFlags)(SCEV::FlagNSW |SCEV::FlagNUW),
2122Depth + 1);
2123 }
2124 }
2125
2126if (proveNoWrapByVaryingStart<SCEVSignExtendExpr>(Start, Step, L)) {
2127setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),SCEV::FlagNSW);
2128 Start =
2129 getExtendAddRecStart<SCEVSignExtendExpr>(AR, Ty,this,Depth + 1);
2130 Step =getSignExtendExpr(Step, Ty,Depth + 1);
2131returngetAddRecExpr(Start, Step, L, AR->getNoWrapFlags());
2132 }
2133 }
2134
2135// If the input value is provably positive and we could not simplify
2136// away the sext build a zext instead.
2137if (isKnownNonNegative(Op))
2138returngetZeroExtendExpr(Op, Ty,Depth + 1);
2139
2140// sext(smin(x, y)) -> smin(sext(x), sext(y))
2141// sext(smax(x, y)) -> smax(sext(x), sext(y))
2142if (isa<SCEVSMinExpr>(Op) || isa<SCEVSMaxExpr>(Op)) {
2143auto *MinMax = cast<SCEVMinMaxExpr>(Op);
2144SmallVector<const SCEV *, 4>Operands;
2145for (auto *Operand :MinMax->operands())
2146Operands.push_back(getSignExtendExpr(Operand, Ty));
2147if (isa<SCEVSMinExpr>(MinMax))
2148returngetSMinExpr(Operands);
2149returngetSMaxExpr(Operands);
2150 }
2151
2152// The cast wasn't folded; create an explicit cast node.
2153// Recompute the insert position, as it may have been invalidated.
2154if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))return S;
2155SCEV *S =new (SCEVAllocator)SCEVSignExtendExpr(ID.Intern(SCEVAllocator),
2156Op, Ty);
2157 UniqueSCEVs.InsertNode(S, IP);
2158registerUser(S, {Op });
2159return S;
2160}
2161
2162constSCEV *ScalarEvolution::getCastExpr(SCEVTypes Kind,constSCEV *Op,
2163Type *Ty) {
2164switch (Kind) {
2165casescTruncate:
2166returngetTruncateExpr(Op, Ty);
2167casescZeroExtend:
2168returngetZeroExtendExpr(Op, Ty);
2169casescSignExtend:
2170returngetSignExtendExpr(Op, Ty);
2171casescPtrToInt:
2172returngetPtrToIntExpr(Op, Ty);
2173default:
2174llvm_unreachable("Not a SCEV cast expression!");
2175 }
2176}
2177
2178/// getAnyExtendExpr - Return a SCEV for the given operand extended with
2179/// unspecified bits out to the given type.
2180constSCEV *ScalarEvolution::getAnyExtendExpr(constSCEV *Op,
2181Type *Ty) {
2182assert(getTypeSizeInBits(Op->getType()) <getTypeSizeInBits(Ty) &&
2183"This is not an extending conversion!");
2184assert(isSCEVable(Ty) &&
2185"This is not a conversion to a SCEVable type!");
2186 Ty =getEffectiveSCEVType(Ty);
2187
2188// Sign-extend negative constants.
2189if (constSCEVConstant *SC = dyn_cast<SCEVConstant>(Op))
2190if (SC->getAPInt().isNegative())
2191returngetSignExtendExpr(Op, Ty);
2192
2193// Peel off a truncate cast.
2194if (constSCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
2195constSCEV *NewOp =T->getOperand();
2196if (getTypeSizeInBits(NewOp->getType()) <getTypeSizeInBits(Ty))
2197returngetAnyExtendExpr(NewOp, Ty);
2198returngetTruncateOrNoop(NewOp, Ty);
2199 }
2200
2201// Next try a zext cast. If the cast is folded, use it.
2202constSCEV *ZExt =getZeroExtendExpr(Op, Ty);
2203if (!isa<SCEVZeroExtendExpr>(ZExt))
2204return ZExt;
2205
2206// Next try a sext cast. If the cast is folded, use it.
2207constSCEV *SExt =getSignExtendExpr(Op, Ty);
2208if (!isa<SCEVSignExtendExpr>(SExt))
2209return SExt;
2210
2211// Force the cast to be folded into the operands of an addrec.
2212if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Op)) {
2213SmallVector<const SCEV *, 4> Ops;
2214for (constSCEV *Op : AR->operands())
2215 Ops.push_back(getAnyExtendExpr(Op, Ty));
2216returngetAddRecExpr(Ops, AR->getLoop(),SCEV::FlagNW);
2217 }
2218
2219// If the expression is obviously signed, use the sext cast value.
2220if (isa<SCEVSMaxExpr>(Op))
2221return SExt;
2222
2223// Absent any other information, use the zext cast value.
2224return ZExt;
2225}
2226
2227/// Process the given Ops list, which is a list of operands to be added under
2228/// the given scale, update the given map. This is a helper function for
2229/// getAddRecExpr. As an example of what it does, given a sequence of operands
2230/// that would form an add expression like this:
2231///
2232/// m + n + 13 + (A * (o + p + (B * (q + m + 29)))) + r + (-1 * r)
2233///
2234/// where A and B are constants, update the map with these values:
2235///
2236/// (m, 1+A*B), (n, 1), (o, A), (p, A), (q, A*B), (r, 0)
2237///
2238/// and add 13 + A*B*29 to AccumulatedConstant.
2239/// This will allow getAddRecExpr to produce this:
2240///
2241/// 13+A*B*29 + n + (m * (1+A*B)) + ((o + p) * A) + (q * A*B)
2242///
2243/// This form often exposes folding opportunities that are hidden in
2244/// the original operand list.
2245///
2246/// Return true iff it appears that any interesting folding opportunities
2247/// may be exposed. This helps getAddRecExpr short-circuit extra work in
2248/// the common case where no interesting opportunities are present, and
2249/// is also used as a check to avoid infinite recursion.
2250staticbool
2251CollectAddOperandsWithScales(SmallDenseMap<const SCEV *, APInt, 16> &M,
2252SmallVectorImpl<const SCEV *> &NewOps,
2253APInt &AccumulatedConstant,
2254ArrayRef<const SCEV *> Ops,constAPInt &Scale,
2255ScalarEvolution &SE) {
2256bool Interesting =false;
2257
2258// Iterate over the add operands. They are sorted, with constants first.
2259unsigned i = 0;
2260while (constSCEVConstant *C = dyn_cast<SCEVConstant>(Ops[i])) {
2261 ++i;
2262// Pull a buried constant out to the outside.
2263if (Scale != 1 || AccumulatedConstant != 0 ||C->getValue()->isZero())
2264 Interesting =true;
2265 AccumulatedConstant += Scale *C->getAPInt();
2266 }
2267
2268// Next comes everything else. We're especially interested in multiplies
2269// here, but they're in the middle, so just visit the rest with one loop.
2270for (; i != Ops.size(); ++i) {
2271constSCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[i]);
2272if (Mul && isa<SCEVConstant>(Mul->getOperand(0))) {
2273APInt NewScale =
2274 Scale * cast<SCEVConstant>(Mul->getOperand(0))->getAPInt();
2275if (Mul->getNumOperands() == 2 && isa<SCEVAddExpr>(Mul->getOperand(1))) {
2276// A multiplication of a constant with another add; recurse.
2277constSCEVAddExpr *Add = cast<SCEVAddExpr>(Mul->getOperand(1));
2278 Interesting |=
2279CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2280Add->operands(), NewScale, SE);
2281 }else {
2282// A multiplication of a constant with some other value. Update
2283// the map.
2284SmallVector<const SCEV *, 4> MulOps(drop_begin(Mul->operands()));
2285constSCEV *Key = SE.getMulExpr(MulOps);
2286auto Pair = M.insert({Key, NewScale});
2287if (Pair.second) {
2288 NewOps.push_back(Pair.first->first);
2289 }else {
2290 Pair.first->second += NewScale;
2291// The map already had an entry for this value, which may indicate
2292// a folding opportunity.
2293 Interesting =true;
2294 }
2295 }
2296 }else {
2297// An ordinary operand. Update the map.
2298 std::pair<DenseMap<const SCEV *, APInt>::iterator,bool> Pair =
2299 M.insert({Ops[i], Scale});
2300if (Pair.second) {
2301 NewOps.push_back(Pair.first->first);
2302 }else {
2303 Pair.first->second += Scale;
2304// The map already had an entry for this value, which may indicate
2305// a folding opportunity.
2306 Interesting =true;
2307 }
2308 }
2309 }
2310
2311return Interesting;
2312}
2313
2314boolScalarEvolution::willNotOverflow(Instruction::BinaryOps BinOp,boolSigned,
2315constSCEV *LHS,constSCEV *RHS,
2316constInstruction *CtxI) {
2317constSCEV *(ScalarEvolution::*Operation)(constSCEV *,constSCEV *,
2318SCEV::NoWrapFlags,unsigned);
2319switch (BinOp) {
2320default:
2321llvm_unreachable("Unsupported binary op");
2322case Instruction::Add:
2323Operation = &ScalarEvolution::getAddExpr;
2324break;
2325case Instruction::Sub:
2326Operation = &ScalarEvolution::getMinusSCEV;
2327break;
2328case Instruction::Mul:
2329Operation = &ScalarEvolution::getMulExpr;
2330break;
2331 }
2332
2333constSCEV *(ScalarEvolution::*Extension)(constSCEV *,Type *,unsigned) =
2334Signed ? &ScalarEvolution::getSignExtendExpr
2335 : &ScalarEvolution::getZeroExtendExpr;
2336
2337// Check ext(LHS op RHS) == ext(LHS) op ext(RHS)
2338auto *NarrowTy = cast<IntegerType>(LHS->getType());
2339auto *WideTy =
2340IntegerType::get(NarrowTy->getContext(), NarrowTy->getBitWidth() * 2);
2341
2342constSCEV *A = (this->*Extension)(
2343 (this->*Operation)(LHS,RHS,SCEV::FlagAnyWrap, 0), WideTy, 0);
2344constSCEV *LHSB = (this->*Extension)(LHS, WideTy, 0);
2345constSCEV *RHSB = (this->*Extension)(RHS, WideTy, 0);
2346constSCEV *B = (this->*Operation)(LHSB, RHSB,SCEV::FlagAnyWrap, 0);
2347if (A ==B)
2348returntrue;
2349// Can we use context to prove the fact we need?
2350if (!CtxI)
2351returnfalse;
2352// TODO: Support mul.
2353if (BinOp == Instruction::Mul)
2354returnfalse;
2355auto *RHSC = dyn_cast<SCEVConstant>(RHS);
2356// TODO: Lift this limitation.
2357if (!RHSC)
2358returnfalse;
2359APIntC = RHSC->getAPInt();
2360unsigned NumBits =C.getBitWidth();
2361bool IsSub = (BinOp == Instruction::Sub);
2362bool IsNegativeConst = (Signed &&C.isNegative());
2363// Compute the direction and magnitude by which we need to check overflow.
2364bool OverflowDown = IsSub ^ IsNegativeConst;
2365APInt Magnitude =C;
2366if (IsNegativeConst) {
2367if (C ==APInt::getSignedMinValue(NumBits))
2368// TODO: SINT_MIN on inversion gives the same negative value, we don't
2369// want to deal with that.
2370returnfalse;
2371 Magnitude = -C;
2372 }
2373
2374ICmpInst::Predicate Pred =Signed ?ICmpInst::ICMP_SLE :ICmpInst::ICMP_ULE;
2375if (OverflowDown) {
2376// To avoid overflow down, we need to make sure that MIN + Magnitude <= LHS.
2377APInt Min =Signed ?APInt::getSignedMinValue(NumBits)
2378 :APInt::getMinValue(NumBits);
2379APInt Limit = Min + Magnitude;
2380returnisKnownPredicateAt(Pred,getConstant(Limit),LHS, CtxI);
2381 }else {
2382// To avoid overflow up, we need to make sure that LHS <= MAX - Magnitude.
2383APInt Max =Signed ?APInt::getSignedMaxValue(NumBits)
2384 :APInt::getMaxValue(NumBits);
2385APInt Limit = Max - Magnitude;
2386returnisKnownPredicateAt(Pred,LHS,getConstant(Limit), CtxI);
2387 }
2388}
2389
2390std::optional<SCEV::NoWrapFlags>
2391ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp(
2392constOverflowingBinaryOperator *OBO) {
2393// It cannot be done any better.
2394if (OBO->hasNoUnsignedWrap() && OBO->hasNoSignedWrap())
2395return std::nullopt;
2396
2397SCEV::NoWrapFlags Flags =SCEV::NoWrapFlags::FlagAnyWrap;
2398
2399if (OBO->hasNoUnsignedWrap())
2400 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNUW);
2401if (OBO->hasNoSignedWrap())
2402 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNSW);
2403
2404bool Deduced =false;
2405
2406if (OBO->getOpcode() != Instruction::Add &&
2407 OBO->getOpcode() != Instruction::Sub &&
2408 OBO->getOpcode() != Instruction::Mul)
2409return std::nullopt;
2410
2411constSCEV *LHS =getSCEV(OBO->getOperand(0));
2412constSCEV *RHS =getSCEV(OBO->getOperand(1));
2413
2414constInstruction *CtxI =
2415UseContextForNoWrapFlagInference ? dyn_cast<Instruction>(OBO) :nullptr;
2416if (!OBO->hasNoUnsignedWrap() &&
2417willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(),
2418/* Signed */false,LHS,RHS, CtxI)) {
2419 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNUW);
2420 Deduced =true;
2421 }
2422
2423if (!OBO->hasNoSignedWrap() &&
2424willNotOverflow((Instruction::BinaryOps)OBO->getOpcode(),
2425/* Signed */true,LHS,RHS, CtxI)) {
2426 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNSW);
2427 Deduced =true;
2428 }
2429
2430if (Deduced)
2431return Flags;
2432return std::nullopt;
2433}
2434
2435// We're trying to construct a SCEV of type `Type' with `Ops' as operands and
2436// `OldFlags' as can't-wrap behavior. Infer a more aggressive set of
2437// can't-overflow flags for the operation if possible.
2438staticSCEV::NoWrapFlags
2439StrengthenNoWrapFlags(ScalarEvolution *SE,SCEVTypesType,
2440constArrayRef<const SCEV *> Ops,
2441SCEV::NoWrapFlags Flags) {
2442using namespacestd::placeholders;
2443
2444usingOBO =OverflowingBinaryOperator;
2445
2446bool CanAnalyze =
2447Type ==scAddExpr ||Type ==scAddRecExpr ||Type ==scMulExpr;
2448 (void)CanAnalyze;
2449assert(CanAnalyze &&"don't call from other places!");
2450
2451int SignOrUnsignMask =SCEV::FlagNUW |SCEV::FlagNSW;
2452SCEV::NoWrapFlags SignOrUnsignWrap =
2453ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2454
2455// If FlagNSW is true and all the operands are non-negative, infer FlagNUW.
2456auto IsKnownNonNegative = [&](constSCEV *S) {
2457return SE->isKnownNonNegative(S);
2458 };
2459
2460if (SignOrUnsignWrap ==SCEV::FlagNSW &&all_of(Ops, IsKnownNonNegative))
2461 Flags =
2462ScalarEvolution::setFlags(Flags, (SCEV::NoWrapFlags)SignOrUnsignMask);
2463
2464 SignOrUnsignWrap =ScalarEvolution::maskFlags(Flags, SignOrUnsignMask);
2465
2466if (SignOrUnsignWrap != SignOrUnsignMask &&
2467 (Type ==scAddExpr ||Type ==scMulExpr) && Ops.size() == 2 &&
2468 isa<SCEVConstant>(Ops[0])) {
2469
2470auto Opcode = [&] {
2471switch (Type) {
2472casescAddExpr:
2473return Instruction::Add;
2474casescMulExpr:
2475return Instruction::Mul;
2476default:
2477llvm_unreachable("Unexpected SCEV op.");
2478 }
2479 }();
2480
2481constAPInt &C = cast<SCEVConstant>(Ops[0])->getAPInt();
2482
2483// (A <opcode> C) --> (A <opcode> C)<nsw> if the op doesn't sign overflow.
2484if (!(SignOrUnsignWrap &SCEV::FlagNSW)) {
2485auto NSWRegion =ConstantRange::makeGuaranteedNoWrapRegion(
2486 Opcode,C, OBO::NoSignedWrap);
2487if (NSWRegion.contains(SE->getSignedRange(Ops[1])))
2488 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNSW);
2489 }
2490
2491// (A <opcode> C) --> (A <opcode> C)<nuw> if the op doesn't unsign overflow.
2492if (!(SignOrUnsignWrap &SCEV::FlagNUW)) {
2493auto NUWRegion =ConstantRange::makeGuaranteedNoWrapRegion(
2494 Opcode,C, OBO::NoUnsignedWrap);
2495if (NUWRegion.contains(SE->getUnsignedRange(Ops[1])))
2496 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNUW);
2497 }
2498 }
2499
2500// <0,+,nonnegative><nw> is also nuw
2501// TODO: Add corresponding nsw case
2502if (Type ==scAddRecExpr &&ScalarEvolution::hasFlags(Flags,SCEV::FlagNW) &&
2503 !ScalarEvolution::hasFlags(Flags,SCEV::FlagNUW) && Ops.size() == 2 &&
2504 Ops[0]->isZero() && IsKnownNonNegative(Ops[1]))
2505 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNUW);
2506
2507// both (udiv X, Y) * Y and Y * (udiv X, Y) are always NUW
2508if (Type ==scMulExpr && !ScalarEvolution::hasFlags(Flags,SCEV::FlagNUW) &&
2509 Ops.size() == 2) {
2510if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[0]))
2511if (UDiv->getOperand(1) == Ops[1])
2512 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNUW);
2513if (auto *UDiv = dyn_cast<SCEVUDivExpr>(Ops[1]))
2514if (UDiv->getOperand(1) == Ops[0])
2515 Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNUW);
2516 }
2517
2518return Flags;
2519}
2520
2521boolScalarEvolution::isAvailableAtLoopEntry(constSCEV *S,constLoop *L) {
2522returnisLoopInvariant(S, L) &&properlyDominates(S, L->getHeader());
2523}
2524
2525/// Get a canonical add expression, or something simpler if possible.
2526constSCEV *ScalarEvolution::getAddExpr(SmallVectorImpl<const SCEV *> &Ops,
2527SCEV::NoWrapFlags OrigFlags,
2528unsignedDepth) {
2529assert(!(OrigFlags & ~(SCEV::FlagNUW |SCEV::FlagNSW)) &&
2530"only nuw or nsw allowed");
2531assert(!Ops.empty() &&"Cannot get empty add!");
2532if (Ops.size() == 1)return Ops[0];
2533#ifndef NDEBUG
2534Type *ETy =getEffectiveSCEVType(Ops[0]->getType());
2535for (unsigned i = 1, e = Ops.size(); i != e; ++i)
2536assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
2537"SCEVAddExpr operand types don't match!");
2538unsigned NumPtrs =count_if(
2539 Ops, [](constSCEV *Op) {returnOp->getType()->isPointerTy(); });
2540assert(NumPtrs <= 1 &&"add has at most one pointer operand");
2541#endif
2542
2543constSCEV *Folded =constantFoldAndGroupOps(
2544 *this, LI, DT, Ops,
2545 [](constAPInt &C1,constAPInt &C2) {return C1 + C2; },
2546 [](constAPInt &C) {returnC.isZero(); },// identity
2547 [](constAPInt &C) {returnfalse; });// absorber
2548if (Folded)
2549return Folded;
2550
2551unsignedIdx = isa<SCEVConstant>(Ops[0]) ? 1 : 0;
2552
2553// Delay expensive flag strengthening until necessary.
2554auto ComputeFlags = [this, OrigFlags](constArrayRef<const SCEV *> Ops) {
2555returnStrengthenNoWrapFlags(this,scAddExpr, Ops, OrigFlags);
2556 };
2557
2558// Limit recursion calls depth.
2559if (Depth >MaxArithDepth ||hasHugeExpression(Ops))
2560return getOrCreateAddExpr(Ops, ComputeFlags(Ops));
2561
2562if (SCEV *S = findExistingSCEVInCache(scAddExpr, Ops)) {
2563// Don't strengthen flags if we have no new information.
2564SCEVAddExpr *Add =static_cast<SCEVAddExpr *>(S);
2565if (Add->getNoWrapFlags(OrigFlags) != OrigFlags)
2566Add->setNoWrapFlags(ComputeFlags(Ops));
2567return S;
2568 }
2569
2570// Okay, check to see if the same value occurs in the operand list more than
2571// once. If so, merge them together into an multiply expression. Since we
2572// sorted the list, these values are required to be adjacent.
2573Type *Ty = Ops[0]->getType();
2574bool FoundMatch =false;
2575for (unsigned i = 0, e = Ops.size(); i != e-1; ++i)
2576if (Ops[i] == Ops[i+1]) {// X + Y + Y --> X + Y*2
2577// Scan ahead to count how many equal operands there are.
2578unsigned Count = 2;
2579while (i+Count != e && Ops[i+Count] == Ops[i])
2580 ++Count;
2581// Merge the values into a multiply.
2582constSCEV *Scale =getConstant(Ty, Count);
2583constSCEV *Mul =getMulExpr(Scale, Ops[i],SCEV::FlagAnyWrap,Depth + 1);
2584if (Ops.size() == Count)
2585returnMul;
2586 Ops[i] =Mul;
2587 Ops.erase(Ops.begin()+i+1, Ops.begin()+i+Count);
2588 --i; e -= Count - 1;
2589 FoundMatch =true;
2590 }
2591if (FoundMatch)
2592returngetAddExpr(Ops, OrigFlags,Depth + 1);
2593
2594// Check for truncates. If all the operands are truncated from the same
2595// type, see if factoring out the truncate would permit the result to be
2596// folded. eg., n*trunc(x) + m*trunc(y) --> trunc(trunc(m)*x + trunc(n)*y)
2597// if the contents of the resulting outer trunc fold to something simple.
2598auto FindTruncSrcType = [&]() ->Type * {
2599// We're ultimately looking to fold an addrec of truncs and muls of only
2600// constants and truncs, so if we find any other types of SCEV
2601// as operands of the addrec then we bail and return nullptr here.
2602// Otherwise, we return the type of the operand of a trunc that we find.
2603if (auto *T = dyn_cast<SCEVTruncateExpr>(Ops[Idx]))
2604returnT->getOperand()->getType();
2605if (constauto *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
2606constauto *LastOp =Mul->getOperand(Mul->getNumOperands() - 1);
2607if (constauto *T = dyn_cast<SCEVTruncateExpr>(LastOp))
2608returnT->getOperand()->getType();
2609 }
2610returnnullptr;
2611 };
2612if (auto *SrcType = FindTruncSrcType()) {
2613SmallVector<const SCEV *, 8> LargeOps;
2614bool Ok =true;
2615// Check all the operands to see if they can be represented in the
2616// source type of the truncate.
2617for (constSCEV *Op : Ops) {
2618if (constSCEVTruncateExpr *T = dyn_cast<SCEVTruncateExpr>(Op)) {
2619if (T->getOperand()->getType() != SrcType) {
2620 Ok =false;
2621break;
2622 }
2623 LargeOps.push_back(T->getOperand());
2624 }elseif (constSCEVConstant *C = dyn_cast<SCEVConstant>(Op)) {
2625 LargeOps.push_back(getAnyExtendExpr(C, SrcType));
2626 }elseif (constSCEVMulExpr *M = dyn_cast<SCEVMulExpr>(Op)) {
2627SmallVector<const SCEV *, 8> LargeMulOps;
2628for (unsigned j = 0, f = M->getNumOperands(); j != f && Ok; ++j) {
2629if (constSCEVTruncateExpr *T =
2630 dyn_cast<SCEVTruncateExpr>(M->getOperand(j))) {
2631if (T->getOperand()->getType() != SrcType) {
2632 Ok =false;
2633break;
2634 }
2635 LargeMulOps.push_back(T->getOperand());
2636 }elseif (constauto *C = dyn_cast<SCEVConstant>(M->getOperand(j))) {
2637 LargeMulOps.push_back(getAnyExtendExpr(C, SrcType));
2638 }else {
2639 Ok =false;
2640break;
2641 }
2642 }
2643if (Ok)
2644 LargeOps.push_back(getMulExpr(LargeMulOps,SCEV::FlagAnyWrap,Depth + 1));
2645 }else {
2646 Ok =false;
2647break;
2648 }
2649 }
2650if (Ok) {
2651// Evaluate the expression in the larger type.
2652constSCEV *Fold =getAddExpr(LargeOps,SCEV::FlagAnyWrap,Depth + 1);
2653// If it folds to something simple, use it. Otherwise, don't.
2654if (isa<SCEVConstant>(Fold) || isa<SCEVUnknown>(Fold))
2655returngetTruncateExpr(Fold, Ty);
2656 }
2657 }
2658
2659if (Ops.size() == 2) {
2660// Check if we have an expression of the form ((X + C1) - C2), where C1 and
2661// C2 can be folded in a way that allows retaining wrapping flags of (X +
2662// C1).
2663constSCEV *A = Ops[0];
2664constSCEV *B = Ops[1];
2665auto *AddExpr = dyn_cast<SCEVAddExpr>(B);
2666auto *C = dyn_cast<SCEVConstant>(A);
2667if (AddExpr &&C && isa<SCEVConstant>(AddExpr->getOperand(0))) {
2668auto C1 = cast<SCEVConstant>(AddExpr->getOperand(0))->getAPInt();
2669auto C2 =C->getAPInt();
2670SCEV::NoWrapFlags PreservedFlags =SCEV::FlagAnyWrap;
2671
2672APInt ConstAdd = C1 + C2;
2673auto AddFlags = AddExpr->getNoWrapFlags();
2674// Adding a smaller constant is NUW if the original AddExpr was NUW.
2675if (ScalarEvolution::hasFlags(AddFlags,SCEV::FlagNUW) &&
2676 ConstAdd.ule(C1)) {
2677 PreservedFlags =
2678ScalarEvolution::setFlags(PreservedFlags,SCEV::FlagNUW);
2679 }
2680
2681// Adding a constant with the same sign and small magnitude is NSW, if the
2682// original AddExpr was NSW.
2683if (ScalarEvolution::hasFlags(AddFlags,SCEV::FlagNSW) &&
2684 C1.isSignBitSet() == ConstAdd.isSignBitSet() &&
2685 ConstAdd.abs().ule(C1.abs())) {
2686 PreservedFlags =
2687ScalarEvolution::setFlags(PreservedFlags,SCEV::FlagNSW);
2688 }
2689
2690if (PreservedFlags !=SCEV::FlagAnyWrap) {
2691SmallVector<const SCEV *, 4> NewOps(AddExpr->operands());
2692 NewOps[0] =getConstant(ConstAdd);
2693returngetAddExpr(NewOps, PreservedFlags);
2694 }
2695 }
2696 }
2697
2698// Canonicalize (-1 * urem X, Y) + X --> (Y * X/Y)
2699if (Ops.size() == 2) {
2700constSCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[0]);
2701if (Mul &&Mul->getNumOperands() == 2 &&
2702Mul->getOperand(0)->isAllOnesValue()) {
2703constSCEV *X;
2704constSCEV *Y;
2705if (matchURem(Mul->getOperand(1),X,Y) &&X == Ops[1]) {
2706returngetMulExpr(Y,getUDivExpr(X,Y));
2707 }
2708 }
2709 }
2710
2711// Skip past any other cast SCEVs.
2712while (Idx < Ops.size() && Ops[Idx]->getSCEVType() <scAddExpr)
2713 ++Idx;
2714
2715// If there are add operands they would be next.
2716if (Idx < Ops.size()) {
2717bool DeletedAdd =false;
2718// If the original flags and all inlined SCEVAddExprs are NUW, use the
2719// common NUW flag for expression after inlining. Other flags cannot be
2720// preserved, because they may depend on the original order of operations.
2721SCEV::NoWrapFlags CommonFlags =maskFlags(OrigFlags,SCEV::FlagNUW);
2722while (constSCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[Idx])) {
2723if (Ops.size() >AddOpsInlineThreshold ||
2724Add->getNumOperands() >AddOpsInlineThreshold)
2725break;
2726// If we have an add, expand the add operands onto the end of the operands
2727// list.
2728 Ops.erase(Ops.begin()+Idx);
2729append_range(Ops,Add->operands());
2730 DeletedAdd =true;
2731 CommonFlags =maskFlags(CommonFlags,Add->getNoWrapFlags());
2732 }
2733
2734// If we deleted at least one add, we added operands to the end of the list,
2735// and they are not necessarily sorted. Recurse to resort and resimplify
2736// any operands we just acquired.
2737if (DeletedAdd)
2738returngetAddExpr(Ops, CommonFlags,Depth + 1);
2739 }
2740
2741// Skip over the add expression until we get to a multiply.
2742while (Idx < Ops.size() && Ops[Idx]->getSCEVType() <scMulExpr)
2743 ++Idx;
2744
2745// Check to see if there are any folding opportunities present with
2746// operands multiplied by constant values.
2747if (Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx])) {
2748uint64_tBitWidth =getTypeSizeInBits(Ty);
2749SmallDenseMap<const SCEV *, APInt, 16> M;
2750SmallVector<const SCEV *, 8> NewOps;
2751APInt AccumulatedConstant(BitWidth, 0);
2752if (CollectAddOperandsWithScales(M, NewOps, AccumulatedConstant,
2753 Ops,APInt(BitWidth, 1), *this)) {
2754structAPIntCompare {
2755bool operator()(constAPInt &LHS,constAPInt &RHS) const{
2756returnLHS.ult(RHS);
2757 }
2758 };
2759
2760// Some interesting folding opportunity is present, so its worthwhile to
2761// re-generate the operands list. Group the operands by constant scale,
2762// to avoid multiplying by the same constant scale multiple times.
2763 std::map<APInt, SmallVector<const SCEV *, 4>, APIntCompare> MulOpLists;
2764for (constSCEV *NewOp : NewOps)
2765 MulOpLists[M.find(NewOp)->second].push_back(NewOp);
2766// Re-generate the operands list.
2767 Ops.clear();
2768if (AccumulatedConstant != 0)
2769 Ops.push_back(getConstant(AccumulatedConstant));
2770for (auto &MulOp : MulOpLists) {
2771if (MulOp.first == 1) {
2772 Ops.push_back(getAddExpr(MulOp.second,SCEV::FlagAnyWrap,Depth + 1));
2773 }elseif (MulOp.first != 0) {
2774 Ops.push_back(getMulExpr(
2775getConstant(MulOp.first),
2776getAddExpr(MulOp.second,SCEV::FlagAnyWrap,Depth + 1),
2777SCEV::FlagAnyWrap,Depth + 1));
2778 }
2779 }
2780if (Ops.empty())
2781returngetZero(Ty);
2782if (Ops.size() == 1)
2783return Ops[0];
2784returngetAddExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
2785 }
2786 }
2787
2788// If we are adding something to a multiply expression, make sure the
2789// something is not already an operand of the multiply. If so, merge it into
2790// the multiply.
2791for (;Idx < Ops.size() && isa<SCEVMulExpr>(Ops[Idx]); ++Idx) {
2792constSCEVMulExpr *Mul = cast<SCEVMulExpr>(Ops[Idx]);
2793for (unsigned MulOp = 0, e =Mul->getNumOperands(); MulOp != e; ++MulOp) {
2794constSCEV *MulOpSCEV =Mul->getOperand(MulOp);
2795if (isa<SCEVConstant>(MulOpSCEV))
2796continue;
2797for (unsigned AddOp = 0, e = Ops.size(); AddOp != e; ++AddOp)
2798if (MulOpSCEV == Ops[AddOp]) {
2799// Fold W + X + (X * Y * Z) --> W + (X * ((Y*Z)+1))
2800constSCEV *InnerMul =Mul->getOperand(MulOp == 0);
2801if (Mul->getNumOperands() != 2) {
2802// If the multiply has more than two operands, we must get the
2803// Y*Z term.
2804SmallVector<const SCEV *, 4> MulOps(
2805Mul->operands().take_front(MulOp));
2806append_range(MulOps,Mul->operands().drop_front(MulOp + 1));
2807 InnerMul =getMulExpr(MulOps,SCEV::FlagAnyWrap,Depth + 1);
2808 }
2809SmallVector<const SCEV *, 2> TwoOps = {getOne(Ty), InnerMul};
2810constSCEV *AddOne =getAddExpr(TwoOps,SCEV::FlagAnyWrap,Depth + 1);
2811constSCEV *OuterMul =getMulExpr(AddOne, MulOpSCEV,
2812SCEV::FlagAnyWrap,Depth + 1);
2813if (Ops.size() == 2)return OuterMul;
2814if (AddOp <Idx) {
2815 Ops.erase(Ops.begin()+AddOp);
2816 Ops.erase(Ops.begin()+Idx-1);
2817 }else {
2818 Ops.erase(Ops.begin()+Idx);
2819 Ops.erase(Ops.begin()+AddOp-1);
2820 }
2821 Ops.push_back(OuterMul);
2822returngetAddExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
2823 }
2824
2825// Check this multiply against other multiplies being added together.
2826for (unsigned OtherMulIdx =Idx+1;
2827 OtherMulIdx < Ops.size() && isa<SCEVMulExpr>(Ops[OtherMulIdx]);
2828 ++OtherMulIdx) {
2829constSCEVMulExpr *OtherMul = cast<SCEVMulExpr>(Ops[OtherMulIdx]);
2830// If MulOp occurs in OtherMul, we can fold the two multiplies
2831// together.
2832for (unsigned OMulOp = 0, e = OtherMul->getNumOperands();
2833 OMulOp != e; ++OMulOp)
2834if (OtherMul->getOperand(OMulOp) == MulOpSCEV) {
2835// Fold X + (A*B*C) + (A*D*E) --> X + (A*(B*C+D*E))
2836constSCEV *InnerMul1 =Mul->getOperand(MulOp == 0);
2837if (Mul->getNumOperands() != 2) {
2838SmallVector<const SCEV *, 4> MulOps(
2839Mul->operands().take_front(MulOp));
2840append_range(MulOps,Mul->operands().drop_front(MulOp+1));
2841 InnerMul1 =getMulExpr(MulOps,SCEV::FlagAnyWrap,Depth + 1);
2842 }
2843constSCEV *InnerMul2 = OtherMul->getOperand(OMulOp == 0);
2844if (OtherMul->getNumOperands() != 2) {
2845SmallVector<const SCEV *, 4> MulOps(
2846 OtherMul->operands().take_front(OMulOp));
2847append_range(MulOps, OtherMul->operands().drop_front(OMulOp+1));
2848 InnerMul2 =getMulExpr(MulOps,SCEV::FlagAnyWrap,Depth + 1);
2849 }
2850SmallVector<const SCEV *, 2> TwoOps = {InnerMul1, InnerMul2};
2851constSCEV *InnerMulSum =
2852getAddExpr(TwoOps,SCEV::FlagAnyWrap,Depth + 1);
2853constSCEV *OuterMul =getMulExpr(MulOpSCEV, InnerMulSum,
2854SCEV::FlagAnyWrap,Depth + 1);
2855if (Ops.size() == 2)return OuterMul;
2856 Ops.erase(Ops.begin()+Idx);
2857 Ops.erase(Ops.begin()+OtherMulIdx-1);
2858 Ops.push_back(OuterMul);
2859returngetAddExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
2860 }
2861 }
2862 }
2863 }
2864
2865// If there are any add recurrences in the operands list, see if any other
2866// added values are loop invariant. If so, we can fold them into the
2867// recurrence.
2868while (Idx < Ops.size() && Ops[Idx]->getSCEVType() <scAddRecExpr)
2869 ++Idx;
2870
2871// Scan over all recurrences, trying to fold loop invariants into them.
2872for (;Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
2873// Scan all of the other operands to this add and add them to the vector if
2874// they are loop invariant w.r.t. the recurrence.
2875SmallVector<const SCEV *, 8> LIOps;
2876constSCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
2877constLoop *AddRecLoop = AddRec->getLoop();
2878for (unsigned i = 0, e = Ops.size(); i != e; ++i)
2879if (isAvailableAtLoopEntry(Ops[i], AddRecLoop)) {
2880 LIOps.push_back(Ops[i]);
2881 Ops.erase(Ops.begin()+i);
2882 --i; --e;
2883 }
2884
2885// If we found some loop invariants, fold them into the recurrence.
2886if (!LIOps.empty()) {
2887// Compute nowrap flags for the addition of the loop-invariant ops and
2888// the addrec. Temporarily push it as an operand for that purpose. These
2889// flags are valid in the scope of the addrec only.
2890 LIOps.push_back(AddRec);
2891SCEV::NoWrapFlags Flags = ComputeFlags(LIOps);
2892 LIOps.pop_back();
2893
2894// NLI + LI + {Start,+,Step} --> NLI + {LI+Start,+,Step}
2895 LIOps.push_back(AddRec->getStart());
2896
2897SmallVector<const SCEV *, 4> AddRecOps(AddRec->operands());
2898
2899// It is not in general safe to propagate flags valid on an add within
2900// the addrec scope to one outside it. We must prove that the inner
2901// scope is guaranteed to execute if the outer one does to be able to
2902// safely propagate. We know the program is undefined if poison is
2903// produced on the inner scoped addrec. We also know that *for this use*
2904// the outer scoped add can't overflow (because of the flags we just
2905// computed for the inner scoped add) without the program being undefined.
2906// Proving that entry to the outer scope neccesitates entry to the inner
2907// scope, thus proves the program undefined if the flags would be violated
2908// in the outer scope.
2909SCEV::NoWrapFlags AddFlags = Flags;
2910if (AddFlags !=SCEV::FlagAnyWrap) {
2911auto *DefI = getDefiningScopeBound(LIOps);
2912auto *ReachI = &*AddRecLoop->getHeader()->begin();
2913if (!isGuaranteedToTransferExecutionTo(DefI, ReachI))
2914 AddFlags =SCEV::FlagAnyWrap;
2915 }
2916 AddRecOps[0] =getAddExpr(LIOps, AddFlags,Depth + 1);
2917
2918// Build the new addrec. Propagate the NUW and NSW flags if both the
2919// outer add and the inner addrec are guaranteed to have no overflow.
2920// Always propagate NW.
2921 Flags = AddRec->getNoWrapFlags(setFlags(Flags,SCEV::FlagNW));
2922constSCEV *NewRec =getAddRecExpr(AddRecOps, AddRecLoop, Flags);
2923
2924// If all of the other operands were loop invariant, we are done.
2925if (Ops.size() == 1)return NewRec;
2926
2927// Otherwise, add the folded AddRec by the non-invariant parts.
2928for (unsigned i = 0;; ++i)
2929if (Ops[i] == AddRec) {
2930 Ops[i] = NewRec;
2931break;
2932 }
2933returngetAddExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
2934 }
2935
2936// Okay, if there weren't any loop invariants to be folded, check to see if
2937// there are multiple AddRec's with the same loop induction variable being
2938// added together. If so, we can fold them.
2939for (unsigned OtherIdx =Idx+1;
2940 OtherIdx < Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2941 ++OtherIdx) {
2942// We expect the AddRecExpr's to be sorted in reverse dominance order,
2943// so that the 1st found AddRecExpr is dominated by all others.
2944assert(DT.dominates(
2945 cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()->getHeader(),
2946 AddRec->getLoop()->getHeader()) &&
2947"AddRecExprs are not sorted in reverse dominance order?");
2948if (AddRecLoop == cast<SCEVAddRecExpr>(Ops[OtherIdx])->getLoop()) {
2949// Other + {A,+,B}<L> + {C,+,D}<L> --> Other + {A+C,+,B+D}<L>
2950SmallVector<const SCEV *, 4> AddRecOps(AddRec->operands());
2951for (; OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
2952 ++OtherIdx) {
2953constauto *OtherAddRec = cast<SCEVAddRecExpr>(Ops[OtherIdx]);
2954if (OtherAddRec->getLoop() == AddRecLoop) {
2955for (unsigned i = 0, e = OtherAddRec->getNumOperands();
2956 i != e; ++i) {
2957if (i >= AddRecOps.size()) {
2958append_range(AddRecOps, OtherAddRec->operands().drop_front(i));
2959break;
2960 }
2961SmallVector<const SCEV *, 2> TwoOps = {
2962 AddRecOps[i], OtherAddRec->getOperand(i)};
2963 AddRecOps[i] =getAddExpr(TwoOps,SCEV::FlagAnyWrap,Depth + 1);
2964 }
2965 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
2966 }
2967 }
2968// Step size has changed, so we cannot guarantee no self-wraparound.
2969 Ops[Idx] =getAddRecExpr(AddRecOps, AddRecLoop,SCEV::FlagAnyWrap);
2970returngetAddExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
2971 }
2972 }
2973
2974// Otherwise couldn't fold anything into this recurrence. Move onto the
2975// next one.
2976 }
2977
2978// Okay, it looks like we really DO need an add expr. Check to see if we
2979// already have one, otherwise create a new one.
2980return getOrCreateAddExpr(Ops, ComputeFlags(Ops));
2981}
2982
2983constSCEV *
2984ScalarEvolution::getOrCreateAddExpr(ArrayRef<const SCEV *> Ops,
2985SCEV::NoWrapFlags Flags) {
2986FoldingSetNodeIDID;
2987ID.AddInteger(scAddExpr);
2988for (constSCEV *Op : Ops)
2989ID.AddPointer(Op);
2990void *IP =nullptr;
2991SCEVAddExpr *S =
2992static_cast<SCEVAddExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
2993if (!S) {
2994constSCEV **O = SCEVAllocator.Allocate<constSCEV *>(Ops.size());
2995 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
2996 S =new (SCEVAllocator)
2997SCEVAddExpr(ID.Intern(SCEVAllocator), O, Ops.size());
2998 UniqueSCEVs.InsertNode(S, IP);
2999registerUser(S, Ops);
3000 }
3001 S->setNoWrapFlags(Flags);
3002return S;
3003}
3004
3005constSCEV *
3006ScalarEvolution::getOrCreateAddRecExpr(ArrayRef<const SCEV *> Ops,
3007constLoop *L,SCEV::NoWrapFlags Flags) {
3008FoldingSetNodeIDID;
3009ID.AddInteger(scAddRecExpr);
3010for (constSCEV *Op : Ops)
3011ID.AddPointer(Op);
3012ID.AddPointer(L);
3013void *IP =nullptr;
3014SCEVAddRecExpr *S =
3015static_cast<SCEVAddRecExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
3016if (!S) {
3017constSCEV **O = SCEVAllocator.Allocate<constSCEV *>(Ops.size());
3018 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3019 S =new (SCEVAllocator)
3020SCEVAddRecExpr(ID.Intern(SCEVAllocator),O, Ops.size(),L);
3021 UniqueSCEVs.InsertNode(S, IP);
3022 LoopUsers[L].push_back(S);
3023registerUser(S, Ops);
3024 }
3025setNoWrapFlags(S, Flags);
3026return S;
3027}
3028
3029constSCEV *
3030ScalarEvolution::getOrCreateMulExpr(ArrayRef<const SCEV *> Ops,
3031SCEV::NoWrapFlags Flags) {
3032FoldingSetNodeIDID;
3033ID.AddInteger(scMulExpr);
3034for (constSCEV *Op : Ops)
3035ID.AddPointer(Op);
3036void *IP =nullptr;
3037SCEVMulExpr *S =
3038static_cast<SCEVMulExpr *>(UniqueSCEVs.FindNodeOrInsertPos(ID, IP));
3039if (!S) {
3040constSCEV **O = SCEVAllocator.Allocate<constSCEV *>(Ops.size());
3041 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3042 S =new (SCEVAllocator)SCEVMulExpr(ID.Intern(SCEVAllocator),
3043O, Ops.size());
3044 UniqueSCEVs.InsertNode(S, IP);
3045registerUser(S, Ops);
3046 }
3047 S->setNoWrapFlags(Flags);
3048return S;
3049}
3050
3051staticuint64_tumul_ov(uint64_t i,uint64_t j,bool &Overflow) {
3052uint64_t k = i*j;
3053if (j > 1 && k / j != i) Overflow =true;
3054return k;
3055}
3056
3057/// Compute the result of "n choose k", the binomial coefficient. If an
3058/// intermediate computation overflows, Overflow will be set and the return will
3059/// be garbage. Overflow is not cleared on absence of overflow.
3060staticuint64_tChoose(uint64_t n,uint64_t k,bool &Overflow) {
3061// We use the multiplicative formula:
3062// n(n-1)(n-2)...(n-(k-1)) / k(k-1)(k-2)...1 .
3063// At each iteration, we take the n-th term of the numeral and divide by the
3064// (k-n)th term of the denominator. This division will always produce an
3065// integral result, and helps reduce the chance of overflow in the
3066// intermediate computations. However, we can still overflow even when the
3067// final result would fit.
3068
3069if (n == 0 || n == k)return 1;
3070if (k > n)return 0;
3071
3072if (k > n/2)
3073 k = n-k;
3074
3075uint64_t r = 1;
3076for (uint64_t i = 1; i <= k; ++i) {
3077 r =umul_ov(r, n-(i-1), Overflow);
3078 r /= i;
3079 }
3080return r;
3081}
3082
3083/// Determine if any of the operands in this SCEV are a constant or if
3084/// any of the add or multiply expressions in this SCEV contain a constant.
3085staticboolcontainsConstantInAddMulChain(constSCEV *StartExpr) {
3086structFindConstantInAddMulChain {
3087bool FoundConstant =false;
3088
3089bool follow(constSCEV *S) {
3090 FoundConstant |= isa<SCEVConstant>(S);
3091return isa<SCEVAddExpr>(S) || isa<SCEVMulExpr>(S);
3092 }
3093
3094bool isDone() const{
3095return FoundConstant;
3096 }
3097 };
3098
3099 FindConstantInAddMulChainF;
3100SCEVTraversal<FindConstantInAddMulChain> ST(F);
3101 ST.visitAll(StartExpr);
3102returnF.FoundConstant;
3103}
3104
3105/// Get a canonical multiply expression, or something simpler if possible.
3106constSCEV *ScalarEvolution::getMulExpr(SmallVectorImpl<const SCEV *> &Ops,
3107SCEV::NoWrapFlags OrigFlags,
3108unsignedDepth) {
3109assert(OrigFlags ==maskFlags(OrigFlags,SCEV::FlagNUW |SCEV::FlagNSW) &&
3110"only nuw or nsw allowed");
3111assert(!Ops.empty() &&"Cannot get empty mul!");
3112if (Ops.size() == 1)return Ops[0];
3113#ifndef NDEBUG
3114Type *ETy = Ops[0]->getType();
3115assert(!ETy->isPointerTy());
3116for (unsigned i = 1, e = Ops.size(); i != e; ++i)
3117assert(Ops[i]->getType() == ETy &&
3118"SCEVMulExpr operand types don't match!");
3119#endif
3120
3121constSCEV *Folded =constantFoldAndGroupOps(
3122 *this, LI, DT, Ops,
3123 [](constAPInt &C1,constAPInt &C2) {return C1 * C2; },
3124 [](constAPInt &C) {returnC.isOne(); },// identity
3125 [](constAPInt &C) {returnC.isZero(); });// absorber
3126if (Folded)
3127return Folded;
3128
3129// Delay expensive flag strengthening until necessary.
3130auto ComputeFlags = [this, OrigFlags](constArrayRef<const SCEV *> Ops) {
3131returnStrengthenNoWrapFlags(this,scMulExpr, Ops, OrigFlags);
3132 };
3133
3134// Limit recursion calls depth.
3135if (Depth >MaxArithDepth ||hasHugeExpression(Ops))
3136return getOrCreateMulExpr(Ops, ComputeFlags(Ops));
3137
3138if (SCEV *S = findExistingSCEVInCache(scMulExpr, Ops)) {
3139// Don't strengthen flags if we have no new information.
3140SCEVMulExpr *Mul =static_cast<SCEVMulExpr *>(S);
3141if (Mul->getNoWrapFlags(OrigFlags) != OrigFlags)
3142Mul->setNoWrapFlags(ComputeFlags(Ops));
3143return S;
3144 }
3145
3146if (constSCEVConstant *LHSC = dyn_cast<SCEVConstant>(Ops[0])) {
3147if (Ops.size() == 2) {
3148// C1*(C2+V) -> C1*C2 + C1*V
3149if (constSCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1]))
3150// If any of Add's ops are Adds or Muls with a constant, apply this
3151// transformation as well.
3152//
3153// TODO: There are some cases where this transformation is not
3154// profitable; for example, Add = (C0 + X) * Y + Z. Maybe the scope of
3155// this transformation should be narrowed down.
3156if (Add->getNumOperands() == 2 &&containsConstantInAddMulChain(Add)) {
3157constSCEV *LHS =getMulExpr(LHSC,Add->getOperand(0),
3158SCEV::FlagAnyWrap,Depth + 1);
3159constSCEV *RHS =getMulExpr(LHSC,Add->getOperand(1),
3160SCEV::FlagAnyWrap,Depth + 1);
3161returngetAddExpr(LHS,RHS,SCEV::FlagAnyWrap,Depth + 1);
3162 }
3163
3164if (Ops[0]->isAllOnesValue()) {
3165// If we have a mul by -1 of an add, try distributing the -1 among the
3166// add operands.
3167if (constSCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Ops[1])) {
3168SmallVector<const SCEV *, 4> NewOps;
3169bool AnyFolded =false;
3170for (constSCEV *AddOp :Add->operands()) {
3171constSCEV *Mul =getMulExpr(Ops[0], AddOp,SCEV::FlagAnyWrap,
3172Depth + 1);
3173if (!isa<SCEVMulExpr>(Mul)) AnyFolded =true;
3174 NewOps.push_back(Mul);
3175 }
3176if (AnyFolded)
3177returngetAddExpr(NewOps,SCEV::FlagAnyWrap,Depth + 1);
3178 }elseif (constauto *AddRec = dyn_cast<SCEVAddRecExpr>(Ops[1])) {
3179// Negation preserves a recurrence's no self-wrap property.
3180SmallVector<const SCEV *, 4>Operands;
3181for (constSCEV *AddRecOp : AddRec->operands())
3182Operands.push_back(getMulExpr(Ops[0], AddRecOp,SCEV::FlagAnyWrap,
3183Depth + 1));
3184// Let M be the minimum representable signed value. AddRec with nsw
3185// multiplied by -1 can have signed overflow if and only if it takes a
3186// value of M: M * (-1) would stay M and (M + 1) * (-1) would be the
3187// maximum signed value. In all other cases signed overflow is
3188// impossible.
3189auto FlagsMask =SCEV::FlagNW;
3190if (hasFlags(AddRec->getNoWrapFlags(),SCEV::FlagNSW)) {
3191auto MinInt =
3192APInt::getSignedMinValue(getTypeSizeInBits(AddRec->getType()));
3193if (getSignedRangeMin(AddRec) != MinInt)
3194 FlagsMask =setFlags(FlagsMask,SCEV::FlagNSW);
3195 }
3196returngetAddRecExpr(Operands, AddRec->getLoop(),
3197 AddRec->getNoWrapFlags(FlagsMask));
3198 }
3199 }
3200 }
3201 }
3202
3203// Skip over the add expression until we get to a multiply.
3204unsignedIdx = 0;
3205while (Idx < Ops.size() && Ops[Idx]->getSCEVType() <scMulExpr)
3206 ++Idx;
3207
3208// If there are mul operands inline them all into this expression.
3209if (Idx < Ops.size()) {
3210bool DeletedMul =false;
3211while (constSCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(Ops[Idx])) {
3212if (Ops.size() >MulOpsInlineThreshold)
3213break;
3214// If we have an mul, expand the mul operands onto the end of the
3215// operands list.
3216 Ops.erase(Ops.begin()+Idx);
3217append_range(Ops,Mul->operands());
3218 DeletedMul =true;
3219 }
3220
3221// If we deleted at least one mul, we added operands to the end of the
3222// list, and they are not necessarily sorted. Recurse to resort and
3223// resimplify any operands we just acquired.
3224if (DeletedMul)
3225returngetMulExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
3226 }
3227
3228// If there are any add recurrences in the operands list, see if any other
3229// added values are loop invariant. If so, we can fold them into the
3230// recurrence.
3231while (Idx < Ops.size() && Ops[Idx]->getSCEVType() <scAddRecExpr)
3232 ++Idx;
3233
3234// Scan over all recurrences, trying to fold loop invariants into them.
3235for (;Idx < Ops.size() && isa<SCEVAddRecExpr>(Ops[Idx]); ++Idx) {
3236// Scan all of the other operands to this mul and add them to the vector
3237// if they are loop invariant w.r.t. the recurrence.
3238SmallVector<const SCEV *, 8> LIOps;
3239constSCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(Ops[Idx]);
3240for (unsigned i = 0, e = Ops.size(); i != e; ++i)
3241if (isAvailableAtLoopEntry(Ops[i], AddRec->getLoop())) {
3242 LIOps.push_back(Ops[i]);
3243 Ops.erase(Ops.begin()+i);
3244 --i; --e;
3245 }
3246
3247// If we found some loop invariants, fold them into the recurrence.
3248if (!LIOps.empty()) {
3249// NLI * LI * {Start,+,Step} --> NLI * {LI*Start,+,LI*Step}
3250SmallVector<const SCEV *, 4> NewOps;
3251 NewOps.reserve(AddRec->getNumOperands());
3252constSCEV *Scale =getMulExpr(LIOps,SCEV::FlagAnyWrap,Depth + 1);
3253
3254// If both the mul and addrec are nuw, we can preserve nuw.
3255// If both the mul and addrec are nsw, we can only preserve nsw if either
3256// a) they are also nuw, or
3257// b) all multiplications of addrec operands with scale are nsw.
3258SCEV::NoWrapFlags Flags =
3259 AddRec->getNoWrapFlags(ComputeFlags({Scale, AddRec}));
3260
3261for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
3262 NewOps.push_back(getMulExpr(Scale, AddRec->getOperand(i),
3263SCEV::FlagAnyWrap,Depth + 1));
3264
3265if (hasFlags(Flags,SCEV::FlagNSW) && !hasFlags(Flags,SCEV::FlagNUW)) {
3266ConstantRange NSWRegion =ConstantRange::makeGuaranteedNoWrapRegion(
3267 Instruction::Mul,getSignedRange(Scale),
3268OverflowingBinaryOperator::NoSignedWrap);
3269if (!NSWRegion.contains(getSignedRange(AddRec->getOperand(i))))
3270 Flags =clearFlags(Flags,SCEV::FlagNSW);
3271 }
3272 }
3273
3274constSCEV *NewRec =getAddRecExpr(NewOps, AddRec->getLoop(), Flags);
3275
3276// If all of the other operands were loop invariant, we are done.
3277if (Ops.size() == 1)return NewRec;
3278
3279// Otherwise, multiply the folded AddRec by the non-invariant parts.
3280for (unsigned i = 0;; ++i)
3281if (Ops[i] == AddRec) {
3282 Ops[i] = NewRec;
3283break;
3284 }
3285returngetMulExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
3286 }
3287
3288// Okay, if there weren't any loop invariants to be folded, check to see
3289// if there are multiple AddRec's with the same loop induction variable
3290// being multiplied together. If so, we can fold them.
3291
3292// {A1,+,A2,+,...,+,An}<L> * {B1,+,B2,+,...,+,Bn}<L>
3293// = {x=1 in [ sum y=x..2x [ sum z=max(y-x, y-n)..min(x,n) [
3294// choose(x, 2x)*choose(2x-y, x-z)*A_{y-z}*B_z
3295// ]]],+,...up to x=2n}.
3296// Note that the arguments to choose() are always integers with values
3297// known at compile time, never SCEV objects.
3298//
3299// The implementation avoids pointless extra computations when the two
3300// addrec's are of different length (mathematically, it's equivalent to
3301// an infinite stream of zeros on the right).
3302bool OpsModified =false;
3303for (unsigned OtherIdx =Idx+1;
3304 OtherIdx != Ops.size() && isa<SCEVAddRecExpr>(Ops[OtherIdx]);
3305 ++OtherIdx) {
3306constSCEVAddRecExpr *OtherAddRec =
3307 dyn_cast<SCEVAddRecExpr>(Ops[OtherIdx]);
3308if (!OtherAddRec || OtherAddRec->getLoop() != AddRec->getLoop())
3309continue;
3310
3311// Limit max number of arguments to avoid creation of unreasonably big
3312// SCEVAddRecs with very complex operands.
3313if (AddRec->getNumOperands() + OtherAddRec->getNumOperands() - 1 >
3314MaxAddRecSize ||hasHugeExpression({AddRec, OtherAddRec}))
3315continue;
3316
3317bool Overflow =false;
3318Type *Ty = AddRec->getType();
3319bool LargerThan64Bits =getTypeSizeInBits(Ty) > 64;
3320SmallVector<const SCEV*, 7> AddRecOps;
3321for (int x = 0, xe = AddRec->getNumOperands() +
3322 OtherAddRec->getNumOperands() - 1; x != xe && !Overflow; ++x) {
3323 SmallVector <const SCEV *, 7> SumOps;
3324for (int y = x, ye = 2*x+1; y != ye && !Overflow; ++y) {
3325uint64_t Coeff1 =Choose(x, 2*x - y, Overflow);
3326for (int z = std::max(y-x, y-(int)AddRec->getNumOperands()+1),
3327 ze = std::min(x+1, (int)OtherAddRec->getNumOperands());
3328 z < ze && !Overflow; ++z) {
3329uint64_t Coeff2 =Choose(2*x - y, x-z, Overflow);
3330uint64_t Coeff;
3331if (LargerThan64Bits)
3332 Coeff =umul_ov(Coeff1, Coeff2, Overflow);
3333else
3334 Coeff = Coeff1*Coeff2;
3335constSCEV *CoeffTerm =getConstant(Ty, Coeff);
3336constSCEV *Term1 = AddRec->getOperand(y-z);
3337constSCEV *Term2 = OtherAddRec->getOperand(z);
3338 SumOps.push_back(getMulExpr(CoeffTerm, Term1, Term2,
3339SCEV::FlagAnyWrap,Depth + 1));
3340 }
3341 }
3342if (SumOps.empty())
3343 SumOps.push_back(getZero(Ty));
3344 AddRecOps.push_back(getAddExpr(SumOps,SCEV::FlagAnyWrap,Depth + 1));
3345 }
3346if (!Overflow) {
3347constSCEV *NewAddRec =getAddRecExpr(AddRecOps, AddRec->getLoop(),
3348SCEV::FlagAnyWrap);
3349if (Ops.size() == 2)return NewAddRec;
3350 Ops[Idx] = NewAddRec;
3351 Ops.erase(Ops.begin() + OtherIdx); --OtherIdx;
3352 OpsModified =true;
3353 AddRec = dyn_cast<SCEVAddRecExpr>(NewAddRec);
3354if (!AddRec)
3355break;
3356 }
3357 }
3358if (OpsModified)
3359returngetMulExpr(Ops,SCEV::FlagAnyWrap,Depth + 1);
3360
3361// Otherwise couldn't fold anything into this recurrence. Move onto the
3362// next one.
3363 }
3364
3365// Okay, it looks like we really DO need an mul expr. Check to see if we
3366// already have one, otherwise create a new one.
3367return getOrCreateMulExpr(Ops, ComputeFlags(Ops));
3368}
3369
3370/// Represents an unsigned remainder expression based on unsigned division.
3371constSCEV *ScalarEvolution::getURemExpr(constSCEV *LHS,
3372constSCEV *RHS) {
3373assert(getEffectiveSCEVType(LHS->getType()) ==
3374getEffectiveSCEVType(RHS->getType()) &&
3375"SCEVURemExpr operand types don't match!");
3376
3377// Short-circuit easy cases
3378if (constSCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3379// If constant is one, the result is trivial
3380if (RHSC->getValue()->isOne())
3381returngetZero(LHS->getType());// X urem 1 --> 0
3382
3383// If constant is a power of two, fold into a zext(trunc(LHS)).
3384if (RHSC->getAPInt().isPowerOf2()) {
3385Type *FullTy =LHS->getType();
3386Type *TruncTy =
3387IntegerType::get(getContext(), RHSC->getAPInt().logBase2());
3388returngetZeroExtendExpr(getTruncateExpr(LHS, TruncTy), FullTy);
3389 }
3390 }
3391
3392// Fallback to %a == %x urem %y == %x -<nuw> ((%x udiv %y) *<nuw> %y)
3393constSCEV *UDiv =getUDivExpr(LHS,RHS);
3394constSCEV *Mult =getMulExpr(UDiv,RHS,SCEV::FlagNUW);
3395returngetMinusSCEV(LHS, Mult,SCEV::FlagNUW);
3396}
3397
3398/// Get a canonical unsigned division expression, or something simpler if
3399/// possible.
3400constSCEV *ScalarEvolution::getUDivExpr(constSCEV *LHS,
3401constSCEV *RHS) {
3402assert(!LHS->getType()->isPointerTy() &&
3403"SCEVUDivExpr operand can't be pointer!");
3404assert(LHS->getType() ==RHS->getType() &&
3405"SCEVUDivExpr operand types don't match!");
3406
3407FoldingSetNodeIDID;
3408ID.AddInteger(scUDivExpr);
3409ID.AddPointer(LHS);
3410ID.AddPointer(RHS);
3411void *IP =nullptr;
3412if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
3413return S;
3414
3415// 0 udiv Y == 0
3416if (match(LHS,m_scev_Zero()))
3417returnLHS;
3418
3419if (constSCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
3420if (RHSC->getValue()->isOne())
3421returnLHS;// X udiv 1 --> x
3422// If the denominator is zero, the result of the udiv is undefined. Don't
3423// try to analyze it, because the resolution chosen here may differ from
3424// the resolution chosen in other parts of the compiler.
3425if (!RHSC->getValue()->isZero()) {
3426// Determine if the division can be folded into the operands of
3427// its operands.
3428// TODO: Generalize this to non-constants by using known-bits information.
3429Type *Ty =LHS->getType();
3430unsigned LZ = RHSC->getAPInt().countl_zero();
3431unsigned MaxShiftAmt =getTypeSizeInBits(Ty) - LZ - 1;
3432// For non-power-of-two values, effectively round the value up to the
3433// nearest power of two.
3434if (!RHSC->getAPInt().isPowerOf2())
3435 ++MaxShiftAmt;
3436IntegerType *ExtTy =
3437IntegerType::get(getContext(),getTypeSizeInBits(Ty) + MaxShiftAmt);
3438if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
3439if (constSCEVConstant *Step =
3440 dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this))) {
3441// {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
3442constAPInt &StepInt = Step->getAPInt();
3443constAPInt &DivInt = RHSC->getAPInt();
3444if (!StepInt.urem(DivInt) &&
3445getZeroExtendExpr(AR, ExtTy) ==
3446getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3447getZeroExtendExpr(Step, ExtTy),
3448 AR->getLoop(),SCEV::FlagAnyWrap)) {
3449SmallVector<const SCEV *, 4>Operands;
3450for (constSCEV *Op : AR->operands())
3451Operands.push_back(getUDivExpr(Op,RHS));
3452returngetAddRecExpr(Operands, AR->getLoop(),SCEV::FlagNW);
3453 }
3454 /// Get a canonical UDivExpr for a recurrence.
3455 /// {X,+,N}/C => {Y,+,N}/C where Y=X-(X%N). Safe when C%N=0.
3456// We can currently only fold X%N if X is constant.
3457constSCEVConstant *StartC = dyn_cast<SCEVConstant>(AR->getStart());
3458if (StartC && !DivInt.urem(StepInt) &&
3459getZeroExtendExpr(AR, ExtTy) ==
3460getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
3461getZeroExtendExpr(Step, ExtTy),
3462 AR->getLoop(),SCEV::FlagAnyWrap)) {
3463constAPInt &StartInt = StartC->getAPInt();
3464constAPInt &StartRem = StartInt.urem(StepInt);
3465if (StartRem != 0) {
3466constSCEV *NewLHS =
3467getAddRecExpr(getConstant(StartInt - StartRem), Step,
3468 AR->getLoop(),SCEV::FlagNW);
3469if (LHS != NewLHS) {
3470LHS = NewLHS;
3471
3472// Reset the ID to include the new LHS, and check if it is
3473// already cached.
3474ID.clear();
3475ID.AddInteger(scUDivExpr);
3476ID.AddPointer(LHS);
3477ID.AddPointer(RHS);
3478 IP =nullptr;
3479if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))
3480return S;
3481 }
3482 }
3483 }
3484 }
3485// (A*B)/C --> A*(B/C) if safe and B/C can be folded.
3486if (constSCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
3487SmallVector<const SCEV *, 4>Operands;
3488for (constSCEV *Op : M->operands())
3489Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3490if (getZeroExtendExpr(M, ExtTy) ==getMulExpr(Operands))
3491// Find an operand that's safely divisible.
3492for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
3493constSCEV *Op = M->getOperand(i);
3494constSCEV *Div =getUDivExpr(Op, RHSC);
3495if (!isa<SCEVUDivExpr>(Div) &&getMulExpr(Div, RHSC) ==Op) {
3496Operands =SmallVector<const SCEV *, 4>(M->operands());
3497Operands[i] = Div;
3498returngetMulExpr(Operands);
3499 }
3500 }
3501 }
3502
3503// (A/B)/C --> A/(B*C) if safe and B*C can be folded.
3504if (constSCEVUDivExpr *OtherDiv = dyn_cast<SCEVUDivExpr>(LHS)) {
3505if (auto *DivisorConstant =
3506 dyn_cast<SCEVConstant>(OtherDiv->getRHS())) {
3507bool Overflow =false;
3508APInt NewRHS =
3509 DivisorConstant->getAPInt().umul_ov(RHSC->getAPInt(), Overflow);
3510if (Overflow) {
3511returngetConstant(RHSC->getType(), 0,false);
3512 }
3513returngetUDivExpr(OtherDiv->getLHS(),getConstant(NewRHS));
3514 }
3515 }
3516
3517// (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
3518if (constSCEVAddExpr *A = dyn_cast<SCEVAddExpr>(LHS)) {
3519SmallVector<const SCEV *, 4>Operands;
3520for (constSCEV *Op :A->operands())
3521Operands.push_back(getZeroExtendExpr(Op, ExtTy));
3522if (getZeroExtendExpr(A, ExtTy) ==getAddExpr(Operands)) {
3523Operands.clear();
3524for (unsigned i = 0, e =A->getNumOperands(); i != e; ++i) {
3525constSCEV *Op =getUDivExpr(A->getOperand(i),RHS);
3526if (isa<SCEVUDivExpr>(Op) ||
3527getMulExpr(Op,RHS) !=A->getOperand(i))
3528break;
3529Operands.push_back(Op);
3530 }
3531if (Operands.size() ==A->getNumOperands())
3532returngetAddExpr(Operands);
3533 }
3534 }
3535
3536// Fold if both operands are constant.
3537if (constSCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS))
3538returngetConstant(LHSC->getAPInt().udiv(RHSC->getAPInt()));
3539 }
3540 }
3541
3542// ((-C + (C smax %x)) /u %x) evaluates to zero, for any positive constant C.
3543if (constauto *AE = dyn_cast<SCEVAddExpr>(LHS);
3544 AE && AE->getNumOperands() == 2) {
3545if (constauto *VC = dyn_cast<SCEVConstant>(AE->getOperand(0))) {
3546constAPInt &NegC = VC->getAPInt();
3547if (NegC.isNegative() && !NegC.isMinSignedValue()) {
3548constauto *MME = dyn_cast<SCEVSMaxExpr>(AE->getOperand(1));
3549if (MME && MME->getNumOperands() == 2 &&
3550 isa<SCEVConstant>(MME->getOperand(0)) &&
3551 cast<SCEVConstant>(MME->getOperand(0))->getAPInt() == -NegC &&
3552 MME->getOperand(1) ==RHS)
3553returngetZero(LHS->getType());
3554 }
3555 }
3556 }
3557
3558// The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs
3559// changes). Make sure we get a new one.
3560 IP =nullptr;
3561if (constSCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP))return S;
3562SCEV *S =new (SCEVAllocator)SCEVUDivExpr(ID.Intern(SCEVAllocator),
3563LHS,RHS);
3564 UniqueSCEVs.InsertNode(S, IP);
3565registerUser(S, {LHS,RHS});
3566return S;
3567}
3568
3569APIntgcd(constSCEVConstant *C1,constSCEVConstant *C2) {
3570APIntA = C1->getAPInt().abs();
3571APIntB = C2->getAPInt().abs();
3572uint32_t ABW =A.getBitWidth();
3573uint32_t BBW =B.getBitWidth();
3574
3575if (ABW > BBW)
3576B =B.zext(ABW);
3577elseif (ABW < BBW)
3578A =A.zext(BBW);
3579
3580returnAPIntOps::GreatestCommonDivisor(std::move(A), std::move(B));
3581}
3582
3583/// Get a canonical unsigned division expression, or something simpler if
3584/// possible. There is no representation for an exact udiv in SCEV IR, but we
3585/// can attempt to remove factors from the LHS and RHS. We can't do this when
3586/// it's not exact because the udiv may be clearing bits.
3587constSCEV *ScalarEvolution::getUDivExactExpr(constSCEV *LHS,
3588constSCEV *RHS) {
3589// TODO: we could try to find factors in all sorts of things, but for now we
3590// just deal with u/exact (multiply, constant). See SCEVDivision towards the
3591// end of this file for inspiration.
3592
3593constSCEVMulExpr *Mul = dyn_cast<SCEVMulExpr>(LHS);
3594if (!Mul || !Mul->hasNoUnsignedWrap())
3595returngetUDivExpr(LHS,RHS);
3596
3597if (constSCEVConstant *RHSCst = dyn_cast<SCEVConstant>(RHS)) {
3598// If the mulexpr multiplies by a constant, then that constant must be the
3599// first element of the mulexpr.
3600if (constauto *LHSCst = dyn_cast<SCEVConstant>(Mul->getOperand(0))) {
3601if (LHSCst == RHSCst) {
3602SmallVector<const SCEV *, 2>Operands(drop_begin(Mul->operands()));
3603returngetMulExpr(Operands);
3604 }
3605
3606// We can't just assume that LHSCst divides RHSCst cleanly, it could be
3607// that there's a factor provided by one of the other terms. We need to
3608// check.
3609APInt Factor =gcd(LHSCst, RHSCst);
3610if (!Factor.isIntN(1)) {
3611 LHSCst =
3612 cast<SCEVConstant>(getConstant(LHSCst->getAPInt().udiv(Factor)));
3613 RHSCst =
3614 cast<SCEVConstant>(getConstant(RHSCst->getAPInt().udiv(Factor)));
3615SmallVector<const SCEV *, 2>Operands;
3616Operands.push_back(LHSCst);
3617append_range(Operands,Mul->operands().drop_front());
3618LHS =getMulExpr(Operands);
3619RHS = RHSCst;
3620Mul = dyn_cast<SCEVMulExpr>(LHS);
3621if (!Mul)
3622returngetUDivExactExpr(LHS,RHS);
3623 }
3624 }
3625 }
3626
3627for (int i = 0, e =Mul->getNumOperands(); i != e; ++i) {
3628if (Mul->getOperand(i) ==RHS) {
3629SmallVector<const SCEV *, 2>Operands;
3630append_range(Operands,Mul->operands().take_front(i));
3631append_range(Operands,Mul->operands().drop_front(i + 1));
3632returngetMulExpr(Operands);
3633 }
3634 }
3635
3636returngetUDivExpr(LHS,RHS);
3637}
3638
3639/// Get an add recurrence expression for the specified loop. Simplify the
3640/// expression as much as possible.
3641constSCEV *ScalarEvolution::getAddRecExpr(constSCEV *Start,constSCEV *Step,
3642constLoop *L,
3643SCEV::NoWrapFlags Flags) {
3644SmallVector<const SCEV *, 4>Operands;
3645Operands.push_back(Start);
3646if (constSCEVAddRecExpr *StepChrec = dyn_cast<SCEVAddRecExpr>(Step))
3647if (StepChrec->getLoop() == L) {
3648append_range(Operands, StepChrec->operands());
3649returngetAddRecExpr(Operands, L,maskFlags(Flags,SCEV::FlagNW));
3650 }
3651
3652Operands.push_back(Step);
3653returngetAddRecExpr(Operands, L, Flags);
3654}
3655
3656/// Get an add recurrence expression for the specified loop. Simplify the
3657/// expression as much as possible.
3658constSCEV *
3659ScalarEvolution::getAddRecExpr(SmallVectorImpl<const SCEV *> &Operands,
3660constLoop *L,SCEV::NoWrapFlags Flags) {
3661if (Operands.size() == 1)returnOperands[0];
3662#ifndef NDEBUG
3663Type *ETy =getEffectiveSCEVType(Operands[0]->getType());
3664for (constSCEV *Op :llvm::drop_begin(Operands)) {
3665assert(getEffectiveSCEVType(Op->getType()) == ETy &&
3666"SCEVAddRecExpr operand types don't match!");
3667assert(!Op->getType()->isPointerTy() &&"Step must be integer");
3668 }
3669for (constSCEV *Op :Operands)
3670assert(isAvailableAtLoopEntry(Op, L) &&
3671"SCEVAddRecExpr operand is not available at loop entry!");
3672#endif
3673
3674if (Operands.back()->isZero()) {
3675Operands.pop_back();
3676returngetAddRecExpr(Operands, L,SCEV::FlagAnyWrap);// {X,+,0} --> X
3677 }
3678
3679// It's tempting to want to call getConstantMaxBackedgeTakenCount count here and
3680// use that information to infer NUW and NSW flags. However, computing a
3681// BE count requires calling getAddRecExpr, so we may not yet have a
3682// meaningful BE count at this point (and if we don't, we'd be stuck
3683// with a SCEVCouldNotCompute as the cached BE count).
3684
3685 Flags =StrengthenNoWrapFlags(this,scAddRecExpr,Operands, Flags);
3686
3687// Canonicalize nested AddRecs in by nesting them in order of loop depth.
3688if (constSCEVAddRecExpr *NestedAR = dyn_cast<SCEVAddRecExpr>(Operands[0])) {
3689constLoop *NestedLoop = NestedAR->getLoop();
3690if (L->contains(NestedLoop)
3691 ? (L->getLoopDepth() < NestedLoop->getLoopDepth())
3692 : (!NestedLoop->contains(L) &&
3693 DT.dominates(L->getHeader(), NestedLoop->getHeader()))) {
3694SmallVector<const SCEV *, 4> NestedOperands(NestedAR->operands());
3695Operands[0] = NestedAR->getStart();
3696// AddRecs require their operands be loop-invariant with respect to their
3697// loops. Don't perform this transformation if it would break this
3698// requirement.
3699bool AllInvariant =all_of(
3700Operands, [&](constSCEV *Op) {returnisLoopInvariant(Op, L); });
3701
3702if (AllInvariant) {
3703// Create a recurrence for the outer loop with the same step size.
3704//
3705// The outer recurrence keeps its NW flag but only keeps NUW/NSW if the
3706// inner recurrence has the same property.
3707SCEV::NoWrapFlags OuterFlags =
3708maskFlags(Flags,SCEV::FlagNW | NestedAR->getNoWrapFlags());
3709
3710 NestedOperands[0] =getAddRecExpr(Operands, L, OuterFlags);
3711 AllInvariant =all_of(NestedOperands, [&](constSCEV *Op) {
3712returnisLoopInvariant(Op, NestedLoop);
3713 });
3714
3715if (AllInvariant) {
3716// Ok, both add recurrences are valid after the transformation.
3717//
3718// The inner recurrence keeps its NW flag but only keeps NUW/NSW if
3719// the outer recurrence has the same property.
3720SCEV::NoWrapFlags InnerFlags =
3721maskFlags(NestedAR->getNoWrapFlags(),SCEV::FlagNW | Flags);
3722returngetAddRecExpr(NestedOperands, NestedLoop, InnerFlags);
3723 }
3724 }
3725// Reset Operands to its original state.
3726Operands[0] = NestedAR;
3727 }
3728 }
3729
3730// Okay, it looks like we really DO need an addrec expr. Check to see if we
3731// already have one, otherwise create a new one.
3732return getOrCreateAddRecExpr(Operands, L, Flags);
3733}
3734
3735constSCEV *
3736ScalarEvolution::getGEPExpr(GEPOperator *GEP,
3737constSmallVectorImpl<const SCEV *> &IndexExprs) {
3738constSCEV *BaseExpr =getSCEV(GEP->getPointerOperand());
3739// getSCEV(Base)->getType() has the same address space as Base->getType()
3740// because SCEV::getType() preserves the address space.
3741Type *IntIdxTy =getEffectiveSCEVType(BaseExpr->getType());
3742GEPNoWrapFlags NW =GEP->getNoWrapFlags();
3743if (NW !=GEPNoWrapFlags::none()) {
3744// We'd like to propagate flags from the IR to the corresponding SCEV nodes,
3745// but to do that, we have to ensure that said flag is valid in the entire
3746// defined scope of the SCEV.
3747// TODO: non-instructions have global scope. We might be able to prove
3748// some global scope cases
3749auto *GEPI = dyn_cast<Instruction>(GEP);
3750if (!GEPI || !isSCEVExprNeverPoison(GEPI))
3751 NW =GEPNoWrapFlags::none();
3752 }
3753
3754SCEV::NoWrapFlags OffsetWrap =SCEV::FlagAnyWrap;
3755if (NW.hasNoUnsignedSignedWrap())
3756 OffsetWrap =setFlags(OffsetWrap,SCEV::FlagNSW);
3757if (NW.hasNoUnsignedWrap())
3758 OffsetWrap =setFlags(OffsetWrap,SCEV::FlagNUW);
3759
3760Type *CurTy =GEP->getType();
3761bool FirstIter =true;
3762SmallVector<const SCEV *, 4> Offsets;
3763for (constSCEV *IndexExpr : IndexExprs) {
3764// Compute the (potentially symbolic) offset in bytes for this index.
3765if (StructType *STy = dyn_cast<StructType>(CurTy)) {
3766// For a struct, add the member offset.
3767ConstantInt *Index = cast<SCEVConstant>(IndexExpr)->getValue();
3768unsigned FieldNo = Index->getZExtValue();
3769constSCEV *FieldOffset =getOffsetOfExpr(IntIdxTy, STy, FieldNo);
3770 Offsets.push_back(FieldOffset);
3771
3772// Update CurTy to the type of the field at Index.
3773 CurTy = STy->getTypeAtIndex(Index);
3774 }else {
3775// Update CurTy to its element type.
3776if (FirstIter) {
3777assert(isa<PointerType>(CurTy) &&
3778"The first index of a GEP indexes a pointer");
3779 CurTy =GEP->getSourceElementType();
3780 FirstIter =false;
3781 }else {
3782 CurTy =GetElementPtrInst::getTypeAtIndex(CurTy, (uint64_t)0);
3783 }
3784// For an array, add the element offset, explicitly scaled.
3785constSCEV *ElementSize =getSizeOfExpr(IntIdxTy, CurTy);
3786// Getelementptr indices are signed.
3787 IndexExpr =getTruncateOrSignExtend(IndexExpr, IntIdxTy);
3788
3789// Multiply the index by the element size to compute the element offset.
3790constSCEV *LocalOffset =getMulExpr(IndexExpr, ElementSize, OffsetWrap);
3791 Offsets.push_back(LocalOffset);
3792 }
3793 }
3794
3795// Handle degenerate case of GEP without offsets.
3796if (Offsets.empty())
3797return BaseExpr;
3798
3799// Add the offsets together, assuming nsw if inbounds.
3800constSCEV *Offset =getAddExpr(Offsets, OffsetWrap);
3801// Add the base address and the offset. We cannot use the nsw flag, as the
3802// base address is unsigned. However, if we know that the offset is
3803// non-negative, we can use nuw.
3804bool NUW = NW.hasNoUnsignedWrap() ||
3805 (NW.hasNoUnsignedSignedWrap() &&isKnownNonNegative(Offset));
3806SCEV::NoWrapFlags BaseWrap = NUW ?SCEV::FlagNUW :SCEV::FlagAnyWrap;
3807auto *GEPExpr =getAddExpr(BaseExpr,Offset, BaseWrap);
3808assert(BaseExpr->getType() == GEPExpr->getType() &&
3809"GEP should not change type mid-flight.");
3810return GEPExpr;
3811}
3812
3813SCEV *ScalarEvolution::findExistingSCEVInCache(SCEVTypes SCEVType,
3814ArrayRef<const SCEV *> Ops) {
3815FoldingSetNodeIDID;
3816ID.AddInteger(SCEVType);
3817for (constSCEV *Op : Ops)
3818ID.AddPointer(Op);
3819void *IP =nullptr;
3820return UniqueSCEVs.FindNodeOrInsertPos(ID, IP);
3821}
3822
3823constSCEV *ScalarEvolution::getAbsExpr(constSCEV *Op,bool IsNSW) {
3824SCEV::NoWrapFlags Flags = IsNSW ?SCEV::FlagNSW :SCEV::FlagAnyWrap;
3825returngetSMaxExpr(Op,getNegativeSCEV(Op, Flags));
3826}
3827
3828constSCEV *ScalarEvolution::getMinMaxExpr(SCEVTypes Kind,
3829SmallVectorImpl<const SCEV *> &Ops) {
3830assert(SCEVMinMaxExpr::isMinMaxType(Kind) &&"Not a SCEVMinMaxExpr!");
3831assert(!Ops.empty() &&"Cannot get empty (u|s)(min|max)!");
3832if (Ops.size() == 1)return Ops[0];
3833#ifndef NDEBUG
3834Type *ETy =getEffectiveSCEVType(Ops[0]->getType());
3835for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
3836assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
3837"Operand types don't match!");
3838assert(Ops[0]->getType()->isPointerTy() ==
3839 Ops[i]->getType()->isPointerTy() &&
3840"min/max should be consistently pointerish");
3841 }
3842#endif
3843
3844bool IsSigned = Kind ==scSMaxExpr || Kind ==scSMinExpr;
3845bool IsMax = Kind ==scSMaxExpr || Kind ==scUMaxExpr;
3846
3847constSCEV *Folded =constantFoldAndGroupOps(
3848 *this, LI, DT, Ops,
3849 [&](constAPInt &C1,constAPInt &C2) {
3850switch (Kind) {
3851casescSMaxExpr:
3852returnAPIntOps::smax(C1, C2);
3853casescSMinExpr:
3854returnAPIntOps::smin(C1, C2);
3855casescUMaxExpr:
3856returnAPIntOps::umax(C1, C2);
3857casescUMinExpr:
3858returnAPIntOps::umin(C1, C2);
3859default:
3860llvm_unreachable("Unknown SCEV min/max opcode");
3861 }
3862 },
3863 [&](constAPInt &C) {
3864// identity
3865if (IsMax)
3866return IsSigned ?C.isMinSignedValue() :C.isMinValue();
3867else
3868return IsSigned ?C.isMaxSignedValue() :C.isMaxValue();
3869 },
3870 [&](constAPInt &C) {
3871// absorber
3872if (IsMax)
3873return IsSigned ?C.isMaxSignedValue() :C.isMaxValue();
3874else
3875return IsSigned ?C.isMinSignedValue() :C.isMinValue();
3876 });
3877if (Folded)
3878return Folded;
3879
3880// Check if we have created the same expression before.
3881if (constSCEV *S = findExistingSCEVInCache(Kind, Ops)) {
3882return S;
3883 }
3884
3885// Find the first operation of the same kind
3886unsignedIdx = 0;
3887while (Idx < Ops.size() && Ops[Idx]->getSCEVType() < Kind)
3888 ++Idx;
3889
3890// Check to see if one of the operands is of the same kind. If so, expand its
3891// operands onto our operand list, and recurse to simplify.
3892if (Idx < Ops.size()) {
3893bool DeletedAny =false;
3894while (Ops[Idx]->getSCEVType() == Kind) {
3895constSCEVMinMaxExpr *SMME = cast<SCEVMinMaxExpr>(Ops[Idx]);
3896 Ops.erase(Ops.begin()+Idx);
3897append_range(Ops, SMME->operands());
3898 DeletedAny =true;
3899 }
3900
3901if (DeletedAny)
3902returngetMinMaxExpr(Kind, Ops);
3903 }
3904
3905// Okay, check to see if the same value occurs in the operand list twice. If
3906// so, delete one. Since we sorted the list, these values are required to
3907// be adjacent.
3908llvm::CmpInst::Predicate GEPred =
3909 IsSigned ?ICmpInst::ICMP_SGE :ICmpInst::ICMP_UGE;
3910llvm::CmpInst::Predicate LEPred =
3911 IsSigned ?ICmpInst::ICMP_SLE :ICmpInst::ICMP_ULE;
3912llvm::CmpInst::Predicate FirstPred = IsMax ? GEPred : LEPred;
3913llvm::CmpInst::Predicate SecondPred = IsMax ? LEPred : GEPred;
3914for (unsigned i = 0, e = Ops.size() - 1; i != e; ++i) {
3915if (Ops[i] == Ops[i + 1] ||
3916 isKnownViaNonRecursiveReasoning(FirstPred, Ops[i], Ops[i + 1])) {
3917// X op Y op Y --> X op Y
3918// X op Y --> X, if we know X, Y are ordered appropriately
3919 Ops.erase(Ops.begin() + i + 1, Ops.begin() + i + 2);
3920 --i;
3921 --e;
3922 }elseif (isKnownViaNonRecursiveReasoning(SecondPred, Ops[i],
3923 Ops[i + 1])) {
3924// X op Y --> Y, if we know X, Y are ordered appropriately
3925 Ops.erase(Ops.begin() + i, Ops.begin() + i + 1);
3926 --i;
3927 --e;
3928 }
3929 }
3930
3931if (Ops.size() == 1)return Ops[0];
3932
3933assert(!Ops.empty() &&"Reduced smax down to nothing!");
3934
3935// Okay, it looks like we really DO need an expr. Check to see if we
3936// already have one, otherwise create a new one.
3937FoldingSetNodeIDID;
3938ID.AddInteger(Kind);
3939for (constSCEV *Op : Ops)
3940ID.AddPointer(Op);
3941void *IP =nullptr;
3942constSCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP);
3943if (ExistingSCEV)
3944return ExistingSCEV;
3945constSCEV **O = SCEVAllocator.Allocate<constSCEV *>(Ops.size());
3946 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
3947SCEV *S =new (SCEVAllocator)
3948SCEVMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
3949
3950 UniqueSCEVs.InsertNode(S, IP);
3951registerUser(S, Ops);
3952return S;
3953}
3954
3955namespace{
3956
3957classSCEVSequentialMinMaxDeduplicatingVisitor final
3958 :publicSCEVVisitor<SCEVSequentialMinMaxDeduplicatingVisitor,
3959 std::optional<const SCEV *>> {
3960usingRetVal = std::optional<const SCEV *>;
3961usingBase =SCEVVisitor<SCEVSequentialMinMaxDeduplicatingVisitor, RetVal>;
3962
3963ScalarEvolution &SE;
3964constSCEVTypes RootKind;// Must be a sequential min/max expression.
3965constSCEVTypes NonSequentialRootKind;// Non-sequential variant of RootKind.
3966SmallPtrSet<const SCEV *, 16> SeenOps;
3967
3968bool canRecurseInto(SCEVTypes Kind) const{
3969// We can only recurse into the SCEV expression of the same effective type
3970// as the type of our root SCEV expression.
3971return RootKind == Kind || NonSequentialRootKind == Kind;
3972 };
3973
3974 RetVal visitAnyMinMaxExpr(constSCEV *S) {
3975assert((isa<SCEVMinMaxExpr>(S) || isa<SCEVSequentialMinMaxExpr>(S)) &&
3976"Only for min/max expressions.");
3977SCEVTypes Kind = S->getSCEVType();
3978
3979if (!canRecurseInto(Kind))
3980return S;
3981
3982auto *NAry = cast<SCEVNAryExpr>(S);
3983SmallVector<const SCEV *> NewOps;
3984bool Changed =visit(Kind, NAry->operands(), NewOps);
3985
3986if (!Changed)
3987return S;
3988if (NewOps.empty())
3989return std::nullopt;
3990
3991return isa<SCEVSequentialMinMaxExpr>(S)
3992 ? SE.getSequentialMinMaxExpr(Kind, NewOps)
3993 : SE.getMinMaxExpr(Kind, NewOps);
3994 }
3995
3996 RetValvisit(constSCEV *S) {
3997// Has the whole operand been seen already?
3998if (!SeenOps.insert(S).second)
3999return std::nullopt;
4000return Base::visit(S);
4001 }
4002
4003public:
4004 SCEVSequentialMinMaxDeduplicatingVisitor(ScalarEvolution &SE,
4005SCEVTypes RootKind)
4006 : SE(SE), RootKind(RootKind),
4007 NonSequentialRootKind(
4008SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(
4009 RootKind)) {}
4010
4011bool/*Changed*/visit(SCEVTypes Kind,ArrayRef<const SCEV *> OrigOps,
4012SmallVectorImpl<const SCEV *> &NewOps) {
4013bool Changed =false;
4014SmallVector<const SCEV *> Ops;
4015 Ops.reserve(OrigOps.size());
4016
4017for (constSCEV *Op : OrigOps) {
4018 RetVal NewOp =visit(Op);
4019if (NewOp !=Op)
4020 Changed =true;
4021if (NewOp)
4022 Ops.emplace_back(*NewOp);
4023 }
4024
4025if (Changed)
4026 NewOps = std::move(Ops);
4027return Changed;
4028 }
4029
4030 RetVal visitConstant(constSCEVConstant *Constant) {returnConstant; }
4031
4032 RetVal visitVScale(constSCEVVScale *VScale) {return VScale; }
4033
4034 RetVal visitPtrToIntExpr(constSCEVPtrToIntExpr *Expr) {return Expr; }
4035
4036 RetVal visitTruncateExpr(constSCEVTruncateExpr *Expr) {return Expr; }
4037
4038 RetVal visitZeroExtendExpr(constSCEVZeroExtendExpr *Expr) {return Expr; }
4039
4040 RetVal visitSignExtendExpr(constSCEVSignExtendExpr *Expr) {return Expr; }
4041
4042 RetVal visitAddExpr(constSCEVAddExpr *Expr) {return Expr; }
4043
4044 RetVal visitMulExpr(constSCEVMulExpr *Expr) {return Expr; }
4045
4046 RetVal visitUDivExpr(constSCEVUDivExpr *Expr) {return Expr; }
4047
4048 RetVal visitAddRecExpr(constSCEVAddRecExpr *Expr) {return Expr; }
4049
4050 RetVal visitSMaxExpr(constSCEVSMaxExpr *Expr) {
4051return visitAnyMinMaxExpr(Expr);
4052 }
4053
4054 RetVal visitUMaxExpr(constSCEVUMaxExpr *Expr) {
4055return visitAnyMinMaxExpr(Expr);
4056 }
4057
4058 RetVal visitSMinExpr(constSCEVSMinExpr *Expr) {
4059return visitAnyMinMaxExpr(Expr);
4060 }
4061
4062 RetVal visitUMinExpr(constSCEVUMinExpr *Expr) {
4063return visitAnyMinMaxExpr(Expr);
4064 }
4065
4066 RetVal visitSequentialUMinExpr(constSCEVSequentialUMinExpr *Expr) {
4067return visitAnyMinMaxExpr(Expr);
4068 }
4069
4070 RetVal visitUnknown(constSCEVUnknown *Expr) {return Expr; }
4071
4072 RetVal visitCouldNotCompute(constSCEVCouldNotCompute *Expr) {return Expr; }
4073};
4074
4075}// namespace
4076
4077staticboolscevUnconditionallyPropagatesPoisonFromOperands(SCEVTypes Kind) {
4078switch (Kind) {
4079casescConstant:
4080casescVScale:
4081casescTruncate:
4082casescZeroExtend:
4083casescSignExtend:
4084casescPtrToInt:
4085casescAddExpr:
4086casescMulExpr:
4087casescUDivExpr:
4088casescAddRecExpr:
4089casescUMaxExpr:
4090casescSMaxExpr:
4091casescUMinExpr:
4092casescSMinExpr:
4093casescUnknown:
4094// If any operand is poison, the whole expression is poison.
4095returntrue;
4096casescSequentialUMinExpr:
4097// FIXME: if the *first* operand is poison, the whole expression is poison.
4098returnfalse;// Pessimistically, say that it does not propagate poison.
4099casescCouldNotCompute:
4100llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
4101 }
4102llvm_unreachable("Unknown SCEV kind!");
4103}
4104
4105namespace{
4106// The only way poison may be introduced in a SCEV expression is from a
4107// poison SCEVUnknown (ConstantExprs are also represented as SCEVUnknown,
4108// not SCEVConstant). Notably, nowrap flags in SCEV nodes can *not*
4109// introduce poison -- they encode guaranteed, non-speculated knowledge.
4110//
4111// Additionally, all SCEV nodes propagate poison from inputs to outputs,
4112// with the notable exception of umin_seq, where only poison from the first
4113// operand is (unconditionally) propagated.
4114structSCEVPoisonCollector {
4115bool LookThroughMaybePoisonBlocking;
4116SmallPtrSet<const SCEVUnknown *, 4> MaybePoison;
4117 SCEVPoisonCollector(bool LookThroughMaybePoisonBlocking)
4118 : LookThroughMaybePoisonBlocking(LookThroughMaybePoisonBlocking) {}
4119
4120bool follow(constSCEV *S) {
4121if (!LookThroughMaybePoisonBlocking &&
4122 !scevUnconditionallyPropagatesPoisonFromOperands(S->getSCEVType()))
4123returnfalse;
4124
4125if (auto *SU = dyn_cast<SCEVUnknown>(S)) {
4126if (!isGuaranteedNotToBePoison(SU->getValue()))
4127 MaybePoison.insert(SU);
4128 }
4129returntrue;
4130 }
4131bool isDone() const{returnfalse; }
4132};
4133}// namespace
4134
4135/// Return true if V is poison given that AssumedPoison is already poison.
4136staticboolimpliesPoison(constSCEV *AssumedPoison,constSCEV *S) {
4137// First collect all SCEVs that might result in AssumedPoison to be poison.
4138// We need to look through potentially poison-blocking operations here,
4139// because we want to find all SCEVs that *might* result in poison, not only
4140// those that are *required* to.
4141 SCEVPoisonCollector PC1(/* LookThroughMaybePoisonBlocking */true);
4142visitAll(AssumedPoison, PC1);
4143
4144// AssumedPoison is never poison. As the assumption is false, the implication
4145// is true. Don't bother walking the other SCEV in this case.
4146if (PC1.MaybePoison.empty())
4147returntrue;
4148
4149// Collect all SCEVs in S that, if poison, *will* result in S being poison
4150// as well. We cannot look through potentially poison-blocking operations
4151// here, as their arguments only *may* make the result poison.
4152 SCEVPoisonCollector PC2(/* LookThroughMaybePoisonBlocking */false);
4153visitAll(S, PC2);
4154
4155// Make sure that no matter which SCEV in PC1.MaybePoison is actually poison,
4156// it will also make S poison by being part of PC2.MaybePoison.
4157returnllvm::set_is_subset(PC1.MaybePoison, PC2.MaybePoison);
4158}
4159
4160voidScalarEvolution::getPoisonGeneratingValues(
4161SmallPtrSetImpl<const Value *> &Result,constSCEV *S) {
4162 SCEVPoisonCollector PC(/* LookThroughMaybePoisonBlocking */false);
4163visitAll(S, PC);
4164for (constSCEVUnknown *SU : PC.MaybePoison)
4165 Result.insert(SU->getValue());
4166}
4167
4168boolScalarEvolution::canReuseInstruction(
4169constSCEV *S,Instruction *I,
4170SmallVectorImpl<Instruction *> &DropPoisonGeneratingInsts) {
4171// If the instruction cannot be poison, it's always safe to reuse.
4172if (programUndefinedIfPoison(I))
4173returntrue;
4174
4175// Otherwise, it is possible that I is more poisonous that S. Collect the
4176// poison-contributors of S, and then check whether I has any additional
4177// poison-contributors. Poison that is contributed through poison-generating
4178// flags is handled by dropping those flags instead.
4179SmallPtrSet<const Value *, 8> PoisonVals;
4180getPoisonGeneratingValues(PoisonVals, S);
4181
4182SmallVector<Value *> Worklist;
4183SmallPtrSet<Value *, 8> Visited;
4184 Worklist.push_back(I);
4185while (!Worklist.empty()) {
4186Value *V = Worklist.pop_back_val();
4187if (!Visited.insert(V).second)
4188continue;
4189
4190// Avoid walking large instruction graphs.
4191if (Visited.size() > 16)
4192returnfalse;
4193
4194// Either the value can't be poison, or the S would also be poison if it
4195// is.
4196if (PoisonVals.contains(V) || ::isGuaranteedNotToBePoison(V))
4197continue;
4198
4199auto *I = dyn_cast<Instruction>(V);
4200if (!I)
4201returnfalse;
4202
4203// Disjoint or instructions are interpreted as adds by SCEV. However, we
4204// can't replace an arbitrary add with disjoint or, even if we drop the
4205// flag. We would need to convert the or into an add.
4206if (auto *PDI = dyn_cast<PossiblyDisjointInst>(I))
4207if (PDI->isDisjoint())
4208returnfalse;
4209
4210// FIXME: Ignore vscale, even though it technically could be poison. Do this
4211// because SCEV currently assumes it can't be poison. Remove this special
4212// case once we proper model when vscale can be poison.
4213if (auto *II = dyn_cast<IntrinsicInst>(I);
4214II &&II->getIntrinsicID() == Intrinsic::vscale)
4215continue;
4216
4217if (canCreatePoison(cast<Operator>(I),/*ConsiderFlagsAndMetadata*/false))
4218returnfalse;
4219
4220// If the instruction can't create poison, we can recurse to its operands.
4221if (I->hasPoisonGeneratingAnnotations())
4222 DropPoisonGeneratingInsts.push_back(I);
4223
4224for (Value *Op :I->operands())
4225 Worklist.push_back(Op);
4226 }
4227returntrue;
4228}
4229
4230constSCEV *
4231ScalarEvolution::getSequentialMinMaxExpr(SCEVTypes Kind,
4232SmallVectorImpl<const SCEV *> &Ops) {
4233assert(SCEVSequentialMinMaxExpr::isSequentialMinMaxType(Kind) &&
4234"Not a SCEVSequentialMinMaxExpr!");
4235assert(!Ops.empty() &&"Cannot get empty (u|s)(min|max)!");
4236if (Ops.size() == 1)
4237return Ops[0];
4238#ifndef NDEBUG
4239Type *ETy =getEffectiveSCEVType(Ops[0]->getType());
4240for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
4241assert(getEffectiveSCEVType(Ops[i]->getType()) == ETy &&
4242"Operand types don't match!");
4243assert(Ops[0]->getType()->isPointerTy() ==
4244 Ops[i]->getType()->isPointerTy() &&
4245"min/max should be consistently pointerish");
4246 }
4247#endif
4248
4249// Note that SCEVSequentialMinMaxExpr is *NOT* commutative,
4250// so we can *NOT* do any kind of sorting of the expressions!
4251
4252// Check if we have created the same expression before.
4253if (constSCEV *S = findExistingSCEVInCache(Kind, Ops))
4254return S;
4255
4256// FIXME: there are *some* simplifications that we can do here.
4257
4258// Keep only the first instance of an operand.
4259 {
4260 SCEVSequentialMinMaxDeduplicatingVisitor Deduplicator(*this, Kind);
4261bool Changed = Deduplicator.visit(Kind, Ops, Ops);
4262if (Changed)
4263returngetSequentialMinMaxExpr(Kind, Ops);
4264 }
4265
4266// Check to see if one of the operands is of the same kind. If so, expand its
4267// operands onto our operand list, and recurse to simplify.
4268 {
4269unsignedIdx = 0;
4270bool DeletedAny =false;
4271while (Idx < Ops.size()) {
4272if (Ops[Idx]->getSCEVType() != Kind) {
4273 ++Idx;
4274continue;
4275 }
4276constauto *SMME = cast<SCEVSequentialMinMaxExpr>(Ops[Idx]);
4277 Ops.erase(Ops.begin() +Idx);
4278 Ops.insert(Ops.begin() +Idx, SMME->operands().begin(),
4279 SMME->operands().end());
4280 DeletedAny =true;
4281 }
4282
4283if (DeletedAny)
4284returngetSequentialMinMaxExpr(Kind, Ops);
4285 }
4286
4287constSCEV *SaturationPoint;
4288ICmpInst::Predicate Pred;
4289switch (Kind) {
4290casescSequentialUMinExpr:
4291 SaturationPoint =getZero(Ops[0]->getType());
4292 Pred =ICmpInst::ICMP_ULE;
4293break;
4294default:
4295llvm_unreachable("Not a sequential min/max type.");
4296 }
4297
4298for (unsigned i = 1, e = Ops.size(); i != e; ++i) {
4299if (!isGuaranteedNotToCauseUB(Ops[i]))
4300continue;
4301// We can replace %x umin_seq %y with %x umin %y if either:
4302// * %y being poison implies %x is also poison.
4303// * %x cannot be the saturating value (e.g. zero for umin).
4304if (::impliesPoison(Ops[i], Ops[i - 1]) ||
4305 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_NE, Ops[i - 1],
4306 SaturationPoint)) {
4307SmallVector<const SCEV *> SeqOps = {Ops[i - 1], Ops[i]};
4308 Ops[i - 1] =getMinMaxExpr(
4309SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(Kind),
4310 SeqOps);
4311 Ops.erase(Ops.begin() + i);
4312returngetSequentialMinMaxExpr(Kind, Ops);
4313 }
4314// Fold %x umin_seq %y to %x if %x ule %y.
4315// TODO: We might be able to prove the predicate for a later operand.
4316if (isKnownViaNonRecursiveReasoning(Pred, Ops[i - 1], Ops[i])) {
4317 Ops.erase(Ops.begin() + i);
4318returngetSequentialMinMaxExpr(Kind, Ops);
4319 }
4320 }
4321
4322// Okay, it looks like we really DO need an expr. Check to see if we
4323// already have one, otherwise create a new one.
4324FoldingSetNodeIDID;
4325ID.AddInteger(Kind);
4326for (constSCEV *Op : Ops)
4327ID.AddPointer(Op);
4328void *IP =nullptr;
4329constSCEV *ExistingSCEV = UniqueSCEVs.FindNodeOrInsertPos(ID, IP);
4330if (ExistingSCEV)
4331return ExistingSCEV;
4332
4333constSCEV **O = SCEVAllocator.Allocate<constSCEV *>(Ops.size());
4334 std::uninitialized_copy(Ops.begin(), Ops.end(), O);
4335SCEV *S =new (SCEVAllocator)
4336SCEVSequentialMinMaxExpr(ID.Intern(SCEVAllocator), Kind, O, Ops.size());
4337
4338 UniqueSCEVs.InsertNode(S, IP);
4339registerUser(S, Ops);
4340return S;
4341}
4342
4343constSCEV *ScalarEvolution::getSMaxExpr(constSCEV *LHS,constSCEV *RHS) {
4344SmallVector<const SCEV *, 2> Ops = {LHS,RHS};
4345returngetSMaxExpr(Ops);
4346}
4347
4348constSCEV *ScalarEvolution::getSMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
4349returngetMinMaxExpr(scSMaxExpr, Ops);
4350}
4351
4352constSCEV *ScalarEvolution::getUMaxExpr(constSCEV *LHS,constSCEV *RHS) {
4353SmallVector<const SCEV *, 2> Ops = {LHS,RHS};
4354returngetUMaxExpr(Ops);
4355}
4356
4357constSCEV *ScalarEvolution::getUMaxExpr(SmallVectorImpl<const SCEV *> &Ops) {
4358returngetMinMaxExpr(scUMaxExpr, Ops);
4359}
4360
4361constSCEV *ScalarEvolution::getSMinExpr(constSCEV *LHS,
4362constSCEV *RHS) {
4363SmallVector<const SCEV *, 2> Ops = {LHS,RHS };
4364returngetSMinExpr(Ops);
4365}
4366
4367constSCEV *ScalarEvolution::getSMinExpr(SmallVectorImpl<const SCEV *> &Ops) {
4368returngetMinMaxExpr(scSMinExpr, Ops);
4369}
4370
4371constSCEV *ScalarEvolution::getUMinExpr(constSCEV *LHS,constSCEV *RHS,
4372bool Sequential) {
4373SmallVector<const SCEV *, 2> Ops = {LHS,RHS };
4374returngetUMinExpr(Ops, Sequential);
4375}
4376
4377constSCEV *ScalarEvolution::getUMinExpr(SmallVectorImpl<const SCEV *> &Ops,
4378bool Sequential) {
4379return Sequential ?getSequentialMinMaxExpr(scSequentialUMinExpr, Ops)
4380 :getMinMaxExpr(scUMinExpr, Ops);
4381}
4382
4383constSCEV *
4384ScalarEvolution::getSizeOfExpr(Type *IntTy,TypeSizeSize) {
4385constSCEV *Res =getConstant(IntTy,Size.getKnownMinValue());
4386if (Size.isScalable())
4387 Res =getMulExpr(Res,getVScale(IntTy));
4388return Res;
4389}
4390
4391constSCEV *ScalarEvolution::getSizeOfExpr(Type *IntTy,Type *AllocTy) {
4392returngetSizeOfExpr(IntTy,getDataLayout().getTypeAllocSize(AllocTy));
4393}
4394
4395constSCEV *ScalarEvolution::getStoreSizeOfExpr(Type *IntTy,Type *StoreTy) {
4396returngetSizeOfExpr(IntTy,getDataLayout().getTypeStoreSize(StoreTy));
4397}
4398
4399constSCEV *ScalarEvolution::getOffsetOfExpr(Type *IntTy,
4400StructType *STy,
4401unsigned FieldNo) {
4402// We can bypass creating a target-independent constant expression and then
4403// folding it back into a ConstantInt. This is just a compile-time
4404// optimization.
4405constStructLayout *SL =getDataLayout().getStructLayout(STy);
4406assert(!SL->getSizeInBits().isScalable() &&
4407"Cannot get offset for structure containing scalable vector types");
4408returngetConstant(IntTy, SL->getElementOffset(FieldNo));
4409}
4410
4411constSCEV *ScalarEvolution::getUnknown(Value *V) {
4412// Don't attempt to do anything other than create a SCEVUnknown object
4413// here. createSCEV only calls getUnknown after checking for all other
4414// interesting possibilities, and any other code that calls getUnknown
4415// is doing so in order to hide a value from SCEV canonicalization.
4416
4417FoldingSetNodeIDID;
4418ID.AddInteger(scUnknown);
4419ID.AddPointer(V);
4420void *IP =nullptr;
4421if (SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) {
4422assert(cast<SCEVUnknown>(S)->getValue() == V &&
4423"Stale SCEVUnknown in uniquing map!");
4424return S;
4425 }
4426SCEV *S =new (SCEVAllocator)SCEVUnknown(ID.Intern(SCEVAllocator), V,this,
4427 FirstUnknown);
4428 FirstUnknown = cast<SCEVUnknown>(S);
4429 UniqueSCEVs.InsertNode(S, IP);
4430return S;
4431}
4432
4433//===----------------------------------------------------------------------===//
4434// Basic SCEV Analysis and PHI Idiom Recognition Code
4435//
4436
4437/// Test if values of the given type are analyzable within the SCEV
4438/// framework. This primarily includes integer types, and it can optionally
4439/// include pointer types if the ScalarEvolution class has access to
4440/// target-specific information.
4441boolScalarEvolution::isSCEVable(Type *Ty) const{
4442// Integers and pointers are always SCEVable.
4443return Ty->isIntOrPtrTy();
4444}
4445
4446/// Return the size in bits of the specified type, for which isSCEVable must
4447/// return true.
4448uint64_tScalarEvolution::getTypeSizeInBits(Type *Ty) const{
4449assert(isSCEVable(Ty) &&"Type is not SCEVable!");
4450if (Ty->isPointerTy())
4451returngetDataLayout().getIndexTypeSizeInBits(Ty);
4452returngetDataLayout().getTypeSizeInBits(Ty);
4453}
4454
4455/// Return a type with the same bitwidth as the given type and which represents
4456/// how SCEV will treat the given type, for which isSCEVable must return
4457/// true. For pointer types, this is the pointer index sized integer type.
4458Type *ScalarEvolution::getEffectiveSCEVType(Type *Ty) const{
4459assert(isSCEVable(Ty) &&"Type is not SCEVable!");
4460
4461if (Ty->isIntegerTy())
4462return Ty;
4463
4464// The only other support type is pointer.
4465assert(Ty->isPointerTy() &&"Unexpected non-pointer non-integer type!");
4466returngetDataLayout().getIndexType(Ty);
4467}
4468
4469Type *ScalarEvolution::getWiderType(Type *T1,Type *T2) const{
4470returngetTypeSizeInBits(T1) >=getTypeSizeInBits(T2) ?T1 : T2;
4471}
4472
4473boolScalarEvolution::instructionCouldExistWithOperands(constSCEV *A,
4474constSCEV *B) {
4475 /// For a valid use point to exist, the defining scope of one operand
4476 /// must dominate the other.
4477bool PreciseA, PreciseB;
4478auto *ScopeA = getDefiningScopeBound({A}, PreciseA);
4479auto *ScopeB = getDefiningScopeBound({B}, PreciseB);
4480if (!PreciseA || !PreciseB)
4481// Can't tell.
4482returnfalse;
4483return (ScopeA == ScopeB) || DT.dominates(ScopeA, ScopeB) ||
4484 DT.dominates(ScopeB, ScopeA);
4485}
4486
4487constSCEV *ScalarEvolution::getCouldNotCompute() {
4488return CouldNotCompute.get();
4489}
4490
4491bool ScalarEvolution::checkValidity(constSCEV *S) const{
4492bool ContainsNulls =SCEVExprContains(S, [](constSCEV *S) {
4493auto *SU = dyn_cast<SCEVUnknown>(S);
4494return SU && SU->getValue() ==nullptr;
4495 });
4496
4497return !ContainsNulls;
4498}
4499
4500boolScalarEvolution::containsAddRecurrence(constSCEV *S) {
4501HasRecMapType::iteratorI = HasRecMap.find(S);
4502if (I != HasRecMap.end())
4503returnI->second;
4504
4505bool FoundAddRec =
4506SCEVExprContains(S, [](constSCEV *S) {return isa<SCEVAddRecExpr>(S); });
4507 HasRecMap.insert({S, FoundAddRec});
4508return FoundAddRec;
4509}
4510
4511/// Return the ValueOffsetPair set for \p S. \p S can be represented
4512/// by the value and offset from any ValueOffsetPair in the set.
4513ArrayRef<Value *> ScalarEvolution::getSCEVValues(constSCEV *S) {
4514ExprValueMapType::iterator SI = ExprValueMap.find_as(S);
4515if (SI == ExprValueMap.end())
4516return {};
4517return SI->second.getArrayRef();
4518}
4519
4520/// Erase Value from ValueExprMap and ExprValueMap. ValueExprMap.erase(V)
4521/// cannot be used separately. eraseValueFromMap should be used to remove
4522/// V from ValueExprMap and ExprValueMap at the same time.
4523void ScalarEvolution::eraseValueFromMap(Value *V) {
4524ValueExprMapType::iteratorI = ValueExprMap.find_as(V);
4525if (I != ValueExprMap.end()) {
4526auto EVIt = ExprValueMap.find(I->second);
4527bool Removed = EVIt->second.remove(V);
4528 (void) Removed;
4529assert(Removed &&"Value not in ExprValueMap?");
4530 ValueExprMap.erase(I);
4531 }
4532}
4533
4534void ScalarEvolution::insertValueToMap(Value *V,constSCEV *S) {
4535// A recursive query may have already computed the SCEV. It should be
4536// equivalent, but may not necessarily be exactly the same, e.g. due to lazily
4537// inferred nowrap flags.
4538auto It = ValueExprMap.find_as(V);
4539if (It == ValueExprMap.end()) {
4540 ValueExprMap.insert({SCEVCallbackVH(V,this), S});
4541 ExprValueMap[S].insert(V);
4542 }
4543}
4544
4545/// Return an existing SCEV if it exists, otherwise analyze the expression and
4546/// create a new one.
4547constSCEV *ScalarEvolution::getSCEV(Value *V) {
4548assert(isSCEVable(V->getType()) &&"Value is not SCEVable!");
4549
4550if (constSCEV *S =getExistingSCEV(V))
4551return S;
4552return createSCEVIter(V);
4553}
4554
4555constSCEV *ScalarEvolution::getExistingSCEV(Value *V) {
4556assert(isSCEVable(V->getType()) &&"Value is not SCEVable!");
4557
4558ValueExprMapType::iteratorI = ValueExprMap.find_as(V);
4559if (I != ValueExprMap.end()) {
4560constSCEV *S =I->second;
4561assert(checkValidity(S) &&
4562"existing SCEV has not been properly invalidated");
4563return S;
4564 }
4565returnnullptr;
4566}
4567
4568/// Return a SCEV corresponding to -V = -1*V
4569constSCEV *ScalarEvolution::getNegativeSCEV(constSCEV *V,
4570SCEV::NoWrapFlags Flags) {
4571if (constSCEVConstant *VC = dyn_cast<SCEVConstant>(V))
4572returngetConstant(
4573 cast<ConstantInt>(ConstantExpr::getNeg(VC->getValue())));
4574
4575Type *Ty = V->getType();
4576 Ty =getEffectiveSCEVType(Ty);
4577returngetMulExpr(V,getMinusOne(Ty), Flags);
4578}
4579
4580/// If Expr computes ~A, return A else return nullptr
4581staticconstSCEV *MatchNotExpr(constSCEV *Expr) {
4582constSCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(Expr);
4583if (!Add ||Add->getNumOperands() != 2 ||
4584 !Add->getOperand(0)->isAllOnesValue())
4585returnnullptr;
4586
4587constSCEVMulExpr *AddRHS = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
4588if (!AddRHS || AddRHS->getNumOperands() != 2 ||
4589 !AddRHS->getOperand(0)->isAllOnesValue())
4590returnnullptr;
4591
4592return AddRHS->getOperand(1);
4593}
4594
4595/// Return a SCEV corresponding to ~V = -1-V
4596constSCEV *ScalarEvolution::getNotSCEV(constSCEV *V) {
4597assert(!V->getType()->isPointerTy() &&"Can't negate pointer");
4598
4599if (constSCEVConstant *VC = dyn_cast<SCEVConstant>(V))
4600returngetConstant(
4601 cast<ConstantInt>(ConstantExpr::getNot(VC->getValue())));
4602
4603// Fold ~(u|s)(min|max)(~x, ~y) to (u|s)(max|min)(x, y)
4604if (constSCEVMinMaxExpr *MME = dyn_cast<SCEVMinMaxExpr>(V)) {
4605auto MatchMinMaxNegation = [&](constSCEVMinMaxExpr *MME) {
4606SmallVector<const SCEV *, 2> MatchedOperands;
4607for (constSCEV *Operand : MME->operands()) {
4608constSCEV *Matched =MatchNotExpr(Operand);
4609if (!Matched)
4610return (constSCEV *)nullptr;
4611 MatchedOperands.push_back(Matched);
4612 }
4613returngetMinMaxExpr(SCEVMinMaxExpr::negate(MME->getSCEVType()),
4614 MatchedOperands);
4615 };
4616if (constSCEV *Replaced = MatchMinMaxNegation(MME))
4617return Replaced;
4618 }
4619
4620Type *Ty = V->getType();
4621 Ty =getEffectiveSCEVType(Ty);
4622returngetMinusSCEV(getMinusOne(Ty), V);
4623}
4624
4625constSCEV *ScalarEvolution::removePointerBase(constSCEV *P) {
4626assert(P->getType()->isPointerTy());
4627
4628if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(P)) {
4629// The base of an AddRec is the first operand.
4630SmallVector<const SCEV *> Ops{AddRec->operands()};
4631 Ops[0] =removePointerBase(Ops[0]);
4632// Don't try to transfer nowrap flags for now. We could in some cases
4633// (for example, if pointer operand of the AddRec is a SCEVUnknown).
4634returngetAddRecExpr(Ops, AddRec->getLoop(),SCEV::FlagAnyWrap);
4635 }
4636if (auto *Add = dyn_cast<SCEVAddExpr>(P)) {
4637// The base of an Add is the pointer operand.
4638SmallVector<const SCEV *> Ops{Add->operands()};
4639constSCEV **PtrOp =nullptr;
4640for (constSCEV *&AddOp : Ops) {
4641if (AddOp->getType()->isPointerTy()) {
4642assert(!PtrOp &&"Cannot have multiple pointer ops");
4643 PtrOp = &AddOp;
4644 }
4645 }
4646 *PtrOp =removePointerBase(*PtrOp);
4647// Don't try to transfer nowrap flags for now. We could in some cases
4648// (for example, if the pointer operand of the Add is a SCEVUnknown).
4649returngetAddExpr(Ops);
4650 }
4651// Any other expression must be a pointer base.
4652returngetZero(P->getType());
4653}
4654
4655constSCEV *ScalarEvolution::getMinusSCEV(constSCEV *LHS,constSCEV *RHS,
4656SCEV::NoWrapFlags Flags,
4657unsignedDepth) {
4658// Fast path: X - X --> 0.
4659if (LHS ==RHS)
4660returngetZero(LHS->getType());
4661
4662// If we subtract two pointers with different pointer bases, bail.
4663// Eventually, we're going to add an assertion to getMulExpr that we
4664// can't multiply by a pointer.
4665if (RHS->getType()->isPointerTy()) {
4666if (!LHS->getType()->isPointerTy() ||
4667getPointerBase(LHS) !=getPointerBase(RHS))
4668returngetCouldNotCompute();
4669LHS =removePointerBase(LHS);
4670RHS =removePointerBase(RHS);
4671 }
4672
4673// We represent LHS - RHS as LHS + (-1)*RHS. This transformation
4674// makes it so that we cannot make much use of NUW.
4675auto AddFlags =SCEV::FlagAnyWrap;
4676constbool RHSIsNotMinSigned =
4677 !getSignedRangeMin(RHS).isMinSignedValue();
4678if (hasFlags(Flags,SCEV::FlagNSW)) {
4679// Let M be the minimum representable signed value. Then (-1)*RHS
4680// signed-wraps if and only if RHS is M. That can happen even for
4681// a NSW subtraction because e.g. (-1)*M signed-wraps even though
4682// -1 - M does not. So to transfer NSW from LHS - RHS to LHS +
4683// (-1)*RHS, we need to prove that RHS != M.
4684//
4685// If LHS is non-negative and we know that LHS - RHS does not
4686// signed-wrap, then RHS cannot be M. So we can rule out signed-wrap
4687// either by proving that RHS > M or that LHS >= 0.
4688if (RHSIsNotMinSigned ||isKnownNonNegative(LHS)) {
4689 AddFlags =SCEV::FlagNSW;
4690 }
4691 }
4692
4693// FIXME: Find a correct way to transfer NSW to (-1)*M when LHS -
4694// RHS is NSW and LHS >= 0.
4695//
4696// The difficulty here is that the NSW flag may have been proven
4697// relative to a loop that is to be found in a recurrence in LHS and
4698// not in RHS. Applying NSW to (-1)*M may then let the NSW have a
4699// larger scope than intended.
4700auto NegFlags = RHSIsNotMinSigned ?SCEV::FlagNSW :SCEV::FlagAnyWrap;
4701
4702returngetAddExpr(LHS,getNegativeSCEV(RHS, NegFlags), AddFlags,Depth);
4703}
4704
4705constSCEV *ScalarEvolution::getTruncateOrZeroExtend(constSCEV *V,Type *Ty,
4706unsignedDepth) {
4707Type *SrcTy = V->getType();
4708assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4709"Cannot truncate or zero extend with non-integer arguments!");
4710if (getTypeSizeInBits(SrcTy) ==getTypeSizeInBits(Ty))
4711return V;// No conversion
4712if (getTypeSizeInBits(SrcTy) >getTypeSizeInBits(Ty))
4713returngetTruncateExpr(V, Ty,Depth);
4714returngetZeroExtendExpr(V, Ty,Depth);
4715}
4716
4717constSCEV *ScalarEvolution::getTruncateOrSignExtend(constSCEV *V,Type *Ty,
4718unsignedDepth) {
4719Type *SrcTy = V->getType();
4720assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4721"Cannot truncate or zero extend with non-integer arguments!");
4722if (getTypeSizeInBits(SrcTy) ==getTypeSizeInBits(Ty))
4723return V;// No conversion
4724if (getTypeSizeInBits(SrcTy) >getTypeSizeInBits(Ty))
4725returngetTruncateExpr(V, Ty,Depth);
4726returngetSignExtendExpr(V, Ty,Depth);
4727}
4728
4729constSCEV *
4730ScalarEvolution::getNoopOrZeroExtend(constSCEV *V,Type *Ty) {
4731Type *SrcTy = V->getType();
4732assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4733"Cannot noop or zero extend with non-integer arguments!");
4734assert(getTypeSizeInBits(SrcTy) <=getTypeSizeInBits(Ty) &&
4735"getNoopOrZeroExtend cannot truncate!");
4736if (getTypeSizeInBits(SrcTy) ==getTypeSizeInBits(Ty))
4737return V;// No conversion
4738returngetZeroExtendExpr(V, Ty);
4739}
4740
4741constSCEV *
4742ScalarEvolution::getNoopOrSignExtend(constSCEV *V,Type *Ty) {
4743Type *SrcTy = V->getType();
4744assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4745"Cannot noop or sign extend with non-integer arguments!");
4746assert(getTypeSizeInBits(SrcTy) <=getTypeSizeInBits(Ty) &&
4747"getNoopOrSignExtend cannot truncate!");
4748if (getTypeSizeInBits(SrcTy) ==getTypeSizeInBits(Ty))
4749return V;// No conversion
4750returngetSignExtendExpr(V, Ty);
4751}
4752
4753constSCEV *
4754ScalarEvolution::getNoopOrAnyExtend(constSCEV *V,Type *Ty) {
4755Type *SrcTy = V->getType();
4756assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4757"Cannot noop or any extend with non-integer arguments!");
4758assert(getTypeSizeInBits(SrcTy) <=getTypeSizeInBits(Ty) &&
4759"getNoopOrAnyExtend cannot truncate!");
4760if (getTypeSizeInBits(SrcTy) ==getTypeSizeInBits(Ty))
4761return V;// No conversion
4762returngetAnyExtendExpr(V, Ty);
4763}
4764
4765constSCEV *
4766ScalarEvolution::getTruncateOrNoop(constSCEV *V,Type *Ty) {
4767Type *SrcTy = V->getType();
4768assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4769"Cannot truncate or noop with non-integer arguments!");
4770assert(getTypeSizeInBits(SrcTy) >=getTypeSizeInBits(Ty) &&
4771"getTruncateOrNoop cannot extend!");
4772if (getTypeSizeInBits(SrcTy) ==getTypeSizeInBits(Ty))
4773return V;// No conversion
4774returngetTruncateExpr(V, Ty);
4775}
4776
4777constSCEV *ScalarEvolution::getUMaxFromMismatchedTypes(constSCEV *LHS,
4778constSCEV *RHS) {
4779constSCEV *PromotedLHS =LHS;
4780constSCEV *PromotedRHS =RHS;
4781
4782if (getTypeSizeInBits(LHS->getType()) >getTypeSizeInBits(RHS->getType()))
4783 PromotedRHS =getZeroExtendExpr(RHS,LHS->getType());
4784else
4785 PromotedLHS =getNoopOrZeroExtend(LHS,RHS->getType());
4786
4787returngetUMaxExpr(PromotedLHS, PromotedRHS);
4788}
4789
4790constSCEV *ScalarEvolution::getUMinFromMismatchedTypes(constSCEV *LHS,
4791constSCEV *RHS,
4792bool Sequential) {
4793SmallVector<const SCEV *, 2> Ops = {LHS,RHS };
4794returngetUMinFromMismatchedTypes(Ops, Sequential);
4795}
4796
4797constSCEV *
4798ScalarEvolution::getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV *> &Ops,
4799bool Sequential) {
4800assert(!Ops.empty() &&"At least one operand must be!");
4801// Trivial case.
4802if (Ops.size() == 1)
4803return Ops[0];
4804
4805// Find the max type first.
4806Type *MaxType =nullptr;
4807for (constauto *S : Ops)
4808if (MaxType)
4809 MaxType =getWiderType(MaxType, S->getType());
4810else
4811 MaxType = S->getType();
4812assert(MaxType &&"Failed to find maximum type!");
4813
4814// Extend all ops to max type.
4815SmallVector<const SCEV *, 2> PromotedOps;
4816for (constauto *S : Ops)
4817 PromotedOps.push_back(getNoopOrZeroExtend(S, MaxType));
4818
4819// Generate umin.
4820returngetUMinExpr(PromotedOps, Sequential);
4821}
4822
4823constSCEV *ScalarEvolution::getPointerBase(constSCEV *V) {
4824// A pointer operand may evaluate to a nonpointer expression, such as null.
4825if (!V->getType()->isPointerTy())
4826return V;
4827
4828while (true) {
4829if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
4830 V = AddRec->getStart();
4831 }elseif (auto *Add = dyn_cast<SCEVAddExpr>(V)) {
4832constSCEV *PtrOp =nullptr;
4833for (constSCEV *AddOp :Add->operands()) {
4834if (AddOp->getType()->isPointerTy()) {
4835assert(!PtrOp &&"Cannot have multiple pointer ops");
4836 PtrOp = AddOp;
4837 }
4838 }
4839assert(PtrOp &&"Must have pointer op");
4840 V = PtrOp;
4841 }else// Not something we can look further into.
4842return V;
4843 }
4844}
4845
4846/// Push users of the given Instruction onto the given Worklist.
4847staticvoidPushDefUseChildren(Instruction *I,
4848SmallVectorImpl<Instruction *> &Worklist,
4849SmallPtrSetImpl<Instruction *> &Visited) {
4850// Push the def-use children onto the Worklist stack.
4851for (User *U :I->users()) {
4852auto *UserInsn = cast<Instruction>(U);
4853if (Visited.insert(UserInsn).second)
4854 Worklist.push_back(UserInsn);
4855 }
4856}
4857
4858namespace{
4859
4860/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its start
4861/// expression in case its Loop is L. If it is not L then
4862/// if IgnoreOtherLoops is true then use AddRec itself
4863/// otherwise rewrite cannot be done.
4864/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4865classSCEVInitRewriter :publicSCEVRewriteVisitor<SCEVInitRewriter> {
4866public:
4867staticconstSCEV *rewrite(constSCEV *S,constLoop *L,ScalarEvolution &SE,
4868bool IgnoreOtherLoops =true) {
4869 SCEVInitRewriterRewriter(L, SE);
4870constSCEV *Result =Rewriter.visit(S);
4871if (Rewriter.hasSeenLoopVariantSCEVUnknown())
4872return SE.getCouldNotCompute();
4873returnRewriter.hasSeenOtherLoops() && !IgnoreOtherLoops
4874 ? SE.getCouldNotCompute()
4875 :Result;
4876 }
4877
4878constSCEV *visitUnknown(constSCEVUnknown *Expr) {
4879if (!SE.isLoopInvariant(Expr, L))
4880 SeenLoopVariantSCEVUnknown =true;
4881return Expr;
4882 }
4883
4884constSCEV *visitAddRecExpr(constSCEVAddRecExpr *Expr) {
4885// Only re-write AddRecExprs for this loop.
4886if (Expr->getLoop() == L)
4887return Expr->getStart();
4888 SeenOtherLoops =true;
4889return Expr;
4890 }
4891
4892bool hasSeenLoopVariantSCEVUnknown() {return SeenLoopVariantSCEVUnknown; }
4893
4894bool hasSeenOtherLoops() {return SeenOtherLoops; }
4895
4896private:
4897explicit SCEVInitRewriter(constLoop *L,ScalarEvolution &SE)
4898 :SCEVRewriteVisitor(SE),L(L) {}
4899
4900constLoop *L;
4901bool SeenLoopVariantSCEVUnknown =false;
4902bool SeenOtherLoops =false;
4903};
4904
4905/// Takes SCEV S and Loop L. For each AddRec sub-expression, use its post
4906/// increment expression in case its Loop is L. If it is not L then
4907/// use AddRec itself.
4908/// If SCEV contains non-invariant unknown SCEV rewrite cannot be done.
4909classSCEVPostIncRewriter :publicSCEVRewriteVisitor<SCEVPostIncRewriter> {
4910public:
4911staticconstSCEV *rewrite(constSCEV *S,constLoop *L,ScalarEvolution &SE) {
4912 SCEVPostIncRewriterRewriter(L, SE);
4913constSCEV *Result =Rewriter.visit(S);
4914returnRewriter.hasSeenLoopVariantSCEVUnknown()
4915 ? SE.getCouldNotCompute()
4916 :Result;
4917 }
4918
4919constSCEV *visitUnknown(constSCEVUnknown *Expr) {
4920if (!SE.isLoopInvariant(Expr, L))
4921 SeenLoopVariantSCEVUnknown =true;
4922return Expr;
4923 }
4924
4925constSCEV *visitAddRecExpr(constSCEVAddRecExpr *Expr) {
4926// Only re-write AddRecExprs for this loop.
4927if (Expr->getLoop() == L)
4928return Expr->getPostIncExpr(SE);
4929 SeenOtherLoops =true;
4930return Expr;
4931 }
4932
4933bool hasSeenLoopVariantSCEVUnknown() {return SeenLoopVariantSCEVUnknown; }
4934
4935bool hasSeenOtherLoops() {return SeenOtherLoops; }
4936
4937private:
4938explicit SCEVPostIncRewriter(constLoop *L,ScalarEvolution &SE)
4939 :SCEVRewriteVisitor(SE),L(L) {}
4940
4941constLoop *L;
4942bool SeenLoopVariantSCEVUnknown =false;
4943bool SeenOtherLoops =false;
4944};
4945
4946/// This class evaluates the compare condition by matching it against the
4947/// condition of loop latch. If there is a match we assume a true value
4948/// for the condition while building SCEV nodes.
4949classSCEVBackedgeConditionFolder
4950 :publicSCEVRewriteVisitor<SCEVBackedgeConditionFolder> {
4951public:
4952staticconstSCEV *rewrite(constSCEV *S,constLoop *L,
4953ScalarEvolution &SE) {
4954bool IsPosBECond =false;
4955Value *BECond =nullptr;
4956if (BasicBlock *Latch =L->getLoopLatch()) {
4957BranchInst *BI = dyn_cast<BranchInst>(Latch->getTerminator());
4958if (BI && BI->isConditional()) {
4959assert(BI->getSuccessor(0) != BI->getSuccessor(1) &&
4960"Both outgoing branches should not target same header!");
4961 BECond = BI->getCondition();
4962 IsPosBECond = BI->getSuccessor(0) ==L->getHeader();
4963 }else {
4964return S;
4965 }
4966 }
4967 SCEVBackedgeConditionFolderRewriter(L, BECond, IsPosBECond, SE);
4968returnRewriter.visit(S);
4969 }
4970
4971constSCEV *visitUnknown(constSCEVUnknown *Expr) {
4972constSCEV *Result = Expr;
4973bool InvariantF = SE.isLoopInvariant(Expr, L);
4974
4975if (!InvariantF) {
4976Instruction *I = cast<Instruction>(Expr->getValue());
4977switch (I->getOpcode()) {
4978case Instruction::Select: {
4979SelectInst *SI = cast<SelectInst>(I);
4980 std::optional<const SCEV *> Res =
4981 compareWithBackedgeCondition(SI->getCondition());
4982if (Res) {
4983bool IsOne = cast<SCEVConstant>(*Res)->getValue()->isOne();
4984Result = SE.getSCEV(IsOne ?SI->getTrueValue() :SI->getFalseValue());
4985 }
4986break;
4987 }
4988default: {
4989 std::optional<const SCEV *> Res = compareWithBackedgeCondition(I);
4990if (Res)
4991Result = *Res;
4992break;
4993 }
4994 }
4995 }
4996returnResult;
4997 }
4998
4999private:
5000explicit SCEVBackedgeConditionFolder(constLoop *L,Value *BECond,
5001bool IsPosBECond,ScalarEvolution &SE)
5002 :SCEVRewriteVisitor(SE),L(L), BackedgeCond(BECond),
5003 IsPositiveBECond(IsPosBECond) {}
5004
5005 std::optional<const SCEV *> compareWithBackedgeCondition(Value *IC);
5006
5007constLoop *L;
5008 /// Loop back condition.
5009Value *BackedgeCond =nullptr;
5010 /// Set to true if loop back is on positive branch condition.
5011bool IsPositiveBECond;
5012};
5013
5014std::optional<const SCEV *>
5015SCEVBackedgeConditionFolder::compareWithBackedgeCondition(Value *IC) {
5016
5017// If value matches the backedge condition for loop latch,
5018// then return a constant evolution node based on loopback
5019// branch taken.
5020if (BackedgeCond == IC)
5021return IsPositiveBECond ? SE.getOne(Type::getInt1Ty(SE.getContext()))
5022 : SE.getZero(Type::getInt1Ty(SE.getContext()));
5023return std::nullopt;
5024}
5025
5026classSCEVShiftRewriter :publicSCEVRewriteVisitor<SCEVShiftRewriter> {
5027public:
5028staticconstSCEV *rewrite(constSCEV *S,constLoop *L,
5029ScalarEvolution &SE) {
5030 SCEVShiftRewriterRewriter(L, SE);
5031constSCEV *Result =Rewriter.visit(S);
5032returnRewriter.isValid() ?Result : SE.getCouldNotCompute();
5033 }
5034
5035constSCEV *visitUnknown(constSCEVUnknown *Expr) {
5036// Only allow AddRecExprs for this loop.
5037if (!SE.isLoopInvariant(Expr, L))
5038 Valid =false;
5039return Expr;
5040 }
5041
5042constSCEV *visitAddRecExpr(constSCEVAddRecExpr *Expr) {
5043if (Expr->getLoop() == L && Expr->isAffine())
5044return SE.getMinusSCEV(Expr, Expr->getStepRecurrence(SE));
5045 Valid =false;
5046return Expr;
5047 }
5048
5049boolisValid() {return Valid; }
5050
5051private:
5052explicit SCEVShiftRewriter(constLoop *L,ScalarEvolution &SE)
5053 :SCEVRewriteVisitor(SE),L(L) {}
5054
5055constLoop *L;
5056bool Valid =true;
5057};
5058
5059}// end anonymous namespace
5060
5061SCEV::NoWrapFlags
5062ScalarEvolution::proveNoWrapViaConstantRanges(constSCEVAddRecExpr *AR) {
5063if (!AR->isAffine())
5064returnSCEV::FlagAnyWrap;
5065
5066usingOBO =OverflowingBinaryOperator;
5067
5068SCEV::NoWrapFlagsResult =SCEV::FlagAnyWrap;
5069
5070if (!AR->hasNoSelfWrap()) {
5071constSCEV *BECount =getConstantMaxBackedgeTakenCount(AR->getLoop());
5072if (constSCEVConstant *BECountMax = dyn_cast<SCEVConstant>(BECount)) {
5073ConstantRange StepCR =getSignedRange(AR->getStepRecurrence(*this));
5074constAPInt &BECountAP = BECountMax->getAPInt();
5075unsigned NoOverflowBitWidth =
5076 BECountAP.getActiveBits() + StepCR.getMinSignedBits();
5077if (NoOverflowBitWidth <=getTypeSizeInBits(AR->getType()))
5078Result =ScalarEvolution::setFlags(Result,SCEV::FlagNW);
5079 }
5080 }
5081
5082if (!AR->hasNoSignedWrap()) {
5083ConstantRange AddRecRange =getSignedRange(AR);
5084ConstantRange IncRange =getSignedRange(AR->getStepRecurrence(*this));
5085
5086auto NSWRegion =ConstantRange::makeGuaranteedNoWrapRegion(
5087 Instruction::Add, IncRange, OBO::NoSignedWrap);
5088if (NSWRegion.contains(AddRecRange))
5089Result =ScalarEvolution::setFlags(Result,SCEV::FlagNSW);
5090 }
5091
5092if (!AR->hasNoUnsignedWrap()) {
5093ConstantRange AddRecRange =getUnsignedRange(AR);
5094ConstantRange IncRange =getUnsignedRange(AR->getStepRecurrence(*this));
5095
5096auto NUWRegion =ConstantRange::makeGuaranteedNoWrapRegion(
5097 Instruction::Add, IncRange, OBO::NoUnsignedWrap);
5098if (NUWRegion.contains(AddRecRange))
5099Result =ScalarEvolution::setFlags(Result,SCEV::FlagNUW);
5100 }
5101
5102returnResult;
5103}
5104
5105SCEV::NoWrapFlags
5106ScalarEvolution::proveNoSignedWrapViaInduction(constSCEVAddRecExpr *AR) {
5107SCEV::NoWrapFlagsResult = AR->getNoWrapFlags();
5108
5109if (AR->hasNoSignedWrap())
5110returnResult;
5111
5112if (!AR->isAffine())
5113returnResult;
5114
5115// This function can be expensive, only try to prove NSW once per AddRec.
5116if (!SignedWrapViaInductionTried.insert(AR).second)
5117returnResult;
5118
5119constSCEV *Step = AR->getStepRecurrence(*this);
5120constLoop *L = AR->getLoop();
5121
5122// Check whether the backedge-taken count is SCEVCouldNotCompute.
5123// Note that this serves two purposes: It filters out loops that are
5124// simply not analyzable, and it covers the case where this code is
5125// being called from within backedge-taken count analysis, such that
5126// attempting to ask for the backedge-taken count would likely result
5127// in infinite recursion. In the later case, the analysis code will
5128// cope with a conservative value, and it will take care to purge
5129// that value once it has finished.
5130constSCEV *MaxBECount =getConstantMaxBackedgeTakenCount(L);
5131
5132// Normally, in the cases we can prove no-overflow via a
5133// backedge guarding condition, we can also compute a backedge
5134// taken count for the loop. The exceptions are assumptions and
5135// guards present in the loop -- SCEV is not great at exploiting
5136// these to compute max backedge taken counts, but can still use
5137// these to prove lack of overflow. Use this fact to avoid
5138// doing extra work that may not pay off.
5139
5140if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards &&
5141 AC.assumptions().empty())
5142returnResult;
5143
5144// If the backedge is guarded by a comparison with the pre-inc value the
5145// addrec is safe. Also, if the entry is guarded by a comparison with the
5146// start value and the backedge is guarded by a comparison with the post-inc
5147// value, the addrec is safe.
5148ICmpInst::Predicate Pred;
5149constSCEV *OverflowLimit =
5150getSignedOverflowLimitForStep(Step, &Pred,this);
5151if (OverflowLimit &&
5152 (isLoopBackedgeGuardedByCond(L, Pred, AR, OverflowLimit) ||
5153isKnownOnEveryIteration(Pred, AR, OverflowLimit))) {
5154Result =setFlags(Result,SCEV::FlagNSW);
5155 }
5156returnResult;
5157}
5158SCEV::NoWrapFlags
5159ScalarEvolution::proveNoUnsignedWrapViaInduction(constSCEVAddRecExpr *AR) {
5160SCEV::NoWrapFlagsResult = AR->getNoWrapFlags();
5161
5162if (AR->hasNoUnsignedWrap())
5163returnResult;
5164
5165if (!AR->isAffine())
5166returnResult;
5167
5168// This function can be expensive, only try to prove NUW once per AddRec.
5169if (!UnsignedWrapViaInductionTried.insert(AR).second)
5170returnResult;
5171
5172constSCEV *Step = AR->getStepRecurrence(*this);
5173unsignedBitWidth =getTypeSizeInBits(AR->getType());
5174constLoop *L = AR->getLoop();
5175
5176// Check whether the backedge-taken count is SCEVCouldNotCompute.
5177// Note that this serves two purposes: It filters out loops that are
5178// simply not analyzable, and it covers the case where this code is
5179// being called from within backedge-taken count analysis, such that
5180// attempting to ask for the backedge-taken count would likely result
5181// in infinite recursion. In the later case, the analysis code will
5182// cope with a conservative value, and it will take care to purge
5183// that value once it has finished.
5184constSCEV *MaxBECount =getConstantMaxBackedgeTakenCount(L);
5185
5186// Normally, in the cases we can prove no-overflow via a
5187// backedge guarding condition, we can also compute a backedge
5188// taken count for the loop. The exceptions are assumptions and
5189// guards present in the loop -- SCEV is not great at exploiting
5190// these to compute max backedge taken counts, but can still use
5191// these to prove lack of overflow. Use this fact to avoid
5192// doing extra work that may not pay off.
5193
5194if (isa<SCEVCouldNotCompute>(MaxBECount) && !HasGuards &&
5195 AC.assumptions().empty())
5196returnResult;
5197
5198// If the backedge is guarded by a comparison with the pre-inc value the
5199// addrec is safe. Also, if the entry is guarded by a comparison with the
5200// start value and the backedge is guarded by a comparison with the post-inc
5201// value, the addrec is safe.
5202if (isKnownPositive(Step)) {
5203constSCEV *N =getConstant(APInt::getMinValue(BitWidth) -
5204getUnsignedRangeMax(Step));
5205if (isLoopBackedgeGuardedByCond(L,ICmpInst::ICMP_ULT, AR,N) ||
5206isKnownOnEveryIteration(ICmpInst::ICMP_ULT, AR,N)) {
5207Result =setFlags(Result,SCEV::FlagNUW);
5208 }
5209 }
5210
5211returnResult;
5212}
5213
5214namespace{
5215
5216/// Represents an abstract binary operation. This may exist as a
5217/// normal instruction or constant expression, or may have been
5218/// derived from an expression tree.
5219structBinaryOp {
5220unsigned Opcode;
5221Value *LHS;
5222Value *RHS;
5223bool IsNSW =false;
5224bool IsNUW =false;
5225
5226 /// Op is set if this BinaryOp corresponds to a concrete LLVM instruction or
5227 /// constant expression.
5228Operator *Op =nullptr;
5229
5230explicit BinaryOp(Operator *Op)
5231 : Opcode(Op->getOpcode()),LHS(Op->getOperand(0)),RHS(Op->getOperand(1)),
5232Op(Op) {
5233if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(Op)) {
5234 IsNSW = OBO->hasNoSignedWrap();
5235 IsNUW = OBO->hasNoUnsignedWrap();
5236 }
5237 }
5238
5239explicit BinaryOp(unsigned Opcode,Value *LHS,Value *RHS,bool IsNSW =false,
5240bool IsNUW =false)
5241 : Opcode(Opcode),LHS(LHS),RHS(RHS), IsNSW(IsNSW), IsNUW(IsNUW) {}
5242};
5243
5244}// end anonymous namespace
5245
5246/// Try to map \p V into a BinaryOp, and return \c std::nullopt on failure.
5247static std::optional<BinaryOp>MatchBinaryOp(Value *V,constDataLayout &DL,
5248AssumptionCache &AC,
5249constDominatorTree &DT,
5250constInstruction *CxtI) {
5251auto *Op = dyn_cast<Operator>(V);
5252if (!Op)
5253return std::nullopt;
5254
5255// Implementation detail: all the cleverness here should happen without
5256// creating new SCEV expressions -- our caller knowns tricks to avoid creating
5257// SCEV expressions when possible, and we should not break that.
5258
5259switch (Op->getOpcode()) {
5260case Instruction::Add:
5261case Instruction::Sub:
5262case Instruction::Mul:
5263case Instruction::UDiv:
5264case Instruction::URem:
5265case Instruction::And:
5266case Instruction::AShr:
5267case Instruction::Shl:
5268return BinaryOp(Op);
5269
5270case Instruction::Or: {
5271// Convert or disjoint into add nuw nsw.
5272if (cast<PossiblyDisjointInst>(Op)->isDisjoint())
5273return BinaryOp(Instruction::Add,Op->getOperand(0),Op->getOperand(1),
5274/*IsNSW=*/true,/*IsNUW=*/true);
5275return BinaryOp(Op);
5276 }
5277
5278case Instruction::Xor:
5279if (auto *RHSC = dyn_cast<ConstantInt>(Op->getOperand(1)))
5280// If the RHS of the xor is a signmask, then this is just an add.
5281// Instcombine turns add of signmask into xor as a strength reduction step.
5282if (RHSC->getValue().isSignMask())
5283return BinaryOp(Instruction::Add,Op->getOperand(0),Op->getOperand(1));
5284// Binary `xor` is a bit-wise `add`.
5285if (V->getType()->isIntegerTy(1))
5286return BinaryOp(Instruction::Add,Op->getOperand(0),Op->getOperand(1));
5287return BinaryOp(Op);
5288
5289case Instruction::LShr:
5290// Turn logical shift right of a constant into a unsigned divide.
5291if (ConstantInt *SA = dyn_cast<ConstantInt>(Op->getOperand(1))) {
5292uint32_tBitWidth = cast<IntegerType>(Op->getType())->getBitWidth();
5293
5294// If the shift count is not less than the bitwidth, the result of
5295// the shift is undefined. Don't try to analyze it, because the
5296// resolution chosen here may differ from the resolution chosen in
5297// other parts of the compiler.
5298if (SA->getValue().ult(BitWidth)) {
5299Constant *X =
5300 ConstantInt::get(SA->getContext(),
5301APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
5302return BinaryOp(Instruction::UDiv,Op->getOperand(0),X);
5303 }
5304 }
5305return BinaryOp(Op);
5306
5307case Instruction::ExtractValue: {
5308auto *EVI = cast<ExtractValueInst>(Op);
5309if (EVI->getNumIndices() != 1 || EVI->getIndices()[0] != 0)
5310break;
5311
5312auto *WO = dyn_cast<WithOverflowInst>(EVI->getAggregateOperand());
5313if (!WO)
5314break;
5315
5316Instruction::BinaryOps BinOp = WO->getBinaryOp();
5317boolSigned = WO->isSigned();
5318// TODO: Should add nuw/nsw flags for mul as well.
5319if (BinOp == Instruction::Mul || !isOverflowIntrinsicNoWrap(WO, DT))
5320return BinaryOp(BinOp, WO->getLHS(), WO->getRHS());
5321
5322// Now that we know that all uses of the arithmetic-result component of
5323// CI are guarded by the overflow check, we can go ahead and pretend
5324// that the arithmetic is non-overflowing.
5325return BinaryOp(BinOp, WO->getLHS(), WO->getRHS(),
5326/* IsNSW = */Signed,/* IsNUW = */ !Signed);
5327 }
5328
5329default:
5330break;
5331 }
5332
5333// Recognise intrinsic loop.decrement.reg, and as this has exactly the same
5334// semantics as a Sub, return a binary sub expression.
5335if (auto *II = dyn_cast<IntrinsicInst>(V))
5336if (II->getIntrinsicID() == Intrinsic::loop_decrement_reg)
5337return BinaryOp(Instruction::Sub,II->getOperand(0),II->getOperand(1));
5338
5339return std::nullopt;
5340}
5341
5342/// Helper function to createAddRecFromPHIWithCasts. We have a phi
5343/// node whose symbolic (unknown) SCEV is \p SymbolicPHI, which is updated via
5344/// the loop backedge by a SCEVAddExpr, possibly also with a few casts on the
5345/// way. This function checks if \p Op, an operand of this SCEVAddExpr,
5346/// follows one of the following patterns:
5347/// Op == (SExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
5348/// Op == (ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy)
5349/// If the SCEV expression of \p Op conforms with one of the expected patterns
5350/// we return the type of the truncation operation, and indicate whether the
5351/// truncated type should be treated as signed/unsigned by setting
5352/// \p Signed to true/false, respectively.
5353staticType *isSimpleCastedPHI(constSCEV *Op,constSCEVUnknown *SymbolicPHI,
5354bool &Signed,ScalarEvolution &SE) {
5355// The case where Op == SymbolicPHI (that is, with no type conversions on
5356// the way) is handled by the regular add recurrence creating logic and
5357// would have already been triggered in createAddRecForPHI. Reaching it here
5358// means that createAddRecFromPHI had failed for this PHI before (e.g.,
5359// because one of the other operands of the SCEVAddExpr updating this PHI is
5360// not invariant).
5361//
5362// Here we look for the case where Op = (ext(trunc(SymbolicPHI))), and in
5363// this case predicates that allow us to prove that Op == SymbolicPHI will
5364// be added.
5365if (Op == SymbolicPHI)
5366returnnullptr;
5367
5368unsigned SourceBits = SE.getTypeSizeInBits(SymbolicPHI->getType());
5369unsigned NewBits = SE.getTypeSizeInBits(Op->getType());
5370if (SourceBits != NewBits)
5371returnnullptr;
5372
5373constSCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(Op);
5374constSCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(Op);
5375if (!SExt && !ZExt)
5376returnnullptr;
5377constSCEVTruncateExpr *Trunc =
5378 SExt ? dyn_cast<SCEVTruncateExpr>(SExt->getOperand())
5379 : dyn_cast<SCEVTruncateExpr>(ZExt->getOperand());
5380if (!Trunc)
5381returnnullptr;
5382constSCEV *X = Trunc->getOperand();
5383if (X != SymbolicPHI)
5384returnnullptr;
5385Signed = SExt !=nullptr;
5386return Trunc->getType();
5387}
5388
5389staticconstLoop *isIntegerLoopHeaderPHI(constPHINode *PN,LoopInfo &LI) {
5390if (!PN->getType()->isIntegerTy())
5391returnnullptr;
5392constLoop *L = LI.getLoopFor(PN->getParent());
5393if (!L || L->getHeader() != PN->getParent())
5394returnnullptr;
5395return L;
5396}
5397
5398// Analyze \p SymbolicPHI, a SCEV expression of a phi node, and check if the
5399// computation that updates the phi follows the following pattern:
5400// (SExt/ZExt ix (Trunc iy (%SymbolicPHI) to ix) to iy) + InvariantAccum
5401// which correspond to a phi->trunc->sext/zext->add->phi update chain.
5402// If so, try to see if it can be rewritten as an AddRecExpr under some
5403// Predicates. If successful, return them as a pair. Also cache the results
5404// of the analysis.
5405//
5406// Example usage scenario:
5407// Say the Rewriter is called for the following SCEV:
5408// 8 * ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
5409// where:
5410// %X = phi i64 (%Start, %BEValue)
5411// It will visitMul->visitAdd->visitSExt->visitTrunc->visitUnknown(%X),
5412// and call this function with %SymbolicPHI = %X.
5413//
5414// The analysis will find that the value coming around the backedge has
5415// the following SCEV:
5416// BEValue = ((sext i32 (trunc i64 %X to i32) to i64) + %Step)
5417// Upon concluding that this matches the desired pattern, the function
5418// will return the pair {NewAddRec, SmallPredsVec} where:
5419// NewAddRec = {%Start,+,%Step}
5420// SmallPredsVec = {P1, P2, P3} as follows:
5421// P1(WrapPred): AR: {trunc(%Start),+,(trunc %Step)}<nsw> Flags: <nssw>
5422// P2(EqualPred): %Start == (sext i32 (trunc i64 %Start to i32) to i64)
5423// P3(EqualPred): %Step == (sext i32 (trunc i64 %Step to i32) to i64)
5424// The returned pair means that SymbolicPHI can be rewritten into NewAddRec
5425// under the predicates {P1,P2,P3}.
5426// This predicated rewrite will be cached in PredicatedSCEVRewrites:
5427// PredicatedSCEVRewrites[{%X,L}] = {NewAddRec, {P1,P2,P3)}
5428//
5429// TODO's:
5430//
5431// 1) Extend the Induction descriptor to also support inductions that involve
5432// casts: When needed (namely, when we are called in the context of the
5433// vectorizer induction analysis), a Set of cast instructions will be
5434// populated by this method, and provided back to isInductionPHI. This is
5435// needed to allow the vectorizer to properly record them to be ignored by
5436// the cost model and to avoid vectorizing them (otherwise these casts,
5437// which are redundant under the runtime overflow checks, will be
5438// vectorized, which can be costly).
5439//
5440// 2) Support additional induction/PHISCEV patterns: We also want to support
5441// inductions where the sext-trunc / zext-trunc operations (partly) occur
5442// after the induction update operation (the induction increment):
5443//
5444// (Trunc iy (SExt/ZExt ix (%SymbolicPHI + InvariantAccum) to iy) to ix)
5445// which correspond to a phi->add->trunc->sext/zext->phi update chain.
5446//
5447// (Trunc iy ((SExt/ZExt ix (%SymbolicPhi) to iy) + InvariantAccum) to ix)
5448// which correspond to a phi->trunc->add->sext/zext->phi update chain.
5449//
5450// 3) Outline common code with createAddRecFromPHI to avoid duplication.
5451std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5452ScalarEvolution::createAddRecFromPHIWithCastsImpl(constSCEVUnknown *SymbolicPHI) {
5453SmallVector<const SCEVPredicate *, 3> Predicates;
5454
5455// *** Part1: Analyze if we have a phi-with-cast pattern for which we can
5456// return an AddRec expression under some predicate.
5457
5458auto *PN = cast<PHINode>(SymbolicPHI->getValue());
5459constLoop *L =isIntegerLoopHeaderPHI(PN, LI);
5460assert(L &&"Expecting an integer loop header phi");
5461
5462// The loop may have multiple entrances or multiple exits; we can analyze
5463// this phi as an addrec if it has a unique entry value and a unique
5464// backedge value.
5465Value *BEValueV =nullptr, *StartValueV =nullptr;
5466for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5467Value *V = PN->getIncomingValue(i);
5468if (L->contains(PN->getIncomingBlock(i))) {
5469if (!BEValueV) {
5470 BEValueV =V;
5471 }elseif (BEValueV != V) {
5472 BEValueV =nullptr;
5473break;
5474 }
5475 }elseif (!StartValueV) {
5476 StartValueV =V;
5477 }elseif (StartValueV != V) {
5478 StartValueV =nullptr;
5479break;
5480 }
5481 }
5482if (!BEValueV || !StartValueV)
5483return std::nullopt;
5484
5485constSCEV *BEValue =getSCEV(BEValueV);
5486
5487// If the value coming around the backedge is an add with the symbolic
5488// value we just inserted, possibly with casts that we can ignore under
5489// an appropriate runtime guard, then we found a simple induction variable!
5490constauto *Add = dyn_cast<SCEVAddExpr>(BEValue);
5491if (!Add)
5492return std::nullopt;
5493
5494// If there is a single occurrence of the symbolic value, possibly
5495// casted, replace it with a recurrence.
5496unsigned FoundIndex =Add->getNumOperands();
5497Type *TruncTy =nullptr;
5498boolSigned;
5499for (unsigned i = 0, e =Add->getNumOperands(); i != e; ++i)
5500if ((TruncTy =
5501isSimpleCastedPHI(Add->getOperand(i), SymbolicPHI,Signed, *this)))
5502if (FoundIndex == e) {
5503 FoundIndex = i;
5504break;
5505 }
5506
5507if (FoundIndex ==Add->getNumOperands())
5508return std::nullopt;
5509
5510// Create an add with everything but the specified operand.
5511SmallVector<const SCEV *, 8> Ops;
5512for (unsigned i = 0, e =Add->getNumOperands(); i != e; ++i)
5513if (i != FoundIndex)
5514 Ops.push_back(Add->getOperand(i));
5515constSCEV *Accum =getAddExpr(Ops);
5516
5517// The runtime checks will not be valid if the step amount is
5518// varying inside the loop.
5519if (!isLoopInvariant(Accum, L))
5520return std::nullopt;
5521
5522// *** Part2: Create the predicates
5523
5524// Analysis was successful: we have a phi-with-cast pattern for which we
5525// can return an AddRec expression under the following predicates:
5526//
5527// P1: A Wrap predicate that guarantees that Trunc(Start) + i*Trunc(Accum)
5528// fits within the truncated type (does not overflow) for i = 0 to n-1.
5529// P2: An Equal predicate that guarantees that
5530// Start = (Ext ix (Trunc iy (Start) to ix) to iy)
5531// P3: An Equal predicate that guarantees that
5532// Accum = (Ext ix (Trunc iy (Accum) to ix) to iy)
5533//
5534// As we next prove, the above predicates guarantee that:
5535// Start + i*Accum = (Ext ix (Trunc iy ( Start + i*Accum ) to ix) to iy)
5536//
5537//
5538// More formally, we want to prove that:
5539// Expr(i+1) = Start + (i+1) * Accum
5540// = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5541//
5542// Given that:
5543// 1) Expr(0) = Start
5544// 2) Expr(1) = Start + Accum
5545// = (Ext ix (Trunc iy (Start) to ix) to iy) + Accum :: from P2
5546// 3) Induction hypothesis (step i):
5547// Expr(i) = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum
5548//
5549// Proof:
5550// Expr(i+1) =
5551// = Start + (i+1)*Accum
5552// = (Start + i*Accum) + Accum
5553// = Expr(i) + Accum
5554// = (Ext ix (Trunc iy (Expr(i-1)) to ix) to iy) + Accum + Accum
5555// :: from step i
5556//
5557// = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy) + Accum + Accum
5558//
5559// = (Ext ix (Trunc iy (Start + (i-1)*Accum) to ix) to iy)
5560// + (Ext ix (Trunc iy (Accum) to ix) to iy)
5561// + Accum :: from P3
5562//
5563// = (Ext ix (Trunc iy ((Start + (i-1)*Accum) + Accum) to ix) to iy)
5564// + Accum :: from P1: Ext(x)+Ext(y)=>Ext(x+y)
5565//
5566// = (Ext ix (Trunc iy (Start + i*Accum) to ix) to iy) + Accum
5567// = (Ext ix (Trunc iy (Expr(i)) to ix) to iy) + Accum
5568//
5569// By induction, the same applies to all iterations 1<=i<n:
5570//
5571
5572// Create a truncated addrec for which we will add a no overflow check (P1).
5573constSCEV *StartVal =getSCEV(StartValueV);
5574constSCEV *PHISCEV =
5575getAddRecExpr(getTruncateExpr(StartVal, TruncTy),
5576getTruncateExpr(Accum, TruncTy), L,SCEV::FlagAnyWrap);
5577
5578// PHISCEV can be either a SCEVConstant or a SCEVAddRecExpr.
5579// ex: If truncated Accum is 0 and StartVal is a constant, then PHISCEV
5580// will be constant.
5581//
5582// If PHISCEV is a constant, then P1 degenerates into P2 or P3, so we don't
5583// add P1.
5584if (constauto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
5585SCEVWrapPredicate::IncrementWrapFlags AddedFlags =
5586Signed ?SCEVWrapPredicate::IncrementNSSW
5587 :SCEVWrapPredicate::IncrementNUSW;
5588constSCEVPredicate *AddRecPred =getWrapPredicate(AR, AddedFlags);
5589 Predicates.push_back(AddRecPred);
5590 }
5591
5592// Create the Equal Predicates P2,P3:
5593
5594// It is possible that the predicates P2 and/or P3 are computable at
5595// compile time due to StartVal and/or Accum being constants.
5596// If either one is, then we can check that now and escape if either P2
5597// or P3 is false.
5598
5599// Construct the extended SCEV: (Ext ix (Trunc iy (Expr) to ix) to iy)
5600// for each of StartVal and Accum
5601auto getExtendedExpr = [&](constSCEV *Expr,
5602bool CreateSignExtend) ->constSCEV * {
5603assert(isLoopInvariant(Expr, L) &&"Expr is expected to be invariant");
5604constSCEV *TruncatedExpr =getTruncateExpr(Expr, TruncTy);
5605constSCEV *ExtendedExpr =
5606 CreateSignExtend ?getSignExtendExpr(TruncatedExpr, Expr->getType())
5607 :getZeroExtendExpr(TruncatedExpr, Expr->getType());
5608return ExtendedExpr;
5609 };
5610
5611// Given:
5612// ExtendedExpr = (Ext ix (Trunc iy (Expr) to ix) to iy
5613// = getExtendedExpr(Expr)
5614// Determine whether the predicate P: Expr == ExtendedExpr
5615// is known to be false at compile time
5616auto PredIsKnownFalse = [&](constSCEV *Expr,
5617constSCEV *ExtendedExpr) ->bool {
5618return Expr != ExtendedExpr &&
5619isKnownPredicate(ICmpInst::ICMP_NE, Expr, ExtendedExpr);
5620 };
5621
5622constSCEV *StartExtended = getExtendedExpr(StartVal,Signed);
5623if (PredIsKnownFalse(StartVal, StartExtended)) {
5624LLVM_DEBUG(dbgs() <<"P2 is compile-time false\n";);
5625return std::nullopt;
5626 }
5627
5628// The Step is always Signed (because the overflow checks are either
5629// NSSW or NUSW)
5630constSCEV *AccumExtended = getExtendedExpr(Accum,/*CreateSignExtend=*/true);
5631if (PredIsKnownFalse(Accum, AccumExtended)) {
5632LLVM_DEBUG(dbgs() <<"P3 is compile-time false\n";);
5633return std::nullopt;
5634 }
5635
5636auto AppendPredicate = [&](constSCEV *Expr,
5637constSCEV *ExtendedExpr) ->void {
5638if (Expr != ExtendedExpr &&
5639 !isKnownPredicate(ICmpInst::ICMP_EQ, Expr, ExtendedExpr)) {
5640constSCEVPredicate *Pred =getEqualPredicate(Expr, ExtendedExpr);
5641LLVM_DEBUG(dbgs() <<"Added Predicate: " << *Pred);
5642 Predicates.push_back(Pred);
5643 }
5644 };
5645
5646 AppendPredicate(StartVal, StartExtended);
5647 AppendPredicate(Accum, AccumExtended);
5648
5649// *** Part3: Predicates are ready. Now go ahead and create the new addrec in
5650// which the casts had been folded away. The caller can rewrite SymbolicPHI
5651// into NewAR if it will also add the runtime overflow checks specified in
5652// Predicates.
5653auto *NewAR =getAddRecExpr(StartVal, Accum, L,SCEV::FlagAnyWrap);
5654
5655 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> PredRewrite =
5656 std::make_pair(NewAR, Predicates);
5657// Remember the result of the analysis for this SCEV at this locayyytion.
5658 PredicatedSCEVRewrites[{SymbolicPHI,L}] = PredRewrite;
5659return PredRewrite;
5660}
5661
5662std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5663ScalarEvolution::createAddRecFromPHIWithCasts(constSCEVUnknown *SymbolicPHI) {
5664auto *PN = cast<PHINode>(SymbolicPHI->getValue());
5665constLoop *L =isIntegerLoopHeaderPHI(PN, LI);
5666if (!L)
5667return std::nullopt;
5668
5669// Check to see if we already analyzed this PHI.
5670autoI = PredicatedSCEVRewrites.find({SymbolicPHI, L});
5671if (I != PredicatedSCEVRewrites.end()) {
5672 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>> Rewrite =
5673I->second;
5674// Analysis was done before and failed to create an AddRec:
5675if (Rewrite.first == SymbolicPHI)
5676return std::nullopt;
5677// Analysis was done before and succeeded to create an AddRec under
5678// a predicate:
5679assert(isa<SCEVAddRecExpr>(Rewrite.first) &&"Expected an AddRec");
5680assert(!(Rewrite.second).empty() &&"Expected to find Predicates");
5681return Rewrite;
5682 }
5683
5684 std::optional<std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
5685 Rewrite = createAddRecFromPHIWithCastsImpl(SymbolicPHI);
5686
5687// Record in the cache that the analysis failed
5688if (!Rewrite) {
5689SmallVector<const SCEVPredicate *, 3> Predicates;
5690 PredicatedSCEVRewrites[{SymbolicPHI, L}] = {SymbolicPHI, Predicates};
5691return std::nullopt;
5692 }
5693
5694return Rewrite;
5695}
5696
5697// FIXME: This utility is currently required because the Rewriter currently
5698// does not rewrite this expression:
5699// {0, +, (sext ix (trunc iy to ix) to iy)}
5700// into {0, +, %step},
5701// even when the following Equal predicate exists:
5702// "%step == (sext ix (trunc iy to ix) to iy)".
5703boolPredicatedScalarEvolution::areAddRecsEqualWithPreds(
5704constSCEVAddRecExpr *AR1,constSCEVAddRecExpr *AR2) const{
5705if (AR1 == AR2)
5706returntrue;
5707
5708auto areExprsEqual = [&](constSCEV *Expr1,constSCEV *Expr2) ->bool {
5709if (Expr1 != Expr2 &&
5710 !Preds->implies(SE.getEqualPredicate(Expr1, Expr2), SE) &&
5711 !Preds->implies(SE.getEqualPredicate(Expr2, Expr1), SE))
5712returnfalse;
5713returntrue;
5714 };
5715
5716if (!areExprsEqual(AR1->getStart(), AR2->getStart()) ||
5717 !areExprsEqual(AR1->getStepRecurrence(SE), AR2->getStepRecurrence(SE)))
5718returnfalse;
5719returntrue;
5720}
5721
5722/// A helper function for createAddRecFromPHI to handle simple cases.
5723///
5724/// This function tries to find an AddRec expression for the simplest (yet most
5725/// common) cases: PN = PHI(Start, OP(Self, LoopInvariant)).
5726/// If it fails, createAddRecFromPHI will use a more general, but slow,
5727/// technique for finding the AddRec expression.
5728constSCEV *ScalarEvolution::createSimpleAffineAddRec(PHINode *PN,
5729Value *BEValueV,
5730Value *StartValueV) {
5731constLoop *L = LI.getLoopFor(PN->getParent());
5732assert(L && L->getHeader() == PN->getParent());
5733assert(BEValueV && StartValueV);
5734
5735auto BO =MatchBinaryOp(BEValueV,getDataLayout(), AC, DT, PN);
5736if (!BO)
5737returnnullptr;
5738
5739if (BO->Opcode != Instruction::Add)
5740returnnullptr;
5741
5742constSCEV *Accum =nullptr;
5743if (BO->LHS == PN && L->isLoopInvariant(BO->RHS))
5744 Accum =getSCEV(BO->RHS);
5745elseif (BO->RHS == PN && L->isLoopInvariant(BO->LHS))
5746 Accum =getSCEV(BO->LHS);
5747
5748if (!Accum)
5749returnnullptr;
5750
5751SCEV::NoWrapFlags Flags =SCEV::FlagAnyWrap;
5752if (BO->IsNUW)
5753 Flags =setFlags(Flags,SCEV::FlagNUW);
5754if (BO->IsNSW)
5755 Flags =setFlags(Flags,SCEV::FlagNSW);
5756
5757constSCEV *StartVal =getSCEV(StartValueV);
5758constSCEV *PHISCEV =getAddRecExpr(StartVal, Accum, L, Flags);
5759 insertValueToMap(PN, PHISCEV);
5760
5761if (auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
5762setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),
5763 (SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
5764 proveNoWrapViaConstantRanges(AR)));
5765 }
5766
5767// We can add Flags to the post-inc expression only if we
5768// know that it is *undefined behavior* for BEValueV to
5769// overflow.
5770if (auto *BEInst = dyn_cast<Instruction>(BEValueV)) {
5771assert(isLoopInvariant(Accum, L) &&
5772"Accum is defined outside L, but is not invariant?");
5773if (isAddRecNeverPoison(BEInst, L))
5774 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum, L, Flags);
5775 }
5776
5777return PHISCEV;
5778}
5779
5780constSCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
5781constLoop *L = LI.getLoopFor(PN->getParent());
5782if (!L ||L->getHeader() != PN->getParent())
5783returnnullptr;
5784
5785// The loop may have multiple entrances or multiple exits; we can analyze
5786// this phi as an addrec if it has a unique entry value and a unique
5787// backedge value.
5788Value *BEValueV =nullptr, *StartValueV =nullptr;
5789for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
5790Value *V = PN->getIncomingValue(i);
5791if (L->contains(PN->getIncomingBlock(i))) {
5792if (!BEValueV) {
5793 BEValueV =V;
5794 }elseif (BEValueV != V) {
5795 BEValueV =nullptr;
5796break;
5797 }
5798 }elseif (!StartValueV) {
5799 StartValueV =V;
5800 }elseif (StartValueV != V) {
5801 StartValueV =nullptr;
5802break;
5803 }
5804 }
5805if (!BEValueV || !StartValueV)
5806returnnullptr;
5807
5808assert(ValueExprMap.find_as(PN) == ValueExprMap.end() &&
5809"PHI node already processed?");
5810
5811// First, try to find AddRec expression without creating a fictituos symbolic
5812// value for PN.
5813if (auto *S = createSimpleAffineAddRec(PN, BEValueV, StartValueV))
5814return S;
5815
5816// Handle PHI node value symbolically.
5817constSCEV *SymbolicName =getUnknown(PN);
5818 insertValueToMap(PN, SymbolicName);
5819
5820// Using this symbolic name for the PHI, analyze the value coming around
5821// the back-edge.
5822constSCEV *BEValue =getSCEV(BEValueV);
5823
5824// NOTE: If BEValue is loop invariant, we know that the PHI node just
5825// has a special value for the first iteration of the loop.
5826
5827// If the value coming around the backedge is an add with the symbolic
5828// value we just inserted, then we found a simple induction variable!
5829if (constSCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(BEValue)) {
5830// If there is a single occurrence of the symbolic value, replace it
5831// with a recurrence.
5832unsigned FoundIndex =Add->getNumOperands();
5833for (unsigned i = 0, e =Add->getNumOperands(); i != e; ++i)
5834if (Add->getOperand(i) == SymbolicName)
5835if (FoundIndex == e) {
5836 FoundIndex = i;
5837break;
5838 }
5839
5840if (FoundIndex !=Add->getNumOperands()) {
5841// Create an add with everything but the specified operand.
5842SmallVector<const SCEV *, 8> Ops;
5843for (unsigned i = 0, e =Add->getNumOperands(); i != e; ++i)
5844if (i != FoundIndex)
5845 Ops.push_back(SCEVBackedgeConditionFolder::rewrite(Add->getOperand(i),
5846 L, *this));
5847constSCEV *Accum =getAddExpr(Ops);
5848
5849// This is not a valid addrec if the step amount is varying each
5850// loop iteration, but is not itself an addrec in this loop.
5851if (isLoopInvariant(Accum, L) ||
5852 (isa<SCEVAddRecExpr>(Accum) &&
5853 cast<SCEVAddRecExpr>(Accum)->getLoop() == L)) {
5854SCEV::NoWrapFlagsFlags =SCEV::FlagAnyWrap;
5855
5856if (auto BO =MatchBinaryOp(BEValueV,getDataLayout(), AC, DT, PN)) {
5857if (BO->Opcode == Instruction::Add && BO->LHS == PN) {
5858if (BO->IsNUW)
5859Flags =setFlags(Flags,SCEV::FlagNUW);
5860if (BO->IsNSW)
5861Flags =setFlags(Flags,SCEV::FlagNSW);
5862 }
5863 }elseif (GEPOperator *GEP = dyn_cast<GEPOperator>(BEValueV)) {
5864if (GEP->getOperand(0) == PN) {
5865GEPNoWrapFlags NW =GEP->getNoWrapFlags();
5866// If the increment has any nowrap flags, then we know the address
5867// space cannot be wrapped around.
5868if (NW !=GEPNoWrapFlags::none())
5869Flags =setFlags(Flags,SCEV::FlagNW);
5870// If the GEP is nuw or nusw with non-negative offset, we know that
5871// no unsigned wrap occurs. We cannot set the nsw flag as only the
5872// offset is treated as signed, while the base is unsigned.
5873if (NW.hasNoUnsignedWrap() ||
5874 (NW.hasNoUnsignedSignedWrap() &&isKnownNonNegative(Accum)))
5875Flags =setFlags(Flags,SCEV::FlagNUW);
5876 }
5877
5878// We cannot transfer nuw and nsw flags from subtraction
5879// operations -- sub nuw X, Y is not the same as add nuw X, -Y
5880// for instance.
5881 }
5882
5883constSCEV *StartVal =getSCEV(StartValueV);
5884constSCEV *PHISCEV =getAddRecExpr(StartVal, Accum, L, Flags);
5885
5886// Okay, for the entire analysis of this edge we assumed the PHI
5887// to be symbolic. We now need to go back and purge all of the
5888// entries for the scalars that use the symbolic expression.
5889 forgetMemoizedResults(SymbolicName);
5890 insertValueToMap(PN, PHISCEV);
5891
5892if (auto *AR = dyn_cast<SCEVAddRecExpr>(PHISCEV)) {
5893setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR),
5894 (SCEV::NoWrapFlags)(AR->getNoWrapFlags() |
5895 proveNoWrapViaConstantRanges(AR)));
5896 }
5897
5898// We can add Flags to the post-inc expression only if we
5899// know that it is *undefined behavior* for BEValueV to
5900// overflow.
5901if (auto *BEInst = dyn_cast<Instruction>(BEValueV))
5902if (isLoopInvariant(Accum, L) && isAddRecNeverPoison(BEInst, L))
5903 (void)getAddRecExpr(getAddExpr(StartVal, Accum), Accum,L,Flags);
5904
5905return PHISCEV;
5906 }
5907 }
5908 }else {
5909// Otherwise, this could be a loop like this:
5910// i = 0; for (j = 1; ..; ++j) { .... i = j; }
5911// In this case, j = {1,+,1} and BEValue is j.
5912// Because the other in-value of i (0) fits the evolution of BEValue
5913// i really is an addrec evolution.
5914//
5915// We can generalize this saying that i is the shifted value of BEValue
5916// by one iteration:
5917// PHI(f(0), f({1,+,1})) --> f({0,+,1})
5918
5919// Do not allow refinement in rewriting of BEValue.
5920if (isGuaranteedNotToCauseUB(BEValue)) {
5921constSCEV *Shifted = SCEVShiftRewriter::rewrite(BEValue, L, *this);
5922constSCEV *Start = SCEVInitRewriter::rewrite(Shifted, L, *this,false);
5923if (Shifted !=getCouldNotCompute() && Start !=getCouldNotCompute() &&
5924::impliesPoison(BEValue, Start)) {
5925constSCEV *StartVal =getSCEV(StartValueV);
5926if (Start == StartVal) {
5927// Okay, for the entire analysis of this edge we assumed the PHI
5928// to be symbolic. We now need to go back and purge all of the
5929// entries for the scalars that use the symbolic expression.
5930 forgetMemoizedResults(SymbolicName);
5931 insertValueToMap(PN, Shifted);
5932return Shifted;
5933 }
5934 }
5935 }
5936 }
5937
5938// Remove the temporary PHI node SCEV that has been inserted while intending
5939// to create an AddRecExpr for this PHI node. We can not keep this temporary
5940// as it will prevent later (possibly simpler) SCEV expressions to be added
5941// to the ValueExprMap.
5942 eraseValueFromMap(PN);
5943
5944returnnullptr;
5945}
5946
5947// Try to match a control flow sequence that branches out at BI and merges back
5948// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful
5949// match.
5950staticboolBrPHIToSelect(DominatorTree &DT,BranchInst *BI,PHINode *Merge,
5951Value *&C,Value *&LHS,Value *&RHS) {
5952C = BI->getCondition();
5953
5954BasicBlockEdge LeftEdge(BI->getParent(), BI->getSuccessor(0));
5955BasicBlockEdge RightEdge(BI->getParent(), BI->getSuccessor(1));
5956
5957if (!LeftEdge.isSingleEdge())
5958returnfalse;
5959
5960assert(RightEdge.isSingleEdge() &&"Follows from LeftEdge.isSingleEdge()");
5961
5962Use &LeftUse =Merge->getOperandUse(0);
5963Use &RightUse =Merge->getOperandUse(1);
5964
5965if (DT.dominates(LeftEdge, LeftUse) && DT.dominates(RightEdge, RightUse)) {
5966LHS = LeftUse;
5967RHS = RightUse;
5968returntrue;
5969 }
5970
5971if (DT.dominates(LeftEdge, RightUse) && DT.dominates(RightEdge, LeftUse)) {
5972LHS = RightUse;
5973RHS = LeftUse;
5974returntrue;
5975 }
5976
5977returnfalse;
5978}
5979
5980constSCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
5981auto IsReachable =
5982 [&](BasicBlock *BB) {return DT.isReachableFromEntry(BB); };
5983if (PN->getNumIncomingValues() == 2 &&all_of(PN->blocks(), IsReachable)) {
5984// Try to match
5985//
5986// br %cond, label %left, label %right
5987// left:
5988// br label %merge
5989// right:
5990// br label %merge
5991// merge:
5992// V = phi [ %x, %left ], [ %y, %right ]
5993//
5994// as "select %cond, %x, %y"
5995
5996BasicBlock *IDom = DT[PN->getParent()]->getIDom()->getBlock();
5997assert(IDom &&"At least the entry block should dominate PN");
5998
5999auto *BI = dyn_cast<BranchInst>(IDom->getTerminator());
6000Value *Cond =nullptr, *LHS =nullptr, *RHS =nullptr;
6001
6002if (BI && BI->isConditional() &&
6003BrPHIToSelect(DT, BI, PN,Cond, LHS, RHS) &&
6004properlyDominates(getSCEV(LHS), PN->getParent()) &&
6005properlyDominates(getSCEV(RHS), PN->getParent()))
6006return createNodeForSelectOrPHI(PN,Cond, LHS, RHS);
6007 }
6008
6009returnnullptr;
6010}
6011
6012/// Returns SCEV for the first operand of a phi if all phi operands have
6013/// identical opcodes and operands
6014/// eg.
6015/// a: %add = %a + %b
6016/// br %c
6017/// b: %add1 = %a + %b
6018/// br %c
6019/// c: %phi = phi [%add, a], [%add1, b]
6020/// scev(%phi) => scev(%add)
6021constSCEV *
6022ScalarEvolution::createNodeForPHIWithIdenticalOperands(PHINode *PN) {
6023BinaryOperator *CommonInst =nullptr;
6024// Check if instructions are identical.
6025for (Value *Incoming : PN->incoming_values()) {
6026auto *IncomingInst = dyn_cast<BinaryOperator>(Incoming);
6027if (!IncomingInst)
6028returnnullptr;
6029if (CommonInst) {
6030if (!CommonInst->isIdenticalToWhenDefined(IncomingInst))
6031returnnullptr;// Not identical, give up
6032 }else {
6033// Remember binary operator
6034 CommonInst = IncomingInst;
6035 }
6036 }
6037if (!CommonInst)
6038returnnullptr;
6039
6040// Check if SCEV exprs for instructions are identical.
6041constSCEV *CommonSCEV =getSCEV(CommonInst);
6042bool SCEVExprsIdentical =
6043all_of(drop_begin(PN->incoming_values()),
6044 [this, CommonSCEV](Value *V) { return CommonSCEV == getSCEV(V); });
6045return SCEVExprsIdentical ? CommonSCEV :nullptr;
6046}
6047
6048constSCEV *ScalarEvolution::createNodeForPHI(PHINode *PN) {
6049if (constSCEV *S = createAddRecFromPHI(PN))
6050return S;
6051
6052// We do not allow simplifying phi (undef, X) to X here, to avoid reusing the
6053// phi node for X.
6054if (Value *V =simplifyInstruction(
6055 PN, {getDataLayout(), &TLI, &DT, &AC,/*CtxI=*/nullptr,
6056/*UseInstrInfo=*/true,/*CanUseUndef=*/false}))
6057returngetSCEV(V);
6058
6059if (constSCEV *S = createNodeForPHIWithIdenticalOperands(PN))
6060return S;
6061
6062if (constSCEV *S = createNodeFromSelectLikePHI(PN))
6063return S;
6064
6065// If it's not a loop phi, we can't handle it yet.
6066returngetUnknown(PN);
6067}
6068
6069boolSCEVMinMaxExprContains(constSCEV *Root,constSCEV *OperandToFind,
6070SCEVTypes RootKind) {
6071structFindClosure {
6072constSCEV *OperandToFind;
6073constSCEVTypes RootKind;// Must be a sequential min/max expression.
6074constSCEVTypes NonSequentialRootKind;// Non-seq variant of RootKind.
6075
6076bool Found =false;
6077
6078bool canRecurseInto(SCEVTypes Kind) const{
6079// We can only recurse into the SCEV expression of the same effective type
6080// as the type of our root SCEV expression, and into zero-extensions.
6081return RootKind == Kind || NonSequentialRootKind == Kind ||
6082scZeroExtend == Kind;
6083 };
6084
6085 FindClosure(constSCEV *OperandToFind,SCEVTypes RootKind)
6086 : OperandToFind(OperandToFind), RootKind(RootKind),
6087 NonSequentialRootKind(
6088SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType(
6089 RootKind)) {}
6090
6091bool follow(constSCEV *S) {
6092 Found = S == OperandToFind;
6093
6094return !isDone() && canRecurseInto(S->getSCEVType());
6095 }
6096
6097bool isDone() const{return Found; }
6098 };
6099
6100 FindClosure FC(OperandToFind, RootKind);
6101visitAll(Root, FC);
6102return FC.Found;
6103}
6104
6105std::optional<const SCEV *>
6106ScalarEvolution::createNodeForSelectOrPHIInstWithICmpInstCond(Type *Ty,
6107ICmpInst *Cond,
6108Value *TrueVal,
6109Value *FalseVal) {
6110// Try to match some simple smax or umax patterns.
6111auto *ICI =Cond;
6112
6113Value *LHS = ICI->getOperand(0);
6114Value *RHS = ICI->getOperand(1);
6115
6116switch (ICI->getPredicate()) {
6117caseICmpInst::ICMP_SLT:
6118caseICmpInst::ICMP_SLE:
6119caseICmpInst::ICMP_ULT:
6120caseICmpInst::ICMP_ULE:
6121std::swap(LHS, RHS);
6122 [[fallthrough]];
6123caseICmpInst::ICMP_SGT:
6124caseICmpInst::ICMP_SGE:
6125caseICmpInst::ICMP_UGT:
6126caseICmpInst::ICMP_UGE:
6127// a > b ? a+x : b+x -> max(a, b)+x
6128// a > b ? b+x : a+x -> min(a, b)+x
6129if (getTypeSizeInBits(LHS->getType()) <=getTypeSizeInBits(Ty)) {
6130boolSigned = ICI->isSigned();
6131constSCEV *LA =getSCEV(TrueVal);
6132constSCEV *RA =getSCEV(FalseVal);
6133constSCEV *LS =getSCEV(LHS);
6134constSCEV *RS =getSCEV(RHS);
6135if (LA->getType()->isPointerTy()) {
6136// FIXME: Handle cases where LS/RS are pointers not equal to LA/RA.
6137// Need to make sure we can't produce weird expressions involving
6138// negated pointers.
6139if (LA == LS &&RA == RS)
6140returnSigned ?getSMaxExpr(LS, RS) :getUMaxExpr(LS, RS);
6141if (LA == RS &&RA == LS)
6142returnSigned ?getSMinExpr(LS, RS) :getUMinExpr(LS, RS);
6143 }
6144auto CoerceOperand = [&](constSCEV *Op) ->constSCEV * {
6145if (Op->getType()->isPointerTy()) {
6146Op =getLosslessPtrToIntExpr(Op);
6147if (isa<SCEVCouldNotCompute>(Op))
6148returnOp;
6149 }
6150if (Signed)
6151Op =getNoopOrSignExtend(Op, Ty);
6152else
6153Op =getNoopOrZeroExtend(Op, Ty);
6154returnOp;
6155 };
6156LS = CoerceOperand(LS);
6157 RS = CoerceOperand(RS);
6158if (isa<SCEVCouldNotCompute>(LS) || isa<SCEVCouldNotCompute>(RS))
6159break;
6160constSCEV *LDiff =getMinusSCEV(LA, LS);
6161constSCEV *RDiff =getMinusSCEV(RA, RS);
6162if (LDiff == RDiff)
6163returngetAddExpr(Signed ?getSMaxExpr(LS, RS) :getUMaxExpr(LS, RS),
6164 LDiff);
6165 LDiff =getMinusSCEV(LA, RS);
6166 RDiff =getMinusSCEV(RA, LS);
6167if (LDiff == RDiff)
6168returngetAddExpr(Signed ?getSMinExpr(LS, RS) :getUMinExpr(LS, RS),
6169 LDiff);
6170 }
6171break;
6172caseICmpInst::ICMP_NE:
6173// x != 0 ? x+y : C+y -> x == 0 ? C+y : x+y
6174std::swap(TrueVal, FalseVal);
6175 [[fallthrough]];
6176caseICmpInst::ICMP_EQ:
6177// x == 0 ? C+y : x+y -> umax(x, C)+y iff C u<= 1
6178if (getTypeSizeInBits(LHS->getType()) <=getTypeSizeInBits(Ty) &&
6179 isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()) {
6180constSCEV *X =getNoopOrZeroExtend(getSCEV(LHS), Ty);
6181constSCEV *TrueValExpr =getSCEV(TrueVal);// C+y
6182constSCEV *FalseValExpr =getSCEV(FalseVal);// x+y
6183constSCEV *Y =getMinusSCEV(FalseValExpr,X);// y = (x+y)-x
6184constSCEV *C =getMinusSCEV(TrueValExpr,Y);// C = (C+y)-y
6185if (isa<SCEVConstant>(C) && cast<SCEVConstant>(C)->getAPInt().ule(1))
6186returngetAddExpr(getUMaxExpr(X,C),Y);
6187 }
6188// x == 0 ? 0 : umin (..., x, ...) -> umin_seq(x, umin (...))
6189// x == 0 ? 0 : umin_seq(..., x, ...) -> umin_seq(x, umin_seq(...))
6190// x == 0 ? 0 : umin (..., umin_seq(..., x, ...), ...)
6191// -> umin_seq(x, umin (..., umin_seq(...), ...))
6192if (isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero() &&
6193 isa<ConstantInt>(TrueVal) && cast<ConstantInt>(TrueVal)->isZero()) {
6194constSCEV *X =getSCEV(LHS);
6195while (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(X))
6196X = ZExt->getOperand();
6197if (getTypeSizeInBits(X->getType()) <=getTypeSizeInBits(Ty)) {
6198constSCEV *FalseValExpr =getSCEV(FalseVal);
6199if (SCEVMinMaxExprContains(FalseValExpr,X,scSequentialUMinExpr))
6200returngetUMinExpr(getNoopOrZeroExtend(X, Ty), FalseValExpr,
6201/*Sequential=*/true);
6202 }
6203 }
6204break;
6205default:
6206break;
6207 }
6208
6209return std::nullopt;
6210}
6211
6212static std::optional<const SCEV *>
6213createNodeForSelectViaUMinSeq(ScalarEvolution *SE,constSCEV *CondExpr,
6214constSCEV *TrueExpr,constSCEV *FalseExpr) {
6215assert(CondExpr->getType()->isIntegerTy(1) &&
6216 TrueExpr->getType() == FalseExpr->getType() &&
6217 TrueExpr->getType()->isIntegerTy(1) &&
6218"Unexpected operands of a select.");
6219
6220// i1 cond ? i1 x : i1 C --> C + (i1 cond ? (i1 x - i1 C) : i1 0)
6221// --> C + (umin_seq cond, x - C)
6222//
6223// i1 cond ? i1 C : i1 x --> C + (i1 cond ? i1 0 : (i1 x - i1 C))
6224// --> C + (i1 ~cond ? (i1 x - i1 C) : i1 0)
6225// --> C + (umin_seq ~cond, x - C)
6226
6227// FIXME: while we can't legally model the case where both of the hands
6228// are fully variable, we only require that the *difference* is constant.
6229if (!isa<SCEVConstant>(TrueExpr) && !isa<SCEVConstant>(FalseExpr))
6230return std::nullopt;
6231
6232constSCEV *X, *C;
6233if (isa<SCEVConstant>(TrueExpr)) {
6234 CondExpr = SE->getNotSCEV(CondExpr);
6235X = FalseExpr;
6236C = TrueExpr;
6237 }else {
6238X = TrueExpr;
6239C = FalseExpr;
6240 }
6241return SE->getAddExpr(C, SE->getUMinExpr(CondExpr, SE->getMinusSCEV(X,C),
6242/*Sequential=*/true));
6243}
6244
6245static std::optional<const SCEV *>
6246createNodeForSelectViaUMinSeq(ScalarEvolution *SE,Value *Cond,Value *TrueVal,
6247Value *FalseVal) {
6248if (!isa<ConstantInt>(TrueVal) && !isa<ConstantInt>(FalseVal))
6249return std::nullopt;
6250
6251constauto *SECond = SE->getSCEV(Cond);
6252constauto *SETrue = SE->getSCEV(TrueVal);
6253constauto *SEFalse = SE->getSCEV(FalseVal);
6254returncreateNodeForSelectViaUMinSeq(SE, SECond, SETrue, SEFalse);
6255}
6256
6257constSCEV *ScalarEvolution::createNodeForSelectOrPHIViaUMinSeq(
6258Value *V,Value *Cond,Value *TrueVal,Value *FalseVal) {
6259assert(Cond->getType()->isIntegerTy(1) &&"Select condition is not an i1?");
6260assert(TrueVal->getType() ==FalseVal->getType() &&
6261V->getType() ==TrueVal->getType() &&
6262"Types of select hands and of the result must match.");
6263
6264// For now, only deal with i1-typed `select`s.
6265if (!V->getType()->isIntegerTy(1))
6266returngetUnknown(V);
6267
6268if (std::optional<const SCEV *> S =
6269createNodeForSelectViaUMinSeq(this,Cond, TrueVal, FalseVal))
6270return *S;
6271
6272returngetUnknown(V);
6273}
6274
6275constSCEV *ScalarEvolution::createNodeForSelectOrPHI(Value *V,Value *Cond,
6276Value *TrueVal,
6277Value *FalseVal) {
6278// Handle "constant" branch or select. This can occur for instance when a
6279// loop pass transforms an inner loop and moves on to process the outer loop.
6280if (auto *CI = dyn_cast<ConstantInt>(Cond))
6281returngetSCEV(CI->isOne() ? TrueVal : FalseVal);
6282
6283if (auto *I = dyn_cast<Instruction>(V)) {
6284if (auto *ICI = dyn_cast<ICmpInst>(Cond)) {
6285if (std::optional<const SCEV *> S =
6286 createNodeForSelectOrPHIInstWithICmpInstCond(I->getType(), ICI,
6287 TrueVal, FalseVal))
6288return *S;
6289 }
6290 }
6291
6292return createNodeForSelectOrPHIViaUMinSeq(V,Cond, TrueVal, FalseVal);
6293}
6294
6295/// Expand GEP instructions into add and multiply operations. This allows them
6296/// to be analyzed by regular SCEV code.
6297constSCEV *ScalarEvolution::createNodeForGEP(GEPOperator *GEP) {
6298assert(GEP->getSourceElementType()->isSized() &&
6299"GEP source element type must be sized");
6300
6301SmallVector<const SCEV *, 4> IndexExprs;
6302for (Value *Index :GEP->indices())
6303 IndexExprs.push_back(getSCEV(Index));
6304returngetGEPExpr(GEP, IndexExprs);
6305}
6306
6307APInt ScalarEvolution::getConstantMultipleImpl(constSCEV *S) {
6308uint64_tBitWidth =getTypeSizeInBits(S->getType());
6309auto GetShiftedByZeros = [BitWidth](uint32_t TrailingZeros) {
6310return TrailingZeros >=BitWidth
6311 ?APInt::getZero(BitWidth)
6312 :APInt::getOneBitSet(BitWidth, TrailingZeros);
6313 };
6314auto GetGCDMultiple = [this](constSCEVNAryExpr *N) {
6315// The result is GCD of all operands results.
6316APInt Res =getConstantMultiple(N->getOperand(0));
6317for (unsignedI = 1, E =N->getNumOperands();I < E && Res != 1; ++I)
6318 Res =APIntOps::GreatestCommonDivisor(
6319 Res,getConstantMultiple(N->getOperand(I)));
6320return Res;
6321 };
6322
6323switch (S->getSCEVType()) {
6324casescConstant:
6325return cast<SCEVConstant>(S)->getAPInt();
6326casescPtrToInt:
6327returngetConstantMultiple(cast<SCEVPtrToIntExpr>(S)->getOperand());
6328casescUDivExpr:
6329casescVScale:
6330returnAPInt(BitWidth, 1);
6331casescTruncate: {
6332// Only multiples that are a power of 2 will hold after truncation.
6333constSCEVTruncateExpr *T = cast<SCEVTruncateExpr>(S);
6334uint32_t TZ =getMinTrailingZeros(T->getOperand());
6335return GetShiftedByZeros(TZ);
6336 }
6337casescZeroExtend: {
6338constSCEVZeroExtendExpr *Z = cast<SCEVZeroExtendExpr>(S);
6339returngetConstantMultiple(Z->getOperand()).zext(BitWidth);
6340 }
6341casescSignExtend: {
6342// Only multiples that are a power of 2 will hold after sext.
6343constSCEVSignExtendExpr *E = cast<SCEVSignExtendExpr>(S);
6344uint32_t TZ =getMinTrailingZeros(E->getOperand());
6345return GetShiftedByZeros(TZ);
6346 }
6347casescMulExpr: {
6348constSCEVMulExpr *M = cast<SCEVMulExpr>(S);
6349if (M->hasNoUnsignedWrap()) {
6350// The result is the product of all operand results.
6351APInt Res =getConstantMultiple(M->getOperand(0));
6352for (constSCEV *Operand :M->operands().drop_front())
6353 Res = Res *getConstantMultiple(Operand);
6354return Res;
6355 }
6356
6357// If there are no wrap guarentees, find the trailing zeros, which is the
6358// sum of trailing zeros for all its operands.
6359uint32_t TZ = 0;
6360for (constSCEV *Operand :M->operands())
6361 TZ +=getMinTrailingZeros(Operand);
6362return GetShiftedByZeros(TZ);
6363 }
6364casescAddExpr:
6365casescAddRecExpr: {
6366constSCEVNAryExpr *N = cast<SCEVNAryExpr>(S);
6367if (N->hasNoUnsignedWrap())
6368return GetGCDMultiple(N);
6369// Find the trailing bits, which is the minimum of its operands.
6370uint32_t TZ =getMinTrailingZeros(N->getOperand(0));
6371for (constSCEV *Operand :N->operands().drop_front())
6372 TZ = std::min(TZ,getMinTrailingZeros(Operand));
6373return GetShiftedByZeros(TZ);
6374 }
6375casescUMaxExpr:
6376casescSMaxExpr:
6377casescUMinExpr:
6378casescSMinExpr:
6379casescSequentialUMinExpr:
6380return GetGCDMultiple(cast<SCEVNAryExpr>(S));
6381casescUnknown: {
6382// ask ValueTracking for known bits
6383constSCEVUnknown *U = cast<SCEVUnknown>(S);
6384unsigned Known =
6385computeKnownBits(U->getValue(),getDataLayout(), 0, &AC,nullptr, &DT)
6386 .countMinTrailingZeros();
6387return GetShiftedByZeros(Known);
6388 }
6389casescCouldNotCompute:
6390llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6391 }
6392llvm_unreachable("Unknown SCEV kind!");
6393}
6394
6395APIntScalarEvolution::getConstantMultiple(constSCEV *S) {
6396autoI = ConstantMultipleCache.find(S);
6397if (I != ConstantMultipleCache.end())
6398returnI->second;
6399
6400APInt Result = getConstantMultipleImpl(S);
6401auto InsertPair = ConstantMultipleCache.insert({S, Result});
6402assert(InsertPair.second &&"Should insert a new key");
6403return InsertPair.first->second;
6404}
6405
6406APIntScalarEvolution::getNonZeroConstantMultiple(constSCEV *S) {
6407APInt Multiple =getConstantMultiple(S);
6408return Multiple == 0 ?APInt(Multiple.getBitWidth(), 1) : Multiple;
6409}
6410
6411uint32_tScalarEvolution::getMinTrailingZeros(constSCEV *S) {
6412return std::min(getConstantMultiple(S).countTrailingZeros(),
6413 (unsigned)getTypeSizeInBits(S->getType()));
6414}
6415
6416/// Helper method to assign a range to V from metadata present in the IR.
6417static std::optional<ConstantRange>GetRangeFromMetadata(Value *V) {
6418if (Instruction *I = dyn_cast<Instruction>(V)) {
6419if (MDNode *MD =I->getMetadata(LLVMContext::MD_range))
6420returngetConstantRangeFromMetadata(*MD);
6421if (constauto *CB = dyn_cast<CallBase>(V))
6422if (std::optional<ConstantRange>Range = CB->getRange())
6423returnRange;
6424 }
6425if (auto *A = dyn_cast<Argument>(V))
6426if (std::optional<ConstantRange>Range =A->getRange())
6427returnRange;
6428
6429return std::nullopt;
6430}
6431
6432voidScalarEvolution::setNoWrapFlags(SCEVAddRecExpr *AddRec,
6433SCEV::NoWrapFlags Flags) {
6434if (AddRec->getNoWrapFlags(Flags) != Flags) {
6435 AddRec->setNoWrapFlags(Flags);
6436 UnsignedRanges.erase(AddRec);
6437 SignedRanges.erase(AddRec);
6438 ConstantMultipleCache.erase(AddRec);
6439 }
6440}
6441
6442ConstantRange ScalarEvolution::
6443getRangeForUnknownRecurrence(constSCEVUnknown *U) {
6444constDataLayout &DL =getDataLayout();
6445
6446unsignedBitWidth =getTypeSizeInBits(U->getType());
6447constConstantRange FullSet(BitWidth,/*isFullSet=*/true);
6448
6449// Match a simple recurrence of the form: <start, ShiftOp, Step>, and then
6450// use information about the trip count to improve our available range. Note
6451// that the trip count independent cases are already handled by known bits.
6452// WARNING: The definition of recurrence used here is subtly different than
6453// the one used by AddRec (and thus most of this file). Step is allowed to
6454// be arbitrarily loop varying here, where AddRec allows only loop invariant
6455// and other addrecs in the same loop (for non-affine addrecs). The code
6456// below intentionally handles the case where step is not loop invariant.
6457auto *P = dyn_cast<PHINode>(U->getValue());
6458if (!P)
6459return FullSet;
6460
6461// Make sure that no Phi input comes from an unreachable block. Otherwise,
6462// even the values that are not available in these blocks may come from them,
6463// and this leads to false-positive recurrence test.
6464for (auto *Pred :predecessors(P->getParent()))
6465if (!DT.isReachableFromEntry(Pred))
6466return FullSet;
6467
6468BinaryOperator *BO;
6469Value *Start, *Step;
6470if (!matchSimpleRecurrence(P, BO, Start, Step))
6471return FullSet;
6472
6473// If we found a recurrence in reachable code, we must be in a loop. Note
6474// that BO might be in some subloop of L, and that's completely okay.
6475auto *L = LI.getLoopFor(P->getParent());
6476assert(L && L->getHeader() ==P->getParent());
6477if (!L->contains(BO->getParent()))
6478// NOTE: This bailout should be an assert instead. However, asserting
6479// the condition here exposes a case where LoopFusion is querying SCEV
6480// with malformed loop information during the midst of the transform.
6481// There doesn't appear to be an obvious fix, so for the moment bailout
6482// until the caller issue can be fixed. PR49566 tracks the bug.
6483return FullSet;
6484
6485// TODO: Extend to other opcodes such as mul, and div
6486switch (BO->getOpcode()) {
6487default:
6488return FullSet;
6489case Instruction::AShr:
6490case Instruction::LShr:
6491case Instruction::Shl:
6492break;
6493 };
6494
6495if (BO->getOperand(0) !=P)
6496// TODO: Handle the power function forms some day.
6497return FullSet;
6498
6499unsigned TC =getSmallConstantMaxTripCount(L);
6500if (!TC || TC >=BitWidth)
6501return FullSet;
6502
6503auto KnownStart =computeKnownBits(Start, DL, 0, &AC,nullptr, &DT);
6504auto KnownStep =computeKnownBits(Step, DL, 0, &AC,nullptr, &DT);
6505assert(KnownStart.getBitWidth() ==BitWidth &&
6506 KnownStep.getBitWidth() ==BitWidth);
6507
6508// Compute total shift amount, being careful of overflow and bitwidths.
6509auto MaxShiftAmt = KnownStep.getMaxValue();
6510APInt TCAP(BitWidth, TC-1);
6511bool Overflow =false;
6512auto TotalShift = MaxShiftAmt.umul_ov(TCAP, Overflow);
6513if (Overflow)
6514return FullSet;
6515
6516switch (BO->getOpcode()) {
6517default:
6518llvm_unreachable("filtered out above");
6519case Instruction::AShr: {
6520// For each ashr, three cases:
6521// shift = 0 => unchanged value
6522// saturation => 0 or -1
6523// other => a value closer to zero (of the same sign)
6524// Thus, the end value is closer to zero than the start.
6525auto KnownEnd =KnownBits::ashr(KnownStart,
6526KnownBits::makeConstant(TotalShift));
6527if (KnownStart.isNonNegative())
6528// Analogous to lshr (simply not yet canonicalized)
6529returnConstantRange::getNonEmpty(KnownEnd.getMinValue(),
6530 KnownStart.getMaxValue() + 1);
6531if (KnownStart.isNegative())
6532// End >=u Start && End <=s Start
6533returnConstantRange::getNonEmpty(KnownStart.getMinValue(),
6534 KnownEnd.getMaxValue() + 1);
6535break;
6536 }
6537case Instruction::LShr: {
6538// For each lshr, three cases:
6539// shift = 0 => unchanged value
6540// saturation => 0
6541// other => a smaller positive number
6542// Thus, the low end of the unsigned range is the last value produced.
6543auto KnownEnd =KnownBits::lshr(KnownStart,
6544KnownBits::makeConstant(TotalShift));
6545returnConstantRange::getNonEmpty(KnownEnd.getMinValue(),
6546 KnownStart.getMaxValue() + 1);
6547 }
6548case Instruction::Shl: {
6549// Iff no bits are shifted out, value increases on every shift.
6550auto KnownEnd =KnownBits::shl(KnownStart,
6551KnownBits::makeConstant(TotalShift));
6552if (TotalShift.ult(KnownStart.countMinLeadingZeros()))
6553returnConstantRange(KnownStart.getMinValue(),
6554 KnownEnd.getMaxValue() + 1);
6555break;
6556 }
6557 };
6558return FullSet;
6559}
6560
6561constConstantRange &
6562ScalarEvolution::getRangeRefIter(constSCEV *S,
6563 ScalarEvolution::RangeSignHint SignHint) {
6564DenseMap<const SCEV *, ConstantRange> &Cache =
6565 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
6566 : SignedRanges;
6567SmallVector<const SCEV *> WorkList;
6568SmallPtrSet<const SCEV *, 8> Seen;
6569
6570// Add Expr to the worklist, if Expr is either an N-ary expression or a
6571// SCEVUnknown PHI node.
6572auto AddToWorklist = [&WorkList, &Seen, &Cache](constSCEV *Expr) {
6573if (!Seen.insert(Expr).second)
6574return;
6575if (Cache.contains(Expr))
6576return;
6577switch (Expr->getSCEVType()) {
6578casescUnknown:
6579if (!isa<PHINode>(cast<SCEVUnknown>(Expr)->getValue()))
6580break;
6581 [[fallthrough]];
6582casescConstant:
6583casescVScale:
6584casescTruncate:
6585casescZeroExtend:
6586casescSignExtend:
6587casescPtrToInt:
6588casescAddExpr:
6589casescMulExpr:
6590casescUDivExpr:
6591casescAddRecExpr:
6592casescUMaxExpr:
6593casescSMaxExpr:
6594casescUMinExpr:
6595casescSMinExpr:
6596casescSequentialUMinExpr:
6597 WorkList.push_back(Expr);
6598break;
6599casescCouldNotCompute:
6600llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6601 }
6602 };
6603 AddToWorklist(S);
6604
6605// Build worklist by queuing operands of N-ary expressions and phi nodes.
6606for (unsignedI = 0;I != WorkList.size(); ++I) {
6607constSCEV *P = WorkList[I];
6608auto *UnknownS = dyn_cast<SCEVUnknown>(P);
6609// If it is not a `SCEVUnknown`, just recurse into operands.
6610if (!UnknownS) {
6611for (constSCEV *Op :P->operands())
6612 AddToWorklist(Op);
6613continue;
6614 }
6615// `SCEVUnknown`'s require special treatment.
6616if (constPHINode *P = dyn_cast<PHINode>(UnknownS->getValue())) {
6617if (!PendingPhiRangesIter.insert(P).second)
6618continue;
6619for (auto &Op :reverse(P->operands()))
6620 AddToWorklist(getSCEV(Op));
6621 }
6622 }
6623
6624if (!WorkList.empty()) {
6625// Use getRangeRef to compute ranges for items in the worklist in reverse
6626// order. This will force ranges for earlier operands to be computed before
6627// their users in most cases.
6628for (constSCEV *P :reverse(drop_begin(WorkList))) {
6629 getRangeRef(P, SignHint);
6630
6631if (auto *UnknownS = dyn_cast<SCEVUnknown>(P))
6632if (constPHINode *P = dyn_cast<PHINode>(UnknownS->getValue()))
6633 PendingPhiRangesIter.erase(P);
6634 }
6635 }
6636
6637return getRangeRef(S, SignHint, 0);
6638}
6639
6640/// Determine the range for a particular SCEV. If SignHint is
6641/// HINT_RANGE_UNSIGNED (resp. HINT_RANGE_SIGNED) then getRange prefers ranges
6642/// with a "cleaner" unsigned (resp. signed) representation.
6643constConstantRange &ScalarEvolution::getRangeRef(
6644constSCEV *S, ScalarEvolution::RangeSignHint SignHint,unsignedDepth) {
6645DenseMap<const SCEV *, ConstantRange> &Cache =
6646 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ? UnsignedRanges
6647 : SignedRanges;
6648ConstantRange::PreferredRangeType RangeType =
6649 SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED ?ConstantRange::Unsigned
6650 :ConstantRange::Signed;
6651
6652// See if we've computed this range already.
6653DenseMap<const SCEV *, ConstantRange>::iteratorI = Cache.find(S);
6654if (I != Cache.end())
6655returnI->second;
6656
6657if (constSCEVConstant *C = dyn_cast<SCEVConstant>(S))
6658return setRange(C, SignHint,ConstantRange(C->getAPInt()));
6659
6660// Switch to iteratively computing the range for S, if it is part of a deeply
6661// nested expression.
6662if (Depth >RangeIterThreshold)
6663return getRangeRefIter(S, SignHint);
6664
6665unsignedBitWidth =getTypeSizeInBits(S->getType());
6666ConstantRange ConservativeResult(BitWidth,/*isFullSet=*/true);
6667usingOBO =OverflowingBinaryOperator;
6668
6669// If the value has known zeros, the maximum value will have those known zeros
6670// as well.
6671if (SignHint == ScalarEvolution::HINT_RANGE_UNSIGNED) {
6672APInt Multiple =getNonZeroConstantMultiple(S);
6673APInt Remainder =APInt::getMaxValue(BitWidth).urem(Multiple);
6674if (!Remainder.isZero())
6675 ConservativeResult =
6676ConstantRange(APInt::getMinValue(BitWidth),
6677APInt::getMaxValue(BitWidth) - Remainder + 1);
6678 }
6679else {
6680uint32_t TZ =getMinTrailingZeros(S);
6681if (TZ != 0) {
6682 ConservativeResult =ConstantRange(
6683APInt::getSignedMinValue(BitWidth),
6684APInt::getSignedMaxValue(BitWidth).ashr(TZ).shl(TZ) + 1);
6685 }
6686 }
6687
6688switch (S->getSCEVType()) {
6689casescConstant:
6690llvm_unreachable("Already handled above.");
6691casescVScale:
6692return setRange(S, SignHint,getVScaleRange(&F,BitWidth));
6693casescTruncate: {
6694constSCEVTruncateExpr *Trunc = cast<SCEVTruncateExpr>(S);
6695ConstantRangeX = getRangeRef(Trunc->getOperand(), SignHint,Depth + 1);
6696return setRange(
6697 Trunc, SignHint,
6698 ConservativeResult.intersectWith(X.truncate(BitWidth), RangeType));
6699 }
6700casescZeroExtend: {
6701constSCEVZeroExtendExpr *ZExt = cast<SCEVZeroExtendExpr>(S);
6702ConstantRangeX = getRangeRef(ZExt->getOperand(), SignHint,Depth + 1);
6703return setRange(
6704 ZExt, SignHint,
6705 ConservativeResult.intersectWith(X.zeroExtend(BitWidth), RangeType));
6706 }
6707casescSignExtend: {
6708constSCEVSignExtendExpr *SExt = cast<SCEVSignExtendExpr>(S);
6709ConstantRangeX = getRangeRef(SExt->getOperand(), SignHint,Depth + 1);
6710return setRange(
6711 SExt, SignHint,
6712 ConservativeResult.intersectWith(X.signExtend(BitWidth), RangeType));
6713 }
6714casescPtrToInt: {
6715constSCEVPtrToIntExpr *PtrToInt = cast<SCEVPtrToIntExpr>(S);
6716ConstantRangeX = getRangeRef(PtrToInt->getOperand(), SignHint,Depth + 1);
6717return setRange(PtrToInt, SignHint,X);
6718 }
6719casescAddExpr: {
6720constSCEVAddExpr *Add = cast<SCEVAddExpr>(S);
6721ConstantRangeX = getRangeRef(Add->getOperand(0), SignHint,Depth + 1);
6722unsigned WrapType = OBO::AnyWrap;
6723if (Add->hasNoSignedWrap())
6724 WrapType |= OBO::NoSignedWrap;
6725if (Add->hasNoUnsignedWrap())
6726 WrapType |= OBO::NoUnsignedWrap;
6727for (constSCEV *Op :drop_begin(Add->operands()))
6728X =X.addWithNoWrap(getRangeRef(Op, SignHint,Depth + 1), WrapType,
6729 RangeType);
6730return setRange(Add, SignHint,
6731 ConservativeResult.intersectWith(X, RangeType));
6732 }
6733casescMulExpr: {
6734constSCEVMulExpr *Mul = cast<SCEVMulExpr>(S);
6735ConstantRangeX = getRangeRef(Mul->getOperand(0), SignHint,Depth + 1);
6736for (constSCEV *Op :drop_begin(Mul->operands()))
6737X =X.multiply(getRangeRef(Op, SignHint,Depth + 1));
6738return setRange(Mul, SignHint,
6739 ConservativeResult.intersectWith(X, RangeType));
6740 }
6741casescUDivExpr: {
6742constSCEVUDivExpr *UDiv = cast<SCEVUDivExpr>(S);
6743ConstantRangeX = getRangeRef(UDiv->getLHS(), SignHint,Depth + 1);
6744ConstantRangeY = getRangeRef(UDiv->getRHS(), SignHint,Depth + 1);
6745return setRange(UDiv, SignHint,
6746 ConservativeResult.intersectWith(X.udiv(Y), RangeType));
6747 }
6748casescAddRecExpr: {
6749constSCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(S);
6750// If there's no unsigned wrap, the value will never be less than its
6751// initial value.
6752if (AddRec->hasNoUnsignedWrap()) {
6753APInt UnsignedMinValue =getUnsignedRangeMin(AddRec->getStart());
6754if (!UnsignedMinValue.isZero())
6755 ConservativeResult = ConservativeResult.intersectWith(
6756ConstantRange(UnsignedMinValue,APInt(BitWidth, 0)), RangeType);
6757 }
6758
6759// If there's no signed wrap, and all the operands except initial value have
6760// the same sign or zero, the value won't ever be:
6761// 1: smaller than initial value if operands are non negative,
6762// 2: bigger than initial value if operands are non positive.
6763// For both cases, value can not cross signed min/max boundary.
6764if (AddRec->hasNoSignedWrap()) {
6765bool AllNonNeg =true;
6766bool AllNonPos =true;
6767for (unsigned i = 1, e = AddRec->getNumOperands(); i != e; ++i) {
6768if (!isKnownNonNegative(AddRec->getOperand(i)))
6769 AllNonNeg =false;
6770if (!isKnownNonPositive(AddRec->getOperand(i)))
6771 AllNonPos =false;
6772 }
6773if (AllNonNeg)
6774 ConservativeResult = ConservativeResult.intersectWith(
6775ConstantRange::getNonEmpty(getSignedRangeMin(AddRec->getStart()),
6776APInt::getSignedMinValue(BitWidth)),
6777 RangeType);
6778elseif (AllNonPos)
6779 ConservativeResult = ConservativeResult.intersectWith(
6780ConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth),
6781getSignedRangeMax(AddRec->getStart()) +
6782 1),
6783 RangeType);
6784 }
6785
6786// TODO: non-affine addrec
6787if (AddRec->isAffine()) {
6788constSCEV *MaxBEScev =
6789getConstantMaxBackedgeTakenCount(AddRec->getLoop());
6790if (!isa<SCEVCouldNotCompute>(MaxBEScev)) {
6791APInt MaxBECount = cast<SCEVConstant>(MaxBEScev)->getAPInt();
6792
6793// Adjust MaxBECount to the same bitwidth as AddRec. We can truncate if
6794// MaxBECount's active bits are all <= AddRec's bit width.
6795if (MaxBECount.getBitWidth() >BitWidth &&
6796 MaxBECount.getActiveBits() <=BitWidth)
6797 MaxBECount = MaxBECount.trunc(BitWidth);
6798elseif (MaxBECount.getBitWidth() <BitWidth)
6799 MaxBECount = MaxBECount.zext(BitWidth);
6800
6801if (MaxBECount.getBitWidth() ==BitWidth) {
6802auto RangeFromAffine = getRangeForAffineAR(
6803 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount);
6804 ConservativeResult =
6805 ConservativeResult.intersectWith(RangeFromAffine, RangeType);
6806
6807auto RangeFromFactoring = getRangeViaFactoring(
6808 AddRec->getStart(), AddRec->getStepRecurrence(*this), MaxBECount);
6809 ConservativeResult =
6810 ConservativeResult.intersectWith(RangeFromFactoring, RangeType);
6811 }
6812 }
6813
6814// Now try symbolic BE count and more powerful methods.
6815if (UseExpensiveRangeSharpening) {
6816constSCEV *SymbolicMaxBECount =
6817getSymbolicMaxBackedgeTakenCount(AddRec->getLoop());
6818if (!isa<SCEVCouldNotCompute>(SymbolicMaxBECount) &&
6819getTypeSizeInBits(MaxBEScev->getType()) <=BitWidth &&
6820 AddRec->hasNoSelfWrap()) {
6821auto RangeFromAffineNew = getRangeForAffineNoSelfWrappingAR(
6822 AddRec, SymbolicMaxBECount,BitWidth, SignHint);
6823 ConservativeResult =
6824 ConservativeResult.intersectWith(RangeFromAffineNew, RangeType);
6825 }
6826 }
6827 }
6828
6829return setRange(AddRec, SignHint, std::move(ConservativeResult));
6830 }
6831casescUMaxExpr:
6832casescSMaxExpr:
6833casescUMinExpr:
6834casescSMinExpr:
6835casescSequentialUMinExpr: {
6836Intrinsic::IDID;
6837switch (S->getSCEVType()) {
6838casescUMaxExpr:
6839ID = Intrinsic::umax;
6840break;
6841casescSMaxExpr:
6842ID = Intrinsic::smax;
6843break;
6844casescUMinExpr:
6845casescSequentialUMinExpr:
6846ID = Intrinsic::umin;
6847break;
6848casescSMinExpr:
6849ID = Intrinsic::smin;
6850break;
6851default:
6852llvm_unreachable("Unknown SCEVMinMaxExpr/SCEVSequentialMinMaxExpr.");
6853 }
6854
6855constauto *NAry = cast<SCEVNAryExpr>(S);
6856ConstantRangeX = getRangeRef(NAry->getOperand(0), SignHint,Depth + 1);
6857for (unsigned i = 1, e = NAry->getNumOperands(); i != e; ++i)
6858X =X.intrinsic(
6859ID, {X, getRangeRef(NAry->getOperand(i), SignHint,Depth + 1)});
6860return setRange(S, SignHint,
6861 ConservativeResult.intersectWith(X, RangeType));
6862 }
6863casescUnknown: {
6864constSCEVUnknown *U = cast<SCEVUnknown>(S);
6865Value *V =U->getValue();
6866
6867// Check if the IR explicitly contains !range metadata.
6868 std::optional<ConstantRange> MDRange =GetRangeFromMetadata(V);
6869if (MDRange)
6870 ConservativeResult =
6871 ConservativeResult.intersectWith(*MDRange, RangeType);
6872
6873// Use facts about recurrences in the underlying IR. Note that add
6874// recurrences are AddRecExprs and thus don't hit this path. This
6875// primarily handles shift recurrences.
6876auto CR = getRangeForUnknownRecurrence(U);
6877 ConservativeResult = ConservativeResult.intersectWith(CR);
6878
6879// See if ValueTracking can give us a useful range.
6880constDataLayout &DL =getDataLayout();
6881KnownBits Known =computeKnownBits(V, DL, 0, &AC,nullptr, &DT);
6882if (Known.getBitWidth() !=BitWidth)
6883 Known = Known.zextOrTrunc(BitWidth);
6884
6885// ValueTracking may be able to compute a tighter result for the number of
6886// sign bits than for the value of those sign bits.
6887unsigned NS =ComputeNumSignBits(V, DL, 0, &AC,nullptr, &DT);
6888if (U->getType()->isPointerTy()) {
6889// If the pointer size is larger than the index size type, this can cause
6890// NS to be larger than BitWidth. So compensate for this.
6891unsigned ptrSize =DL.getPointerTypeSizeInBits(U->getType());
6892int ptrIdxDiff = ptrSize -BitWidth;
6893if (ptrIdxDiff > 0 && ptrSize >BitWidth && NS > (unsigned)ptrIdxDiff)
6894 NS -= ptrIdxDiff;
6895 }
6896
6897if (NS > 1) {
6898// If we know any of the sign bits, we know all of the sign bits.
6899if (!Known.Zero.getHiBits(NS).isZero())
6900 Known.Zero.setHighBits(NS);
6901if (!Known.One.getHiBits(NS).isZero())
6902 Known.One.setHighBits(NS);
6903 }
6904
6905if (Known.getMinValue() != Known.getMaxValue() + 1)
6906 ConservativeResult = ConservativeResult.intersectWith(
6907ConstantRange(Known.getMinValue(), Known.getMaxValue() + 1),
6908 RangeType);
6909if (NS > 1)
6910 ConservativeResult = ConservativeResult.intersectWith(
6911ConstantRange(APInt::getSignedMinValue(BitWidth).ashr(NS - 1),
6912APInt::getSignedMaxValue(BitWidth).ashr(NS - 1) + 1),
6913 RangeType);
6914
6915if (U->getType()->isPointerTy() && SignHint == HINT_RANGE_UNSIGNED) {
6916// Strengthen the range if the underlying IR value is a
6917// global/alloca/heap allocation using the size of the object.
6918bool CanBeNull, CanBeFreed;
6919uint64_t DerefBytes =
6920V->getPointerDereferenceableBytes(DL, CanBeNull, CanBeFreed);
6921if (DerefBytes > 1 &&isUIntN(BitWidth, DerefBytes)) {
6922// The highest address the object can start is DerefBytes bytes before
6923// the end (unsigned max value). If this value is not a multiple of the
6924// alignment, the last possible start value is the next lowest multiple
6925// of the alignment. Note: The computations below cannot overflow,
6926// because if they would there's no possible start address for the
6927// object.
6928APInt MaxVal =
6929APInt::getMaxValue(BitWidth) -APInt(BitWidth, DerefBytes);
6930uint64_tAlign =U->getValue()->getPointerAlignment(DL).value();
6931uint64_t Rem = MaxVal.urem(Align);
6932 MaxVal -=APInt(BitWidth, Rem);
6933APInt MinVal =APInt::getZero(BitWidth);
6934if (llvm::isKnownNonZero(V, DL))
6935 MinVal =Align;
6936 ConservativeResult = ConservativeResult.intersectWith(
6937ConstantRange::getNonEmpty(MinVal, MaxVal + 1), RangeType);
6938 }
6939 }
6940
6941// A range of Phi is a subset of union of all ranges of its input.
6942if (PHINode *Phi = dyn_cast<PHINode>(V)) {
6943// Make sure that we do not run over cycled Phis.
6944if (PendingPhiRanges.insert(Phi).second) {
6945ConstantRange RangeFromOps(BitWidth,/*isFullSet=*/false);
6946
6947for (constauto &Op :Phi->operands()) {
6948auto OpRange = getRangeRef(getSCEV(Op), SignHint,Depth + 1);
6949 RangeFromOps = RangeFromOps.unionWith(OpRange);
6950// No point to continue if we already have a full set.
6951if (RangeFromOps.isFullSet())
6952break;
6953 }
6954 ConservativeResult =
6955 ConservativeResult.intersectWith(RangeFromOps, RangeType);
6956bool Erased = PendingPhiRanges.erase(Phi);
6957assert(Erased &&"Failed to erase Phi properly?");
6958 (void)Erased;
6959 }
6960 }
6961
6962// vscale can't be equal to zero
6963if (constauto *II = dyn_cast<IntrinsicInst>(V))
6964if (II->getIntrinsicID() == Intrinsic::vscale) {
6965ConstantRange Disallowed =APInt::getZero(BitWidth);
6966 ConservativeResult = ConservativeResult.difference(Disallowed);
6967 }
6968
6969return setRange(U, SignHint, std::move(ConservativeResult));
6970 }
6971casescCouldNotCompute:
6972llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
6973 }
6974
6975return setRange(S, SignHint, std::move(ConservativeResult));
6976}
6977
6978// Given a StartRange, Step and MaxBECount for an expression compute a range of
6979// values that the expression can take. Initially, the expression has a value
6980// from StartRange and then is changed by Step up to MaxBECount times. Signed
6981// argument defines if we treat Step as signed or unsigned.
6982staticConstantRangegetRangeForAffineARHelper(APInt Step,
6983constConstantRange &StartRange,
6984constAPInt &MaxBECount,
6985boolSigned) {
6986unsignedBitWidth = Step.getBitWidth();
6987assert(BitWidth == StartRange.getBitWidth() &&
6988BitWidth == MaxBECount.getBitWidth() &&"mismatched bit widths");
6989// If either Step or MaxBECount is 0, then the expression won't change, and we
6990// just need to return the initial range.
6991if (Step == 0 || MaxBECount == 0)
6992return StartRange;
6993
6994// If we don't know anything about the initial value (i.e. StartRange is
6995// FullRange), then we don't know anything about the final range either.
6996// Return FullRange.
6997if (StartRange.isFullSet())
6998return ConstantRange::getFull(BitWidth);
6999
7000// If Step is signed and negative, then we use its absolute value, but we also
7001// note that we're moving in the opposite direction.
7002bool Descending =Signed && Step.isNegative();
7003
7004if (Signed)
7005// This is correct even for INT_SMIN. Let's look at i8 to illustrate this:
7006// abs(INT_SMIN) = abs(-128) = abs(0x80) = -0x80 = 0x80 = 128.
7007// This equations hold true due to the well-defined wrap-around behavior of
7008// APInt.
7009 Step = Step.abs();
7010
7011// Check if Offset is more than full span of BitWidth. If it is, the
7012// expression is guaranteed to overflow.
7013if (APInt::getMaxValue(StartRange.getBitWidth()).udiv(Step).ult(MaxBECount))
7014return ConstantRange::getFull(BitWidth);
7015
7016// Offset is by how much the expression can change. Checks above guarantee no
7017// overflow here.
7018APIntOffset = Step * MaxBECount;
7019
7020// Minimum value of the final range will match the minimal value of StartRange
7021// if the expression is increasing and will be decreased by Offset otherwise.
7022// Maximum value of the final range will match the maximal value of StartRange
7023// if the expression is decreasing and will be increased by Offset otherwise.
7024APInt StartLower = StartRange.getLower();
7025APInt StartUpper = StartRange.getUpper() - 1;
7026APInt MovedBoundary = Descending ? (StartLower - std::move(Offset))
7027 : (StartUpper + std::move(Offset));
7028
7029// It's possible that the new minimum/maximum value will fall into the initial
7030// range (due to wrap around). This means that the expression can take any
7031// value in this bitwidth, and we have to return full range.
7032if (StartRange.contains(MovedBoundary))
7033return ConstantRange::getFull(BitWidth);
7034
7035APInt NewLower =
7036 Descending ? std::move(MovedBoundary) : std::move(StartLower);
7037APInt NewUpper =
7038 Descending ? std::move(StartUpper) : std::move(MovedBoundary);
7039 NewUpper += 1;
7040
7041// No overflow detected, return [StartLower, StartUpper + Offset + 1) range.
7042returnConstantRange::getNonEmpty(std::move(NewLower), std::move(NewUpper));
7043}
7044
7045ConstantRange ScalarEvolution::getRangeForAffineAR(constSCEV *Start,
7046constSCEV *Step,
7047constAPInt &MaxBECount) {
7048assert(getTypeSizeInBits(Start->getType()) ==
7049getTypeSizeInBits(Step->getType()) &&
7050getTypeSizeInBits(Start->getType()) == MaxBECount.getBitWidth() &&
7051"mismatched bit widths");
7052
7053// First, consider step signed.
7054ConstantRange StartSRange =getSignedRange(Start);
7055ConstantRange StepSRange =getSignedRange(Step);
7056
7057// If Step can be both positive and negative, we need to find ranges for the
7058// maximum absolute step values in both directions and union them.
7059ConstantRange SR =getRangeForAffineARHelper(
7060 StepSRange.getSignedMin(), StartSRange, MaxBECount,/* Signed = */true);
7061 SR = SR.unionWith(getRangeForAffineARHelper(StepSRange.getSignedMax(),
7062 StartSRange, MaxBECount,
7063/* Signed = */true));
7064
7065// Next, consider step unsigned.
7066ConstantRange UR =getRangeForAffineARHelper(
7067getUnsignedRangeMax(Step),getUnsignedRange(Start), MaxBECount,
7068/* Signed = */false);
7069
7070// Finally, intersect signed and unsigned ranges.
7071return SR.intersectWith(UR,ConstantRange::Smallest);
7072}
7073
7074ConstantRange ScalarEvolution::getRangeForAffineNoSelfWrappingAR(
7075constSCEVAddRecExpr *AddRec,constSCEV *MaxBECount,unsignedBitWidth,
7076 ScalarEvolution::RangeSignHint SignHint) {
7077assert(AddRec->isAffine() &&"Non-affine AddRecs are not suppored!\n");
7078assert(AddRec->hasNoSelfWrap() &&
7079"This only works for non-self-wrapping AddRecs!");
7080constbool IsSigned = SignHint == HINT_RANGE_SIGNED;
7081constSCEV *Step = AddRec->getStepRecurrence(*this);
7082// Only deal with constant step to save compile time.
7083if (!isa<SCEVConstant>(Step))
7084return ConstantRange::getFull(BitWidth);
7085// Let's make sure that we can prove that we do not self-wrap during
7086// MaxBECount iterations. We need this because MaxBECount is a maximum
7087// iteration count estimate, and we might infer nw from some exit for which we
7088// do not know max exit count (or any other side reasoning).
7089// TODO: Turn into assert at some point.
7090if (getTypeSizeInBits(MaxBECount->getType()) >
7091getTypeSizeInBits(AddRec->getType()))
7092return ConstantRange::getFull(BitWidth);
7093 MaxBECount =getNoopOrZeroExtend(MaxBECount, AddRec->getType());
7094constSCEV *RangeWidth =getMinusOne(AddRec->getType());
7095constSCEV *StepAbs =getUMinExpr(Step,getNegativeSCEV(Step));
7096constSCEV *MaxItersWithoutWrap =getUDivExpr(RangeWidth, StepAbs);
7097if (!isKnownPredicateViaConstantRanges(ICmpInst::ICMP_ULE, MaxBECount,
7098 MaxItersWithoutWrap))
7099return ConstantRange::getFull(BitWidth);
7100
7101ICmpInst::Predicate LEPred =
7102 IsSigned ?ICmpInst::ICMP_SLE :ICmpInst::ICMP_ULE;
7103ICmpInst::Predicate GEPred =
7104 IsSigned ?ICmpInst::ICMP_SGE :ICmpInst::ICMP_UGE;
7105constSCEV *End = AddRec->evaluateAtIteration(MaxBECount, *this);
7106
7107// We know that there is no self-wrap. Let's take Start and End values and
7108// look at all intermediate values V1, V2, ..., Vn that IndVar takes during
7109// the iteration. They either lie inside the range [Min(Start, End),
7110// Max(Start, End)] or outside it:
7111//
7112// Case 1: RangeMin ... Start V1 ... VN End ... RangeMax;
7113// Case 2: RangeMin Vk ... V1 Start ... End Vn ... Vk + 1 RangeMax;
7114//
7115// No self wrap flag guarantees that the intermediate values cannot be BOTH
7116// outside and inside the range [Min(Start, End), Max(Start, End)]. Using that
7117// knowledge, let's try to prove that we are dealing with Case 1. It is so if
7118// Start <= End and step is positive, or Start >= End and step is negative.
7119constSCEV *Start =applyLoopGuards(AddRec->getStart(), AddRec->getLoop());
7120ConstantRange StartRange = getRangeRef(Start, SignHint);
7121ConstantRange EndRange = getRangeRef(End, SignHint);
7122ConstantRange RangeBetween = StartRange.unionWith(EndRange);
7123// If they already cover full iteration space, we will know nothing useful
7124// even if we prove what we want to prove.
7125if (RangeBetween.isFullSet())
7126return RangeBetween;
7127// Only deal with ranges that do not wrap (i.e. RangeMin < RangeMax).
7128bool IsWrappedSet = IsSigned ? RangeBetween.isSignWrappedSet()
7129 : RangeBetween.isWrappedSet();
7130if (IsWrappedSet)
7131return ConstantRange::getFull(BitWidth);
7132
7133if (isKnownPositive(Step) &&
7134 isKnownPredicateViaConstantRanges(LEPred, Start,End))
7135return RangeBetween;
7136if (isKnownNegative(Step) &&
7137 isKnownPredicateViaConstantRanges(GEPred, Start,End))
7138return RangeBetween;
7139return ConstantRange::getFull(BitWidth);
7140}
7141
7142ConstantRange ScalarEvolution::getRangeViaFactoring(constSCEV *Start,
7143constSCEV *Step,
7144constAPInt &MaxBECount) {
7145// RangeOf({C?A:B,+,C?P:Q}) == RangeOf(C?{A,+,P}:{B,+,Q})
7146// == RangeOf({A,+,P}) union RangeOf({B,+,Q})
7147
7148unsignedBitWidth = MaxBECount.getBitWidth();
7149assert(getTypeSizeInBits(Start->getType()) ==BitWidth &&
7150getTypeSizeInBits(Step->getType()) ==BitWidth &&
7151"mismatched bit widths");
7152
7153structSelectPattern {
7154Value *Condition =nullptr;
7155APInt TrueValue;
7156APInt FalseValue;
7157
7158explicit SelectPattern(ScalarEvolution &SE,unsignedBitWidth,
7159constSCEV *S) {
7160 std::optional<unsigned> CastOp;
7161APIntOffset(BitWidth, 0);
7162
7163assert(SE.getTypeSizeInBits(S->getType()) ==BitWidth &&
7164"Should be!");
7165
7166// Peel off a constant offset:
7167if (auto *SA = dyn_cast<SCEVAddExpr>(S)) {
7168// In the future we could consider being smarter here and handle
7169// {Start+Step,+,Step} too.
7170if (SA->getNumOperands() != 2 || !isa<SCEVConstant>(SA->getOperand(0)))
7171return;
7172
7173Offset = cast<SCEVConstant>(SA->getOperand(0))->getAPInt();
7174 S = SA->getOperand(1);
7175 }
7176
7177// Peel off a cast operation
7178if (auto *SCast = dyn_cast<SCEVIntegralCastExpr>(S)) {
7179 CastOp = SCast->getSCEVType();
7180 S = SCast->getOperand();
7181 }
7182
7183using namespacellvm::PatternMatch;
7184
7185auto *SU = dyn_cast<SCEVUnknown>(S);
7186constAPInt *TrueVal, *FalseVal;
7187if (!SU ||
7188 !match(SU->getValue(),m_Select(m_Value(Condition),m_APInt(TrueVal),
7189m_APInt(FalseVal)))) {
7190 Condition =nullptr;
7191return;
7192 }
7193
7194 TrueValue = *TrueVal;
7195 FalseValue = *FalseVal;
7196
7197// Re-apply the cast we peeled off earlier
7198if (CastOp)
7199switch (*CastOp) {
7200default:
7201llvm_unreachable("Unknown SCEV cast type!");
7202
7203casescTruncate:
7204 TrueValue = TrueValue.trunc(BitWidth);
7205 FalseValue = FalseValue.trunc(BitWidth);
7206break;
7207casescZeroExtend:
7208 TrueValue = TrueValue.zext(BitWidth);
7209 FalseValue = FalseValue.zext(BitWidth);
7210break;
7211casescSignExtend:
7212 TrueValue = TrueValue.sext(BitWidth);
7213 FalseValue = FalseValue.sext(BitWidth);
7214break;
7215 }
7216
7217// Re-apply the constant offset we peeled off earlier
7218 TrueValue +=Offset;
7219 FalseValue +=Offset;
7220 }
7221
7222bool isRecognized() {return Condition !=nullptr; }
7223 };
7224
7225 SelectPattern StartPattern(*this,BitWidth, Start);
7226if (!StartPattern.isRecognized())
7227return ConstantRange::getFull(BitWidth);
7228
7229 SelectPattern StepPattern(*this,BitWidth, Step);
7230if (!StepPattern.isRecognized())
7231return ConstantRange::getFull(BitWidth);
7232
7233if (StartPattern.Condition != StepPattern.Condition) {
7234// We don't handle this case today; but we could, by considering four
7235// possibilities below instead of two. I'm not sure if there are cases where
7236// that will help over what getRange already does, though.
7237return ConstantRange::getFull(BitWidth);
7238 }
7239
7240// NB! Calling ScalarEvolution::getConstant is fine, but we should not try to
7241// construct arbitrary general SCEV expressions here. This function is called
7242// from deep in the call stack, and calling getSCEV (on a sext instruction,
7243// say) can end up caching a suboptimal value.
7244
7245// FIXME: without the explicit `this` receiver below, MSVC errors out with
7246// C2352 and C2512 (otherwise it isn't needed).
7247
7248constSCEV *TrueStart = this->getConstant(StartPattern.TrueValue);
7249constSCEV *TrueStep = this->getConstant(StepPattern.TrueValue);
7250constSCEV *FalseStart = this->getConstant(StartPattern.FalseValue);
7251constSCEV *FalseStep = this->getConstant(StepPattern.FalseValue);
7252
7253ConstantRange TrueRange =
7254 this->getRangeForAffineAR(TrueStart, TrueStep, MaxBECount);
7255ConstantRange FalseRange =
7256 this->getRangeForAffineAR(FalseStart, FalseStep, MaxBECount);
7257
7258return TrueRange.unionWith(FalseRange);
7259}
7260
7261SCEV::NoWrapFlags ScalarEvolution::getNoWrapFlagsFromUB(constValue *V) {
7262if (isa<ConstantExpr>(V))returnSCEV::FlagAnyWrap;
7263constBinaryOperator *BinOp = cast<BinaryOperator>(V);
7264
7265// Return early if there are no flags to propagate to the SCEV.
7266SCEV::NoWrapFlagsFlags =SCEV::FlagAnyWrap;
7267if (BinOp->hasNoUnsignedWrap())
7268Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNUW);
7269if (BinOp->hasNoSignedWrap())
7270Flags =ScalarEvolution::setFlags(Flags,SCEV::FlagNSW);
7271if (Flags ==SCEV::FlagAnyWrap)
7272returnSCEV::FlagAnyWrap;
7273
7274return isSCEVExprNeverPoison(BinOp) ?Flags :SCEV::FlagAnyWrap;
7275}
7276
7277constInstruction *
7278ScalarEvolution::getNonTrivialDefiningScopeBound(constSCEV *S) {
7279if (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S))
7280return &*AddRec->getLoop()->getHeader()->begin();
7281if (auto *U = dyn_cast<SCEVUnknown>(S))
7282if (auto *I = dyn_cast<Instruction>(U->getValue()))
7283returnI;
7284returnnullptr;
7285}
7286
7287constInstruction *
7288ScalarEvolution::getDefiningScopeBound(ArrayRef<const SCEV *> Ops,
7289bool &Precise) {
7290 Precise =true;
7291// Do a bounded search of the def relation of the requested SCEVs.
7292SmallSet<const SCEV *, 16> Visited;
7293SmallVector<const SCEV *> Worklist;
7294auto pushOp = [&](constSCEV *S) {
7295if (!Visited.insert(S).second)
7296return;
7297// Threshold of 30 here is arbitrary.
7298if (Visited.size() > 30) {
7299 Precise =false;
7300return;
7301 }
7302 Worklist.push_back(S);
7303 };
7304
7305for (constauto *S : Ops)
7306 pushOp(S);
7307
7308constInstruction *Bound =nullptr;
7309while (!Worklist.empty()) {
7310auto *S = Worklist.pop_back_val();
7311if (auto *DefI = getNonTrivialDefiningScopeBound(S)) {
7312if (!Bound || DT.dominates(Bound, DefI))
7313 Bound = DefI;
7314 }else {
7315for (constauto *Op : S->operands())
7316 pushOp(Op);
7317 }
7318 }
7319return Bound ? Bound : &*F.getEntryBlock().begin();
7320}
7321
7322constInstruction *
7323ScalarEvolution::getDefiningScopeBound(ArrayRef<const SCEV *> Ops) {
7324bool Discard;
7325return getDefiningScopeBound(Ops, Discard);
7326}
7327
7328bool ScalarEvolution::isGuaranteedToTransferExecutionTo(constInstruction *A,
7329constInstruction *B) {
7330if (A->getParent() ==B->getParent() &&
7331isGuaranteedToTransferExecutionToSuccessor(A->getIterator(),
7332B->getIterator()))
7333returntrue;
7334
7335auto *BLoop = LI.getLoopFor(B->getParent());
7336if (BLoop && BLoop->getHeader() ==B->getParent() &&
7337 BLoop->getLoopPreheader() ==A->getParent() &&
7338isGuaranteedToTransferExecutionToSuccessor(A->getIterator(),
7339A->getParent()->end()) &&
7340isGuaranteedToTransferExecutionToSuccessor(B->getParent()->begin(),
7341B->getIterator()))
7342returntrue;
7343returnfalse;
7344}
7345
7346bool ScalarEvolution::isGuaranteedNotToBePoison(constSCEV *Op) {
7347 SCEVPoisonCollector PC(/* LookThroughMaybePoisonBlocking */true);
7348visitAll(Op, PC);
7349return PC.MaybePoison.empty();
7350}
7351
7352bool ScalarEvolution::isGuaranteedNotToCauseUB(constSCEV *Op) {
7353return !SCEVExprContains(Op, [this](constSCEV *S) {
7354auto *UDiv = dyn_cast<SCEVUDivExpr>(S);
7355// The UDiv may be UB if the divisor is poison or zero. Unless the divisor
7356// is a non-zero constant, we have to assume the UDiv may be UB.
7357return UDiv && (!isKnownNonZero(UDiv->getOperand(1)) ||
7358 !isGuaranteedNotToBePoison(UDiv->getOperand(1)));
7359 });
7360}
7361
7362bool ScalarEvolution::isSCEVExprNeverPoison(constInstruction *I) {
7363// Only proceed if we can prove that I does not yield poison.
7364if (!programUndefinedIfPoison(I))
7365returnfalse;
7366
7367// At this point we know that if I is executed, then it does not wrap
7368// according to at least one of NSW or NUW. If I is not executed, then we do
7369// not know if the calculation that I represents would wrap. Multiple
7370// instructions can map to the same SCEV. If we apply NSW or NUW from I to
7371// the SCEV, we must guarantee no wrapping for that SCEV also when it is
7372// derived from other instructions that map to the same SCEV. We cannot make
7373// that guarantee for cases where I is not executed. So we need to find a
7374// upper bound on the defining scope for the SCEV, and prove that I is
7375// executed every time we enter that scope. When the bounding scope is a
7376// loop (the common case), this is equivalent to proving I executes on every
7377// iteration of that loop.
7378SmallVector<const SCEV *> SCEVOps;
7379for (constUse &Op :I->operands()) {
7380// I could be an extractvalue from a call to an overflow intrinsic.
7381// TODO: We can do better here in some cases.
7382if (isSCEVable(Op->getType()))
7383 SCEVOps.push_back(getSCEV(Op));
7384 }
7385auto *DefI = getDefiningScopeBound(SCEVOps);
7386return isGuaranteedToTransferExecutionTo(DefI,I);
7387}
7388
7389bool ScalarEvolution::isAddRecNeverPoison(constInstruction *I,constLoop *L) {
7390// If we know that \c I can never be poison period, then that's enough.
7391if (isSCEVExprNeverPoison(I))
7392returntrue;
7393
7394// If the loop only has one exit, then we know that, if the loop is entered,
7395// any instruction dominating that exit will be executed. If any such
7396// instruction would result in UB, the addrec cannot be poison.
7397//
7398// This is basically the same reasoning as in isSCEVExprNeverPoison(), but
7399// also handles uses outside the loop header (they just need to dominate the
7400// single exit).
7401
7402auto *ExitingBB =L->getExitingBlock();
7403if (!ExitingBB || !loopHasNoAbnormalExits(L))
7404returnfalse;
7405
7406SmallPtrSet<const Value *, 16> KnownPoison;
7407SmallVector<const Instruction *, 8> Worklist;
7408
7409// We start by assuming \c I, the post-inc add recurrence, is poison. Only
7410// things that are known to be poison under that assumption go on the
7411// Worklist.
7412 KnownPoison.insert(I);
7413 Worklist.push_back(I);
7414
7415while (!Worklist.empty()) {
7416constInstruction *Poison = Worklist.pop_back_val();
7417
7418for (constUse &U :Poison->uses()) {
7419constInstruction *PoisonUser = cast<Instruction>(U.getUser());
7420if (mustTriggerUB(PoisonUser, KnownPoison) &&
7421 DT.dominates(PoisonUser->getParent(), ExitingBB))
7422returntrue;
7423
7424if (propagatesPoison(U) &&L->contains(PoisonUser))
7425if (KnownPoison.insert(PoisonUser).second)
7426 Worklist.push_back(PoisonUser);
7427 }
7428 }
7429
7430returnfalse;
7431}
7432
7433ScalarEvolution::LoopProperties
7434ScalarEvolution::getLoopProperties(constLoop *L) {
7435usingLoopProperties = ScalarEvolution::LoopProperties;
7436
7437auto Itr = LoopPropertiesCache.find(L);
7438if (Itr == LoopPropertiesCache.end()) {
7439auto HasSideEffects = [](Instruction *I) {
7440if (auto *SI = dyn_cast<StoreInst>(I))
7441return !SI->isSimple();
7442
7443returnI->mayThrow() ||I->mayWriteToMemory();
7444 };
7445
7446 LoopProperties LP = {/* HasNoAbnormalExits */true,
7447/*HasNoSideEffects*/true};
7448
7449for (auto *BB :L->getBlocks())
7450for (auto &I : *BB) {
7451if (!isGuaranteedToTransferExecutionToSuccessor(&I))
7452 LP.HasNoAbnormalExits =false;
7453if (HasSideEffects(&I))
7454 LP.HasNoSideEffects =false;
7455if (!LP.HasNoAbnormalExits && !LP.HasNoSideEffects)
7456break;// We're already as pessimistic as we can get.
7457 }
7458
7459auto InsertPair = LoopPropertiesCache.insert({L, LP});
7460assert(InsertPair.second &&"We just checked!");
7461 Itr = InsertPair.first;
7462 }
7463
7464return Itr->second;
7465}
7466
7467boolScalarEvolution::loopIsFiniteByAssumption(constLoop *L) {
7468// A mustprogress loop without side effects must be finite.
7469// TODO: The check used here is very conservative. It's only *specific*
7470// side effects which are well defined in infinite loops.
7471returnisFinite(L) || (isMustProgress(L) && loopHasNoSideEffects(L));
7472}
7473
7474constSCEV *ScalarEvolution::createSCEVIter(Value *V) {
7475// Worklist item with a Value and a bool indicating whether all operands have
7476// been visited already.
7477usingPointerTy =PointerIntPair<Value *, 1, bool>;
7478SmallVector<PointerTy> Stack;
7479
7480 Stack.emplace_back(V,true);
7481 Stack.emplace_back(V,false);
7482while (!Stack.empty()) {
7483auto E = Stack.pop_back_val();
7484Value *CurV = E.getPointer();
7485
7486if (getExistingSCEV(CurV))
7487continue;
7488
7489SmallVector<Value *> Ops;
7490constSCEV *CreatedSCEV =nullptr;
7491// If all operands have been visited already, create the SCEV.
7492if (E.getInt()) {
7493 CreatedSCEV = createSCEV(CurV);
7494 }else {
7495// Otherwise get the operands we need to create SCEV's for before creating
7496// the SCEV for CurV. If the SCEV for CurV can be constructed trivially,
7497// just use it.
7498 CreatedSCEV = getOperandsToCreate(CurV, Ops);
7499 }
7500
7501if (CreatedSCEV) {
7502 insertValueToMap(CurV, CreatedSCEV);
7503 }else {
7504// Queue CurV for SCEV creation, followed by its's operands which need to
7505// be constructed first.
7506Stack.emplace_back(CurV,true);
7507for (Value *Op : Ops)
7508Stack.emplace_back(Op,false);
7509 }
7510 }
7511
7512returngetExistingSCEV(V);
7513}
7514
7515constSCEV *
7516ScalarEvolution::getOperandsToCreate(Value *V,SmallVectorImpl<Value *> &Ops) {
7517if (!isSCEVable(V->getType()))
7518returngetUnknown(V);
7519
7520if (Instruction *I = dyn_cast<Instruction>(V)) {
7521// Don't attempt to analyze instructions in blocks that aren't
7522// reachable. Such instructions don't matter, and they aren't required
7523// to obey basic rules for definitions dominating uses which this
7524// analysis depends on.
7525if (!DT.isReachableFromEntry(I->getParent()))
7526returngetUnknown(PoisonValue::get(V->getType()));
7527 }elseif (ConstantInt *CI = dyn_cast<ConstantInt>(V))
7528returngetConstant(CI);
7529elseif (isa<GlobalAlias>(V))
7530returngetUnknown(V);
7531elseif (!isa<ConstantExpr>(V))
7532returngetUnknown(V);
7533
7534Operator *U = cast<Operator>(V);
7535if (auto BO =
7536MatchBinaryOp(U,getDataLayout(), AC, DT, dyn_cast<Instruction>(V))) {
7537bool IsConstArg = isa<ConstantInt>(BO->RHS);
7538switch (BO->Opcode) {
7539case Instruction::Add:
7540case Instruction::Mul: {
7541// For additions and multiplications, traverse add/mul chains for which we
7542// can potentially create a single SCEV, to reduce the number of
7543// get{Add,Mul}Expr calls.
7544do {
7545if (BO->Op) {
7546if (BO->Op != V &&getExistingSCEV(BO->Op)) {
7547 Ops.push_back(BO->Op);
7548break;
7549 }
7550 }
7551 Ops.push_back(BO->RHS);
7552auto NewBO =MatchBinaryOp(BO->LHS,getDataLayout(), AC, DT,
7553 dyn_cast<Instruction>(V));
7554if (!NewBO ||
7555 (BO->Opcode == Instruction::Add &&
7556 (NewBO->Opcode != Instruction::Add &&
7557 NewBO->Opcode != Instruction::Sub)) ||
7558 (BO->Opcode == Instruction::Mul &&
7559 NewBO->Opcode != Instruction::Mul)) {
7560 Ops.push_back(BO->LHS);
7561break;
7562 }
7563// CreateSCEV calls getNoWrapFlagsFromUB, which under certain conditions
7564// requires a SCEV for the LHS.
7565if (BO->Op && (BO->IsNSW || BO->IsNUW)) {
7566auto *I = dyn_cast<Instruction>(BO->Op);
7567if (I &&programUndefinedIfPoison(I)) {
7568 Ops.push_back(BO->LHS);
7569break;
7570 }
7571 }
7572 BO = NewBO;
7573 }while (true);
7574returnnullptr;
7575 }
7576case Instruction::Sub:
7577case Instruction::UDiv:
7578case Instruction::URem:
7579break;
7580case Instruction::AShr:
7581case Instruction::Shl:
7582case Instruction::Xor:
7583if (!IsConstArg)
7584returnnullptr;
7585break;
7586case Instruction::And:
7587case Instruction::Or:
7588if (!IsConstArg && !BO->LHS->getType()->isIntegerTy(1))
7589returnnullptr;
7590break;
7591case Instruction::LShr:
7592returngetUnknown(V);
7593default:
7594llvm_unreachable("Unhandled binop");
7595break;
7596 }
7597
7598 Ops.push_back(BO->LHS);
7599 Ops.push_back(BO->RHS);
7600returnnullptr;
7601 }
7602
7603switch (U->getOpcode()) {
7604case Instruction::Trunc:
7605case Instruction::ZExt:
7606case Instruction::SExt:
7607case Instruction::PtrToInt:
7608 Ops.push_back(U->getOperand(0));
7609returnnullptr;
7610
7611case Instruction::BitCast:
7612if (isSCEVable(U->getType()) &&isSCEVable(U->getOperand(0)->getType())) {
7613 Ops.push_back(U->getOperand(0));
7614returnnullptr;
7615 }
7616returngetUnknown(V);
7617
7618case Instruction::SDiv:
7619case Instruction::SRem:
7620 Ops.push_back(U->getOperand(0));
7621 Ops.push_back(U->getOperand(1));
7622returnnullptr;
7623
7624case Instruction::GetElementPtr:
7625assert(cast<GEPOperator>(U)->getSourceElementType()->isSized() &&
7626"GEP source element type must be sized");
7627for (Value *Index :U->operands())
7628 Ops.push_back(Index);
7629returnnullptr;
7630
7631case Instruction::IntToPtr:
7632returngetUnknown(V);
7633
7634case Instruction::PHI:
7635// Keep constructing SCEVs' for phis recursively for now.
7636returnnullptr;
7637
7638case Instruction::Select: {
7639// Check if U is a select that can be simplified to a SCEVUnknown.
7640auto CanSimplifyToUnknown = [this,U]() {
7641if (U->getType()->isIntegerTy(1) || isa<ConstantInt>(U->getOperand(0)))
7642returnfalse;
7643
7644auto *ICI = dyn_cast<ICmpInst>(U->getOperand(0));
7645if (!ICI)
7646returnfalse;
7647Value *LHS = ICI->getOperand(0);
7648Value *RHS = ICI->getOperand(1);
7649if (ICI->getPredicate() ==CmpInst::ICMP_EQ ||
7650 ICI->getPredicate() ==CmpInst::ICMP_NE) {
7651if (!(isa<ConstantInt>(RHS) && cast<ConstantInt>(RHS)->isZero()))
7652returntrue;
7653 }elseif (getTypeSizeInBits(LHS->getType()) >
7654getTypeSizeInBits(U->getType()))
7655returntrue;
7656returnfalse;
7657 };
7658if (CanSimplifyToUnknown())
7659returngetUnknown(U);
7660
7661for (Value *Inc :U->operands())
7662 Ops.push_back(Inc);
7663returnnullptr;
7664break;
7665 }
7666case Instruction::Call:
7667case Instruction::Invoke:
7668if (Value *RV = cast<CallBase>(U)->getReturnedArgOperand()) {
7669 Ops.push_back(RV);
7670returnnullptr;
7671 }
7672
7673if (auto *II = dyn_cast<IntrinsicInst>(U)) {
7674switch (II->getIntrinsicID()) {
7675case Intrinsic::abs:
7676 Ops.push_back(II->getArgOperand(0));
7677returnnullptr;
7678case Intrinsic::umax:
7679case Intrinsic::umin:
7680case Intrinsic::smax:
7681case Intrinsic::smin:
7682case Intrinsic::usub_sat:
7683case Intrinsic::uadd_sat:
7684 Ops.push_back(II->getArgOperand(0));
7685 Ops.push_back(II->getArgOperand(1));
7686returnnullptr;
7687case Intrinsic::start_loop_iterations:
7688case Intrinsic::annotation:
7689case Intrinsic::ptr_annotation:
7690 Ops.push_back(II->getArgOperand(0));
7691returnnullptr;
7692default:
7693break;
7694 }
7695 }
7696break;
7697 }
7698
7699returnnullptr;
7700}
7701
7702constSCEV *ScalarEvolution::createSCEV(Value *V) {
7703if (!isSCEVable(V->getType()))
7704returngetUnknown(V);
7705
7706if (Instruction *I = dyn_cast<Instruction>(V)) {
7707// Don't attempt to analyze instructions in blocks that aren't
7708// reachable. Such instructions don't matter, and they aren't required
7709// to obey basic rules for definitions dominating uses which this
7710// analysis depends on.
7711if (!DT.isReachableFromEntry(I->getParent()))
7712returngetUnknown(PoisonValue::get(V->getType()));
7713 }elseif (ConstantInt *CI = dyn_cast<ConstantInt>(V))
7714returngetConstant(CI);
7715elseif (isa<GlobalAlias>(V))
7716returngetUnknown(V);
7717elseif (!isa<ConstantExpr>(V))
7718returngetUnknown(V);
7719
7720constSCEV *LHS;
7721constSCEV *RHS;
7722
7723Operator *U = cast<Operator>(V);
7724if (auto BO =
7725MatchBinaryOp(U,getDataLayout(), AC, DT, dyn_cast<Instruction>(V))) {
7726switch (BO->Opcode) {
7727case Instruction::Add: {
7728// The simple thing to do would be to just call getSCEV on both operands
7729// and call getAddExpr with the result. However if we're looking at a
7730// bunch of things all added together, this can be quite inefficient,
7731// because it leads to N-1 getAddExpr calls for N ultimate operands.
7732// Instead, gather up all the operands and make a single getAddExpr call.
7733// LLVM IR canonical form means we need only traverse the left operands.
7734SmallVector<const SCEV *, 4> AddOps;
7735do {
7736if (BO->Op) {
7737if (auto *OpSCEV =getExistingSCEV(BO->Op)) {
7738 AddOps.push_back(OpSCEV);
7739break;
7740 }
7741
7742// If a NUW or NSW flag can be applied to the SCEV for this
7743// addition, then compute the SCEV for this addition by itself
7744// with a separate call to getAddExpr. We need to do that
7745// instead of pushing the operands of the addition onto AddOps,
7746// since the flags are only known to apply to this particular
7747// addition - they may not apply to other additions that can be
7748// formed with operands from AddOps.
7749constSCEV *RHS =getSCEV(BO->RHS);
7750SCEV::NoWrapFlagsFlags = getNoWrapFlagsFromUB(BO->Op);
7751if (Flags !=SCEV::FlagAnyWrap) {
7752constSCEV *LHS =getSCEV(BO->LHS);
7753if (BO->Opcode == Instruction::Sub)
7754 AddOps.push_back(getMinusSCEV(LHS, RHS, Flags));
7755else
7756 AddOps.push_back(getAddExpr(LHS, RHS, Flags));
7757break;
7758 }
7759 }
7760
7761if (BO->Opcode == Instruction::Sub)
7762 AddOps.push_back(getNegativeSCEV(getSCEV(BO->RHS)));
7763else
7764 AddOps.push_back(getSCEV(BO->RHS));
7765
7766auto NewBO =MatchBinaryOp(BO->LHS,getDataLayout(), AC, DT,
7767 dyn_cast<Instruction>(V));
7768if (!NewBO || (NewBO->Opcode != Instruction::Add &&
7769 NewBO->Opcode != Instruction::Sub)) {
7770 AddOps.push_back(getSCEV(BO->LHS));
7771break;
7772 }
7773 BO = NewBO;
7774 }while (true);
7775
7776returngetAddExpr(AddOps);
7777 }
7778
7779case Instruction::Mul: {
7780SmallVector<const SCEV *, 4> MulOps;
7781do {
7782if (BO->Op) {
7783if (auto *OpSCEV =getExistingSCEV(BO->Op)) {
7784 MulOps.push_back(OpSCEV);
7785break;
7786 }
7787
7788SCEV::NoWrapFlagsFlags = getNoWrapFlagsFromUB(BO->Op);
7789if (Flags !=SCEV::FlagAnyWrap) {
7790LHS =getSCEV(BO->LHS);
7791RHS =getSCEV(BO->RHS);
7792 MulOps.push_back(getMulExpr(LHS, RHS, Flags));
7793break;
7794 }
7795 }
7796
7797 MulOps.push_back(getSCEV(BO->RHS));
7798auto NewBO =MatchBinaryOp(BO->LHS,getDataLayout(), AC, DT,
7799 dyn_cast<Instruction>(V));
7800if (!NewBO || NewBO->Opcode != Instruction::Mul) {
7801 MulOps.push_back(getSCEV(BO->LHS));
7802break;
7803 }
7804 BO = NewBO;
7805 }while (true);
7806
7807returngetMulExpr(MulOps);
7808 }
7809case Instruction::UDiv:
7810LHS =getSCEV(BO->LHS);
7811RHS =getSCEV(BO->RHS);
7812returngetUDivExpr(LHS, RHS);
7813case Instruction::URem:
7814LHS =getSCEV(BO->LHS);
7815RHS =getSCEV(BO->RHS);
7816returngetURemExpr(LHS, RHS);
7817case Instruction::Sub: {
7818SCEV::NoWrapFlagsFlags =SCEV::FlagAnyWrap;
7819if (BO->Op)
7820Flags = getNoWrapFlagsFromUB(BO->Op);
7821LHS =getSCEV(BO->LHS);
7822RHS =getSCEV(BO->RHS);
7823returngetMinusSCEV(LHS, RHS, Flags);
7824 }
7825case Instruction::And:
7826// For an expression like x&255 that merely masks off the high bits,
7827// use zext(trunc(x)) as the SCEV expression.
7828if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
7829if (CI->isZero())
7830returngetSCEV(BO->RHS);
7831if (CI->isMinusOne())
7832returngetSCEV(BO->LHS);
7833constAPInt &A = CI->getValue();
7834
7835// Instcombine's ShrinkDemandedConstant may strip bits out of
7836// constants, obscuring what would otherwise be a low-bits mask.
7837// Use computeKnownBits to compute what ShrinkDemandedConstant
7838// knew about to reconstruct a low-bits mask value.
7839unsigned LZ =A.countl_zero();
7840unsigned TZ =A.countr_zero();
7841unsignedBitWidth =A.getBitWidth();
7842KnownBits Known(BitWidth);
7843computeKnownBits(BO->LHS, Known,getDataLayout(),
7844 0, &AC,nullptr, &DT);
7845
7846APInt EffectiveMask =
7847APInt::getLowBitsSet(BitWidth,BitWidth - LZ - TZ).shl(TZ);
7848if ((LZ != 0 || TZ != 0) && !((~A & ~Known.Zero) & EffectiveMask)) {
7849constSCEV *MulCount =getConstant(APInt::getOneBitSet(BitWidth, TZ));
7850constSCEV *LHS =getSCEV(BO->LHS);
7851constSCEV *ShiftedLHS =nullptr;
7852if (auto *LHSMul = dyn_cast<SCEVMulExpr>(LHS)) {
7853if (auto *OpC = dyn_cast<SCEVConstant>(LHSMul->getOperand(0))) {
7854// For an expression like (x * 8) & 8, simplify the multiply.
7855unsigned MulZeros = OpC->getAPInt().countr_zero();
7856unsigned GCD = std::min(MulZeros, TZ);
7857APInt DivAmt =APInt::getOneBitSet(BitWidth, TZ - GCD);
7858SmallVector<const SCEV*, 4> MulOps;
7859 MulOps.push_back(getConstant(OpC->getAPInt().lshr(GCD)));
7860append_range(MulOps, LHSMul->operands().drop_front());
7861auto *NewMul =getMulExpr(MulOps, LHSMul->getNoWrapFlags());
7862 ShiftedLHS =getUDivExpr(NewMul,getConstant(DivAmt));
7863 }
7864 }
7865if (!ShiftedLHS)
7866 ShiftedLHS =getUDivExpr(LHS, MulCount);
7867returngetMulExpr(
7868getZeroExtendExpr(
7869getTruncateExpr(ShiftedLHS,
7870IntegerType::get(getContext(),BitWidth - LZ - TZ)),
7871 BO->LHS->getType()),
7872 MulCount);
7873 }
7874 }
7875// Binary `and` is a bit-wise `umin`.
7876if (BO->LHS->getType()->isIntegerTy(1)) {
7877LHS =getSCEV(BO->LHS);
7878RHS =getSCEV(BO->RHS);
7879returngetUMinExpr(LHS, RHS);
7880 }
7881break;
7882
7883case Instruction::Or:
7884// Binary `or` is a bit-wise `umax`.
7885if (BO->LHS->getType()->isIntegerTy(1)) {
7886LHS =getSCEV(BO->LHS);
7887RHS =getSCEV(BO->RHS);
7888returngetUMaxExpr(LHS, RHS);
7889 }
7890break;
7891
7892case Instruction::Xor:
7893if (ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS)) {
7894// If the RHS of xor is -1, then this is a not operation.
7895if (CI->isMinusOne())
7896returngetNotSCEV(getSCEV(BO->LHS));
7897
7898// Model xor(and(x, C), C) as and(~x, C), if C is a low-bits mask.
7899// This is a variant of the check for xor with -1, and it handles
7900// the case where instcombine has trimmed non-demanded bits out
7901// of an xor with -1.
7902if (auto *LBO = dyn_cast<BinaryOperator>(BO->LHS))
7903if (ConstantInt *LCI = dyn_cast<ConstantInt>(LBO->getOperand(1)))
7904if (LBO->getOpcode() == Instruction::And &&
7905 LCI->getValue() == CI->getValue())
7906if (constSCEVZeroExtendExpr *Z =
7907 dyn_cast<SCEVZeroExtendExpr>(getSCEV(BO->LHS))) {
7908Type *UTy = BO->LHS->getType();
7909constSCEV *Z0 =Z->getOperand();
7910Type *Z0Ty = Z0->getType();
7911unsigned Z0TySize =getTypeSizeInBits(Z0Ty);
7912
7913// If C is a low-bits mask, the zero extend is serving to
7914// mask off the high bits. Complement the operand and
7915// re-apply the zext.
7916if (CI->getValue().isMask(Z0TySize))
7917returngetZeroExtendExpr(getNotSCEV(Z0), UTy);
7918
7919// If C is a single bit, it may be in the sign-bit position
7920// before the zero-extend. In this case, represent the xor
7921// using an add, which is equivalent, and re-apply the zext.
7922APInt Trunc = CI->getValue().trunc(Z0TySize);
7923if (Trunc.zext(getTypeSizeInBits(UTy)) == CI->getValue() &&
7924 Trunc.isSignMask())
7925returngetZeroExtendExpr(getAddExpr(Z0,getConstant(Trunc)),
7926 UTy);
7927 }
7928 }
7929break;
7930
7931case Instruction::Shl:
7932// Turn shift left of a constant amount into a multiply.
7933if (ConstantInt *SA = dyn_cast<ConstantInt>(BO->RHS)) {
7934uint32_tBitWidth = cast<IntegerType>(SA->getType())->getBitWidth();
7935
7936// If the shift count is not less than the bitwidth, the result of
7937// the shift is undefined. Don't try to analyze it, because the
7938// resolution chosen here may differ from the resolution chosen in
7939// other parts of the compiler.
7940if (SA->getValue().uge(BitWidth))
7941break;
7942
7943// We can safely preserve the nuw flag in all cases. It's also safe to
7944// turn a nuw nsw shl into a nuw nsw mul. However, nsw in isolation
7945// requires special handling. It can be preserved as long as we're not
7946// left shifting by bitwidth - 1.
7947autoFlags =SCEV::FlagAnyWrap;
7948if (BO->Op) {
7949auto MulFlags = getNoWrapFlagsFromUB(BO->Op);
7950if ((MulFlags &SCEV::FlagNSW) &&
7951 ((MulFlags &SCEV::FlagNUW) || SA->getValue().ult(BitWidth - 1)))
7952Flags = (SCEV::NoWrapFlags)(Flags |SCEV::FlagNSW);
7953if (MulFlags &SCEV::FlagNUW)
7954Flags = (SCEV::NoWrapFlags)(Flags |SCEV::FlagNUW);
7955 }
7956
7957ConstantInt *X = ConstantInt::get(
7958getContext(),APInt::getOneBitSet(BitWidth, SA->getZExtValue()));
7959returngetMulExpr(getSCEV(BO->LHS),getConstant(X), Flags);
7960 }
7961break;
7962
7963case Instruction::AShr:
7964// AShr X, C, where C is a constant.
7965ConstantInt *CI = dyn_cast<ConstantInt>(BO->RHS);
7966if (!CI)
7967break;
7968
7969Type *OuterTy = BO->LHS->getType();
7970uint64_tBitWidth =getTypeSizeInBits(OuterTy);
7971// If the shift count is not less than the bitwidth, the result of
7972// the shift is undefined. Don't try to analyze it, because the
7973// resolution chosen here may differ from the resolution chosen in
7974// other parts of the compiler.
7975if (CI->getValue().uge(BitWidth))
7976break;
7977
7978if (CI->isZero())
7979returngetSCEV(BO->LHS);// shift by zero --> noop
7980
7981uint64_t AShrAmt = CI->getZExtValue();
7982Type *TruncTy =IntegerType::get(getContext(),BitWidth - AShrAmt);
7983
7984Operator *L = dyn_cast<Operator>(BO->LHS);
7985constSCEV *AddTruncateExpr =nullptr;
7986ConstantInt *ShlAmtCI =nullptr;
7987constSCEV *AddConstant =nullptr;
7988
7989if (L &&L->getOpcode() == Instruction::Add) {
7990// X = Shl A, n
7991// Y = Add X, c
7992// Z = AShr Y, m
7993// n, c and m are constants.
7994
7995Operator *LShift = dyn_cast<Operator>(L->getOperand(0));
7996ConstantInt *AddOperandCI = dyn_cast<ConstantInt>(L->getOperand(1));
7997if (LShift && LShift->getOpcode() == Instruction::Shl) {
7998if (AddOperandCI) {
7999constSCEV *ShlOp0SCEV =getSCEV(LShift->getOperand(0));
8000 ShlAmtCI = dyn_cast<ConstantInt>(LShift->getOperand(1));
8001// since we truncate to TruncTy, the AddConstant should be of the
8002// same type, so create a new Constant with type same as TruncTy.
8003// Also, the Add constant should be shifted right by AShr amount.
8004APInt AddOperand = AddOperandCI->getValue().ashr(AShrAmt);
8005 AddConstant =getConstant(AddOperand.trunc(BitWidth - AShrAmt));
8006// we model the expression as sext(add(trunc(A), c << n)), since the
8007// sext(trunc) part is already handled below, we create a
8008// AddExpr(TruncExp) which will be used later.
8009 AddTruncateExpr =getTruncateExpr(ShlOp0SCEV, TruncTy);
8010 }
8011 }
8012 }elseif (L &&L->getOpcode() == Instruction::Shl) {
8013// X = Shl A, n
8014// Y = AShr X, m
8015// Both n and m are constant.
8016
8017constSCEV *ShlOp0SCEV =getSCEV(L->getOperand(0));
8018 ShlAmtCI = dyn_cast<ConstantInt>(L->getOperand(1));
8019 AddTruncateExpr =getTruncateExpr(ShlOp0SCEV, TruncTy);
8020 }
8021
8022if (AddTruncateExpr && ShlAmtCI) {
8023// We can merge the two given cases into a single SCEV statement,
8024// incase n = m, the mul expression will be 2^0, so it gets resolved to
8025// a simpler case. The following code handles the two cases:
8026//
8027// 1) For a two-shift sext-inreg, i.e. n = m,
8028// use sext(trunc(x)) as the SCEV expression.
8029//
8030// 2) When n > m, use sext(mul(trunc(x), 2^(n-m)))) as the SCEV
8031// expression. We already checked that ShlAmt < BitWidth, so
8032// the multiplier, 1 << (ShlAmt - AShrAmt), fits into TruncTy as
8033// ShlAmt - AShrAmt < Amt.
8034constAPInt &ShlAmt = ShlAmtCI->getValue();
8035if (ShlAmt.ult(BitWidth) && ShlAmt.uge(AShrAmt)) {
8036APIntMul =APInt::getOneBitSet(BitWidth - AShrAmt,
8037 ShlAmtCI->getZExtValue() - AShrAmt);
8038constSCEV *CompositeExpr =
8039getMulExpr(AddTruncateExpr,getConstant(Mul));
8040if (L->getOpcode() != Instruction::Shl)
8041 CompositeExpr =getAddExpr(CompositeExpr, AddConstant);
8042
8043returngetSignExtendExpr(CompositeExpr, OuterTy);
8044 }
8045 }
8046break;
8047 }
8048 }
8049
8050switch (U->getOpcode()) {
8051case Instruction::Trunc:
8052returngetTruncateExpr(getSCEV(U->getOperand(0)),U->getType());
8053
8054case Instruction::ZExt:
8055returngetZeroExtendExpr(getSCEV(U->getOperand(0)),U->getType());
8056
8057case Instruction::SExt:
8058if (auto BO =MatchBinaryOp(U->getOperand(0),getDataLayout(), AC, DT,
8059 dyn_cast<Instruction>(V))) {
8060// The NSW flag of a subtract does not always survive the conversion to
8061// A + (-1)*B. By pushing sign extension onto its operands we are much
8062// more likely to preserve NSW and allow later AddRec optimisations.
8063//
8064// NOTE: This is effectively duplicating this logic from getSignExtend:
8065// sext((A + B + ...)<nsw>) --> (sext(A) + sext(B) + ...)<nsw>
8066// but by that point the NSW information has potentially been lost.
8067if (BO->Opcode == Instruction::Sub && BO->IsNSW) {
8068Type *Ty =U->getType();
8069auto *V1 =getSignExtendExpr(getSCEV(BO->LHS), Ty);
8070auto *V2 =getSignExtendExpr(getSCEV(BO->RHS), Ty);
8071returngetMinusSCEV(V1, V2,SCEV::FlagNSW);
8072 }
8073 }
8074returngetSignExtendExpr(getSCEV(U->getOperand(0)),U->getType());
8075
8076case Instruction::BitCast:
8077// BitCasts are no-op casts so we just eliminate the cast.
8078if (isSCEVable(U->getType()) &&isSCEVable(U->getOperand(0)->getType()))
8079returngetSCEV(U->getOperand(0));
8080break;
8081
8082case Instruction::PtrToInt: {
8083// Pointer to integer cast is straight-forward, so do model it.
8084constSCEV *Op =getSCEV(U->getOperand(0));
8085Type *DstIntTy =U->getType();
8086// But only if effective SCEV (integer) type is wide enough to represent
8087// all possible pointer values.
8088constSCEV *IntOp =getPtrToIntExpr(Op, DstIntTy);
8089if (isa<SCEVCouldNotCompute>(IntOp))
8090returngetUnknown(V);
8091return IntOp;
8092 }
8093case Instruction::IntToPtr:
8094// Just don't deal with inttoptr casts.
8095returngetUnknown(V);
8096
8097case Instruction::SDiv:
8098// If both operands are non-negative, this is just an udiv.
8099if (isKnownNonNegative(getSCEV(U->getOperand(0))) &&
8100isKnownNonNegative(getSCEV(U->getOperand(1))))
8101returngetUDivExpr(getSCEV(U->getOperand(0)),getSCEV(U->getOperand(1)));
8102break;
8103
8104case Instruction::SRem:
8105// If both operands are non-negative, this is just an urem.
8106if (isKnownNonNegative(getSCEV(U->getOperand(0))) &&
8107isKnownNonNegative(getSCEV(U->getOperand(1))))
8108returngetURemExpr(getSCEV(U->getOperand(0)),getSCEV(U->getOperand(1)));
8109break;
8110
8111case Instruction::GetElementPtr:
8112return createNodeForGEP(cast<GEPOperator>(U));
8113
8114case Instruction::PHI:
8115return createNodeForPHI(cast<PHINode>(U));
8116
8117case Instruction::Select:
8118return createNodeForSelectOrPHI(U,U->getOperand(0),U->getOperand(1),
8119U->getOperand(2));
8120
8121case Instruction::Call:
8122case Instruction::Invoke:
8123if (Value *RV = cast<CallBase>(U)->getReturnedArgOperand())
8124returngetSCEV(RV);
8125
8126if (auto *II = dyn_cast<IntrinsicInst>(U)) {
8127switch (II->getIntrinsicID()) {
8128case Intrinsic::abs:
8129returngetAbsExpr(
8130getSCEV(II->getArgOperand(0)),
8131/*IsNSW=*/cast<ConstantInt>(II->getArgOperand(1))->isOne());
8132case Intrinsic::umax:
8133LHS =getSCEV(II->getArgOperand(0));
8134RHS =getSCEV(II->getArgOperand(1));
8135returngetUMaxExpr(LHS, RHS);
8136case Intrinsic::umin:
8137LHS =getSCEV(II->getArgOperand(0));
8138RHS =getSCEV(II->getArgOperand(1));
8139returngetUMinExpr(LHS, RHS);
8140case Intrinsic::smax:
8141LHS =getSCEV(II->getArgOperand(0));
8142RHS =getSCEV(II->getArgOperand(1));
8143returngetSMaxExpr(LHS, RHS);
8144case Intrinsic::smin:
8145LHS =getSCEV(II->getArgOperand(0));
8146RHS =getSCEV(II->getArgOperand(1));
8147returngetSMinExpr(LHS, RHS);
8148case Intrinsic::usub_sat: {
8149constSCEV *X =getSCEV(II->getArgOperand(0));
8150constSCEV *Y =getSCEV(II->getArgOperand(1));
8151constSCEV *ClampedY =getUMinExpr(X,Y);
8152returngetMinusSCEV(X, ClampedY,SCEV::FlagNUW);
8153 }
8154case Intrinsic::uadd_sat: {
8155constSCEV *X =getSCEV(II->getArgOperand(0));
8156constSCEV *Y =getSCEV(II->getArgOperand(1));
8157constSCEV *ClampedX =getUMinExpr(X,getNotSCEV(Y));
8158returngetAddExpr(ClampedX,Y,SCEV::FlagNUW);
8159 }
8160case Intrinsic::start_loop_iterations:
8161case Intrinsic::annotation:
8162case Intrinsic::ptr_annotation:
8163// A start_loop_iterations or llvm.annotation or llvm.prt.annotation is
8164// just eqivalent to the first operand for SCEV purposes.
8165returngetSCEV(II->getArgOperand(0));
8166case Intrinsic::vscale:
8167returngetVScale(II->getType());
8168default:
8169break;
8170 }
8171 }
8172break;
8173 }
8174
8175returngetUnknown(V);
8176}
8177
8178//===----------------------------------------------------------------------===//
8179// Iteration Count Computation Code
8180//
8181
8182constSCEV *ScalarEvolution::getTripCountFromExitCount(constSCEV *ExitCount) {
8183if (isa<SCEVCouldNotCompute>(ExitCount))
8184returngetCouldNotCompute();
8185
8186auto *ExitCountType = ExitCount->getType();
8187assert(ExitCountType->isIntegerTy());
8188auto *EvalTy =Type::getIntNTy(ExitCountType->getContext(),
8189 1 + ExitCountType->getScalarSizeInBits());
8190returngetTripCountFromExitCount(ExitCount, EvalTy,nullptr);
8191}
8192
8193constSCEV *ScalarEvolution::getTripCountFromExitCount(constSCEV *ExitCount,
8194Type *EvalTy,
8195constLoop *L) {
8196if (isa<SCEVCouldNotCompute>(ExitCount))
8197returngetCouldNotCompute();
8198
8199unsigned ExitCountSize =getTypeSizeInBits(ExitCount->getType());
8200unsigned EvalSize = EvalTy->getPrimitiveSizeInBits();
8201
8202auto CanAddOneWithoutOverflow = [&]() {
8203ConstantRange ExitCountRange =
8204 getRangeRef(ExitCount, RangeSignHint::HINT_RANGE_UNSIGNED);
8205if (!ExitCountRange.contains(APInt::getMaxValue(ExitCountSize)))
8206returntrue;
8207
8208return L &&isLoopEntryGuardedByCond(L,ICmpInst::ICMP_NE, ExitCount,
8209getMinusOne(ExitCount->getType()));
8210 };
8211
8212// If we need to zero extend the backedge count, check if we can add one to
8213// it prior to zero extending without overflow. Provided this is safe, it
8214// allows better simplification of the +1.
8215if (EvalSize > ExitCountSize && CanAddOneWithoutOverflow())
8216returngetZeroExtendExpr(
8217getAddExpr(ExitCount,getOne(ExitCount->getType())), EvalTy);
8218
8219// Get the total trip count from the count by adding 1. This may wrap.
8220returngetAddExpr(getTruncateOrZeroExtend(ExitCount, EvalTy),getOne(EvalTy));
8221}
8222
8223staticunsignedgetConstantTripCount(constSCEVConstant *ExitCount) {
8224if (!ExitCount)
8225return 0;
8226
8227ConstantInt *ExitConst = ExitCount->getValue();
8228
8229// Guard against huge trip counts.
8230if (ExitConst->getValue().getActiveBits() > 32)
8231return 0;
8232
8233// In case of integer overflow, this returns 0, which is correct.
8234return ((unsigned)ExitConst->getZExtValue()) + 1;
8235}
8236
8237unsignedScalarEvolution::getSmallConstantTripCount(constLoop *L) {
8238auto *ExitCount = dyn_cast<SCEVConstant>(getBackedgeTakenCount(L,Exact));
8239returngetConstantTripCount(ExitCount);
8240}
8241
8242unsigned
8243ScalarEvolution::getSmallConstantTripCount(constLoop *L,
8244constBasicBlock *ExitingBlock) {
8245assert(ExitingBlock &&"Must pass a non-null exiting block!");
8246assert(L->isLoopExiting(ExitingBlock) &&
8247"Exiting block must actually branch out of the loop!");
8248constSCEVConstant *ExitCount =
8249 dyn_cast<SCEVConstant>(getExitCount(L, ExitingBlock));
8250returngetConstantTripCount(ExitCount);
8251}
8252
8253unsignedScalarEvolution::getSmallConstantMaxTripCount(
8254constLoop *L,SmallVectorImpl<const SCEVPredicate *> *Predicates) {
8255
8256constauto *MaxExitCount =
8257 Predicates ?getPredicatedConstantMaxBackedgeTakenCount(L, *Predicates)
8258 :getConstantMaxBackedgeTakenCount(L);
8259returngetConstantTripCount(dyn_cast<SCEVConstant>(MaxExitCount));
8260}
8261
8262unsignedScalarEvolution::getSmallConstantTripMultiple(constLoop *L) {
8263SmallVector<BasicBlock *, 8> ExitingBlocks;
8264 L->getExitingBlocks(ExitingBlocks);
8265
8266 std::optional<unsigned> Res;
8267for (auto *ExitingBB : ExitingBlocks) {
8268unsigned Multiple =getSmallConstantTripMultiple(L, ExitingBB);
8269if (!Res)
8270 Res = Multiple;
8271 Res = (unsigned)std::gcd(*Res, Multiple);
8272 }
8273return Res.value_or(1);
8274}
8275
8276unsignedScalarEvolution::getSmallConstantTripMultiple(constLoop *L,
8277constSCEV *ExitCount) {
8278if (ExitCount ==getCouldNotCompute())
8279return 1;
8280
8281// Get the trip count
8282constSCEV *TCExpr =getTripCountFromExitCount(applyLoopGuards(ExitCount, L));
8283
8284APInt Multiple =getNonZeroConstantMultiple(TCExpr);
8285// If a trip multiple is huge (>=2^32), the trip count is still divisible by
8286// the greatest power of 2 divisor less than 2^32.
8287return Multiple.getActiveBits() > 32
8288 ? 1U << std::min((unsigned)31, Multiple.countTrailingZeros())
8289 : (unsigned)Multiple.zextOrTrunc(32).getZExtValue();
8290}
8291
8292/// Returns the largest constant divisor of the trip count of this loop as a
8293/// normal unsigned value, if possible. This means that the actual trip count is
8294/// always a multiple of the returned value (don't forget the trip count could
8295/// very well be zero as well!).
8296///
8297/// Returns 1 if the trip count is unknown or not guaranteed to be the
8298/// multiple of a constant (which is also the case if the trip count is simply
8299/// constant, use getSmallConstantTripCount for that case), Will also return 1
8300/// if the trip count is very large (>= 2^32).
8301///
8302/// As explained in the comments for getSmallConstantTripCount, this assumes
8303/// that control exits the loop via ExitingBlock.
8304unsigned
8305ScalarEvolution::getSmallConstantTripMultiple(constLoop *L,
8306constBasicBlock *ExitingBlock) {
8307assert(ExitingBlock &&"Must pass a non-null exiting block!");
8308assert(L->isLoopExiting(ExitingBlock) &&
8309"Exiting block must actually branch out of the loop!");
8310constSCEV *ExitCount =getExitCount(L, ExitingBlock);
8311returngetSmallConstantTripMultiple(L, ExitCount);
8312}
8313
8314constSCEV *ScalarEvolution::getExitCount(constLoop *L,
8315constBasicBlock *ExitingBlock,
8316ExitCountKind Kind) {
8317switch (Kind) {
8318caseExact:
8319return getBackedgeTakenInfo(L).getExact(ExitingBlock,this);
8320caseSymbolicMaximum:
8321return getBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock,this);
8322caseConstantMaximum:
8323return getBackedgeTakenInfo(L).getConstantMax(ExitingBlock,this);
8324 };
8325llvm_unreachable("Invalid ExitCountKind!");
8326}
8327
8328constSCEV *ScalarEvolution::getPredicatedExitCount(
8329constLoop *L,constBasicBlock *ExitingBlock,
8330SmallVectorImpl<const SCEVPredicate *> *Predicates,ExitCountKind Kind) {
8331switch (Kind) {
8332caseExact:
8333return getPredicatedBackedgeTakenInfo(L).getExact(ExitingBlock,this,
8334 Predicates);
8335caseSymbolicMaximum:
8336return getPredicatedBackedgeTakenInfo(L).getSymbolicMax(ExitingBlock,this,
8337 Predicates);
8338caseConstantMaximum:
8339return getPredicatedBackedgeTakenInfo(L).getConstantMax(ExitingBlock,this,
8340 Predicates);
8341 };
8342llvm_unreachable("Invalid ExitCountKind!");
8343}
8344
8345constSCEV *ScalarEvolution::getPredicatedBackedgeTakenCount(
8346constLoop *L,SmallVectorImpl<const SCEVPredicate *> &Preds) {
8347return getPredicatedBackedgeTakenInfo(L).getExact(L,this, &Preds);
8348}
8349
8350constSCEV *ScalarEvolution::getBackedgeTakenCount(constLoop *L,
8351ExitCountKind Kind) {
8352switch (Kind) {
8353caseExact:
8354return getBackedgeTakenInfo(L).getExact(L,this);
8355caseConstantMaximum:
8356return getBackedgeTakenInfo(L).getConstantMax(this);
8357caseSymbolicMaximum:
8358return getBackedgeTakenInfo(L).getSymbolicMax(L,this);
8359 };
8360llvm_unreachable("Invalid ExitCountKind!");
8361}
8362
8363constSCEV *ScalarEvolution::getPredicatedSymbolicMaxBackedgeTakenCount(
8364constLoop *L,SmallVectorImpl<const SCEVPredicate *> &Preds) {
8365return getPredicatedBackedgeTakenInfo(L).getSymbolicMax(L,this, &Preds);
8366}
8367
8368constSCEV *ScalarEvolution::getPredicatedConstantMaxBackedgeTakenCount(
8369constLoop *L,SmallVectorImpl<const SCEVPredicate *> &Preds) {
8370return getPredicatedBackedgeTakenInfo(L).getConstantMax(this, &Preds);
8371}
8372
8373boolScalarEvolution::isBackedgeTakenCountMaxOrZero(constLoop *L) {
8374return getBackedgeTakenInfo(L).isConstantMaxOrZero(this);
8375}
8376
8377/// Push PHI nodes in the header of the given loop onto the given Worklist.
8378staticvoidPushLoopPHIs(constLoop *L,
8379SmallVectorImpl<Instruction *> &Worklist,
8380SmallPtrSetImpl<Instruction *> &Visited) {
8381BasicBlock *Header = L->getHeader();
8382
8383// Push all Loop-header PHIs onto the Worklist stack.
8384for (PHINode &PN : Header->phis())
8385if (Visited.insert(&PN).second)
8386 Worklist.push_back(&PN);
8387}
8388
8389ScalarEvolution::BackedgeTakenInfo &
8390ScalarEvolution::getPredicatedBackedgeTakenInfo(constLoop *L) {
8391auto &BTI = getBackedgeTakenInfo(L);
8392if (BTI.hasFullInfo())
8393return BTI;
8394
8395auto Pair = PredicatedBackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
8396
8397if (!Pair.second)
8398return Pair.first->second;
8399
8400 BackedgeTakenInfoResult =
8401 computeBackedgeTakenCount(L,/*AllowPredicates=*/true);
8402
8403return PredicatedBackedgeTakenCounts.find(L)->second = std::move(Result);
8404}
8405
8406ScalarEvolution::BackedgeTakenInfo &
8407ScalarEvolution::getBackedgeTakenInfo(constLoop *L) {
8408// Initially insert an invalid entry for this loop. If the insertion
8409// succeeds, proceed to actually compute a backedge-taken count and
8410// update the value. The temporary CouldNotCompute value tells SCEV
8411// code elsewhere that it shouldn't attempt to request a new
8412// backedge-taken count, which could result in infinite recursion.
8413 std::pair<DenseMap<const Loop *, BackedgeTakenInfo>::iterator,bool> Pair =
8414 BackedgeTakenCounts.insert({L, BackedgeTakenInfo()});
8415if (!Pair.second)
8416return Pair.first->second;
8417
8418// computeBackedgeTakenCount may allocate memory for its result. Inserting it
8419// into the BackedgeTakenCounts map transfers ownership. Otherwise, the result
8420// must be cleared in this scope.
8421 BackedgeTakenInfoResult = computeBackedgeTakenCount(L);
8422
8423// Now that we know more about the trip count for this loop, forget any
8424// existing SCEV values for PHI nodes in this loop since they are only
8425// conservative estimates made without the benefit of trip count
8426// information. This invalidation is not necessary for correctness, and is
8427// only done to produce more precise results.
8428if (Result.hasAnyInfo()) {
8429// Invalidate any expression using an addrec in this loop.
8430SmallVector<const SCEV *, 8> ToForget;
8431auto LoopUsersIt = LoopUsers.find(L);
8432if (LoopUsersIt != LoopUsers.end())
8433append_range(ToForget, LoopUsersIt->second);
8434 forgetMemoizedResults(ToForget);
8435
8436// Invalidate constant-evolved loop header phis.
8437for (PHINode &PN :L->getHeader()->phis())
8438 ConstantEvolutionLoopExitValue.erase(&PN);
8439 }
8440
8441// Re-lookup the insert position, since the call to
8442// computeBackedgeTakenCount above could result in a
8443// recusive call to getBackedgeTakenInfo (on a different
8444// loop), which would invalidate the iterator computed
8445// earlier.
8446return BackedgeTakenCounts.find(L)->second = std::move(Result);
8447}
8448
8449voidScalarEvolution::forgetAllLoops() {
8450// This method is intended to forget all info about loops. It should
8451// invalidate caches as if the following happened:
8452// - The trip counts of all loops have changed arbitrarily
8453// - Every llvm::Value has been updated in place to produce a different
8454// result.
8455 BackedgeTakenCounts.clear();
8456 PredicatedBackedgeTakenCounts.clear();
8457 BECountUsers.clear();
8458 LoopPropertiesCache.clear();
8459 ConstantEvolutionLoopExitValue.clear();
8460 ValueExprMap.clear();
8461 ValuesAtScopes.clear();
8462 ValuesAtScopesUsers.clear();
8463 LoopDispositions.clear();
8464 BlockDispositions.clear();
8465 UnsignedRanges.clear();
8466 SignedRanges.clear();
8467 ExprValueMap.clear();
8468 HasRecMap.clear();
8469 ConstantMultipleCache.clear();
8470 PredicatedSCEVRewrites.clear();
8471 FoldCache.clear();
8472 FoldCacheUser.clear();
8473}
8474void ScalarEvolution::visitAndClearUsers(
8475SmallVectorImpl<Instruction *> &Worklist,
8476SmallPtrSetImpl<Instruction *> &Visited,
8477SmallVectorImpl<const SCEV *> &ToForget) {
8478while (!Worklist.empty()) {
8479Instruction *I = Worklist.pop_back_val();
8480if (!isSCEVable(I->getType()) && !isa<WithOverflowInst>(I))
8481continue;
8482
8483ValueExprMapType::iterator It =
8484 ValueExprMap.find_as(static_cast<Value *>(I));
8485if (It != ValueExprMap.end()) {
8486 eraseValueFromMap(It->first);
8487 ToForget.push_back(It->second);
8488if (PHINode *PN = dyn_cast<PHINode>(I))
8489 ConstantEvolutionLoopExitValue.erase(PN);
8490 }
8491
8492PushDefUseChildren(I, Worklist, Visited);
8493 }
8494}
8495
8496voidScalarEvolution::forgetLoop(constLoop *L) {
8497SmallVector<const Loop *, 16> LoopWorklist(1, L);
8498SmallVector<Instruction *, 32> Worklist;
8499SmallPtrSet<Instruction *, 16> Visited;
8500SmallVector<const SCEV *, 16> ToForget;
8501
8502// Iterate over all the loops and sub-loops to drop SCEV information.
8503while (!LoopWorklist.empty()) {
8504auto *CurrL = LoopWorklist.pop_back_val();
8505
8506// Drop any stored trip count value.
8507 forgetBackedgeTakenCounts(CurrL,/* Predicated */false);
8508 forgetBackedgeTakenCounts(CurrL,/* Predicated */true);
8509
8510// Drop information about predicated SCEV rewrites for this loop.
8511for (autoI = PredicatedSCEVRewrites.begin();
8512I != PredicatedSCEVRewrites.end();) {
8513 std::pair<const SCEV *, const Loop *> Entry =I->first;
8514if (Entry.second == CurrL)
8515 PredicatedSCEVRewrites.erase(I++);
8516else
8517 ++I;
8518 }
8519
8520auto LoopUsersItr = LoopUsers.find(CurrL);
8521if (LoopUsersItr != LoopUsers.end()) {
8522 ToForget.insert(ToForget.end(), LoopUsersItr->second.begin(),
8523 LoopUsersItr->second.end());
8524 }
8525
8526// Drop information about expressions based on loop-header PHIs.
8527PushLoopPHIs(CurrL, Worklist, Visited);
8528 visitAndClearUsers(Worklist, Visited, ToForget);
8529
8530 LoopPropertiesCache.erase(CurrL);
8531// Forget all contained loops too, to avoid dangling entries in the
8532// ValuesAtScopes map.
8533 LoopWorklist.append(CurrL->begin(), CurrL->end());
8534 }
8535 forgetMemoizedResults(ToForget);
8536}
8537
8538voidScalarEvolution::forgetTopmostLoop(constLoop *L) {
8539forgetLoop(L->getOutermostLoop());
8540}
8541
8542voidScalarEvolution::forgetValue(Value *V) {
8543Instruction *I = dyn_cast<Instruction>(V);
8544if (!I)return;
8545
8546// Drop information about expressions based on loop-header PHIs.
8547SmallVector<Instruction *, 16> Worklist;
8548SmallPtrSet<Instruction *, 8> Visited;
8549SmallVector<const SCEV *, 8> ToForget;
8550 Worklist.push_back(I);
8551 Visited.insert(I);
8552 visitAndClearUsers(Worklist, Visited, ToForget);
8553
8554 forgetMemoizedResults(ToForget);
8555}
8556
8557voidScalarEvolution::forgetLcssaPhiWithNewPredecessor(Loop *L,PHINode *V) {
8558if (!isSCEVable(V->getType()))
8559return;
8560
8561// If SCEV looked through a trivial LCSSA phi node, we might have SCEV's
8562// directly using a SCEVUnknown/SCEVAddRec defined in the loop. After an
8563// extra predecessor is added, this is no longer valid. Find all Unknowns and
8564// AddRecs defined in the loop and invalidate any SCEV's making use of them.
8565if (constSCEV *S =getExistingSCEV(V)) {
8566structInvalidationRootCollector {
8567Loop *L;
8568SmallVector<const SCEV *, 8> Roots;
8569
8570 InvalidationRootCollector(Loop *L) : L(L) {}
8571
8572bool follow(constSCEV *S) {
8573if (auto *SU = dyn_cast<SCEVUnknown>(S)) {
8574if (auto *I = dyn_cast<Instruction>(SU->getValue()))
8575if (L->contains(I))
8576 Roots.push_back(S);
8577 }elseif (auto *AddRec = dyn_cast<SCEVAddRecExpr>(S)) {
8578if (L->contains(AddRec->getLoop()))
8579 Roots.push_back(S);
8580 }
8581returntrue;
8582 }
8583bool isDone() const{returnfalse; }
8584 };
8585
8586 InvalidationRootCollectorC(L);
8587visitAll(S,C);
8588 forgetMemoizedResults(C.Roots);
8589 }
8590
8591// Also perform the normal invalidation.
8592forgetValue(V);
8593}
8594
8595voidScalarEvolution::forgetLoopDispositions() { LoopDispositions.clear(); }
8596
8597voidScalarEvolution::forgetBlockAndLoopDispositions(Value *V) {
8598// Unless a specific value is passed to invalidation, completely clear both
8599// caches.
8600if (!V) {
8601 BlockDispositions.clear();
8602 LoopDispositions.clear();
8603return;
8604 }
8605
8606if (!isSCEVable(V->getType()))
8607return;
8608
8609constSCEV *S =getExistingSCEV(V);
8610if (!S)
8611return;
8612
8613// Invalidate the block and loop dispositions cached for S. Dispositions of
8614// S's users may change if S's disposition changes (i.e. a user may change to
8615// loop-invariant, if S changes to loop invariant), so also invalidate
8616// dispositions of S's users recursively.
8617SmallVector<const SCEV *, 8> Worklist = {S};
8618SmallPtrSet<const SCEV *, 8> Seen = {S};
8619while (!Worklist.empty()) {
8620constSCEV *Curr = Worklist.pop_back_val();
8621bool LoopDispoRemoved = LoopDispositions.erase(Curr);
8622bool BlockDispoRemoved = BlockDispositions.erase(Curr);
8623if (!LoopDispoRemoved && !BlockDispoRemoved)
8624continue;
8625autoUsers = SCEVUsers.find(Curr);
8626if (Users != SCEVUsers.end())
8627for (constauto *User :Users->second)
8628if (Seen.insert(User).second)
8629 Worklist.push_back(User);
8630 }
8631}
8632
8633/// Get the exact loop backedge taken count considering all loop exits. A
8634/// computable result can only be returned for loops with all exiting blocks
8635/// dominating the latch. howFarToZero assumes that the limit of each loop test
8636/// is never skipped. This is a valid assumption as long as the loop exits via
8637/// that test. For precise results, it is the caller's responsibility to specify
8638/// the relevant loop exiting block using getExact(ExitingBlock, SE).
8639constSCEV *ScalarEvolution::BackedgeTakenInfo::getExact(
8640constLoop *L,ScalarEvolution *SE,
8641SmallVectorImpl<const SCEVPredicate *> *Preds) const{
8642// If any exits were not computable, the loop is not computable.
8643if (!isComplete() || ExitNotTaken.empty())
8644return SE->getCouldNotCompute();
8645
8646constBasicBlock *Latch = L->getLoopLatch();
8647// All exiting blocks we have collected must dominate the only backedge.
8648if (!Latch)
8649return SE->getCouldNotCompute();
8650
8651// All exiting blocks we have gathered dominate loop's latch, so exact trip
8652// count is simply a minimum out of all these calculated exit counts.
8653SmallVector<const SCEV *, 2> Ops;
8654for (constauto &ENT : ExitNotTaken) {
8655constSCEV *BECount = ENT.ExactNotTaken;
8656assert(BECount != SE->getCouldNotCompute() &&"Bad exit SCEV!");
8657assert(SE->DT.dominates(ENT.ExitingBlock, Latch) &&
8658"We should only have known counts for exiting blocks that dominate "
8659"latch!");
8660
8661 Ops.push_back(BECount);
8662
8663if (Preds)
8664append_range(*Preds, ENT.Predicates);
8665
8666assert((Preds || ENT.hasAlwaysTruePredicate()) &&
8667"Predicate should be always true!");
8668 }
8669
8670// If an earlier exit exits on the first iteration (exit count zero), then
8671// a later poison exit count should not propagate into the result. This are
8672// exactly the semantics provided by umin_seq.
8673return SE->getUMinFromMismatchedTypes(Ops,/* Sequential */true);
8674}
8675
8676const ScalarEvolution::ExitNotTakenInfo *
8677ScalarEvolution::BackedgeTakenInfo::getExitNotTaken(
8678constBasicBlock *ExitingBlock,
8679SmallVectorImpl<const SCEVPredicate *> *Predicates) const{
8680for (constauto &ENT : ExitNotTaken)
8681if (ENT.ExitingBlock == ExitingBlock) {
8682if (ENT.hasAlwaysTruePredicate())
8683return &ENT;
8684elseif (Predicates) {
8685append_range(*Predicates, ENT.Predicates);
8686return &ENT;
8687 }
8688 }
8689
8690returnnullptr;
8691}
8692
8693/// getConstantMax - Get the constant max backedge taken count for the loop.
8694constSCEV *ScalarEvolution::BackedgeTakenInfo::getConstantMax(
8695ScalarEvolution *SE,
8696SmallVectorImpl<const SCEVPredicate *> *Predicates) const{
8697if (!getConstantMax())
8698return SE->getCouldNotCompute();
8699
8700for (constauto &ENT : ExitNotTaken)
8701if (!ENT.hasAlwaysTruePredicate()) {
8702if (!Predicates)
8703return SE->getCouldNotCompute();
8704append_range(*Predicates, ENT.Predicates);
8705 }
8706
8707assert((isa<SCEVCouldNotCompute>(getConstantMax()) ||
8708 isa<SCEVConstant>(getConstantMax())) &&
8709"No point in having a non-constant max backedge taken count!");
8710return getConstantMax();
8711}
8712
8713constSCEV *ScalarEvolution::BackedgeTakenInfo::getSymbolicMax(
8714constLoop *L,ScalarEvolution *SE,
8715SmallVectorImpl<const SCEVPredicate *> *Predicates) {
8716if (!SymbolicMax) {
8717// Form an expression for the maximum exit count possible for this loop. We
8718// merge the max and exact information to approximate a version of
8719// getConstantMaxBackedgeTakenCount which isn't restricted to just
8720// constants.
8721SmallVector<const SCEV *, 4> ExitCounts;
8722
8723for (constauto &ENT : ExitNotTaken) {
8724constSCEV *ExitCount = ENT.SymbolicMaxNotTaken;
8725if (!isa<SCEVCouldNotCompute>(ExitCount)) {
8726assert(SE->DT.dominates(ENT.ExitingBlock,L->getLoopLatch()) &&
8727"We should only have known counts for exiting blocks that "
8728"dominate latch!");
8729 ExitCounts.push_back(ExitCount);
8730if (Predicates)
8731append_range(*Predicates, ENT.Predicates);
8732
8733assert((Predicates || ENT.hasAlwaysTruePredicate()) &&
8734"Predicate should be always true!");
8735 }
8736 }
8737if (ExitCounts.empty())
8738 SymbolicMax = SE->getCouldNotCompute();
8739else
8740 SymbolicMax =
8741 SE->getUMinFromMismatchedTypes(ExitCounts,/*Sequential*/true);
8742 }
8743return SymbolicMax;
8744}
8745
8746bool ScalarEvolution::BackedgeTakenInfo::isConstantMaxOrZero(
8747ScalarEvolution *SE) const{
8748auto PredicateNotAlwaysTrue = [](const ExitNotTakenInfo &ENT) {
8749return !ENT.hasAlwaysTruePredicate();
8750 };
8751return MaxOrZero && !any_of(ExitNotTaken, PredicateNotAlwaysTrue);
8752}
8753
8754ScalarEvolution::ExitLimit::ExitLimit(constSCEV *E)
8755 :ExitLimit(E, E, E,false) {}
8756
8757ScalarEvolution::ExitLimit::ExitLimit(
8758constSCEV *E,constSCEV *ConstantMaxNotTaken,
8759constSCEV *SymbolicMaxNotTaken,bool MaxOrZero,
8760ArrayRef<ArrayRef<const SCEVPredicate *>> PredLists)
8761 : ExactNotTaken(E), ConstantMaxNotTaken(ConstantMaxNotTaken),
8762 SymbolicMaxNotTaken(SymbolicMaxNotTaken), MaxOrZero(MaxOrZero) {
8763// If we prove the max count is zero, so is the symbolic bound. This happens
8764// in practice due to differences in a) how context sensitive we've chosen
8765// to be and b) how we reason about bounds implied by UB.
8766if (ConstantMaxNotTaken->isZero()) {
8767 this->ExactNotTaken = E =ConstantMaxNotTaken;
8768 this->SymbolicMaxNotTaken =SymbolicMaxNotTaken =ConstantMaxNotTaken;
8769 }
8770
8771assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
8772 !isa<SCEVCouldNotCompute>(ConstantMaxNotTaken)) &&
8773"Exact is not allowed to be less precise than Constant Max");
8774assert((isa<SCEVCouldNotCompute>(ExactNotTaken) ||
8775 !isa<SCEVCouldNotCompute>(SymbolicMaxNotTaken)) &&
8776"Exact is not allowed to be less precise than Symbolic Max");
8777assert((isa<SCEVCouldNotCompute>(SymbolicMaxNotTaken) ||
8778 !isa<SCEVCouldNotCompute>(ConstantMaxNotTaken)) &&
8779"Symbolic Max is not allowed to be less precise than Constant Max");
8780assert((isa<SCEVCouldNotCompute>(ConstantMaxNotTaken) ||
8781 isa<SCEVConstant>(ConstantMaxNotTaken)) &&
8782"No point in having a non-constant max backedge taken count!");
8783SmallPtrSet<const SCEVPredicate *, 4> SeenPreds;
8784for (constauto PredList : PredLists)
8785for (constauto *P : PredList) {
8786if (SeenPreds.contains(P))
8787continue;
8788assert(!isa<SCEVUnionPredicate>(P) &&"Only add leaf predicates here!");
8789 SeenPreds.insert(P);
8790Predicates.push_back(P);
8791 }
8792assert((isa<SCEVCouldNotCompute>(E) || !E->getType()->isPointerTy()) &&
8793"Backedge count should be int");
8794assert((isa<SCEVCouldNotCompute>(ConstantMaxNotTaken) ||
8795 !ConstantMaxNotTaken->getType()->isPointerTy()) &&
8796"Max backedge count should be int");
8797}
8798
8799ScalarEvolution::ExitLimit::ExitLimit(constSCEV *E,
8800constSCEV *ConstantMaxNotTaken,
8801constSCEV *SymbolicMaxNotTaken,
8802bool MaxOrZero,
8803ArrayRef<const SCEVPredicate *> PredList)
8804 :ExitLimit(E, ConstantMaxNotTaken, SymbolicMaxNotTaken, MaxOrZero,
8805ArrayRef({PredList})) {}
8806
8807/// Allocate memory for BackedgeTakenInfo and copy the not-taken count of each
8808/// computable exit into a persistent ExitNotTakenInfo array.
8809ScalarEvolution::BackedgeTakenInfo::BackedgeTakenInfo(
8810ArrayRef<ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo> ExitCounts,
8811bool IsComplete,constSCEV *ConstantMax,bool MaxOrZero)
8812 : ConstantMax(ConstantMax), IsComplete(IsComplete), MaxOrZero(MaxOrZero) {
8813usingEdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
8814
8815 ExitNotTaken.reserve(ExitCounts.size());
8816 std::transform(ExitCounts.begin(), ExitCounts.end(),
8817 std::back_inserter(ExitNotTaken),
8818 [&](const EdgeExitInfo &EEI) {
8819 BasicBlock *ExitBB = EEI.first;
8820 const ExitLimit &EL = EEI.second;
8821 return ExitNotTakenInfo(ExitBB, EL.ExactNotTaken,
8822 EL.ConstantMaxNotTaken, EL.SymbolicMaxNotTaken,
8823 EL.Predicates);
8824 });
8825assert((isa<SCEVCouldNotCompute>(ConstantMax) ||
8826 isa<SCEVConstant>(ConstantMax)) &&
8827"No point in having a non-constant max backedge taken count!");
8828}
8829
8830/// Compute the number of times the backedge of the specified loop will execute.
8831ScalarEvolution::BackedgeTakenInfo
8832ScalarEvolution::computeBackedgeTakenCount(constLoop *L,
8833bool AllowPredicates) {
8834SmallVector<BasicBlock *, 8> ExitingBlocks;
8835 L->getExitingBlocks(ExitingBlocks);
8836
8837usingEdgeExitInfo = ScalarEvolution::BackedgeTakenInfo::EdgeExitInfo;
8838
8839SmallVector<EdgeExitInfo, 4> ExitCounts;
8840bool CouldComputeBECount =true;
8841BasicBlock *Latch = L->getLoopLatch();// may be NULL.
8842constSCEV *MustExitMaxBECount =nullptr;
8843constSCEV *MayExitMaxBECount =nullptr;
8844bool MustExitMaxOrZero =false;
8845bool IsOnlyExit = ExitingBlocks.size() == 1;
8846
8847// Compute the ExitLimit for each loop exit. Use this to populate ExitCounts
8848// and compute maxBECount.
8849// Do a union of all the predicates here.
8850for (BasicBlock *ExitBB : ExitingBlocks) {
8851// We canonicalize untaken exits to br (constant), ignore them so that
8852// proving an exit untaken doesn't negatively impact our ability to reason
8853// about the loop as whole.
8854if (auto *BI = dyn_cast<BranchInst>(ExitBB->getTerminator()))
8855if (auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
8856bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
8857if (ExitIfTrue == CI->isZero())
8858continue;
8859 }
8860
8861 ExitLimit EL = computeExitLimit(L, ExitBB, IsOnlyExit, AllowPredicates);
8862
8863assert((AllowPredicates || EL.Predicates.empty()) &&
8864"Predicated exit limit when predicates are not allowed!");
8865
8866// 1. For each exit that can be computed, add an entry to ExitCounts.
8867// CouldComputeBECount is true only if all exits can be computed.
8868if (EL.ExactNotTaken !=getCouldNotCompute())
8869 ++NumExitCountsComputed;
8870else
8871// We couldn't compute an exact value for this exit, so
8872// we won't be able to compute an exact value for the loop.
8873 CouldComputeBECount =false;
8874// Remember exit count if either exact or symbolic is known. Because
8875// Exact always implies symbolic, only check symbolic.
8876if (EL.SymbolicMaxNotTaken !=getCouldNotCompute())
8877 ExitCounts.emplace_back(ExitBB, EL);
8878else {
8879assert(EL.ExactNotTaken ==getCouldNotCompute() &&
8880"Exact is known but symbolic isn't?");
8881 ++NumExitCountsNotComputed;
8882 }
8883
8884// 2. Derive the loop's MaxBECount from each exit's max number of
8885// non-exiting iterations. Partition the loop exits into two kinds:
8886// LoopMustExits and LoopMayExits.
8887//
8888// If the exit dominates the loop latch, it is a LoopMustExit otherwise it
8889// is a LoopMayExit. If any computable LoopMustExit is found, then
8890// MaxBECount is the minimum EL.ConstantMaxNotTaken of computable
8891// LoopMustExits. Otherwise, MaxBECount is conservatively the maximum
8892// EL.ConstantMaxNotTaken, where CouldNotCompute is considered greater than
8893// any
8894// computable EL.ConstantMaxNotTaken.
8895if (EL.ConstantMaxNotTaken !=getCouldNotCompute() && Latch &&
8896 DT.dominates(ExitBB, Latch)) {
8897if (!MustExitMaxBECount) {
8898 MustExitMaxBECount = EL.ConstantMaxNotTaken;
8899 MustExitMaxOrZero = EL.MaxOrZero;
8900 }else {
8901 MustExitMaxBECount =getUMinFromMismatchedTypes(MustExitMaxBECount,
8902 EL.ConstantMaxNotTaken);
8903 }
8904 }elseif (MayExitMaxBECount !=getCouldNotCompute()) {
8905if (!MayExitMaxBECount || EL.ConstantMaxNotTaken ==getCouldNotCompute())
8906 MayExitMaxBECount = EL.ConstantMaxNotTaken;
8907else {
8908 MayExitMaxBECount =getUMaxFromMismatchedTypes(MayExitMaxBECount,
8909 EL.ConstantMaxNotTaken);
8910 }
8911 }
8912 }
8913constSCEV *MaxBECount = MustExitMaxBECount ? MustExitMaxBECount :
8914 (MayExitMaxBECount ? MayExitMaxBECount :getCouldNotCompute());
8915// The loop backedge will be taken the maximum or zero times if there's
8916// a single exit that must be taken the maximum or zero times.
8917bool MaxOrZero = (MustExitMaxOrZero && ExitingBlocks.size() == 1);
8918
8919// Remember which SCEVs are used in exit limits for invalidation purposes.
8920// We only care about non-constant SCEVs here, so we can ignore
8921// EL.ConstantMaxNotTaken
8922// and MaxBECount, which must be SCEVConstant.
8923for (constauto &Pair : ExitCounts) {
8924if (!isa<SCEVConstant>(Pair.second.ExactNotTaken))
8925 BECountUsers[Pair.second.ExactNotTaken].insert({L, AllowPredicates});
8926if (!isa<SCEVConstant>(Pair.second.SymbolicMaxNotTaken))
8927 BECountUsers[Pair.second.SymbolicMaxNotTaken].insert(
8928 {L, AllowPredicates});
8929 }
8930return BackedgeTakenInfo(std::move(ExitCounts), CouldComputeBECount,
8931 MaxBECount, MaxOrZero);
8932}
8933
8934ScalarEvolution::ExitLimit
8935ScalarEvolution::computeExitLimit(constLoop *L,BasicBlock *ExitingBlock,
8936bool IsOnlyExit,bool AllowPredicates) {
8937assert(L->contains(ExitingBlock) &&"Exit count for non-loop block?");
8938// If our exiting block does not dominate the latch, then its connection with
8939// loop's exit limit may be far from trivial.
8940constBasicBlock *Latch =L->getLoopLatch();
8941if (!Latch || !DT.dominates(ExitingBlock, Latch))
8942returngetCouldNotCompute();
8943
8944Instruction *Term = ExitingBlock->getTerminator();
8945if (BranchInst *BI = dyn_cast<BranchInst>(Term)) {
8946assert(BI->isConditional() &&"If unconditional, it can't be in loop!");
8947bool ExitIfTrue = !L->contains(BI->getSuccessor(0));
8948assert(ExitIfTrue ==L->contains(BI->getSuccessor(1)) &&
8949"It should have one successor in loop and one exit block!");
8950// Proceed to the next level to examine the exit condition expression.
8951returncomputeExitLimitFromCond(L, BI->getCondition(), ExitIfTrue,
8952/*ControlsOnlyExit=*/IsOnlyExit,
8953 AllowPredicates);
8954 }
8955
8956if (SwitchInst *SI = dyn_cast<SwitchInst>(Term)) {
8957// For switch, make sure that there is a single exit from the loop.
8958BasicBlock *Exit =nullptr;
8959for (auto *SBB :successors(ExitingBlock))
8960if (!L->contains(SBB)) {
8961if (Exit)// Multiple exit successors.
8962returngetCouldNotCompute();
8963Exit =SBB;
8964 }
8965assert(Exit &&"Exiting block must have at least one exit");
8966return computeExitLimitFromSingleExitSwitch(
8967 L, SI, Exit,/*ControlsOnlyExit=*/IsOnlyExit);
8968 }
8969
8970returngetCouldNotCompute();
8971}
8972
8973ScalarEvolution::ExitLimitScalarEvolution::computeExitLimitFromCond(
8974constLoop *L,Value *ExitCond,bool ExitIfTrue,bool ControlsOnlyExit,
8975bool AllowPredicates) {
8976 ScalarEvolution::ExitLimitCacheTy Cache(L, ExitIfTrue, AllowPredicates);
8977return computeExitLimitFromCondCached(Cache, L, ExitCond, ExitIfTrue,
8978 ControlsOnlyExit, AllowPredicates);
8979}
8980
8981std::optional<ScalarEvolution::ExitLimit>
8982ScalarEvolution::ExitLimitCache::find(constLoop *L,Value *ExitCond,
8983bool ExitIfTrue,bool ControlsOnlyExit,
8984bool AllowPredicates) {
8985 (void)this->L;
8986 (void)this->ExitIfTrue;
8987 (void)this->AllowPredicates;
8988
8989assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
8990 this->AllowPredicates == AllowPredicates &&
8991"Variance in assumed invariant key components!");
8992auto Itr = TripCountMap.find({ExitCond, ControlsOnlyExit});
8993if (Itr == TripCountMap.end())
8994return std::nullopt;
8995return Itr->second;
8996}
8997
8998void ScalarEvolution::ExitLimitCache::insert(constLoop *L,Value *ExitCond,
8999bool ExitIfTrue,
9000bool ControlsOnlyExit,
9001bool AllowPredicates,
9002const ExitLimit &EL) {
9003assert(this->L == L && this->ExitIfTrue == ExitIfTrue &&
9004 this->AllowPredicates == AllowPredicates &&
9005"Variance in assumed invariant key components!");
9006
9007auto InsertResult = TripCountMap.insert({{ExitCond, ControlsOnlyExit}, EL});
9008assert(InsertResult.second &&"Expected successful insertion!");
9009 (void)InsertResult;
9010 (void)ExitIfTrue;
9011}
9012
9013ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondCached(
9014 ExitLimitCacheTy &Cache,constLoop *L,Value *ExitCond,bool ExitIfTrue,
9015bool ControlsOnlyExit,bool AllowPredicates) {
9016
9017if (auto MaybeEL = Cache.find(L, ExitCond, ExitIfTrue, ControlsOnlyExit,
9018 AllowPredicates))
9019return *MaybeEL;
9020
9021 ExitLimit EL = computeExitLimitFromCondImpl(
9022 Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates);
9023 Cache.insert(L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates, EL);
9024return EL;
9025}
9026
9027ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromCondImpl(
9028 ExitLimitCacheTy &Cache,constLoop *L,Value *ExitCond,bool ExitIfTrue,
9029bool ControlsOnlyExit,bool AllowPredicates) {
9030// Handle BinOp conditions (And, Or).
9031if (auto LimitFromBinOp = computeExitLimitFromCondFromBinOp(
9032 Cache, L, ExitCond, ExitIfTrue, ControlsOnlyExit, AllowPredicates))
9033return *LimitFromBinOp;
9034
9035// With an icmp, it may be feasible to compute an exact backedge-taken count.
9036// Proceed to the next level to examine the icmp.
9037if (ICmpInst *ExitCondICmp = dyn_cast<ICmpInst>(ExitCond)) {
9038 ExitLimit EL =
9039 computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue, ControlsOnlyExit);
9040if (EL.hasFullInfo() || !AllowPredicates)
9041return EL;
9042
9043// Try again, but use SCEV predicates this time.
9044return computeExitLimitFromICmp(L, ExitCondICmp, ExitIfTrue,
9045 ControlsOnlyExit,
9046/*AllowPredicates=*/true);
9047 }
9048
9049// Check for a constant condition. These are normally stripped out by
9050// SimplifyCFG, but ScalarEvolution may be used by a pass which wishes to
9051// preserve the CFG and is temporarily leaving constant conditions
9052// in place.
9053if (ConstantInt *CI = dyn_cast<ConstantInt>(ExitCond)) {
9054if (ExitIfTrue == !CI->getZExtValue())
9055// The backedge is always taken.
9056returngetCouldNotCompute();
9057// The backedge is never taken.
9058returngetZero(CI->getType());
9059 }
9060
9061// If we're exiting based on the overflow flag of an x.with.overflow intrinsic
9062// with a constant step, we can form an equivalent icmp predicate and figure
9063// out how many iterations will be taken before we exit.
9064constWithOverflowInst *WO;
9065constAPInt *C;
9066if (match(ExitCond, m_ExtractValue<1>(m_WithOverflowInst(WO))) &&
9067match(WO->getRHS(),m_APInt(C))) {
9068ConstantRange NWR =
9069ConstantRange::makeExactNoWrapRegion(WO->getBinaryOp(), *C,
9070 WO->getNoWrapKind());
9071CmpInst::Predicate Pred;
9072APInt NewRHSC,Offset;
9073 NWR.getEquivalentICmp(Pred, NewRHSC,Offset);
9074if (!ExitIfTrue)
9075 Pred =ICmpInst::getInversePredicate(Pred);
9076auto *LHS =getSCEV(WO->getLHS());
9077if (Offset != 0)
9078LHS =getAddExpr(LHS,getConstant(Offset));
9079auto EL = computeExitLimitFromICmp(L, Pred, LHS,getConstant(NewRHSC),
9080 ControlsOnlyExit, AllowPredicates);
9081if (EL.hasAnyInfo())
9082return EL;
9083 }
9084
9085// If it's not an integer or pointer comparison then compute it the hard way.
9086return computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
9087}
9088
9089std::optional<ScalarEvolution::ExitLimit>
9090ScalarEvolution::computeExitLimitFromCondFromBinOp(
9091 ExitLimitCacheTy &Cache,constLoop *L,Value *ExitCond,bool ExitIfTrue,
9092bool ControlsOnlyExit,bool AllowPredicates) {
9093// Check if the controlling expression for this loop is an And or Or.
9094Value *Op0, *Op1;
9095bool IsAnd =false;
9096if (match(ExitCond,m_LogicalAnd(m_Value(Op0),m_Value(Op1))))
9097 IsAnd =true;
9098elseif (match(ExitCond,m_LogicalOr(m_Value(Op0),m_Value(Op1))))
9099 IsAnd =false;
9100else
9101return std::nullopt;
9102
9103// EitherMayExit is true in these two cases:
9104// br (and Op0 Op1), loop, exit
9105// br (or Op0 Op1), exit, loop
9106bool EitherMayExit = IsAnd ^ ExitIfTrue;
9107 ExitLimit EL0 = computeExitLimitFromCondCached(
9108 Cache, L, Op0, ExitIfTrue, ControlsOnlyExit && !EitherMayExit,
9109 AllowPredicates);
9110 ExitLimit EL1 = computeExitLimitFromCondCached(
9111 Cache, L, Op1, ExitIfTrue, ControlsOnlyExit && !EitherMayExit,
9112 AllowPredicates);
9113
9114// Be robust against unsimplified IR for the form "op i1 X, NeutralElement"
9115constConstant *NeutralElement = ConstantInt::get(ExitCond->getType(), IsAnd);
9116if (isa<ConstantInt>(Op1))
9117return Op1 == NeutralElement ? EL0 : EL1;
9118if (isa<ConstantInt>(Op0))
9119return Op0 == NeutralElement ? EL1 : EL0;
9120
9121constSCEV *BECount =getCouldNotCompute();
9122constSCEV *ConstantMaxBECount =getCouldNotCompute();
9123constSCEV *SymbolicMaxBECount =getCouldNotCompute();
9124if (EitherMayExit) {
9125bool UseSequentialUMin = !isa<BinaryOperator>(ExitCond);
9126// Both conditions must be same for the loop to continue executing.
9127// Choose the less conservative count.
9128if (EL0.ExactNotTaken !=getCouldNotCompute() &&
9129 EL1.ExactNotTaken !=getCouldNotCompute()) {
9130 BECount =getUMinFromMismatchedTypes(EL0.ExactNotTaken, EL1.ExactNotTaken,
9131 UseSequentialUMin);
9132 }
9133if (EL0.ConstantMaxNotTaken ==getCouldNotCompute())
9134 ConstantMaxBECount = EL1.ConstantMaxNotTaken;
9135elseif (EL1.ConstantMaxNotTaken ==getCouldNotCompute())
9136 ConstantMaxBECount = EL0.ConstantMaxNotTaken;
9137else
9138 ConstantMaxBECount =getUMinFromMismatchedTypes(EL0.ConstantMaxNotTaken,
9139 EL1.ConstantMaxNotTaken);
9140if (EL0.SymbolicMaxNotTaken ==getCouldNotCompute())
9141 SymbolicMaxBECount = EL1.SymbolicMaxNotTaken;
9142elseif (EL1.SymbolicMaxNotTaken ==getCouldNotCompute())
9143 SymbolicMaxBECount = EL0.SymbolicMaxNotTaken;
9144else
9145 SymbolicMaxBECount =getUMinFromMismatchedTypes(
9146 EL0.SymbolicMaxNotTaken, EL1.SymbolicMaxNotTaken, UseSequentialUMin);
9147 }else {
9148// Both conditions must be same at the same time for the loop to exit.
9149// For now, be conservative.
9150if (EL0.ExactNotTaken == EL1.ExactNotTaken)
9151 BECount = EL0.ExactNotTaken;
9152 }
9153
9154// There are cases (e.g. PR26207) where computeExitLimitFromCond is able
9155// to be more aggressive when computing BECount than when computing
9156// ConstantMaxBECount. In these cases it is possible for EL0.ExactNotTaken
9157// and
9158// EL1.ExactNotTaken to match, but for EL0.ConstantMaxNotTaken and
9159// EL1.ConstantMaxNotTaken to not.
9160if (isa<SCEVCouldNotCompute>(ConstantMaxBECount) &&
9161 !isa<SCEVCouldNotCompute>(BECount))
9162 ConstantMaxBECount =getConstant(getUnsignedRangeMax(BECount));
9163if (isa<SCEVCouldNotCompute>(SymbolicMaxBECount))
9164 SymbolicMaxBECount =
9165 isa<SCEVCouldNotCompute>(BECount) ? ConstantMaxBECount : BECount;
9166return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount,false,
9167 {ArrayRef(EL0.Predicates),ArrayRef(EL1.Predicates)});
9168}
9169
9170ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
9171constLoop *L,ICmpInst *ExitCond,bool ExitIfTrue,bool ControlsOnlyExit,
9172bool AllowPredicates) {
9173// If the condition was exit on true, convert the condition to exit on false
9174CmpPredicate Pred;
9175if (!ExitIfTrue)
9176 Pred = ExitCond->getCmpPredicate();
9177else
9178 Pred = ExitCond->getInverseCmpPredicate();
9179constICmpInst::Predicate OriginalPred = Pred;
9180
9181constSCEV *LHS =getSCEV(ExitCond->getOperand(0));
9182constSCEV *RHS =getSCEV(ExitCond->getOperand(1));
9183
9184 ExitLimit EL = computeExitLimitFromICmp(L, Pred, LHS, RHS, ControlsOnlyExit,
9185 AllowPredicates);
9186if (EL.hasAnyInfo())
9187return EL;
9188
9189auto *ExhaustiveCount =
9190 computeExitCountExhaustively(L, ExitCond, ExitIfTrue);
9191
9192if (!isa<SCEVCouldNotCompute>(ExhaustiveCount))
9193return ExhaustiveCount;
9194
9195return computeShiftCompareExitLimit(ExitCond->getOperand(0),
9196 ExitCond->getOperand(1), L, OriginalPred);
9197}
9198ScalarEvolution::ExitLimit ScalarEvolution::computeExitLimitFromICmp(
9199constLoop *L,CmpPredicate Pred,constSCEV *LHS,constSCEV *RHS,
9200bool ControlsOnlyExit,bool AllowPredicates) {
9201
9202// Try to evaluate any dependencies out of the loop.
9203LHS =getSCEVAtScope(LHS, L);
9204RHS =getSCEVAtScope(RHS, L);
9205
9206// At this point, we would like to compute how many iterations of the
9207// loop the predicate will return true for these inputs.
9208if (isLoopInvariant(LHS, L) && !isLoopInvariant(RHS, L)) {
9209// If there is a loop-invariant, force it into the RHS.
9210std::swap(LHS, RHS);
9211 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
9212 }
9213
9214bool ControllingFiniteLoop = ControlsOnlyExit &&loopHasNoAbnormalExits(L) &&
9215loopIsFiniteByAssumption(L);
9216// Simplify the operands before analyzing them.
9217 (void)SimplifyICmpOperands(Pred, LHS, RHS,/*Depth=*/0);
9218
9219// If we have a comparison of a chrec against a constant, try to use value
9220// ranges to answer this query.
9221if (constSCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS))
9222if (constSCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(LHS))
9223if (AddRec->getLoop() ==L) {
9224// Form the constant range.
9225ConstantRange CompRange =
9226ConstantRange::makeExactICmpRegion(Pred, RHSC->getAPInt());
9227
9228constSCEV *Ret = AddRec->getNumIterationsInRange(CompRange, *this);
9229if (!isa<SCEVCouldNotCompute>(Ret))returnRet;
9230 }
9231
9232// If this loop must exit based on this condition (or execute undefined
9233// behaviour), see if we can improve wrap flags. This is essentially
9234// a must execute style proof.
9235if (ControllingFiniteLoop &&isLoopInvariant(RHS, L)) {
9236// If we can prove the test sequence produced must repeat the same values
9237// on self-wrap of the IV, then we can infer that IV doesn't self wrap
9238// because if it did, we'd have an infinite (undefined) loop.
9239// TODO: We can peel off any functions which are invertible *in L*. Loop
9240// invariant terms are effectively constants for our purposes here.
9241auto *InnerLHS =LHS;
9242if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS))
9243 InnerLHS = ZExt->getOperand();
9244if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(InnerLHS);
9245 AR && !AR->hasNoSelfWrap() && AR->getLoop() == L && AR->isAffine() &&
9246isKnownToBeAPowerOfTwo(AR->getStepRecurrence(*this),/*OrZero=*/true,
9247/*OrNegative=*/true)) {
9248autoFlags = AR->getNoWrapFlags();
9249Flags =setFlags(Flags,SCEV::FlagNW);
9250SmallVector<const SCEV *>Operands{AR->operands()};
9251Flags =StrengthenNoWrapFlags(this,scAddRecExpr,Operands, Flags);
9252setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
9253 }
9254
9255// For a slt/ult condition with a positive step, can we prove nsw/nuw?
9256// From no-self-wrap, this follows trivially from the fact that every
9257// (un)signed-wrapped, but not self-wrapped value must be LT than the
9258// last value before (un)signed wrap. Since we know that last value
9259// didn't exit, nor will any smaller one.
9260if (Pred ==ICmpInst::ICMP_SLT || Pred ==ICmpInst::ICMP_ULT) {
9261auto WrapType = Pred ==ICmpInst::ICMP_SLT ?SCEV::FlagNSW :SCEV::FlagNUW;
9262if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS);
9263 AR && AR->getLoop() == L && AR->isAffine() &&
9264 !AR->getNoWrapFlags(WrapType) && AR->hasNoSelfWrap() &&
9265isKnownPositive(AR->getStepRecurrence(*this))) {
9266autoFlags = AR->getNoWrapFlags();
9267Flags =setFlags(Flags, WrapType);
9268SmallVector<const SCEV*>Operands{AR->operands()};
9269Flags =StrengthenNoWrapFlags(this,scAddRecExpr,Operands, Flags);
9270setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
9271 }
9272 }
9273 }
9274
9275switch (Pred) {
9276caseICmpInst::ICMP_NE: {// while (X != Y)
9277// Convert to: while (X-Y != 0)
9278if (LHS->getType()->isPointerTy()) {
9279LHS =getLosslessPtrToIntExpr(LHS);
9280if (isa<SCEVCouldNotCompute>(LHS))
9281returnLHS;
9282 }
9283if (RHS->getType()->isPointerTy()) {
9284RHS =getLosslessPtrToIntExpr(RHS);
9285if (isa<SCEVCouldNotCompute>(RHS))
9286returnRHS;
9287 }
9288 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit,
9289 AllowPredicates);
9290if (EL.hasAnyInfo())
9291return EL;
9292break;
9293 }
9294caseICmpInst::ICMP_EQ: {// while (X == Y)
9295// Convert to: while (X-Y == 0)
9296if (LHS->getType()->isPointerTy()) {
9297LHS =getLosslessPtrToIntExpr(LHS);
9298if (isa<SCEVCouldNotCompute>(LHS))
9299returnLHS;
9300 }
9301if (RHS->getType()->isPointerTy()) {
9302RHS =getLosslessPtrToIntExpr(RHS);
9303if (isa<SCEVCouldNotCompute>(RHS))
9304returnRHS;
9305 }
9306 ExitLimit EL = howFarToNonZero(getMinusSCEV(LHS, RHS), L);
9307if (EL.hasAnyInfo())return EL;
9308break;
9309 }
9310caseICmpInst::ICMP_SLE:
9311caseICmpInst::ICMP_ULE:
9312// Since the loop is finite, an invariant RHS cannot include the boundary
9313// value, otherwise it would loop forever.
9314if (!EnableFiniteLoopControl || !ControllingFiniteLoop ||
9315 !isLoopInvariant(RHS, L)) {
9316// Otherwise, perform the addition in a wider type, to avoid overflow.
9317// If the LHS is an addrec with the appropriate nowrap flag, the
9318// extension will be sunk into it and the exit count can be analyzed.
9319auto *OldType = dyn_cast<IntegerType>(LHS->getType());
9320if (!OldType)
9321break;
9322// Prefer doubling the bitwidth over adding a single bit to make it more
9323// likely that we use a legal type.
9324auto *NewType =
9325Type::getIntNTy(OldType->getContext(), OldType->getBitWidth() * 2);
9326if (ICmpInst::isSigned(Pred)) {
9327LHS =getSignExtendExpr(LHS, NewType);
9328RHS =getSignExtendExpr(RHS, NewType);
9329 }else {
9330LHS =getZeroExtendExpr(LHS, NewType);
9331RHS =getZeroExtendExpr(RHS, NewType);
9332 }
9333 }
9334RHS =getAddExpr(getOne(RHS->getType()), RHS);
9335 [[fallthrough]];
9336caseICmpInst::ICMP_SLT:
9337caseICmpInst::ICMP_ULT: {// while (X < Y)
9338bool IsSigned =ICmpInst::isSigned(Pred);
9339 ExitLimit EL = howManyLessThans(LHS, RHS, L, IsSigned, ControlsOnlyExit,
9340 AllowPredicates);
9341if (EL.hasAnyInfo())
9342return EL;
9343break;
9344 }
9345caseICmpInst::ICMP_SGE:
9346caseICmpInst::ICMP_UGE:
9347// Since the loop is finite, an invariant RHS cannot include the boundary
9348// value, otherwise it would loop forever.
9349if (!EnableFiniteLoopControl || !ControllingFiniteLoop ||
9350 !isLoopInvariant(RHS, L))
9351break;
9352RHS =getAddExpr(getMinusOne(RHS->getType()), RHS);
9353 [[fallthrough]];
9354caseICmpInst::ICMP_SGT:
9355caseICmpInst::ICMP_UGT: {// while (X > Y)
9356bool IsSigned =ICmpInst::isSigned(Pred);
9357 ExitLimit EL = howManyGreaterThans(LHS, RHS, L, IsSigned, ControlsOnlyExit,
9358 AllowPredicates);
9359if (EL.hasAnyInfo())
9360return EL;
9361break;
9362 }
9363default:
9364break;
9365 }
9366
9367returngetCouldNotCompute();
9368}
9369
9370ScalarEvolution::ExitLimit
9371ScalarEvolution::computeExitLimitFromSingleExitSwitch(constLoop *L,
9372SwitchInst *Switch,
9373BasicBlock *ExitingBlock,
9374bool ControlsOnlyExit) {
9375assert(!L->contains(ExitingBlock) &&"Not an exiting block!");
9376
9377// Give up if the exit is the default dest of a switch.
9378if (Switch->getDefaultDest() == ExitingBlock)
9379returngetCouldNotCompute();
9380
9381assert(L->contains(Switch->getDefaultDest()) &&
9382"Default case must not exit the loop!");
9383constSCEV *LHS =getSCEVAtScope(Switch->getCondition(), L);
9384constSCEV *RHS =getConstant(Switch->findCaseDest(ExitingBlock));
9385
9386// while (X != Y) --> while (X-Y != 0)
9387 ExitLimit EL = howFarToZero(getMinusSCEV(LHS, RHS), L, ControlsOnlyExit);
9388if (EL.hasAnyInfo())
9389return EL;
9390
9391returngetCouldNotCompute();
9392}
9393
9394staticConstantInt *
9395EvaluateConstantChrecAtConstant(constSCEVAddRecExpr *AddRec,ConstantInt *C,
9396ScalarEvolution &SE) {
9397constSCEV *InVal = SE.getConstant(C);
9398constSCEV *Val = AddRec->evaluateAtIteration(InVal, SE);
9399assert(isa<SCEVConstant>(Val) &&
9400"Evaluation of SCEV at constant didn't fold correctly?");
9401return cast<SCEVConstant>(Val)->getValue();
9402}
9403
9404ScalarEvolution::ExitLimit ScalarEvolution::computeShiftCompareExitLimit(
9405Value *LHS,Value *RHSV,constLoop *L,ICmpInst::Predicate Pred) {
9406ConstantInt *RHS = dyn_cast<ConstantInt>(RHSV);
9407if (!RHS)
9408returngetCouldNotCompute();
9409
9410constBasicBlock *Latch =L->getLoopLatch();
9411if (!Latch)
9412returngetCouldNotCompute();
9413
9414constBasicBlock *Predecessor =L->getLoopPredecessor();
9415if (!Predecessor)
9416returngetCouldNotCompute();
9417
9418// Return true if V is of the form "LHS `shift_op` <positive constant>".
9419// Return LHS in OutLHS and shift_opt in OutOpCode.
9420auto MatchPositiveShift =
9421 [](Value *V,Value *&OutLHS,Instruction::BinaryOps &OutOpCode) {
9422
9423using namespacePatternMatch;
9424
9425ConstantInt *ShiftAmt;
9426if (match(V,m_LShr(m_Value(OutLHS),m_ConstantInt(ShiftAmt))))
9427 OutOpCode = Instruction::LShr;
9428elseif (match(V,m_AShr(m_Value(OutLHS),m_ConstantInt(ShiftAmt))))
9429 OutOpCode = Instruction::AShr;
9430elseif (match(V,m_Shl(m_Value(OutLHS),m_ConstantInt(ShiftAmt))))
9431 OutOpCode = Instruction::Shl;
9432else
9433returnfalse;
9434
9435return ShiftAmt->getValue().isStrictlyPositive();
9436 };
9437
9438// Recognize a "shift recurrence" either of the form %iv or of %iv.shifted in
9439//
9440// loop:
9441// %iv = phi i32 [ %iv.shifted, %loop ], [ %val, %preheader ]
9442// %iv.shifted = lshr i32 %iv, <positive constant>
9443//
9444// Return true on a successful match. Return the corresponding PHI node (%iv
9445// above) in PNOut and the opcode of the shift operation in OpCodeOut.
9446auto MatchShiftRecurrence =
9447 [&](Value *V,PHINode *&PNOut,Instruction::BinaryOps &OpCodeOut) {
9448 std::optional<Instruction::BinaryOps> PostShiftOpCode;
9449
9450 {
9451Instruction::BinaryOps OpC;
9452Value *V;
9453
9454// If we encounter a shift instruction, "peel off" the shift operation,
9455// and remember that we did so. Later when we inspect %iv's backedge
9456// value, we will make sure that the backedge value uses the same
9457// operation.
9458//
9459// Note: the peeled shift operation does not have to be the same
9460// instruction as the one feeding into the PHI's backedge value. We only
9461// really care about it being the same *kind* of shift instruction --
9462// that's all that is required for our later inferences to hold.
9463if (MatchPositiveShift(LHS, V, OpC)) {
9464 PostShiftOpCode = OpC;
9465LHS =V;
9466 }
9467 }
9468
9469 PNOut = dyn_cast<PHINode>(LHS);
9470if (!PNOut || PNOut->getParent() !=L->getHeader())
9471returnfalse;
9472
9473Value *BEValue = PNOut->getIncomingValueForBlock(Latch);
9474Value *OpLHS;
9475
9476return
9477// The backedge value for the PHI node must be a shift by a positive
9478// amount
9479 MatchPositiveShift(BEValue, OpLHS, OpCodeOut) &&
9480
9481// of the PHI node itself
9482 OpLHS == PNOut &&
9483
9484// and the kind of shift should be match the kind of shift we peeled
9485// off, if any.
9486 (!PostShiftOpCode || *PostShiftOpCode == OpCodeOut);
9487 };
9488
9489PHINode *PN;
9490Instruction::BinaryOpsOpCode;
9491if (!MatchShiftRecurrence(LHS, PN, OpCode))
9492returngetCouldNotCompute();
9493
9494constDataLayout &DL =getDataLayout();
9495
9496// The key rationale for this optimization is that for some kinds of shift
9497// recurrences, the value of the recurrence "stabilizes" to either 0 or -1
9498// within a finite number of iterations. If the condition guarding the
9499// backedge (in the sense that the backedge is taken if the condition is true)
9500// is false for the value the shift recurrence stabilizes to, then we know
9501// that the backedge is taken only a finite number of times.
9502
9503ConstantInt *StableValue =nullptr;
9504switch (OpCode) {
9505default:
9506llvm_unreachable("Impossible case!");
9507
9508case Instruction::AShr: {
9509// {K,ashr,<positive-constant>} stabilizes to signum(K) in at most
9510// bitwidth(K) iterations.
9511Value *FirstValue = PN->getIncomingValueForBlock(Predecessor);
9512KnownBits Known =computeKnownBits(FirstValue, DL, 0, &AC,
9513 Predecessor->getTerminator(), &DT);
9514auto *Ty = cast<IntegerType>(RHS->getType());
9515if (Known.isNonNegative())
9516 StableValue = ConstantInt::get(Ty, 0);
9517elseif (Known.isNegative())
9518 StableValue = ConstantInt::get(Ty, -1,true);
9519else
9520returngetCouldNotCompute();
9521
9522break;
9523 }
9524case Instruction::LShr:
9525case Instruction::Shl:
9526// Both {K,lshr,<positive-constant>} and {K,shl,<positive-constant>}
9527// stabilize to 0 in at most bitwidth(K) iterations.
9528 StableValue = ConstantInt::get(cast<IntegerType>(RHS->getType()), 0);
9529break;
9530 }
9531
9532auto *Result =
9533ConstantFoldCompareInstOperands(Pred, StableValue, RHS, DL, &TLI);
9534assert(Result->getType()->isIntegerTy(1) &&
9535"Otherwise cannot be an operand to a branch instruction");
9536
9537if (Result->isZeroValue()) {
9538unsignedBitWidth =getTypeSizeInBits(RHS->getType());
9539constSCEV *UpperBound =
9540getConstant(getEffectiveSCEVType(RHS->getType()),BitWidth);
9541return ExitLimit(getCouldNotCompute(), UpperBound, UpperBound,false);
9542 }
9543
9544returngetCouldNotCompute();
9545}
9546
9547/// Return true if we can constant fold an instruction of the specified type,
9548/// assuming that all operands were constants.
9549staticboolCanConstantFold(constInstruction *I) {
9550if (isa<BinaryOperator>(I) || isa<CmpInst>(I) ||
9551 isa<SelectInst>(I) || isa<CastInst>(I) || isa<GetElementPtrInst>(I) ||
9552 isa<LoadInst>(I) || isa<ExtractValueInst>(I))
9553returntrue;
9554
9555if (constCallInst *CI = dyn_cast<CallInst>(I))
9556if (constFunction *F = CI->getCalledFunction())
9557returncanConstantFoldCallTo(CI, F);
9558returnfalse;
9559}
9560
9561/// Determine whether this instruction can constant evolve within this loop
9562/// assuming its operands can all constant evolve.
9563staticboolcanConstantEvolve(Instruction *I,constLoop *L) {
9564// An instruction outside of the loop can't be derived from a loop PHI.
9565if (!L->contains(I))returnfalse;
9566
9567if (isa<PHINode>(I)) {
9568// We don't currently keep track of the control flow needed to evaluate
9569// PHIs, so we cannot handle PHIs inside of loops.
9570return L->getHeader() ==I->getParent();
9571 }
9572
9573// If we won't be able to constant fold this expression even if the operands
9574// are constants, bail early.
9575returnCanConstantFold(I);
9576}
9577
9578/// getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by
9579/// recursing through each instruction operand until reaching a loop header phi.
9580staticPHINode *
9581getConstantEvolvingPHIOperands(Instruction *UseInst,constLoop *L,
9582DenseMap<Instruction *, PHINode *> &PHIMap,
9583unsignedDepth) {
9584if (Depth >MaxConstantEvolvingDepth)
9585returnnullptr;
9586
9587// Otherwise, we can evaluate this instruction if all of its operands are
9588// constant or derived from a PHI node themselves.
9589PHINode *PHI =nullptr;
9590for (Value *Op : UseInst->operands()) {
9591if (isa<Constant>(Op))continue;
9592
9593Instruction *OpInst = dyn_cast<Instruction>(Op);
9594if (!OpInst || !canConstantEvolve(OpInst, L))returnnullptr;
9595
9596PHINode *P = dyn_cast<PHINode>(OpInst);
9597if (!P)
9598// If this operand is already visited, reuse the prior result.
9599// We may have P != PHI if this is the deepest point at which the
9600// inconsistent paths meet.
9601P = PHIMap.lookup(OpInst);
9602if (!P) {
9603// Recurse and memoize the results, whether a phi is found or not.
9604// This recursive call invalidates pointers into PHIMap.
9605P =getConstantEvolvingPHIOperands(OpInst, L, PHIMap,Depth + 1);
9606 PHIMap[OpInst] =P;
9607 }
9608if (!P)
9609returnnullptr;// Not evolving from PHI
9610if (PHI &&PHI !=P)
9611returnnullptr;// Evolving from multiple different PHIs.
9612PHI =P;
9613 }
9614// This is a expression evolving from a constant PHI!
9615returnPHI;
9616}
9617
9618/// getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node
9619/// in the loop that V is derived from. We allow arbitrary operations along the
9620/// way, but the operands of an operation must either be constants or a value
9621/// derived from a constant PHI. If this expression does not fit with these
9622/// constraints, return null.
9623staticPHINode *getConstantEvolvingPHI(Value *V,constLoop *L) {
9624Instruction *I = dyn_cast<Instruction>(V);
9625if (!I || !canConstantEvolve(I, L))returnnullptr;
9626
9627if (PHINode *PN = dyn_cast<PHINode>(I))
9628return PN;
9629
9630// Record non-constant instructions contained by the loop.
9631DenseMap<Instruction *, PHINode *> PHIMap;
9632returngetConstantEvolvingPHIOperands(I, L, PHIMap, 0);
9633}
9634
9635/// EvaluateExpression - Given an expression that passes the
9636/// getConstantEvolvingPHI predicate, evaluate its value assuming the PHI node
9637/// in the loop has the value PHIVal. If we can't fold this expression for some
9638/// reason, return null.
9639staticConstant *EvaluateExpression(Value *V,constLoop *L,
9640DenseMap<Instruction *, Constant *> &Vals,
9641constDataLayout &DL,
9642constTargetLibraryInfo *TLI) {
9643// Convenient constant check, but redundant for recursive calls.
9644if (Constant *C = dyn_cast<Constant>(V))returnC;
9645Instruction *I = dyn_cast<Instruction>(V);
9646if (!I)returnnullptr;
9647
9648if (Constant *C = Vals.lookup(I))returnC;
9649
9650// An instruction inside the loop depends on a value outside the loop that we
9651// weren't given a mapping for, or a value such as a call inside the loop.
9652if (!canConstantEvolve(I, L))returnnullptr;
9653
9654// An unmapped PHI can be due to a branch or another loop inside this loop,
9655// or due to this not being the initial iteration through a loop where we
9656// couldn't compute the evolution of this particular PHI last time.
9657if (isa<PHINode>(I))returnnullptr;
9658
9659 std::vector<Constant*>Operands(I->getNumOperands());
9660
9661for (unsigned i = 0, e =I->getNumOperands(); i != e; ++i) {
9662Instruction *Operand = dyn_cast<Instruction>(I->getOperand(i));
9663if (!Operand) {
9664Operands[i] = dyn_cast<Constant>(I->getOperand(i));
9665if (!Operands[i])returnnullptr;
9666continue;
9667 }
9668Constant *C =EvaluateExpression(Operand, L, Vals,DL, TLI);
9669 Vals[Operand] =C;
9670if (!C)returnnullptr;
9671Operands[i] =C;
9672 }
9673
9674returnConstantFoldInstOperands(I,Operands,DL, TLI,
9675/*AllowNonDeterministic=*/false);
9676}
9677
9678
9679// If every incoming value to PN except the one for BB is a specific Constant,
9680// return that, else return nullptr.
9681staticConstant *getOtherIncomingValue(PHINode *PN,BasicBlock *BB) {
9682Constant *IncomingVal =nullptr;
9683
9684for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
9685if (PN->getIncomingBlock(i) == BB)
9686continue;
9687
9688auto *CurrentVal = dyn_cast<Constant>(PN->getIncomingValue(i));
9689if (!CurrentVal)
9690returnnullptr;
9691
9692if (IncomingVal != CurrentVal) {
9693if (IncomingVal)
9694returnnullptr;
9695 IncomingVal = CurrentVal;
9696 }
9697 }
9698
9699return IncomingVal;
9700}
9701
9702/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
9703/// in the header of its containing loop, we know the loop executes a
9704/// constant number of times, and the PHI node is just a recurrence
9705/// involving constants, fold it.
9706Constant *
9707ScalarEvolution::getConstantEvolutionLoopExitValue(PHINode *PN,
9708constAPInt &BEs,
9709constLoop *L) {
9710auto [I,Inserted] = ConstantEvolutionLoopExitValue.try_emplace(PN);
9711if (!Inserted)
9712returnI->second;
9713
9714if (BEs.ugt(MaxBruteForceIterations))
9715returnnullptr;// Not going to evaluate it.
9716
9717Constant *&RetVal =I->second;
9718
9719DenseMap<Instruction *, Constant *> CurrentIterVals;
9720BasicBlock *Header =L->getHeader();
9721assert(PN->getParent() == Header &&"Can't evaluate PHI not in loop header!");
9722
9723BasicBlock *Latch =L->getLoopLatch();
9724if (!Latch)
9725returnnullptr;
9726
9727for (PHINode &PHI : Header->phis()) {
9728if (auto *StartCST =getOtherIncomingValue(&PHI, Latch))
9729 CurrentIterVals[&PHI] = StartCST;
9730 }
9731if (!CurrentIterVals.count(PN))
9732return RetVal =nullptr;
9733
9734Value *BEValue = PN->getIncomingValueForBlock(Latch);
9735
9736// Execute the loop symbolically to determine the exit value.
9737assert(BEs.getActiveBits() < CHAR_BIT *sizeof(unsigned) &&
9738"BEs is <= MaxBruteForceIterations which is an 'unsigned'!");
9739
9740unsigned NumIterations = BEs.getZExtValue();// must be in range
9741unsigned IterationNum = 0;
9742constDataLayout &DL =getDataLayout();
9743for (; ; ++IterationNum) {
9744if (IterationNum == NumIterations)
9745return RetVal = CurrentIterVals[PN];// Got exit value!
9746
9747// Compute the value of the PHIs for the next iteration.
9748// EvaluateExpression adds non-phi values to the CurrentIterVals map.
9749DenseMap<Instruction *, Constant *> NextIterVals;
9750Constant *NextPHI =
9751EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
9752if (!NextPHI)
9753returnnullptr;// Couldn't evaluate!
9754 NextIterVals[PN] = NextPHI;
9755
9756bool StoppedEvolving = NextPHI == CurrentIterVals[PN];
9757
9758// Also evaluate the other PHI nodes. However, we don't get to stop if we
9759// cease to be able to evaluate one of them or if they stop evolving,
9760// because that doesn't necessarily prevent us from computing PN.
9761SmallVector<std::pair<PHINode *, Constant *>, 8> PHIsToCompute;
9762for (constauto &I : CurrentIterVals) {
9763PHINode *PHI = dyn_cast<PHINode>(I.first);
9764if (!PHI ||PHI == PN ||PHI->getParent() != Header)continue;
9765 PHIsToCompute.emplace_back(PHI,I.second);
9766 }
9767// We use two distinct loops because EvaluateExpression may invalidate any
9768// iterators into CurrentIterVals.
9769for (constauto &I : PHIsToCompute) {
9770PHINode *PHI =I.first;
9771Constant *&NextPHI = NextIterVals[PHI];
9772if (!NextPHI) {// Not already computed.
9773Value *BEValue =PHI->getIncomingValueForBlock(Latch);
9774 NextPHI =EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
9775 }
9776if (NextPHI !=I.second)
9777 StoppedEvolving =false;
9778 }
9779
9780// If all entries in CurrentIterVals == NextIterVals then we can stop
9781// iterating, the loop can't continue to change.
9782if (StoppedEvolving)
9783return RetVal = CurrentIterVals[PN];
9784
9785 CurrentIterVals.swap(NextIterVals);
9786 }
9787}
9788
9789constSCEV *ScalarEvolution::computeExitCountExhaustively(constLoop *L,
9790Value *Cond,
9791bool ExitWhen) {
9792PHINode *PN =getConstantEvolvingPHI(Cond, L);
9793if (!PN)returngetCouldNotCompute();
9794
9795// If the loop is canonicalized, the PHI will have exactly two entries.
9796// That's the only form we support here.
9797if (PN->getNumIncomingValues() != 2)returngetCouldNotCompute();
9798
9799DenseMap<Instruction *, Constant *> CurrentIterVals;
9800BasicBlock *Header =L->getHeader();
9801assert(PN->getParent() == Header &&"Can't evaluate PHI not in loop header!");
9802
9803BasicBlock *Latch =L->getLoopLatch();
9804assert(Latch &&"Should follow from NumIncomingValues == 2!");
9805
9806for (PHINode &PHI : Header->phis()) {
9807if (auto *StartCST =getOtherIncomingValue(&PHI, Latch))
9808 CurrentIterVals[&PHI] = StartCST;
9809 }
9810if (!CurrentIterVals.count(PN))
9811returngetCouldNotCompute();
9812
9813// Okay, we find a PHI node that defines the trip count of this loop. Execute
9814// the loop symbolically to determine when the condition gets a value of
9815// "ExitWhen".
9816unsigned MaxIterations =MaxBruteForceIterations;// Limit analysis.
9817constDataLayout &DL =getDataLayout();
9818for (unsigned IterationNum = 0; IterationNum != MaxIterations;++IterationNum){
9819auto *CondVal = dyn_cast_or_null<ConstantInt>(
9820EvaluateExpression(Cond, L, CurrentIterVals, DL, &TLI));
9821
9822// Couldn't symbolically evaluate.
9823if (!CondVal)returngetCouldNotCompute();
9824
9825if (CondVal->getValue() ==uint64_t(ExitWhen)) {
9826 ++NumBruteForceTripCountsComputed;
9827returngetConstant(Type::getInt32Ty(getContext()), IterationNum);
9828 }
9829
9830// Update all the PHI nodes for the next iteration.
9831DenseMap<Instruction *, Constant *> NextIterVals;
9832
9833// Create a list of which PHIs we need to compute. We want to do this before
9834// calling EvaluateExpression on them because that may invalidate iterators
9835// into CurrentIterVals.
9836SmallVector<PHINode *, 8> PHIsToCompute;
9837for (constauto &I : CurrentIterVals) {
9838PHINode *PHI = dyn_cast<PHINode>(I.first);
9839if (!PHI ||PHI->getParent() != Header)continue;
9840 PHIsToCompute.push_back(PHI);
9841 }
9842for (PHINode *PHI : PHIsToCompute) {
9843Constant *&NextPHI = NextIterVals[PHI];
9844if (NextPHI)continue;// Already computed!
9845
9846Value *BEValue =PHI->getIncomingValueForBlock(Latch);
9847 NextPHI =EvaluateExpression(BEValue, L, CurrentIterVals, DL, &TLI);
9848 }
9849 CurrentIterVals.swap(NextIterVals);
9850 }
9851
9852// Too many iterations were needed to evaluate.
9853returngetCouldNotCompute();
9854}
9855
9856constSCEV *ScalarEvolution::getSCEVAtScope(constSCEV *V,constLoop *L) {
9857SmallVector<std::pair<const Loop *, const SCEV *>, 2> &Values =
9858 ValuesAtScopes[V];
9859// Check to see if we've folded this expression at this loop before.
9860for (auto &LS : Values)
9861if (LS.first == L)
9862return LS.second ? LS.second : V;
9863
9864 Values.emplace_back(L,nullptr);
9865
9866// Otherwise compute it.
9867constSCEV *C = computeSCEVAtScope(V, L);
9868for (auto &LS :reverse(ValuesAtScopes[V]))
9869if (LS.first == L) {
9870 LS.second =C;
9871if (!isa<SCEVConstant>(C))
9872 ValuesAtScopesUsers[C].push_back({L, V});
9873break;
9874 }
9875returnC;
9876}
9877
9878/// This builds up a Constant using the ConstantExpr interface. That way, we
9879/// will return Constants for objects which aren't represented by a
9880/// SCEVConstant, because SCEVConstant is restricted to ConstantInt.
9881/// Returns NULL if the SCEV isn't representable as a Constant.
9882staticConstant *BuildConstantFromSCEV(constSCEV *V) {
9883switch (V->getSCEVType()) {
9884casescCouldNotCompute:
9885casescAddRecExpr:
9886casescVScale:
9887returnnullptr;
9888casescConstant:
9889return cast<SCEVConstant>(V)->getValue();
9890casescUnknown:
9891return dyn_cast<Constant>(cast<SCEVUnknown>(V)->getValue());
9892casescPtrToInt: {
9893constSCEVPtrToIntExpr *P2I = cast<SCEVPtrToIntExpr>(V);
9894if (Constant *CastOp =BuildConstantFromSCEV(P2I->getOperand()))
9895returnConstantExpr::getPtrToInt(CastOp, P2I->getType());
9896
9897returnnullptr;
9898 }
9899casescTruncate: {
9900constSCEVTruncateExpr *ST = cast<SCEVTruncateExpr>(V);
9901if (Constant *CastOp =BuildConstantFromSCEV(ST->getOperand()))
9902returnConstantExpr::getTrunc(CastOp, ST->getType());
9903returnnullptr;
9904 }
9905casescAddExpr: {
9906constSCEVAddExpr *SA = cast<SCEVAddExpr>(V);
9907Constant *C =nullptr;
9908for (constSCEV *Op : SA->operands()) {
9909Constant *OpC =BuildConstantFromSCEV(Op);
9910if (!OpC)
9911returnnullptr;
9912if (!C) {
9913C = OpC;
9914continue;
9915 }
9916assert(!C->getType()->isPointerTy() &&
9917"Can only have one pointer, and it must be last");
9918if (OpC->getType()->isPointerTy()) {
9919// The offsets have been converted to bytes. We can add bytes using
9920// an i8 GEP.
9921C =ConstantExpr::getGetElementPtr(Type::getInt8Ty(C->getContext()),
9922 OpC,C);
9923 }else {
9924C =ConstantExpr::getAdd(C, OpC);
9925 }
9926 }
9927returnC;
9928 }
9929casescMulExpr:
9930casescSignExtend:
9931casescZeroExtend:
9932casescUDivExpr:
9933casescSMaxExpr:
9934casescUMaxExpr:
9935casescSMinExpr:
9936casescUMinExpr:
9937casescSequentialUMinExpr:
9938returnnullptr;
9939 }
9940llvm_unreachable("Unknown SCEV kind!");
9941}
9942
9943constSCEV *
9944ScalarEvolution::getWithOperands(constSCEV *S,
9945SmallVectorImpl<const SCEV *> &NewOps) {
9946switch (S->getSCEVType()) {
9947casescTruncate:
9948casescZeroExtend:
9949casescSignExtend:
9950casescPtrToInt:
9951returngetCastExpr(S->getSCEVType(), NewOps[0], S->getType());
9952casescAddRecExpr: {
9953auto *AddRec = cast<SCEVAddRecExpr>(S);
9954returngetAddRecExpr(NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags());
9955 }
9956casescAddExpr:
9957returngetAddExpr(NewOps, cast<SCEVAddExpr>(S)->getNoWrapFlags());
9958casescMulExpr:
9959returngetMulExpr(NewOps, cast<SCEVMulExpr>(S)->getNoWrapFlags());
9960casescUDivExpr:
9961returngetUDivExpr(NewOps[0], NewOps[1]);
9962casescUMaxExpr:
9963casescSMaxExpr:
9964casescUMinExpr:
9965casescSMinExpr:
9966returngetMinMaxExpr(S->getSCEVType(), NewOps);
9967casescSequentialUMinExpr:
9968returngetSequentialMinMaxExpr(S->getSCEVType(), NewOps);
9969casescConstant:
9970casescVScale:
9971casescUnknown:
9972return S;
9973casescCouldNotCompute:
9974llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
9975 }
9976llvm_unreachable("Unknown SCEV kind!");
9977}
9978
9979constSCEV *ScalarEvolution::computeSCEVAtScope(constSCEV *V,constLoop *L) {
9980switch (V->getSCEVType()) {
9981casescConstant:
9982casescVScale:
9983returnV;
9984casescAddRecExpr: {
9985// If this is a loop recurrence for a loop that does not contain L, then we
9986// are dealing with the final value computed by the loop.
9987constSCEVAddRecExpr *AddRec = cast<SCEVAddRecExpr>(V);
9988// First, attempt to evaluate each operand.
9989// Avoid performing the look-up in the common case where the specified
9990// expression has no loop-variant portions.
9991for (unsigned i = 0, e = AddRec->getNumOperands(); i != e; ++i) {
9992constSCEV *OpAtScope =getSCEVAtScope(AddRec->getOperand(i), L);
9993if (OpAtScope == AddRec->getOperand(i))
9994continue;
9995
9996// Okay, at least one of these operands is loop variant but might be
9997// foldable. Build a new instance of the folded commutative expression.
9998SmallVector<const SCEV *, 8> NewOps;
9999 NewOps.reserve(AddRec->getNumOperands());
10000append_range(NewOps, AddRec->operands().take_front(i));
10001 NewOps.push_back(OpAtScope);
10002for (++i; i !=e; ++i)
10003 NewOps.push_back(getSCEVAtScope(AddRec->getOperand(i), L));
10004
10005constSCEV *FoldedRec =getAddRecExpr(
10006 NewOps, AddRec->getLoop(), AddRec->getNoWrapFlags(SCEV::FlagNW));
10007 AddRec = dyn_cast<SCEVAddRecExpr>(FoldedRec);
10008// The addrec may be folded to a nonrecurrence, for example, if the
10009// induction variable is multiplied by zero after constant folding. Go
10010// ahead and return the folded value.
10011if (!AddRec)
10012return FoldedRec;
10013break;
10014 }
10015
10016// If the scope is outside the addrec's loop, evaluate it by using the
10017// loop exit value of the addrec.
10018if (!AddRec->getLoop()->contains(L)) {
10019// To evaluate this recurrence, we need to know how many times the AddRec
10020// loop iterates. Compute this now.
10021constSCEV *BackedgeTakenCount =getBackedgeTakenCount(AddRec->getLoop());
10022if (BackedgeTakenCount ==getCouldNotCompute())
10023return AddRec;
10024
10025// Then, evaluate the AddRec.
10026return AddRec->evaluateAtIteration(BackedgeTakenCount, *this);
10027 }
10028
10029return AddRec;
10030 }
10031casescTruncate:
10032casescZeroExtend:
10033casescSignExtend:
10034casescPtrToInt:
10035casescAddExpr:
10036casescMulExpr:
10037casescUDivExpr:
10038casescUMaxExpr:
10039casescSMaxExpr:
10040casescUMinExpr:
10041casescSMinExpr:
10042casescSequentialUMinExpr: {
10043ArrayRef<const SCEV *> Ops =V->operands();
10044// Avoid performing the look-up in the common case where the specified
10045// expression has no loop-variant portions.
10046for (unsigned i = 0, e = Ops.size(); i != e; ++i) {
10047constSCEV *OpAtScope =getSCEVAtScope(Ops[i], L);
10048if (OpAtScope != Ops[i]) {
10049// Okay, at least one of these operands is loop variant but might be
10050// foldable. Build a new instance of the folded commutative expression.
10051SmallVector<const SCEV *, 8> NewOps;
10052 NewOps.reserve(Ops.size());
10053append_range(NewOps, Ops.take_front(i));
10054 NewOps.push_back(OpAtScope);
10055
10056for (++i; i !=e; ++i) {
10057 OpAtScope =getSCEVAtScope(Ops[i], L);
10058 NewOps.push_back(OpAtScope);
10059 }
10060
10061return getWithOperands(V, NewOps);
10062 }
10063 }
10064// If we got here, all operands are loop invariant.
10065returnV;
10066 }
10067casescUnknown: {
10068// If this instruction is evolved from a constant-evolving PHI, compute the
10069// exit value from the loop without using SCEVs.
10070constSCEVUnknown *SU = cast<SCEVUnknown>(V);
10071Instruction *I = dyn_cast<Instruction>(SU->getValue());
10072if (!I)
10073returnV;// This is some other type of SCEVUnknown, just return it.
10074
10075if (PHINode *PN = dyn_cast<PHINode>(I)) {
10076constLoop *CurrLoop = this->LI[I->getParent()];
10077// Looking for loop exit value.
10078if (CurrLoop && CurrLoop->getParentLoop() == L &&
10079 PN->getParent() == CurrLoop->getHeader()) {
10080// Okay, there is no closed form solution for the PHI node. Check
10081// to see if the loop that contains it has a known backedge-taken
10082// count. If so, we may be able to force computation of the exit
10083// value.
10084constSCEV *BackedgeTakenCount =getBackedgeTakenCount(CurrLoop);
10085// This trivial case can show up in some degenerate cases where
10086// the incoming IR has not yet been fully simplified.
10087if (BackedgeTakenCount->isZero()) {
10088Value *InitValue =nullptr;
10089bool MultipleInitValues =false;
10090for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
10091if (!CurrLoop->contains(PN->getIncomingBlock(i))) {
10092if (!InitValue)
10093 InitValue = PN->getIncomingValue(i);
10094elseif (InitValue != PN->getIncomingValue(i)) {
10095 MultipleInitValues =true;
10096break;
10097 }
10098 }
10099 }
10100if (!MultipleInitValues && InitValue)
10101returngetSCEV(InitValue);
10102 }
10103// Do we have a loop invariant value flowing around the backedge
10104// for a loop which must execute the backedge?
10105if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) &&
10106isKnownNonZero(BackedgeTakenCount) &&
10107 PN->getNumIncomingValues() == 2) {
10108
10109unsigned InLoopPred =
10110 CurrLoop->contains(PN->getIncomingBlock(0)) ? 0 : 1;
10111Value *BackedgeVal = PN->getIncomingValue(InLoopPred);
10112if (CurrLoop->isLoopInvariant(BackedgeVal))
10113returngetSCEV(BackedgeVal);
10114 }
10115if (auto *BTCC = dyn_cast<SCEVConstant>(BackedgeTakenCount)) {
10116// Okay, we know how many times the containing loop executes. If
10117// this is a constant evolving PHI node, get the final value at
10118// the specified iteration number.
10119Constant *RV =
10120 getConstantEvolutionLoopExitValue(PN, BTCC->getAPInt(), CurrLoop);
10121if (RV)
10122returngetSCEV(RV);
10123 }
10124 }
10125 }
10126
10127// Okay, this is an expression that we cannot symbolically evaluate
10128// into a SCEV. Check to see if it's possible to symbolically evaluate
10129// the arguments into constants, and if so, try to constant propagate the
10130// result. This is particularly useful for computing loop exit values.
10131if (!CanConstantFold(I))
10132returnV;// This is some other type of SCEVUnknown, just return it.
10133
10134SmallVector<Constant *, 4>Operands;
10135Operands.reserve(I->getNumOperands());
10136bool MadeImprovement =false;
10137for (Value *Op :I->operands()) {
10138if (Constant *C = dyn_cast<Constant>(Op)) {
10139Operands.push_back(C);
10140continue;
10141 }
10142
10143// If any of the operands is non-constant and if they are
10144// non-integer and non-pointer, don't even try to analyze them
10145// with scev techniques.
10146if (!isSCEVable(Op->getType()))
10147returnV;
10148
10149constSCEV *OrigV =getSCEV(Op);
10150constSCEV *OpV =getSCEVAtScope(OrigV, L);
10151 MadeImprovement |= OrigV != OpV;
10152
10153Constant *C =BuildConstantFromSCEV(OpV);
10154if (!C)
10155returnV;
10156assert(C->getType() ==Op->getType() &&"Type mismatch");
10157Operands.push_back(C);
10158 }
10159
10160// Check to see if getSCEVAtScope actually made an improvement.
10161if (!MadeImprovement)
10162returnV;// This is some other type of SCEVUnknown, just return it.
10163
10164Constant *C =nullptr;
10165constDataLayout &DL =getDataLayout();
10166C =ConstantFoldInstOperands(I,Operands, DL, &TLI,
10167/*AllowNonDeterministic=*/false);
10168if (!C)
10169returnV;
10170returngetSCEV(C);
10171 }
10172casescCouldNotCompute:
10173llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
10174 }
10175llvm_unreachable("Unknown SCEV type!");
10176}
10177
10178constSCEV *ScalarEvolution::getSCEVAtScope(Value *V,constLoop *L) {
10179returngetSCEVAtScope(getSCEV(V), L);
10180}
10181
10182constSCEV *ScalarEvolution::stripInjectiveFunctions(constSCEV *S) const{
10183if (constSCEVZeroExtendExpr *ZExt = dyn_cast<SCEVZeroExtendExpr>(S))
10184return stripInjectiveFunctions(ZExt->getOperand());
10185if (constSCEVSignExtendExpr *SExt = dyn_cast<SCEVSignExtendExpr>(S))
10186return stripInjectiveFunctions(SExt->getOperand());
10187return S;
10188}
10189
10190/// Finds the minimum unsigned root of the following equation:
10191///
10192/// A * X = B (mod N)
10193///
10194/// where N = 2^BW and BW is the common bit width of A and B. The signedness of
10195/// A and B isn't important.
10196///
10197/// If the equation does not have a solution, SCEVCouldNotCompute is returned.
10198staticconstSCEV *
10199SolveLinEquationWithOverflow(constAPInt &A,constSCEV *B,
10200SmallVectorImpl<const SCEVPredicate *> *Predicates,
10201
10202ScalarEvolution &SE) {
10203uint32_t BW =A.getBitWidth();
10204assert(BW == SE.getTypeSizeInBits(B->getType()));
10205assert(A != 0 &&"A must be non-zero.");
10206
10207// 1. D = gcd(A, N)
10208//
10209// The gcd of A and N may have only one prime factor: 2. The number of
10210// trailing zeros in A is its multiplicity
10211uint32_t Mult2 =A.countr_zero();
10212// D = 2^Mult2
10213
10214// 2. Check if B is divisible by D.
10215//
10216// B is divisible by D if and only if the multiplicity of prime factor 2 for B
10217// is not less than multiplicity of this prime factor for D.
10218if (SE.getMinTrailingZeros(B) < Mult2) {
10219// Check if we can prove there's no remainder using URem.
10220constSCEV *URem =
10221 SE.getURemExpr(B, SE.getConstant(APInt::getOneBitSet(BW, Mult2)));
10222constSCEV *Zero = SE.getZero(B->getType());
10223if (!SE.isKnownPredicate(CmpInst::ICMP_EQ, URem, Zero)) {
10224// Try to add a predicate ensuring B is a multiple of 1 << Mult2.
10225if (!Predicates)
10226return SE.getCouldNotCompute();
10227
10228// Avoid adding a predicate that is known to be false.
10229if (SE.isKnownPredicate(CmpInst::ICMP_NE, URem, Zero))
10230return SE.getCouldNotCompute();
10231 Predicates->push_back(SE.getEqualPredicate(URem, Zero));
10232 }
10233 }
10234
10235// 3. Compute I: the multiplicative inverse of (A / D) in arithmetic
10236// modulo (N / D).
10237//
10238// If D == 1, (N / D) == N == 2^BW, so we need one extra bit to represent
10239// (N / D) in general. The inverse itself always fits into BW bits, though,
10240// so we immediately truncate it.
10241APInt AD =A.lshr(Mult2).trunc(BW - Mult2);// AD = A / D
10242APIntI = AD.multiplicativeInverse().zext(BW);
10243
10244// 4. Compute the minimum unsigned root of the equation:
10245// I * (B / D) mod (N / D)
10246// To simplify the computation, we factor out the divide by D:
10247// (I * B mod N) / D
10248constSCEV *D = SE.getConstant(APInt::getOneBitSet(BW, Mult2));
10249return SE.getUDivExactExpr(SE.getMulExpr(B, SE.getConstant(I)),D);
10250}
10251
10252/// For a given quadratic addrec, generate coefficients of the corresponding
10253/// quadratic equation, multiplied by a common value to ensure that they are
10254/// integers.
10255/// The returned value is a tuple { A, B, C, M, BitWidth }, where
10256/// Ax^2 + Bx + C is the quadratic function, M is the value that A, B and C
10257/// were multiplied by, and BitWidth is the bit width of the original addrec
10258/// coefficients.
10259/// This function returns std::nullopt if the addrec coefficients are not
10260/// compile- time constants.
10261static std::optional<std::tuple<APInt, APInt, APInt, APInt, unsigned>>
10262GetQuadraticEquation(constSCEVAddRecExpr *AddRec) {
10263assert(AddRec->getNumOperands() == 3 &&"This is not a quadratic chrec!");
10264constSCEVConstant *LC = dyn_cast<SCEVConstant>(AddRec->getOperand(0));
10265constSCEVConstant *MC = dyn_cast<SCEVConstant>(AddRec->getOperand(1));
10266constSCEVConstant *NC = dyn_cast<SCEVConstant>(AddRec->getOperand(2));
10267LLVM_DEBUG(dbgs() << __func__ <<": analyzing quadratic addrec: "
10268 << *AddRec <<'\n');
10269
10270// We currently can only solve this if the coefficients are constants.
10271if (!LC || !MC || !NC) {
10272LLVM_DEBUG(dbgs() << __func__ <<": coefficients are not constant\n");
10273return std::nullopt;
10274 }
10275
10276APInt L = LC->getAPInt();
10277APInt M = MC->getAPInt();
10278APIntN =NC->getAPInt();
10279assert(!N.isZero() &&"This is not a quadratic addrec");
10280
10281unsignedBitWidth = LC->getAPInt().getBitWidth();
10282unsigned NewWidth =BitWidth + 1;
10283LLVM_DEBUG(dbgs() << __func__ <<": addrec coeff bw: "
10284 <<BitWidth <<'\n');
10285// The sign-extension (as opposed to a zero-extension) here matches the
10286// extension used in SolveQuadraticEquationWrap (with the same motivation).
10287N =N.sext(NewWidth);
10288 M = M.sext(NewWidth);
10289 L = L.sext(NewWidth);
10290
10291// The increments are M, M+N, M+2N, ..., so the accumulated values are
10292// L+M, (L+M)+(M+N), (L+M)+(M+N)+(M+2N), ..., that is,
10293// L+M, L+2M+N, L+3M+3N, ...
10294// After n iterations the accumulated value Acc is L + nM + n(n-1)/2 N.
10295//
10296// The equation Acc = 0 is then
10297// L + nM + n(n-1)/2 N = 0, or 2L + 2M n + n(n-1) N = 0.
10298// In a quadratic form it becomes:
10299// N n^2 + (2M-N) n + 2L = 0.
10300
10301APIntA =N;
10302APIntB = 2 * M -A;
10303APIntC = 2 * L;
10304APIntT =APInt(NewWidth, 2);
10305LLVM_DEBUG(dbgs() << __func__ <<": equation " <<A <<"x^2 + " <<B
10306 <<"x + " <<C <<", coeff bw: " << NewWidth
10307 <<", multiplied by " <<T <<'\n');
10308return std::make_tuple(A,B,C,T,BitWidth);
10309}
10310
10311/// Helper function to compare optional APInts:
10312/// (a) if X and Y both exist, return min(X, Y),
10313/// (b) if neither X nor Y exist, return std::nullopt,
10314/// (c) if exactly one of X and Y exists, return that value.
10315static std::optional<APInt>MinOptional(std::optional<APInt>X,
10316 std::optional<APInt>Y) {
10317if (X &&Y) {
10318unsigned W = std::max(X->getBitWidth(),Y->getBitWidth());
10319APInt XW =X->sext(W);
10320APInt YW =Y->sext(W);
10321return XW.slt(YW) ? *X : *Y;
10322 }
10323if (!X && !Y)
10324return std::nullopt;
10325returnX ? *X : *Y;
10326}
10327
10328/// Helper function to truncate an optional APInt to a given BitWidth.
10329/// When solving addrec-related equations, it is preferable to return a value
10330/// that has the same bit width as the original addrec's coefficients. If the
10331/// solution fits in the original bit width, truncate it (except for i1).
10332/// Returning a value of a different bit width may inhibit some optimizations.
10333///
10334/// In general, a solution to a quadratic equation generated from an addrec
10335/// may require BW+1 bits, where BW is the bit width of the addrec's
10336/// coefficients. The reason is that the coefficients of the quadratic
10337/// equation are BW+1 bits wide (to avoid truncation when converting from
10338/// the addrec to the equation).
10339static std::optional<APInt>TruncIfPossible(std::optional<APInt>X,
10340unsignedBitWidth) {
10341if (!X)
10342return std::nullopt;
10343unsigned W =X->getBitWidth();
10344if (BitWidth > 1 && BitWidth < W && X->isIntN(BitWidth))
10345returnX->trunc(BitWidth);
10346returnX;
10347}
10348
10349/// Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n
10350/// iterations. The values L, M, N are assumed to be signed, and they
10351/// should all have the same bit widths.
10352/// Find the least n >= 0 such that c(n) = 0 in the arithmetic modulo 2^BW,
10353/// where BW is the bit width of the addrec's coefficients.
10354/// If the calculated value is a BW-bit integer (for BW > 1), it will be
10355/// returned as such, otherwise the bit width of the returned value may
10356/// be greater than BW.
10357///
10358/// This function returns std::nullopt if
10359/// (a) the addrec coefficients are not constant, or
10360/// (b) SolveQuadraticEquationWrap was unable to find a solution. For cases
10361/// like x^2 = 5, no integer solutions exist, in other cases an integer
10362/// solution may exist, but SolveQuadraticEquationWrap may fail to find it.
10363static std::optional<APInt>
10364SolveQuadraticAddRecExact(constSCEVAddRecExpr *AddRec,ScalarEvolution &SE) {
10365APIntA,B,C, M;
10366unsignedBitWidth;
10367autoT =GetQuadraticEquation(AddRec);
10368if (!T)
10369return std::nullopt;
10370
10371 std::tie(A,B,C, M,BitWidth) = *T;
10372LLVM_DEBUG(dbgs() << __func__ <<": solving for unsigned overflow\n");
10373 std::optional<APInt>X =
10374APIntOps::SolveQuadraticEquationWrap(A,B,C,BitWidth + 1);
10375if (!X)
10376return std::nullopt;
10377
10378ConstantInt *CX = ConstantInt::get(SE.getContext(), *X);
10379ConstantInt *V =EvaluateConstantChrecAtConstant(AddRec, CX, SE);
10380if (!V->isZero())
10381return std::nullopt;
10382
10383returnTruncIfPossible(X,BitWidth);
10384}
10385
10386/// Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n
10387/// iterations. The values M, N are assumed to be signed, and they
10388/// should all have the same bit widths.
10389/// Find the least n such that c(n) does not belong to the given range,
10390/// while c(n-1) does.
10391///
10392/// This function returns std::nullopt if
10393/// (a) the addrec coefficients are not constant, or
10394/// (b) SolveQuadraticEquationWrap was unable to find a solution for the
10395/// bounds of the range.
10396static std::optional<APInt>
10397SolveQuadraticAddRecRange(constSCEVAddRecExpr *AddRec,
10398constConstantRange &Range,ScalarEvolution &SE) {
10399assert(AddRec->getOperand(0)->isZero() &&
10400"Starting value of addrec should be 0");
10401LLVM_DEBUG(dbgs() << __func__ <<": solving boundary crossing for range "
10402 <<Range <<", addrec " << *AddRec <<'\n');
10403// This case is handled in getNumIterationsInRange. Here we can assume that
10404// we start in the range.
10405assert(Range.contains(APInt(SE.getTypeSizeInBits(AddRec->getType()), 0)) &&
10406"Addrec's initial value should be in range");
10407
10408APIntA,B,C, M;
10409unsignedBitWidth;
10410autoT =GetQuadraticEquation(AddRec);
10411if (!T)
10412return std::nullopt;
10413
10414// Be careful about the return value: there can be two reasons for not
10415// returning an actual number. First, if no solutions to the equations
10416// were found, and second, if the solutions don't leave the given range.
10417// The first case means that the actual solution is "unknown", the second
10418// means that it's known, but not valid. If the solution is unknown, we
10419// cannot make any conclusions.
10420// Return a pair: the optional solution and a flag indicating if the
10421// solution was found.
10422auto SolveForBoundary =
10423 [&](APInt Bound) -> std::pair<std::optional<APInt>,bool> {
10424// Solve for signed overflow and unsigned overflow, pick the lower
10425// solution.
10426LLVM_DEBUG(dbgs() <<"SolveQuadraticAddRecRange: checking boundary "
10427 << Bound <<" (before multiplying by " << M <<")\n");
10428 Bound *= M;// The quadratic equation multiplier.
10429
10430 std::optional<APInt> SO;
10431if (BitWidth > 1) {
10432LLVM_DEBUG(dbgs() <<"SolveQuadraticAddRecRange: solving for "
10433"signed overflow\n");
10434 SO =APIntOps::SolveQuadraticEquationWrap(A,B, -Bound,BitWidth);
10435 }
10436LLVM_DEBUG(dbgs() <<"SolveQuadraticAddRecRange: solving for "
10437"unsigned overflow\n");
10438 std::optional<APInt> UO =
10439APIntOps::SolveQuadraticEquationWrap(A,B, -Bound,BitWidth + 1);
10440
10441auto LeavesRange = [&] (constAPInt &X) {
10442ConstantInt *C0 = ConstantInt::get(SE.getContext(),X);
10443ConstantInt *V0 =EvaluateConstantChrecAtConstant(AddRec, C0, SE);
10444if (Range.contains(V0->getValue()))
10445returnfalse;
10446// X should be at least 1, so X-1 is non-negative.
10447ConstantInt *C1 = ConstantInt::get(SE.getContext(),X-1);
10448ConstantInt *V1 =EvaluateConstantChrecAtConstant(AddRec, C1, SE);
10449if (Range.contains(V1->getValue()))
10450returntrue;
10451returnfalse;
10452 };
10453
10454// If SolveQuadraticEquationWrap returns std::nullopt, it means that there
10455// can be a solution, but the function failed to find it. We cannot treat it
10456// as "no solution".
10457if (!SO || !UO)
10458return {std::nullopt,false};
10459
10460// Check the smaller value first to see if it leaves the range.
10461// At this point, both SO and UO must have values.
10462 std::optional<APInt> Min =MinOptional(SO, UO);
10463if (LeavesRange(*Min))
10464return { Min,true };
10465 std::optional<APInt> Max = Min == SO ? UO : SO;
10466if (LeavesRange(*Max))
10467return { Max,true };
10468
10469// Solutions were found, but were eliminated, hence the "true".
10470return {std::nullopt,true};
10471 };
10472
10473 std::tie(A,B,C, M,BitWidth) = *T;
10474// Lower bound is inclusive, subtract 1 to represent the exiting value.
10475APIntLower =Range.getLower().sext(A.getBitWidth()) - 1;
10476APIntUpper =Range.getUpper().sext(A.getBitWidth());
10477auto SL = SolveForBoundary(Lower);
10478auto SU = SolveForBoundary(Upper);
10479// If any of the solutions was unknown, no meaninigful conclusions can
10480// be made.
10481if (!SL.second || !SU.second)
10482return std::nullopt;
10483
10484// Claim: The correct solution is not some value between Min and Max.
10485//
10486// Justification: Assuming that Min and Max are different values, one of
10487// them is when the first signed overflow happens, the other is when the
10488// first unsigned overflow happens. Crossing the range boundary is only
10489// possible via an overflow (treating 0 as a special case of it, modeling
10490// an overflow as crossing k*2^W for some k).
10491//
10492// The interesting case here is when Min was eliminated as an invalid
10493// solution, but Max was not. The argument is that if there was another
10494// overflow between Min and Max, it would also have been eliminated if
10495// it was considered.
10496//
10497// For a given boundary, it is possible to have two overflows of the same
10498// type (signed/unsigned) without having the other type in between: this
10499// can happen when the vertex of the parabola is between the iterations
10500// corresponding to the overflows. This is only possible when the two
10501// overflows cross k*2^W for the same k. In such case, if the second one
10502// left the range (and was the first one to do so), the first overflow
10503// would have to enter the range, which would mean that either we had left
10504// the range before or that we started outside of it. Both of these cases
10505// are contradictions.
10506//
10507// Claim: In the case where SolveForBoundary returns std::nullopt, the correct
10508// solution is not some value between the Max for this boundary and the
10509// Min of the other boundary.
10510//
10511// Justification: Assume that we had such Max_A and Min_B corresponding
10512// to range boundaries A and B and such that Max_A < Min_B. If there was
10513// a solution between Max_A and Min_B, it would have to be caused by an
10514// overflow corresponding to either A or B. It cannot correspond to B,
10515// since Min_B is the first occurrence of such an overflow. If it
10516// corresponded to A, it would have to be either a signed or an unsigned
10517// overflow that is larger than both eliminated overflows for A. But
10518// between the eliminated overflows and this overflow, the values would
10519// cover the entire value space, thus crossing the other boundary, which
10520// is a contradiction.
10521
10522returnTruncIfPossible(MinOptional(SL.first, SU.first),BitWidth);
10523}
10524
10525ScalarEvolution::ExitLimit ScalarEvolution::howFarToZero(constSCEV *V,
10526constLoop *L,
10527bool ControlsOnlyExit,
10528bool AllowPredicates) {
10529
10530// This is only used for loops with a "x != y" exit test. The exit condition
10531// is now expressed as a single expression, V = x-y. So the exit test is
10532// effectively V != 0. We know and take advantage of the fact that this
10533// expression only being used in a comparison by zero context.
10534
10535SmallVector<const SCEVPredicate *> Predicates;
10536// If the value is a constant
10537if (constSCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
10538// If the value is already zero, the branch will execute zero times.
10539if (C->getValue()->isZero())returnC;
10540returngetCouldNotCompute();// Otherwise it will loop infinitely.
10541 }
10542
10543constSCEVAddRecExpr *AddRec =
10544 dyn_cast<SCEVAddRecExpr>(stripInjectiveFunctions(V));
10545
10546if (!AddRec && AllowPredicates)
10547// Try to make this an AddRec using runtime tests, in the first X
10548// iterations of this loop, where X is the SCEV expression found by the
10549// algorithm below.
10550 AddRec =convertSCEVToAddRecWithPredicates(V, L, Predicates);
10551
10552if (!AddRec || AddRec->getLoop() != L)
10553returngetCouldNotCompute();
10554
10555// If this is a quadratic (3-term) AddRec {L,+,M,+,N}, find the roots of
10556// the quadratic equation to solve it.
10557if (AddRec->isQuadratic() && AddRec->getType()->isIntegerTy()) {
10558// We can only use this value if the chrec ends up with an exact zero
10559// value at this index. When solving for "X*X != 5", for example, we
10560// should not accept a root of 2.
10561if (auto S =SolveQuadraticAddRecExact(AddRec, *this)) {
10562constauto *R = cast<SCEVConstant>(getConstant(*S));
10563return ExitLimit(R, R, R,false, Predicates);
10564 }
10565returngetCouldNotCompute();
10566 }
10567
10568// Otherwise we can only handle this if it is affine.
10569if (!AddRec->isAffine())
10570returngetCouldNotCompute();
10571
10572// If this is an affine expression, the execution count of this branch is
10573// the minimum unsigned root of the following equation:
10574//
10575// Start + Step*N = 0 (mod 2^BW)
10576//
10577// equivalent to:
10578//
10579// Step*N = -Start (mod 2^BW)
10580//
10581// where BW is the common bit width of Start and Step.
10582
10583// Get the initial value for the loop.
10584constSCEV *Start =getSCEVAtScope(AddRec->getStart(),L->getParentLoop());
10585constSCEV *Step =getSCEVAtScope(AddRec->getOperand(1),L->getParentLoop());
10586
10587if (!isLoopInvariant(Step, L))
10588returngetCouldNotCompute();
10589
10590 LoopGuards Guards =LoopGuards::collect(L, *this);
10591// Specialize step for this loop so we get context sensitive facts below.
10592constSCEV *StepWLG =applyLoopGuards(Step, Guards);
10593
10594// For positive steps (counting up until unsigned overflow):
10595// N = -Start/Step (as unsigned)
10596// For negative steps (counting down to zero):
10597// N = Start/-Step
10598// First compute the unsigned distance from zero in the direction of Step.
10599bool CountDown =isKnownNegative(StepWLG);
10600if (!CountDown && !isKnownNonNegative(StepWLG))
10601returngetCouldNotCompute();
10602
10603constSCEV *Distance = CountDown ? Start :getNegativeSCEV(Start);
10604// Handle unitary steps, which cannot wraparound.
10605// 1*N = -Start; -1*N = Start (mod 2^BW), so:
10606// N = Distance (as unsigned)
10607
10608if (match(Step,m_CombineOr(m_scev_One(),m_scev_AllOnes()))) {
10609APInt MaxBECount =getUnsignedRangeMax(applyLoopGuards(Distance, Guards));
10610 MaxBECount =APIntOps::umin(MaxBECount,getUnsignedRangeMax(Distance));
10611
10612// When a loop like "for (int i = 0; i != n; ++i) { /* body */ }" is rotated,
10613// we end up with a loop whose backedge-taken count is n - 1. Detect this
10614// case, and see if we can improve the bound.
10615//
10616// Explicitly handling this here is necessary because getUnsignedRange
10617// isn't context-sensitive; it doesn't know that we only care about the
10618// range inside the loop.
10619constSCEV *Zero =getZero(Distance->getType());
10620constSCEV *One =getOne(Distance->getType());
10621constSCEV *DistancePlusOne =getAddExpr(Distance, One);
10622if (isLoopEntryGuardedByCond(L,ICmpInst::ICMP_NE, DistancePlusOne, Zero)) {
10623// If Distance + 1 doesn't overflow, we can compute the maximum distance
10624// as "unsigned_max(Distance + 1) - 1".
10625ConstantRange CR =getUnsignedRange(DistancePlusOne);
10626 MaxBECount =APIntOps::umin(MaxBECount, CR.getUnsignedMax() - 1);
10627 }
10628return ExitLimit(Distance,getConstant(MaxBECount), Distance,false,
10629 Predicates);
10630 }
10631
10632// If the condition controls loop exit (the loop exits only if the expression
10633// is true) and the addition is no-wrap we can use unsigned divide to
10634// compute the backedge count. In this case, the step may not divide the
10635// distance, but we don't care because if the condition is "missed" the loop
10636// will have undefined behavior due to wrapping.
10637if (ControlsOnlyExit && AddRec->hasNoSelfWrap() &&
10638loopHasNoAbnormalExits(AddRec->getLoop())) {
10639
10640// If the stride is zero, the loop must be infinite. In C++, most loops
10641// are finite by assumption, in which case the step being zero implies
10642// UB must execute if the loop is entered.
10643if (!loopIsFiniteByAssumption(L) && !isKnownNonZero(StepWLG))
10644returngetCouldNotCompute();
10645
10646constSCEV *Exact =
10647getUDivExpr(Distance, CountDown ?getNegativeSCEV(Step) : Step);
10648constSCEV *ConstantMax =getCouldNotCompute();
10649if (Exact !=getCouldNotCompute()) {
10650APInt MaxInt =getUnsignedRangeMax(applyLoopGuards(Exact, Guards));
10651 ConstantMax =
10652getConstant(APIntOps::umin(MaxInt,getUnsignedRangeMax(Exact)));
10653 }
10654constSCEV *SymbolicMax =
10655 isa<SCEVCouldNotCompute>(Exact) ? ConstantMax :Exact;
10656return ExitLimit(Exact, ConstantMax, SymbolicMax,false, Predicates);
10657 }
10658
10659// Solve the general equation.
10660constSCEVConstant *StepC = dyn_cast<SCEVConstant>(Step);
10661if (!StepC || StepC->getValue()->isZero())
10662returngetCouldNotCompute();
10663constSCEV *E =SolveLinEquationWithOverflow(
10664 StepC->getAPInt(),getNegativeSCEV(Start),
10665 AllowPredicates ? &Predicates :nullptr, *this);
10666
10667constSCEV *M = E;
10668if (E !=getCouldNotCompute()) {
10669APInt MaxWithGuards =getUnsignedRangeMax(applyLoopGuards(E, Guards));
10670M =getConstant(APIntOps::umin(MaxWithGuards,getUnsignedRangeMax(E)));
10671 }
10672auto *S = isa<SCEVCouldNotCompute>(E) ?M : E;
10673return ExitLimit(E, M, S,false, Predicates);
10674}
10675
10676ScalarEvolution::ExitLimit
10677ScalarEvolution::howFarToNonZero(constSCEV *V,constLoop *L) {
10678// Loops that look like: while (X == 0) are very strange indeed. We don't
10679// handle them yet except for the trivial case. This could be expanded in the
10680// future as needed.
10681
10682// If the value is a constant, check to see if it is known to be non-zero
10683// already. If so, the backedge will execute zero times.
10684if (constSCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
10685if (!C->getValue()->isZero())
10686returngetZero(C->getType());
10687returngetCouldNotCompute();// Otherwise it will loop infinitely.
10688 }
10689
10690// We could implement others, but I really doubt anyone writes loops like
10691// this, and if they did, they would already be constant folded.
10692returngetCouldNotCompute();
10693}
10694
10695std::pair<const BasicBlock *, const BasicBlock *>
10696ScalarEvolution::getPredecessorWithUniqueSuccessorForBB(constBasicBlock *BB)
10697 const{
10698// If the block has a unique predecessor, then there is no path from the
10699// predecessor to the block that does not go through the direct edge
10700// from the predecessor to the block.
10701if (constBasicBlock *Pred = BB->getSinglePredecessor())
10702return {Pred, BB};
10703
10704// A loop's header is defined to be a block that dominates the loop.
10705// If the header has a unique predecessor outside the loop, it must be
10706// a block that has exactly one successor that can reach the loop.
10707if (constLoop *L = LI.getLoopFor(BB))
10708return {L->getLoopPredecessor(),L->getHeader()};
10709
10710return {nullptr, BB};
10711}
10712
10713/// SCEV structural equivalence is usually sufficient for testing whether two
10714/// expressions are equal, however for the purposes of looking for a condition
10715/// guarding a loop, it can be useful to be a little more general, since a
10716/// front-end may have replicated the controlling expression.
10717staticboolHasSameValue(constSCEV *A,constSCEV *B) {
10718// Quick check to see if they are the same SCEV.
10719if (A ==B)returntrue;
10720
10721auto ComputesEqualValues = [](constInstruction *A,constInstruction *B) {
10722// Not all instructions that are "identical" compute the same value. For
10723// instance, two distinct alloca instructions allocating the same type are
10724// identical and do not read memory; but compute distinct values.
10725returnA->isIdenticalTo(B) && (isa<BinaryOperator>(A) || isa<GetElementPtrInst>(A));
10726 };
10727
10728// Otherwise, if they're both SCEVUnknown, it's possible that they hold
10729// two different instructions with the same value. Check for this case.
10730if (constSCEVUnknown *AU = dyn_cast<SCEVUnknown>(A))
10731if (constSCEVUnknown *BU = dyn_cast<SCEVUnknown>(B))
10732if (constInstruction *AI = dyn_cast<Instruction>(AU->getValue()))
10733if (constInstruction *BI = dyn_cast<Instruction>(BU->getValue()))
10734if (ComputesEqualValues(AI, BI))
10735returntrue;
10736
10737// Otherwise assume they may have a different value.
10738returnfalse;
10739}
10740
10741staticboolMatchBinarySub(constSCEV *S,constSCEV *&LHS,constSCEV *&RHS) {
10742constSCEVAddExpr *Add = dyn_cast<SCEVAddExpr>(S);
10743if (!Add ||Add->getNumOperands() != 2)
10744returnfalse;
10745if (auto *ME = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
10746 ME && ME->getNumOperands() == 2 && ME->getOperand(0)->isAllOnesValue()) {
10747LHS =Add->getOperand(1);
10748RHS = ME->getOperand(1);
10749returntrue;
10750 }
10751if (auto *ME = dyn_cast<SCEVMulExpr>(Add->getOperand(1));
10752 ME && ME->getNumOperands() == 2 && ME->getOperand(0)->isAllOnesValue()) {
10753LHS =Add->getOperand(0);
10754RHS = ME->getOperand(1);
10755returntrue;
10756 }
10757returnfalse;
10758}
10759
10760boolScalarEvolution::SimplifyICmpOperands(CmpPredicate &Pred,constSCEV *&LHS,
10761constSCEV *&RHS,unsignedDepth) {
10762bool Changed =false;
10763// Simplifies ICMP to trivial true or false by turning it into '0 == 0' or
10764// '0 != 0'.
10765auto TrivialCase = [&](bool TriviallyTrue) {
10766LHS =RHS =getConstant(ConstantInt::getFalse(getContext()));
10767 Pred = TriviallyTrue ?ICmpInst::ICMP_EQ :ICmpInst::ICMP_NE;
10768returntrue;
10769 };
10770// If we hit the max recursion limit bail out.
10771if (Depth >= 3)
10772returnfalse;
10773
10774// Canonicalize a constant to the right side.
10775if (constSCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
10776// Check for both operands constant.
10777if (constSCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
10778if (!ICmpInst::compare(LHSC->getAPInt(), RHSC->getAPInt(), Pred))
10779return TrivialCase(false);
10780return TrivialCase(true);
10781 }
10782// Otherwise swap the operands to put the constant on the right.
10783std::swap(LHS,RHS);
10784 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
10785 Changed =true;
10786 }
10787
10788// If we're comparing an addrec with a value which is loop-invariant in the
10789// addrec's loop, put the addrec on the left. Also make a dominance check,
10790// as both operands could be addrecs loop-invariant in each other's loop.
10791if (constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(RHS)) {
10792constLoop *L = AR->getLoop();
10793if (isLoopInvariant(LHS, L) &&properlyDominates(LHS, L->getHeader())) {
10794std::swap(LHS,RHS);
10795 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
10796 Changed =true;
10797 }
10798 }
10799
10800// If there's a constant operand, canonicalize comparisons with boundary
10801// cases, and canonicalize *-or-equal comparisons to regular comparisons.
10802if (constSCEVConstant *RC = dyn_cast<SCEVConstant>(RHS)) {
10803constAPInt &RA = RC->getAPInt();
10804
10805bool SimplifiedByConstantRange =false;
10806
10807if (!ICmpInst::isEquality(Pred)) {
10808ConstantRange ExactCR =ConstantRange::makeExactICmpRegion(Pred,RA);
10809if (ExactCR.isFullSet())
10810return TrivialCase(true);
10811if (ExactCR.isEmptySet())
10812return TrivialCase(false);
10813
10814APInt NewRHS;
10815CmpInst::Predicate NewPred;
10816if (ExactCR.getEquivalentICmp(NewPred, NewRHS) &&
10817ICmpInst::isEquality(NewPred)) {
10818// We were able to convert an inequality to an equality.
10819 Pred = NewPred;
10820RHS =getConstant(NewRHS);
10821 Changed = SimplifiedByConstantRange =true;
10822 }
10823 }
10824
10825if (!SimplifiedByConstantRange) {
10826switch (Pred) {
10827default:
10828break;
10829caseICmpInst::ICMP_EQ:
10830caseICmpInst::ICMP_NE:
10831// Fold ((-1) * %a) + %b == 0 (equivalent to %b-%a == 0) into %a == %b.
10832if (RA.isZero() &&MatchBinarySub(LHS,LHS,RHS))
10833 Changed =true;
10834break;
10835
10836// The "Should have been caught earlier!" messages refer to the fact
10837// that the ExactCR.isFullSet() or ExactCR.isEmptySet() check above
10838// should have fired on the corresponding cases, and canonicalized the
10839// check to trivial case.
10840
10841caseICmpInst::ICMP_UGE:
10842assert(!RA.isMinValue() &&"Should have been caught earlier!");
10843 Pred =ICmpInst::ICMP_UGT;
10844RHS =getConstant(RA - 1);
10845 Changed =true;
10846break;
10847caseICmpInst::ICMP_ULE:
10848assert(!RA.isMaxValue() &&"Should have been caught earlier!");
10849 Pred =ICmpInst::ICMP_ULT;
10850RHS =getConstant(RA + 1);
10851 Changed =true;
10852break;
10853caseICmpInst::ICMP_SGE:
10854assert(!RA.isMinSignedValue() &&"Should have been caught earlier!");
10855 Pred =ICmpInst::ICMP_SGT;
10856RHS =getConstant(RA - 1);
10857 Changed =true;
10858break;
10859caseICmpInst::ICMP_SLE:
10860assert(!RA.isMaxSignedValue() &&"Should have been caught earlier!");
10861 Pred =ICmpInst::ICMP_SLT;
10862RHS =getConstant(RA + 1);
10863 Changed =true;
10864break;
10865 }
10866 }
10867 }
10868
10869// Check for obvious equality.
10870if (HasSameValue(LHS,RHS)) {
10871if (ICmpInst::isTrueWhenEqual(Pred))
10872return TrivialCase(true);
10873if (ICmpInst::isFalseWhenEqual(Pred))
10874return TrivialCase(false);
10875 }
10876
10877// If possible, canonicalize GE/LE comparisons to GT/LT comparisons, by
10878// adding or subtracting 1 from one of the operands.
10879switch (Pred) {
10880caseICmpInst::ICMP_SLE:
10881if (!getSignedRangeMax(RHS).isMaxSignedValue()) {
10882RHS =getAddExpr(getConstant(RHS->getType(), 1,true),RHS,
10883SCEV::FlagNSW);
10884 Pred =ICmpInst::ICMP_SLT;
10885 Changed =true;
10886 }elseif (!getSignedRangeMin(LHS).isMinSignedValue()) {
10887LHS =getAddExpr(getConstant(RHS->getType(), (uint64_t)-1,true),LHS,
10888SCEV::FlagNSW);
10889 Pred =ICmpInst::ICMP_SLT;
10890 Changed =true;
10891 }
10892break;
10893caseICmpInst::ICMP_SGE:
10894if (!getSignedRangeMin(RHS).isMinSignedValue()) {
10895RHS =getAddExpr(getConstant(RHS->getType(), (uint64_t)-1,true),RHS,
10896SCEV::FlagNSW);
10897 Pred =ICmpInst::ICMP_SGT;
10898 Changed =true;
10899 }elseif (!getSignedRangeMax(LHS).isMaxSignedValue()) {
10900LHS =getAddExpr(getConstant(RHS->getType(), 1,true),LHS,
10901SCEV::FlagNSW);
10902 Pred =ICmpInst::ICMP_SGT;
10903 Changed =true;
10904 }
10905break;
10906caseICmpInst::ICMP_ULE:
10907if (!getUnsignedRangeMax(RHS).isMaxValue()) {
10908RHS =getAddExpr(getConstant(RHS->getType(), 1,true),RHS,
10909SCEV::FlagNUW);
10910 Pred =ICmpInst::ICMP_ULT;
10911 Changed =true;
10912 }elseif (!getUnsignedRangeMin(LHS).isMinValue()) {
10913LHS =getAddExpr(getConstant(RHS->getType(), (uint64_t)-1,true),LHS);
10914 Pred =ICmpInst::ICMP_ULT;
10915 Changed =true;
10916 }
10917break;
10918caseICmpInst::ICMP_UGE:
10919if (!getUnsignedRangeMin(RHS).isMinValue()) {
10920RHS =getAddExpr(getConstant(RHS->getType(), (uint64_t)-1,true),RHS);
10921 Pred =ICmpInst::ICMP_UGT;
10922 Changed =true;
10923 }elseif (!getUnsignedRangeMax(LHS).isMaxValue()) {
10924LHS =getAddExpr(getConstant(RHS->getType(), 1,true),LHS,
10925SCEV::FlagNUW);
10926 Pred =ICmpInst::ICMP_UGT;
10927 Changed =true;
10928 }
10929break;
10930default:
10931break;
10932 }
10933
10934// TODO: More simplifications are possible here.
10935
10936// Recursively simplify until we either hit a recursion limit or nothing
10937// changes.
10938if (Changed)
10939returnSimplifyICmpOperands(Pred,LHS,RHS,Depth + 1);
10940
10941return Changed;
10942}
10943
10944boolScalarEvolution::isKnownNegative(constSCEV *S) {
10945returngetSignedRangeMax(S).isNegative();
10946}
10947
10948boolScalarEvolution::isKnownPositive(constSCEV *S) {
10949returngetSignedRangeMin(S).isStrictlyPositive();
10950}
10951
10952boolScalarEvolution::isKnownNonNegative(constSCEV *S) {
10953return !getSignedRangeMin(S).isNegative();
10954}
10955
10956boolScalarEvolution::isKnownNonPositive(constSCEV *S) {
10957return !getSignedRangeMax(S).isStrictlyPositive();
10958}
10959
10960boolScalarEvolution::isKnownNonZero(constSCEV *S) {
10961// Query push down for cases where the unsigned range is
10962// less than sufficient.
10963if (constauto *SExt = dyn_cast<SCEVSignExtendExpr>(S))
10964returnisKnownNonZero(SExt->getOperand(0));
10965returngetUnsignedRangeMin(S) != 0;
10966}
10967
10968boolScalarEvolution::isKnownToBeAPowerOfTwo(constSCEV *S,bool OrZero,
10969bool OrNegative) {
10970auto NonRecursive = [this, OrNegative](constSCEV *S) {
10971if (auto *C = dyn_cast<SCEVConstant>(S))
10972returnC->getAPInt().isPowerOf2() ||
10973 (OrNegative &&C->getAPInt().isNegatedPowerOf2());
10974
10975// The vscale_range indicates vscale is a power-of-two.
10976return isa<SCEVVScale>(S) && F.hasFnAttribute(Attribute::VScaleRange);
10977 };
10978
10979if (NonRecursive(S))
10980returntrue;
10981
10982auto *Mul = dyn_cast<SCEVMulExpr>(S);
10983if (!Mul)
10984returnfalse;
10985returnall_of(Mul->operands(), NonRecursive) && (OrZero ||isKnownNonZero(S));
10986}
10987
10988std::pair<const SCEV *, const SCEV *>
10989ScalarEvolution::SplitIntoInitAndPostInc(constLoop *L,constSCEV *S) {
10990// Compute SCEV on entry of loop L.
10991constSCEV *Start = SCEVInitRewriter::rewrite(S, L, *this);
10992if (Start ==getCouldNotCompute())
10993return { Start, Start };
10994// Compute post increment SCEV for loop L.
10995constSCEV *PostInc = SCEVPostIncRewriter::rewrite(S, L, *this);
10996assert(PostInc !=getCouldNotCompute() &&"Unexpected could not compute");
10997return { Start,PostInc };
10998}
10999
11000boolScalarEvolution::isKnownViaInduction(CmpPredicate Pred,constSCEV *LHS,
11001constSCEV *RHS) {
11002// First collect all loops.
11003SmallPtrSet<const Loop *, 8> LoopsUsed;
11004 getUsedLoops(LHS, LoopsUsed);
11005 getUsedLoops(RHS, LoopsUsed);
11006
11007if (LoopsUsed.empty())
11008returnfalse;
11009
11010// Domination relationship must be a linear order on collected loops.
11011#ifndef NDEBUG
11012for (constauto *L1 : LoopsUsed)
11013for (constauto *L2 : LoopsUsed)
11014assert((DT.dominates(L1->getHeader(), L2->getHeader()) ||
11015 DT.dominates(L2->getHeader(), L1->getHeader())) &&
11016"Domination relationship is not a linear order");
11017#endif
11018
11019constLoop *MDL =
11020 *llvm::max_element(LoopsUsed, [&](constLoop *L1,constLoop *L2) {
11021return DT.properlyDominates(L1->getHeader(), L2->getHeader());
11022 });
11023
11024// Get init and post increment value for LHS.
11025auto SplitLHS =SplitIntoInitAndPostInc(MDL,LHS);
11026// if LHS contains unknown non-invariant SCEV then bail out.
11027if (SplitLHS.first ==getCouldNotCompute())
11028returnfalse;
11029assert (SplitLHS.second !=getCouldNotCompute() &&"Unexpected CNC");
11030// Get init and post increment value for RHS.
11031auto SplitRHS =SplitIntoInitAndPostInc(MDL,RHS);
11032// if RHS contains unknown non-invariant SCEV then bail out.
11033if (SplitRHS.first ==getCouldNotCompute())
11034returnfalse;
11035assert (SplitRHS.second !=getCouldNotCompute() &&"Unexpected CNC");
11036// It is possible that init SCEV contains an invariant load but it does
11037// not dominate MDL and is not available at MDL loop entry, so we should
11038// check it here.
11039if (!isAvailableAtLoopEntry(SplitLHS.first, MDL) ||
11040 !isAvailableAtLoopEntry(SplitRHS.first, MDL))
11041returnfalse;
11042
11043// It seems backedge guard check is faster than entry one so in some cases
11044// it can speed up whole estimation by short circuit
11045returnisLoopBackedgeGuardedByCond(MDL, Pred, SplitLHS.second,
11046 SplitRHS.second) &&
11047isLoopEntryGuardedByCond(MDL, Pred, SplitLHS.first, SplitRHS.first);
11048}
11049
11050boolScalarEvolution::isKnownPredicate(CmpPredicate Pred,constSCEV *LHS,
11051constSCEV *RHS) {
11052// Canonicalize the inputs first.
11053 (void)SimplifyICmpOperands(Pred,LHS,RHS);
11054
11055if (isKnownViaInduction(Pred,LHS,RHS))
11056returntrue;
11057
11058if (isKnownPredicateViaSplitting(Pred,LHS,RHS))
11059returntrue;
11060
11061// Otherwise see what can be done with some simple reasoning.
11062return isKnownViaNonRecursiveReasoning(Pred,LHS,RHS);
11063}
11064
11065std::optional<bool>ScalarEvolution::evaluatePredicate(CmpPredicate Pred,
11066constSCEV *LHS,
11067constSCEV *RHS) {
11068if (isKnownPredicate(Pred,LHS,RHS))
11069returntrue;
11070if (isKnownPredicate(ICmpInst::getInverseCmpPredicate(Pred),LHS,RHS))
11071returnfalse;
11072return std::nullopt;
11073}
11074
11075boolScalarEvolution::isKnownPredicateAt(CmpPredicate Pred,constSCEV *LHS,
11076constSCEV *RHS,
11077constInstruction *CtxI) {
11078// TODO: Analyze guards and assumes from Context's block.
11079returnisKnownPredicate(Pred,LHS,RHS) ||
11080isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred,LHS,RHS);
11081}
11082
11083std::optional<bool>
11084ScalarEvolution::evaluatePredicateAt(CmpPredicate Pred,constSCEV *LHS,
11085constSCEV *RHS,constInstruction *CtxI) {
11086 std::optional<bool> KnownWithoutContext =evaluatePredicate(Pred,LHS,RHS);
11087if (KnownWithoutContext)
11088return KnownWithoutContext;
11089
11090if (isBasicBlockEntryGuardedByCond(CtxI->getParent(), Pred,LHS,RHS))
11091returntrue;
11092if (isBasicBlockEntryGuardedByCond(
11093 CtxI->getParent(),ICmpInst::getInverseCmpPredicate(Pred),LHS,RHS))
11094returnfalse;
11095return std::nullopt;
11096}
11097
11098boolScalarEvolution::isKnownOnEveryIteration(CmpPredicate Pred,
11099constSCEVAddRecExpr *LHS,
11100constSCEV *RHS) {
11101constLoop *L =LHS->getLoop();
11102returnisLoopEntryGuardedByCond(L, Pred,LHS->getStart(),RHS) &&
11103isLoopBackedgeGuardedByCond(L, Pred,LHS->getPostIncExpr(*this),RHS);
11104}
11105
11106std::optional<ScalarEvolution::MonotonicPredicateType>
11107ScalarEvolution::getMonotonicPredicateType(constSCEVAddRecExpr *LHS,
11108ICmpInst::Predicate Pred) {
11109auto Result = getMonotonicPredicateTypeImpl(LHS, Pred);
11110
11111#ifndef NDEBUG
11112// Verify an invariant: inverting the predicate should turn a monotonically
11113// increasing change to a monotonically decreasing one, and vice versa.
11114if (Result) {
11115auto ResultSwapped =
11116 getMonotonicPredicateTypeImpl(LHS,ICmpInst::getSwappedPredicate(Pred));
11117
11118assert(*ResultSwapped != *Result &&
11119"monotonicity should flip as we flip the predicate");
11120 }
11121#endif
11122
11123return Result;
11124}
11125
11126std::optional<ScalarEvolution::MonotonicPredicateType>
11127ScalarEvolution::getMonotonicPredicateTypeImpl(constSCEVAddRecExpr *LHS,
11128ICmpInst::Predicate Pred) {
11129// A zero step value for LHS means the induction variable is essentially a
11130// loop invariant value. We don't really depend on the predicate actually
11131// flipping from false to true (for increasing predicates, and the other way
11132// around for decreasing predicates), all we care about is that *if* the
11133// predicate changes then it only changes from false to true.
11134//
11135// A zero step value in itself is not very useful, but there may be places
11136// where SCEV can prove X >= 0 but not prove X > 0, so it is helpful to be
11137// as general as possible.
11138
11139// Only handle LE/LT/GE/GT predicates.
11140if (!ICmpInst::isRelational(Pred))
11141return std::nullopt;
11142
11143bool IsGreater =ICmpInst::isGE(Pred) ||ICmpInst::isGT(Pred);
11144assert((IsGreater ||ICmpInst::isLE(Pred) ||ICmpInst::isLT(Pred)) &&
11145"Should be greater or less!");
11146
11147// Check that AR does not wrap.
11148if (ICmpInst::isUnsigned(Pred)) {
11149if (!LHS->hasNoUnsignedWrap())
11150return std::nullopt;
11151return IsGreater ?MonotonicallyIncreasing :MonotonicallyDecreasing;
11152 }
11153assert(ICmpInst::isSigned(Pred) &&
11154"Relational predicate is either signed or unsigned!");
11155if (!LHS->hasNoSignedWrap())
11156return std::nullopt;
11157
11158constSCEV *Step =LHS->getStepRecurrence(*this);
11159
11160if (isKnownNonNegative(Step))
11161return IsGreater ?MonotonicallyIncreasing :MonotonicallyDecreasing;
11162
11163if (isKnownNonPositive(Step))
11164return !IsGreater ?MonotonicallyIncreasing :MonotonicallyDecreasing;
11165
11166return std::nullopt;
11167}
11168
11169std::optional<ScalarEvolution::LoopInvariantPredicate>
11170ScalarEvolution::getLoopInvariantPredicate(ICmpInst::Predicate Pred,
11171constSCEV *LHS,constSCEV *RHS,
11172constLoop *L,
11173constInstruction *CtxI) {
11174// If there is a loop-invariant, force it into the RHS, otherwise bail out.
11175if (!isLoopInvariant(RHS, L)) {
11176if (!isLoopInvariant(LHS, L))
11177return std::nullopt;
11178
11179std::swap(LHS,RHS);
11180 Pred =ICmpInst::getSwappedPredicate(Pred);
11181 }
11182
11183constSCEVAddRecExpr *ArLHS = dyn_cast<SCEVAddRecExpr>(LHS);
11184if (!ArLHS || ArLHS->getLoop() != L)
11185return std::nullopt;
11186
11187autoMonotonicType =getMonotonicPredicateType(ArLHS, Pred);
11188if (!MonotonicType)
11189return std::nullopt;
11190// If the predicate "ArLHS `Pred` RHS" monotonically increases from false to
11191// true as the loop iterates, and the backedge is control dependent on
11192// "ArLHS `Pred` RHS" == true then we can reason as follows:
11193//
11194// * if the predicate was false in the first iteration then the predicate
11195// is never evaluated again, since the loop exits without taking the
11196// backedge.
11197// * if the predicate was true in the first iteration then it will
11198// continue to be true for all future iterations since it is
11199// monotonically increasing.
11200//
11201// For both the above possibilities, we can replace the loop varying
11202// predicate with its value on the first iteration of the loop (which is
11203// loop invariant).
11204//
11205// A similar reasoning applies for a monotonically decreasing predicate, by
11206// replacing true with false and false with true in the above two bullets.
11207bool Increasing = *MonotonicType ==ScalarEvolution::MonotonicallyIncreasing;
11208autoP = Increasing ? Pred :ICmpInst::getInversePredicate(Pred);
11209
11210if (isLoopBackedgeGuardedByCond(L,P,LHS,RHS))
11211returnScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(),
11212RHS);
11213
11214if (!CtxI)
11215return std::nullopt;
11216// Try to prove via context.
11217// TODO: Support other cases.
11218switch (Pred) {
11219default:
11220break;
11221caseICmpInst::ICMP_ULE:
11222caseICmpInst::ICMP_ULT: {
11223assert(ArLHS->hasNoUnsignedWrap() &&"Is a requirement of monotonicity!");
11224// Given preconditions
11225// (1) ArLHS does not cross the border of positive and negative parts of
11226// range because of:
11227// - Positive step; (TODO: lift this limitation)
11228// - nuw - does not cross zero boundary;
11229// - nsw - does not cross SINT_MAX boundary;
11230// (2) ArLHS <s RHS
11231// (3) RHS >=s 0
11232// we can replace the loop variant ArLHS <u RHS condition with loop
11233// invariant Start(ArLHS) <u RHS.
11234//
11235// Because of (1) there are two options:
11236// - ArLHS is always negative. It means that ArLHS <u RHS is always false;
11237// - ArLHS is always non-negative. Because of (3) RHS is also non-negative.
11238// It means that ArLHS <s RHS <=> ArLHS <u RHS.
11239// Because of (2) ArLHS <u RHS is trivially true.
11240// All together it means that ArLHS <u RHS <=> Start(ArLHS) >=s 0.
11241// We can strengthen this to Start(ArLHS) <u RHS.
11242auto SignFlippedPred =ICmpInst::getFlippedSignednessPredicate(Pred);
11243if (ArLHS->hasNoSignedWrap() && ArLHS->isAffine() &&
11244isKnownPositive(ArLHS->getStepRecurrence(*this)) &&
11245isKnownNonNegative(RHS) &&
11246isKnownPredicateAt(SignFlippedPred, ArLHS,RHS, CtxI))
11247returnScalarEvolution::LoopInvariantPredicate(Pred, ArLHS->getStart(),
11248RHS);
11249 }
11250 }
11251
11252return std::nullopt;
11253}
11254
11255std::optional<ScalarEvolution::LoopInvariantPredicate>
11256ScalarEvolution::getLoopInvariantExitCondDuringFirstIterations(
11257CmpPredicate Pred,constSCEV *LHS,constSCEV *RHS,constLoop *L,
11258constInstruction *CtxI,constSCEV *MaxIter) {
11259if (auto LIP =getLoopInvariantExitCondDuringFirstIterationsImpl(
11260 Pred,LHS,RHS, L, CtxI, MaxIter))
11261return LIP;
11262if (auto *UMin = dyn_cast<SCEVUMinExpr>(MaxIter))
11263// Number of iterations expressed as UMIN isn't always great for expressing
11264// the value on the last iteration. If the straightforward approach didn't
11265// work, try the following trick: if the a predicate is invariant for X, it
11266// is also invariant for umin(X, ...). So try to find something that works
11267// among subexpressions of MaxIter expressed as umin.
11268for (auto *Op :UMin->operands())
11269if (auto LIP =getLoopInvariantExitCondDuringFirstIterationsImpl(
11270 Pred,LHS,RHS, L, CtxI,Op))
11271return LIP;
11272return std::nullopt;
11273}
11274
11275std::optional<ScalarEvolution::LoopInvariantPredicate>
11276ScalarEvolution::getLoopInvariantExitCondDuringFirstIterationsImpl(
11277CmpPredicate Pred,constSCEV *LHS,constSCEV *RHS,constLoop *L,
11278constInstruction *CtxI,constSCEV *MaxIter) {
11279// Try to prove the following set of facts:
11280// - The predicate is monotonic in the iteration space.
11281// - If the check does not fail on the 1st iteration:
11282// - No overflow will happen during first MaxIter iterations;
11283// - It will not fail on the MaxIter'th iteration.
11284// If the check does fail on the 1st iteration, we leave the loop and no
11285// other checks matter.
11286
11287// If there is a loop-invariant, force it into the RHS, otherwise bail out.
11288if (!isLoopInvariant(RHS, L)) {
11289if (!isLoopInvariant(LHS, L))
11290return std::nullopt;
11291
11292std::swap(LHS,RHS);
11293 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
11294 }
11295
11296auto *AR = dyn_cast<SCEVAddRecExpr>(LHS);
11297if (!AR || AR->getLoop() != L)
11298return std::nullopt;
11299
11300// The predicate must be relational (i.e. <, <=, >=, >).
11301if (!ICmpInst::isRelational(Pred))
11302return std::nullopt;
11303
11304// TODO: Support steps other than +/- 1.
11305constSCEV *Step = AR->getStepRecurrence(*this);
11306auto *One =getOne(Step->getType());
11307auto *MinusOne =getNegativeSCEV(One);
11308if (Step != One && Step != MinusOne)
11309return std::nullopt;
11310
11311// Type mismatch here means that MaxIter is potentially larger than max
11312// unsigned value in start type, which mean we cannot prove no wrap for the
11313// indvar.
11314if (AR->getType() != MaxIter->getType())
11315return std::nullopt;
11316
11317// Value of IV on suggested last iteration.
11318constSCEV *Last = AR->evaluateAtIteration(MaxIter, *this);
11319// Does it still meet the requirement?
11320if (!isLoopBackedgeGuardedByCond(L, Pred,Last,RHS))
11321return std::nullopt;
11322// Because step is +/- 1 and MaxIter has same type as Start (i.e. it does
11323// not exceed max unsigned value of this type), this effectively proves
11324// that there is no wrap during the iteration. To prove that there is no
11325// signed/unsigned wrap, we need to check that
11326// Start <= Last for step = 1 or Start >= Last for step = -1.
11327ICmpInst::Predicate NoOverflowPred =
11328CmpInst::isSigned(Pred) ?ICmpInst::ICMP_SLE :ICmpInst::ICMP_ULE;
11329if (Step == MinusOne)
11330 NoOverflowPred =ICmpInst::getSwappedCmpPredicate(NoOverflowPred);
11331constSCEV *Start = AR->getStart();
11332if (!isKnownPredicateAt(NoOverflowPred, Start,Last, CtxI))
11333return std::nullopt;
11334
11335// Everything is fine.
11336returnScalarEvolution::LoopInvariantPredicate(Pred, Start,RHS);
11337}
11338
11339bool ScalarEvolution::isKnownPredicateViaConstantRanges(CmpPredicate Pred,
11340constSCEV *LHS,
11341constSCEV *RHS) {
11342if (HasSameValue(LHS, RHS))
11343returnICmpInst::isTrueWhenEqual(Pred);
11344
11345// This code is split out from isKnownPredicate because it is called from
11346// within isLoopEntryGuardedByCond.
11347
11348auto CheckRanges = [&](constConstantRange &RangeLHS,
11349constConstantRange &RangeRHS) {
11350return RangeLHS.icmp(Pred, RangeRHS);
11351 };
11352
11353// The check at the top of the function catches the case where the values are
11354// known to be equal.
11355if (Pred ==CmpInst::ICMP_EQ)
11356returnfalse;
11357
11358if (Pred ==CmpInst::ICMP_NE) {
11359auto SL =getSignedRange(LHS);
11360auto SR =getSignedRange(RHS);
11361if (CheckRanges(SL, SR))
11362returntrue;
11363auto UL =getUnsignedRange(LHS);
11364auto UR =getUnsignedRange(RHS);
11365if (CheckRanges(UL, UR))
11366returntrue;
11367auto *Diff =getMinusSCEV(LHS, RHS);
11368return !isa<SCEVCouldNotCompute>(Diff) &&isKnownNonZero(Diff);
11369 }
11370
11371if (CmpInst::isSigned(Pred)) {
11372auto SL =getSignedRange(LHS);
11373auto SR =getSignedRange(RHS);
11374return CheckRanges(SL, SR);
11375 }
11376
11377auto UL =getUnsignedRange(LHS);
11378auto UR =getUnsignedRange(RHS);
11379return CheckRanges(UL, UR);
11380}
11381
11382bool ScalarEvolution::isKnownPredicateViaNoOverflow(CmpPredicate Pred,
11383constSCEV *LHS,
11384constSCEV *RHS) {
11385// Match X to (A + C1)<ExpectedFlags> and Y to (A + C2)<ExpectedFlags>, where
11386// C1 and C2 are constant integers. If either X or Y are not add expressions,
11387// consider them as X + 0 and Y + 0 respectively. C1 and C2 are returned via
11388// OutC1 and OutC2.
11389auto MatchBinaryAddToConst = [this](constSCEV *X,constSCEV *Y,
11390APInt &OutC1,APInt &OutC2,
11391SCEV::NoWrapFlags ExpectedFlags) {
11392constSCEV *XNonConstOp, *XConstOp;
11393constSCEV *YNonConstOp, *YConstOp;
11394SCEV::NoWrapFlags XFlagsPresent;
11395SCEV::NoWrapFlags YFlagsPresent;
11396
11397if (!splitBinaryAdd(X, XConstOp, XNonConstOp, XFlagsPresent)) {
11398 XConstOp =getZero(X->getType());
11399 XNonConstOp =X;
11400 XFlagsPresent = ExpectedFlags;
11401 }
11402if (!isa<SCEVConstant>(XConstOp) ||
11403 (XFlagsPresent & ExpectedFlags) != ExpectedFlags)
11404returnfalse;
11405
11406if (!splitBinaryAdd(Y, YConstOp, YNonConstOp, YFlagsPresent)) {
11407 YConstOp =getZero(Y->getType());
11408 YNonConstOp =Y;
11409 YFlagsPresent = ExpectedFlags;
11410 }
11411
11412if (!isa<SCEVConstant>(YConstOp) ||
11413 (YFlagsPresent & ExpectedFlags) != ExpectedFlags)
11414returnfalse;
11415
11416if (YNonConstOp != XNonConstOp)
11417returnfalse;
11418
11419 OutC1 = cast<SCEVConstant>(XConstOp)->getAPInt();
11420 OutC2 = cast<SCEVConstant>(YConstOp)->getAPInt();
11421
11422returntrue;
11423 };
11424
11425APInt C1;
11426APInt C2;
11427
11428switch (Pred) {
11429default:
11430break;
11431
11432caseICmpInst::ICMP_SGE:
11433std::swap(LHS, RHS);
11434 [[fallthrough]];
11435caseICmpInst::ICMP_SLE:
11436// (X + C1)<nsw> s<= (X + C2)<nsw> if C1 s<= C2.
11437if (MatchBinaryAddToConst(LHS, RHS, C1, C2,SCEV::FlagNSW) && C1.sle(C2))
11438returntrue;
11439
11440break;
11441
11442caseICmpInst::ICMP_SGT:
11443std::swap(LHS, RHS);
11444 [[fallthrough]];
11445caseICmpInst::ICMP_SLT:
11446// (X + C1)<nsw> s< (X + C2)<nsw> if C1 s< C2.
11447if (MatchBinaryAddToConst(LHS, RHS, C1, C2,SCEV::FlagNSW) && C1.slt(C2))
11448returntrue;
11449
11450break;
11451
11452caseICmpInst::ICMP_UGE:
11453std::swap(LHS, RHS);
11454 [[fallthrough]];
11455caseICmpInst::ICMP_ULE:
11456// (X + C1)<nuw> u<= (X + C2)<nuw> for C1 u<= C2.
11457if (MatchBinaryAddToConst(LHS, RHS, C1, C2,SCEV::FlagNUW) && C1.ule(C2))
11458returntrue;
11459
11460break;
11461
11462caseICmpInst::ICMP_UGT:
11463std::swap(LHS, RHS);
11464 [[fallthrough]];
11465caseICmpInst::ICMP_ULT:
11466// (X + C1)<nuw> u< (X + C2)<nuw> if C1 u< C2.
11467if (MatchBinaryAddToConst(LHS, RHS, C1, C2,SCEV::FlagNUW) && C1.ult(C2))
11468returntrue;
11469break;
11470 }
11471
11472returnfalse;
11473}
11474
11475bool ScalarEvolution::isKnownPredicateViaSplitting(CmpPredicate Pred,
11476constSCEV *LHS,
11477constSCEV *RHS) {
11478if (Pred !=ICmpInst::ICMP_ULT || ProvingSplitPredicate)
11479returnfalse;
11480
11481// Allowing arbitrary number of activations of isKnownPredicateViaSplitting on
11482// the stack can result in exponential time complexity.
11483SaveAndRestore Restore(ProvingSplitPredicate,true);
11484
11485// If L >= 0 then I `ult` L <=> I >= 0 && I `slt` L
11486//
11487// To prove L >= 0 we use isKnownNonNegative whereas to prove I >= 0 we use
11488// isKnownPredicate. isKnownPredicate is more powerful, but also more
11489// expensive; and using isKnownNonNegative(RHS) is sufficient for most of the
11490// interesting cases seen in practice. We can consider "upgrading" L >= 0 to
11491// use isKnownPredicate later if needed.
11492returnisKnownNonNegative(RHS) &&
11493isKnownPredicate(CmpInst::ICMP_SGE, LHS,getZero(LHS->getType())) &&
11494isKnownPredicate(CmpInst::ICMP_SLT, LHS, RHS);
11495}
11496
11497bool ScalarEvolution::isImpliedViaGuard(constBasicBlock *BB,CmpPredicate Pred,
11498constSCEV *LHS,constSCEV *RHS) {
11499// No need to even try if we know the module has no guards.
11500if (!HasGuards)
11501returnfalse;
11502
11503returnany_of(*BB, [&](constInstruction &I) {
11504using namespacellvm::PatternMatch;
11505
11506Value *Condition;
11507returnmatch(&I, m_Intrinsic<Intrinsic::experimental_guard>(
11508m_Value(Condition))) &&
11509 isImpliedCond(Pred, LHS, RHS, Condition,false);
11510 });
11511}
11512
11513/// isLoopBackedgeGuardedByCond - Test whether the backedge of the loop is
11514/// protected by a conditional between LHS and RHS. This is used to
11515/// to eliminate casts.
11516boolScalarEvolution::isLoopBackedgeGuardedByCond(constLoop *L,
11517CmpPredicate Pred,
11518constSCEV *LHS,
11519constSCEV *RHS) {
11520// Interpret a null as meaning no loop, where there is obviously no guard
11521// (interprocedural conditions notwithstanding). Do not bother about
11522// unreachable loops.
11523if (!L || !DT.isReachableFromEntry(L->getHeader()))
11524returntrue;
11525
11526if (VerifyIR)
11527assert(!verifyFunction(*L->getHeader()->getParent(), &dbgs()) &&
11528"This cannot be done on broken IR!");
11529
11530
11531if (isKnownViaNonRecursiveReasoning(Pred,LHS,RHS))
11532returntrue;
11533
11534BasicBlock *Latch = L->getLoopLatch();
11535if (!Latch)
11536returnfalse;
11537
11538BranchInst *LoopContinuePredicate =
11539 dyn_cast<BranchInst>(Latch->getTerminator());
11540if (LoopContinuePredicate && LoopContinuePredicate->isConditional() &&
11541 isImpliedCond(Pred,LHS,RHS,
11542 LoopContinuePredicate->getCondition(),
11543 LoopContinuePredicate->getSuccessor(0) != L->getHeader()))
11544returntrue;
11545
11546// We don't want more than one activation of the following loops on the stack
11547// -- that can lead to O(n!) time complexity.
11548if (WalkingBEDominatingConds)
11549returnfalse;
11550
11551SaveAndRestore ClearOnExit(WalkingBEDominatingConds,true);
11552
11553// See if we can exploit a trip count to prove the predicate.
11554constauto &BETakenInfo = getBackedgeTakenInfo(L);
11555constSCEV *LatchBECount = BETakenInfo.getExact(Latch,this);
11556if (LatchBECount !=getCouldNotCompute()) {
11557// We know that Latch branches back to the loop header exactly
11558// LatchBECount times. This means the backdege condition at Latch is
11559// equivalent to "{0,+,1} u< LatchBECount".
11560Type *Ty = LatchBECount->getType();
11561auto NoWrapFlags =SCEV::NoWrapFlags(SCEV::FlagNUW |SCEV::FlagNW);
11562constSCEV *LoopCounter =
11563getAddRecExpr(getZero(Ty),getOne(Ty), L, NoWrapFlags);
11564if (isImpliedCond(Pred,LHS,RHS,ICmpInst::ICMP_ULT, LoopCounter,
11565 LatchBECount))
11566returntrue;
11567 }
11568
11569// Check conditions due to any @llvm.assume intrinsics.
11570for (auto &AssumeVH : AC.assumptions()) {
11571if (!AssumeVH)
11572continue;
11573auto *CI = cast<CallInst>(AssumeVH);
11574if (!DT.dominates(CI, Latch->getTerminator()))
11575continue;
11576
11577if (isImpliedCond(Pred,LHS,RHS, CI->getArgOperand(0),false))
11578returntrue;
11579 }
11580
11581if (isImpliedViaGuard(Latch, Pred,LHS,RHS))
11582returntrue;
11583
11584for (DomTreeNode *DTN = DT[Latch], *HeaderDTN = DT[L->getHeader()];
11585 DTN != HeaderDTN; DTN = DTN->getIDom()) {
11586assert(DTN &&"should reach the loop header before reaching the root!");
11587
11588BasicBlock *BB = DTN->getBlock();
11589if (isImpliedViaGuard(BB, Pred,LHS,RHS))
11590returntrue;
11591
11592BasicBlock *PBB = BB->getSinglePredecessor();
11593if (!PBB)
11594continue;
11595
11596BranchInst *ContinuePredicate = dyn_cast<BranchInst>(PBB->getTerminator());
11597if (!ContinuePredicate || !ContinuePredicate->isConditional())
11598continue;
11599
11600Value *Condition = ContinuePredicate->getCondition();
11601
11602// If we have an edge `E` within the loop body that dominates the only
11603// latch, the condition guarding `E` also guards the backedge. This
11604// reasoning works only for loops with a single latch.
11605
11606BasicBlockEdge DominatingEdge(PBB, BB);
11607if (DominatingEdge.isSingleEdge()) {
11608// We're constructively (and conservatively) enumerating edges within the
11609// loop body that dominate the latch. The dominator tree better agree
11610// with us on this:
11611assert(DT.dominates(DominatingEdge, Latch) &&"should be!");
11612
11613if (isImpliedCond(Pred,LHS,RHS, Condition,
11614 BB != ContinuePredicate->getSuccessor(0)))
11615returntrue;
11616 }
11617 }
11618
11619returnfalse;
11620}
11621
11622boolScalarEvolution::isBasicBlockEntryGuardedByCond(constBasicBlock *BB,
11623CmpPredicate Pred,
11624constSCEV *LHS,
11625constSCEV *RHS) {
11626// Do not bother proving facts for unreachable code.
11627if (!DT.isReachableFromEntry(BB))
11628returntrue;
11629if (VerifyIR)
11630assert(!verifyFunction(*BB->getParent(), &dbgs()) &&
11631"This cannot be done on broken IR!");
11632
11633// If we cannot prove strict comparison (e.g. a > b), maybe we can prove
11634// the facts (a >= b && a != b) separately. A typical situation is when the
11635// non-strict comparison is known from ranges and non-equality is known from
11636// dominating predicates. If we are proving strict comparison, we always try
11637// to prove non-equality and non-strict comparison separately.
11638auto NonStrictPredicate =ICmpInst::getNonStrictPredicate(Pred);
11639constbool ProvingStrictComparison = (Pred != NonStrictPredicate);
11640bool ProvedNonStrictComparison =false;
11641bool ProvedNonEquality =false;
11642
11643auto SplitAndProve = [&](std::function<bool(CmpPredicate)> Fn) ->bool {
11644if (!ProvedNonStrictComparison)
11645 ProvedNonStrictComparison = Fn(NonStrictPredicate);
11646if (!ProvedNonEquality)
11647 ProvedNonEquality = Fn(ICmpInst::ICMP_NE);
11648if (ProvedNonStrictComparison && ProvedNonEquality)
11649returntrue;
11650returnfalse;
11651 };
11652
11653if (ProvingStrictComparison) {
11654auto ProofFn = [&](CmpPredicateP) {
11655return isKnownViaNonRecursiveReasoning(P,LHS,RHS);
11656 };
11657if (SplitAndProve(ProofFn))
11658returntrue;
11659 }
11660
11661// Try to prove (Pred, LHS, RHS) using isImpliedCond.
11662auto ProveViaCond = [&](constValue *Condition,boolInverse) {
11663constInstruction *CtxI = &BB->front();
11664if (isImpliedCond(Pred,LHS,RHS, Condition,Inverse, CtxI))
11665returntrue;
11666if (ProvingStrictComparison) {
11667auto ProofFn = [&](CmpPredicateP) {
11668return isImpliedCond(P,LHS,RHS, Condition,Inverse, CtxI);
11669 };
11670if (SplitAndProve(ProofFn))
11671returntrue;
11672 }
11673returnfalse;
11674 };
11675
11676// Starting at the block's predecessor, climb up the predecessor chain, as long
11677// as there are predecessors that can be found that have unique successors
11678// leading to the original block.
11679constLoop *ContainingLoop = LI.getLoopFor(BB);
11680constBasicBlock *PredBB;
11681if (ContainingLoop && ContainingLoop->getHeader() == BB)
11682 PredBB = ContainingLoop->getLoopPredecessor();
11683else
11684 PredBB = BB->getSinglePredecessor();
11685for (std::pair<const BasicBlock *, const BasicBlock *> Pair(PredBB, BB);
11686 Pair.first; Pair = getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
11687constBranchInst *BlockEntryPredicate =
11688 dyn_cast<BranchInst>(Pair.first->getTerminator());
11689if (!BlockEntryPredicate || BlockEntryPredicate->isUnconditional())
11690continue;
11691
11692if (ProveViaCond(BlockEntryPredicate->getCondition(),
11693 BlockEntryPredicate->getSuccessor(0) != Pair.second))
11694returntrue;
11695 }
11696
11697// Check conditions due to any @llvm.assume intrinsics.
11698for (auto &AssumeVH : AC.assumptions()) {
11699if (!AssumeVH)
11700continue;
11701auto *CI = cast<CallInst>(AssumeVH);
11702if (!DT.dominates(CI, BB))
11703continue;
11704
11705if (ProveViaCond(CI->getArgOperand(0),false))
11706returntrue;
11707 }
11708
11709// Check conditions due to any @llvm.experimental.guard intrinsics.
11710auto *GuardDecl =Intrinsic::getDeclarationIfExists(
11711 F.getParent(), Intrinsic::experimental_guard);
11712if (GuardDecl)
11713for (constauto *GU : GuardDecl->users())
11714if (constauto *Guard = dyn_cast<IntrinsicInst>(GU))
11715if (Guard->getFunction() == BB->getParent() && DT.dominates(Guard, BB))
11716if (ProveViaCond(Guard->getArgOperand(0),false))
11717returntrue;
11718returnfalse;
11719}
11720
11721boolScalarEvolution::isLoopEntryGuardedByCond(constLoop *L,CmpPredicate Pred,
11722constSCEV *LHS,
11723constSCEV *RHS) {
11724// Interpret a null as meaning no loop, where there is obviously no guard
11725// (interprocedural conditions notwithstanding).
11726if (!L)
11727returnfalse;
11728
11729// Both LHS and RHS must be available at loop entry.
11730assert(isAvailableAtLoopEntry(LHS, L) &&
11731"LHS is not available at Loop Entry");
11732assert(isAvailableAtLoopEntry(RHS, L) &&
11733"RHS is not available at Loop Entry");
11734
11735if (isKnownViaNonRecursiveReasoning(Pred,LHS,RHS))
11736returntrue;
11737
11738returnisBasicBlockEntryGuardedByCond(L->getHeader(), Pred,LHS,RHS);
11739}
11740
11741bool ScalarEvolution::isImpliedCond(CmpPredicate Pred,constSCEV *LHS,
11742constSCEV *RHS,
11743constValue *FoundCondValue,boolInverse,
11744constInstruction *CtxI) {
11745// False conditions implies anything. Do not bother analyzing it further.
11746if (FoundCondValue ==
11747ConstantInt::getBool(FoundCondValue->getContext(),Inverse))
11748returntrue;
11749
11750if (!PendingLoopPredicates.insert(FoundCondValue).second)
11751returnfalse;
11752
11753auto ClearOnExit =
11754make_scope_exit([&]() { PendingLoopPredicates.erase(FoundCondValue); });
11755
11756// Recursively handle And and Or conditions.
11757constValue *Op0, *Op1;
11758if (match(FoundCondValue,m_LogicalAnd(m_Value(Op0),m_Value(Op1)))) {
11759if (!Inverse)
11760return isImpliedCond(Pred, LHS, RHS, Op0,Inverse, CtxI) ||
11761 isImpliedCond(Pred, LHS, RHS, Op1,Inverse, CtxI);
11762 }elseif (match(FoundCondValue,m_LogicalOr(m_Value(Op0),m_Value(Op1)))) {
11763if (Inverse)
11764return isImpliedCond(Pred, LHS, RHS, Op0,Inverse, CtxI) ||
11765 isImpliedCond(Pred, LHS, RHS, Op1,Inverse, CtxI);
11766 }
11767
11768constICmpInst *ICI = dyn_cast<ICmpInst>(FoundCondValue);
11769if (!ICI)returnfalse;
11770
11771// Now that we found a conditional branch that dominates the loop or controls
11772// the loop latch. Check to see if it is the comparison we are looking for.
11773CmpPredicate FoundPred;
11774if (Inverse)
11775 FoundPred = ICI->getInverseCmpPredicate();
11776else
11777 FoundPred = ICI->getCmpPredicate();
11778
11779constSCEV *FoundLHS =getSCEV(ICI->getOperand(0));
11780constSCEV *FoundRHS =getSCEV(ICI->getOperand(1));
11781
11782return isImpliedCond(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS, CtxI);
11783}
11784
11785bool ScalarEvolution::isImpliedCond(CmpPredicate Pred,constSCEV *LHS,
11786constSCEV *RHS,CmpPredicate FoundPred,
11787constSCEV *FoundLHS,constSCEV *FoundRHS,
11788constInstruction *CtxI) {
11789// Balance the types.
11790if (getTypeSizeInBits(LHS->getType()) <
11791getTypeSizeInBits(FoundLHS->getType())) {
11792// For unsigned and equality predicates, try to prove that both found
11793// operands fit into narrow unsigned range. If so, try to prove facts in
11794// narrow types.
11795if (!CmpInst::isSigned(FoundPred) && !FoundLHS->getType()->isPointerTy() &&
11796 !FoundRHS->getType()->isPointerTy()) {
11797auto *NarrowType =LHS->getType();
11798auto *WideType = FoundLHS->getType();
11799autoBitWidth =getTypeSizeInBits(NarrowType);
11800constSCEV *MaxValue =getZeroExtendExpr(
11801getConstant(APInt::getMaxValue(BitWidth)), WideType);
11802if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundLHS,
11803 MaxValue) &&
11804 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, FoundRHS,
11805 MaxValue)) {
11806constSCEV *TruncFoundLHS =getTruncateExpr(FoundLHS, NarrowType);
11807constSCEV *TruncFoundRHS =getTruncateExpr(FoundRHS, NarrowType);
11808if (isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, TruncFoundLHS,
11809 TruncFoundRHS, CtxI))
11810returntrue;
11811 }
11812 }
11813
11814if (LHS->getType()->isPointerTy() ||RHS->getType()->isPointerTy())
11815returnfalse;
11816if (CmpInst::isSigned(Pred)) {
11817LHS =getSignExtendExpr(LHS, FoundLHS->getType());
11818RHS =getSignExtendExpr(RHS, FoundLHS->getType());
11819 }else {
11820LHS =getZeroExtendExpr(LHS, FoundLHS->getType());
11821RHS =getZeroExtendExpr(RHS, FoundLHS->getType());
11822 }
11823 }elseif (getTypeSizeInBits(LHS->getType()) >
11824getTypeSizeInBits(FoundLHS->getType())) {
11825if (FoundLHS->getType()->isPointerTy() || FoundRHS->getType()->isPointerTy())
11826returnfalse;
11827if (CmpInst::isSigned(FoundPred)) {
11828 FoundLHS =getSignExtendExpr(FoundLHS,LHS->getType());
11829 FoundRHS =getSignExtendExpr(FoundRHS,LHS->getType());
11830 }else {
11831 FoundLHS =getZeroExtendExpr(FoundLHS,LHS->getType());
11832 FoundRHS =getZeroExtendExpr(FoundRHS,LHS->getType());
11833 }
11834 }
11835return isImpliedCondBalancedTypes(Pred, LHS, RHS, FoundPred, FoundLHS,
11836 FoundRHS, CtxI);
11837}
11838
11839bool ScalarEvolution::isImpliedCondBalancedTypes(
11840CmpPredicate Pred,constSCEV *LHS,constSCEV *RHS,CmpPredicate FoundPred,
11841constSCEV *FoundLHS,constSCEV *FoundRHS,constInstruction *CtxI) {
11842assert(getTypeSizeInBits(LHS->getType()) ==
11843getTypeSizeInBits(FoundLHS->getType()) &&
11844"Types should be balanced!");
11845// Canonicalize the query to match the way instcombine will have
11846// canonicalized the comparison.
11847if (SimplifyICmpOperands(Pred, LHS, RHS))
11848if (LHS == RHS)
11849returnCmpInst::isTrueWhenEqual(Pred);
11850if (SimplifyICmpOperands(FoundPred, FoundLHS, FoundRHS))
11851if (FoundLHS == FoundRHS)
11852returnCmpInst::isFalseWhenEqual(FoundPred);
11853
11854// Check to see if we can make the LHS or RHS match.
11855if (LHS == FoundRHS || RHS == FoundLHS) {
11856if (isa<SCEVConstant>(RHS)) {
11857std::swap(FoundLHS, FoundRHS);
11858 FoundPred =ICmpInst::getSwappedCmpPredicate(FoundPred);
11859 }else {
11860std::swap(LHS, RHS);
11861 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
11862 }
11863 }
11864
11865// Check whether the found predicate is the same as the desired predicate.
11866// FIXME: use CmpPredicate::getMatching here.
11867if (FoundPred ==static_cast<CmpInst::Predicate>(Pred))
11868return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI);
11869
11870// Check whether swapping the found predicate makes it the same as the
11871// desired predicate.
11872// FIXME: use CmpPredicate::getMatching here.
11873if (ICmpInst::getSwappedCmpPredicate(FoundPred) ==
11874static_cast<CmpInst::Predicate>(Pred)) {
11875// We can write the implication
11876// 0. LHS Pred RHS <- FoundLHS SwapPred FoundRHS
11877// using one of the following ways:
11878// 1. LHS Pred RHS <- FoundRHS Pred FoundLHS
11879// 2. RHS SwapPred LHS <- FoundLHS SwapPred FoundRHS
11880// 3. LHS Pred RHS <- ~FoundLHS Pred ~FoundRHS
11881// 4. ~LHS SwapPred ~RHS <- FoundLHS SwapPred FoundRHS
11882// Forms 1. and 2. require swapping the operands of one condition. Don't
11883// do this if it would break canonical constant/addrec ordering.
11884if (!isa<SCEVConstant>(RHS) && !isa<SCEVAddRecExpr>(LHS))
11885return isImpliedCondOperands(FoundPred, RHS, LHS, FoundLHS, FoundRHS,
11886 CtxI);
11887if (!isa<SCEVConstant>(FoundRHS) && !isa<SCEVAddRecExpr>(FoundLHS))
11888return isImpliedCondOperands(Pred, LHS, RHS, FoundRHS, FoundLHS, CtxI);
11889
11890// There's no clear preference between forms 3. and 4., try both. Avoid
11891// forming getNotSCEV of pointer values as the resulting subtract is
11892// not legal.
11893if (!LHS->getType()->isPointerTy() && !RHS->getType()->isPointerTy() &&
11894 isImpliedCondOperands(FoundPred,getNotSCEV(LHS),getNotSCEV(RHS),
11895 FoundLHS, FoundRHS, CtxI))
11896returntrue;
11897
11898if (!FoundLHS->getType()->isPointerTy() &&
11899 !FoundRHS->getType()->isPointerTy() &&
11900 isImpliedCondOperands(Pred, LHS, RHS,getNotSCEV(FoundLHS),
11901getNotSCEV(FoundRHS), CtxI))
11902returntrue;
11903
11904returnfalse;
11905 }
11906
11907auto IsSignFlippedPredicate = [](CmpInst::PredicateP1,
11908CmpInst::Predicate P2) {
11909assert(P1 != P2 &&"Handled earlier!");
11910returnCmpInst::isRelational(P2) &&
11911P1 ==ICmpInst::getFlippedSignednessPredicate(P2);
11912 };
11913if (IsSignFlippedPredicate(Pred, FoundPred)) {
11914// Unsigned comparison is the same as signed comparison when both the
11915// operands are non-negative or negative.
11916if ((isKnownNonNegative(FoundLHS) &&isKnownNonNegative(FoundRHS)) ||
11917 (isKnownNegative(FoundLHS) &&isKnownNegative(FoundRHS)))
11918return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI);
11919// Create local copies that we can freely swap and canonicalize our
11920// conditions to "le/lt".
11921CmpPredicate CanonicalPred = Pred, CanonicalFoundPred = FoundPred;
11922constSCEV *CanonicalLHS =LHS, *CanonicalRHS =RHS,
11923 *CanonicalFoundLHS = FoundLHS, *CanonicalFoundRHS = FoundRHS;
11924if (ICmpInst::isGT(CanonicalPred) ||ICmpInst::isGE(CanonicalPred)) {
11925 CanonicalPred =ICmpInst::getSwappedCmpPredicate(CanonicalPred);
11926 CanonicalFoundPred =ICmpInst::getSwappedCmpPredicate(CanonicalFoundPred);
11927std::swap(CanonicalLHS, CanonicalRHS);
11928std::swap(CanonicalFoundLHS, CanonicalFoundRHS);
11929 }
11930assert((ICmpInst::isLT(CanonicalPred) ||ICmpInst::isLE(CanonicalPred)) &&
11931"Must be!");
11932assert((ICmpInst::isLT(CanonicalFoundPred) ||
11933ICmpInst::isLE(CanonicalFoundPred)) &&
11934"Must be!");
11935if (ICmpInst::isSigned(CanonicalPred) &&isKnownNonNegative(CanonicalRHS))
11936// Use implication:
11937// x <u y && y >=s 0 --> x <s y.
11938// If we can prove the left part, the right part is also proven.
11939return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS,
11940 CanonicalRHS, CanonicalFoundLHS,
11941 CanonicalFoundRHS);
11942if (ICmpInst::isUnsigned(CanonicalPred) &&isKnownNegative(CanonicalRHS))
11943// Use implication:
11944// x <s y && y <s 0 --> x <u y.
11945// If we can prove the left part, the right part is also proven.
11946return isImpliedCondOperands(CanonicalFoundPred, CanonicalLHS,
11947 CanonicalRHS, CanonicalFoundLHS,
11948 CanonicalFoundRHS);
11949 }
11950
11951// Check if we can make progress by sharpening ranges.
11952if (FoundPred ==ICmpInst::ICMP_NE &&
11953 (isa<SCEVConstant>(FoundLHS) || isa<SCEVConstant>(FoundRHS))) {
11954
11955constSCEVConstant *C =nullptr;
11956constSCEV *V =nullptr;
11957
11958if (isa<SCEVConstant>(FoundLHS)) {
11959C = cast<SCEVConstant>(FoundLHS);
11960V = FoundRHS;
11961 }else {
11962C = cast<SCEVConstant>(FoundRHS);
11963V = FoundLHS;
11964 }
11965
11966// The guarding predicate tells us that C != V. If the known range
11967// of V is [C, t), we can sharpen the range to [C + 1, t). The
11968// range we consider has to correspond to same signedness as the
11969// predicate we're interested in folding.
11970
11971APInt Min =ICmpInst::isSigned(Pred) ?
11972getSignedRangeMin(V) :getUnsignedRangeMin(V);
11973
11974if (Min ==C->getAPInt()) {
11975// Given (V >= Min && V != Min) we conclude V >= (Min + 1).
11976// This is true even if (Min + 1) wraps around -- in case of
11977// wraparound, (Min + 1) < Min, so (V >= Min => V >= (Min + 1)).
11978
11979APInt SharperMin = Min + 1;
11980
11981switch (Pred) {
11982caseICmpInst::ICMP_SGE:
11983caseICmpInst::ICMP_UGE:
11984// We know V `Pred` SharperMin. If this implies LHS `Pred`
11985// RHS, we're done.
11986if (isImpliedCondOperands(Pred, LHS, RHS, V,getConstant(SharperMin),
11987 CtxI))
11988returntrue;
11989 [[fallthrough]];
11990
11991caseICmpInst::ICMP_SGT:
11992caseICmpInst::ICMP_UGT:
11993// We know from the range information that (V `Pred` Min ||
11994// V == Min). We know from the guarding condition that !(V
11995// == Min). This gives us
11996//
11997// V `Pred` Min || V == Min && !(V == Min)
11998// => V `Pred` Min
11999//
12000// If V `Pred` Min implies LHS `Pred` RHS, we're done.
12001
12002if (isImpliedCondOperands(Pred, LHS, RHS, V,getConstant(Min), CtxI))
12003returntrue;
12004break;
12005
12006// `LHS < RHS` and `LHS <= RHS` are handled in the same way as `RHS > LHS` and `RHS >= LHS` respectively.
12007caseICmpInst::ICMP_SLE:
12008caseICmpInst::ICMP_ULE:
12009if (isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(Pred), RHS,
12010 LHS, V,getConstant(SharperMin), CtxI))
12011returntrue;
12012 [[fallthrough]];
12013
12014caseICmpInst::ICMP_SLT:
12015caseICmpInst::ICMP_ULT:
12016if (isImpliedCondOperands(ICmpInst::getSwappedCmpPredicate(Pred), RHS,
12017 LHS, V,getConstant(Min), CtxI))
12018returntrue;
12019break;
12020
12021default:
12022// No change
12023break;
12024 }
12025 }
12026 }
12027
12028// Check whether the actual condition is beyond sufficient.
12029if (FoundPred ==ICmpInst::ICMP_EQ)
12030if (ICmpInst::isTrueWhenEqual(Pred))
12031if (isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
12032returntrue;
12033if (Pred ==ICmpInst::ICMP_NE)
12034if (!ICmpInst::isTrueWhenEqual(FoundPred))
12035if (isImpliedCondOperands(FoundPred, LHS, RHS, FoundLHS, FoundRHS, CtxI))
12036returntrue;
12037
12038if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, FoundPred, FoundLHS, FoundRHS))
12039returntrue;
12040
12041// Otherwise assume the worst.
12042returnfalse;
12043}
12044
12045bool ScalarEvolution::splitBinaryAdd(constSCEV *Expr,
12046constSCEV *&L,constSCEV *&R,
12047SCEV::NoWrapFlags &Flags) {
12048constauto *AE = dyn_cast<SCEVAddExpr>(Expr);
12049if (!AE || AE->getNumOperands() != 2)
12050returnfalse;
12051
12052L = AE->getOperand(0);
12053R = AE->getOperand(1);
12054Flags = AE->getNoWrapFlags();
12055returntrue;
12056}
12057
12058std::optional<APInt>
12059ScalarEvolution::computeConstantDifference(constSCEV *More,constSCEV *Less) {
12060// We avoid subtracting expressions here because this function is usually
12061// fairly deep in the call stack (i.e. is called many times).
12062
12063unsigned BW =getTypeSizeInBits(More->getType());
12064APInt Diff(BW, 0);
12065APInt DiffMul(BW, 1);
12066// Try various simplifications to reduce the difference to a constant. Limit
12067// the number of allowed simplifications to keep compile-time low.
12068for (unsignedI = 0;I < 8; ++I) {
12069if (More ==Less)
12070return Diff;
12071
12072// Reduce addrecs with identical steps to their start value.
12073if (isa<SCEVAddRecExpr>(Less) && isa<SCEVAddRecExpr>(More)) {
12074constauto *LAR = cast<SCEVAddRecExpr>(Less);
12075constauto *MAR = cast<SCEVAddRecExpr>(More);
12076
12077if (LAR->getLoop() != MAR->getLoop())
12078return std::nullopt;
12079
12080// We look at affine expressions only; not for correctness but to keep
12081// getStepRecurrence cheap.
12082if (!LAR->isAffine() || !MAR->isAffine())
12083return std::nullopt;
12084
12085if (LAR->getStepRecurrence(*this) != MAR->getStepRecurrence(*this))
12086return std::nullopt;
12087
12088Less = LAR->getStart();
12089 More = MAR->getStart();
12090continue;
12091 }
12092
12093// Try to match a common constant multiply.
12094auto MatchConstMul =
12095 [](constSCEV *S) -> std::optional<std::pair<const SCEV *, APInt>> {
12096auto *M = dyn_cast<SCEVMulExpr>(S);
12097if (!M || M->getNumOperands() != 2 ||
12098 !isa<SCEVConstant>(M->getOperand(0)))
12099return std::nullopt;
12100return {
12101 {M->getOperand(1), cast<SCEVConstant>(M->getOperand(0))->getAPInt()}};
12102 };
12103if (auto MatchedMore = MatchConstMul(More)) {
12104if (auto MatchedLess = MatchConstMul(Less)) {
12105if (MatchedMore->second == MatchedLess->second) {
12106 More = MatchedMore->first;
12107Less = MatchedLess->first;
12108 DiffMul *= MatchedMore->second;
12109continue;
12110 }
12111 }
12112 }
12113
12114// Try to cancel out common factors in two add expressions.
12115SmallDenseMap<const SCEV *, int, 8> Multiplicity;
12116autoAdd = [&](constSCEV *S,intMul) {
12117if (auto *C = dyn_cast<SCEVConstant>(S)) {
12118if (Mul == 1) {
12119 Diff +=C->getAPInt() * DiffMul;
12120 }else {
12121assert(Mul == -1);
12122 Diff -=C->getAPInt() * DiffMul;
12123 }
12124 }else
12125 Multiplicity[S] +=Mul;
12126 };
12127auto Decompose = [&](constSCEV *S,intMul) {
12128if (isa<SCEVAddExpr>(S)) {
12129for (constSCEV *Op : S->operands())
12130Add(Op,Mul);
12131 }else
12132Add(S,Mul);
12133 };
12134 Decompose(More, 1);
12135 Decompose(Less, -1);
12136
12137// Check whether all the non-constants cancel out, or reduce to new
12138// More/Less values.
12139constSCEV *NewMore =nullptr, *NewLess =nullptr;
12140for (constauto &[S,Mul] : Multiplicity) {
12141if (Mul == 0)
12142continue;
12143if (Mul == 1) {
12144if (NewMore)
12145return std::nullopt;
12146 NewMore = S;
12147 }elseif (Mul == -1) {
12148if (NewLess)
12149return std::nullopt;
12150 NewLess = S;
12151 }else
12152return std::nullopt;
12153 }
12154
12155// Values stayed the same, no point in trying further.
12156if (NewMore == More || NewLess ==Less)
12157return std::nullopt;
12158
12159 More = NewMore;
12160Less = NewLess;
12161
12162// Reduced to constant.
12163if (!More && !Less)
12164return Diff;
12165
12166// Left with variable on only one side, bail out.
12167if (!More || !Less)
12168return std::nullopt;
12169 }
12170
12171// Did not reduce to constant.
12172return std::nullopt;
12173}
12174
12175bool ScalarEvolution::isImpliedCondOperandsViaAddRecStart(
12176CmpPredicate Pred,constSCEV *LHS,constSCEV *RHS,constSCEV *FoundLHS,
12177constSCEV *FoundRHS,constInstruction *CtxI) {
12178// Try to recognize the following pattern:
12179//
12180// FoundRHS = ...
12181// ...
12182// loop:
12183// FoundLHS = {Start,+,W}
12184// context_bb: // Basic block from the same loop
12185// known(Pred, FoundLHS, FoundRHS)
12186//
12187// If some predicate is known in the context of a loop, it is also known on
12188// each iteration of this loop, including the first iteration. Therefore, in
12189// this case, `FoundLHS Pred FoundRHS` implies `Start Pred FoundRHS`. Try to
12190// prove the original pred using this fact.
12191if (!CtxI)
12192returnfalse;
12193constBasicBlock *ContextBB = CtxI->getParent();
12194// Make sure AR varies in the context block.
12195if (auto *AR = dyn_cast<SCEVAddRecExpr>(FoundLHS)) {
12196constLoop *L = AR->getLoop();
12197// Make sure that context belongs to the loop and executes on 1st iteration
12198// (if it ever executes at all).
12199if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch()))
12200returnfalse;
12201if (!isAvailableAtLoopEntry(FoundRHS, AR->getLoop()))
12202returnfalse;
12203return isImpliedCondOperands(Pred, LHS, RHS, AR->getStart(), FoundRHS);
12204 }
12205
12206if (auto *AR = dyn_cast<SCEVAddRecExpr>(FoundRHS)) {
12207constLoop *L = AR->getLoop();
12208// Make sure that context belongs to the loop and executes on 1st iteration
12209// (if it ever executes at all).
12210if (!L->contains(ContextBB) || !DT.dominates(ContextBB, L->getLoopLatch()))
12211returnfalse;
12212if (!isAvailableAtLoopEntry(FoundLHS, AR->getLoop()))
12213returnfalse;
12214return isImpliedCondOperands(Pred, LHS, RHS, FoundLHS, AR->getStart());
12215 }
12216
12217returnfalse;
12218}
12219
12220bool ScalarEvolution::isImpliedCondOperandsViaNoOverflow(CmpPredicate Pred,
12221constSCEV *LHS,
12222constSCEV *RHS,
12223constSCEV *FoundLHS,
12224constSCEV *FoundRHS) {
12225if (Pred !=CmpInst::ICMP_SLT && Pred !=CmpInst::ICMP_ULT)
12226returnfalse;
12227
12228constauto *AddRecLHS = dyn_cast<SCEVAddRecExpr>(LHS);
12229if (!AddRecLHS)
12230returnfalse;
12231
12232constauto *AddRecFoundLHS = dyn_cast<SCEVAddRecExpr>(FoundLHS);
12233if (!AddRecFoundLHS)
12234returnfalse;
12235
12236// We'd like to let SCEV reason about control dependencies, so we constrain
12237// both the inequalities to be about add recurrences on the same loop. This
12238// way we can use isLoopEntryGuardedByCond later.
12239
12240constLoop *L = AddRecFoundLHS->getLoop();
12241if (L != AddRecLHS->getLoop())
12242returnfalse;
12243
12244// FoundLHS u< FoundRHS u< -C => (FoundLHS + C) u< (FoundRHS + C) ... (1)
12245//
12246// FoundLHS s< FoundRHS s< INT_MIN - C => (FoundLHS + C) s< (FoundRHS + C)
12247// ... (2)
12248//
12249// Informal proof for (2), assuming (1) [*]:
12250//
12251// We'll also assume (A s< B) <=> ((A + INT_MIN) u< (B + INT_MIN)) ... (3)[**]
12252//
12253// Then
12254//
12255// FoundLHS s< FoundRHS s< INT_MIN - C
12256// <=> (FoundLHS + INT_MIN) u< (FoundRHS + INT_MIN) u< -C [ using (3) ]
12257// <=> (FoundLHS + INT_MIN + C) u< (FoundRHS + INT_MIN + C) [ using (1) ]
12258// <=> (FoundLHS + INT_MIN + C + INT_MIN) s<
12259// (FoundRHS + INT_MIN + C + INT_MIN) [ using (3) ]
12260// <=> FoundLHS + C s< FoundRHS + C
12261//
12262// [*]: (1) can be proved by ruling out overflow.
12263//
12264// [**]: This can be proved by analyzing all the four possibilities:
12265// (A s< 0, B s< 0), (A s< 0, B s>= 0), (A s>= 0, B s< 0) and
12266// (A s>= 0, B s>= 0).
12267//
12268// Note:
12269// Despite (2), "FoundRHS s< INT_MIN - C" does not mean that "FoundRHS + C"
12270// will not sign underflow. For instance, say FoundLHS = (i8 -128), FoundRHS
12271// = (i8 -127) and C = (i8 -100). Then INT_MIN - C = (i8 -28), and FoundRHS
12272// s< (INT_MIN - C). Lack of sign overflow / underflow in "FoundRHS + C" is
12273// neither necessary nor sufficient to prove "(FoundLHS + C) s< (FoundRHS +
12274// C)".
12275
12276 std::optional<APInt> LDiff =computeConstantDifference(LHS, FoundLHS);
12277if (!LDiff)
12278returnfalse;
12279 std::optional<APInt> RDiff =computeConstantDifference(RHS, FoundRHS);
12280if (!RDiff || *LDiff != *RDiff)
12281returnfalse;
12282
12283if (LDiff->isMinValue())
12284returntrue;
12285
12286APInt FoundRHSLimit;
12287
12288if (Pred ==CmpInst::ICMP_ULT) {
12289 FoundRHSLimit = -(*RDiff);
12290 }else {
12291assert(Pred ==CmpInst::ICMP_SLT &&"Checked above!");
12292 FoundRHSLimit =APInt::getSignedMinValue(getTypeSizeInBits(RHS->getType())) - *RDiff;
12293 }
12294
12295// Try to prove (1) or (2), as needed.
12296returnisAvailableAtLoopEntry(FoundRHS, L) &&
12297isLoopEntryGuardedByCond(L, Pred, FoundRHS,
12298getConstant(FoundRHSLimit));
12299}
12300
12301bool ScalarEvolution::isImpliedViaMerge(CmpPredicate Pred,constSCEV *LHS,
12302constSCEV *RHS,constSCEV *FoundLHS,
12303constSCEV *FoundRHS,unsignedDepth) {
12304constPHINode *LPhi =nullptr, *RPhi =nullptr;
12305
12306auto ClearOnExit =make_scope_exit([&]() {
12307if (LPhi) {
12308bool Erased = PendingMerges.erase(LPhi);
12309assert(Erased &&"Failed to erase LPhi!");
12310 (void)Erased;
12311 }
12312if (RPhi) {
12313bool Erased = PendingMerges.erase(RPhi);
12314assert(Erased &&"Failed to erase RPhi!");
12315 (void)Erased;
12316 }
12317 });
12318
12319// Find respective Phis and check that they are not being pending.
12320if (constSCEVUnknown *LU = dyn_cast<SCEVUnknown>(LHS))
12321if (auto *Phi = dyn_cast<PHINode>(LU->getValue())) {
12322if (!PendingMerges.insert(Phi).second)
12323returnfalse;
12324 LPhi =Phi;
12325 }
12326if (constSCEVUnknown *RU = dyn_cast<SCEVUnknown>(RHS))
12327if (auto *Phi = dyn_cast<PHINode>(RU->getValue())) {
12328// If we detect a loop of Phi nodes being processed by this method, for
12329// example:
12330//
12331// %a = phi i32 [ %some1, %preheader ], [ %b, %latch ]
12332// %b = phi i32 [ %some2, %preheader ], [ %a, %latch ]
12333//
12334// we don't want to deal with a case that complex, so return conservative
12335// answer false.
12336if (!PendingMerges.insert(Phi).second)
12337returnfalse;
12338 RPhi =Phi;
12339 }
12340
12341// If none of LHS, RHS is a Phi, nothing to do here.
12342if (!LPhi && !RPhi)
12343returnfalse;
12344
12345// If there is a SCEVUnknown Phi we are interested in, make it left.
12346if (!LPhi) {
12347std::swap(LHS, RHS);
12348std::swap(FoundLHS, FoundRHS);
12349std::swap(LPhi, RPhi);
12350 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
12351 }
12352
12353assert(LPhi &&"LPhi should definitely be a SCEVUnknown Phi!");
12354constBasicBlock *LBB = LPhi->getParent();
12355constSCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
12356
12357auto ProvedEasily = [&](constSCEV *S1,constSCEV *S2) {
12358return isKnownViaNonRecursiveReasoning(Pred,S1, S2) ||
12359 isImpliedCondOperandsViaRanges(Pred,S1, S2, Pred, FoundLHS, FoundRHS) ||
12360 isImpliedViaOperations(Pred,S1, S2, FoundLHS, FoundRHS,Depth);
12361 };
12362
12363if (RPhi && RPhi->getParent() == LBB) {
12364// Case one: RHS is also a SCEVUnknown Phi from the same basic block.
12365// If we compare two Phis from the same block, and for each entry block
12366// the predicate is true for incoming values from this block, then the
12367// predicate is also true for the Phis.
12368for (constBasicBlock *IncBB :predecessors(LBB)) {
12369constSCEV *L =getSCEV(LPhi->getIncomingValueForBlock(IncBB));
12370constSCEV *R =getSCEV(RPhi->getIncomingValueForBlock(IncBB));
12371if (!ProvedEasily(L, R))
12372returnfalse;
12373 }
12374 }elseif (RAR && RAR->getLoop()->getHeader() == LBB) {
12375// Case two: RHS is also a Phi from the same basic block, and it is an
12376// AddRec. It means that there is a loop which has both AddRec and Unknown
12377// PHIs, for it we can compare incoming values of AddRec from above the loop
12378// and latch with their respective incoming values of LPhi.
12379// TODO: Generalize to handle loops with many inputs in a header.
12380if (LPhi->getNumIncomingValues() != 2)returnfalse;
12381
12382auto *RLoop = RAR->getLoop();
12383auto *Predecessor = RLoop->getLoopPredecessor();
12384assert(Predecessor &&"Loop with AddRec with no predecessor?");
12385constSCEV *L1 =getSCEV(LPhi->getIncomingValueForBlock(Predecessor));
12386if (!ProvedEasily(L1, RAR->getStart()))
12387returnfalse;
12388auto *Latch = RLoop->getLoopLatch();
12389assert(Latch &&"Loop with AddRec with no latch?");
12390constSCEV *L2 =getSCEV(LPhi->getIncomingValueForBlock(Latch));
12391if (!ProvedEasily(L2, RAR->getPostIncExpr(*this)))
12392returnfalse;
12393 }else {
12394// In all other cases go over inputs of LHS and compare each of them to RHS,
12395// the predicate is true for (LHS, RHS) if it is true for all such pairs.
12396// At this point RHS is either a non-Phi, or it is a Phi from some block
12397// different from LBB.
12398for (constBasicBlock *IncBB :predecessors(LBB)) {
12399// Check that RHS is available in this block.
12400if (!dominates(RHS, IncBB))
12401returnfalse;
12402constSCEV *L =getSCEV(LPhi->getIncomingValueForBlock(IncBB));
12403// Make sure L does not refer to a value from a potentially previous
12404// iteration of a loop.
12405if (!properlyDominates(L, LBB))
12406returnfalse;
12407if (!ProvedEasily(L, RHS))
12408returnfalse;
12409 }
12410 }
12411returntrue;
12412}
12413
12414bool ScalarEvolution::isImpliedCondOperandsViaShift(CmpPredicate Pred,
12415constSCEV *LHS,
12416constSCEV *RHS,
12417constSCEV *FoundLHS,
12418constSCEV *FoundRHS) {
12419// We want to imply LHS < RHS from LHS < (RHS >> shiftvalue). First, make
12420// sure that we are dealing with same LHS.
12421if (RHS == FoundRHS) {
12422std::swap(LHS, RHS);
12423std::swap(FoundLHS, FoundRHS);
12424 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
12425 }
12426if (LHS != FoundLHS)
12427returnfalse;
12428
12429auto *SUFoundRHS = dyn_cast<SCEVUnknown>(FoundRHS);
12430if (!SUFoundRHS)
12431returnfalse;
12432
12433Value *Shiftee, *ShiftValue;
12434
12435using namespacePatternMatch;
12436if (match(SUFoundRHS->getValue(),
12437m_LShr(m_Value(Shiftee),m_Value(ShiftValue)))) {
12438auto *ShifteeS =getSCEV(Shiftee);
12439// Prove one of the following:
12440// LHS <u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <u RHS
12441// LHS <=u (shiftee >> shiftvalue) && shiftee <=u RHS ---> LHS <=u RHS
12442// LHS <s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0
12443// ---> LHS <s RHS
12444// LHS <=s (shiftee >> shiftvalue) && shiftee <=s RHS && shiftee >=s 0
12445// ---> LHS <=s RHS
12446if (Pred ==ICmpInst::ICMP_ULT || Pred ==ICmpInst::ICMP_ULE)
12447returnisKnownPredicate(ICmpInst::ICMP_ULE, ShifteeS, RHS);
12448if (Pred ==ICmpInst::ICMP_SLT || Pred ==ICmpInst::ICMP_SLE)
12449if (isKnownNonNegative(ShifteeS))
12450returnisKnownPredicate(ICmpInst::ICMP_SLE, ShifteeS, RHS);
12451 }
12452
12453returnfalse;
12454}
12455
12456bool ScalarEvolution::isImpliedCondOperands(CmpPredicate Pred,constSCEV *LHS,
12457constSCEV *RHS,
12458constSCEV *FoundLHS,
12459constSCEV *FoundRHS,
12460constInstruction *CtxI) {
12461if (isImpliedCondOperandsViaRanges(Pred, LHS, RHS, Pred, FoundLHS, FoundRHS))
12462returntrue;
12463
12464if (isImpliedCondOperandsViaNoOverflow(Pred, LHS, RHS, FoundLHS, FoundRHS))
12465returntrue;
12466
12467if (isImpliedCondOperandsViaShift(Pred, LHS, RHS, FoundLHS, FoundRHS))
12468returntrue;
12469
12470if (isImpliedCondOperandsViaAddRecStart(Pred, LHS, RHS, FoundLHS, FoundRHS,
12471 CtxI))
12472returntrue;
12473
12474return isImpliedCondOperandsHelper(Pred, LHS, RHS,
12475 FoundLHS, FoundRHS);
12476}
12477
12478/// Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values?
12479template <typename MinMaxExprType>
12480staticboolIsMinMaxConsistingOf(constSCEV *MaybeMinMaxExpr,
12481constSCEV *Candidate) {
12482const MinMaxExprType *MinMaxExpr = dyn_cast<MinMaxExprType>(MaybeMinMaxExpr);
12483if (!MinMaxExpr)
12484returnfalse;
12485
12486returnis_contained(MinMaxExpr->operands(), Candidate);
12487}
12488
12489staticboolIsKnownPredicateViaAddRecStart(ScalarEvolution &SE,
12490CmpPredicate Pred,constSCEV *LHS,
12491constSCEV *RHS) {
12492// If both sides are affine addrecs for the same loop, with equal
12493// steps, and we know the recurrences don't wrap, then we only
12494// need to check the predicate on the starting values.
12495
12496if (!ICmpInst::isRelational(Pred))
12497returnfalse;
12498
12499constSCEVAddRecExpr *LAR = dyn_cast<SCEVAddRecExpr>(LHS);
12500if (!LAR)
12501returnfalse;
12502constSCEVAddRecExpr *RAR = dyn_cast<SCEVAddRecExpr>(RHS);
12503if (!RAR)
12504returnfalse;
12505if (LAR->getLoop() != RAR->getLoop())
12506returnfalse;
12507if (!LAR->isAffine() || !RAR->isAffine())
12508returnfalse;
12509
12510if (LAR->getStepRecurrence(SE) != RAR->getStepRecurrence(SE))
12511returnfalse;
12512
12513SCEV::NoWrapFlags NW =ICmpInst::isSigned(Pred) ?
12514SCEV::FlagNSW :SCEV::FlagNUW;
12515if (!LAR->getNoWrapFlags(NW) || !RAR->getNoWrapFlags(NW))
12516returnfalse;
12517
12518return SE.isKnownPredicate(Pred, LAR->getStart(), RAR->getStart());
12519}
12520
12521/// Is LHS `Pred` RHS true on the virtue of LHS or RHS being a Min or Max
12522/// expression?
12523staticboolIsKnownPredicateViaMinOrMax(ScalarEvolution &SE,CmpPredicate Pred,
12524constSCEV *LHS,constSCEV *RHS) {
12525switch (Pred) {
12526default:
12527returnfalse;
12528
12529caseICmpInst::ICMP_SGE:
12530std::swap(LHS,RHS);
12531 [[fallthrough]];
12532caseICmpInst::ICMP_SLE:
12533return
12534// min(A, ...) <= A
12535 IsMinMaxConsistingOf<SCEVSMinExpr>(LHS,RHS) ||
12536// A <= max(A, ...)
12537 IsMinMaxConsistingOf<SCEVSMaxExpr>(RHS,LHS);
12538
12539caseICmpInst::ICMP_UGE:
12540std::swap(LHS,RHS);
12541 [[fallthrough]];
12542caseICmpInst::ICMP_ULE:
12543return
12544// min(A, ...) <= A
12545// FIXME: what about umin_seq?
12546 IsMinMaxConsistingOf<SCEVUMinExpr>(LHS,RHS) ||
12547// A <= max(A, ...)
12548 IsMinMaxConsistingOf<SCEVUMaxExpr>(RHS,LHS);
12549 }
12550
12551llvm_unreachable("covered switch fell through?!");
12552}
12553
12554bool ScalarEvolution::isImpliedViaOperations(CmpPredicate Pred,constSCEV *LHS,
12555constSCEV *RHS,
12556constSCEV *FoundLHS,
12557constSCEV *FoundRHS,
12558unsignedDepth) {
12559assert(getTypeSizeInBits(LHS->getType()) ==
12560getTypeSizeInBits(RHS->getType()) &&
12561"LHS and RHS have different sizes?");
12562assert(getTypeSizeInBits(FoundLHS->getType()) ==
12563getTypeSizeInBits(FoundRHS->getType()) &&
12564"FoundLHS and FoundRHS have different sizes?");
12565// We want to avoid hurting the compile time with analysis of too big trees.
12566if (Depth >MaxSCEVOperationsImplicationDepth)
12567returnfalse;
12568
12569// We only want to work with GT comparison so far.
12570if (Pred ==ICmpInst::ICMP_ULT || Pred ==ICmpInst::ICMP_SLT) {
12571 Pred =ICmpInst::getSwappedCmpPredicate(Pred);
12572std::swap(LHS, RHS);
12573std::swap(FoundLHS, FoundRHS);
12574 }
12575
12576// For unsigned, try to reduce it to corresponding signed comparison.
12577if (Pred ==ICmpInst::ICMP_UGT)
12578// We can replace unsigned predicate with its signed counterpart if all
12579// involved values are non-negative.
12580// TODO: We could have better support for unsigned.
12581if (isKnownNonNegative(FoundLHS) &&isKnownNonNegative(FoundRHS)) {
12582// Knowing that both FoundLHS and FoundRHS are non-negative, and knowing
12583// FoundLHS >u FoundRHS, we also know that FoundLHS >s FoundRHS. Let us
12584// use this fact to prove that LHS and RHS are non-negative.
12585constSCEV *MinusOne =getMinusOne(LHS->getType());
12586if (isImpliedCondOperands(ICmpInst::ICMP_SGT, LHS, MinusOne, FoundLHS,
12587 FoundRHS) &&
12588 isImpliedCondOperands(ICmpInst::ICMP_SGT, RHS, MinusOne, FoundLHS,
12589 FoundRHS))
12590 Pred =ICmpInst::ICMP_SGT;
12591 }
12592
12593if (Pred !=ICmpInst::ICMP_SGT)
12594returnfalse;
12595
12596auto GetOpFromSExt = [&](constSCEV *S) {
12597if (auto *Ext = dyn_cast<SCEVSignExtendExpr>(S))
12598returnExt->getOperand();
12599// TODO: If S is a SCEVConstant then you can cheaply "strip" the sext off
12600// the constant in some cases.
12601return S;
12602 };
12603
12604// Acquire values from extensions.
12605auto *OrigLHS =LHS;
12606auto *OrigFoundLHS = FoundLHS;
12607LHS = GetOpFromSExt(LHS);
12608 FoundLHS = GetOpFromSExt(FoundLHS);
12609
12610// Is the SGT predicate can be proved trivially or using the found context.
12611auto IsSGTViaContext = [&](constSCEV *S1,constSCEV *S2) {
12612return isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGT,S1, S2) ||
12613 isImpliedViaOperations(ICmpInst::ICMP_SGT,S1, S2, OrigFoundLHS,
12614 FoundRHS,Depth + 1);
12615 };
12616
12617if (auto *LHSAddExpr = dyn_cast<SCEVAddExpr>(LHS)) {
12618// We want to avoid creation of any new non-constant SCEV. Since we are
12619// going to compare the operands to RHS, we should be certain that we don't
12620// need any size extensions for this. So let's decline all cases when the
12621// sizes of types of LHS and RHS do not match.
12622// TODO: Maybe try to get RHS from sext to catch more cases?
12623if (getTypeSizeInBits(LHS->getType()) !=getTypeSizeInBits(RHS->getType()))
12624returnfalse;
12625
12626// Should not overflow.
12627if (!LHSAddExpr->hasNoSignedWrap())
12628returnfalse;
12629
12630auto *LL = LHSAddExpr->getOperand(0);
12631auto *LR = LHSAddExpr->getOperand(1);
12632auto *MinusOne =getMinusOne(RHS->getType());
12633
12634// Checks that S1 >= 0 && S2 > RHS, trivially or using the found context.
12635auto IsSumGreaterThanRHS = [&](constSCEV *S1,constSCEV *S2) {
12636return IsSGTViaContext(S1, MinusOne) && IsSGTViaContext(S2, RHS);
12637 };
12638// Try to prove the following rule:
12639// (LHS = LL + LR) && (LL >= 0) && (LR > RHS) => (LHS > RHS).
12640// (LHS = LL + LR) && (LR >= 0) && (LL > RHS) => (LHS > RHS).
12641if (IsSumGreaterThanRHS(LL, LR) || IsSumGreaterThanRHS(LR, LL))
12642returntrue;
12643 }elseif (auto *LHSUnknownExpr = dyn_cast<SCEVUnknown>(LHS)) {
12644Value *LL, *LR;
12645// FIXME: Once we have SDiv implemented, we can get rid of this matching.
12646
12647using namespacellvm::PatternMatch;
12648
12649if (match(LHSUnknownExpr->getValue(),m_SDiv(m_Value(LL),m_Value(LR)))) {
12650// Rules for division.
12651// We are going to perform some comparisons with Denominator and its
12652// derivative expressions. In general case, creating a SCEV for it may
12653// lead to a complex analysis of the entire graph, and in particular it
12654// can request trip count recalculation for the same loop. This would
12655// cache as SCEVCouldNotCompute to avoid the infinite recursion. To avoid
12656// this, we only want to create SCEVs that are constants in this section.
12657// So we bail if Denominator is not a constant.
12658if (!isa<ConstantInt>(LR))
12659returnfalse;
12660
12661auto *Denominator = cast<SCEVConstant>(getSCEV(LR));
12662
12663// We want to make sure that LHS = FoundLHS / Denominator. If it is so,
12664// then a SCEV for the numerator already exists and matches with FoundLHS.
12665auto *Numerator =getExistingSCEV(LL);
12666if (!Numerator || Numerator->getType() != FoundLHS->getType())
12667returnfalse;
12668
12669// Make sure that the numerator matches with FoundLHS and the denominator
12670// is positive.
12671if (!HasSameValue(Numerator, FoundLHS) || !isKnownPositive(Denominator))
12672returnfalse;
12673
12674auto *DTy = Denominator->getType();
12675auto *FRHSTy = FoundRHS->getType();
12676if (DTy->isPointerTy() != FRHSTy->isPointerTy())
12677// One of types is a pointer and another one is not. We cannot extend
12678// them properly to a wider type, so let us just reject this case.
12679// TODO: Usage of getEffectiveSCEVType for DTy, FRHSTy etc should help
12680// to avoid this check.
12681returnfalse;
12682
12683// Given that:
12684// FoundLHS > FoundRHS, LHS = FoundLHS / Denominator, Denominator > 0.
12685auto *WTy =getWiderType(DTy, FRHSTy);
12686auto *DenominatorExt =getNoopOrSignExtend(Denominator, WTy);
12687auto *FoundRHSExt =getNoopOrSignExtend(FoundRHS, WTy);
12688
12689// Try to prove the following rule:
12690// (FoundRHS > Denominator - 2) && (RHS <= 0) => (LHS > RHS).
12691// For example, given that FoundLHS > 2. It means that FoundLHS is at
12692// least 3. If we divide it by Denominator < 4, we will have at least 1.
12693auto *DenomMinusTwo =getMinusSCEV(DenominatorExt,getConstant(WTy, 2));
12694if (isKnownNonPositive(RHS) &&
12695 IsSGTViaContext(FoundRHSExt, DenomMinusTwo))
12696returntrue;
12697
12698// Try to prove the following rule:
12699// (FoundRHS > -1 - Denominator) && (RHS < 0) => (LHS > RHS).
12700// For example, given that FoundLHS > -3. Then FoundLHS is at least -2.
12701// If we divide it by Denominator > 2, then:
12702// 1. If FoundLHS is negative, then the result is 0.
12703// 2. If FoundLHS is non-negative, then the result is non-negative.
12704// Anyways, the result is non-negative.
12705auto *MinusOne =getMinusOne(WTy);
12706auto *NegDenomMinusOne =getMinusSCEV(MinusOne, DenominatorExt);
12707if (isKnownNegative(RHS) &&
12708 IsSGTViaContext(FoundRHSExt, NegDenomMinusOne))
12709returntrue;
12710 }
12711 }
12712
12713// If our expression contained SCEVUnknown Phis, and we split it down and now
12714// need to prove something for them, try to prove the predicate for every
12715// possible incoming values of those Phis.
12716if (isImpliedViaMerge(Pred, OrigLHS, RHS, OrigFoundLHS, FoundRHS,Depth + 1))
12717returntrue;
12718
12719returnfalse;
12720}
12721
12722staticboolisKnownPredicateExtendIdiom(CmpPredicate Pred,constSCEV *LHS,
12723constSCEV *RHS) {
12724// zext x u<= sext x, sext x s<= zext x
12725constSCEV *Op;
12726switch (Pred) {
12727caseICmpInst::ICMP_SGE:
12728std::swap(LHS,RHS);
12729 [[fallthrough]];
12730caseICmpInst::ICMP_SLE: {
12731// If operand >=s 0 then ZExt == SExt. If operand <s 0 then SExt <s ZExt.
12732returnmatch(LHS,m_scev_SExt(m_SCEV(Op))) &&
12733match(RHS,m_scev_ZExt(m_Specific(Op)));
12734 }
12735caseICmpInst::ICMP_UGE:
12736std::swap(LHS,RHS);
12737 [[fallthrough]];
12738caseICmpInst::ICMP_ULE: {
12739// If operand >=u 0 then ZExt == SExt. If operand <u 0 then ZExt <u SExt.
12740returnmatch(LHS,m_scev_ZExt(m_SCEV(Op))) &&
12741match(RHS,m_scev_SExt(m_Specific(Op)));
12742 }
12743default:
12744returnfalse;
12745 };
12746llvm_unreachable("unhandled case");
12747}
12748
12749bool ScalarEvolution::isKnownViaNonRecursiveReasoning(CmpPredicate Pred,
12750constSCEV *LHS,
12751constSCEV *RHS) {
12752returnisKnownPredicateExtendIdiom(Pred, LHS, RHS) ||
12753 isKnownPredicateViaConstantRanges(Pred, LHS, RHS) ||
12754IsKnownPredicateViaMinOrMax(*this, Pred, LHS, RHS) ||
12755IsKnownPredicateViaAddRecStart(*this, Pred, LHS, RHS) ||
12756 isKnownPredicateViaNoOverflow(Pred, LHS, RHS);
12757}
12758
12759bool ScalarEvolution::isImpliedCondOperandsHelper(CmpPredicate Pred,
12760constSCEV *LHS,
12761constSCEV *RHS,
12762constSCEV *FoundLHS,
12763constSCEV *FoundRHS) {
12764switch (Pred) {
12765default:
12766llvm_unreachable("Unexpected CmpPredicate value!");
12767caseICmpInst::ICMP_EQ:
12768caseICmpInst::ICMP_NE:
12769if (HasSameValue(LHS, FoundLHS) &&HasSameValue(RHS, FoundRHS))
12770returntrue;
12771break;
12772caseICmpInst::ICMP_SLT:
12773caseICmpInst::ICMP_SLE:
12774if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, LHS, FoundLHS) &&
12775 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, RHS, FoundRHS))
12776returntrue;
12777break;
12778caseICmpInst::ICMP_SGT:
12779caseICmpInst::ICMP_SGE:
12780if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SGE, LHS, FoundLHS) &&
12781 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_SLE, RHS, FoundRHS))
12782returntrue;
12783break;
12784caseICmpInst::ICMP_ULT:
12785caseICmpInst::ICMP_ULE:
12786if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, LHS, FoundLHS) &&
12787 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, RHS, FoundRHS))
12788returntrue;
12789break;
12790caseICmpInst::ICMP_UGT:
12791caseICmpInst::ICMP_UGE:
12792if (isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_UGE, LHS, FoundLHS) &&
12793 isKnownViaNonRecursiveReasoning(ICmpInst::ICMP_ULE, RHS, FoundRHS))
12794returntrue;
12795break;
12796 }
12797
12798// Maybe it can be proved via operations?
12799if (isImpliedViaOperations(Pred, LHS, RHS, FoundLHS, FoundRHS))
12800returntrue;
12801
12802returnfalse;
12803}
12804
12805bool ScalarEvolution::isImpliedCondOperandsViaRanges(
12806CmpPredicate Pred,constSCEV *LHS,constSCEV *RHS,CmpPredicate FoundPred,
12807constSCEV *FoundLHS,constSCEV *FoundRHS) {
12808if (!isa<SCEVConstant>(RHS) || !isa<SCEVConstant>(FoundRHS))
12809// The restriction on `FoundRHS` be lifted easily -- it exists only to
12810// reduce the compile time impact of this optimization.
12811returnfalse;
12812
12813 std::optional<APInt> Addend =computeConstantDifference(LHS, FoundLHS);
12814if (!Addend)
12815returnfalse;
12816
12817constAPInt &ConstFoundRHS = cast<SCEVConstant>(FoundRHS)->getAPInt();
12818
12819// `FoundLHSRange` is the range we know `FoundLHS` to be in by virtue of the
12820// antecedent "`FoundLHS` `FoundPred` `FoundRHS`".
12821ConstantRange FoundLHSRange =
12822ConstantRange::makeExactICmpRegion(FoundPred, ConstFoundRHS);
12823
12824// Since `LHS` is `FoundLHS` + `Addend`, we can compute a range for `LHS`:
12825ConstantRange LHSRange = FoundLHSRange.add(ConstantRange(*Addend));
12826
12827// We can also compute the range of values for `LHS` that satisfy the
12828// consequent, "`LHS` `Pred` `RHS`":
12829constAPInt &ConstRHS = cast<SCEVConstant>(RHS)->getAPInt();
12830// The antecedent implies the consequent if every value of `LHS` that
12831// satisfies the antecedent also satisfies the consequent.
12832return LHSRange.icmp(Pred, ConstRHS);
12833}
12834
12835bool ScalarEvolution::canIVOverflowOnLT(constSCEV *RHS,constSCEV *Stride,
12836bool IsSigned) {
12837assert(isKnownPositive(Stride) &&"Positive stride expected!");
12838
12839unsignedBitWidth =getTypeSizeInBits(RHS->getType());
12840constSCEV *One =getOne(Stride->getType());
12841
12842if (IsSigned) {
12843APInt MaxRHS =getSignedRangeMax(RHS);
12844APInt MaxValue =APInt::getSignedMaxValue(BitWidth);
12845APInt MaxStrideMinusOne =getSignedRangeMax(getMinusSCEV(Stride, One));
12846
12847// SMaxRHS + SMaxStrideMinusOne > SMaxValue => overflow!
12848return (std::move(MaxValue) - MaxStrideMinusOne).slt(MaxRHS);
12849 }
12850
12851APInt MaxRHS =getUnsignedRangeMax(RHS);
12852APInt MaxValue =APInt::getMaxValue(BitWidth);
12853APInt MaxStrideMinusOne =getUnsignedRangeMax(getMinusSCEV(Stride, One));
12854
12855// UMaxRHS + UMaxStrideMinusOne > UMaxValue => overflow!
12856return (std::move(MaxValue) - MaxStrideMinusOne).ult(MaxRHS);
12857}
12858
12859bool ScalarEvolution::canIVOverflowOnGT(constSCEV *RHS,constSCEV *Stride,
12860bool IsSigned) {
12861
12862unsignedBitWidth =getTypeSizeInBits(RHS->getType());
12863constSCEV *One =getOne(Stride->getType());
12864
12865if (IsSigned) {
12866APInt MinRHS =getSignedRangeMin(RHS);
12867APInt MinValue =APInt::getSignedMinValue(BitWidth);
12868APInt MaxStrideMinusOne =getSignedRangeMax(getMinusSCEV(Stride, One));
12869
12870// SMinRHS - SMaxStrideMinusOne < SMinValue => overflow!
12871return (std::move(MinValue) + MaxStrideMinusOne).sgt(MinRHS);
12872 }
12873
12874APInt MinRHS =getUnsignedRangeMin(RHS);
12875APInt MinValue =APInt::getMinValue(BitWidth);
12876APInt MaxStrideMinusOne =getUnsignedRangeMax(getMinusSCEV(Stride, One));
12877
12878// UMinRHS - UMaxStrideMinusOne < UMinValue => overflow!
12879return (std::move(MinValue) + MaxStrideMinusOne).ugt(MinRHS);
12880}
12881
12882constSCEV *ScalarEvolution::getUDivCeilSCEV(constSCEV *N,constSCEV *D) {
12883// umin(N, 1) + floor((N - umin(N, 1)) / D)
12884// This is equivalent to "1 + floor((N - 1) / D)" for N != 0. The umin
12885// expression fixes the case of N=0.
12886constSCEV *MinNOne =getUMinExpr(N,getOne(N->getType()));
12887constSCEV *NMinusOne =getMinusSCEV(N, MinNOne);
12888returngetAddExpr(MinNOne,getUDivExpr(NMinusOne,D));
12889}
12890
12891constSCEV *ScalarEvolution::computeMaxBECountForLT(constSCEV *Start,
12892constSCEV *Stride,
12893constSCEV *End,
12894unsignedBitWidth,
12895bool IsSigned) {
12896// The logic in this function assumes we can represent a positive stride.
12897// If we can't, the backedge-taken count must be zero.
12898if (IsSigned &&BitWidth == 1)
12899returngetZero(Stride->getType());
12900
12901// This code below only been closely audited for negative strides in the
12902// unsigned comparison case, it may be correct for signed comparison, but
12903// that needs to be established.
12904if (IsSigned &&isKnownNegative(Stride))
12905returngetCouldNotCompute();
12906
12907// Calculate the maximum backedge count based on the range of values
12908// permitted by Start, End, and Stride.
12909APInt MinStart =
12910 IsSigned ?getSignedRangeMin(Start) :getUnsignedRangeMin(Start);
12911
12912APInt MinStride =
12913 IsSigned ?getSignedRangeMin(Stride) :getUnsignedRangeMin(Stride);
12914
12915// We assume either the stride is positive, or the backedge-taken count
12916// is zero. So force StrideForMaxBECount to be at least one.
12917APInt One(BitWidth, 1);
12918APInt StrideForMaxBECount = IsSigned ?APIntOps::smax(One, MinStride)
12919 : APIntOps::umax(One, MinStride);
12920
12921APInt MaxValue = IsSigned ?APInt::getSignedMaxValue(BitWidth)
12922 :APInt::getMaxValue(BitWidth);
12923APInt Limit = MaxValue - (StrideForMaxBECount - 1);
12924
12925// Although End can be a MAX expression we estimate MaxEnd considering only
12926// the case End = RHS of the loop termination condition. This is safe because
12927// in the other case (End - Start) is zero, leading to a zero maximum backedge
12928// taken count.
12929APInt MaxEnd = IsSigned ?APIntOps::smin(getSignedRangeMax(End), Limit)
12930 : APIntOps::umin(getUnsignedRangeMax(End), Limit);
12931
12932// MaxBECount = ceil((max(MaxEnd, MinStart) - MinStart) / Stride)
12933 MaxEnd = IsSigned ?APIntOps::smax(MaxEnd, MinStart)
12934 : APIntOps::umax(MaxEnd, MinStart);
12935
12936returngetUDivCeilSCEV(getConstant(MaxEnd - MinStart)/* Delta */,
12937getConstant(StrideForMaxBECount)/* Step */);
12938}
12939
12940ScalarEvolution::ExitLimit
12941ScalarEvolution::howManyLessThans(constSCEV *LHS,constSCEV *RHS,
12942constLoop *L,bool IsSigned,
12943bool ControlsOnlyExit,bool AllowPredicates) {
12944SmallVector<const SCEVPredicate *> Predicates;
12945
12946constSCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
12947bool PredicatedIV =false;
12948if (!IV) {
12949if (auto *ZExt = dyn_cast<SCEVZeroExtendExpr>(LHS)) {
12950constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(ZExt->getOperand());
12951if (AR && AR->getLoop() == L && AR->isAffine()) {
12952auto canProveNUW = [&]() {
12953// We can use the comparison to infer no-wrap flags only if it fully
12954// controls the loop exit.
12955if (!ControlsOnlyExit)
12956returnfalse;
12957
12958if (!isLoopInvariant(RHS, L))
12959returnfalse;
12960
12961if (!isKnownNonZero(AR->getStepRecurrence(*this)))
12962// We need the sequence defined by AR to strictly increase in the
12963// unsigned integer domain for the logic below to hold.
12964returnfalse;
12965
12966constunsigned InnerBitWidth =getTypeSizeInBits(AR->getType());
12967constunsigned OuterBitWidth =getTypeSizeInBits(RHS->getType());
12968// If RHS <=u Limit, then there must exist a value V in the sequence
12969// defined by AR (e.g. {Start,+,Step}) such that V >u RHS, and
12970// V <=u UINT_MAX. Thus, we must exit the loop before unsigned
12971// overflow occurs. This limit also implies that a signed comparison
12972// (in the wide bitwidth) is equivalent to an unsigned comparison as
12973// the high bits on both sides must be zero.
12974APInt StrideMax =getUnsignedRangeMax(AR->getStepRecurrence(*this));
12975APInt Limit =APInt::getMaxValue(InnerBitWidth) - (StrideMax - 1);
12976 Limit = Limit.zext(OuterBitWidth);
12977returngetUnsignedRangeMax(applyLoopGuards(RHS, L)).ule(Limit);
12978 };
12979autoFlags = AR->getNoWrapFlags();
12980if (!hasFlags(Flags,SCEV::FlagNUW) && canProveNUW())
12981Flags =setFlags(Flags,SCEV::FlagNUW);
12982
12983setNoWrapFlags(const_cast<SCEVAddRecExpr *>(AR), Flags);
12984if (AR->hasNoUnsignedWrap()) {
12985// Emulate what getZeroExtendExpr would have done during construction
12986// if we'd been able to infer the fact just above at that time.
12987constSCEV *Step = AR->getStepRecurrence(*this);
12988Type *Ty = ZExt->getType();
12989auto *S =getAddRecExpr(
12990 getExtendAddRecStart<SCEVZeroExtendExpr>(AR, Ty,this, 0),
12991getZeroExtendExpr(Step, Ty, 0), L, AR->getNoWrapFlags());
12992IV = dyn_cast<SCEVAddRecExpr>(S);
12993 }
12994 }
12995 }
12996 }
12997
12998
12999if (!IV && AllowPredicates) {
13000// Try to make this an AddRec using runtime tests, in the first X
13001// iterations of this loop, where X is the SCEV expression found by the
13002// algorithm below.
13003IV =convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
13004 PredicatedIV =true;
13005 }
13006
13007// Avoid weird loops
13008if (!IV ||IV->getLoop() != L || !IV->isAffine())
13009returngetCouldNotCompute();
13010
13011// A precondition of this method is that the condition being analyzed
13012// reaches an exiting branch which dominates the latch. Given that, we can
13013// assume that an increment which violates the nowrap specification and
13014// produces poison must cause undefined behavior when the resulting poison
13015// value is branched upon and thus we can conclude that the backedge is
13016// taken no more often than would be required to produce that poison value.
13017// Note that a well defined loop can exit on the iteration which violates
13018// the nowrap specification if there is another exit (either explicit or
13019// implicit/exceptional) which causes the loop to execute before the
13020// exiting instruction we're analyzing would trigger UB.
13021auto WrapType = IsSigned ?SCEV::FlagNSW :SCEV::FlagNUW;
13022bool NoWrap = ControlsOnlyExit &&IV->getNoWrapFlags(WrapType);
13023ICmpInst::PredicateCond = IsSigned ?ICmpInst::ICMP_SLT :ICmpInst::ICMP_ULT;
13024
13025constSCEV *Stride =IV->getStepRecurrence(*this);
13026
13027bool PositiveStride =isKnownPositive(Stride);
13028
13029// Avoid negative or zero stride values.
13030if (!PositiveStride) {
13031// We can compute the correct backedge taken count for loops with unknown
13032// strides if we can prove that the loop is not an infinite loop with side
13033// effects. Here's the loop structure we are trying to handle -
13034//
13035// i = start
13036// do {
13037// A[i] = i;
13038// i += s;
13039// } while (i < end);
13040//
13041// The backedge taken count for such loops is evaluated as -
13042// (max(end, start + stride) - start - 1) /u stride
13043//
13044// The additional preconditions that we need to check to prove correctness
13045// of the above formula is as follows -
13046//
13047// a) IV is either nuw or nsw depending upon signedness (indicated by the
13048// NoWrap flag).
13049// b) the loop is guaranteed to be finite (e.g. is mustprogress and has
13050// no side effects within the loop)
13051// c) loop has a single static exit (with no abnormal exits)
13052//
13053// Precondition a) implies that if the stride is negative, this is a single
13054// trip loop. The backedge taken count formula reduces to zero in this case.
13055//
13056// Precondition b) and c) combine to imply that if rhs is invariant in L,
13057// then a zero stride means the backedge can't be taken without executing
13058// undefined behavior.
13059//
13060// The positive stride case is the same as isKnownPositive(Stride) returning
13061// true (original behavior of the function).
13062//
13063if (PredicatedIV || !NoWrap || !loopIsFiniteByAssumption(L) ||
13064 !loopHasNoAbnormalExits(L))
13065returngetCouldNotCompute();
13066
13067if (!isKnownNonZero(Stride)) {
13068// If we have a step of zero, and RHS isn't invariant in L, we don't know
13069// if it might eventually be greater than start and if so, on which
13070// iteration. We can't even produce a useful upper bound.
13071if (!isLoopInvariant(RHS, L))
13072returngetCouldNotCompute();
13073
13074// We allow a potentially zero stride, but we need to divide by stride
13075// below. Since the loop can't be infinite and this check must control
13076// the sole exit, we can infer the exit must be taken on the first
13077// iteration (e.g. backedge count = 0) if the stride is zero. Given that,
13078// we know the numerator in the divides below must be zero, so we can
13079// pick an arbitrary non-zero value for the denominator (e.g. stride)
13080// and produce the right result.
13081// FIXME: Handle the case where Stride is poison?
13082auto wouldZeroStrideBeUB = [&]() {
13083// Proof by contradiction. Suppose the stride were zero. If we can
13084// prove that the backedge *is* taken on the first iteration, then since
13085// we know this condition controls the sole exit, we must have an
13086// infinite loop. We can't have a (well defined) infinite loop per
13087// check just above.
13088// Note: The (Start - Stride) term is used to get the start' term from
13089// (start' + stride,+,stride). Remember that we only care about the
13090// result of this expression when stride == 0 at runtime.
13091auto *StartIfZero =getMinusSCEV(IV->getStart(), Stride);
13092returnisLoopEntryGuardedByCond(L,Cond, StartIfZero, RHS);
13093 };
13094if (!wouldZeroStrideBeUB()) {
13095 Stride =getUMaxExpr(Stride,getOne(Stride->getType()));
13096 }
13097 }
13098 }elseif (!NoWrap) {
13099// Avoid proven overflow cases: this will ensure that the backedge taken
13100// count will not generate any unsigned overflow.
13101if (canIVOverflowOnLT(RHS, Stride, IsSigned))
13102returngetCouldNotCompute();
13103 }
13104
13105// On all paths just preceeding, we established the following invariant:
13106// IV can be assumed not to overflow up to and including the exiting
13107// iteration. We proved this in one of two ways:
13108// 1) We can show overflow doesn't occur before the exiting iteration
13109// 1a) canIVOverflowOnLT, and b) step of one
13110// 2) We can show that if overflow occurs, the loop must execute UB
13111// before any possible exit.
13112// Note that we have not yet proved RHS invariant (in general).
13113
13114constSCEV *Start =IV->getStart();
13115
13116// Preserve pointer-typed Start/RHS to pass to isLoopEntryGuardedByCond.
13117// If we convert to integers, isLoopEntryGuardedByCond will miss some cases.
13118// Use integer-typed versions for actual computation; we can't subtract
13119// pointers in general.
13120constSCEV *OrigStart = Start;
13121constSCEV *OrigRHS =RHS;
13122if (Start->getType()->isPointerTy()) {
13123 Start =getLosslessPtrToIntExpr(Start);
13124if (isa<SCEVCouldNotCompute>(Start))
13125return Start;
13126 }
13127if (RHS->getType()->isPointerTy()) {
13128RHS =getLosslessPtrToIntExpr(RHS);
13129if (isa<SCEVCouldNotCompute>(RHS))
13130returnRHS;
13131 }
13132
13133constSCEV *End =nullptr, *BECount =nullptr,
13134 *BECountIfBackedgeTaken =nullptr;
13135if (!isLoopInvariant(RHS, L)) {
13136constauto *RHSAddRec = dyn_cast<SCEVAddRecExpr>(RHS);
13137if (PositiveStride && RHSAddRec !=nullptr && RHSAddRec->getLoop() == L &&
13138 RHSAddRec->getNoWrapFlags()) {
13139// The structure of loop we are trying to calculate backedge count of:
13140//
13141// left = left_start
13142// right = right_start
13143//
13144// while(left < right){
13145// ... do something here ...
13146// left += s1; // stride of left is s1 (s1 > 0)
13147// right += s2; // stride of right is s2 (s2 < 0)
13148// }
13149//
13150
13151constSCEV *RHSStart = RHSAddRec->getStart();
13152constSCEV *RHSStride = RHSAddRec->getStepRecurrence(*this);
13153
13154// If Stride - RHSStride is positive and does not overflow, we can write
13155// backedge count as ->
13156// ceil((End - Start) /u (Stride - RHSStride))
13157// Where, End = max(RHSStart, Start)
13158
13159// Check if RHSStride < 0 and Stride - RHSStride will not overflow.
13160if (isKnownNegative(RHSStride) &&
13161willNotOverflow(Instruction::Sub,/*Signed=*/true, Stride,
13162 RHSStride)) {
13163
13164constSCEV *Denominator =getMinusSCEV(Stride, RHSStride);
13165if (isKnownPositive(Denominator)) {
13166End = IsSigned ?getSMaxExpr(RHSStart, Start)
13167 :getUMaxExpr(RHSStart, Start);
13168
13169// We can do this because End >= Start, as End = max(RHSStart, Start)
13170constSCEV *Delta =getMinusSCEV(End, Start);
13171
13172 BECount =getUDivCeilSCEV(Delta, Denominator);
13173 BECountIfBackedgeTaken =
13174getUDivCeilSCEV(getMinusSCEV(RHSStart, Start), Denominator);
13175 }
13176 }
13177 }
13178if (BECount ==nullptr) {
13179// If we cannot calculate ExactBECount, we can calculate the MaxBECount,
13180// given the start, stride and max value for the end bound of the
13181// loop (RHS), and the fact that IV does not overflow (which is
13182// checked above).
13183constSCEV *MaxBECount = computeMaxBECountForLT(
13184 Start, Stride, RHS,getTypeSizeInBits(LHS->getType()), IsSigned);
13185return ExitLimit(getCouldNotCompute()/* ExactNotTaken */, MaxBECount,
13186 MaxBECount,false/*MaxOrZero*/, Predicates);
13187 }
13188 }else {
13189// We use the expression (max(End,Start)-Start)/Stride to describe the
13190// backedge count, as if the backedge is taken at least once
13191// max(End,Start) is End and so the result is as above, and if not
13192// max(End,Start) is Start so we get a backedge count of zero.
13193auto *OrigStartMinusStride =getMinusSCEV(OrigStart, Stride);
13194assert(isAvailableAtLoopEntry(OrigStartMinusStride, L) &&"Must be!");
13195assert(isAvailableAtLoopEntry(OrigStart, L) &&"Must be!");
13196assert(isAvailableAtLoopEntry(OrigRHS, L) &&"Must be!");
13197// Can we prove (max(RHS,Start) > Start - Stride?
13198if (isLoopEntryGuardedByCond(L,Cond, OrigStartMinusStride, OrigStart) &&
13199isLoopEntryGuardedByCond(L,Cond, OrigStartMinusStride, OrigRHS)) {
13200// In this case, we can use a refined formula for computing backedge
13201// taken count. The general formula remains:
13202// "End-Start /uceiling Stride" where "End = max(RHS,Start)"
13203// We want to use the alternate formula:
13204// "((End - 1) - (Start - Stride)) /u Stride"
13205// Let's do a quick case analysis to show these are equivalent under
13206// our precondition that max(RHS,Start) > Start - Stride.
13207// * For RHS <= Start, the backedge-taken count must be zero.
13208// "((End - 1) - (Start - Stride)) /u Stride" reduces to
13209// "((Start - 1) - (Start - Stride)) /u Stride" which simplies to
13210// "Stride - 1 /u Stride" which is indeed zero for all non-zero values
13211// of Stride. For 0 stride, we've use umin(1,Stride) above,
13212// reducing this to the stride of 1 case.
13213// * For RHS >= Start, the backedge count must be "RHS-Start /uceil
13214// Stride".
13215// "((End - 1) - (Start - Stride)) /u Stride" reduces to
13216// "((RHS - 1) - (Start - Stride)) /u Stride" reassociates to
13217// "((RHS - (Start - Stride) - 1) /u Stride".
13218// Our preconditions trivially imply no overflow in that form.
13219constSCEV *MinusOne =getMinusOne(Stride->getType());
13220constSCEV *Numerator =
13221getMinusSCEV(getAddExpr(RHS, MinusOne),getMinusSCEV(Start, Stride));
13222 BECount =getUDivExpr(Numerator, Stride);
13223 }
13224
13225if (!BECount) {
13226auto canProveRHSGreaterThanEqualStart = [&]() {
13227auto CondGE = IsSigned ?ICmpInst::ICMP_SGE :ICmpInst::ICMP_UGE;
13228constSCEV *GuardedRHS =applyLoopGuards(OrigRHS, L);
13229constSCEV *GuardedStart =applyLoopGuards(OrigStart, L);
13230
13231if (isLoopEntryGuardedByCond(L, CondGE, OrigRHS, OrigStart) ||
13232isKnownPredicate(CondGE, GuardedRHS, GuardedStart))
13233returntrue;
13234
13235// (RHS > Start - 1) implies RHS >= Start.
13236// * "RHS >= Start" is trivially equivalent to "RHS > Start - 1" if
13237// "Start - 1" doesn't overflow.
13238// * For signed comparison, if Start - 1 does overflow, it's equal
13239// to INT_MAX, and "RHS >s INT_MAX" is trivially false.
13240// * For unsigned comparison, if Start - 1 does overflow, it's equal
13241// to UINT_MAX, and "RHS >u UINT_MAX" is trivially false.
13242//
13243// FIXME: Should isLoopEntryGuardedByCond do this for us?
13244auto CondGT = IsSigned ?ICmpInst::ICMP_SGT :ICmpInst::ICMP_UGT;
13245auto *StartMinusOne =
13246getAddExpr(OrigStart,getMinusOne(OrigStart->getType()));
13247returnisLoopEntryGuardedByCond(L, CondGT, OrigRHS, StartMinusOne);
13248 };
13249
13250// If we know that RHS >= Start in the context of loop, then we know
13251// that max(RHS, Start) = RHS at this point.
13252if (canProveRHSGreaterThanEqualStart()) {
13253End =RHS;
13254 }else {
13255// If RHS < Start, the backedge will be taken zero times. So in
13256// general, we can write the backedge-taken count as:
13257//
13258// RHS >= Start ? ceil(RHS - Start) / Stride : 0
13259//
13260// We convert it to the following to make it more convenient for SCEV:
13261//
13262// ceil(max(RHS, Start) - Start) / Stride
13263End = IsSigned ?getSMaxExpr(RHS, Start) :getUMaxExpr(RHS, Start);
13264
13265// See what would happen if we assume the backedge is taken. This is
13266// used to compute MaxBECount.
13267 BECountIfBackedgeTaken =
13268getUDivCeilSCEV(getMinusSCEV(RHS, Start), Stride);
13269 }
13270
13271// At this point, we know:
13272//
13273// 1. If IsSigned, Start <=s End; otherwise, Start <=u End
13274// 2. The index variable doesn't overflow.
13275//
13276// Therefore, we know N exists such that
13277// (Start + Stride * N) >= End, and computing "(Start + Stride * N)"
13278// doesn't overflow.
13279//
13280// Using this information, try to prove whether the addition in
13281// "(Start - End) + (Stride - 1)" has unsigned overflow.
13282constSCEV *One =getOne(Stride->getType());
13283bool MayAddOverflow = [&] {
13284if (isKnownToBeAPowerOfTwo(Stride)) {
13285// Suppose Stride is a power of two, and Start/End are unsigned
13286// integers. Let UMAX be the largest representable unsigned
13287// integer.
13288//
13289// By the preconditions of this function, we know
13290// "(Start + Stride * N) >= End", and this doesn't overflow.
13291// As a formula:
13292//
13293// End <= (Start + Stride * N) <= UMAX
13294//
13295// Subtracting Start from all the terms:
13296//
13297// End - Start <= Stride * N <= UMAX - Start
13298//
13299// Since Start is unsigned, UMAX - Start <= UMAX. Therefore:
13300//
13301// End - Start <= Stride * N <= UMAX
13302//
13303// Stride * N is a multiple of Stride. Therefore,
13304//
13305// End - Start <= Stride * N <= UMAX - (UMAX mod Stride)
13306//
13307// Since Stride is a power of two, UMAX + 1 is divisible by
13308// Stride. Therefore, UMAX mod Stride == Stride - 1. So we can
13309// write:
13310//
13311// End - Start <= Stride * N <= UMAX - Stride - 1
13312//
13313// Dropping the middle term:
13314//
13315// End - Start <= UMAX - Stride - 1
13316//
13317// Adding Stride - 1 to both sides:
13318//
13319// (End - Start) + (Stride - 1) <= UMAX
13320//
13321// In other words, the addition doesn't have unsigned overflow.
13322//
13323// A similar proof works if we treat Start/End as signed values.
13324// Just rewrite steps before "End - Start <= Stride * N <= UMAX"
13325// to use signed max instead of unsigned max. Note that we're
13326// trying to prove a lack of unsigned overflow in either case.
13327returnfalse;
13328 }
13329if (Start == Stride || Start ==getMinusSCEV(Stride, One)) {
13330// If Start is equal to Stride, (End - Start) + (Stride - 1) == End
13331// - 1. If !IsSigned, 0 <u Stride == Start <=u End; so 0 <u End - 1
13332// <u End. If IsSigned, 0 <s Stride == Start <=s End; so 0 <s End -
13333// 1 <s End.
13334//
13335// If Start is equal to Stride - 1, (End - Start) + Stride - 1 ==
13336// End.
13337returnfalse;
13338 }
13339returntrue;
13340 }();
13341
13342constSCEV *Delta =getMinusSCEV(End, Start);
13343if (!MayAddOverflow) {
13344// floor((D + (S - 1)) / S)
13345// We prefer this formulation if it's legal because it's fewer
13346// operations.
13347 BECount =
13348getUDivExpr(getAddExpr(Delta,getMinusSCEV(Stride, One)), Stride);
13349 }else {
13350 BECount =getUDivCeilSCEV(Delta, Stride);
13351 }
13352 }
13353 }
13354
13355constSCEV *ConstantMaxBECount;
13356bool MaxOrZero =false;
13357if (isa<SCEVConstant>(BECount)) {
13358 ConstantMaxBECount = BECount;
13359 }elseif (BECountIfBackedgeTaken &&
13360 isa<SCEVConstant>(BECountIfBackedgeTaken)) {
13361// If we know exactly how many times the backedge will be taken if it's
13362// taken at least once, then the backedge count will either be that or
13363// zero.
13364 ConstantMaxBECount = BECountIfBackedgeTaken;
13365 MaxOrZero =true;
13366 }else {
13367 ConstantMaxBECount = computeMaxBECountForLT(
13368 Start, Stride, RHS,getTypeSizeInBits(LHS->getType()), IsSigned);
13369 }
13370
13371if (isa<SCEVCouldNotCompute>(ConstantMaxBECount) &&
13372 !isa<SCEVCouldNotCompute>(BECount))
13373 ConstantMaxBECount =getConstant(getUnsignedRangeMax(BECount));
13374
13375constSCEV *SymbolicMaxBECount =
13376 isa<SCEVCouldNotCompute>(BECount) ? ConstantMaxBECount : BECount;
13377return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount, MaxOrZero,
13378 Predicates);
13379}
13380
13381ScalarEvolution::ExitLimit ScalarEvolution::howManyGreaterThans(
13382constSCEV *LHS,constSCEV *RHS,constLoop *L,bool IsSigned,
13383bool ControlsOnlyExit,bool AllowPredicates) {
13384SmallVector<const SCEVPredicate *> Predicates;
13385// We handle only IV > Invariant
13386if (!isLoopInvariant(RHS, L))
13387returngetCouldNotCompute();
13388
13389constSCEVAddRecExpr *IV = dyn_cast<SCEVAddRecExpr>(LHS);
13390if (!IV && AllowPredicates)
13391// Try to make this an AddRec using runtime tests, in the first X
13392// iterations of this loop, where X is the SCEV expression found by the
13393// algorithm below.
13394IV =convertSCEVToAddRecWithPredicates(LHS, L, Predicates);
13395
13396// Avoid weird loops
13397if (!IV ||IV->getLoop() != L || !IV->isAffine())
13398returngetCouldNotCompute();
13399
13400auto WrapType = IsSigned ?SCEV::FlagNSW :SCEV::FlagNUW;
13401bool NoWrap = ControlsOnlyExit &&IV->getNoWrapFlags(WrapType);
13402ICmpInst::PredicateCond = IsSigned ?ICmpInst::ICMP_SGT :ICmpInst::ICMP_UGT;
13403
13404constSCEV *Stride =getNegativeSCEV(IV->getStepRecurrence(*this));
13405
13406// Avoid negative or zero stride values
13407if (!isKnownPositive(Stride))
13408returngetCouldNotCompute();
13409
13410// Avoid proven overflow cases: this will ensure that the backedge taken count
13411// will not generate any unsigned overflow. Relaxed no-overflow conditions
13412// exploit NoWrapFlags, allowing to optimize in presence of undefined
13413// behaviors like the case of C language.
13414if (!Stride->isOne() && !NoWrap)
13415if (canIVOverflowOnGT(RHS, Stride, IsSigned))
13416returngetCouldNotCompute();
13417
13418constSCEV *Start =IV->getStart();
13419constSCEV *End =RHS;
13420if (!isLoopEntryGuardedByCond(L,Cond,getAddExpr(Start, Stride), RHS)) {
13421// If we know that Start >= RHS in the context of loop, then we know that
13422// min(RHS, Start) = RHS at this point.
13423if (isLoopEntryGuardedByCond(
13424 L, IsSigned ?ICmpInst::ICMP_SGE :ICmpInst::ICMP_UGE, Start, RHS))
13425End =RHS;
13426else
13427End = IsSigned ?getSMinExpr(RHS, Start) :getUMinExpr(RHS, Start);
13428 }
13429
13430if (Start->getType()->isPointerTy()) {
13431 Start =getLosslessPtrToIntExpr(Start);
13432if (isa<SCEVCouldNotCompute>(Start))
13433return Start;
13434 }
13435if (End->getType()->isPointerTy()) {
13436End =getLosslessPtrToIntExpr(End);
13437if (isa<SCEVCouldNotCompute>(End))
13438returnEnd;
13439 }
13440
13441// Compute ((Start - End) + (Stride - 1)) / Stride.
13442// FIXME: This can overflow. Holding off on fixing this for now;
13443// howManyGreaterThans will hopefully be gone soon.
13444constSCEV *One =getOne(Stride->getType());
13445constSCEV *BECount =getUDivExpr(
13446getAddExpr(getMinusSCEV(Start,End),getMinusSCEV(Stride, One)), Stride);
13447
13448APInt MaxStart = IsSigned ?getSignedRangeMax(Start)
13449 :getUnsignedRangeMax(Start);
13450
13451APInt MinStride = IsSigned ?getSignedRangeMin(Stride)
13452 :getUnsignedRangeMin(Stride);
13453
13454unsignedBitWidth =getTypeSizeInBits(LHS->getType());
13455APInt Limit = IsSigned ?APInt::getSignedMinValue(BitWidth) + (MinStride - 1)
13456 :APInt::getMinValue(BitWidth) + (MinStride - 1);
13457
13458// Although End can be a MIN expression we estimate MinEnd considering only
13459// the case End = RHS. This is safe because in the other case (Start - End)
13460// is zero, leading to a zero maximum backedge taken count.
13461APInt MinEnd =
13462 IsSigned ?APIntOps::smax(getSignedRangeMin(RHS), Limit)
13463 : APIntOps::umax(getUnsignedRangeMin(RHS), Limit);
13464
13465constSCEV *ConstantMaxBECount =
13466 isa<SCEVConstant>(BECount)
13467 ? BECount
13468 :getUDivCeilSCEV(getConstant(MaxStart - MinEnd),
13469getConstant(MinStride));
13470
13471if (isa<SCEVCouldNotCompute>(ConstantMaxBECount))
13472 ConstantMaxBECount = BECount;
13473constSCEV *SymbolicMaxBECount =
13474 isa<SCEVCouldNotCompute>(BECount) ? ConstantMaxBECount : BECount;
13475
13476return ExitLimit(BECount, ConstantMaxBECount, SymbolicMaxBECount,false,
13477 Predicates);
13478}
13479
13480constSCEV *SCEVAddRecExpr::getNumIterationsInRange(constConstantRange &Range,
13481ScalarEvolution &SE) const{
13482if (Range.isFullSet())// Infinite loop.
13483return SE.getCouldNotCompute();
13484
13485// If the start is a non-zero constant, shift the range to simplify things.
13486if (constSCEVConstant *SC = dyn_cast<SCEVConstant>(getStart()))
13487if (!SC->getValue()->isZero()) {
13488SmallVector<const SCEV *, 4>Operands(operands());
13489Operands[0] = SE.getZero(SC->getType());
13490constSCEV *Shifted = SE.getAddRecExpr(Operands, getLoop(),
13491 getNoWrapFlags(FlagNW));
13492if (constauto *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
13493return ShiftedAddRec->getNumIterationsInRange(
13494Range.subtract(SC->getAPInt()), SE);
13495// This is strange and shouldn't happen.
13496return SE.getCouldNotCompute();
13497 }
13498
13499// The only time we can solve this is when we have all constant indices.
13500// Otherwise, we cannot determine the overflow conditions.
13501if (any_of(operands(), [](constSCEV *Op) {return !isa<SCEVConstant>(Op); }))
13502return SE.getCouldNotCompute();
13503
13504// Okay at this point we know that all elements of the chrec are constants and
13505// that the start element is zero.
13506
13507// First check to see if the range contains zero. If not, the first
13508// iteration exits.
13509unsignedBitWidth = SE.getTypeSizeInBits(getType());
13510if (!Range.contains(APInt(BitWidth, 0)))
13511return SE.getZero(getType());
13512
13513if (isAffine()) {
13514// If this is an affine expression then we have this situation:
13515// Solve {0,+,A} in Range === Ax in Range
13516
13517// We know that zero is in the range. If A is positive then we know that
13518// the upper value of the range must be the first possible exit value.
13519// If A is negative then the lower of the range is the last possible loop
13520// value. Also note that we already checked for a full range.
13521APIntA = cast<SCEVConstant>(getOperand(1))->getAPInt();
13522APIntEnd =A.sge(1) ? (Range.getUpper() - 1) :Range.getLower();
13523
13524// The exit value should be (End+A)/A.
13525APInt ExitVal = (End +A).udiv(A);
13526ConstantInt *ExitValue = ConstantInt::get(SE.getContext(), ExitVal);
13527
13528// Evaluate at the exit value. If we really did fall out of the valid
13529// range, then we computed our trip count, otherwise wrap around or other
13530// things must have happened.
13531ConstantInt *Val =EvaluateConstantChrecAtConstant(this, ExitValue, SE);
13532if (Range.contains(Val->getValue()))
13533return SE.getCouldNotCompute();// Something strange happened
13534
13535// Ensure that the previous value is in the range.
13536assert(Range.contains(
13537EvaluateConstantChrecAtConstant(this,
13538 ConstantInt::get(SE.getContext(), ExitVal - 1), SE)->getValue()) &&
13539"Linear scev computation is off in a bad way!");
13540return SE.getConstant(ExitValue);
13541 }
13542
13543if (isQuadratic()) {
13544if (auto S =SolveQuadraticAddRecRange(this,Range, SE))
13545return SE.getConstant(*S);
13546 }
13547
13548return SE.getCouldNotCompute();
13549}
13550
13551constSCEVAddRecExpr *
13552SCEVAddRecExpr::getPostIncExpr(ScalarEvolution &SE) const{
13553assert(getNumOperands() > 1 &&"AddRec with zero step?");
13554// There is a temptation to just call getAddExpr(this, getStepRecurrence(SE)),
13555// but in this case we cannot guarantee that the value returned will be an
13556// AddRec because SCEV does not have a fixed point where it stops
13557// simplification: it is legal to return ({rec1} + {rec2}). For example, it
13558// may happen if we reach arithmetic depth limit while simplifying. So we
13559// construct the returned value explicitly.
13560SmallVector<const SCEV *, 3> Ops;
13561// If this is {A,+,B,+,C,...,+,N}, then its step is {B,+,C,+,...,+,N}, and
13562// (this + Step) is {A+B,+,B+C,+...,+,N}.
13563for (unsigned i = 0, e = getNumOperands() - 1; i < e; ++i)
13564 Ops.push_back(SE.getAddExpr(getOperand(i), getOperand(i + 1)));
13565// We know that the last operand is not a constant zero (otherwise it would
13566// have been popped out earlier). This guarantees us that if the result has
13567// the same last operand, then it will also not be popped out, meaning that
13568// the returned value will be an AddRec.
13569constSCEV *Last = getOperand(getNumOperands() - 1);
13570assert(!Last->isZero() &&"Recurrency with zero step?");
13571 Ops.push_back(Last);
13572return cast<SCEVAddRecExpr>(SE.getAddRecExpr(Ops, getLoop(),
13573SCEV::FlagAnyWrap));
13574}
13575
13576// Return true when S contains at least an undef value.
13577boolScalarEvolution::containsUndefs(constSCEV *S) const{
13578returnSCEVExprContains(S, [](constSCEV *S) {
13579if (constauto *SU = dyn_cast<SCEVUnknown>(S))
13580return isa<UndefValue>(SU->getValue());
13581returnfalse;
13582 });
13583}
13584
13585// Return true when S contains a value that is a nullptr.
13586boolScalarEvolution::containsErasedValue(constSCEV *S) const{
13587returnSCEVExprContains(S, [](constSCEV *S) {
13588if (constauto *SU = dyn_cast<SCEVUnknown>(S))
13589return SU->getValue() ==nullptr;
13590returnfalse;
13591 });
13592}
13593
13594/// Return the size of an element read or written by Inst.
13595constSCEV *ScalarEvolution::getElementSize(Instruction *Inst) {
13596Type *Ty;
13597if (StoreInst *Store = dyn_cast<StoreInst>(Inst))
13598 Ty = Store->getValueOperand()->getType();
13599elseif (LoadInst *Load = dyn_cast<LoadInst>(Inst))
13600 Ty = Load->getType();
13601else
13602returnnullptr;
13603
13604Type *ETy =getEffectiveSCEVType(PointerType::getUnqual(Inst->getContext()));
13605returngetSizeOfExpr(ETy, Ty);
13606}
13607
13608//===----------------------------------------------------------------------===//
13609// SCEVCallbackVH Class Implementation
13610//===----------------------------------------------------------------------===//
13611
13612void ScalarEvolution::SCEVCallbackVH::deleted() {
13613assert(SE &&"SCEVCallbackVH called with a null ScalarEvolution!");
13614if (PHINode *PN = dyn_cast<PHINode>(getValPtr()))
13615 SE->ConstantEvolutionLoopExitValue.erase(PN);
13616 SE->eraseValueFromMap(getValPtr());
13617// this now dangles!
13618}
13619
13620void ScalarEvolution::SCEVCallbackVH::allUsesReplacedWith(Value *V) {
13621assert(SE &&"SCEVCallbackVH called with a null ScalarEvolution!");
13622
13623// Forget all the expressions associated with users of the old value,
13624// so that future queries will recompute the expressions using the new
13625// value.
13626 SE->forgetValue(getValPtr());
13627// this now dangles!
13628}
13629
13630ScalarEvolution::SCEVCallbackVH::SCEVCallbackVH(Value *V,ScalarEvolution *se)
13631 :CallbackVH(V), SE(se) {}
13632
13633//===----------------------------------------------------------------------===//
13634// ScalarEvolution Class Implementation
13635//===----------------------------------------------------------------------===//
13636
13637ScalarEvolution::ScalarEvolution(Function &F,TargetLibraryInfo &TLI,
13638AssumptionCache &AC,DominatorTree &DT,
13639LoopInfo &LI)
13640 :F(F),DL(F.getDataLayout()), TLI(TLI), AC(AC), DT(DT), LI(LI),
13641 CouldNotCompute(newSCEVCouldNotCompute()), ValuesAtScopes(64),
13642 LoopDispositions(64), BlockDispositions(64) {
13643// To use guards for proving predicates, we need to scan every instruction in
13644// relevant basic blocks, and not just terminators. Doing this is a waste of
13645// time if the IR does not actually contain any calls to
13646// @llvm.experimental.guard, so do a quick check and remember this beforehand.
13647//
13648// This pessimizes the case where a pass that preserves ScalarEvolution wants
13649// to _add_ guards to the module when there weren't any before, and wants
13650// ScalarEvolution to optimize based on those guards. For now we prefer to be
13651// efficient in lieu of being smart in that rather obscure case.
13652
13653auto *GuardDecl =Intrinsic::getDeclarationIfExists(
13654F.getParent(), Intrinsic::experimental_guard);
13655 HasGuards = GuardDecl && !GuardDecl->use_empty();
13656}
13657
13658ScalarEvolution::ScalarEvolution(ScalarEvolution &&Arg)
13659 :F(Arg.F),DL(Arg.DL), HasGuards(Arg.HasGuards), TLI(Arg.TLI), AC(Arg.AC),
13660 DT(Arg.DT), LI(Arg.LI), CouldNotCompute(std::move(Arg.CouldNotCompute)),
13661 ValueExprMap(std::move(Arg.ValueExprMap)),
13662 PendingLoopPredicates(std::move(Arg.PendingLoopPredicates)),
13663 PendingPhiRanges(std::move(Arg.PendingPhiRanges)),
13664 PendingMerges(std::move(Arg.PendingMerges)),
13665 ConstantMultipleCache(std::move(Arg.ConstantMultipleCache)),
13666 BackedgeTakenCounts(std::move(Arg.BackedgeTakenCounts)),
13667 PredicatedBackedgeTakenCounts(
13668std::move(Arg.PredicatedBackedgeTakenCounts)),
13669 BECountUsers(std::move(Arg.BECountUsers)),
13670 ConstantEvolutionLoopExitValue(
13671std::move(Arg.ConstantEvolutionLoopExitValue)),
13672 ValuesAtScopes(std::move(Arg.ValuesAtScopes)),
13673 ValuesAtScopesUsers(std::move(Arg.ValuesAtScopesUsers)),
13674 LoopDispositions(std::move(Arg.LoopDispositions)),
13675 LoopPropertiesCache(std::move(Arg.LoopPropertiesCache)),
13676 BlockDispositions(std::move(Arg.BlockDispositions)),
13677 SCEVUsers(std::move(Arg.SCEVUsers)),
13678 UnsignedRanges(std::move(Arg.UnsignedRanges)),
13679 SignedRanges(std::move(Arg.SignedRanges)),
13680 UniqueSCEVs(std::move(Arg.UniqueSCEVs)),
13681 UniquePreds(std::move(Arg.UniquePreds)),
13682 SCEVAllocator(std::move(Arg.SCEVAllocator)),
13683 LoopUsers(std::move(Arg.LoopUsers)),
13684 PredicatedSCEVRewrites(std::move(Arg.PredicatedSCEVRewrites)),
13685 FirstUnknown(Arg.FirstUnknown) {
13686 Arg.FirstUnknown =nullptr;
13687}
13688
13689ScalarEvolution::~ScalarEvolution() {
13690// Iterate through all the SCEVUnknown instances and call their
13691// destructors, so that they release their references to their values.
13692for (SCEVUnknown *U = FirstUnknown; U;) {
13693SCEVUnknown *Tmp = U;
13694 U = U->Next;
13695 Tmp->~SCEVUnknown();
13696 }
13697 FirstUnknown =nullptr;
13698
13699 ExprValueMap.clear();
13700 ValueExprMap.clear();
13701 HasRecMap.clear();
13702 BackedgeTakenCounts.clear();
13703 PredicatedBackedgeTakenCounts.clear();
13704
13705assert(PendingLoopPredicates.empty() &&"isImpliedCond garbage");
13706assert(PendingPhiRanges.empty() &&"getRangeRef garbage");
13707assert(PendingMerges.empty() &&"isImpliedViaMerge garbage");
13708assert(!WalkingBEDominatingConds &&"isLoopBackedgeGuardedByCond garbage!");
13709assert(!ProvingSplitPredicate &&"ProvingSplitPredicate garbage!");
13710}
13711
13712boolScalarEvolution::hasLoopInvariantBackedgeTakenCount(constLoop *L) {
13713return !isa<SCEVCouldNotCompute>(getBackedgeTakenCount(L));
13714}
13715
13716/// When printing a top-level SCEV for trip counts, it's helpful to include
13717/// a type for constants which are otherwise hard to disambiguate.
13718staticvoidPrintSCEVWithTypeHint(raw_ostream &OS,constSCEV* S) {
13719if (isa<SCEVConstant>(S))
13720OS << *S->getType() <<" ";
13721OS << *S;
13722}
13723
13724staticvoidPrintLoopInfo(raw_ostream &OS,ScalarEvolution *SE,
13725constLoop *L) {
13726// Print all inner loops first
13727for (Loop *I : *L)
13728PrintLoopInfo(OS, SE,I);
13729
13730OS <<"Loop ";
13731 L->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13732OS <<": ";
13733
13734SmallVector<BasicBlock *, 8> ExitingBlocks;
13735 L->getExitingBlocks(ExitingBlocks);
13736if (ExitingBlocks.size() != 1)
13737OS <<"<multiple exits> ";
13738
13739auto *BTC = SE->getBackedgeTakenCount(L);
13740if (!isa<SCEVCouldNotCompute>(BTC)) {
13741OS <<"backedge-taken count is ";
13742PrintSCEVWithTypeHint(OS, BTC);
13743 }else
13744OS <<"Unpredictable backedge-taken count.";
13745OS <<"\n";
13746
13747if (ExitingBlocks.size() > 1)
13748for (BasicBlock *ExitingBlock : ExitingBlocks) {
13749OS <<" exit count for " << ExitingBlock->getName() <<": ";
13750constSCEV *EC = SE->getExitCount(L, ExitingBlock);
13751PrintSCEVWithTypeHint(OS, EC);
13752if (isa<SCEVCouldNotCompute>(EC)) {
13753// Retry with predicates.
13754SmallVector<const SCEVPredicate *> Predicates;
13755 EC = SE->getPredicatedExitCount(L, ExitingBlock, &Predicates);
13756if (!isa<SCEVCouldNotCompute>(EC)) {
13757OS <<"\n predicated exit count for " << ExitingBlock->getName()
13758 <<": ";
13759PrintSCEVWithTypeHint(OS, EC);
13760OS <<"\n Predicates:\n";
13761for (constauto *P : Predicates)
13762P->print(OS, 4);
13763 }
13764 }
13765OS <<"\n";
13766 }
13767
13768OS <<"Loop ";
13769 L->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13770OS <<": ";
13771
13772auto *ConstantBTC = SE->getConstantMaxBackedgeTakenCount(L);
13773if (!isa<SCEVCouldNotCompute>(ConstantBTC)) {
13774OS <<"constant max backedge-taken count is ";
13775PrintSCEVWithTypeHint(OS, ConstantBTC);
13776if (SE->isBackedgeTakenCountMaxOrZero(L))
13777OS <<", actual taken count either this or zero.";
13778 }else {
13779OS <<"Unpredictable constant max backedge-taken count. ";
13780 }
13781
13782OS <<"\n"
13783"Loop ";
13784 L->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13785OS <<": ";
13786
13787auto *SymbolicBTC = SE->getSymbolicMaxBackedgeTakenCount(L);
13788if (!isa<SCEVCouldNotCompute>(SymbolicBTC)) {
13789OS <<"symbolic max backedge-taken count is ";
13790PrintSCEVWithTypeHint(OS, SymbolicBTC);
13791if (SE->isBackedgeTakenCountMaxOrZero(L))
13792OS <<", actual taken count either this or zero.";
13793 }else {
13794OS <<"Unpredictable symbolic max backedge-taken count. ";
13795 }
13796OS <<"\n";
13797
13798if (ExitingBlocks.size() > 1)
13799for (BasicBlock *ExitingBlock : ExitingBlocks) {
13800OS <<" symbolic max exit count for " << ExitingBlock->getName() <<": ";
13801auto *ExitBTC = SE->getExitCount(L, ExitingBlock,
13802ScalarEvolution::SymbolicMaximum);
13803PrintSCEVWithTypeHint(OS, ExitBTC);
13804if (isa<SCEVCouldNotCompute>(ExitBTC)) {
13805// Retry with predicates.
13806SmallVector<const SCEVPredicate *> Predicates;
13807 ExitBTC = SE->getPredicatedExitCount(L, ExitingBlock, &Predicates,
13808ScalarEvolution::SymbolicMaximum);
13809if (!isa<SCEVCouldNotCompute>(ExitBTC)) {
13810OS <<"\n predicated symbolic max exit count for "
13811 << ExitingBlock->getName() <<": ";
13812PrintSCEVWithTypeHint(OS, ExitBTC);
13813OS <<"\n Predicates:\n";
13814for (constauto *P : Predicates)
13815P->print(OS, 4);
13816 }
13817 }
13818OS <<"\n";
13819 }
13820
13821SmallVector<const SCEVPredicate *, 4> Preds;
13822auto *PBT = SE->getPredicatedBackedgeTakenCount(L, Preds);
13823if (PBT != BTC) {
13824assert(!Preds.empty() &&"Different predicated BTC, but no predicates");
13825OS <<"Loop ";
13826 L->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13827OS <<": ";
13828if (!isa<SCEVCouldNotCompute>(PBT)) {
13829OS <<"Predicated backedge-taken count is ";
13830PrintSCEVWithTypeHint(OS, PBT);
13831 }else
13832OS <<"Unpredictable predicated backedge-taken count.";
13833OS <<"\n";
13834OS <<" Predicates:\n";
13835for (constauto *P : Preds)
13836P->print(OS, 4);
13837 }
13838 Preds.clear();
13839
13840auto *PredConstantMax =
13841 SE->getPredicatedConstantMaxBackedgeTakenCount(L, Preds);
13842if (PredConstantMax != ConstantBTC) {
13843assert(!Preds.empty() &&
13844"different predicated constant max BTC but no predicates");
13845OS <<"Loop ";
13846 L->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13847OS <<": ";
13848if (!isa<SCEVCouldNotCompute>(PredConstantMax)) {
13849OS <<"Predicated constant max backedge-taken count is ";
13850PrintSCEVWithTypeHint(OS, PredConstantMax);
13851 }else
13852OS <<"Unpredictable predicated constant max backedge-taken count.";
13853OS <<"\n";
13854OS <<" Predicates:\n";
13855for (constauto *P : Preds)
13856P->print(OS, 4);
13857 }
13858 Preds.clear();
13859
13860auto *PredSymbolicMax =
13861 SE->getPredicatedSymbolicMaxBackedgeTakenCount(L, Preds);
13862if (SymbolicBTC != PredSymbolicMax) {
13863assert(!Preds.empty() &&
13864"Different predicated symbolic max BTC, but no predicates");
13865OS <<"Loop ";
13866 L->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13867OS <<": ";
13868if (!isa<SCEVCouldNotCompute>(PredSymbolicMax)) {
13869OS <<"Predicated symbolic max backedge-taken count is ";
13870PrintSCEVWithTypeHint(OS, PredSymbolicMax);
13871 }else
13872OS <<"Unpredictable predicated symbolic max backedge-taken count.";
13873OS <<"\n";
13874OS <<" Predicates:\n";
13875for (constauto *P : Preds)
13876P->print(OS, 4);
13877 }
13878
13879if (SE->hasLoopInvariantBackedgeTakenCount(L)) {
13880OS <<"Loop ";
13881 L->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13882OS <<": ";
13883OS <<"Trip multiple is " << SE->getSmallConstantTripMultiple(L) <<"\n";
13884 }
13885}
13886
13887namespacellvm {
13888raw_ostream &operator<<(raw_ostream &OS,ScalarEvolution::LoopDisposition LD) {
13889switch (LD) {
13890caseScalarEvolution::LoopVariant:
13891OS <<"Variant";
13892break;
13893caseScalarEvolution::LoopInvariant:
13894OS <<"Invariant";
13895break;
13896caseScalarEvolution::LoopComputable:
13897OS <<"Computable";
13898break;
13899 }
13900returnOS;
13901}
13902
13903raw_ostream &operator<<(raw_ostream &OS,ScalarEvolution::BlockDisposition BD) {
13904switch (BD) {
13905caseScalarEvolution::DoesNotDominateBlock:
13906OS <<"DoesNotDominate";
13907break;
13908caseScalarEvolution::DominatesBlock:
13909OS <<"Dominates";
13910break;
13911caseScalarEvolution::ProperlyDominatesBlock:
13912OS <<"ProperlyDominates";
13913break;
13914 }
13915returnOS;
13916}
13917}// namespace llvm
13918
13919voidScalarEvolution::print(raw_ostream &OS) const{
13920// ScalarEvolution's implementation of the print method is to print
13921// out SCEV values of all instructions that are interesting. Doing
13922// this potentially causes it to create new SCEV objects though,
13923// which technically conflicts with the const qualifier. This isn't
13924// observable from outside the class though, so casting away the
13925// const isn't dangerous.
13926ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
13927
13928if (ClassifyExpressions) {
13929OS <<"Classifying expressions for: ";
13930 F.printAsOperand(OS,/*PrintType=*/false);
13931OS <<"\n";
13932for (Instruction &I :instructions(F))
13933if (isSCEVable(I.getType()) && !isa<CmpInst>(I)) {
13934OS <<I <<'\n';
13935OS <<" --> ";
13936constSCEV *SV = SE.getSCEV(&I);
13937 SV->print(OS);
13938if (!isa<SCEVCouldNotCompute>(SV)) {
13939OS <<" U: ";
13940 SE.getUnsignedRange(SV).print(OS);
13941OS <<" S: ";
13942 SE.getSignedRange(SV).print(OS);
13943 }
13944
13945constLoop *L = LI.getLoopFor(I.getParent());
13946
13947constSCEV *AtUse = SE.getSCEVAtScope(SV, L);
13948if (AtUse != SV) {
13949OS <<" --> ";
13950 AtUse->print(OS);
13951if (!isa<SCEVCouldNotCompute>(AtUse)) {
13952OS <<" U: ";
13953 SE.getUnsignedRange(AtUse).print(OS);
13954OS <<" S: ";
13955 SE.getSignedRange(AtUse).print(OS);
13956 }
13957 }
13958
13959if (L) {
13960OS <<"\t\t""Exits: ";
13961constSCEV *ExitValue = SE.getSCEVAtScope(SV, L->getParentLoop());
13962if (!SE.isLoopInvariant(ExitValue, L)) {
13963OS <<"<<Unknown>>";
13964 }else {
13965OS << *ExitValue;
13966 }
13967
13968boolFirst =true;
13969for (constauto *Iter = L; Iter; Iter = Iter->getParentLoop()) {
13970if (First) {
13971OS <<"\t\t""LoopDispositions: { ";
13972First =false;
13973 }else {
13974OS <<", ";
13975 }
13976
13977 Iter->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13978OS <<": " << SE.getLoopDisposition(SV, Iter);
13979 }
13980
13981for (constauto *InnerL :depth_first(L)) {
13982if (InnerL == L)
13983continue;
13984if (First) {
13985OS <<"\t\t""LoopDispositions: { ";
13986First =false;
13987 }else {
13988OS <<", ";
13989 }
13990
13991 InnerL->getHeader()->printAsOperand(OS,/*PrintType=*/false);
13992OS <<": " << SE.getLoopDisposition(SV, InnerL);
13993 }
13994
13995OS <<" }";
13996 }
13997
13998OS <<"\n";
13999 }
14000 }
14001
14002OS <<"Determining loop execution counts for: ";
14003 F.printAsOperand(OS,/*PrintType=*/false);
14004OS <<"\n";
14005for (Loop *I : LI)
14006PrintLoopInfo(OS, &SE,I);
14007}
14008
14009ScalarEvolution::LoopDisposition
14010ScalarEvolution::getLoopDisposition(constSCEV *S,constLoop *L) {
14011auto &Values = LoopDispositions[S];
14012for (auto &V : Values) {
14013if (V.getPointer() == L)
14014return V.getInt();
14015 }
14016 Values.emplace_back(L,LoopVariant);
14017LoopDispositionD = computeLoopDisposition(S, L);
14018auto &Values2 = LoopDispositions[S];
14019for (auto &V :llvm::reverse(Values2)) {
14020if (V.getPointer() == L) {
14021 V.setInt(D);
14022break;
14023 }
14024 }
14025returnD;
14026}
14027
14028ScalarEvolution::LoopDisposition
14029ScalarEvolution::computeLoopDisposition(constSCEV *S,constLoop *L) {
14030switch (S->getSCEVType()) {
14031casescConstant:
14032casescVScale:
14033returnLoopInvariant;
14034casescAddRecExpr: {
14035constSCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
14036
14037// If L is the addrec's loop, it's computable.
14038if (AR->getLoop() == L)
14039returnLoopComputable;
14040
14041// Add recurrences are never invariant in the function-body (null loop).
14042if (!L)
14043returnLoopVariant;
14044
14045// Everything that is not defined at loop entry is variant.
14046if (DT.dominates(L->getHeader(), AR->getLoop()->getHeader()))
14047returnLoopVariant;
14048assert(!L->contains(AR->getLoop()) &&"Containing loop's header does not"
14049" dominate the contained loop's header?");
14050
14051// This recurrence is invariant w.r.t. L if AR's loop contains L.
14052if (AR->getLoop()->contains(L))
14053returnLoopInvariant;
14054
14055// This recurrence is variant w.r.t. L if any of its operands
14056// are variant.
14057for (constauto *Op : AR->operands())
14058if (!isLoopInvariant(Op, L))
14059returnLoopVariant;
14060
14061// Otherwise it's loop-invariant.
14062returnLoopInvariant;
14063 }
14064casescTruncate:
14065casescZeroExtend:
14066casescSignExtend:
14067casescPtrToInt:
14068casescAddExpr:
14069casescMulExpr:
14070casescUDivExpr:
14071casescUMaxExpr:
14072casescSMaxExpr:
14073casescUMinExpr:
14074casescSMinExpr:
14075casescSequentialUMinExpr: {
14076bool HasVarying =false;
14077for (constauto *Op : S->operands()) {
14078LoopDispositionD =getLoopDisposition(Op, L);
14079if (D ==LoopVariant)
14080returnLoopVariant;
14081if (D ==LoopComputable)
14082 HasVarying =true;
14083 }
14084return HasVarying ?LoopComputable :LoopInvariant;
14085 }
14086casescUnknown:
14087// All non-instruction values are loop invariant. All instructions are loop
14088// invariant if they are not contained in the specified loop.
14089// Instructions are never considered invariant in the function body
14090// (null loop) because they are defined within the "loop".
14091if (auto *I = dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue()))
14092return (L && !L->contains(I)) ?LoopInvariant :LoopVariant;
14093returnLoopInvariant;
14094casescCouldNotCompute:
14095llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
14096 }
14097llvm_unreachable("Unknown SCEV kind!");
14098}
14099
14100boolScalarEvolution::isLoopInvariant(constSCEV *S,constLoop *L) {
14101returngetLoopDisposition(S, L) ==LoopInvariant;
14102}
14103
14104boolScalarEvolution::hasComputableLoopEvolution(constSCEV *S,constLoop *L) {
14105returngetLoopDisposition(S, L) ==LoopComputable;
14106}
14107
14108ScalarEvolution::BlockDisposition
14109ScalarEvolution::getBlockDisposition(constSCEV *S,constBasicBlock *BB) {
14110auto &Values = BlockDispositions[S];
14111for (auto &V : Values) {
14112if (V.getPointer() == BB)
14113return V.getInt();
14114 }
14115 Values.emplace_back(BB,DoesNotDominateBlock);
14116BlockDispositionD = computeBlockDisposition(S, BB);
14117auto &Values2 = BlockDispositions[S];
14118for (auto &V :llvm::reverse(Values2)) {
14119if (V.getPointer() == BB) {
14120 V.setInt(D);
14121break;
14122 }
14123 }
14124returnD;
14125}
14126
14127ScalarEvolution::BlockDisposition
14128ScalarEvolution::computeBlockDisposition(constSCEV *S,constBasicBlock *BB) {
14129switch (S->getSCEVType()) {
14130casescConstant:
14131casescVScale:
14132returnProperlyDominatesBlock;
14133casescAddRecExpr: {
14134// This uses a "dominates" query instead of "properly dominates" query
14135// to test for proper dominance too, because the instruction which
14136// produces the addrec's value is a PHI, and a PHI effectively properly
14137// dominates its entire containing block.
14138constSCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(S);
14139if (!DT.dominates(AR->getLoop()->getHeader(), BB))
14140returnDoesNotDominateBlock;
14141
14142// Fall through into SCEVNAryExpr handling.
14143 [[fallthrough]];
14144 }
14145casescTruncate:
14146casescZeroExtend:
14147casescSignExtend:
14148casescPtrToInt:
14149casescAddExpr:
14150casescMulExpr:
14151casescUDivExpr:
14152casescUMaxExpr:
14153casescSMaxExpr:
14154casescUMinExpr:
14155casescSMinExpr:
14156casescSequentialUMinExpr: {
14157bool Proper =true;
14158for (constSCEV *NAryOp : S->operands()) {
14159BlockDispositionD =getBlockDisposition(NAryOp, BB);
14160if (D ==DoesNotDominateBlock)
14161returnDoesNotDominateBlock;
14162if (D ==DominatesBlock)
14163 Proper =false;
14164 }
14165return Proper ?ProperlyDominatesBlock :DominatesBlock;
14166 }
14167casescUnknown:
14168if (Instruction *I =
14169 dyn_cast<Instruction>(cast<SCEVUnknown>(S)->getValue())) {
14170if (I->getParent() == BB)
14171returnDominatesBlock;
14172if (DT.properlyDominates(I->getParent(), BB))
14173returnProperlyDominatesBlock;
14174returnDoesNotDominateBlock;
14175 }
14176returnProperlyDominatesBlock;
14177casescCouldNotCompute:
14178llvm_unreachable("Attempt to use a SCEVCouldNotCompute object!");
14179 }
14180llvm_unreachable("Unknown SCEV kind!");
14181}
14182
14183boolScalarEvolution::dominates(constSCEV *S,constBasicBlock *BB) {
14184returngetBlockDisposition(S, BB) >=DominatesBlock;
14185}
14186
14187boolScalarEvolution::properlyDominates(constSCEV *S,constBasicBlock *BB) {
14188returngetBlockDisposition(S, BB) ==ProperlyDominatesBlock;
14189}
14190
14191boolScalarEvolution::hasOperand(constSCEV *S,constSCEV *Op) const{
14192returnSCEVExprContains(S, [&](constSCEV *Expr) {return Expr ==Op; });
14193}
14194
14195void ScalarEvolution::forgetBackedgeTakenCounts(constLoop *L,
14196boolPredicated) {
14197auto &BECounts =
14198Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts;
14199auto It = BECounts.find(L);
14200if (It != BECounts.end()) {
14201for (const ExitNotTakenInfo &ENT : It->second.ExitNotTaken) {
14202for (constSCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) {
14203if (!isa<SCEVConstant>(S)) {
14204auto UserIt = BECountUsers.find(S);
14205assert(UserIt != BECountUsers.end());
14206 UserIt->second.erase({L,Predicated});
14207 }
14208 }
14209 }
14210 BECounts.erase(It);
14211 }
14212}
14213
14214void ScalarEvolution::forgetMemoizedResults(ArrayRef<const SCEV *> SCEVs) {
14215SmallPtrSet<const SCEV *, 8> ToForget(SCEVs.begin(), SCEVs.end());
14216SmallVector<const SCEV *, 8> Worklist(ToForget.begin(), ToForget.end());
14217
14218while (!Worklist.empty()) {
14219constSCEV *Curr = Worklist.pop_back_val();
14220autoUsers = SCEVUsers.find(Curr);
14221if (Users != SCEVUsers.end())
14222for (constauto *User :Users->second)
14223if (ToForget.insert(User).second)
14224 Worklist.push_back(User);
14225 }
14226
14227for (constauto *S : ToForget)
14228 forgetMemoizedResultsImpl(S);
14229
14230for (autoI = PredicatedSCEVRewrites.begin();
14231I != PredicatedSCEVRewrites.end();) {
14232 std::pair<const SCEV *, const Loop *>Entry =I->first;
14233if (ToForget.count(Entry.first))
14234 PredicatedSCEVRewrites.erase(I++);
14235else
14236 ++I;
14237 }
14238}
14239
14240void ScalarEvolution::forgetMemoizedResultsImpl(constSCEV *S) {
14241 LoopDispositions.erase(S);
14242 BlockDispositions.erase(S);
14243 UnsignedRanges.erase(S);
14244 SignedRanges.erase(S);
14245 HasRecMap.erase(S);
14246 ConstantMultipleCache.erase(S);
14247
14248if (auto *AR = dyn_cast<SCEVAddRecExpr>(S)) {
14249 UnsignedWrapViaInductionTried.erase(AR);
14250 SignedWrapViaInductionTried.erase(AR);
14251 }
14252
14253auto ExprIt = ExprValueMap.find(S);
14254if (ExprIt != ExprValueMap.end()) {
14255for (Value *V : ExprIt->second) {
14256auto ValueIt = ValueExprMap.find_as(V);
14257if (ValueIt != ValueExprMap.end())
14258 ValueExprMap.erase(ValueIt);
14259 }
14260 ExprValueMap.erase(ExprIt);
14261 }
14262
14263auto ScopeIt = ValuesAtScopes.find(S);
14264if (ScopeIt != ValuesAtScopes.end()) {
14265for (constauto &Pair : ScopeIt->second)
14266if (!isa_and_nonnull<SCEVConstant>(Pair.second))
14267llvm::erase(ValuesAtScopesUsers[Pair.second],
14268 std::make_pair(Pair.first, S));
14269 ValuesAtScopes.erase(ScopeIt);
14270 }
14271
14272auto ScopeUserIt = ValuesAtScopesUsers.find(S);
14273if (ScopeUserIt != ValuesAtScopesUsers.end()) {
14274for (constauto &Pair : ScopeUserIt->second)
14275llvm::erase(ValuesAtScopes[Pair.second], std::make_pair(Pair.first, S));
14276 ValuesAtScopesUsers.erase(ScopeUserIt);
14277 }
14278
14279auto BEUsersIt = BECountUsers.find(S);
14280if (BEUsersIt != BECountUsers.end()) {
14281// Work on a copy, as forgetBackedgeTakenCounts() will modify the original.
14282autoCopy = BEUsersIt->second;
14283for (constauto &Pair : Copy)
14284 forgetBackedgeTakenCounts(Pair.getPointer(), Pair.getInt());
14285 BECountUsers.erase(BEUsersIt);
14286 }
14287
14288auto FoldUser = FoldCacheUser.find(S);
14289if (FoldUser != FoldCacheUser.end())
14290for (auto &KV : FoldUser->second)
14291 FoldCache.erase(KV);
14292 FoldCacheUser.erase(S);
14293}
14294
14295void
14296ScalarEvolution::getUsedLoops(constSCEV *S,
14297SmallPtrSetImpl<const Loop *> &LoopsUsed) {
14298structFindUsedLoops {
14299 FindUsedLoops(SmallPtrSetImpl<const Loop *> &LoopsUsed)
14300 : LoopsUsed(LoopsUsed) {}
14301SmallPtrSetImpl<const Loop *> &LoopsUsed;
14302bool follow(constSCEV *S) {
14303if (auto *AR = dyn_cast<SCEVAddRecExpr>(S))
14304 LoopsUsed.insert(AR->getLoop());
14305returntrue;
14306 }
14307
14308bool isDone() const{returnfalse; }
14309 };
14310
14311 FindUsedLoops F(LoopsUsed);
14312SCEVTraversal<FindUsedLoops>(F).visitAll(S);
14313}
14314
14315void ScalarEvolution::getReachableBlocks(
14316SmallPtrSetImpl<BasicBlock *> &Reachable,Function &F) {
14317SmallVector<BasicBlock *> Worklist;
14318 Worklist.push_back(&F.getEntryBlock());
14319while (!Worklist.empty()) {
14320BasicBlock *BB = Worklist.pop_back_val();
14321if (!Reachable.insert(BB).second)
14322continue;
14323
14324Value *Cond;
14325BasicBlock *TrueBB, *FalseBB;
14326if (match(BB->getTerminator(),m_Br(m_Value(Cond),m_BasicBlock(TrueBB),
14327m_BasicBlock(FalseBB)))) {
14328if (auto *C = dyn_cast<ConstantInt>(Cond)) {
14329 Worklist.push_back(C->isOne() ? TrueBB : FalseBB);
14330continue;
14331 }
14332
14333if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
14334constSCEV *L =getSCEV(Cmp->getOperand(0));
14335constSCEV *R =getSCEV(Cmp->getOperand(1));
14336if (isKnownPredicateViaConstantRanges(Cmp->getCmpPredicate(), L, R)) {
14337 Worklist.push_back(TrueBB);
14338continue;
14339 }
14340if (isKnownPredicateViaConstantRanges(Cmp->getInverseCmpPredicate(), L,
14341 R)) {
14342 Worklist.push_back(FalseBB);
14343continue;
14344 }
14345 }
14346 }
14347
14348append_range(Worklist,successors(BB));
14349 }
14350}
14351
14352voidScalarEvolution::verify() const{
14353ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
14354ScalarEvolution SE2(F, TLI, AC, DT, LI);
14355
14356SmallVector<Loop *, 8> LoopStack(LI.begin(), LI.end());
14357
14358// Map's SCEV expressions from one ScalarEvolution "universe" to another.
14359structSCEVMapper :publicSCEVRewriteVisitor<SCEVMapper> {
14360 SCEVMapper(ScalarEvolution &SE) :SCEVRewriteVisitor<SCEVMapper>(SE) {}
14361
14362constSCEV *visitConstant(constSCEVConstant *Constant) {
14363return SE.getConstant(Constant->getAPInt());
14364 }
14365
14366constSCEV *visitUnknown(constSCEVUnknown *Expr) {
14367return SE.getUnknown(Expr->getValue());
14368 }
14369
14370constSCEV *visitCouldNotCompute(constSCEVCouldNotCompute *Expr) {
14371return SE.getCouldNotCompute();
14372 }
14373 };
14374
14375 SCEVMapper SCM(SE2);
14376SmallPtrSet<BasicBlock *, 16> ReachableBlocks;
14377 SE2.getReachableBlocks(ReachableBlocks,F);
14378
14379auto GetDelta = [&](constSCEV *Old,constSCEV *New) ->constSCEV * {
14380if (containsUndefs(Old) ||containsUndefs(New)) {
14381// SCEV treats "undef" as an unknown but consistent value (i.e. it does
14382// not propagate undef aggressively). This means we can (and do) fail
14383// verification in cases where a transform makes a value go from "undef"
14384// to "undef+1" (say). The transform is fine, since in both cases the
14385// result is "undef", but SCEV thinks the value increased by 1.
14386returnnullptr;
14387 }
14388
14389// Unless VerifySCEVStrict is set, we only compare constant deltas.
14390constSCEV *Delta = SE2.getMinusSCEV(Old, New);
14391if (!VerifySCEVStrict && !isa<SCEVConstant>(Delta))
14392returnnullptr;
14393
14394return Delta;
14395 };
14396
14397while (!LoopStack.empty()) {
14398auto *L = LoopStack.pop_back_val();
14399llvm::append_range(LoopStack, *L);
14400
14401// Only verify BECounts in reachable loops. For an unreachable loop,
14402// any BECount is legal.
14403if (!ReachableBlocks.contains(L->getHeader()))
14404continue;
14405
14406// Only verify cached BECounts. Computing new BECounts may change the
14407// results of subsequent SCEV uses.
14408auto It = BackedgeTakenCounts.find(L);
14409if (It == BackedgeTakenCounts.end())
14410continue;
14411
14412auto *CurBECount =
14413 SCM.visit(It->second.getExact(L,const_cast<ScalarEvolution *>(this)));
14414auto *NewBECount = SE2.getBackedgeTakenCount(L);
14415
14416if (CurBECount == SE2.getCouldNotCompute() ||
14417 NewBECount == SE2.getCouldNotCompute()) {
14418// NB! This situation is legal, but is very suspicious -- whatever pass
14419// change the loop to make a trip count go from could not compute to
14420// computable or vice-versa *should have* invalidated SCEV. However, we
14421// choose not to assert here (for now) since we don't want false
14422// positives.
14423continue;
14424 }
14425
14426if (SE.getTypeSizeInBits(CurBECount->getType()) >
14427 SE.getTypeSizeInBits(NewBECount->getType()))
14428 NewBECount = SE2.getZeroExtendExpr(NewBECount, CurBECount->getType());
14429elseif (SE.getTypeSizeInBits(CurBECount->getType()) <
14430 SE.getTypeSizeInBits(NewBECount->getType()))
14431 CurBECount = SE2.getZeroExtendExpr(CurBECount, NewBECount->getType());
14432
14433constSCEV *Delta = GetDelta(CurBECount, NewBECount);
14434if (Delta && !Delta->isZero()) {
14435dbgs() <<"Trip Count for " << *L <<" Changed!\n";
14436dbgs() <<"Old: " << *CurBECount <<"\n";
14437dbgs() <<"New: " << *NewBECount <<"\n";
14438dbgs() <<"Delta: " << *Delta <<"\n";
14439 std::abort();
14440 }
14441 }
14442
14443// Collect all valid loops currently in LoopInfo.
14444SmallPtrSet<Loop *, 32> ValidLoops;
14445SmallVector<Loop *, 32> Worklist(LI.begin(), LI.end());
14446while (!Worklist.empty()) {
14447Loop *L = Worklist.pop_back_val();
14448if (ValidLoops.insert(L).second)
14449 Worklist.append(L->begin(), L->end());
14450 }
14451for (constauto &KV : ValueExprMap) {
14452#ifndef NDEBUG
14453// Check for SCEV expressions referencing invalid/deleted loops.
14454if (auto *AR = dyn_cast<SCEVAddRecExpr>(KV.second)) {
14455assert(ValidLoops.contains(AR->getLoop()) &&
14456"AddRec references invalid loop");
14457 }
14458#endif
14459
14460// Check that the value is also part of the reverse map.
14461auto It = ExprValueMap.find(KV.second);
14462if (It == ExprValueMap.end() || !It->second.contains(KV.first)) {
14463dbgs() <<"Value " << *KV.first
14464 <<" is in ValueExprMap but not in ExprValueMap\n";
14465 std::abort();
14466 }
14467
14468if (auto *I = dyn_cast<Instruction>(&*KV.first)) {
14469if (!ReachableBlocks.contains(I->getParent()))
14470continue;
14471constSCEV *OldSCEV = SCM.visit(KV.second);
14472constSCEV *NewSCEV = SE2.getSCEV(I);
14473constSCEV *Delta = GetDelta(OldSCEV, NewSCEV);
14474if (Delta && !Delta->isZero()) {
14475dbgs() <<"SCEV for value " << *I <<" changed!\n"
14476 <<"Old: " << *OldSCEV <<"\n"
14477 <<"New: " << *NewSCEV <<"\n"
14478 <<"Delta: " << *Delta <<"\n";
14479 std::abort();
14480 }
14481 }
14482 }
14483
14484for (constauto &KV : ExprValueMap) {
14485for (Value *V : KV.second) {
14486auto It = ValueExprMap.find_as(V);
14487if (It == ValueExprMap.end()) {
14488dbgs() <<"Value " << *V
14489 <<" is in ExprValueMap but not in ValueExprMap\n";
14490 std::abort();
14491 }
14492if (It->second != KV.first) {
14493dbgs() <<"Value " << *V <<" mapped to " << *It->second
14494 <<" rather than " << *KV.first <<"\n";
14495 std::abort();
14496 }
14497 }
14498 }
14499
14500// Verify integrity of SCEV users.
14501for (constauto &S : UniqueSCEVs) {
14502for (constauto *Op : S.operands()) {
14503// We do not store dependencies of constants.
14504if (isa<SCEVConstant>(Op))
14505continue;
14506auto It = SCEVUsers.find(Op);
14507if (It != SCEVUsers.end() && It->second.count(&S))
14508continue;
14509dbgs() <<"Use of operand " << *Op <<" by user " << S
14510 <<" is not being tracked!\n";
14511 std::abort();
14512 }
14513 }
14514
14515// Verify integrity of ValuesAtScopes users.
14516for (constauto &ValueAndVec : ValuesAtScopes) {
14517constSCEV *Value = ValueAndVec.first;
14518for (constauto &LoopAndValueAtScope : ValueAndVec.second) {
14519constLoop *L = LoopAndValueAtScope.first;
14520constSCEV *ValueAtScope = LoopAndValueAtScope.second;
14521if (!isa<SCEVConstant>(ValueAtScope)) {
14522auto It = ValuesAtScopesUsers.find(ValueAtScope);
14523if (It != ValuesAtScopesUsers.end() &&
14524is_contained(It->second, std::make_pair(L,Value)))
14525continue;
14526dbgs() <<"Value: " << *Value <<", Loop: " << *L <<", ValueAtScope: "
14527 << *ValueAtScope <<" missing in ValuesAtScopesUsers\n";
14528 std::abort();
14529 }
14530 }
14531 }
14532
14533for (constauto &ValueAtScopeAndVec : ValuesAtScopesUsers) {
14534constSCEV *ValueAtScope = ValueAtScopeAndVec.first;
14535for (constauto &LoopAndValue : ValueAtScopeAndVec.second) {
14536constLoop *L = LoopAndValue.first;
14537constSCEV *Value = LoopAndValue.second;
14538assert(!isa<SCEVConstant>(Value));
14539auto It = ValuesAtScopes.find(Value);
14540if (It != ValuesAtScopes.end() &&
14541is_contained(It->second, std::make_pair(L, ValueAtScope)))
14542continue;
14543dbgs() <<"Value: " << *Value <<", Loop: " << *L <<", ValueAtScope: "
14544 << *ValueAtScope <<" missing in ValuesAtScopes\n";
14545 std::abort();
14546 }
14547 }
14548
14549// Verify integrity of BECountUsers.
14550auto VerifyBECountUsers = [&](boolPredicated) {
14551auto &BECounts =
14552Predicated ? PredicatedBackedgeTakenCounts : BackedgeTakenCounts;
14553for (constauto &LoopAndBEInfo : BECounts) {
14554for (const ExitNotTakenInfo &ENT : LoopAndBEInfo.second.ExitNotTaken) {
14555for (constSCEV *S : {ENT.ExactNotTaken, ENT.SymbolicMaxNotTaken}) {
14556if (!isa<SCEVConstant>(S)) {
14557auto UserIt = BECountUsers.find(S);
14558if (UserIt != BECountUsers.end() &&
14559 UserIt->second.contains({ LoopAndBEInfo.first,Predicated }))
14560continue;
14561dbgs() <<"Value " << *S <<" for loop " << *LoopAndBEInfo.first
14562 <<" missing from BECountUsers\n";
14563 std::abort();
14564 }
14565 }
14566 }
14567 }
14568 };
14569 VerifyBECountUsers(/* Predicated */false);
14570 VerifyBECountUsers(/* Predicated */true);
14571
14572// Verify intergity of loop disposition cache.
14573for (auto &[S, Values] : LoopDispositions) {
14574for (auto [Loop, CachedDisposition] : Values) {
14575constauto RecomputedDisposition = SE2.getLoopDisposition(S,Loop);
14576if (CachedDisposition != RecomputedDisposition) {
14577dbgs() <<"Cached disposition of " << *S <<" for loop " << *Loop
14578 <<" is incorrect: cached " << CachedDisposition <<", actual "
14579 << RecomputedDisposition <<"\n";
14580 std::abort();
14581 }
14582 }
14583 }
14584
14585// Verify integrity of the block disposition cache.
14586for (auto &[S, Values] : BlockDispositions) {
14587for (auto [BB, CachedDisposition] : Values) {
14588constauto RecomputedDisposition = SE2.getBlockDisposition(S, BB);
14589if (CachedDisposition != RecomputedDisposition) {
14590dbgs() <<"Cached disposition of " << *S <<" for block %"
14591 << BB->getName() <<" is incorrect: cached " << CachedDisposition
14592 <<", actual " << RecomputedDisposition <<"\n";
14593 std::abort();
14594 }
14595 }
14596 }
14597
14598// Verify FoldCache/FoldCacheUser caches.
14599for (auto [FoldID, Expr] : FoldCache) {
14600autoI = FoldCacheUser.find(Expr);
14601if (I == FoldCacheUser.end()) {
14602dbgs() <<"Missing entry in FoldCacheUser for cached expression " << *Expr
14603 <<"!\n";
14604 std::abort();
14605 }
14606if (!is_contained(I->second,FoldID)) {
14607dbgs() <<"Missing FoldID in cached users of " << *Expr <<"!\n";
14608 std::abort();
14609 }
14610 }
14611for (auto [Expr, IDs] : FoldCacheUser) {
14612for (auto &FoldID : IDs) {
14613autoI = FoldCache.find(FoldID);
14614if (I == FoldCache.end()) {
14615dbgs() <<"Missing entry in FoldCache for expression " << *Expr
14616 <<"!\n";
14617 std::abort();
14618 }
14619if (I->second != Expr) {
14620dbgs() <<"Entry in FoldCache doesn't match FoldCacheUser: "
14621 << *I->second <<" != " << *Expr <<"!\n";
14622 std::abort();
14623 }
14624 }
14625 }
14626
14627// Verify that ConstantMultipleCache computations are correct. We check that
14628// cached multiples and recomputed multiples are multiples of each other to
14629// verify correctness. It is possible that a recomputed multiple is different
14630// from the cached multiple due to strengthened no wrap flags or changes in
14631// KnownBits computations.
14632for (auto [S, Multiple] : ConstantMultipleCache) {
14633APInt RecomputedMultiple = SE2.getConstantMultiple(S);
14634if ((Multiple != 0 && RecomputedMultiple != 0 &&
14635 Multiple.urem(RecomputedMultiple) != 0 &&
14636 RecomputedMultiple.urem(Multiple) != 0)) {
14637dbgs() <<"Incorrect cached computation in ConstantMultipleCache for "
14638 << *S <<" : Computed " << RecomputedMultiple
14639 <<" but cache contains " << Multiple <<"!\n";
14640 std::abort();
14641 }
14642 }
14643}
14644
14645boolScalarEvolution::invalidate(
14646Function &F,constPreservedAnalyses &PA,
14647FunctionAnalysisManager::Invalidator &Inv) {
14648// Invalidate the ScalarEvolution object whenever it isn't preserved or one
14649// of its dependencies is invalidated.
14650auto PAC = PA.getChecker<ScalarEvolutionAnalysis>();
14651return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
14652 Inv.invalidate<AssumptionAnalysis>(F, PA) ||
14653 Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
14654 Inv.invalidate<LoopAnalysis>(F, PA);
14655}
14656
14657AnalysisKey ScalarEvolutionAnalysis::Key;
14658
14659ScalarEvolutionScalarEvolutionAnalysis::run(Function &F,
14660FunctionAnalysisManager &AM) {
14661auto &TLI = AM.getResult<TargetLibraryAnalysis>(F);
14662auto &AC = AM.getResult<AssumptionAnalysis>(F);
14663auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
14664auto &LI = AM.getResult<LoopAnalysis>(F);
14665returnScalarEvolution(F, TLI, AC, DT, LI);
14666}
14667
14668PreservedAnalyses
14669ScalarEvolutionVerifierPass::run(Function &F,FunctionAnalysisManager &AM) {
14670 AM.getResult<ScalarEvolutionAnalysis>(F).verify();
14671returnPreservedAnalyses::all();
14672}
14673
14674PreservedAnalyses
14675ScalarEvolutionPrinterPass::run(Function &F,FunctionAnalysisManager &AM) {
14676// For compatibility with opt's -analyze feature under legacy pass manager
14677// which was not ported to NPM. This keeps tests using
14678// update_analyze_test_checks.py working.
14679 OS <<"Printing analysis 'Scalar Evolution Analysis' for function '"
14680 <<F.getName() <<"':\n";
14681 AM.getResult<ScalarEvolutionAnalysis>(F).print(OS);
14682returnPreservedAnalyses::all();
14683}
14684
14685INITIALIZE_PASS_BEGIN(ScalarEvolutionWrapperPass,"scalar-evolution",
14686"Scalar Evolution Analysis",false,true)
14687INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
14688INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
14689INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
14690INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
14691INITIALIZE_PASS_END(ScalarEvolutionWrapperPass, "scalar-evolution",
14692 "Scalar EvolutionAnalysis",false,true)
14693
14694charScalarEvolutionWrapperPass::ID = 0;
14695
14696ScalarEvolutionWrapperPass::ScalarEvolutionWrapperPass() :FunctionPass(ID) {
14697initializeScalarEvolutionWrapperPassPass(*PassRegistry::getPassRegistry());
14698}
14699
14700boolScalarEvolutionWrapperPass::runOnFunction(Function &F) {
14701 SE.reset(newScalarEvolution(
14702F, getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F),
14703 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F),
14704 getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
14705 getAnalysis<LoopInfoWrapperPass>().getLoopInfo()));
14706returnfalse;
14707}
14708
14709voidScalarEvolutionWrapperPass::releaseMemory() { SE.reset(); }
14710
14711voidScalarEvolutionWrapperPass::print(raw_ostream &OS,constModule *) const{
14712 SE->print(OS);
14713}
14714
14715voidScalarEvolutionWrapperPass::verifyAnalysis() const{
14716if (!VerifySCEV)
14717return;
14718
14719 SE->verify();
14720}
14721
14722voidScalarEvolutionWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const{
14723 AU.setPreservesAll();
14724 AU.addRequiredTransitive<AssumptionCacheTracker>();
14725 AU.addRequiredTransitive<LoopInfoWrapperPass>();
14726 AU.addRequiredTransitive<DominatorTreeWrapperPass>();
14727 AU.addRequiredTransitive<TargetLibraryInfoWrapperPass>();
14728}
14729
14730constSCEVPredicate *ScalarEvolution::getEqualPredicate(constSCEV *LHS,
14731constSCEV *RHS) {
14732returngetComparePredicate(ICmpInst::ICMP_EQ,LHS,RHS);
14733}
14734
14735constSCEVPredicate *
14736ScalarEvolution::getComparePredicate(constICmpInst::Predicate Pred,
14737constSCEV *LHS,constSCEV *RHS) {
14738FoldingSetNodeIDID;
14739assert(LHS->getType() ==RHS->getType() &&
14740"Type mismatch between LHS and RHS");
14741// Unique this node based on the arguments
14742ID.AddInteger(SCEVPredicate::P_Compare);
14743ID.AddInteger(Pred);
14744ID.AddPointer(LHS);
14745ID.AddPointer(RHS);
14746void *IP =nullptr;
14747if (constauto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
14748return S;
14749SCEVComparePredicate *Eq =new (SCEVAllocator)
14750SCEVComparePredicate(ID.Intern(SCEVAllocator), Pred,LHS,RHS);
14751 UniquePreds.InsertNode(Eq, IP);
14752return Eq;
14753}
14754
14755constSCEVPredicate *ScalarEvolution::getWrapPredicate(
14756constSCEVAddRecExpr *AR,
14757SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
14758FoldingSetNodeIDID;
14759// Unique this node based on the arguments
14760ID.AddInteger(SCEVPredicate::P_Wrap);
14761ID.AddPointer(AR);
14762ID.AddInteger(AddedFlags);
14763void *IP =nullptr;
14764if (constauto *S = UniquePreds.FindNodeOrInsertPos(ID, IP))
14765return S;
14766auto *OF =new (SCEVAllocator)
14767SCEVWrapPredicate(ID.Intern(SCEVAllocator), AR, AddedFlags);
14768 UniquePreds.InsertNode(OF, IP);
14769return OF;
14770}
14771
14772namespace{
14773
14774classSCEVPredicateRewriter :publicSCEVRewriteVisitor<SCEVPredicateRewriter> {
14775public:
14776
14777 /// Rewrites \p S in the context of a loop L and the SCEV predication
14778 /// infrastructure.
14779 ///
14780 /// If \p Pred is non-null, the SCEV expression is rewritten to respect the
14781 /// equivalences present in \p Pred.
14782 ///
14783 /// If \p NewPreds is non-null, rewrite is free to add further predicates to
14784 /// \p NewPreds such that the result will be an AddRecExpr.
14785staticconstSCEV *rewrite(constSCEV *S,constLoop *L,ScalarEvolution &SE,
14786SmallVectorImpl<const SCEVPredicate *> *NewPreds,
14787constSCEVPredicate *Pred) {
14788 SCEVPredicateRewriterRewriter(L, SE, NewPreds, Pred);
14789returnRewriter.visit(S);
14790 }
14791
14792constSCEV *visitUnknown(constSCEVUnknown *Expr) {
14793if (Pred) {
14794if (auto *U = dyn_cast<SCEVUnionPredicate>(Pred)) {
14795for (constauto *Pred : U->getPredicates())
14796if (constauto *IPred = dyn_cast<SCEVComparePredicate>(Pred))
14797if (IPred->getLHS() == Expr &&
14798 IPred->getPredicate() == ICmpInst::ICMP_EQ)
14799return IPred->getRHS();
14800 }elseif (constauto *IPred = dyn_cast<SCEVComparePredicate>(Pred)) {
14801if (IPred->getLHS() == Expr &&
14802 IPred->getPredicate() == ICmpInst::ICMP_EQ)
14803return IPred->getRHS();
14804 }
14805 }
14806return convertToAddRecWithPreds(Expr);
14807 }
14808
14809constSCEV *visitZeroExtendExpr(constSCEVZeroExtendExpr *Expr) {
14810constSCEV *Operand =visit(Expr->getOperand());
14811constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
14812if (AR && AR->getLoop() == L && AR->isAffine()) {
14813// This couldn't be folded because the operand didn't have the nuw
14814// flag. Add the nusw flag as an assumption that we could make.
14815constSCEV *Step = AR->getStepRecurrence(SE);
14816Type *Ty = Expr->getType();
14817if (addOverflowAssumption(AR,SCEVWrapPredicate::IncrementNUSW))
14818return SE.getAddRecExpr(SE.getZeroExtendExpr(AR->getStart(), Ty),
14819 SE.getSignExtendExpr(Step, Ty), L,
14820 AR->getNoWrapFlags());
14821 }
14822return SE.getZeroExtendExpr(Operand, Expr->getType());
14823 }
14824
14825constSCEV *visitSignExtendExpr(constSCEVSignExtendExpr *Expr) {
14826constSCEV *Operand =visit(Expr->getOperand());
14827constSCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Operand);
14828if (AR && AR->getLoop() == L && AR->isAffine()) {
14829// This couldn't be folded because the operand didn't have the nsw
14830// flag. Add the nssw flag as an assumption that we could make.
14831constSCEV *Step = AR->getStepRecurrence(SE);
14832Type *Ty = Expr->getType();
14833if (addOverflowAssumption(AR,SCEVWrapPredicate::IncrementNSSW))
14834return SE.getAddRecExpr(SE.getSignExtendExpr(AR->getStart(), Ty),
14835 SE.getSignExtendExpr(Step, Ty), L,
14836 AR->getNoWrapFlags());
14837 }
14838return SE.getSignExtendExpr(Operand, Expr->getType());
14839 }
14840
14841private:
14842explicit SCEVPredicateRewriter(
14843constLoop *L,ScalarEvolution &SE,
14844SmallVectorImpl<const SCEVPredicate *> *NewPreds,
14845constSCEVPredicate *Pred)
14846 :SCEVRewriteVisitor(SE), NewPreds(NewPreds), Pred(Pred),L(L) {}
14847
14848bool addOverflowAssumption(constSCEVPredicate *P) {
14849if (!NewPreds) {
14850// Check if we've already made this assumption.
14851return Pred && Pred->implies(P, SE);
14852 }
14853 NewPreds->push_back(P);
14854returntrue;
14855 }
14856
14857bool addOverflowAssumption(constSCEVAddRecExpr *AR,
14858SCEVWrapPredicate::IncrementWrapFlags AddedFlags) {
14859auto *A = SE.getWrapPredicate(AR, AddedFlags);
14860return addOverflowAssumption(A);
14861 }
14862
14863// If \p Expr represents a PHINode, we try to see if it can be represented
14864// as an AddRec, possibly under a predicate (PHISCEVPred). If it is possible
14865// to add this predicate as a runtime overflow check, we return the AddRec.
14866// If \p Expr does not meet these conditions (is not a PHI node, or we
14867// couldn't create an AddRec for it, or couldn't add the predicate), we just
14868// return \p Expr.
14869constSCEV *convertToAddRecWithPreds(constSCEVUnknown *Expr) {
14870if (!isa<PHINode>(Expr->getValue()))
14871return Expr;
14872 std::optional<
14873 std::pair<const SCEV *, SmallVector<const SCEVPredicate *, 3>>>
14874 PredicatedRewrite = SE.createAddRecFromPHIWithCasts(Expr);
14875if (!PredicatedRewrite)
14876return Expr;
14877for (constauto *P : PredicatedRewrite->second){
14878// Wrap predicates from outer loops are not supported.
14879if (auto *WP = dyn_cast<const SCEVWrapPredicate>(P)) {
14880if (L != WP->getExpr()->getLoop())
14881return Expr;
14882 }
14883if (!addOverflowAssumption(P))
14884return Expr;
14885 }
14886return PredicatedRewrite->first;
14887 }
14888
14889SmallVectorImpl<const SCEVPredicate *> *NewPreds;
14890constSCEVPredicate *Pred;
14891constLoop *L;
14892};
14893
14894}// end anonymous namespace
14895
14896constSCEV *
14897ScalarEvolution::rewriteUsingPredicate(constSCEV *S,constLoop *L,
14898constSCEVPredicate &Preds) {
14899return SCEVPredicateRewriter::rewrite(S, L, *this,nullptr, &Preds);
14900}
14901
14902constSCEVAddRecExpr *ScalarEvolution::convertSCEVToAddRecWithPredicates(
14903constSCEV *S,constLoop *L,
14904SmallVectorImpl<const SCEVPredicate *> &Preds) {
14905SmallVector<const SCEVPredicate *> TransformPreds;
14906 S = SCEVPredicateRewriter::rewrite(S, L, *this, &TransformPreds,nullptr);
14907auto *AddRec = dyn_cast<SCEVAddRecExpr>(S);
14908
14909if (!AddRec)
14910returnnullptr;
14911
14912// Since the transformation was successful, we can now transfer the SCEV
14913// predicates.
14914 Preds.append(TransformPreds.begin(), TransformPreds.end());
14915
14916return AddRec;
14917}
14918
14919/// SCEV predicates
14920SCEVPredicate::SCEVPredicate(constFoldingSetNodeIDRefID,
14921SCEVPredicateKind Kind)
14922 : FastID(ID), Kind(Kind) {}
14923
14924SCEVComparePredicate::SCEVComparePredicate(constFoldingSetNodeIDRefID,
14925constICmpInst::Predicate Pred,
14926constSCEV *LHS,constSCEV *RHS)
14927 :SCEVPredicate(ID, P_Compare), Pred(Pred),LHS(LHS),RHS(RHS) {
14928assert(LHS->getType() ==RHS->getType() &&"LHS and RHS types don't match");
14929assert(LHS !=RHS &&"LHS and RHS are the same SCEV");
14930}
14931
14932boolSCEVComparePredicate::implies(constSCEVPredicate *N,
14933ScalarEvolution &SE) const{
14934constauto *Op = dyn_cast<SCEVComparePredicate>(N);
14935
14936if (!Op)
14937returnfalse;
14938
14939if (Pred !=ICmpInst::ICMP_EQ)
14940returnfalse;
14941
14942returnOp->LHS ==LHS &&Op->RHS ==RHS;
14943}
14944
14945boolSCEVComparePredicate::isAlwaysTrue() const{returnfalse; }
14946
14947voidSCEVComparePredicate::print(raw_ostream &OS,unsignedDepth) const{
14948if (Pred ==ICmpInst::ICMP_EQ)
14949OS.indent(Depth) <<"Equal predicate: " << *LHS <<" == " << *RHS <<"\n";
14950else
14951OS.indent(Depth) <<"Compare predicate: " << *LHS <<" " << Pred <<") "
14952 << *RHS <<"\n";
14953
14954}
14955
14956SCEVWrapPredicate::SCEVWrapPredicate(constFoldingSetNodeIDRefID,
14957constSCEVAddRecExpr *AR,
14958IncrementWrapFlags Flags)
14959 :SCEVPredicate(ID, P_Wrap), AR(AR), Flags(Flags) {}
14960
14961constSCEVAddRecExpr *SCEVWrapPredicate::getExpr() const{return AR; }
14962
14963boolSCEVWrapPredicate::implies(constSCEVPredicate *N,
14964ScalarEvolution &SE) const{
14965constauto *Op = dyn_cast<SCEVWrapPredicate>(N);
14966if (!Op ||setFlags(Flags,Op->Flags) != Flags)
14967returnfalse;
14968
14969if (Op->AR == AR)
14970returntrue;
14971
14972if (Flags !=SCEVWrapPredicate::IncrementNSSW &&
14973 Flags !=SCEVWrapPredicate::IncrementNUSW)
14974returnfalse;
14975
14976constSCEV *Start = AR->getStart();
14977constSCEV *OpStart =Op->AR->getStart();
14978if (Start->getType()->isPointerTy() != OpStart->getType()->isPointerTy())
14979returnfalse;
14980
14981constSCEV *Step = AR->getStepRecurrence(SE);
14982constSCEV *OpStep =Op->AR->getStepRecurrence(SE);
14983if (!SE.isKnownPositive(Step) || !SE.isKnownPositive(OpStep))
14984returnfalse;
14985
14986// If both steps are positive, this implies N, if N's start and step are
14987// ULE/SLE (for NSUW/NSSW) than this'.
14988Type *WiderTy = SE.getWiderType(Step->getType(), OpStep->getType());
14989 Step = SE.getNoopOrZeroExtend(Step, WiderTy);
14990 OpStep = SE.getNoopOrZeroExtend(OpStep, WiderTy);
14991
14992bool IsNUW = Flags ==SCEVWrapPredicate::IncrementNUSW;
14993 OpStart = IsNUW ? SE.getNoopOrZeroExtend(OpStart, WiderTy)
14994 : SE.getNoopOrSignExtend(OpStart, WiderTy);
14995 Start = IsNUW ? SE.getNoopOrZeroExtend(Start, WiderTy)
14996 : SE.getNoopOrSignExtend(Start, WiderTy);
14997CmpInst::Predicate Pred = IsNUW ?CmpInst::ICMP_ULE :CmpInst::ICMP_SLE;
14998return SE.isKnownPredicate(Pred, OpStep, Step) &&
14999 SE.isKnownPredicate(Pred, OpStart, Start);
15000}
15001
15002boolSCEVWrapPredicate::isAlwaysTrue() const{
15003SCEV::NoWrapFlags ScevFlags = AR->getNoWrapFlags();
15004IncrementWrapFlags IFlags = Flags;
15005
15006if (ScalarEvolution::setFlags(ScevFlags,SCEV::FlagNSW) == ScevFlags)
15007 IFlags =clearFlags(IFlags,IncrementNSSW);
15008
15009return IFlags ==IncrementAnyWrap;
15010}
15011
15012voidSCEVWrapPredicate::print(raw_ostream &OS,unsignedDepth) const{
15013OS.indent(Depth) << *getExpr() <<" Added Flags: ";
15014if (SCEVWrapPredicate::IncrementNUSW &getFlags())
15015OS <<"<nusw>";
15016if (SCEVWrapPredicate::IncrementNSSW &getFlags())
15017OS <<"<nssw>";
15018OS <<"\n";
15019}
15020
15021SCEVWrapPredicate::IncrementWrapFlags
15022SCEVWrapPredicate::getImpliedFlags(constSCEVAddRecExpr *AR,
15023ScalarEvolution &SE) {
15024IncrementWrapFlags ImpliedFlags =IncrementAnyWrap;
15025SCEV::NoWrapFlags StaticFlags = AR->getNoWrapFlags();
15026
15027// We can safely transfer the NSW flag as NSSW.
15028if (ScalarEvolution::setFlags(StaticFlags,SCEV::FlagNSW) == StaticFlags)
15029 ImpliedFlags =IncrementNSSW;
15030
15031if (ScalarEvolution::setFlags(StaticFlags,SCEV::FlagNUW) == StaticFlags) {
15032// If the increment is positive, the SCEV NUW flag will also imply the
15033// WrapPredicate NUSW flag.
15034if (constauto *Step = dyn_cast<SCEVConstant>(AR->getStepRecurrence(SE)))
15035if (Step->getValue()->getValue().isNonNegative())
15036 ImpliedFlags =setFlags(ImpliedFlags,IncrementNUSW);
15037 }
15038
15039return ImpliedFlags;
15040}
15041
15042/// Union predicates don't get cached so create a dummy set ID for it.
15043SCEVUnionPredicate::SCEVUnionPredicate(ArrayRef<const SCEVPredicate *> Preds,
15044ScalarEvolution &SE)
15045 :SCEVPredicate(FoldingSetNodeIDRef(nullptr, 0), P_Union) {
15046for (constauto *P : Preds)
15047 add(P, SE);
15048}
15049
15050boolSCEVUnionPredicate::isAlwaysTrue() const{
15051returnall_of(Preds,
15052 [](constSCEVPredicate *I) {returnI->isAlwaysTrue(); });
15053}
15054
15055boolSCEVUnionPredicate::implies(constSCEVPredicate *N,
15056ScalarEvolution &SE) const{
15057if (constauto *Set = dyn_cast<SCEVUnionPredicate>(N))
15058returnall_of(Set->Preds, [this, &SE](constSCEVPredicate *I) {
15059 return this->implies(I, SE);
15060 });
15061
15062returnany_of(Preds,
15063 [N, &SE](constSCEVPredicate *I) {returnI->implies(N, SE); });
15064}
15065
15066voidSCEVUnionPredicate::print(raw_ostream &OS,unsignedDepth) const{
15067for (constauto *Pred : Preds)
15068 Pred->print(OS,Depth);
15069}
15070
15071void SCEVUnionPredicate::add(constSCEVPredicate *N,ScalarEvolution &SE) {
15072if (constauto *Set = dyn_cast<SCEVUnionPredicate>(N)) {
15073for (constauto *Pred : Set->Preds)
15074 add(Pred, SE);
15075return;
15076 }
15077
15078// Only add predicate if it is not already implied by this union predicate.
15079if (implies(N, SE))
15080return;
15081
15082// Build a new vector containing the current predicates, except the ones that
15083// are implied by the new predicate N.
15084SmallVector<const SCEVPredicate *> PrunedPreds;
15085for (auto *P : Preds) {
15086if (N->implies(P, SE))
15087continue;
15088 PrunedPreds.push_back(P);
15089 }
15090 Preds = std::move(PrunedPreds);
15091 Preds.push_back(N);
15092}
15093
15094PredicatedScalarEvolution::PredicatedScalarEvolution(ScalarEvolution &SE,
15095Loop &L)
15096 : SE(SE), L(L) {
15097SmallVector<const SCEVPredicate*, 4> Empty;
15098 Preds = std::make_unique<SCEVUnionPredicate>(Empty, SE);
15099}
15100
15101voidScalarEvolution::registerUser(constSCEV *User,
15102ArrayRef<const SCEV *> Ops) {
15103for (constauto *Op : Ops)
15104// We do not expect that forgetting cached data for SCEVConstants will ever
15105// open any prospects for sharpening or introduce any correctness issues,
15106// so we don't bother storing their dependencies.
15107if (!isa<SCEVConstant>(Op))
15108 SCEVUsers[Op].insert(User);
15109}
15110
15111constSCEV *PredicatedScalarEvolution::getSCEV(Value *V) {
15112constSCEV *Expr = SE.getSCEV(V);
15113 RewriteEntry &Entry = RewriteMap[Expr];
15114
15115// If we already have an entry and the version matches, return it.
15116if (Entry.second && Generation == Entry.first)
15117return Entry.second;
15118
15119// We found an entry but it's stale. Rewrite the stale entry
15120// according to the current predicate.
15121if (Entry.second)
15122 Expr = Entry.second;
15123
15124constSCEV *NewSCEV = SE.rewriteUsingPredicate(Expr, &L, *Preds);
15125 Entry = {Generation, NewSCEV};
15126
15127return NewSCEV;
15128}
15129
15130constSCEV *PredicatedScalarEvolution::getBackedgeTakenCount() {
15131if (!BackedgeCount) {
15132SmallVector<const SCEVPredicate *, 4> Preds;
15133 BackedgeCount = SE.getPredicatedBackedgeTakenCount(&L, Preds);
15134for (constauto *P : Preds)
15135addPredicate(*P);
15136 }
15137return BackedgeCount;
15138}
15139
15140constSCEV *PredicatedScalarEvolution::getSymbolicMaxBackedgeTakenCount() {
15141if (!SymbolicMaxBackedgeCount) {
15142SmallVector<const SCEVPredicate *, 4> Preds;
15143 SymbolicMaxBackedgeCount =
15144 SE.getPredicatedSymbolicMaxBackedgeTakenCount(&L, Preds);
15145for (constauto *P : Preds)
15146addPredicate(*P);
15147 }
15148return SymbolicMaxBackedgeCount;
15149}
15150
15151unsignedPredicatedScalarEvolution::getSmallConstantMaxTripCount() {
15152if (!SmallConstantMaxTripCount) {
15153SmallVector<const SCEVPredicate *, 4> Preds;
15154 SmallConstantMaxTripCount = SE.getSmallConstantMaxTripCount(&L, &Preds);
15155for (constauto *P : Preds)
15156addPredicate(*P);
15157 }
15158return *SmallConstantMaxTripCount;
15159}
15160
15161voidPredicatedScalarEvolution::addPredicate(constSCEVPredicate &Pred) {
15162if (Preds->implies(&Pred, SE))
15163return;
15164
15165SmallVector<const SCEVPredicate *, 4> NewPreds(Preds->getPredicates());
15166 NewPreds.push_back(&Pred);
15167 Preds = std::make_unique<SCEVUnionPredicate>(NewPreds, SE);
15168 updateGeneration();
15169}
15170
15171constSCEVPredicate &PredicatedScalarEvolution::getPredicate() const{
15172return *Preds;
15173}
15174
15175void PredicatedScalarEvolution::updateGeneration() {
15176// If the generation number wrapped recompute everything.
15177if (++Generation == 0) {
15178for (auto &II : RewriteMap) {
15179constSCEV *Rewritten =II.second.second;
15180II.second = {Generation, SE.rewriteUsingPredicate(Rewritten, &L, *Preds)};
15181 }
15182 }
15183}
15184
15185voidPredicatedScalarEvolution::setNoOverflow(
15186Value *V,SCEVWrapPredicate::IncrementWrapFlags Flags) {
15187constSCEV *Expr =getSCEV(V);
15188constauto *AR = cast<SCEVAddRecExpr>(Expr);
15189
15190auto ImpliedFlags =SCEVWrapPredicate::getImpliedFlags(AR, SE);
15191
15192// Clear the statically implied flags.
15193 Flags =SCEVWrapPredicate::clearFlags(Flags, ImpliedFlags);
15194addPredicate(*SE.getWrapPredicate(AR, Flags));
15195
15196autoII = FlagsMap.insert({V, Flags});
15197if (!II.second)
15198II.first->second =SCEVWrapPredicate::setFlags(Flags,II.first->second);
15199}
15200
15201boolPredicatedScalarEvolution::hasNoOverflow(
15202Value *V,SCEVWrapPredicate::IncrementWrapFlags Flags) {
15203constSCEV *Expr =getSCEV(V);
15204constauto *AR = cast<SCEVAddRecExpr>(Expr);
15205
15206 Flags =SCEVWrapPredicate::clearFlags(
15207 Flags,SCEVWrapPredicate::getImpliedFlags(AR, SE));
15208
15209autoII = FlagsMap.find(V);
15210
15211if (II != FlagsMap.end())
15212 Flags =SCEVWrapPredicate::clearFlags(Flags,II->second);
15213
15214return Flags ==SCEVWrapPredicate::IncrementAnyWrap;
15215}
15216
15217constSCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) {
15218constSCEV *Expr = this->getSCEV(V);
15219SmallVector<const SCEVPredicate *, 4> NewPreds;
15220auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, NewPreds);
15221
15222if (!New)
15223returnnullptr;
15224
15225for (constauto *P : NewPreds)
15226addPredicate(*P);
15227
15228 RewriteMap[SE.getSCEV(V)] = {Generation, New};
15229return New;
15230}
15231
15232PredicatedScalarEvolution::PredicatedScalarEvolution(
15233constPredicatedScalarEvolution &Init)
15234 : RewriteMap(Init.RewriteMap), SE(Init.SE), L(Init.L),
15235 Preds(std::make_unique<SCEVUnionPredicate>(Init.Preds->getPredicates(),
15236 SE)),
15237 Generation(Init.Generation), BackedgeCount(Init.BackedgeCount) {
15238for (autoI :Init.FlagsMap)
15239 FlagsMap.insert(I);
15240}
15241
15242voidPredicatedScalarEvolution::print(raw_ostream &OS,unsignedDepth) const{
15243// For each block.
15244for (auto *BB : L.getBlocks())
15245for (auto &I : *BB) {
15246if (!SE.isSCEVable(I.getType()))
15247continue;
15248
15249auto *Expr = SE.getSCEV(&I);
15250autoII = RewriteMap.find(Expr);
15251
15252if (II == RewriteMap.end())
15253continue;
15254
15255// Don't print things that are not interesting.
15256if (II->second.second == Expr)
15257continue;
15258
15259OS.indent(Depth) <<"[PSE]" <<I <<":\n";
15260OS.indent(Depth + 2) << *Expr <<"\n";
15261OS.indent(Depth + 2) <<"--> " << *II->second.second <<"\n";
15262 }
15263}
15264
15265// Match the mathematical pattern A - (A / B) * B, where A and B can be
15266// arbitrary expressions. Also match zext (trunc A to iB) to iY, which is used
15267// for URem with constant power-of-2 second operands.
15268// It's not always easy, as A and B can be folded (imagine A is X / 2, and B is
15269// 4, A / B becomes X / 8).
15270bool ScalarEvolution::matchURem(constSCEV *Expr,constSCEV *&LHS,
15271constSCEV *&RHS) {
15272if (Expr->getType()->isPointerTy())
15273returnfalse;
15274
15275// Try to match 'zext (trunc A to iB) to iY', which is used
15276// for URem with constant power-of-2 second operands. Make sure the size of
15277// the operand A matches the size of the whole expressions.
15278if (constauto *ZExt = dyn_cast<SCEVZeroExtendExpr>(Expr))
15279if (constauto *Trunc = dyn_cast<SCEVTruncateExpr>(ZExt->getOperand(0))) {
15280LHS = Trunc->getOperand();
15281// Bail out if the type of the LHS is larger than the type of the
15282// expression for now.
15283if (getTypeSizeInBits(LHS->getType()) >
15284getTypeSizeInBits(Expr->getType()))
15285returnfalse;
15286if (LHS->getType() != Expr->getType())
15287LHS =getZeroExtendExpr(LHS, Expr->getType());
15288RHS =getConstant(APInt(getTypeSizeInBits(Expr->getType()), 1)
15289 <<getTypeSizeInBits(Trunc->getType()));
15290returntrue;
15291 }
15292constauto *Add = dyn_cast<SCEVAddExpr>(Expr);
15293if (Add ==nullptr ||Add->getNumOperands() != 2)
15294returnfalse;
15295
15296constSCEV *A =Add->getOperand(1);
15297constauto *Mul = dyn_cast<SCEVMulExpr>(Add->getOperand(0));
15298
15299if (Mul ==nullptr)
15300returnfalse;
15301
15302constauto MatchURemWithDivisor = [&](constSCEV *B) {
15303// (SomeExpr + (-(SomeExpr / B) * B)).
15304if (Expr ==getURemExpr(A,B)) {
15305LHS =A;
15306RHS =B;
15307returntrue;
15308 }
15309returnfalse;
15310 };
15311
15312// (SomeExpr + (-1 * (SomeExpr / B) * B)).
15313if (Mul->getNumOperands() == 3 && isa<SCEVConstant>(Mul->getOperand(0)))
15314return MatchURemWithDivisor(Mul->getOperand(1)) ||
15315 MatchURemWithDivisor(Mul->getOperand(2));
15316
15317// (SomeExpr + ((-SomeExpr / B) * B)) or (SomeExpr + ((SomeExpr / B) * -B)).
15318if (Mul->getNumOperands() == 2)
15319return MatchURemWithDivisor(Mul->getOperand(1)) ||
15320 MatchURemWithDivisor(Mul->getOperand(0)) ||
15321 MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(1))) ||
15322 MatchURemWithDivisor(getNegativeSCEV(Mul->getOperand(0)));
15323returnfalse;
15324}
15325
15326ScalarEvolution::LoopGuards
15327ScalarEvolution::LoopGuards::collect(constLoop *L,ScalarEvolution &SE) {
15328BasicBlock *Header = L->getHeader();
15329BasicBlock *Pred = L->getLoopPredecessor();
15330LoopGuards Guards(SE);
15331if (!Pred)
15332return Guards;
15333SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
15334 collectFromBlock(SE, Guards, Header, Pred, VisitedBlocks);
15335return Guards;
15336}
15337
15338void ScalarEvolution::LoopGuards::collectFromPHI(
15339ScalarEvolution &SE,ScalarEvolution::LoopGuards &Guards,
15340constPHINode &Phi,SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks,
15341SmallDenseMap<const BasicBlock *, LoopGuards> &IncomingGuards,
15342unsignedDepth) {
15343if (!SE.isSCEVable(Phi.getType()))
15344return;
15345
15346usingMinMaxPattern = std::pair<const SCEVConstant *, SCEVTypes>;
15347auto GetMinMaxConst = [&](unsigned IncomingIdx) -> MinMaxPattern {
15348constBasicBlock *InBlock = Phi.getIncomingBlock(IncomingIdx);
15349if (!VisitedBlocks.insert(InBlock).second)
15350return {nullptr,scCouldNotCompute};
15351auto [G, Inserted] = IncomingGuards.try_emplace(InBlock, LoopGuards(SE));
15352if (Inserted)
15353 collectFromBlock(SE,G->second, Phi.getParent(),InBlock, VisitedBlocks,
15354Depth + 1);
15355auto &RewriteMap =G->second.RewriteMap;
15356if (RewriteMap.empty())
15357return {nullptr,scCouldNotCompute};
15358auto S = RewriteMap.find(SE.getSCEV(Phi.getIncomingValue(IncomingIdx)));
15359if (S == RewriteMap.end())
15360return {nullptr,scCouldNotCompute};
15361auto *SM = dyn_cast_if_present<SCEVMinMaxExpr>(S->second);
15362if (!SM)
15363return {nullptr,scCouldNotCompute};
15364if (constSCEVConstant *C0 = dyn_cast<SCEVConstant>(SM->getOperand(0)))
15365return {C0, SM->getSCEVType()};
15366return {nullptr,scCouldNotCompute};
15367 };
15368auto MergeMinMaxConst = [](MinMaxPatternP1,
15369 MinMaxPattern P2) -> MinMaxPattern {
15370auto [C1,T1] =P1;
15371auto [C2, T2] = P2;
15372if (!C1 || !C2 || T1 != T2)
15373return {nullptr,scCouldNotCompute};
15374switch (T1) {
15375casescUMaxExpr:
15376return {C1->getAPInt().ult(C2->getAPInt()) ? C1 : C2, T1};
15377casescSMaxExpr:
15378return {C1->getAPInt().slt(C2->getAPInt()) ? C1 : C2, T1};
15379casescUMinExpr:
15380return {C1->getAPInt().ugt(C2->getAPInt()) ? C1 : C2, T1};
15381casescSMinExpr:
15382return {C1->getAPInt().sgt(C2->getAPInt()) ? C1 : C2, T1};
15383default:
15384llvm_unreachable("Trying to merge non-MinMaxExpr SCEVs.");
15385 }
15386 };
15387autoP = GetMinMaxConst(0);
15388for (unsignedint In = 1;In <Phi.getNumIncomingValues();In++) {
15389if (!P.first)
15390break;
15391P = MergeMinMaxConst(P, GetMinMaxConst(In));
15392 }
15393if (P.first) {
15394constSCEV *LHS = SE.getSCEV(const_cast<PHINode *>(&Phi));
15395SmallVector<const SCEV *, 2> Ops({P.first,LHS});
15396constSCEV *RHS = SE.getMinMaxExpr(P.second, Ops);
15397 Guards.RewriteMap.insert({LHS,RHS});
15398 }
15399}
15400
15401void ScalarEvolution::LoopGuards::collectFromBlock(
15402ScalarEvolution &SE,ScalarEvolution::LoopGuards &Guards,
15403constBasicBlock *Block,constBasicBlock *Pred,
15404SmallPtrSetImpl<const BasicBlock *> &VisitedBlocks,unsignedDepth) {
15405SmallVector<const SCEV *> ExprsToRewrite;
15406auto CollectCondition = [&](ICmpInst::PredicatePredicate,constSCEV *LHS,
15407constSCEV *RHS,
15408DenseMap<const SCEV *, const SCEV *>
15409 &RewriteMap) {
15410// WARNING: It is generally unsound to apply any wrap flags to the proposed
15411// replacement SCEV which isn't directly implied by the structure of that
15412// SCEV. In particular, using contextual facts to imply flags is *NOT*
15413// legal. See the scoping rules for flags in the header to understand why.
15414
15415// If LHS is a constant, apply information to the other expression.
15416if (isa<SCEVConstant>(LHS)) {
15417std::swap(LHS, RHS);
15418Predicate =CmpInst::getSwappedPredicate(Predicate);
15419 }
15420
15421// Check for a condition of the form (-C1 + X < C2). InstCombine will
15422// create this form when combining two checks of the form (X u< C2 + C1) and
15423// (X >=u C1).
15424auto MatchRangeCheckIdiom = [&SE,Predicate,LHS,RHS, &RewriteMap,
15425 &ExprsToRewrite]() {
15426constSCEVConstant *C1;
15427constSCEVUnknown *LHSUnknown;
15428auto *C2 = dyn_cast<SCEVConstant>(RHS);
15429if (!match(LHS,
15430m_scev_Add(m_SCEVConstant(C1),m_SCEVUnknown(LHSUnknown))) ||
15431 !C2)
15432returnfalse;
15433
15434auto ExactRegion =
15435ConstantRange::makeExactICmpRegion(Predicate, C2->getAPInt())
15436 .sub(C1->getAPInt());
15437
15438// Bail out, unless we have a non-wrapping, monotonic range.
15439if (ExactRegion.isWrappedSet() || ExactRegion.isFullSet())
15440returnfalse;
15441autoI = RewriteMap.find(LHSUnknown);
15442constSCEV *RewrittenLHS =I != RewriteMap.end() ?I->second : LHSUnknown;
15443 RewriteMap[LHSUnknown] = SE.getUMaxExpr(
15444 SE.getConstant(ExactRegion.getUnsignedMin()),
15445 SE.getUMinExpr(RewrittenLHS,
15446 SE.getConstant(ExactRegion.getUnsignedMax())));
15447 ExprsToRewrite.push_back(LHSUnknown);
15448returntrue;
15449 };
15450if (MatchRangeCheckIdiom())
15451return;
15452
15453// Return true if \p Expr is a MinMax SCEV expression with a non-negative
15454// constant operand. If so, return in \p SCTy the SCEV type and in \p RHS
15455// the non-constant operand and in \p LHS the constant operand.
15456auto IsMinMaxSCEVWithNonNegativeConstant =
15457 [&](constSCEV *Expr,SCEVTypes &SCTy,constSCEV *&LHS,
15458constSCEV *&RHS) {
15459if (auto *MinMax = dyn_cast<SCEVMinMaxExpr>(Expr)) {
15460if (MinMax->getNumOperands() != 2)
15461returnfalse;
15462if (auto *C = dyn_cast<SCEVConstant>(MinMax->getOperand(0))) {
15463if (C->getAPInt().isNegative())
15464returnfalse;
15465 SCTy =MinMax->getSCEVType();
15466LHS =MinMax->getOperand(0);
15467RHS =MinMax->getOperand(1);
15468returntrue;
15469 }
15470 }
15471returnfalse;
15472 };
15473
15474// Checks whether Expr is a non-negative constant, and Divisor is a positive
15475// constant, and returns their APInt in ExprVal and in DivisorVal.
15476auto GetNonNegExprAndPosDivisor = [&](constSCEV *Expr,constSCEV *Divisor,
15477APInt &ExprVal,APInt &DivisorVal) {
15478auto *ConstExpr = dyn_cast<SCEVConstant>(Expr);
15479auto *ConstDivisor = dyn_cast<SCEVConstant>(Divisor);
15480if (!ConstExpr || !ConstDivisor)
15481returnfalse;
15482 ExprVal = ConstExpr->getAPInt();
15483 DivisorVal = ConstDivisor->getAPInt();
15484return ExprVal.isNonNegative() && !DivisorVal.isNonPositive();
15485 };
15486
15487// Return a new SCEV that modifies \p Expr to the closest number divides by
15488// \p Divisor and greater or equal than Expr.
15489// For now, only handle constant Expr and Divisor.
15490auto GetNextSCEVDividesByDivisor = [&](constSCEV *Expr,
15491constSCEV *Divisor) {
15492APInt ExprVal;
15493APInt DivisorVal;
15494if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal))
15495return Expr;
15496APInt Rem = ExprVal.urem(DivisorVal);
15497if (!Rem.isZero())
15498// return the SCEV: Expr + Divisor - Expr % Divisor
15499return SE.getConstant(ExprVal + DivisorVal - Rem);
15500return Expr;
15501 };
15502
15503// Return a new SCEV that modifies \p Expr to the closest number divides by
15504// \p Divisor and less or equal than Expr.
15505// For now, only handle constant Expr and Divisor.
15506auto GetPreviousSCEVDividesByDivisor = [&](constSCEV *Expr,
15507constSCEV *Divisor) {
15508APInt ExprVal;
15509APInt DivisorVal;
15510if (!GetNonNegExprAndPosDivisor(Expr, Divisor, ExprVal, DivisorVal))
15511return Expr;
15512APInt Rem = ExprVal.urem(DivisorVal);
15513// return the SCEV: Expr - Expr % Divisor
15514return SE.getConstant(ExprVal - Rem);
15515 };
15516
15517// Apply divisibilty by \p Divisor on MinMaxExpr with constant values,
15518// recursively. This is done by aligning up/down the constant value to the
15519// Divisor.
15520 std::function<constSCEV *(constSCEV *,constSCEV *)>
15521 ApplyDivisibiltyOnMinMaxExpr = [&](constSCEV *MinMaxExpr,
15522constSCEV *Divisor) {
15523constSCEV *MinMaxLHS =nullptr, *MinMaxRHS =nullptr;
15524SCEVTypes SCTy;
15525if (!IsMinMaxSCEVWithNonNegativeConstant(MinMaxExpr, SCTy, MinMaxLHS,
15526 MinMaxRHS))
15527return MinMaxExpr;
15528auto IsMin =
15529 isa<SCEVSMinExpr>(MinMaxExpr) || isa<SCEVUMinExpr>(MinMaxExpr);
15530assert(SE.isKnownNonNegative(MinMaxLHS) &&
15531"Expected non-negative operand!");
15532auto *DivisibleExpr =
15533 IsMin ? GetPreviousSCEVDividesByDivisor(MinMaxLHS, Divisor)
15534 : GetNextSCEVDividesByDivisor(MinMaxLHS, Divisor);
15535SmallVector<const SCEV *> Ops = {
15536 ApplyDivisibiltyOnMinMaxExpr(MinMaxRHS, Divisor), DivisibleExpr};
15537return SE.getMinMaxExpr(SCTy, Ops);
15538 };
15539
15540// If we have LHS == 0, check if LHS is computing a property of some unknown
15541// SCEV %v which we can rewrite %v to express explicitly.
15542if (Predicate ==CmpInst::ICMP_EQ &&match(RHS,m_scev_Zero())) {
15543// If LHS is A % B, i.e. A % B == 0, rewrite A to (A /u B) * B to
15544// explicitly express that.
15545constSCEV *URemLHS =nullptr;
15546constSCEV *URemRHS =nullptr;
15547if (SE.matchURem(LHS, URemLHS, URemRHS)) {
15548if (constSCEVUnknown *LHSUnknown = dyn_cast<SCEVUnknown>(URemLHS)) {
15549autoI = RewriteMap.find(LHSUnknown);
15550constSCEV *RewrittenLHS =
15551I != RewriteMap.end() ?I->second : LHSUnknown;
15552 RewrittenLHS = ApplyDivisibiltyOnMinMaxExpr(RewrittenLHS, URemRHS);
15553constauto *Multiple =
15554 SE.getMulExpr(SE.getUDivExpr(RewrittenLHS, URemRHS), URemRHS);
15555 RewriteMap[LHSUnknown] = Multiple;
15556 ExprsToRewrite.push_back(LHSUnknown);
15557return;
15558 }
15559 }
15560 }
15561
15562// Do not apply information for constants or if RHS contains an AddRec.
15563if (isa<SCEVConstant>(LHS) || SE.containsAddRecurrence(RHS))
15564return;
15565
15566// If RHS is SCEVUnknown, make sure the information is applied to it.
15567if (!isa<SCEVUnknown>(LHS) && isa<SCEVUnknown>(RHS)) {
15568std::swap(LHS, RHS);
15569Predicate =CmpInst::getSwappedPredicate(Predicate);
15570 }
15571
15572// Puts rewrite rule \p From -> \p To into the rewrite map. Also if \p From
15573// and \p FromRewritten are the same (i.e. there has been no rewrite
15574// registered for \p From), then puts this value in the list of rewritten
15575// expressions.
15576auto AddRewrite = [&](constSCEV *From,constSCEV *FromRewritten,
15577constSCEV *To) {
15578if (From == FromRewritten)
15579 ExprsToRewrite.push_back(From);
15580 RewriteMap[From] = To;
15581 };
15582
15583// Checks whether \p S has already been rewritten. In that case returns the
15584// existing rewrite because we want to chain further rewrites onto the
15585// already rewritten value. Otherwise returns \p S.
15586auto GetMaybeRewritten = [&](constSCEV *S) {
15587autoI = RewriteMap.find(S);
15588returnI != RewriteMap.end() ?I->second : S;
15589 };
15590
15591// Check for the SCEV expression (A /u B) * B while B is a constant, inside
15592// \p Expr. The check is done recuresively on \p Expr, which is assumed to
15593// be a composition of Min/Max SCEVs. Return whether the SCEV expression (A
15594// /u B) * B was found, and return the divisor B in \p DividesBy. For
15595// example, if Expr = umin (umax ((A /u 8) * 8, 16), 64), return true since
15596// (A /u 8) * 8 matched the pattern, and return the constant SCEV 8 in \p
15597// DividesBy.
15598 std::function<bool(constSCEV *,constSCEV *&)> HasDivisibiltyInfo =
15599 [&](constSCEV *Expr,constSCEV *&DividesBy) {
15600if (auto *Mul = dyn_cast<SCEVMulExpr>(Expr)) {
15601if (Mul->getNumOperands() != 2)
15602returnfalse;
15603auto *MulLHS =Mul->getOperand(0);
15604auto *MulRHS =Mul->getOperand(1);
15605if (isa<SCEVConstant>(MulLHS))
15606std::swap(MulLHS, MulRHS);
15607if (auto *Div = dyn_cast<SCEVUDivExpr>(MulLHS))
15608if (Div->getOperand(1) == MulRHS) {
15609 DividesBy = MulRHS;
15610returntrue;
15611 }
15612 }
15613if (auto *MinMax = dyn_cast<SCEVMinMaxExpr>(Expr))
15614return HasDivisibiltyInfo(MinMax->getOperand(0), DividesBy) ||
15615 HasDivisibiltyInfo(MinMax->getOperand(1), DividesBy);
15616returnfalse;
15617 };
15618
15619// Return true if Expr known to divide by \p DividesBy.
15620 std::function<bool(constSCEV *,constSCEV *&)> IsKnownToDivideBy =
15621 [&](constSCEV *Expr,constSCEV *DividesBy) {
15622if (SE.getURemExpr(Expr, DividesBy)->isZero())
15623returntrue;
15624if (auto *MinMax = dyn_cast<SCEVMinMaxExpr>(Expr))
15625return IsKnownToDivideBy(MinMax->getOperand(0), DividesBy) &&
15626 IsKnownToDivideBy(MinMax->getOperand(1), DividesBy);
15627returnfalse;
15628 };
15629
15630constSCEV *RewrittenLHS = GetMaybeRewritten(LHS);
15631constSCEV *DividesBy =nullptr;
15632if (HasDivisibiltyInfo(RewrittenLHS, DividesBy))
15633// Check that the whole expression is divided by DividesBy
15634 DividesBy =
15635 IsKnownToDivideBy(RewrittenLHS, DividesBy) ? DividesBy :nullptr;
15636
15637// Collect rewrites for LHS and its transitive operands based on the
15638// condition.
15639// For min/max expressions, also apply the guard to its operands:
15640// 'min(a, b) >= c' -> '(a >= c) and (b >= c)',
15641// 'min(a, b) > c' -> '(a > c) and (b > c)',
15642// 'max(a, b) <= c' -> '(a <= c) and (b <= c)',
15643// 'max(a, b) < c' -> '(a < c) and (b < c)'.
15644
15645// We cannot express strict predicates in SCEV, so instead we replace them
15646// with non-strict ones against plus or minus one of RHS depending on the
15647// predicate.
15648constSCEV *One = SE.getOne(RHS->getType());
15649switch (Predicate) {
15650caseCmpInst::ICMP_ULT:
15651if (RHS->getType()->isPointerTy())
15652return;
15653RHS = SE.getUMaxExpr(RHS, One);
15654 [[fallthrough]];
15655caseCmpInst::ICMP_SLT: {
15656RHS = SE.getMinusSCEV(RHS, One);
15657RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) :RHS;
15658break;
15659 }
15660caseCmpInst::ICMP_UGT:
15661caseCmpInst::ICMP_SGT:
15662RHS = SE.getAddExpr(RHS, One);
15663RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) :RHS;
15664break;
15665caseCmpInst::ICMP_ULE:
15666caseCmpInst::ICMP_SLE:
15667RHS = DividesBy ? GetPreviousSCEVDividesByDivisor(RHS, DividesBy) :RHS;
15668break;
15669caseCmpInst::ICMP_UGE:
15670caseCmpInst::ICMP_SGE:
15671RHS = DividesBy ? GetNextSCEVDividesByDivisor(RHS, DividesBy) :RHS;
15672break;
15673default:
15674break;
15675 }
15676
15677SmallVector<const SCEV *, 16> Worklist(1, LHS);
15678SmallPtrSet<const SCEV *, 16> Visited;
15679
15680auto EnqueueOperands = [&Worklist](constSCEVNAryExpr *S) {
15681append_range(Worklist, S->operands());
15682 };
15683
15684while (!Worklist.empty()) {
15685constSCEV *From = Worklist.pop_back_val();
15686if (isa<SCEVConstant>(From))
15687continue;
15688if (!Visited.insert(From).second)
15689continue;
15690constSCEV *FromRewritten = GetMaybeRewritten(From);
15691constSCEV *To =nullptr;
15692
15693switch (Predicate) {
15694caseCmpInst::ICMP_ULT:
15695caseCmpInst::ICMP_ULE:
15696 To = SE.getUMinExpr(FromRewritten, RHS);
15697if (auto *UMax = dyn_cast<SCEVUMaxExpr>(FromRewritten))
15698 EnqueueOperands(UMax);
15699break;
15700caseCmpInst::ICMP_SLT:
15701caseCmpInst::ICMP_SLE:
15702 To = SE.getSMinExpr(FromRewritten, RHS);
15703if (auto *SMax = dyn_cast<SCEVSMaxExpr>(FromRewritten))
15704 EnqueueOperands(SMax);
15705break;
15706caseCmpInst::ICMP_UGT:
15707caseCmpInst::ICMP_UGE:
15708 To = SE.getUMaxExpr(FromRewritten, RHS);
15709if (auto *UMin = dyn_cast<SCEVUMinExpr>(FromRewritten))
15710 EnqueueOperands(UMin);
15711break;
15712caseCmpInst::ICMP_SGT:
15713caseCmpInst::ICMP_SGE:
15714 To = SE.getSMaxExpr(FromRewritten, RHS);
15715if (auto *SMin = dyn_cast<SCEVSMinExpr>(FromRewritten))
15716 EnqueueOperands(SMin);
15717break;
15718caseCmpInst::ICMP_EQ:
15719if (isa<SCEVConstant>(RHS))
15720 To =RHS;
15721break;
15722caseCmpInst::ICMP_NE:
15723if (match(RHS,m_scev_Zero())) {
15724constSCEV *OneAlignedUp =
15725 DividesBy ? GetNextSCEVDividesByDivisor(One, DividesBy) : One;
15726 To = SE.getUMaxExpr(FromRewritten, OneAlignedUp);
15727 }
15728break;
15729default:
15730break;
15731 }
15732
15733if (To)
15734 AddRewrite(From, FromRewritten, To);
15735 }
15736 };
15737
15738SmallVector<PointerIntPair<Value *, 1, bool>> Terms;
15739// First, collect information from assumptions dominating the loop.
15740for (auto &AssumeVH : SE.AC.assumptions()) {
15741if (!AssumeVH)
15742continue;
15743auto *AssumeI = cast<CallInst>(AssumeVH);
15744if (!SE.DT.dominates(AssumeI,Block))
15745continue;
15746 Terms.emplace_back(AssumeI->getOperand(0),true);
15747 }
15748
15749// Second, collect information from llvm.experimental.guards dominating the loop.
15750auto *GuardDecl =Intrinsic::getDeclarationIfExists(
15751 SE.F.getParent(), Intrinsic::experimental_guard);
15752if (GuardDecl)
15753for (constauto *GU : GuardDecl->users())
15754if (constauto *Guard = dyn_cast<IntrinsicInst>(GU))
15755if (Guard->getFunction() ==Block->getParent() &&
15756 SE.DT.dominates(Guard,Block))
15757 Terms.emplace_back(Guard->getArgOperand(0),true);
15758
15759// Third, collect conditions from dominating branches. Starting at the loop
15760// predecessor, climb up the predecessor chain, as long as there are
15761// predecessors that can be found that have unique successors leading to the
15762// original header.
15763// TODO: share this logic with isLoopEntryGuardedByCond.
15764unsigned NumCollectedConditions = 0;
15765 VisitedBlocks.insert(Block);
15766 std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred,Block);
15767for (; Pair.first;
15768 Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
15769 VisitedBlocks.insert(Pair.second);
15770constBranchInst *LoopEntryPredicate =
15771 dyn_cast<BranchInst>(Pair.first->getTerminator());
15772if (!LoopEntryPredicate || LoopEntryPredicate->isUnconditional())
15773continue;
15774
15775 Terms.emplace_back(LoopEntryPredicate->getCondition(),
15776 LoopEntryPredicate->getSuccessor(0) == Pair.second);
15777 NumCollectedConditions++;
15778
15779// If we are recursively collecting guards stop after 2
15780// conditions to limit compile-time impact for now.
15781if (Depth > 0 && NumCollectedConditions == 2)
15782break;
15783 }
15784// Finally, if we stopped climbing the predecessor chain because
15785// there wasn't a unique one to continue, try to collect conditions
15786// for PHINodes by recursively following all of their incoming
15787// blocks and try to merge the found conditions to build a new one
15788// for the Phi.
15789if (Pair.second->hasNPredecessorsOrMore(2) &&
15790Depth <MaxLoopGuardCollectionDepth) {
15791SmallDenseMap<const BasicBlock *, LoopGuards> IncomingGuards;
15792for (auto &Phi : Pair.second->phis())
15793 collectFromPHI(SE, Guards, Phi, VisitedBlocks, IncomingGuards,Depth);
15794 }
15795
15796// Now apply the information from the collected conditions to
15797// Guards.RewriteMap. Conditions are processed in reverse order, so the
15798// earliest conditions is processed first. This ensures the SCEVs with the
15799// shortest dependency chains are constructed first.
15800for (auto [Term, EnterIfTrue] :reverse(Terms)) {
15801SmallVector<Value *, 8> Worklist;
15802SmallPtrSet<Value *, 8> Visited;
15803 Worklist.push_back(Term);
15804while (!Worklist.empty()) {
15805Value *Cond = Worklist.pop_back_val();
15806if (!Visited.insert(Cond).second)
15807continue;
15808
15809if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
15810autoPredicate =
15811 EnterIfTrue ?Cmp->getPredicate() :Cmp->getInversePredicate();
15812constauto *LHS = SE.getSCEV(Cmp->getOperand(0));
15813constauto *RHS = SE.getSCEV(Cmp->getOperand(1));
15814 CollectCondition(Predicate, LHS, RHS, Guards.RewriteMap);
15815continue;
15816 }
15817
15818Value *L, *R;
15819if (EnterIfTrue ?match(Cond,m_LogicalAnd(m_Value(L),m_Value(R)))
15820 :match(Cond,m_LogicalOr(m_Value(L),m_Value(R)))) {
15821 Worklist.push_back(L);
15822 Worklist.push_back(R);
15823 }
15824 }
15825 }
15826
15827// Let the rewriter preserve NUW/NSW flags if the unsigned/signed ranges of
15828// the replacement expressions are contained in the ranges of the replaced
15829// expressions.
15830 Guards.PreserveNUW =true;
15831 Guards.PreserveNSW =true;
15832for (constSCEV *Expr : ExprsToRewrite) {
15833constSCEV *RewriteTo = Guards.RewriteMap[Expr];
15834 Guards.PreserveNUW &=
15835 SE.getUnsignedRange(Expr).contains(SE.getUnsignedRange(RewriteTo));
15836 Guards.PreserveNSW &=
15837 SE.getSignedRange(Expr).contains(SE.getSignedRange(RewriteTo));
15838 }
15839
15840// Now that all rewrite information is collect, rewrite the collected
15841// expressions with the information in the map. This applies information to
15842// sub-expressions.
15843if (ExprsToRewrite.size() > 1) {
15844for (constSCEV *Expr : ExprsToRewrite) {
15845constSCEV *RewriteTo = Guards.RewriteMap[Expr];
15846 Guards.RewriteMap.erase(Expr);
15847 Guards.RewriteMap.insert({Expr, Guards.rewrite(RewriteTo)});
15848 }
15849 }
15850}
15851
15852constSCEV *ScalarEvolution::LoopGuards::rewrite(constSCEV *Expr) const{
15853 /// A rewriter to replace SCEV expressions in Map with the corresponding entry
15854 /// in the map. It skips AddRecExpr because we cannot guarantee that the
15855 /// replacement is loop invariant in the loop of the AddRec.
15856classSCEVLoopGuardRewriter
15857 :publicSCEVRewriteVisitor<SCEVLoopGuardRewriter> {
15858constDenseMap<const SCEV *, const SCEV *> &Map;
15859
15860SCEV::NoWrapFlags FlagMask =SCEV::FlagAnyWrap;
15861
15862public:
15863 SCEVLoopGuardRewriter(ScalarEvolution &SE,
15864constScalarEvolution::LoopGuards &Guards)
15865 :SCEVRewriteVisitor(SE), Map(Guards.RewriteMap) {
15866if (Guards.PreserveNUW)
15867 FlagMask =ScalarEvolution::setFlags(FlagMask,SCEV::FlagNUW);
15868if (Guards.PreserveNSW)
15869 FlagMask =ScalarEvolution::setFlags(FlagMask,SCEV::FlagNSW);
15870 }
15871
15872constSCEV *visitAddRecExpr(constSCEVAddRecExpr *Expr) {return Expr; }
15873
15874constSCEV *visitUnknown(constSCEVUnknown *Expr) {
15875autoI = Map.find(Expr);
15876if (I == Map.end())
15877return Expr;
15878returnI->second;
15879 }
15880
15881constSCEV *visitZeroExtendExpr(constSCEVZeroExtendExpr *Expr) {
15882autoI = Map.find(Expr);
15883if (I == Map.end()) {
15884// If we didn't find the extact ZExt expr in the map, check if there's
15885// an entry for a smaller ZExt we can use instead.
15886Type *Ty = Expr->getType();
15887constSCEV *Op = Expr->getOperand(0);
15888unsigned Bitwidth = Ty->getScalarSizeInBits() / 2;
15889while (Bitwidth % 8 == 0 && Bitwidth >= 8 &&
15890 Bitwidth >Op->getType()->getScalarSizeInBits()) {
15891Type *NarrowTy =IntegerType::get(SE.getContext(), Bitwidth);
15892auto *NarrowExt = SE.getZeroExtendExpr(Op, NarrowTy);
15893autoI = Map.find(NarrowExt);
15894if (I != Map.end())
15895return SE.getZeroExtendExpr(I->second, Ty);
15896 Bitwidth = Bitwidth / 2;
15897 }
15898
15899returnSCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitZeroExtendExpr(
15900 Expr);
15901 }
15902returnI->second;
15903 }
15904
15905constSCEV *visitSignExtendExpr(constSCEVSignExtendExpr *Expr) {
15906autoI = Map.find(Expr);
15907if (I == Map.end())
15908returnSCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSignExtendExpr(
15909 Expr);
15910returnI->second;
15911 }
15912
15913constSCEV *visitUMinExpr(constSCEVUMinExpr *Expr) {
15914autoI = Map.find(Expr);
15915if (I == Map.end())
15916returnSCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitUMinExpr(Expr);
15917returnI->second;
15918 }
15919
15920constSCEV *visitSMinExpr(constSCEVSMinExpr *Expr) {
15921autoI = Map.find(Expr);
15922if (I == Map.end())
15923returnSCEVRewriteVisitor<SCEVLoopGuardRewriter>::visitSMinExpr(Expr);
15924returnI->second;
15925 }
15926
15927constSCEV *visitAddExpr(constSCEVAddExpr *Expr) {
15928SmallVector<const SCEV *, 2>Operands;
15929bool Changed =false;
15930for (constauto *Op : Expr->operands()) {
15931Operands.push_back(
15932SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visit(Op));
15933 Changed |=Op !=Operands.back();
15934 }
15935// We are only replacing operands with equivalent values, so transfer the
15936// flags from the original expression.
15937return !Changed ? Expr
15938 : SE.getAddExpr(Operands,
15939ScalarEvolution::maskFlags(
15940 Expr->getNoWrapFlags(), FlagMask));
15941 }
15942
15943constSCEV *visitMulExpr(constSCEVMulExpr *Expr) {
15944SmallVector<const SCEV *, 2>Operands;
15945bool Changed =false;
15946for (constauto *Op : Expr->operands()) {
15947Operands.push_back(
15948SCEVRewriteVisitor<SCEVLoopGuardRewriter>::visit(Op));
15949 Changed |=Op !=Operands.back();
15950 }
15951// We are only replacing operands with equivalent values, so transfer the
15952// flags from the original expression.
15953return !Changed ? Expr
15954 : SE.getMulExpr(Operands,
15955ScalarEvolution::maskFlags(
15956 Expr->getNoWrapFlags(), FlagMask));
15957 }
15958 };
15959
15960if (RewriteMap.empty())
15961return Expr;
15962
15963 SCEVLoopGuardRewriterRewriter(SE, *this);
15964returnRewriter.visit(Expr);
15965}
15966
15967constSCEV *ScalarEvolution::applyLoopGuards(constSCEV *Expr,constLoop *L) {
15968returnapplyLoopGuards(Expr,LoopGuards::collect(L, *this));
15969}
15970
15971constSCEV *ScalarEvolution::applyLoopGuards(constSCEV *Expr,
15972constLoopGuards &Guards) {
15973return Guards.rewrite(Expr);
15974}
Poison
@ Poison
Definition:AArch64AsmPrinter.cpp:72
S1
static const LLT S1
Definition:AMDGPULegalizerInfo.cpp:282
PHI
Rewrite undef for PHI
Definition:AMDGPURewriteUndefForPHI.cpp:100
APInt.h
This file implements a class to represent arbitrary precision integral constant values and operations...
PostInc
@ PostInc
Definition:ARCInstrInfo.cpp:34
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition:ARMSLSHardening.cpp:73
ArrayRef.h
AssumptionCache.h
instructions
Expand Atomic instructions
Definition:AtomicExpandPass.cpp:172
true
basic Basic Alias true
Definition:BasicAliasAnalysis.cpp:1981
Analysis
block Block Frequency Analysis
Definition:BlockFrequencyInfo.cpp:300
From
BlockVerifier::State From
Definition:BlockVerifier.cpp:57
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Casting.h
CommandLine.h
Compiler.h
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition:Compiler.h:622
ConstantFolding.h
ConstantRange.h
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DataLayout.h
Idx
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
Definition:DeadArgumentElimination.cpp:353
Debug.h
LLVM_DEBUG
#define LLVM_DEBUG(...)
Definition:Debug.h:106
DenseMap.h
This file defines the DenseMap class.
DepthFirstIterator.h
This file builds on the ADT/GraphTraits.h file to build generic depth first graph iterator.
DerivedTypes.h
Dominators.h
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
End
bool End
Definition:ELF_riscv.cpp:480
EquivalenceClasses.h
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
isSigned
static bool isSigned(unsigned int Opcode)
Definition:ExpandLargeDivRem.cpp:52
FoldingSet.h
This file defines a hash set that can be used to remove duplication of nodes in a graph.
GlobalAlias.h
GlobalValue.h
op
#define op(i)
GEP
Hexagon Common GEP
Definition:HexagonCommonGEP.cpp:170
Argument.h
BasicBlock.h
CFG.h
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
Constant.h
Function.h
Instruction.h
IntrinsicInst.h
Operator.h
Type.h
Use.h
This defines the Use class.
User.h
Value.h
Users
iv Induction Variable Users
Definition:IVUsers.cpp:48
InitializePasses.h
InstIterator.h
InstrTypes.h
MonotonicType
MonotonicType
Definition:InstructionSimplify.cpp:3042
InstructionSimplify.h
Instructions.h
Intrinsics.h
KnownBits.h
LLVMContext.h
isZero
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition:Lint.cpp:557
LoopInfo.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
I
#define I(x, y, z)
Definition:MD5.cpp:58
G
#define G(x, y, z)
Definition:MD5.cpp:56
Operands
mir Rename Register Operands
Definition:MIRNamerPass.cpp:74
MemoryBuiltins.h
T1
#define T1
Definition:Mips16ISelLowering.cpp:340
Signed
@ Signed
Definition:NVPTXISelLowering.cpp:4789
Range
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
II
uint64_t IntrinsicInst * II
Definition:NVVMIntrRange.cpp:51
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
P
#define P(N)
verify
ppc ctr loops verify
Definition:PPCCTRLoopsVerify.cpp:72
Operation
PowerPC Reduce CR logical Operation
Definition:PPCReduceCRLogicals.cpp:735
if
if(PassOpts->AAPipeline)
Definition:PassBuilderBindings.cpp:64
INITIALIZE_PASS_DEPENDENCY
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition:PassSupport.h:55
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:57
INITIALIZE_PASS_BEGIN
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:52
Pass.h
PatternMatch.h
Merge
R600 Clause Merge
Definition:R600ClauseMergePass.cpp:70
Cond
const SmallVectorImpl< MachineOperand > & Cond
Definition:RISCVRedundantCopyElimination.cpp:75
isValid
static bool isValid(const char C)
Returns true if C is a valid mangled character: <0-9a-zA-Z_>.
Definition:RustDemangle.cpp:181
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
RA
SI optimize exec mask operations pre RA
Definition:SIOptimizeExecMaskingPreRA.cpp:71
visit
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> op)
Definition:SPIRVPostLegalizer.cpp:132
STLExtras.h
This file contains some templates that are useful if you are working with the STL at all.
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
SaveAndRestore.h
This file provides utility classes that use RAII to save and restore values.
ScalarEvolutionExpressions.h
ScalarEvolutionPatternMatch.h
SCEVMinMaxExprContains
bool SCEVMinMaxExprContains(const SCEV *Root, const SCEV *OperandToFind, SCEVTypes RootKind)
Definition:ScalarEvolution.cpp:6069
MaxAddRecSize
static cl::opt< unsigned > MaxAddRecSize("scalar-evolution-max-add-rec-size", cl::Hidden, cl::desc("Max coefficients in AddRec during evolving"), cl::init(8))
RangeIterThreshold
static cl::opt< unsigned > RangeIterThreshold("scev-range-iter-threshold", cl::Hidden, cl::desc("Threshold for switching to iteratively computing SCEV ranges"), cl::init(32))
isIntegerLoopHeaderPHI
static const Loop * isIntegerLoopHeaderPHI(const PHINode *PN, LoopInfo &LI)
Definition:ScalarEvolution.cpp:5389
getConstantTripCount
static unsigned getConstantTripCount(const SCEVConstant *ExitCount)
Definition:ScalarEvolution.cpp:8223
CompareValueComplexity
static int CompareValueComplexity(const LoopInfo *const LI, Value *LV, Value *RV, unsigned Depth)
Compare the two values LV and RV in terms of their "complexity" where "complexity" is a partial (and ...
Definition:ScalarEvolution.cpp:579
PushLoopPHIs
static void PushLoopPHIs(const Loop *L, SmallVectorImpl< Instruction * > &Worklist, SmallPtrSetImpl< Instruction * > &Visited)
Push PHI nodes in the header of the given loop onto the given Worklist.
Definition:ScalarEvolution.cpp:8378
insertFoldCacheEntry
static void insertFoldCacheEntry(const ScalarEvolution::FoldID &ID, const SCEV *S, DenseMap< ScalarEvolution::FoldID, const SCEV * > &FoldCache, DenseMap< const SCEV *, SmallVector< ScalarEvolution::FoldID, 2 > > &FoldCacheUser)
Definition:ScalarEvolution.cpp:1542
ClassifyExpressions
static cl::opt< bool > ClassifyExpressions("scalar-evolution-classify-expressions", cl::Hidden, cl::init(true), cl::desc("When printing analysis, include information on every instruction"))
CanConstantFold
static bool CanConstantFold(const Instruction *I)
Return true if we can constant fold an instruction of the specified type, assuming that all operands ...
Definition:ScalarEvolution.cpp:9549
AddOpsInlineThreshold
static cl::opt< unsigned > AddOpsInlineThreshold("scev-addops-inline-threshold", cl::Hidden, cl::desc("Threshold for inlining addition operands into a SCEV"), cl::init(500))
MaxLoopGuardCollectionDepth
static cl::opt< unsigned > MaxLoopGuardCollectionDepth("scalar-evolution-max-loop-guard-collection-depth", cl::Hidden, cl::desc("Maximum depth for recursive loop guard collection"), cl::init(1))
VerifyIR
static cl::opt< bool > VerifyIR("scev-verify-ir", cl::Hidden, cl::desc("Verify IR correctness when making sensitive SCEV queries (slow)"), cl::init(false))
BrPHIToSelect
static bool BrPHIToSelect(DominatorTree &DT, BranchInst *BI, PHINode *Merge, Value *&C, Value *&LHS, Value *&RHS)
Definition:ScalarEvolution.cpp:5950
CompareSCEVComplexity
static std::optional< int > CompareSCEVComplexity(EquivalenceClasses< const SCEV * > &EqCacheSCEV, const LoopInfo *const LI, const SCEV *LHS, const SCEV *RHS, DominatorTree &DT, unsigned Depth=0)
Definition:ScalarEvolution.cpp:656
getPreStartForExtend
static const SCEV * getPreStartForExtend(const SCEVAddRecExpr *AR, Type *Ty, ScalarEvolution *SE, unsigned Depth)
Definition:ScalarEvolution.cpp:1339
MinOptional
static std::optional< APInt > MinOptional(std::optional< APInt > X, std::optional< APInt > Y)
Helper function to compare optional APInts: (a) if X and Y both exist, return min(X,...
Definition:ScalarEvolution.cpp:10315
MulOpsInlineThreshold
static cl::opt< unsigned > MulOpsInlineThreshold("scev-mulops-inline-threshold", cl::Hidden, cl::desc("Threshold for inlining multiplication operands into a SCEV"), cl::init(32))
GroupByComplexity
static void GroupByComplexity(SmallVectorImpl< const SCEV * > &Ops, LoopInfo *LI, DominatorTree &DT)
Given a list of SCEV objects, order them by their complexity, and group objects of the same complexit...
Definition:ScalarEvolution.cpp:774
constantFoldAndGroupOps
static const SCEV * constantFoldAndGroupOps(ScalarEvolution &SE, LoopInfo &LI, DominatorTree &DT, SmallVectorImpl< const SCEV * > &Ops, FoldT Fold, IsIdentityT IsIdentity, IsAbsorberT IsAbsorber)
Performs a number of common optimizations on the passed Ops.
Definition:ScalarEvolution.cpp:838
createNodeForSelectViaUMinSeq
static std::optional< const SCEV * > createNodeForSelectViaUMinSeq(ScalarEvolution *SE, const SCEV *CondExpr, const SCEV *TrueExpr, const SCEV *FalseExpr)
Definition:ScalarEvolution.cpp:6213
BuildConstantFromSCEV
static Constant * BuildConstantFromSCEV(const SCEV *V)
This builds up a Constant using the ConstantExpr interface.
Definition:ScalarEvolution.cpp:9882
EvaluateConstantChrecAtConstant
static ConstantInt * EvaluateConstantChrecAtConstant(const SCEVAddRecExpr *AddRec, ConstantInt *C, ScalarEvolution &SE)
Definition:ScalarEvolution.cpp:9395
BinomialCoefficient
static const SCEV * BinomialCoefficient(const SCEV *It, unsigned K, ScalarEvolution &SE, Type *ResultTy)
Compute BC(It, K). The result has width W. Assume, K > 0.
Definition:ScalarEvolution.cpp:876
MaxCastDepth
static cl::opt< unsigned > MaxCastDepth("scalar-evolution-max-cast-depth", cl::Hidden, cl::desc("Maximum depth of recursive SExt/ZExt/Trunc"), cl::init(8))
IsMinMaxConsistingOf
static bool IsMinMaxConsistingOf(const SCEV *MaybeMinMaxExpr, const SCEV *Candidate)
Is MaybeMinMaxExpr an (U|S)(Min|Max) of Candidate and some other values?
Definition:ScalarEvolution.cpp:12480
getConstantEvolvingPHI
static PHINode * getConstantEvolvingPHI(Value *V, const Loop *L)
getConstantEvolvingPHI - Given an LLVM value and a loop, return a PHI node in the loop that V is deri...
Definition:ScalarEvolution.cpp:9623
MaxBruteForceIterations
static cl::opt< unsigned > MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden, cl::desc("Maximum number of iterations SCEV will " "symbolically execute a constant " "derived loop"), cl::init(100))
MatchBinarySub
static bool MatchBinarySub(const SCEV *S, const SCEV *&LHS, const SCEV *&RHS)
Definition:ScalarEvolution.cpp:10741
umul_ov
static uint64_t umul_ov(uint64_t i, uint64_t j, bool &Overflow)
Definition:ScalarEvolution.cpp:3051
PrintSCEVWithTypeHint
static void PrintSCEVWithTypeHint(raw_ostream &OS, const SCEV *S)
When printing a top-level SCEV for trip counts, it's helpful to include a type for constants which ar...
Definition:ScalarEvolution.cpp:13718
PrintLoopInfo
static void PrintLoopInfo(raw_ostream &OS, ScalarEvolution *SE, const Loop *L)
Definition:ScalarEvolution.cpp:13724
containsConstantInAddMulChain
static bool containsConstantInAddMulChain(const SCEV *StartExpr)
Determine if any of the operands in this SCEV are a constant or if any of the add or multiply express...
Definition:ScalarEvolution.cpp:3085
getExtendAddRecStart
static const SCEV * getExtendAddRecStart(const SCEVAddRecExpr *AR, Type *Ty, ScalarEvolution *SE, unsigned Depth)
Definition:ScalarEvolution.cpp:1416
hasHugeExpression
static bool hasHugeExpression(ArrayRef< const SCEV * > Ops)
Returns true if Ops contains a huge SCEV (the subtree of S contains at least HugeExprThreshold nodes)...
Definition:ScalarEvolution.cpp:822
MaxPhiSCCAnalysisSize
static cl::opt< unsigned > MaxPhiSCCAnalysisSize("scalar-evolution-max-scc-analysis-depth", cl::Hidden, cl::desc("Maximum amount of nodes to process while searching SCEVUnknown " "Phi strongly connected components"), cl::init(8))
IsKnownPredicateViaAddRecStart
static bool IsKnownPredicateViaAddRecStart(ScalarEvolution &SE, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:12489
MaxSCEVOperationsImplicationDepth
static cl::opt< unsigned > MaxSCEVOperationsImplicationDepth("scalar-evolution-max-scev-operations-implication-depth", cl::Hidden, cl::desc("Maximum depth of recursive SCEV operations implication analysis"), cl::init(2))
PushDefUseChildren
static void PushDefUseChildren(Instruction *I, SmallVectorImpl< Instruction * > &Worklist, SmallPtrSetImpl< Instruction * > &Visited)
Push users of the given Instruction onto the given Worklist.
Definition:ScalarEvolution.cpp:4847
SolveQuadraticAddRecRange
static std::optional< APInt > SolveQuadraticAddRecRange(const SCEVAddRecExpr *AddRec, const ConstantRange &Range, ScalarEvolution &SE)
Let c(n) be the value of the quadratic chrec {0,+,M,+,N} after n iterations.
Definition:ScalarEvolution.cpp:10397
UseContextForNoWrapFlagInference
static cl::opt< bool > UseContextForNoWrapFlagInference("scalar-evolution-use-context-for-no-wrap-flag-strenghening", cl::Hidden, cl::desc("Infer nuw/nsw flags using context where suitable"), cl::init(true))
EnableFiniteLoopControl
static cl::opt< bool > EnableFiniteLoopControl("scalar-evolution-finite-loop", cl::Hidden, cl::desc("Handle <= and >= in finite loops"), cl::init(true))
GetQuadraticEquation
static std::optional< std::tuple< APInt, APInt, APInt, APInt, unsigned > > GetQuadraticEquation(const SCEVAddRecExpr *AddRec)
For a given quadratic addrec, generate coefficients of the corresponding quadratic equation,...
Definition:ScalarEvolution.cpp:10262
isKnownPredicateExtendIdiom
static bool isKnownPredicateExtendIdiom(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:12722
MatchBinaryOp
static std::optional< BinaryOp > MatchBinaryOp(Value *V, const DataLayout &DL, AssumptionCache &AC, const DominatorTree &DT, const Instruction *CxtI)
Try to map V into a BinaryOp, and return std::nullopt on failure.
Definition:ScalarEvolution.cpp:5247
SolveQuadraticAddRecExact
static std::optional< APInt > SolveQuadraticAddRecExact(const SCEVAddRecExpr *AddRec, ScalarEvolution &SE)
Let c(n) be the value of the quadratic chrec {L,+,M,+,N} after n iterations.
Definition:ScalarEvolution.cpp:10364
TruncIfPossible
static std::optional< APInt > TruncIfPossible(std::optional< APInt > X, unsigned BitWidth)
Helper function to truncate an optional APInt to a given BitWidth.
Definition:ScalarEvolution.cpp:10339
MaxSCEVCompareDepth
static cl::opt< unsigned > MaxSCEVCompareDepth("scalar-evolution-max-scev-compare-depth", cl::Hidden, cl::desc("Maximum depth of recursive SCEV complexity comparisons"), cl::init(32))
extractConstantWithoutWrapping
static APInt extractConstantWithoutWrapping(ScalarEvolution &SE, const SCEVConstant *ConstantTerm, const SCEVAddExpr *WholeAddExpr)
Definition:ScalarEvolution.cpp:1510
MaxConstantEvolvingDepth
static cl::opt< unsigned > MaxConstantEvolvingDepth("scalar-evolution-max-constant-evolving-depth", cl::Hidden, cl::desc("Maximum depth of recursive constant evolving"), cl::init(32))
getRangeForAffineARHelper
static ConstantRange getRangeForAffineARHelper(APInt Step, const ConstantRange &StartRange, const APInt &MaxBECount, bool Signed)
Definition:ScalarEvolution.cpp:6982
GetRangeFromMetadata
static std::optional< ConstantRange > GetRangeFromMetadata(Value *V)
Helper method to assign a range to V from metadata present in the IR.
Definition:ScalarEvolution.cpp:6417
SolveLinEquationWithOverflow
static const SCEV * SolveLinEquationWithOverflow(const APInt &A, const SCEV *B, SmallVectorImpl< const SCEVPredicate * > *Predicates, ScalarEvolution &SE)
Finds the minimum unsigned root of the following equation:
Definition:ScalarEvolution.cpp:10199
HugeExprThreshold
static cl::opt< unsigned > HugeExprThreshold("scalar-evolution-huge-expr-threshold", cl::Hidden, cl::desc("Size of the expression which is considered huge"), cl::init(4096))
isSimpleCastedPHI
static Type * isSimpleCastedPHI(const SCEV *Op, const SCEVUnknown *SymbolicPHI, bool &Signed, ScalarEvolution &SE)
Helper function to createAddRecFromPHIWithCasts.
Definition:ScalarEvolution.cpp:5353
EvaluateExpression
static Constant * EvaluateExpression(Value *V, const Loop *L, DenseMap< Instruction *, Constant * > &Vals, const DataLayout &DL, const TargetLibraryInfo *TLI)
EvaluateExpression - Given an expression that passes the getConstantEvolvingPHI predicate,...
Definition:ScalarEvolution.cpp:9639
MatchNotExpr
static const SCEV * MatchNotExpr(const SCEV *Expr)
If Expr computes ~A, return A else return nullptr.
Definition:ScalarEvolution.cpp:4581
MaxValueCompareDepth
static cl::opt< unsigned > MaxValueCompareDepth("scalar-evolution-max-value-compare-depth", cl::Hidden, cl::desc("Maximum depth of recursive value complexity comparisons"), cl::init(2))
VerifySCEVOpt
static cl::opt< bool, true > VerifySCEVOpt("verify-scev", cl::Hidden, cl::location(VerifySCEV), cl::desc("Verify ScalarEvolution's backedge taken counts (slow)"))
getSignedOverflowLimitForStep
static const SCEV * getSignedOverflowLimitForStep(const SCEV *Step, ICmpInst::Predicate *Pred, ScalarEvolution *SE)
Definition:ScalarEvolution.cpp:1247
StrengthenNoWrapFlags
static SCEV::NoWrapFlags StrengthenNoWrapFlags(ScalarEvolution *SE, SCEVTypes Type, const ArrayRef< const SCEV * > Ops, SCEV::NoWrapFlags Flags)
Definition:ScalarEvolution.cpp:2439
MaxArithDepth
static cl::opt< unsigned > MaxArithDepth("scalar-evolution-max-arith-depth", cl::Hidden, cl::desc("Maximum depth of recursive arithmetics"), cl::init(32))
HasSameValue
static bool HasSameValue(const SCEV *A, const SCEV *B)
SCEV structural equivalence is usually sufficient for testing whether two expressions are equal,...
Definition:ScalarEvolution.cpp:10717
Choose
static uint64_t Choose(uint64_t n, uint64_t k, bool &Overflow)
Compute the result of "n choose k", the binomial coefficient.
Definition:ScalarEvolution.cpp:3060
CollectAddOperandsWithScales
static bool CollectAddOperandsWithScales(SmallDenseMap< const SCEV *, APInt, 16 > &M, SmallVectorImpl< const SCEV * > &NewOps, APInt &AccumulatedConstant, ArrayRef< const SCEV * > Ops, const APInt &Scale, ScalarEvolution &SE)
Process the given Ops list, which is a list of operands to be added under the given scale,...
Definition:ScalarEvolution.cpp:2251
canConstantEvolve
static bool canConstantEvolve(Instruction *I, const Loop *L)
Determine whether this instruction can constant evolve within this loop assuming its operands can all...
Definition:ScalarEvolution.cpp:9563
getConstantEvolvingPHIOperands
static PHINode * getConstantEvolvingPHIOperands(Instruction *UseInst, const Loop *L, DenseMap< Instruction *, PHINode * > &PHIMap, unsigned Depth)
getConstantEvolvingPHIOperands - Implement getConstantEvolvingPHI by recursing through each instructi...
Definition:ScalarEvolution.cpp:9581
scevUnconditionallyPropagatesPoisonFromOperands
static bool scevUnconditionallyPropagatesPoisonFromOperands(SCEVTypes Kind)
Definition:ScalarEvolution.cpp:4077
VerifySCEVStrict
static cl::opt< bool > VerifySCEVStrict("verify-scev-strict", cl::Hidden, cl::desc("Enable stricter verification with -verify-scev is passed"))
getOtherIncomingValue
static Constant * getOtherIncomingValue(PHINode *PN, BasicBlock *BB)
Definition:ScalarEvolution.cpp:9681
evolution
scalar evolution
Definition:ScalarEvolution.cpp:14691
UseExpensiveRangeSharpening
static cl::opt< bool > UseExpensiveRangeSharpening("scalar-evolution-use-expensive-range-sharpening", cl::Hidden, cl::init(false), cl::desc("Use more powerful methods of sharpening expression ranges. May " "be costly in terms of compile time"))
getUnsignedOverflowLimitForStep
static const SCEV * getUnsignedOverflowLimitForStep(const SCEV *Step, ICmpInst::Predicate *Pred, ScalarEvolution *SE)
Definition:ScalarEvolution.cpp:1267
IsKnownPredicateViaMinOrMax
static bool IsKnownPredicateViaMinOrMax(ScalarEvolution &SE, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Is LHS Pred RHS true on the virtue of LHS or RHS being a Min or Max expression?
Definition:ScalarEvolution.cpp:12523
ScalarEvolution.h
ScopeExit.h
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
InBlock
static bool InBlock(const Value *V, const BasicBlock *BB)
Definition:SelectionDAGBuilder.cpp:2423
Sequence.h
Provides some synthesis utilities to produce sequences of values.
SmallPtrSet.h
This file defines the SmallPtrSet class.
SmallSet.h
This file defines the SmallSet class.
SmallVector.h
This file defines the SmallVector class.
Statistic.h
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
STATISTIC
#define STATISTIC(VARNAME, DESC)
Definition:Statistic.h:166
StringExtras.h
This file contains some functions that are useful when dealing with strings.
StringRef.h
getType
static SymbolRef::Type getType(const Symbol *Sym)
Definition:TapiFile.cpp:39
TargetLibraryInfo.h
getOpcode
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition:VPlanSLP.cpp:191
ValueTracking.h
Verifier.h
Rewriter
Virtual Register Rewriter
Definition:VirtRegMap.cpp:261
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
IV
static const uint32_t IV[8]
Definition:blake3_impl.h:78
Predicate
Definition:AMDGPURegBankLegalizeRules.cpp:332
T
bool
llvm::APInt
Class for arbitrary precision integers.
Definition:APInt.h:78
llvm::APInt::umul_ov
APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition:APInt.cpp:1945
llvm::APInt::udiv
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition:APInt.cpp:1547
llvm::APInt::zext
APInt zext(unsigned width) const
Zero extend to a new width.
Definition:APInt.cpp:986
llvm::APInt::isMinSignedValue
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition:APInt.h:423
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition:APInt.h:1520
llvm::APInt::setHighBits
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition:APInt.h:1392
llvm::APInt::getHiBits
APInt getHiBits(unsigned numBits) const
Compute an APInt containing numBits highbits from this APInt.
Definition:APInt.cpp:612
llvm::APInt::zextOrTrunc
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition:APInt.cpp:1007
llvm::APInt::getActiveBits
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition:APInt.h:1492
llvm::APInt::trunc
APInt trunc(unsigned width) const
Truncate to new width.
Definition:APInt.cpp:910
llvm::APInt::getMaxValue
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition:APInt.h:206
llvm::APInt::abs
APInt abs() const
Get the absolute value.
Definition:APInt.h:1773
llvm::APInt::sgt
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition:APInt.h:1201
llvm::APInt::ugt
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition:APInt.h:1182
llvm::APInt::isZero
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition:APInt.h:380
llvm::APInt::isSignMask
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition:APInt.h:466
llvm::APInt::urem
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition:APInt.cpp:1640
llvm::APInt::getBitWidth
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition:APInt.h:1468
llvm::APInt::ult
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition:APInt.h:1111
llvm::APInt::getSignedMaxValue
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition:APInt.h:209
llvm::APInt::getMinValue
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition:APInt.h:216
llvm::APInt::isNegative
bool isNegative() const
Determine sign of this APInt.
Definition:APInt.h:329
llvm::APInt::sle
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition:APInt.h:1166
llvm::APInt::getSignedMinValue
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition:APInt.h:219
llvm::APInt::countTrailingZeros
unsigned countTrailingZeros() const
Definition:APInt.h:1626
llvm::APInt::isStrictlyPositive
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition:APInt.h:356
llvm::APInt::ashr
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition:APInt.h:827
llvm::APInt::multiplicativeInverse
APInt multiplicativeInverse() const
Definition:APInt.cpp:1248
llvm::APInt::ule
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition:APInt.h:1150
llvm::APInt::sext
APInt sext(unsigned width) const
Sign extend to a new width.
Definition:APInt.cpp:959
llvm::APInt::shl
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition:APInt.h:873
llvm::APInt::getLowBitsSet
static APInt getLowBitsSet(unsigned numBits, unsigned loBitsSet)
Constructs an APInt value that has the bottom loBitsSet bits set.
Definition:APInt.h:306
llvm::APInt::isSignBitSet
bool isSignBitSet() const
Determine if sign bit of this APInt is set.
Definition:APInt.h:341
llvm::APInt::slt
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition:APInt.h:1130
llvm::APInt::getZero
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition:APInt.h:200
llvm::APInt::isIntN
bool isIntN(unsigned N) const
Check if this APInt has an N-bits unsigned integer value.
Definition:APInt.h:432
llvm::APInt::getOneBitSet
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition:APInt.h:239
llvm::APInt::uge
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition:APInt.h:1221
llvm::AllAnalysesOn
This templated class represents "all analyses that operate over <a particular IR unit>" (e....
Definition:Analysis.h:49
llvm::AnalysisManager::Invalidator
API to communicate dependencies between analyses during invalidation.
Definition:PassManager.h:292
llvm::AnalysisManager::Invalidator::invalidate
bool invalidate(IRUnitT &IR, const PreservedAnalyses &PA)
Trigger the invalidation of some other analysis pass if not already handled and return whether it was...
Definition:PassManager.h:310
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition:PassManager.h:253
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition:PassManager.h:410
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition:PassAnalysisSupport.h:130
llvm::AnalysisUsage::addRequiredTransitive
AnalysisUsage & addRequiredTransitive()
Definition:PassAnalysisSupport.h:81
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::take_front
ArrayRef< T > take_front(size_t N=1) const
Return a copy of *this with only the first N elements.
Definition:ArrayRef.h:231
llvm::ArrayRef::end
iterator end() const
Definition:ArrayRef.h:157
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::begin
iterator begin() const
Definition:ArrayRef.h:156
llvm::AssumptionAnalysis
A function analysis which provides an AssumptionCache.
Definition:AssumptionCache.h:173
llvm::AssumptionCacheTracker
An immutable pass that tracks lazily created AssumptionCache objects.
Definition:AssumptionCache.h:204
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition:AssumptionCache.h:42
llvm::AssumptionCache::assumptions
MutableArrayRef< ResultElem > assumptions()
Access the list of assumption handles currently tracked for this function.
Definition:AssumptionCache.h:150
llvm::BasicBlockEdge
Definition:Dominators.h:94
llvm::BasicBlockEdge::isSingleEdge
bool isSingleEdge() const
Check if this is the only edge between Start and End.
Definition:Dominators.cpp:51
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:451
llvm::BasicBlock::front
const Instruction & front() const
Definition:BasicBlock.h:474
llvm::BasicBlock::getSinglePredecessor
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition:BasicBlock.cpp:471
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition:BasicBlock.h:220
llvm::BasicBlock::getTerminator
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition:BasicBlock.h:240
llvm::BinaryOpIntrinsic::getRHS
Value * getRHS() const
Definition:IntrinsicInst.h:914
llvm::BinaryOpIntrinsic::getNoWrapKind
unsigned getNoWrapKind() const
Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
Definition:IntrinsicInst.cpp:835
llvm::BinaryOpIntrinsic::getBinaryOp
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Definition:IntrinsicInst.cpp:802
llvm::BinaryOpIntrinsic::getLHS
Value * getLHS() const
Definition:IntrinsicInst.h:913
llvm::BinaryOperator
Definition:InstrTypes.h:170
llvm::BinaryOperator::getOpcode
BinaryOps getOpcode() const
Definition:InstrTypes.h:370
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition:Instructions.h:3016
llvm::BranchInst::isConditional
bool isConditional() const
Definition:Instructions.h:3090
llvm::BranchInst::getSuccessor
BasicBlock * getSuccessor(unsigned i) const
Definition:Instructions.h:3104
llvm::BranchInst::isUnconditional
bool isUnconditional() const
Definition:Instructions.h:3089
llvm::BranchInst::getCondition
Value * getCondition() const
Definition:Instructions.h:3092
llvm::BumpPtrAllocatorImpl::Allocate
LLVM_ATTRIBUTE_RETURNS_NONNULL void * Allocate(size_t Size, Align Alignment)
Allocate space at the specified alignment.
Definition:Allocator.h:148
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition:Instructions.h:1479
llvm::CallbackVH
Value handle with callbacks on RAUW and destruction.
Definition:ValueHandle.h:383
llvm::CallbackVH::setValPtr
void setValPtr(Value *P)
Definition:ValueHandle.h:390
llvm::CmpInst::isFalseWhenEqual
bool isFalseWhenEqual() const
This is just a convenience.
Definition:InstrTypes.h:946
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition:InstrTypes.h:673
llvm::CmpInst::BAD_ICMP_PREDICATE
@ BAD_ICMP_PREDICATE
Definition:InstrTypes.h:706
llvm::CmpInst::ICMP_SLT
@ ICMP_SLT
signed less than
Definition:InstrTypes.h:702
llvm::CmpInst::ICMP_SLE
@ ICMP_SLE
signed less or equal
Definition:InstrTypes.h:703
llvm::CmpInst::ICMP_UGE
@ ICMP_UGE
unsigned greater or equal
Definition:InstrTypes.h:697
llvm::CmpInst::ICMP_UGT
@ ICMP_UGT
unsigned greater than
Definition:InstrTypes.h:696
llvm::CmpInst::ICMP_SGT
@ ICMP_SGT
signed greater than
Definition:InstrTypes.h:700
llvm::CmpInst::ICMP_ULT
@ ICMP_ULT
unsigned less than
Definition:InstrTypes.h:698
llvm::CmpInst::ICMP_EQ
@ ICMP_EQ
equal
Definition:InstrTypes.h:694
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition:InstrTypes.h:695
llvm::CmpInst::ICMP_SGE
@ ICMP_SGE
signed greater or equal
Definition:InstrTypes.h:701
llvm::CmpInst::ICMP_ULE
@ ICMP_ULE
unsigned less or equal
Definition:InstrTypes.h:699
llvm::CmpInst::isSigned
bool isSigned() const
Definition:InstrTypes.h:928
llvm::CmpInst::getSwappedPredicate
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition:InstrTypes.h:825
llvm::CmpInst::isTrueWhenEqual
bool isTrueWhenEqual() const
This is just a convenience.
Definition:InstrTypes.h:940
llvm::CmpInst::getNonStrictPredicate
Predicate getNonStrictPredicate() const
For example, SGT -> SGE, SLT -> SLE, ULT -> ULE, UGT -> UGE.
Definition:InstrTypes.h:869
llvm::CmpInst::getInversePredicate
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition:InstrTypes.h:787
llvm::CmpInst::isUnsigned
bool isUnsigned() const
Definition:InstrTypes.h:934
llvm::CmpInst::isRelational
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition:InstrTypes.h:924
llvm::CmpPredicate
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition:CmpPredicate.h:22
llvm::ConstantExpr::getNot
static Constant * getNot(Constant *C)
Definition:Constants.cpp:2632
llvm::ConstantExpr::getPtrToInt
static Constant * getPtrToInt(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition:Constants.cpp:2293
llvm::ConstantExpr::getGetElementPtr
static Constant * getGetElementPtr(Type *Ty, Constant *C, ArrayRef< Constant * > IdxList, GEPNoWrapFlags NW=GEPNoWrapFlags::none(), std::optional< ConstantRange > InRange=std::nullopt, Type *OnlyIfReducedTy=nullptr)
Getelementptr form.
Definition:Constants.h:1267
llvm::ConstantExpr::getAdd
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition:Constants.cpp:2638
llvm::ConstantExpr::getNeg
static Constant * getNeg(Constant *C, bool HasNSW=false)
Definition:Constants.cpp:2626
llvm::ConstantExpr::getTrunc
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition:Constants.cpp:2279
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition:Constants.h:83
llvm::ConstantInt::isZero
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition:Constants.h:208
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition:Constants.cpp:873
llvm::ConstantInt::getZExtValue
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition:Constants.h:157
llvm::ConstantInt::getValue
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition:Constants.h:148
llvm::ConstantInt::getBool
static ConstantInt * getBool(LLVMContext &Context, bool V)
Definition:Constants.cpp:880
llvm::ConstantRange
This class represents a range of values.
Definition:ConstantRange.h:47
llvm::ConstantRange::add
ConstantRange add(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an addition of a value in this ran...
Definition:ConstantRange.cpp:1067
llvm::ConstantRange::zextOrTrunc
ConstantRange zextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
Definition:ConstantRange.cpp:917
llvm::ConstantRange::PreferredRangeType
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
Definition:ConstantRange.h:327
llvm::ConstantRange::Smallest
@ Smallest
Definition:ConstantRange.h:327
llvm::ConstantRange::Signed
@ Signed
Definition:ConstantRange.h:327
llvm::ConstantRange::Unsigned
@ Unsigned
Definition:ConstantRange.h:327
llvm::ConstantRange::getEquivalentICmp
bool getEquivalentICmp(CmpInst::Predicate &Pred, APInt &RHS) const
Set up Pred and RHS such that ConstantRange::makeExactICmpRegion(Pred, RHS) == *this.
Definition:ConstantRange.cpp:236
llvm::ConstantRange::subtract
ConstantRange subtract(const APInt &CI) const
Subtract the specified constant from the endpoints of this constant range.
Definition:ConstantRange.cpp:549
llvm::ConstantRange::getLower
const APInt & getLower() const
Return the lower value for this range.
Definition:ConstantRange.h:203
llvm::ConstantRange::truncate
ConstantRange truncate(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly smaller than the current typ...
Definition:ConstantRange.cpp:864
llvm::ConstantRange::isFullSet
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
Definition:ConstantRange.cpp:414
llvm::ConstantRange::icmp
bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
Definition:ConstantRange.cpp:243
llvm::ConstantRange::isEmptySet
bool isEmptySet() const
Return true if this set contains no members.
Definition:ConstantRange.cpp:418
llvm::ConstantRange::zeroExtend
ConstantRange zeroExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
Definition:ConstantRange.cpp:829
llvm::ConstantRange::isSignWrappedSet
bool isSignWrappedSet() const
Return true if this set wraps around the signed domain.
Definition:ConstantRange.cpp:430
llvm::ConstantRange::getSignedMin
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
Definition:ConstantRange.cpp:501
llvm::ConstantRange::isWrappedSet
bool isWrappedSet() const
Return true if this set wraps around the unsigned domain.
Definition:ConstantRange.cpp:422
llvm::ConstantRange::print
void print(raw_ostream &OS) const
Print out the bounds to a stream.
Definition:ConstantRange.cpp:2249
llvm::ConstantRange::signExtend
ConstantRange signExtend(uint32_t BitWidth) const
Return a new range in the specified integer type, which must be strictly larger than the current type...
Definition:ConstantRange.cpp:846
llvm::ConstantRange::getUpper
const APInt & getUpper() const
Return the upper value for this range.
Definition:ConstantRange.h:206
llvm::ConstantRange::unionWith
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
Definition:ConstantRange.cpp:687
llvm::ConstantRange::makeExactICmpRegion
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
Definition:ConstantRange.cpp:158
llvm::ConstantRange::contains
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
Definition:ConstantRange.cpp:507
llvm::ConstantRange::getUnsignedMax
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
Definition:ConstantRange.cpp:483
llvm::ConstantRange::intersectWith
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
Definition:ConstantRange.cpp:581
llvm::ConstantRange::getSignedMax
APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
Definition:ConstantRange.cpp:495
llvm::ConstantRange::getNonEmpty
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition:ConstantRange.h:84
llvm::ConstantRange::makeGuaranteedNoWrapRegion
static ConstantRange makeGuaranteedNoWrapRegion(Instruction::BinaryOps BinOp, const ConstantRange &Other, unsigned NoWrapKind)
Produce the largest range containing all X such that "X BinOp Y" is guaranteed not to wrap (overflow)...
Definition:ConstantRange.cpp:315
llvm::ConstantRange::getMinSignedBits
unsigned getMinSignedBits() const
Compute the maximal number of bits needed to represent every value in this signed range.
Definition:ConstantRange.cpp:541
llvm::ConstantRange::getBitWidth
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
Definition:ConstantRange.h:209
llvm::ConstantRange::sub
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
Definition:ConstantRange.cpp:1114
llvm::ConstantRange::sextOrTrunc
ConstantRange sextOrTrunc(uint32_t BitWidth) const
Make this range have the bit width given by BitWidth.
Definition:ConstantRange.cpp:926
llvm::ConstantRange::makeExactNoWrapRegion
static ConstantRange makeExactNoWrapRegion(Instruction::BinaryOps BinOp, const APInt &Other, unsigned NoWrapKind)
Produce the range that contains X if and only if "X BinOp Other" does not wrap.
Definition:ConstantRange.cpp:389
llvm::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition:DWARFExpression.h:32
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition:DataLayout.h:63
llvm::DataLayout::getStructLayout
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition:DataLayout.cpp:709
llvm::DataLayout::getIntPtrType
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space.
Definition:DataLayout.cpp:851
llvm::DataLayout::getIndexTypeSizeInBits
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition:DataLayout.cpp:754
llvm::DataLayout::getIndexType
IntegerType * getIndexType(LLVMContext &C, unsigned AddressSpace) const
Returns the type of a GEP index in AddressSpace.
Definition:DataLayout.cpp:878
llvm::DataLayout::getTypeSizeInBits
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition:DataLayout.h:617
llvm::DenseMapBase::lookup
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition:DenseMap.h:194
llvm::DenseMapBase::find
iterator find(const_arg_type_t< KeyT > Val)
Definition:DenseMap.h:156
llvm::DenseMapBase::try_emplace
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition:DenseMap.h:226
llvm::DenseMapBase::erase
bool erase(const KeyT &Val)
Definition:DenseMap.h:321
llvm::DenseMapBase::iterator
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition:DenseMap.h:71
llvm::DenseMapBase::find_as
iterator find_as(const LookupKeyT &Val)
Alternate version of find() which allows a different, and possibly less expensive,...
Definition:DenseMap.h:176
llvm::DenseMapBase::count
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition:DenseMap.h:152
llvm::DenseMapBase::end
iterator end()
Definition:DenseMap.h:84
llvm::DenseMapBase::contains
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
Definition:DenseMap.h:147
llvm::DenseMapBase::insert
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition:DenseMap.h:211
llvm::DenseMapBase::clear
void clear()
Definition:DenseMap.h:110
llvm::DenseMapIterator
Definition:DenseMap.h:1189
llvm::DenseMap
Definition:DenseMap.h:727
llvm::DomTreeNodeBase< BasicBlock >
llvm::DominatorTreeAnalysis
Analysis pass which computes a DominatorTree.
Definition:Dominators.h:279
llvm::DominatorTreeBase::properlyDominates
bool properlyDominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
properlyDominates - Returns true iff A dominates B and A != B.
Definition:GenericDomTree.h:443
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition:Dominators.h:317
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition:Dominators.h:162
llvm::DominatorTree::isReachableFromEntry
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition:Dominators.cpp:321
llvm::DominatorTree::dominates
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition:Dominators.cpp:122
llvm::ElementCount
Definition:TypeSize.h:300
llvm::EquivalenceClasses
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
Definition:EquivalenceClasses.h:60
llvm::EquivalenceClasses::unionSets
member_iterator unionSets(const ElemTy &V1, const ElemTy &V2)
union - Merge the two equivalence sets for the specified values, inserting them if they do not alread...
Definition:EquivalenceClasses.h:238
llvm::EquivalenceClasses::isEquivalent
bool isEquivalent(const ElemTy &V1, const ElemTy &V2) const
Definition:EquivalenceClasses.h:264
llvm::FoldingSetNodeIDRef
FoldingSetNodeIDRef - This class describes a reference to an interned FoldingSetNodeID,...
Definition:FoldingSet.h:290
llvm::FoldingSetNodeID
FoldingSetNodeID - This class is used to gather all the unique data bits of a node.
Definition:FoldingSet.h:327
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition:Pass.h:310
llvm::Function
Definition:Function.h:63
llvm::Function::getEntryBlock
const BasicBlock & getEntryBlock() const
Definition:Function.h:809
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition:Function.cpp:731
llvm::GEPNoWrapFlags
Represents flags for the getelementptr instruction/expression.
Definition:GEPNoWrapFlags.h:26
llvm::GEPNoWrapFlags::hasNoUnsignedSignedWrap
bool hasNoUnsignedSignedWrap() const
Definition:GEPNoWrapFlags.h:64
llvm::GEPNoWrapFlags::hasNoUnsignedWrap
bool hasNoUnsignedWrap() const
Definition:GEPNoWrapFlags.h:65
llvm::GEPNoWrapFlags::none
static GEPNoWrapFlags none()
Definition:GEPNoWrapFlags.h:46
llvm::GEPOperator
Definition:Operator.h:425
llvm::GetElementPtrInst::getTypeAtIndex
static Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
Definition:Instructions.cpp:1474
llvm::GlobalValue
Definition:GlobalValue.h:48
llvm::GlobalValue::getParent
Module * getParent()
Get the module that this global value is contained inside of...
Definition:GlobalValue.h:657
llvm::GlobalValue::isPrivateLinkage
static bool isPrivateLinkage(LinkageTypes Linkage)
Definition:GlobalValue.h:407
llvm::GlobalValue::isInternalLinkage
static bool isInternalLinkage(LinkageTypes Linkage)
Definition:GlobalValue.h:404
llvm::ICmpInst
This instruction compares its operands according to the predicate given to the constructor.
Definition:Instructions.h:1158
llvm::ICmpInst::getCmpPredicate
CmpPredicate getCmpPredicate() const
Definition:Instructions.h:1208
llvm::ICmpInst::isGE
static bool isGE(Predicate P)
Return true if the predicate is SGE or UGE.
Definition:Instructions.h:1329
llvm::ICmpInst::getSwappedCmpPredicate
CmpPredicate getSwappedCmpPredicate() const
Definition:Instructions.h:1230
llvm::ICmpInst::compare
static bool compare(const APInt &LHS, const APInt &RHS, ICmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
Definition:Instructions.cpp:3745
llvm::ICmpInst::isLT
static bool isLT(Predicate P)
Return true if the predicate is SLT or ULT.
Definition:Instructions.h:1323
llvm::ICmpInst::getInverseCmpPredicate
CmpPredicate getInverseCmpPredicate() const
Definition:Instructions.h:1219
llvm::ICmpInst::isGT
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
Definition:Instructions.h:1317
llvm::ICmpInst::getFlippedSignednessPredicate
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
Definition:Instructions.h:1265
llvm::ICmpInst::getInverseCmpPredicate
static CmpPredicate getInverseCmpPredicate(CmpPredicate Pred)
Definition:Instructions.h:1214
llvm::ICmpInst::isEquality
bool isEquality() const
Return true if this predicate is either EQ or NE.
Definition:Instructions.h:1291
llvm::ICmpInst::isRelational
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition:Instructions.h:1305
llvm::ICmpInst::isLE
static bool isLE(Predicate P)
Return true if the predicate is SLE or ULE.
Definition:Instructions.h:1335
llvm::Init
Definition:Record.h:285
llvm::Instruction
Definition:Instruction.h:68
llvm::Instruction::hasNoUnsignedWrap
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
Definition:Instruction.cpp:403
llvm::Instruction::hasNoSignedWrap
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
Definition:Instruction.cpp:410
llvm::Instruction::isIdenticalToWhenDefined
bool isIdenticalToWhenDefined(const Instruction *I, bool IntersectAttrs=false) const LLVM_READONLY
This is like isIdenticalTo, except that it ignores the SubclassOptionalData flags,...
Definition:Instruction.cpp:919
llvm::Instruction::BinaryOps
BinaryOps
Definition:Instruction.h:989
llvm::IntegerType
Class to represent integer types.
Definition:DerivedTypes.h:42
llvm::IntegerType::get
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition:Type.cpp:311
llvm::LoadInst
An instruction for reading from memory.
Definition:Instructions.h:176
llvm::LoopAnalysis
Analysis pass that exposes the LoopInfo for a function.
Definition:LoopInfo.h:566
llvm::LoopBase::contains
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition:GenericLoopInfo.h:124
llvm::LoopBase::getHeader
BlockT * getHeader() const
Definition:GenericLoopInfo.h:90
llvm::LoopBase::getLoopDepth
unsigned getLoopDepth() const
Return the nesting level of this loop.
Definition:GenericLoopInfo.h:82
llvm::LoopBase::getLoopPredecessor
BlockT * getLoopPredecessor() const
If the given loop's header has exactly one unique predecessor outside the loop, return it.
Definition:GenericLoopInfoImpl.h:235
llvm::LoopBase::getParentLoop
LoopT * getParentLoop() const
Return the parent loop if it exists or nullptr for top level loops.
Definition:GenericLoopInfo.h:99
llvm::LoopInfoBase::end
iterator end() const
Definition:GenericLoopInfo.h:582
llvm::LoopInfoBase::getLoopDepth
unsigned getLoopDepth(const BlockT *BB) const
Return the loop nesting level of the specified block.
Definition:GenericLoopInfo.h:613
llvm::LoopInfoBase::begin
iterator begin() const
Definition:GenericLoopInfo.h:581
llvm::LoopInfoBase::getLoopFor
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition:GenericLoopInfo.h:606
llvm::LoopInfoWrapperPass
The legacy pass manager's analysis pass to compute loop information.
Definition:LoopInfo.h:593
llvm::LoopInfo
Definition:LoopInfo.h:407
llvm::Loop
Represents a single loop in the control flow graph.
Definition:LoopInfo.h:39
llvm::Loop::isLoopInvariant
bool isLoopInvariant(const Value *V) const
Return true if the specified value is loop invariant.
Definition:LoopInfo.cpp:61
llvm::MDNode
Metadata node.
Definition:Metadata.h:1073
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::Operator
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition:Operator.h:32
llvm::Operator::getOpcode
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition:Operator.h:42
llvm::OverflowingBinaryOperator
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition:Operator.h:77
llvm::OverflowingBinaryOperator::hasNoSignedWrap
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
Definition:Operator.h:110
llvm::OverflowingBinaryOperator::NoSignedWrap
@ NoSignedWrap
Definition:Operator.h:82
llvm::OverflowingBinaryOperator::hasNoUnsignedWrap
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
Definition:Operator.h:104
llvm::PHINode
Definition:Instructions.h:2600
llvm::PHINode::blocks
iterator_range< const_block_iterator > blocks() const
Definition:Instructions.h:2661
llvm::PHINode::incoming_values
op_range incoming_values()
Definition:Instructions.h:2665
llvm::PHINode::getIncomingValueForBlock
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition:Instructions.h:2775
llvm::PHINode::getIncomingBlock
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Definition:Instructions.h:2695
llvm::PHINode::getIncomingValue
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Definition:Instructions.h:2675
llvm::PHINode::getNumIncomingValues
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition:Instructions.h:2671
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition:PassRegistry.cpp:24
llvm::PointerIntPair
PointerIntPair - This class implements a pair of a pointer and small integer.
Definition:PointerIntPair.h:80
llvm::PointerType::getUnqual
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the default address space (address sp...
Definition:DerivedTypes.h:686
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition:Constants.cpp:1878
llvm::PredicatedScalarEvolution
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
Definition:ScalarEvolution.h:2383
llvm::PredicatedScalarEvolution::addPredicate
void addPredicate(const SCEVPredicate &Pred)
Adds a new predicate.
Definition:ScalarEvolution.cpp:15161
llvm::PredicatedScalarEvolution::getPredicate
const SCEVPredicate & getPredicate() const
Definition:ScalarEvolution.cpp:15171
llvm::PredicatedScalarEvolution::hasNoOverflow
bool hasNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags)
Returns true if we've proved that V doesn't wrap by means of a SCEV predicate.
Definition:ScalarEvolution.cpp:15201
llvm::PredicatedScalarEvolution::setNoOverflow
void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags)
Proves that V doesn't overflow by adding SCEV predicate.
Definition:ScalarEvolution.cpp:15185
llvm::PredicatedScalarEvolution::print
void print(raw_ostream &OS, unsigned Depth) const
Print the SCEV mappings done by the Predicated Scalar Evolution.
Definition:ScalarEvolution.cpp:15242
llvm::PredicatedScalarEvolution::areAddRecsEqualWithPreds
bool areAddRecsEqualWithPreds(const SCEVAddRecExpr *AR1, const SCEVAddRecExpr *AR2) const
Check if AR1 and AR2 are equal, while taking into account Equal predicates in Preds.
Definition:ScalarEvolution.cpp:5703
llvm::PredicatedScalarEvolution::PredicatedScalarEvolution
PredicatedScalarEvolution(ScalarEvolution &SE, Loop &L)
Definition:ScalarEvolution.cpp:15094
llvm::PredicatedScalarEvolution::getAsAddRec
const SCEVAddRecExpr * getAsAddRec(Value *V)
Attempts to produce an AddRecExpr for V by adding additional SCEV predicates.
Definition:ScalarEvolution.cpp:15217
llvm::PredicatedScalarEvolution::getSmallConstantMaxTripCount
unsigned getSmallConstantMaxTripCount()
Returns the upper bound of the loop trip count as a normal unsigned value, or 0 if the trip count is ...
Definition:ScalarEvolution.cpp:15151
llvm::PredicatedScalarEvolution::getBackedgeTakenCount
const SCEV * getBackedgeTakenCount()
Get the (predicated) backedge count for the analyzed loop.
Definition:ScalarEvolution.cpp:15130
llvm::PredicatedScalarEvolution::getSymbolicMaxBackedgeTakenCount
const SCEV * getSymbolicMaxBackedgeTakenCount()
Get the (predicated) symbolic max backedge count for the analyzed loop.
Definition:ScalarEvolution.cpp:15140
llvm::PredicatedScalarEvolution::getSCEV
const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
Definition:ScalarEvolution.cpp:15111
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition:Analysis.h:111
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition:Analysis.h:117
llvm::PreservedAnalyses::getChecker
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition:Analysis.h:264
llvm::Register::isValid
constexpr bool isValid() const
Definition:Register.h:115
llvm::SCEVAddExpr
This node represents an addition of some number of SCEVs.
Definition:ScalarEvolutionExpressions.h:266
llvm::SCEVAddRecExpr
This node represents a polynomial recurrence on the trip count of the specified loop.
Definition:ScalarEvolutionExpressions.h:347
llvm::SCEVAddRecExpr::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:357
llvm::SCEVAddRecExpr::getStart
const SCEV * getStart() const
Definition:ScalarEvolutionExpressions.h:358
llvm::SCEVAddRecExpr::evaluateAtIteration
const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
Definition:ScalarEvolution.cpp:989
llvm::SCEVAddRecExpr::getStepRecurrence
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
Definition:ScalarEvolutionExpressions.h:365
llvm::SCEVAddRecExpr::setNoWrapFlags
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a recurrence without clearing any previously set flags.
Definition:ScalarEvolutionExpressions.h:389
llvm::SCEVAddRecExpr::isAffine
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
Definition:ScalarEvolutionExpressions.h:375
llvm::SCEVAddRecExpr::isQuadratic
bool isQuadratic() const
Return true if this represents an expression A + B*x + C*x^2 where A, B and C are loop invariant valu...
Definition:ScalarEvolutionExpressions.h:384
llvm::SCEVAddRecExpr::getNumIterationsInRange
const SCEV * getNumIterationsInRange(const ConstantRange &Range, ScalarEvolution &SE) const
Return the number of iterations of this loop that produce values in the specified constant range.
Definition:ScalarEvolution.cpp:13480
llvm::SCEVAddRecExpr::getPostIncExpr
const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
Return an expression representing the value of this expression one iteration of the loop ahead.
Definition:ScalarEvolution.cpp:13552
llvm::SCEVAddRecExpr::getLoop
const Loop * getLoop() const
Definition:ScalarEvolutionExpressions.h:359
llvm::SCEVCastExpr
This is the base class for unary cast operator classes.
Definition:ScalarEvolutionExpressions.h:103
llvm::SCEVCastExpr::Ty
Type * Ty
Definition:ScalarEvolutionExpressions.h:106
llvm::SCEVCastExpr::getOperand
const SCEV * getOperand() const
Definition:ScalarEvolutionExpressions.h:112
llvm::SCEVCastExpr::SCEVCastExpr
SCEVCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, const SCEV *op, Type *ty)
Definition:ScalarEvolution.cpp:513
llvm::SCEVCastExpr::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:119
llvm::SCEVCommutativeExpr::setNoWrapFlags
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a non-recurrence without clearing previously set flags.
Definition:ScalarEvolutionExpressions.h:262
llvm::SCEVComparePredicate
This class represents an assumption that the expression LHS Pred RHS evaluates to true,...
Definition:ScalarEvolution.h:277
llvm::SCEVComparePredicate::SCEVComparePredicate
SCEVComparePredicate(const FoldingSetNodeIDRef ID, const ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:14924
llvm::SCEVComparePredicate::isAlwaysTrue
bool isAlwaysTrue() const override
Returns true if the predicate is always true.
Definition:ScalarEvolution.cpp:14945
llvm::SCEVComparePredicate::print
void print(raw_ostream &OS, unsigned Depth=0) const override
Prints a textual representation of this predicate with an indentation of Depth.
Definition:ScalarEvolution.cpp:14947
llvm::SCEVComparePredicate::implies
bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override
Implementation of the SCEVPredicate interface.
Definition:ScalarEvolution.cpp:14932
llvm::SCEVConstant
This class represents a constant integer value.
Definition:ScalarEvolutionExpressions.h:60
llvm::SCEVConstant::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:72
llvm::SCEVConstant::getValue
ConstantInt * getValue() const
Definition:ScalarEvolutionExpressions.h:69
llvm::SCEVConstant::getAPInt
const APInt & getAPInt() const
Definition:ScalarEvolutionExpressions.h:70
llvm::SCEVIntegralCastExpr
This is the base class for unary integral cast operator classes.
Definition:ScalarEvolutionExpressions.h:141
llvm::SCEVIntegralCastExpr::SCEVIntegralCastExpr
SCEVIntegralCastExpr(const FoldingSetNodeIDRef ID, SCEVTypes SCEVTy, const SCEV *op, Type *ty)
Definition:ScalarEvolution.cpp:524
llvm::SCEVMinMaxExpr
This node is the base class min/max selections.
Definition:ScalarEvolutionExpressions.h:424
llvm::SCEVMinMaxExpr::negate
static enum SCEVTypes negate(enum SCEVTypes T)
Definition:ScalarEvolutionExpressions.h:447
llvm::SCEVMulExpr
This node represents multiplication of some number of SCEVs.
Definition:ScalarEvolutionExpressions.h:290
llvm::SCEVNAryExpr
This node is a base class providing common functionality for n'ary operators.
Definition:ScalarEvolutionExpressions.h:196
llvm::SCEVNAryExpr::hasNoUnsignedWrap
bool hasNoUnsignedWrap() const
Definition:ScalarEvolutionExpressions.h:226
llvm::SCEVNAryExpr::hasNoSelfWrap
bool hasNoSelfWrap() const
Definition:ScalarEvolutionExpressions.h:234
llvm::SCEVNAryExpr::getNumOperands
size_t getNumOperands() const
Definition:ScalarEvolutionExpressions.h:211
llvm::SCEVNAryExpr::hasNoSignedWrap
bool hasNoSignedWrap() const
Definition:ScalarEvolutionExpressions.h:230
llvm::SCEVNAryExpr::getNoWrapFlags
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
Definition:ScalarEvolutionExpressions.h:222
llvm::SCEVNAryExpr::getOperand
const SCEV * getOperand(unsigned i) const
Definition:ScalarEvolutionExpressions.h:213
llvm::SCEVNAryExpr::Operands
const SCEV *const * Operands
Definition:ScalarEvolutionExpressions.h:202
llvm::SCEVNAryExpr::operands
ArrayRef< const SCEV * > operands() const
Definition:ScalarEvolutionExpressions.h:218
llvm::SCEVPredicate
This class represents an assumption made using SCEV expressions which can be checked at run-time.
Definition:ScalarEvolution.h:214
llvm::SCEVPredicate::SCEVPredicate
SCEVPredicate(const SCEVPredicate &)=default
llvm::SCEVPredicate::SCEVPredicateKind
SCEVPredicateKind
Definition:ScalarEvolution.h:222
llvm::SCEVPredicate::P_Compare
@ P_Compare
Definition:ScalarEvolution.h:222
llvm::SCEVPredicate::P_Wrap
@ P_Wrap
Definition:ScalarEvolution.h:222
llvm::SCEVPredicate::implies
virtual bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const =0
Returns true if this predicate implies N.
llvm::SCEVPredicate::print
virtual void print(raw_ostream &OS, unsigned Depth=0) const =0
Prints a textual representation of this predicate with an indentation of Depth.
llvm::SCEVPtrToIntExpr
This class represents a cast from a pointer to a pointer-sized integer value.
Definition:ScalarEvolutionExpressions.h:130
llvm::SCEVRewriteVisitor
This visitor recursively visits a SCEV expression and re-writes it.
Definition:ScalarEvolutionExpressions.h:747
llvm::SCEVRewriteVisitor::visitSignExtendExpr
const SCEV * visitSignExtendExpr(const SCEVSignExtendExpr *Expr)
Definition:ScalarEvolutionExpressions.h:795
llvm::SCEVRewriteVisitor::visitZeroExtendExpr
const SCEV * visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr)
Definition:ScalarEvolutionExpressions.h:788
llvm::SCEVRewriteVisitor::visitSMinExpr
const SCEV * visitSMinExpr(const SCEVSMinExpr *Expr)
Definition:ScalarEvolutionExpressions.h:861
llvm::SCEVRewriteVisitor::visitUMinExpr
const SCEV * visitUMinExpr(const SCEVUMinExpr *Expr)
Definition:ScalarEvolutionExpressions.h:871
llvm::SCEVSMaxExpr
This class represents a signed maximum selection.
Definition:ScalarEvolutionExpressions.h:464
llvm::SCEVSMinExpr
This class represents a signed minimum selection.
Definition:ScalarEvolutionExpressions.h:488
llvm::SCEVSequentialMinMaxExpr
This node is the base class for sequential/in-order min/max selections.
Definition:ScalarEvolutionExpressions.h:517
llvm::SCEVSequentialMinMaxExpr::getEquivalentNonSequentialSCEVType
SCEVTypes getEquivalentNonSequentialSCEVType() const
Definition:ScalarEvolutionExpressions.h:550
llvm::SCEVSequentialUMinExpr
This class represents a sequential/in-order unsigned minimum selection.
Definition:ScalarEvolutionExpressions.h:560
llvm::SCEVSignExtendExpr
This class represents a sign extension of a small integer value to a larger integer value.
Definition:ScalarEvolutionExpressions.h:182
llvm::SCEVTraversal
Visit all nodes in the expression tree using worklist traversal.
Definition:ScalarEvolutionExpressions.h:662
llvm::SCEVTraversal::visitAll
void visitAll(const SCEV *Root)
Definition:ScalarEvolutionExpressions.h:675
llvm::SCEVTruncateExpr
This class represents a truncation of an integer value to a smaller integer value.
Definition:ScalarEvolutionExpressions.h:156
llvm::SCEVUDivExpr
This class represents a binary unsigned division operation.
Definition:ScalarEvolutionExpressions.h:304
llvm::SCEVUDivExpr::getLHS
const SCEV * getLHS() const
Definition:ScalarEvolutionExpressions.h:316
llvm::SCEVUDivExpr::getRHS
const SCEV * getRHS() const
Definition:ScalarEvolutionExpressions.h:317
llvm::SCEVUMaxExpr
This class represents an unsigned maximum selection.
Definition:ScalarEvolutionExpressions.h:476
llvm::SCEVUMinExpr
This class represents an unsigned minimum selection.
Definition:ScalarEvolutionExpressions.h:500
llvm::SCEVUnionPredicate
This class represents a composition of other SCEV predicates, and is the class that most clients will...
Definition:ScalarEvolution.h:412
llvm::SCEVUnionPredicate::print
void print(raw_ostream &OS, unsigned Depth) const override
Prints a textual representation of this predicate with an indentation of Depth.
Definition:ScalarEvolution.cpp:15066
llvm::SCEVUnionPredicate::implies
bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override
Returns true if this predicate implies N.
Definition:ScalarEvolution.cpp:15055
llvm::SCEVUnionPredicate::SCEVUnionPredicate
SCEVUnionPredicate(ArrayRef< const SCEVPredicate * > Preds, ScalarEvolution &SE)
Union predicates don't get cached so create a dummy set ID for it.
Definition:ScalarEvolution.cpp:15043
llvm::SCEVUnionPredicate::isAlwaysTrue
bool isAlwaysTrue() const override
Implementation of the SCEVPredicate interface.
Definition:ScalarEvolution.cpp:15050
llvm::SCEVUnknown
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
Definition:ScalarEvolutionExpressions.h:577
llvm::SCEVUnknown::getType
Type * getType() const
Definition:ScalarEvolutionExpressions.h:600
llvm::SCEVUnknown::getValue
Value * getValue() const
Definition:ScalarEvolutionExpressions.h:598
llvm::SCEVVScale
This class represents the value of vscale, as used when defining the length of a scalable vector or r...
Definition:ScalarEvolutionExpressions.h:80
llvm::SCEVWrapPredicate
This class represents an assumption made on an AddRec expression.
Definition:ScalarEvolution.h:317
llvm::SCEVWrapPredicate::IncrementWrapFlags
IncrementWrapFlags
Similar to SCEV::NoWrapFlags, but with slightly different semantics for FlagNUSW.
Definition:ScalarEvolution.h:341
llvm::SCEVWrapPredicate::IncrementAnyWrap
@ IncrementAnyWrap
Definition:ScalarEvolution.h:342
llvm::SCEVWrapPredicate::IncrementNUSW
@ IncrementNUSW
Definition:ScalarEvolution.h:343
llvm::SCEVWrapPredicate::IncrementNSSW
@ IncrementNSSW
Definition:ScalarEvolution.h:344
llvm::SCEVWrapPredicate::SCEVWrapPredicate
SCEVWrapPredicate(const FoldingSetNodeIDRef ID, const SCEVAddRecExpr *AR, IncrementWrapFlags Flags)
Definition:ScalarEvolution.cpp:14956
llvm::SCEVWrapPredicate::implies
bool implies(const SCEVPredicate *N, ScalarEvolution &SE) const override
Returns true if this predicate implies N.
Definition:ScalarEvolution.cpp:14963
llvm::SCEVWrapPredicate::setFlags
static SCEVWrapPredicate::IncrementWrapFlags setFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, SCEVWrapPredicate::IncrementWrapFlags OnFlags)
Definition:ScalarEvolution.h:368
llvm::SCEVWrapPredicate::print
void print(raw_ostream &OS, unsigned Depth=0) const override
Prints a textual representation of this predicate with an indentation of Depth.
Definition:ScalarEvolution.cpp:15012
llvm::SCEVWrapPredicate::isAlwaysTrue
bool isAlwaysTrue() const override
Returns true if the predicate is always true.
Definition:ScalarEvolution.cpp:15002
llvm::SCEVWrapPredicate::getExpr
const SCEVAddRecExpr * getExpr() const
Implementation of the SCEVPredicate interface.
Definition:ScalarEvolution.cpp:14961
llvm::SCEVWrapPredicate::clearFlags
static SCEVWrapPredicate::IncrementWrapFlags clearFlags(SCEVWrapPredicate::IncrementWrapFlags Flags, SCEVWrapPredicate::IncrementWrapFlags OffFlags)
Convenient IncrementWrapFlags manipulation methods.
Definition:ScalarEvolution.h:351
llvm::SCEVWrapPredicate::getImpliedFlags
static SCEVWrapPredicate::IncrementWrapFlags getImpliedFlags(const SCEVAddRecExpr *AR, ScalarEvolution &SE)
Returns the set of SCEVWrapPredicate no wrap flags implied by a SCEVAddRecExpr.
Definition:ScalarEvolution.cpp:15022
llvm::SCEVWrapPredicate::getFlags
IncrementWrapFlags getFlags() const
Returns the set assumed no overflow flags.
Definition:ScalarEvolution.h:392
llvm::SCEVZeroExtendExpr
This class represents a zero extension of a small integer value to a larger integer value.
Definition:ScalarEvolutionExpressions.h:168
llvm::SCEV
This class represents an analyzed expression in the program.
Definition:ScalarEvolution.h:71
llvm::SCEV::operands
ArrayRef< const SCEV * > operands() const
Return operands of this SCEV expression.
Definition:ScalarEvolution.cpp:420
llvm::SCEV::getExpressionSize
unsigned short getExpressionSize() const
Definition:ScalarEvolution.h:169
llvm::SCEV::isOne
bool isOne() const
Return true if the expression is a constant one.
Definition:ScalarEvolution.cpp:450
llvm::SCEV::isZero
bool isZero() const
Return true if the expression is a constant zero.
Definition:ScalarEvolution.cpp:448
llvm::SCEV::dump
void dump() const
This method is used for debugging.
Definition:ScalarEvolution.cpp:267
llvm::SCEV::isAllOnesValue
bool isAllOnesValue() const
Return true if the expression is a constant all-ones value.
Definition:ScalarEvolution.cpp:452
llvm::SCEV::isNonConstantNegative
bool isNonConstantNegative() const
Return true if the specified scev is negated, but not a constant.
Definition:ScalarEvolution.cpp:454
llvm::SCEV::print
void print(raw_ostream &OS) const
Print out the internal representation of this scalar to the specified stream.
Definition:ScalarEvolution.cpp:273
llvm::SCEV::getSCEVType
SCEVTypes getSCEVType() const
Definition:ScalarEvolution.h:140
llvm::SCEV::getType
Type * getType() const
Return the LLVM type of this SCEV expression.
Definition:ScalarEvolution.cpp:386
llvm::SCEV::NoWrapFlags
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
Definition:ScalarEvolution.h:126
llvm::SCEV::FlagAnyWrap
@ FlagAnyWrap
Definition:ScalarEvolution.h:127
llvm::SCEV::FlagNSW
@ FlagNSW
Definition:ScalarEvolution.h:130
llvm::SCEV::FlagNUW
@ FlagNUW
Definition:ScalarEvolution.h:129
llvm::SCEV::FlagNW
@ FlagNW
Definition:ScalarEvolution.h:128
llvm::ScalarEvolutionAnalysis
Analysis pass that exposes the ScalarEvolution for a function.
Definition:ScalarEvolution.h:2320
llvm::ScalarEvolutionAnalysis::run
ScalarEvolution run(Function &F, FunctionAnalysisManager &AM)
Definition:ScalarEvolution.cpp:14659
llvm::ScalarEvolutionPrinterPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition:ScalarEvolution.cpp:14675
llvm::ScalarEvolutionVerifierPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition:ScalarEvolution.cpp:14669
llvm::ScalarEvolutionWrapperPass
Definition:ScalarEvolution.h:2352
llvm::ScalarEvolutionWrapperPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition:ScalarEvolution.cpp:14722
llvm::ScalarEvolutionWrapperPass::print
void print(raw_ostream &OS, const Module *=nullptr) const override
print - Print out the internal state of the pass.
Definition:ScalarEvolution.cpp:14711
llvm::ScalarEvolutionWrapperPass::runOnFunction
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Definition:ScalarEvolution.cpp:14700
llvm::ScalarEvolutionWrapperPass::releaseMemory
void releaseMemory() override
releaseMemory() - This member can be implemented by a pass if it wants to be able to release its memo...
Definition:ScalarEvolution.cpp:14709
llvm::ScalarEvolutionWrapperPass::verifyAnalysis
void verifyAnalysis() const override
verifyAnalysis() - This member can be implemented by a analysis pass to check state of analysis infor...
Definition:ScalarEvolution.cpp:14715
llvm::ScalarEvolution::FoldID
Definition:ScalarEvolution.h:1374
llvm::ScalarEvolution::LoopGuards
Definition:ScalarEvolution.h:1309
llvm::ScalarEvolution::LoopGuards::collect
static LoopGuards collect(const Loop *L, ScalarEvolution &SE)
Collect rewrite map for loop guards for loop L, together with flags indicating if NUW and NSW can be ...
Definition:ScalarEvolution.cpp:15327
llvm::ScalarEvolution::LoopGuards::rewrite
const SCEV * rewrite(const SCEV *Expr) const
Try to apply the collected loop guards to Expr.
Definition:ScalarEvolution.cpp:15852
llvm::ScalarEvolution
The main scalar evolution driver.
Definition:ScalarEvolution.h:447
llvm::ScalarEvolution::getConstantMaxBackedgeTakenCount
const SCEV * getConstantMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEVConstant that is greater than or equal to (i.e.
Definition:ScalarEvolution.h:907
llvm::ScalarEvolution::hasFlags
static bool hasFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags TestFlags)
Definition:ScalarEvolution.h:479
llvm::ScalarEvolution::getDataLayout
const DataLayout & getDataLayout() const
Return the DataLayout associated with the module this SCEV instance is operating on.
Definition:ScalarEvolution.h:1275
llvm::ScalarEvolution::isKnownNonNegative
bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
Definition:ScalarEvolution.cpp:10952
llvm::ScalarEvolution::isKnownOnEveryIteration
bool isKnownOnEveryIteration(CmpPredicate Pred, const SCEVAddRecExpr *LHS, const SCEV *RHS)
Test if the condition described by Pred, LHS, RHS is known to be true on every iteration of the loop ...
Definition:ScalarEvolution.cpp:11098
llvm::ScalarEvolution::getNegativeSCEV
const SCEV * getNegativeSCEV(const SCEV *V, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
Return the SCEV object corresponding to -V.
Definition:ScalarEvolution.cpp:4569
llvm::ScalarEvolution::getLoopInvariantExitCondDuringFirstIterationsImpl
std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterationsImpl(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
Definition:ScalarEvolution.cpp:11276
llvm::ScalarEvolution::getSMaxExpr
const SCEV * getSMaxExpr(const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:4343
llvm::ScalarEvolution::getUDivCeilSCEV
const SCEV * getUDivCeilSCEV(const SCEV *N, const SCEV *D)
Compute ceil(N / D).
Definition:ScalarEvolution.cpp:12882
llvm::ScalarEvolution::getGEPExpr
const SCEV * getGEPExpr(GEPOperator *GEP, const SmallVectorImpl< const SCEV * > &IndexExprs)
Returns an expression for a GEP.
Definition:ScalarEvolution.cpp:3736
llvm::ScalarEvolution::getLoopInvariantExitCondDuringFirstIterations
std::optional< LoopInvariantPredicate > getLoopInvariantExitCondDuringFirstIterations(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI, const SCEV *MaxIter)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L at given Context duri...
Definition:ScalarEvolution.cpp:11256
llvm::ScalarEvolution::getWiderType
Type * getWiderType(Type *Ty1, Type *Ty2) const
Definition:ScalarEvolution.cpp:4469
llvm::ScalarEvolution::getAbsExpr
const SCEV * getAbsExpr(const SCEV *Op, bool IsNSW)
Definition:ScalarEvolution.cpp:3823
llvm::ScalarEvolution::isKnownNonPositive
bool isKnownNonPositive(const SCEV *S)
Test if the given expression is known to be non-positive.
Definition:ScalarEvolution.cpp:10956
llvm::ScalarEvolution::getURemExpr
const SCEV * getURemExpr(const SCEV *LHS, const SCEV *RHS)
Represents an unsigned remainder expression based on unsigned division.
Definition:ScalarEvolution.cpp:3371
llvm::ScalarEvolution::getConstantMultiple
APInt getConstantMultiple(const SCEV *S)
Returns the max constant multiple of S.
Definition:ScalarEvolution.cpp:6395
llvm::ScalarEvolution::isKnownNegative
bool isKnownNegative(const SCEV *S)
Test if the given expression is known to be negative.
Definition:ScalarEvolution.cpp:10944
llvm::ScalarEvolution::getPredicatedConstantMaxBackedgeTakenCount
const SCEV * getPredicatedConstantMaxBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getConstantMaxBackedgeTakenCount, except it will add a set of SCEV predicates to Predicate...
Definition:ScalarEvolution.cpp:8368
llvm::ScalarEvolution::removePointerBase
const SCEV * removePointerBase(const SCEV *S)
Compute an expression equivalent to S - getPointerBase(S).
Definition:ScalarEvolution.cpp:4625
llvm::ScalarEvolution::isLoopEntryGuardedByCond
bool isLoopEntryGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the loop is protected by a conditional between LHS and RHS.
Definition:ScalarEvolution.cpp:11721
llvm::ScalarEvolution::isKnownNonZero
bool isKnownNonZero(const SCEV *S)
Test if the given expression is known to be non-zero.
Definition:ScalarEvolution.cpp:10960
llvm::ScalarEvolution::getSCEVAtScope
const SCEV * getSCEVAtScope(const SCEV *S, const Loop *L)
Return a SCEV expression for the specified value at the specified scope in the program.
Definition:ScalarEvolution.cpp:9856
llvm::ScalarEvolution::getSMinExpr
const SCEV * getSMinExpr(const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:4361
llvm::ScalarEvolution::getBackedgeTakenCount
const SCEV * getBackedgeTakenCount(const Loop *L, ExitCountKind Kind=Exact)
If the specified loop has a predictable backedge-taken count, return it, otherwise return a SCEVCould...
Definition:ScalarEvolution.cpp:8350
llvm::ScalarEvolution::getUMaxExpr
const SCEV * getUMaxExpr(const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:4352
llvm::ScalarEvolution::setNoWrapFlags
void setNoWrapFlags(SCEVAddRecExpr *AddRec, SCEV::NoWrapFlags Flags)
Update no-wrap flags of an AddRec.
Definition:ScalarEvolution.cpp:6432
llvm::ScalarEvolution::getUMaxFromMismatchedTypes
const SCEV * getUMaxFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS)
Promote the operands to the wider of the types using zero-extension, and then perform a umax operatio...
Definition:ScalarEvolution.cpp:4777
llvm::ScalarEvolution::getZero
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
Definition:ScalarEvolution.h:653
llvm::ScalarEvolution::willNotOverflow
bool willNotOverflow(Instruction::BinaryOps BinOp, bool Signed, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI=nullptr)
Is operation BinOp between LHS and RHS provably does not have a signed/unsigned overflow (Signed)?...
Definition:ScalarEvolution.cpp:2314
llvm::ScalarEvolution::computeExitLimitFromCond
ExitLimit computeExitLimitFromCond(const Loop *L, Value *ExitCond, bool ExitIfTrue, bool ControlsOnlyExit, bool AllowPredicates=false)
Compute the number of times the backedge of the specified loop will execute if its exit condition wer...
Definition:ScalarEvolution.cpp:8973
llvm::ScalarEvolution::getZeroExtendExprImpl
const SCEV * getZeroExtendExprImpl(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1584
llvm::ScalarEvolution::getEqualPredicate
const SCEVPredicate * getEqualPredicate(const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:14730
llvm::ScalarEvolution::getSmallConstantTripMultiple
unsigned getSmallConstantTripMultiple(const Loop *L, const SCEV *ExitCount)
Returns the largest constant divisor of the trip count as a normal unsigned value,...
Definition:ScalarEvolution.cpp:8276
llvm::ScalarEvolution::getTypeSizeInBits
uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
Definition:ScalarEvolution.cpp:4448
llvm::ScalarEvolution::getConstant
const SCEV * getConstant(ConstantInt *V)
Definition:ScalarEvolution.cpp:473
llvm::ScalarEvolution::getPredicatedBackedgeTakenCount
const SCEV * getPredicatedBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getBackedgeTakenCount, except it will add a set of SCEV predicates to Predicates that are ...
Definition:ScalarEvolution.cpp:8345
llvm::ScalarEvolution::getSCEV
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
Definition:ScalarEvolution.cpp:4547
llvm::ScalarEvolution::getSignedRange
ConstantRange getSignedRange(const SCEV *S)
Determine the signed range for a particular SCEV.
Definition:ScalarEvolution.h:1013
llvm::ScalarEvolution::getNoopOrSignExtend
const SCEV * getNoopOrSignExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4742
llvm::ScalarEvolution::loopHasNoAbnormalExits
bool loopHasNoAbnormalExits(const Loop *L)
Return true if the loop has no abnormal exits.
Definition:ScalarEvolution.h:1353
llvm::ScalarEvolution::getTripCountFromExitCount
const SCEV * getTripCountFromExitCount(const SCEV *ExitCount)
A version of getTripCountFromExitCount below which always picks an evaluation type which can not resu...
Definition:ScalarEvolution.cpp:8182
llvm::ScalarEvolution::ScalarEvolution
ScalarEvolution(Function &F, TargetLibraryInfo &TLI, AssumptionCache &AC, DominatorTree &DT, LoopInfo &LI)
Definition:ScalarEvolution.cpp:13637
llvm::ScalarEvolution::getOne
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
Definition:ScalarEvolution.h:656
llvm::ScalarEvolution::getTruncateOrNoop
const SCEV * getTruncateOrNoop(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4766
llvm::ScalarEvolution::getCastExpr
const SCEV * getCastExpr(SCEVTypes Kind, const SCEV *Op, Type *Ty)
Definition:ScalarEvolution.cpp:2162
llvm::ScalarEvolution::getSequentialMinMaxExpr
const SCEV * getSequentialMinMaxExpr(SCEVTypes Kind, SmallVectorImpl< const SCEV * > &Operands)
Definition:ScalarEvolution.cpp:4231
llvm::ScalarEvolution::getLosslessPtrToIntExpr
const SCEV * getLosslessPtrToIntExpr(const SCEV *Op, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1016
llvm::ScalarEvolution::evaluatePredicateAt
std::optional< bool > evaluatePredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Check whether the condition described by Pred, LHS, and RHS is true or false in the given Context.
Definition:ScalarEvolution.cpp:11084
llvm::ScalarEvolution::getSmallConstantMaxTripCount
unsigned getSmallConstantMaxTripCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Returns the upper bound of the loop trip count as a normal unsigned value.
Definition:ScalarEvolution.cpp:8253
llvm::ScalarEvolution::getPtrToIntExpr
const SCEV * getPtrToIntExpr(const SCEV *Op, Type *Ty)
Definition:ScalarEvolution.cpp:1140
llvm::ScalarEvolution::isBackedgeTakenCountMaxOrZero
bool isBackedgeTakenCountMaxOrZero(const Loop *L)
Return true if the backedge taken count is either the value returned by getConstantMaxBackedgeTakenCo...
Definition:ScalarEvolution.cpp:8373
llvm::ScalarEvolution::forgetLoop
void forgetLoop(const Loop *L)
This method should be called by the client when it has changed a loop in a way that may effect Scalar...
Definition:ScalarEvolution.cpp:8496
llvm::ScalarEvolution::isLoopInvariant
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
Definition:ScalarEvolution.cpp:14100
llvm::ScalarEvolution::isKnownPositive
bool isKnownPositive(const SCEV *S)
Test if the given expression is known to be positive.
Definition:ScalarEvolution.cpp:10948
llvm::ScalarEvolution::getUnsignedRangeMin
APInt getUnsignedRangeMin(const SCEV *S)
Determine the min of the unsigned range for a particular SCEV.
Definition:ScalarEvolution.h:1002
llvm::ScalarEvolution::SimplifyICmpOperands
bool SimplifyICmpOperands(CmpPredicate &Pred, const SCEV *&LHS, const SCEV *&RHS, unsigned Depth=0)
Simplify LHS and RHS in a comparison with predicate Pred.
Definition:ScalarEvolution.cpp:10760
llvm::ScalarEvolution::getOffsetOfExpr
const SCEV * getOffsetOfExpr(Type *IntTy, StructType *STy, unsigned FieldNo)
Return an expression for offsetof on the given field with type IntTy.
Definition:ScalarEvolution.cpp:4399
llvm::ScalarEvolution::getLoopDisposition
LoopDisposition getLoopDisposition(const SCEV *S, const Loop *L)
Return the "disposition" of the given SCEV with respect to the given loop.
Definition:ScalarEvolution.cpp:14010
llvm::ScalarEvolution::containsAddRecurrence
bool containsAddRecurrence(const SCEV *S)
Return true if the SCEV is a scAddRecExpr or it contains scAddRecExpr.
Definition:ScalarEvolution.cpp:4500
llvm::ScalarEvolution::getSignExtendExprImpl
const SCEV * getSignExtendExprImpl(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1919
llvm::ScalarEvolution::getAddRecExpr
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
Definition:ScalarEvolution.cpp:3641
llvm::ScalarEvolution::hasOperand
bool hasOperand(const SCEV *S, const SCEV *Op) const
Test whether the given SCEV has Op as a direct or indirect operand.
Definition:ScalarEvolution.cpp:14191
llvm::ScalarEvolution::getUDivExpr
const SCEV * getUDivExpr(const SCEV *LHS, const SCEV *RHS)
Get a canonical unsigned division expression, or something simpler if possible.
Definition:ScalarEvolution.cpp:3400
llvm::ScalarEvolution::getZeroExtendExpr
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1565
llvm::ScalarEvolution::isSCEVable
bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
Definition:ScalarEvolution.cpp:4441
llvm::ScalarEvolution::getEffectiveSCEVType
Type * getEffectiveSCEVType(Type *Ty) const
Return a type with the same bitwidth as the given type and which represents how SCEV will treat the g...
Definition:ScalarEvolution.cpp:4458
llvm::ScalarEvolution::getComparePredicate
const SCEVPredicate * getComparePredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS)
Definition:ScalarEvolution.cpp:14736
llvm::ScalarEvolution::getNotSCEV
const SCEV * getNotSCEV(const SCEV *V)
Return the SCEV object corresponding to ~V.
Definition:ScalarEvolution.cpp:4596
llvm::ScalarEvolution::getLoopInvariantPredicate
std::optional< LoopInvariantPredicate > getLoopInvariantPredicate(ICmpInst::Predicate Pred, const SCEV *LHS, const SCEV *RHS, const Loop *L, const Instruction *CtxI=nullptr)
If the result of the predicate LHS Pred RHS is loop invariant with respect to L, return a LoopInvaria...
Definition:ScalarEvolution.cpp:11170
llvm::ScalarEvolution::instructionCouldExistWithOperands
bool instructionCouldExistWithOperands(const SCEV *A, const SCEV *B)
Return true if there exists a point in the program at which both A and B could be operands to the sam...
Definition:ScalarEvolution.cpp:4473
llvm::ScalarEvolution::getUnsignedRange
ConstantRange getUnsignedRange(const SCEV *S)
Determine the unsigned range for a particular SCEV.
Definition:ScalarEvolution.h:997
llvm::ScalarEvolution::getMinTrailingZeros
uint32_t getMinTrailingZeros(const SCEV *S)
Determine the minimum number of zero bits that S is guaranteed to end in (at every loop iteration).
Definition:ScalarEvolution.cpp:6411
llvm::ScalarEvolution::print
void print(raw_ostream &OS) const
Definition:ScalarEvolution.cpp:13919
llvm::ScalarEvolution::getUMinExpr
const SCEV * getUMinExpr(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
Definition:ScalarEvolution.cpp:4371
llvm::ScalarEvolution::getPredicatedExitCount
const SCEV * getPredicatedExitCount(const Loop *L, const BasicBlock *ExitingBlock, SmallVectorImpl< const SCEVPredicate * > *Predicates, ExitCountKind Kind=Exact)
Same as above except this uses the predicated backedge taken info and may require predicates.
Definition:ScalarEvolution.cpp:8328
llvm::ScalarEvolution::clearFlags
static SCEV::NoWrapFlags clearFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OffFlags)
Definition:ScalarEvolution.h:476
llvm::ScalarEvolution::forgetTopmostLoop
void forgetTopmostLoop(const Loop *L)
Definition:ScalarEvolution.cpp:8538
llvm::ScalarEvolution::forgetValue
void forgetValue(Value *V)
This method should be called by the client when it has changed a value in a way that may effect its v...
Definition:ScalarEvolution.cpp:8542
llvm::ScalarEvolution::getSignedRangeMin
APInt getSignedRangeMin(const SCEV *S)
Determine the min of the signed range for a particular SCEV.
Definition:ScalarEvolution.h:1018
llvm::ScalarEvolution::getNoopOrAnyExtend
const SCEV * getNoopOrAnyExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4754
llvm::ScalarEvolution::forgetBlockAndLoopDispositions
void forgetBlockAndLoopDispositions(Value *V=nullptr)
Called when the client has changed the disposition of values in a loop or block.
Definition:ScalarEvolution.cpp:8597
llvm::ScalarEvolution::getTruncateExpr
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1150
llvm::ScalarEvolution::MonotonicallyDecreasing
@ MonotonicallyDecreasing
Definition:ScalarEvolution.h:1178
llvm::ScalarEvolution::MonotonicallyIncreasing
@ MonotonicallyIncreasing
Definition:ScalarEvolution.h:1177
llvm::ScalarEvolution::getStoreSizeOfExpr
const SCEV * getStoreSizeOfExpr(Type *IntTy, Type *StoreTy)
Return an expression for the store size of StoreTy that is type IntTy.
Definition:ScalarEvolution.cpp:4395
llvm::ScalarEvolution::getWrapPredicate
const SCEVPredicate * getWrapPredicate(const SCEVAddRecExpr *AR, SCEVWrapPredicate::IncrementWrapFlags AddedFlags)
Definition:ScalarEvolution.cpp:14755
llvm::ScalarEvolution::isLoopBackedgeGuardedByCond
bool isLoopBackedgeGuardedByCond(const Loop *L, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether the backedge of the loop is protected by a conditional between LHS and RHS.
Definition:ScalarEvolution.cpp:11516
llvm::ScalarEvolution::getMinusSCEV
const SCEV * getMinusSCEV(const SCEV *LHS, const SCEV *RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
Definition:ScalarEvolution.cpp:4655
llvm::ScalarEvolution::getNonZeroConstantMultiple
APInt getNonZeroConstantMultiple(const SCEV *S)
Definition:ScalarEvolution.cpp:6406
llvm::ScalarEvolution::getMinusOne
const SCEV * getMinusOne(Type *Ty)
Return a SCEV for the constant -1 of a specific type.
Definition:ScalarEvolution.h:665
llvm::ScalarEvolution::setFlags
static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags)
Definition:ScalarEvolution.h:471
llvm::ScalarEvolution::hasLoopInvariantBackedgeTakenCount
bool hasLoopInvariantBackedgeTakenCount(const Loop *L)
Return true if the specified loop has an analyzable loop-invariant backedge-taken count.
Definition:ScalarEvolution.cpp:13712
llvm::ScalarEvolution::getBlockDisposition
BlockDisposition getBlockDisposition(const SCEV *S, const BasicBlock *BB)
Return the "disposition" of the given SCEV with respect to the given block.
Definition:ScalarEvolution.cpp:14109
llvm::ScalarEvolution::getNoopOrZeroExtend
const SCEV * getNoopOrZeroExtend(const SCEV *V, Type *Ty)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4730
llvm::ScalarEvolution::invalidate
bool invalidate(Function &F, const PreservedAnalyses &PA, FunctionAnalysisManager::Invalidator &Inv)
Definition:ScalarEvolution.cpp:14645
llvm::ScalarEvolution::SCEVUnknown
friend class SCEVUnknown
Definition:ScalarEvolution.h:1413
llvm::ScalarEvolution::getUMinFromMismatchedTypes
const SCEV * getUMinFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
Promote the operands to the wider of the types using zero-extension, and then perform a umin operatio...
Definition:ScalarEvolution.cpp:4790
llvm::ScalarEvolution::loopIsFiniteByAssumption
bool loopIsFiniteByAssumption(const Loop *L)
Return true if this loop is finite by assumption.
Definition:ScalarEvolution.cpp:7467
llvm::ScalarEvolution::getExistingSCEV
const SCEV * getExistingSCEV(Value *V)
Return an existing SCEV for V if there is one, otherwise return nullptr.
Definition:ScalarEvolution.cpp:4555
llvm::ScalarEvolution::LoopDisposition
LoopDisposition
An enum describing the relationship between a SCEV and a loop.
Definition:ScalarEvolution.h:452
llvm::ScalarEvolution::LoopComputable
@ LoopComputable
The SCEV varies predictably with the loop.
Definition:ScalarEvolution.h:455
llvm::ScalarEvolution::LoopVariant
@ LoopVariant
The SCEV is loop-variant (unknown).
Definition:ScalarEvolution.h:453
llvm::ScalarEvolution::LoopInvariant
@ LoopInvariant
The SCEV is loop-invariant.
Definition:ScalarEvolution.h:454
llvm::ScalarEvolution::SCEVCallbackVH
friend class SCEVCallbackVH
Definition:ScalarEvolution.h:1411
llvm::ScalarEvolution::getAnyExtendExpr
const SCEV * getAnyExtendExpr(const SCEV *Op, Type *Ty)
getAnyExtendExpr - Return a SCEV for the given operand extended with unspecified bits out to the give...
Definition:ScalarEvolution.cpp:2180
llvm::ScalarEvolution::isKnownToBeAPowerOfTwo
bool isKnownToBeAPowerOfTwo(const SCEV *S, bool OrZero=false, bool OrNegative=false)
Test if the given expression is known to be a power of 2.
Definition:ScalarEvolution.cpp:10968
llvm::ScalarEvolution::getStrengthenedNoWrapFlagsFromBinOp
std::optional< SCEV::NoWrapFlags > getStrengthenedNoWrapFlagsFromBinOp(const OverflowingBinaryOperator *OBO)
Parse NSW/NUW flags from add/sub/mul IR binary operation Op into SCEV no-wrap flags,...
Definition:ScalarEvolution.cpp:2391
llvm::ScalarEvolution::forgetLcssaPhiWithNewPredecessor
void forgetLcssaPhiWithNewPredecessor(Loop *L, PHINode *V)
Forget LCSSA phi node V of loop L to which a new predecessor was added, such that it may no longer be...
Definition:ScalarEvolution.cpp:8557
llvm::ScalarEvolution::containsUndefs
bool containsUndefs(const SCEV *S) const
Return true if the SCEV expression contains an undef value.
Definition:ScalarEvolution.cpp:13577
llvm::ScalarEvolution::getMonotonicPredicateType
std::optional< MonotonicPredicateType > getMonotonicPredicateType(const SCEVAddRecExpr *LHS, ICmpInst::Predicate Pred)
If, for all loop invariant X, the predicate "LHS `Pred` X" is monotonically increasing or decreasing,...
Definition:ScalarEvolution.cpp:11107
llvm::ScalarEvolution::getCouldNotCompute
const SCEV * getCouldNotCompute()
Definition:ScalarEvolution.cpp:4487
llvm::ScalarEvolution::isAvailableAtLoopEntry
bool isAvailableAtLoopEntry(const SCEV *S, const Loop *L)
Determine if the SCEV can be evaluated at loop's entry.
Definition:ScalarEvolution.cpp:2521
llvm::ScalarEvolution::BlockDisposition
BlockDisposition
An enum describing the relationship between a SCEV and a basic block.
Definition:ScalarEvolution.h:459
llvm::ScalarEvolution::DominatesBlock
@ DominatesBlock
The SCEV dominates the block.
Definition:ScalarEvolution.h:461
llvm::ScalarEvolution::ProperlyDominatesBlock
@ ProperlyDominatesBlock
The SCEV properly dominates the block.
Definition:ScalarEvolution.h:462
llvm::ScalarEvolution::DoesNotDominateBlock
@ DoesNotDominateBlock
The SCEV does not dominate the block.
Definition:ScalarEvolution.h:460
llvm::ScalarEvolution::getExitCount
const SCEV * getExitCount(const Loop *L, const BasicBlock *ExitingBlock, ExitCountKind Kind=Exact)
Return the number of times the backedge executes before the given exit would be taken; if not exactly...
Definition:ScalarEvolution.cpp:8314
llvm::ScalarEvolution::getSignExtendExpr
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
Definition:ScalarEvolution.cpp:1900
llvm::ScalarEvolution::getPoisonGeneratingValues
void getPoisonGeneratingValues(SmallPtrSetImpl< const Value * > &Result, const SCEV *S)
Return the set of Values that, if poison, will definitively result in S being poison as well.
Definition:ScalarEvolution.cpp:4160
llvm::ScalarEvolution::forgetLoopDispositions
void forgetLoopDispositions()
Called when the client has changed the disposition of values in this loop.
Definition:ScalarEvolution.cpp:8595
llvm::ScalarEvolution::getVScale
const SCEV * getVScale(Type *Ty)
Definition:ScalarEvolution.cpp:494
llvm::ScalarEvolution::getSmallConstantTripCount
unsigned getSmallConstantTripCount(const Loop *L)
Returns the exact trip count of the loop if we can compute it, and the result is a small constant.
Definition:ScalarEvolution.cpp:8237
llvm::ScalarEvolution::hasComputableLoopEvolution
bool hasComputableLoopEvolution(const SCEV *S, const Loop *L)
Return true if the given SCEV changes value in a known way in the specified loop.
Definition:ScalarEvolution.cpp:14104
llvm::ScalarEvolution::getPointerBase
const SCEV * getPointerBase(const SCEV *V)
Transitively follow the chain of pointer-type operands until reaching a SCEV that does not have a sin...
Definition:ScalarEvolution.cpp:4823
llvm::ScalarEvolution::getMinMaxExpr
const SCEV * getMinMaxExpr(SCEVTypes Kind, SmallVectorImpl< const SCEV * > &Operands)
Definition:ScalarEvolution.cpp:3828
llvm::ScalarEvolution::forgetAllLoops
void forgetAllLoops()
Definition:ScalarEvolution.cpp:8449
llvm::ScalarEvolution::dominates
bool dominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV dominate the specified basic block.
Definition:ScalarEvolution.cpp:14183
llvm::ScalarEvolution::getUnsignedRangeMax
APInt getUnsignedRangeMax(const SCEV *S)
Determine the max of the unsigned range for a particular SCEV.
Definition:ScalarEvolution.h:1007
llvm::ScalarEvolution::ExitCountKind
ExitCountKind
The terms "backedge taken count" and "exit count" are used interchangeably to refer to the number of ...
Definition:ScalarEvolution.h:858
llvm::ScalarEvolution::SymbolicMaximum
@ SymbolicMaximum
An expression which provides an upper bound on the exact trip count.
Definition:ScalarEvolution.h:865
llvm::ScalarEvolution::ConstantMaximum
@ ConstantMaximum
A constant which provides an upper bound on the exact trip count.
Definition:ScalarEvolution.h:863
llvm::ScalarEvolution::Exact
@ Exact
An expression exactly describing the number of times the backedge has executed when a loop is exited.
Definition:ScalarEvolution.h:861
llvm::ScalarEvolution::applyLoopGuards
const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
Definition:ScalarEvolution.cpp:15967
llvm::ScalarEvolution::getMulExpr
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
Definition:ScalarEvolution.cpp:3106
llvm::ScalarEvolution::convertSCEVToAddRecWithPredicates
const SCEVAddRecExpr * convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Preds)
Tries to convert the S expression to an AddRec expression, adding additional predicates to Preds as r...
Definition:ScalarEvolution.cpp:14902
llvm::ScalarEvolution::getElementSize
const SCEV * getElementSize(Instruction *Inst)
Return the size of an element read or written by Inst.
Definition:ScalarEvolution.cpp:13595
llvm::ScalarEvolution::getSizeOfExpr
const SCEV * getSizeOfExpr(Type *IntTy, TypeSize Size)
Return an expression for a TypeSize.
Definition:ScalarEvolution.cpp:4384
llvm::ScalarEvolution::evaluatePredicate
std::optional< bool > evaluatePredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Check whether the condition described by Pred, LHS, and RHS is true or false.
Definition:ScalarEvolution.cpp:11065
llvm::ScalarEvolution::getUnknown
const SCEV * getUnknown(Value *V)
Definition:ScalarEvolution.cpp:4411
llvm::ScalarEvolution::createAddRecFromPHIWithCasts
std::optional< std::pair< const SCEV *, SmallVector< const SCEVPredicate *, 3 > > > createAddRecFromPHIWithCasts(const SCEVUnknown *SymbolicPHI)
Checks if SymbolicPHI can be rewritten as an AddRecExpr under some Predicates.
Definition:ScalarEvolution.cpp:5663
llvm::ScalarEvolution::getTruncateOrZeroExtend
const SCEV * getTruncateOrZeroExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4705
llvm::ScalarEvolution::getElementCount
const SCEV * getElementCount(Type *Ty, ElementCount EC)
Definition:ScalarEvolution.cpp:506
llvm::ScalarEvolution::maskFlags
static SCEV::NoWrapFlags maskFlags(SCEV::NoWrapFlags Flags, int Mask)
Convenient NoWrapFlags manipulation that hides enum casts and is visible in the ScalarEvolution name ...
Definition:ScalarEvolution.h:467
llvm::ScalarEvolution::computeConstantDifference
std::optional< APInt > computeConstantDifference(const SCEV *LHS, const SCEV *RHS)
Compute LHS - RHS and returns the result as an APInt if it is a constant, and std::nullopt if it isn'...
Definition:ScalarEvolution.cpp:12059
llvm::ScalarEvolution::properlyDominates
bool properlyDominates(const SCEV *S, const BasicBlock *BB)
Return true if elements that makes up the given SCEV properly dominate the specified basic block.
Definition:ScalarEvolution.cpp:14187
llvm::ScalarEvolution::rewriteUsingPredicate
const SCEV * rewriteUsingPredicate(const SCEV *S, const Loop *L, const SCEVPredicate &A)
Re-writes the SCEV according to the Predicates in A.
Definition:ScalarEvolution.cpp:14897
llvm::ScalarEvolution::SplitIntoInitAndPostInc
std::pair< const SCEV *, const SCEV * > SplitIntoInitAndPostInc(const Loop *L, const SCEV *S)
Splits SCEV expression S into two SCEVs.
Definition:ScalarEvolution.cpp:10989
llvm::ScalarEvolution::canReuseInstruction
bool canReuseInstruction(const SCEV *S, Instruction *I, SmallVectorImpl< Instruction * > &DropPoisonGeneratingInsts)
Check whether it is poison-safe to represent the expression S using the instruction I.
Definition:ScalarEvolution.cpp:4168
llvm::ScalarEvolution::isKnownPredicateAt
bool isKnownPredicateAt(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const Instruction *CtxI)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
Definition:ScalarEvolution.cpp:11075
llvm::ScalarEvolution::getPredicatedSymbolicMaxBackedgeTakenCount
const SCEV * getPredicatedSymbolicMaxBackedgeTakenCount(const Loop *L, SmallVectorImpl< const SCEVPredicate * > &Predicates)
Similar to getSymbolicMaxBackedgeTakenCount, except it will add a set of SCEV predicates to Predicate...
Definition:ScalarEvolution.cpp:8363
llvm::ScalarEvolution::~ScalarEvolution
~ScalarEvolution()
Definition:ScalarEvolution.cpp:13689
llvm::ScalarEvolution::getUDivExactExpr
const SCEV * getUDivExactExpr(const SCEV *LHS, const SCEV *RHS)
Get a canonical unsigned division expression, or something simpler if possible.
Definition:ScalarEvolution.cpp:3587
llvm::ScalarEvolution::registerUser
void registerUser(const SCEV *User, ArrayRef< const SCEV * > Ops)
Notify this ScalarEvolution that User directly uses SCEVs in Ops.
Definition:ScalarEvolution.cpp:15101
llvm::ScalarEvolution::getAddExpr
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
Definition:ScalarEvolution.cpp:2526
llvm::ScalarEvolution::isBasicBlockEntryGuardedByCond
bool isBasicBlockEntryGuardedByCond(const BasicBlock *BB, CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test whether entry to the basic block is protected by a conditional between LHS and RHS.
Definition:ScalarEvolution.cpp:11622
llvm::ScalarEvolution::getTruncateOrSignExtend
const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
Definition:ScalarEvolution.cpp:4717
llvm::ScalarEvolution::containsErasedValue
bool containsErasedValue(const SCEV *S) const
Return true if the SCEV expression contains a Value that has been optimised out and is now a nullptr.
Definition:ScalarEvolution.cpp:13586
llvm::ScalarEvolution::isKnownPredicate
bool isKnownPredicate(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
Definition:ScalarEvolution.cpp:11050
llvm::ScalarEvolution::isKnownViaInduction
bool isKnownViaInduction(CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS)
We'd like to check the predicate on every iteration of the most dominated loop between loops used in ...
Definition:ScalarEvolution.cpp:11000
llvm::ScalarEvolution::getSymbolicMaxBackedgeTakenCount
const SCEV * getSymbolicMaxBackedgeTakenCount(const Loop *L)
When successful, this returns a SCEV that is greater than or equal to (i.e.
Definition:ScalarEvolution.h:922
llvm::ScalarEvolution::getSignedRangeMax
APInt getSignedRangeMax(const SCEV *S)
Determine the max of the signed range for a particular SCEV.
Definition:ScalarEvolution.h:1023
llvm::ScalarEvolution::verify
void verify() const
Definition:ScalarEvolution.cpp:14352
llvm::ScalarEvolution::getContext
LLVMContext & getContext() const
Definition:ScalarEvolution.h:489
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition:Instructions.h:1657
llvm::SmallDenseMap
Definition:DenseMap.h:883
llvm::SmallPtrSetImplBase::size
size_type size() const
Definition:SmallPtrSet.h:94
llvm::SmallPtrSetImplBase::empty
bool empty() const
Definition:SmallPtrSet.h:93
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition:SmallPtrSet.h:363
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition:SmallPtrSet.h:384
llvm::SmallPtrSetImpl::contains
bool contains(ConstPtrType Ptr) const
Definition:SmallPtrSet.h:458
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition:SmallSet.h:132
llvm::SmallSet::insert
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition:SmallSet.h:181
llvm::SmallSet::size
size_type size() const
Definition:SmallSet.h:170
llvm::SmallVectorBase::empty
bool empty() const
Definition:SmallVector.h:81
llvm::SmallVectorBase::size
size_t size() const
Definition:SmallVector.h:78
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition:SmallVector.h:573
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition:SmallVector.h:673
llvm::SmallVectorImpl::emplace_back
reference emplace_back(ArgTypes &&... Args)
Definition:SmallVector.h:937
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition:SmallVector.h:663
llvm::SmallVectorImpl::erase
iterator erase(const_iterator CI)
Definition:SmallVector.h:737
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition:SmallVector.h:683
llvm::SmallVectorImpl::insert
iterator insert(iterator I, T &&Elt)
Definition:SmallVector.h:805
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorTemplateBase::pop_back
void pop_back()
Definition:SmallVector.h:425
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::SmallVectorTemplateCommon::end
iterator end()
Definition:SmallVector.h:269
llvm::SmallVectorTemplateCommon::begin
iterator begin()
Definition:SmallVector.h:267
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StoreInst
An instruction for storing to memory.
Definition:Instructions.h:292
llvm::StructLayout
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition:DataLayout.h:567
llvm::StructLayout::getElementOffset
TypeSize getElementOffset(unsigned Idx) const
Definition:DataLayout.h:596
llvm::StructLayout::getSizeInBits
TypeSize getSizeInBits() const
Definition:DataLayout.h:576
llvm::StructType
Class to represent struct types.
Definition:DerivedTypes.h:218
llvm::SwitchInst
Multiway switch.
Definition:Instructions.h:3154
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition:TargetLibraryInfo.h:614
llvm::TargetLibraryInfoWrapperPass
Definition:TargetLibraryInfo.h:639
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition:TargetLibraryInfo.h:280
llvm::TypeSize
Definition:TypeSize.h:334
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition:Type.h:264
llvm::Type::getInt1Ty
static IntegerType * getInt1Ty(LLVMContext &C)
llvm::Type::getIntNTy
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
llvm::Type::isIntOrPtrTy
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition:Type.h:252
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition:Type.h:237
llvm::Type::getPrimitiveSizeInBits
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition:Use.h:43
llvm::User
Definition:User.h:44
llvm::User::operands
op_range operands()
Definition:User.h:288
llvm::User::Op
Use & Op()
Definition:User.h:192
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition:User.h:228
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition:Value.h:255
llvm::Value::getValueID
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition:Value.h:532
llvm::Value::printAsOperand
void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
Definition:AsmWriter.cpp:5144
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition:Value.cpp:1075
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition:Value.cpp:309
llvm::WithOverflowInst
Represents an op.with.overflow intrinsic.
Definition:IntrinsicInst.h:927
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::details::FixedOrScalableQuantity::isScalable
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition:TypeSize.h:171
llvm::ilist_detail::node_parent_access::getParent
const ParentTy * getParent() const
Definition:ilist_node.h:32
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::raw_ostream::indent
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
Definition:raw_ostream.cpp:495
uint32_t
uint64_t
unsigned
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
false
Definition:StackSlotColoring.cpp:193
llvm::AArch64CC::LS
@ LS
Definition:AArch64BaseInfo.h:264
llvm::AMDGPU::P1
@ P1
Definition:AMDGPURegBankLegalizeRules.h:53
llvm::APIntOps::smin
const APInt & smin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be signed.
Definition:APInt.h:2217
llvm::APIntOps::smax
const APInt & smax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be signed.
Definition:APInt.h:2222
llvm::APIntOps::umin
const APInt & umin(const APInt &A, const APInt &B)
Determine the smaller of two APInts considered to be unsigned.
Definition:APInt.h:2227
llvm::APIntOps::SolveQuadraticEquationWrap
std::optional< APInt > SolveQuadraticEquationWrap(APInt A, APInt B, APInt C, unsigned RangeWidth)
Let q(n) = An^2 + Bn + C, and BW = bit width of the value range (e.g.
Definition:APInt.cpp:2785
llvm::APIntOps::umax
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition:APInt.h:2232
llvm::APIntOps::GreatestCommonDivisor
APInt GreatestCommonDivisor(APInt A, APInt B)
Compute GCD of two unsigned APInt values.
Definition:APInt.cpp:771
llvm::ARCCC::Z
@ Z
Definition:ARCInfo.h:41
llvm::ARM::ProfileKind::M
@ M
llvm::COFF::Entry
@ Entry
Definition:COFF.h:844
llvm::COFF::Exit
@ Exit
Definition:COFF.h:845
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition:CallingConv.h:24
llvm::Intrinsic::getDeclarationIfExists
Function * getDeclarationIfExists(Module *M, ID id, ArrayRef< Type * > Tys, FunctionType *FT=nullptr)
This version supports overloaded intrinsics.
Definition:Intrinsics.cpp:747
llvm::M68kBeads::Term
@ Term
Definition:M68kBaseInfo.h:116
llvm::M68k::MemAddrModeKind::U
@ U
llvm::M68k::MemAddrModeKind::V
@ V
llvm::M68k::MemAddrModeKind::L
@ L
llvm::MipsISD::Ret
@ Ret
Definition:MipsISelLowering.h:117
llvm::MipsISD::Ext
@ Ext
Definition:MipsISelLowering.h:157
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition:NVPTX.h:163
llvm::PatternMatch
Definition:PatternMatch.h:47
llvm::PatternMatch::m_AShr
BinaryOp_match< LHS, RHS, Instruction::AShr > m_AShr(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1246
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition:PatternMatch.h:49
llvm::PatternMatch::m_Specific
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition:PatternMatch.h:885
llvm::PatternMatch::m_ConstantInt
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition:PatternMatch.h:168
llvm::PatternMatch::m_Select
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
Definition:PatternMatch.h:1799
llvm::PatternMatch::m_WithOverflowInst
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition:PatternMatch.h:832
llvm::PatternMatch::m_LogicalOr
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
Definition:PatternMatch.h:3099
llvm::PatternMatch::m_Br
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Definition:PatternMatch.h:2220
llvm::PatternMatch::m_SDiv
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1186
llvm::PatternMatch::m_APInt
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition:PatternMatch.h:299
llvm::PatternMatch::m_Value
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition:PatternMatch.h:92
llvm::PatternMatch::m_LShr
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1240
llvm::PatternMatch::m_Shl
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1234
llvm::PatternMatch::m_LogicalAnd
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Definition:PatternMatch.h:3081
llvm::PatternMatch::m_BasicBlock
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition:PatternMatch.h:189
llvm::PatternMatch::m_CombineOr
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition:PatternMatch.h:239
llvm::RISCVFenceField::R
@ R
Definition:RISCVBaseInfo.h:373
llvm::RISCVFenceField::O
@ O
Definition:RISCVBaseInfo.h:372
llvm::SCEVPatternMatch::m_scev_AllOnes
cst_pred_ty< is_all_ones > m_scev_AllOnes()
Match an integer with all bits set.
Definition:ScalarEvolutionPatternMatch.h:51
llvm::SCEVPatternMatch::m_scev_ZExt
SCEVUnaryExpr_match< SCEVZeroExtendExpr, Op0_t > m_scev_ZExt(const Op0_t &Op0)
Definition:ScalarEvolutionPatternMatch.h:119
llvm::SCEVPatternMatch::m_scev_One
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
Definition:ScalarEvolutionPatternMatch.h:45
llvm::SCEVPatternMatch::m_scev_SExt
SCEVUnaryExpr_match< SCEVSignExtendExpr, Op0_t > m_scev_SExt(const Op0_t &Op0)
Definition:ScalarEvolutionPatternMatch.h:113
llvm::SCEVPatternMatch::m_scev_Zero
cst_pred_ty< is_zero > m_scev_Zero()
Match an integer 0.
Definition:ScalarEvolutionPatternMatch.h:39
llvm::SCEVPatternMatch::m_SCEVConstant
bind_ty< const SCEVConstant > m_SCEVConstant(const SCEVConstant *&V)
Definition:ScalarEvolutionPatternMatch.h:75
llvm::SCEVPatternMatch::m_SCEV
bind_ty< const SCEV > m_SCEV(const SCEV *&V)
Match a SCEV, capturing it if we match.
Definition:ScalarEvolutionPatternMatch.h:74
llvm::SCEVPatternMatch::m_scev_Add
SCEVBinaryExpr_match< SCEVAddExpr, Op0_t, Op1_t > m_scev_Add(const Op0_t &Op0, const Op1_t &Op1)
Definition:ScalarEvolutionPatternMatch.h:146
llvm::SCEVPatternMatch::match
bool match(const SCEV *S, const Pattern &P)
Definition:ScalarEvolutionPatternMatch.h:22
llvm::SCEVPatternMatch::m_SCEVUnknown
bind_ty< const SCEVUnknown > m_SCEVUnknown(const SCEVUnknown *&V)
Definition:ScalarEvolutionPatternMatch.h:78
llvm::SIEncodingFamily::SI
@ SI
Definition:SIDefines.h:36
llvm::X86ISD::SBB
@ SBB
Definition:X86ISelLowering.h:409
llvm::X86::FirstMacroFusionInstKind::Cmp
@ Cmp
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::ReallyHidden
@ ReallyHidden
Definition:CommandLine.h:138
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm::cl::location
LocationClass< Ty > location(Ty &L)
Definition:CommandLine.h:463
llvm::codeview::FrameCookieKind::Copy
@ Copy
llvm::coro::ABI::Switch
@ Switch
The "resume-switch" lowering, where there are separate resume and destroy functions that are shared b...
llvm::dxil::OpCode
OpCode
Definition:DXILConstants.h:18
llvm::logicalview::LVAttributeKind::Inserted
@ Inserted
llvm::logicalview::LVAttributeKind::Zero
@ Zero
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::numbers::e
constexpr double e
Definition:MathExtras.h:47
llvm::omp::RTLDependInfoFields::Flags
@ Flags
llvm::pdb::PDB_MemoryType::Stack
@ Stack
llvm::rdf::Phi
NodeAddr< PhiNode * > Phi
Definition:RDFGraph.h:390
llvm::sampleprof::Base
@ Base
Definition:Discriminator.h:58
llvm::tgtok::TrueVal
@ TrueVal
Definition:TGLexer.h:58
llvm::tgtok::In
@ In
Definition:TGLexer.h:84
llvm::tgtok::FalseVal
@ FalseVal
Definition:TGLexer.h:59
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::visitAll
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
Definition:ScalarEvolutionExpressions.h:713
llvm::drop_begin
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition:STLExtras.h:329
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::gcd
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt gcd(const DynamicAPInt &A, const DynamicAPInt &B)
Definition:DynamicAPInt.h:390
llvm::stable_sort
void stable_sort(R &&Range)
Definition:STLExtras.h:2037
llvm::all_of
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1739
llvm::PseudoProbeType::Block
@ Block
llvm::canCreatePoison
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
Definition:ValueTracking.cpp:7636
llvm::mustTriggerUB
bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
Definition:ValueTracking.cpp:8151
llvm::isUIntN
bool isUIntN(unsigned N, uint64_t x)
Checks if an unsigned integer fits into the given (dynamic) bit width.
Definition:MathExtras.h:256
llvm::make_scope_exit
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition:ScopeExit.h:59
llvm::Depth
@ Depth
Definition:SIMachineScheduler.h:36
llvm::LoopIdiomVectorizeStyle::Predicated
@ Predicated
llvm::canConstantFoldCallTo
bool canConstantFoldCallTo(const CallBase *Call, const Function *F)
canConstantFoldCallTo - Return true if its even possible to fold a call to the specified function.
Definition:ConstantFolding.cpp:1565
llvm::verifyFunction
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
Definition:Verifier.cpp:7301
llvm::successors
auto successors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1376
llvm::PointerTy
void * PointerTy
Definition:GenericValue.h:21
llvm::set_is_subset
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
Definition:SetOperations.h:151
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition:STLExtras.h:2115
llvm::ConstantFoldCompareInstOperands
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
Definition:ConstantFolding.cpp:1186
llvm::computeExpressionSize
unsigned short computeExpressionSize(ArrayRef< const SCEV * > Args)
Definition:ScalarEvolutionExpressions.h:95
llvm::VerifySCEV
bool VerifySCEV
Definition:ScalarEvolution.cpp:151
llvm::print
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr)
Definition:GCNRegPressure.cpp:227
llvm::getConstantRangeFromMetadata
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
Definition:ConstantRange.cpp:2264
llvm::countr_zero
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition:bit.h:215
llvm::simplifyInstruction
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q)
See if we can compute a simplified version of this instruction.
Definition:InstructionSimplify.cpp:7234
llvm::isOverflowIntrinsicNoWrap
bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
Definition:ValueTracking.cpp:7387
llvm::matchSimpleRecurrence
bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
Definition:ValueTracking.cpp:9207
llvm::erase
void erase(Container &C, ValueType V)
Wrapper function to remove a value from a container:
Definition:STLExtras.h:2107
llvm::any_of
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1746
llvm::initializeScalarEvolutionWrapperPassPass
void initializeScalarEvolutionWrapperPassPass(PassRegistry &)
llvm::HexPrintStyle::Upper
@ Upper
llvm::HexPrintStyle::Lower
@ Lower
llvm::reverse
auto reverse(ContainerTy &&C)
Definition:STLExtras.h:420
llvm::isMustProgress
bool isMustProgress(const Loop *L)
Return true if this loop can be assumed to make progress.
Definition:LoopInfo.cpp:1162
llvm::impliesPoison
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
Definition:ValueTracking.cpp:7690
llvm::isFinite
bool isFinite(const Loop *L)
Return true if this loop can be assumed to run for a finite number of iterations.
Definition:LoopInfo.cpp:1152
llvm::programUndefinedIfPoison
bool programUndefinedIfPoison(const Instruction *Inst)
Definition:ValueTracking.cpp:8259
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::isPointerTy
bool isPointerTy(const Type *T)
Definition:SPIRVUtils.h:256
llvm::getVScaleRange
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
Definition:ValueTracking.cpp:1058
llvm::ConstantFoldInstOperands
Constant * ConstantFoldInstOperands(Instruction *I, ArrayRef< Constant * > Ops, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, bool AllowNonDeterministic=true)
ConstantFoldInstOperands - Attempt to constant fold an instruction with the specified operands.
Definition:ConstantFolding.cpp:1177
llvm::CodeGenOptLevel::Less
@ Less
-O1
llvm::isKnownNonZero
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
Definition:ValueTracking.cpp:3487
llvm::IRMemLocation::First
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
llvm::propagatesPoison
bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
Definition:ValueTracking.cpp:7997
llvm::RecurKind::UMin
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
llvm::RecurKind::Mul
@ Mul
Product of integers.
llvm::RecurKind::SMax
@ SMax
Signed integer max implemented in terms of select(cmp()).
llvm::RecurKind::SMin
@ SMin
Signed integer min implemented in terms of select(cmp()).
llvm::RecurKind::Add
@ Add
Sum of integers.
llvm::RecurKind::UMax
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
llvm::isIntN
bool isIntN(unsigned N, int64_t x)
Checks if an signed integer fits into the given (dynamic) bit width.
Definition:MathExtras.h:261
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition:STLExtras.h:1938
llvm::computeKnownBits
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
Definition:ValueTracking.cpp:164
llvm::Op
DWARFExpression::Operation Op
Definition:DWARFExpression.cpp:22
llvm::max_element
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
Definition:STLExtras.h:2014
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition:APFixedPoint.h:303
llvm::BitWidth
constexpr unsigned BitWidth
Definition:BitmaskEnum.h:217
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1873
llvm::isGuaranteedToTransferExecutionToSuccessor
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
Definition:ValueTracking.cpp:7920
llvm::PseudoProbeReservedId::Last
@ Last
llvm::count_if
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition:STLExtras.h:1945
llvm::predecessors
auto predecessors(const MachineBasicBlock *BB)
Definition:MachineBasicBlock.h:1377
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition:STLExtras.h:1903
llvm::SCEVTypes
SCEVTypes
Definition:ScalarEvolutionExpressions.h:37
llvm::scUMinExpr
@ scUMinExpr
Definition:ScalarEvolutionExpressions.h:51
llvm::scAddRecExpr
@ scAddRecExpr
Definition:ScalarEvolutionExpressions.h:48
llvm::scSequentialUMinExpr
@ scSequentialUMinExpr
Definition:ScalarEvolutionExpressions.h:53
llvm::scAddExpr
@ scAddExpr
Definition:ScalarEvolutionExpressions.h:45
llvm::scVScale
@ scVScale
Definition:ScalarEvolutionExpressions.h:41
llvm::scSMaxExpr
@ scSMaxExpr
Definition:ScalarEvolutionExpressions.h:50
llvm::scUnknown
@ scUnknown
Definition:ScalarEvolutionExpressions.h:55
llvm::scSMinExpr
@ scSMinExpr
Definition:ScalarEvolutionExpressions.h:52
llvm::scCouldNotCompute
@ scCouldNotCompute
Definition:ScalarEvolutionExpressions.h:56
llvm::scConstant
@ scConstant
Definition:ScalarEvolutionExpressions.h:40
llvm::scSignExtend
@ scSignExtend
Definition:ScalarEvolutionExpressions.h:44
llvm::scTruncate
@ scTruncate
Definition:ScalarEvolutionExpressions.h:42
llvm::scUMaxExpr
@ scUMaxExpr
Definition:ScalarEvolutionExpressions.h:49
llvm::scZeroExtend
@ scZeroExtend
Definition:ScalarEvolutionExpressions.h:43
llvm::scPtrToInt
@ scPtrToInt
Definition:ScalarEvolutionExpressions.h:54
llvm::scUDivExpr
@ scUDivExpr
Definition:ScalarEvolutionExpressions.h:47
llvm::scMulExpr
@ scMulExpr
Definition:ScalarEvolutionExpressions.h:46
llvm::ComputeNumSignBits
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
Definition:ValueTracking.cpp:351
llvm::depth_first
iterator_range< df_iterator< T > > depth_first(const T &G)
Definition:DepthFirstIterator.h:233
llvm::seq
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition:Sequence.h:305
llvm::isGuaranteedNotToBePoison
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
Definition:ValueTracking.cpp:7849
llvm::SCEVExprContains
bool SCEVExprContains(const SCEV *Root, PredTy Pred)
Return true if any node in Root satisfies the predicate Pred.
Definition:ScalarEvolutionExpressions.h:720
std
Implement std::hash so that hash_code can be used in STL containers.
Definition:BitVector.h:858
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition:BitVector.h:860
raw_ostream.h
N
#define N
NC
#define NC
Definition:regutils.h:42
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition:Alignment.h:39
llvm::AnalysisKey
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition:Analysis.h:28
llvm::Incoming
Incoming for lane maks phi as machine instruction, incoming register Reg and incoming block Block are...
Definition:SILowerI1Copies.h:25
llvm::Inverse
Definition:GraphTraits.h:123
llvm::KnownBits
Definition:KnownBits.h:23
llvm::KnownBits::makeConstant
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition:KnownBits.h:293
llvm::KnownBits::isNonNegative
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition:KnownBits.h:100
llvm::KnownBits::ashr
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition:KnownBits.cpp:428
llvm::KnownBits::getBitWidth
unsigned getBitWidth() const
Get the bit width of this value.
Definition:KnownBits.h:43
llvm::KnownBits::lshr
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition:KnownBits.cpp:370
llvm::KnownBits::zextOrTrunc
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition:KnownBits.h:188
llvm::KnownBits::getMaxValue
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition:KnownBits.h:137
llvm::KnownBits::getMinValue
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition:KnownBits.h:121
llvm::KnownBits::isNegative
bool isNegative() const
Returns true if this value is known to be negative.
Definition:KnownBits.h:97
llvm::KnownBits::One
APInt One
Definition:KnownBits.h:25
llvm::KnownBits::Zero
APInt Zero
Definition:KnownBits.h:24
llvm::KnownBits::shl
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition:KnownBits.cpp:285
llvm::MinMax
Definition:AssumeBundleQueries.h:70
llvm::SCEVCouldNotCompute
An object of this class is returned by queries that could not be answered.
Definition:ScalarEvolution.h:205
llvm::SCEVCouldNotCompute::SCEVCouldNotCompute
SCEVCouldNotCompute()
Definition:ScalarEvolution.cpp:466
llvm::SCEVCouldNotCompute::classof
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition:ScalarEvolution.cpp:469
llvm::SCEVVisitor
This class defines a simple visitor class that may be used for various SCEV analysis purposes.
Definition:ScalarEvolutionExpressions.h:608
llvm::SaveAndRestore
A utility class that uses RAII to save and restore the value of a variable.
Definition:SaveAndRestore.h:23
llvm::ScalarEvolution::ExitLimit
Information about the number of loop iterations for which a loop exit's branch condition evaluates to...
Definition:ScalarEvolution.h:1117
llvm::ScalarEvolution::ExitLimit::ExitLimit
ExitLimit(const SCEV *E)
Construct either an exact exit limit from a constant, or an unknown one from a SCEVCouldNotCompute.
Definition:ScalarEvolution.cpp:8754
llvm::ScalarEvolution::ExitLimit::ExactNotTaken
const SCEV * ExactNotTaken
Definition:ScalarEvolution.h:1118
llvm::ScalarEvolution::ExitLimit::SymbolicMaxNotTaken
const SCEV * SymbolicMaxNotTaken
Definition:ScalarEvolution.h:1121
llvm::ScalarEvolution::ExitLimit::Predicates
SmallVector< const SCEVPredicate *, 4 > Predicates
A vector of predicate guards for this ExitLimit.
Definition:ScalarEvolution.h:1129
llvm::ScalarEvolution::ExitLimit::ConstantMaxNotTaken
const SCEV * ConstantMaxNotTaken
Definition:ScalarEvolution.h:1119
llvm::ScalarEvolution::LoopInvariantPredicate
Definition:ScalarEvolution.h:1190
llvm::cl::desc
Definition:CommandLine.h:409

Generated on Thu Jul 17 2025 11:05:43 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp