| 1 |
//===- InferIntRangeInterfaceTest.cpp - Unit Tests for InferIntRange... --===// |
| 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 |
|
| 9 |
#include "mlir/Interfaces/InferIntRangeInterface.h" |
| 10 |
#include "llvm/ADT/APInt.h" |
| 11 |
#include <limits> |
| 12 |
|
| 13 |
#include <gtest/gtest.h> |
| 14 |
|
| 15 |
using namespace mlir; |
| 16 |
|
| 17 |
TEST(IntRangeAttrs, BasicConstructors) { |
| 18 |
APInt zero = APInt::getZero(64); |
| 19 |
APInt two(64, 2); |
| 20 |
APInt three(64, 3); |
| 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); |
| 26 |
} |
| 27 |
|
| 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; |
| 33 |
|
| 34 |
ConstantIntRanges canPortToSigned = |
| 35 |
ConstantIntRanges::fromUnsigned(zero, maxInt); |
| 36 |
EXPECT_EQ(canPortToSigned.smin(), zero); |
| 37 |
EXPECT_EQ(canPortToSigned.smax(), maxInt); |
| 38 |
|
| 39 |
ConstantIntRanges cantPortToSigned = |
| 40 |
ConstantIntRanges::fromUnsigned(zero, minInt); |
| 41 |
EXPECT_EQ(cantPortToSigned.smin(), minInt); |
| 42 |
EXPECT_EQ(cantPortToSigned.smax(), maxInt); |
| 43 |
|
| 44 |
ConstantIntRanges signedNegative = |
| 45 |
ConstantIntRanges::fromUnsigned(minInt, minIntPlusOne); |
| 46 |
EXPECT_EQ(signedNegative.smin(), minInt); |
| 47 |
EXPECT_EQ(signedNegative.smax(), minIntPlusOne); |
| 48 |
} |
| 49 |
|
| 50 |
TEST(IntRangeAttrs, FromSigned) { |
| 51 |
APInt zero = APInt::getZero(64); |
| 52 |
APInt one = zero + 1; |
| 53 |
APInt negOne = zero - 1; |
| 54 |
APInt intMax = APInt::getSignedMaxValue(64); |
| 55 |
APInt intMin = APInt::getSignedMinValue(64); |
| 56 |
APInt uintMax = APInt::getMaxValue(64); |
| 57 |
|
| 58 |
ConstantIntRanges noUnsignedBound = |
| 59 |
ConstantIntRanges::fromSigned(negOne, one); |
| 60 |
EXPECT_EQ(noUnsignedBound.umin(), zero); |
| 61 |
EXPECT_EQ(noUnsignedBound.umax(), uintMax); |
| 62 |
|
| 63 |
ConstantIntRanges positive = ConstantIntRanges::fromSigned(one, intMax); |
| 64 |
EXPECT_EQ(positive.umin(), one); |
| 65 |
EXPECT_EQ(positive.umax(), intMax); |
| 66 |
|
| 67 |
ConstantIntRanges negative = ConstantIntRanges::fromSigned(intMin, negOne); |
| 68 |
EXPECT_EQ(negative.umin(), intMin); |
| 69 |
EXPECT_EQ(negative.umax(), negOne); |
| 70 |
|
| 71 |
ConstantIntRanges preserved = ConstantIntRanges::fromSigned(zero, one); |
| 72 |
EXPECT_EQ(preserved.umin(), zero); |
| 73 |
EXPECT_EQ(preserved.umax(), one); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(IntRangeAttrs, Join) { |
| 77 |
APInt zero = APInt::getZero(64); |
| 78 |
APInt one = zero + 1; |
| 79 |
APInt two = zero + 2; |
| 80 |
APInt intMin = APInt::getSignedMinValue(64); |
| 81 |
APInt intMax = APInt::getSignedMaxValue(64); |
| 82 |
APInt uintMax = APInt::getMaxValue(64); |
| 83 |
|
| 84 |
ConstantIntRanges maximal(zero, uintMax, intMin, intMax); |
| 85 |
ConstantIntRanges zeroOne(zero, one, zero, one); |
| 86 |
|
| 87 |
EXPECT_EQ(zeroOne.rangeUnion(maximal), maximal); |
| 88 |
EXPECT_EQ(maximal.rangeUnion(zeroOne), maximal); |
| 89 |
|
| 90 |
EXPECT_EQ(zeroOne.rangeUnion(zeroOne), zeroOne); |
| 91 |
|
| 92 |
ConstantIntRanges oneTwo(one, two, one, two); |
| 93 |
ConstantIntRanges zeroTwo(zero, two, zero, two); |
| 94 |
EXPECT_EQ(zeroOne.rangeUnion(oneTwo), zeroTwo); |
| 95 |
|
| 96 |
ConstantIntRanges zeroOneUnsignedOnly(zero, one, intMin, intMax); |
| 97 |
ConstantIntRanges zeroOneSignedOnly(zero, uintMax, zero, one); |
| 98 |
EXPECT_EQ(zeroOneUnsignedOnly.rangeUnion(zeroOneSignedOnly), maximal); |
| 99 |
} |