1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass collects the count of all instructions and reports them
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/Statistic.h"
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/InstVisitor.h"
17 #include "llvm/Pass.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/raw_ostream.h"
23 #define DEBUG_TYPE "instcount"
25 STATISTIC(TotalInsts
, "Number of instructions (of all types)");
26 STATISTIC(TotalBlocks
, "Number of basic blocks");
27 STATISTIC(TotalFuncs
, "Number of non-external functions");
29 #define HANDLE_INST(N, OPCODE, CLASS) \
30 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
32 #include "llvm/IR/Instruction.def"
35 class InstCount
: public FunctionPass
, public InstVisitor
<InstCount
> {
36 friend class InstVisitor
<InstCount
>;
38 void visitFunction (Function
&F
) { ++TotalFuncs
; }
39 void visitBasicBlock(BasicBlock
&BB
) { ++TotalBlocks
; }
41 #define HANDLE_INST(N, OPCODE, CLASS) \
42 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
44 #include "llvm/IR/Instruction.def"
46 void visitInstruction(Instruction
&I
) {
47 errs() << "Instruction Count does not know about " << I
;
48 llvm_unreachable(nullptr);
51 static char ID
; // Pass identification, replacement for typeid
52 InstCount() : FunctionPass(ID
) {
53 initializeInstCountPass(*PassRegistry::getPassRegistry());
56 bool runOnFunction(Function
&F
) override
;
58 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
61 void print(raw_ostream
&O
, const Module
*M
) const override
{}
66 char InstCount::ID
= 0;
67 INITIALIZE_PASS(InstCount
, "instcount",
68 "Counts the various types of Instructions", false, true)
70 FunctionPass
*llvm::createInstCountPass() { return new InstCount(); }
72 // InstCount::run - This is the main Analysis entry point for a
75 bool InstCount::runOnFunction(Function
&F
) {