1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass collects the count of all instructions and reports them
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "instcount"
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Function.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/InstVisitor.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/ADT/Statistic.h"
24 STATISTIC(TotalInsts , "Number of instructions (of all types)");
25 STATISTIC(TotalBlocks, "Number of basic blocks");
26 STATISTIC(TotalFuncs , "Number of non-external functions");
27 STATISTIC(TotalMemInst, "Number of memory instructions");
29 #define HANDLE_INST(N, OPCODE, CLASS) \
30 STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
32 #include "llvm/Instruction.def"
36 class InstCount : public FunctionPass, public InstVisitor<InstCount> {
37 friend class InstVisitor<InstCount>;
39 void visitFunction (Function &F) { ++TotalFuncs; }
40 void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
42 #define HANDLE_INST(N, OPCODE, CLASS) \
43 void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
45 #include "llvm/Instruction.def"
47 void visitInstruction(Instruction &I) {
48 errs() << "Instruction Count does not know about " << I;
52 static char ID; // Pass identification, replacement for typeid
53 InstCount() : FunctionPass(&ID) {}
55 virtual bool runOnFunction(Function &F);
57 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 virtual void print(raw_ostream &O, const Module *M) const {}
65 char InstCount::ID = 0;
66 static RegisterPass<InstCount>
67 X("instcount", "Counts the various types of Instructions", false, true);
69 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
71 // InstCount::run - This is the main Analysis entry point for a
74 bool InstCount::runOnFunction(Function &F) {
75 unsigned StartMemInsts =
76 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
77 NumInvokeInst + NumAllocaInst;
79 unsigned EndMemInsts =
80 NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
81 NumInvokeInst + NumAllocaInst;
82 TotalMemInst += EndMemInsts-StartMemInsts;