xref: /freebsd-src/contrib/llvm-project/llvm/lib/Analysis/InstCount.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric //===-- InstCount.cpp - Collects the count of all instructions ------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass collects the count of all instructions and reports them
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13*e8d8bef9SDimitry Andric #include "llvm/Analysis/InstCount.h"
140b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h"
150b57cec5SDimitry Andric #include "llvm/Analysis/Passes.h"
160b57cec5SDimitry Andric #include "llvm/IR/Function.h"
170b57cec5SDimitry Andric #include "llvm/IR/InstVisitor.h"
180b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
190b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
200b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #define DEBUG_TYPE "instcount"
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric STATISTIC(TotalInsts, "Number of instructions (of all types)");
260b57cec5SDimitry Andric STATISTIC(TotalBlocks, "Number of basic blocks");
270b57cec5SDimitry Andric STATISTIC(TotalFuncs, "Number of non-external functions");
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric #define HANDLE_INST(N, OPCODE, CLASS)                                          \
300b57cec5SDimitry Andric   STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #include "llvm/IR/Instruction.def"
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric namespace {
35*e8d8bef9SDimitry Andric class InstCount : public InstVisitor<InstCount> {
360b57cec5SDimitry Andric   friend class InstVisitor<InstCount>;
370b57cec5SDimitry Andric 
visitFunction(Function & F)380b57cec5SDimitry Andric   void visitFunction(Function &F) { ++TotalFuncs; }
visitBasicBlock(BasicBlock & BB)390b57cec5SDimitry Andric   void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric #define HANDLE_INST(N, OPCODE, CLASS)                                          \
42*e8d8bef9SDimitry Andric   void visit##OPCODE(CLASS &) {                                                \
43*e8d8bef9SDimitry Andric     ++Num##OPCODE##Inst;                                                       \
44*e8d8bef9SDimitry Andric     ++TotalInsts;                                                              \
45*e8d8bef9SDimitry Andric   }
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric #include "llvm/IR/Instruction.def"
480b57cec5SDimitry Andric 
visitInstruction(Instruction & I)490b57cec5SDimitry Andric   void visitInstruction(Instruction &I) {
500b57cec5SDimitry Andric     errs() << "Instruction Count does not know about " << I;
510b57cec5SDimitry Andric     llvm_unreachable(nullptr);
520b57cec5SDimitry Andric   }
53*e8d8bef9SDimitry Andric };
54*e8d8bef9SDimitry Andric } // namespace
55*e8d8bef9SDimitry Andric 
run(Function & F,FunctionAnalysisManager & FAM)56*e8d8bef9SDimitry Andric PreservedAnalyses InstCountPass::run(Function &F,
57*e8d8bef9SDimitry Andric                                      FunctionAnalysisManager &FAM) {
58*e8d8bef9SDimitry Andric   LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
59*e8d8bef9SDimitry Andric                     << "\n");
60*e8d8bef9SDimitry Andric   InstCount().visit(F);
61*e8d8bef9SDimitry Andric 
62*e8d8bef9SDimitry Andric   return PreservedAnalyses::all();
630b57cec5SDimitry Andric }
64