InstCount.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "llvm/Analysis/InstCount.h"
  13. #include "llvm/ADT/Statistic.h"
  14. #include "llvm/Analysis/Passes.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/InstVisitor.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/Pass.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/ErrorHandling.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. using namespace llvm;
  23. #define DEBUG_TYPE "instcount"
  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. #define HANDLE_INST(N, OPCODE, CLASS) \
  28. STATISTIC(Num##OPCODE##Inst, "Number of " #OPCODE " insts");
  29. #include "llvm/IR/Instruction.def"
  30. namespace {
  31. class InstCount : public InstVisitor<InstCount> {
  32. friend class InstVisitor<InstCount>;
  33. void visitFunction(Function &F) { ++TotalFuncs; }
  34. void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
  35. #define HANDLE_INST(N, OPCODE, CLASS) \
  36. void visit##OPCODE(CLASS &) { \
  37. ++Num##OPCODE##Inst; \
  38. ++TotalInsts; \
  39. }
  40. #include "llvm/IR/Instruction.def"
  41. void visitInstruction(Instruction &I) {
  42. errs() << "Instruction Count does not know about " << I;
  43. llvm_unreachable(nullptr);
  44. }
  45. };
  46. } // namespace
  47. PreservedAnalyses InstCountPass::run(Function &F,
  48. FunctionAnalysisManager &FAM) {
  49. LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
  50. << "\n");
  51. InstCount().visit(F);
  52. return PreservedAnalyses::all();
  53. }
  54. namespace {
  55. class InstCountLegacyPass : public FunctionPass {
  56. public:
  57. static char ID; // Pass identification, replacement for typeid
  58. InstCountLegacyPass() : FunctionPass(ID) {
  59. initializeInstCountLegacyPassPass(*PassRegistry::getPassRegistry());
  60. }
  61. bool runOnFunction(Function &F) override {
  62. LLVM_DEBUG(dbgs() << "INSTCOUNT: running on function " << F.getName()
  63. << "\n");
  64. InstCount().visit(F);
  65. return false;
  66. };
  67. void getAnalysisUsage(AnalysisUsage &AU) const override {
  68. AU.setPreservesAll();
  69. }
  70. void print(raw_ostream &O, const Module *M) const override {}
  71. };
  72. } // namespace
  73. char InstCountLegacyPass::ID = 0;
  74. INITIALIZE_PASS(InstCountLegacyPass, "instcount",
  75. "Counts the various types of Instructions", false, true)
  76. FunctionPass *llvm::createInstCountPass() { return new InstCountLegacyPass(); }