[PATCH] D109289: [InstCombine] snprintf(NULL, 0, "%s", str) to strlen(str)

Bjorn Pettersson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 15 10:29:45 PDT 2021


bjope added inline comments.


================
Comment at: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp:2594
     if (FormatStr[1] == 's') {
+      // snprintf(NULL, 0, "%s", str) to strlen(str)
+      if (isa<ConstantPointerNull>(CI->getArgOperand(0)) && N == 0)
----------------
Is there a specific use-case for doing this (I guess a user could use strlen instead if that is what they want). Is it to save cycles due to strlen probably being faster, or because it is more likely that there are subsequent transformations involving strlen?


================
Comment at: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp:2597
+        if (auto *V = emitStrLen(CI->getArgOperand(3), B, DL, TLI)) {
+          if (CI->getType() != V->getType())
+            V = B.CreateTrunc(V, CI->getType());
----------------
xbolva00 wrote:
> snprintf returns int, strlen returns size_t so...  trunc should be fine?
Isn't the transform only valid if the number of bits in CI is greater than the number of bits in V?
I.e. the unsigned value returned by strlen must fit into the non-negative part of the signed destination type. And given that constraint we always need the trunc.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D109289/new/

https://reviews.llvm.org/D109289



More information about the llvm-commits mailing list