[llvm] r187249 - When InstCombine tries to fold away (fsub x, (fneg y)) into (fadd x, y), it is

Owen Anderson resistor at mac.com
Fri Jul 26 14:40:29 PDT 2013


Author: resistor
Date: Fri Jul 26 16:40:29 2013
New Revision: 187249

URL: http://llvm.org/viewvc/llvm-project?rev=187249&view=rev
Log:
When InstCombine tries to fold away (fsub x, (fneg y)) into (fadd x, y), it is
also worthwhile for it to look through FP extensions and truncations, whose
application commutes with fneg.

Added:
    llvm/trunk/test/Transforms/InstCombine/fneg-ext.ll
Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp?rev=187249&r1=187248&r2=187249&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineAddSub.cpp Fri Jul 26 16:40:29 2013
@@ -1528,9 +1528,21 @@ Instruction *InstCombiner::visitFSub(Bin
       if (Instruction *NV = FoldOpIntoSelect(I, SI))
         return NV;
 
-  // If this is a 'B = x-(-A)', change to B = x+A...
+  // 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 (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);
+    }
+  } else if (FPExtInst *FPEI = dyn_cast<FPExtInst>(Op1)) {
+    if (Value *V = dyn_castFNegVal(FPEI->getOperand(0))) {
+      Value *NewTrunc = Builder->CreateFPExt(V, I.getType());
+      return BinaryOperator::CreateFAdd(Op0, NewTrunc);
+    }
+  }
 
   if (I.hasUnsafeAlgebra()) {
     if (Value *V = FAddCombine(Builder).simplify(&I))

Added: llvm/trunk/test/Transforms/InstCombine/fneg-ext.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fneg-ext.ll?rev=187249&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fneg-ext.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/fneg-ext.ll Fri Jul 26 16:40:29 2013
@@ -0,0 +1,12 @@
+; RUN: opt -instcombine -S < %s | FileCheck %s
+
+; CHECK: test1
+define double @test1(float %a, double %b) nounwind readnone ssp uwtable {
+; CHECK-NOT: fsub
+; CHECK: fpext
+; CHECK: fadd
+  %1 = fsub float -0.000000e+00, %a
+  %2 = fpext float %1 to double
+  %3 = fsub double %b, %2
+  ret double %3
+}





More information about the llvm-commits mailing list