[llvm] [CycleInfo] Represent cycles by an opaque handle. NFC (PR #210117)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 00:10:19 PDT 2026


================
@@ -91,36 +93,38 @@ template <typename ContextT> class GenericCycle {
 
 public:
   GenericCycle() = default;
+};
 
-  /// Iteration over child cycles: the first child (if any) immediately
-  /// follows this cycle in the preorder array, and each next sibling follows
-  /// the previous child's subtree.
-  //@{
-  struct const_child_iterator
-      : iterator_facade_base<const_child_iterator, std::forward_iterator_tag,
-                             GenericCycle *, std::ptrdiff_t, GenericCycle *,
-                             GenericCycle *> {
-    const GenericCycle *C = nullptr;
+/// Opaque handle to a cycle within a GenericCycleInfo that wraps the cycle's
+/// preorder index. Handles remain valid as long as the cycle forest is not
+/// recomputed; addBlockToCycle() adds a block but never adds, removes, or
+/// reorders cycles, so it leaves every handle valid.
+class CycleRef {
+  static constexpr unsigned InvalidIndex = ~0u;
+  unsigned Index = InvalidIndex;
 
-    const_child_iterator() = default;
-    explicit const_child_iterator(const GenericCycle *C) : C(C) {}
+  explicit CycleRef(unsigned Index) : Index(Index) {}
+  template <typename ContextT> friend class GenericCycleInfo;
+  friend struct DenseMapInfo<CycleRef>;
 
-    GenericCycle *operator*() const { return const_cast<GenericCycle *>(C); }
-    const_child_iterator &operator++() {
-      C += 1 + C->NumDescendants;
-      return *this;
-    }
-    bool operator==(const const_child_iterator &Other) const {
-      return C == Other.C;
-    }
-  };
-  //@}
+public:
+  CycleRef() = default;
+  bool isValid() const { return Index != InvalidIndex; }
+  explicit operator bool() const { return isValid(); }
+  bool operator==(CycleRef O) const { return Index == O.Index; }
+  bool operator!=(CycleRef O) const { return Index != O.Index; }
+};
+
+template <> struct DenseMapInfo<CycleRef> {
+  static unsigned getHashValue(CycleRef C) { return C.Index; }
----------------
MaskRay wrote:

Nice catch! Fixed

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


More information about the llvm-commits mailing list