[PATCH] D68949: [DAGCombiner] fold select-of-constants based on sign-bit test

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 14 10:15:45 PDT 2019


spatel created this revision.
spatel added reviewers: lebedev.ri, RKSimon, xbolva00.
Herald added subscribers: hiraditya, mcrosier.
Herald added a project: LLVM.

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.


https://reviews.llvm.org/D68949

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/test/CodeGen/X86/select-sra.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68949.224873.patch
Type: text/x-patch
Size: 9002 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191014/ae8114de/attachment.bin>


More information about the llvm-commits mailing list