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

via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 12:46:55 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: IƱaki V Arrechea (InakiVA)

<details>
<summary>Changes</summary>

Counts the number of Basic Block successors when stats are enabled

---
Full diff: https://github.com/llvm/llvm-project/pull/171670.diff


3 Files Affected:

- (modified) llvm/lib/Analysis/InstCount.cpp (+6-1) 
- (modified) llvm/lib/Passes/PassBuilderPipelines.cpp (+6) 
- (added) llvm/test/Other/instcount.ll (+76) 


``````````diff
diff --git a/llvm/lib/Analysis/InstCount.cpp b/llvm/lib/Analysis/InstCount.cpp
index b43c9cd074b9d..f7831c49c34f2 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/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index 4de527d9ef85e..edafde9f68279 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"
@@ -1737,8 +1738,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;
 }
 
diff --git a/llvm/test/Other/instcount.ll b/llvm/test/Other/instcount.ll
new file mode 100644
index 0000000000000..7c19668857538
--- /dev/null
+++ b/llvm/test/Other/instcount.ll
@@ -0,0 +1,76 @@
+; 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)
+; UNOPT-DAG: 14 instcount - Number of basic block successors
+
+; --- 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)
+; OPT-DAG: 14 instcount - Number of basic block successors
+
+
+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()

``````````

</details>


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


More information about the llvm-commits mailing list