[PATCH] D85709: [InstSimplify] Implement Instruction simplification for X/sqrt(X) to sqrt(X).

Venkataramanan Kumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 00:09:54 PDT 2020


venkataramanan.kumar.llvm created this revision.
venkataramanan.kumar.llvm added reviewers: spatel, raghesh.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
venkataramanan.kumar.llvm requested review of this revision.

This patch simplifies "X/sqrt(X)" to "sqrt(X)".   For "X/sqrt(X)",  LLVM generates 2 operations "sqrt" and "div".  Simplifying it results in one "sqrt" operation.  The simplification is enabled when re-association is set.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85709

Files:
  llvm/lib/Analysis/InstructionSimplify.cpp
  llvm/test/Transforms/InstSimplify/fdiv.ll


Index: llvm/test/Transforms/InstSimplify/fdiv.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/fdiv.ll
+++ llvm/test/Transforms/InstSimplify/fdiv.ll
@@ -26,6 +26,16 @@
   ret double %d
 }
 
+define double @sqrt_fdiv_common_operand(double %x) {
+; CHECK-LABEL: @sqrt_fdiv_common_operand(
+; CHECK-NEXT:    %0 = tail call fast double @llvm.sqrt.f64(double %x)
+; CHECK-NEXT:    ret double %x
+;
+  %0 = tail call fast double @llvm.sqrt.f64(double %x)
+  %1 = fdiv fast double %x, %0
+  ret double %1
+}
+
 ; Negative test - the fdiv must be reassociative and not allow NaNs.
 
 define double @fmul_fdiv_common_operand_too_strict(double %x, double %y) {
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4890,6 +4890,11 @@
       return ConstantFP::get(Op0->getType(), -1.0);
   }
 
+  // x/sqrt(x)  = sqrt(x)
+  if (match(Op1, m_Intrinsic<Intrinsic::sqrt>(m_Specific(Op0))) &&
+      FMF.allowReassoc() && FMF.noNaNs() && FMF.noSignedZeros())
+    return Op1;
+
   return nullptr;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85709.284586.patch
Type: text/x-patch
Size: 1207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200811/bf084e4c/attachment.bin>


More information about the llvm-commits mailing list