LLVM 18.0.0git
InstCount.cpp
Go to the documentation of this file.
1//===-- InstCount.cpp - Collects the count of all instructions ------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass collects the count of all instructions and reports them
10//
11//===----------------------------------------------------------------------===//
12
14#include "llvm/ADT/Statistic.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/InstVisitor.h"
19#include "llvm/Pass.h"
20#include "llvm/Support/Debug.h"
23using namespace llvm;
24
25#define DEBUG_TYPE "instcount"
26
27STATISTIC(TotalInsts, "Number of instructions (of all types)");
28STATISTIC(TotalBlocks, "Number of basic blocks");
29STATISTIC(TotalFuncs, "Number of non-external functions");
30
31#define HANDLE_INST(N, OPCODE, CLASS) \
32 STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
33
34#include "llvm/IR/Instruction.def"
35
36namespace {
37class InstCount : public InstVisitor<InstCount> {
38 friend class InstVisitor<InstCount>;
39
40 void visitFunction(Function &F) { ++TotalFuncs; }
41 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
42
43#define HANDLE_INST(N, OPCODE, CLASS) \
44 void visit##OPCODE(CLASS &) { \
45 ++Num##OPCODE##Inst; \
46 ++TotalInsts; \
47 }
48
49#include "llvm/IR/Instruction.def"
50
52 errs() << "Instruction Count does not know about " << I;
53 llvm_unreachable(nullptr);
54 }
55};
56} // namespace
57
60 LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
61 << "\n");
62 InstCount().visit(F);
63
65}
66
67namespace {
68class InstCountLegacyPass : public FunctionPass {
69public:
70 static char ID; // Pass identification, replacement for typeid
71 InstCountLegacyPass() : FunctionPass(ID) {
73 }
74
75 bool runOnFunction(Function &F) override {
76 LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
77 << "\n");
78 InstCount().visit(F);
79 return false;
80 };
81
82 void getAnalysisUsage(AnalysisUsage &AU) const override {
83 AU.setPreservesAll();
84 }
85
86 void print(raw_ostream &O, const Module *M) const override {}
87};
88} // namespace
89
90char InstCountLegacyPass::ID = 0;
91INITIALIZE_PASS(InstCountLegacyPass, "instcount",
92 "Counts the various types of Instructions", false, true)
93
94FunctionPass *llvm::createInstCountPass() { return new InstCountLegacyPass(); }
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static bool runOnFunction(Function &F, bool PostInlining)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionAnalysisManager FAM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:167
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
Base class for instruction visitors.
Definition: InstVisitor.h:78
void visitFunction(Function &F)
Definition: InstVisitor.h:142
void visitBasicBlock(BasicBlock &BB)
Definition: InstVisitor.h:143
void visitInstruction(Instruction &I)
Definition: InstVisitor.h:280
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeInstCountLegacyPassPass(PassRegistry &)
FunctionPass * createInstCountPass()
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Definition: InstCount.cpp:58