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 | 419454ad3720b8cf4613d9e790669c8beaccd1a4 (patch) | |
tree | 4a708cde9e05335c87db1a4b789f94010d66a077 | |
parent | 12785e87e88309c1934add000b754eba51a6c391 (diff) | |
download | llvm-419454ad3720b8cf4613d9e790669c8beaccd1a4.tar.gz llvm-419454ad3720b8cf4613d9e790669c8beaccd1a4.tar.bz2 llvm-419454ad3720b8cf4613d9e790669c8beaccd1a4.tar.xz |
Optimize sprintf -> siprintf if there are no floating point arguments
and siprintf is available on the target.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126937 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/Target/TargetLibraryInfo.h | 3 | ||||
-rw-r--r-- | lib/Target/TargetLibraryInfo.cpp | 6 | ||||
-rw-r--r-- | lib/Transforms/Scalar/SimplifyLibCalls.cpp | 36 | ||||
-rw-r--r-- | test/Transforms/SimplifyLibCalls/iprintf.ll | 21 |
4 files changed, 56 insertions, 10 deletions
diff --git a/include/llvm/Target/TargetLibraryInfo.h b/include/llvm/Target/TargetLibraryInfo.h index bc119e11cb..8474fc57bc 100644 --- a/include/llvm/Target/TargetLibraryInfo.h +++ b/include/llvm/Target/TargetLibraryInfo.h @@ -29,6 +29,9 @@ namespace llvm { /// int iprintf(const char *format, ...); iprintf, + /// int siprintf(char *str, const char *format, ...); + siprintf, + NumLibFuncs }; } diff --git a/lib/Target/TargetLibraryInfo.cpp b/lib/Target/TargetLibraryInfo.cpp index 6bf6c0d1a6..345d914a58 100644 --- a/lib/Target/TargetLibraryInfo.cpp +++ b/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/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp index c0bef26291..60f235ce3c 100644 --- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/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; + } }; //===---------------------------------------===// diff --git a/test/Transforms/SimplifyLibCalls/iprintf.ll b/test/Transforms/SimplifyLibCalls/iprintf.ll index 11385cb75d..b99de6efb8 100644 --- a/test/Transforms/SimplifyLibCalls/iprintf.ll +++ b/test/Transforms/SimplifyLibCalls/iprintf.ll @@ -26,4 +26,25 @@ entry: ret void } +; Verify sprintf with no floating point arguments is transformed to siprintf +define i32 @f2(i8* %p, i32 %x) nounwind { +entry: +; CHECK: define i32 @f2 +; CHECK: @siprintf +; CHECK: } + %0 = tail call i32 (i8*, i8*, ...)* @sprintf(i8 *%p, i8* getelementptr ([4 x i8]* @.str1, i32 0, i32 0), i32 %x) + ret i32 %0 +} + +; Verify we don't turn this into an siprintf call +define i32 @f3(i8* %p, double %x) nounwind { +entry: +; CHECK: define i32 @f3 +; CHECK: @sprintf +; CHECK: } + %0 = tail call i32 (i8*, i8*, ...)* @sprintf(i8 *%p, i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0), double %x) + ret i32 %0 +} + declare i32 @printf(i8* nocapture, ...) nounwind +declare i32 @sprintf(i8* nocapture, i8* nocapture, ...) nounwind |