[llvm] [LLVM] Successor count added to InstCount (PR #171670)

IƱaki V Arrechea via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 12:49:17 PST 2025


https://github.com/InakiVA updated https://github.com/llvm/llvm-project/pull/171670

>From 8618bf0d0cad36b5b875a0dd4f33a09eeb71c853 Mon Sep 17 00:00:00 2001
From: Inaki Arrechea <inakiarrechea at google.com>
Date: Wed, 10 Dec 2025 17:12:00 +0000
Subject: [PATCH 1/6] Inst Count implemented for metrics pass

---
 llvm/lib/Passes/PassBuilderPipelines.cpp |  7 ++-
 llvm/test/Other/instcount.ll             | 74 ++++++++++++++++++++++++
 2 files changed, 80 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Other/instcount.ll

diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index c6beb3fdf09bd..865f24802af4b 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -21,6 +21,7 @@
 #include "llvm/Analysis/CtxProfAnalysis.h"
 #include "llvm/Analysis/GlobalsModRef.h"
 #include "llvm/Analysis/InlineAdvisor.h"
+#include "llvm/Analysis/InstCount.h"
 #include "llvm/Analysis/ProfileSummaryInfo.h"
 #include "llvm/Analysis/ScopedNoAliasAA.h"
 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
@@ -1701,6 +1702,11 @@ PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
   // Emit annotation remarks.
   addAnnotationRemarksPass(MPM);
 
+
+  // Count the types of instructions used
+  if (AreStatisticsEnabled())
+    MPM.addPass(createModuleToFunctionPassAdaptor(InstCountPass()));
+
   if (isLTOPreLink(Phase))
     addRequiredLTOPreLinkPasses(MPM);
   return MPM;
@@ -1813,7 +1819,6 @@ PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) {
   addAnnotationRemarksPass(MPM);
 
   addRequiredLTOPreLinkPasses(MPM);
-
   return MPM;
 }
 
diff --git a/llvm/test/Other/instcount.ll b/llvm/test/Other/instcount.ll
new file mode 100644
index 0000000000000..b043f26b28837
--- /dev/null
+++ b/llvm/test/Other/instcount.ll
@@ -0,0 +1,74 @@
+; RUN: opt -stats -passes=instcount < %s 2>&1 | FileCheck %s --check-prefix=UNOPT
+; RUN: opt -stats -O3 < %s 2>&1 | FileCheck %s --check-prefix=OPT
+
+; --- FIRST RUN (UNOPTIMIZED) ---
+; UNOPT-DAG: 8 instcount - Number of Br insts
+; UNOPT-DAG: 6 instcount - Number of Call insts
+; UNOPT-DAG: 2 instcount - Number of ICmp insts
+; UNOPT-DAG: 1 instcount - Number of Ret insts
+; UNOPT-DAG: 1 instcount - Number of Switch insts
+; UNOPT-DAG: 10 instcount - Number of basic blocks
+; UNOPT-DAG: 1 instcount - Number of non-external functions
+; UNOPT-DAG: 18 instcount - Number of instructions (of all types)
+
+; --- SECOND RUN (OPTIMIZED) ---
+; OPT-DAG: 8 instcount - Number of Br insts
+; OPT-DAG: 6 instcount - Number of Call insts
+; OPT-DAG: 2 instcount - Number of ICmp insts
+; OPT-DAG: 1 instcount - Number of Ret insts
+; OPT-DAG: 1 instcount - Number of Switch insts
+; OPT-DAG: 10 instcount - Number of basic blocks
+; OPT-DAG: 1 instcount - Number of non-external functions
+; OPT-DAG: 18 instcount - Number of instructions (of all types)
+
+
+define dso_local void @foo(i32 noundef %i, i32 noundef %j, i32 noundef %n) {
+entry:
+  %cmp = icmp slt i32 %i, %j
+  br i1 %cmp, label %if.then, label %if.end
+
+if.then:
+  call void @f()
+  br label %if.end
+
+if.end:
+  switch i32 %i, label %sw.default [
+    i32 1, label %sw.bb
+    i32 2, label %sw.bb1
+    i32 3, label %sw.bb1
+  ]
+
+sw.bb:
+  call void @g()
+  br label %sw.epilog
+
+sw.bb1:
+  call void @h()
+  br label %sw.epilog
+
+sw.default:
+  call void @k()
+  br label %sw.epilog
+
+sw.epilog:
+  %cmp2 = icmp sgt i32 %i, %n
+  br i1 %cmp2, label %if.then3, label %if.else
+
+if.then3:
+  call void @l()
+  br label %if.end4
+
+if.else:
+  call void @m()
+  br label %if.end4
+
+if.end4:
+  ret void
+}
+
+declare void @f()
+declare void @g()
+declare void @h()
+declare void @k()
+declare void @l()
+declare void @m()

>From c43bd804d6622e49d6febcfeb917f93b65915a36 Mon Sep 17 00:00:00 2001
From: Inaki Arrechea <inakiarrechea at google.com>
Date: Wed, 10 Dec 2025 17:36:16 +0000
Subject: [PATCH 2/6] Whitespace fix

---
 llvm/lib/Passes/PassBuilderPipelines.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 865f24802af4b..cf9c3075ce9f2 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -1702,13 +1702,13 @@ PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
   // Emit annotation remarks.
   addAnnotationRemarksPass(MPM);
 
-
   // Count the types of instructions used
   if (AreStatisticsEnabled())
     MPM.addPass(createModuleToFunctionPassAdaptor(InstCountPass()));
 
   if (isLTOPreLink(Phase))
     addRequiredLTOPreLinkPasses(MPM);
+  
   return MPM;
 }
 

>From 777eb40adefd877a825b670c4f5b62c71adffc20 Mon Sep 17 00:00:00 2001
From: Inaki Arrechea <inakiarrechea at google.com>
Date: Wed, 10 Dec 2025 17:37:49 +0000
Subject: [PATCH 3/6] Whitespace fix

---
 llvm/lib/Passes/PassBuilderPipelines.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index cf9c3075ce9f2..0632858813dd3 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -1708,7 +1708,7 @@ PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
 
   if (isLTOPreLink(Phase))
     addRequiredLTOPreLinkPasses(MPM);
-  
+
   return MPM;
 }
 
@@ -1819,6 +1819,7 @@ PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) {
   addAnnotationRemarksPass(MPM);
 
   addRequiredLTOPreLinkPasses(MPM);
+  
   return MPM;
 }
 

>From 77deef83a99020d4963f98b3c042b3d793756e52 Mon Sep 17 00:00:00 2001
From: Inaki Arrechea <inakiarrechea at google.com>
Date: Wed, 10 Dec 2025 17:52:17 +0000
Subject: [PATCH 4/6] Format

---
 llvm/lib/Passes/PassBuilderPipelines.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 8e5d6b368c5b5..edafde9f68279 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -1855,7 +1855,7 @@ PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level) {
   addAnnotationRemarksPass(MPM);
 
   addRequiredLTOPreLinkPasses(MPM);
-  
+
   return MPM;
 }
 

>From 2e285d952ae3bdb5b59c3ca66ebaf344daf2d75f Mon Sep 17 00:00:00 2001
From: Inaki Arrechea <inakiarrechea at google.com>
Date: Wed, 10 Dec 2025 18:21:09 +0000
Subject: [PATCH 5/6] Successor count added to InstCount

---
 llvm/lib/Analysis/InstCount.cpp | 7 ++++++-
 llvm/test/Other/instcount.ll    | 2 ++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/InstCount.cpp b/llvm/lib/Analysis/InstCount.cpp
index b43c9cd074b9d..c6aa02f764a7d 100644
--- a/llvm/lib/Analysis/InstCount.cpp
+++ b/llvm/lib/Analysis/InstCount.cpp
@@ -24,6 +24,7 @@ using namespace llvm;
 STATISTIC(TotalInsts, "Number of instructions (of all types)");
 STATISTIC(TotalBlocks, "Number of basic blocks");
 STATISTIC(TotalFuncs, "Number of non-external functions");
+STATISTIC(TotalBlockSucs, "Number of basic block successors");
 
 #define HANDLE_INST(N, OPCODE, CLASS)                                          \
   STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
@@ -35,7 +36,11 @@ class InstCount : public InstVisitor<InstCount> {
   friend class InstVisitor<InstCount>;
 
   void visitFunction(Function &F) { ++TotalFuncs; }
-  void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
+  void visitBasicBlock(BasicBlock &BB) { 
+    Instruction *I = BB.getTerminator();
+    TotalBlockSucs+=I->getNumSuccessors();
+    ++TotalBlocks; 
+  }
 
 #define HANDLE_INST(N, OPCODE, CLASS)                                          \
   void visit##OPCODE(CLASS &) {                                                \
diff --git a/llvm/test/Other/instcount.ll b/llvm/test/Other/instcount.ll
index b043f26b28837..7c19668857538 100644
--- a/llvm/test/Other/instcount.ll
+++ b/llvm/test/Other/instcount.ll
@@ -10,6 +10,7 @@
 ; UNOPT-DAG: 10 instcount - Number of basic blocks
 ; UNOPT-DAG: 1 instcount - Number of non-external functions
 ; UNOPT-DAG: 18 instcount - Number of instructions (of all types)
+; UNOPT-DAG: 14 instcount - Number of basic block successors
 
 ; --- SECOND RUN (OPTIMIZED) ---
 ; OPT-DAG: 8 instcount - Number of Br insts
@@ -20,6 +21,7 @@
 ; OPT-DAG: 10 instcount - Number of basic blocks
 ; OPT-DAG: 1 instcount - Number of non-external functions
 ; OPT-DAG: 18 instcount - Number of instructions (of all types)
+; OPT-DAG: 14 instcount - Number of basic block successors
 
 
 define dso_local void @foo(i32 noundef %i, i32 noundef %j, i32 noundef %n) {

>From 86f3506d6964814b6d60cf679913036c4e2fe419 Mon Sep 17 00:00:00 2001
From: Inaki Arrechea <inakiarrechea at google.com>
Date: Wed, 10 Dec 2025 20:20:39 +0000
Subject: [PATCH 6/6] Format

---
 llvm/lib/Analysis/InstCount.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/llvm/lib/Analysis/InstCount.cpp b/llvm/lib/Analysis/InstCount.cpp
index c6aa02f764a7d..f7831c49c34f2 100644
--- a/llvm/lib/Analysis/InstCount.cpp
+++ b/llvm/lib/Analysis/InstCount.cpp
@@ -36,10 +36,10 @@ class InstCount : public InstVisitor<InstCount> {
   friend class InstVisitor<InstCount>;
 
   void visitFunction(Function &F) { ++TotalFuncs; }
-  void visitBasicBlock(BasicBlock &BB) { 
+  void visitBasicBlock(BasicBlock &BB) {
     Instruction *I = BB.getTerminator();
-    TotalBlockSucs+=I->getNumSuccessors();
-    ++TotalBlocks; 
+    TotalBlockSucs += I->getNumSuccessors();
+    ++TotalBlocks;
   }
 
 #define HANDLE_INST(N, OPCODE, CLASS)                                          \



More information about the llvm-commits mailing list