Optimize a printf with a double procent to putchar.

llvm-svn: 268922
This commit is contained in:
Joerg Sonnenberger 2016-05-09 14:36:16 +00:00
parent 5c20e27b7f
commit 8ffe7ab7c2
2 changed files with 14 additions and 2 deletions

View File

@ -1598,8 +1598,8 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
if (!CI->use_empty())
return nullptr;
// printf("x") -> putchar('x'), even for '%'.
if (FormatStr.size() == 1)
// printf("x") -> putchar('x'), even for "%" and "%%".
if (FormatStr.size() == 1 || FormatStr == "%%")
return emitPutChar(B.getInt32(FormatStr[0]), B, TLI);
// printf("%s", "a") --> putchar('a')

View File

@ -7,6 +7,7 @@ target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f3
@hello_world = constant [13 x i8] c"hello world\0A\00"
@h = constant [2 x i8] c"h\00"
@h2 = constant [3 x i8] c"%%\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"
@ -38,6 +39,17 @@ define void @test_simplify2() {
; CHECK-NEXT: ret void
}
; Special case: printf("%%") -> putchar('%').
define void @test_simplify2b() {
; CHECK-LABEL: @test_simplify2b(
%fmt = getelementptr [3 x i8], [3 x i8]* @h2, i32 0, i32 0
call i32 (i8*, ...) @printf(i8* %fmt)
; CHECK-NEXT: call i32 @putchar(i32 37)
ret void
; CHECK-NEXT: ret void
}
define void @test_simplify3() {
; CHECK-LABEL: @test_simplify3(
%fmt = getelementptr [2 x i8], [2 x i8]* @percent, i32 0, i32 0