1 //===- InferIntRangeInterface.cpp - Integer range inference interface ---===//
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 #include "mlir/Interfaces/InferIntRangeInterface.h"
10 #include "mlir/IR/BuiltinTypes.h"
12 #include "mlir/Interfaces/InferIntRangeInterface.cpp.inc"
16 bool ConstantIntRanges::operator==(const ConstantIntRanges
&other
) const {
17 return umin().getBitWidth() == other
.umin().getBitWidth() &&
18 umin() == other
.umin() && umax() == other
.umax() &&
19 smin() == other
.smin() && smax() == other
.smax();
22 const APInt
&ConstantIntRanges::umin() const { return uminVal
; }
24 const APInt
&ConstantIntRanges::umax() const { return umaxVal
; }
26 const APInt
&ConstantIntRanges::smin() const { return sminVal
; }
28 const APInt
&ConstantIntRanges::smax() const { return smaxVal
; }
30 unsigned ConstantIntRanges::getStorageBitwidth(Type type
) {
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.
39 ConstantIntRanges
ConstantIntRanges::maxRange(unsigned bitwidth
) {
40 return fromUnsigned(APInt::getZero(bitwidth
), APInt::getMaxValue(bitwidth
));
43 ConstantIntRanges
ConstantIntRanges::constant(const APInt
&value
) {
44 return {value
, value
, value
, value
};
47 ConstantIntRanges
ConstantIntRanges::range(const APInt
&min
, const APInt
&max
,
50 return fromSigned(min
, max
);
51 return fromUnsigned(min
, max
);
54 ConstantIntRanges
ConstantIntRanges::fromSigned(const APInt
&smin
,
56 unsigned int width
= smin
.getBitWidth();
58 if (smin
.isNonNegative() == smax
.isNonNegative()) {
59 umin
= smin
.ult(smax
) ? smin
: smax
;
60 umax
= smin
.ugt(smax
) ? smin
: smax
;
62 umin
= APInt::getMinValue(width
);
63 umax
= APInt::getMaxValue(width
);
65 return {umin
, umax
, smin
, smax
};
68 ConstantIntRanges
ConstantIntRanges::fromUnsigned(const APInt
&umin
,
70 unsigned int width
= umin
.getBitWidth();
72 if (umin
.isNonNegative() == umax
.isNonNegative()) {
73 smin
= umin
.slt(umax
) ? umin
: umax
;
74 smax
= umin
.sgt(umax
) ? umin
: umax
;
76 smin
= APInt::getSignedMinValue(width
);
77 smax
= APInt::getSignedMaxValue(width
);
79 return {umin
, umax
, smin
, smax
};
83 ConstantIntRanges::rangeUnion(const ConstantIntRanges
&other
) const {
84 // "Not an integer" poisons everything and also cannot be fed to comparison
86 if (umin().getBitWidth() == 0)
88 if (other
.umin().getBitWidth() == 0)
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();
96 return {uminUnion
, umaxUnion
, sminUnion
, smaxUnion
};
100 ConstantIntRanges::intersection(const ConstantIntRanges
&other
) const {
101 // "Not an integer" poisons everything and also cannot be fed to comparison
103 if (umin().getBitWidth() == 0)
105 if (other
.umin().getBitWidth() == 0)
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();
113 return {uminIntersect
, umaxIntersect
, sminIntersect
, smaxIntersect
};
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)
120 if (smin() == smax() && smin().getBitWidth() != 0)
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() << "]";