blob: fe2839b3251244da81deaa6c9a3caa4fae8799d2 [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===-- CombineBranch.cpp -------------------------------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
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 Lattner44d2c352003-10-13 03:32:08 +00009//
Brian Gaekea9c57792004-06-03 05:03:02 +000010// 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
12// such sink.
Chris Lattner44d2c352003-10-13 03:32:08 +000013//
Anand Shukla89233e12003-07-18 16:08:32 +000014//===----------------------------------------------------------------------===//
15
Anand Shukla89233e12003-07-18 16:08:32 +000016#include "llvm/Support/CFG.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000017#include "llvm/Instructions.h"
Anand Shukla89233e12003-07-18 16:08:32 +000018#include "llvm/Function.h"
19#include "llvm/Pass.h"
20
Brian Gaeke960707c2003-11-11 22:41:34 +000021namespace llvm {
22
Brian Gaeke960707c2003-11-11 22:41:34 +000023namespace {
Anand Shukla89233e12003-07-18 16:08:32 +000024 struct CombineBranches : public FunctionPass {
25 private:
Brian Gaekeef327be2004-03-30 19:53:46 +000026 /// Possible colors that a vertex can have during depth-first search for
27 /// back-edges.
28 ///
29 enum Color { WHITE, GREY, BLACK };
30
Anand Shukla89233e12003-07-18 16:08:32 +000031 void getBackEdgesVisit(BasicBlock *u,
32 std::map<BasicBlock *, Color > &color,
33 std::map<BasicBlock *, int > &d,
34 int &time,
35 std::map<BasicBlock *, BasicBlock *> &be);
36 void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be);
Anand Shukla89233e12003-07-18 16:08:32 +000037 public:
38 bool runOnFunction(Function &F);
39 };
40
Brian Gaekeef327be2004-03-30 19:53:46 +000041 RegisterOpt<CombineBranches>
42 X("branch-combine", "Multiple backedges going to same target are merged");
Anand Shukla89233e12003-07-18 16:08:32 +000043}
44
Brian Gaekeef327be2004-03-30 19:53:46 +000045/// 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.
52///
Anand Shukla89233e12003-07-18 16:08:32 +000053void CombineBranches::getBackEdgesVisit(BasicBlock *u,
54 std::map<BasicBlock *, Color > &color,
55 std::map<BasicBlock *, int > &d,
56 int &time,
57 std::map<BasicBlock *, BasicBlock *> &be) {
58
59 color[u]=GREY;
60 time++;
61 d[u]=time;
62
Chris Lattnera9400952003-09-24 22:06:25 +000063 for (succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
Anand Shukla89233e12003-07-18 16:08:32 +000064 BasicBlock *BB = *vl;
65
Brian Gaekeef327be2004-03-30 19:53:46 +000066 if(color[BB]!=GREY && color[BB]!=BLACK)
Anand Shukla89233e12003-07-18 16:08:32 +000067 getBackEdgesVisit(BB, color, d, time, be);
Anand Shukla89233e12003-07-18 16:08:32 +000068
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
Brian Gaekeef327be2004-03-30 19:53:46 +000072 if(d[u] >= d[BB]) // u->BB is a backedge
Anand Shukla89233e12003-07-18 16:08:32 +000073 be[u] = BB;
Anand Shukla89233e12003-07-18 16:08:32 +000074 }
75 }
76 color[u]=BLACK;//done with visiting the node and its neighbors
77}
78
Brian Gaekeef327be2004-03-30 19:53:46 +000079/// removeRedundant - Remove all back-edges that are dominated by other
80/// back-edges in the set.
81///
Anand Shukla89233e12003-07-18 16:08:32 +000082void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){
83 std::vector<BasicBlock *> toDelete;
84 std::map<BasicBlock *, int> seenBB;
85
86 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
87 ME = be.end(); MI != ME; ++MI){
88
89 if(seenBB[MI->second])
90 continue;
91
92 seenBB[MI->second] = 1;
93
94 std::vector<BasicBlock *> sameTarget;
95 sameTarget.clear();
96
97 for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(),
98 MME = be.end(); MMI != MME; ++MMI){
99
100 if(MMI->first == MI->first)
101 continue;
102
103 if(MMI->second == MI->second)
104 sameTarget.push_back(MMI->first);
105
106 }
107
108 //so more than one branch to same target
109 if(sameTarget.size()){
110
111 sameTarget.push_back(MI->first);
112
113 BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent());
Brian Gaekec0b9b832004-06-01 20:06:10 +0000114 BranchInst *newBranch = new BranchInst(MI->second, newBB);
Anand Shukla89233e12003-07-18 16:08:32 +0000115
116 std::map<PHINode *, std::vector<unsigned int> > phiMap;
117
Anand Shukla89233e12003-07-18 16:08:32 +0000118 for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(),
119 VBE = sameTarget.end(); VBI != VBE; ++VBI){
120
Anand Shukla89233e12003-07-18 16:08:32 +0000121 BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator());
122 unsigned char index = 1;
Brian Gaekeef327be2004-03-30 19:53:46 +0000123 if(ti->getSuccessor(0) == MI->second)
Anand Shukla89233e12003-07-18 16:08:32 +0000124 index = 0;
Anand Shukla89233e12003-07-18 16:08:32 +0000125
126 ti->setSuccessor(index, newBB);
127
128 for(BasicBlock::iterator BB2Inst = MI->second->begin(),
129 BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){
130
131 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
132 int bbIndex;
133 bbIndex = phiInst->getBasicBlockIndex(*VBI);
Brian Gaekeef327be2004-03-30 19:53:46 +0000134 if(bbIndex>=0)
Anand Shukla89233e12003-07-18 16:08:32 +0000135 phiMap[phiInst].push_back(bbIndex);
Anand Shukla89233e12003-07-18 16:08:32 +0000136 }
137 }
138 }
139
140 for(std::map<PHINode *, std::vector<unsigned int> >::iterator
141 PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){
142
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));
148 }
149
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));
154 }
155
156 for(std::vector<BasicBlock *>::iterator II = tempBB.begin(),
157 IE = tempBB.end(); II != IE; ++II){
158 PI->first->removeIncomingValue(*II);
159 }
160
161 PI->first->addIncoming(phiNode, newBB);
162 }
Anand Shukla89233e12003-07-18 16:08:32 +0000163 }
164 }
165}
166
Brian Gaekeef327be2004-03-30 19:53:46 +0000167/// runOnFunction - Per function pass for combining branches.
168///
169bool CombineBranches::runOnFunction(Function &F){
170 if (F.isExternal ())
171 return false;
172
173 // Find and remove "redundant" back-edges.
174 std::map<BasicBlock *, Color> color;
Anand Shukla89233e12003-07-18 16:08:32 +0000175 std::map<BasicBlock *, int> d;
176 std::map<BasicBlock *, BasicBlock *> be;
Brian Gaekeef327be2004-03-30 19:53:46 +0000177 int time = 0;
178 getBackEdgesVisit (F.begin (), color, d, time, be);
179 removeRedundant (be);
Anand Shukla89233e12003-07-18 16:08:32 +0000180
Brian Gaekeef327be2004-03-30 19:53:46 +0000181 return true; // FIXME: assumes a modification was always made.
Anand Shukla89233e12003-07-18 16:08:32 +0000182}
Brian Gaeke960707c2003-11-11 22:41:34 +0000183
184} // End llvm namespace