This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold signed min/max intrinsics with negated operands
ClosedPublic

Authored by spatel on Aug 16 2021, 2:18 PM.

Details

Summary

If both operands are negated, we can invert the min/max and do the negation after:
smax (neg nsw X), (neg nsw Y) --> neg nsw (smin X, Y)
smin (neg nsw X), (neg nsw Y) --> neg nsw (smax X, Y)

This is visible as a remaining regression in D98152. I don't see a way to generalize this for 'unsigned' or adapt Negator to handle it. This only appears to be safe with 'nsw':
https://alive2.llvm.org/ce/z/GUy1zJ

Diff Detail

Event Timeline

spatel created this revision.Aug 16 2021, 2:18 PM
spatel requested review of this revision.Aug 16 2021, 2:18 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 16 2021, 2:18 PM

adapt Negator to handle it

Hmm, thanks for the hint, i was looking for this fold for a while :)
Indeed, negator does not track NSW-ness. What we can do for the signed variant is:
https://alive2.llvm.org/ce/z/MXZXKp aka https://alive2.llvm.org/ce/z/VJwMwD

lebedev.ri accepted this revision.Aug 16 2021, 2:44 PM

LG.
I'm not sure we have similar fold for umin/umax.
There, only inversion comes to mind https://alive2.llvm.org/ce/z/4RcW8r, but apparently we already do that for all 4 variants.

This revision is now accepted and ready to land.Aug 16 2021, 2:44 PM
This revision was landed with ongoing or failed builds.Aug 17 2021, 5:16 AM
This revision was automatically updated to reflect the committed changes.