1 //===- ScopedHashTable.h - A simple scoped hash table -----------*- C++ -*-===//
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/ADT/DenseMapInfo.h"
36 #include "llvm/Support/Allocator.h"
42 template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
43 typename AllocatorTy = MallocAllocator>
44 class ScopedHashTable;
46 template <typename K, typename V>
47 class ScopedHashTableVal {
48 ScopedHashTableVal *NextInScope;
49 ScopedHashTableVal *NextForKey;
53 ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
56 const K &getKey() const { return Key; }
57 const V &getValue() const { return Val; }
58 V &getValue() { return Val; }
60 ScopedHashTableVal *getNextForKey() { return NextForKey; }
61 const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
62 ScopedHashTableVal *getNextInScope() { return NextInScope; }
64 template <typename AllocatorTy>
65 static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
66 ScopedHashTableVal *nextForKey,
67 const K &key, const V &val,
68 AllocatorTy &Allocator) {
69 ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
71 new (New) ScopedHashTableVal(key, val);
72 New->NextInScope = nextInScope;
73 New->NextForKey = nextForKey;
77 template <typename AllocatorTy> void Destroy(AllocatorTy &Allocator) {
78 // Free memory referenced by the item.
79 this->~ScopedHashTableVal();
80 Allocator.Deallocate(this);
84 template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
85 typename AllocatorTy = MallocAllocator>
86 class ScopedHashTableScope {
87 /// HT - The hashtable that we are active for.
88 ScopedHashTable<K, V, KInfo, AllocatorTy> &HT;
90 /// PrevScope - This is the scope that we are shadowing in HT.
91 ScopedHashTableScope *PrevScope;
93 /// LastValInScope - This is the last value that was inserted for this scope
94 /// or null if none have been inserted yet.
95 ScopedHashTableVal<K, V> *LastValInScope;
98 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, AllocatorTy> &HT);
99 ScopedHashTableScope(ScopedHashTableScope &) = delete;
100 ScopedHashTableScope &operator=(ScopedHashTableScope &) = delete;
101 ~ScopedHashTableScope();
103 ScopedHashTableScope *getParentScope() { return PrevScope; }
104 const ScopedHashTableScope *getParentScope() const { return PrevScope; }
107 friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
109 ScopedHashTableVal<K, V> *getLastValInScope() {
110 return LastValInScope;
113 void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
114 LastValInScope = Val;
118 template <typename K, typename V, typename KInfo = DenseMapInfo<K>>
119 class ScopedHashTableIterator {
120 ScopedHashTableVal<K, V> *Node;
123 ScopedHashTableIterator(ScopedHashTableVal<K, V> *node) : Node(node) {}
125 V &operator*() const {
126 assert(Node && "Dereference end()");
127 return Node->getValue();
129 V *operator->() const {
130 return &Node->getValue();
133 bool operator==(const ScopedHashTableIterator &RHS) const {
134 return Node == RHS.Node;
136 bool operator!=(const ScopedHashTableIterator &RHS) const {
137 return Node != RHS.Node;
140 inline ScopedHashTableIterator& operator++() { // Preincrement
141 assert(Node && "incrementing past end()");
142 Node = Node->getNextForKey();
145 ScopedHashTableIterator operator++(int) { // Postincrement
146 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
150 template <typename K, typename V, typename KInfo, typename AllocatorTy>
151 class ScopedHashTable {
153 /// ScopeTy - This is a helpful typedef that allows clients to get easy access
154 /// to the name of the scope for this hash table.
155 using ScopeTy = ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
156 using size_type = unsigned;
159 friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
161 using ValTy = ScopedHashTableVal<K, V>;
163 DenseMap<K, ValTy*, KInfo> TopLevelMap;
164 ScopeTy *CurScope = nullptr;
166 AllocatorTy Allocator;
169 ScopedHashTable() = default;
170 ScopedHashTable(AllocatorTy A) : Allocator(A) {}
171 ScopedHashTable(const ScopedHashTable &) = delete;
172 ScopedHashTable &operator=(const ScopedHashTable &) = delete;
175 assert(!CurScope && TopLevelMap.empty() && "Scope imbalance!");
178 /// Access to the allocator.
179 AllocatorTy &getAllocator() { return Allocator; }
180 const AllocatorTy &getAllocator() const { return Allocator; }
182 /// Return 1 if the specified key is in the table, 0 otherwise.
183 size_type count(const K &Key) const {
184 return TopLevelMap.count(Key);
187 V lookup(const K &Key) const {
188 auto I = TopLevelMap.find(Key);
189 if (I != TopLevelMap.end())
190 return I->second->getValue();
195 void insert(const K &Key, const V &Val) {
196 insertIntoScope(CurScope, Key, Val);
199 using iterator = ScopedHashTableIterator<K, V, KInfo>;
201 iterator end() { return iterator(0); }
203 iterator begin(const K &Key) {
204 typename DenseMap<K, ValTy*, KInfo>::iterator I =
205 TopLevelMap.find(Key);
206 if (I == TopLevelMap.end()) return end();
207 return iterator(I->second);
210 ScopeTy *getCurScope() { return CurScope; }
211 const ScopeTy *getCurScope() const { return CurScope; }
213 /// insertIntoScope - This inserts the specified key/value at the specified
214 /// (possibly not the current) scope. While it is ok to insert into a scope
215 /// that isn't the current one, it isn't ok to insert *underneath* an existing
216 /// value of the specified key.
217 void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
218 assert(S && "No scope active!");
219 ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
220 KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
222 S->setLastValInScope(KeyEntry);
226 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
228 template <typename K, typename V, typename KInfo, typename Allocator>
229 ScopedHashTableScope<K, V, KInfo, Allocator>::
230 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
231 PrevScope = HT.CurScope;
233 LastValInScope = nullptr;
236 template <typename K, typename V, typename KInfo, typename Allocator>
237 ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
238 assert(HT.CurScope == this && "Scope imbalance!");
239 HT.CurScope = PrevScope;
241 // Pop and delete all values corresponding to this scope.
242 while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
243 // Pop this value out of the TopLevelMap.
244 if (!ThisEntry->getNextForKey()) {
245 assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
247 HT.TopLevelMap.erase(ThisEntry->getKey());
249 ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
250 assert(KeyEntry == ThisEntry && "Scope imbalance!");
251 KeyEntry = ThisEntry->getNextForKey();
254 // Pop this value out of the scope.
255 LastValInScope = ThisEntry->getNextInScope();
257 // Delete this entry.
258 ThisEntry->Destroy(HT.getAllocator());
262 } // end namespace llvm
264 #endif // LLVM_ADT_SCOPEDHASHTABLE_H