1//===- llvm/Analysis/ScalarEvolutionExpressions.h - SCEV Exprs --*- C++ -*-===// 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 7//===----------------------------------------------------------------------===// 9// This file defines the classes used to represent and build scalar expressions. 11//===----------------------------------------------------------------------===// 13#ifndef LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H 14#define LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H 38// These should be ordered in terms of increasing complexity to make the 59/// This class represents a constant integer value. 74 /// Methods for support type inquiry through isa, cast, and dyn_cast: 78/// This class represents the value of vscale, as used when defining the length 79/// of a scalable vector or returned by the llvm.vscale() intrinsic. 91 /// Methods for support type inquiry through isa, cast, and dyn_cast: 97for (
constauto *Arg : Args)
99return (
unsignedshort)
Size.getZExtValue();
102/// This is the base class for unary cast operator classes. 114assert(i == 0 &&
"Operand index out of range!");
121 /// Methods for support type inquiry through isa, cast, and dyn_cast: 128/// This class represents a cast from a pointer to a pointer-sized integer 136 /// Methods for support type inquiry through isa, cast, and dyn_cast: 140/// This is the base class for unary integral cast operator classes. 147 /// Methods for support type inquiry through isa, cast, and dyn_cast: 154/// This class represents a truncation of an integer value to a 155/// smaller integer value. 162 /// Methods for support type inquiry through isa, cast, and dyn_cast: 166/// This class represents a zero extension of a small integer value 167/// to a larger integer value. 174 /// Methods for support type inquiry through isa, cast, and dyn_cast: 180/// This class represents a sign extension of a small integer value 181/// to a larger integer value. 188 /// Methods for support type inquiry through isa, cast, and dyn_cast: 194/// This node is a base class providing common functionality for 198// Since SCEVs are immutable, ScalarEvolution allocates operand 199// arrays with its SCEVAllocator, so this class just needs a simple 200// pointer rather than a more elaborate vector-like data structure. 201// This also avoids the need for a non-trivial destructor. 206constSCEV *
const *O,
size_tN)
236 /// Methods for support type inquiry through isa, cast, and dyn_cast: 246/// This node is the base class for n'ary commutative operators. 250constSCEV *
const *O,
size_tN)
254 /// Methods for support type inquiry through isa, cast, and dyn_cast: 261 /// Set flags for a non-recurrence without clearing previously set flags. 265/// This node represents an addition of some number of SCEVs. 276if (FirstPointerTypedOp !=
operands().end())
277 Ty = (*FirstPointerTypedOp)->getType();
285 /// Methods for support type inquiry through isa, cast, and dyn_cast: 289/// This node represents multiplication of some number of SCEVs. 299 /// Methods for support type inquiry through isa, cast, and dyn_cast: 303/// This class represents a binary unsigned division operation. 307 std::array<const SCEV *, 2>
Operands;
320assert((i == 0 || i == 1) &&
"Operand index out of range!");
327// In most cases the types of LHS and RHS will be the same, but in some 328// crazy cases one or the other may be a pointer. ScalarEvolution doesn't 329// depend on the type for correctness, but handling types carefully can 330// avoid extra casts in the SCEVExpander. The LHS is more likely to be 331// a pointer type than the RHS, so use the RHS' type here. 335 /// Methods for support type inquiry through isa, cast, and dyn_cast: 339/// This node represents a polynomial recurrence on the trip count 340/// of the specified loop. This is the primary focus of the 341/// ScalarEvolution framework; all the other SCEV subclasses are 342/// mostly just supporting infrastructure to allow SCEVAddRecExpr 343/// expressions to be created and analyzed. 345/// All operands of an AddRec are required to be loop invariant. 361 /// Constructs and returns the recurrence indicating how much this 362 /// expression steps by. If this is a polynomial of degree N, it 363 /// returns a chrec of degree N-1. We cannot determine whether 364 /// the step recurrence has self-wraparound. 373 /// Return true if this represents an expression A + B*x where A 374 /// and B are loop invariant values. 376// We know that the start value is invariant. This expression is thus 377// affine iff the step is also invariant. 381 /// Return true if this represents an expression A + B*x + C*x^2 382 /// where A, B and C are loop invariant values. This corresponds 383 /// to an addrec of the form {L,+,M,+,N} 386 /// Set flags for a recurrence without clearing any previously set flags. 387 /// For AddRec, either NUW or NSW implies NW. Keep track of this fact here 388 /// to make it easier to propagate flags. 395 /// Return the value of this chain of recurrences at the specified 396 /// iteration number. 399 /// Return the value of this chain of recurrences at the specified iteration 400 /// number. Takes an explicit list of operands to represent an AddRec. 404 /// Return the number of iterations of this loop that produce 405 /// values in the specified constant range. Another way of 406 /// looking at this is that it returns the first iteration number 407 /// where the value is not in the condition, thus computing the 408 /// exit count. If the iteration count can't be computed, an 409 /// instance of SCEVCouldNotCompute is returned. 413 /// Return an expression representing the value of this expression 414 /// one iteration of the loop ahead. 417 /// Methods for support type inquiry through isa, cast, and dyn_cast: 423/// This node is the base class min/max selections. 433 /// Note: Constructing subclasses via this constructor is allowed 435constSCEV *
const *O,
size_tN)
438// Min and max never overflow 463/// This class represents a signed maximum selection. 471 /// Methods for support type inquiry through isa, cast, and dyn_cast: 475/// This class represents an unsigned maximum selection. 483 /// Methods for support type inquiry through isa, cast, and dyn_cast: 487/// This class represents a signed minimum selection. 495 /// Methods for support type inquiry through isa, cast, and dyn_cast: 499/// This class represents an unsigned minimum selection. 507 /// Methods for support type inquiry through isa, cast, and dyn_cast: 511/// This node is the base class for sequential/in-order min/max selections. 512/// Note that their fundamental difference from SCEVMinMaxExpr's is that they 513/// are early-returning upon reaching saturation point. 514/// I.e. given `0 umin_seq poison`, the result will be `0`, while the result of 515/// `0 umin poison` is `poison`. When returning early, later expressions are not 516/// executed, so `0 umin_seq (%x u/ 0)` does not result in undefined behavior. 520staticbool isSequentialMinMaxType(
enumSCEVTypesT) {
524 /// Set flags for a non-recurrence without clearing previously set flags. 528 /// Note: Constructing subclasses via this constructor is allowed 530constSCEV *
const *O,
size_tN)
532assert(isSequentialMinMaxType(
T));
533// Min and max never overflow 541assert(isSequentialMinMaxType(Ty));
559/// This class represents a sequential/in-order unsigned minimum selection. 568 /// Methods for support type inquiry through isa, cast, and dyn_cast: 574/// This means that we are dealing with an entirely unknown SCEV 575/// value, and only represent it as its LLVM Value. This is the 576/// "bottom" value for the analysis. 580 /// The parent ScalarEvolution value. This is used to update the 581 /// parent's maps when the value associated with a SCEVUnknown is 582 /// deleted or RAUW'd. 585 /// The next pointer in the linked list of all SCEVUnknown 586 /// instances owned by a ScalarEvolution. 593// Implement CallbackVH. 594void deleted()
override;
595void allUsesReplacedWith(
Value *New)
override;
602 /// Methods for support type inquiry through isa, cast, and dyn_cast: 606/// This class defines a simple visitor class that may be used for 607/// various SCEV analysis purposes. 612return ((SC *)
this)->visitConstant((
constSCEVConstant *)S);
614return ((SC *)
this)->visitVScale((
constSCEVVScale *)S);
624return ((SC *)
this)->visitAddExpr((
constSCEVAddExpr *)S);
626return ((SC *)
this)->visitMulExpr((
constSCEVMulExpr *)S);
628return ((SC *)
this)->visitUDivExpr((
constSCEVUDivExpr *)S);
632return ((SC *)
this)->visitSMaxExpr((
constSCEVSMaxExpr *)S);
634return ((SC *)
this)->visitUMaxExpr((
constSCEVUMaxExpr *)S);
636return ((SC *)
this)->visitSMinExpr((
constSCEVSMinExpr *)S);
638return ((SC *)
this)->visitUMinExpr((
constSCEVUMinExpr *)S);
643return ((SC *)
this)->visitUnknown((
constSCEVUnknown *)S);
655/// Visit all nodes in the expression tree using worklist traversal. 657/// Visitor implements: 658/// // return true to follow this node. 659/// bool follow(const SCEV *S); 660/// // return true to terminate the search. 667void push(
constSCEV *S) {
668if (Visited.
insert(S).second && Visitor.follow(S))
677while (!Worklist.
empty() && !Visitor.isDone()) {
712/// Use SCEVTraversal to visit all nodes in the given expression tree. 718/// Return true if any node in \p Root satisfies the predicate \p Pred. 719template <
typename PredTy>
725 FindClosure(PredTy Pred) : Pred(Pred) {}
727bool follow(
constSCEV *S) {
735bool isDone()
const{
return Found; }
738 FindClosure FC(Pred);
743/// This visitor recursively visits a SCEV expression and re-writes it. 744/// The result from each visit is cached, so it will return the same 745/// SCEV for the same input. 746template <
typename SC>
750// Memoize the result of each visit so that we only compute once for 751// the same input SCEV. This is to avoid redundant computations when 752// a SCEV is referenced by multiple SCEVs. Without memoization, this 753// visit algorithm would have exponential time complexity in the worst 754// case, causing the compiler to hang on certain tests. 766assert(Result.second &&
"Should insert a new entry");
767return Result.first->second;
823auto *
LHS = ((SC *)
this)->visit(Expr->
getLHS());
824auto *
RHS = ((SC *)
this)->visit(Expr->
getRHS());
836return !Changed ? Expr
901/// The SCEVParameterRewriter takes a scalar evolution expression and updates 902/// the SCEVUnknown components following the Map (Value -> SCEV). 927/// The SCEVLoopAddRecRewriter takes a scalar evolution expression and applies 928/// the Map (Loop -> SCEV) to all AddRecExprs. 947if (0 == Map.
count(L))
957}
// end namespace llvm 959#endif// LLVM_ANALYSIS_SCALAREVOLUTIONEXPRESSIONS_H This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
mir Rename Register Operands
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Virtual Register Rewriter
Class for arbitrary precision integers.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Value handle with callbacks on RAUW and destruction.
This is the shared class of boolean and integer constants.
const APInt & getValue() const
Return the constant as an APInt value reference.
This class represents a range of values.
This is an important base class in LLVM.
This class represents an Operation in the Expression.
iterator find(const_arg_type_t< KeyT > Val)
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
FoldingSetNodeIDRef - This class describes a reference to an interned FoldingSetNodeID,...
Represents a single loop in the control flow graph.
This node represents an addition of some number of SCEVs.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This node represents a polynomial recurrence on the trip count of the specified loop.
const SCEV * getStart() const
const SCEV * evaluateAtIteration(const SCEV *It, ScalarEvolution &SE) const
Return the value of this chain of recurrences at the specified iteration number.
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a recurrence without clearing any previously set flags.
bool isAffine() const
Return true if this represents an expression A + B*x where A and B are loop invariant values.
bool isQuadratic() const
Return true if this represents an expression A + B*x + C*x^2 where A, B and C are loop invariant valu...
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.
const SCEVAddRecExpr * getPostIncExpr(ScalarEvolution &SE) const
Return an expression representing the value of this expression one iteration of the loop ahead.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
const Loop * getLoop() const
This is the base class for unary cast operator classes.
const SCEV * getOperand(unsigned i) const
ArrayRef< const SCEV * > operands() const
size_t getNumOperands() const
const SCEV * getOperand() const
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This node is the base class for n'ary commutative operators.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
void setNoWrapFlags(NoWrapFlags Flags)
Set flags for a non-recurrence without clearing previously set flags.
SCEVCommutativeExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEV *const *O, size_t N)
This class represents a constant integer value.
ConstantInt * getValue() const
const APInt & getAPInt() const
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This is the base class for unary integral cast operator classes.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
The SCEVLoopAddRecRewriter takes a scalar evolution expression and applies the Map (Loop -> SCEV) to ...
static const SCEV * rewrite(const SCEV *Scev, LoopToScevMapT &Map, ScalarEvolution &SE)
const SCEV * visitAddRecExpr(const SCEVAddRecExpr *Expr)
SCEVLoopAddRecRewriter(ScalarEvolution &SE, LoopToScevMapT &M)
This node is the base class min/max selections.
static enum SCEVTypes negate(enum SCEVTypes T)
SCEVMinMaxExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEV *const *O, size_t N)
Note: Constructing subclasses via this constructor is allowed.
static bool classof(const SCEV *S)
This node represents multiplication of some number of SCEVs.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This node is a base class providing common functionality for n'ary operators.
bool hasNoUnsignedWrap() const
bool hasNoSelfWrap() const
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
size_t getNumOperands() const
bool hasNoSignedWrap() const
NoWrapFlags getNoWrapFlags(NoWrapFlags Mask=NoWrapMask) const
SCEVNAryExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEV *const *O, size_t N)
const SCEV * getOperand(unsigned i) const
const SCEV *const * Operands
ArrayRef< const SCEV * > operands() const
The SCEVParameterRewriter takes a scalar evolution expression and updates the SCEVUnknown components ...
const SCEV * visitUnknown(const SCEVUnknown *Expr)
static const SCEV * rewrite(const SCEV *Scev, ScalarEvolution &SE, ValueToSCEVMapTy &Map)
SCEVParameterRewriter(ScalarEvolution &SE, ValueToSCEVMapTy &M)
This class represents a cast from a pointer to a pointer-sized integer value.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This visitor recursively visits a SCEV expression and re-writes it.
const SCEV * visitSignExtendExpr(const SCEVSignExtendExpr *Expr)
const SCEV * visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr)
const SCEV * visit(const SCEV *S)
const SCEV * visitZeroExtendExpr(const SCEVZeroExtendExpr *Expr)
const SCEV * visitUnknown(const SCEVUnknown *Expr)
const SCEV * visitSMinExpr(const SCEVSMinExpr *Expr)
SCEVRewriteVisitor(ScalarEvolution &SE)
const SCEV * visitSequentialUMinExpr(const SCEVSequentialUMinExpr *Expr)
const SCEV * visitAddExpr(const SCEVAddExpr *Expr)
const SCEV * visitUMinExpr(const SCEVUMinExpr *Expr)
const SCEV * visitMulExpr(const SCEVMulExpr *Expr)
SmallDenseMap< const SCEV *, const SCEV * > RewriteResults
const SCEV * visitTruncateExpr(const SCEVTruncateExpr *Expr)
const SCEV * visitUMaxExpr(const SCEVUMaxExpr *Expr)
const SCEV * visitSMaxExpr(const SCEVSMaxExpr *Expr)
const SCEV * visitUDivExpr(const SCEVUDivExpr *Expr)
const SCEV * visitCouldNotCompute(const SCEVCouldNotCompute *Expr)
const SCEV * visitVScale(const SCEVVScale *VScale)
const SCEV * visitAddRecExpr(const SCEVAddRecExpr *Expr)
const SCEV * visitConstant(const SCEVConstant *Constant)
This class represents a signed maximum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a signed minimum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This node is the base class for sequential/in-order min/max selections.
SCEVSequentialMinMaxExpr(const FoldingSetNodeIDRef ID, enum SCEVTypes T, const SCEV *const *O, size_t N)
Note: Constructing subclasses via this constructor is allowed.
static bool classof(const SCEV *S)
static SCEVTypes getEquivalentNonSequentialSCEVType(SCEVTypes Ty)
SCEVTypes getEquivalentNonSequentialSCEVType() const
This class represents a sequential/in-order unsigned minimum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a sign extension of a small integer value to a larger integer value.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
Visit all nodes in the expression tree using worklist traversal.
void visitAll(const SCEV *Root)
This class represents a truncation of an integer value to a smaller integer value.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a binary unsigned division operation.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
ArrayRef< const SCEV * > operands() const
const SCEV * getOperand(unsigned i) const
const SCEV * getLHS() const
size_t getNumOperands() const
const SCEV * getRHS() const
This class represents an unsigned maximum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents an unsigned minimum selection.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This means that we are dealing with an entirely unknown SCEV value, and only represent it as its LLVM...
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents the value of vscale, as used when defining the length of a scalable vector or r...
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a zero extension of a small integer value to a larger integer value.
static bool classof(const SCEV *S)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents an analyzed expression in the program.
ArrayRef< const SCEV * > operands() const
Return operands of this SCEV expression.
SCEVTypes getSCEVType() const
unsigned short SubclassData
This field is initialized to zero and may be used in subclasses to store miscellaneous information.
Type * getType() const
Return the LLVM type of this SCEV expression.
NoWrapFlags
NoWrapFlags are bitfield indices into SubclassData.
The main scalar evolution driver.
const SCEV * getSMaxExpr(const SCEV *LHS, const SCEV *RHS)
const SCEV * getSMinExpr(const SCEV *LHS, const SCEV *RHS)
const SCEV * getUMaxExpr(const SCEV *LHS, const SCEV *RHS)
const SCEV * getPtrToIntExpr(const SCEV *Op, Type *Ty)
const SCEV * getAddRecExpr(const SCEV *Start, const SCEV *Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
const SCEV * getUDivExpr(const SCEV *LHS, const SCEV *RHS)
Get a canonical unsigned division expression, or something simpler if possible.
const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
const SCEV * getUMinExpr(const SCEV *LHS, const SCEV *RHS, bool Sequential=false)
const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
static SCEV::NoWrapFlags setFlags(SCEV::NoWrapFlags Flags, SCEV::NoWrapFlags OnFlags)
const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
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.
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.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Value * getValPtr() const
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
void visitAll(const SCEV *Root, SV &Visitor)
Use SCEVTraversal to visit all nodes in the given expression tree.
unsigned short computeExpressionSize(ArrayRef< const SCEV * > Args)
bool isPointerTy(const Type *T)
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
bool SCEVExprContains(const SCEV *Root, PredTy Pred)
Return true if any node in Root satisfies the predicate Pred.
An object of this class is returned by queries that could not be answered.
This class defines a simple visitor class that may be used for various SCEV analysis purposes.
RetVal visit(const SCEV *S)
RetVal visitCouldNotCompute(const SCEVCouldNotCompute *S)