Preserve fast-math flags when folding (fsub x, (fneg y)) to (fadd x, y).
authorOwen Anderson <resistor@mac.com>
Tue, 30 Jul 2013 23:53:17 +0000 (23:53 +0000)
committerOwen Anderson <resistor@mac.com>
Tue, 30 Jul 2013 23:53:17 +0000 (23:53 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@187462 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineAddSub.cpp
test/Transforms/InstCombine/fneg-ext.ll

index 1fa6897ddd2c48f796c4a03a9fa05a9559174c1c..534feb8fad21c316ea784b1688dc2c3979b8e92f 100644 (file)
@@ -1530,17 +1530,24 @@ Instruction *InstCombiner::visitFSub(BinaryOperator &I) {
 
   // If this is a 'B = x-(-A)', change to B = x+A, potentially looking
   // through FP extensions/truncations along the way.
-  if (Value *V = dyn_castFNegVal(Op1))
-    return BinaryOperator::CreateFAdd(Op0, V);
+  if (Value *V = dyn_castFNegVal(Op1)) {
+    Instruction *NewI = BinaryOperator::CreateFAdd(Op0, V);
+    NewI->copyFastMathFlags(&I);
+    return NewI;
+  }
   if (FPTruncInst *FPTI = dyn_cast<FPTruncInst>(Op1)) {
     if (Value *V = dyn_castFNegVal(FPTI->getOperand(0))) {
       Value *NewTrunc = Builder->CreateFPTrunc(V, I.getType());
-      return BinaryOperator::CreateFAdd(Op0, NewTrunc);
+      Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewTrunc);
+      NewI->copyFastMathFlags(&I);
+      return NewI;
     }
   } else if (FPExtInst *FPEI = dyn_cast<FPExtInst>(Op1)) {
     if (Value *V = dyn_castFNegVal(FPEI->getOperand(0))) {
       Value *NewExt = Builder->CreateFPExt(V, I.getType());
-      return BinaryOperator::CreateFAdd(Op0, NewExt);
+      Instruction *NewI = BinaryOperator::CreateFAdd(Op0, NewExt);
+      NewI->copyFastMathFlags(&I);
+      return NewI;
     }
   }
 
index 49ad2325261bb913ad5bd6c382c5412c7d744beb..922d26a465b71d39e93ed5782a53411a98e6d33e 100644 (file)
@@ -10,3 +10,14 @@ define double @test1(float %a, double %b) nounwind readnone ssp uwtable {
   %3 = fsub double %b, %2
   ret double %3
 }
+
+; CHECK: test2
+define double @test2(float %a, double %b) nounwind readnone ssp uwtable {
+; CHECK-NOT: fsub
+; CHECK: fpext
+; CHECK: fadd fast
+  %1 = fsub float -0.000000e+00, %a
+  %2 = fpext float %1 to double
+  %3 = fsub fast double %b, %2
+  ret double %3
+}