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 | // |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 10 | // Combine branches |
Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 11 | // |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 14 | #include "llvm/Support/CFG.h" |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 15 | #include "llvm/iTerminators.h" |
| 16 | #include "llvm/iPHINode.h" |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 17 | #include "llvm/Function.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 22 | namespace { |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 23 | struct CombineBranches : public FunctionPass { |
| 24 | private: |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 25 | /// Possible colors that a vertex can have during depth-first search for |
| 26 | /// back-edges. |
| 27 | /// |
| 28 | enum Color { WHITE, GREY, BLACK }; |
| 29 | |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 30 | void getBackEdgesVisit(BasicBlock *u, |
| 31 | std::map<BasicBlock *, Color > &color, |
| 32 | std::map<BasicBlock *, int > &d, |
| 33 | int &time, |
| 34 | std::map<BasicBlock *, BasicBlock *> &be); |
| 35 | void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be); |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 36 | public: |
| 37 | bool runOnFunction(Function &F); |
| 38 | }; |
| 39 | |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 40 | RegisterOpt<CombineBranches> |
| 41 | X("branch-combine", "Multiple backedges going to same target are merged"); |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 44 | /// getBackEdgesVisit - Get the back-edges of the control-flow graph for this |
| 45 | /// function. We proceed recursively using depth-first search. We get |
| 46 | /// back-edges by associating a time and a color with each vertex. The time of a |
| 47 | /// vertex is the time when it was first visited. The color of a vertex is |
| 48 | /// initially WHITE, changes to GREY when it is first visited, and changes to |
| 49 | /// BLACK when ALL its neighbors have been visited. So we have a back edge when |
| 50 | /// we meet a successor of a node with smaller time, and GREY color. |
| 51 | /// |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 52 | void CombineBranches::getBackEdgesVisit(BasicBlock *u, |
| 53 | std::map<BasicBlock *, Color > &color, |
| 54 | std::map<BasicBlock *, int > &d, |
| 55 | int &time, |
| 56 | std::map<BasicBlock *, BasicBlock *> &be) { |
| 57 | |
| 58 | color[u]=GREY; |
| 59 | time++; |
| 60 | d[u]=time; |
| 61 | |
Chris Lattner | a940095 | 2003-09-24 22:06:25 +0000 | [diff] [blame] | 62 | 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] | 63 | BasicBlock *BB = *vl; |
| 64 | |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 65 | if(color[BB]!=GREY && color[BB]!=BLACK) |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 66 | getBackEdgesVisit(BB, color, d, time, be); |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 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 |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 71 | if(d[u] >= d[BB]) // u->BB is a backedge |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 72 | be[u] = BB; |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | color[u]=BLACK;//done with visiting the node and its neighbors |
| 76 | } |
| 77 | |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 78 | /// removeRedundant - Remove all back-edges that are dominated by other |
| 79 | /// back-edges in the set. |
| 80 | /// |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 81 | void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){ |
| 82 | std::vector<BasicBlock *> toDelete; |
| 83 | std::map<BasicBlock *, int> seenBB; |
| 84 | |
| 85 | for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(), |
| 86 | ME = be.end(); MI != ME; ++MI){ |
| 87 | |
| 88 | if(seenBB[MI->second]) |
| 89 | continue; |
| 90 | |
| 91 | seenBB[MI->second] = 1; |
| 92 | |
| 93 | std::vector<BasicBlock *> sameTarget; |
| 94 | sameTarget.clear(); |
| 95 | |
| 96 | for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(), |
| 97 | MME = be.end(); MMI != MME; ++MMI){ |
| 98 | |
| 99 | if(MMI->first == MI->first) |
| 100 | continue; |
| 101 | |
| 102 | if(MMI->second == MI->second) |
| 103 | sameTarget.push_back(MMI->first); |
| 104 | |
| 105 | } |
| 106 | |
| 107 | //so more than one branch to same target |
| 108 | if(sameTarget.size()){ |
| 109 | |
| 110 | sameTarget.push_back(MI->first); |
| 111 | |
| 112 | BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent()); |
Brian Gaeke | c0b9b83 | 2004-06-01 20:06:10 +0000 | [diff] [blame^] | 113 | BranchInst *newBranch = new BranchInst(MI->second, newBB); |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 114 | |
| 115 | std::map<PHINode *, std::vector<unsigned int> > phiMap; |
| 116 | |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 117 | for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(), |
| 118 | VBE = sameTarget.end(); VBI != VBE; ++VBI){ |
| 119 | |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 120 | BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator()); |
| 121 | unsigned char index = 1; |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 122 | if(ti->getSuccessor(0) == MI->second) |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 123 | index = 0; |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 124 | |
| 125 | ti->setSuccessor(index, newBB); |
| 126 | |
| 127 | for(BasicBlock::iterator BB2Inst = MI->second->begin(), |
| 128 | BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){ |
| 129 | |
| 130 | if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){ |
| 131 | int bbIndex; |
| 132 | bbIndex = phiInst->getBasicBlockIndex(*VBI); |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 133 | if(bbIndex>=0) |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 134 | phiMap[phiInst].push_back(bbIndex); |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | for(std::map<PHINode *, std::vector<unsigned int> >::iterator |
| 140 | PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){ |
| 141 | |
| 142 | PHINode *phiNode = new PHINode(PI->first->getType(), "phi", newBranch); |
| 143 | for(std::vector<unsigned int>::iterator II = PI->second.begin(), |
| 144 | IE = PI->second.end(); II != IE; ++II){ |
| 145 | phiNode->addIncoming(PI->first->getIncomingValue(*II), |
| 146 | PI->first->getIncomingBlock(*II)); |
| 147 | } |
| 148 | |
| 149 | std::vector<BasicBlock *> tempBB; |
| 150 | for(std::vector<unsigned int>::iterator II = PI->second.begin(), |
| 151 | IE = PI->second.end(); II != IE; ++II){ |
| 152 | tempBB.push_back(PI->first->getIncomingBlock(*II)); |
| 153 | } |
| 154 | |
| 155 | for(std::vector<BasicBlock *>::iterator II = tempBB.begin(), |
| 156 | IE = tempBB.end(); II != IE; ++II){ |
| 157 | PI->first->removeIncomingValue(*II); |
| 158 | } |
| 159 | |
| 160 | PI->first->addIncoming(phiNode, newBB); |
| 161 | } |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 166 | /// runOnFunction - Per function pass for combining branches. |
| 167 | /// |
| 168 | bool CombineBranches::runOnFunction(Function &F){ |
| 169 | if (F.isExternal ()) |
| 170 | return false; |
| 171 | |
| 172 | // Find and remove "redundant" back-edges. |
| 173 | std::map<BasicBlock *, Color> color; |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 174 | std::map<BasicBlock *, int> d; |
| 175 | std::map<BasicBlock *, BasicBlock *> be; |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 176 | int time = 0; |
| 177 | getBackEdgesVisit (F.begin (), color, d, time, be); |
| 178 | removeRedundant (be); |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 179 | |
Brian Gaeke | ef327be | 2004-03-30 19:53:46 +0000 | [diff] [blame] | 180 | return true; // FIXME: assumes a modification was always made. |
Anand Shukla | 89233e1 | 2003-07-18 16:08:32 +0000 | [diff] [blame] | 181 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 182 | |
| 183 | } // End llvm namespace |