[llvm] [IR] Add block number traits to CFG (PR #102758)

Alexis Engelke via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 10 09:26:09 PDT 2024


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

This enables the use of the more efficient dominator tree node access.

>From 25562e5cf176aef38317565f87df3227615f3372 Mon Sep 17 00:00:00 2001
From: Alexis Engelke <engelke at in.tum.de>
Date: Thu, 8 Aug 2024 12:55:02 +0000
Subject: [PATCH] [IR] Add block number traits to CFG

This enables the use of the more efficient dominator tree node access.
---
 llvm/include/llvm/IR/CFG.h | 44 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/llvm/include/llvm/IR/CFG.h b/llvm/include/llvm/IR/CFG.h
index 12ca1b1b9aa979..675f928118f9eb 100644
--- a/llvm/include/llvm/IR/CFG.h
+++ b/llvm/include/llvm/IR/CFG.h
@@ -304,8 +304,13 @@ template <> struct GraphTraits<BasicBlock*> {
   static NodeRef getEntryNode(BasicBlock *BB) { return BB; }
   static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
   static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
+
+  static unsigned getNumber(BasicBlock *BB) { return BB->getNumber(); }
 };
 
+static_assert(GraphHasNodeNumbers<BasicBlock *>,
+              "GraphTraits getNumber() not detected");
+
 template <> struct GraphTraits<const BasicBlock*> {
   using NodeRef = const BasicBlock *;
   using ChildIteratorType = const_succ_iterator;
@@ -314,8 +319,13 @@ template <> struct GraphTraits<const BasicBlock*> {
 
   static ChildIteratorType child_begin(NodeRef N) { return succ_begin(N); }
   static ChildIteratorType child_end(NodeRef N) { return succ_end(N); }
+
+  static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
 };
 
+static_assert(GraphHasNodeNumbers<const BasicBlock *>,
+              "GraphTraits getNumber() not detected");
+
 // Provide specializations of GraphTraits to be able to treat a function as a
 // graph of basic blocks... and to walk it in inverse order.  Inverse order for
 // a function is considered to be when traversing the predecessor edges of a BB
@@ -328,8 +338,13 @@ template <> struct GraphTraits<Inverse<BasicBlock*>> {
   static NodeRef getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
   static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
   static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
+
+  static unsigned getNumber(BasicBlock *BB) { return BB->getNumber(); }
 };
 
+static_assert(GraphHasNodeNumbers<Inverse<BasicBlock *>>,
+              "GraphTraits getNumber() not detected");
+
 template <> struct GraphTraits<Inverse<const BasicBlock*>> {
   using NodeRef = const BasicBlock *;
   using ChildIteratorType = const_pred_iterator;
@@ -337,8 +352,13 @@ template <> struct GraphTraits<Inverse<const BasicBlock*>> {
   static NodeRef getEntryNode(Inverse<const BasicBlock *> G) { return G.Graph; }
   static ChildIteratorType child_begin(NodeRef N) { return pred_begin(N); }
   static ChildIteratorType child_end(NodeRef N) { return pred_end(N); }
+
+  static unsigned getNumber(const BasicBlock *BB) { return BB->getNumber(); }
 };
 
+static_assert(GraphHasNodeNumbers<Inverse<const BasicBlock *>>,
+              "GraphTraits getNumber() not detected");
+
 //===--------------------------------------------------------------------===//
 // GraphTraits specializations for function basic block graphs (CFGs)
 //===--------------------------------------------------------------------===//
@@ -362,6 +382,11 @@ template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
   }
 
   static size_t size(Function *F) { return F->size(); }
+
+  static unsigned getMaxNumber(Function *F) { return F->getMaxBlockNumber(); }
+  static unsigned getNumberEpoch(Function *F) {
+    return F->getBlockNumberEpoch();
+  }
 };
 template <> struct GraphTraits<const Function*> :
   public GraphTraits<const BasicBlock*> {
@@ -379,6 +404,13 @@ template <> struct GraphTraits<const Function*> :
   }
 
   static size_t size(const Function *F) { return F->size(); }
+
+  static unsigned getMaxNumber(const Function *F) {
+    return F->getMaxBlockNumber();
+  }
+  static unsigned getNumberEpoch(const Function *F) {
+    return F->getBlockNumberEpoch();
+  }
 };
 
 // Provide specializations of GraphTraits to be able to treat a function as a
@@ -391,12 +423,24 @@ template <> struct GraphTraits<Inverse<Function*>> :
   static NodeRef getEntryNode(Inverse<Function *> G) {
     return &G.Graph->getEntryBlock();
   }
+
+  static unsigned getMaxNumber(Function *F) { return F->getMaxBlockNumber(); }
+  static unsigned getNumberEpoch(Function *F) {
+    return F->getBlockNumberEpoch();
+  }
 };
 template <> struct GraphTraits<Inverse<const Function*>> :
   public GraphTraits<Inverse<const BasicBlock*>> {
   static NodeRef getEntryNode(Inverse<const Function *> G) {
     return &G.Graph->getEntryBlock();
   }
+
+  static unsigned getMaxNumber(const Function *F) {
+    return F->getMaxBlockNumber();
+  }
+  static unsigned getNumberEpoch(const Function *F) {
+    return F->getBlockNumberEpoch();
+  }
 };
 
 } // end namespace llvm



More information about the llvm-commits mailing list