Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 1 | //===-- InstCount.cpp - Collects the count of all instructions ------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 9 | // |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 10 | // This pass collects the count of all instructions and reports them |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 11 | // |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "instcount" |
Chris Lattner | ca014ad | 2005-10-24 01:00:45 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
Chandler Carruth | dbd6958 | 2012-11-30 03:08:41 +0000 | [diff] [blame] | 18 | #include "llvm/InstVisitor.h" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
David Greene | a7b92ee | 2009-12-23 20:34:27 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | b25de3f | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | f006840 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 57ef942 | 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 | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 30 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 31 | STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts"); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 32 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Instruction.def" |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 37 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Reid Spencer | f5e3cfe | 2004-11-16 06:58:55 +0000 | [diff] [blame] | 38 | friend class InstVisitor<InstCount>; |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 40 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 41 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 42 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 43 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 44 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 45 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 46 | #include "llvm/IR/Instruction.def" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 47 | |
Argyrios Kyrtzidis | d0fcc9a | 2010-08-15 10:27:23 +0000 | [diff] [blame] | 48 | void visitInstruction(Instruction &I) { |
David Greene | 1495b5f | 2009-12-23 23:29:28 +0000 | [diff] [blame] | 49 | errs() << "Instruction Count does not know about " << I; |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 50 | llvm_unreachable(0); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 51 | } |
| 52 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 53 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 54 | InstCount() : FunctionPass(ID) { |
| 55 | initializeInstCountPass(*PassRegistry::getPassRegistry()); |
| 56 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 57 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame^] | 58 | bool runOnFunction(Function &F) override; |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 59 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame^] | 60 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 61 | AU.setPreservesAll(); |
| 62 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame^] | 63 | void print(raw_ostream &O, const Module *M) const override {} |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 64 | |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 65 | }; |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 68 | char InstCount::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 69 | INITIALIZE_PASS(InstCount, "instcount", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 70 | "Counts the various types of Instructions", false, true) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 71 | |
Chris Lattner | ca014ad | 2005-10-24 01:00:45 +0000 | [diff] [blame] | 72 | FunctionPass *llvm::createInstCountPass() { return new InstCount(); } |
| 73 | |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 74 | // InstCount::run - This is the main Analysis entry point for a |
| 75 | // function. |
| 76 | // |
Chris Lattner | 5f9be67 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 77 | bool InstCount::runOnFunction(Function &F) { |
Chris Lattner | f006840 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 78 | unsigned StartMemInsts = |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 79 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | de5ad42 | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 80 | NumInvokeInst + NumAllocaInst; |
Chris Lattner | 5f9be67 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 81 | visit(F); |
Chris Lattner | f006840 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 82 | unsigned EndMemInsts = |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 83 | NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst + |
Victor Hernandez | de5ad42 | 2009-10-26 23:43:48 +0000 | [diff] [blame] | 84 | NumInvokeInst + NumAllocaInst; |
Chris Lattner | f006840 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 85 | TotalMemInst += EndMemInsts-StartMemInsts; |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 86 | return false; |
| 87 | } |