Skip to content

Commit 02a8a2e

Browse files
committed
fixup! [ConstantRange] Improve ConstantRange::binaryXor
Address review comments.
1 parent 3909948 commit 02a8a2e

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

llvm/lib/IR/ConstantRange.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1478,16 +1478,10 @@ ConstantRange ConstantRange::binaryXor(const ConstantRange &Other) const {
14781478
// If LHS is known to be the subset of RHS, treat LHS ^ RHS as RHS -nuw/nsw
14791479
// LHS. If RHS is known to be the subset of LHS, treat LHS ^ RHS as LHS
14801480
// -nuw/nsw RHS.
1481-
if (LHSKnown.getMaxValue().isSubsetOf(RHSKnown.getMinValue()))
1482-
CR = CR.intersectWith(
1483-
Other.subWithNoWrap(*this, OverflowingBinaryOperator::NoUnsignedWrap |
1484-
OverflowingBinaryOperator::NoSignedWrap),
1485-
PreferredRangeType::Unsigned);
1486-
else if (RHSKnown.getMaxValue().isSubsetOf(LHSKnown.getMinValue()))
1487-
CR = CR.intersectWith(
1488-
subWithNoWrap(Other, OverflowingBinaryOperator::NoUnsignedWrap |
1489-
OverflowingBinaryOperator::NoSignedWrap),
1490-
PreferredRangeType::Unsigned);
1481+
if ((~LHSKnown.Zero).isSubsetOf(RHSKnown.One))
1482+
CR = CR.intersectWith(Other.sub(*this), PreferredRangeType::Unsigned);
1483+
else if ((~RHSKnown.Zero).isSubsetOf(LHSKnown.One))
1484+
CR = CR.intersectWith(this->sub(Other), PreferredRangeType::Unsigned);
14911485
return CR;
14921486
}
14931487

llvm/unittests/IR/ConstantRangeTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,6 +2565,12 @@ TEST_F(ConstantRangeTest, binaryXor) {
25652565
EXPECT_EQ(R16_35.binaryXor(R0_99), ConstantRange(APInt(8, 0), APInt(8, 128)));
25662566
EXPECT_EQ(R0_99.binaryXor(R16_35), ConstantRange(APInt(8, 0), APInt(8, 128)));
25672567

2568+
// Treat xor A, B as sub nsw nuw A, B
2569+
ConstantRange R0_51(APInt(8, 0), APInt(8, 51));
2570+
ConstantRange R63(APInt(8, 63));
2571+
EXPECT_EQ(R0_51.binaryXor(R63), ConstantRange(APInt(8, 13), APInt(8, 64)));
2572+
EXPECT_EQ(R63.binaryXor(R0_51), ConstantRange(APInt(8, 13), APInt(8, 64)));
2573+
25682574
TestBinaryOpExhaustive(
25692575
[](const ConstantRange &CR1, const ConstantRange &CR2) {
25702576
return CR1.binaryXor(CR2);

0 commit comments

Comments
 (0)