[PATCH] D82277: [InstCombine] fold fmul/fdiv with fabs operands

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 21 07:25:08 PDT 2020


spatel created this revision.
spatel added reviewers: cameron.mcinally, arsenm, mcberg2017, lebedev.ri, aqjune, zhengyangl, nlopes, regehr.
Herald added subscribers: hiraditya, wdng, mcrosier.
Herald added a project: LLVM.

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.]


https://reviews.llvm.org/D82277

Files:
  llvm/lib/Transforms/InstCombine/InstCombineInternal.h
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/fdiv.ll
  llvm/test/Transforms/InstCombine/fmul.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82277.272308.patch
Type: text/x-patch
Size: 6391 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200621/494a753b/attachment.bin>


More information about the llvm-commits mailing list