MachineBasicBlock: Use MCPhysReg instead of unsigned in livein API
[oota-llvm.git] / include / llvm / ADT / ScopedHashTable.h
index af3c482043b1eba557c313afda90c736e9b39243..6b0c2995d2bb5e8e54965fea5534846abf8628ce 100644 (file)
@@ -47,8 +47,8 @@ class ScopedHashTableVal {
   K Key;
   V Val;
   ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
-public:
 
+public:
   const K &getKey() const { return Key; }
   const V &getValue() const { return Val; }
   V &getValue() { return Val; }
@@ -56,7 +56,7 @@ public:
   ScopedHashTableVal *getNextForKey() { return NextForKey; }
   const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
   ScopedHashTableVal *getNextInScope() { return NextInScope; }
-  
+
   template <typename AllocatorTy>
   static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
                                     ScopedHashTableVal *nextForKey,
@@ -66,12 +66,11 @@ public:
     // Set up the value.
     new (New) ScopedHashTableVal(key, val);
     New->NextInScope = nextInScope;
-    New->NextForKey = nextForKey; 
+    New->NextForKey = nextForKey;
     return New;
   }
-  
-  template <typename AllocatorTy>
-  void Destroy(AllocatorTy &Allocator) {
+
+  template <typename AllocatorTy> void Destroy(AllocatorTy &Allocator) {
     // Free memory referenced by the item.
     this->~ScopedHashTableVal();
     Allocator.Deallocate(this);
@@ -90,12 +89,16 @@ class ScopedHashTableScope {
   /// LastValInScope - This is the last value that was inserted for this scope
   /// or null if none have been inserted yet.
   ScopedHashTableVal<K, V> *LastValInScope;
-  void operator=(ScopedHashTableScope&);       // DO NOT IMPLEMENT
-  ScopedHashTableScope(ScopedHashTableScope&); // DO NOT IMPLEMENT
+  void operator=(ScopedHashTableScope &) = delete;
+  ScopedHashTableScope(ScopedHashTableScope &) = delete;
+
 public:
   ScopedHashTableScope(ScopedHashTable<K, V, KInfo, AllocatorTy> &HT);
   ~ScopedHashTableScope();
 
+  ScopedHashTableScope *getParentScope() { return PrevScope; }
+  const ScopedHashTableScope *getParentScope() const { return PrevScope; }
+
 private:
   friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
   ScopedHashTableVal<K, V> *getLastValInScope() {
@@ -106,10 +109,10 @@ private:
   }
 };
 
-
-template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
+template <typename K, typename V, typename KInfo = DenseMapInfo<K>>
 class ScopedHashTableIterator {
   ScopedHashTableVal<K, V> *Node;
+
 public:
   ScopedHashTableIterator(ScopedHashTableVal<K, V> *node) : Node(node) {}
 
@@ -138,36 +141,38 @@ public:
   }
 };
 
-
 template <typename K, typename V, typename KInfo, typename AllocatorTy>
 class ScopedHashTable {
+public:
+  /// ScopeTy - This is a helpful typedef that allows clients to get easy access
+  /// to the name of the scope for this hash table.
+  typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
+  typedef unsigned size_type;
+
+private:
   typedef ScopedHashTableVal<K, V> ValTy;
   DenseMap<K, ValTy*, KInfo> TopLevelMap;
-  ScopedHashTableScope<K, V, KInfo, AllocatorTy> *CurScope;
-  
+  ScopeTy *CurScope;
+
   AllocatorTy Allocator;
-  
-  ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
-  void operator=(const ScopedHashTable&);  // NOT YET IMPLEMENTED
+
+  ScopedHashTable(const ScopedHashTable &); // NOT YET IMPLEMENTED
+  void operator=(const ScopedHashTable &);  // NOT YET IMPLEMENTED
   friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
+
 public:
-  ScopedHashTable() : CurScope(0) {}
+  ScopedHashTable() : CurScope(nullptr) {}
   ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
   ~ScopedHashTable() {
-    assert(CurScope == 0 && TopLevelMap.empty() && "Scope imbalance!");
+    assert(!CurScope && TopLevelMap.empty() && "Scope imbalance!");
   }
-  
-  /// ScopeTy - This is a helpful typedef that allows clients to get easy access
-  /// to the name of the scope for this hash table.
-  typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
 
   /// Access to the allocator.
-  typedef typename ReferenceAdder<AllocatorTy>::result AllocatorRefTy;
-  typedef typename ReferenceAdder<const AllocatorTy>::result AllocatorCRefTy;
-  AllocatorRefTy getAllocator() { return Allocator; }
-  AllocatorCRefTy getAllocator() const { return Allocator; }
+  AllocatorTy &getAllocator() { return Allocator; }
+  const AllocatorTy &getAllocator() const { return Allocator; }
 
-  bool count(const K &Key) const {
+  /// Return 1 if the specified key is in the table, 0 otherwise.
+  size_type count(const K &Key) const {
     return TopLevelMap.count(Key);
   }
 
@@ -175,18 +180,12 @@ public:
     typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
     if (I != TopLevelMap.end())
       return I->second->getValue();
-      
+
     return V();
   }
 
   void insert(const K &Key, const V &Val) {
-    assert(CurScope && "No scope active!");
-
-    ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
-
-    KeyEntry = ValTy::Create(CurScope->getLastValInScope(), KeyEntry, Key, Val,
-                             Allocator);
-    CurScope->setLastValInScope(KeyEntry);
+    insertIntoScope(CurScope, Key, Val);
   }
 
   typedef ScopedHashTableIterator<K, V, KInfo> iterator;
@@ -199,6 +198,21 @@ public:
     if (I == TopLevelMap.end()) return end();
     return iterator(I->second);
   }
+
+  ScopeTy *getCurScope() { return CurScope; }
+  const ScopeTy *getCurScope() const { return CurScope; }
+
+  /// insertIntoScope - This inserts the specified key/value at the specified
+  /// (possibly not the current) scope.  While it is ok to insert into a scope
+  /// that isn't the current one, it isn't ok to insert *underneath* an existing
+  /// value of the specified key.
+  void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
+    assert(S && "No scope active!");
+    ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
+    KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
+                             Allocator);
+    S->setLastValInScope(KeyEntry);
+  }
 };
 
 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
@@ -208,7 +222,7 @@ ScopedHashTableScope<K, V, KInfo, Allocator>::
   ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
   PrevScope = HT.CurScope;
   HT.CurScope = this;
-  LastValInScope = 0;
+  LastValInScope = nullptr;
 }
 
 template <typename K, typename V, typename KInfo, typename Allocator>
@@ -219,7 +233,7 @@ ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
   // Pop and delete all values corresponding to this scope.
   while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
     // Pop this value out of the TopLevelMap.
-    if (ThisEntry->getNextForKey() == 0) {
+    if (!ThisEntry->getNextForKey()) {
       assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
              "Scope imbalance!");
       HT.TopLevelMap.erase(ThisEntry->getKey());