Skip to content

Commit 0c82d9b

Browse files
committedMay 15, 2019
Teach InstSimplify -X + X --> 0.0 about unary FNeg
Differential Revision: https://reviews.llvm.org/D61916 llvm-svn: 360777
1 parent eaf4413 commit 0c82d9b

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed
 

‎llvm/lib/Analysis/InstructionSimplify.cpp

+10-4
Original file line numberDiff line numberDiff line change
@@ -4316,16 +4316,22 @@ static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
43164316
(FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
43174317
return Op0;
43184318

4319-
// With nnan: (+/-0.0 - X) + X --> 0.0 (and commuted variant)
4319+
// With nnan: -X + X --> 0.0 (and commuted variant)
43204320
// We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
43214321
// Negative zeros are allowed because we always end up with positive zero:
43224322
// X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
43234323
// X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
43244324
// X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
43254325
// X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
4326-
if (FMF.noNaNs() && (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
4327-
match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0)))))
4328-
return ConstantFP::getNullValue(Op0->getType());
4326+
if (FMF.noNaNs()) {
4327+
if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
4328+
match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
4329+
return ConstantFP::getNullValue(Op0->getType());
4330+
4331+
if (match(Op0, m_FNeg(m_Specific(Op1))) ||
4332+
match(Op1, m_FNeg(m_Specific(Op0))))
4333+
return ConstantFP::getNullValue(Op0->getType());
4334+
}
43294335

43304336
// (X - Y) + Y --> X
43314337
// Y + (X - Y) --> X

‎llvm/test/Transforms/InstSimplify/fast-math.ll

+22-4
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,44 @@ define float @no_mul_zero_3(float %a) {
5656

5757
; -X + X --> 0.0 (with nnan on the fadd)
5858

59-
define float @fadd_fnegx(float %x) {
60-
; CHECK-LABEL: @fadd_fnegx(
59+
define float @fadd_binary_fnegx(float %x) {
60+
; CHECK-LABEL: @fadd_binary_fnegx(
6161
; CHECK-NEXT: ret float 0.000000e+00
6262
;
6363
%negx = fsub float -0.0, %x
6464
%r = fadd nnan float %negx, %x
6565
ret float %r
6666
}
6767

68+
define float @fadd_unary_fnegx(float %x) {
69+
; CHECK-LABEL: @fadd_unary_fnegx(
70+
; CHECK-NEXT: ret float 0.000000e+00
71+
;
72+
%negx = fneg float %x
73+
%r = fadd nnan float %negx, %x
74+
ret float %r
75+
}
76+
6877
; X + -X --> 0.0 (with nnan on the fadd)
6978

70-
define <2 x float> @fadd_fnegx_commute_vec(<2 x float> %x) {
71-
; CHECK-LABEL: @fadd_fnegx_commute_vec(
79+
define <2 x float> @fadd_binary_fnegx_commute_vec(<2 x float> %x) {
80+
; CHECK-LABEL: @fadd_binary_fnegx_commute_vec(
7281
; CHECK-NEXT: ret <2 x float> zeroinitializer
7382
;
7483
%negx = fsub <2 x float> <float -0.0, float -0.0>, %x
7584
%r = fadd nnan <2 x float> %x, %negx
7685
ret <2 x float> %r
7786
}
7887

88+
define <2 x float> @fadd_unary_fnegx_commute_vec(<2 x float> %x) {
89+
; CHECK-LABEL: @fadd_unary_fnegx_commute_vec(
90+
; CHECK-NEXT: ret <2 x float> zeroinitializer
91+
;
92+
%negx = fneg <2 x float> %x
93+
%r = fadd nnan <2 x float> %x, %negx
94+
ret <2 x float> %r
95+
}
96+
7997
define <2 x float> @fadd_fnegx_commute_vec_undef(<2 x float> %x) {
8098
; CHECK-LABEL: @fadd_fnegx_commute_vec_undef(
8199
; CHECK-NEXT: ret <2 x float> zeroinitializer

0 commit comments

Comments
 (0)
Please sign in to comment.