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