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 | |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Function.h" |
Chandler Carruth | 67f6bf7 | 2014-03-06 03:23:41 +0000 | [diff] [blame] | 17 | #include "llvm/IR/InstVisitor.h" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
David Greene | 270862d | 2009-12-23 20:34:27 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Torok Edwin | 7d696d8 | 2009-07-11 13:10:19 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 6ae7e98 | 2005-03-22 03:55:10 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chandler Carruth | 4da2537 | 2014-04-22 02:48:03 +0000 | [diff] [blame] | 24 | #define DEBUG_TYPE "instcount" |
| 25 | |
Chris Lattner | 3b27d68 | 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 | 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 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 33 | #include "llvm/IR/Instruction.def" |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 3b27d68 | 2006-12-19 22:30:33 +0000 | [diff] [blame] | 35 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 36 | class InstCount : public FunctionPass, public InstVisitor<InstCount> { |
Reid Spencer | e26057a | 2004-11-16 06:58:55 +0000 | [diff] [blame] | 37 | friend class InstVisitor<InstCount>; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 38 | |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 39 | void visitFunction (Function &F) { ++TotalFuncs; } |
| 40 | void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } |
| 41 | |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 42 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
Chris Lattner | a1af8bd | 2002-12-07 23:24:24 +0000 | [diff] [blame] | 43 | void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 44 | |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 45 | #include "llvm/IR/Instruction.def" |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 46 | |
Argyrios Kyrtzidis | 8c8b9ee | 2010-08-15 10:27:23 +0000 | [diff] [blame] | 47 | void visitInstruction(Instruction &I) { |
David Greene | b64ca13 | 2009-12-23 23:29:28 +0000 | [diff] [blame] | 48 | errs() << "Instruction Count does not know about " << I; |
Craig Topper | 570e52c | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 49 | llvm_unreachable(nullptr); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 50 | } |
| 51 | public: |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 52 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 53 | InstCount() : FunctionPass(ID) { |
| 54 | initializeInstCountPass(*PassRegistry::getPassRegistry()); |
| 55 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 56 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 57 | bool runOnFunction(Function &F) override; |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 58 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 59 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 60 | AU.setPreservesAll(); |
| 61 | } |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 62 | void print(raw_ostream &O, const Module *M) const override {} |
Chris Lattner | 149a520 | 2002-12-03 19:40:16 +0000 | [diff] [blame] | 63 | |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 64 | }; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 65 | } |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 66 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 67 | char InstCount::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 68 | INITIALIZE_PASS(InstCount, "instcount", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 69 | "Counts the various types of Instructions", false, true) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 4fb1b21 | 2005-10-24 01:00:45 +0000 | [diff] [blame] | 71 | FunctionPass *llvm::createInstCountPass() { return new InstCount(); } |
| 72 | |
Dinakar Dhurjati | e555460 | 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 | 6666a04 | 2003-08-29 14:43:17 +0000 | [diff] [blame] | 76 | bool InstCount::runOnFunction(Function &F) { |
| 77 | visit(F); |
Dinakar Dhurjati | e555460 | 2002-11-13 18:22:13 +0000 | [diff] [blame] | 78 | return false; |
| 79 | } |