[PATCH] InstCombine: fsub nsz 0, X ==> fsub nsz -0.0, X

Sanjay Patel spatel at rotateright.com
Wed Dec 31 14:15:17 PST 2014


REPOSITORY
  rL LLVM

http://reviews.llvm.org/D6731

Files:
  llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
  llvm/trunk/test/Transforms/InstCombine/fast-math.ll

Index: llvm/trunk/test/Transforms/InstCombine/fast-math.ll
===================================================================
--- llvm/trunk/test/Transforms/InstCombine/fast-math.ll
+++ llvm/trunk/test/Transforms/InstCombine/fast-math.ll
@@ -322,6 +322,14 @@
 ; 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
Index: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1713,6 +1713,14 @@
                                   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))

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D6731.17752.patch
Type: text/x-patch
Size: 1391 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20141231/44fbed33/attachment.bin>


More information about the llvm-commits mailing list