[llvm] [LoopInfo] Reuse DomTree's own DFS numbering in analyze() (PR #207650)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 6 00:49:19 PDT 2026


================
@@ -576,16 +576,23 @@ void PopulateLoopsDFS<BlockT, LoopT>::insertIntoLoop(BlockT *Block) {
 /// insertions per block.
 template <class BlockT, class LoopT>
 void LoopInfoBase<BlockT, LoopT>::analyze(const DomTreeBase<BlockT> &DomTree) {
-  // Postorder traversal of the dominator tree.
   const DomTreeNodeBase<BlockT> *DomRoot = DomTree.getRootNode();
   if constexpr (GraphHasNodeNumbers<const BlockT *>) {
     ParentPtr = DomRoot->getBlock()->getParent();
     BlockNumberEpoch = GraphTraits<ParentT>::getNumberEpoch(ParentPtr);
     unsigned Max = GraphTraits<ParentT>::getMaxNumber(ParentPtr);
     BBMap.resize(Max);
   }
-  for (auto DomNode : post_order(DomRoot)) {
 
+  // Visit dominator tree nodes in reverse preorder: like postorder, this
+  // guarantees a sub-loop is discovered before the outer loop.
+  DomTree.updateDFSNumbers();
+  SmallVector<const DomTreeNodeBase<BlockT> *, 32> PreorderNodes(
+      DomRoot->getDFSNumOut());
+  for (const DomTreeNodeBase<BlockT> *Node : DomTree.nodes())
+    PreorderNodes[Node->getDFSNumIn()] = Node;
----------------
aengelke wrote:

NB: this prevents skipping DFS number invalidation for eraseNode or intentionally keeping holes to avoid full renumbering on changes; this is the first use of these numbers that requires them to be consecutive. (I experimented with that before, eraseNode is not called often enough for this to make a substantial difference, often, the DT is recomputed afterwards anyway.) I don't particularly like this, but then, I don't particularly like LoopInfo and if it becomes a problem, it is easy to change later on.

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


More information about the llvm-commits mailing list