Skip to content

Commit

Permalink
Add support for sqrt, sqrtl, and sqrtf in TargetLibraryInfo. Disable
Browse files Browse the repository at this point in the history
(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
  • Loading branch information
Chad Rosier committed Nov 29, 2011
1 parent 6029b6d commit 3d925d2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
9 changes: 9 additions & 0 deletions include/llvm/Target/TargetLibraryInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions lib/Target/TargetLibraryInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const char* TargetLibraryInfo::StandardNames[LibFunc::NumLibFuncs] =
"memset_pattern16",
"iprintf",
"siprintf",
"sqrt",
"sqrtl",
"sqrtf",
"fiprintf",
"fwrite",
"fputs"
Expand Down
7 changes: 4 additions & 3 deletions lib/Transforms/InstCombine/InstCombineCasts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 2 additions & 0 deletions lib/Transforms/InstCombine/InstructionCombining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -79,6 +80,7 @@ INITIALIZE_PASS(InstCombiner, "instcombine",

void InstCombiner::getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesCFG();
AU.addRequired<TargetLibraryInfo>();
}


Expand Down
17 changes: 17 additions & 0 deletions test/Transforms/InstCombine/fold-sqrt-sqrtf.ll
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 3d925d2

Please sign in to comment.