Skip to content

Commit c25878a

Browse files
committed
[Float2Int] Make sure the CFP can be represented in the integer type
1 parent d0e02a9 commit c25878a

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

llvm/lib/Transforms/Scalar/Float2Int.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,14 @@ std::optional<ConstantRange> Float2IntPass::calcRange(Instruction *I) {
237237
// OK, it's representable. Now get it.
238238
APSInt Int(MaxIntegerBW+1, false);
239239
bool Exact;
240-
CF->getValueAPF().convertToInteger(Int,
241-
APFloat::rmNearestTiesToEven,
242-
&Exact);
243-
OpRanges.push_back(ConstantRange(Int));
240+
APFloat::opStatus Status = CF->getValueAPF().convertToInteger(
241+
Int, APFloat::rmNearestTiesToEven, &Exact);
242+
// Although the round above is loseless, we still need to check if the
243+
// floating-point value can be represented in the integer type.
244+
if (Status == APFloat::opOK || Status == APFloat::opInexact)
245+
OpRanges.push_back(ConstantRange(Int));
246+
else
247+
return badRange();
244248
} else {
245249
llvm_unreachable("Should have already marked this as badRange!");
246250
}

llvm/test/Transforms/Float2Int/pr167627.ll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
define i1 @pr167627() {
88
; CHECK-LABEL: define i1 @pr167627() {
99
; CHECK-NEXT: [[ENTRY:.*:]]
10-
; CHECK-NEXT: ret i1 false
10+
; CHECK-NEXT: [[FADD:%.*]] = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000
11+
; CHECK-NEXT: [[CMP:%.*]] = fcmp one float [[FADD]], 0.000000e+00
12+
; CHECK-NEXT: ret i1 [[CMP]]
1113
;
1214
entry:
1315
%fadd = fadd float 0xC5AAD8ABE0000000, 0xC57E819700000000

0 commit comments

Comments
 (0)