LLVM 24.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"
37#include <cassert>
38#include <new>
39
40namespace llvm {
41
42template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
43 typename AllocatorTy = MallocAllocator>
44class ScopedHashTable;
45
46template <typename K, typename V>
47class ScopedHashTableVal {
48 ScopedHashTableVal *NextInScope;
49 ScopedHashTableVal *NextForKey;
50 ScopedHashTableVal *PreInScope;
51 K Key;
52 V Val;
53
54 ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
55
56public:
57 const K &getKey() const { return Key; }
58 const V &getValue() const { return Val; }
59 V &getValue() { return Val; }
60
61 ScopedHashTableVal *getNextForKey() { return NextForKey; }
62 const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
63 ScopedHashTableVal *getNextInScope() { return NextInScope; }
64 ScopedHashTableVal *getPreInScope() { return PreInScope; }
65
66 template <typename AllocatorTy>
67 static ScopedHashTableVal *Create(ScopedHashTableVal *nextInScope,
68 ScopedHashTableVal *nextForKey,
69 const K &key, const V &val,
70 AllocatorTy &Allocator) {
71 ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
72 // Set up the value.
73 new (New) ScopedHashTableVal(key, val);
74 New->NextInScope = nextInScope;
75 New->NextForKey = nextForKey;
76 New->PreInScope = nullptr;
77 if (nextInScope)
78 nextInScope->PreInScope = New;
79 return New;
80 }
81
82 template <typename AllocatorTy> void Destroy(AllocatorTy &Allocator) {
83 // Free memory referenced by the item.
84 this->~ScopedHashTableVal();
85 Allocator.Deallocate(this);
86 }
87
88 template <typename AllocatorTy>
89 static void erase(ScopedHashTableVal<K, V> *&ThisEntry,
90 AllocatorTy &Allocator) {
91 ScopedHashTableVal<K, V> *ToDestroy = ThisEntry;
92 ScopedHashTableVal<K, V> *NextInScope = ThisEntry->NextInScope;
93 ScopedHashTableVal<K, V> *PrevInScope = ThisEntry->PreInScope;
94 if (PrevInScope)
95 PrevInScope->NextInScope = NextInScope;
96 if (NextInScope)
97 NextInScope->PreInScope = PrevInScope;
98 ThisEntry = ThisEntry->NextForKey;
99 ToDestroy->Destroy(Allocator);
100 }
101};
102
103template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
104 typename AllocatorTy = MallocAllocator>
106 /// HT - The hashtable that we are active for.
108
109 /// PrevScope - This is the scope that we are shadowing in HT.
110 ScopedHashTableScope *PrevScope;
111
112 /// LastValInScope - This is the last value that was inserted for this scope
113 /// or null if none have been inserted yet.
114 ScopedHashTableVal<K, V> *LastValInScope;
115
116public:
121
122 ScopedHashTableScope *getParentScope() { return PrevScope; }
123 const ScopedHashTableScope *getParentScope() const { return PrevScope; }
124 void erase(const K &key);
125
126private:
127 friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
128
129 ScopedHashTableVal<K, V> *getLastValInScope() {
130 return LastValInScope;
131 }
132
133 void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
134 LastValInScope = Val;
135 }
136};
137
138template <typename K, typename V, typename KInfo = DenseMapInfo<K>>
141
142public:
144
145 V &operator*() const {
146 assert(Node && "Dereference end()");
147 return Node->getValue();
148 }
149 V *operator->() const {
150 return &Node->getValue();
151 }
152
154 return Node == RHS.Node;
155 }
157 return Node != RHS.Node;
158 }
159
160 inline ScopedHashTableIterator& operator++() { // Preincrement
161 assert(Node && "incrementing past end()");
162 Node = Node->getNextForKey();
163 return *this;
164 }
165 ScopedHashTableIterator operator++(int) { // Postincrement
166 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
167 }
168};
169
170template <typename K, typename V, typename KInfo, typename AllocatorTy>
172 : detail::AllocatorHolder<AllocatorTy> {
174
175public:
176 /// ScopeTy - A type alias for easy access to the name of the scope for this
177 /// hash table.
180
181private:
182 friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
183
184 using ValTy = ScopedHashTableVal<K, V>;
185
186 DenseMap<K, ValTy*, KInfo> TopLevelMap;
187 ScopeTy *CurScope = nullptr;
188
189public:
190 ScopedHashTable() = default;
191 ScopedHashTable(AllocatorTy A) : AllocTy(A) {}
194
196 assert(!CurScope && TopLevelMap.empty() && "Scope imbalance!");
197 }
198
199 /// Access to the allocator.
200 using AllocTy::getAllocator;
201
202 /// Return 1 if the specified key is in the table, 0 otherwise.
203 size_type count(const K &Key) const {
204 return TopLevelMap.count(Key);
205 }
206
207 V lookup(const K &Key) const {
208 auto I = TopLevelMap.find(Key);
209 if (I != TopLevelMap.end())
210 return I->second->getValue();
211
212 return V();
213 }
214
215 void insert(const K &Key, const V &Val) {
216 insertIntoScope(CurScope, Key, Val);
217 }
218
220
221 iterator end() { return iterator(nullptr); }
222
223 iterator begin(const K &Key) {
224 auto I = TopLevelMap.find(Key);
225 if (I == TopLevelMap.end()) return end();
226 return iterator(I->second);
227 }
228
229 ScopeTy *getCurScope() { return CurScope; }
230 const ScopeTy *getCurScope() const { return CurScope; }
231
232 /// insertIntoScope - This inserts the specified key/value at the specified
233 /// (possibly not the current) scope. While it is ok to insert into a scope
234 /// that isn't the current one, it isn't ok to insert *underneath* an existing
235 /// value of the specified key.
236 void insertIntoScope(ScopeTy *S, const K &Key, const V &Val) {
237 assert(S && "No scope active!");
238 ScopedHashTableVal<K, V> *&KeyEntry = TopLevelMap[Key];
239 KeyEntry = ValTy::Create(S->getLastValInScope(), KeyEntry, Key, Val,
240 getAllocator());
241 S->setLastValInScope(KeyEntry);
242 }
243
244 void erase(const K &key) { CurScope->erase(key); }
245};
246
247/// ScopedHashTableScope ctor - Install this as the current scope for the hash
248/// table.
249template <typename K, typename V, typename KInfo, typename Allocator>
251 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
252 PrevScope = HT.CurScope;
253 HT.CurScope = this;
254 LastValInScope = nullptr;
255}
256
257template <typename K, typename V, typename KInfo, typename Allocator>
259 assert(HT.CurScope == this && "Scope imbalance!");
260 HT.CurScope = PrevScope;
261
262 // Pop and delete all values corresponding to this scope.
263 while (ScopedHashTableVal<K, V> *ThisEntry = LastValInScope) {
264 // Pop this value out of the TopLevelMap.
265 if (!ThisEntry->getNextForKey()) {
266 assert(HT.TopLevelMap[ThisEntry->getKey()] == ThisEntry &&
267 "Scope imbalance!");
268 HT.TopLevelMap.erase(ThisEntry->getKey());
269 } else {
270 ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
271 assert(KeyEntry == ThisEntry && "Scope imbalance!");
272 KeyEntry = ThisEntry->getNextForKey();
273 }
274
275 // Pop this value out of the scope.
276 LastValInScope = ThisEntry->getNextInScope();
277
278 // Delete this entry.
279 ThisEntry->Destroy(HT.getAllocator());
280 }
281}
282
283/// This method undoes the latest binding of the given key, effectively
284/// reverting to the previous state for that key. In the example at the
285/// beginning of this file, if we execute `HT.erase(0)` immediately after
286/// `HT.insert(0, 42);`, then the value associated with key "0" reverts to 0.
287/// This value is owned by "Scope1(HT)".
288template <typename K, typename V, typename KInfo, typename Allocator>
290 auto It = HT.TopLevelMap.find(Key);
291 if (It == HT.TopLevelMap.end())
292 return;
293 ScopedHashTableVal<K, V> *&ThisEntry = It->second;
294
295 // `ThisEntry` may be the LastValInScope of a parent scope rather than the
296 // current scope. We iterate through the scope chain to find the scope
297 // that owns ThisEntry as its LastValInScope and update it accordingly.
298 auto *S = this;
299 while (S) {
300 if (ThisEntry == S->LastValInScope) {
301 S->LastValInScope = ThisEntry->getNextInScope();
302 break;
303 }
304 S = S->PrevScope;
305 }
306 if (ThisEntry->getNextForKey() == nullptr)
307 HT.TopLevelMap.erase(It);
308 ScopedHashTableVal<K, V>::erase(ThisEntry, HT.getAllocator());
309}
310} // end namespace llvm
311
312#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")
#define LLVM_ATTRIBUTE_WARN_UNUSED
Definition Compiler.h:240
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
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 MachineInstr *&Key, const unsigned &Val)
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