OptimalEdgeProfiling.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //===- OptimalEdgeProfiling.cpp - Insert counters for opt. 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. //===----------------------------------------------------------------------===//
  15. #define DEBUG_TYPE "insert-optimal-edge-profiling"
  16. #include "ProfilingUtils.h"
  17. #include "llvm/Module.h"
  18. #include "llvm/Pass.h"
  19. #include "llvm/Analysis/Passes.h"
  20. #include "llvm/Analysis/ProfileInfo.h"
  21. #include "llvm/Analysis/ProfileInfoLoader.h"
  22. #include "llvm/Support/raw_ostream.h"
  23. #include "llvm/Support/Debug.h"
  24. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  25. #include "llvm/Transforms/Instrumentation.h"
  26. #include "llvm/ADT/DenseSet.h"
  27. #include "llvm/ADT/Statistic.h"
  28. #include "MaximumSpanningTree.h"
  29. #include <set>
  30. using namespace llvm;
  31. STATISTIC(NumEdgesInserted, "The # of edges inserted.");
  32. namespace {
  33. class OptimalEdgeProfiler : public ModulePass {
  34. bool runOnModule(Module &M);
  35. public:
  36. static char ID; // Pass identification, replacement for typeid
  37. OptimalEdgeProfiler() : ModulePass(&ID) {}
  38. void getAnalysisUsage(AnalysisUsage &AU) const {
  39. AU.addRequiredID(ProfileEstimatorPassID);
  40. AU.addRequired<ProfileInfo>();
  41. }
  42. virtual const char *getPassName() const {
  43. return "Optimal Edge Profiler";
  44. }
  45. };
  46. }
  47. char OptimalEdgeProfiler::ID = 0;
  48. static RegisterPass<OptimalEdgeProfiler>
  49. X("insert-optimal-edge-profiling",
  50. "Insert optimal instrumentation for edge profiling");
  51. ModulePass *llvm::createOptimalEdgeProfilerPass() {
  52. return new OptimalEdgeProfiler();
  53. }
  54. inline static void printEdgeCounter(ProfileInfo::Edge e,
  55. BasicBlock* b,
  56. unsigned i) {
  57. DEBUG(errs() << "--Edge Counter for " << (e) << " in " \
  58. << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n");
  59. }
  60. bool OptimalEdgeProfiler::runOnModule(Module &M) {
  61. Function *Main = M.getFunction("main");
  62. if (Main == 0) {
  63. errs() << "WARNING: cannot insert edge profiling into a module"
  64. << " with no main function!\n";
  65. return false; // No main, no instrumentation!
  66. }
  67. // NumEdges counts all the edges that may be instrumented. Later on its
  68. // decided which edges to actually instrument, to achieve optimal profiling.
  69. // For the entry block a virtual edge (0,entry) is reserved, for each block
  70. // with no successors an edge (BB,0) is reserved. These edges are necessary
  71. // to calculate a truly optimal maximum spanning tree and thus an optimal
  72. // instrumentation.
  73. unsigned NumEdges = 0;
  74. for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
  75. if (F->isDeclaration()) continue;
  76. // Reserve space for (0,entry) edge.
  77. ++NumEdges;
  78. for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
  79. // Keep track of which blocks need to be instrumented. We don't want to
  80. // instrument blocks that are added as the result of breaking critical
  81. // edges!
  82. if (BB->getTerminator()->getNumSuccessors() == 0) {
  83. // Reserve space for (BB,0) edge.
  84. ++NumEdges;
  85. } else {
  86. NumEdges += BB->getTerminator()->getNumSuccessors();
  87. }
  88. }
  89. }
  90. // In the profiling output a counter for each edge is reserved, but only few
  91. // are used. This is done to be able to read back in the profile without
  92. // calulating the maximum spanning tree again, instead each edge counter that
  93. // is not used is initialised with -1 to signal that this edge counter has to
  94. // be calculated from other edge counters on reading the profile info back
  95. // in.
  96. const Type *Int32 = Type::getInt32Ty(M.getContext());
  97. const ArrayType *ATy = ArrayType::get(Int32, NumEdges);
  98. GlobalVariable *Counters =
  99. new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
  100. Constant::getNullValue(ATy), "OptEdgeProfCounters");
  101. NumEdgesInserted = 0;
  102. std::vector<Constant*> Initializer(NumEdges);
  103. Constant* Zero = ConstantInt::get(Int32, 0);
  104. Constant* Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted);
  105. // Instrument all of the edges not in MST...
  106. unsigned i = 0;
  107. for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) {
  108. if (F->isDeclaration()) continue;
  109. DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n");
  110. // Calculate a Maximum Spanning Tree with the edge weights determined by
  111. // ProfileEstimator. ProfileEstimator also assign weights to the virtual
  112. // edges (0,entry) and (BB,0) (for blocks with no successors) and this
  113. // edges also participate in the maximum spanning tree calculation.
  114. // The third parameter of MaximumSpanningTree() has the effect that not the
  115. // actual MST is returned but the edges _not_ in the MST.
  116. ProfileInfo::EdgeWeights ECs =
  117. getAnalysisID<ProfileInfo>(ProfileEstimatorPassID, *F).getEdgeWeights(F);
  118. std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end());
  119. MaximumSpanningTree<BasicBlock> MST (EdgeVector);
  120. std::stable_sort(MST.begin(),MST.end());
  121. // Check if (0,entry) not in the MST. If not, instrument edge
  122. // (IncrementCounterInBlock()) and set the counter initially to zero, if
  123. // the edge is in the MST the counter is initialised to -1.
  124. BasicBlock *entry = &(F->getEntryBlock());
  125. ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry);
  126. if (!std::binary_search(MST.begin(), MST.end(), edge)) {
  127. printEdgeCounter(edge,entry,i);
  128. IncrementCounterInBlock(entry, i, Counters); NumEdgesInserted++;
  129. Initializer[i++] = (Zero);
  130. } else{
  131. Initializer[i++] = (Uncounted);
  132. }
  133. // InsertedBlocks contains all blocks that were inserted for splitting an
  134. // edge, this blocks do not have to be instrumented.
  135. DenseSet<BasicBlock*> InsertedBlocks;
  136. for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
  137. // Check if block was not inserted and thus does not have to be
  138. // instrumented.
  139. if (InsertedBlocks.count(BB)) continue;
  140. // Okay, we have to add a counter of each outgoing edge not in MST. If
  141. // the outgoing edge is not critical don't split it, just insert the
  142. // counter in the source or destination of the edge. Also, if the block
  143. // has no successors, the virtual edge (BB,0) is processed.
  144. TerminatorInst *TI = BB->getTerminator();
  145. if (TI->getNumSuccessors() == 0) {
  146. ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0);
  147. if (!std::binary_search(MST.begin(), MST.end(), edge)) {
  148. printEdgeCounter(edge,BB,i);
  149. IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
  150. Initializer[i++] = (Zero);
  151. } else{
  152. Initializer[i++] = (Uncounted);
  153. }
  154. }
  155. for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) {
  156. BasicBlock *Succ = TI->getSuccessor(s);
  157. ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ);
  158. if (!std::binary_search(MST.begin(), MST.end(), edge)) {
  159. // If the edge is critical, split it.
  160. bool wasInserted = SplitCriticalEdge(TI, s, this);
  161. Succ = TI->getSuccessor(s);
  162. if (wasInserted)
  163. InsertedBlocks.insert(Succ);
  164. // Okay, we are guaranteed that the edge is no longer critical. If
  165. // we only have a single successor, insert the counter in this block,
  166. // otherwise insert it in the successor block.
  167. if (TI->getNumSuccessors() == 1) {
  168. // Insert counter at the start of the block
  169. printEdgeCounter(edge,BB,i);
  170. IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++;
  171. } else {
  172. // Insert counter at the start of the block
  173. printEdgeCounter(edge,Succ,i);
  174. IncrementCounterInBlock(Succ, i, Counters); NumEdgesInserted++;
  175. }
  176. Initializer[i++] = (Zero);
  177. } else {
  178. Initializer[i++] = (Uncounted);
  179. }
  180. }
  181. }
  182. }
  183. // Check if the number of edges counted at first was the number of edges we
  184. // considered for instrumentation.
  185. assert(i==NumEdges && "the number of edges in counting array is wrong");
  186. // Assing the now completely defined initialiser to the array.
  187. Constant *init = ConstantArray::get(ATy, Initializer);
  188. Counters->setInitializer(init);
  189. // Add the initialization call to main.
  190. InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters);
  191. return true;
  192. }