Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 1 | //===-- InstCount.cpp - Collects the count of all instructions ------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 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. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 10 | // This pass collects the count of all instructions and reports them |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 11 | // |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 4fb1b21 | 2005-10-24 01:00:45 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/Passes.h" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 15 | #include "llvm/Pass.h" |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/InstVisitor.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 18 | #include "llvm/Support/Streams.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Statistic.h" |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 20 | #include <ostream> |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 21 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 23 | namespace { |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 24 | Statistic<> TotalInsts ("instcount", "Number of instructions (of all types)"); |
| 25 | Statistic<> TotalBlocks("instcount", "Number of basic blocks"); |
| 26 | Statistic<> TotalFuncs ("instcount", "Number of non-external functions"); |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 27 | Statistic<> TotalMemInst("instcount", "Number of memory instructions"); |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 28 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 29 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 30 | Statistic<> Num##OPCODE##Inst("instcount", "Number of " #OPCODE " insts"); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 32 | #include "llvm/Instruction.def" |
| 33 | |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 34 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Reid Spencer | e26057a | 2004-11-16 06:58:55 +0000 | [diff] [blame] | 35 | friend class InstVisitor<InstCount>; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 36 | |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 37 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 38 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 39 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 40 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 41 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 43 | #include "llvm/Instruction.def" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 45 | void visitInstruction(Instruction &I) { |
Bill Wendling | 6f81b51 | 2006-11-28 22:46:12 +0000 | [diff] [blame^] | 46 | llvm_cerr << "Instruction Count does not know about " << I; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 47 | abort(); |
| 48 | } |
| 49 | public: |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 50 | virtual bool runOnFunction(Function &F); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 51 | |
| 52 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 53 | AU.setPreservesAll(); |
| 54 | } |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 55 | virtual void print(std::ostream &O, const Module *M) const {} |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 56 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 59 | RegisterPass<InstCount> X("instcount", |
| 60 | "Counts the various types of Instructions"); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Lattner | 4fb1b21 | 2005-10-24 01:00:45 +0000 | [diff] [blame] | 63 | FunctionPass *llvm::createInstCountPass() { return new InstCount(); } |
| 64 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 65 | // InstCount::run - This is the main Analysis entry point for a |
| 66 | // function. |
| 67 | // |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 68 | bool InstCount::runOnFunction(Function &F) { |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 69 | unsigned StartMemInsts = |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 70 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 71 | NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst; |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 72 | visit(F); |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 73 | unsigned EndMemInsts = |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 74 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 75 | NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst; |
| 76 | TotalMemInst += EndMemInsts-StartMemInsts; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 77 | return false; |
| 78 | } |