Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 1 | //===-- InstCount.cpp - Collects the count of all instructions ------------===// |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 9 | // |
| 10 | // This pass collects the count of all instructions and reports them |
| 11 | // |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Pass.h" |
Chris Lattner | 5f9be67 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 15 | #include "llvm/Function.h" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 16 | #include "llvm/Support/InstVisitor.h" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 17 | #include "Support/Statistic.h" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 18 | |
| 19 | namespace { |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 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 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 24 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 25 | Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts"); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 26 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 27 | #include "llvm/Instruction.def" |
| 28 | |
Chris Lattner | 5f9be67 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 29 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 30 | friend class InstVisitor<InstCount>; |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 32 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 33 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 34 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 35 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 36 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 38 | #include "llvm/Instruction.def" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 40 | void visitInstruction(Instruction &I) { |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 41 | std::cerr << "Instruction Count does not know about " << I; |
| 42 | abort(); |
| 43 | } |
| 44 | public: |
Chris Lattner | 5f9be67 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 45 | virtual bool runOnFunction(Function &F); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 46 | |
| 47 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 48 | AU.setPreservesAll(); |
| 49 | } |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 50 | virtual void print(std::ostream &O, const Module *M) const {} |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 51 | |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
Chris Lattner | 342f681 | 2002-11-17 22:15:40 +0000 | [diff] [blame] | 54 | RegisterAnalysis<InstCount> X("instcount", |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 55 | "Counts the various types of Instructions"); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 58 | // InstCount::run - This is the main Analysis entry point for a |
| 59 | // function. |
| 60 | // |
Chris Lattner | 5f9be67 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 61 | bool InstCount::runOnFunction(Function &F) { |
| 62 | visit(F); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 63 | return false; |
| 64 | } |