-LexicalScope *LexicalScopes::getOrCreateRegularScope(MDNode *Scope) {
- DIDescriptor D = DIDescriptor(Scope);
- if (D.isLexicalBlockFile())
- Scope = DILexicalBlockFile(Scope).getScope();
-
- LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
- if (WScope)
- return WScope;
-
- LexicalScope *Parent = NULL;
- if (D.isLexicalBlock())
- Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
- WScope = new LexicalScope(Parent, DIDescriptor(Scope), NULL, false);
- LexicalScopeMap.insert(std::make_pair(Scope, WScope));
- if (!Parent && DIDescriptor(Scope).isSubprogram()
- && DISubprogram(Scope).describes(MF->getFunction()))
- CurrentFnLexicalScope = WScope;
-
- return WScope;
+LexicalScope *
+LexicalScopes::getOrCreateRegularScope(const DILocalScope *Scope) {
+ if (auto *File = dyn_cast<DILexicalBlockFile>(Scope))
+ Scope = File->getScope();
+
+ auto I = LexicalScopeMap.find(Scope);
+ if (I != LexicalScopeMap.end())
+ return &I->second;
+
+ // FIXME: Should the following dyn_cast be DILexicalBlock?
+ LexicalScope *Parent = nullptr;
+ if (auto *Block = dyn_cast<DILexicalBlockBase>(Scope))
+ Parent = getOrCreateLexicalScope(Block->getScope());
+ I = LexicalScopeMap.emplace(std::piecewise_construct,
+ std::forward_as_tuple(Scope),
+ std::forward_as_tuple(Parent, Scope, nullptr,
+ false)).first;
+
+ if (!Parent) {
+ assert(cast<DISubprogram>(Scope)->describes(MF->getFunction()));
+ assert(!CurrentFnLexicalScope);
+ CurrentFnLexicalScope = &I->second;
+ }
+
+ return &I->second;