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;
112 void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
113 LastValInScope = Val;
117 template <typename K, typename V, typename KInfo = DenseMapInfo<K>>
118 class ScopedHashTableIterator {
119 ScopedHashTableVal<K, V> *Node;
122 ScopedHashTableIterator(ScopedHashTableVal<K, V> *node) : Node(node) {}
124 V &operator*() const {
125 assert(Node && "Dereference end()");
126 return Node->getValue();
128 V *operator->() const {
129 return &Node->getValue();
132 bool operator==(const ScopedHashTableIterator &RHS) const {
133 return Node == RHS.Node;
135 bool operator!=(const ScopedHashTableIterator &RHS) const {
136 return Node != RHS.Node;
139 inline ScopedHashTableIterator& operator++() { // Preincrement
140 assert(Node && "incrementing past end()");
141 Node = Node->getNextForKey();
144 ScopedHashTableIterator operator++(int) { // Postincrement
145 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
149 template <typename K, typename V, typename KInfo, typename AllocatorTy>
150 class ScopedHashTable {
152 /// ScopeTy - This is a helpful typedef that allows clients to get easy access
153 /// to the name of the scope for this hash table.
154 typedef ScopedHashTableScope<K, V, KInfo, AllocatorTy> ScopeTy;
155 typedef unsigned size_type;
158 friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
160 typedef ScopedHashTableVal<K, V> ValTy;
161 DenseMap<K, ValTy*, KInfo> TopLevelMap;
162 ScopeTy *CurScope = nullptr;
164 AllocatorTy Allocator;
167 ScopedHashTable() = default;
168 ScopedHashTable(AllocatorTy A) : CurScope(0), Allocator(A) {}
169 ScopedHashTable(const ScopedHashTable &) = delete;
170 ScopedHashTable &operator=(const ScopedHashTable &) = delete;
173 assert(!CurScope && TopLevelMap.empty() && "Scope imbalance!");
176 /// Access to the allocator.
177 AllocatorTy &getAllocator() { return Allocator; }
178 const AllocatorTy &getAllocator() const { return Allocator; }
180 /// Return 1 if the specified key is in the table, 0 otherwise.
181 size_type count(const K &Key) const {
182 return TopLevelMap.count(Key);
185 V lookup(const K &Key) {
186 typename DenseMap<K, ValTy*, KInfo>::iterator I = TopLevelMap.find(Key);
187 if (I != TopLevelMap.end())
188 return I->second->getValue();
193 void insert(const K &Key, const V &Val) {
194 insertIntoScope(CurScope, Key, Val);
197 typedef ScopedHashTableIterator<K, V, KInfo> iterator;
199 iterator end() { return iterator(0); }
201 iterator begin(const K &Key) {
202 typename DenseMap<K, ValTy*, KInfo>::iterator I =
203 TopLevelMap.find(Key);
204 if (I == TopLevelMap.end()) return end();
205 return iterator(I->second);
208 ScopeTy *getCurScope() { return CurScope; }
209 const ScopeTy *getCurScope() const { return CurScope; }
211 /// insertIntoScope - This inserts the specified key/value at the specified
212 /// (possibly not the current) scope. While it is ok to insert into a scope
213 /// that isn't the current one, it isn't ok to insert *underneath* an existing
214 /// value of the specified key.
215 void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
216 assert(S && "No scope active!");
217 ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
218 KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
220 S->setLastValInScope(KeyEntry);
224 /// ScopedHashTableScope ctor - Install this as the current scope for the hash
226 template <typename K, typename V, typename KInfo, typename Allocator>
227 ScopedHashTableScope<K, V, KInfo, Allocator>::
228 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
229 PrevScope = HT.CurScope;
231 LastValInScope = nullptr;
234 template <typename K, typename V, typename KInfo, typename Allocator>
235 ScopedHashTableScope<K, V, KInfo, Allocator>::~ScopedHashTableScope() {
236 assert(HT.CurScope == this && "Scope imbalance!");
237 HT.CurScope = PrevScope;
239 // Pop and delete all values corresponding to this scope.
240 while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
241 // Pop this value out of the TopLevelMap.
242 if (!ThisEntry->getNextForKey()) {
243 assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
245 HT.TopLevelMap.erase(ThisEntry->getKey());
247 ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
248 assert(KeyEntry == ThisEntry && "Scope imbalance!");
249 KeyEntry = ThisEntry->getNextForKey();
252 // Pop this value out of the scope.
253 LastValInScope = ThisEntry->getNextInScope();
255 // Delete this entry.
256 ThisEntry->Destroy(HT.getAllocator());
260 } // end namespace llvm
262 #endif // LLVM_ADT_SCOPEDHASHTABLE_H