<div class="gmail_quote">On Wed, Sep 14, 2011 at 9:45 AM, Jakob Stoklund Olesen <span dir="ltr"><<a href="mailto:stoklund@2pi.dk">stoklund@2pi.dk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Author: stoklund<br>
Date: Wed Sep 14 11:45:39 2011<br>
New Revision: 139698<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=139698&view=rev" target="_blank">http://llvm.org/viewvc/llvm-project?rev=139698&view=rev</a><br>
Log:<br>
Hoist back-copies to the least busy dominator.<br>
<br>
When a back-copy is hoisted to the nearest common dominator, keep<br>
looking up the dominator tree for a less loopy dominator, and place the<br>
back-copy there instead.<br>
<br>
Don't do this when a single existing back-copy dominates all the others.<br>
Assume the client knows what he is doing, and keep the dominating<br>
back-copy.<br>
<br>
This prevents us from hoisting back-copies into loops in most cases.  If<br>
a value is defined in a loop with multiple exits, we may still hoist<br>
back-copies into that loop.  That is the speed/size tradeoff.<br></blockquote><div><br></div><div>This sounds quite cool; testcase?</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<br>
Modified:<br>
    llvm/trunk/lib/CodeGen/SplitKit.cpp<br>
    llvm/trunk/lib/CodeGen/SplitKit.h<br>
<br>
Modified: llvm/trunk/lib/CodeGen/SplitKit.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SplitKit.cpp?rev=139698&r1=139697&r2=139698&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SplitKit.cpp?rev=139698&r1=139697&r2=139698&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/CodeGen/SplitKit.cpp (original)<br>
+++ llvm/trunk/lib/CodeGen/SplitKit.cpp Wed Sep 14 11:45:39 2011<br>
@@ -20,6 +20,7 @@<br>
 #include "llvm/CodeGen/LiveIntervalAnalysis.h"<br>
 #include "llvm/CodeGen/MachineDominators.h"<br>
 #include "llvm/CodeGen/MachineInstrBuilder.h"<br>
+#include "llvm/CodeGen/MachineLoopInfo.h"<br>
 #include "llvm/CodeGen/MachineRegisterInfo.h"<br>
 #include "llvm/Support/Debug.h"<br>
 #include "llvm/Support/raw_ostream.h"<br>
@@ -635,6 +636,60 @@<br>
   }<br>
 }<br>
<br>
+MachineBasicBlock*<br>
+SplitEditor::findShallowDominator(MachineBasicBlock *MBB,<br>
+                                  MachineBasicBlock *DefMBB) {<br>
+  if (MBB == DefMBB)<br>
+    return MBB;<br>
+  assert(MDT.dominates(DefMBB, MBB) && "MBB must be dominated by the def.");<br>
+<br>
+  const MachineLoopInfo &Loops = SA.Loops;<br>
+  const MachineLoop *DefLoop = Loops.getLoopFor(DefMBB);<br>
+  MachineDomTreeNode *DefDomNode = MDT[DefMBB];<br>
+<br>
+  // Best candidate so far.<br>
+  MachineBasicBlock *BestMBB = MBB;<br>
+  unsigned BestDepth = UINT_MAX;<br>
+<br>
+  for (;;) {<br>
+    const MachineLoop *Loop = Loops.getLoopFor(MBB);<br>
+<br>
+    // MBB isn't in a loop, it doesn't get any better.  All dominators have a<br>
+    // higher frequency by definition.<br>
+    if (!Loop) {<br>
+      DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"<br>
+                   << MBB->getNumber() << " at depth 0\n");<br>
+      return MBB;<br>
+    }<br>
+<br>
+    // We'll never be able to exit the DefLoop.<br>
+    if (Loop == DefLoop) {<br>
+      DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"<br>
+                   << MBB->getNumber() << " in the same loop\n");<br>
+      return MBB;<br>
+    }<br>
+<br>
+    // Least busy dominator seen so far.<br>
+    unsigned Depth = Loop->getLoopDepth();<br>
+    if (Depth < BestDepth) {<br>
+      BestMBB = MBB;<br>
+      BestDepth = Depth;<br>
+      DEBUG(dbgs() << "Def in BB#" << DefMBB->getNumber() << " dominates BB#"<br>
+                   << MBB->getNumber() << " at depth " << Depth << '\n');<br>
+    }<br>
+<br>
+    // Leave loop by going to the immediate dominator of the loop header.<br>
+    // This is a bigger stride than simply walking up the dominator tree.<br>
+    MachineDomTreeNode *IDom = MDT[Loop->getHeader()]->getIDom();<br>
+<br>
+    // Too far up the dominator tree?<br>
+    if (!IDom || !MDT.dominates(DefDomNode, IDom))<br>
+      return BestMBB;<br>
+<br>
+    MBB = IDom->getBlock();<br>
+  }<br>
+}<br>
+<br>
 void SplitEditor::hoistCopiesForSize() {<br>
   // Get the complement interval, always RegIdx 0.<br>
   LiveInterval *LI = Edit->get(0);<br>
@@ -706,10 +761,14 @@<br>
     DomPair &Dom = NearestDom[i];<br>
     if (!Dom.first || Dom.second.isValid())<br>
       continue;<br>
-    // This value needs a hoisted copy inserted at the end of Dom.second.<br>
+    // This value needs a hoisted copy inserted at the end of Dom.first.<br>
+    VNInfo *ParentVNI = Parent->getValNumInfo(i);<br>
+    MachineBasicBlock *DefMBB = LIS.getMBBFromIndex(ParentVNI->def);<br>
+    // Get a less loopy dominator than Dom.first.<br>
+    Dom.first = findShallowDominator(Dom.first, DefMBB);<br>
     SlotIndex Last = LIS.getMBBEndIdx(Dom.first).getPrevSlot();<br>
     Dom.second =<br>
-      defFromParent(0, Parent->getValNumInfo(i), Last, *Dom.first,<br>
+      defFromParent(0, ParentVNI, Last, *Dom.first,<br>
                     LIS.getLastSplitPoint(Edit->getParent(), Dom.first))->def;<br>
   }<br>
<br>
<br>
Modified: llvm/trunk/lib/CodeGen/SplitKit.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SplitKit.h?rev=139698&r1=139697&r2=139698&view=diff" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SplitKit.h?rev=139698&r1=139697&r2=139698&view=diff</a><br>

==============================================================================<br>
--- llvm/trunk/lib/CodeGen/SplitKit.h (original)<br>
+++ llvm/trunk/lib/CodeGen/SplitKit.h Wed Sep 14 11:45:39 2011<br>
@@ -315,6 +315,11 @@<br>
   /// in the vector in the complement interval.<br>
   void removeBackCopies(SmallVectorImpl<VNInfo*> &Copies);<br>
<br>
+  /// getShallowDominator - Returns the least busy dominator of MBB that is<br>
+  /// also dominated by DefMBB.  Busy is measured by loop depth.<br>
+  MachineBasicBlock *findShallowDominator(MachineBasicBlock *MBB,<br>
+                                          MachineBasicBlock *DefMBB);<br>
+<br>
   /// hoistCopiesForSize - Hoist back-copies to the complement interval in a<br>
   /// way that minimizes code size. This implements the SM_Size spill mode.<br>
   void hoistCopiesForSize();<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits</a><br>
</blockquote></div><br>