| 1 | //===-- InstCount.cpp - Collects the count of all instructions ------------===// |
| 2 | // |
| 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 |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This pass collects the count of all instructions and reports them |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Analysis/InstCount.h" |
| 14 | #include "llvm/ADT/Statistic.h" |
| 15 | #include "llvm/IR/Function.h" |
| 16 | #include "llvm/IR/InstVisitor.h" |
| 17 | #include "llvm/Support/Debug.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | using namespace llvm; |
| 21 | |
| 22 | #define DEBUG_TYPE "instcount" |
| 23 | |
| 24 | STATISTIC(TotalInsts, "Number of instructions of all types" ); |
| 25 | STATISTIC(TotalBlocks, "Number of basic blocks" ); |
| 26 | STATISTIC(TotalFuncs, "Number of non-external functions" ); |
| 27 | |
| 28 | STATISTIC(LargestFunctionSize, |
| 29 | "Largest number of instructions in a single function" ); |
| 30 | STATISTIC(LargestFunctionBBCount, |
| 31 | "Largest number of basic blocks in a single function" ); |
| 32 | |
| 33 | STATISTIC(TotalInstsPreOptimization, |
| 34 | "Number of instructions of all types (before optimizations)" ); |
| 35 | STATISTIC(TotalBlocksPreOptimization, |
| 36 | "Number of basic blocks (before optimizations)" ); |
| 37 | STATISTIC(TotalFuncsPreOptimization, |
| 38 | "Number of non-external functions (before optimizations)" ); |
| 39 | |
| 40 | STATISTIC(LargestFunctionSizePreOptimization, |
| 41 | "Largest number of instructions in a single function (before " |
| 42 | "optimizations)" ); |
| 43 | STATISTIC(LargestFunctionBBCountPreOptimization, |
| 44 | "Largest number of basic blocks in a single function (before " |
| 45 | "optimizations)" ); |
| 46 | |
| 47 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 48 | STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts"); \ |
| 49 | STATISTIC(Num##OPCODE##InstPreOptimization, \ |
| 50 | "Number of " #OPCODE " insts (before optimizations)"); |
| 51 | |
| 52 | #include "llvm/IR/Instruction.def" |
| 53 | |
| 54 | namespace { |
| 55 | class InstCount : public InstVisitor<InstCount> { |
| 56 | friend class InstVisitor<InstCount>; |
| 57 | |
| 58 | bool IsPreOptimization; |
| 59 | |
| 60 | void visitFunction(Function &F) { |
| 61 | if (IsPreOptimization) { |
| 62 | ++TotalFuncsPreOptimization; |
| 63 | LargestFunctionSizePreOptimization.updateMax(V: F.getInstructionCount()); |
| 64 | LargestFunctionBBCountPreOptimization.updateMax(V: F.size()); |
| 65 | } else { |
| 66 | ++TotalFuncs; |
| 67 | LargestFunctionSize.updateMax(V: F.getInstructionCount()); |
| 68 | LargestFunctionBBCount.updateMax(V: F.size()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void visitBasicBlock(BasicBlock &BB) { |
| 73 | if (IsPreOptimization) |
| 74 | ++TotalBlocksPreOptimization; |
| 75 | else |
| 76 | ++TotalBlocks; |
| 77 | } |
| 78 | |
| 79 | #define HANDLE_INST(N, OPCODE, CLASS) \ |
| 80 | void visit##OPCODE(CLASS &) { \ |
| 81 | if (IsPreOptimization) { \ |
| 82 | ++Num##OPCODE##InstPreOptimization; \ |
| 83 | ++TotalInstsPreOptimization; \ |
| 84 | } else { \ |
| 85 | ++Num##OPCODE##Inst; \ |
| 86 | ++TotalInsts; \ |
| 87 | } \ |
| 88 | } |
| 89 | |
| 90 | #include "llvm/IR/Instruction.def" |
| 91 | |
| 92 | void visitInstruction(Instruction &I) { |
| 93 | errs() << "Instruction Count does not know about " << I; |
| 94 | llvm_unreachable(nullptr); |
| 95 | } |
| 96 | |
| 97 | public: |
| 98 | InstCount(bool IsPreOptimization = false) |
| 99 | : IsPreOptimization(IsPreOptimization) {} |
| 100 | }; |
| 101 | } // namespace |
| 102 | |
| 103 | PreservedAnalyses InstCountPass::run(Function &F, |
| 104 | FunctionAnalysisManager &FAM) { |
| 105 | LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName() |
| 106 | << "\n" ); |
| 107 | InstCount(this->IsPreOptimization).visit(F); |
| 108 | |
| 109 | return PreservedAnalyses::all(); |
| 110 | } |
| 111 | |