[SimplifyLibCalls] Transform printf("%s", "a") -> putchar('a').

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264588 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Davide Italiano
2016-03-28 15:54:01 +00:00
parent f63fe23e0d
commit adf9f00eed
2 changed files with 30 additions and 0 deletions
+18
View File
@@ -1839,6 +1839,24 @@ Value *LibCallSimplifier::optimizePrintFString(CallInst *CI, IRBuilder<> &B) {
return B.CreateIntCast(Res, CI->getType(), true);
}
// printf("%s", "a") --> putchar('a')
if (FormatStr == "%s" && CI->getNumArgOperands() > 1) {
StringRef ChrStr;
if (!getConstantStringInfo(CI->getOperand(1), ChrStr))
return nullptr;
if (ChrStr.size() != 1)
return nullptr;
Value *Res = emitPutChar(B.getInt32(ChrStr[0]), B, TLI);
// FIXME: Here we check that the return value is not used
// but ealier we prevent transformations in case it is.
// This should probably be an assert.
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('%') == StringRef::npos) { // No format characters.
+12
View File
@@ -7,6 +7,8 @@ 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"
@percent_s = constant [4 x i8] c"%s\0A\00"
@format_str = constant [3 x i8] c"%s\00"
@charstr = constant [2 x i8] c"a\00"
declare void @printf(i8*, ...)
@@ -39,3 +41,13 @@ define void @test_simplify6() {
ret void
; CHECK-NEXT: ret void
}
define void @test_simplify7() {
; CHECK-LABEL: @test_simplify7(
%fmt = getelementptr [3 x i8], [3 x i8]* @format_str, i32 0, i32 0
%str = getelementptr [2 x i8], [2 x i8]* @charstr, i32 0, i32 0
call void (i8*, ...) @printf(i8* %fmt, i8* %str)
; CHECK-NEXT: call i32 @putchar(i32 97)
ret void
; CHECK-NEXT: ret void
}