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