Skip to content

Commit 61808ef

Browse files
committed
[InstCombine] Canonicalize (uitofp x) -> (sitofp x) if x >= 0
Generally we support more transforms peeking through `sitofp` Proofs: https://alive2.llvm.org/ce/z/9W4VFm
1 parent 039ef79 commit 61808ef

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1929,7 +1929,11 @@ Instruction *InstCombinerImpl::visitFPToSI(FPToSIInst &FI) {
19291929
}
19301930

19311931
Instruction *InstCombinerImpl::visitUIToFP(CastInst &CI) {
1932-
return commonCastTransforms(CI);
1932+
if (Instruction *R = commonCastTransforms(CI))
1933+
return R;
1934+
if (isKnownNonNegative(CI.getOperand(0), SQ))
1935+
return new SIToFPInst(CI.getOperand(0), CI.getType());
1936+
return nullptr;
19331937
}
19341938

19351939
Instruction *InstCombinerImpl::visitSIToFP(CastInst &CI) {

llvm/test/Transforms/InstCombine/add-sitofp.ll

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ define double @test_2_uitofp(i32 %a, i32 %b) {
6767
; CHECK-LABEL: @test_2_uitofp(
6868
; CHECK-NEXT: [[A_AND:%.*]] = and i32 [[A:%.*]], 1073741823
6969
; CHECK-NEXT: [[B_AND:%.*]] = and i32 [[B:%.*]], 1073741823
70-
; CHECK-NEXT: [[A_AND_FP:%.*]] = uitofp i32 [[A_AND]] to double
71-
; CHECK-NEXT: [[B_AND_FP:%.*]] = uitofp i32 [[B_AND]] to double
72-
; CHECK-NEXT: [[RES:%.*]] = fadd double [[A_AND_FP]], [[B_AND_FP]]
70+
; CHECK-NEXT: [[ADDCONV:%.*]] = add nuw nsw i32 [[A_AND]], [[B_AND]]
71+
; CHECK-NEXT: [[RES:%.*]] = sitofp i32 [[ADDCONV]] to double
7372
; CHECK-NEXT: ret double [[RES]]
7473
;
7574
; Drop two highest bits to guarantee that %a + %b doesn't overflow

llvm/test/Transforms/InstCombine/clamp-to-minmax.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ define float @ui32_clamp_and_cast_to_float(i32 %x) {
472472
; CHECK-LABEL: @ui32_clamp_and_cast_to_float(
473473
; CHECK-NEXT: [[LO_CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
474474
; CHECK-NEXT: [[MIN1:%.*]] = call i32 @llvm.umin.i32(i32 [[X]], i32 255)
475-
; CHECK-NEXT: [[MIN:%.*]] = uitofp i32 [[MIN1]] to float
475+
; CHECK-NEXT: [[MIN:%.*]] = sitofp i32 [[MIN1]] to float
476476
; CHECK-NEXT: [[R:%.*]] = select i1 [[LO_CMP]], float 1.000000e+00, float [[MIN]]
477477
; CHECK-NEXT: ret float [[R]]
478478
;
@@ -488,7 +488,7 @@ define float @ui64_clamp_and_cast_to_float(i64 %x) {
488488
; CHECK-LABEL: @ui64_clamp_and_cast_to_float(
489489
; CHECK-NEXT: [[LO_CMP:%.*]] = icmp eq i64 [[X:%.*]], 0
490490
; CHECK-NEXT: [[MIN1:%.*]] = call i64 @llvm.umin.i64(i64 [[X]], i64 255)
491-
; CHECK-NEXT: [[MIN:%.*]] = uitofp i64 [[MIN1]] to float
491+
; CHECK-NEXT: [[MIN:%.*]] = sitofp i64 [[MIN1]] to float
492492
; CHECK-NEXT: [[R:%.*]] = select i1 [[LO_CMP]], float 1.000000e+00, float [[MIN]]
493493
; CHECK-NEXT: ret float [[R]]
494494
;

llvm/test/Transforms/InstCombine/fpcast.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ define half @uint_to_fptrunc(i32 %x) {
266266
define half @masked_uint_to_fptrunc1(i32 %x) {
267267
; CHECK-LABEL: @masked_uint_to_fptrunc1(
268268
; CHECK-NEXT: [[M:%.*]] = and i32 [[X:%.*]], 16777215
269-
; CHECK-NEXT: [[R:%.*]] = uitofp i32 [[M]] to half
269+
; CHECK-NEXT: [[R:%.*]] = sitofp i32 [[M]] to half
270270
; CHECK-NEXT: ret half [[R]]
271271
;
272272
%m = and i32 %x, 16777215
@@ -278,7 +278,7 @@ define half @masked_uint_to_fptrunc1(i32 %x) {
278278
define half @masked_uint_to_fptrunc2(i32 %x) {
279279
; CHECK-LABEL: @masked_uint_to_fptrunc2(
280280
; CHECK-NEXT: [[M:%.*]] = lshr i32 [[X:%.*]], 8
281-
; CHECK-NEXT: [[R:%.*]] = uitofp i32 [[M]] to half
281+
; CHECK-NEXT: [[R:%.*]] = sitofp i32 [[M]] to half
282282
; CHECK-NEXT: ret half [[R]]
283283
;
284284
%m = lshr i32 %x, 8
@@ -290,7 +290,7 @@ define half @masked_uint_to_fptrunc2(i32 %x) {
290290
define half @masked_uint_to_fptrunc3(i32 %x) {
291291
; CHECK-LABEL: @masked_uint_to_fptrunc3(
292292
; CHECK-NEXT: [[M:%.*]] = lshr i32 [[X:%.*]], 7
293-
; CHECK-NEXT: [[F:%.*]] = uitofp i32 [[M]] to float
293+
; CHECK-NEXT: [[F:%.*]] = sitofp i32 [[M]] to float
294294
; CHECK-NEXT: [[R:%.*]] = fptrunc float [[F]] to half
295295
; CHECK-NEXT: ret half [[R]]
296296
;
@@ -314,7 +314,7 @@ define double @uint_to_fpext(i32 %x) {
314314
define double @masked_uint_to_fpext1(i32 %x) {
315315
; CHECK-LABEL: @masked_uint_to_fpext1(
316316
; CHECK-NEXT: [[M:%.*]] = and i32 [[X:%.*]], 16777215
317-
; CHECK-NEXT: [[R:%.*]] = uitofp i32 [[M]] to double
317+
; CHECK-NEXT: [[R:%.*]] = sitofp i32 [[M]] to double
318318
; CHECK-NEXT: ret double [[R]]
319319
;
320320
%m = and i32 %x, 16777215
@@ -326,7 +326,7 @@ define double @masked_uint_to_fpext1(i32 %x) {
326326
define double @masked_uint_to_fpext2(i32 %x) {
327327
; CHECK-LABEL: @masked_uint_to_fpext2(
328328
; CHECK-NEXT: [[M:%.*]] = lshr i32 [[X:%.*]], 8
329-
; CHECK-NEXT: [[R:%.*]] = uitofp i32 [[M]] to double
329+
; CHECK-NEXT: [[R:%.*]] = sitofp i32 [[M]] to double
330330
; CHECK-NEXT: ret double [[R]]
331331
;
332332
%m = lshr i32 %x, 8
@@ -338,7 +338,7 @@ define double @masked_uint_to_fpext2(i32 %x) {
338338
define double @masked_uint_to_fpext3(i32 %x) {
339339
; CHECK-LABEL: @masked_uint_to_fpext3(
340340
; CHECK-NEXT: [[M:%.*]] = lshr i32 [[X:%.*]], 7
341-
; CHECK-NEXT: [[F:%.*]] = uitofp i32 [[M]] to float
341+
; CHECK-NEXT: [[F:%.*]] = sitofp i32 [[M]] to float
342342
; CHECK-NEXT: [[R:%.*]] = fpext float [[F]] to double
343343
; CHECK-NEXT: ret double [[R]]
344344
;

llvm/test/Transforms/InstCombine/sitofp.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ define i25 @consider_lowbits_masked_input(i25 %A) {
256256
define i32 @overflow_masked_input(i32 %A) {
257257
; CHECK-LABEL: @overflow_masked_input(
258258
; CHECK-NEXT: [[M:%.*]] = and i32 [[A:%.*]], 16777217
259-
; CHECK-NEXT: [[B:%.*]] = uitofp i32 [[M]] to float
259+
; CHECK-NEXT: [[B:%.*]] = sitofp i32 [[M]] to float
260260
; CHECK-NEXT: [[C:%.*]] = fptoui float [[B]] to i32
261261
; CHECK-NEXT: ret i32 [[C]]
262262
;

0 commit comments

Comments
 (0)