[PATCH] D128089: [InstCombine] Fold strncmp of constant arrays and variable size

Martin Sebor via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 17 12:05:28 PDT 2022


msebor created this revision.
msebor added reviewers: courbet, nikic, xbolva00.
Herald added a subscriber: hiraditya.
Herald added a project: All.
msebor requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This change extends the solution accepted in D127766 <https://reviews.llvm.org/D127766> to `strncmp`: it simplifies `strncmp(A, B, N)` calls with constant  `A` and `B` and variable `N` to the equivalent of

  N <= Pos ? 0 : (A < B ? -1 : B < A ? +1 : 0)

where `Pos` is the offset of either the first mismatch between `A` and `B` or the terminating null character if both `A` and `B` are equal strings.

I couldn't find a good way to preserve the form of the loop in `optimizeMemCmpVarSize` so this change reverts it to the form originally proposed in D127766 <https://reviews.llvm.org/D127766>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128089

Files:
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
  llvm/test/Transforms/InstCombine/strncmp-5.ll
  llvm/test/Transforms/InstCombine/strncmp-6.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128089.437997.patch
Type: text/x-patch
Size: 23723 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220617/cdba71f4/attachment.bin>


More information about the llvm-commits mailing list