1 //===-- CombineBranch.cpp -------------------------------------------------===//
3 // Pass to instrument loops
5 // At every backedge, insert a counter for that backedge and a call function
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Analysis/Dominators.h"
10 #include "llvm/Support/CFG.h"
11 #include "llvm/Constants.h"
12 #include "llvm/iMemory.h"
13 #include "llvm/GlobalVariable.h"
14 #include "llvm/DerivedTypes.h"
15 #include "llvm/iOther.h"
16 #include "llvm/iOperators.h"
17 #include "llvm/iTerminators.h"
18 #include "llvm/iPHINode.h"
19 #include "llvm/Module.h"
20 #include "llvm/Function.h"
21 #include "llvm/Pass.h"
23 //this is used to color vertices
33 struct CombineBranches : public FunctionPass {
36 void getBackEdgesVisit(BasicBlock *u,
37 std::map<BasicBlock *, Color > &color,
38 std::map<BasicBlock *, int > &d,
40 std::map<BasicBlock *, BasicBlock *> &be);
41 void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be);
42 void getBackEdges(Function &F);
44 bool runOnFunction(Function &F);
47 RegisterOpt<CombineBranches> X("branch-combine", "Multiple backedges going to same target are merged");
50 //helper function to get back edges: it is called by
51 //the "getBackEdges" function below
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){
66 if(color[BB]!=GREY && color[BB]!=BLACK){
67 getBackEdgesVisit(BB, color, d, time, be);
70 //now checking for d and f vals
71 else if(color[BB]==GREY){
72 //so v is ancestor of u if time of u > time of v
79 color[u]=BLACK;//done with visiting the node and its neighbors
82 //look at all BEs, and remove all BEs that are dominated by other BE's in the
84 void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){
85 std::vector<BasicBlock *> toDelete;
86 std::map<BasicBlock *, int> seenBB;
88 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
89 ME = be.end(); MI != ME; ++MI){
91 if(seenBB[MI->second])
94 seenBB[MI->second] = 1;
96 std::vector<BasicBlock *> sameTarget;
99 for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(),
100 MME = be.end(); MMI != MME; ++MMI){
102 if(MMI->first == MI->first)
105 if(MMI->second == MI->second)
106 sameTarget.push_back(MMI->first);
110 //so more than one branch to same target
111 if(sameTarget.size()){
113 sameTarget.push_back(MI->first);
115 BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent());
116 BranchInst *newBranch = new BranchInst(MI->second);
118 newBB->getInstList().push_back(newBranch);
120 std::map<PHINode *, std::vector<unsigned int> > phiMap;
123 for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(),
124 VBE = sameTarget.end(); VBI != VBE; ++VBI){
126 //std::cerr<<(*VBI)->getName()<<"\n";
128 BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator());
129 unsigned char index = 1;
130 if(ti->getSuccessor(0) == MI->second){
134 ti->setSuccessor(index, newBB);
136 for(BasicBlock::iterator BB2Inst = MI->second->begin(),
137 BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){
139 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
141 bbIndex = phiInst->getBasicBlockIndex(*VBI);
143 phiMap[phiInst].push_back(bbIndex);
144 //phiInst->setIncomingBlock(bbIndex, newBB);
150 for(std::map<PHINode *, std::vector<unsigned int> >::iterator
151 PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){
153 PHINode *phiNode = new PHINode(PI->first->getType(), "phi", newBranch);
154 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
155 IE = PI->second.end(); II != IE; ++II){
156 phiNode->addIncoming(PI->first->getIncomingValue(*II),
157 PI->first->getIncomingBlock(*II));
160 std::vector<BasicBlock *> tempBB;
161 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
162 IE = PI->second.end(); II != IE; ++II){
163 tempBB.push_back(PI->first->getIncomingBlock(*II));
166 for(std::vector<BasicBlock *>::iterator II = tempBB.begin(),
167 IE = tempBB.end(); II != IE; ++II){
168 PI->first->removeIncomingValue(*II);
171 PI->first->addIncoming(phiNode, newBB);
173 //std::cerr<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
174 //std::cerr<<MI->second;
175 //std::cerr<<"-----------------------------------\n";
177 //std::cerr<<"END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
183 //getting the backedges in a graph
184 //Its a variation of DFS to get the backedges in the graph
185 //We get back edges by associating a time
186 //and a color with each vertex.
187 //The time of a vertex is the time when it was first visited
188 //The color of a vertex is initially WHITE,
189 //Changes to GREY when it is first visited,
190 //and changes to BLACK when ALL its neighbors
192 //So we have a back edge when we meet a successor of
193 //a node with smaller time, and GREY color
194 void CombineBranches::getBackEdges(Function &F){
195 std::map<BasicBlock *, Color > color;
196 std::map<BasicBlock *, int> d;
197 std::map<BasicBlock *, BasicBlock *> be;
199 getBackEdgesVisit(F.begin(), color, d, time, be);
204 //Per function pass for inserting counters and call function
205 bool CombineBranches::runOnFunction(Function &F){
211 //if(F.getName() == "main"){
212 // F.setName("llvm_gprof_main");
216 //std::cerr<<"///////////////////////////////////////////////\n";