30#include "llvm/ADT/STLExtras.h"
31#include "llvm/Support/Casting.h"
32#include "llvm/Support/Debug.h"
33#include "llvm/Support/DebugLog.h"
38#define DEBUG_TYPE "int-range-analysis"
53 auto nonNegativePred = [&solver](
Value v) ->
bool {
57 llvm::all_of(op->
getResults(), nonNegativePred));
87 auto inferrable = dyn_cast<InferIntRangeInterface>(op);
93 LDBG() <<
"Inferring ranges for "
95 auto argRanges = llvm::map_to_vector(
101 auto result = dyn_cast<OpResult>(v);
106 LDBG() <<
"Inferred range " << attrs;
111 inferrable.inferResultRangesFromOptional(argRanges, joinCallback);
120 assert(nonSuccessorInputs.size() == nonSuccessorInputLattices.size() &&
122 if (
auto inferrable = dyn_cast<InferIntRangeInterface>(op)) {
123 LDBG() <<
"Inferring ranges for "
127 return getLattice(value)->getValue();
131 auto it = llvm::find(nonSuccessorInputs, v);
132 if (it == nonSuccessorInputs.end())
135 LDBG() <<
"Inferred range " << attrs;
136 unsigned nonSuccessorInputIdx =
137 std::distance(nonSuccessorInputs.begin(), it);
139 nonSuccessorInputLattices[nonSuccessorInputIdx];
143 inferrable.inferResultRangesFromOptional(argRanges, joinCallback);
152 if (
auto attr = dyn_cast<Attribute>(loopBound)) {
153 if (
auto bound = dyn_cast<IntegerAttr>(attr))
154 return bound.getValue();
155 }
else if (
auto value = llvm::dyn_cast<Value>(loopBound)) {
164 return getUpper ? APInt::getSignedMaxValue(width)
165 : APInt::getSignedMinValue(width);
169 if (
auto loop = dyn_cast<LoopLikeOpInterface>(op)) {
170 std::optional<llvm::SmallVector<Value>> maybeIvs =
171 loop.getLoopInductionVars();
174 op, successor, nonSuccessorInputs, nonSuccessorInputLattices,
180 std::optional<SmallVector<OpFoldResult>> maybeLowerBounds =
181 loop.getLoopLowerBounds();
182 std::optional<SmallVector<OpFoldResult>> maybeUpperBounds =
183 loop.getLoopUpperBounds();
184 std::optional<SmallVector<OpFoldResult>> maybeSteps = loop.getLoopSteps();
185 if (!maybeLowerBounds || !maybeUpperBounds || !maybeSteps) {
187 op, successor, nonSuccessorInputs, nonSuccessorInputLattices,
193 for (
auto [iv, lowerBound, upperBound, step] :
194 llvm::zip_equal(*maybeIvs, lowerBounds, upperBounds, steps)) {
195 APInt
min = getLoopBoundFromFold(lowerBound, iv.getType(),
197 APInt
max = getLoopBoundFromFold(upperBound, iv.getType(),
201 getLoopBoundFromFold(step, iv.getType(),
true);
203 if (stepVal.isNegative()) {
224 op, successor, nonSuccessorInputs, nonSuccessorInputLattices, getLattice);
static constexpr unsigned kIntegerRangeWideningBudget
Number of merge-site joins a single integer-range lattice element is allowed to absorb before Integer...
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
A set of arbitrary-precision integers representing bounds on a given integer value.
const APInt & smax() const
The maximum value of an integer when it is interpreted as signed.
const APInt & smin() const
The minimum value of an integer when it is interpreted as signed.
static ConstantIntRanges fromSigned(const APInt &smin, const APInt &smax)
Create an ConstantIntRanges with the signed minimum and maximum equal to smin and smax,...
static unsigned getStorageBitwidth(Type type)
Return the bitwidth that should be used for integer ranges describing type.
void propagateIfChanged(AnalysisState *state, ChangeResult changed)
Propagate an update to a state if it changed.
The general data-flow analysis solver.
const StateT * lookupState(AnchorT anchor) const
Lookup an analysis state for the given lattice anchor.
This lattice value represents the integer range of an SSA value.
const ConstantIntRanges & getValue() const
Get the known integer value range.
bool isUninitialized() const
Whether the range is uninitialized.
static IntegerValueRange getMaxRange(Value value)
Create a maximal range ([0, uint_max(t)] / [int_min(t), int_max(t)]) range that is used to mark the v...
This class represents a single result from folding an operation.
Set of flags used to control the behavior of the various IR print methods (e.g.
A wrapper class that allows for printing an operation with a set of flags, useful to act as a "stream...
Operation is the basic unit of execution within MLIR.
operand_range getOperands()
Returns an iterator on the underlying Value's.
result_range getResults()
This class represents a successor of a region.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
void visitNonControlFlowArguments(Operation *op, const RegionSuccessor &successor, ValueRange nonSuccessorInputs, ArrayRef< IntegerValueRangeLattice * > nonSuccessorInputLattices, GetLatticeFn getLattice) override
Visit block arguments or operation results of an operation with region control-flow for which values ...
LogicalResult visitOperation(Operation *op, ArrayRef< const IntegerValueRangeLattice * > operands, ArrayRef< IntegerValueRangeLattice * > results) override
Visit an operation.
This lattice element represents the integer value range of an SSA value.
ChangeResult join(const AbstractSparseLattice &rhs) override
Join the information contained in 'rhs' into this lattice.
ValueT & getValue()
Return the value held by this lattice.
ChangeResult join(const AbstractSparseLattice &rhs) override
Join the information contained in the 'rhs' lattice into this lattice.
AbstractSparseLattice(Value value)
IntegerValueRangeLattice * getLatticeElement(Value value) override
function_ref< const IntegerValueRangeLattice *(Value)> GetLatticeFn
virtual void visitNonControlFlowArguments(Operation *op, const RegionSuccessor &successor, ValueRange nonSuccessorInputs, ArrayRef< StateT * > nonSuccessorInputLattices, GetLatticeFn getLattice)
Given an operation with possible region control-flow, the lattices of the operands,...
void setAllToEntryStates(ArrayRef< IntegerValueRangeLattice * > lattices)
LogicalResult staticallyNonNegative(DataFlowSolver &solver, Operation *op)
Succeeds if an op can be converted to its unsigned equivalent without changing its semantics.
Include the generated interface declarations.
ChangeResult
A result type used to indicate if a change happened.