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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 27 02:06:25 PST 2023


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

>From ba58ec0f7ce15178aa2edff53f522eedb59f56b7 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 1/2] [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 6c847ce54ef259a..1e0e042c06a8a01 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();
@@ -307,6 +307,7 @@ struct SemiNCAInfo {
     // during path compression in Eval.
     for (unsigned i = 2; i < NextDFSNum; ++i) {
       auto &WInfo = *NumToInfo[i];
+      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) {

>From a5909c31590bd13eb46c81c8dd3dfae7911f489c Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 27 Nov 2023 11:03:51 +0100
Subject: [PATCH 2/2] Use NumToInfo now that we can

---
 llvm/include/llvm/Support/GenericDomTreeConstruction.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/GenericDomTreeConstruction.h b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
index 1e0e042c06a8a01..a85cd6ca5054ccc 100644
--- a/llvm/include/llvm/Support/GenericDomTreeConstruction.h
+++ b/llvm/include/llvm/Support/GenericDomTreeConstruction.h
@@ -308,7 +308,7 @@ struct SemiNCAInfo {
     for (unsigned i = 2; i < NextDFSNum; ++i) {
       auto &WInfo = *NumToInfo[i];
       assert(WInfo.Semi != 0);
-      const unsigned SDomNum = NodeToInfo[NumToNode[WInfo.Semi]].DFSNum;
+      const unsigned SDomNum = NumToInfo[WInfo.Semi]->DFSNum;
       NodePtr WIDomCandidate = WInfo.IDom;
       while (NodeToInfo[WIDomCandidate].DFSNum > SDomNum)
         WIDomCandidate = NodeToInfo[WIDomCandidate].IDom;



More information about the llvm-commits mailing list