Skip to content

Commit

Permalink
[InstCombine] Handle -(X-Y) --> (Y-X) for unary fneg when NSZ
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D62612

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363082 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
mcinally committed Jun 11, 2019
1 parent 1e4ae05 commit 46cf29e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/Transforms/InstCombine/InstCombineAddSub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1857,13 +1857,22 @@ static Instruction *foldFNegIntoConstant(Instruction &I) {
}

Instruction *InstCombiner::visitFNeg(UnaryOperator &I) {
if (Value *V = SimplifyFNegInst(I.getOperand(0), I.getFastMathFlags(),
Value *Op = I.getOperand(0);

if (Value *V = SimplifyFNegInst(Op, I.getFastMathFlags(),
SQ.getWithInstruction(&I)))
return replaceInstUsesWith(I, V);

if (Instruction *X = foldFNegIntoConstant(I))
return X;

Value *X, *Y;

// If we can ignore the sign of zeros: -(X - Y) --> (Y - X)
if (I.hasNoSignedZeros() &&
match(Op, m_OneUse(m_FSub(m_Value(X), m_Value(Y)))))
return BinaryOperator::CreateFSubFMF(Y, X, &I);

return nullptr;
}

Expand Down
4 changes: 1 addition & 3 deletions test/Transforms/InstCombine/fsub.ll
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,9 @@ define float @neg_sub_nsz(float %x, float %y) {
ret float %t2
}

; FIXME: This combine isn't working.
define float @unary_neg_sub_nsz(float %x, float %y) {
; CHECK-LABEL: @unary_neg_sub_nsz(
; CHECK-NEXT: [[T1:%.*]] = fsub float [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[T2:%.*]] = fneg nsz float [[T1]]
; CHECK-NEXT: [[T2:%.*]] = fsub nsz float [[Y:%.*]], [[X:%.*]]
; CHECK-NEXT: ret float [[T2]]
;
%t1 = fsub float %x, %y
Expand Down

0 comments on commit 46cf29e

Please sign in to comment.