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 | // |
Chris Lattner | 4ee451d | 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. |
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" |
David Greene | 270862d | 2009-12-23 20:34:27 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ErrorHandling.h" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 20 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
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 { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 37 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Reid Spencer | e26057a | 2004-11-16 06:58:55 +0000 | [diff] [blame] | 38 | friend class InstVisitor<InstCount>; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 40 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 41 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 42 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 43 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 44 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 46 | #include "llvm/Instruction.def" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 48 | void visitInstruction(Instruction &I) { |
David Greene | b64ca13 | 2009-12-23 23:29:28 +0000 | [diff] [blame] | 49 | errs() << "Instruction Count does not know about " << I; |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 50 | llvm_unreachable(0); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 51 | } |
| 52 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 53 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 9ccaf53 | 2010-08-05 23:42:04 +0000 | [diff] [blame] | 54 | InstCount() : FunctionPass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 56 | virtual bool runOnFunction(Function &F); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 57 | |
| 58 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 59 | AU.setPreservesAll(); |
| 60 | } |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 61 | virtual void print(raw_ostream &O, const Module *M) const {} |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 62 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 63 | }; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 66 | char InstCount::ID = 0; |
Owen Anderson | d13db2c | 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 | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 4fb1b21 | 2005-10-24 01:00:45 +0000 | [diff] [blame] | 70 | FunctionPass *llvm::createInstCountPass() { return new InstCount(); } |
| 71 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 72 | // InstCount::run - This is the main Analysis entry point for a |
| 73 | // function. |
| 74 | // |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 75 | bool InstCount::runOnFunction(Function &F) { |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 76 | unsigned StartMemInsts = |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 77 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | 046e78c | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 78 | NumInvokeInst + NumAllocaInst; |
Chris Lattner | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 79 | visit(F); |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 80 | unsigned EndMemInsts = |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 81 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | 046e78c | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 82 | NumInvokeInst + NumAllocaInst; |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 83 | TotalMemInst += EndMemInsts-StartMemInsts; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 84 | return false; |
| 85 | } |