[PATCH] D123818: [InstCombine] Fold strnlen calls in equality to zero.

Martin Sebor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 25 10:59:21 PDT 2022


msebor added inline comments.


================
Comment at: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp:645
+    //   strnlen(x, N) == 0 --> *x == 0
+    return B.CreateZExt(B.CreateLoad(CharTy, Src, "strlenfirst"),
+                        CI->getType());
----------------
xbolva00 wrote:
> maybe you can drop “strlenfirst” here.. 
Because the function also handles `strnlen` besides `strlen`?  I can do that but since other loads in the file are annotated I've changed it to `"char0"`.  Let me know if I misunderstood.


================
Comment at: llvm/test/Transforms/InstCombine/strnlen-5.ll:42
 
 ; Fold strnlen(ax, 1) == 0 to *ax == 0.
 
----------------
xbolva00 wrote:
> What about “strnlen(…) < 1” ? Do we have such test?
I've added a couple.  I expected `isOnlyUsedInZeroEqualityComparison()` to return true for such expressions but it only handles straight equality.  The case is folded by the subsequent code path but the emitted code is slightly different.  It seems it might be worth looking into enhancing the function to handle both `< 1` and `== 0` with unsigned operands (as well as the converse).


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D123818



More information about the llvm-commits mailing list