Optimize printf -> iprintf if there are no floating point arguments

and iprintf is available on the target. Currently iprintf is only
marked as being available on the XCore.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@126935 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Richard Osborne 2011-03-03 13:17:51 +00:00
parent 49d7999b89
commit 3649824bec
4 changed files with 78 additions and 11 deletions

View File

@ -26,6 +26,9 @@ namespace llvm {
/// void memset_pattern16(void *b, const void *pattern16, size_t len);
memset_pattern16,
/// int iprintf(const char *format, ...);
iprintf,
NumLibFuncs
};
}

View File

@ -30,7 +30,10 @@ static void initialize(TargetLibraryInfo &TLI, const Triple &T) {
// memset_pattern16 is only available on iOS 3.0 and Mac OS/X 10.5 and later.
if (T.getOS() != Triple::Darwin || T.getDarwinMajorNumber() < 9)
TLI.setUnavailable(LibFunc::memset_pattern16);
// iprintf is only available on XCore.
if (T.getArch() != Triple::xcore)
TLI.setUnavailable(LibFunc::iprintf);
}

View File

@ -49,6 +49,7 @@ class LibCallOptimization {
protected:
Function *Caller;
const TargetData *TD;
const TargetLibraryInfo *TLI;
LLVMContext* Context;
public:
LibCallOptimization() { }
@ -62,9 +63,11 @@ public:
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B)
=0;
Value *OptimizeCall(CallInst *CI, const TargetData *TD, IRBuilder<> &B) {
Value *OptimizeCall(CallInst *CI, const TargetData *TD,
const TargetLibraryInfo *TLI, IRBuilder<> &B) {
Caller = CI->getParent()->getParent();
this->TD = TD;
this->TLI = TLI;
if (CI->getCalledFunction())
Context = &CI->getCalledFunction()->getContext();
@ -97,6 +100,15 @@ static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
}
return true;
}
static bool CallHasFloatingPointArgument(const CallInst *CI) {
for (CallInst::const_op_iterator it = CI->op_begin(), e = CI->op_end();
it != e; ++it) {
if ((*it)->getType()->isFloatingPointTy())
return true;
}
return false;
}
/// IsOnlyUsedInEqualityComparison - Return true if it is only used in equality
/// comparisons with With.
@ -1075,14 +1087,8 @@ struct ToAsciiOpt : public LibCallOptimization {
// 'printf' Optimizations
struct PrintFOpt : public LibCallOptimization {
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
// Require one fixed pointer argument and an integer/void result.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
!(FT->getReturnType()->isIntegerTy() ||
FT->getReturnType()->isVoidTy()))
return 0;
Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
IRBuilder<> &B) {
// Check for a fixed format string.
std::string FormatStr;
if (!GetConstantStringInfo(CI->getArgOperand(0), FormatStr))
@ -1138,6 +1144,32 @@ struct PrintFOpt : public LibCallOptimization {
}
return 0;
}
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
// Require one fixed pointer argument and an integer/void result.
const FunctionType *FT = Callee->getFunctionType();
if (FT->getNumParams() < 1 || !FT->getParamType(0)->isPointerTy() ||
!(FT->getReturnType()->isIntegerTy() ||
FT->getReturnType()->isVoidTy()))
return 0;
if (Value *V = OptimizeFixedFormatString(Callee, CI, B)) {
return V;
}
// printf(format, ...) -> iprintf(format, ...) if no floating point
// arguments.
if (TLI->has(LibFunc::iprintf) && !CallHasFloatingPointArgument(CI)) {
Module *M = B.GetInsertBlock()->getParent()->getParent();
Constant *IPrintFFn =
M->getOrInsertFunction("iprintf", FT, Callee->getAttributes());
CallInst *New = cast<CallInst>(CI->clone());
New->setCalledFunction(IPrintFFn);
B.Insert(New);
return New;
}
return 0;
}
};
//===---------------------------------------===//
@ -1545,7 +1577,7 @@ bool SimplifyLibCalls::runOnFunction(Function &F) {
Builder.SetInsertPoint(BB, I);
// Try to optimize this call.
Value *Result = LCO->OptimizeCall(CI, TD, Builder);
Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);
if (Result == 0) continue;
DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;

View File

@ -0,0 +1,29 @@
; RUN: opt < %s -simplify-libcalls -S -o %t
; RUN: FileCheck < %t %s
target datalayout = "e-p:32:32:32-i1:8:32-i8:8:32-i16:16:32-i32:32:32-i64:32:32-f32:32:32-f64:32:32-v64:64:64-v128:128:128-a0:0:32"
target triple = "xcore-xmos-elf"
@.str = internal constant [4 x i8] c"%f\0A\00" ; <[4 x i8]*> [#uses=1]
@.str1 = internal constant [4 x i8] c"%d\0A\00" ; <[4 x i8]*> [#uses=1]
; Verify printf with no floating point arguments is transformed to iprintf
define i32 @f0(i32 %x) nounwind {
entry:
; CHECK: define i32 @f0
; CHECK: @iprintf
; CHECK: }
%0 = tail call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @.str1, i32 0, i32 0), i32 %x) ; <i32> [#uses=0]
ret i32 %0
}
; Verify we don't turn this into an iprintf call
define void @f1(double %x) nounwind {
entry:
; CHECK: define void @f1
; CHECK: @printf
; CHECK: }
%0 = tail call i32 (i8*, ...)* @printf(i8* getelementptr ([4 x i8]* @.str, i32 0, i32 0), double %x) nounwind ; <i32> [#uses=0]
ret void
}
declare i32 @printf(i8* nocapture, ...) nounwind