[llvm] [ADCE][NFC] Use FindFunctionBackedges (PR #189405)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 30 08:28:31 PDT 2026


https://github.com/aengelke created https://github.com/llvm/llvm-project/pull/189405

Slightly less efficient than it could be, because the backedges are
materialized, but more efficient than using a DenseMap.


>From c2b5da40d6bb701967bc598549b7223e74c89990 Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Mon, 30 Mar 2026 15:27:49 +0000
Subject: [PATCH] [spr] initial version

Created using spr 1.3.8-wip
---
 llvm/lib/Transforms/Scalar/ADCE.cpp | 45 ++++-------------------------
 1 file changed, 6 insertions(+), 39 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/ADCE.cpp b/llvm/lib/Transforms/Scalar/ADCE.cpp
index 5eef1bb209c03..639b15b103a44 100644
--- a/llvm/lib/Transforms/Scalar/ADCE.cpp
+++ b/llvm/lib/Transforms/Scalar/ADCE.cpp
@@ -23,6 +23,7 @@
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
+#include "llvm/Analysis/CFG.h"
 #include "llvm/Analysis/DomTreeUpdater.h"
 #include "llvm/Analysis/GlobalsModRef.h"
 #include "llvm/Analysis/IteratedDominanceFrontier.h"
@@ -250,45 +251,11 @@ void AggressiveDeadCodeElimination::initialize() {
     return;
 
   if (!RemoveLoops) {
-    // This stores state for the depth-first iterator. In addition
-    // to recording which nodes have been visited we also record whether
-    // a node is currently on the "stack" of active ancestors of the current
-    // node.
-    using StatusMap = DenseMap<BasicBlock *, bool>;
-
-    class DFState : public StatusMap {
-    public:
-      std::pair<StatusMap::iterator, bool> insert(BasicBlock *BB) {
-        return StatusMap::insert(std::make_pair(BB, true));
-      }
-
-      // Invoked after we have visited all children of a node.
-      void completed(BasicBlock *BB) { (*this)[BB] = false; }
-
-      // Return true if \p BB is currently on the active stack
-      // of ancestors.
-      bool onStack(BasicBlock *BB) {
-        auto Iter = find(BB);
-        return Iter != end() && Iter->second;
-      }
-    } State;
-
-    State.reserve(F.size());
-    // Iterate over blocks in depth-first pre-order and
-    // treat all edges to a block already seen as loop back edges
-    // and mark the branch live it if there is a back edge.
-    for (auto *BB: depth_first_ext(&F.getEntryBlock(), State)) {
-      Instruction *Term = BB->getTerminator();
-      if (isLive(Term))
-        continue;
-
-      for (auto *Succ : successors(BB))
-        if (State.onStack(Succ)) {
-          // back edge....
-          markLive(Term);
-          break;
-        }
-    }
+    // Mark all terminators that have backedges as live.
+    SmallVector<std::pair<const BasicBlock *, const BasicBlock *>> Backedges;
+    FindFunctionBackedges(F, Backedges);
+    for (const auto &[Src, Dst] : Backedges)
+      markLive(const_cast<Instruction *>(Src->getTerminator()));
   }
 
   // Mark blocks live if there is no path from the block to a



More information about the llvm-commits mailing list