Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 1 | //===-- InstLoops.cpp ---------------------------------------- ---*- C++ -*--=// |
| 2 | // Pass to instrument loops |
| 3 | // |
| 4 | // At every backedge, insert a counter for that backedge and a call function |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 7 | #include "llvm/Analysis/Dominators.h" |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 8 | #include "llvm/Support/CFG.h" |
| 9 | #include "llvm/Constants.h" |
| 10 | #include "llvm/iMemory.h" |
| 11 | #include "llvm/GlobalVariable.h" |
| 12 | #include "llvm/DerivedTypes.h" |
| 13 | #include "llvm/iOther.h" |
| 14 | #include "llvm/iOperators.h" |
| 15 | #include "llvm/iTerminators.h" |
| 16 | #include "llvm/iPHINode.h" |
| 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | |
| 21 | //this is used to color vertices |
| 22 | //during DFS |
| 23 | |
| 24 | enum Color{ |
| 25 | WHITE, |
| 26 | GREY, |
| 27 | BLACK |
| 28 | }; |
| 29 | |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 30 | namespace{ |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 31 | typedef std::map<BasicBlock *, BasicBlock *> BBMap; |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 32 | struct InstLoops : public FunctionPass { |
| 33 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 34 | AU.addRequired<DominatorSet>(); |
| 35 | } |
| 36 | private: |
| 37 | DominatorSet *DS; |
| 38 | void getBackEdgesVisit(BasicBlock *u, |
| 39 | std::map<BasicBlock *, Color > &color, |
| 40 | std::map<BasicBlock *, int > &d, |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 41 | int &time, BBMap &be); |
| 42 | void removeRedundant(BBMap &be); |
| 43 | void findAndInstrumentBackEdges(Function &F); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 44 | public: |
| 45 | bool runOnFunction(Function &F); |
| 46 | }; |
| 47 | |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 48 | RegisterOpt<InstLoops> X("instloops", "Instrument backedges for profiling"); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 49 | } |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 50 | |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 51 | //helper function to get back edges: it is called by |
| 52 | //the "getBackEdges" function below |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 53 | void InstLoops::getBackEdgesVisit(BasicBlock *u, |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 54 | std::map<BasicBlock *, Color > &color, |
| 55 | std::map<BasicBlock *, int > &d, |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 56 | int &time, BBMap &be) { |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 57 | color[u]=GREY; |
| 58 | time++; |
| 59 | d[u]=time; |
| 60 | |
Chris Lattner | a940095 | 2003-09-24 22:06:25 +0000 | [diff] [blame] | 61 | for(succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){ |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 62 | BasicBlock *BB = *vl; |
| 63 | |
| 64 | if(color[BB]!=GREY && color[BB]!=BLACK){ |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 65 | getBackEdgesVisit(BB, color, d, time, be); |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | //now checking for d and f vals |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 69 | else if(color[BB]==GREY){ |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 70 | //so v is ancestor of u if time of u > time of v |
| 71 | if(d[u] >= d[BB]){ |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 72 | //u->BB is a backedge |
| 73 | be[u] = BB; |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | color[u]=BLACK;//done with visiting the node and its neighbors |
| 78 | } |
| 79 | |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 80 | //look at all BEs, and remove all BEs that are dominated by other BE's in the |
| 81 | //set |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 82 | void InstLoops::removeRedundant(BBMap &be) { |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 83 | std::vector<BasicBlock *> toDelete; |
| 84 | for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(), |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 85 | ME = be.end(); MI != ME; ++MI) |
| 86 | for(BBMap::iterator MMI = be.begin(), MME = be.end(); MMI != MME; ++MMI) |
| 87 | if(DS->properlyDominates(MI->first, MMI->first)) |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 88 | toDelete.push_back(MMI->first); |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 89 | // Remove all the back-edges we found from be. |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 90 | for(std::vector<BasicBlock *>::iterator VI = toDelete.begin(), |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 91 | VE = toDelete.end(); VI != VE; ++VI) |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 92 | be.erase(*VI); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 93 | } |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 94 | |
| 95 | //getting the backedges in a graph |
| 96 | //Its a variation of DFS to get the backedges in the graph |
| 97 | //We get back edges by associating a time |
| 98 | //and a color with each vertex. |
| 99 | //The time of a vertex is the time when it was first visited |
| 100 | //The color of a vertex is initially WHITE, |
| 101 | //Changes to GREY when it is first visited, |
| 102 | //and changes to BLACK when ALL its neighbors |
| 103 | //have been visited |
| 104 | //So we have a back edge when we meet a successor of |
| 105 | //a node with smaller time, and GREY color |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 106 | void InstLoops::findAndInstrumentBackEdges(Function &F){ |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 107 | std::map<BasicBlock *, Color > color; |
| 108 | std::map<BasicBlock *, int> d; |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 109 | BBMap be; |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 110 | int time=0; |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 111 | getBackEdgesVisit(F.begin(), color, d, time, be); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 112 | |
| 113 | removeRedundant(be); |
| 114 | |
Chris Lattner | 25bc3f8 | 2003-08-31 00:21:59 +0000 | [diff] [blame] | 115 | // FIXME: THIS IS HORRIBLY BROKEN. FunctionPass's cannot do this, except in |
| 116 | // their initialize function!! |
| 117 | Function *inCountMth = |
| 118 | F.getParent()->getOrInsertFunction("llvm_first_trigger", |
| 119 | Type::VoidTy, 0); |
| 120 | |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 121 | for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(), |
| 122 | ME = be.end(); MI != ME; ++MI){ |
| 123 | BasicBlock *u = MI->first; |
| 124 | BasicBlock *BB = MI->second; |
| 125 | //std::cerr<<"Edge from: "<<BB->getName()<<"->"<<u->getName()<<"\n"; |
| 126 | //insert a new basic block: modify terminator accordingly! |
| 127 | BasicBlock *newBB = new BasicBlock("", u->getParent()); |
| 128 | BranchInst *ti = cast<BranchInst>(u->getTerminator()); |
| 129 | unsigned char index = 1; |
| 130 | if(ti->getSuccessor(0) == BB){ |
| 131 | index = 0; |
| 132 | } |
| 133 | assert(ti->getNumSuccessors() > index && "Not enough successors!"); |
| 134 | ti->setSuccessor(index, newBB); |
| 135 | |
| 136 | BasicBlock::InstListType < = newBB->getInstList(); |
| 137 | |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 138 | Instruction *call = new CallInst(inCountMth); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 139 | lt.push_back(call); |
| 140 | lt.push_back(new BranchInst(BB)); |
| 141 | |
| 142 | //now iterate over *vl, and set its Phi nodes right |
| 143 | for(BasicBlock::iterator BB2Inst = BB->begin(), BBend = BB->end(); |
| 144 | BB2Inst != BBend; ++BB2Inst){ |
| 145 | |
| 146 | if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){ |
| 147 | int bbIndex = phiInst->getBasicBlockIndex(u); |
| 148 | if(bbIndex>=0){ |
| 149 | phiInst->setIncomingBlock(bbIndex, newBB); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 156 | /// Entry point for FunctionPass that inserts calls to trigger function. |
| 157 | /// |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 158 | bool InstLoops::runOnFunction(Function &F){ |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 159 | DS = &getAnalysis<DominatorSet>(); |
Anand Shukla | b3d794a | 2003-07-10 21:55:57 +0000 | [diff] [blame] | 160 | if(F.isExternal()) { |
| 161 | return false; |
| 162 | } |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 163 | // Add a call to reoptimizerInitialize() to beginning of function named main. |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 164 | if(F.getName() == "main"){ |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 165 | std::vector<const Type*> argTypes; // Empty formal parameter list. |
| 166 | const FunctionType *Fty = FunctionType::get(Type::VoidTy, argTypes, false); |
| 167 | Function *initialMeth = |
| 168 | F.getParent()->getOrInsertFunction("reoptimizerInitialize", Fty); |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 169 | assert(initialMeth && "Initialize method could not be inserted!"); |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 170 | new CallInst(initialMeth, "", F.begin()->begin()); // Insert it. |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 171 | } |
Brian Gaeke | 709a16a | 2003-08-12 22:00:24 +0000 | [diff] [blame] | 172 | findAndInstrumentBackEdges(F); |
| 173 | return true; // Function was modified. |
Anand Shukla | 320febb | 2002-11-03 01:45:20 +0000 | [diff] [blame] | 174 | } |