LLVM 23.0.0git
ScopedHashTable.h
Go to the documentation of this file.
1//===- ScopedHashTable.h - A simple scoped hash table -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements an efficient scoped hash table, which is useful for
10// things like dominator-based optimizations. This allows clients to do things
11// like this:
12//
13// ScopedHashTable<int, int> HT;
14// {
15// ScopedHashTableScope<int, int> Scope1(HT);
16// HT.insert(0, 0);
17// HT.insert(1, 1);
18// {
19// ScopedHashTableScope<int, int> Scope2(HT);
20// HT.insert(0, 42);
21// }
22// }
23//
24// Looking up the value for "0" in the Scope2 block will return 42. Looking
25// up the value for 0 before 42 is inserted or after Scope2 is popped will
26// return 0.
27//
28//===----------------------------------------------------------------------===//
29
30#ifndef LLVM_ADT_SCOPEDHASHTABLE_H
31#define LLVM_ADT_SCOPEDHASHTABLE_H
32
33#include "llvm/ADT/DenseMap.h"
36#include <cassert>
37#include <new>
38
39namespace llvm {
40
41template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
42 typename AllocatorTy = MallocAllocator>
43class ScopedHashTable;
44
45template <typename K, typename V>
46class ScopedHashTableVal {
47 ScopedHashTableVal *NextInScope;
48 ScopedHashTableVal *NextForKey;
49 ScopedHashTableVal *PreInScope;
50 K Key;
51 V Val;
52
53 ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
54
55public:
56 const K &getKey() const { return Key; }
57 const V &getValue() const { return Val; }
58 V &getValue() { return Val; }
59
60 ScopedHashTableVal *getNextForKey() { return NextForKey; }
61 const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
62 ScopedHashTableVal *getNextInScope() { return NextInScope; }
63 ScopedHashTableVal *getPreInScope() { return PreInScope; }
64
65 template <typename AllocatorTy>
66 static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
67 ScopedHashTableVal *nextForKey,
68 const K &key, const V &val,
69 AllocatorTy &Allocator) {
70 ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
71 // Set up the value.
72 new (New) ScopedHashTableVal(key, val);
73 New->NextInScope = nextInScope;
74 New->NextForKey = nextForKey;
75 New->PreInScope = nullptr;
76 if (nextInScope)
77 nextInScope->PreInScope = New;
78 return New;
79 }
80
81 template <typename AllocatorTy> void Destroy(AllocatorTy &Allocator) {
82 // Free memory referenced by the item.
83 this->~ScopedHashTableVal();
84 Allocator.Deallocate(this);
85 }
86
87 template <typename AllocatorTy>
88 static void erase(ScopedHashTableVal<K, V> *&ThisEntry,
89 AllocatorTy &Allocator) {
90 ScopedHashTableVal<K, V> *ToDestroy = ThisEntry;
91 ScopedHashTableVal<K, V> *NextInScope = ThisEntry->NextInScope;
92 ScopedHashTableVal<K, V> *PrevInScope = ThisEntry->PreInScope;
93 if (PrevInScope)
94 PrevInScope->NextInScope = NextInScope;
95 if (NextInScope)
96 NextInScope->PreInScope = PrevInScope;
97 ThisEntry = ThisEntry->NextForKey;
98 ToDestroy->Destroy(Allocator);
99 }
100};
101
102template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
103 typename AllocatorTy = MallocAllocator>
105 /// HT - The hashtable that we are active for.
107
108 /// PrevScope - This is the scope that we are shadowing in HT.
109 ScopedHashTableScope *PrevScope;
110
111 /// LastValInScope - This is the last value that was inserted for this scope
112 /// or null if none have been inserted yet.
113 ScopedHashTableVal<K, V> *LastValInScope;
114
115public:
120
121 ScopedHashTableScope *getParentScope() { return PrevScope; }
122 const ScopedHashTableScope *getParentScope() const { return PrevScope; }
123 void erase(const K &key);
124
125private:
126 friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
127
128 ScopedHashTableVal<K, V> *getLastValInScope() {
129 return LastValInScope;
130 }
131
132 void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
133 LastValInScope = Val;
134 }
135};
136
137template <typename K, typename V, typename KInfo = DenseMapInfo<K>>
140
141public:
143
144 V &operator*() const {
145 assert(Node && "Dereference end()");
146 return Node->getValue();
147 }
148 V *operator->() const {
149 return &Node->getValue();
150 }
151
153 return Node == RHS.Node;
154 }
156 return Node != RHS.Node;
157 }
158
159 inline ScopedHashTableIterator& operator++() { // Preincrement
160 assert(Node && "incrementing past end()");
161 Node = Node->getNextForKey();
162 return *this;
163 }
164 ScopedHashTableIterator operator++(int) { // Postincrement
165 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
166 }
167};
168
169template <typename K, typename V, typename KInfo, typename AllocatorTy>
172
173public:
174 /// ScopeTy - A type alias for easy access to the name of the scope for this
175 /// hash table.
178
179private:
180 friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
181
182 using ValTy = ScopedHashTableVal<K, V>;
183
184 DenseMap<K, ValTy*, KInfo> TopLevelMap;
185 ScopeTy *CurScope = nullptr;
186
187public:
188 ScopedHashTable() = default;
189 ScopedHashTable(AllocatorTy A) : AllocTy(A) {}
192
194 assert(!CurScope && TopLevelMap.empty() && "Scope imbalance!");
195 }
196
197 /// Access to the allocator.
199
200 /// Return 1 if the specified key is in the table, 0 otherwise.
201 size_type count(const K &Key) const {
202 return TopLevelMap.count(Key);
203 }
204
205 V lookup(const K &Key) const {
206 auto I = TopLevelMap.find(Key);
207 if (I != TopLevelMap.end())
208 return I->second->getValue();
209
210 return V();
211 }
212
213 void insert(const K &Key, const V &Val) {
214 insertIntoScope(CurScope, Key, Val);
215 }
216
218
219 iterator end() { return iterator(nullptr); }
220
221 iterator begin(const K &Key) {
223 TopLevelMap.find(Key);
224 if (I == TopLevelMap.end()) return end();
225 return iterator(I->second);
226 }
227
228 ScopeTy *getCurScope() { return CurScope; }
229 const ScopeTy *getCurScope() const { return CurScope; }
230
231 /// insertIntoScope - This inserts the specified key/value at the specified
232 /// (possibly not the current) scope. While it is ok to insert into a scope
233 /// that isn't the current one, it isn't ok to insert *underneath* an existing
234 /// value of the specified key.
235 void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
236 assert(S && "No scope active!");
237 ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
238 KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
239 getAllocator());
240 S->setLastValInScope(KeyEntry);
241 }
242
243 void erase(const K &key) { CurScope->erase(key); }
244};
245
246/// ScopedHashTableScope ctor - Install this as the current scope for the hash
247/// table.
248template <typename K, typename V, typename KInfo, typename Allocator>
250 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
251 PrevScope = HT.CurScope;
252 HT.CurScope = this;
253 LastValInScope = nullptr;
254}
255
256template <typename K, typename V, typename KInfo, typename Allocator>
258 assert(HT.CurScope == this && "Scope imbalance!");
259 HT.CurScope = PrevScope;
260
261 // Pop and delete all values corresponding to this scope.
262 while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
263 // Pop this value out of the TopLevelMap.
264 if (!ThisEntry->getNextForKey()) {
265 assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
266 "Scope imbalance!");
267 HT.TopLevelMap.erase(ThisEntry->getKey());
268 } else {
269 ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
270 assert(KeyEntry == ThisEntry && "Scope imbalance!");
271 KeyEntry = ThisEntry->getNextForKey();
272 }
273
274 // Pop this value out of the scope.
275 LastValInScope = ThisEntry->getNextInScope();
276
277 // Delete this entry.
278 ThisEntry->Destroy(HT.getAllocator());
279 }
280}
281
282/// This method undoes the latest binding of the given key, effectively
283/// reverting to the previous state for that key. In the example at the
284/// beginning of this file, if we execute `HT.erase(0)` immediately after
285/// `HT.insert(0, 42);`, then the value associated with key "0" reverts to 0.
286/// This value is owned by "Scope1(HT)".
287template <typename K, typename V, typename KInfo, typename Allocator>
289 auto It = HT.TopLevelMap.find(Key);
290 if (It == HT.TopLevelMap.end())
291 return;
292 ScopedHashTableVal<K, V> *&ThisEntry = It->second;
293
294 // `ThisEntry` may be the LastValInScope of a parent scope rather than the
295 // current scope. We iterate through the scope chain to find the scope
296 // that owns ThisEntry as its LastValInScope and update it accordingly.
297 auto *S = this;
298 while (S) {
299 if (ThisEntry == S->LastValInScope) {
300 S->LastValInScope = ThisEntry->getNextInScope();
301 break;
302 }
303 S = S->PrevScope;
304 }
305 if (ThisEntry->getNextForKey() == nullptr)
306 HT.TopLevelMap.erase(It);
307 ScopedHashTableVal<K, V>::erase(ThisEntry, HT.getAllocator());
308}
309} // end namespace llvm
310
311#endif // LLVM_ADT_SCOPEDHASHTABLE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines MallocAllocator.
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
This file defines DenseMapInfo traits for DenseMap.
This file defines the DenseMap class.
#define I(x, y, z)
Definition MD5.cpp:57
Basic Register Allocator
Value * RHS
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:74
ScopedHashTableIterator(ScopedHashTableVal< K, V > *node)
bool operator==(const ScopedHashTableIterator &RHS) const
ScopedHashTableIterator & operator++()
bool operator!=(const ScopedHashTableIterator &RHS) const
ScopedHashTableIterator operator++(int)
ScopedHashTableScope & operator=(ScopedHashTableScope &)=delete
void erase(const K &key)
This method undoes the latest binding of the given key, effectively reverting to the previous state f...
ScopedHashTableScope(ScopedHashTableScope &)=delete
const ScopedHashTableScope * getParentScope() const
ScopedHashTableScope * getParentScope()
ScopedHashTableScope(ScopedHashTable< K, V, KInfo, AllocatorTy > &HT)
ScopedHashTableVal * getNextInScope()
ScopedHashTableVal * getNextForKey()
ScopedHashTableVal * getPreInScope()
static ScopedHashTableVal * Create(ScopedHashTableVal *nextInScope, ScopedHashTableVal *nextForKey, const K &key, const V &val, AllocatorTy &Allocator)
const ScopedHashTableVal * getNextForKey() const
const K & getKey() const
static void erase(ScopedHashTableVal< K, V > *&ThisEntry, AllocatorTy &Allocator)
const V & getValue() const
void Destroy(AllocatorTy &Allocator)
iterator begin(const K &Key)
ScopedHashTable(AllocatorTy A)
void erase(const K &key)
void insertIntoScope(ScopeTy *S, const K &Key, const V &Val)
insertIntoScope - This inserts the specified key/value at the specified (possibly not the current) sc...
size_type count(const K &Key) const
Return 1 if the specified key is in the table, 0 otherwise.
const ScopeTy * getCurScope() const
void insert(const K &Key, const V &Val)
V lookup(const K &Key) const
ScopedHashTableScope< K, V, KInfo, AllocatorTy > ScopeTy
ScopeTy - A type alias for easy access to the name of the scope for this hash table.
ScopedHashTable(const ScopedHashTable &)=delete
ScopedHashTable & operator=(const ScopedHashTable &)=delete
ScopedHashTableIterator< K, V, KInfo > iterator
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key