xref: /minix3/external/bsd/llvm/dist/llvm/lib/Analysis/InstCount.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- InstCount.cpp - Collects the count of all instructions ------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This pass collects the count of all instructions and reports them
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "llvm/Analysis/Passes.h"
15f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
16f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
17*0a6a1f1dSLionel Sambuc #include "llvm/IR/InstVisitor.h"
18f4a2713aSLionel Sambuc #include "llvm/Pass.h"
19f4a2713aSLionel Sambuc #include "llvm/Support/Debug.h"
20f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
22f4a2713aSLionel Sambuc using namespace llvm;
23f4a2713aSLionel Sambuc 
24*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "instcount"
25*0a6a1f1dSLionel Sambuc 
26f4a2713aSLionel Sambuc STATISTIC(TotalInsts , "Number of instructions (of all types)");
27f4a2713aSLionel Sambuc STATISTIC(TotalBlocks, "Number of basic blocks");
28f4a2713aSLionel Sambuc STATISTIC(TotalFuncs , "Number of non-external functions");
29f4a2713aSLionel Sambuc STATISTIC(TotalMemInst, "Number of memory instructions");
30f4a2713aSLionel Sambuc 
31f4a2713aSLionel Sambuc #define HANDLE_INST(N, OPCODE, CLASS) \
32f4a2713aSLionel Sambuc   STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc #include "llvm/IR/Instruction.def"
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc 
37f4a2713aSLionel Sambuc namespace {
38f4a2713aSLionel Sambuc   class InstCount : public FunctionPass, public InstVisitor<InstCount> {
39f4a2713aSLionel Sambuc     friend class InstVisitor<InstCount>;
40f4a2713aSLionel Sambuc 
visitFunction(Function & F)41f4a2713aSLionel Sambuc     void visitFunction  (Function &F) { ++TotalFuncs; }
visitBasicBlock(BasicBlock & BB)42f4a2713aSLionel Sambuc     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc #define HANDLE_INST(N, OPCODE, CLASS) \
45f4a2713aSLionel Sambuc     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
46f4a2713aSLionel Sambuc 
47f4a2713aSLionel Sambuc #include "llvm/IR/Instruction.def"
48f4a2713aSLionel Sambuc 
visitInstruction(Instruction & I)49f4a2713aSLionel Sambuc     void visitInstruction(Instruction &I) {
50f4a2713aSLionel Sambuc       errs() << "Instruction Count does not know about " << I;
51*0a6a1f1dSLionel Sambuc       llvm_unreachable(nullptr);
52f4a2713aSLionel Sambuc     }
53f4a2713aSLionel Sambuc   public:
54f4a2713aSLionel Sambuc     static char ID; // Pass identification, replacement for typeid
InstCount()55f4a2713aSLionel Sambuc     InstCount() : FunctionPass(ID) {
56f4a2713aSLionel Sambuc       initializeInstCountPass(*PassRegistry::getPassRegistry());
57f4a2713aSLionel Sambuc     }
58f4a2713aSLionel Sambuc 
59*0a6a1f1dSLionel Sambuc     bool runOnFunction(Function &F) override;
60f4a2713aSLionel Sambuc 
getAnalysisUsage(AnalysisUsage & AU) const61*0a6a1f1dSLionel Sambuc     void getAnalysisUsage(AnalysisUsage &AU) const override {
62f4a2713aSLionel Sambuc       AU.setPreservesAll();
63f4a2713aSLionel Sambuc     }
print(raw_ostream & O,const Module * M) const64*0a6a1f1dSLionel Sambuc     void print(raw_ostream &O, const Module *M) const override {}
65f4a2713aSLionel Sambuc 
66f4a2713aSLionel Sambuc   };
67f4a2713aSLionel Sambuc }
68f4a2713aSLionel Sambuc 
69f4a2713aSLionel Sambuc char InstCount::ID = 0;
70f4a2713aSLionel Sambuc INITIALIZE_PASS(InstCount, "instcount",
71f4a2713aSLionel Sambuc                 "Counts the various types of Instructions", false, true)
72f4a2713aSLionel Sambuc 
createInstCountPass()73f4a2713aSLionel Sambuc FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc // InstCount::run - This is the main Analysis entry point for a
76f4a2713aSLionel Sambuc // function.
77f4a2713aSLionel Sambuc //
runOnFunction(Function & F)78f4a2713aSLionel Sambuc bool InstCount::runOnFunction(Function &F) {
79f4a2713aSLionel Sambuc   unsigned StartMemInsts =
80f4a2713aSLionel Sambuc     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
81f4a2713aSLionel Sambuc     NumInvokeInst + NumAllocaInst;
82f4a2713aSLionel Sambuc   visit(F);
83f4a2713aSLionel Sambuc   unsigned EndMemInsts =
84f4a2713aSLionel Sambuc     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
85f4a2713aSLionel Sambuc     NumInvokeInst + NumAllocaInst;
86f4a2713aSLionel Sambuc   TotalMemInst += EndMemInsts-StartMemInsts;
87f4a2713aSLionel Sambuc   return false;
88f4a2713aSLionel Sambuc }
89