Fix a serious performance regression introduced by r108687 on linux:
authorChris Lattner <sabre@nondot.org>
Tue, 7 Sep 2010 20:01:38 +0000 (20:01 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 7 Sep 2010 20:01:38 +0000 (20:01 +0000)
turning (fptrunc (sqrt (fpext x))) -> (sqrtf x)  is great, but we have
to delete the original sqrt as well.  Not doing so causes us to do
two sqrt's when building with -fmath-errno (the default on linux).

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

lib/Transforms/InstCombine/InstCombineCasts.cpp
test/Transforms/InstCombine/sqrt.ll

index 6c2477d52c77578e8fc5313c1c3fbace31d5f838..79a9b09c64d07ddf3339ebedf78bf0643e6c08eb 100644 (file)
@@ -1139,7 +1139,7 @@ Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
         Arg->getOperand(0)->getType()->isFloatTy()) {
       Function *Callee = Call->getCalledFunction();
       Module *M = CI.getParent()->getParent()->getParent();
-      ConstantSqrtfFunc = M->getOrInsertFunction("sqrtf", 
+      Constant *SqrtfFunc = M->getOrInsertFunction("sqrtf", 
                                                    Callee->getAttributes(),
                                                    Builder->getFloatTy(),
                                                    Builder->getFloatTy(),
@@ -1147,6 +1147,11 @@ Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
       CallInst *ret = CallInst::Create(SqrtfFunc, Arg->getOperand(0),
                                        "sqrtfcall");
       ret->setAttributes(Callee->getAttributes());
+      
+      
+      // Remove the old Call.  With -fmath-errno, it won't get marked readnone.
+      Call->replaceAllUsesWith(UndefValue::get(Call->getType()));
+      EraseInstFromFunction(*Call);
       return ret;
     }
   }
index 65053820482ffb55d40a6cf9f3d22fc764348c7e..69e511bfb3bd48734bf98c7082af785cf6af20c5 100644 (file)
@@ -1,16 +1,32 @@
 ; RUN: opt -S -instcombine %s | FileCheck %s
 
-define float @foo(float %x) nounwind readnone ssp {
+define float @test1(float %x) nounwind readnone ssp {
 entry:
+; CHECK: @test1
 ; CHECK-NOT: fpext
 ; CHECK-NOT: sqrt(
 ; CHECK: sqrtf(
 ; CHECK-NOT: fptrunc
   %conv = fpext float %x to double                ; <double> [#uses=1]
-  %call = tail call double @sqrt(double %conv) nounwind ; <double> [#uses=1]
+  %call = tail call double @sqrt(double %conv) readnone nounwind ; <double> [#uses=1]
   %conv1 = fptrunc double %call to float          ; <float> [#uses=1]
 ; CHECK: ret float
   ret float %conv1
 }
 
-declare double @sqrt(double) readnone
+declare double @sqrt(double)
+
+; PR8096
+define float @test2(float %x) nounwind readnone ssp {
+entry:
+; CHECK: @test2
+; CHECK-NOT: fpext
+; CHECK-NOT: sqrt(
+; CHECK: sqrtf(
+; CHECK-NOT: fptrunc
+  %conv = fpext float %x to double                ; <double> [#uses=1]
+  %call = tail call double @sqrt(double %conv) nounwind ; <double> [#uses=1]
+  %conv1 = fptrunc double %call to float          ; <float> [#uses=1]
+; CHECK: ret float
+  ret float %conv1
+}