InstCount.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #define DEBUG_TYPE "instcount"
  14. #include "llvm/Analysis/Passes.h"
  15. #include "llvm/Pass.h"
  16. #include "llvm/Function.h"
  17. #include "llvm/Support/Compiler.h"
  18. #include "llvm/Support/InstVisitor.h"
  19. #include "llvm/Support/Streams.h"
  20. #include "llvm/ADT/Statistic.h"
  21. #include <ostream>
  22. using namespace llvm;
  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/Instruction.def"
  30. namespace {
  31. class VISIBILITY_HIDDEN InstCount
  32. : public FunctionPass, public InstVisitor<InstCount> {
  33. friend class InstVisitor<InstCount>;
  34. void visitFunction (Function &F) { ++TotalFuncs; }
  35. void visitBasicBlock(BasicBlock &BB) { ++TotalBlocks; }
  36. #define HANDLE_INST(N, OPCODE, CLASS) \
  37. void visit##OPCODE(CLASS &) { ++Num##OPCODE##Inst; ++TotalInsts; }
  38. #include "llvm/Instruction.def"
  39. void visitInstruction(Instruction &I) {
  40. cerr << "Instruction Count does not know about " << I;
  41. abort();
  42. }
  43. public:
  44. static char ID; // Pass identification, replacement for typeid
  45. InstCount() : FunctionPass(&ID) {}
  46. virtual bool runOnFunction(Function &F);
  47. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  48. AU.setPreservesAll();
  49. }
  50. virtual void print(std::ostream &O, const Module *M) const {}
  51. };
  52. }
  53. char InstCount::ID = 0;
  54. static RegisterPass<InstCount>
  55. X("instcount", "Counts the various types of Instructions", false, true);
  56. FunctionPass *llvm::createInstCountPass() { return new InstCount(); }
  57. // InstCount::run - This is the main Analysis entry point for a
  58. // function.
  59. //
  60. bool InstCount::runOnFunction(Function &F) {
  61. unsigned StartMemInsts =
  62. NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
  63. NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
  64. visit(F);
  65. unsigned EndMemInsts =
  66. NumGetElementPtrInst + NumLoadInst + NumStoreInst + NumCallInst +
  67. NumInvokeInst + NumAllocaInst + NumMallocInst + NumFreeInst;
  68. TotalMemInst += EndMemInsts-StartMemInsts;
  69. return false;
  70. }