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