1 //===-- CombineBranch.cpp -------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // Combine multiple back-edges going to the same sink into a single
11 // back-edge. This introduces a new basic block and back-edge branch for each
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Support/CFG.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/Function.h"
19 #include "llvm/Pass.h"
24 struct CombineBranches : public FunctionPass {
26 /// Possible colors that a vertex can have during depth-first search for
29 enum Color { WHITE, GREY, BLACK };
31 void getBackEdgesVisit(BasicBlock *u,
32 std::map<BasicBlock *, Color > &color,
33 std::map<BasicBlock *, int > &d,
35 std::map<BasicBlock *, BasicBlock *> &be);
36 void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be);
38 bool runOnFunction(Function &F);
41 RegisterOpt<CombineBranches>
42 X("branch-combine", "Multiple backedges going to same target are merged");
45 /// getBackEdgesVisit - Get the back-edges of the control-flow graph for this
46 /// function. We proceed recursively using depth-first search. We get
47 /// back-edges by associating a time and a color with each vertex. The time of a
48 /// vertex is the time when it was first visited. The color of a vertex is
49 /// initially WHITE, changes to GREY when it is first visited, and changes to
50 /// BLACK when ALL its neighbors have been visited. So we have a back edge when
51 /// we meet a successor of a node with smaller time, and GREY color.
53 void CombineBranches::getBackEdgesVisit(BasicBlock *u,
54 std::map<BasicBlock *, Color > &color,
55 std::map<BasicBlock *, int > &d,
57 std::map<BasicBlock *, BasicBlock *> &be) {
63 for (succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
66 if(color[BB]!=GREY && color[BB]!=BLACK)
67 getBackEdgesVisit(BB, color, d, time, be);
69 //now checking for d and f vals
70 else if(color[BB]==GREY){
71 //so v is ancestor of u if time of u > time of v
72 if(d[u] >= d[BB]) // u->BB is a backedge
76 color[u]=BLACK;//done with visiting the node and its neighbors
79 /// removeRedundant - Remove all back-edges that are dominated by other
80 /// back-edges in the set.
82 void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){
83 std::vector<BasicBlock *> toDelete;
84 std::map<BasicBlock *, int> seenBB;
86 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
87 ME = be.end(); MI != ME; ++MI){
89 if(seenBB[MI->second])
92 seenBB[MI->second] = 1;
94 std::vector<BasicBlock *> sameTarget;
97 for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(),
98 MME = be.end(); MMI != MME; ++MMI){
100 if(MMI->first == MI->first)
103 if(MMI->second == MI->second)
104 sameTarget.push_back(MMI->first);
108 //so more than one branch to same target
109 if(sameTarget.size()){
111 sameTarget.push_back(MI->first);
113 BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent());
114 BranchInst *newBranch = new BranchInst(MI->second, newBB);
116 std::map<PHINode *, std::vector<unsigned int> > phiMap;
118 for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(),
119 VBE = sameTarget.end(); VBI != VBE; ++VBI){
121 BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator());
122 unsigned char index = 1;
123 if(ti->getSuccessor(0) == MI->second)
126 ti->setSuccessor(index, newBB);
128 for(BasicBlock::iterator BB2Inst = MI->second->begin(),
129 BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){
131 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
133 bbIndex = phiInst->getBasicBlockIndex(*VBI);
135 phiMap[phiInst].push_back(bbIndex);
140 for(std::map<PHINode *, std::vector<unsigned int> >::iterator
141 PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){
143 PHINode *phiNode = new PHINode(PI->first->getType(), "phi", newBranch);
144 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
145 IE = PI->second.end(); II != IE; ++II){
146 phiNode->addIncoming(PI->first->getIncomingValue(*II),
147 PI->first->getIncomingBlock(*II));
150 std::vector<BasicBlock *> tempBB;
151 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
152 IE = PI->second.end(); II != IE; ++II){
153 tempBB.push_back(PI->first->getIncomingBlock(*II));
156 for(std::vector<BasicBlock *>::iterator II = tempBB.begin(),
157 IE = tempBB.end(); II != IE; ++II){
158 PI->first->removeIncomingValue(*II);
161 PI->first->addIncoming(phiNode, newBB);
167 /// runOnFunction - Per function pass for combining branches.
169 bool CombineBranches::runOnFunction(Function &F){
173 // Find and remove "redundant" back-edges.
174 std::map<BasicBlock *, Color> color;
175 std::map<BasicBlock *, int> d;
176 std::map<BasicBlock *, BasicBlock *> be;
178 getBackEdgesVisit (F.begin (), color, d, time, be);
179 removeRedundant (be);
181 return true; // FIXME: assumes a modification was always made.
184 FunctionPass *createCombineBranchesPass () {
185 return new CombineBranches();
188 } // End llvm namespace