This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold logic-and/logic-or by distributive laws part2
ClosedPublic

Authored by bcl5980 on Dec 8 2022, 6:38 AM.

Diff Detail

Event Timeline

bcl5980 created this revision.Dec 8 2022, 6:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 6:38 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
bcl5980 requested review of this revision.Dec 8 2022, 6:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 8 2022, 6:38 AM
bcl5980 updated this revision to Diff 482604.Dec 13 2022, 12:54 PM
bcl5980 updated this revision to Diff 482610.Dec 13 2022, 1:12 PM
bcl5980 edited the summary of this revision. (Show Details)
bcl5980 updated this revision to Diff 482622.Dec 13 2022, 1:51 PM

simpilify code

spatel added inline comments.Jan 3 2023, 9:40 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2777–2778

Can we add some description of the different possibilities?

I know that handling all of the patterns poison-safely is not uniform, but what if we use freeze to make it safe with regular logic ops instead:
https://alive2.llvm.org/ce/z/xb_moG

There is an existing code example for a similar transform below this block of code - search for "MayNeedFreeze".

llvm/test/Transforms/InstCombine/select-factorize.ll
207

I am struggling to see all of the possible patterns, but this should fold:
https://alive2.llvm.org/ce/z/yXjD82

Does handling that case make the code for this patch simpler?
It might be better to split up the changes either way, just so we can be sure that we are not missing anything.

bcl5980 updated this revision to Diff 486204.Jan 4 2023, 2:24 AM

update code first to review. Will add detail comments later.

bcl5980 added inline comments.Jan 4 2023, 2:25 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2777–2778

So which way are you prefer now? Not uniform code without freeze instruction or use freeze asap to make code easier?

spatel added inline comments.Jan 4 2023, 9:35 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2777–2778

If there are no known regressions, let's go straight to the code with freeze. That should be the ideal form for most of these patterns.

bcl5980 added inline comments.Jan 4 2023, 8:33 PM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2777–2778

I try a lot of ways to add freeze into the code today. But it looks it will make the code more complicate.
Current code already fix all cases without freeze. What freeze can get benefit is convert logical-and to normal and, logical-or to normal or.
So the question becomes which pattern is better freeze + and + or, select + select? Only if the first pattern is better we need to add freeze I think.
I'm sorry I haven't find a elegant way to do it. Can you give me some suggestions for that?

spatel added a subscriber: nikic.Jan 5 2023, 11:08 AM
spatel added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2777–2778

I don't have any good suggestions, so I suppose this is ok.

It might be possible to reduce select in some of these patterns to a real logic (and/or) instruction as a smaller step.

@nikic or @lebedev.ri might have other ideas.

The tests looks complete, so presumably the logic is correct - have you verified that everything passes with Alive2?

bcl5980 edited the summary of this revision. (Show Details)Jan 5 2023, 6:11 PM
bcl5980 added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2777–2778

I add all proofs in the patch description.

spatel accepted this revision.Jan 6 2023, 8:31 AM

We may still want some folds to reduce selects to real logic, but this is an improvement, so LGTM.

This revision is now accepted and ready to land.Jan 6 2023, 8:31 AM
This revision was landed with ongoing or failed builds.Jan 8 2023, 6:21 PM
This revision was automatically updated to reflect the committed changes.