[PATCH] D82716: [DAGCombiner] reassociate reciprocal sqrt expression to eliminate FP division

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 28 07:36:31 PDT 2020


spatel created this revision.
spatel added reviewers: craig.topper, lebedev.ri, RKSimon, qshanz, shchenz.
Herald added subscribers: ecnelises, hiraditya, mcrosier.
Herald added a project: LLVM.
spatel edited reviewers, added: steven.zhang; removed: qshanz.
spatel updated this revision to Diff 273935.
spatel added a comment.

Patch updated:
The previous draft included creation of an unused sqrt node. That was part of an earlier experiment and unnecessarily included here.


X / (fabs(A) * sqrt(Z)) --> X / sqrt(A*A*Z) --> X * rsqrt(A*A*Z)

In the motivating case from PR46406:
https://bugs.llvm.org/show_bug.cgi?id=46406
...this is restoring the sequence that was originally in the source code. We extracted a term from within the sqrt because we do not know in instcombine whether a target will expand a sqrt call. 
Note: we could say that the transform in IR should be restricted, but that would not solve the problem if the source was originally in the pattern shown here.

This is a gray area for fast-math-flag requirements. I think we should at least check fast-math-flags on the fdiv and fmul because I view this transform as 2 pieces: reassociate the fmul operands and form reciprocal from the fdiv (as with the existing transform). We could argue that the sqrt also needs FMF, but that was not required before, so I think we should change that in a preliminary or follow-up patch if that seems better.

We don't currently have a way to check that the target will produce a sqrt or recip estimate without actually creating nodes (the APIs are SDValue getSqrtEstimate() and SDValue getRecipEstimate()), so we clean up speculatively created nodes if we are not able to create an estimate. The x86 test with doubles verifies that we are not changing a test with no estimate sequence.


https://reviews.llvm.org/D82716

Files:
  llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  llvm/test/CodeGen/X86/sqrt-fastmath.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D82716.273935.patch
Type: text/x-patch
Size: 8532 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200628/a0e3060e/attachment-0001.bin>


More information about the llvm-commits mailing list