Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- InstCount.cpp - Collects the count of all instructions ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass collects the count of all instructions and reports them |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "instcount" |
| 15 | #include "llvm/Analysis/Passes.h" |
| 16 | #include "llvm/Pass.h" |
| 17 | #include "llvm/Function.h" |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 8a6411c | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
| 24 | STATISTIC(TotalInsts , "Number of instructions (of all types)"); |
| 25 | STATISTIC(TotalBlocks, "Number of basic blocks"); |
| 26 | STATISTIC(TotalFuncs , "Number of non-external functions"); |
| 27 | STATISTIC(TotalMemInst, "Number of memory instructions"); |
| 28 | |
| 29 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 30 | STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts"); |
| 31 | |
| 32 | #include "llvm/Instruction.def" |
| 33 | |
| 34 | |
| 35 | namespace { |
Nick Lewycky | 492d06e | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 36 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 37 | friend class InstVisitor<InstCount>; |
| 38 | |
| 39 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 40 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 41 | |
| 42 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 43 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
| 44 | |
| 45 | #include "llvm/Instruction.def" |
| 46 | |
| 47 | void visitInstruction(Instruction &I) { |
Chris Lattner | 8a6411c | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 48 | errs() << "Instruction Count does not know about " << I; |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 49 | llvm_unreachable(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | } |
| 51 | public: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 53 | InstCount() : FunctionPass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | |
| 55 | virtual bool runOnFunction(Function &F); |
| 56 | |
| 57 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 58 | AU.setPreservesAll(); |
| 59 | } |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 60 | virtual void print(raw_ostream &O, const Module *M) const {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 61 | |
| 62 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 65 | char InstCount::ID = 0; |
| 66 | static RegisterPass<InstCount> |
| 67 | X("instcount", "Counts the various types of Instructions", false, true); |
| 68 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | FunctionPass *llvm::createInstCountPass() { return new InstCount(); } |
| 70 | |
| 71 | // InstCount::run - This is the main Analysis entry point for a |
| 72 | // function. |
| 73 | // |
| 74 | bool InstCount::runOnFunction(Function &F) { |
| 75 | unsigned StartMemInsts = |
| 76 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 77 | NumInvokeInst + NumAllocaInst + NumFreeInst; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 78 | visit(F); |
| 79 | unsigned EndMemInsts = |
| 80 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | 37f513d | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 81 | NumInvokeInst + NumAllocaInst + NumFreeInst; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | TotalMemInst += EndMemInsts-StartMemInsts; |
| 83 | return false; |
| 84 | } |