[llvm] [DomTree] Fix root attachment in runDFS() (PR #73148)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 22 09:33:31 PST 2023


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/73148

Currently, runDFS() only sets the Parent of the DFS root if it is already in the NodeToInfo map. This works out okay if we're running DFS on the DT root, which doesn't have a parent anyway. However, when running on PDT roots, this means we end up keeping the parent at 0, rather than setting it to 1 for the virtual PDT root. Because the virtual root (nullptr) has the same value as the dummy value in NumToNode (nullptr) this happens to work out by accident right now.

I believe we should always be setting the parent in runDFS(), and adjust AttachToNum in doFullDFSWalk() to be 1 (the virtual root) for PDTs.

>From 4629882890d6ac2f0c81231022708c261904e1ad Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Wed, 22 Nov 2023 17:54:45 +0100
Subject: [PATCH] [DomTree] Fix root attachment in runDFS()

Currently, runDFS() only sets the Parent of the DFS root if it is
already in the NodeToInfo map. This works out okay if we're running
DFS on the DT root, which doesn't have a parent anyway. However,
when running on PDT roots, this means we end up keeping the parent
at 0, rather than setting it to 1 for the virtual PDT root. Because
the virtual root (nullptr) has the same value as the dummy value
in NumToNode (nullptr) this happens to work out by accident right
now.

I believe we should always be setting the parent in runDFS(),
and adjust AttachToNum in doFullDFSWalk() to be 1 (the virtual
root) for PDTs.
---
 llvm/include/llvm/Support/GenericDomTreeConstruction.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Support/GenericDomTreeConstruction.h b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
index 6564f98ab0234b0..0a70648c9eab36e 100644
--- a/llvm/include/llvm/Support/GenericDomTreeConstruction.h
+++ b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
@@ -181,7 +181,7 @@ struct SemiNCAInfo {
                   const NodeOrderMap *SuccOrder = nullptr) {
     assert(V);
     SmallVector<NodePtr, 64> WorkList = {V};
-    if (NodeToInfo.count(V) != 0) NodeToInfo[V].Parent = AttachToNum;
+    NodeToInfo[V].Parent = AttachToNum;
 
     while (!WorkList.empty()) {
       const NodePtr BB = WorkList.pop_back_val();
@@ -306,6 +306,7 @@ struct SemiNCAInfo {
     for (unsigned i = 2; i < NextDFSNum; ++i) {
       const NodePtr W = NumToNode[i];
       auto &WInfo = NodeToInfo[W];
+      assert(WInfo.Semi != 0);
       const unsigned SDomNum = NodeToInfo[NumToNode[WInfo.Semi]].DFSNum;
       NodePtr WIDomCandidate = WInfo.IDom;
       while (NodeToInfo[WIDomCandidate].DFSNum > SDomNum)
@@ -552,7 +553,7 @@ struct SemiNCAInfo {
 
     addVirtualRoot();
     unsigned Num = 1;
-    for (const NodePtr Root : DT.Roots) Num = runDFS(Root, Num, DC, 0);
+    for (const NodePtr Root : DT.Roots) Num = runDFS(Root, Num, DC, 1);
   }
 
   static void CalculateFromScratch(DomTreeT &DT, BatchUpdatePtr BUI) {



More information about the llvm-commits mailing list