Changes to runtime framework

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5572 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Anand Shukla 2003-02-14 20:41:53 +00:00
parent f1c283859b
commit 92d5e30e76
7 changed files with 188 additions and 83 deletions

View File

@ -25,7 +25,7 @@ using std::vector;
static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
Value *cnt){
Value *cnt, Instruction *rInst){
vector<const Type*> args;
//args.push_back(PointerType::get(Type::SByteTy));
@ -33,8 +33,15 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
args.push_back(Type::IntTy);
//args.push_back(Type::IntTy);
args.push_back(PointerType::get(Type::IntTy));
args.push_back(PointerType::get(Type::IntTy));
const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);
vector<Value *> tmpVec;
tmpVec.push_back(Constant::getNullValue(Type::LongTy));
tmpVec.push_back(Constant::getNullValue(Type::LongTy));
Instruction *Idx = new GetElementPtrInst(cnt, tmpVec, "");//,
BB->getInstList().push_back(Idx);
Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
assert(trigMeth && "trigger method could not be inserted!");
@ -42,7 +49,8 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
trargs.push_back(pathNo);
trargs.push_back(cnt);
trargs.push_back(Idx);
trargs.push_back(rInst);
Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin());
BB->getInstList().push_back(callInst);
@ -52,7 +60,7 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
//get the code to be inserted on the edge
//This is determined from cond (1-6)
void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
void getEdgeCode::getCode(Instruction *rInst, Value *countInst,
Function *M, BasicBlock *BB,
vector<Value *> &retVec){
@ -105,9 +113,14 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
//count[inc]++
case 4:{
Instruction *Idx = new GetElementPtrInst(countInst,
vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
"");//, InsertPos);
vector<Value *> tmpVec;
tmpVec.push_back(Constant::getNullValue(Type::LongTy));
tmpVec.push_back(ConstantSInt::get(Type::LongTy, inc));
Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
//Instruction *Idx = new GetElementPtrInst(countInst,
// vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
// "");//, InsertPos);
BB->getInstList().push_back(Idx);
Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos);
@ -156,8 +169,10 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
Type::LongTy,"ctin");//, InsertPos);
BB->getInstList().push_back(castInst);
Instruction *Idx = new GetElementPtrInst(countInst,
vector<Value*>(1,castInst), "");//,
vector<Value *> tmpVec;
tmpVec.push_back(Constant::getNullValue(Type::LongTy));
tmpVec.push_back(castInst);
Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
// InsertPos);
BB->getInstList().push_back(Idx);
@ -166,6 +181,7 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
Value *cons=ConstantSInt::get(Type::IntTy,1);
//count[addIndex]++
//std::cerr<<"Type ldInst:"<<ldInst->getType()<<"\t cons:"<<cons->getType()<<"\n";
Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
cons,"");
BB->getInstList().push_back(newCount);
@ -194,8 +210,14 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin");
BB->getInstList().push_back(castInst2);
Instruction *Idx = new GetElementPtrInst(countInst,
vector<Value*>(1,castInst2), "");
vector<Value *> tmpVec;
tmpVec.push_back(Constant::getNullValue(Type::LongTy));
tmpVec.push_back(castInst2);
Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
//Instruction *Idx = new GetElementPtrInst(countInst,
// vector<Value*>(1,castInst2), "");
BB->getInstList().push_back(Idx);
Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos);
@ -233,17 +255,16 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
//the number of executions of path k
void insertInTopBB(BasicBlock *front,
int k,
Instruction *rVar,
Instruction *countVar, Value *threshold){
Instruction *rVar, Value *threshold){
//rVar is variable r,
//countVar is array Count, and these are allocatted outside
//countVar is count[]
Value *Int0 = ConstantInt::get(Type::IntTy, 0);
//now push all instructions in front of the BB
BasicBlock::iterator here=front->begin();
front->getInstList().insert(here, rVar);
front->getInstList().insert(here,countVar);
//front->getInstList().insert(here,countVar);
//Initialize Count[...] with 0
@ -258,19 +279,19 @@ void insertInTopBB(BasicBlock *front,
new StoreInst(Int0, rVar, here);
//insert initialize function for initializing
vector<const Type*> inCountArgs;
inCountArgs.push_back(PointerType::get(Type::IntTy));
inCountArgs.push_back(Type::IntTy);
//vector<const Type*> inCountArgs;
//inCountArgs.push_back(PointerType::get(Type::IntTy));
//inCountArgs.push_back(Type::IntTy);
const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs,
false);
Function *inCountMth = front->getParent()->getParent()->getOrInsertFunction("llvmInitializeCounter", cFty);
assert(inCountMth && "Initialize method could not be inserted!");
//const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs,
// false);
//Function *inCountMth = front->getParent()->getParent()->getOrInsertFunction("llvmInitializeCounter", cFty);
//assert(inCountMth && "Initialize method could not be inserted!");
vector<Value *> iniArgs;
iniArgs.push_back(countVar);
iniArgs.push_back(ConstantSInt::get(Type::IntTy, k));
new CallInst(inCountMth, iniArgs, "", here);
//vector<Value *> iniArgs;
//iniArgs.push_back(countVar);
//iniArgs.push_back(ConstantSInt::get(Type::IntTy, k));
//new CallInst(inCountMth, iniArgs, "", here);
if(front->getParent()->getName() == "main"){
@ -296,7 +317,7 @@ void insertInTopBB(BasicBlock *front,
void insertBB(Edge ed,
getEdgeCode *edgeCode,
Instruction *rInst,
Instruction *countInst,
Value *countInst,
int numPaths, int Methno, Value *threshold){
BasicBlock* BB1=ed.getFirst()->getElement();
@ -338,7 +359,7 @@ void insertBB(Edge ed,
if(retVec.size()>0){
triggerBB = new BasicBlock("trigger", BB1->getParent());
getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno,
retVec[1], countInst);//retVec[0]);
retVec[1], countInst, rInst);//retVec[0]);
//Instruction *castInst = new CastInst(retVec[0], Type::IntTy, "");
Instruction *etr = new LoadInst(threshold, "threshold");

View File

@ -151,7 +151,7 @@ bool Graph::hasEdgeAndWt(Edge ed){
return false;
Node *nd2=ed.getSecond();
nodeList nli = nodes[ed.getFirst()];//getNodeList(ed.getFirst());
nodeList &nli = nodes[ed.getFirst()];//getNodeList(ed.getFirst());
for(nodeList::iterator NI=nli.begin(), NE=nli.end(); NI!=NE; ++NI)
if(*NI->element == *nd2 && ed.getWeight()==NI->weight)
@ -400,7 +400,7 @@ Graph* Graph::getMaxSpanningTree(){
//assign wt(v) to all adjacent vertices v of u
//only if v is in vt
Graph::nodeList nl=getNodeList(u);
Graph::nodeList &nl = getNodeList(u);
for(nodeList::iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI){
Node *v=NI->element;
int weight=-NI->weight;
@ -437,7 +437,7 @@ void Graph::printGraph(){
cerr<<"Graph---------------------\n";
for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){
cerr<<((*LI)->getElement())->getName()<<"->";
Graph::nodeList nl=getNodeList(*LI);
Graph::nodeList &nl = getNodeList(*LI);
for(Graph::nodeList::iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI){
cerr<<":"<<"("<<(NI->element->getElement())
->getName()<<":"<<NI->element->getWeight()<<","<<NI->weight<<")";
@ -483,7 +483,7 @@ void Graph::makeUnDirectional(){
vector<Node* > allNodes=getAllNodes();
for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
++NI) {
nodeList nl=getNodeList(*NI);
nodeList &nl = getNodeList(*NI);
for(nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE; ++NLI){
Edge ed(NLI->element, *NI, NLI->weight);
if(!hasEdgeAndWt(ed)){
@ -502,7 +502,7 @@ void Graph::reverseWts(){
vector<Node *> allNodes=getAllNodes();
for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
++NI) {
nodeList node_list=getNodeList(*NI);
nodeList &node_list = getNodeList(*NI);
for(nodeList::iterator NLI=nodes[*NI].begin(), NLE=nodes[*NI].end();
NLI!=NLE; ++NLI)
NLI->weight=-NLI->weight;

View File

@ -33,7 +33,6 @@ public:
inline bool operator==(Node& nd) const { return element==nd.element; }
};
//Class Edge
//Denotes an edge in the graph
class Edge{
@ -410,7 +409,7 @@ public:
//get the code to be inserted on the edge
//This is determined from cond (1-6)
void getCode(Instruction *a, Instruction *b, Function *M, BasicBlock *BB,
void getCode(Instruction *a, Value *b, Function *M, BasicBlock *BB,
std::vector<Value *> &retVec);
};
@ -423,7 +422,7 @@ void printEdge(Edge ed);
//Do graph processing: to determine minimal edge increments,
//appropriate code insertions etc and insert the code at
//appropriate locations
void processGraph(Graph &g, Instruction *rInst, Instruction *countInst, std::vector<Edge> &be, std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, int n, int MethNo, Value *threshold);
void processGraph(Graph &g, Instruction *rInst, Value *countInst, std::vector<Edge> &be, std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, int n, int MethNo, Value *threshold);
//print the graph (for debugging)
void printGraph(Graph &g);
@ -432,7 +431,7 @@ void printGraph(Graph &g);
//void printGraph(const Graph g);
//insert a basic block with appropriate code
//along a given edge
void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Instruction *countInst, int n, int Methno, Value *threshold);
void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Value *countInst, int n, int Methno, Value *threshold);
//Insert the initialization code in the top BB
//this includes initializing r, and count
@ -442,7 +441,7 @@ void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Instruction *c
//number of that path
//Count is an array, where Count[k] represents
//the number of executions of path k
void insertInTopBB(BasicBlock *front, int k, Instruction *rVar, Instruction *countVar, Value *threshold);
void insertInTopBB(BasicBlock *front, int k, Instruction *rVar, Value *threshold);
//Add dummy edges corresponding to the back edges
//If a->b is a backedge

View File

@ -479,7 +479,7 @@ static void moveDummyCode(vector<Edge> &stDummy,
//appropriate locations
void processGraph(Graph &g,
Instruction *rInst,
Instruction *countInst,
Value *countInst,
vector<Edge >& be,
vector<Edge >& stDummy,
vector<Edge >& exDummy,

View File

@ -479,7 +479,7 @@ static void moveDummyCode(vector<Edge> &stDummy,
//appropriate locations
void processGraph(Graph &g,
Instruction *rInst,
Instruction *countInst,
Value *countInst,
vector<Edge >& be,
vector<Edge >& stDummy,
vector<Edge >& exDummy,

View File

@ -155,6 +155,8 @@ bool ProfilePaths::runOnFunction(Function &F){
// numPaths is the number of acyclic paths in the graph
int numPaths=valueAssignmentToEdges(g, nodePriority, be);
//if(numPaths<=1) return false;
if(numPaths<=1 || numPaths >5000) return false;
#ifdef DEBUG_PATH_PROFILES
@ -172,10 +174,17 @@ bool ProfilePaths::runOnFunction(Function &F){
AllocaInst(Type::IntTy,
ConstantUInt::get(Type::UIntTy,1),"R");
Instruction *countVar=new
AllocaInst(Type::IntTy,
ConstantUInt::get(Type::UIntTy, numPaths), "Count");
//Instruction *countVar=new
//AllocaInst(Type::IntTy,
// ConstantUInt::get(Type::UIntTy, numPaths), "Count");
//initialize counter array!
std::vector<Constant*> arrayInitialize;
for(int xi=0; xi<numPaths; xi++)
arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0));
Constant *initializer = ConstantArray::get(ArrayType::get(Type::IntTy, numPaths), arrayInitialize);
GlobalVariable *countVar = new GlobalVariable(ArrayType::get(Type::IntTy, numPaths), false, true, initializer, "Count", F.getParent());
static GlobalVariable *threshold = NULL;
static bool insertedThreshold = false;
@ -191,7 +200,7 @@ bool ProfilePaths::runOnFunction(Function &F){
// insert initialization code in first (entry) BB
// this includes initializing r and count
insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar, threshold);
insertInTopBB(&F.getEntryNode(),numPaths, rVar, threshold);
//now process the graph: get path numbers,
//get increments along different paths,

View File

@ -16,12 +16,14 @@ using std::cerr;
//Routines to get the path trace!
void getPathFrmNode(Node *n, vector<BasicBlock*> &vBB, int pathNo, Graph g,
void getPathFrmNode(Node *n, vector<BasicBlock*> &vBB, int pathNo, Graph &g,
vector<Edge> &stDummy, vector<Edge> &exDummy,
vector<Edge> &be,
double strand){
Graph::nodeList nlist=g.getNodeList(n);
Graph::nodeList &nlist = g.getNodeList(n);
//printGraph(g);
//std::cerr<<"Path No: "<<pathNo<<"\n";
int maxCount=-9999999;
bool isStart=false;
@ -30,7 +32,7 @@ void getPathFrmNode(Node *n, vector<BasicBlock*> &vBB, int pathNo, Graph g,
double edgeRnd=0;
Node *nextRoot=n;
for(Graph::nodeList::iterator NLI=nlist.begin(), NLE=nlist.end(); NLI!=NLE;
for(Graph::nodeList::iterator NLI = nlist.begin(), NLE=nlist.end(); NLI!=NLE;
++NLI){
if(NLI->weight>maxCount && NLI->weight<=pathNo){
maxCount=NLI->weight;
@ -116,7 +118,8 @@ static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
return NULL;
}
void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){
void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){//,
// vector<Instruction *> &instToErase){
//step 1: create graph
//Transform the cfg s.t. we have just one exit node
@ -124,8 +127,14 @@ void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){
std::vector<Edge> edges;
Node *tmp;
Node *exitNode=0, *startNode=0;
static std::map<Function *, Graph *> graphMap;
//Creat cfg just once for each function!
static std::map<Function *, Graph *> graphMap;
//get backedges, exit and start edges for the graphs and store them
static std::map<Function *, vector<Edge> > stMap, exMap, beMap;
static std::map<Function *, Value *> pathReg; //path register
if(!graphMap[M]){
BasicBlock *ExitNode = 0;
@ -142,17 +151,39 @@ void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){
//The nodes must be uniquely identified:
//That is, no two nodes must hav same BB*
//keep a map for trigger basicblocks!
std::map<BasicBlock *, unsigned char> triggerBBs;
//First enter just nodes: later enter edges
for(Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
if(BB->size()==2){
const Instruction *inst = BB->getInstList().begin();
if(isa<CallInst>(inst)){
Instruction *ii1 = BB->getInstList().begin();
CallInst *callInst = dyn_cast<CallInst>(ii1);
if(callInst->getCalledFunction()->getName()=="trigger")
continue;
bool cont = false;
if(BB->size()==3 || BB->size() ==2){
for(BasicBlock::iterator II = BB->begin(), IE = BB->end();
II != IE; ++II){
if(CallInst *callInst = dyn_cast<CallInst>(&*II)){
//std::cerr<<*callInst;
Function *calledFunction = callInst->getCalledFunction();
if(calledFunction && calledFunction->getName() == "trigger"){
triggerBBs[BB] = 9;
cont = true;
//std::cerr<<"Found trigger!\n";
break;
}
}
}
}
if(cont)
continue;
// const Instruction *inst = BB->getInstList().begin();
// if(isa<CallInst>(inst)){
// Instruction *ii1 = BB->getInstList().begin();
// CallInst *callInst = dyn_cast<CallInst>(ii1);
// if(callInst->getCalledFunction()->getName()=="trigger")
// continue;
// }
Node *nd=new Node(BB);
nodes.push_back(nd);
if(&*BB==ExitNode)
@ -164,32 +195,55 @@ void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){
assert(exitNode!=0 && startNode!=0 && "Start or exit not found!");
for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
if(BB->size()==2){
const Instruction *inst = BB->getInstList().begin();
if(isa<CallInst>(inst)){
Instruction *ii1 = BB->getInstList().begin();
CallInst *callInst = dyn_cast<CallInst>(ii1);
if(callInst->getCalledFunction()->getName()=="trigger")
continue;
}
}
if(triggerBBs[BB] == 9)
continue;
//if(BB->size()==3)
//if(CallInst *callInst = dyn_cast<CallInst>(&*BB->getInstList().begin()))
//if(callInst->getCalledFunction()->getName() == "trigger")
//continue;
// if(BB->size()==2){
// const Instruction *inst = BB->getInstList().begin();
// if(isa<CallInst>(inst)){
// Instruction *ii1 = BB->getInstList().begin();
// CallInst *callInst = dyn_cast<CallInst>(ii1);
// if(callInst->getCalledFunction()->getName()=="trigger")
// continue;
// }
// }
Node *nd=findBB(nodes, BB);
assert(nd && "No node for this edge!");
for(BasicBlock::succ_iterator s=succ_begin(&*BB), se=succ_end(&*BB);
s!=se; ++s){
if((*s)->size()==2){
const Instruction *inst = (*s)->getInstList().begin();
if(isa<CallInst>(inst)){
Instruction *ii1 = (*s)->getInstList().begin();
CallInst *callInst = dyn_cast<CallInst>(ii1);
if(callInst->getCalledFunction()->getName()=="trigger")
continue;
}
if(triggerBBs[*s] == 9){
//if(!pathReg[M]){ //Get the path register for this!
//if(BB->size()>8)
// if(LoadInst *ldInst = dyn_cast<LoadInst>(&*BB->getInstList().begin()))
// pathReg[M] = ldInst->getPointerOperand();
//}
continue;
}
Node *nd2=findBB(nodes,*s);
//if((*s)->size()==3)
//if(CallInst *callInst =
// dyn_cast<CallInst>(&*(*s)->getInstList().begin()))
// if(callInst->getCalledFunction()->getName() == "trigger")
// continue;
// if((*s)->size()==2){
// const Instruction *inst = (*s)->getInstList().begin();
// if(isa<CallInst>(inst)){
// Instruction *ii1 = (*s)->getInstList().begin();
// CallInst *callInst = dyn_cast<CallInst>(ii1);
// if(callInst->getCalledFunction()->getName()=="trigger")
// continue;
// }
// }
Node *nd2 = findBB(nodes,*s);
assert(nd2 && "No node for this edge!");
Edge ed(nd,nd2,0);
edges.push_back(ed);
@ -201,23 +255,45 @@ void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){
Graph *g = graphMap[M];
if (M->size() <= 1) return; //uninstrumented
//step 2: getBackEdges
//vector<Edge> be;
std::map<Node *, int> nodePriority;
g->getBackEdges(beMap[M], nodePriority);
//step 3: add dummy edges
//vector<Edge> stDummy;
//vector<Edge> exDummy;
addDummyEdges(stMap[M], exMap[M], *g, beMap[M]);
//step 4: value assgn to edges
int numPaths = valueAssignmentToEdges(*g, nodePriority, beMap[M]);
}
//step 5: now travel from root, select max(edge) < pathNo,
//and go on until reach the exit
return getPathFrmNode(graphMap[M]->getRoot(), vBB, pathNo, *graphMap[M],
stMap[M], exMap[M], beMap[M], -1);
getPathFrmNode(graphMap[M]->getRoot(), vBB, pathNo, *graphMap[M],
stMap[M], exMap[M], beMap[M], -1);
//post process vBB to locate instructions to be erased
/*
if(pathReg[M]){
for(vector<BasicBlock *>::iterator VBI = vBB.begin(), VBE = vBB.end();
VBI != VBE; ++VBI){
for(BasicBlock::iterator BBI = (*VBI)->begin(), BBE = (*VBI)->end();
BBI != BBE; ++BBI){
if(LoadInst *ldInst = dyn_cast<LoadInst>(&*BBI)){
if(pathReg[M] == ldInst->getPointerOperand())
instToErase.push_back(ldInst);
}
else if(StoreInst *stInst = dyn_cast<StoreInst>(&*BBI)){
if(pathReg[M] == stInst->getPointerOperand())
instToErase.push_back(stInst);
}
}
}
}
*/
}