mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-16 14:31:59 +00:00
instcombine: Migrate printf optimizations
This patch migrates the printf optimizations from the simplify-libcalls pass into the instcombine library call simplifier. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168604 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
22689b6034
commit
d7aa3231f7
@ -99,93 +99,6 @@ namespace {
|
||||
// Formatting and IO Optimizations
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
//===---------------------------------------===//
|
||||
// 'printf' Optimizations
|
||||
|
||||
struct PrintFOpt : public LibCallOptimization {
|
||||
Value *OptimizeFixedFormatString(Function *Callee, CallInst *CI,
|
||||
IRBuilder<> &B) {
|
||||
// Check for a fixed format string.
|
||||
StringRef FormatStr;
|
||||
if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
|
||||
return 0;
|
||||
|
||||
// Empty format string -> noop.
|
||||
if (FormatStr.empty()) // Tolerate printf's declared void.
|
||||
return CI->use_empty() ? (Value*)CI :
|
||||
ConstantInt::get(CI->getType(), 0);
|
||||
|
||||
// Do not do any of the following transformations if the printf return value
|
||||
// is used, in general the printf return value is not compatible with either
|
||||
// putchar() or puts().
|
||||
if (!CI->use_empty())
|
||||
return 0;
|
||||
|
||||
// printf("x") -> putchar('x'), even for '%'.
|
||||
if (FormatStr.size() == 1) {
|
||||
Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
|
||||
if (CI->use_empty() || !Res) return Res;
|
||||
return B.CreateIntCast(Res, CI->getType(), true);
|
||||
}
|
||||
|
||||
// printf("foo\n") --> puts("foo")
|
||||
if (FormatStr[FormatStr.size()-1] == '\n' &&
|
||||
FormatStr.find('%') == std::string::npos) { // no format characters.
|
||||
// Create a string literal with no \n on it. We expect the constant merge
|
||||
// pass to be run after this pass, to merge duplicate strings.
|
||||
FormatStr = FormatStr.drop_back();
|
||||
Value *GV = B.CreateGlobalString(FormatStr, "str");
|
||||
Value *NewCI = EmitPutS(GV, B, TD, TLI);
|
||||
return (CI->use_empty() || !NewCI) ?
|
||||
NewCI :
|
||||
ConstantInt::get(CI->getType(), FormatStr.size()+1);
|
||||
}
|
||||
|
||||
// Optimize specific format strings.
|
||||
// printf("%c", chr) --> putchar(chr)
|
||||
if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
|
||||
CI->getArgOperand(1)->getType()->isIntegerTy()) {
|
||||
Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
|
||||
|
||||
if (CI->use_empty() || !Res) return Res;
|
||||
return B.CreateIntCast(Res, CI->getType(), true);
|
||||
}
|
||||
|
||||
// printf("%s\n", str) --> puts(str)
|
||||
if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
|
||||
CI->getArgOperand(1)->getType()->isPointerTy()) {
|
||||
return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual Value *CallOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
|
||||
// Require one fixed pointer argument and an integer/void result.
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
//===---------------------------------------===//
|
||||
// 'sprintf' Optimizations
|
||||
|
||||
@ -463,7 +376,7 @@ namespace {
|
||||
|
||||
StringMap<LibCallOptimization*> Optimizations;
|
||||
// Formatting and IO Optimizations
|
||||
SPrintFOpt SPrintF; PrintFOpt PrintF;
|
||||
SPrintFOpt SPrintF;
|
||||
FWriteOpt FWrite; FPutsOpt FPuts; FPrintFOpt FPrintF;
|
||||
PutsOpt Puts;
|
||||
|
||||
@ -522,7 +435,6 @@ void SimplifyLibCalls::AddOpt(LibFunc::Func F1, LibFunc::Func F2,
|
||||
void SimplifyLibCalls::InitOptimizations() {
|
||||
// Formatting and IO Optimizations
|
||||
Optimizations["sprintf"] = &SPrintF;
|
||||
Optimizations["printf"] = &PrintF;
|
||||
AddOpt(LibFunc::fwrite, &FWrite);
|
||||
AddOpt(LibFunc::fputs, &FPuts);
|
||||
Optimizations["fprintf"] = &FPrintF;
|
||||
|
@ -102,6 +102,15 @@ static bool isOnlyUsedInEqualityComparison(Value *V, Value *With) {
|
||||
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;
|
||||
}
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Fortified Library Call Optimizations
|
||||
//===----------------------------------------------------------------------===//
|
||||
@ -1312,6 +1321,94 @@ struct ToAsciiOpt : public LibCallOptimization {
|
||||
}
|
||||
};
|
||||
|
||||
//===----------------------------------------------------------------------===//
|
||||
// Formatting and IO Library Call Optimizations
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
struct PrintFOpt : public LibCallOptimization {
|
||||
Value *optimizeFixedFormatString(Function *Callee, CallInst *CI,
|
||||
IRBuilder<> &B) {
|
||||
// Check for a fixed format string.
|
||||
StringRef FormatStr;
|
||||
if (!getConstantStringInfo(CI->getArgOperand(0), FormatStr))
|
||||
return 0;
|
||||
|
||||
// Empty format string -> noop.
|
||||
if (FormatStr.empty()) // Tolerate printf's declared void.
|
||||
return CI->use_empty() ? (Value*)CI :
|
||||
ConstantInt::get(CI->getType(), 0);
|
||||
|
||||
// Do not do any of the following transformations if the printf return value
|
||||
// is used, in general the printf return value is not compatible with either
|
||||
// putchar() or puts().
|
||||
if (!CI->use_empty())
|
||||
return 0;
|
||||
|
||||
// printf("x") -> putchar('x'), even for '%'.
|
||||
if (FormatStr.size() == 1) {
|
||||
Value *Res = EmitPutChar(B.getInt32(FormatStr[0]), B, TD, TLI);
|
||||
if (CI->use_empty() || !Res) return Res;
|
||||
return B.CreateIntCast(Res, CI->getType(), true);
|
||||
}
|
||||
|
||||
// printf("foo\n") --> puts("foo")
|
||||
if (FormatStr[FormatStr.size()-1] == '\n' &&
|
||||
FormatStr.find('%') == std::string::npos) { // no format characters.
|
||||
// Create a string literal with no \n on it. We expect the constant merge
|
||||
// pass to be run after this pass, to merge duplicate strings.
|
||||
FormatStr = FormatStr.drop_back();
|
||||
Value *GV = B.CreateGlobalString(FormatStr, "str");
|
||||
Value *NewCI = EmitPutS(GV, B, TD, TLI);
|
||||
return (CI->use_empty() || !NewCI) ?
|
||||
NewCI :
|
||||
ConstantInt::get(CI->getType(), FormatStr.size()+1);
|
||||
}
|
||||
|
||||
// Optimize specific format strings.
|
||||
// printf("%c", chr) --> putchar(chr)
|
||||
if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
|
||||
CI->getArgOperand(1)->getType()->isIntegerTy()) {
|
||||
Value *Res = EmitPutChar(CI->getArgOperand(1), B, TD, TLI);
|
||||
|
||||
if (CI->use_empty() || !Res) return Res;
|
||||
return B.CreateIntCast(Res, CI->getType(), true);
|
||||
}
|
||||
|
||||
// printf("%s\n", str) --> puts(str)
|
||||
if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
|
||||
CI->getArgOperand(1)->getType()->isPointerTy()) {
|
||||
return EmitPutS(CI->getArgOperand(1), B, TD, TLI);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
virtual Value *callOptimizer(Function *Callee, CallInst *CI, IRBuilder<> &B) {
|
||||
// Require one fixed pointer argument and an integer/void result.
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
} // End anonymous namespace.
|
||||
|
||||
namespace llvm {
|
||||
@ -1365,6 +1462,9 @@ class LibCallSimplifierImpl {
|
||||
IsAsciiOpt IsAscii;
|
||||
ToAsciiOpt ToAscii;
|
||||
|
||||
// Formatting and IO library call optimizations.
|
||||
PrintFOpt PrintF;
|
||||
|
||||
void initOptimizations();
|
||||
void addOpt(LibFunc::Func F, LibCallOptimization* Opt);
|
||||
void addOpt(LibFunc::Func F1, LibFunc::Func F2, LibCallOptimization* Opt);
|
||||
@ -1485,6 +1585,9 @@ void LibCallSimplifierImpl::initOptimizations() {
|
||||
addOpt(LibFunc::isdigit, &IsDigit);
|
||||
addOpt(LibFunc::isascii, &IsAscii);
|
||||
addOpt(LibFunc::toascii, &ToAscii);
|
||||
|
||||
// Formatting and IO library call optimizations.
|
||||
addOpt(LibFunc::printf, &PrintF);
|
||||
}
|
||||
|
||||
Value *LibCallSimplifierImpl::optimizeCall(CallInst *CI) {
|
||||
|
@ -1,4 +1,4 @@
|
||||
; RUN: opt -simplify-libcalls -S < %s | FileCheck %s
|
||||
; RUN: opt -instcombine -S < %s | FileCheck %s
|
||||
|
||||
|
||||
@.str = private constant [3 x i8] c"%c\00"
|
119
test/Transforms/InstCombine/printf-1.ll
Normal file
119
test/Transforms/InstCombine/printf-1.ll
Normal file
@ -0,0 +1,119 @@
|
||||
; Test that the printf library call simplifier works correctly.
|
||||
;
|
||||
; RUN: opt < %s -instcombine -S | FileCheck %s
|
||||
; RUN: opt < %s -mtriple xcore-xmos-elf -instcombine -S | FileCheck %s -check-prefix=IPRINTF
|
||||
|
||||
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
|
||||
|
||||
@hello_world = constant [13 x i8] c"hello world\0A\00"
|
||||
@h = constant [2 x i8] c"h\00"
|
||||
@percent = constant [2 x i8] c"%\00"
|
||||
@percent_c = constant [3 x i8] c"%c\00"
|
||||
@percent_d = constant [3 x i8] c"%d\00"
|
||||
@percent_f = constant [3 x i8] c"%f\00"
|
||||
@percent_s = constant [4 x i8] c"%s\0A\00"
|
||||
@empty = constant [1 x i8] c"\00"
|
||||
; CHECK: [[STR:@[a-z0-9]+]] = private unnamed_addr constant [12 x i8] c"hello world\00"
|
||||
|
||||
declare i32 @printf(i8*, ...)
|
||||
|
||||
; Check printf("") -> noop.
|
||||
|
||||
define void @test_simplify1() {
|
||||
; CHECK: @test_simplify1
|
||||
%fmt = getelementptr [1 x i8]* @empty, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt)
|
||||
ret void
|
||||
; CHECK-NEXT: ret void
|
||||
}
|
||||
|
||||
; Check printf("x") -> putchar('x'), even for '%'.
|
||||
|
||||
define void @test_simplify2() {
|
||||
; CHECK: @test_simplify2
|
||||
%fmt = getelementptr [2 x i8]* @h, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt)
|
||||
; CHECK-NEXT: call i32 @putchar(i32 104)
|
||||
ret void
|
||||
; CHECK-NEXT: ret void
|
||||
}
|
||||
|
||||
define void @test_simplify3() {
|
||||
; CHECK: @test_simplify3
|
||||
%fmt = getelementptr [2 x i8]* @percent, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt)
|
||||
; CHECK-NEXT: call i32 @putchar(i32 37)
|
||||
ret void
|
||||
; CHECK-NEXT: ret void
|
||||
}
|
||||
|
||||
; Check printf("foo\n") -> puts("foo").
|
||||
|
||||
define void @test_simplify4() {
|
||||
; CHECK: @test_simplify4
|
||||
%fmt = getelementptr [13 x i8]* @hello_world, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt)
|
||||
; CHECK-NEXT: call i32 @puts(i8* getelementptr inbounds ([12 x i8]* [[STR]], i32 0, i32 0))
|
||||
ret void
|
||||
; CHECK-NEXT: ret void
|
||||
}
|
||||
|
||||
; Check printf("%c", chr) -> putchar(chr).
|
||||
|
||||
define void @test_simplify5() {
|
||||
; CHECK: @test_simplify5
|
||||
%fmt = getelementptr [3 x i8]* @percent_c, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt, i8 104)
|
||||
; CHECK-NEXT: call i32 @putchar(i32 104)
|
||||
ret void
|
||||
; CHECK-NEXT: ret void
|
||||
}
|
||||
|
||||
; Check printf("%s\n", str) -> puts(str).
|
||||
|
||||
define void @test_simplify6() {
|
||||
; CHECK: @test_simplify6
|
||||
%fmt = getelementptr [4 x i8]* @percent_s, i32 0, i32 0
|
||||
%str = getelementptr [13 x i8]* @hello_world, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt, i8* %str)
|
||||
; CHECK-NEXT: call i32 @puts(i8* getelementptr inbounds ([13 x i8]* @hello_world, i32 0, i32 0))
|
||||
ret void
|
||||
; CHECK-NEXT: ret void
|
||||
}
|
||||
|
||||
; Check printf(format, ...) -> iprintf(format, ...) if no floating point.
|
||||
|
||||
define void @test_simplify7() {
|
||||
; CHECK-IPRINTF: @test_simplify7
|
||||
%fmt = getelementptr [3 x i8]* @percent_d, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt, i32 187)
|
||||
; CHECK-NEXT-IPRINTF: call i32 (i8*, ...)* @iprintf(i8* getelementptr inbounds ([3 x i8]* @percent_d, i32 0, i32 0), i32 187)
|
||||
ret void
|
||||
; CHECK-NEXT-IPRINTF: ret void
|
||||
}
|
||||
|
||||
define void @test_no_simplify1() {
|
||||
; CHECK-IPRINTF: @test_no_simplify1
|
||||
%fmt = getelementptr [3 x i8]* @percent_f, i32 0, i32 0
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt, double 1.87)
|
||||
; CHECK-NEXT-IPRINTF: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([3 x i8]* @percent_f, i32 0, i32 0), double 1.870000e+00)
|
||||
ret void
|
||||
; CHECK-NEXT-IPRINTF: ret void
|
||||
}
|
||||
|
||||
define void @test_no_simplify2(i8* %fmt, double %d) {
|
||||
; CHECK: @test_no_simplify2
|
||||
call i32 (i8*, ...)* @printf(i8* %fmt, double %d)
|
||||
; CHECK-NEXT: call i32 (i8*, ...)* @printf(i8* %fmt, double %d)
|
||||
ret void
|
||||
; CHECK-NEXT: ret void
|
||||
}
|
||||
|
||||
define i32 @test_no_simplify3() {
|
||||
; CHECK: @test_no_simplify3
|
||||
%fmt = getelementptr [2 x i8]* @h, i32 0, i32 0
|
||||
%ret = call i32 (i8*, ...)* @printf(i8* %fmt)
|
||||
; CHECK-NEXT: call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8]* @h, i32 0, i32 0))
|
||||
ret i32 %ret
|
||||
; CHECK-NEXT: ret i32 %ret
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
; RUN: opt < %s -simplify-libcalls -S | FileCheck %s
|
||||
|
||||
@str = internal constant [13 x i8] c"hello world\0A\00" ; <[13 x i8]*> [#uses=1]
|
||||
@str1 = internal constant [2 x i8] c"h\00" ; <[2 x i8]*> [#uses=1]
|
||||
|
||||
; CHECK: private unnamed_addr constant [12 x i8] c"hello world\00"
|
||||
|
||||
declare i32 @printf(i8*, ...)
|
||||
|
||||
; CHECK: define void @f0
|
||||
; CHECK-NOT: printf
|
||||
; CHECK: }
|
||||
define void @f0() {
|
||||
entry:
|
||||
%tmp1 = tail call i32 (i8*, ...)* @printf( i8* getelementptr ([13 x i8]* @str, i32 0, i32 0) ) ; <i32> [#uses=0]
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: define void @f1
|
||||
; CHECK-NOT: printf
|
||||
; CHECK: }
|
||||
define void @f1() {
|
||||
entry:
|
||||
%tmp1 = tail call i32 (i8*, ...)* @printf( i8* getelementptr ([2 x i8]* @str1, i32 0, i32 0) ) ; <i32> [#uses=0]
|
||||
ret void
|
||||
}
|
||||
|
||||
; Verify that we don't turn this into a putchar call (thus changing the return
|
||||
; value).
|
||||
;
|
||||
; CHECK: define i32 @f2
|
||||
; CHECK: printf
|
||||
; CHECK: }
|
||||
define i32 @f2() {
|
||||
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([2 x i8]* @str1, i32 0, i32 0))
|
||||
ret i32 %call
|
||||
}
|
@ -6,26 +6,6 @@ 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
|
||||
}
|
||||
|
||||
; Verify sprintf with no floating point arguments is transformed to siprintf
|
||||
define i32 @f2(i8* %p, i32 %x) nounwind {
|
||||
entry:
|
||||
@ -66,6 +46,5 @@ entry:
|
||||
ret i32 %0
|
||||
}
|
||||
|
||||
declare i32 @printf(i8* nocapture, ...) nounwind
|
||||
declare i32 @sprintf(i8* nocapture, i8* nocapture, ...) nounwind
|
||||
declare i32 @fprintf(i8* nocapture, i8* nocapture, ...) nounwind
|
||||
|
Loading…
x
Reference in New Issue
Block a user