BlockProfiling.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
  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 instruments the specified program with counters for basic block or
  11. // function profiling. This is the most basic form of profiling, which can tell
  12. // which blocks are hot, but cannot reliably detect hot paths through the CFG.
  13. // Block profiling counts the number of times each basic block executes, and
  14. // function profiling counts the number of times each function is called.
  15. //
  16. // Note that this implementation is very naive. Control equivalent regions of
  17. // the CFG should not require duplicate counters, but we do put duplicate
  18. // counters in.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #include "llvm/Constants.h"
  22. #include "llvm/DerivedTypes.h"
  23. #include "llvm/LLVMContext.h"
  24. #include "llvm/Module.h"
  25. #include "llvm/Pass.h"
  26. #include "llvm/Support/Compiler.h"
  27. #include "llvm/Support/Streams.h"
  28. #include "llvm/Transforms/Instrumentation.h"
  29. #include "RSProfiling.h"
  30. #include "ProfilingUtils.h"
  31. using namespace llvm;
  32. namespace {
  33. class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std {
  34. public:
  35. static char ID;
  36. bool runOnModule(Module &M);
  37. };
  38. }
  39. char FunctionProfiler::ID = 0;
  40. static RegisterPass<FunctionProfiler>
  41. X("insert-function-profiling",
  42. "Insert instrumentation for function profiling");
  43. static RegisterAnalysisGroup<RSProfilers> XG(X);
  44. ModulePass *llvm::createFunctionProfilerPass() {
  45. return new FunctionProfiler();
  46. }
  47. bool FunctionProfiler::runOnModule(Module &M) {
  48. Function *Main = M.getFunction("main");
  49. if (Main == 0) {
  50. cerr << "WARNING: cannot insert function profiling into a module"
  51. << " with no main function!\n";
  52. return false; // No main, no instrumentation!
  53. }
  54. unsigned NumFunctions = 0;
  55. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  56. if (!I->isDeclaration())
  57. ++NumFunctions;
  58. const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()),
  59. NumFunctions);
  60. GlobalVariable *Counters =
  61. new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
  62. Constant::getNullValue(ATy), "FuncProfCounters");
  63. // Instrument all of the functions...
  64. unsigned i = 0;
  65. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  66. if (!I->isDeclaration())
  67. // Insert counter at the start of the function
  68. IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters);
  69. // Add the initialization call to main.
  70. InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
  71. return true;
  72. }
  73. namespace {
  74. class BlockProfiler : public RSProfilers_std {
  75. bool runOnModule(Module &M);
  76. public:
  77. static char ID;
  78. };
  79. }
  80. char BlockProfiler::ID = 0;
  81. static RegisterPass<BlockProfiler>
  82. Y("insert-block-profiling", "Insert instrumentation for block profiling");
  83. static RegisterAnalysisGroup<RSProfilers> YG(Y);
  84. ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
  85. bool BlockProfiler::runOnModule(Module &M) {
  86. Function *Main = M.getFunction("main");
  87. if (Main == 0) {
  88. cerr << "WARNING: cannot insert block profiling into a module"
  89. << " with no main function!\n";
  90. return false; // No main, no instrumentation!
  91. }
  92. unsigned NumBlocks = 0;
  93. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  94. if (!I->isDeclaration())
  95. NumBlocks += I->size();
  96. const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks);
  97. GlobalVariable *Counters =
  98. new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
  99. Constant::getNullValue(ATy), "BlockProfCounters");
  100. // Instrument all of the blocks...
  101. unsigned i = 0;
  102. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
  103. if (I->isDeclaration()) continue;
  104. for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
  105. // Insert counter at the start of the block
  106. IncrementCounterInBlock(BB, i++, Counters);
  107. }
  108. // Add the initialization call to main.
  109. InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
  110. return true;
  111. }