This is an archive of the discontinued LLVM Phabricator instance.

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

Authored by bcl5980 on Dec 6 2022, 2:45 AM.

Diff Detail

Event Timeline

bcl5980 created this revision.Dec 6 2022, 2:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 6 2022, 2:45 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
bcl5980 requested review of this revision.Dec 6 2022, 2:45 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 6 2022, 2:45 AM
RKSimon added inline comments.Dec 8 2022, 5:57 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2799

Sorry if I've missed something (looooong day) - but how can this match as a SelectInst if its already matched as m_LogicalAnd above?

lebedev.ri added inline comments.Dec 8 2022, 5:58 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2799
/// Matches L && R either in the form of L & R or L ? R : false.
/// Note that the latter form is poison-blocking.
template <typename LHS, typename RHS>
inline LogicalOp_match<LHS, RHS, Instruction::And> m_LogicalAnd(const LHS &L,
                                                                const RHS &R) {
  return LogicalOp_match<LHS, RHS, Instruction::And>(L, R);
}

/// Matches L && R where L and R are arbitrary values.
inline auto m_LogicalAnd() { return m_LogicalAnd(m_Value(), m_Value()); }
RKSimon added inline comments.Dec 8 2022, 6:07 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2799

Cheers - so what do we gain from matching with m_LogicalAnd first and then checking isa<SelectInst> ?

bcl5980 updated this revision to Diff 481260.Dec 8 2022, 6:13 AM

fix tests.

bcl5980 added inline comments.Dec 8 2022, 6:15 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2799

Hi @RKSimon , this should be first part of this change. Later I will send two folllow up patch for the case like:
and + and + select, and + select + select.
So check the m_LogicalAnd first is necessary.

spatel added inline comments.Dec 9 2022, 10:24 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2795

Use the same names as the code for consistency:

// (A && B) || (C && B) --> (A || C) && B

With logical-and/or, the pattern is not commutative. I think the code is correct, but it might be better to put this kind of label just above the line that does the transform, so it is clear that order of the common operand does matter.

bcl5980 updated this revision to Diff 482003.Dec 11 2022, 11:28 PM

address comments

bcl5980 updated this revision to Diff 482070.Dec 12 2022, 4:20 AM

rebase code

I rebase the code.
I think rG9ddff66d0c9c3e18d56e6b20aa26a2a8cdfb6d2b can cover part of this path. And it can't cover some cases like:

@logic_and_logic_or_3
@logic_and_logic_or_4
@logic_and_logic_or_6
@logic_and_logic_or_7

If any of you think this is redundant I can abandon it.

spatel accepted this revision.Dec 13 2022, 7:03 AM

I'm not sure how much overlap we have vs. generality for these kinds of patterns, but this is still showing improvements, so LGTM.

This revision is now accepted and ready to land.Dec 13 2022, 7:03 AM

This patch is not rebased, as per the test diffs.

This patch is not rebased, as per the test diffs.

Do you mean rGa888825aeef8d6592c6cf5f4e5854cc39af49633 update test will add {{[^@]+}} for every test?
It looks latest code already revert it by rG697bfa40a3f853d8b9103ead53cd80a4f7c5a7df.

This revision was landed with ongoing or failed builds.Dec 13 2022, 12:43 PM
This revision was automatically updated to reflect the committed changes.

This patch is not rebased, as per the test diffs.

Do you mean rGa888825aeef8d6592c6cf5f4e5854cc39af49633 update test will add {{[^@]+}} for every test?
It looks latest code already revert it by rG697bfa40a3f853d8b9103ead53cd80a4f7c5a7df.

No, i'm saying that the test diff was showing spurious/misleading changes.
As you've noted, many of those tests (e.g. @logic_and_logic_or_1) got handled
in another patch, but the diff was still showing as-if they weren't handled before this patch.

chfast added a subscriber: chfast.Jul 18 2023, 2:58 AM
chfast added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2804

The new created select instructions do not copy metadata from the original ones. Is this worth doing so?

nikic added inline comments.Jul 18 2023, 5:41 AM
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
2804

I don't think metadata can be safely preserved in this transform.