[PATCH] D11855: SelectionDAG: Prefer to combine multiplication with less uses for fma

Xuetian Weng via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 7 17:37:34 PDT 2015


wengxt created this revision.
wengxt added reviewers: jingyue, hfinkel, apazos.
wengxt added a subscriber: llvm-commits.
Herald added a subscriber: jholewinski.

For example:

  s6 = s0*s5;
  s2 = s6*s6 + s6;
  ...
  s4 = s6*s3;

We notice that it is possible for s2 is folded to fma (s0, s5, fmul (s6 s6)).
This only happens when Aggressive is true, otherwise hasOneUse() check
already prevents from folding the multiplication with more uses.

Test Plan: test/CodeGen/NVPTX/fma-assoc.ll

http://reviews.llvm.org/D11855

Files:
  lib/CodeGen/SelectionDAG/DAGCombiner.cpp
  test/CodeGen/NVPTX/fma-assoc.ll

Index: test/CodeGen/NVPTX/fma-assoc.ll
===================================================================
--- test/CodeGen/NVPTX/fma-assoc.ll
+++ test/CodeGen/NVPTX/fma-assoc.ll
@@ -23,3 +23,19 @@
   %d = fadd double %c, %z
   ret double %d
 }
+
+define double @two_choice(double %val1, double %val2) #0 {
+; CHECK-LABEL: .visible .entry two_choice
+; CHECK: mul.f64
+; CHECK-NOT: mul.f64
+; CHECK: fma.rn.f64
+  %1 = fmul double %val1, %val2
+  %2 = fmul double %1, %1
+  %3 = fadd double %1, %2
+
+  ret double %3
+}
+
+!nvvm.annotations = !{!0}
+
+!0 = !{double (double, double)* @two_choice, !"kernel", i32 1}
Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp
===================================================================
--- lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -7426,6 +7426,16 @@
   bool Aggressive = TLI.enableAggressiveFMAFusion(VT);
   bool LookThroughFPExt = TLI.isFPExtFree(VT);
 
+  // if we have two choice trying to fold (fadd (fmul u, v), (fmul x, y)) anyway, prefer to
+  // fold the multiplication with less use.
+  if (Aggressive && N0.getOpcode() == ISD::FMUL &&
+      N1.getOpcode() == ISD::FMUL) {
+    if (N0.getNode()->use_size() > N1.getNode()->use_size()) {
+      using std::swap;
+      swap(N0, N1);
+    }
+  }
+
   // fold (fadd (fmul x, y), z) -> (fma x, y, z)
   if (N0.getOpcode() == ISD::FMUL &&
       (Aggressive || N0->hasOneUse())) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D11855.31563.patch
Type: text/x-patch
Size: 1431 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150808/23cd3c81/attachment.bin>


More information about the llvm-commits mailing list