2
0

InstCount.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===-- InstCount.cpp - Collects the count of all instructions ------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This pass collects the count of all instructions and reports them
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/Passes.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/IR/Function.h"
  16. #include "llvm/IR/InstVisitor.h"
  17. #include "llvm/Pass.h"
  18. #include "llvm/Support/Debug.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. #define DEBUG_TYPE "instcount"
  23. STATISTIC(TotalInsts , "Number of instructions (of all types)");
  24. STATISTIC(TotalBlocks, "Number of basic blocks");
  25. STATISTIC(TotalFuncs , "Number of non-external functions");
  26. STATISTIC(TotalMemInst, "Number of memory instructions");
  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 FunctionPass, 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 &) { ++Num##OPCODE##Inst; ++TotalInsts; }
  37. #include "llvm/IR/Instruction.def"
  38. void visitInstruction(Instruction &I) {
  39. errs() << "Instruction Count does not know about " << I;
  40. llvm_unreachable(nullptr);
  41. }
  42. public:
  43. static char ID; // Pass identification, replacement for typeid
  44. InstCount() : FunctionPass(ID) {
  45. initializeInstCountPass(*PassRegistry::getPassRegistry());
  46. }
  47. bool runOnFunction(Function &F) override;
  48. void getAnalysisUsage(AnalysisUsage &AU) const override {
  49. AU.setPreservesAll();
  50. }
  51. void print(raw_ostream &O, const Module *M) const override {}
  52. };
  53. }
  54. char InstCount::ID = 0;
  55. INITIALIZE_PASS(InstCount, "instcount",
  56. "Counts the various types of Instructions", false, true)
  57. FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
  58. // InstCount::run - This is the main Analysis entry point for a
  59. // function.
  60. //
  61. bool InstCount::runOnFunction(Function &F) {
  62. unsigned StartMemInsts =
  63. NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
  64. NumInvokeInst + NumAllocaInst;
  65. visit(F);
  66. unsigned EndMemInsts =
  67. NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
  68. NumInvokeInst + NumAllocaInst;
  69. TotalMemInst += EndMemInsts-StartMemInsts;
  70. return false;
  71. }