InstLoops.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //===-- InstLoops.cpp -----------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by the LLVM research group and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This is the first-level instrumentation pass for the Reoptimizer. It
  11. // instrument the back-edges of loops by inserting a basic block
  12. // containing a call to llvm_first_trigger (the first-level trigger function),
  13. // and inserts an initialization call to the main() function.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "llvm/Analysis/Dominators.h"
  17. #include "llvm/Support/CFG.h"
  18. #include "llvm/Instructions.h"
  19. #include "llvm/Module.h"
  20. #include "llvm/Pass.h"
  21. #include "llvm/Type.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Transforms/Instrumentation.h"
  24. #include "../ProfilingUtils.h"
  25. namespace llvm {
  26. //this is used to color vertices
  27. //during DFS
  28. enum Color{
  29. WHITE,
  30. GREY,
  31. BLACK
  32. };
  33. namespace {
  34. typedef std::map<BasicBlock *, BasicBlock *> BBMap;
  35. struct InstLoops : public FunctionPass {
  36. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  37. AU.addRequired<DominatorSet>();
  38. }
  39. private:
  40. Function *inCountMth;
  41. DominatorSet *DS;
  42. void getBackEdgesVisit(BasicBlock *u,
  43. std::map<BasicBlock *, Color > &color,
  44. std::map<BasicBlock *, int > &d,
  45. int &time, BBMap &be);
  46. void removeRedundant(BBMap &be);
  47. void findAndInstrumentBackEdges(Function &F);
  48. public:
  49. bool doInitialization(Module &M);
  50. bool runOnFunction(Function &F);
  51. };
  52. RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling");
  53. }
  54. //helper function to get back edges: it is called by
  55. //the "getBackEdges" function below
  56. void InstLoops::getBackEdgesVisit(BasicBlock *u,
  57. std::map<BasicBlock *, Color > &color,
  58. std::map<BasicBlock *, int > &d,
  59. int &time, BBMap &be) {
  60. color[u]=GREY;
  61. time++;
  62. d[u]=time;
  63. for(succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
  64. BasicBlock *BB = *vl;
  65. if(color[BB]!=GREY && color[BB]!=BLACK){
  66. getBackEdgesVisit(BB, color, d, time, be);
  67. }
  68. //now checking for d and f vals
  69. else if(color[BB]==GREY){
  70. //so v is ancestor of u if time of u > time of v
  71. if(d[u] >= d[BB]){
  72. //u->BB is a backedge
  73. be[u] = BB;
  74. }
  75. }
  76. }
  77. color[u]=BLACK;//done with visiting the node and its neighbors
  78. }
  79. //look at all BEs, and remove all BEs that are dominated by other BE's in the
  80. //set
  81. void InstLoops::removeRedundant(BBMap &be) {
  82. std::vector<BasicBlock *> toDelete;
  83. for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
  84. ME = be.end(); MI != ME; ++MI)
  85. for(BBMap::iterator MMI = be.begin(), MME = be.end(); MMI != MME; ++MMI)
  86. if(DS->properlyDominates(MI->first, MMI->first))
  87. toDelete.push_back(MMI->first);
  88. // Remove all the back-edges we found from be.
  89. for(std::vector<BasicBlock *>::iterator VI = toDelete.begin(),
  90. VE = toDelete.end(); VI != VE; ++VI)
  91. be.erase(*VI);
  92. }
  93. //getting the backedges in a graph
  94. //Its a variation of DFS to get the backedges in the graph
  95. //We get back edges by associating a time
  96. //and a color with each vertex.
  97. //The time of a vertex is the time when it was first visited
  98. //The color of a vertex is initially WHITE,
  99. //Changes to GREY when it is first visited,
  100. //and changes to BLACK when ALL its neighbors
  101. //have been visited
  102. //So we have a back edge when we meet a successor of
  103. //a node with smaller time, and GREY color
  104. void InstLoops::findAndInstrumentBackEdges(Function &F){
  105. std::map<BasicBlock *, Color > color;
  106. std::map<BasicBlock *, int> d;
  107. BBMap be;
  108. int time=0;
  109. getBackEdgesVisit(F.begin(), color, d, time, be);
  110. removeRedundant(be);
  111. for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
  112. ME = be.end(); MI != ME; ++MI){
  113. BasicBlock *u = MI->first;
  114. BasicBlock *BB = MI->second;
  115. // We have a back-edge from BB --> u.
  116. DEBUG (std::cerr << "Instrumenting back-edge from " << BB->getName ()
  117. << "-->" << u->getName () << "\n");
  118. // Split the back-edge, inserting a new basic block on it, and modify the
  119. // source BB's terminator accordingly.
  120. BasicBlock *newBB = new BasicBlock("backEdgeInst", u->getParent());
  121. BranchInst *ti = cast<BranchInst>(u->getTerminator());
  122. unsigned char index = ((ti->getSuccessor(0) == BB) ? 0 : 1);
  123. assert(ti->getNumSuccessors() > index && "Not enough successors!");
  124. ti->setSuccessor(index, newBB);
  125. BasicBlock::InstListType &lt = newBB->getInstList();
  126. lt.push_back(new CallInst(inCountMth));
  127. new BranchInst(BB, newBB);
  128. // Now, set the sources of Phi nodes corresponding to the back-edge
  129. // in BB to come from the instrumentation block instead.
  130. for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end();
  131. BB2Inst != BBend; ++BB2Inst) {
  132. if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)) {
  133. int bbIndex = phiInst->getBasicBlockIndex(u);
  134. if (bbIndex>=0)
  135. phiInst->setIncomingBlock(bbIndex, newBB);
  136. }
  137. }
  138. }
  139. }
  140. bool InstLoops::doInitialization (Module &M) {
  141. inCountMth = M.getOrInsertFunction("llvm_first_trigger", Type::VoidTy,
  142. (Type *)0);
  143. return true; // Module was modified.
  144. }
  145. /// runOnFunction - Entry point for FunctionPass that inserts calls to
  146. /// trigger function.
  147. ///
  148. bool InstLoops::runOnFunction(Function &F){
  149. if (F.isExternal ())
  150. return false;
  151. DS = &getAnalysis<DominatorSet> ();
  152. // Add a call to reoptimizerInitialize() to beginning of function named main.
  153. if (F.getName() == "main")
  154. InsertProfilingInitCall (&F, "reoptimizerInitialize");
  155. findAndInstrumentBackEdges(F);
  156. return true; // Function was modified.
  157. }
  158. FunctionPass *createLoopInstrumentationPass () {
  159. return new InstLoops();
  160. }
  161. } // End llvm namespace