LLVM 19.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"
18#include "llvm/Support/Debug.h"
21using namespace llvm;
22
23#define DEBUG_TYPE "instcount"
24
25STATISTIC(TotalInsts, "Number of instructions (of all types)");
26STATISTIC(TotalBlocks, "Number of basic blocks");
27STATISTIC(TotalFuncs, "Number of non-external functions");
28
29#define HANDLE_INST(N, OPCODE, CLASS) \
30 STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
31
32#include "llvm/IR/Instruction.def"
33
34namespace {
35class InstCount : public InstVisitor<InstCount> {
36 friend class InstVisitor<InstCount>;
37
38 void visitFunction(Function &F) { ++TotalFuncs; }
39 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
40
41#define HANDLE_INST(N, OPCODE, CLASS) \
42 void visit##OPCODE(CLASS &) { \
43 ++Num##OPCODE##Inst; \
44 ++TotalInsts; \
45 }
46
47#include "llvm/IR/Instruction.def"
48
50 errs() << "Instruction Count does not know about " << I;
51 llvm_unreachable(nullptr);
52 }
53};
54} // namespace
55
58 LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
59 << "\n");
60 InstCount().visit(F);
61
63}
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
FunctionAnalysisManager FAM
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:348
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
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 set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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:56