MLIR  20.0.0git
InferIntRangeInterface.cpp
Go to the documentation of this file.
1 //===- InferIntRangeInterface.cpp - Integer range inference interface ---===//
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 
10 #include "mlir/IR/BuiltinTypes.h"
11 #include "mlir/Interfaces/InferIntRangeInterface.cpp.inc"
12 #include <optional>
13 
14 using namespace mlir;
15 
17  return umin().getBitWidth() == other.umin().getBitWidth() &&
18  umin() == other.umin() && umax() == other.umax() &&
19  smin() == other.smin() && smax() == other.smax();
20 }
21 
22 const APInt &ConstantIntRanges::umin() const { return uminVal; }
23 
24 const APInt &ConstantIntRanges::umax() const { return umaxVal; }
25 
26 const APInt &ConstantIntRanges::smin() const { return sminVal; }
27 
28 const APInt &ConstantIntRanges::smax() const { return smaxVal; }
29 
31  if (type.isIndex())
32  return IndexType::kInternalStorageBitWidth;
33  if (auto integerType = dyn_cast<IntegerType>(type))
34  return integerType.getWidth();
35  // Non-integer types have their bounds stored in width 0 `APInt`s.
36  return 0;
37 }
38 
40  return fromUnsigned(APInt::getZero(bitwidth), APInt::getMaxValue(bitwidth));
41 }
42 
44  return {value, value, value, value};
45 }
46 
48  bool isSigned) {
49  if (isSigned)
50  return fromSigned(min, max);
51  return fromUnsigned(min, max);
52 }
53 
55  const APInt &smax) {
56  unsigned int width = smin.getBitWidth();
57  APInt umin, umax;
58  if (smin.isNonNegative() == smax.isNonNegative()) {
59  umin = smin.ult(smax) ? smin : smax;
60  umax = smin.ugt(smax) ? smin : smax;
61  } else {
62  umin = APInt::getMinValue(width);
63  umax = APInt::getMaxValue(width);
64  }
65  return {umin, umax, smin, smax};
66 }
67 
69  const APInt &umax) {
70  unsigned int width = umin.getBitWidth();
71  APInt smin, smax;
72  if (umin.isNonNegative() == umax.isNonNegative()) {
73  smin = umin.slt(umax) ? umin : umax;
74  smax = umin.sgt(umax) ? umin : umax;
75  } else {
76  smin = APInt::getSignedMinValue(width);
77  smax = APInt::getSignedMaxValue(width);
78  }
79  return {umin, umax, smin, smax};
80 }
81 
84  // "Not an integer" poisons everything and also cannot be fed to comparison
85  // operators.
86  if (umin().getBitWidth() == 0)
87  return *this;
88  if (other.umin().getBitWidth() == 0)
89  return other;
90 
91  const APInt &uminUnion = umin().ult(other.umin()) ? umin() : other.umin();
92  const APInt &umaxUnion = umax().ugt(other.umax()) ? umax() : other.umax();
93  const APInt &sminUnion = smin().slt(other.smin()) ? smin() : other.smin();
94  const APInt &smaxUnion = smax().sgt(other.smax()) ? smax() : other.smax();
95 
96  return {uminUnion, umaxUnion, sminUnion, smaxUnion};
97 }
98 
101  // "Not an integer" poisons everything and also cannot be fed to comparison
102  // operators.
103  if (umin().getBitWidth() == 0)
104  return *this;
105  if (other.umin().getBitWidth() == 0)
106  return other;
107 
108  const APInt &uminIntersect = umin().ugt(other.umin()) ? umin() : other.umin();
109  const APInt &umaxIntersect = umax().ult(other.umax()) ? umax() : other.umax();
110  const APInt &sminIntersect = smin().sgt(other.smin()) ? smin() : other.smin();
111  const APInt &smaxIntersect = smax().slt(other.smax()) ? smax() : other.smax();
112 
113  return {uminIntersect, umaxIntersect, sminIntersect, smaxIntersect};
114 }
115 
116 std::optional<APInt> ConstantIntRanges::getConstantValue() const {
117  // Note: we need to exclude the trivially-equal width 0 values here.
118  if (umin() == umax() && umin().getBitWidth() != 0)
119  return umin();
120  if (smin() == smax() && smin().getBitWidth() != 0)
121  return smin();
122  return std::nullopt;
123 }
124 
125 raw_ostream &mlir::operator<<(raw_ostream &os, const ConstantIntRanges &range) {
126  return os << "unsigned : [" << range.umin() << ", " << range.umax()
127  << "] signed : [" << range.smin() << ", " << range.smax() << "]";
128 }
129 
131  unsigned width = ConstantIntRanges::getStorageBitwidth(value.getType());
132  if (width == 0)
133  return {};
134 
135  APInt umin = APInt::getMinValue(width);
136  APInt umax = APInt::getMaxValue(width);
137  APInt smin = width != 0 ? APInt::getSignedMinValue(width) : umin;
138  APInt smax = width != 0 ? APInt::getSignedMaxValue(width) : umax;
139  return IntegerValueRange{ConstantIntRanges{umin, umax, smin, smax}};
140 }
141 
142 raw_ostream &mlir::operator<<(raw_ostream &os, const IntegerValueRange &range) {
143  range.print(os);
144  return os;
145 }
146 
148  InferIntRangeInterface interface, ArrayRef<IntegerValueRange> argRanges,
149  SetIntLatticeFn setResultRanges) {
151  unpacked.reserve(argRanges.size());
152 
153  for (const IntegerValueRange &range : argRanges) {
154  if (range.isUninitialized())
155  return;
156  unpacked.push_back(range.getValue());
157  }
158 
159  interface.inferResultRanges(
160  unpacked,
161  [&setResultRanges](Value value, const ConstantIntRanges &argRanges) {
162  setResultRanges(value, IntegerValueRange{argRanges});
163  });
164 }
165 
167  InferIntRangeInterface interface, ArrayRef<ConstantIntRanges> argRanges,
168  SetIntRangeFn setResultRanges) {
169  auto ranges = llvm::to_vector_of<IntegerValueRange>(argRanges);
170  interface.inferResultRangesFromOptional(
171  ranges,
172  [&setResultRanges](Value value, const IntegerValueRange &argRanges) {
173  if (!argRanges.isUninitialized())
174  setResultRanges(value, argRanges.getValue());
175  });
176 }
static Value getZero(OpBuilder &b, Location loc, Type elementType)
Get zero value for an element type.
static Value max(ImplicitLocOpBuilder &builder, Value value, Value bound)
static Value min(ImplicitLocOpBuilder &builder, Value value, Value bound)
static unsigned getBitWidth(Type type)
Returns the bit width of integer, float or vector of float or integer values.
Definition: SPIRVToLLVM.cpp:71
A set of arbitrary-precision integers representing bounds on a given integer value.
static ConstantIntRanges maxRange(unsigned bitwidth)
Create a ConstantIntRanges with the maximum bounds for the width bitwidth, that is - [0,...
const APInt & smax() const
The maximum value of an integer when it is interpreted as signed.
static ConstantIntRanges constant(const APInt &value)
Create a ConstantIntRanges with a constant value - that is, with the bounds [value,...
static ConstantIntRanges fromUnsigned(const APInt &umin, const APInt &umax)
Create an ConstantIntRanges with the unsigned minimum and maximum equal to umin and umax and the sign...
const APInt & smin() const
The minimum value of an integer when it is interpreted as signed.
static ConstantIntRanges range(const APInt &min, const APInt &max, bool isSigned)
Create a ConstantIntRanges whose minimum is min and maximum is max with isSigned specifying if the mi...
ConstantIntRanges intersection(const ConstantIntRanges &other) const
Returns the intersection (computed separately for signed and unsigned bounds) of this range and other...
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.
std::optional< APInt > getConstantValue() const
If either the signed or unsigned interpretations of the range indicate that the value it bounds is a ...
const APInt & umax() const
The maximum value of an integer when it is interpreted as unsigned.
const APInt & umin() const
The minimum value of an integer when it is interpreted as unsigned.
ConstantIntRanges rangeUnion(const ConstantIntRanges &other) const
Returns the union (computed separately for signed and unsigned bounds) of this range and other.
bool operator==(const ConstantIntRanges &other) const
This lattice value represents the integer range of an SSA value.
void print(raw_ostream &os) const
Print the integer value range.
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...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
bool isIndex() const
Definition: Types.cpp:57
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Type getType() const
Return the type of this value.
Definition: Value.h:129
void defaultInferResultRanges(InferIntRangeInterface interface, ArrayRef< IntegerValueRange > argRanges, SetIntLatticeFn setResultRanges)
Default implementation of inferResultRanges which dispatches to the inferResultRangesFromOptional.
void defaultInferResultRangesFromOptional(InferIntRangeInterface interface, ArrayRef< ConstantIntRanges > argRanges, SetIntRangeFn setResultRanges)
Default implementation of inferResultRangesFromOptional which dispatches to the inferResultRanges.
Include the generated interface declarations.
raw_ostream & operator<<(raw_ostream &os, const AliasResult &result)
Definition: AliasAnalysis.h:78