This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] fold select-of-constants based on sign-bit test
ClosedPublic

Authored by spatel on Oct 14 2019, 10:14 AM.

Details

Summary

Examples:

i32 X > -1 ? C1 : -1 --> (X >>s 31) | C1
i8 X < 0 ? C1 : 0 --> (X >>s 7) & C1

This is a small generalization of a fold requested in PR43650:
https://bugs.llvm.org/show_bug.cgi?id=43650

The sign-bit of the condition operand can be used as a mask for the true operand:
https://rise4fun.com/Alive/paT

Note that we already handle some of the patterns (isNegative + scalar) because there's an over-specialized, yet over-reaching fold for that in foldSelectCCToShiftAnd(). It doesn't use any TLI hooks, so I can't easily rip out that code even though we're duplicating part of it here. This fold is guarded by TLI.convertSelectOfConstantsToMath(), so it should not cause problems for targets that prefer select over shift.

Also worth noting: I thought we could generalize this further to include the case where the true operand of the select is not constant, but Alive says that may allow poison to pass through where it does not in the original select form of the code.

Diff Detail

Event Timeline

spatel created this revision.Oct 14 2019, 10:14 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2019, 10:14 AM
craig.topper added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
8208

What about i8 X > -1 ? 0 : C1 or do we canonicalize that to i8 X < 0 ? C1 : 0?

spatel marked an inline comment as done.Oct 14 2019, 10:42 AM
spatel added inline comments.
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
8208

Instcombine canonicalizes those commuted patterns in IR, but it's not handled in SDAG. Worth duplicating?

craig.topper added inline comments.Oct 14 2019, 12:50 PM
llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
8208

If InstCombine gets it that's probably fine. Can we add a comment here?

spatel updated this revision to Diff 224893.Oct 14 2019, 1:07 PM
spatel marked an inline comment as done.

Patch updated:
Added code comment about the inverted/commuted variations of these patterns.

This revision is now accepted and ready to land.Oct 14 2019, 1:46 PM
This revision was automatically updated to reflect the committed changes.