This is an archive of the discontinued LLVM Phabricator instance.

[DAG] Legalize abs(x) -> smax(x,sub(0,x)) iff smax/sub are legal
ClosedPublic

Authored by RKSimon on Nov 25 2020, 5:37 AM.

Details

Summary

If smax() is legal, this is likely to result in smaller codegen expansion for abs(x) than the xor(add,ashr) method.

This is also what powerpc has been doing for its abs implementation, so it lets us get rid of a load of custom lowering code there (and which was never updated when they added smax lowering).

Alive2 check : https://alive2.llvm.org/ce/z/xRk3cD

Diff Detail

Event Timeline

RKSimon created this revision.Nov 25 2020, 5:37 AM
RKSimon requested review of this revision.Nov 25 2020, 5:37 AM

Remind me, for ISD::ABS, what happens for INT_MIN input?
https://alive2.llvm.org/ce/z/_KMTWi
https://alive2.llvm.org/ce/z/p8CnZa

/// ABS - Determine the unsigned absolute value of a signed integer value of
/// the same bitwidth.
/// Note: A value of INT_MIN will return INT_MIN, no saturation or overflow
/// is performed.
ABS,
lebedev.ri accepted this revision.Nov 25 2020, 6:00 AM

Actually, never mind. The current expansion is identically undef-unsafe, iff ISD::ABS is ever undef-producing/inducing:
https://alive2.llvm.org/ce/z/eJwDHC
https://alive2.llvm.org/ce/z/z6fGbG

So looks good to me.

This revision is now accepted and ready to land.Nov 25 2020, 6:00 AM