expose a ctor
[oota-llvm.git] / lib / Analysis / InstCount.cpp
1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass collects the count of all instructions and reports them
11 //
12 //===----------------------------------------------------------------------===//
13
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"
19 using namespace llvm;
20
21 namespace {
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");
26
27 #define HANDLE_INST(N, OPCODE, CLASS) \
28     Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
29
30 #include "llvm/Instruction.def"
31
32   class InstCount : public FunctionPass, public InstVisitor<InstCount> {
33     friend class InstVisitor<InstCount>;
34
35     void visitFunction  (Function &F) { ++TotalFuncs; }
36     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
37
38 #define HANDLE_INST(N, OPCODE, CLASS) \
39     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
40
41 #include "llvm/Instruction.def"
42
43     void visitInstruction(Instruction &I) {
44       std::cerr << "Instruction Count does not know about " << I;
45       abort();
46     }
47   public:
48     virtual bool runOnFunction(Function &F);
49
50     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51       AU.setPreservesAll();
52     }
53     virtual void print(std::ostream &O, const Module *M) const {}
54
55   };
56
57   RegisterAnalysis<InstCount> X("instcount",
58                                 "Counts the various types of Instructions");
59 }
60
61 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
62
63 // InstCount::run - This is the main Analysis entry point for a
64 // function.
65 //
66 bool InstCount::runOnFunction(Function &F) {
67   unsigned StartMemInsts =
68     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
69     NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
70   visit(F);
71   unsigned EndMemInsts =
72     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
73     NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
74   TotalMemInst += EndMemInsts-StartMemInsts;
75   return false;
76 }