[PATCH] D18423: [SimplifyLibCalls] Transform printf("%s", "a") into putchar('a')

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Sun Mar 27 18:51:23 PDT 2016


davide updated this revision to Diff 51766.
davide added a comment.

Sanjay's comments.


http://reviews.llvm.org/D18423

Files:
  lib/Transforms/Utils/SimplifyLibCalls.cpp
  test/Transforms/InstCombine/printf-2.ll

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"
+ 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 @@
   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
+}
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(ChrStr[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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18423.51766.patch
Type: text/x-patch
Size: 1753 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160328/7a90258d/attachment.bin>


More information about the llvm-commits mailing list