This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold strnlen with a bound of zero and one.
ClosedPublic

Authored by msebor on Apr 14 2022, 1:18 PM.

Details

Summary

This patch in the strnlen series implements folding of calls with a bound of either zero or one.

Depends on D123815.

Diff Detail

Event Timeline

msebor created this revision.Apr 14 2022, 1:18 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2022, 1:18 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
msebor requested review of this revision.Apr 14 2022, 1:18 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 14 2022, 1:18 PM
msebor edited the summary of this revision. (Show Details)Apr 14 2022, 1:38 PM
msebor added reviewers: nikic, xbolva00.
nikic requested changes to this revision.Apr 14 2022, 1:46 PM
nikic added inline comments.
llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
242

nit: Indent not aligned.

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

Isn't this select the wrong way around? You're doing *s != 0 ? 0 : 1 here.

You might also want to directly emit the zext form if InstCombine is going to fold it anyway, so that would be zext(s != 0).

This revision now requires changes to proceed.Apr 14 2022, 1:46 PM
msebor marked an inline comment as done.Apr 25 2022, 12:25 PM
msebor added inline comments.
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
649

Yes, it is backwards. Good catch! The test is wrong too. I was worried about something like this happening after checking in the baseline tests first and then relying on update_test_checks.py to update the assertions.

msebor updated this revision to Diff 425004.Apr 25 2022, 1:01 PM

Fix logic error and use zext instead of select.

nikic added inline comments.Apr 25 2022, 1:10 PM
llvm/test/Transforms/InstCombine/strnlen-2.ll
92

Hm, how does this one get folded? It doesn't have either 0 or 1 as the bound.

msebor added inline comments.Apr 25 2022, 1:40 PM
llvm/test/Transforms/InstCombine/strnlen-2.ll
92

This is strnlen(C ? "123" : "12345", 6). (I use sN for a constant string of length N and aN for a non-const array of N elements.)

nikic added inline comments.Apr 25 2022, 1:51 PM
llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
648

This should be B., causes the build failure in pre-merge checks.

llvm/test/Transforms/InstCombine/strnlen-2.ll
92

I still don't see how this one would get folded as part of this patch. Are you sure it's not from one of the later patches?

msebor updated this revision to Diff 425055.Apr 25 2022, 4:16 PM

Fix merge problems.

msebor added inline comments.Apr 25 2022, 4:20 PM
llvm/test/Transforms/InstCombine/strnlen-2.ll
92

Yes, I screwed up the merge. I'm not used to working this way. I'm spending more time moving code around than actually developing it, and making mistakes in the process.

nikic accepted this revision.Apr 26 2022, 12:38 AM

LGTM

This revision is now accepted and ready to land.Apr 26 2022, 12:38 AM
This revision was automatically updated to reflect the committed changes.