Skip to content

Commit

Permalink
[InstSimplify] fsub 0.0, (fsub -0.0, X) ==> X is only safe if signed …
Browse files Browse the repository at this point in the history
…zeros are ignored.

Only allow fsub -0.0, (fsub -0.0, X) ==> X without nsz. PR26746.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@262212 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
d0k committed Feb 29, 2016
1 parent 0562fb3 commit e8af9d9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
15 changes: 8 additions & 7 deletions lib/Analysis/InstructionSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -833,14 +833,15 @@ static Value *SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
(FMF.noSignedZeros() || CannotBeNegativeZero(Op0)))
return Op0;

// fsub 0, (fsub -0.0, X) ==> X
// fsub -0.0, (fsub -0.0, X) ==> X
Value *X;
if (match(Op0, m_AnyZero())) {
if (match(Op1, m_FSub(m_NegZero(), m_Value(X))))
return X;
if (FMF.noSignedZeros() && match(Op1, m_FSub(m_AnyZero(), m_Value(X))))
return X;
}
if (match(Op0, m_NegZero()) && match(Op1, m_FSub(m_NegZero(), m_Value(X))))
return X;

// fsub 0.0, (fsub 0.0, X) ==> X if signed zeros are ignored.
if (FMF.noSignedZeros() && match(Op0, m_NegZero()) &&
match(Op1, m_FSub(m_AnyZero(), m_Value(X))))
return X;

// fsub nnan x, x ==> 0.0
if (FMF.noNaNs() && Op0 == Op1)
Expand Down
26 changes: 23 additions & 3 deletions test/Transforms/InstSimplify/floating-point-arithmetic.ll
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
; RUN: opt < %s -instsimplify -S | FileCheck %s

; fsub 0, (fsub 0, X) ==> X
; CHECK-LABEL: @fsub_0_0_x(
define float @fsub_0_0_x(float %a) {
; fsub -0.0, (fsub -0.0, X) ==> X
; CHECK-LABEL: @fsub_-0_-0_x(
define float @fsub_-0_-0_x(float %a) {
%t1 = fsub float -0.0, %a
%ret = fsub float -0.0, %t1

; CHECK: ret float %a
ret float %ret
}

; fsub 0.0, (fsub -0.0, X) != X
; CHECK-LABEL: @fsub_0_-0_x(
define float @fsub_0_-0_x(float %a) {
%t1 = fsub float 0.0, %a
%ret = fsub float -0.0, %t1

; CHECK-NOT: ret float %a
ret float %ret
}

; fsub -0.0, (fsub 0.0, X) != X
; CHECK-LABEL: @fsub_-0_0_x(
define float @fsub_-0_0_x(float %a) {
%t1 = fsub float -0.0, %a
%ret = fsub float 0.0, %t1

; CHECK-NOT: ret float %a
ret float %ret
}

; fsub X, 0 ==> X
; CHECK-LABEL: @fsub_x_0(
define float @fsub_x_0(float %a) {
Expand Down

0 comments on commit e8af9d9

Please sign in to comment.