This is an archive of the discontinued LLVM Phabricator instance.

[SimplifyLibCalls] Fix memchr opt to use CreateLogicalAnd
ClosedPublic

Authored by aqjune on Jun 25 2021, 2:23 AM.

Details

Summary

This fixes a bug at LibCallSimplifier::optimizeMemChr which does the following transformation:

// memchr("\r\n", C, 2) != nullptr -> (1 << C & ((1 << '\r') | (1 << '\n')))
// != 0
//   after bounds check.

As written above, a bounds check on C (whether it is less than integer bitwidth) is done before doing 1 << C otherwise 1 << C will overflow.
If the bounds check is false, the result of (1 << C & ...) must not be used at all, otherwise the result of shift (which is poison) will contaminate the whole results.
A correct way to encode this is select i1 (bounds check), (1 << C & ...), false because select does not allow the unused operand to contaminate the result.
However, this optimization was introducing and (bounds check), (1 << C & ...) which cannot do that.

The bug was found from compilation of this C++ code: https://reviews.llvm.org/rG2fd3037ac615#1007197

Diff Detail

Event Timeline

aqjune created this revision.Jun 25 2021, 2:23 AM
aqjune requested review of this revision.Jun 25 2021, 2:23 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 25 2021, 2:23 AM
aqjune updated this revision to Diff 354452.Jun 25 2021, 2:26 AM

clang-format

nikic accepted this revision.Jun 25 2021, 2:31 AM

LGTM

This revision is now accepted and ready to land.Jun 25 2021, 2:31 AM
This revision was landed with ongoing or failed builds.Jun 25 2021, 1:59 PM
This revision was automatically updated to reflect the committed changes.