Skip to content

Commit

Permalink
Teach InstSimplify -X + X --> 0.0 about unary FNeg
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D61916

llvm-svn: 360777
  • Loading branch information
mcinally committed May 15, 2019
1 parent eaf4413 commit 0c82d9b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
14 changes: 10 additions & 4 deletions llvm/lib/Analysis/InstructionSimplify.cpp
Expand Up @@ -4316,16 +4316,22 @@ static Value *SimplifyFAddInst(Value *Op0, Value *Op1, FastMathFlags FMF,
(FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
return Op0;

// With nnan: (+/-0.0 - X) + X --> 0.0 (and commuted variant)
// With nnan: -X + X --> 0.0 (and commuted variant)
// We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
// Negative zeros are allowed because we always end up with positive zero:
// X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
// X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
// X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
// X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
if (FMF.noNaNs() && (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0)))))
return ConstantFP::getNullValue(Op0->getType());
if (FMF.noNaNs()) {
if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
return ConstantFP::getNullValue(Op0->getType());

if (match(Op0, m_FNeg(m_Specific(Op1))) ||
match(Op1, m_FNeg(m_Specific(Op0))))
return ConstantFP::getNullValue(Op0->getType());
}

// (X - Y) + Y --> X
// Y + (X - Y) --> X
Expand Down
26 changes: 22 additions & 4 deletions llvm/test/Transforms/InstSimplify/fast-math.ll
Expand Up @@ -56,26 +56,44 @@ define float @no_mul_zero_3(float %a) {

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

define float @fadd_fnegx(float %x) {
; CHECK-LABEL: @fadd_fnegx(
define float @fadd_binary_fnegx(float %x) {
; CHECK-LABEL: @fadd_binary_fnegx(
; CHECK-NEXT: ret float 0.000000e+00
;
%negx = fsub float -0.0, %x
%r = fadd nnan float %negx, %x
ret float %r
}

define float @fadd_unary_fnegx(float %x) {
; CHECK-LABEL: @fadd_unary_fnegx(
; CHECK-NEXT: ret float 0.000000e+00
;
%negx = fneg float %x
%r = fadd nnan float %negx, %x
ret float %r
}

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

define <2 x float> @fadd_fnegx_commute_vec(<2 x float> %x) {
; CHECK-LABEL: @fadd_fnegx_commute_vec(
define <2 x float> @fadd_binary_fnegx_commute_vec(<2 x float> %x) {
; CHECK-LABEL: @fadd_binary_fnegx_commute_vec(
; CHECK-NEXT: ret <2 x float> zeroinitializer
;
%negx = fsub <2 x float> <float -0.0, float -0.0>, %x
%r = fadd nnan <2 x float> %x, %negx
ret <2 x float> %r
}

define <2 x float> @fadd_unary_fnegx_commute_vec(<2 x float> %x) {
; CHECK-LABEL: @fadd_unary_fnegx_commute_vec(
; CHECK-NEXT: ret <2 x float> zeroinitializer
;
%negx = fneg <2 x float> %x
%r = fadd nnan <2 x float> %x, %negx
ret <2 x float> %r
}

define <2 x float> @fadd_fnegx_commute_vec_undef(<2 x float> %x) {
; CHECK-LABEL: @fadd_fnegx_commute_vec_undef(
; CHECK-NEXT: ret <2 x float> zeroinitializer
Expand Down

0 comments on commit 0c82d9b

Please sign in to comment.