Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 1 | //===-- InstCount.cpp - Collects the count of all instructions ------------===// |
| 2 | // |
| 3 | // This pass collects the count of all instructions and reports them |
| 4 | // |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===// |
| 6 | |
| 7 | #include "llvm/Pass.h" |
| 8 | #include "llvm/Module.h" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 9 | #include "llvm/Support/InstVisitor.h" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 10 | #include "Support/Statistic.h" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 11 | |
| 12 | namespace { |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 17 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 18 | Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts"); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 20 | #include "llvm/Instruction.def" |
| 21 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 22 | class InstCount : public Pass, public InstVisitor<InstCount> { |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 23 | friend class InstVisitor<InstCount>; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 24 | |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 25 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 26 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 27 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 28 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 29 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 31 | #include "llvm/Instruction.def" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 33 | void visitInstruction(Instruction &I) { |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 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 | } |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 43 | virtual void print(std::ostream &O, const Module *M) const {} |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 44 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 45 | }; |
| 46 | |
Chris Lattner | db85e38 | 2002-11-17 22:15:40 +0000 | [diff] [blame] | 47 | RegisterAnalysis<InstCount> X("instcount", |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 48 | "Counts the various types of Instructions"); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 51 | // InstCount::run - This is the main Analysis entry point for a |
| 52 | // function. |
| 53 | // |
| 54 | bool InstCount::run(Module &M) { |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 55 | visit(M); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 56 | return false; |
| 57 | } |