[llvm] r207724 - LexicalScopes: use unique_ptr to own LexicalScope objects.

David Blaikie dblaikie at gmail.com
Wed Apr 30 16:41:00 PDT 2014


Author: dblaikie
Date: Wed Apr 30 18:40:59 2014
New Revision: 207724

URL: http://llvm.org/viewvc/llvm-project?rev=207724&view=rev
Log:
LexicalScopes: use unique_ptr to own LexicalScope objects.

Ownership of abstract scopes coming soon.

Modified:
    llvm/trunk/include/llvm/CodeGen/LexicalScopes.h
    llvm/trunk/lib/CodeGen/LexicalScopes.cpp

Modified: llvm/trunk/include/llvm/CodeGen/LexicalScopes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/LexicalScopes.h?rev=207724&r1=207723&r2=207724&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/LexicalScopes.h (original)
+++ llvm/trunk/include/llvm/CodeGen/LexicalScopes.h Wed Apr 30 18:40:59 2014
@@ -25,6 +25,7 @@
 #include "llvm/IR/Metadata.h"
 #include "llvm/IR/ValueHandle.h"
 #include <utility>
+#include <memory>
 namespace llvm {
 
 class MachineInstr;
@@ -100,7 +101,8 @@ public:
 
   /// findLexicalScope - Find regular lexical scope or return NULL.
   LexicalScope *findLexicalScope(const MDNode *N) {
-    return LexicalScopeMap.lookup(N);
+    auto I = LexicalScopeMap.find(N);
+    return I != LexicalScopeMap.end() ? I->second.get() : nullptr;
   }
 
   /// dump - Print data structures to dbgs().
@@ -134,7 +136,7 @@ private:
 
   /// LexicalScopeMap - Tracks the scopes in the current function.  Owns the
   /// contained LexicalScope*s.
-  DenseMap<const MDNode *, LexicalScope *> LexicalScopeMap;
+  DenseMap<const MDNode *, std::unique_ptr<LexicalScope>> LexicalScopeMap;
 
   /// InlinedLexicalScopeMap - Tracks inlined function scopes in current
   /// function.

Modified: llvm/trunk/lib/CodeGen/LexicalScopes.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/LexicalScopes.cpp?rev=207724&r1=207723&r2=207724&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/LexicalScopes.cpp (original)
+++ llvm/trunk/lib/CodeGen/LexicalScopes.cpp Wed Apr 30 18:40:59 2014
@@ -33,7 +33,6 @@ LexicalScopes::~LexicalScopes() { reset(
 void LexicalScopes::reset() {
   MF = nullptr;
   CurrentFnLexicalScope = nullptr;
-  DeleteContainerSeconds(LexicalScopeMap);
   DeleteContainerSeconds(AbstractScopeMap);
   InlinedLexicalScopeMap.clear();
   AbstractScopesList.clear();
@@ -124,7 +123,8 @@ LexicalScope *LexicalScopes::findLexical
 
   if (IA)
     return InlinedLexicalScopeMap.lookup(DebugLoc::getFromDILocation(IA));
-  return LexicalScopeMap.lookup(Scope);
+  auto I = LexicalScopeMap.find(Scope);
+  return I != LexicalScopeMap.end() ? I->second.get() : nullptr;
 }
 
 /// getOrCreateLexicalScope - Find lexical scope for the given DebugLoc. If
@@ -152,35 +152,35 @@ LexicalScope *LexicalScopes::getOrCreate
     D = DIDescriptor(Scope);
   }
 
-  LexicalScope *WScope = LexicalScopeMap.lookup(Scope);
-  if (WScope)
-    return WScope;
+  auto IterBool = LexicalScopeMap.insert(std::make_pair(Scope, std::unique_ptr<LexicalScope>()));
+  auto &MapValue = *IterBool.first;
+  if (!IterBool.second)
+    return MapValue.second.get();
 
   LexicalScope *Parent = nullptr;
   if (D.isLexicalBlock())
     Parent = getOrCreateLexicalScope(DebugLoc::getFromDILexicalBlock(Scope));
-  WScope = new LexicalScope(Parent, DIDescriptor(Scope), nullptr, false);
-  LexicalScopeMap.insert(std::make_pair(Scope, WScope));
+  MapValue.second = make_unique<LexicalScope>(Parent, DIDescriptor(Scope), nullptr, false);
   if (!Parent && DIDescriptor(Scope).isSubprogram() &&
       DISubprogram(Scope).describes(MF->getFunction()))
-    CurrentFnLexicalScope = WScope;
+    CurrentFnLexicalScope = MapValue.second.get();
 
-  return WScope;
+  return MapValue.second.get();
 }
 
 /// getOrCreateInlinedScope - Find or create an inlined lexical scope.
 LexicalScope *LexicalScopes::getOrCreateInlinedScope(MDNode *Scope,
                                                      MDNode *InlinedAt) {
-  LexicalScope *InlinedScope = LexicalScopeMap.lookup(InlinedAt);
-  if (InlinedScope)
-    return InlinedScope;
+  auto IterBool = LexicalScopeMap.insert(std::make_pair(InlinedAt, std::unique_ptr<LexicalScope>()));
+  auto &MapValue = *IterBool.first;
+  if (!IterBool.second)
+    return MapValue.second.get();
 
   DebugLoc InlinedLoc = DebugLoc::getFromDILocation(InlinedAt);
-  InlinedScope = new LexicalScope(getOrCreateLexicalScope(InlinedLoc),
+  MapValue.second = make_unique<LexicalScope>(getOrCreateLexicalScope(InlinedLoc),
                                   DIDescriptor(Scope), InlinedAt, false);
-  InlinedLexicalScopeMap[InlinedLoc] = InlinedScope;
-  LexicalScopeMap[InlinedAt] = InlinedScope;
-  return InlinedScope;
+  InlinedLexicalScopeMap[InlinedLoc] = MapValue.second.get();
+  return MapValue.second.get();
 }
 
 /// getOrCreateAbstractScope - Find or create an abstract lexical scope.





More information about the llvm-commits mailing list