This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold memrchr calls with a constant character.
ClosedPublic

Authored by msebor on Apr 12 2022, 1:36 PM.

Details

Summary

This change implements folding of memrchr calls with a constant array and character, analogous to memchr.

Depends on D123628.

Diff Detail

Event Timeline

msebor created this revision.Apr 12 2022, 1:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2022, 1:36 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
msebor requested review of this revision.Apr 12 2022, 1:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 12 2022, 1:36 PM
msebor edited the summary of this revision. (Show Details)Apr 12 2022, 1:51 PM
msebor updated this revision to Diff 422358.Apr 12 2022, 4:08 PM

Add context.

nikic requested changes to this revision.Apr 13 2022, 3:47 AM
nikic added inline comments.
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
944

Use NullPtr?

952

I don't think this is correct. Consider an unknown size and a string with two occurrences of C in it. Now assume that at runtime, the size is chosen to lie between the two occurrences of C. I believe your implementation will return null for that case, while it should return the first occurrence.

This revision now requires changes to proceed.Apr 13 2022, 3:47 AM
nikic added inline comments.Apr 13 2022, 3:56 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
952

Gah, I just realized that this code is only used for a known length, unlike the corresponding memchr() implementation. In this case EndOff will be the exact end offset, and not just an upper bound on it, and this code will always constant fold, so it's fine.

I think it would be clearer that this is a fully static if this directly emitted the appropriate result based on LenC->getValue().ule(Pos) rather than going through constant folding.

nikic added inline comments.Apr 13 2022, 4:04 AM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
952

Actually, if the length is constant, shouldn't this just directly return the GEP without any further comparisons?

msebor added inline comments.Apr 13 2022, 1:25 PM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
944

Sure.

952

You're right, thanks! Your comment makes me realize it's possible to also fold the call when n isn't known as long as there's just one occurrence of the character in the string. Let me do both.

msebor updated this revision to Diff 422638.Apr 13 2022, 1:27 PM

Simplify folding when Len is constant and handle subset of cases when it isn't.

nikic accepted this revision.Apr 14 2022, 5:46 AM

LG apart from test nits.

llvm/test/Transforms/InstCombine/memrchr-3.ll
151–164

Should pass 1 here to match test description.

198

Missing check lines?

This revision is now accepted and ready to land.Apr 14 2022, 5:46 AM
This revision was landed with ongoing or failed builds.Apr 26 2022, 1:03 PM
This revision was automatically updated to reflect the committed changes.