Skip to content

Commit f71e4e9

Browse files
authored
[InstSimplify] Handle nsz when simplifying X * 0.0 (#142181)
If ValueTracking can guarantee non-NaN and non-INF and the `nsz` fast-math flag is set, we can simplify X * 0.0 ==> 0.0. https://alive2.llvm.org/ce/z/XacRQZ
1 parent 11f915f commit f71e4e9

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5844,6 +5844,9 @@ static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
58445844
KnownFPClass Known =
58455845
computeKnownFPClass(Op0, FMF, fcInf | fcNan, /*Depth=*/0, Q);
58465846
if (Known.isKnownNever(fcInf | fcNan)) {
5847+
// if nsz is set, return 0.0
5848+
if (FMF.noSignedZeros())
5849+
return ConstantFP::getZero(Op0->getType());
58475850
// +normal number * (-)0.0 --> (-)0.0
58485851
if (Known.SignBit == false)
58495852
return Op1;

llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,22 @@ define double @fmul_nnan_ninf_nneg_n0.0_commute(i127 %x) {
211211
ret double %r
212212
}
213213

214+
define float @fmul_ninf_nnan_mul_zero_nsz(float nofpclass(inf nan) %f) {
215+
; CHECK-LABEL: @fmul_ninf_nnan_mul_zero_nsz(
216+
; CHECK-NEXT: ret float 0.000000e+00
217+
;
218+
%r = fmul nsz float %f, 0.0
219+
ret float %r
220+
}
221+
222+
define float @fmul_ninf_nnan_mul_nzero_nsz(float nofpclass(inf nan) %f) {
223+
; CHECK-LABEL: @fmul_ninf_nnan_mul_nzero_nsz(
224+
; CHECK-NEXT: ret float 0.000000e+00
225+
;
226+
%r = fmul nsz float %f, -0.0
227+
ret float %r
228+
}
229+
214230
define float @src_mul_nzero_neg(float nofpclass(inf nan pzero psub pnorm) %f) {
215231
; CHECK-LABEL: @src_mul_nzero_neg(
216232
; CHECK-NEXT: ret float 0.000000e+00

0 commit comments

Comments
 (0)