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