1 //===-- InstCount.cpp - Collects the count of all instructions ------------===//
2 //
3 // This pass collects the count of all instructions and reports them
4 //
5 //===----------------------------------------------------------------------===//
6 
7 #include "llvm/Pass.h"
8 #include "llvm/Module.h"
9 #include "llvm/Support/InstVisitor.h"
10 #include "Support/Statistic.h"
11 
12 namespace {
13   Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)");
14   Statistic<> TotalBlocks("instcount", "Number of basic blocks");
15   Statistic<> TotalFuncs ("instcount", "Number of non-external functions");
16 
17 #define HANDLE_INST(N, OPCODE, CLASS) \
18     Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts");
19 
20 #include "llvm/Instruction.def"
21 
22   class InstCount : public Pass, public InstVisitor<InstCount> {
23     friend class InstVisitor<InstCount>;
24 
25     void visitFunction  (Function &F) { ++TotalFuncs; }
26     void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
27 
28 #define HANDLE_INST(N, OPCODE, CLASS) \
29     void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
30 
31 #include "llvm/Instruction.def"
32 
33     void visitInstruction(Instruction &I) {
34       std::cerr << "Instruction Count does not know about " << I;
35       abort();
36     }
37   public:
38     virtual bool run(Module &M);
39 
40     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
41       AU.setPreservesAll();
42     }
43     virtual void print(std::ostream &O, const Module *M) const {}
44 
45   };
46 
47   RegisterAnalysis<InstCount> X("instcount",
48                                 "Counts the various types of Instructions");
49 }
50 
51 // InstCount::run - This is the main Analysis entry point for a
52 // function.
53 //
54 bool InstCount::run(Module &M) {
55   visit(M);
56   return false;
57 }
58