This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] fold fmul/fdiv with fabs operands
ClosedPublic

Authored by spatel on Jun 21 2020, 7:07 AM.

Details

Summary

fabs(X) * fabs(Y) --> fabs(X * Y)
fabs(X) / fabs(Y) --> fabs(X / Y)

If both operands of fmul/fdiv are positive, then the result must be positive.

There's a NAN corner-case that prevents removing the more specific fold just above this one:
fabs(X) * fabs(X) -> X * X
That fold works even with NAN because the sign-bit result of the multiply is not specified if X is NAN.

We can't remove that and use the more general fold that is proposed here because once we convert to this:
fabs (X * X)
...it is not legal to simplify the 'fabs' out of that expression when X is NAN. That's because fabs() guarantees that the sign-bit is always cleared - even for NAN values.

So this patch has the potential to lose information, but it seems unlikely if we do the more specific fold ahead of this one.

[Side note: Alive2 doesn't appear to support 'llvm.fabs' yet - that would be a nice enhancement.]

Diff Detail

Event Timeline

spatel created this revision.Jun 21 2020, 7:07 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 21 2020, 7:07 AM
mcberg2017 accepted this revision.Jun 22 2020, 9:54 AM

LGTM. My read on this change is that you have set it up to never make more code than we have, and transformation is safe.

This revision is now accepted and ready to land.Jun 22 2020, 9:54 AM
This revision was automatically updated to reflect the committed changes.