This is an archive of the discontinued LLVM Phabricator instance.

DAG combine "and|or (select c, -1, 0), x" -> "select c, x, 0|-1"
ClosedPublic

Authored by rampitec on Jun 18 2018, 3:14 PM.

Details

Summary

Allowed folding for "and/or" binops with non-constant operand if
arguments of select are 0/-1 values.

Normally this code with "and" opcode does not get to a DAG combiner
and simplified yet in the InstCombine. However AMDGPU produces it
during lowering and InstCombine has no chance to optimize it out.

In turn the same pattern with "or" opcode can reach DAG.

Diff Detail

Repository
rL LLVM

Event Timeline

rampitec created this revision.Jun 18 2018, 3:14 PM
rampitec updated this revision to Diff 151831.Jun 18 2018, 5:16 PM

Updated x86 test after rebase.

rampitec updated this revision to Diff 151955.Jun 19 2018, 11:26 AM

Updated test.

rampitec updated this revision to Diff 152147.Jun 20 2018, 1:33 PM
rampitec edited the summary of this revision. (Show Details)

Rebased to master.

All prerequisites are submitted, this change is ready now.

spatel added inline comments.Jun 20 2018, 4:39 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
1904–1905 ↗(On Diff #152147)

This deserves an example in the comment since it doesn't match the formula below here.

Something like:
and (select Cond, 0, -1), X --> select Cond, 0, X
or X, (select Cond, -1, 0) --> select Cond, -1, X

1908–1911 ↗(On Diff #152147)

This should handle vector types. Currently, it will crash:

define <4 x i32> @select_or1_vec(i32 %x, <4 x i32> %y) {
  %c = icmp slt i32 %x, 11
  %s = select i1 %c, <4 x i32> zeroinitializer, <4 x i32><i32 -1, i32 -1, i32 -1, i32 -1>
  %a = or <4 x i32> %y, %s
  ret <4 x i32> %a
}
test/CodeGen/X86/dagcombine-select.ll
12 ↗(On Diff #152147)

We need some tests where the select's true value is the -1.

rampitec marked 3 inline comments as done.Jun 20 2018, 5:42 PM
rampitec added inline comments.
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
1908–1911 ↗(On Diff #152147)

Thank you!

rampitec updated this revision to Diff 152207.Jun 20 2018, 5:43 PM
rampitec marked an inline comment as done.

Addressed review comments.

spatel accepted this revision.Jun 21 2018, 7:39 AM

LGTM.

This revision is now accepted and ready to land.Jun 21 2018, 7:39 AM
This revision was automatically updated to reflect the committed changes.