EdgeProfiling.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===- EdgeProfiling.cpp - Insert counters for edge 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 edge profiling.
  11. // Edge profiling can give a reasonable approximation of the hot paths through a
  12. // program, and is used for a wide variety of program transformations.
  13. //
  14. // Note that this implementation is very naive. We insert a counter for *every*
  15. // edge in the program, instead of using control flow information to prune the
  16. // number of counters inserted.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #define DEBUG_TYPE "insert-edge-profiling"
  20. #include "ProfilingUtils.h"
  21. #include "llvm/Module.h"
  22. #include "llvm/Pass.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  25. #include "llvm/Transforms/Instrumentation.h"
  26. #include "llvm/ADT/Statistic.h"
  27. #include <set>
  28. using namespace llvm;
  29. STATISTIC(NumEdgesInserted, "The # of edges inserted.");
  30. namespace {
  31. class EdgeProfiler : public ModulePass {
  32. bool runOnModule(Module &M);
  33. public:
  34. static char ID; // Pass identification, replacement for typeid
  35. EdgeProfiler() : ModulePass(&ID) {}
  36. virtual const char *getPassName() const {
  37. return "Edge Profiler";
  38. }
  39. };
  40. }
  41. char EdgeProfiler::ID = 0;
  42. static RegisterPass<EdgeProfiler>
  43. X("insert-edge-profiling", "Insert instrumentation for edge profiling");
  44. ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); }
  45. bool EdgeProfiler::runOnModule(Module &M) {
  46. Function *Main = M.getFunction("main");
  47. if (Main == 0) {
  48. errs() << "WARNING: cannot insert edge profiling into a module"
  49. << " with no main function!\n";
  50. return false; // No main, no instrumentation!
  51. }
  52. std::set<BasicBlock*> BlocksToInstrument;
  53. unsigned NumEdges = 0;
  54. for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
  55. if (F->isDeclaration()) continue;
  56. // Reserve space for (0,entry) edge.
  57. ++NumEdges;
  58. for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
  59. // Keep track of which blocks need to be instrumented. We don't want to
  60. // instrument blocks that are added as the result of breaking critical
  61. // edges!
  62. BlocksToInstrument.insert(BB);
  63. NumEdges += BB->getTerminator()->getNumSuccessors();
  64. }
  65. }
  66. const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumEdges);
  67. GlobalVariable *Counters =
  68. new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
  69. Constant::getNullValue(ATy), "EdgeProfCounters");
  70. NumEdgesInserted = NumEdges;
  71. // Instrument all of the edges...
  72. unsigned i = 0;
  73. for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
  74. if (F->isDeclaration()) continue;
  75. // Create counter for (0,entry) edge.
  76. IncrementCounterInBlock(&F->getEntryBlock(), i++, Counters);
  77. for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
  78. if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks
  79. // Okay, we have to add a counter of each outgoing edge. If the
  80. // outgoing edge is not critical don't split it, just insert the counter
  81. // in the source or destination of the edge.
  82. TerminatorInst *TI = BB->getTerminator();
  83. for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
  84. // If the edge is critical, split it.
  85. SplitCriticalEdge(TI, s, this);
  86. // Okay, we are guaranteed that the edge is no longer critical. If we
  87. // only have a single successor, insert the counter in this block,
  88. // otherwise insert it in the successor block.
  89. if (TI->getNumSuccessors() == 1) {
  90. // Insert counter at the start of the block
  91. IncrementCounterInBlock(BB, i++, Counters);
  92. } else {
  93. // Insert counter at the start of the block
  94. IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters);
  95. }
  96. }
  97. }
  98. }
  99. // Add the initialization call to main.
  100. InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters);
  101. return true;
  102. }