From 3610d31e7a366da27901313e03089736ec65c91d Mon Sep 17 00:00:00 2001 From: Jan-Willem Maessen Date: Mon, 8 Jun 2020 18:38:16 +0100 Subject: [PATCH] [NFC] Fix quadratic LexicalScopes::constructScopeNest We sometimes have functions with large numbers of sibling basic blocks (usually with an error path exit from each one). This was triggering the qudratic behavior in this function - after visiting each child llvm would re-scan the parent from the beginning again. We modify the work stack to record the next index to be worked on alongside the pointer. This avoids the need to linearly search for the next unfinished child. Differential Revision: https://reviews.llvm.org/D80029 --- llvm/lib/CodeGen/LexicalScopes.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/llvm/lib/CodeGen/LexicalScopes.cpp b/llvm/lib/CodeGen/LexicalScopes.cpp index c5d786b13f90..690b429832a5 100644 --- a/llvm/lib/CodeGen/LexicalScopes.cpp +++ b/llvm/lib/CodeGen/LexicalScopes.cpp @@ -230,24 +230,24 @@ LexicalScopes::getOrCreateAbstractScope(const DILocalScope *Scope) { 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 WorkStack; - WorkStack.push_back(Scope); + SmallVector, 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 &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); }