diff --git a/llvm/lib/CodeGen/LexicalScopes.cpp b/llvm/lib/CodeGen/LexicalScopes.cpp --- a/llvm/lib/CodeGen/LexicalScopes.cpp +++ b/llvm/lib/CodeGen/LexicalScopes.cpp @@ -232,21 +232,20 @@ /// constructScopeNest 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 &p = WorkStack.back(); + LexicalScope *WS = p.first; + size_t i = p.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 (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); }