This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] Add bswap(logic_op(bswap(x), y)) optimization
ClosedPublic

Authored by austin880625 on May 3 2023, 1:45 PM.

Details

Summary

This is the implementation of D149782

The patch implements a helper function that matches and fold the following cases in the DAGCombiner:

  1. bswap(logic_op(x, bswap(y))) -> logic_op(bswap(x), y)
  2. bswap(logic_op(bswap(x), y)) -> logic_op(x, bswap(y))
  3. bswap(logic_op(bswap(x), bswap(y))) -> logic_op(x, y) in multiuse case, which still reduces the number of instructions.

The helper function accepts SDValue with BSWAP and BITREVERSE opcode. This patch folds the BSWAP cases and remain the BITREVERSE optimization in the future

Diff Detail

Event Timeline

austin880625 created this revision.May 3 2023, 1:45 PM
austin880625 requested review of this revision.May 3 2023, 1:45 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 3 2023, 1:45 PM

include full patch context

goldstein.w.n added inline comments.May 3 2023, 2:29 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
9985

There is no opcode parameter. Maybe 'bswap or bitreverse nodes'?

The summary/commit message should explain the transform you're adding.

RKSimon added inline comments.May 4 2023, 5:18 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
10000

OldLHS might be from a node with multiple results - you need OldLHS->hasOneUse() (same for OldRHS below)

goldstein.w.n added inline comments.May 4 2023, 8:50 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
10000

Thats already there in: if (OldLHS.getOpcode() == Opcode && OldLHS.hasOneUse()) { no?

Although I realized we will miss the following case:
(bswap (logic (bswap X), (bswap Y)) -> (logic X, Y)
if (bswap X) and / or (bswap Y) are multi-use.

Even if both arms are mult-use we still save an instruction doing this optimizations.
Maybe there should be a check if both arms match opcode, and if so, to ignore
multiuse check?

austin880625 edited the summary of this revision. (Show Details)

Add implementation of bswap(logic_op(bswap(x), bswap(y))) -> logic_op(x, y) fold
Update some comments that are not aligned with code

goldstein.w.n accepted this revision.May 4 2023, 8:32 PM

LGTM. Note, I'm not a maintainer so please wait a day or so before pushing so others can check it out.

Maybe follow up patch to enable for bitreverse?

This revision is now accepted and ready to land.May 4 2023, 8:32 PM

update with the base commit of test cases

austin880625 updated this revision to Diff 520122.EditedMay 6 2023, 1:18 PM

update with test cases and rebase onto main

RKSimon requested changes to this revision.May 7 2023, 7:20 AM

Please can you rebase on D149782 ?

This revision now requires changes to proceed.May 7 2023, 7:20 AM
austin880625 added a comment.EditedMay 7 2023, 8:19 AM

oh by rebase onto main I mean I rebased the two commit(for D149782 and for D149783) in my branch onto the main branch together, so this patch is still based on the commit for D149782. For clarity these are the commands I used:

git diff main HEAD~ -U999999 > ../62236-DAG-test.patch #for D149782, based on previously pulled main branch
git diff HEAD~ HEAD -U999999 > ../62236-DAG-impl.patch #for this D149783

Let me know if I did something wrong here(or there's better commit arrangement

oh by rebase onto main I mean I rebased the two commit(for D149782 and for D149783) in my branch onto the main branch together, so this patch is still based on the commit for D149782. For clarity these are the commands I used:

git diff main HEAD~ -U999999 > ../62236-DAG-test.patch #for D149782, based on previously pulled main branch
git diff HEAD~ HEAD -U999999 > ../62236-DAG-impl.patch #for this D149783

The issue is that you updated purely D149782 with the new tests, but the current version of D149783 is not based on the version of D149782 with the new tests.

Yes, we're missing the new arm test coverage

@goldstein.w.n Sorry I downloaded the raw patch and compared with my git diff one without finding actual difference. Might need more clarification on how to confirm "not based on the version of D149782"

From the diff with previous patch the bs_xor_rhs_brev32 test case was removed in the diff context, so I thought it should be based on the new test cases alr? However the test cases in ARM backend only contains negative tests so they have no diff in this patch, maybe I should also copy some positive ones from x86.

Yes, we're missing the new arm test coverage

noted, adding the ARM positive test cases

contain the diff with updated positive tests(based on new test case change)

RKSimon added inline comments.May 8 2023, 3:50 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
10003

(style) Break up if-else chains as each cases returns

fix if styling on early return

goldstein.w.n accepted this revision.May 15 2023, 6:59 PM

LGTM. @RKSimon do you still have concerns?

RKSimon accepted this revision.May 16 2023, 1:26 AM

LGTM - @goldstein.w.n please can you help commit Austin's bswap patches?

This revision is now accepted and ready to land.May 16 2023, 1:26 AM

LGTM - @goldstein.w.n please can you help commit Austin's bswap patches?

Sure.

This revision was landed with ongoing or failed builds.May 16 2023, 4:58 PM
This revision was automatically updated to reflect the committed changes.