[PATCH] D33769: [SelectionDAG] Get rid of recursion in CalcNodeSethiUllmanNumber

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 00:17:13 PDT 2017


mkazantsev updated this revision to Diff 101997.
mkazantsev marked an inline comment as done.
mkazantsev added a comment.

Rewritten in a more clear manner.


https://reviews.llvm.org/D33769

Files:
  lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp


Index: lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
===================================================================
--- lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
+++ lib/CodeGen/SelectionDAG/ScheduleDAGRRList.cpp
@@ -1861,28 +1861,65 @@
 /// Smaller number is the higher priority.
 static unsigned
 CalcNodeSethiUllmanNumber(const SUnit *SU, std::vector<unsigned> &SUNumbers) {
-  unsigned &SethiUllmanNumber = SUNumbers[SU->NodeNum];
-  if (SethiUllmanNumber != 0)
-    return SethiUllmanNumber;
-
-  unsigned Extra = 0;
-  for (const SDep &Pred : SU->Preds) {
-    if (Pred.isCtrl()) continue;  // ignore chain preds
-    SUnit *PredSU = Pred.getSUnit();
-    unsigned PredSethiUllman = CalcNodeSethiUllmanNumber(PredSU, SUNumbers);
-    if (PredSethiUllman > SethiUllmanNumber) {
-      SethiUllmanNumber = PredSethiUllman;
-      Extra = 0;
-    } else if (PredSethiUllman == SethiUllmanNumber)
-      ++Extra;
-  }
+  if (SUNumbers[SU->NodeNum] == 0) {
+    // Use WorkList to avoid stack overflow on excessively large IRs.
+    struct WorkState {
+      WorkState(const SUnit *SU) : SU(SU) {}
+      const SUnit *SU;
+      unsigned PredsProcessed = 0;
+    };
+    SmallVector<WorkState, 16> WorkList;
+    WorkList.push_back(SU);
+    while (!WorkList.empty()) {
+      auto &Temp = WorkList.back();
+      SU = Temp.SU;
+      bool AllPredsKnown = true;
+      // Try to find a non-evaluated pred and push it into the processing stack.
+      for (unsigned P = Temp.PredsProcessed; P < SU->Preds.size(); ++P) {
+        auto &Pred = SU->Preds[P];
+        if (Pred.isCtrl()) continue;  // ignore chain preds
+        SUnit *PredSU = Pred.getSUnit();
+        if (SUNumbers[PredSU->NodeNum] == 0) {
+#ifndef NDEBUG
+          // In debug mode, check that we don't have such element in the stack.
+          for (auto It : WorkList)
+            assert(It.SU != PredSU && "Trying to push an element twice?");
+#endif
+          WorkList.push_back(PredSU);
+          AllPredsKnown = false;
+          // Next time start processing this one starting from the next pred.
+          Temp.PredsProcessed = P + 1;
+          break;
+        }
+      }
 
-  SethiUllmanNumber += Extra;
+      if (!AllPredsKnown)
+        continue;
 
-  if (SethiUllmanNumber == 0)
-    SethiUllmanNumber = 1;
+      // Once all preds are known, we can calculate the answer for this one.
+      unsigned SethiUllmanNumber = 0;
+      unsigned Extra = 0;
+      for (const SDep &Pred : SU->Preds) {
+        if (Pred.isCtrl()) continue;  // ignore chain preds
+        SUnit *PredSU = Pred.getSUnit();
+        unsigned PredSethiUllman = SUNumbers[PredSU->NodeNum];
+        assert(PredSethiUllman > 0 && "We should have evaluated this pred!");
+        if (PredSethiUllman > SethiUllmanNumber) {
+          SethiUllmanNumber = PredSethiUllman;
+          Extra = 0;
+        } else if (PredSethiUllman == SethiUllmanNumber)
+          ++Extra;
+      }
 
-  return SethiUllmanNumber;
+      SethiUllmanNumber += Extra;
+      if (SethiUllmanNumber == 0)
+        SethiUllmanNumber = 1;
+      SUNumbers[SU->NodeNum] = SethiUllmanNumber;
+      WorkList.pop_back();
+    }
+  }
+  assert(SUNumbers[SU->NodeNum] > 0 && "SethiUllman should never be zero!");
+  return SUNumbers[SU->NodeNum];
 }
 
 /// CalculateSethiUllmanNumbers - Calculate Sethi-Ullman numbers of all


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33769.101997.patch
Type: text/x-patch
Size: 3362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170609/ca6beeb0/attachment.bin>


More information about the llvm-commits mailing list