Skip to content

Commit 6f53eaa

Browse files
committed
[InstSimply] Simplify (fmul -x, +/-0) -> -/+0
We already handle the `+x` case, and noticed it was missing in the bug affecting #82555 Proofs: https://alive2.llvm.org/ce/z/WUSvmV
1 parent b7244bc commit 6f53eaa

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5767,11 +5767,18 @@ static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
57675767
if (FMF.noNaNs() && FMF.noSignedZeros())
57685768
return ConstantFP::getZero(Op0->getType());
57695769

5770-
// +normal number * (-)0.0 --> (-)0.0
57715770
KnownFPClass Known =
57725771
computeKnownFPClass(Op0, FMF, fcInf | fcNan, /*Depth=*/0, Q);
5773-
if (Known.SignBit == false && Known.isKnownNever(fcInf | fcNan))
5774-
return Op1;
5772+
if (Known.isKnownNever(fcInf | fcNan)) {
5773+
// +normal number * (-)0.0 --> (-)0.0
5774+
if (Known.SignBit == false)
5775+
return Op1;
5776+
// -normal number * (-)0.0 --> -(-)0.0
5777+
if (Known.SignBit == true)
5778+
return match(Op1, m_PosZeroFP())
5779+
? ConstantFP::getNegativeZero(Op0->getType())
5780+
: ConstantFP::getZero(Op0->getType());
5781+
}
57755782
}
57765783

57775784
// sqrt(X) * sqrt(X) --> X, if we can:

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,15 @@ define double @fmul_nnan_ninf_nneg_n0.0_commute(i127 %x) {
213213

214214
define float @src_mul_nzero_neg(float nofpclass(inf nan pzero psub pnorm) %f) {
215215
; CHECK-LABEL: @src_mul_nzero_neg(
216-
; CHECK-NEXT: [[R:%.*]] = fmul float [[F:%.*]], -0.000000e+00
217-
; CHECK-NEXT: ret float [[R]]
216+
; CHECK-NEXT: ret float 0.000000e+00
218217
;
219218
%r = fmul float %f, -0.0
220219
ret float %r
221220
}
222221

223222
define float @src_mul_zero_neg(float nofpclass(inf nan pzero psub pnorm) %f) {
224223
; CHECK-LABEL: @src_mul_zero_neg(
225-
; CHECK-NEXT: [[R:%.*]] = fmul float [[F:%.*]], 0.000000e+00
226-
; CHECK-NEXT: ret float [[R]]
224+
; CHECK-NEXT: ret float -0.000000e+00
227225
;
228226
%r = fmul float %f, 0.0
229227
ret float %r

0 commit comments

Comments
 (0)