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 // Pass to instrument loops
12 // At every backedge, insert a counter for that backedge and a call function
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Analysis/Dominators.h"
17 #include "llvm/Support/CFG.h"
18 #include "llvm/Constants.h"
19 #include "llvm/iMemory.h"
20 #include "llvm/GlobalVariable.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/iOther.h"
23 #include "llvm/iOperators.h"
24 #include "llvm/iTerminators.h"
25 #include "llvm/iPHINode.h"
26 #include "llvm/Module.h"
27 #include "llvm/Function.h"
28 #include "llvm/Pass.h"
32 //this is used to color vertices
42 struct CombineBranches : public FunctionPass {
45 void getBackEdgesVisit(BasicBlock *u,
46 std::map<BasicBlock *, Color > &color,
47 std::map<BasicBlock *, int > &d,
49 std::map<BasicBlock *, BasicBlock *> &be);
50 void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be);
51 void getBackEdges(Function &F);
53 bool runOnFunction(Function &F);
56 RegisterOpt<CombineBranches> X("branch-combine", "Multiple backedges going to same target are merged");
59 //helper function to get back edges: it is called by
60 //the "getBackEdges" function below
61 void CombineBranches::getBackEdgesVisit(BasicBlock *u,
62 std::map<BasicBlock *, Color > &color,
63 std::map<BasicBlock *, int > &d,
65 std::map<BasicBlock *, BasicBlock *> &be) {
71 for (succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
75 if(color[BB]!=GREY && color[BB]!=BLACK){
76 getBackEdgesVisit(BB, color, d, time, be);
79 //now checking for d and f vals
80 else if(color[BB]==GREY){
81 //so v is ancestor of u if time of u > time of v
88 color[u]=BLACK;//done with visiting the node and its neighbors
91 //look at all BEs, and remove all BEs that are dominated by other BE's in the
93 void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){
94 std::vector<BasicBlock *> toDelete;
95 std::map<BasicBlock *, int> seenBB;
97 for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
98 ME = be.end(); MI != ME; ++MI){
100 if(seenBB[MI->second])
103 seenBB[MI->second] = 1;
105 std::vector<BasicBlock *> sameTarget;
108 for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(),
109 MME = be.end(); MMI != MME; ++MMI){
111 if(MMI->first == MI->first)
114 if(MMI->second == MI->second)
115 sameTarget.push_back(MMI->first);
119 //so more than one branch to same target
120 if(sameTarget.size()){
122 sameTarget.push_back(MI->first);
124 BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent());
125 BranchInst *newBranch = new BranchInst(MI->second, 0, 0, newBB);
127 std::map<PHINode *, std::vector<unsigned int> > phiMap;
130 for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(),
131 VBE = sameTarget.end(); VBI != VBE; ++VBI){
133 //std::cerr<<(*VBI)->getName()<<"\n";
135 BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator());
136 unsigned char index = 1;
137 if(ti->getSuccessor(0) == MI->second){
141 ti->setSuccessor(index, newBB);
143 for(BasicBlock::iterator BB2Inst = MI->second->begin(),
144 BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){
146 if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
148 bbIndex = phiInst->getBasicBlockIndex(*VBI);
150 phiMap[phiInst].push_back(bbIndex);
151 //phiInst->setIncomingBlock(bbIndex, newBB);
157 for(std::map<PHINode *, std::vector<unsigned int> >::iterator
158 PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){
160 PHINode *phiNode = new PHINode(PI->first->getType(), "phi", newBranch);
161 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
162 IE = PI->second.end(); II != IE; ++II){
163 phiNode->addIncoming(PI->first->getIncomingValue(*II),
164 PI->first->getIncomingBlock(*II));
167 std::vector<BasicBlock *> tempBB;
168 for(std::vector<unsigned int>::iterator II = PI->second.begin(),
169 IE = PI->second.end(); II != IE; ++II){
170 tempBB.push_back(PI->first->getIncomingBlock(*II));
173 for(std::vector<BasicBlock *>::iterator II = tempBB.begin(),
174 IE = tempBB.end(); II != IE; ++II){
175 PI->first->removeIncomingValue(*II);
178 PI->first->addIncoming(phiNode, newBB);
180 //std::cerr<<"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
181 //std::cerr<<MI->second;
182 //std::cerr<<"-----------------------------------\n";
184 //std::cerr<<"END%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n";
190 //getting the backedges in a graph
191 //Its a variation of DFS to get the backedges in the graph
192 //We get back edges by associating a time
193 //and a color with each vertex.
194 //The time of a vertex is the time when it was first visited
195 //The color of a vertex is initially WHITE,
196 //Changes to GREY when it is first visited,
197 //and changes to BLACK when ALL its neighbors
199 //So we have a back edge when we meet a successor of
200 //a node with smaller time, and GREY color
201 void CombineBranches::getBackEdges(Function &F){
202 std::map<BasicBlock *, Color > color;
203 std::map<BasicBlock *, int> d;
204 std::map<BasicBlock *, BasicBlock *> be;
206 getBackEdgesVisit(F.begin(), color, d, time, be);
211 //Per function pass for inserting counters and call function
212 bool CombineBranches::runOnFunction(Function &F){
218 //if(F.getName() == "main"){
219 // F.setName("llvm_gprof_main");
223 //std::cerr<<"///////////////////////////////////////////////\n";
229 } // End llvm namespace