From 6db1dcbf6b42d1eedc070585c65e6fe7dab25e54 Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Mon, 28 Mar 2016 15:54:01 +0000 Subject: [PATCH] [SimplifyLibCalls] Transform printf("%s", "a") -> putchar('a'). llvm-svn: 264588 --- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp | 18 ++++++++++++++++++ llvm/test/Transforms/InstCombine/printf-2.ll | 12 ++++++++++++ 2 files changed, 30 insertions(+) diff --git a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp index 4252cf5..3712575 100644 --- a/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -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. diff --git a/llvm/test/Transforms/InstCombine/printf-2.ll b/llvm/test/Transforms/InstCombine/printf-2.ll index d676985..fbd5b1b 100644 --- a/llvm/test/Transforms/InstCombine/printf-2.ll +++ b/llvm/test/Transforms/InstCombine/printf-2.ll @@ -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 +} -- 2.7.4