This is an archive of the discontinued LLVM Phabricator instance.

[SDAG] fold bitwise logic with shifted operands
ClosedPublic

Authored by spatel on Feb 24 2022, 1:47 PM.

Details

Summary

LOGIC (LOGIC (SH X0, Y), Z), (SH X1, Y) --> LOGIC (SH (LOGIC X0, X1), Y), Z

https://alive2.llvm.org/ce/z/QmR9rR

This is effectively a reassociation + factoring fold. The common shift operation is moved after a bitwise logic op on 2 input operands.
We get simpler cases of these patterns in IR, but I suspect we would miss all of these exact tests in IR too. We also handle the simpler form of this plus several other folds in DAGCombiner::hoistLogicOpWithSameOpcodeHands().

This is a partial implementation of a transform suggested in D111530 (only handles 'or' bitwise logic as a first step - need to stamp out more tests for other opcodes).
Several of the same tests added for D111530 are altered here (but not fully optimized). I'm not sure yet if this would help/hinder that patch, but this should be an improvement for all tests added with ecf606cb4329ae since it removes a shift operation in those examples.

Diff Detail

Event Timeline

spatel created this revision.Feb 24 2022, 1:47 PM
spatel requested review of this revision.Feb 24 2022, 1:47 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 24 2022, 1:47 PM
RKSimon accepted this revision.Feb 25 2022, 8:00 AM

LGTM with one (optional) minor

This revision is now accepted and ready to land.Feb 25 2022, 8:00 AM

LGTM with one (optional) minor

Am I missing it or did the optional minor not make it to phabricator?

LGTM with one (optional) minor

Am I missing it or did the optional minor not make it to phabricator?

It didn't make it - if you create/delete/undo comments phab sometimes swallows them :-(

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
6705

Maybe add an assert that the logic opcode is ISD::OR until we have actual uses for AND/XOR?

spatel edited the summary of this revision. (Show Details)Feb 25 2022, 9:22 AM
spatel marked an inline comment as done.Feb 25 2022, 9:34 AM
spatel added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
6705

Sure - an assert will be good even after the planned follow-ups for the other bitwise logic. I'll put a TODO comment on top of that.

spatel updated this revision to Diff 411437.Feb 25 2022, 9:35 AM
spatel marked an inline comment as done.

Added assert/todo for ISD::OR opcode.

This revision was landed with ongoing or failed builds.Feb 27 2022, 6:54 AM
This revision was automatically updated to reflect the committed changes.