eliminate static ctors from Statistics
[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 #define DEBUG_TYPE "instcount"
15 #include "llvm/Analysis/Passes.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Function.h"
18 #include "llvm/Support/InstVisitor.h"
19 #include "llvm/Support/Streams.h"
20 #include "llvm/ADT/Statistic.h"
21 #include <ostream>
22 using namespace llvm;
23
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");
28
29 #define HANDLE_INST(N, OPCODE, CLASS) \
30   STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts");
31
32 #include "llvm/Instruction.def"
33
34
35 namespace {
36   class InstCount : public FunctionPass, public InstVisitor<InstCount> {
37     friend class InstVisitor<InstCount>;
38
39     void visitFunction  (Function &F) { ++TotalFuncs; }
40     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
41
42 #define HANDLE_INST(N, OPCODE, CLASS) \
43     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
44
45 #include "llvm/Instruction.def"
46
47     void visitInstruction(Instruction &I) {
48       cerr << "Instruction Count does not know about " << I;
49       abort();
50     }
51   public:
52     virtual bool runOnFunction(Function &F);
53
54     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55       AU.setPreservesAll();
56     }
57     virtual void print(std::ostream &O, const Module *M) const {}
58
59   };
60
61   RegisterPass<InstCount> X("instcount",
62                             "Counts the various types of Instructions");
63 }
64
65 FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
66
67 // InstCount::run - This is the main Analysis entry point for a
68 // function.
69 //
70 bool InstCount::runOnFunction(Function &F) {
71   unsigned StartMemInsts =
72     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
73     NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
74   visit(F);
75   unsigned EndMemInsts =
76     NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
77     NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
78   TotalMemInst += EndMemInsts-StartMemInsts;
79   return false;
80 }