[llvm] [IDF] Use BitVectors indexed by DFS number for visited sets (NFC). (PR #227832)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 1 05:48:37 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Florian Hahn (fhahn)

<details>
<summary>Changes</summary>

IDFCalculatorBase::calculate tracks visited dominator tree nodes in two SmallPtrSets. The DFS numbers computed at the start of calculate are unique and dense in [0, number of nodes), with the DFS out-number of the root being the number of nodes, so use them to index BitVectors instead.

This reduces hashing overhead and improves compile-time, depending on configuration/workload:

stage1-O3: -0.04%
stage1-ReleaseThinLTO: -0.05%
stage1-ReleaseLTO-g: -0.22%
stage1-aarch64-O3: -0.06%
stage2-O3: -0.04%
stage2-clang: -0.04%

https://llvm-compile-time-tracker.com/compare.php?from=b96b66160ace30c2b5eef1f4afdb14c95ecc26cd&to=5ffb13d6d1f87195bba8af13366a152a7de2bfe0&stat=instructions:u

Aided by Opus 5.5

---
Full diff: https://github.com/llvm/llvm-project/pull/227832.diff


1 Files Affected:

- (modified) llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h (+11-9) 


``````````diff
diff --git a/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h b/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h
index c52b2cab377bf..32baf09b8b19d 100644
--- a/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h
+++ b/llvm/include/llvm/Support/GenericIteratedDominanceFrontier.h
@@ -144,18 +144,20 @@ void IDFCalculatorBase<NodeTy, IsPostDom>::calculate(
 
   DT.updateDFSNumbers();
 
+  // The DFS in-numbers are unique and dense in [0, number of nodes), with the
+  // root's DFS out-number being the number of nodes. Use them to index the
+  // visited sets.
+  const DomTreeNodeBase<NodeTy> *RootNode = DT.getRootNode();
+  unsigned NumNodes = RootNode ? RootNode->getDFSNumOut() : 0;
+
   SmallVector<DomTreeNodeBase<NodeTy> *, 32> Worklist;
-  SmallPtrSet<DomTreeNodeBase<NodeTy> *, 16> VisitedPQ;
-  SmallPtrSet<DomTreeNodeBase<NodeTy> *, 16> VisitedWorklist;
-  if (useLiveIn) {
-    VisitedPQ.reserve(LiveInBlocks->size());
-    VisitedWorklist.reserve(LiveInBlocks->size());
-  }
+  SmallVector<bool, 32> VisitedPQ(NumNodes, false);
+  SmallVector<bool, 32> VisitedWorklist(NumNodes, false);
 
   for (NodeTy *BB : *DefBlocks)
     if (DomTreeNodeBase<NodeTy> *Node = DT.getNode(BB)) {
       PQ.push({Node, std::make_pair(Node->getLevel(), Node->getDFSNumIn())});
-      VisitedWorklist.insert(Node);
+      VisitedWorklist[Node->getDFSNumIn()] = true;
     }
 
   while (!PQ.empty()) {
@@ -184,7 +186,7 @@ void IDFCalculatorBase<NodeTy, IsPostDom>::calculate(
         if (SuccLevel > RootLevel)
           return;
 
-        if (!VisitedPQ.insert(SuccNode).second)
+        if (std::exchange(VisitedPQ[SuccNode->getDFSNumIn()], true))
           return;
 
         NodeTy *SuccBB = SuccNode->getBlock();
@@ -201,7 +203,7 @@ void IDFCalculatorBase<NodeTy, IsPostDom>::calculate(
         DoWork(Succ);
 
       for (auto DomChild : *Node) {
-        if (VisitedWorklist.insert(DomChild).second)
+        if (!std::exchange(VisitedWorklist[DomChild->getDFSNumIn()], true))
           Worklist.push_back(DomChild);
       }
     }

``````````

</details>


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


More information about the llvm-commits mailing list