This is an archive of the discontinued LLVM Phabricator instance.

TargetLowering: Improve handling of (setcc ([sz]ext x) 0, cc) in SimplifySetCC
ClosedPublic

Authored by tstellarAMD on Nov 27 2015, 3:10 AM.

Details

Summary

When SimplifySetCC sees a setcc node that compares the result of a
value extension operation with a constant, it tries to simplify the
setcc node by eliminating the extension and shrinking the constant.

If shrinking the inputs to setcc is deemed not desirable by the target
(e.g. the target does not want a setcc comparing i1 values), then it
is still possible to optimize this sequence in some cases.

This patch adds the following combines to SimplifySetCC when shrinking setcc
inputs is not desirable:

(setcc ([sz]ext (setcc x, y, cc)), 0, setne) -> (setcc (x, y, cc))
(setcc ([sz]ext (setcc x, y, cc)), 0, seteq) -> (setcc (x, Y, !cc))

There are no tests for this yet, but once AMDGPU correctly implements
TargetLowering::isTypeDesirableForOp(), this new combine will be
exercised by the existing CodeGen/AMDGPU/setcc-opt.ll test.

Diff Detail

Repository
rL LLVM

Event Timeline

tstellarAMD retitled this revision from to TargetLowering: Improve handling of (setcc ([sz]ext x) 0, cc) in SimplifySetCC.
tstellarAMD updated this object.
tstellarAMD added a reviewer: resistor.
tstellarAMD added a subscriber: llvm-commits.
arsenm added a subscriber: arsenm.Nov 30 2015, 3:53 PM
arsenm added inline comments.
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1391–1392 ↗(On Diff #41284)

What about the opposite compare against the true value case?

lib/CodeGen/SelectionDAG/TargetLowering.cpp
1391–1392 ↗(On Diff #41284)

I was concerned this case would break things, since targets have different values for boolean true. I think the current transform is safe for all targets.

arsenm added inline comments.Dec 10 2015, 11:37 AM
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1391–1392 ↗(On Diff #41284)

That's easy to check. If it's also just for i1 it doesn't really matter

Handle more variants of [sz]ext + setcc

arsenm added inline comments.Jan 11 2016, 2:16 PM
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1394–1398 ↗(On Diff #44536)

I think you should use TLI::isConstTrueVal/isConstFalseVal so this only happens if the correct BooleanContents is used

lib/CodeGen/SelectionDAG/TargetLowering.cpp
1394–1398 ↗(On Diff #44536)

Do you think this is still necessary since we are only looking at i1 setcc operations?

jroelofs added inline comments.
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1394–1398 ↗(On Diff #44536)

Yes. I know of at least one out-of-tree target where this matters.

Use isConstTrueVal and isConstFalseVal

arsenm added inline comments.Jan 13 2016, 10:06 AM
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1394–1396 ↗(On Diff #44765)

I didn't expect to still have the isOne/isAllOnesValue. I didn't realize that you would still need to check the compare type in addition to making sure the true value matched the extend type. In that case it might be be clearer to check the boolean content along with the value.

In either case this is a lot of parentheses so splitting these into a helper isExtendedTrueVal or something like that would help.

Add isExtendedTrueVal() helper.

arsenm accepted this revision.Jan 15 2016, 10:39 AM
arsenm added a reviewer: arsenm.

LGTM with that fixed

lib/CodeGen/SelectionDAG/TargetLowering.cpp
1248 ↗(On Diff #45011)

I think this should be !N->isNull(). If you have a constant true i1, it is -1 for example.

This revision is now accepted and ready to land.Jan 15 2016, 10:39 AM
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1248 ↗(On Diff #45011)

isOne() and isAllOnesValue() both return true for i1 1.

arsenm added inline comments.Jan 15 2016, 10:46 AM
lib/CodeGen/SelectionDAG/TargetLowering.cpp
1248 ↗(On Diff #45011)

OK, then it doesn't really matter

This revision was automatically updated to reflect the committed changes.