Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp +++ lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1839,6 +1839,19 @@ 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(FormatStr[0]), B, 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('%') == StringRef::npos) { // No format characters. Index: test/Transforms/InstCombine/printf-2.ll =================================================================== --- test/Transforms/InstCombine/printf-2.ll +++ test/Transforms/InstCombine/printf-2.ll @@ -7,6 +7,8 @@ @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 @@ 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 37) + ret void +; CHECK-NEXT: ret void +}