[llvm] [CycleInfo] Store blocks using Euler tour representation (PR #208614)

Jay Foad via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 10 03:09:40 PDT 2026


================
@@ -333,20 +344,66 @@ void GenericCycleInfo<ContextT>::addBlockToCycle(BlockT *Block, CycleT *Cycle) {
   if (Number >= BlockMap.size())
     BlockMap.resize(GraphTraits<FunctionT *>::getMaxNumber(Block->getParent()));
 
-  // FixMe: Appending NewBlock is fine as a set of blocks in a cycle. When
-  // printing, cycle NewBlock is at the end of list but it should be in the
-  // middle to represent actual traversal of a cycle.
-  Cycle->appendBlock(Block);
+  // Insert Block at Pos (end of Cycle's slice) and shift every later cycle's
+  // range right, keyed on IdxBegin so a nested cycle sharing end == Pos is
+  // left alone; Cycle and its ancestors then grow to contain it, below.
+  // FixMe: appended at the slice end, not the traversal-order middle.
+  unsigned Pos = Cycle->IdxEnd;
+  BlockLayout.insert(BlockLayout.begin() + Pos, Block);
+  for (CycleT *TLC : toplevel_cycles())
+    for (CycleT *C : depth_first(TLC))
+      if (C->IdxBegin >= Pos) {
+        ++C->IdxBegin;
+        ++C->IdxEnd;
+      }
   addToBlockMap(Block, Cycle);
+  // Cycle and its ancestors gain the new block: extend each one's slice and
+  // invalidate its exit-block cache in a single walk up the tree.
+  for (CycleT *C = Cycle; C; C = C->getParentCycle()) {
----------------
jayfoad wrote:

Just a drive- by thought: maybe this could be incorporated into the loop above, detecting cycles that include the insert position based on their IdxBegin/IdxEnd instead of by walking the parent tree?

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


More information about the llvm-commits mailing list