[PATCH] D80029: Fix quadratic LexicalScopes::constructScopeNest(...), NFC

Jan-Willem Maessen via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 21 07:32:04 PDT 2020


jmaessen updated this revision to Diff 265504.
jmaessen added a comment.

Address review comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80029/new/

https://reviews.llvm.org/D80029

Files:
  llvm/lib/CodeGen/LexicalScopes.cpp


Index: llvm/lib/CodeGen/LexicalScopes.cpp
===================================================================
--- llvm/lib/CodeGen/LexicalScopes.cpp
+++ llvm/lib/CodeGen/LexicalScopes.cpp
@@ -229,24 +229,25 @@
   return &I->second;
 }
 
-/// constructScopeNest
+/// constructScopeNest - Traverse the Scope tree depth-first, toring
+/// traversal state in WorkStack and recording the depth-first
+/// numbering (setDFSIn, setDFSOut) for edge classification.
 void LexicalScopes::constructScopeNest(LexicalScope *Scope) {
   assert(Scope && "Unable to calculate scope dominance graph!");
-  SmallVector<LexicalScope *, 4> WorkStack;
-  WorkStack.push_back(Scope);
+  SmallVector<std::pair<LexicalScope *, size_t>, 4> WorkStack;
+  WorkStack.push_back(std::make_pair(Scope, 0));
   unsigned Counter = 0;
   while (!WorkStack.empty()) {
-    LexicalScope *WS = WorkStack.back();
+    auto &ScopePosition = WorkStack.back();
+    LexicalScope *WS = ScopePosition.first;
+    size_t i = ScopePosition.second++;
     const SmallVectorImpl<LexicalScope *> &Children = WS->getChildren();
-    bool visitedChildren = false;
-    for (auto &ChildScope : Children)
-      if (!ChildScope->getDFSOut()) {
-        WorkStack.push_back(ChildScope);
-        visitedChildren = true;
-        ChildScope->setDFSIn(++Counter);
-        break;
-      }
-    if (!visitedChildren) {
+    if (i < Children.size()) {
+      auto &ChildScope = Children[i];
+      // Because of tree structure, !ChildScope->getDFSIn().
+      WorkStack.push_back(std::make_pair(ChildScope, 0));
+      ChildScope->setDFSIn(++Counter);
+    } else {
       WorkStack.pop_back();
       WS->setDFSOut(++Counter);
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80029.265504.patch
Type: text/x-patch
Size: 1678 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200521/2029735e/attachment.bin>


More information about the llvm-commits mailing list