diff --git a/include/llvm/Analysis/InstCount.h b/include/llvm/Analysis/InstCount.h new file mode 100644 index 00000000000..e5ce822caf6 --- /dev/null +++ b/include/llvm/Analysis/InstCount.h @@ -0,0 +1,28 @@ +//===- InstCount.h - Collects the count of all instructions -----*- C++ -*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// This pass collects the count of all instructions and reports them +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ANALYSIS_INSTCOUNT_H +#define LLVM_ANALYSIS_INSTCOUNT_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { + +class Function; + +struct InstCountPass : PassInfoMixin { + PreservedAnalyses run(Function &F, FunctionAnalysisManager &); +}; + +} // end namespace llvm + +#endif // LLVM_ANALYSIS_INSTCOUNT_H diff --git a/include/llvm/InitializePasses.h b/include/llvm/InitializePasses.h index 6601d446ec4..ec97b7f276b 100644 --- a/include/llvm/InitializePasses.h +++ b/include/llvm/InitializePasses.h @@ -192,7 +192,7 @@ void initializeInferAddressSpacesPass(PassRegistry&); void initializeInferFunctionAttrsLegacyPassPass(PassRegistry&); void initializeInjectTLIMappingsLegacyPass(PassRegistry &); void initializeInlineCostAnalysisPass(PassRegistry&); -void initializeInstCountPass(PassRegistry&); +void initializeInstCountLegacyPassPass(PassRegistry &); void initializeInstNamerPass(PassRegistry&); void initializeInstSimplifyLegacyPassPass(PassRegistry &); void initializeInstrProfilingLegacyPassPass(PassRegistry&); diff --git a/lib/Analysis/Analysis.cpp b/lib/Analysis/Analysis.cpp index af718526684..a9ece42df85 100644 --- a/lib/Analysis/Analysis.cpp +++ b/lib/Analysis/Analysis.cpp @@ -50,7 +50,7 @@ void llvm::initializeAnalysis(PassRegistry &Registry) { initializeAAResultsWrapperPassPass(Registry); initializeGlobalsAAWrapperPassPass(Registry); initializeIVUsersWrapperPassPass(Registry); - initializeInstCountPass(Registry); + initializeInstCountLegacyPassPass(Registry); initializeIntervalPartitionPass(Registry); initializeLazyBranchProbabilityInfoPassPass(Registry); initializeLazyBlockFrequencyInfoPassPass(Registry); diff --git a/lib/Analysis/InstCount.cpp b/lib/Analysis/InstCount.cpp index bb9c7b7eb11..8366bee083f 100644 --- a/lib/Analysis/InstCount.cpp +++ b/lib/Analysis/InstCount.cpp @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Analysis/InstCount.h" #include "llvm/ADT/Statistic.h" #include "llvm/Analysis/Passes.h" #include "llvm/IR/Function.h" @@ -23,57 +24,71 @@ using namespace llvm; #define DEBUG_TYPE "instcount" -STATISTIC(TotalInsts , "Number of instructions (of all types)"); +STATISTIC(TotalInsts, "Number of instructions (of all types)"); STATISTIC(TotalBlocks, "Number of basic blocks"); -STATISTIC(TotalFuncs , "Number of non-external functions"); +STATISTIC(TotalFuncs, "Number of non-external functions"); -#define HANDLE_INST(N, OPCODE, CLASS) \ - STATISTIC(Num ## OPCODE ## Inst, "Number of " #OPCODE " insts"); +#define HANDLE_INST(N, OPCODE, CLASS) \ + STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts"); #include "llvm/IR/Instruction.def" namespace { - class InstCount : public FunctionPass, public InstVisitor { - friend class InstVisitor; +class InstCount : public InstVisitor { + friend class InstVisitor; - void visitFunction (Function &F) { ++TotalFuncs; } - void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } + void visitFunction(Function &F) { ++TotalFuncs; } + void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; } -#define HANDLE_INST(N, OPCODE, CLASS) \ - void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; } +#define HANDLE_INST(N, OPCODE, CLASS) \ + void visit##OPCODE(CLASS &) { \ + ++Num##OPCODE##Inst; \ + ++TotalInsts; \ + } #include "llvm/IR/Instruction.def" - void visitInstruction(Instruction &I) { - errs() << "Instruction Count does not know about " << I; - llvm_unreachable(nullptr); - } - public: - static char ID; // Pass identification, replacement for typeid - InstCount() : FunctionPass(ID) { - initializeInstCountPass(*PassRegistry::getPassRegistry()); - } + void visitInstruction(Instruction &I) { + errs() << "Instruction Count does not know about " << I; + llvm_unreachable(nullptr); + } +}; +} // namespace - bool runOnFunction(Function &F) override; +PreservedAnalyses InstCountPass::run(Function &F, + FunctionAnalysisManager &FAM) { + LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName() + << "\n"); + InstCount().visit(F); - void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.setPreservesAll(); - } - void print(raw_ostream &O, const Module *M) const override {} - - }; + return PreservedAnalyses::all(); } -char InstCount::ID = 0; -INITIALIZE_PASS(InstCount, "instcount", +namespace { +class InstCountLegacyPass : public FunctionPass { +public: + static char ID; // Pass identification, replacement for typeid + InstCountLegacyPass() : FunctionPass(ID) { + initializeInstCountLegacyPassPass(*PassRegistry::getPassRegistry()); + } + + bool runOnFunction(Function &F) override { + LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName() + << "\n"); + InstCount().visit(F); + return false; + }; + + void getAnalysisUsage(AnalysisUsage &AU) const override { + AU.setPreservesAll(); + } + + void print(raw_ostream &O, const Module *M) const override {} +}; +} // namespace + +char InstCountLegacyPass::ID = 0; +INITIALIZE_PASS(InstCountLegacyPass, "instcount", "Counts the various types of Instructions", false, true) -FunctionPass *llvm::createInstCountPass() { return new InstCount(); } - -// InstCount::run - This is the main Analysis entry point for a -// function. -// -bool InstCount::runOnFunction(Function &F) { - visit(F); - return false; -} +FunctionPass *llvm::createInstCountPass() { return new InstCountLegacyPass(); } diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp index b9d1e76fcf1..010567f1c7c 100644 --- a/lib/Passes/PassBuilder.cpp +++ b/lib/Passes/PassBuilder.cpp @@ -36,6 +36,7 @@ #include "llvm/Analysis/IVUsers.h" #include "llvm/Analysis/InlineAdvisor.h" #include "llvm/Analysis/InlineSizeEstimatorAnalysis.h" +#include "llvm/Analysis/InstCount.h" #include "llvm/Analysis/LazyCallGraph.h" #include "llvm/Analysis/LazyValueInfo.h" #include "llvm/Analysis/LoopAccessAnalysis.h" diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def index 1176db872c5..4bbecfeb82a 100644 --- a/lib/Passes/PassRegistry.def +++ b/lib/Passes/PassRegistry.def @@ -196,6 +196,7 @@ FUNCTION_PASS("make-guards-explicit", MakeGuardsExplicitPass()) FUNCTION_PASS("post-inline-ee-instrument", EntryExitInstrumenterPass(/*PostInlining=*/true)) FUNCTION_PASS("gvn-hoist", GVNHoistPass()) FUNCTION_PASS("instcombine", InstCombinePass()) +FUNCTION_PASS("instcount", InstCountPass()) FUNCTION_PASS("instsimplify", InstSimplifyPass()) FUNCTION_PASS("invalidate", InvalidateAllAnalysesPass()) FUNCTION_PASS("irce", IRCEPass())