1 //===- InferIntRangeInterfaceTest.cpp - Unit Tests for InferIntRange... --===//
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 "llvm/ADT/APInt.h"
13 #include <gtest/gtest.h>
17 TEST(IntRangeAttrs
, BasicConstructors
) {
18 APInt zero
= APInt::getZero(64);
21 ConstantIntRanges
boundedAbove(zero
, two
, zero
, three
);
22 EXPECT_EQ(boundedAbove
.umin(), zero
);
23 EXPECT_EQ(boundedAbove
.umax(), two
);
24 EXPECT_EQ(boundedAbove
.smin(), zero
);
25 EXPECT_EQ(boundedAbove
.smax(), three
);
28 TEST(IntRangeAttrs
, FromUnsigned
) {
29 APInt zero
= APInt::getZero(64);
30 APInt maxInt
= APInt::getSignedMaxValue(64);
31 APInt minInt
= APInt::getSignedMinValue(64);
32 APInt minIntPlusOne
= minInt
+ 1;
34 ConstantIntRanges canPortToSigned
=
35 ConstantIntRanges::fromUnsigned(zero
, maxInt
);
36 EXPECT_EQ(canPortToSigned
.smin(), zero
);
37 EXPECT_EQ(canPortToSigned
.smax(), maxInt
);
39 ConstantIntRanges cantPortToSigned
=
40 ConstantIntRanges::fromUnsigned(zero
, minInt
);
41 EXPECT_EQ(cantPortToSigned
.smin(), minInt
);
42 EXPECT_EQ(cantPortToSigned
.smax(), maxInt
);
44 ConstantIntRanges signedNegative
=
45 ConstantIntRanges::fromUnsigned(minInt
, minIntPlusOne
);
46 EXPECT_EQ(signedNegative
.smin(), minInt
);
47 EXPECT_EQ(signedNegative
.smax(), minIntPlusOne
);
50 TEST(IntRangeAttrs
, FromSigned
) {
51 APInt zero
= APInt::getZero(64);
53 APInt negOne
= zero
- 1;
54 APInt intMax
= APInt::getSignedMaxValue(64);
55 APInt intMin
= APInt::getSignedMinValue(64);
56 APInt uintMax
= APInt::getMaxValue(64);
58 ConstantIntRanges noUnsignedBound
=
59 ConstantIntRanges::fromSigned(negOne
, one
);
60 EXPECT_EQ(noUnsignedBound
.umin(), zero
);
61 EXPECT_EQ(noUnsignedBound
.umax(), uintMax
);
63 ConstantIntRanges positive
= ConstantIntRanges::fromSigned(one
, intMax
);
64 EXPECT_EQ(positive
.umin(), one
);
65 EXPECT_EQ(positive
.umax(), intMax
);
67 ConstantIntRanges negative
= ConstantIntRanges::fromSigned(intMin
, negOne
);
68 EXPECT_EQ(negative
.umin(), intMin
);
69 EXPECT_EQ(negative
.umax(), negOne
);
71 ConstantIntRanges preserved
= ConstantIntRanges::fromSigned(zero
, one
);
72 EXPECT_EQ(preserved
.umin(), zero
);
73 EXPECT_EQ(preserved
.umax(), one
);
76 TEST(IntRangeAttrs
, Join
) {
77 APInt zero
= APInt::getZero(64);
80 APInt intMin
= APInt::getSignedMinValue(64);
81 APInt intMax
= APInt::getSignedMaxValue(64);
82 APInt uintMax
= APInt::getMaxValue(64);
84 ConstantIntRanges
maximal(zero
, uintMax
, intMin
, intMax
);
85 ConstantIntRanges
zeroOne(zero
, one
, zero
, one
);
87 EXPECT_EQ(zeroOne
.rangeUnion(maximal
), maximal
);
88 EXPECT_EQ(maximal
.rangeUnion(zeroOne
), maximal
);
90 EXPECT_EQ(zeroOne
.rangeUnion(zeroOne
), zeroOne
);
92 ConstantIntRanges
oneTwo(one
, two
, one
, two
);
93 ConstantIntRanges
zeroTwo(zero
, two
, zero
, two
);
94 EXPECT_EQ(zeroOne
.rangeUnion(oneTwo
), zeroTwo
);
96 ConstantIntRanges
zeroOneUnsignedOnly(zero
, one
, intMin
, intMax
);
97 ConstantIntRanges
zeroOneSignedOnly(zero
, uintMax
, zero
, one
);
98 EXPECT_EQ(zeroOneUnsignedOnly
.rangeUnion(zeroOneSignedOnly
), maximal
);