This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold copysign with constant sign argument to (fneg+)fabs
ClosedPublic

Authored by spatel on Nov 27 2019, 9:36 AM.

Details

Summary

If the sign of the sign argument is known (this could be extended to use ValueTracking), then we can use fneg+fabs to clear/set the sign bit of the magnitude argument.
http://llvm.org/docs/LangRef.html#llvm-copysign-intrinsic

This transform is already done in DAGCombiner, but we can do it sooner in IR as suggested in PR44153:
https://bugs.llvm.org/show_bug.cgi?id=44153

We have effectively no analysis for copysign in IR, so I think it's ok to take the unusual step of increasing the number of IR instructions for the negative constant case.

Diff Detail

Event Timeline

spatel created this revision.Nov 27 2019, 9:36 AM
Herald added a project: Restricted Project. · View Herald TranscriptNov 27 2019, 9:36 AM
efriedma accepted this revision.Nov 27 2019, 9:55 AM

LGTM. I agree it makes sense to consider fabs+fneg canonical, as opposed to an "expensive" operation like fcopysign.

This revision is now accepted and ready to land.Nov 27 2019, 9:55 AM
This revision was automatically updated to reflect the committed changes.