This is an archive of the discontinued LLVM Phabricator instance.

[ISEL] Canonicalise constant splats to RHS.
ClosedPublic

Authored by sdesmalen on Jan 20 2022, 8:47 AM.

Details

Summary

SelectionDAG::getNode() canonicalises constants to the RHS if the
operation is commutative, but it doesn't do so for constant splat
vectors. Doing this early helps making certain folds on vector types,
simplifying the code required for target DAGCombines that are enabled
before Type legalization.

Somewhat to my surprise, DAGCombine doesn't seem to traverse the
DAG in a post-order DFS, so at the time of doing some custom fold where
the input is a MUL, DAGCombiner::visitMUL hasn't yet reordered the
constant splat to the RHS.

This patch leads to a few improvements, but also a few minor regressions,
which I traced down to D46492. When I tried reverting this change to see
if the changes were still necessary, I ran into some segfaults. Not sure
if there is some latent bug there.

Diff Detail

Event Timeline

sdesmalen created this revision.Jan 20 2022, 8:47 AM
sdesmalen requested review of this revision.Jan 20 2022, 8:47 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 20 2022, 8:47 AM
craig.topper added inline comments.Jan 20 2022, 8:57 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5612–5613

We could move the dyn_casts below the isCommutative block and fully rely on isConstantIntBuildVectorOrConstantInt inside the if. That would remove the need to swap the N1C/N2C.

5615–5619

Just to confirm, This will also handle SPLAT_VECTOR despite its name. Right?

sdesmalen updated this revision to Diff 401762.Jan 20 2022, 1:16 PM

Moved dyn_casts below the isCommutative block.

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
5612–5613

Good point.

5615–5619

Yes, that's correct.

craig.topper accepted this revision.Jan 20 2022, 2:15 PM

LGTM

llvm/test/CodeGen/AArch64/unfold-masked-merge-vector-variablemask-const.ll
132

This appears to now produce the same codegen we get if this xor is commuted in the IR. So at least we're consistent now. So I don't think that should block this patch. I assume InstCombine would usually have commuted the Xor or folded this pattern to an Or anyway.

This revision is now accepted and ready to land.Jan 20 2022, 2:15 PM
This revision was landed with ongoing or failed builds.Jan 24 2022, 1:39 AM
This revision was automatically updated to reflect the committed changes.