[PATCH] D18656: [SimplifyLibCalls] Strip dead code in printf() transformations

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 31 10:18:14 PDT 2016


davide created this revision.
davide added reviewers: majnemer, spatel.
davide added a subscriber: llvm-commits.

If the return value of printf() is used, we don't do any transformation in SimplifyLibCalls.
Yet, in each transformation we check again if `Ci->use_empty()`. I think this check is redundant, because it's always true. This also allows to strip some code. IT passes the testsuite, but given I wasn't entirely sure of what are the effects, I put an assert() instead. 

http://reviews.llvm.org/D18656

Files:
  lib/Transforms/Utils/SimplifyLibCalls.cpp

Index: lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1834,9 +1834,9 @@
   // printf("x") -> putchar('x'), even for '%'.
   if (FormatStr.size() == 1) {
     Value *Res = emitPutChar(B.getInt32(FormatStr[0]), B, TLI);
-    if (CI->use_empty() || !Res)
-      return Res;
-    return B.CreateIntCast(Res, CI->getType(), true);
+    assert(CI->use_empty() &&
+           "printf return value used should inhibit optimizations");
+    return Res;
   }
 
   // printf("%s", "a") --> putchar('a')
@@ -1847,14 +1847,9 @@
     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);
+    assert(CI->use_empty() &&
+           "printf return value used should inhibit optimizations");
+    return Res;
   }
 
   // printf("foo\n") --> puts("foo")
@@ -1865,26 +1860,28 @@
     FormatStr = FormatStr.drop_back();
     Value *GV = B.CreateGlobalString(FormatStr, "str");
     Value *NewCI = emitPutS(GV, B, TLI);
-    return (CI->use_empty() || !NewCI)
-               ? NewCI
-               : ConstantInt::get(CI->getType(), FormatStr.size() + 1);
+    assert(CI->use_empty() &&
+           "printf return value used should inhibit optimizations");
+    return NewCI;
   }
 
   // Optimize specific format strings.
   // printf("%c", chr) --> putchar(chr)
   if (FormatStr == "%c" && CI->getNumArgOperands() > 1 &&
       CI->getArgOperand(1)->getType()->isIntegerTy()) {
     Value *Res = emitPutChar(CI->getArgOperand(1), B, TLI);
-
-    if (CI->use_empty() || !Res)
-      return Res;
-    return B.CreateIntCast(Res, CI->getType(), true);
+    assert(CI->use_empty() &&
+           "printf return value used should inhibit optimizations");
+    return Res;
   }
 
   // printf("%s\n", str) --> puts(str)
   if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&
       CI->getArgOperand(1)->getType()->isPointerTy()) {
-    return emitPutS(CI->getArgOperand(1), B, TLI);
+    Value *Res = emitPutS(CI->getArgOperand(1), B, TLI);
+    assert(CI->use_empty() &&
+           "printf return value used should inhibit optimizations");
+    return Res;
   }
   return nullptr;
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18656.52234.patch
Type: text/x-patch
Size: 2563 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160331/ac83dda9/attachment.bin>


More information about the llvm-commits mailing list