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

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 24 08:34:19 PDT 2016


spatel added inline comments.

================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:1843
@@ +1842,3 @@
+  // printf("%s", "a") --> putchar('a')
+  if (FormatStr == "%s" && CI->getNumArgOperands() > 1) {
+    StringRef ChrStr;
----------------
The number of arguments should be an exact comparison? 
CI->getNumArgOperands() == 2

================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:1849
@@ +1848,3 @@
+      return nullptr;
+    Value *Res = emitPutChar(B.getInt32(FormatStr[0]), B, TLI);
+    if (CI->use_empty() || !Res)
----------------
FormatStr[0] --> ChrStr[0] ?

================
Comment at: lib/Transforms/Utils/SimplifyLibCalls.cpp:1852
@@ +1851,3 @@
+      return Res;
+    return B.CreateIntCast(Res, CI->getType(), true);
+  }
----------------
This does not behave the way that we need.
printf() returns the number of characters that were successfully printed or an error code.
putchar() returns the character value that was printed or an error code.
So on success, the transformed call must return '1'. On error, some translation of the error code may be needed:
http://www.cplusplus.com/reference/cstdio/printf/
http://www.cplusplus.com/reference/cstdio/putchar/

================
Comment at: test/Transforms/InstCombine/printf-2.ll:13
@@ -10,3 +12,3 @@
 
 declare void @printf(i8*, ...)
 
----------------
Please add/use test cases that verify that the return value of printf is handled correctly by this transform.


http://reviews.llvm.org/D18423





More information about the llvm-commits mailing list