Skip to content

Commit d6715cd

Browse files
authored
[LLVM] InstCount pass added when enabling stats (#171658)
Shows the different types of IR instructions when stats are enabled.
1 parent 63d165a commit d6715cd

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

llvm/lib/Passes/PassBuilderPipelines.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/Analysis/CtxProfAnalysis.h"
2222
#include "llvm/Analysis/GlobalsModRef.h"
2323
#include "llvm/Analysis/InlineAdvisor.h"
24+
#include "llvm/Analysis/InstCount.h"
2425
#include "llvm/Analysis/ProfileSummaryInfo.h"
2526
#include "llvm/Analysis/ScopedNoAliasAA.h"
2627
#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
@@ -411,6 +412,9 @@ void PassBuilder::invokePipelineEarlySimplificationEPCallbacks(
411412
// Helper to add AnnotationRemarksPass.
412413
static void addAnnotationRemarksPass(ModulePassManager &MPM) {
413414
MPM.addPass(createModuleToFunctionPassAdaptor(AnnotationRemarksPass()));
415+
// Count the types of instructions used
416+
if (AreStatisticsEnabled())
417+
MPM.addPass(createModuleToFunctionPassAdaptor(InstCountPass()));
414418
}
415419

416420
// Helper to check if the current compilation phase is preparing for LTO
@@ -2420,7 +2424,8 @@ PassBuilder::buildO0DefaultPipeline(OptimizationLevel Level,
24202424
if (isLTOPreLink(Phase))
24212425
addRequiredLTOPreLinkPasses(MPM);
24222426

2423-
MPM.addPass(createModuleToFunctionPassAdaptor(AnnotationRemarksPass()));
2427+
// Emit annotation remarks.
2428+
addAnnotationRemarksPass(MPM);
24242429

24252430
return MPM;
24262431
}

llvm/test/Other/instcount.ll

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
; REQUIRES: asserts
2+
; RUN: opt -stats -passes=instcount -disable-output < %s 2>&1 | FileCheck %s
3+
; RUN: opt -stats -passes='thinlto<O3>' -disable-output < %s 2>&1 | FileCheck %s
4+
; RUN: opt -stats -passes='thinlto-pre-link<O2>' -disable-output < %s 2>&1 | FileCheck %s
5+
; RUN: opt -stats -passes='lto<O1>' -disable-output < %s 2>&1 | FileCheck %s
6+
; RUN: opt -stats -passes='lto-pre-link<Os>' -disable-output < %s 2>&1 | FileCheck %s
7+
; RUN: opt -stats -O3 -disable-output < %s 2>&1 | FileCheck %s
8+
; RUN: opt -stats -O0 -disable-output < %s 2>&1 | FileCheck %s
9+
10+
; CHECK-DAG: 8 instcount - Number of Br insts
11+
; CHECK-DAG: 6 instcount - Number of Call insts
12+
; CHECK-DAG: 2 instcount - Number of ICmp insts
13+
; CHECK-DAG: 1 instcount - Number of Ret insts
14+
; CHECK-DAG: 1 instcount - Number of Switch insts
15+
; CHECK-DAG: 10 instcount - Number of basic blocks
16+
; CHECK-DAG: 1 instcount - Number of non-external functions
17+
; CHECK-DAG: 18 instcount - Number of instructions (of all types)
18+
19+
20+
define void @foo(i32 %i, i32 %j, i32 %n) {
21+
entry:
22+
%cmp = icmp slt i32 %i, %j
23+
br i1 %cmp, label %if.then, label %if.end
24+
25+
if.then:
26+
call void @f()
27+
br label %if.end
28+
29+
if.end:
30+
switch i32 %i, label %sw.default [
31+
i32 1, label %sw.bb
32+
i32 2, label %sw.bb1
33+
i32 3, label %sw.bb1
34+
]
35+
36+
sw.bb:
37+
call void @g()
38+
br label %sw.epilog
39+
40+
sw.bb1:
41+
call void @h()
42+
br label %sw.epilog
43+
44+
sw.default:
45+
call void @k()
46+
br label %sw.epilog
47+
48+
sw.epilog:
49+
%cmp2 = icmp sgt i32 %i, %n
50+
br i1 %cmp2, label %if.then3, label %if.else
51+
52+
if.then3:
53+
call void @l()
54+
br label %if.end4
55+
56+
if.else:
57+
call void @m()
58+
br label %if.end4
59+
60+
if.end4:
61+
ret void
62+
}
63+
64+
declare void @f()
65+
declare void @g()
66+
declare void @h()
67+
declare void @k()
68+
declare void @l()
69+
declare void @m()

0 commit comments

Comments
 (0)