diff --git a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp index 8668be15905..e118ea5abde 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp @@ -16,7 +16,6 @@ #include "llvm/iOperators.h" #include "llvm/iPHINode.h" #include "llvm/Module.h" -#include #include #define INSERT_LOAD_COUNT @@ -27,11 +26,7 @@ using std::vector; static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, Value *cnt, Instruction *InsertPos){ - static int i=-1; - i++; - char gstr[100]; - sprintf(gstr,"globalVar%d",i); - std::string globalVarName=gstr; + vector args; //args.push_back(PointerType::get(Type::SByteTy)); args.push_back(Type::IntTy); @@ -39,39 +34,15 @@ static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, args.push_back(Type::IntTy); const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false); - // Function *triggerMeth = M->getOrInsertFunction("trigger", MTy); Function *trigMeth = M->getOrInsertFunction("trigger", MTy); assert(trigMeth && "trigger method could not be inserted!"); - //if (Value *triggerMeth = ST->lookup(PointerType::get(MTy), "trigger")) { - //Function *trigMeth = cast(triggerMeth); + vector trargs; - //pred_iterator piter=BB->pred_begin(); - //std::string predName = "uu";//BB->getName(); - //Constant *bbName=ConstantArray::get(predName);//BB->getName()); - //GlobalVariable *gbl=new GlobalVariable(ArrayType::get(Type::SByteTy, - // predName.size()+1), - // true, true, bbName, gstr); - - //M->getGlobalList().push_back(gbl); - - //vector elargs; - //elargs.push_back(ConstantSInt::get(Type::LongTy, 0)); - //elargs.push_back(ConstantSInt::get(Type::LongTy, 0)); - - // commented out bb name frm which its called - //Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst"); - - //trargs.push_back(ConstantArray::get(BB->getName())); - - //trargs.push_back(getElmntInst); - //trargs.push_back(bbName); - trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo)); - - //trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this trargs.push_back(pathNo); trargs.push_back(cnt); + Instruction *callInst=new CallInst(trigMeth, trargs, "", InsertPos); } @@ -83,7 +54,7 @@ void getEdgeCode::getCode(Instruction *rInst, Function *M, BasicBlock *BB, int numPaths, int MethNo){ - Instruction *InsertPos = BB->begin(); + Instruction *InsertPos = BB->getInstList().begin(); //case: r=k code to be inserted switch(cond){ @@ -149,10 +120,8 @@ void getEdgeCode::getCode(Instruction *rInst, Value *val=ConstantSInt::get(Type::IntTy,inc); Instruction *addIndex=BinaryOperator:: create(Instruction::Add, ldIndex, val,"ti2", InsertPos); - //erase following 1 line - //Value *valtemp=ConstantSInt::get(Type::IntTy,999); - //now load count[addIndex] + //now load count[addIndex] Instruction *castInst=new CastInst(addIndex, Type::LongTy,"ctin", InsertPos); Instruction *Idx = new GetElementPtrInst(countInst, @@ -162,13 +131,11 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *ldInst=new LoadInst(Idx, "ti3", InsertPos); Value *cons=ConstantSInt::get(Type::IntTy,1); //count[addIndex]++ - Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, cons, - "ti4", InsertPos); + Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, + cons,"", InsertPos); #ifdef INSERT_STORE - ///* - new StoreInst(addIn, Idx, InsertPos); - //*/ + Instruction *stInst = new StoreInst(addIn, Idx, InsertPos); #endif //insert trigger @@ -202,7 +169,6 @@ void getEdgeCode::getCode(Instruction *rInst, //insert trigger getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn, InsertPos); //end trigger code - break; } @@ -252,6 +218,30 @@ void insertInTopBB(BasicBlock *front, //store uint 0, uint *%R new StoreInst(Int0, rVar, here); + + if(front->getParent()->getName() == "main"){ + + //if its a main function, do the following! + //A global variable: %llvm_threshold + //%llvm_threshold = uninitialized global int + GlobalVariable *threshold = new GlobalVariable(Type::IntTy, false, true, 0, + "reopt_threshold"); + + front->getParent()->getParent()->getGlobalList().push_back(threshold); + + vector initialize_args; + initialize_args.push_back(PointerType::get(Type::IntTy)); + + const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args, + false); + Function *initialMeth = front->getParent()->getParent()->getOrInsertFunction("reoptimizerInitialize", Fty); + assert(initialMeth && "Initialize method could not be inserted!"); + + vector trargs; + trargs.push_back(threshold); + + new CallInst(initialMeth, trargs, "", front->begin()); + } } @@ -262,8 +252,7 @@ void insertBB(Edge ed, Instruction *rInst, Instruction *countInst, int numPaths, int Methno){ - static int i=-1; - i++; + BasicBlock* BB1=ed.getFirst()->getElement(); BasicBlock* BB2=ed.getSecond()->getElement(); @@ -274,21 +263,13 @@ void insertBB(Edge ed, cerr<<"########################\n"; #endif - char counterstr[100]; - sprintf(counterstr,"counter%d",i); - std::string ctr=counterstr; - //We need to insert a BB between BB1 and BB2 TerminatorInst *TI=BB1->getTerminator(); - BasicBlock *newBB=new BasicBlock(ctr, BB1->getParent()); + BasicBlock *newBB=new BasicBlock("counter", BB1->getParent()); //Is terminator a branch instruction? //then we need to change branch destinations to include new BB - //std::cerr<<"before cast!\n"; - //std::cerr<<"Method no in Edgecode:"<(TI); if(BI->isUnconditional()){ @@ -310,10 +291,8 @@ void insertBB(Edge ed, //get code for the new BB edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, numPaths, Methno); - - //std::cerr<<"After casting\n"; //get code for the new BB - //now iterate over BB2, and set its Phi nodes right + //now iterate over BB2, and set its Phi nodes right for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end(); BB2Inst != BBend; ++BB2Inst){ diff --git a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp index 7b8069cfee2..f6280e84707 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp @@ -52,30 +52,75 @@ Graph::Graph(std::vector n, std::vector e, } //sorting edgelist, called by backEdgeVist ONLY!!! -Graph::nodeList &Graph::sortNodeList(Node *par, nodeList &nl){ +Graph::nodeList &Graph::sortNodeList(Node *par, nodeList &nl, vector &be){ assert(par && "null node pointer"); BasicBlock *bbPar = par->getElement(); if(nl.size()<=1) return nl; + if(getExit() == par) return nl; for(nodeList::iterator NLI = nl.begin(), NLE = nl.end()-1; NLI != NLE; ++NLI){ nodeList::iterator min = NLI; for(nodeList::iterator LI = NLI+1, LE = nl.end(); LI!=LE; ++LI){ //if LI < min, min = LI - if(min->element->getElement() == LI->element->getElement()) - continue; - + if(min->element->getElement() == LI->element->getElement() && + min->element == getExit()){ - TerminatorInst *tti = par->getElement()->getTerminator(); - BranchInst *ti = cast(tti); - assert(ti && "not a branch"); - assert(ti->getNumSuccessors()==2 && "less successors!"); + //same successors: so might be exit??? + //if it is exit, then see which is backedge + //check if LI is a left back edge! + + TerminatorInst *tti = par->getElement()->getTerminator(); + BranchInst *ti = cast(tti); + + assert(ti && "not a branch"); + assert(ti->getNumSuccessors()==2 && "less successors!"); + + BasicBlock *tB = ti->getSuccessor(0); + BasicBlock *fB = ti->getSuccessor(1); + //so one of LI or min must be back edge! + //Algo: if succ(0)!=LI (and so !=min) then succ(0) is backedge + //and then see which of min or LI is backedge + //THEN if LI is in be, then min=LI + if(LI->element->getElement() != tB){//so backedge must be made min! + for(vector::iterator VBEI = be.begin(), VBEE = be.end(); + VBEI != VBEE; ++VBEI){ + if(VBEI->getRandId() == LI->randId){ + min = LI; + break; + } + else if(VBEI->getRandId() == min->randId) + break; + } + } + else{// if(LI->element->getElement() != fB) + for(vector::iterator VBEI = be.begin(), VBEE = be.end(); + VBEI != VBEE; ++VBEI){ + if(VBEI->getRandId() == min->randId){ + min = LI; + break; + } + else if(VBEI->getRandId() == LI->randId) + break; + } + } + } - BasicBlock *tB = ti->getSuccessor(0); - BasicBlock *fB = ti->getSuccessor(1); - - if(tB == LI->element->getElement() || fB == min->element->getElement()) - min = LI; + else if (min->element->getElement() != LI->element->getElement()){ + TerminatorInst *tti = par->getElement()->getTerminator(); + BranchInst *ti = cast(tti); + assert(ti && "not a branch"); + + if(ti->getNumSuccessors()<=1) continue; + + assert(ti->getNumSuccessors()==2 && "less successors!"); + + BasicBlock *tB = ti->getSuccessor(0); + BasicBlock *fB = ti->getSuccessor(1); + + if(tB == LI->element->getElement() || fB == min->element->getElement()) + min = LI; + } } graphListElement tmpElmnt = *min; @@ -416,12 +461,6 @@ vector Graph::reverseTopologicalSort(){ DFS_Visit(*LI, toReturn); } - //print nodes - //std::cerr<<"Topological sort--------\n"; - //for(vector::iterator VI = toReturn.begin(), VE = toReturn.end(); - // VI!=VE; ++VI) - //std::cerr<<(*VI)->getElement()->getName()<<"->"; - //std::cerr<<"\n----------------------\n"; return toReturn; } @@ -487,15 +526,9 @@ void Graph::reverseWts(){ //a node with smaller time, and GREY color void Graph::getBackEdges(vector &be, map &d){ map color; - //map d; - //vector allNodes=getAllNodes(); int time=0; - //for(vector::iterator NI=allNodes.begin(), NE=allNodes.end(); - // NI!=NE; ++NI){ - //if(color[*NI]!=GREY && color[*NI]!=BLACK) - //printGraph(); - getBackEdgesVisit(getRoot(), be, color, d, time);//*NI, be, color, d, time); - //} + + getBackEdgesVisit(getRoot(), be, color, d, time); } //helper function to get back edges: it is called by @@ -507,25 +540,14 @@ void Graph::getBackEdgesVisit(Node *u, vector &be, time++; d[u]=time; - //std::cerr<<"Node list-----\n"; - vector succ_list = getSortedNodeList(u); - - //for(vector::iterator vl=succ_list.begin(), - // ve=succ_list.end(); vl!=ve; ++vl){ - //Node *v=vl->element; - //std::cerr<getElement()->getName()<<"->"; - //} - //std::cerr<<"\n-------- end Node list\n"; + vector &succ_list = getNodeList(u); for(vector::iterator vl=succ_list.begin(), ve=succ_list.end(); vl!=ve; ++vl){ Node *v=vl->element; - // for(vector::const_iterator v=succ_list.begin(), ve=succ_list.end(); - // v!=ve; ++v){ - - if(color[v]!=GREY && color[v]!=BLACK){ - getBackEdgesVisit(v, be, color, d, time); - } + if(color[v]!=GREY && color[v]!=BLACK){ + getBackEdgesVisit(v, be, color, d, time); + } //now checking for d and f vals if(color[v]==GREY){ diff --git a/lib/Transforms/Instrumentation/ProfilePaths/Graph.h b/lib/Transforms/Instrumentation/ProfilePaths/Graph.h index 8eb2f724f5e..1da7bf10613 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/Graph.h +++ b/lib/Transforms/Instrumentation/ProfilePaths/Graph.h @@ -149,7 +149,8 @@ struct NodeListSort{ return name1 &be, std::map &d); - nodeList &sortNodeList(Node *par, nodeList &nl); + nodeList &sortNodeList(Node *par, nodeList &nl, std::vector &be); //Get the Maximal spanning tree (also a graph) //of the graph @@ -324,10 +334,10 @@ public: return nodes[nd];//sortNodeList(nd, nli->second); } - nodeList &getSortedNodeList(Node *nd) { + nodeList &getSortedNodeList(Node *nd, std::vector &be) { elementIterator nli = nodes.find(nd); assert(nli != nodes.end() && "Node must be in nodes map"); - return sortNodeList(nd, nodes[nd]); + return sortNodeList(nd, nodes[nd], be); } //get the root of the graph @@ -450,7 +460,8 @@ void addDummyEdges(std::vector &stDummy, std::vector &exDummy, Graph //such that if we traverse along any path from root to exit, and //add up the edge values, we get a path number that uniquely //refers to the path we travelled -int valueAssignmentToEdges(Graph& g, std::map nodePriority); +int valueAssignmentToEdges(Graph& g, std::map nodePriority, + std::vector &be); void getBBtrace(std::vector &vBB, int pathNo, Function *M); #endif diff --git a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp index 37dd6ae5d6b..45ceadaa930 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp @@ -7,15 +7,18 @@ //===----------------------------------------------------------------------===// #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" +#include "llvm/Transforms/Instrumentation/Graph.h" #include "llvm/Function.h" #include "llvm/Pass.h" +#include "llvm/Module.h" +#include "llvm/Function.h" #include "llvm/BasicBlock.h" #include "llvm/InstrTypes.h" -#include "llvm/Transforms/Instrumentation/Graph.h" #include "llvm/iTerminators.h" #include #include #include +#include #include //using std::list; @@ -69,7 +72,8 @@ static void removeTreeEdges(Graph &g, Graph& t){ //such that if we traverse along any path from root to exit, and //add up the edge values, we get a path number that uniquely //refers to the path we travelled -int valueAssignmentToEdges(Graph& g, map nodePriority){ +int valueAssignmentToEdges(Graph& g, map nodePriority, + vector &be){ vector revtop=g.reverseTopologicalSort(); map NumPaths; for(vector::iterator RI=revtop.begin(), RE=revtop.end(); @@ -79,7 +83,9 @@ int valueAssignmentToEdges(Graph& g, map nodePriority){ else{ NumPaths[*RI]=0; - Graph::nodeList &nlist=g.getNodeList(*RI); + // Modified Graph::nodeList &nlist=g.getNodeList(*RI); + Graph::nodeList &nlist=g.getSortedNodeList(*RI, be); + //sort nodelist by increasing order of numpaths int sz=nlist.size(); @@ -104,7 +110,7 @@ int valueAssignmentToEdges(Graph& g, map nodePriority){ else{ TerminatorInst *tti = (*RI)->getElement()->getTerminator(); - //std::cerr<<*tti<(tti); assert(ti && "not a branch"); assert(ti->getNumSuccessors()==2 && "less successors!"); @@ -123,14 +129,11 @@ int valueAssignmentToEdges(Graph& g, map nodePriority){ } //sorted now! - //std::cerr<<"Considering Order-----\n"; for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end(); GLI!=GLE; ++GLI){ - //std::cerr<element->getElement()->getName()<<"->"; - GLI->weight=NumPaths[*RI]; + GLI->weight=NumPaths[*RI]; NumPaths[*RI]+=NumPaths[GLI->element]; } - //std::cerr<<"\nend order $$$$$$$$$$$$$$$$$$$$$$$$\n"; } } return NumPaths[g.getRoot()]; @@ -165,7 +168,7 @@ static int inc_Dir(Edge e, Edge f){ //used for getting edge increments (read comments above in inc_Dir) //inc_DFS is a modification of DFS -static void inc_DFS(Graph& g,Graph& t,map& Increment, +static void inc_DFS(Graph& g,Graph& t,map& Increment, int events, Node *v, Edge e){ vector allNodes=t.getAllNodes(); @@ -219,15 +222,17 @@ static void inc_DFS(Graph& g,Graph& t,map& Increment, //and assign them some values such that //if we consider just this subset, it still represents //the path sum along any path in the graph -static map getEdgeIncrements(Graph& g, Graph& t){ +static map getEdgeIncrements(Graph& g, Graph& t, + vector &be){ //get all edges in g-t - map Increment; + map Increment; vector allNodes=g.getAllNodes(); for(vector::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ - Graph::nodeList node_list=g.getNodeList(*NI); + Graph::nodeList node_list=g.getSortedNodeList(*NI, be); + //modified g.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!=NLE; ++NLI){ Edge ed(*NI, NLI->element,NLI->weight,NLI->randId); @@ -242,7 +247,8 @@ static map getEdgeIncrements(Graph& g, Graph& t){ for(vector::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ - Graph::nodeList node_list=g.getNodeList(*NI); + Graph::nodeList node_list=g.getSortedNodeList(*NI, be); + //modified g.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!=NLE; ++NLI){ Edge ed(*NI, NLI->element,NLI->weight, NLI->randId); @@ -267,9 +273,9 @@ graphListElement *findNodeInList(Graph::nodeList &NL, Node *N); //the kind of code to be inserted along an edge //The idea here is to minimize the computation //by inserting only the needed code -static void getCodeInsertions(Graph &g, map &instr, +static void getCodeInsertions(Graph &g, map &instr, vector &chords, - map &edIncrements){ + map &edIncrements){ //Register initialization code vector ws; @@ -302,14 +308,12 @@ static void getCodeInsertions(Graph &g, map &i } else if(g.getNumberOfIncomingEdges(w)==1){ ws.push_back(w); - //std::cerr<<"Added w\n"; } else{ getEdgeCode *edCd=new getEdgeCode(); edCd->setCond(2); edCd->setInc(0); instr[ed]=edCd; - //std::cerr<<"Case 2\n"; } } } @@ -328,39 +332,42 @@ static void getCodeInsertions(Graph &g, map &i for(vector::iterator EII=lllt.begin(); EII!=lllt.end() ;++EII){ Node *lnode=*EII; Graph::nodeList &nl = g.getNodeList(lnode); - graphListElement *N = findNodeInList(nl, w); - if (N){ - Node *v=lnode; - - //if chords has v->w - Edge ed(v,w, N->weight, N->randId); - getEdgeCode *edCd=new getEdgeCode(); - bool hasEdge=false; - for(vector::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; - ++CI){ - if(*CI==ed && CI->getWeight()==N->weight){ - hasEdge=true; - break; - } - } - if(hasEdge){ - char str[100]; - if(instr[ed]!=NULL && instr[ed]->getCond()==1){ - instr[ed]->setCond(4); - } - else{ - edCd->setCond(5); - edCd->setInc(edIncrements[ed]); - instr[ed]=edCd; - } - - } - else if(g.getNumberOfOutgoingEdges(v)==1) - ws.push_back(v); - else{ - edCd->setCond(6); - instr[ed]=edCd; - } + //graphListElement *N = findNodeInList(nl, w); + for(Graph::nodeList::const_iterator N = nl.begin(), + NNEN = nl.end(); N!= NNEN; ++N){ + if (*N->element == *w){ + Node *v=lnode; + + //if chords has v->w + Edge ed(v,w, N->weight, N->randId); + getEdgeCode *edCd=new getEdgeCode(); + bool hasEdge=false; + for(vector::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; + ++CI){ + if(*CI==ed && CI->getWeight()==N->weight){ + hasEdge=true; + break; + } + } + if(hasEdge){ + //char str[100]; + if(instr[ed]!=NULL && instr[ed]->getCond()==1){ + instr[ed]->setCond(4); + } + else{ + edCd->setCond(5); + edCd->setInc(edIncrements[ed]); + instr[ed]=edCd; + } + + } + else if(g.getNumberOfOutgoingEdges(v)==1) + ws.push_back(v); + else{ + edCd->setCond(6); + instr[ed]=edCd; + } + } } } } @@ -416,14 +423,14 @@ void printEdge(Edge ed){ static void moveDummyCode(vector &stDummy, vector &exDummy, vector &be, - map &insertions, + map &insertions, Graph &g){ typedef vector::iterator vec_iter; - map temp; + map temp; //iterate over edges with code std::vector toErase; - for(map::iterator MI=insertions.begin(), + for(map::iterator MI=insertions.begin(), ME=insertions.end(); MI!=ME; ++MI){ Edge ed=MI->first; getEdgeCode *edCd=MI->second; @@ -462,7 +469,7 @@ static void moveDummyCode(vector &stDummy, g.removeEdgeWithWt(*vmi); } - for(map::iterator MI=temp.begin(), + for(map::iterator MI=temp.begin(), ME=temp.end(); MI!=ME; ++MI){ insertions[MI->first]=MI->second; } @@ -584,15 +591,15 @@ void processGraph(Graph &g, //if we consider just this subset, it still represents //the path sum along any path in the graph - map increment=getEdgeIncrements(g,*t); + map increment=getEdgeIncrements(g,*t, be); #ifdef DEBUG_PATH_PROFILES //print edge increments for debugging - - for(map::iterator M_I=increment.begin(), M_E=increment.end(); - M_I!=M_E; ++M_I){ - printEdge(M_I->first); - cerr<<"Increment for above:"<second<<"\n"; + std::cerr<<"Edge Increments------\n"; + for(map::iterator MMI=increment.begin(), MME=increment.end(); MMI != MME; ++MMI){ + printEdge(MMI->first); + std::cerr<<"Increment for above:"<second<<"\n"; } + std::cerr<<"-------end of edge increments\n"; #endif @@ -606,15 +613,13 @@ void processGraph(Graph &g, getChords(chords, g, *t); - //cerr<<"Graph before getCodeInsertion:\n"; - //printGraph(g); - map codeInsertions; + map codeInsertions; getCodeInsertions(g, codeInsertions, chords,increment); #ifdef DEBUG_PATH_PROFILES //print edges with code for debugging cerr<<"Code inserted in following---------------\n"; - for(map::iterator cd_i=codeInsertions.begin(), + for(map::iterator cd_i=codeInsertions.begin(), cd_e=codeInsertions.end(); cd_i!=cd_e; ++cd_i){ printEdge(cd_i->first); cerr<second->getCond()<<":"<second->getInc()<<"\n"; diff --git a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp index 37dd6ae5d6b..45ceadaa930 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxillary.cpp @@ -7,15 +7,18 @@ //===----------------------------------------------------------------------===// #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" +#include "llvm/Transforms/Instrumentation/Graph.h" #include "llvm/Function.h" #include "llvm/Pass.h" +#include "llvm/Module.h" +#include "llvm/Function.h" #include "llvm/BasicBlock.h" #include "llvm/InstrTypes.h" -#include "llvm/Transforms/Instrumentation/Graph.h" #include "llvm/iTerminators.h" #include #include #include +#include #include //using std::list; @@ -69,7 +72,8 @@ static void removeTreeEdges(Graph &g, Graph& t){ //such that if we traverse along any path from root to exit, and //add up the edge values, we get a path number that uniquely //refers to the path we travelled -int valueAssignmentToEdges(Graph& g, map nodePriority){ +int valueAssignmentToEdges(Graph& g, map nodePriority, + vector &be){ vector revtop=g.reverseTopologicalSort(); map NumPaths; for(vector::iterator RI=revtop.begin(), RE=revtop.end(); @@ -79,7 +83,9 @@ int valueAssignmentToEdges(Graph& g, map nodePriority){ else{ NumPaths[*RI]=0; - Graph::nodeList &nlist=g.getNodeList(*RI); + // Modified Graph::nodeList &nlist=g.getNodeList(*RI); + Graph::nodeList &nlist=g.getSortedNodeList(*RI, be); + //sort nodelist by increasing order of numpaths int sz=nlist.size(); @@ -104,7 +110,7 @@ int valueAssignmentToEdges(Graph& g, map nodePriority){ else{ TerminatorInst *tti = (*RI)->getElement()->getTerminator(); - //std::cerr<<*tti<(tti); assert(ti && "not a branch"); assert(ti->getNumSuccessors()==2 && "less successors!"); @@ -123,14 +129,11 @@ int valueAssignmentToEdges(Graph& g, map nodePriority){ } //sorted now! - //std::cerr<<"Considering Order-----\n"; for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end(); GLI!=GLE; ++GLI){ - //std::cerr<element->getElement()->getName()<<"->"; - GLI->weight=NumPaths[*RI]; + GLI->weight=NumPaths[*RI]; NumPaths[*RI]+=NumPaths[GLI->element]; } - //std::cerr<<"\nend order $$$$$$$$$$$$$$$$$$$$$$$$\n"; } } return NumPaths[g.getRoot()]; @@ -165,7 +168,7 @@ static int inc_Dir(Edge e, Edge f){ //used for getting edge increments (read comments above in inc_Dir) //inc_DFS is a modification of DFS -static void inc_DFS(Graph& g,Graph& t,map& Increment, +static void inc_DFS(Graph& g,Graph& t,map& Increment, int events, Node *v, Edge e){ vector allNodes=t.getAllNodes(); @@ -219,15 +222,17 @@ static void inc_DFS(Graph& g,Graph& t,map& Increment, //and assign them some values such that //if we consider just this subset, it still represents //the path sum along any path in the graph -static map getEdgeIncrements(Graph& g, Graph& t){ +static map getEdgeIncrements(Graph& g, Graph& t, + vector &be){ //get all edges in g-t - map Increment; + map Increment; vector allNodes=g.getAllNodes(); for(vector::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ - Graph::nodeList node_list=g.getNodeList(*NI); + Graph::nodeList node_list=g.getSortedNodeList(*NI, be); + //modified g.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!=NLE; ++NLI){ Edge ed(*NI, NLI->element,NLI->weight,NLI->randId); @@ -242,7 +247,8 @@ static map getEdgeIncrements(Graph& g, Graph& t){ for(vector::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ - Graph::nodeList node_list=g.getNodeList(*NI); + Graph::nodeList node_list=g.getSortedNodeList(*NI, be); + //modified g.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!=NLE; ++NLI){ Edge ed(*NI, NLI->element,NLI->weight, NLI->randId); @@ -267,9 +273,9 @@ graphListElement *findNodeInList(Graph::nodeList &NL, Node *N); //the kind of code to be inserted along an edge //The idea here is to minimize the computation //by inserting only the needed code -static void getCodeInsertions(Graph &g, map &instr, +static void getCodeInsertions(Graph &g, map &instr, vector &chords, - map &edIncrements){ + map &edIncrements){ //Register initialization code vector ws; @@ -302,14 +308,12 @@ static void getCodeInsertions(Graph &g, map &i } else if(g.getNumberOfIncomingEdges(w)==1){ ws.push_back(w); - //std::cerr<<"Added w\n"; } else{ getEdgeCode *edCd=new getEdgeCode(); edCd->setCond(2); edCd->setInc(0); instr[ed]=edCd; - //std::cerr<<"Case 2\n"; } } } @@ -328,39 +332,42 @@ static void getCodeInsertions(Graph &g, map &i for(vector::iterator EII=lllt.begin(); EII!=lllt.end() ;++EII){ Node *lnode=*EII; Graph::nodeList &nl = g.getNodeList(lnode); - graphListElement *N = findNodeInList(nl, w); - if (N){ - Node *v=lnode; - - //if chords has v->w - Edge ed(v,w, N->weight, N->randId); - getEdgeCode *edCd=new getEdgeCode(); - bool hasEdge=false; - for(vector::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; - ++CI){ - if(*CI==ed && CI->getWeight()==N->weight){ - hasEdge=true; - break; - } - } - if(hasEdge){ - char str[100]; - if(instr[ed]!=NULL && instr[ed]->getCond()==1){ - instr[ed]->setCond(4); - } - else{ - edCd->setCond(5); - edCd->setInc(edIncrements[ed]); - instr[ed]=edCd; - } - - } - else if(g.getNumberOfOutgoingEdges(v)==1) - ws.push_back(v); - else{ - edCd->setCond(6); - instr[ed]=edCd; - } + //graphListElement *N = findNodeInList(nl, w); + for(Graph::nodeList::const_iterator N = nl.begin(), + NNEN = nl.end(); N!= NNEN; ++N){ + if (*N->element == *w){ + Node *v=lnode; + + //if chords has v->w + Edge ed(v,w, N->weight, N->randId); + getEdgeCode *edCd=new getEdgeCode(); + bool hasEdge=false; + for(vector::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; + ++CI){ + if(*CI==ed && CI->getWeight()==N->weight){ + hasEdge=true; + break; + } + } + if(hasEdge){ + //char str[100]; + if(instr[ed]!=NULL && instr[ed]->getCond()==1){ + instr[ed]->setCond(4); + } + else{ + edCd->setCond(5); + edCd->setInc(edIncrements[ed]); + instr[ed]=edCd; + } + + } + else if(g.getNumberOfOutgoingEdges(v)==1) + ws.push_back(v); + else{ + edCd->setCond(6); + instr[ed]=edCd; + } + } } } } @@ -416,14 +423,14 @@ void printEdge(Edge ed){ static void moveDummyCode(vector &stDummy, vector &exDummy, vector &be, - map &insertions, + map &insertions, Graph &g){ typedef vector::iterator vec_iter; - map temp; + map temp; //iterate over edges with code std::vector toErase; - for(map::iterator MI=insertions.begin(), + for(map::iterator MI=insertions.begin(), ME=insertions.end(); MI!=ME; ++MI){ Edge ed=MI->first; getEdgeCode *edCd=MI->second; @@ -462,7 +469,7 @@ static void moveDummyCode(vector &stDummy, g.removeEdgeWithWt(*vmi); } - for(map::iterator MI=temp.begin(), + for(map::iterator MI=temp.begin(), ME=temp.end(); MI!=ME; ++MI){ insertions[MI->first]=MI->second; } @@ -584,15 +591,15 @@ void processGraph(Graph &g, //if we consider just this subset, it still represents //the path sum along any path in the graph - map increment=getEdgeIncrements(g,*t); + map increment=getEdgeIncrements(g,*t, be); #ifdef DEBUG_PATH_PROFILES //print edge increments for debugging - - for(map::iterator M_I=increment.begin(), M_E=increment.end(); - M_I!=M_E; ++M_I){ - printEdge(M_I->first); - cerr<<"Increment for above:"<second<<"\n"; + std::cerr<<"Edge Increments------\n"; + for(map::iterator MMI=increment.begin(), MME=increment.end(); MMI != MME; ++MMI){ + printEdge(MMI->first); + std::cerr<<"Increment for above:"<second<<"\n"; } + std::cerr<<"-------end of edge increments\n"; #endif @@ -606,15 +613,13 @@ void processGraph(Graph &g, getChords(chords, g, *t); - //cerr<<"Graph before getCodeInsertion:\n"; - //printGraph(g); - map codeInsertions; + map codeInsertions; getCodeInsertions(g, codeInsertions, chords,increment); #ifdef DEBUG_PATH_PROFILES //print edges with code for debugging cerr<<"Code inserted in following---------------\n"; - for(map::iterator cd_i=codeInsertions.begin(), + for(map::iterator cd_i=codeInsertions.begin(), cd_e=codeInsertions.end(); cd_i!=cd_e; ++cd_i){ printEdge(cd_i->first); cerr<second->getCond()<<":"<second->getInc()<<"\n";