[PATCH] D77511: [RDA] Avoid full reprocessing of blocks in loops (NFCI)

Nikita Popov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 5 13:21:32 PDT 2020


nikic created this revision.
nikic added reviewers: samparker, SjoerdMeijer, craig.topper.
Herald added subscribers: llvm-commits, mgrang.
Herald added a project: LLVM.
nikic added parent revisions: D77508: [RDA] Only store most recent reaching def from predecessors (NFCI), D77506: [RDA] Don't adjust ReachingDefDefaultVal (NFCI).

RDA sometimes needs to visit blocks twice, to take into account reaching defs coming in along loop back edges. Currently it handles repeated visitation the same way as usual, which means that it will scan through all instructions and their reg unit defs again. Not only is this very inefficient, it also means that all reaching defs in loops are going to be inserted twice.

We can do much better than this. The only thing we need to handle is a new reaching def from a predecessor, which either needs to be prepended to the reaching definitions (if there was no reaching def from a predecessor), or needs to replace an existing predecessor reaching def, if it is more recent. Since D77508 <https://reviews.llvm.org/D77508> we only store the most recent predecessor reaching def, so that's the only one that may need updating.

This also has the nice side-effect that reaching definitions are now automatically sorted and unique, so drop the `llvm::sort()` call in favor of an assertion.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77511

Files:
  include/llvm/CodeGen/ReachingDefAnalysis.h
  lib/CodeGen/ReachingDefAnalysis.cpp


Index: lib/CodeGen/ReachingDefAnalysis.cpp
===================================================================
--- lib/CodeGen/ReachingDefAnalysis.cpp
+++ lib/CodeGen/ReachingDefAnalysis.cpp
@@ -137,6 +137,46 @@
   ++CurInstr;
 }
 
+void ReachingDefAnalysis::reprocessBasicBlock(MachineBasicBlock *MBB) {
+  unsigned MBBNumber = MBB->getNumber();
+  assert(MBBNumber < MBBReachingDefs.size() &&
+         "Unexpected basic block number.");
+
+  // When reprocessing a block, the only thing we need to do is check whether
+  // there is now a more recent incoming reaching definition from a predecessor.
+  for (MachineBasicBlock *pred : MBB->predecessors()) {
+    assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
+           "Should have pre-allocated MBBInfos for all MBBs");
+    const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
+    // Incoming may be empty for dead predecessors.
+    if (Incoming.empty())
+      continue;
+
+    for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
+      int Def = Incoming[Unit];
+      if (Def != ReachingDefDefaultVal) {
+        auto Start = MBBReachingDefs[MBBNumber][Unit].begin();
+        if (Start != MBBReachingDefs[MBBNumber][Unit].end() && *Start < 0) {
+          if (*Start >= Def)
+            continue;
+
+          // Update existing reaching def from predecessor to a more recent one.
+          *Start = Def;
+        } else {
+          // Insert new reaching def from predecessor.
+          MBBReachingDefs[MBBNumber][Unit].insert(Start, Def);
+        }
+
+        // Update reaching def at end of of BB. Keep in mind that these are
+        // adjusted relative to the end of the basic block.
+        int NumInsts = MBB->size();
+        if (MBBOutRegsInfos[MBBNumber][Unit] < Def - NumInsts)
+          MBBOutRegsInfos[MBBNumber][Unit] = Def - NumInsts;
+      }
+    }
+  }
+}
+
 void ReachingDefAnalysis::processBasicBlock(
     const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
   MachineBasicBlock *MBB = TraversedMBB.MBB;
@@ -144,6 +184,12 @@
                     << (!TraversedMBB.IsDone ? ": incomplete\n"
                                              : ": all preds known\n"));
 
+  if (!TraversedMBB.PrimaryPass) {
+    // Reprocess MBB that is part of a loop.
+    reprocessBasicBlock(MBB);
+    return;
+  }
+
   enterBasicBlock(MBB);
   for (MachineInstr &MI : *MBB) {
     if (!MI.isDebugInstr())
@@ -188,11 +234,18 @@
   // Traverse the basic blocks.
   for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder)
     processBasicBlock(TraversedMBB);
-  // Sorting all reaching defs found for a ceartin reg unit in a given BB.
+#ifndef NDEBUG
+  // Make sure reaching defs are sorted and unique.
   for (MBBDefsInfo &MBBDefs : MBBReachingDefs) {
-    for (MBBRegUnitDefs &RegUnitDefs : MBBDefs)
-      llvm::sort(RegUnitDefs);
+    for (MBBRegUnitDefs &RegUnitDefs : MBBDefs) {
+      int LastDef = ReachingDefDefaultVal;
+      for (int Def : RegUnitDefs) {
+        assert(Def > LastDef && "Defs must be sorted and unique");
+        LastDef = Def;
+      }
+    }
   }
+#endif
 }
 
 int ReachingDefAnalysis::getReachingDef(MachineInstr *MI, int PhysReg) const {
Index: include/llvm/CodeGen/ReachingDefAnalysis.h
===================================================================
--- include/llvm/CodeGen/ReachingDefAnalysis.h
+++ include/llvm/CodeGen/ReachingDefAnalysis.h
@@ -207,6 +207,9 @@
   /// Process he given basic block.
   void processBasicBlock(const LoopTraversal::TraversedMBBInfo &TraversedMBB);
 
+  /// Process block that is part of a loop again.
+  void reprocessBasicBlock(MachineBasicBlock *MBB);
+
   /// Update def-ages for registers defined by MI.
   /// Also break dependencies on partial defs and undef uses.
   void processDefs(MachineInstr *);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77511.255189.patch
Type: text/x-patch
Size: 3809 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200405/84fe5495/attachment.bin>


More information about the llvm-commits mailing list