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

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 28 08:54:01 PDT 2016


Author: davide
Date: Mon Mar 28 10:54:01 2016
New Revision: 264588

URL: http://llvm.org/viewvc/llvm-project?rev=264588&view=rev
Log:
[SimplifyLibCalls] Transform printf("%s", "a") -> putchar('a').

Modified:
    llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
    llvm/trunk/test/Transforms/InstCombine/printf-2.ll

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=264588&r1=264587&r2=264588&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Mon Mar 28 10:54:01 2016
@@ -1839,6 +1839,24 @@ Value *LibCallSimplifier::optimizePrintF
     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.

Modified: llvm/trunk/test/Transforms/InstCombine/printf-2.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/printf-2.ll?rev=264588&r1=264587&r2=264588&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/printf-2.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/printf-2.ll Mon Mar 28 10:54:01 2016
@@ -7,6 +7,8 @@ target datalayout = "e-p:32:32:32-i1:8: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"
+ at format_str = constant [3 x i8] c"%s\00"
+ at 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
+}




More information about the llvm-commits mailing list