LLVM 24.0.0git
ImmutableMap.h
Go to the documentation of this file.
1//===--- ImmutableMap.h - Immutable (functional) map interface --*- 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/// \file
10/// This file defines the ImmutableMap class.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ADT_IMMUTABLEMAP_H
15#define LLVM_ADT_IMMUTABLEMAP_H
16
17#include "llvm/ADT/FoldingSet.h"
21#include <utility>
22
23namespace llvm {
24
25/// Traits class used by ImmutableMap. While both the first and second elements
26/// in a pair are used to generate profile information, only the first element
27/// (the key) is used by isEqual and isLess.
28template <typename T, typename S>
30 using value_type = const std::pair<T,S>;
31 using value_type_ref = const value_type&;
32 using key_type = const T;
33 using key_type_ref = const T&;
34 using data_type = const S;
35 using data_type_ref = const S&;
36
38 return V.first;
39 }
40
42 return V.second;
43 }
44
45 static inline bool isEqual(key_type_ref L, key_type_ref R) {
47 }
48 static inline bool isLess(key_type_ref L, key_type_ref R) {
50 }
51
52 static inline bool isDataEqual(data_type_ref L, data_type_ref R) {
54 }
55
56 static inline void Profile(FoldingSetNodeID& ID, value_type_ref V) {
59 }
60};
61
62template <typename KeyT, typename ValT,
63 typename ValInfo = ImutKeyValueInfo<KeyT, ValT>,
64 bool Canonicalize = true>
66public:
67 using value_type = typename ValInfo::value_type;
68 using value_type_ref = typename ValInfo::value_type_ref;
69 using key_type = typename ValInfo::key_type;
70 using key_type_ref = typename ValInfo::key_type_ref;
71 using data_type = typename ValInfo::data_type;
72 using data_type_ref = typename ValInfo::data_type_ref;
74
75protected:
77
78public:
79 /// Constructs a map from a pointer to a tree root. In general one
80 /// should use a Factory object to create maps instead of directly
81 /// invoking the constructor, but there are cases where make this
82 /// constructor public is useful.
83 explicit ImmutableMap(const TreeTy *R) : Root(const_cast<TreeTy *>(R)) {}
84
85 class Factory {
86 typename TreeTy::Factory F;
87
88 public:
89 Factory() = default;
90
92
93 Factory(const Factory &) = delete;
94 Factory &operator=(const Factory &) = delete;
95
96 ImmutableMap getEmptyMap() { return ImmutableMap(F.getEmptyTree()); }
97
100 TreeTy *T = F.add(Old.Root.get(), std::pair<key_type, data_type>(K, D));
101 if constexpr (Canonicalize)
102 return ImmutableMap(F.getCanonicalTree(T));
103 else
104 return ImmutableMap(T);
105 }
106
107 /// Merges \p A and \p B in a single traversal (see
108 /// ImutAVLFactory::mergeTrees), sharing subtrees the two maps do not
109 /// overlap. \p Combine(AElem, BElem) yields the stored element for a key;
110 /// \p KeepUnmatched governs keys unique to one side. \p SkipShared returns
111 /// a pointer-identical subtree unchanged in O(1); only pass true when the
112 /// merge is idempotent (Combine(a, a) == a, e.g. a lattice join). This is
113 /// more efficient than repeatedly adding \p B's entries to \p A when \p B
114 /// is large. Only valid for non-canonicalizing factories (the bulk path
115 /// does not canonicalize the nodes it creates). Does not short-circuit
116 /// equal/empty operands or reorder by size; callers wanting those apply
117 /// them first.
118 template <typename CombineFn>
120 CombineFn Combine, bool KeepUnmatched,
121 bool SkipShared = false) {
122 static_assert(!Canonicalize,
123 "mergeWith does not canonicalize the nodes it creates");
124 return ImmutableMap(F.mergeTrees(A.Root.get(), B.Root.get(), Combine,
125 KeepUnmatched, SkipShared));
126 }
127
129 TreeTy *T = F.remove(Old.Root.get(), K);
130 if constexpr (Canonicalize)
131 return ImmutableMap(F.getCanonicalTree(T));
132 else
133 return ImmutableMap(T);
134 }
135
136 /// Returns \p Old without the entries for which \p Pred is false (see
137 /// ImutAVLFactory::filterTree).
138 template <typename PredFn>
139 [[nodiscard]] ImmutableMap filter(ImmutableMap Old, PredFn Pred) {
140 TreeTy *T = F.filterTree(Old.Root.get(), Pred);
141 if constexpr (Canonicalize)
142 return ImmutableMap(F.getCanonicalTree(T));
143 else
144 return ImmutableMap(T);
145 }
146
147 typename TreeTy::Factory *getTreeFactory() const {
148 return const_cast<typename TreeTy::Factory *>(&F);
149 }
150 };
151
152 [[nodiscard]] bool contains(key_type_ref K) const {
153 return Root ? Root->contains(K) : false;
154 }
155
156 /// Compares two maps for equality. For a canonicalizing factory, maps with
157 /// equal contents share the same tree, so this is an O(1) pointer comparison
158 /// (like ImmutableList); only maps created by the same factory may be
159 /// compared. Otherwise it is a structural comparison.
160 [[nodiscard]] bool operator==(const ImmutableMap &RHS) const {
161 if constexpr (Canonicalize)
162 return Root == RHS.Root;
163 else
164 return Root && RHS.Root ? Root->isEqual(*RHS.Root.get())
165 : Root == RHS.Root;
166 }
167
168 [[nodiscard]] bool operator!=(const ImmutableMap &RHS) const {
169 if constexpr (Canonicalize)
170 return Root != RHS.Root;
171 else
172 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root.get())
173 : Root != RHS.Root;
174 }
175
176 [[nodiscard]] TreeTy *getRoot() const {
177 if (Root) { Root->retain(); }
178 return Root.get();
179 }
180
181 [[nodiscard]] TreeTy *getRootWithoutRetain() const { return Root.get(); }
182
184 if (Root) Root->retain();
185 }
186
188 if (Root) Root->release();
189 }
190
191 [[nodiscard]] bool isEmpty() const { return !Root; }
192
193public:
194 //===--------------------------------------------------===//
195 // For testing.
196 //===--------------------------------------------------===//
197
198 void verify() const { if (Root) Root->verify(); }
199
200 //===--------------------------------------------------===//
201 // Iterators.
202 //===--------------------------------------------------===//
203
204 class iterator : public ImutAVLValueIterator<ImmutableMap> {
205 friend class ImmutableMap;
206
207 iterator() = default;
208 explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
209
210 public:
211 key_type_ref getKey() const { return (*this)->first; }
212 data_type_ref getData() const { return (*this)->second; }
213 };
214
215 [[nodiscard]] iterator begin() const { return iterator(Root.get()); }
216 [[nodiscard]] iterator end() const { return iterator(); }
217
218 [[nodiscard]] data_type *lookup(key_type_ref K) const {
219 if (Root) {
220 TreeTy* T = Root->find(K);
221 if (T) return &T->getValue().second;
222 }
223
224 return nullptr;
225 }
226
227 /// Returns the <key,value> pair in the ImmutableMap for which key is the
228 /// highest in the ordering of keys in the map. This method returns NULL if
229 /// the map is empty.
230 [[nodiscard]] value_type *getMaxElement() const {
231 return Root ? &(Root->getMaxElement()->getValue()) : nullptr;
232 }
233
234 //===--------------------------------------------------===//
235 // Utility methods.
236 //===--------------------------------------------------===//
237
238 [[nodiscard]] unsigned getHeight() const {
239 return Root ? Root->getHeight() : 0;
240 }
241
242 static inline void Profile(FoldingSetNodeID& ID, const ImmutableMap& M) {
243 ID.AddPointer(M.Root.get());
244 }
245
246 inline void Profile(FoldingSetNodeID& ID) const {
247 return Profile(ID,*this);
248 }
249};
250
251// NOTE: This will possibly become the new implementation of ImmutableMap some day.
252template <typename KeyT, typename ValT,
253typename ValInfo = ImutKeyValueInfo<KeyT,ValT>>
255public:
256 using value_type = typename ValInfo::value_type;
257 using value_type_ref = typename ValInfo::value_type_ref;
258 using key_type = typename ValInfo::key_type;
259 using key_type_ref = typename ValInfo::key_type_ref;
260 using data_type = typename ValInfo::data_type;
261 using data_type_ref = typename ValInfo::data_type_ref;
263 using FactoryTy = typename TreeTy::Factory;
264
265protected:
268
269public:
270 /// Constructs a map from a pointer to a tree root. In general one
271 /// should use a Factory object to create maps instead of directly
272 /// invoking the constructor, but there are cases where make this
273 /// constructor public is useful.
275 : Root(const_cast<TreeTy *>(R)), Factory(F) {}
276
279 : Root(X.getRootWithoutRetain()), Factory(F.getTreeFactory()) {}
280
282 return ImmutableMapRef(nullptr, F);
283 }
284
286 if (Root) Root->retain();
287 }
288
290 if (Root) Root->release();
291 }
292
294 TreeTy *NewT =
295 Factory->add(Root.get(), std::pair<key_type, data_type>(K, D));
296 return ImmutableMapRef(NewT, Factory);
297 }
298
300 TreeTy *NewT = Factory->remove(Root.get(), K);
301 return ImmutableMapRef(NewT, Factory);
302 }
303
304 /// Returns this map without the entries for which \p Pred is false (see
305 /// ImutAVLFactory::filterTree).
306 template <typename PredFn> ImmutableMapRef filter(PredFn Pred) const {
307 return ImmutableMapRef(Factory->filterTree(Root.get(), Pred), Factory);
308 }
309
310 [[nodiscard]] bool contains(key_type_ref K) const {
311 return Root ? Root->contains(K) : false;
312 }
313
315 return ImmutableMap<KeyT, ValT>(Factory->getCanonicalTree(Root.get()));
316 }
317
318 [[nodiscard]] bool operator==(const ImmutableMapRef &RHS) const {
319 return Root && RHS.Root ? Root->isEqual(*RHS.Root.get()) : Root == RHS.Root;
320 }
321
322 [[nodiscard]] bool operator!=(const ImmutableMapRef &RHS) const {
323 return Root && RHS.Root ? Root->isNotEqual(*RHS.Root.get())
324 : Root != RHS.Root;
325 }
326
327 [[nodiscard]] bool isEmpty() const { return !Root; }
328
329 //===--------------------------------------------------===//
330 // For testing.
331 //===--------------------------------------------------===//
332
333 void verify() const {
334 if (Root)
335 Root->verify();
336 }
337
338 //===--------------------------------------------------===//
339 // Iterators.
340 //===--------------------------------------------------===//
341
342 class iterator : public ImutAVLValueIterator<ImmutableMapRef> {
343 friend class ImmutableMapRef;
344
345 iterator() = default;
346 explicit iterator(TreeTy *Tree) : iterator::ImutAVLValueIterator(Tree) {}
347
348 public:
349 key_type_ref getKey() const { return (*this)->first; }
350 data_type_ref getData() const { return (*this)->second; }
351 };
352
353 [[nodiscard]] iterator begin() const { return iterator(Root.get()); }
354 [[nodiscard]] iterator end() const { return iterator(); }
355
356 [[nodiscard]] data_type *lookup(key_type_ref K) const {
357 if (Root) {
358 TreeTy* T = Root->find(K);
359 if (T) return &T->getValue().second;
360 }
361
362 return nullptr;
363 }
364
365 /// Returns the <key,value> pair in the ImmutableMap for which key is the
366 /// highest in the ordering of keys in the map. This method returns NULL if
367 /// the map is empty.
368 [[nodiscard]] value_type *getMaxElement() const {
369 return Root ? &(Root->getMaxElement()->getValue()) : nullptr;
370 }
371
372 //===--------------------------------------------------===//
373 // Utility methods.
374 //===--------------------------------------------------===//
375
376 [[nodiscard]] unsigned getHeight() const {
377 return Root ? Root->getHeight() : 0;
378 }
379
380 static inline void Profile(FoldingSetNodeID &ID, const ImmutableMapRef &M) {
381 ID.AddPointer(M.Root.get());
382 }
383
384 inline void Profile(FoldingSetNodeID &ID) const { return Profile(ID, *this); }
385};
386
387} // end namespace llvm
388
389#endif // LLVM_ADT_IMMUTABLEMAP_H
SDValue Pred
AMDGPU Uniform Intrinsic Combine
This file defines the BumpPtrAllocator interface.
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define LLVM_ATTRIBUTE_WARN_UNUSED
Definition Compiler.h:240
This file defines a hash set that can be used to remove duplication of nodes in a graph.
This file defines the ImutAVLTree and ImmutableSet classes.
static constexpr Value * getValue(Ty &ValueOrUse)
#define F(x, y, z)
Definition MD5.cpp:54
Load MIR Sample Profile
#define T
Value * RHS
This class is used to gather all the unique data bits of a node.
Definition FoldingSet.h:162
key_type_ref getKey() const
data_type_ref getData() const
unsigned getHeight() const
ImmutableMap< KeyT, ValT > asImmutableMap() const
static ImmutableMapRef getEmptyMap(FactoryTy *F)
void Profile(FoldingSetNodeID &ID) const
bool operator!=(const ImmutableMapRef &RHS) const
bool contains(key_type_ref K) const
static void Profile(FoldingSetNodeID &ID, const ImmutableMapRef &M)
typename ValInfo::data_type data_type
typename ValInfo::data_type_ref data_type_ref
typename ValInfo::key_type key_type
IntrusiveRefCntPtr< TreeTy > Root
ImutAVLTree< ValInfo > TreeTy
typename ValInfo::value_type_ref value_type_ref
bool operator==(const ImmutableMapRef &RHS) const
ImmutableMapRef remove(key_type_ref K) const
typename ValInfo::key_type_ref key_type_ref
value_type * getMaxElement() const
Returns the <key,value> pair in the ImmutableMap for which key is the highest in the ordering of keys...
ImmutableMapRef add(key_type_ref K, data_type_ref D) const
typename ValInfo::value_type value_type
iterator begin() const
ImmutableMapRef filter(PredFn Pred) const
Returns this map without the entries for which Pred is false (see ImutAVLFactory::filterTree).
ImmutableMapRef(const TreeTy *R, FactoryTy *F)
Constructs a map from a pointer to a tree root.
data_type * lookup(key_type_ref K) const
iterator end() const
ImmutableMapRef(const ImmutableMap< KeyT, ValT > &X, typename ImmutableMap< KeyT, ValT >::Factory &F)
typename TreeTy::Factory FactoryTy
TreeTy::Factory * getTreeFactory() const
ImmutableMap remove(ImmutableMap Old, key_type_ref K)
Factory(BumpPtrAllocator &Alloc)
Factory & operator=(const Factory &)=delete
ImmutableMap filter(ImmutableMap Old, PredFn Pred)
Returns Old without the entries for which Pred is false (see ImutAVLFactory::filterTree).
ImmutableMap mergeWith(ImmutableMap A, ImmutableMap B, CombineFn Combine, bool KeepUnmatched, bool SkipShared=false)
Merges A and B in a single traversal (see ImutAVLFactory::mergeTrees), sharing subtrees the two maps ...
ImmutableMap add(ImmutableMap Old, key_type_ref K, data_type_ref D)
Factory(const Factory &)=delete
key_type_ref getKey() const
data_type_ref getData() const
typename ValInfo::data_type_ref data_type_ref
TreeTy * getRootWithoutRetain() const
bool isEmpty() const
iterator begin() const
typename ValInfo::key_type_ref key_type_ref
TreeTy * getRoot() const
typename ValInfo::value_type_ref value_type_ref
iterator end() const
value_type * getMaxElement() const
Returns the <key,value> pair in the ImmutableMap for which key is the highest in the ordering of keys...
void verify() const
typename ValInfo::key_type key_type
typename ValInfo::value_type value_type
typename ValInfo::data_type data_type
void Profile(FoldingSetNodeID &ID) const
bool operator!=(const ImmutableMap &RHS) const
ImmutableMap(const TreeTy *R)
Constructs a map from a pointer to a tree root.
bool contains(key_type_ref K) const
unsigned getHeight() const
data_type * lookup(key_type_ref K) const
IntrusiveRefCntPtr< TreeTy > Root
bool operator==(const ImmutableMap &RHS) const
Compares two maps for equality.
ImutAVLTree< ValInfo, Canonicalize > TreeTy
static void Profile(FoldingSetNodeID &ID, const ImmutableMap &M)
ImutAVLFactory< ValInfo, Canonicalize > Factory
A smart pointer to a reference-counted object that inherits from RefCountedBase or ThreadSafeRefCount...
This is an optimization pass for GlobalISel generic memory operations.
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
static bool isLess(key_type_ref LHS, key_type_ref RHS)
static bool isEqual(key_type_ref LHS, key_type_ref RHS)
Traits class used by ImmutableMap.
const value_type & value_type_ref
static void Profile(FoldingSetNodeID &ID, value_type_ref V)
static bool isLess(key_type_ref L, key_type_ref R)
static key_type_ref KeyOfValue(value_type_ref V)
const std::pair< T, S > value_type
static data_type_ref DataOfValue(value_type_ref V)
static bool isDataEqual(data_type_ref L, data_type_ref R)
static bool isEqual(key_type_ref L, key_type_ref R)
static void Profile(FoldingSetNodeID &ID, value_type_ref X)