This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner][RISCV] Improve computeKnownBits for (smax X, C) where C is non-negative.
ClosedPublic

Authored by craig.topper on Jun 2 2022, 10:12 AM.

Details

Summary

If C is non-negative, the result of the smax must also be
non-negative, so all sign bits of the result are 0.

This allows DAGCombiner to remove a zext_inreg in the modified test.
This zext_inreg started as a sext that became zext before type
legalization then was promoted to a zext_inreg.

Diff Detail

Event Timeline

craig.topper created this revision.Jun 2 2022, 10:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 2 2022, 10:12 AM
craig.topper requested review of this revision.Jun 2 2022, 10:12 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 2 2022, 10:12 AM
reames accepted this revision.Jun 2 2022, 11:59 AM
reames added a subscriber: reames.

LGTM

Seems like maybe something we should generalize for the inverse smin with negative value case and sink into known bits, but that's an entirely optional follow on.

This revision is now accepted and ready to land.Jun 2 2022, 11:59 AM
foad added inline comments.Jun 6 2022, 4:02 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3709

If you break here you don't call smin/smax below, which could compute known low order bits in the result. Wouldn't it be better to add this new code after the calls to smin/smax below, to improve the result that they calculate?

craig.topper added inline comments.Jun 6 2022, 9:31 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3709

I suppose so. I hadn't realized we could compute the lower order bits. I guess there's no test coverage for the constant case. Does the same issue exist with the clamp pattern code that was already here?

foad added inline comments.Jun 6 2022, 12:30 PM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
3709

Yes, it looks like that has the same problem.