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" |
David Greene | 7176e08 | 2009-12-23 20:34:27 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Edwin Török | ced9ff8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 8a6411c | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
| 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"); |
| 29 | |
| 30 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 31 | STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts"); |
| 32 | |
| 33 | #include "llvm/Instruction.def" |
| 34 | |
| 35 | |
| 36 | namespace { |
Nick Lewycky | 492d06e | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 37 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | friend class InstVisitor<InstCount>; |
| 39 | |
| 40 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 41 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 42 | |
| 43 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 44 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
| 45 | |
| 46 | #include "llvm/Instruction.def" |
| 47 | |
Argiris Kirtzidis | fe9d186 | 2010-08-15 10:27:23 +0000 | [diff] [blame] | 48 | void visitInstruction(Instruction &I) { |
David Greene | 8377a15 | 2009-12-23 23:29:28 +0000 | [diff] [blame] | 49 | errs() << "Instruction Count does not know about " << I; |
Edwin Török | bd448e3 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 50 | llvm_unreachable(0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | } |
| 52 | public: |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 53 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 7569322 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 54 | InstCount() : FunctionPass(ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 55 | |
| 56 | virtual bool runOnFunction(Function &F); |
| 57 | |
| 58 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 59 | AU.setPreservesAll(); |
| 60 | } |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 61 | virtual void print(raw_ostream &O, const Module *M) const {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | |
| 63 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 66 | char InstCount::ID = 0; |
Owen Anderson | 6374c3d | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 67 | INITIALIZE_PASS(InstCount, "instcount", |
| 68 | "Counts the various types of Instructions", false, true); |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 69 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | FunctionPass *llvm::createInstCountPass() { return new InstCount(); } |
| 71 | |
| 72 | // InstCount::run - This is the main Analysis entry point for a |
| 73 | // function. |
| 74 | // |
| 75 | bool InstCount::runOnFunction(Function &F) { |
| 76 | unsigned StartMemInsts = |
| 77 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | f9a7a33 | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 78 | NumInvokeInst + NumAllocaInst; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | visit(F); |
| 80 | unsigned EndMemInsts = |
| 81 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | f9a7a33 | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 82 | NumInvokeInst + NumAllocaInst; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 83 | TotalMemInst += EndMemInsts-StartMemInsts; |
| 84 | return false; |
| 85 | } |