InstCombine: fsub nsz 0, X ==> fsub nsz -0.0, X
authorSanjay Patel <spatel@rotateright.com>
Wed, 31 Dec 2014 22:14:05 +0000 (22:14 +0000)
committerSanjay Patel <spatel@rotateright.com>
Wed, 31 Dec 2014 22:14:05 +0000 (22:14 +0000)
Some day the backend may handle instruction-level fast math flags and make
this transform unnecessary, but it's still better practice to use the canonical
representation of fneg when possible (use a -0.0).

This is a partial fix for PR20870 ( http://llvm.org/bugs/show_bug.cgi?id=20870 ).
See also http://reviews.llvm.org/D6723.

Differential Revision: http://reviews.llvm.org/D6731

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

lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/fast-math.ll

index 9ea4bc57cf29bd44d1f8dc2d1e8afa1f33e54c95..7e6ead4190cc9928b68c5d80476e6c5f0fa2cf98 100644 (file)
@@ -1713,6 +1713,14 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
                                   TLI, DT, AT))
     return ReplaceInstUsesWith(I, V);
 
+  // fsub nsz 0, X ==> fsub nsz -0.0, X
+  if (I.getFastMathFlags().noSignedZeros() && match(Op0, m_Zero())) {
+    // Subtraction from -0.0 is the canonical form of fneg.
+    Instruction *NewI = BinaryOperator::CreateFNeg(Op1);
+    NewI->copyFastMathFlags(&I);
+    return NewI;
+  }
+
   if (isa<Constant>(Op0))
     if (SelectInst *SI = dyn_cast<SelectInst>(Op1))
       if (Instruction *NV = FoldOpIntoSelect(I, SI))
index f39adba656015a45b4ad05731fb554d5f806ee56..c6081c399253297b158ae149ebe04d58eb1d1840 100644 (file)
@@ -322,6 +322,14 @@ define float @fneg1(float %f1, float %f2) {
 ; CHECK: fmul float %f1, %f2
 }
 
+define float @fneg2(float %x) {
+  %sub = fsub nsz float 0.0, %x
+  ret float %sub
+; CHECK-LABEL: @fneg2(
+; CHECK-NEXT: fsub nsz float -0.000000e+00, %x
+; CHECK-NEXT: ret float 
+}
+
 ; =========================================================================
 ;
 ;   Testing-cases about div