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
13#include "llvm/Analysis/InstCount.h"
14#include "llvm/ADT/Statistic.h"
15#include "llvm/IR/Function.h"
16#include "llvm/IR/InstVisitor.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/ErrorHandling.h"
19#include "llvm/Support/raw_ostream.h"
20using namespace llvm;
21
22#define DEBUG_TYPE "instcount"
23
24STATISTIC(TotalInsts, "Number of instructions (of all types)");
25STATISTIC(TotalBlocks, "Number of basic blocks");
26STATISTIC(TotalFuncs, "Number of non-external functions");
27STATISTIC(LargestFunctionSize,
28 "Largest number of instructions in a single function");
29STATISTIC(LargestFunctionBBCount,
30 "Largest number of basic blocks in a single function");
31
32#define HANDLE_INST(N, OPCODE, CLASS) \
33 STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
34
35#include "llvm/IR/Instruction.def"
36
37namespace {
38class InstCount : public InstVisitor<InstCount> {
39 friend class InstVisitor<InstCount>;
40
41 void visitFunction(Function &F) {
42 ++TotalFuncs;
43 LargestFunctionSize.updateMax(V: F.getInstructionCount());
44 LargestFunctionBBCount.updateMax(V: F.size());
45 }
46 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
47
48#define HANDLE_INST(N, OPCODE, CLASS) \
49 void visit##OPCODE(CLASS &) { \
50 ++Num##OPCODE##Inst; \
51 ++TotalInsts; \
52 }
53
54#include "llvm/IR/Instruction.def"
55
56 void visitInstruction(Instruction &I) {
57 errs() << "Instruction Count does not know about " << I;
58 llvm_unreachable(nullptr);
59 }
60};
61} // namespace
62
63PreservedAnalyses InstCountPass::run(Function &F,
64 FunctionAnalysisManager &FAM) {
65 LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
66 << "\n");
67 InstCount().visit(F);
68
69 return PreservedAnalyses::all();
70}
71