Add support for sqrt, sqrtl, and sqrtf in TargetLibraryInfo. Disable
authorChad Rosier <mcrosier@apple.com>
Tue, 29 Nov 2011 23:57:10 +0000 (23:57 +0000)
committerChad Rosier <mcrosier@apple.com>
Tue, 29 Nov 2011 23:57:10 +0000 (23:57 +0000)
(fptrunc (sqrt (fpext x))) -> (sqrtf x) transformation if -fno-builtin is
specified.
rdar://10466410

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

include/llvm/Target/TargetLibraryInfo.h
lib/Target/TargetLibraryInfo.cpp
lib/Transforms/InstCombine/InstCombineCasts.cpp
lib/Transforms/InstCombine/InstructionCombining.cpp
test/Transforms/InstCombine/fold-sqrt-sqrtf.ll [new file with mode: 0644]

index 7770a11ac0dbfc0285ddb3f0c9b609cc63578b08..c4c262730ec6e72bd653ed5022f1b77ad7e86d7b 100644 (file)
@@ -35,6 +35,15 @@ namespace llvm {
       
       /// int siprintf(char *str, const char *format, ...);
       siprintf,
+
+      /// double sqrt(double x);
+      sqrt,
+
+      /// long double sqrtl(long double x);
+      sqrtl,
+
+      /// float sqrtf(float x);
+      sqrtf,
       
       /// int fiprintf(FILE *stream, const char *format, ...);
       fiprintf,
index aa2e014b2bc5593d75b24c03de19a1c0289d8fc4..d6dbde5b3acdff792a8c0fd92441182accdb5261 100644 (file)
@@ -28,6 +28,9 @@ const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] =
     "memset_pattern16",
     "iprintf",
     "siprintf",
+    "sqrt",
+    "sqrtl",
+    "sqrtf",
     "fiprintf",
     "fwrite",
     "fputs"
index f10e48abf108824a5725d4b959d9d365afd9ac82..63065e3e2ef499c65cfc1fb670d89b8f51384e57 100644 (file)
@@ -14,6 +14,7 @@
 #include "InstCombine.h"
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetLibraryInfo.h"
 #include "llvm/Support/PatternMatch.h"
 using namespace llvm;
 using namespace PatternMatch;
@@ -1213,10 +1214,10 @@ Instruction *InstCombiner::visitFPTrunc(FPTruncInst &CI) {
   }
   
   // Fold (fptrunc (sqrt (fpext x))) -> (sqrtf x)
-  // NOTE: This should be disabled by -fno-builtin-sqrt if we ever support it.
+  const TargetLibraryInfo &TLI = getAnalysis<TargetLibraryInfo>();
   CallInst *Call = dyn_cast<CallInst>(CI.getOperand(0));
-  if (Call && Call->getCalledFunction() &&
-      Call->getCalledFunction()->getName() == "sqrt" &&
+  if (Call && Call->getCalledFunction() && TLI.has(LibFunc::sqrtf) &&
+      Call->getCalledFunction()->getName() == TLI.getName(LibFunc::sqrt) &&
       Call->getNumArgOperands() == 1 &&
       Call->hasOneUse()) {
     CastInst *Arg = dyn_cast<CastInst>(Call->getArgOperand(0));
index a7a6311c1a8e3b80cf6944a46ccd6ee6e13329f5..60c7fefb15deedd3c3b70d4d64247be13439d114 100644 (file)
@@ -41,6 +41,7 @@
 #include "llvm/Analysis/InstructionSimplify.h"
 #include "llvm/Analysis/MemoryBuiltins.h"
 #include "llvm/Target/TargetData.h"
+#include "llvm/Target/TargetLibraryInfo.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Support/Debug.h"
@@ -79,6 +80,7 @@ INITIALIZE_PASS(InstCombiner, "instcombine",
 
 void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
   AU.setPreservesCFG();
+  AU.addRequired<TargetLibraryInfo>();
 }
 
 
diff --git a/test/Transforms/InstCombine/fold-sqrt-sqrtf.ll b/test/Transforms/InstCombine/fold-sqrt-sqrtf.ll
new file mode 100644 (file)
index 0000000..bd92b4a
--- /dev/null
@@ -0,0 +1,17 @@
+; RUN: opt -instcombine -S -disable-simplify-libcalls < %s | FileCheck %s
+; rdar://10466410
+
+; Instcombine tries to fold (fptrunc (sqrt (fpext x))) -> (sqrtf x), but this
+; shouldn't fold when sqrtf isn't available.
+define float @foo(float %f) uwtable ssp {
+entry:
+; CHECK: %conv = fpext float %f to double
+; CHECK: %call = tail call double @sqrt(double %conv)
+; CHECK: %conv1 = fptrunc double %call to float
+  %conv = fpext float %f to double
+  %call = tail call double @sqrt(double %conv)
+  %conv1 = fptrunc double %call to float
+  ret float %conv1
+}
+
+declare double @sqrt(double)