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

Jeremy Morse via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 8 11:03:11 PDT 2020


This revision was automatically updated to reflect the committed changes.
Closed by commit rG3610d31e7a36 (authored by jmaessen, committed by jmorse).

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
@@ -230,24 +230,24 @@
   return &I->second;
 }
 
-/// constructScopeNest
+/// constructScopeNest - Traverse the Scope tree depth-first, storing
+/// 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 ChildNum = 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 (ChildNum < Children.size()) {
+      auto &ChildScope = Children[ChildNum];
+      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.269291.patch
Type: text/x-patch
Size: 1638 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200608/e9bc42e9/attachment.bin>


More information about the llvm-commits mailing list