diff options
author | Anand Shukla <ashukla@cs.uiuc.edu> | 2002-06-25 21:14:58 +0000 |
---|---|---|
committer | Anand Shukla <ashukla@cs.uiuc.edu> | 2002-06-25 21:14:58 +0000 |
commit | 5cafcfbab42e0160149a3390ee7aae12bbfded27 (patch) | |
tree | e8ac9ffd164d9e8507c2e8c678733054a4888226 /lib/Transforms | |
parent | 881ed6bad465c7e918352dec120a49baddadc497 (diff) |
additions and bug fixes
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@2794 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms')
5 files changed, 1395 insertions, 445 deletions
diff --git a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp index 1c0970537a..efbd5c86c6 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp @@ -7,7 +7,7 @@ //top block of cfg //===----------------------------------------------------------------------===// -#include "Graph.h" +#include "llvm/Transforms/Instrumentation/Graph.h" #include "llvm/BasicBlock.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" @@ -16,15 +16,89 @@ #include "llvm/iOther.h" #include "llvm/iOperators.h" #include "llvm/iPHINode.h" +#include "llvm/Module.h" +#include "llvm/SymbolTable.h" +#include "llvm/GlobalVariable.h" +#include "llvm/Constants.h"//llvm/ConstantVals.h" +#include "llvm/BasicBlock.h" +#include "llvm/Function.h" +#include <string.h> +#include <stdio.h> +#include <iostream> + +#define INSERT_LOAD_COUNT +#define INSERT_STORE using std::vector; + +void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, + Value *cnt){ + // return; + //cerr<<"In trigger code"<<endl; + static int i=-1; + i++; + char gstr[100]; + sprintf(gstr,"globalVar%d",i); + std::string globalVarName=gstr; + SymbolTable *ST = M->getSymbolTable(); + vector<const Type*> args; + args.push_back(PointerType::get(Type::SByteTy)); + args.push_back(Type::IntTy); + args.push_back(Type::IntTy); + 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<Function>(triggerMeth); + vector<Value *> trargs; + + //pred_iterator piter=BB->pred_begin(); + std::string predName=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<Value *> elargs; + elargs.push_back(ConstantUInt::get(Type::UIntTy, 0)); + elargs.push_back(ConstantUInt::get(Type::UIntTy, 0)); + + Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst"); + + //trargs.push_back(ConstantArray::get(BB->getName())); + trargs.push_back(getElmntInst); + 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); + + BasicBlock::InstListType& instList=BB->getInstList(); + BasicBlock::iterator here=instList.begin(); + here = ++instList.insert(here, getElmntInst); + instList.insert(here,callInst); + //} + //else{ + //insert trigger method + + //assert(0&&"No method trigger"); + //} +} + + //get the code to be inserted on the edge //This is determined from cond (1-6) void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst, Function *M, - BasicBlock *BB){ + BasicBlock *BB, int numPaths, int MethNo){ BasicBlock::InstListType& instList=BB->getInstList(); BasicBlock::iterator here=instList.begin(); @@ -33,35 +107,46 @@ void getEdgeCode::getCode(Instruction *rInst, switch(cond){ case 1:{ Value *val=ConstantSInt::get(Type::IntTy,inc); +#ifdef INSERT_STORE Instruction *stInst=new StoreInst(val, rInst); - here=++instList.insert(here,stInst); + here = ++instList.insert(here,stInst); +#endif break; } //case: r=0 to be inserted case 2:{ Value *val=ConstantSInt::get(Type::IntTy,0); +#ifdef INSERT_STORE Instruction *stInst=new StoreInst(val, rInst); - here=++instList.insert(here,stInst); + here = ++instList.insert(here,stInst); +#endif break; } //r+=k case 3:{ + Instruction *ldInst=new LoadInst(rInst, "ti1"); Value *val=ConstantSInt::get(Type::IntTy,inc); Instruction *addIn=BinaryOperator:: create(Instruction::Add, ldInst, val,"ti2"); - +#ifdef INSERT_STORE Instruction *stInst=new StoreInst(addIn, rInst); - here=++instList.insert(here,ldInst); - here=++instList.insert(here,addIn); - here=++instList.insert(here,stInst); +#endif + here = ++instList.insert(here,ldInst); + here = ++instList.insert(here,addIn); +#ifdef INSERT_STORE + here = ++instList.insert(here,stInst); +#endif break; } //count[inc]++ case 4:{ + + assert(inc>=0 && inc<=numPaths && "inc out of bound!"); + Instruction *ldInst=new LoadInst(countInst,vector<Value *> (1,ConstantUInt::get(Type::UIntTy, inc)), "ti1"); @@ -69,53 +154,76 @@ void getEdgeCode::getCode(Instruction *rInst, Instruction *addIn=BinaryOperator:: create(Instruction::Add, ldInst, val,"ti2"); + //insert trigger + getTriggerCode(M->getParent(), BB, MethNo, + ConstantSInt::get(Type::IntTy,inc), addIn); + here=instList.begin(); + //end trigger code + assert(inc>=0 && "IT MUST BE POSITIVE NOW"); +#ifdef INSERT_STORE Instruction *stInst=new StoreInst(addIn, countInst, vector<Value *> (1, ConstantUInt::get(Type::UIntTy,inc))); - - here=++instList.insert(here,ldInst); - here=++instList.insert(here,addIn); - here=++instList.insert(here,stInst); +#endif + here = ++instList.insert(here,ldInst); + here = ++instList.insert(here,addIn); +#ifdef INSERT_STORE + here = ++instList.insert(here,stInst); +#endif break; } //case: count[r+inc]++ case 5:{ + //ti1=inc+r Instruction *ldIndex=new LoadInst(rInst, "ti1"); Value *val=ConstantSInt::get(Type::IntTy,inc); Instruction *addIndex=BinaryOperator:: create(Instruction::Add, ldIndex, val,"ti2"); - + //erase following 1 line + //Value *valtemp=ConstantSInt::get(Type::IntTy,999); //now load count[addIndex] + Instruction *castInst=new CastInst(addIndex, Type::UIntTy,"ctin"); Instruction *ldInst=new LoadInst(countInst, vector<Value *>(1,castInst), "ti3"); Value *cons=ConstantSInt::get(Type::IntTy,1); - //count[addIndex]++ Instruction *addIn=BinaryOperator:: create(Instruction::Add, ldInst, cons,"ti4"); + + //insert trigger + getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn); + here=instList.begin(); + //end trigger code + +#ifdef INSERT_STORE + ///* Instruction *stInst=new StoreInst(addIn, countInst, vector<Value *>(1,castInst)); - - here=++instList.insert(here,ldIndex); - here=++instList.insert(here,addIndex); - here=++instList.insert(here,castInst); - here=++instList.insert(here,ldInst); - here=++instList.insert(here,addIn); - here=++instList.insert(here,stInst); + //*/ +#endif + here = ++instList.insert(here,ldIndex); + here = ++instList.insert(here,addIndex); + here = ++instList.insert(here,castInst); + here = ++instList.insert(here,ldInst); + here = ++instList.insert(here,addIn); +#ifdef INSERT_STORE + here = ++instList.insert(here,stInst); +#endif break; } //case: count[r]+ case 6:{ + //ti1=inc+r Instruction *ldIndex=new LoadInst(rInst, "ti1"); - + //now load count[addIndex] Instruction *castInst2=new CastInst(ldIndex, Type::UIntTy,"ctin"); @@ -126,27 +234,34 @@ void getEdgeCode::getCode(Instruction *rInst, //count[addIndex]++ Instruction *addIn=BinaryOperator:: create(Instruction::Add, ldInst, cons,"ti3"); + + //insert trigger + getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn); + here=instList.begin(); + //end trigger code +#ifdef INSERT_STORE Instruction *stInst=new StoreInst(addIn, countInst, vector<Value *>(1,castInst2)); - - here=++instList.insert(here,ldIndex); - here=++instList.insert(here,castInst2); - here=++instList.insert(here,ldInst); - here=++instList.insert(here,addIn); - here=++instList.insert(here,stInst); +#endif + here = ++instList.insert(here,ldIndex); + here = ++instList.insert(here,castInst2); + here = instList.insert(here,ldInst); + here = instList.insert(here,addIn); +#ifdef INSERT_STORE + here = instList.insert(here,stInst); +#endif break; } } //now check for cdIn and cdOut //first put cdOut - if(cdOut!=NULL){ - cdOut->getCode(rInst, countInst, M, BB); - } if(cdIn!=NULL){ - cdIn->getCode(rInst, countInst, M, BB); + cdIn->getCode(rInst, countInst, M, BB, numPaths, MethNo); + } + if(cdOut!=NULL){ + cdOut->getCode(rInst, countInst, M, BB, numPaths, MethNo); } - } @@ -179,6 +294,7 @@ void insertInTopBB(BasicBlock *front, here=++front->getInstList().insert(here,countVar); //Initialize Count[...] with 0 + for(int i=0;i<k; i++){ Instruction *stInstrC=new StoreInst(ConstantInt::get(Type::IntTy, 0), @@ -186,8 +302,8 @@ void insertInTopBB(BasicBlock *front, (1,ConstantUInt::get(Type::UIntTy, i))); here=++front->getInstList().insert(here,stInstrC); } - - here=++front->getInstList().insert(here,stInstr); + + here = ++front->getInstList().insert(here,stInstr); } @@ -196,21 +312,30 @@ void insertInTopBB(BasicBlock *front, void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, - Instruction *countInst){ - + Instruction *countInst, + int numPaths, int Methno){ + static int i=-1; + i++; BasicBlock* BB1=ed.getFirst()->getElement(); BasicBlock* BB2=ed.getSecond()->getElement(); - DEBUG(cerr << "Edges with codes ######################\n"; - cerr << BB1->getName() << "->" << BB2->getName() << "\n"; - cerr << "########################\n"); +#ifdef DEBUG_PATH_PROFILES + //debugging info + cerr<<"Edges with codes ######################\n"; + cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n"; + 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("counter", BB1->getParent()); + BasicBlock *newBB=new BasicBlock(ctr, BB1->getParent()); //get code for the new BB - edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB); + edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, numPaths, Methno); //Is terminator a branch instruction? //then we need to change branch destinations to include new BB @@ -223,32 +348,26 @@ void insertBB(Edge ed, newBB->getInstList().push_back(newBI2); } else{ - Value *cond=BI->getCondition(); - BasicBlock *fB, *tB; - - if (BI->getSuccessor(0) == BB2){ - tB=newBB; - fB=BI->getSuccessor(1); - } else { - fB=newBB; - tB=BI->getSuccessor(0); - } - - BB1->getInstList().pop_back(); - BB1->getInstList().push_back(new BranchInst(tB,fB,cond)); - newBB->getInstList().push_back(new BranchInst(BB2)); + if(BI->getSuccessor(0)==BB2) + BI->setSuccessor(0, newBB); + + if(BI->getSuccessor(1)==BB2) + BI->setSuccessor(1, newBB); + + Instruction *newBI2=new BranchInst(BB2); + newBB->getInstList().push_back(newBI2); } - //now iterate over BB2, and set its Phi nodes right + //get code for the new BB + //now iterate over BB2, and set its Phi nodes right for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end(); BB2Inst != BBend; ++BB2Inst){ if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){ - DEBUG(cerr<<"YYYYYYYYYYYYYYYYY\n"); - int bbIndex=phiInst->getBasicBlockIndex(BB1); - if(bbIndex>=0) - phiInst->setIncomingBlock(bbIndex, newBB); + assert(bbIndex>=0); + phiInst->setIncomingBlock(bbIndex, newBB); } } } + diff --git a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp index 0e7bce06b4..585aec0a14 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/Graph.cpp @@ -5,18 +5,18 @@ // //===----------------------------------------------------------------------===// -#include "Graph.h" +#include "llvm/Transforms/Instrumentation/Graph.h" #include "llvm/BasicBlock.h" #include <algorithm> #include <iostream> -using std::list; -using std::set; +//using std::list; +//using std::set; using std::map; using std::vector; using std::cerr; -static const graphListElement *findNodeInList(const Graph::nodeList &NL, +const graphListElement *findNodeInList(const Graph::nodeList &NL, Node *N) { for(Graph::nodeList::const_iterator NI = NL.begin(), NE=NL.end(); NI != NE; ++NI) @@ -25,7 +25,7 @@ static const graphListElement *findNodeInList(const Graph::nodeList &NL, return 0; } -static graphListElement *findNodeInList(Graph::nodeList &NL, Node *N) { +graphListElement *findNodeInList(Graph::nodeList &NL, Node *N) { for(Graph::nodeList::iterator NI = NL.begin(), NE=NL.end(); NI != NE; ++NI) if (*NI->element== *N) return &*NI; @@ -33,17 +33,19 @@ static graphListElement *findNodeInList(Graph::nodeList &NL, Node *N) { } //graph constructor with root and exit specified -Graph::Graph(std::set<Node*> n, std::set<Edge> e, +Graph::Graph(std::vector<Node*> n, std::vector<Edge> e, Node *rt, Node *lt){ strt=rt; ext=lt; - for(set<Node* >::iterator x=n.begin(), en=n.end(); x!=en; ++x) - nodes[*x] = list<graphListElement>(); + for(vector<Node* >::iterator x=n.begin(), en=n.end(); x!=en; ++x) + //nodes[*x] = list<graphListElement>(); + nodes[*x] = vector<graphListElement>(); - for(set<Edge >::iterator x=e.begin(), en=e.end(); x!=en; ++x){ + for(vector<Edge >::iterator x=e.begin(), en=e.end(); x!=en; ++x){ Edge ee=*x; int w=ee.getWeight(); - nodes[ee.getFirst()].push_front(graphListElement(ee.getSecond(),w)); + //nodes[ee.getFirst()].push_front(graphListElement(ee.getSecond(),w, ee.getRandId())); + nodes[ee.getFirst()].push_back(graphListElement(ee.getSecond(),w, ee.getRandId())); } } @@ -83,14 +85,14 @@ bool Graph::hasEdgeAndWt(Edge ed) const{ //add a node void Graph::addNode(Node *nd){ - list<Node *> lt=getAllNodes(); + vector<Node *> lt=getAllNodes(); - for(list<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE;++LI){ + for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE;++LI){ if(**LI==*nd) return; } - - nodes[nd] = list<graphListElement>(); + //chng + nodes[nd] =vector<graphListElement>(); //list<graphListElement>(); } //add an edge @@ -105,7 +107,10 @@ void Graph::addEdge(Edge ed, int w){ if(findNodeInList(nodes[ed.getFirst()], nd2)) return; - ndList.push_front(graphListElement(nd2,w)); + //ndList.push_front(graphListElement(nd2,w, ed.getRandId())); + ndList.push_back(graphListElement(nd2,w, ed.getRandId()));//chng + + //sort(ndList.begin(), ndList.end(), NodeListSort()); } //add an edge EVEN IF such an edge already exists @@ -113,8 +118,12 @@ void Graph::addEdge(Edge ed, int w){ //which does happen when we add dummy edges //to the graph, for compensating for back-edges void Graph::addEdgeForce(Edge ed){ - nodes[ed.getFirst()].push_front(graphListElement(ed.getSecond(), - ed.getWeight())); + //nodes[ed.getFirst()].push_front(graphListElement(ed.getSecond(), + //ed.getWeight(), ed.getRandId())); + nodes[ed.getFirst()].push_back + (graphListElement(ed.getSecond(), ed.getWeight(), ed.getRandId())); + + //sort(nodes[ed.getFirst()].begin(), nodes[ed.getFirst()].end(), NodeListSort()); } //remove an edge @@ -132,6 +141,21 @@ void Graph::removeEdge(Edge ed){ } } +//remove an edge with a given wt +//Note that it removes just one edge, +//the first edge that is encountered +void Graph::removeEdgeWithWt(Edge ed){ + nodeList &ndList = nodes[ed.getFirst()]; + Node &nd2 = *ed.getSecond(); + + for(nodeList::iterator NI=ndList.begin(), NE=ndList.end(); NI!=NE ;++NI) { + if(*NI->element == nd2 && NI->weight==ed.getWeight()) { + ndList.erase(NI); + break; + } + } +} + //set the weight of an edge void Graph::setWeight(Edge ed){ graphListElement *El = findNodeInList(nodes[ed.getFirst()], ed.getSecond()); @@ -142,21 +166,34 @@ void Graph::setWeight(Edge ed){ //get the list of successor nodes -list<Node *> Graph::getSuccNodes(Node *nd) const { +vector<Node *> Graph::getSuccNodes(Node *nd) const { nodeMapTy::const_iterator nli = nodes.find(nd); assert(nli != nodes.end() && "Node must be in nodes map"); const nodeList &nl = nli->second; - list<Node *> lt; + vector<Node *> lt; for(nodeList::const_iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI) lt.push_back(NI->element); return lt; } +//get the number of outgoing edges +int Graph::getNumberOfOutgoingEdges(Node *nd) const { + nodeMapTy::const_iterator nli = nodes.find(nd); + assert(nli != nodes.end() && "Node must be in nodes map"); + const nodeList &nl = nli->second; + + int count=0; + for(nodeList::const_iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI) + count++; + + return count; +} + //get the list of predecessor nodes -list<Node *> Graph::getPredNodes(Node *nd) const{ - list<Node *> lt; +vector<Node *> Graph::getPredNodes(Node *nd) const{ + vector<Node *> lt; for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){ Node *lnode=EI->first; const nodeList &nl = getNodeList(lnode); @@ -167,15 +204,37 @@ list<Node *> Graph::getPredNodes(Node *nd) const{ return lt; } +//get the number of predecessor nodes +int Graph::getNumberOfIncomingEdges(Node *nd) const{ + int count=0; + for(nodeMapTy::const_iterator EI=nodes.begin(), EE=nodes.end(); EI!=EE ;++EI){ + Node *lnode=EI->first; + const nodeList &nl = getNodeList(lnode); + for(Graph::nodeList::const_iterator NI = nl.begin(), NE=nl.end(); NI != NE; + ++NI) + if (*NI->element== *nd) + count++; + } + return count; +} + //get the list of all the vertices in graph -list<Node *> Graph::getAllNodes() const{ - list<Node *> lt; +vector<Node *> Graph::getAllNodes() const{ + vector<Node *> lt; for(nodeMapTy::const_iterator x=nodes.begin(), en=nodes.end(); x != en; ++x) lt.push_back(x->first); return lt; } +//get the list of all the vertices in graph +vector<Node *> Graph::getAllNodes(){ + vector<Node *> lt; + for(nodeMapTy::const_iterator x=nodes.begin(), en=nodes.end(); x != en; ++x) + lt.push_back(x->first); + + return lt; +} //class to compare two nodes in graph //based on their wt: this is used in @@ -198,7 +257,7 @@ Graph* Graph::getMaxSpanningTree(){ Graph *st=new Graph();//max spanning tree, undirected edges int inf=9999999;//largest key - list<Node *> lt = getAllNodes(); + vector<Node *> lt = getAllNodes(); //initially put all vertices in vector vt //assign wt(root)=0 @@ -221,7 +280,7 @@ Graph* Graph::getMaxSpanningTree(){ //initialize: wt(root)=0, wt(others)=infinity //parent(root)=NULL, parent(others) not defined (but not null) - for(list<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ + for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ Node *thisNode=*LI; if(*thisNode == *getRoot()){ thisNode->setWeight(0); @@ -295,9 +354,9 @@ Graph* Graph::getMaxSpanningTree(){ //print the graph (for debugging) void Graph::printGraph(){ - list<Node *> lt=getAllNodes(); + vector<Node *> lt=getAllNodes(); cerr<<"Graph---------------------\n"; - for(list<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ + for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ cerr<<((*LI)->getElement())->getName()<<"->"; Graph::nodeList nl=getNodeList(*LI); for(Graph::nodeList::iterator NI=nl.begin(), NE=nl.end(); NI!=NE; ++NI){ @@ -312,10 +371,10 @@ void Graph::printGraph(){ //get a list of nodes in the graph //in r-topological sorted order //note that we assumed graph to be connected -list<Node *> Graph::reverseTopologicalSort() const{ - list <Node *> toReturn; - list<Node *> lt=getAllNodes(); - for(list<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ +vector<Node *> Graph::reverseTopologicalSort() const{ + vector <Node *> toReturn; + vector<Node *> lt=getAllNodes(); + for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ if((*LI)->getWeight()!=GREY && (*LI)->getWeight()!=BLACK) DFS_Visit(*LI, toReturn); } @@ -325,10 +384,10 @@ list<Node *> Graph::reverseTopologicalSort() const{ //a private method for doing DFS traversal of graph //this is used in determining the reverse topological sort //of the graph -void Graph::DFS_Visit(Node *nd, list<Node *> &toReturn) const { +void Graph::DFS_Visit(Node *nd, vector<Node *> &toReturn) const { nd->setWeight(GREY); - list<Node *> lt=getSuccNodes(nd); - for(list<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ + vector<Node *> lt=getSuccNodes(nd); + for(vector<Node *>::iterator LI=lt.begin(), LE=lt.end(); LI!=LE; ++LI){ if((*LI)->getWeight()!=GREY && (*LI)->getWeight()!=BLACK) DFS_Visit(*LI, toReturn); } @@ -341,8 +400,8 @@ void Graph::DFS_Visit(Node *nd, list<Node *> &toReturn) const { //This is done by adding an edge //v->u for all existing edges u->v void Graph::makeUnDirectional(){ - list<Node* > allNodes=getAllNodes(); - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + vector<Node* > allNodes=getAllNodes(); + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI) { nodeList nl=getNodeList(*NI); for(nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE; ++NLI){ @@ -360,8 +419,8 @@ void Graph::makeUnDirectional(){ //this way, max-spanning tree could be obtained //usin min-spanning tree, and vice versa void Graph::reverseWts(){ - list<Node *> allNodes=getAllNodes(); - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + vector<Node *> allNodes=getAllNodes(); + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI) { nodeList node_list=getNodeList(*NI); for(nodeList::iterator NLI=nodes[*NI].begin(), NLE=nodes[*NI].end(); @@ -385,9 +444,9 @@ void Graph::reverseWts(){ void Graph::getBackEdges(vector<Edge > &be) const{ map<Node *, Color > color; map<Node *, int > d; - list<Node *> allNodes=getAllNodes(); + vector<Node *> allNodes=getAllNodes(); int time=0; - for(list<Node *>::const_iterator NI=allNodes.begin(), NE=allNodes.end(); + for(vector<Node *>::const_iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ if(color[*NI]!=GREY && color[*NI]!=BLACK) getBackEdgesVisit(*NI, be, color, d, time); @@ -402,20 +461,24 @@ void Graph::getBackEdgesVisit(Node *u, vector<Edge > &be, color[u]=GREY; time++; d[u]=time; - list<Node *> succ_list=getSuccNodes(u); - for(list<Node *>::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); + vector<graphListElement> succ_list=getNodeList(u); + for(vector<graphListElement>::const_iterator vl=succ_list.begin(), + ve=succ_list.end(); vl!=ve; ++vl){ + Node *v=vl->element; + // for(vector<Node *>::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); } //now checking for d and f vals - if(color[*v]==GREY){ + if(color[v]==GREY){ //so v is ancestor of u if time of u > time of v - if(d[u] >= d[*v]){ - Edge *ed=new Edge(u, *v); - if (!(*u == *getExit() && **v == *getRoot())) + if(d[u] >= d[v]){ + Edge *ed=new Edge(u, v,vl->weight, vl->randId); + if (!(*u == *getExit() && *v == *getRoot())) be.push_back(*ed); // choose the forward edges } } diff --git a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp index b2c5445c45..1eadcb6c6a 100644 --- a/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp +++ b/lib/Transforms/Instrumentation/ProfilePaths/GraphAuxiliary.cpp @@ -6,12 +6,15 @@ // //===----------------------------------------------------------------------===// -#include "Graph.h" +#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" +#include "llvm/Function.h" +#include "llvm/Pass.h" #include "llvm/BasicBlock.h" +#include "llvm/Transforms/Instrumentation/Graph.h" #include <algorithm> #include <iostream> -using std::list; +//using std::list; using std::map; using std::vector; using std::cerr; @@ -25,13 +28,13 @@ static bool edgesEqual(Edge ed1, Edge ed2){ static void getChords(vector<Edge > &chords, Graph &g, Graph st){ //make sure the spanning tree is directional //iterate over ALL the edges of the graph - list<Node *> allNodes=g.getAllNodes(); - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + vector<Node *> allNodes=g.getAllNodes(); + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ Graph::nodeList node_list=g.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!=NLE; ++NLI){ - Edge f(*NI, NLI->element,NLI->weight); + Edge f(*NI, NLI->element,NLI->weight, NLI->randId); if(!(st.hasEdgeAndWt(f)))//addnl chords.push_back(f); } @@ -46,8 +49,8 @@ static void getChords(vector<Edge > &chords, Graph &g, Graph st){ //the tree so that now, all edge directions in the tree match //the edge directions of corresponding edges in the directed graph static void removeTreeEdges(Graph &g, Graph& t){ - list<Node* > allNodes=t.getAllNodes(); - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + vector<Node* > allNodes=t.getAllNodes(); + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ Graph::nodeList nl=t.getNodeList(*NI); for(Graph::nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE;++NLI){ @@ -64,18 +67,40 @@ static void removeTreeEdges(Graph &g, Graph& t){ //add up the edge values, we get a path number that uniquely //refers to the path we travelled int valueAssignmentToEdges(Graph& g){ - list<Node *> revtop=g.reverseTopologicalSort(); + vector<Node *> revtop=g.reverseTopologicalSort(); + /* + std::cerr<<"-----------Reverse topological sort\n"; + for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end(); RI!=RE; ++RI){ + std::cerr<<(*RI)->getElement()->getName()<<":"; + } + std::cerr<<"\n----------------------"<<std::endl; + */ map<Node *,int > NumPaths; - for(list<Node *>::iterator RI=revtop.begin(), RE=revtop.end(); RI!=RE; ++RI){ + for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end(); RI!=RE; ++RI){ if(g.isLeaf(*RI)) NumPaths[*RI]=1; else{ NumPaths[*RI]=0; - list<Node *> succ=g.getSuccNodes(*RI); - for(list<Node *>::iterator SI=succ.begin(), SE=succ.end(); SI!=SE; ++SI){ - Edge ed(*RI,*SI,NumPaths[*RI]); - g.setWeight(ed); - NumPaths[*RI]+=NumPaths[*SI]; + ///// + Graph::nodeList &nlist=g.getNodeList(*RI); + //sort nodelist by increasing order of numpaths + + int sz=nlist.size(); + for(int i=0;i<sz-1; i++){ + int min=i; + for(int j=i+1; j<sz; j++) + if(NumPaths[nlist[j].element]<NumPaths[nlist[min].element]) min=j; + + graphListElement tempEl=nlist[min]; + nlist[min]=nlist[i]; + nlist[i]=tempEl; + } + //sorted now! + + for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end(); + GLI!=GLE; ++GLI){ + GLI->weight=NumPaths[*RI]; + NumPaths[*RI]+=NumPaths[GLI->element]; } } } @@ -108,19 +133,26 @@ static int inc_Dir(Edge e, Edge f){ return -1; } + //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<Edge, int>& Increment, +static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare>& Increment, int events, Node *v, Edge e){ - list<Node *> allNodes=t.getAllNodes(); - - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + vector<Node *> allNodes=t.getAllNodes(); + + + //cerr<<"Called for\n"; + //if(!e.isNull()) + //printEdge(e); + + + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ Graph::nodeList node_list=t.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!= NLE; ++NLI){ - Edge f(*NI, NLI->element,NLI->weight); + Edge f(*NI, NLI->element,NLI->weight, NLI->randId); if(!edgesEqual(f,e) && *v==*(f.getSecond())){ int dir_count=inc_Dir(e,f); int wt=1*f.getWeight(); @@ -129,15 +161,15 @@ static void inc_DFS(Graph& g,Graph& t,map<Edge, int>& Increment, } } - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ Graph::nodeList node_list=t.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!=NLE; ++NLI){ - Edge f(*NI, NLI->element,NLI->weight); + Edge f(*NI, NLI->element,NLI->weight, NLI->randId); if(!edgesEqual(f,e) && *v==*(f.getFirst())){ int dir_count=inc_Dir(e,f); - int wt=1*f.getWeight(); + int wt=f.getWeight(); inc_DFS(g,t, Increment, dir_count*events+wt, f.getSecond(), f); } @@ -145,16 +177,18 @@ static void inc_DFS(Graph& g,Graph& t,map<Edge, int>& Increment, } allNodes=g.getAllNodes(); - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ Graph::nodeList node_list=g.getNodeList(*NI); for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); NLI!=NLE; ++NLI){ - Edge f(*NI, NLI->element,NLI->weight); + Edge f(*NI, NLI->element,NLI->weight, NLI->randId); if(!(t.hasEdgeAndWt(f)) && (*v==*(f.getSecond()) || *v==*(f.getFirst()))){ int dir_count=inc_Dir(e,f); Increment[f]+=dir_count*events; + //cerr<<"assigned "<<Increment[f]<<" to"<<endl; + //printEdge(f); } } } @@ -164,19 +198,19 @@ static void inc_DFS(Graph& g,Graph& t,map<Edge, int>& 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<Edge, int> getEdgeIncrements(Graph& g, Graph& t){ +static map<Edge, int, EdgeCompare> getEdgeIncrements(Graph& g, Graph& t){ //get all edges in g-t - map<Edge, int> Increment; + map<Edge, int, EdgeCompare> Increment; - list<Node *> allNodes=g.getAllNodes(); + vector<Node *> allNodes=g.getAllNodes(); - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; ++NI){ Graph::nodeList node_list=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); - if(!(t.hasEdge(ed))){ + Edge ed(*NI, NLI->element,NLI->weight,NLI->randId); + if(!(t.hasEdgeAndWt(ed))){ Increment[ed]=0;; } } @@ -185,14 +219,13 @@ static map<Edge, int> getEdgeIncrements(Graph& g, Graph& t){ Edge *ed=new Edge(); inc_DFS(g,t,Increment, 0, g.getRoot(), *ed); - - for(list<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; + for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; |