]> granicus.if.org Git - llvm/commitdiff
[InstCombine] Handle -(X-Y) --> (Y-X) for unary fneg when NSZ
authorCameron McInally <cameron.mcinally@nyu.edu>
Tue, 11 Jun 2019 16:21:21 +0000 (16:21 +0000)
committerCameron McInally <cameron.mcinally@nyu.edu>
Tue, 11 Jun 2019 16:21:21 +0000 (16:21 +0000)
Differential Revision: https://reviews.llvm.org/D62612

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363082 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/fsub.ll

index e6b32ba13a4a422d2a77946cfe55ec5b6f440386..f71d23974bae679fec49dc70f51b3f7fb3dacb69 100644 (file)
@@ -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;
 }
 
index ea9f9e54a94626ab237fdb87b05cd685b8331af0..903f9d9797eb372241c240147e42370ec3e6c20a 100644 (file)
@@ -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