LLVM 23.0.0git
EquivalenceClasses.h
Go to the documentation of this file.
1//===- llvm/ADT/EquivalenceClasses.h - Generic Equiv. Classes ---*- 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/// Generic implementation of equivalence classes through the use Tarjan's
11/// efficient union-find algorithm.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ADT_EQUIVALENCECLASSES_H
16#define LLVM_ADT_EQUIVALENCECLASSES_H
17
18#include "llvm/ADT/DenseMap.h"
19#include "llvm/ADT/STLExtras.h"
23#include <cassert>
24#include <cstddef>
25#include <cstdint>
26#include <iterator>
27
28namespace llvm {
29
30/// This represents a collection of equivalence classes and supports three
31/// efficient operations: insert an element into a class of its own, union two
32/// classes, and find the class for a given element. In addition to these
33/// modification methods, it is possible to iterate over all of the equivalence
34/// classes and all of the elements in a class.
35///
36/// This implementation is an efficient implementation that only stores one copy
37/// of the element being indexed per entry in the set, and allows any arbitrary
38/// type to be indexed (as long as it can be implements DenseMapInfo).
39///
40/// Here is a simple example using integers:
41///
42/// \code
43/// EquivalenceClasses<int> EC;
44/// EC.unionSets(1, 2); // insert 1, 2 into the same set
45/// EC.insert(4); EC.insert(5); // insert 4, 5 into own sets
46/// EC.unionSets(5, 1); // merge the set for 1 with 5's set.
47///
48/// for (EquivalenceClasses<int>::iterator I = EC.begin(), E = EC.end();
49/// I != E; ++I) { // Iterate over all of the equivalence sets.
50/// if (!I->isLeader()) continue; // Ignore non-leader sets.
51/// for (EquivalenceClasses<int>::member_iterator MI = EC.member_begin(I);
52/// MI != EC.member_end(); ++MI) // Loop over members in this set.
53/// cerr << *MI << " "; // Print member.
54/// cerr << "\n"; // Finish set.
55/// }
56/// \endcode
57///
58/// This example prints:
59/// 4
60/// 5 1 2
61///
62template <class ElemTy> class EquivalenceClasses {
63public:
64 /// The EquivalenceClasses data structure is just a set of these.
65 /// Each of these represents a relation for a value. First it stores the
66 /// value itself. Next, it provides a "next pointer", which is used to
67 /// enumerate all of the elements in the unioned set. Finally, it defines
68 /// either a "end of list pointer" or "leader pointer" depending on whether
69 /// the value itself is a leader. A "leader pointer" points to the node that
70 /// is the leader for this element, if the node is not a leader. A "end of
71 /// list pointer" points to the last node in the list of members of this list.
72 /// Whether or not a node is a leader is determined by a bit stolen from one
73 /// of the pointers.
74 class ECValue {
75 friend class EquivalenceClasses;
76
77 mutable const ECValue *Leader, *Next;
78 ElemTy Data;
79
80 // ECValue ctor - Start out with EndOfList pointing to this node, Next is
81 // Null, isLeader = true.
82 ECValue(const ElemTy &Elt)
83 : Leader(this),
84 Next(reinterpret_cast<ECValue *>(static_cast<intptr_t>(1))),
85 Data(Elt) {}
86
87 const ECValue *getLeader() const {
88 if (isLeader())
89 return this;
90 if (Leader->isLeader())
91 return Leader;
92 // Path compression.
93 return Leader = Leader->getLeader();
94 }
95
96 const ECValue *getEndOfList() const {
97 assert(isLeader() && "Cannot get the end of a list for a non-leader!");
98 return Leader;
99 }
100
101 void setNext(const ECValue *NewNext) const {
102 assert(getNext() == nullptr && "Already has a next pointer!");
103 Next = reinterpret_cast<const ECValue *>(
104 reinterpret_cast<intptr_t>(NewNext) |
105 static_cast<intptr_t>(isLeader()));
106 }
107
108 public:
109 ECValue(const ECValue &RHS)
110 : Leader(this),
111 Next(reinterpret_cast<ECValue *>(static_cast<intptr_t>(1))),
112 Data(RHS.Data) {
113 // Only support copying of singleton nodes.
114 assert(RHS.isLeader() && RHS.getNext() == nullptr && "Not a singleton!");
115 }
116
117 bool isLeader() const { return (intptr_t)Next & 1; }
118 const ElemTy &getData() const { return Data; }
119
120 const ECValue *getNext() const {
121 return reinterpret_cast<ECValue *>(reinterpret_cast<intptr_t>(Next) &
122 ~static_cast<intptr_t>(1));
123 }
124 };
125
126private:
127 /// This implicitly provides a mapping from ElemTy values to the ECValues, it
128 /// just keeps the key as part of the value.
130
131 /// List of all members, used to provide a deterministic iteration order.
133
134 mutable BumpPtrAllocator ECValueAllocator;
135
136public:
139
141 TheMapping.clear();
142 Members.clear();
143 for (const auto &E : RHS)
144 if (E->isLeader()) {
145 member_iterator MI = RHS.member_begin(*E);
146 member_iterator LeaderIt = member_begin(insert(*MI));
147 for (++MI; MI != member_end(); ++MI)
148 unionSets(LeaderIt, member_begin(insert(*MI)));
149 }
150 return *this;
151 }
152
153 //===--------------------------------------------------------------------===//
154 // Inspection methods
155 //
156
157 /// iterator* - Provides a way to iterate over all values in the set.
159
160 iterator begin() const { return Members.begin(); }
161 iterator end() const { return Members.end(); }
162
163 bool empty() const { return TheMapping.empty(); }
164
165 /// member_* Iterate over the members of an equivalence class.
166 class member_iterator;
167 member_iterator member_begin(const ECValue &ECV) const {
168 // Only leaders provide anything to iterate over.
169 return member_iterator(ECV.isLeader() ? &ECV : nullptr);
170 }
171
172 member_iterator member_end() const { return member_iterator(nullptr); }
173
174 iterator_range<member_iterator> members(const ECValue &ECV) const {
175 return make_range(member_begin(ECV), member_end());
176 }
177
179 return make_range(findLeader(V), member_end());
180 }
181
182 /// Returns true if \p V is contained an equivalence class.
183 [[nodiscard]] bool contains(const ElemTy &V) const {
184 return TheMapping.contains(V);
185 }
186
187 /// Return the leader for the specified value that is in the set. It is an
188 /// error to call this method for a value that is not yet in the set. For
189 /// that, call getOrInsertLeaderValue(V).
190 const ElemTy &getLeaderValue(const ElemTy &V) const {
191 member_iterator MI = findLeader(V);
192 assert(MI != member_end() && "Value is not in the set!");
193 return *MI;
194 }
195
196 /// Return the leader for the specified value that is in the set. If the
197 /// member is not in the set, it is inserted, then returned.
198 const ElemTy &getOrInsertLeaderValue(const ElemTy &V) {
199 member_iterator MI = findLeader(insert(V));
200 assert(MI != member_end() && "Value is not in the set!");
201 return *MI;
202 }
203
204 /// Return the number of equivalence classes in this set. Note that this is a
205 /// linear time operation.
206 unsigned getNumClasses() const {
207 unsigned NC = 0;
208 for (const auto &E : *this)
209 if (E->isLeader())
210 ++NC;
211 return NC;
212 }
213
214 //===--------------------------------------------------------------------===//
215 // Mutation methods
216
217 /// Insert a new value into the union/find set, ignoring the request if the
218 /// value already exists.
219 const ECValue &insert(const ElemTy &Data) {
220 auto [I, Inserted] = TheMapping.try_emplace(Data);
221 if (!Inserted)
222 return *I->second;
223
224 auto *ECV = new (ECValueAllocator) ECValue(Data);
225 I->second = ECV;
226 Members.push_back(ECV);
227 return *ECV;
228 }
229
230 /// Erase a value from the union/find set, return true if erase succeeded, or
231 /// false when the value was not found.
232 bool erase(const ElemTy &V) {
233 if (!TheMapping.contains(V))
234 return false;
235 const ECValue *Cur = TheMapping[V];
236 const ECValue *Next = Cur->getNext();
237 // If the current element is the leader and has a successor element,
238 // update the successor element's 'Leader' field to be the last element,
239 // set the successor element's stolen bit, and set the 'Leader' field of
240 // all other elements in same class to be the successor element.
241 if (Cur->isLeader() && Next) {
242 Next->Leader = Cur->Leader;
243 Next->Next = reinterpret_cast<const ECValue *>(
244 reinterpret_cast<intptr_t>(Next->Next) | static_cast<intptr_t>(1));
245
246 const ECValue *NewLeader = Next;
247 while ((Next = Next->getNext())) {
248 Next->Leader = NewLeader;
249 }
250 } else if (!Cur->isLeader()) {
251 const ECValue *Leader = findLeader(V).Node;
252 const ECValue *Pre = Leader;
253 while (Pre->getNext() != Cur) {
254 Pre = Pre->getNext();
255 }
256 if (!Next) {
257 // If the current element is the last element(not leader), set the
258 // successor of the current element's predecessor to null while
259 // preserving the leader bit, and set the 'Leader' field of the class
260 // leader to the predecessor element.
261 Pre->Next = reinterpret_cast<const ECValue *>(
262 static_cast<intptr_t>(Pre->isLeader()));
263 Leader->Leader = Pre;
264 } else {
265 // If the current element is in the middle of class, then simply
266 // connect the predecessor element and the successor element.
267 Pre->Next = reinterpret_cast<const ECValue *>(
268 reinterpret_cast<intptr_t>(Next) |
269 static_cast<intptr_t>(Pre->isLeader()));
270 Next->Leader = Pre;
271 }
272 }
273
274 // Update 'TheMapping' and 'Members'.
275 assert(TheMapping.contains(V) && "Can't find input in TheMapping!");
276 TheMapping.erase(V);
277 auto I = find(Members, Cur);
278 assert(I != Members.end() && "Can't find input in members!");
279 Members.erase(I);
280 return true;
281 }
282
283 /// Given a value in the set, return a member iterator for the
284 /// equivalence class it is in. This does the path-compression part that
285 /// makes union-find "union findy". This returns an end iterator if the value
286 /// is not in the equivalence class.
287 member_iterator findLeader(const ElemTy &V) const {
288 auto I = TheMapping.find(V);
289 if (I == TheMapping.end())
290 return member_iterator(nullptr);
291 return findLeader(*I->second);
292 }
293 member_iterator findLeader(const ECValue &ECV) const {
294 return member_iterator(ECV.getLeader());
295 }
296
297 /// Erase the class containing \p V, i.e. erase all members of the class from
298 /// the set.
299 void eraseClass(const ElemTy &V) {
300 if (!TheMapping.contains(V))
301 return;
303 for (member_iterator MI = LeaderI.begin(), ME = LeaderI.end(); MI != ME;) {
304 const ElemTy &ToErase = *MI;
305 ++MI;
306 const ECValue *Cur = TheMapping[ToErase];
307 TheMapping.erase(ToErase);
308 auto I = find(Members, Cur);
309 assert(I != Members.end() && "Can't find input in members!");
310 Members.erase(I);
311 }
312 }
313
314 /// Merge the two equivalence sets for the specified values, inserting
315 /// them if they do not already exist in the equivalence set.
316 member_iterator unionSets(const ElemTy &V1, const ElemTy &V2) {
317 const ECValue &V1I = insert(V1), &V2I = insert(V2);
318 return unionSets(findLeader(V1I), findLeader(V2I));
319 }
320 member_iterator unionSets(member_iterator L1, member_iterator L2) {
321 assert(L1 != member_end() && L2 != member_end() && "Illegal inputs!");
322 if (L1 == L2)
323 return L1; // Unifying the same two sets, noop.
324
325 // Otherwise, this is a real union operation. Set the end of the L1 list to
326 // point to the L2 leader node.
327 const ECValue &L1LV = *L1.Node, &L2LV = *L2.Node;
328 L1LV.getEndOfList()->setNext(&L2LV);
329
330 // Update L1LV's end of list pointer.
331 L1LV.Leader = L2LV.getEndOfList();
332
333 // Clear L2's leader flag:
334 L2LV.Next = L2LV.getNext();
335
336 // L2's leader is now L1.
337 L2LV.Leader = &L1LV;
338 return L1;
339 }
340
341 // isEquivalent - Return true if V1 is equivalent to V2. This can happen if
342 // V1 is equal to V2 or if they belong to one equivalence class.
343 bool isEquivalent(const ElemTy &V1, const ElemTy &V2) const {
344 // Fast path: any element is equivalent to itself.
345 if (V1 == V2)
346 return true;
347 auto It = findLeader(V1);
348 return It != member_end() && It == findLeader(V2);
349 }
350
352 friend class EquivalenceClasses;
353
354 const ECValue *Node;
355
356 public:
357 using iterator_category = std::forward_iterator_tag;
358 using value_type = const ElemTy;
359 using size_type = std::size_t;
360 using difference_type = std::ptrdiff_t;
363
364 explicit member_iterator() = default;
365 explicit member_iterator(const ECValue *N) : Node(N) {}
366
368 assert(Node != nullptr && "Dereferencing end()!");
369 return Node->getData();
370 }
371 pointer operator->() const { return &operator*(); }
372
374 assert(Node != nullptr && "++'d off the end of the list!");
375 Node = Node->getNext();
376 return *this;
377 }
378
379 member_iterator operator++(int) { // postincrement operators.
380 member_iterator tmp = *this;
381 ++*this;
382 return tmp;
383 }
384
385 bool operator==(const member_iterator &RHS) const {
386 return Node == RHS.Node;
387 }
388 bool operator!=(const member_iterator &RHS) const {
389 return Node != RHS.Node;
390 }
391 };
392};
393
394} // end namespace llvm
395
396#endif // LLVM_ADT_EQUIVALENCECLASSES_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the BumpPtrAllocator interface.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
Value * RHS
The EquivalenceClasses data structure is just a set of these.
bool operator!=(const member_iterator &RHS) const
bool operator==(const member_iterator &RHS) const
member_iterator member_begin(const ECValue &ECV) const
iterator_range< member_iterator > members(const ECValue &ECV) const
bool contains(const ElemTy &V) const
Returns true if V is contained an equivalence class.
member_iterator findLeader(const ECValue &ECV) const
const ElemTy & getOrInsertLeaderValue(const ElemTy &V)
Return the leader for the specified value that is in the set.
const ECValue & insert(const ElemTy &Data)
Insert a new value into the union/find set, ignoring the request if the value already exists.
bool isEquivalent(const ElemTy &V1, const ElemTy &V2) const
member_iterator member_end() const
const ElemTy & getLeaderValue(const ElemTy &V) const
Return the leader for the specified value that is in the set.
iterator_range< member_iterator > members(const ElemTy &V) const
EquivalenceClasses & operator=(const EquivalenceClasses &RHS)
typename SmallVector< const ECValue * >::const_iterator iterator
iterator* - Provides a way to iterate over all values in the set.
member_iterator findLeader(const ElemTy &V) const
Given a value in the set, return a member iterator for the equivalence class it is in.
unsigned getNumClasses() const
Return the number of equivalence classes in this set.
member_iterator unionSets(member_iterator L1, member_iterator L2)
void eraseClass(const ElemTy &V)
Erase the class containing V, i.e.
member_iterator unionSets(const ElemTy &V1, const ElemTy &V2)
Merge the two equivalence sets for the specified values, inserting them if they do not already exist ...
bool erase(const ElemTy &V)
Erase a value from the union/find set, return true if erase succeeded, or false when the value was no...
EquivalenceClasses(const EquivalenceClasses &RHS)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A range adaptor for a pair of iterators.
IteratorT end() const
IteratorT begin() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This is an optimization pass for GlobalISel generic memory operations.
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1764
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:383
#define N
#define NC
Definition regutils.h:42