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 //===----------------------------------------------------------------------===//
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/CFG.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/iPHINode.h"
17 #include "llvm/Function.h"
18 #include "llvm/Pass.h"
23 struct CombineBranches : public FunctionPass {
25 /// Possible colors that a vertex can have during depth-first search for
28 enum Color { WHITE, GREY, BLACK };
30 void getBackEdgesVisit(BasicBlock *u,
31 std::map<BasicBlock *, Color > &color,
32 std::map<BasicBlock *, int > &d,
34 std::map<BasicBlock *, BasicBlock *> &be);
35 void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be);
37 bool runOnFunction(Function &F);
40 RegisterOpt<CombineBranches>
41 X("branch-combine", "Multiple backedges going to same target are merged");
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.
52 void CombineBranches::getBackEdgesVisit(BasicBlock *u,
53 std::map<BasicBlock *, Color > &color,
54 std::map<BasicBlock *, int > &d,
56 std::map<BasicBlock *, BasicBlock *> &be) {
62 for (succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
65 if(color[BB]!=GREY && color[BB]!=BLACK)
66 getBackEdgesVisit(BB, color, d, time, be);
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
71 if(d[u] >= d[BB]) // u->BB is a backedge
75 color[u]=BLACK;//done with visiting the node and its neighbors
78 /// removeRedundant - Remove all back-edges that are dominated by other
79 /// back-edges in the set.
81 void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){
82 std::vector<BasicBlock *> toDelete;
83 std::map<BasicBlock *, int> seenBB;
85 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
86 ME = be.end(); MI != ME; ++MI){
88 if(seenBB[MI->second])
91 seenBB[MI->second] = 1;
93 std::vector<BasicBlock *> sameTarget;
96 for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(),
97 MME = be.end(); MMI != MME; ++MMI){
99 if(MMI->first == MI->first)
102 if(MMI->second == MI->second)
103 sameTarget.push_back(MMI->first);
107 //so more than one branch to same target
108 if(sameTarget.size()){
110 sameTarget.push_back(MI->first);
112 BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent());
113 BranchInst *newBranch = new BranchInst(MI->second, 0, 0, newBB);
115 std::map<PHINode *, std::vector<unsigned int> > phiMap;
117 for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(),
118 VBE = sameTarget.end(); VBI != VBE; ++VBI){
120 BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator());
121 unsigned char index = 1;
122 if(ti->getSuccessor(0) == MI->second)
125 ti->setSuccessor(index, newBB);
127 for(BasicBlock::iterator BB2Inst = MI->second->begin(),
128 BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){
130 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
132 bbIndex = phiInst->getBasicBlockIndex(*VBI);
134 phiMap[phiInst].push_back(bbIndex);
139 for(std::map<PHINode *, std::vector<unsigned int> >::iterator
140 PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){
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));
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));
155 for(std::vector<BasicBlock *>::iterator II = tempBB.begin(),
156 IE = tempBB.end(); II != IE; ++II){
157 PI->first->removeIncomingValue(*II);
160 PI->first->addIncoming(phiNode, newBB);
166 /// runOnFunction - Per function pass for combining branches.
168 bool CombineBranches::runOnFunction(Function &F){
172 // Find and remove "redundant" back-edges.
173 std::map<BasicBlock *, Color> color;
174 std::map<BasicBlock *, int> d;
175 std::map<BasicBlock *, BasicBlock *> be;
177 getBackEdgesVisit (F.begin (), color, d, time, be);
178 removeRedundant (be);
180 return true; // FIXME: assumes a modification was always made.
183 } // End llvm namespace