This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold memchr and strchr equality with first argument
ClosedPublic

Authored by msebor on Jun 30 2022, 12:51 PM.

Details

Summary

This change enhances memchr and strchr handling to simplify calls to the functions used in equality expressions with the first argument to at most two integer comparisons:

  • memchr(A, C, N) == A to N && *A == C for either a dereferenceable A or a nonzero N,
  • strchr(S, C) == S to *S == C for any S and C, and
  • strchr(S, '\0') == 0 to true for any S

Diff Detail

Event Timeline

msebor created this revision.Jun 30 2022, 12:51 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2022, 12:51 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
msebor requested review of this revision.Jun 30 2022, 12:51 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 30 2022, 12:51 PM
nikic added a comment.Jul 1 2022, 1:09 AM

This looks fine, only bit I'm uncertain about is the special-case for strchr(A, '\0') == null. Please precommit the tests to show current codegen. Possibly we can address that differently, in particular I notice that we're currently missing inbounds on GEPs, which might be limiting subsequent comparison folding.

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
299

NBytes is

301

static Value *memChrToCharCompare

1153

memcmp -> memchr

msebor updated this revision to Diff 441743.Jul 1 2022, 10:43 AM
msebor marked 3 inline comments as done.

Changes in revision 2 of the patch:

  • Fix spelling/typos.
  • Rebase on top of latest trunk with tests precommitted in rGd8b22243c8e9.
nikic accepted this revision.Jul 4 2022, 1:40 AM

LGTM

This revision is now accepted and ready to land.Jul 4 2022, 1:40 AM
This revision was landed with ongoing or failed builds.Jul 7 2022, 2:16 PM
This revision was automatically updated to reflect the committed changes.