Skip to content

Commit ac0ae7c

Browse files
toppercAZero13
authored andcommitted
[ConstantRange] Fix off by 1 bugs in UIToFP and SIToFP handling. (llvm#86041)
We were passing the min and max values of the range to the ConstantRange constructor, but the constructor expects the upper bound to 1 more than the max value so we need to add 1. We also need to use getNonEmpty so that passing 0, 0 to the constructor creates a full range rather than an empty range. And passing smin, smax+1 doesn't cause an assertion. I believe this fixes at least some of the reason llvm#79158 was reverted.
1 parent 5a59b63 commit ac0ae7c

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

llvm/lib/IR/ConstantRange.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
742742
Min = Min.zext(ResultBitWidth);
743743
Max = Max.zext(ResultBitWidth);
744744
}
745-
return ConstantRange(std::move(Min), std::move(Max));
745+
return getNonEmpty(std::move(Min), std::move(Max) + 1);
746746
}
747747
case Instruction::SIToFP: {
748748
// TODO: use input range if available
@@ -753,7 +753,7 @@ ConstantRange ConstantRange::castOp(Instruction::CastOps CastOp,
753753
SMin = SMin.sext(ResultBitWidth);
754754
SMax = SMax.sext(ResultBitWidth);
755755
}
756-
return ConstantRange(std::move(SMin), std::move(SMax));
756+
return getNonEmpty(std::move(SMin), std::move(SMax) + 1);
757757
}
758758
case Instruction::FPTrunc:
759759
case Instruction::FPExt:

llvm/test/Transforms/Float2Int/pr79158.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ define i32 @pr79158(i32 %x) {
66
; CHECK-SAME: i32 [[X:%.*]]) {
77
; CHECK-NEXT: entry:
88
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X]], 0
9-
; CHECK-NEXT: [[TMP0:%.*]] = zext i1 [[CMP]] to i32
10-
; CHECK-NEXT: [[MUL1:%.*]] = mul i32 [[TMP0]], 2147483647
11-
; CHECK-NEXT: ret i32 [[MUL1]]
9+
; CHECK-NEXT: [[TMP0:%.*]] = zext i1 [[CMP]] to i64
10+
; CHECK-NEXT: [[MUL1:%.*]] = mul i64 [[TMP0]], 4294967295
11+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i64 [[MUL1]] to i32
12+
; CHECK-NEXT: ret i32 [[TMP1]]
1213
;
1314
entry:
1415
%cmp = icmp sgt i32 %x, 0

llvm/unittests/IR/ConstantRangeTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,24 @@ TEST_F(ConstantRangeTest, castOps) {
24522452
ConstantRange IntToPtr = A.castOp(Instruction::IntToPtr, 64);
24532453
EXPECT_EQ(64u, IntToPtr.getBitWidth());
24542454
EXPECT_TRUE(IntToPtr.isFullSet());
2455+
2456+
ConstantRange UIToFP = A.castOp(Instruction::UIToFP, 16);
2457+
EXPECT_EQ(16u, UIToFP.getBitWidth());
2458+
EXPECT_TRUE(UIToFP.isFullSet());
2459+
2460+
ConstantRange UIToFP2 = A.castOp(Instruction::UIToFP, 64);
2461+
ConstantRange B(APInt(64, 0), APInt(64, 65536));
2462+
EXPECT_EQ(64u, UIToFP2.getBitWidth());
2463+
EXPECT_EQ(B, UIToFP2);
2464+
2465+
ConstantRange SIToFP = A.castOp(Instruction::SIToFP, 16);
2466+
EXPECT_EQ(16u, SIToFP.getBitWidth());
2467+
EXPECT_TRUE(SIToFP.isFullSet());
2468+
2469+
ConstantRange SIToFP2 = A.castOp(Instruction::SIToFP, 64);
2470+
ConstantRange C(APInt(64, -32768), APInt(64, 32768));
2471+
EXPECT_EQ(64u, SIToFP2.getBitWidth());
2472+
EXPECT_EQ(C, SIToFP2);
24552473
}
24562474

24572475
TEST_F(ConstantRangeTest, binaryAnd) {

0 commit comments

Comments
 (0)