This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] support fold select(X|Y,X|Y,X) to X|Y
ClosedPublic

Authored by HerrCai0907 on Apr 13 2023, 3:38 PM.

Details

Summary

Fixed: https://github.com/llvm/llvm-project/issues/62113
Add addtional check in visitSelectInst to:

  1. match select(X|Y==0, X, X|Y) and replaced with X|Y
  2. match select(X&Y==-1, X, X&Y) and replaced with X&Y

alive proof:
https://alive2.llvm.org/ce/z/4qHmv-
https://alive2.llvm.org/ce/z/c2MBGy

Diff Detail

Event Timeline

HerrCai0907 created this revision.Apr 13 2023, 3:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2023, 3:38 PM
HerrCai0907 requested review of this revision.Apr 13 2023, 3:38 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 13 2023, 3:38 PM
bcl5980 added a subscriber: bcl5980.EditedApr 13 2023, 7:55 PM

We can extend it to and also:

X & Y == -1 ? X : X & Y --> X & Y

And please try to attach or pattern's alive2 proof like this:
https://alive2.llvm.org/ce/z/c2MBGy

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
3585 ↗(On Diff #513376)

If the cmp LHS is FalseVal, pred should be ne here.
If the cmp LHS is TrueVal, pred should be eq I guess.

3587 ↗(On Diff #513376)

TrueVal == X || TrueVal == Y?

HerrCai0907 edited the summary of this revision. (Show Details)Apr 13 2023, 11:36 PM
HerrCai0907 edited the summary of this revision. (Show Details)Apr 13 2023, 11:52 PM
HerrCai0907 marked 2 inline comments as done.
HerrCai0907 edited the summary of this revision. (Show Details)

add select(X&Y==-1, X, X&Y) -> X&Y

remove dbg header

nikic added a subscriber: nikic.

As this does not produce new instructions, it should be part of InstSimplify, somewhere around simplifySelectWithICmpCond.

move to simplifySelectWithICmpCond

nikic added a comment.Apr 16 2023, 1:41 AM

Please add one test using vectors, and a negative test with the wrong constant in the icmp.

llvm/lib/Analysis/InstructionSimplify.cpp
4594

Already checked above.

llvm/test/Transforms/InstSimplify/select_or_and.ll
2

instsimplify

5

Please name instructions in test (run through -passes=instnamer).

add vector and more negative testcase

HerrCai0907 marked 3 inline comments as done.Apr 16 2023, 5:28 AM
nikic added inline comments.Apr 16 2023, 7:02 AM
llvm/test/Transforms/InstSimplify/select_or_and.ll
2

What I meant is to run the test through opt -passes=instnamer and commit the result, so the input does not contain any %0 etc. instnamer shouldn't be part of the RUN line.

HerrCai0907 marked an inline comment as done.Apr 16 2023, 11:22 AM

sorry for misunderstanding, fixed.

goldstein.w.n added inline comments.Apr 16 2023, 4:22 PM
llvm/lib/Analysis/InstructionSimplify.cpp
4585

Should we also have select(X|Y != 0, X | Y, X or Y)?

4588

You could also do:
select(X | Y == X, X, X | Y) -> X | Y
and
select (X|Y == Y, Y, X|Y) -> X|Y
https://alive2.llvm.org/ce/z/hawAog

goldstein.w.n added inline comments.Apr 16 2023, 4:24 PM
llvm/test/Transforms/InstSimplify/select_or_and.ll
11

Can you split the next tests to a prior patch so we can see the diff this patch generates?

HerrCai0907 edited the summary of this revision. (Show Details)Apr 16 2023, 11:35 PM
HerrCai0907 marked an inline comment as done.Apr 16 2023, 11:46 PM
HerrCai0907 added inline comments.
llvm/lib/Analysis/InstructionSimplify.cpp
4585

ne will be changed to eq in line 4498

4588

I think it can be done in next patch

llvm/test/Transforms/InstSimplify/select_or_and.ll
11

before this patch, opt won't do any optimization for this code. Diff between the original code and the checked code is what this patch does.

junaire added inline comments.
llvm/test/Transforms/InstSimplify/select_or_and.ll
11

That's expected. We want to see opt does no optimization for the tests so if you can split the tests into a parent revision then we can see very clearly the changes your patch does in the Phab. Similar to (https://github.com/llvm/llvm-project/commit/87c97d052cfd6dc0c03e5e36be1315f659f9f0ac)

nikic added inline comments.Apr 17 2023, 7:56 AM
llvm/lib/Analysis/InstructionSimplify.cpp
4588

We already handle this case using generic select equivalence replacement. The only reason we need this patch is that the select replacement needs to follow the implication X | Y == 0 => X == 0. (We could generalize the generic code to do that, but I'm fine with keeping it as a special case for now.)

HerrCai0907 marked an inline comment as done.

split two patch, one for add test, another for add optimization

HerrCai0907 marked 2 inline comments as done.Apr 17 2023, 1:00 PM

done. Thanks for your advice

nikic accepted this revision.Apr 17 2023, 1:04 PM

LGTM

This revision is now accepted and ready to land.Apr 17 2023, 1:04 PM
This revision was landed with ongoing or failed builds.Apr 17 2023, 1:07 PM
This revision was automatically updated to reflect the committed changes.
Allen added a subscriber: Allen.Apr 22 2023, 8:25 PM