Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit ee20786

Browse files
committed
Optimize a printf with a double procent to putchar.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268922 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent ec0b9c8 commit ee20786

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,8 +1598,8 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
15981598
if (!CI->use_empty())
15991599
return nullptr;
16001600

1601-
// printf("x") -> putchar('x'), even for '%'.
1602-
if (FormatStr.size() == 1)
1601+
// printf("x") -> putchar('x'), even for "%" and "%%".
1602+
if (FormatStr.size() == 1 || FormatStr == "%%")
16031603
return emitPutChar(B.getInt32(FormatStr[0]), B, TLI);
16041604

16051605
// printf("%s", "a") --> putchar('a')

test/Transforms/InstCombine/printf-1.ll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
77

88
@hello_world = constant [13 x i8] c"hello world\0A\00"
99
@h = constant [2 x i8] c"h\00"
10+
@h2 = constant [3 x i8] c"%%\00"
1011
@percent = constant [2 x i8] c"%\00"
1112
@percent_c = constant [3 x i8] c"%c\00"
1213
@percent_d = constant [3 x i8] c"%d\00"
@@ -38,6 +39,17 @@ define void @test_simplify2() {
3839
; CHECK-NEXT: ret void
3940
}
4041

42+
; Special case: printf("%%") -> putchar('%').
43+
44+
define void @test_simplify2b() {
45+
; CHECK-LABEL: @test_simplify2b(
46+
%fmt = getelementptr [3 x i8], [3 x i8]* @h2, i32 0, i32 0
47+
call i32 (i8*, ...) @printf(i8* %fmt)
48+
; CHECK-NEXT: call i32 @putchar(i32 37)
49+
ret void
50+
; CHECK-NEXT: ret void
51+
}
52+
4153
define void @test_simplify3() {
4254
; CHECK-LABEL: @test_simplify3(
4355
%fmt = getelementptr [2 x i8], [2 x i8]* @percent, i32 0, i32 0

0 commit comments

Comments
 (0)