Skip to content

Commit 08200d6

Browse files
committed
[InstCombine] Handle -(X-Y) --> (Y-X) for unary fneg when NSZ
Differential Revision: https://reviews.llvm.org/D62612 llvm-svn: 363082
1 parent 5058ae0 commit 08200d6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1857,13 +1857,22 @@ static Instruction *foldFNegIntoConstant(Instruction &I) {
18571857
}
18581858

18591859
Instruction *InstCombiner::visitFNeg(UnaryOperator &I) {
1860-
if (Value *V = SimplifyFNegInst(I.getOperand(0), I.getFastMathFlags(),
1860+
Value *Op = I.getOperand(0);
1861+
1862+
if (Value *V = SimplifyFNegInst(Op, I.getFastMathFlags(),
18611863
SQ.getWithInstruction(&I)))
18621864
return replaceInstUsesWith(I, V);
18631865

18641866
if (Instruction *X = foldFNegIntoConstant(I))
18651867
return X;
18661868

1869+
Value *X, *Y;
1870+
1871+
// If we can ignore the sign of zeros: -(X - Y) --> (Y - X)
1872+
if (I.hasNoSignedZeros() &&
1873+
match(Op, m_OneUse(m_FSub(m_Value(X), m_Value(Y)))))
1874+
return BinaryOperator::CreateFSubFMF(Y, X, &I);
1875+
18671876
return nullptr;
18681877
}
18691878

llvm/test/Transforms/InstCombine/fsub.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ define float @neg_sub_nsz(float %x, float %y) {
3838
ret float %t2
3939
}
4040

41-
; FIXME: This combine isn't working.
4241
define float @unary_neg_sub_nsz(float %x, float %y) {
4342
; CHECK-LABEL: @unary_neg_sub_nsz(
44-
; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
45-
; CHECK-NEXT: [[T2:%.*]] = fneg nsz float [[T1]]
43+
; CHECK-NEXT: [[T2:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
4644
; CHECK-NEXT: ret float [[T2]]
4745
;
4846
%t1 = fsub float %x, %y

0 commit comments

Comments
 (0)