From 51572c2cd7200552280599fbc68d016bb1f18934 Mon Sep 17 00:00:00 2001 From: Dhruv Chawla <44582521+dc03@users.noreply.github.com> Date: Wed, 24 May 2023 12:55:22 +0530 Subject: [PATCH] [NFC][DAGCombiner]: Only consider nodes with no uses for pruning when forming initial worklist When the worklist is initially being formed, there is no need to consider all nodes for pruning. This is because the first time calling getNextWorklistEntry will only clear those nodes which have no uses, with their operands being added to the worklist. However, when the worklist is created for the first time all nodes are added anyways, so this operation actually ends up adding no nodes. This patch adds a parameter IsCandidateForPruning to AddToWorklist with a default value of true to avoid having to update every call site. Differential Revision: https://reviews.llvm.org/D151416 --- llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp index 7173f52..5fdc83c 100644 --- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -170,7 +170,8 @@ namespace { /// them) when they are deleted from the underlying DAG. It relies on /// stable indices of nodes within the worklist. DenseMap WorklistMap; - /// This records all nodes attempted to add to the worklist since we + + /// This records all nodes attempted to be added to the worklist since we /// considered a new worklist entry. As we keep do not add duplicate nodes /// in the worklist, this is different from the tail of the worklist. SmallSetVector PruningList; @@ -263,7 +264,7 @@ namespace { /// Add to the worklist making sure its instance is at the back (next to be /// processed.) - void AddToWorklist(SDNode *N) { + void AddToWorklist(SDNode *N, bool IsCandidateForPruning = true) { assert(N->getOpcode() != ISD::DELETED_NODE && "Deleted Node added to Worklist"); @@ -272,7 +273,8 @@ namespace { if (N->getOpcode() == ISD::HANDLENODE) return; - ConsiderForPruning(N); + if (IsCandidateForPruning) + ConsiderForPruning(N); if (WorklistMap.insert(std::make_pair(N, Worklist.size())).second) Worklist.push_back(N); @@ -1770,8 +1772,13 @@ void DAGCombiner::Run(CombineLevel AtLevel) { WorklistInserter AddNodes(*this); // Add all the dag nodes to the worklist. + // + // Note: All nodes are not added to PruningList here, this is because the only + // nodes which can be deleted are those which have no uses and all other nodes + // which would otherwise be added to the worklist by the first call to + // getNextWorklistEntry are already present in it. for (SDNode &Node : DAG.allnodes()) - AddToWorklist(&Node); + AddToWorklist(&Node, /* IsCandidateForPruning */ Node.use_empty()); // Create a dummy node (which is not added to allnodes), that adds a reference // to the root node, preventing it from being deleted, and tracking any -- 2.7.4