1 //===- ScopedHashTable.h - A simple scoped hash table ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements an efficient scoped hash table, which is useful for
11 // things like dominator-based optimizations. This allows clients to do things
14 // ScopedHashTable<int, int> HT;
16 // ScopedHashTableScope<int, int> Scope1(HT);
20 // ScopedHashTableScope<int, int> Scope2(HT);
25 // Looking up the value for "0" in the Scope2 block will return 42. Looking
26 // up the value for 0 before 42 is inserted or after Scope2 is popped will
29 //===----------------------------------------------------------------------===//
31 #ifndef LLVM_ADT_SCOPEDHASHTABLE_H
32 #define LLVM_ADT_SCOPEDHASHTABLE_H
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/Support/Allocator.h"
39 template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
40 typename AllocatorTy = MallocAllocator>
41 class ScopedHashTable;
43 template <typename K, typename V>
44 class ScopedHashTableVal {
45 ScopedHashTableVal *NextInScope;
46 ScopedHashTableVal *NextForKey;
49 ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
52 const K &getKey() const { return Key; }
53 const V &getValue() const { return Val; }
54 V &getValue() { return Val; }
56 ScopedHashTableVal *getNextForKey() { return NextForKey; }
57 const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
58 ScopedHashTableVal *getNextInScope() { return NextInScope; }
60 template <typename AllocatorTy>
61 static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
62 ScopedHashTableVal *nextForKey,
63 const K &key, const V &val,
64 AllocatorTy &Allocator) {
65 ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
67 new (New) ScopedHashTableVal(key, val);
68 New->NextInScope = nextInScope;
69 New->NextForKey = nextForKey;
73 template <typename AllocatorTy>
74 void Destroy(AllocatorTy &Allocator) {
75 // Free memory referenced by the item.
76 this->~ScopedHashTableVal();
77 Allocator.Deallocate(this);
81 template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
82 typename AllocatorTy = MallocAllocator>
83 class ScopedHashTableScope {
84 /// HT - The hashtable that we are active for.
85 ScopedHashTable<K, V, KInfo, AllocatorTy> &HT;
87 /// PrevScope - This is the scope that we are shadowing in HT.
88 ScopedHashTableScope *PrevScope;
90 /// LastValInScope - This is the last value that was inserted for this scope
91 /// or null if none have been inserted yet.
92 ScopedHashTableVal<K, V> *LastValInScope;
93 void operator=(ScopedHashTableScope&) = delete;
94 ScopedHashTableScope(ScopedHashTableScope&) = delete;
97 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, AllocatorTy> &HT);
98 ~ScopedHashTableScope();
100 ScopedHashTableScope *getParentScope() { return PrevScope; }
101 const ScopedHashTableScope *getParentScope() const { return PrevScope; }
104 friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
105 ScopedHashTableVal<K, V> *getLastValInScope() {
106 return LastValInScope;
108 void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
109 LastValInScope = Val;
113 template <typename K, typename V, typename KInfo = DenseMapInfo<K> >
114 class ScopedHashTableIterator {
115 ScopedHashTableVal<K, V> *Node;
118 ScopedHashTableIterator(ScopedHashTableVal<K, V> *node) : Node(node) {}
120 V &operator*() const {
121 assert(Node && "Dereference end()");
122 return Node->getValue();
124 V *operator->() const {
125 return &Node->getValue();
128 bool operator==(const ScopedHashTableIterator &RHS) const {
129 return Node == RHS.Node;
131 bool operator!=(const ScopedHashTableIterator &RHS) const {
132 return Node != RHS.Node;
135 inline ScopedHashTableIterator& operator++() { // Preincrement
136 assert(Node && "incrementing past end()");
137 Node = Node->getNextForKey();
140 ScopedHashTableIterator operator++(int) { // Postincrement
141 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
145 template <typename K, typename V, typename KInfo, typename AllocatorTy>
146 class ScopedHashTable {
148 /// ScopeTy - This is a helpful typedef that allows clients to get easy access
149 /// to the name of the scope for this hash table.
150 typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
151 typedef unsigned size_type;
154 typedef ScopedHashTableVal<K, V> ValTy;
155 DenseMap<K, ValTy*, KInfo> TopLevelMap;
158 AllocatorTy Allocator;
160 ScopedHashTable(const ScopedHashTable&); // NOT YET IMPLEMENTED
161 void operator=(const ScopedHashTable&); // NOT YET IMPLEMENTED
162 friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
165 ScopedHashTable() : CurScope(nullptr) {}
166 ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
168 assert(!CurScope && TopLevelMap.empty() && "Scope imbalance!");
171 /// Access to the allocator.
172 AllocatorTy &getAllocator() { return Allocator; }
173 const AllocatorTy &getAllocator() const { return Allocator; }
175 /// Return 1 if the specified key is in the table, 0 otherwise.
176 size_type count(const K &Key) const {
177 return TopLevelMap.count(Key);
180 V lookup(const K &Key) {
181 typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
182 if (I != TopLevelMap.end())
183 return I->second->getValue();
188 void insert(const K &Key, const V &Val) {
189 insertIntoScope(CurScope, Key, Val);
192 typedef ScopedHashTableIterator<K, V, KInfo> iterator;
194 iterator end() { return iterator(0); }
196 iterator begin(const K &Key) {
197 typename DenseMap<K, ValTy*, KInfo>::iterator I =
198 TopLevelMap.find(Key);
199 if (I == TopLevelMap.end()) return end();
200 return iterator(I->second);
203 ScopeTy *getCurScope() { return CurScope; }
204 const ScopeTy *getCurScope() const { return CurScope; }
206 /// insertIntoScope - This inserts the specified key/value at the specified
207 /// (possibly not the current) scope. While it is ok to insert into a scope
208 /// that isn't the current one, it isn't ok to insert *underneath* an existing
209 /// value of the specified key.
210 void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
211 assert(S && "No scope active!");
212 ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
213 KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
215 S->setLastValInScope(KeyEntry);
219 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
221 template <typename K, typename V, typename KInfo, typename Allocator>
222 ScopedHashTableScope<K, V, KInfo, Allocator>::
223 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
224 PrevScope = HT.CurScope;
226 LastValInScope = nullptr;
229 template <typename K, typename V, typename KInfo, typename Allocator>
230 ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
231 assert(HT.CurScope == this && "Scope imbalance!");
232 HT.CurScope = PrevScope;
234 // Pop and delete all values corresponding to this scope.
235 while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
236 // Pop this value out of the TopLevelMap.
237 if (!ThisEntry->getNextForKey()) {
238 assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
240 HT.TopLevelMap.erase(ThisEntry->getKey());
242 ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
243 assert(KeyEntry == ThisEntry && "Scope imbalance!");
244 KeyEntry = ThisEntry->getNextForKey();
247 // Pop this value out of the scope.
248 LastValInScope = ThisEntry->getNextInScope();
250 // Delete this entry.
251 ThisEntry->Destroy(HT.getAllocator());
255 } // end namespace llvm