This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] add fold for vselect based on mask of signbit, part 3
ClosedPublic

Authored by spatel on Nov 8 2021, 12:30 PM.

Details

Summary

(Cond0 s> -1) ? N1 : 0 --> ~(Cond0 s>> BW-1) & N1

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

This was suggested as a potential enhancement in D113212 (also 7e30404c3b6c ).
This looks like an improvement for AArch that could be generalized ( X > -1 --> X >= 0 ).
For x86, we have a counter-acting fold for most cases that turns the shift+not back into a setcc, so that needs a work-around to get more cases to use "pandn".

Diff Detail

Event Timeline

spatel created this revision.Nov 8 2021, 12:30 PM
spatel requested review of this revision.Nov 8 2021, 12:30 PM
Herald added a project: Restricted Project. · View Herald TranscriptNov 8 2021, 12:30 PM
RKSimon added inline comments.Nov 10 2021, 11:14 AM
llvm/test/CodeGen/X86/vselect-zero.ll
977

Any ideas why AVX2 fails to fold to ANDN(SRA(X,31),Y) ?

spatel marked an inline comment as done.Nov 10 2021, 12:32 PM
spatel added inline comments.
llvm/test/CodeGen/X86/vselect-zero.ll
977

Yes - it's actually most of SSE and AVX1/2. We have a fold that turns "not (sra X, BW-1)" into "X > -1", so we need to match that instead.
I have an x86 patch in progress for it and should have that posted soon.

I added more tests with:
51baafd2382283c9

RKSimon accepted this revision.Nov 10 2021, 1:44 PM

LGTM - cheers

This revision is now accepted and ready to land.Nov 10 2021, 1:44 PM
lebedev.ri accepted this revision.Nov 10 2021, 1:51 PM
lebedev.ri added a subscriber: lebedev.ri.
lebedev.ri added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
9591

Notice the symmetry, there's final 4'th pattern there:
(Cond0 s> -1) ? -1 : N1 --> ~(Cond0 s>> BW-1) | N1
https://alive2.llvm.org/ce/z/ZefLue

lebedev.ri added inline comments.Nov 10 2021, 1:53 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
9591

... but we don't have a hasOrNot() hook, so only adding a comment should be fine i suppose.

spatel marked 3 inline comments as done.Nov 11 2021, 6:03 AM
spatel added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
9591

Yes, there's a potential "part 4" to this series. I'll add the TODO comment. There are also swapped variants for all four of these patterns.

There are also several open questions of canonicalization in IR - and the interaction of rules for {preds, selects, constants, min/max} are complicated.

I got "part 2" in this set wrong. I chose the swapped form that is currently not canonical:
https://alive2.llvm.org/ce/z/e4o96b

We probably want to match all of the swapped forms in SDAG to be more robust/consistent. I'm not sure what forms we'll end up favoring in IR, and we may not always be able to canonicalize if there are extra uses.

This revision was landed with ongoing or failed builds.Nov 11 2021, 7:34 AM
This revision was automatically updated to reflect the committed changes.
spatel marked an inline comment as done.