blob: 16f98e7d08a6df5ae958f60a55e7742d72e07281 [file] [log] [blame]
Brian Gaeke6181cac2003-09-05 19:43:19 +00001//===-- CombineBranch.cpp ------------------------------------ ---*- C++ -*--=//
Anand Shukla89233e12003-07-18 16:08:32 +00002// Pass to instrument loops
3//
4// At every backedge, insert a counter for that backedge and a call function
5//===----------------------------------------------------------------------===//
6
Anand Shukla89233e12003-07-18 16:08:32 +00007#include "llvm/Analysis/Dominators.h"
8#include "llvm/Support/CFG.h"
9#include "llvm/Constants.h"
10#include "llvm/iMemory.h"
11#include "llvm/GlobalVariable.h"
12#include "llvm/DerivedTypes.h"
13#include "llvm/iOther.h"
14#include "llvm/iOperators.h"
15#include "llvm/iTerminators.h"
16#include "llvm/iPHINode.h"
17#include "llvm/Module.h"
18#include "llvm/Function.h"
19#include "llvm/Pass.h"
20
21//this is used to color vertices
22//during DFS
23
24enum Color{
25 WHITE,
26 GREY,
27 BLACK
28};
29
30namespace{
31 struct CombineBranches : public FunctionPass {
32 private:
33 //DominatorSet *DS;
34 void getBackEdgesVisit(BasicBlock *u,
35 std::map<BasicBlock *, Color > &color,
36 std::map<BasicBlock *, int > &d,
37 int &time,
38 std::map<BasicBlock *, BasicBlock *> &be);
39 void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be);
40 void getBackEdges(Function &F);
41 public:
42 bool runOnFunction(Function &F);
43 };
44
45 RegisterOpt<CombineBranches> X("branch-combine", "Multiple backedges going to same target are merged");
46}
47
Anand Shukla89233e12003-07-18 16:08:32 +000048//helper function to get back edges: it is called by
49//the "getBackEdges" function below
50void CombineBranches::getBackEdgesVisit(BasicBlock *u,
51 std::map<BasicBlock *, Color > &color,
52 std::map<BasicBlock *, int > &d,
53 int &time,
54 std::map<BasicBlock *, BasicBlock *> &be) {
55
56 color[u]=GREY;
57 time++;
58 d[u]=time;
59
Chris Lattnera9400952003-09-24 22:06:25 +000060 for (succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
Anand Shukla89233e12003-07-18 16:08:32 +000061
62 BasicBlock *BB = *vl;
63
64 if(color[BB]!=GREY && color[BB]!=BLACK){
65 getBackEdgesVisit(BB, color, d, time, be);
66 }
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
71 if(d[u] >= d[BB]){
72 //u->BB is a backedge
73 be[u] = BB;
74 }
75 }
76 }
77 color[u]=BLACK;//done with visiting the node and its neighbors
78}
79
80//look at all BEs, and remove all BEs that are dominated by other BE's in the
81//set
82void 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());
114 BranchInst *newBranch = new BranchInst(MI->second);
115
116 newBB->getInstList().push_back(newBranch);
117
118 std::map<PHINode *, std::vector<unsigned int> > phiMap;
119
120
121 for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(),
122 VBE = sameTarget.end(); VBI != VBE; ++VBI){
123
124 //std::cerr<<(*VBI)->getName()<<"\n";
125
126 BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator());
127 unsigned char index = 1;
128 if(ti->getSuccessor(0) == MI->second){
129 index = 0;
130 }
131
132 ti->setSuccessor(index, newBB);
133
134 for(BasicBlock::iterator BB2Inst = MI->second->begin(),
135 BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){
136
137 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
138 int bbIndex;
139 bbIndex = phiInst->getBasicBlockIndex(*VBI);
140 if(bbIndex>=0){
141 phiMap[phiInst].push_back(bbIndex);
142 //phiInst->setIncomingBlock(bbIndex, newBB);
143 }
144 }
145 }
146 }
147
148 for(std::map<PHINode *, std::vector<unsigned int> >::iterator
149 PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){
150
151 PHINode *phiNode = new PHINode(PI->first->getType(), "phi", newBranch);
152 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
153 IE = PI->second.end(); II != IE; ++II){
154 phiNode->addIncoming(PI->first->getIncomingValue(*II),
155 PI->first->getIncomingBlock(*II));
156 }
157
158 std::vector<BasicBlock *> tempBB;
159 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
160 IE = PI->second.end(); II != IE; ++II){
161 tempBB.push_back(PI->first->getIncomingBlock(*II));
162 }
163
164 for(std::vector<BasicBlock *>::iterator II = tempBB.begin(),
165 IE = tempBB.end(); II != IE; ++II){
166 PI->first->removeIncomingValue(*II);
167 }
168
169 PI->first->addIncoming(phiNode, newBB);
170 }
171 //std::cerr<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
172 //std::cerr<<MI->second;
173 //std::cerr<<"-----------------------------------\n";
174 //std::cerr<<newBB;
175 //std::cerr<<"END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
176
177 }
178 }
179}
180
181//getting the backedges in a graph
182//Its a variation of DFS to get the backedges in the graph
183//We get back edges by associating a time
184//and a color with each vertex.
185//The time of a vertex is the time when it was first visited
186//The color of a vertex is initially WHITE,
187//Changes to GREY when it is first visited,
188//and changes to BLACK when ALL its neighbors
189//have been visited
190//So we have a back edge when we meet a successor of
191//a node with smaller time, and GREY color
192void CombineBranches::getBackEdges(Function &F){
193 std::map<BasicBlock *, Color > color;
194 std::map<BasicBlock *, int> d;
195 std::map<BasicBlock *, BasicBlock *> be;
196 int time=0;
197 getBackEdgesVisit(F.begin(), color, d, time, be);
198
199 removeRedundant(be);
200}
201
202//Per function pass for inserting counters and call function
203bool CombineBranches::runOnFunction(Function &F){
204
205 if(F.isExternal()) {
206 return false;
207 }
208
209 //if(F.getName() == "main"){
210 // F.setName("llvm_gprof_main");
211 //}
212
213 //std::cerr<<F;
214 //std::cerr<<"///////////////////////////////////////////////\n";
215 getBackEdges(F);
216
217 return true;
218}