Skip to content

Commit 929941c

Browse files
committed
[InstCombine] Fold fpto{s|u}i non-norm to zero
1 parent 077ca13 commit 929941c

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1923,17 +1923,36 @@ Instruction *InstCombinerImpl::foldItoFPtoI(CastInst &FI) {
19231923
return replaceInstUsesWith(FI, X);
19241924
}
19251925

1926+
static Instruction *foldFPtoI(Instruction &FI, InstCombiner &IC) {
1927+
// fpto{u/s}i non-norm --> 0
1928+
FPClassTest Mask =
1929+
FI.getOpcode() == Instruction::FPToUI ? fcPosNormal : fcNormal;
1930+
KnownFPClass FPClass =
1931+
computeKnownFPClass(FI.getOperand(0), Mask, /*Depth=*/0,
1932+
IC.getSimplifyQuery().getWithInstruction(&FI));
1933+
if (FPClass.isKnownNever(Mask))
1934+
return IC.replaceInstUsesWith(FI, ConstantInt::getNullValue(FI.getType()));
1935+
1936+
return nullptr;
1937+
}
1938+
19261939
Instruction *InstCombinerImpl::visitFPToUI(FPToUIInst &FI) {
19271940
if (Instruction *I = foldItoFPtoI(FI))
19281941
return I;
19291942

1943+
if (Instruction *I = foldFPtoI(FI, *this))
1944+
return I;
1945+
19301946
return commonCastTransforms(FI);
19311947
}
19321948

19331949
Instruction *InstCombinerImpl::visitFPToSI(FPToSIInst &FI) {
19341950
if (Instruction *I = foldItoFPtoI(FI))
19351951
return I;
19361952

1953+
if (Instruction *I = foldFPtoI(FI, *this))
1954+
return I;
1955+
19371956
return commonCastTransforms(FI);
19381957
}
19391958

llvm/test/Transforms/InstCombine/fpcast.ll

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -350,27 +350,23 @@ define double @masked_uint_to_fpext3(i32 %x) {
350350

351351
define i32 @fptosi_nonnorm(float nofpclass(norm) %x) {
352352
; CHECK-LABEL: @fptosi_nonnorm(
353-
; CHECK-NEXT: [[RET:%.*]] = fptosi float [[X:%.*]] to i32
354-
; CHECK-NEXT: ret i32 [[RET]]
353+
; CHECK-NEXT: ret i32 0
355354
;
356355
%ret = fptosi float %x to i32
357356
ret i32 %ret
358357
}
359358

360359
define i32 @fptoui_nonnorm(float nofpclass(pnorm) %x) {
361360
; CHECK-LABEL: @fptoui_nonnorm(
362-
; CHECK-NEXT: [[RET:%.*]] = fptoui float [[X:%.*]] to i32
363-
; CHECK-NEXT: ret i32 [[RET]]
361+
; CHECK-NEXT: ret i32 0
364362
;
365363
%ret = fptoui float %x to i32
366364
ret i32 %ret
367365
}
368366

369367
define i32 @fptosi_nonnorm_copysign(float %x) {
370368
; CHECK-LABEL: @fptosi_nonnorm_copysign(
371-
; CHECK-NEXT: [[VAL:%.*]] = call float @llvm.copysign.f32(float 0.000000e+00, float [[X:%.*]])
372-
; CHECK-NEXT: [[RET:%.*]] = fptosi float [[VAL]] to i32
373-
; CHECK-NEXT: ret i32 [[RET]]
369+
; CHECK-NEXT: ret i32 0
374370
;
375371
%val = call float @llvm.copysign.f32(float 0.0, float %x)
376372
%ret = fptosi float %val to i32

0 commit comments

Comments
 (0)