Added LLVM project notice to the top of every C++ source file.
[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 "Support/Statistic.h"
18
19 namespace {
20   Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
21   Statistic<> TotalBlocks("instcount", "Number of basic blocks");
22   Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
23
24 #define HANDLE_INST(N, OPCODE, CLASS) \
25     Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
26
27 #include "llvm/Instruction.def"
28
29   class InstCount : public FunctionPass, public InstVisitor<InstCount> {
30     friend class InstVisitor<InstCount>;
31
32     void visitFunction  (Function &F) { ++TotalFuncs; }
33     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
34
35 #define HANDLE_INST(N, OPCODE, CLASS) \
36     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
37
38 #include "llvm/Instruction.def"
39
40     void visitInstruction(Instruction &I) {
41       std::cerr << "Instruction Count does not know about " << I;
42       abort();
43     }
44   public:
45     virtual bool runOnFunction(Function &F);
46
47     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48       AU.setPreservesAll();
49     }
50     virtual void print(std::ostream &O, const Module *M) const {}
51
52   };
53
54   RegisterAnalysis<InstCount> X("instcount",
55                                 "Counts the various types of Instructions");
56 }
57
58 // InstCount::run - This is the main Analysis entry point for a
59 // function.
60 //
61 bool InstCount::runOnFunction(Function &F) {
62   visit(F);
63   return false;
64 }