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 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Function.h" |
Chandler Carruth | 7da14f1 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 17 | #include "llvm/IR/InstVisitor.h" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
David Greene | a7b92ee | 2009-12-23 20:34:27 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Torok Edwin | ccb29cd | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | b25de3f | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | f006840 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chandler Carruth | f1221bd | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "instcount" |
| 25 | |
Chris Lattner | 57ef942 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 26 | STATISTIC(TotalInsts , "Number of instructions (of all types)"); |
| 27 | STATISTIC(TotalBlocks, "Number of basic blocks"); |
| 28 | STATISTIC(TotalFuncs , "Number of non-external functions"); |
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 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 36 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Reid Spencer | f5e3cfe | 2004-11-16 06:58:55 +0000 | [diff] [blame] | 37 | friend class InstVisitor<InstCount>; |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 39 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 40 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 41 | |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 42 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | 3935d2b | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 43 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 44 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Instruction.def" |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 46 | |
Argyrios Kyrtzidis | d0fcc9a | 2010-08-15 10:27:23 +0000 | [diff] [blame] | 47 | void visitInstruction(Instruction &I) { |
David Greene | 1495b5f | 2009-12-23 23:29:28 +0000 | [diff] [blame] | 48 | errs() << "Instruction Count does not know about " << I; |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 49 | llvm_unreachable(nullptr); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 50 | } |
| 51 | public: |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 52 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 53 | InstCount() : FunctionPass(ID) { |
| 54 | initializeInstCountPass(*PassRegistry::getPassRegistry()); |
| 55 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 56 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 57 | bool runOnFunction(Function &F) override; |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 58 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 59 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 60 | AU.setPreservesAll(); |
| 61 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 62 | void print(raw_ostream &O, const Module *M) const override {} |
Chris Lattner | 5e05817 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 63 | |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 64 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 65 | } |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 66 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 67 | char InstCount::ID = 0; |
Owen Anderson | a57b97e | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 68 | INITIALIZE_PASS(InstCount, "instcount", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 69 | "Counts the various types of Instructions", false, true) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 70 | |
Chris Lattner | ca014ad | 2005-10-24 01:00:45 +0000 | [diff] [blame] | 71 | FunctionPass *llvm::createInstCountPass() { return new InstCount(); } |
| 72 | |
Dinakar Dhurjati | a7be9a7 | 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 | 5f9be67 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 76 | bool InstCount::runOnFunction(Function &F) { |
| 77 | visit(F); |
Dinakar Dhurjati | a7be9a7 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |