[llvm] [EquivalenceClasses] Introduce erase member function (PR #134660)

Jakub Kuderski via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 9 08:36:56 PDT 2025


================
@@ -220,6 +221,56 @@ template <class ElemTy> class EquivalenceClasses {
     return *ECV;
   }
 
+  /// erase - Erase a value from the union/find set, return "true" if erase
+  /// succeeded.
+  bool erase(const ElemTy &V) {
+    if (!TheMapping.contains(V))
+      return false;
+    const ECValue *Cur = TheMapping[V];
+    const ECValue *Next = Cur->getNext();
+    if (Cur->isLeader()) {
+      // If the current element is the leader and has a successor element,
+      // update the successor element's 'Leader' field to be the last element,
+      // set the successor element's stolen bit, and set the 'Leader' field of
+      // all other elements in same class to be the successor element.
+      if (Next) {
+        Next->Leader = Cur->Leader;
+        Next->Next = (const ECValue *)((intptr_t)Next->Next | (intptr_t)1);
+        const ECValue *newLeader = Next;
+        while ((Next = Next->getNext())) {
+          Next->Leader = newLeader;
+        }
+      }
+    } else {
+      const ECValue *Leader = findLeader(V).Node;
+      const ECValue *Pre = Leader;
+      while (Pre->getNext() != Cur) {
+        Pre = Pre->getNext();
+      }
+      if (!Next) {
+        // If the current element is the last element(not leader), set the
+        // successor of the current element's predecessor to null, and set
+        // the 'Leader' field of the class leader to the predecessor element.
+        Pre->Next = nullptr;
+        Leader->Leader = Pre;
+      } else {
+        // If the current element is in the middle of class, then simply
+        // connect the predecessor element and the successor element.
+        Pre->Next =
+            (const ECValue *)((intptr_t)Next | (intptr_t)Pre->isLeader());
----------------
kuhar wrote:

Prefer C++ casts: https://llvm.org/docs/CodingStandards.html#prefer-c-style-casts

https://github.com/llvm/llvm-project/pull/134660


More information about the llvm-commits mailing list