[llvm] [BOLT] Speed up dataflow analysis with RPO (PR #183704)

Haibo Jiang via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 4 17:56:18 PST 2026


https://github.com/Jianghibo updated https://github.com/llvm/llvm-project/pull/183704

>From e13e0760f2b42e8c8526f5bef4f607c9cdf0fb9e Mon Sep 17 00:00:00 2001
From: jianghaibo <jianghaibo9 at huawei.com>
Date: Fri, 27 Feb 2026 16:54:27 +0800
Subject: [PATCH] [BOLT] Speed up dataflow analysis with RPO

---
 bolt/include/bolt/Passes/DataflowAnalysis.h | 39 +++++++++++++++------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/bolt/include/bolt/Passes/DataflowAnalysis.h b/bolt/include/bolt/Passes/DataflowAnalysis.h
index f6ca39cf6f860..1a3c3e2def199 100644
--- a/bolt/include/bolt/Passes/DataflowAnalysis.h
+++ b/bolt/include/bolt/Passes/DataflowAnalysis.h
@@ -11,6 +11,7 @@
 
 #include "bolt/Core/BinaryContext.h"
 #include "bolt/Core/BinaryFunction.h"
+#include "llvm/ADT/PostOrderIterator.h"
 #include "llvm/Support/Errc.h"
 #include <optional>
 #include <queue>
@@ -332,22 +333,38 @@ class DataflowAnalysis {
     }
 
     std::queue<BinaryBasicBlock *> Worklist;
-    // TODO: Pushing this in a DFS ordering will greatly speed up the dataflow
-    // performance.
+    DenseSet<BinaryBasicBlock *> BBs;
     if (!Backward) {
+      llvm::ReversePostOrderTraversal<BinaryFunction *> RPOT(&Func);
+      for (BinaryBasicBlock *BB : RPOT) {
+        BBs.insert(BB);
+      }
+    } else {
+      for (BinaryBasicBlock *BB : post_order(&Func)) {
+        BBs.insert(BB);
+      }
+    }
+
+    // Reverse post-order and post-order will leave unreachable basic
+    // blocks. Here will identify and add them to the worklist.
+    if (BBs.size() != Func.size()) {
       for (BinaryBasicBlock &BB : Func) {
-        Worklist.push(&BB);
-        MCInst *Prev = nullptr;
-        for (MCInst &Inst : BB) {
+        if (!BBs.count(&BB)) {
+          BBs.insert(&BB);
+        }
+      }
+    }
+
+    for (auto *BB : BBs) {
+      Worklist.push(BB);
+      MCInst *Prev = nullptr;
+      if (!Backward) {
+        for (MCInst &Inst : *BB) {
           PrevPoint[&Inst] = Prev ? ProgramPoint(Prev) : ProgramPoint(&BB);
           Prev = &Inst;
         }
-      }
-    } else {
-      for (BinaryBasicBlock &BB : llvm::reverse(Func)) {
-        Worklist.push(&BB);
-        MCInst *Prev = nullptr;
-        for (MCInst &Inst : llvm::reverse(BB)) {
+      } else {
+        for (MCInst &Inst : llvm::reverse(*BB)) {
           PrevPoint[&Inst] = Prev ? ProgramPoint(Prev) : ProgramPoint(&BB);
           Prev = &Inst;
         }



More information about the llvm-commits mailing list