Changes For Bug 352
[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/Pass.h"
15 #include "llvm/Function.h"
16 #include "llvm/Support/InstVisitor.h"
17 #include "llvm/ADT/Statistic.h"
18
19 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
26 #define HANDLE_INST(N, OPCODE, CLASS) \
27     Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
28
29 #include "llvm/Instruction.def"
30
31   class InstCount : public FunctionPass, public InstVisitor<InstCount> {
32     friend class InstVisitor<InstCount>;
33
34     void visitFunction  (Function &F) { ++TotalFuncs; }
35     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
36
37 #define HANDLE_INST(N, OPCODE, CLASS) \
38     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
39
40 #include "llvm/Instruction.def"
41
42     void visitInstruction(Instruction &I) {
43       std::cerr << "Instruction Count does not know about " << I;
44       abort();
45     }
46   public:
47     virtual bool runOnFunction(Function &F);
48
49     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
50       AU.setPreservesAll();
51     }
52     virtual void print(std::ostream &O, const Module *M) const {}
53
54   };
55
56   RegisterAnalysis<InstCount> X("instcount",
57                                 "Counts the various types of Instructions");
58 }
59
60 // InstCount::run - This is the main Analysis entry point for a
61 // function.
62 //
63 bool InstCount::runOnFunction(Function &F) {
64   visit(F);
65   return false;
66 }
67
68 } // End llvm namespace