summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/IR/ConstantRangeTest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/unittests/IR/ConstantRangeTest.cpp')
-rw-r--r--llvm/unittests/IR/ConstantRangeTest.cpp139
1 files changed, 139 insertions, 0 deletions
diff --git a/llvm/unittests/IR/ConstantRangeTest.cpp b/llvm/unittests/IR/ConstantRangeTest.cpp
index 0292f60fe33..b7e6235cec3 100644
--- a/llvm/unittests/IR/ConstantRangeTest.cpp
+++ b/llvm/unittests/IR/ConstantRangeTest.cpp
@@ -715,24 +715,102 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) {
}
}
+ for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) {
+ APInt C(4, Const, true /* = isSigned */);
+
+ auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, C, OBO::NoUnsignedWrap);
+
+ EXPECT_FALSE(NUWRegion.isEmptySet());
+
+ auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, C, OBO::NoSignedWrap);
+
+ EXPECT_FALSE(NSWRegion.isEmptySet());
+
+ auto NoWrapRegion = ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, C, OBO::NoSignedWrap | OBO::NoUnsignedWrap);
+
+ EXPECT_FALSE(NoWrapRegion.isEmptySet());
+ EXPECT_TRUE(NUWRegion.intersectWith(NSWRegion).contains(NoWrapRegion));
+
+ for (APInt I = NUWRegion.getLower(), E = NUWRegion.getUpper(); I != E;
+ ++I) {
+ bool Overflow = false;
+ (void)I.usub_ov(C, Overflow);
+ EXPECT_FALSE(Overflow);
+ }
+
+ for (APInt I = NSWRegion.getLower(), E = NSWRegion.getUpper(); I != E;
+ ++I) {
+ bool Overflow = false;
+ (void)I.ssub_ov(C, Overflow);
+ EXPECT_FALSE(Overflow);
+ }
+
+ for (APInt I = NoWrapRegion.getLower(), E = NoWrapRegion.getUpper(); I != E;
+ ++I) {
+ bool Overflow = false;
+
+ (void)I.ssub_ov(C, Overflow);
+ EXPECT_FALSE(Overflow);
+
+ (void)I.usub_ov(C, Overflow);
+ EXPECT_FALSE(Overflow);
+ }
+ }
+
auto NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
OBO::NoSignedWrap);
EXPECT_TRUE(NSWForAllValues.isSingleElement() &&
NSWForAllValues.getSingleElement()->isMinValue());
+ NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, ConstantRange(32, /* isFullSet = */ true),
+ OBO::NoSignedWrap);
+ EXPECT_TRUE(NSWForAllValues.isSingleElement() &&
+ NSWForAllValues.getSingleElement()->isMaxValue());
+
auto NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
OBO::NoUnsignedWrap);
EXPECT_TRUE(NUWForAllValues.isSingleElement() &&
NUWForAllValues.getSingleElement()->isMinValue());
+ NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, ConstantRange(32, /* isFullSet = */ true),
+ OBO::NoUnsignedWrap);
+ EXPECT_TRUE(NUWForAllValues.isSingleElement() &&
+ NUWForAllValues.getSingleElement()->isMaxValue());
+
auto NUWAndNSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::Add, ConstantRange(32, /* isFullSet = */ true),
OBO::NoUnsignedWrap | OBO::NoSignedWrap);
EXPECT_TRUE(NUWAndNSWForAllValues.isSingleElement() &&
NUWAndNSWForAllValues.getSingleElement()->isMinValue());
+ NUWAndNSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, ConstantRange(32, /* isFullSet = */ true),
+ OBO::NoUnsignedWrap | OBO::NoSignedWrap);
+ EXPECT_TRUE(NUWAndNSWForAllValues.isSingleElement() &&
+ NUWAndNSWForAllValues.getSingleElement()->isMaxValue());
+
+ EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Add, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet());
+ EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Add, APInt(32, 0), OBO::NoSignedWrap).isFullSet());
+ EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Add, APInt(32, 0),
+ OBO::NoUnsignedWrap | OBO::NoSignedWrap).isFullSet());
+ EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet());
+ EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, APInt(32, 0), OBO::NoSignedWrap).isFullSet());
+ EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, APInt(32, 0),
+ OBO::NoUnsignedWrap | OBO::NoSignedWrap).isFullSet());
+
ConstantRange OneToFive(APInt(32, 1), APInt(32, 6));
EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::Add, OneToFive, OBO::NoSignedWrap),
@@ -745,6 +823,17 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) {
ConstantRange::makeGuaranteedNoWrapRegion(
Instruction::Add, OneToFive, OBO::NoUnsignedWrap | OBO::NoSignedWrap),
ConstantRange(APInt::getMinValue(32), APInt::getSignedMaxValue(32) - 4));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, OneToFive, OBO::NoSignedWrap),
+ ConstantRange(APInt::getSignedMinValue(32) + 5,
+ APInt::getSignedMinValue(32)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, OneToFive, OBO::NoUnsignedWrap),
+ ConstantRange(APInt::getMinValue(32) + 5, APInt::getMinValue(32)));
+ EXPECT_EQ(
+ ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, OneToFive, OBO::NoUnsignedWrap | OBO::NoSignedWrap),
+ ConstantRange(APInt::getMinValue(32) + 5, APInt::getSignedMinValue(32)));
ConstantRange MinusFiveToMinusTwo(APInt(32, -5), APInt(32, -1));
EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
@@ -758,6 +847,19 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) {
Instruction::Add, MinusFiveToMinusTwo,
OBO::NoUnsignedWrap | OBO::NoSignedWrap),
ConstantRange(APInt(32, 0), APInt(32, 2)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, MinusFiveToMinusTwo, OBO::NoSignedWrap),
+ ConstantRange(APInt::getSignedMinValue(32),
+ APInt::getSignedMaxValue(32) - 4));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, MinusFiveToMinusTwo, OBO::NoUnsignedWrap),
+ ConstantRange(APInt::getMaxValue(32) - 1,
+ APInt::getMinValue(32)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, MinusFiveToMinusTwo,
+ OBO::NoUnsignedWrap | OBO::NoSignedWrap),
+ ConstantRange(APInt::getMaxValue(32) - 1,
+ APInt::getMinValue(32)));
ConstantRange MinusOneToOne(APInt(32, -1), APInt(32, 2));
EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
@@ -771,6 +873,43 @@ TEST(ConstantRange, MakeGuaranteedNoWrapRegion) {
Instruction::Add, MinusOneToOne,
OBO::NoUnsignedWrap | OBO::NoSignedWrap),
ConstantRange(APInt(32, 0), APInt(32, 1)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, MinusOneToOne, OBO::NoSignedWrap),
+ ConstantRange(APInt::getSignedMinValue(32) + 1,
+ APInt::getSignedMinValue(32) - 1));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, MinusOneToOne, OBO::NoUnsignedWrap),
+ ConstantRange(APInt::getMaxValue(32),
+ APInt::getMinValue(32)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, MinusOneToOne,
+ OBO::NoUnsignedWrap | OBO::NoSignedWrap),
+ ConstantRange(APInt::getMaxValue(32),
+ APInt::getMinValue(32)));
+
+ ConstantRange One(APInt(32, 1), APInt(32, 2));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Add, One, OBO::NoSignedWrap),
+ ConstantRange(APInt::getSignedMinValue(32),
+ APInt::getSignedMaxValue(32)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Add, One, OBO::NoUnsignedWrap),
+ ConstantRange(APInt::getMinValue(32), APInt::getMaxValue(32)));
+ EXPECT_EQ(
+ ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Add, One, OBO::NoUnsignedWrap | OBO::NoSignedWrap),
+ ConstantRange(APInt(32, 0), APInt::getSignedMaxValue(32)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, One, OBO::NoSignedWrap),
+ ConstantRange(APInt::getSignedMinValue(32) + 1,
+ APInt::getSignedMinValue(32)));
+ EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, One, OBO::NoUnsignedWrap),
+ ConstantRange(APInt::getMinValue(32) + 1, APInt::getMinValue(32)));
+ EXPECT_EQ(
+ ConstantRange::makeGuaranteedNoWrapRegion(
+ Instruction::Sub, One, OBO::NoUnsignedWrap | OBO::NoSignedWrap),
+ ConstantRange(APInt::getMinValue(32) + 1, APInt::getSignedMinValue(32)));
}
TEST(ConstantRange, GetEquivalentICmp) {
OpenPOWER on IntegriCloud