1*95aff23eSKrzysztof Drewniak //===- InferIntRangeInterfaceTest.cpp - Unit Tests for InferIntRange... --===//
2*95aff23eSKrzysztof Drewniak //
3*95aff23eSKrzysztof Drewniak // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*95aff23eSKrzysztof Drewniak // See https://llvm.org/LICENSE.txt for license information.
5*95aff23eSKrzysztof Drewniak // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*95aff23eSKrzysztof Drewniak //
7*95aff23eSKrzysztof Drewniak //===----------------------------------------------------------------------===//
8*95aff23eSKrzysztof Drewniak
9*95aff23eSKrzysztof Drewniak #include "mlir/Interfaces/InferIntRangeInterface.h"
10*95aff23eSKrzysztof Drewniak #include "llvm/ADT/APInt.h"
11*95aff23eSKrzysztof Drewniak #include <limits>
12*95aff23eSKrzysztof Drewniak
13*95aff23eSKrzysztof Drewniak #include <gtest/gtest.h>
14*95aff23eSKrzysztof Drewniak
15*95aff23eSKrzysztof Drewniak using namespace mlir;
16*95aff23eSKrzysztof Drewniak
TEST(IntRangeAttrs,BasicConstructors)17*95aff23eSKrzysztof Drewniak TEST(IntRangeAttrs, BasicConstructors) {
18*95aff23eSKrzysztof Drewniak APInt zero = APInt::getZero(64);
19*95aff23eSKrzysztof Drewniak APInt two(64, 2);
20*95aff23eSKrzysztof Drewniak APInt three(64, 3);
21*95aff23eSKrzysztof Drewniak ConstantIntRanges boundedAbove(zero, two, zero, three);
22*95aff23eSKrzysztof Drewniak EXPECT_EQ(boundedAbove.umin(), zero);
23*95aff23eSKrzysztof Drewniak EXPECT_EQ(boundedAbove.umax(), two);
24*95aff23eSKrzysztof Drewniak EXPECT_EQ(boundedAbove.smin(), zero);
25*95aff23eSKrzysztof Drewniak EXPECT_EQ(boundedAbove.smax(), three);
26*95aff23eSKrzysztof Drewniak }
27*95aff23eSKrzysztof Drewniak
TEST(IntRangeAttrs,FromUnsigned)28*95aff23eSKrzysztof Drewniak TEST(IntRangeAttrs, FromUnsigned) {
29*95aff23eSKrzysztof Drewniak APInt zero = APInt::getZero(64);
30*95aff23eSKrzysztof Drewniak APInt maxInt = APInt::getSignedMaxValue(64);
31*95aff23eSKrzysztof Drewniak APInt minInt = APInt::getSignedMinValue(64);
32*95aff23eSKrzysztof Drewniak APInt minIntPlusOne = minInt + 1;
33*95aff23eSKrzysztof Drewniak
34*95aff23eSKrzysztof Drewniak ConstantIntRanges canPortToSigned =
35*95aff23eSKrzysztof Drewniak ConstantIntRanges::fromUnsigned(zero, maxInt);
36*95aff23eSKrzysztof Drewniak EXPECT_EQ(canPortToSigned.smin(), zero);
37*95aff23eSKrzysztof Drewniak EXPECT_EQ(canPortToSigned.smax(), maxInt);
38*95aff23eSKrzysztof Drewniak
39*95aff23eSKrzysztof Drewniak ConstantIntRanges cantPortToSigned =
40*95aff23eSKrzysztof Drewniak ConstantIntRanges::fromUnsigned(zero, minInt);
41*95aff23eSKrzysztof Drewniak EXPECT_EQ(cantPortToSigned.smin(), minInt);
42*95aff23eSKrzysztof Drewniak EXPECT_EQ(cantPortToSigned.smax(), maxInt);
43*95aff23eSKrzysztof Drewniak
44*95aff23eSKrzysztof Drewniak ConstantIntRanges signedNegative =
45*95aff23eSKrzysztof Drewniak ConstantIntRanges::fromUnsigned(minInt, minIntPlusOne);
46*95aff23eSKrzysztof Drewniak EXPECT_EQ(signedNegative.smin(), minInt);
47*95aff23eSKrzysztof Drewniak EXPECT_EQ(signedNegative.smax(), minIntPlusOne);
48*95aff23eSKrzysztof Drewniak }
49*95aff23eSKrzysztof Drewniak
TEST(IntRangeAttrs,FromSigned)50*95aff23eSKrzysztof Drewniak TEST(IntRangeAttrs, FromSigned) {
51*95aff23eSKrzysztof Drewniak APInt zero = APInt::getZero(64);
52*95aff23eSKrzysztof Drewniak APInt one = zero + 1;
53*95aff23eSKrzysztof Drewniak APInt negOne = zero - 1;
54*95aff23eSKrzysztof Drewniak APInt intMax = APInt::getSignedMaxValue(64);
55*95aff23eSKrzysztof Drewniak APInt intMin = APInt::getSignedMinValue(64);
56*95aff23eSKrzysztof Drewniak APInt uintMax = APInt::getMaxValue(64);
57*95aff23eSKrzysztof Drewniak
58*95aff23eSKrzysztof Drewniak ConstantIntRanges noUnsignedBound =
59*95aff23eSKrzysztof Drewniak ConstantIntRanges::fromSigned(negOne, one);
60*95aff23eSKrzysztof Drewniak EXPECT_EQ(noUnsignedBound.umin(), zero);
61*95aff23eSKrzysztof Drewniak EXPECT_EQ(noUnsignedBound.umax(), uintMax);
62*95aff23eSKrzysztof Drewniak
63*95aff23eSKrzysztof Drewniak ConstantIntRanges positive = ConstantIntRanges::fromSigned(one, intMax);
64*95aff23eSKrzysztof Drewniak EXPECT_EQ(positive.umin(), one);
65*95aff23eSKrzysztof Drewniak EXPECT_EQ(positive.umax(), intMax);
66*95aff23eSKrzysztof Drewniak
67*95aff23eSKrzysztof Drewniak ConstantIntRanges negative = ConstantIntRanges::fromSigned(intMin, negOne);
68*95aff23eSKrzysztof Drewniak EXPECT_EQ(negative.umin(), intMin);
69*95aff23eSKrzysztof Drewniak EXPECT_EQ(negative.umax(), negOne);
70*95aff23eSKrzysztof Drewniak
71*95aff23eSKrzysztof Drewniak ConstantIntRanges preserved = ConstantIntRanges::fromSigned(zero, one);
72*95aff23eSKrzysztof Drewniak EXPECT_EQ(preserved.umin(), zero);
73*95aff23eSKrzysztof Drewniak EXPECT_EQ(preserved.umax(), one);
74*95aff23eSKrzysztof Drewniak }
75*95aff23eSKrzysztof Drewniak
TEST(IntRangeAttrs,Join)76*95aff23eSKrzysztof Drewniak TEST(IntRangeAttrs, Join) {
77*95aff23eSKrzysztof Drewniak APInt zero = APInt::getZero(64);
78*95aff23eSKrzysztof Drewniak APInt one = zero + 1;
79*95aff23eSKrzysztof Drewniak APInt two = zero + 2;
80*95aff23eSKrzysztof Drewniak APInt intMin = APInt::getSignedMinValue(64);
81*95aff23eSKrzysztof Drewniak APInt intMax = APInt::getSignedMaxValue(64);
82*95aff23eSKrzysztof Drewniak APInt uintMax = APInt::getMaxValue(64);
83*95aff23eSKrzysztof Drewniak
84*95aff23eSKrzysztof Drewniak ConstantIntRanges maximal(zero, uintMax, intMin, intMax);
85*95aff23eSKrzysztof Drewniak ConstantIntRanges zeroOne(zero, one, zero, one);
86*95aff23eSKrzysztof Drewniak
87*95aff23eSKrzysztof Drewniak EXPECT_EQ(zeroOne.rangeUnion(maximal), maximal);
88*95aff23eSKrzysztof Drewniak EXPECT_EQ(maximal.rangeUnion(zeroOne), maximal);
89*95aff23eSKrzysztof Drewniak
90*95aff23eSKrzysztof Drewniak EXPECT_EQ(zeroOne.rangeUnion(zeroOne), zeroOne);
91*95aff23eSKrzysztof Drewniak
92*95aff23eSKrzysztof Drewniak ConstantIntRanges oneTwo(one, two, one, two);
93*95aff23eSKrzysztof Drewniak ConstantIntRanges zeroTwo(zero, two, zero, two);
94*95aff23eSKrzysztof Drewniak EXPECT_EQ(zeroOne.rangeUnion(oneTwo), zeroTwo);
95*95aff23eSKrzysztof Drewniak
96*95aff23eSKrzysztof Drewniak ConstantIntRanges zeroOneUnsignedOnly(zero, one, intMin, intMax);
97*95aff23eSKrzysztof Drewniak ConstantIntRanges zeroOneSignedOnly(zero, uintMax, zero, one);
98*95aff23eSKrzysztof Drewniak EXPECT_EQ(zeroOneUnsignedOnly.rangeUnion(zeroOneSignedOnly), maximal);
99*95aff23eSKrzysztof Drewniak }
100