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