Page MenuHomePhabricator

[SelectionDAG] Optimize build_vector of truncates and shifts
ClosedPublic

Authored by sebastian-ne on Feb 3 2020, 7:23 AM.

Details

Summary

Add a simplification to fuse a manual vector extract with shifts and
truncate into a bitcast.

Unpacking and packing values into vectors is only optimized with
extractelement instructions, not when manually unpacked using shifts
and truncates.
This patch simplifies shifts and truncates into a bitcast if possible.

Simplify (build_vec (trunc $1)

(trunc (srl $1 width))
(trunc (srl $1 (2 * width))) ...)

to (bitcast $1)

Diff Detail

Event Timeline

sebastian-ne created this revision.Feb 3 2020, 7:23 AM
arsenm added a subscriber: arsenm.Feb 3 2020, 7:32 AM
arsenm added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
17485–17487

Why does this matter? This doesn't touch memory

17503

You don't need the SDValue copy constructor here

sebastian-ne added inline comments.Feb 3 2020, 8:18 AM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
17485–17487

I think it does matter for the shifts and for bitcasting a vector to a number.
If I’m not mistaken, for big endian the function should convert like this:

Simplify (build_vec (trunc (srl $1 (2 * half-width)))
                    (trunc (srl $1 half-width))
                    (trunc $1) …)
to (bitcast $1)

That could be fixed easily, however the selection-dag bitcast documentation states that there may be more shuffling on big endian architectures?
https://llvm.org/doxygen/ISDOpcodes_8h_source.html#l00590

Remove unecessary copy constructor, thanks Matt.

nhaehnle accepted this revision.Feb 5 2020, 5:22 AM
nhaehnle added reviewers: spatel, lebedev.ri.

LGTM, but wait a few days to give others a chance to weigh in.

llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
17485–17487

Right, because bitcasts are defined as behaving as-if you store to and then load from memory. Which may well be quite problematic, but that's what the documentation says...

This revision is now accepted and ready to land.Feb 5 2020, 5:22 AM
This revision was automatically updated to reflect the committed changes.