LLVM  16.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 <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <iterator>
22 #include <set>
23 
24 namespace llvm {
25 
26 /// EquivalenceClasses - This represents a collection of equivalence classes and
27 /// supports three efficient operations: insert an element into a class of its
28 /// own, union two classes, and find the class for a given element. In
29 /// addition to these modification methods, it is possible to iterate over all
30 /// of the equivalence classes and all of the elements in a class.
31 ///
32 /// This implementation is an efficient implementation that only stores one copy
33 /// of the element being indexed per entry in the set, and allows any arbitrary
34 /// type to be indexed (as long as it can be ordered with operator< or a
35 /// comparator is provided).
36 ///
37 /// Here is a simple example using integers:
38 ///
39 /// \code
40 /// EquivalenceClasses<int> EC;
41 /// EC.unionSets(1, 2); // insert 1, 2 into the same set
42 /// EC.insert(4); EC.insert(5); // insert 4, 5 into own sets
43 /// EC.unionSets(5, 1); // merge the set for 1 with 5's set.
44 ///
45 /// for (EquivalenceClasses<int>::iterator I = EC.begin(), E = EC.end();
46 /// I != E; ++I) { // Iterate over all of the equivalence sets.
47 /// if (!I->isLeader()) continue; // Ignore non-leader sets.
48 /// for (EquivalenceClasses<int>::member_iterator MI = EC.member_begin(I);
49 /// MI != EC.member_end(); ++MI) // Loop over members in this set.
50 /// cerr << *MI << " "; // Print member.
51 /// cerr << "\n"; // Finish set.
52 /// }
53 /// \endcode
54 ///
55 /// This example prints:
56 /// 4
57 /// 5 1 2
58 ///
59 template <class ElemTy, class Compare = std::less<ElemTy>>
61  /// ECValue - The EquivalenceClasses data structure is just a set of these.
62  /// Each of these represents a relation for a value. First it stores the
63  /// value itself, which provides the ordering that the set queries. Next, it
64  /// provides a "next pointer", which is used to enumerate all of the elements
65  /// in the unioned set. Finally, it defines either a "end of list pointer" or
66  /// "leader pointer" depending on whether the value itself is a leader. A
67  /// "leader pointer" points to the node that is the leader for this element,
68  /// if the node is not a leader. A "end of list pointer" points to the last
69  /// node in the list of members of this list. Whether or not a node is a
70  /// leader is determined by a bit stolen from one of the pointers.
71  class ECValue {
72  friend class EquivalenceClasses;
73 
74  mutable const ECValue *Leader, *Next;
75  ElemTy Data;
76 
77  // ECValue ctor - Start out with EndOfList pointing to this node, Next is
78  // Null, isLeader = true.
79  ECValue(const ElemTy &Elt)
80  : Leader(this), Next((ECValue*)(intptr_t)1), Data(Elt) {}
81 
82  const ECValue *getLeader() const {
83  if (isLeader()) return this;
84  if (Leader->isLeader()) return Leader;
85  // Path compression.
86  return Leader = Leader->getLeader();
87  }
88 
89  const ECValue *getEndOfList() const {
90  assert(isLeader() && "Cannot get the end of a list for a non-leader!");
91  return Leader;
92  }
93 
94  void setNext(const ECValue *NewNext) const {
95  assert(getNext() == nullptr && "Already has a next pointer!");
96  Next = (const ECValue*)((intptr_t)NewNext | (intptr_t)isLeader());
97  }
98 
99  public:
100  ECValue(const ECValue &RHS) : Leader(this), Next((ECValue*)(intptr_t)1),
101  Data(RHS.Data) {
102  // Only support copying of singleton nodes.
103  assert(RHS.isLeader() && RHS.getNext() == nullptr && "Not a singleton!");
104  }
105 
106  bool isLeader() const { return (intptr_t)Next & 1; }
107  const ElemTy &getData() const { return Data; }
108 
109  const ECValue *getNext() const {
110  return (ECValue*)((intptr_t)Next & ~(intptr_t)1);
111  }
112  };
113 
114  /// A wrapper of the comparator, to be passed to the set.
115  struct ECValueComparator {
116  using is_transparent = void;
117 
118  ECValueComparator() : compare(Compare()) {}
119 
120  bool operator()(const ECValue &lhs, const ECValue &rhs) const {
121  return compare(lhs.Data, rhs.Data);
122  }
123 
124  template <typename T>
125  bool operator()(const T &lhs, const ECValue &rhs) const {
126  return compare(lhs, rhs.Data);
127  }
128 
129  template <typename T>
130  bool operator()(const ECValue &lhs, const T &rhs) const {
131  return compare(lhs.Data, rhs);
132  }
133 
134  const Compare compare;
135  };
136 
137  /// TheMapping - This implicitly provides a mapping from ElemTy values to the
138  /// ECValues, it just keeps the key as part of the value.
139  std::set<ECValue, ECValueComparator> TheMapping;
140 
141 public:
142  EquivalenceClasses() = default;
144  operator=(RHS);
145  }
146 
148  TheMapping.clear();
149  for (iterator I = RHS.begin(), E = RHS.end(); I != E; ++I)
150  if (I->isLeader()) {
151  member_iterator MI = RHS.member_begin(I);
152  member_iterator LeaderIt = member_begin(insert(*MI));
153  for (++MI; MI != member_end(); ++MI)
154  unionSets(LeaderIt, member_begin(insert(*MI)));
155  }
156  return *this;
157  }
158 
159  //===--------------------------------------------------------------------===//
160  // Inspection methods
161  //
162 
163  /// iterator* - Provides a way to iterate over all values in the set.
164  using iterator =
165  typename std::set<ECValue, ECValueComparator>::const_iterator;
166 
167  iterator begin() const { return TheMapping.begin(); }
168  iterator end() const { return TheMapping.end(); }
169 
170  bool empty() const { return TheMapping.empty(); }
171 
172  /// member_* Iterate over the members of an equivalence class.
173  class member_iterator;
174  member_iterator member_begin(iterator I) const {
175  // Only leaders provide anything to iterate over.
176  return member_iterator(I->isLeader() ? &*I : nullptr);
177  }
178  member_iterator member_end() const {
179  return member_iterator(nullptr);
180  }
181 
182  /// findValue - Return an iterator to the specified value. If it does not
183  /// exist, end() is returned.
184  iterator findValue(const ElemTy &V) const {
185  return TheMapping.find(V);
186  }
187 
188  /// getLeaderValue - Return the leader for the specified value that is in the
189  /// set. It is an error to call this method for a value that is not yet in
190  /// the set. For that, call getOrInsertLeaderValue(V).
191  const ElemTy &getLeaderValue(const ElemTy &V) const {
192  member_iterator MI = findLeader(V);
193  assert(MI != member_end() && "Value is not in the set!");
194  return *MI;
195  }
196 
197  /// getOrInsertLeaderValue - Return the leader for the specified value that is
198  /// in the set. If the member is not in the set, it is inserted, then
199  /// returned.
200  const ElemTy &getOrInsertLeaderValue(const ElemTy &V) {
201  member_iterator MI = findLeader(insert(V));
202  assert(MI != member_end() && "Value is not in the set!");
203  return *MI;
204  }
205 
206  /// getNumClasses - Return the number of equivalence classes in this set.
207  /// Note that this is a linear time operation.
208  unsigned getNumClasses() const {
209  unsigned NC = 0;
210  for (iterator I = begin(), E = end(); I != E; ++I)
211  if (I->isLeader()) ++NC;
212  return NC;
213  }
214 
215  //===--------------------------------------------------------------------===//
216  // Mutation methods
217 
218  /// insert - Insert a new value into the union/find set, ignoring the request
219  /// if the value already exists.
220  iterator insert(const ElemTy &Data) {
221  return TheMapping.insert(ECValue(Data)).first;
222  }
223 
224  /// findLeader - Given a value in the set, return a member iterator for the
225  /// equivalence class it is in. This does the path-compression part that
226  /// makes union-find "union findy". This returns an end iterator if the value
227  /// is not in the equivalence class.
228  member_iterator findLeader(iterator I) const {
229  if (I == TheMapping.end()) return member_end();
230  return member_iterator(I->getLeader());
231  }
232  member_iterator findLeader(const ElemTy &V) const {
233  return findLeader(TheMapping.find(V));
234  }
235 
236  /// union - Merge the two equivalence sets for the specified values, inserting
237  /// them if they do not already exist in the equivalence set.
238  member_iterator unionSets(const ElemTy &V1, const ElemTy &V2) {
239  iterator V1I = insert(V1), V2I = insert(V2);
240  return unionSets(findLeader(V1I), findLeader(V2I));
241  }
242  member_iterator unionSets(member_iterator L1, member_iterator L2) {
243  assert(L1 != member_end() && L2 != member_end() && "Illegal inputs!");
244  if (L1 == L2) return L1; // Unifying the same two sets, noop.
245 
246  // Otherwise, this is a real union operation. Set the end of the L1 list to
247  // point to the L2 leader node.
248  const ECValue &L1LV = *L1.Node, &L2LV = *L2.Node;
249  L1LV.getEndOfList()->setNext(&L2LV);
250 
251  // Update L1LV's end of list pointer.
252  L1LV.Leader = L2LV.getEndOfList();
253 
254  // Clear L2's leader flag:
255  L2LV.Next = L2LV.getNext();
256 
257  // L2's leader is now L1.
258  L2LV.Leader = &L1LV;
259  return L1;
260  }
261 
262  // isEquivalent - Return true if V1 is equivalent to V2. This can happen if
263  // V1 is equal to V2 or if they belong to one equivalence class.
264  bool isEquivalent(const ElemTy &V1, const ElemTy &V2) const {
265  // Fast path: any element is equivalent to itself.
266  if (V1 == V2)
267  return true;
268  auto It = findLeader(V1);
269  return It != member_end() && It == findLeader(V2);
270  }
271 
273  friend class EquivalenceClasses;
274 
275  const ECValue *Node;
276 
277  public:
278  using iterator_category = std::forward_iterator_tag;
279  using value_type = const ElemTy;
280  using size_type = std::size_t;
281  using difference_type = std::ptrdiff_t;
282  using pointer = value_type *;
284 
285  explicit member_iterator() = default;
286  explicit member_iterator(const ECValue *N) : Node(N) {}
287 
289  assert(Node != nullptr && "Dereferencing end()!");
290  return Node->getData();
291  }
292  pointer operator->() const { return &operator*(); }
293 
295  assert(Node != nullptr && "++'d off the end of the list!");
296  Node = Node->getNext();
297  return *this;
298  }
299 
300  member_iterator operator++(int) { // postincrement operators.
301  member_iterator tmp = *this;
302  ++*this;
303  return tmp;
304  }
305 
306  bool operator==(const member_iterator &RHS) const {
307  return Node == RHS.Node;
308  }
309  bool operator!=(const member_iterator &RHS) const {
310  return Node != RHS.Node;
311  }
312  };
313 };
314 
315 } // end namespace llvm
316 
317 #endif // LLVM_ADT_EQUIVALENCECLASSES_H
llvm::EquivalenceClasses::member_iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: EquivalenceClasses.h:278
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:109
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::EquivalenceClasses::operator=
const EquivalenceClasses & operator=(const EquivalenceClasses &RHS)
Definition: EquivalenceClasses.h:147
intptr_t
llvm::EquivalenceClasses::getLeaderValue
const ElemTy & getLeaderValue(const ElemTy &V) const
getLeaderValue - Return the leader for the specified value that is in the set.
Definition: EquivalenceClasses.h:191
llvm::EquivalenceClasses::member_iterator::value_type
const ElemTy value_type
Definition: EquivalenceClasses.h:279
llvm::EquivalenceClasses::member_end
member_iterator member_end() const
Definition: EquivalenceClasses.h:178
llvm::EquivalenceClasses
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
Definition: EquivalenceClasses.h:60
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::EquivalenceClasses::insert
iterator insert(const ElemTy &Data)
insert - Insert a new value into the union/find set, ignoring the request if the value already exists...
Definition: EquivalenceClasses.h:220
RHS
Value * RHS
Definition: X86PartialReduction.cpp:76
tmp
alloca< 16 x float >, align 16 %tmp2=alloca< 16 x float >, align 16 store< 16 x float > %A,< 16 x float > *%tmp %s=bitcast< 16 x float > *%tmp to i8 *%s2=bitcast< 16 x float > *%tmp2 to i8 *call void @llvm.memcpy.i64(i8 *%s, i8 *%s2, i64 64, i32 16) %R=load< 16 x float > *%tmp2 ret< 16 x float > %R } declare void @llvm.memcpy.i64(i8 *nocapture, i8 *nocapture, i64, i32) nounwind which compiles to:_foo:subl $140, %esp movaps %xmm3, 112(%esp) movaps %xmm2, 96(%esp) movaps %xmm1, 80(%esp) movaps %xmm0, 64(%esp) movl 60(%esp), %eax movl %eax, 124(%esp) movl 56(%esp), %eax movl %eax, 120(%esp) movl 52(%esp), %eax< many many more 32-bit copies > movaps(%esp), %xmm0 movaps 16(%esp), %xmm1 movaps 32(%esp), %xmm2 movaps 48(%esp), %xmm3 addl $140, %esp ret On Nehalem, it may even be cheaper to just use movups when unaligned than to fall back to lower-granularity chunks. Implement processor-specific optimizations for parity with GCC on these processors. GCC does two optimizations:1. ix86_pad_returns inserts a noop before ret instructions if immediately preceded by a conditional branch or is the target of a jump. 2. ix86_avoid_jump_misspredicts inserts noops in cases where a 16-byte block of code contains more than 3 branches. The first one is done for all AMDs, Core2, and "Generic" The second one is done for:Atom, Pentium Pro, all AMDs, Pentium 4, Nocona, Core 2, and "Generic" Testcase:int x(int a) { return(a &0xf0)> >4 tmp
Definition: README.txt:1347
llvm::EquivalenceClasses::findLeader
member_iterator findLeader(iterator I) const
findLeader - Given a value in the set, return a member iterator for the equivalence class it is in.
Definition: EquivalenceClasses.h:228
llvm::Data
@ Data
Definition: SIMachineScheduler.h:55
llvm::EquivalenceClasses::empty
bool empty() const
Definition: EquivalenceClasses.h:170
llvm::EquivalenceClasses::member_iterator::reference
value_type & reference
Definition: EquivalenceClasses.h:283
L2
add sub stmia L5 ldr L2
Definition: README.txt:201
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::EquivalenceClasses::end
iterator end() const
Definition: EquivalenceClasses.h:168
llvm::EquivalenceClasses::begin
iterator begin() const
Definition: EquivalenceClasses.h:167
llvm::EquivalenceClasses::member_iterator::operator==
bool operator==(const member_iterator &RHS) const
Definition: EquivalenceClasses.h:306
llvm::EquivalenceClasses::findLeader
member_iterator findLeader(const ElemTy &V) const
Definition: EquivalenceClasses.h:232
llvm::EquivalenceClasses::EquivalenceClasses
EquivalenceClasses(const EquivalenceClasses &RHS)
Definition: EquivalenceClasses.h:143
llvm::EquivalenceClasses< llvm::Instruction * >::iterator
typename std::set< ECValue, ECValueComparator >::const_iterator iterator
iterator* - Provides a way to iterate over all values in the set.
Definition: EquivalenceClasses.h:165
llvm::EquivalenceClasses::member_iterator::pointer
value_type * pointer
Definition: EquivalenceClasses.h:282
llvm::EquivalenceClasses::EquivalenceClasses
EquivalenceClasses()=default
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::EquivalenceClasses::unionSets
member_iterator unionSets(member_iterator L1, member_iterator L2)
Definition: EquivalenceClasses.h:242
llvm::EquivalenceClasses::member_iterator::operator!=
bool operator!=(const member_iterator &RHS) const
Definition: EquivalenceClasses.h:309
llvm::EquivalenceClasses::member_iterator::member_iterator
member_iterator()=default
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::EquivalenceClasses::member_iterator::operator*
reference operator*() const
Definition: EquivalenceClasses.h:288
llvm::ScaledNumbers::compare
int compare(DigitsT LDigits, int16_t LScale, DigitsT RDigits, int16_t RScale)
Compare two scaled numbers.
Definition: ScaledNumber.h:251
llvm::EquivalenceClasses::member_iterator::size_type
std::size_t size_type
Definition: EquivalenceClasses.h:280
llvm::EquivalenceClasses::getOrInsertLeaderValue
const ElemTy & getOrInsertLeaderValue(const ElemTy &V)
getOrInsertLeaderValue - Return the leader for the specified value that is in the set.
Definition: EquivalenceClasses.h:200
Compare
QP Compare Ordered outs ins xscmpudp No builtin are required Or llvm fcmp order unorder compare DP QP Compare builtin are required DP Compare
Definition: README_P9.txt:309
llvm::EquivalenceClasses::getNumClasses
unsigned getNumClasses() const
getNumClasses - Return the number of equivalence classes in this set.
Definition: EquivalenceClasses.h:208
llvm::EquivalenceClasses::member_begin
member_iterator member_begin(iterator I) const
Definition: EquivalenceClasses.h:174
Node
Definition: ItaniumDemangle.h:156
llvm::NVPTX::PTXLdStInstCode::V2
@ V2
Definition: NVPTX.h:123
NC
#define NC
Definition: regutils.h:42
llvm::EquivalenceClasses::findValue
iterator findValue(const ElemTy &V) const
findValue - Return an iterator to the specified value.
Definition: EquivalenceClasses.h:184
N
#define N
llvm::EquivalenceClasses::member_iterator::operator++
member_iterator operator++(int)
Definition: EquivalenceClasses.h:300
llvm::EquivalenceClasses::unionSets
member_iterator unionSets(const ElemTy &V1, const ElemTy &V2)
union - Merge the two equivalence sets for the specified values, inserting them if they do not alread...
Definition: EquivalenceClasses.h:238
llvm::EquivalenceClasses::member_iterator::operator->
pointer operator->() const
Definition: EquivalenceClasses.h:292
llvm::EquivalenceClasses::isEquivalent
bool isEquivalent(const ElemTy &V1, const ElemTy &V2) const
Definition: EquivalenceClasses.h:264
llvm::EquivalenceClasses::member_iterator
Definition: EquivalenceClasses.h:272
llvm::EquivalenceClasses::member_iterator::difference_type
std::ptrdiff_t difference_type
Definition: EquivalenceClasses.h:281
llvm::EquivalenceClasses::member_iterator::member_iterator
member_iterator(const ECValue *N)
Definition: EquivalenceClasses.h:286
llvm::EquivalenceClasses::member_iterator::operator++
member_iterator & operator++()
Definition: EquivalenceClasses.h:294