Skip to content

Commit 5265be1

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 Closes #85345
1 parent 6984ba7 commit 5265be1

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5767,11 +5767,16 @@ 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 foldConstant(Instruction::FNeg, Op1, Q);
5779+
}
57755780
}
57765781

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

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,26 +213,23 @@ 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 <2 x float> @src_mul_zero_neg(<2 x float> nofpclass(inf nan pzero psub pnorm) %f) {
224223
; CHECK-LABEL: @src_mul_zero_neg(
225-
; CHECK-NEXT: [[R:%.*]] = fmul <2 x float> zeroinitializer, [[F:%.*]]
226-
; CHECK-NEXT: ret <2 x float> [[R]]
224+
; CHECK-NEXT: ret <2 x float> <float -0.000000e+00, float -0.000000e+00>
227225
;
228226
%r = fmul <2 x float> <float 0.0, float 0.0>, %f
229227
ret <2 x float> %r
230228
}
231229

232230
define <2 x float> @src_mul_zero_and_nzero_neg(<2 x float> nofpclass(inf nan pzero psub pnorm) %f) {
233231
; CHECK-LABEL: @src_mul_zero_and_nzero_neg(
234-
; CHECK-NEXT: [[R:%.*]] = fmul <2 x float> <float -0.000000e+00, float 0.000000e+00>, [[F:%.*]]
235-
; CHECK-NEXT: ret <2 x float> [[R]]
232+
; CHECK-NEXT: ret <2 x float> <float 0.000000e+00, float -0.000000e+00>
236233
;
237234
%r = fmul <2 x float> <float -0.0, float 0.0>, %f
238235
ret <2 x float> %r

0 commit comments

Comments
 (0)