xref: /netbsd-src/external/apache2/llvm/dist/llvm/lib/Analysis/InstCount.cpp (revision 82d56013d7b633d116a93943de88e08335357a7c)
17330f729Sjoerg //===-- InstCount.cpp - Collects the count of all instructions ------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This pass collects the count of all instructions and reports them
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
127330f729Sjoerg 
13*82d56013Sjoerg #include "llvm/Analysis/InstCount.h"
147330f729Sjoerg #include "llvm/ADT/Statistic.h"
157330f729Sjoerg #include "llvm/Analysis/Passes.h"
167330f729Sjoerg #include "llvm/IR/Function.h"
177330f729Sjoerg #include "llvm/IR/InstVisitor.h"
18*82d56013Sjoerg #include "llvm/InitializePasses.h"
197330f729Sjoerg #include "llvm/Pass.h"
207330f729Sjoerg #include "llvm/Support/Debug.h"
217330f729Sjoerg #include "llvm/Support/ErrorHandling.h"
227330f729Sjoerg #include "llvm/Support/raw_ostream.h"
237330f729Sjoerg using namespace llvm;
247330f729Sjoerg 
257330f729Sjoerg #define DEBUG_TYPE "instcount"
267330f729Sjoerg 
277330f729Sjoerg STATISTIC(TotalInsts, "Number of instructions (of all types)");
287330f729Sjoerg STATISTIC(TotalBlocks, "Number of basic blocks");
297330f729Sjoerg STATISTIC(TotalFuncs, "Number of non-external functions");
307330f729Sjoerg 
317330f729Sjoerg #define HANDLE_INST(N, OPCODE, CLASS)                                          \
327330f729Sjoerg   STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
337330f729Sjoerg 
347330f729Sjoerg #include "llvm/IR/Instruction.def"
357330f729Sjoerg 
367330f729Sjoerg namespace {
37*82d56013Sjoerg class InstCount : public InstVisitor<InstCount> {
387330f729Sjoerg   friend class InstVisitor<InstCount>;
397330f729Sjoerg 
visitFunction(Function & F)407330f729Sjoerg   void visitFunction(Function &F) { ++TotalFuncs; }
visitBasicBlock(BasicBlock & BB)417330f729Sjoerg   void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
427330f729Sjoerg 
437330f729Sjoerg #define HANDLE_INST(N, OPCODE, CLASS)                                          \
44*82d56013Sjoerg   void visit##OPCODE(CLASS &) {                                                \
45*82d56013Sjoerg     ++Num##OPCODE##Inst;                                                       \
46*82d56013Sjoerg     ++TotalInsts;                                                              \
47*82d56013Sjoerg   }
487330f729Sjoerg 
497330f729Sjoerg #include "llvm/IR/Instruction.def"
507330f729Sjoerg 
visitInstruction(Instruction & I)517330f729Sjoerg   void visitInstruction(Instruction &I) {
527330f729Sjoerg     errs() << "Instruction Count does not know about " << I;
537330f729Sjoerg     llvm_unreachable(nullptr);
547330f729Sjoerg   }
55*82d56013Sjoerg };
56*82d56013Sjoerg } // namespace
57*82d56013Sjoerg 
run(Function & F,FunctionAnalysisManager & FAM)58*82d56013Sjoerg PreservedAnalyses InstCountPass::run(Function &F,
59*82d56013Sjoerg                                      FunctionAnalysisManager &FAM) {
60*82d56013Sjoerg   LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
61*82d56013Sjoerg                     << "\n");
62*82d56013Sjoerg   InstCount().visit(F);
63*82d56013Sjoerg 
64*82d56013Sjoerg   return PreservedAnalyses::all();
657330f729Sjoerg }
667330f729Sjoerg 
67*82d56013Sjoerg namespace {
68*82d56013Sjoerg class InstCountLegacyPass : public FunctionPass {
69*82d56013Sjoerg public:
70*82d56013Sjoerg   static char ID; // Pass identification, replacement for typeid
InstCountLegacyPass()71*82d56013Sjoerg   InstCountLegacyPass() : FunctionPass(ID) {
72*82d56013Sjoerg     initializeInstCountLegacyPassPass(*PassRegistry::getPassRegistry());
73*82d56013Sjoerg   }
74*82d56013Sjoerg 
runOnFunction(Function & F)75*82d56013Sjoerg   bool runOnFunction(Function &F) override {
76*82d56013Sjoerg     LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
77*82d56013Sjoerg                       << "\n");
78*82d56013Sjoerg     InstCount().visit(F);
79*82d56013Sjoerg     return false;
80*82d56013Sjoerg   };
817330f729Sjoerg 
getAnalysisUsage(AnalysisUsage & AU) const827330f729Sjoerg   void getAnalysisUsage(AnalysisUsage &AU) const override {
837330f729Sjoerg     AU.setPreservesAll();
847330f729Sjoerg   }
85*82d56013Sjoerg 
print(raw_ostream & O,const Module * M) const867330f729Sjoerg   void print(raw_ostream &O, const Module *M) const override {}
877330f729Sjoerg };
88*82d56013Sjoerg } // namespace
897330f729Sjoerg 
90*82d56013Sjoerg char InstCountLegacyPass::ID = 0;
91*82d56013Sjoerg INITIALIZE_PASS(InstCountLegacyPass, "instcount",
927330f729Sjoerg                 "Counts the various types of Instructions", false, true)
937330f729Sjoerg 
createInstCountPass()94*82d56013Sjoerg FunctionPass *llvm::createInstCountPass() { return new InstCountLegacyPass(); }
95