LLVM 20.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>
47 ScopedHashTableVal *NextInScope;
48 ScopedHashTableVal *NextForKey;
49 K Key;
50 V Val;
51
52 ScopedHashTableVal(const K &key, const V &val) : Key(key), Val(val) {}
53
54public:
55 const K &getKey() const { return Key; }
56 const V &getValue() const { return Val; }
57 V &getValue() { return Val; }
58
59 ScopedHashTableVal *getNextForKey() { return NextForKey; }
60 const ScopedHashTableVal *getNextForKey() const { return NextForKey; }
61 ScopedHashTableVal *getNextInScope() { return NextInScope; }
62
63 template <typename AllocatorTy>
65 ScopedHashTableVal *nextForKey,
66 const K &key, const V &val,
67 AllocatorTy &Allocator) {
68 ScopedHashTableVal *New = Allocator.template Allocate<ScopedHashTableVal>();
69 // Set up the value.
70 new (New) ScopedHashTableVal(key, val);
71 New->NextInScope = nextInScope;
72 New->NextForKey = nextForKey;
73 return New;
74 }
75
76 template <typename AllocatorTy> void Destroy(AllocatorTy &Allocator) {
77 // Free memory referenced by the item.
78 this->~ScopedHashTableVal();
79 Allocator.Deallocate(this);
80 }
81};
82
83template <typename K, typename V, typename KInfo = DenseMapInfo<K>,
84 typename AllocatorTy = MallocAllocator>
86 /// HT - The hashtable that we are active for.
88
89 /// PrevScope - This is the scope that we are shadowing in HT.
90 ScopedHashTableScope *PrevScope;
91
92 /// LastValInScope - This is the last value that was inserted for this scope
93 /// or null if none have been inserted yet.
94 ScopedHashTableVal<K, V> *LastValInScope;
95
96public:
101
102 ScopedHashTableScope *getParentScope() { return PrevScope; }
103 const ScopedHashTableScope *getParentScope() const { return PrevScope; }
104
105private:
106 friend class ScopedHashTable<K, V, KInfo, AllocatorTy>;
107
108 ScopedHashTableVal<K, V> *getLastValInScope() {
109 return LastValInScope;
110 }
111
112 void setLastValInScope(ScopedHashTableVal<K, V> *Val) {
113 LastValInScope = Val;
114 }
115};
116
117template <typename K, typename V, typename KInfo = DenseMapInfo<K>>
120
121public:
123
124 V &operator*() const {
125 assert(Node && "Dereference end()");
126 return Node->getValue();
127 }
128 V *operator->() const {
129 return &Node->getValue();
130 }
131
133 return Node == RHS.Node;
134 }
136 return Node != RHS.Node;
137 }
138
139 inline ScopedHashTableIterator& operator++() { // Preincrement
140 assert(Node && "incrementing past end()");
141 Node = Node->getNextForKey();
142 return *this;
143 }
144 ScopedHashTableIterator operator++(int) { // Postincrement
145 ScopedHashTableIterator tmp = *this; ++*this; return tmp;
146 }
147};
148
149template <typename K, typename V, typename KInfo, typename AllocatorTy>
152
153public:
154 /// ScopeTy - This is a helpful typedef that allows clients to get easy access
155 /// to the name of the scope for this hash table.
158
159private:
160 friend class ScopedHashTableScope<K, V, KInfo, AllocatorTy>;
161
163
164 DenseMap<K, ValTy*, KInfo> TopLevelMap;
165 ScopeTy *CurScope = nullptr;
166
167public:
168 ScopedHashTable() = default;
169 ScopedHashTable(AllocatorTy A) : AllocTy(A) {}
172
174 assert(!CurScope && TopLevelMap.empty() && "Scope imbalance!");
175 }
176
177 /// Access to the allocator.
179
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);
183 }
184
185 V lookup(const K &Key) const {
186 auto I = TopLevelMap.find(Key);
187 if (I != TopLevelMap.end())
188 return I->second->getValue();
189
190 return V();
191 }
192
193 void insert(const K &Key, const V &Val) {
194 insertIntoScope(CurScope, Key, Val);
195 }
196
198
199 iterator end() { return iterator(nullptr); }
200
201 iterator begin(const K &Key) {
203 TopLevelMap.find(Key);
204 if (I == TopLevelMap.end()) return end();
205 return iterator(I->second);
206 }
207
208 ScopeTy *getCurScope() { return CurScope; }
209 const ScopeTy *getCurScope() const { return CurScope; }
210
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,
219 getAllocator());
220 S->setLastValInScope(KeyEntry);
221 }
222};
223
224/// ScopedHashTableScope ctor - Install this as the current scope for the hash
225/// table.
226template <typename K, typename V, typename KInfo, typename Allocator>
228 ScopedHashTableScope(ScopedHashTable<K, V, KInfo, Allocator> &ht) : HT(ht) {
229 PrevScope = HT.CurScope;
230 HT.CurScope = this;
231 LastValInScope = nullptr;
232}
233
234template <typename K, typename V, typename KInfo, typename Allocator>
236 assert(HT.CurScope == this && "Scope imbalance!");
237 HT.CurScope = PrevScope;
238
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 &&
244 "Scope imbalance!");
245 HT.TopLevelMap.erase(ThisEntry->getKey());
246 } else {
247 ScopedHashTableVal<K, V> *&KeyEntry = HT.TopLevelMap[ThisEntry->getKey()];
248 assert(KeyEntry == ThisEntry && "Scope imbalance!");
249 KeyEntry = ThisEntry->getNextForKey();
250 }
251
252 // Pop this value out of the scope.
253 LastValInScope = ThisEntry->getNextInScope();
254
255 // Delete this entry.
256 ThisEntry->Destroy(HT.getAllocator());
257 }
258}
259
260} // end namespace llvm
261
262#endif // LLVM_ADT_SCOPEDHASHTABLE_H
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:58
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Value * RHS
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:156
bool empty() const
Definition: DenseMap.h:98
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:152
iterator end()
Definition: DenseMap.h:84
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
ScopedHashTableScope(ScopedHashTableScope &)=delete
const ScopedHashTableScope * getParentScope() const
ScopedHashTableScope * getParentScope()
ScopedHashTableScope(ScopedHashTable< K, V, KInfo, AllocatorTy > &HT)
ScopedHashTableVal * getNextInScope()
ScopedHashTableVal * getNextForKey()
static ScopedHashTableVal * Create(ScopedHashTableVal *nextInScope, ScopedHashTableVal *nextForKey, const K &key, const V &val, AllocatorTy &Allocator)
const ScopedHashTableVal * getNextForKey() const
const K & getKey() const
const V & getValue() const
void Destroy(AllocatorTy &Allocator)
iterator begin(const K &Key)
ScopedHashTable(AllocatorTy A)
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
ScopedHashTable(const ScopedHashTable &)=delete
ScopedHashTable & operator=(const ScopedHashTable &)=delete
ScopedHashTableIterator< K, V, KInfo > iterator
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18