diff options
author | Richard Osborne <richard@xmos.com> | 2011-03-03 14:09:28 +0000 |
---|---|---|
committer | Richard Osborne <richard@xmos.com> | 2011-03-03 14:09:28 +0000 |
commit | 2dfb888392d4d82f467dfc8c9c4d2192cd71afb6 (patch) | |
tree | 11d18960b78a1b1d43b6df4e50dd1dcb470ab241 /llvm/lib | |
parent | 969dfbcff62e8c86084a6d9ae1f05c0a03b347dc (diff) | |
download | bcm5719-llvm-2dfb888392d4d82f467dfc8c9c4d2192cd71afb6.tar.gz bcm5719-llvm-2dfb888392d4d82f467dfc8c9c4d2192cd71afb6.zip |
Optimize sprintf -> siprintf if there are no floating point arguments
and siprintf is available on the target.
llvm-svn: 126937
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Target/TargetLibraryInfo.cpp | 6 | ||||
-rw-r--r-- | llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp | 36 |
2 files changed, 32 insertions, 10 deletions
diff --git a/llvm/lib/Target/TargetLibraryInfo.cpp b/llvm/lib/Target/TargetLibraryInfo.cpp index 6bf6c0d1a6b..345d914a580 100644 --- a/llvm/lib/Target/TargetLibraryInfo.cpp +++ b/llvm/lib/Target/TargetLibraryInfo.cpp @@ -31,9 +31,11 @@ static void initialize(TargetLibraryInfo &TLI, const Triple &T) { if (T.getOS() != Triple::Darwin || T.getDarwinMajorNumber() < 9) TLI.setUnavailable(LibFunc::memset_pattern16); - // iprintf is only available on XCore. - if (T.getArch() != Triple::xcore) + // iprintf and friends are only available on XCore. + if (T.getArch() != Triple::xcore) { TLI.setUnavailable(LibFunc::iprintf); + TLI.setUnavailable(LibFunc::siprintf); + } } diff --git a/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp index c0bef26291a..60f235ce3c8 100644 --- a/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -1176,14 +1176,8 @@ struct PrintFOpt : public LibCallOptimization { // 'sprintf' Optimizations struct SPrintFOpt : public LibCallOptimization { - virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { - // Require two fixed pointer arguments and an integer result. - const FunctionType *FT = Callee->getFunctionType(); - if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || - !FT->getParamType(1)->isPointerTy() || - !FT->getReturnType()->isIntegerTy()) - return 0; - + Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI, + IRBuilder<> &B) { // Check for a fixed format string. std::string FormatStr; if (!GetConstantStringInfo(CI->getArgOperand(1), FormatStr)) @@ -1244,6 +1238,32 @@ struct SPrintFOpt : public LibCallOptimization { } return 0; } + + virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) { + // Require two fixed pointer arguments and an integer result. + const FunctionType *FT = Callee->getFunctionType(); + if (FT->getNumParams() != 2 || !FT->getParamType(0)->isPointerTy() || + !FT->getParamType(1)->isPointerTy() || + !FT->getReturnType()->isIntegerTy()) + return 0; + + if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) { + return V; + } + + // sprintf(str, format, ...) -> iprintf(str, format, ...) if no floating + // point arguments. + if (TLI->has(LibFunc::siprintf) && !CallHasFloatingPointArgument(CI)) { + Module *M = B.GetInsertBlock()->getParent()->getParent(); + Constant *SIPrintFFn = + M->getOrInsertFunction("siprintf", FT, Callee->getAttributes()); + CallInst *New = cast<CallInst>(CI->clone()); + New->setCalledFunction(SIPrintFFn); + B.Insert(New); + return New; + } + return 0; + } }; //===---------------------------------------===// |