BlockProfiling.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/DerivedTypes.h"
  22. #include "llvm/Module.h"
  23. #include "llvm/Pass.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/Transforms/Instrumentation.h"
  26. #include "RSProfiling.h"
  27. #include "ProfilingUtils.h"
  28. using namespace llvm;
  29. namespace {
  30. class FunctionProfiler : public RSProfilers_std {
  31. public:
  32. static char ID;
  33. bool runOnModule(Module &M);
  34. };
  35. }
  36. char FunctionProfiler::ID = 0;
  37. static RegisterPass<FunctionProfiler>
  38. X("insert-function-profiling",
  39. "Insert instrumentation for function profiling");
  40. static RegisterAnalysisGroup<RSProfilers> XG(X);
  41. ModulePass *llvm::createFunctionProfilerPass() {
  42. return new FunctionProfiler();
  43. }
  44. bool FunctionProfiler::runOnModule(Module &M) {
  45. Function *Main = M.getFunction("main");
  46. if (Main == 0) {
  47. errs() << "WARNING: cannot insert function profiling into a module"
  48. << " with no main function!\n";
  49. return false; // No main, no instrumentation!
  50. }
  51. unsigned NumFunctions = 0;
  52. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  53. if (!I->isDeclaration())
  54. ++NumFunctions;
  55. const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()),
  56. NumFunctions);
  57. GlobalVariable *Counters =
  58. new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
  59. Constant::getNullValue(ATy), "FuncProfCounters");
  60. // Instrument all of the functions...
  61. unsigned i = 0;
  62. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  63. if (!I->isDeclaration())
  64. // Insert counter at the start of the function
  65. IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters);
  66. // Add the initialization call to main.
  67. InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
  68. return true;
  69. }
  70. namespace {
  71. class BlockProfiler : public RSProfilers_std {
  72. bool runOnModule(Module &M);
  73. public:
  74. static char ID;
  75. };
  76. }
  77. char BlockProfiler::ID = 0;
  78. static RegisterPass<BlockProfiler>
  79. Y("insert-block-profiling", "Insert instrumentation for block profiling");
  80. static RegisterAnalysisGroup<RSProfilers> YG(Y);
  81. ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
  82. bool BlockProfiler::runOnModule(Module &M) {
  83. Function *Main = M.getFunction("main");
  84. if (Main == 0) {
  85. errs() << "WARNING: cannot insert block profiling into a module"
  86. << " with no main function!\n";
  87. return false; // No main, no instrumentation!
  88. }
  89. unsigned NumBlocks = 0;
  90. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
  91. if (!I->isDeclaration())
  92. NumBlocks += I->size();
  93. const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks);
  94. GlobalVariable *Counters =
  95. new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
  96. Constant::getNullValue(ATy), "BlockProfCounters");
  97. // Instrument all of the blocks...
  98. unsigned i = 0;
  99. for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
  100. if (I->isDeclaration()) continue;
  101. for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
  102. // Insert counter at the start of the block
  103. IncrementCounterInBlock(BB, i++, Counters);
  104. }
  105. // Add the initialization call to main.
  106. InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
  107. return true;
  108. }