Skip to content

Commit 442fe33

Browse files
committed
[InstCombine] Fold fpto{s|u}i non-norm to zero
1 parent b291c49 commit 442fe33

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
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: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -350,17 +350,15 @@ 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
@@ -386,9 +384,7 @@ define i32 @fptoui_nonnnorm(float nofpclass(nnorm) %x) {
386384

387385
define i32 @fptosi_nonnorm_copysign(float %x) {
388386
; CHECK-LABEL: @fptosi_nonnorm_copysign(
389-
; CHECK-NEXT: [[VAL:%.*]] = call float @llvm.copysign.f32(float 0.000000e+00, float [[X:%.*]])
390-
; CHECK-NEXT: [[RET:%.*]] = fptosi float [[VAL]] to i32
391-
; CHECK-NEXT: ret i32 [[RET]]
387+
; CHECK-NEXT: ret i32 0
392388
;
393389
%val = call float @llvm.copysign.f32(float 0.0, float %x)
394390
%ret = fptosi float %val to i32
@@ -397,9 +393,7 @@ define i32 @fptosi_nonnorm_copysign(float %x) {
397393

398394
define <2 x i32> @fptosi_nonnorm_copysign_vec(<2 x float> %x) {
399395
; CHECK-LABEL: @fptosi_nonnorm_copysign_vec(
400-
; CHECK-NEXT: [[VAL:%.*]] = call <2 x float> @llvm.copysign.v2f32(<2 x float> zeroinitializer, <2 x float> [[X:%.*]])
401-
; CHECK-NEXT: [[RET:%.*]] = fptosi <2 x float> [[VAL]] to <2 x i32>
402-
; CHECK-NEXT: ret <2 x i32> [[RET]]
396+
; CHECK-NEXT: ret <2 x i32> zeroinitializer
403397
;
404398
%val = call <2 x float> @llvm.copysign.v2f32(<2 x float> zeroinitializer, <2 x float> %x)
405399
%ret = fptosi <2 x float> %val to <2 x i32>

0 commit comments

Comments
 (0)