1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass collects the count of all instructions and reports them
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/Passes.h"
15 #include "llvm/Pass.h"
16 #include "llvm/Function.h"
17 #include "llvm/Support/InstVisitor.h"
18 #include "llvm/ADT/Statistic.h"
22 Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
23 Statistic<> TotalBlocks("instcount", "Number of basic blocks");
24 Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
25 Statistic<> TotalMemInst("instcount", "Number of memory instructions");
27 #define HANDLE_INST(N, OPCODE, CLASS) \
28 Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
30 #include "llvm/Instruction.def"
32 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
33 friend class InstVisitor<InstCount>;
35 void visitFunction (Function &F) { ++TotalFuncs; }
36 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
38 #define HANDLE_INST(N, OPCODE, CLASS) \
39 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
41 #include "llvm/Instruction.def"
43 void visitInstruction(Instruction &I) {
44 std::cerr << "Instruction Count does not know about " << I;
48 virtual bool runOnFunction(Function &F);
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
53 virtual void print(std::ostream &O, const Module *M) const {}
57 RegisterAnalysis<InstCount> X("instcount",
58 "Counts the various types of Instructions");
61 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
63 // InstCount::run - This is the main Analysis entry point for a
66 bool InstCount::runOnFunction(Function &F) {
67 unsigned StartMemInsts =
68 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
69 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
71 unsigned EndMemInsts =
72 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
73 NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
74 TotalMemInst += EndMemInsts-StartMemInsts;