2003-10-13 03:32:08 +00:00
|
|
|
//===-- CombineBranch.cpp -------------------------------------------------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2003-10-13 03:32:08 +00:00
|
|
|
//
|
2004-06-03 05:03:02 +00:00
|
|
|
// Combine multiple back-edges going to the same sink into a single
|
|
|
|
// back-edge. This introduces a new basic block and back-edge branch for each
|
|
|
|
// such sink.
|
2003-10-13 03:32:08 +00:00
|
|
|
//
|
2003-07-18 16:08:32 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Support/CFG.h"
|
2004-07-29 17:30:56 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2003-07-18 16:08:32 +00:00
|
|
|
#include "llvm/Function.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-12-13 21:28:39 +00:00
|
|
|
#include "llvm/Type.h"
|
2003-07-18 16:08:32 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
namespace {
|
2003-07-18 16:08:32 +00:00
|
|
|
struct CombineBranches : public FunctionPass {
|
|
|
|
private:
|
2004-03-30 19:53:46 +00:00
|
|
|
/// Possible colors that a vertex can have during depth-first search for
|
|
|
|
/// back-edges.
|
|
|
|
///
|
|
|
|
enum Color { WHITE, GREY, BLACK };
|
|
|
|
|
2003-07-18 16:08:32 +00:00
|
|
|
void getBackEdgesVisit(BasicBlock *u,
|
|
|
|
std::map<BasicBlock *, Color > &color,
|
|
|
|
std::map<BasicBlock *, int > &d,
|
|
|
|
int &time,
|
|
|
|
std::map<BasicBlock *, BasicBlock *> &be);
|
|
|
|
void removeRedundant(std::map<BasicBlock *, BasicBlock *> &be);
|
|
|
|
public:
|
|
|
|
bool runOnFunction(Function &F);
|
|
|
|
};
|
|
|
|
|
2004-03-30 19:53:46 +00:00
|
|
|
RegisterOpt<CombineBranches>
|
|
|
|
X("branch-combine", "Multiple backedges going to same target are merged");
|
2003-07-18 16:08:32 +00:00
|
|
|
}
|
|
|
|
|
2004-03-30 19:53:46 +00:00
|
|
|
/// getBackEdgesVisit - Get the back-edges of the control-flow graph for this
|
|
|
|
/// function. We proceed recursively using depth-first search. We get
|
|
|
|
/// back-edges by associating a time and a color with each vertex. The time of a
|
|
|
|
/// vertex is the time when it was first visited. The color of a vertex is
|
|
|
|
/// initially WHITE, changes to GREY when it is first visited, and changes to
|
|
|
|
/// BLACK when ALL its neighbors have been visited. So we have a back edge when
|
|
|
|
/// we meet a successor of a node with smaller time, and GREY color.
|
|
|
|
///
|
2003-07-18 16:08:32 +00:00
|
|
|
void CombineBranches::getBackEdgesVisit(BasicBlock *u,
|
|
|
|
std::map<BasicBlock *, Color > &color,
|
|
|
|
std::map<BasicBlock *, int > &d,
|
|
|
|
int &time,
|
|
|
|
std::map<BasicBlock *, BasicBlock *> &be) {
|
|
|
|
|
|
|
|
color[u]=GREY;
|
|
|
|
time++;
|
|
|
|
d[u]=time;
|
|
|
|
|
2003-09-24 22:06:25 +00:00
|
|
|
for (succ_iterator vl = succ_begin(u), ve = succ_end(u); vl != ve; ++vl){
|
2003-07-18 16:08:32 +00:00
|
|
|
BasicBlock *BB = *vl;
|
|
|
|
|
2004-03-30 19:53:46 +00:00
|
|
|
if(color[BB]!=GREY && color[BB]!=BLACK)
|
2003-07-18 16:08:32 +00:00
|
|
|
getBackEdgesVisit(BB, color, d, time, be);
|
|
|
|
|
|
|
|
//now checking for d and f vals
|
|
|
|
else if(color[BB]==GREY){
|
|
|
|
//so v is ancestor of u if time of u > time of v
|
2004-03-30 19:53:46 +00:00
|
|
|
if(d[u] >= d[BB]) // u->BB is a backedge
|
2003-07-18 16:08:32 +00:00
|
|
|
be[u] = BB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
color[u]=BLACK;//done with visiting the node and its neighbors
|
|
|
|
}
|
|
|
|
|
2004-03-30 19:53:46 +00:00
|
|
|
/// removeRedundant - Remove all back-edges that are dominated by other
|
|
|
|
/// back-edges in the set.
|
|
|
|
///
|
2003-07-18 16:08:32 +00:00
|
|
|
void CombineBranches::removeRedundant(std::map<BasicBlock *, BasicBlock *> &be){
|
|
|
|
std::vector<BasicBlock *> toDelete;
|
|
|
|
std::map<BasicBlock *, int> seenBB;
|
|
|
|
|
|
|
|
for(std::map<BasicBlock *, BasicBlock *>::iterator MI = be.begin(),
|
|
|
|
ME = be.end(); MI != ME; ++MI){
|
|
|
|
|
|
|
|
if(seenBB[MI->second])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
seenBB[MI->second] = 1;
|
|
|
|
|
|
|
|
std::vector<BasicBlock *> sameTarget;
|
|
|
|
sameTarget.clear();
|
|
|
|
|
|
|
|
for(std::map<BasicBlock *, BasicBlock *>::iterator MMI = be.begin(),
|
|
|
|
MME = be.end(); MMI != MME; ++MMI){
|
|
|
|
|
|
|
|
if(MMI->first == MI->first)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if(MMI->second == MI->second)
|
|
|
|
sameTarget.push_back(MMI->first);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//so more than one branch to same target
|
|
|
|
if(sameTarget.size()){
|
|
|
|
|
|
|
|
sameTarget.push_back(MI->first);
|
|
|
|
|
|
|
|
BasicBlock *newBB = new BasicBlock("newCommon", MI->first->getParent());
|
2004-06-01 20:06:10 +00:00
|
|
|
BranchInst *newBranch = new BranchInst(MI->second, newBB);
|
2003-07-18 16:08:32 +00:00
|
|
|
|
|
|
|
std::map<PHINode *, std::vector<unsigned int> > phiMap;
|
|
|
|
|
|
|
|
for(std::vector<BasicBlock *>::iterator VBI = sameTarget.begin(),
|
|
|
|
VBE = sameTarget.end(); VBI != VBE; ++VBI){
|
|
|
|
|
|
|
|
BranchInst *ti = cast<BranchInst>((*VBI)->getTerminator());
|
|
|
|
unsigned char index = 1;
|
2004-03-30 19:53:46 +00:00
|
|
|
if(ti->getSuccessor(0) == MI->second)
|
2003-07-18 16:08:32 +00:00
|
|
|
index = 0;
|
|
|
|
|
|
|
|
ti->setSuccessor(index, newBB);
|
|
|
|
|
|
|
|
for(BasicBlock::iterator BB2Inst = MI->second->begin(),
|
|
|
|
BBend = MI->second->end(); BB2Inst != BBend; ++BB2Inst){
|
|
|
|
|
|
|
|
if (PHINode *phiInst = dyn_cast<PHINode>(BB2Inst)){
|
|
|
|
int bbIndex;
|
|
|
|
bbIndex = phiInst->getBasicBlockIndex(*VBI);
|
2004-03-30 19:53:46 +00:00
|
|
|
if(bbIndex>=0)
|
2003-07-18 16:08:32 +00:00
|
|
|
phiMap[phiInst].push_back(bbIndex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(std::map<PHINode *, std::vector<unsigned int> >::iterator
|
|
|
|
PI = phiMap.begin(), PE = phiMap.end(); PI != PE; ++PI){
|
|
|
|
|
|
|
|
PHINode *phiNode = new PHINode(PI->first->getType(), "phi", newBranch);
|
|
|
|
for(std::vector<unsigned int>::iterator II = PI->second.begin(),
|
|
|
|
IE = PI->second.end(); II != IE; ++II){
|
|
|
|
phiNode->addIncoming(PI->first->getIncomingValue(*II),
|
|
|
|
PI->first->getIncomingBlock(*II));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<BasicBlock *> tempBB;
|
|
|
|
for(std::vector<unsigned int>::iterator II = PI->second.begin(),
|
|
|
|
IE = PI->second.end(); II != IE; ++II){
|
|
|
|
tempBB.push_back(PI->first->getIncomingBlock(*II));
|
|
|
|
}
|
|
|
|
|
|
|
|
for(std::vector<BasicBlock *>::iterator II = tempBB.begin(),
|
|
|
|
IE = tempBB.end(); II != IE; ++II){
|
|
|
|
PI->first->removeIncomingValue(*II);
|
|
|
|
}
|
|
|
|
|
|
|
|
PI->first->addIncoming(phiNode, newBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-03-30 19:53:46 +00:00
|
|
|
/// runOnFunction - Per function pass for combining branches.
|
|
|
|
///
|
2003-07-18 16:08:32 +00:00
|
|
|
bool CombineBranches::runOnFunction(Function &F){
|
2004-03-30 19:53:46 +00:00
|
|
|
if (F.isExternal ())
|
2003-07-18 16:08:32 +00:00
|
|
|
return false;
|
|
|
|
|
2004-03-30 19:53:46 +00:00
|
|
|
// Find and remove "redundant" back-edges.
|
|
|
|
std::map<BasicBlock *, Color> color;
|
|
|
|
std::map<BasicBlock *, int> d;
|
|
|
|
std::map<BasicBlock *, BasicBlock *> be;
|
|
|
|
int time = 0;
|
|
|
|
getBackEdgesVisit (F.begin (), color, d, time, be);
|
|
|
|
removeRedundant (be);
|
2003-07-18 16:08:32 +00:00
|
|
|
|
2004-03-30 19:53:46 +00:00
|
|
|
return true; // FIXME: assumes a modification was always made.
|
2003-07-18 16:08:32 +00:00
|
|
|
}
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-09-30 20:14:29 +00:00
|
|
|
FunctionPass *createCombineBranchesPass () {
|
|
|
|
return new CombineBranches();
|
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|