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