This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Fold abs of known negative operand
ClosedPublic

Authored by nikic on Sep 5 2020, 8:37 AM.

Details

Summary

If we know that the abs operand is known negative, we can replace it with a neg.

To avoid computing known bits twice, I've removed the fold for the non-negative case from InstSimplify. Both the non-negative and the negative case are handled by InstCombine now, with one known bits call.

Diff Detail

Event Timeline

nikic created this revision.Sep 5 2020, 8:37 AM
Herald added a project: Restricted Project. · View Herald TranscriptSep 5 2020, 8:37 AM
nikic requested review of this revision.Sep 5 2020, 8:37 AM
nikic added inline comments.
llvm/test/Transforms/InstCombine/abs-intrinsic.ll
230

I've duplicated the relevant InstSimplify tests above here, to make sure they still fold in InstCombine.

lebedev.ri added inline comments.Sep 5 2020, 8:47 AM
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
664–665

Either the function should have other name, or the return value pattern should be inverted,
because if the value is negative, we for sure know that the sign is 1, not 0.

nikic updated this revision to Diff 290098.Sep 5 2020, 8:56 AM

Invert return value of getKnownSign().

nikic marked an inline comment as done.Sep 5 2020, 8:58 AM
nikic added inline comments.
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
664–665

Uh yeah, this didn't make sense. I couldn't think of a good name, so I ended up inverting the return value.

spatel accepted this revision.Sep 8 2020, 7:32 AM

LGTM

This revision is now accepted and ready to land.Sep 8 2020, 7:32 AM
This revision was automatically updated to reflect the committed changes.
nikic marked an inline comment as done.