Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +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 | |
| 7 | #include "llvm/Reoptimizer/InstLoops.h" |
| 8 | #include "llvm/Analysis/Dominators.h" |
| 9 | #include "llvm/Support/CFG.h" |
| 10 | #include "llvm/Constants.h" |
| 11 | #include "llvm/iMemory.h" |
| 12 | #include "llvm/GlobalVariable.h" |
| 13 | #include "llvm/DerivedTypes.h" |
| 14 | #include "llvm/iOther.h" |
| 15 | #include "llvm/iOperators.h" |
| 16 | #include "llvm/iTerminators.h" |
| 17 | #include "llvm/iPHINode.h" |
| 18 | #include "llvm/Module.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/Pass.h" |
| 21 | |
| 22 | //this is used to color vertices |
| 23 | //during DFS |
| 24 | |
| 25 | enum Color{ |
| 26 | WHITE, |
| 27 | GREY, |
| 28 | BLACK |
| 29 | }; |
| 30 | |
| 31 | namespace{ |
| 32 | struct CombineBranches : public FunctionPass { |
| 33 | private: |
| 34 | //DominatorSet *DS; |
| 35 | void getBackEdgesVisit(BasicBlock *u, |
| 36 | std::map<BasicBlock *, Color > &color, |
| 37 | std::map<BasicBlock *, int > &d, |
| 38 | int &time, |
| 39 | std::map<BasicBlock *, BasicBlock *> &be); |
| 40 | void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be); |
| 41 | void getBackEdges(Function &F); |
| 42 | public: |
| 43 | bool runOnFunction(Function &F); |
| 44 | }; |
| 45 | |
| 46 | RegisterOpt<CombineBranches> X("branch-combine", "Multiple backedges going to same target are merged"); |
| 47 | } |
| 48 | |
| 49 | // Create a new pass to merge branches |
| 50 | // |
| 51 | Pass *createCombineBranchesPass() { |
| 52 | return new CombineBranches(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | //helper function to get back edges: it is called by |
| 57 | //the "getBackEdges" function below |
| 58 | void CombineBranches::getBackEdgesVisit(BasicBlock *u, |
| 59 | std::map<BasicBlock *, Color > &color, |
| 60 | std::map<BasicBlock *, int > &d, |
| 61 | int &time, |
| 62 | std::map<BasicBlock *, BasicBlock *> &be) { |
| 63 | |
| 64 | color[u]=GREY; |
| 65 | time++; |
| 66 | d[u]=time; |
| 67 | |
| 68 | for(BasicBlock::succ_iterator vl = succ_begin(u), |
| 69 | ve = succ_end(u); vl != ve; ++vl){ |
| 70 | |
| 71 | BasicBlock *BB = *vl; |
| 72 | |
| 73 | if(color[BB]!=GREY && color[BB]!=BLACK){ |
| 74 | getBackEdgesVisit(BB, color, d, time, be); |
| 75 | } |
| 76 | |
| 77 | //now checking for d and f vals |
| 78 | else if(color[BB]==GREY){ |
| 79 | //so v is ancestor of u if time of u > time of v |
| 80 | if(d[u] >= d[BB]){ |
| 81 | //u->BB is a backedge |
| 82 | be[u] = BB; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | color[u]=BLACK;//done with visiting the node and its neighbors |
| 87 | } |
| 88 | |
| 89 | //look at all BEs, and remove all BEs that are dominated by other BE's in the |
| 90 | //set |
| 91 | void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){ |
| 92 | std::vector<BasicBlock *> toDelete; |
| 93 | std::map<BasicBlock *, int> seenBB; |
| 94 | |
| 95 | for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(), |
| 96 | ME = be.end(); MI != ME; ++MI){ |
| 97 | |
| 98 | if(seenBB[MI->second]) |
| 99 | continue; |
| 100 | |
| 101 | seenBB[MI->second] = 1; |
| 102 | |
| 103 | std::vector<BasicBlock *> sameTarget; |
| 104 | sameTarget.clear(); |
| 105 | |
| 106 | for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(), |
| 107 | MME = be.end(); MMI != MME; ++MMI){ |
| 108 | |
| 109 | if(MMI->first == MI->first) |
| 110 | continue; |
| 111 | |
| 112 | if(MMI->second == MI->second) |
| 113 | sameTarget.push_back(MMI->first); |
| 114 | |
| 115 | } |
| 116 | |
| 117 | //so more than one branch to same target |
| 118 | if(sameTarget.size()){ |
| 119 | |
| 120 | sameTarget.push_back(MI->first); |
| 121 | |
| 122 | BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent()); |
| 123 | BranchInst *newBranch = new BranchInst(MI->second); |
| 124 | |
| 125 | newBB->getInstList().push_back(newBranch); |
| 126 | |
| 127 | std::map<PHINode *, std::vector<unsigned int> > phiMap; |
| 128 | |
| 129 | |
| 130 | for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(), |
| 131 | VBE = sameTarget.end(); VBI != VBE; ++VBI){ |
| 132 | |
| 133 | //std::cerr<<(*VBI)->getName()<<"\n"; |
| 134 | |
| 135 | BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator()); |
| 136 | unsigned char index = 1; |
| 137 | if(ti->getSuccessor(0) == MI->second){ |
| 138 | index = 0; |
| 139 | } |
| 140 | |
| 141 | ti->setSuccessor(index, newBB); |
| 142 | |
| 143 | for(BasicBlock::iterator BB2Inst = MI->second->begin(), |
| 144 | BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){ |
| 145 | |
| 146 | if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){ |
| 147 | int bbIndex; |
| 148 | bbIndex = phiInst->getBasicBlockIndex(*VBI); |
| 149 | if(bbIndex>=0){ |
| 150 | phiMap[phiInst].push_back(bbIndex); |
| 151 | //phiInst->setIncomingBlock(bbIndex, newBB); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | for(std::map<PHINode *, std::vector<unsigned int> >::iterator |
| 158 | PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){ |
| 159 | |
| 160 | PHINode *phiNode = new PHINode(PI->first->getType(), "phi", newBranch); |
| 161 | for(std::vector<unsigned int>::iterator II = PI->second.begin(), |
| 162 | IE = PI->second.end(); II != IE; ++II){ |
| 163 | phiNode->addIncoming(PI->first->getIncomingValue(*II), |
| 164 | PI->first->getIncomingBlock(*II)); |
| 165 | } |
| 166 | |
| 167 | std::vector<BasicBlock *> tempBB; |
| 168 | for(std::vector<unsigned int>::iterator II = PI->second.begin(), |
| 169 | IE = PI->second.end(); II != IE; ++II){ |
| 170 | tempBB.push_back(PI->first->getIncomingBlock(*II)); |
| 171 | } |
| 172 | |
| 173 | for(std::vector<BasicBlock *>::iterator II = tempBB.begin(), |
| 174 | IE = tempBB.end(); II != IE; ++II){ |
| 175 | PI->first->removeIncomingValue(*II); |
| 176 | } |
| 177 | |
| 178 | PI->first->addIncoming(phiNode, newBB); |
| 179 | } |
| 180 | //std::cerr<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"; |
| 181 | //std::cerr<<MI->second; |
| 182 | //std::cerr<<"-----------------------------------\n"; |
| 183 | //std::cerr<<newBB; |
| 184 | //std::cerr<<"END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"; |
| 185 | |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | //getting the backedges in a graph |
| 191 | //Its a variation of DFS to get the backedges in the graph |
| 192 | //We get back edges by associating a time |
| 193 | //and a color with each vertex. |
| 194 | //The time of a vertex is the time when it was first visited |
| 195 | //The color of a vertex is initially WHITE, |
| 196 | //Changes to GREY when it is first visited, |
| 197 | //and changes to BLACK when ALL its neighbors |
| 198 | //have been visited |
| 199 | //So we have a back edge when we meet a successor of |
| 200 | //a node with smaller time, and GREY color |
| 201 | void CombineBranches::getBackEdges(Function &F){ |
| 202 | std::map<BasicBlock *, Color > color; |
| 203 | std::map<BasicBlock *, int> d; |
| 204 | std::map<BasicBlock *, BasicBlock *> be; |
| 205 | int time=0; |
| 206 | getBackEdgesVisit(F.begin(), color, d, time, be); |
| 207 | |
| 208 | removeRedundant(be); |
| 209 | } |
| 210 | |
| 211 | //Per function pass for inserting counters and call function |
| 212 | bool CombineBranches::runOnFunction(Function &F){ |
| 213 | |
| 214 | if(F.isExternal()) { |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | //if(F.getName() == "main"){ |
| 219 | // F.setName("llvm_gprof_main"); |
| 220 | //} |
| 221 | |
| 222 | //std::cerr<<F; |
| 223 | //std::cerr<<"///////////////////////////////////////////////\n"; |
| 224 | getBackEdges(F); |
| 225 | |
| 226 | return true; |
| 227 | } |